-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathusb.c
More file actions
383 lines (347 loc) · 9.37 KB
/
Copy pathusb.c
File metadata and controls
383 lines (347 loc) · 9.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
#define _POSIX_C_SOURCE 201501L
#include <unistd.h>
#include <libusb.h>
#include <stdio.h>
#include <inttypes.h>
#include <stdlib.h>
#include <string.h>
#include "usb.h"
/*
extern const char *usage;
extern int min_argc, max_argc;
int run_usb(libusb_context *ctx, libusb_device_handle *hdev, int argc, char **argv);
*/
libusb_device ** usb_common_get_device_list(libusb_context *ctx)
{
libusb_device **devs = NULL;
ssize_t ndevs = libusb_get_device_list(ctx, &devs);
if (ndevs < 0) {
fprintf(stderr, "error enumerating USB devices: %s\n",
libusb_error_name(ndevs));
} else if (!ndevs) {
fprintf(stderr, "error: no USB devices found\n");
}
return devs;
}
static int usb_desc_eq_vid_pid(
const struct libusb_device_descriptor *desc,
const addr_t addr
) {
return desc->idVendor == addr[0] && desc->idProduct == addr[1];
}
libusb_device_handle * usb_common_find_device(
libusb_context *ctx, struct dev_spec *spec,
const struct dev_type *dev_types, unsigned n_dev_types
) {
struct libusb_device_descriptor desc;
libusb_device_handle *hdev = NULL;
libusb_device **j, **devs, *dev = NULL;
addr_t addr;
const addr_t *a;
unsigned i;
int r;
devs = usb_common_get_device_list(ctx);
if (!devs)
return NULL;
for (j = devs; *j; j++) {
addr[0] = libusb_get_bus_number(*j);
addr[1] = libusb_get_device_address(*j);
if (spec->have_bus_addr) {
if (memcmp(addr, spec->bus_addr, sizeof(addr_t)))
continue;
} else {
r = libusb_get_device_descriptor(*j, &desc);
if (r) {
fprintf(stderr,
"warning: unable to access descriptor "
"of device on %hu.%hu: %s\n",
addr[0], addr[1], libusb_error_name(r));
goto out;
}
if (spec->have_vid_pid) {
if (usb_desc_eq_vid_pid(&desc, spec->vid_pid))
goto found;
} else if (spec->dev_type) {
a = &spec->dev_type->addr;
if (usb_desc_eq_vid_pid(&desc, *a))
goto found;
} else {
for (i=0; i<n_dev_types; i++) {
a = &dev_types[i].addr;
if (usb_desc_eq_vid_pid(&desc, *a))
goto found;
}
}
/* nothing matched */
continue;
}
found:
/* take this one */
if (!dev) {
dev = *j;
continue;
}
/* not unique */
fprintf(stderr,
"ambigious device specifier: "
"both %hhu.%hhu and %hu.%hu match\n",
libusb_get_bus_number(dev),
libusb_get_device_address(dev),
addr[0], addr[1]);
goto out;
}
if (!dev) {
fprintf(stderr, "no matching USB device found\n");
} else {
r = libusb_get_device_descriptor(dev, &desc);
for (i=0; i<n_dev_types; i++)
if (usb_desc_eq_vid_pid(&desc, dev_types[i].addr)) {
spec->dev_type = dev_types + i;
break;
}
fprintf(stderr, "using %s device %04hx:%04hx on bus.addr %hhu.%hhu\n",
spec->dev_type ? spec->dev_type->name : "unknown",
desc.idVendor, desc.idProduct,
libusb_get_bus_number(dev),
libusb_get_device_address(dev));
r = libusb_open(dev, &hdev);
if (r) {
fprintf(stderr, "error opening device: %s\n",
libusb_error_name(r));
}
}
out:
libusb_free_device_list(devs, 1);
return hdev;
}
static int parse_dev_spec(struct dev_spec *spec, const char *dev_addr)
{
addr_t *addr;
const char *delim = dev_addr + strcspn(dev_addr, ".:");
const char *fmt = NULL;
int k;
switch (*delim) {
case '.':
fmt = "%hu.%hu";
addr = &spec->bus_addr;
spec->have_bus_addr = 1;
break;
case ':':
fmt = "%04hx:%04hx";
addr = &spec->vid_pid;
spec->have_vid_pid = 1;
break;
}
if (!fmt || sscanf(dev_addr, fmt, *addr + 0, *addr + 1) != 2) {
fprintf(stderr,"invalid device address: '%s'\n",dev_addr);
return 1;
}
return 0;
}
static const char *usb_common_dev_spec_help = "\
-c <bus>.<addr> N: bus #, M: device # (see /sys/bus/usb/devices/N-*/devnum)\n\
<vid>:<pid> address USB device via a Vendor / Product ID pair\n\
both formats override " ENV_DEV_ADDR "= in environment\n\
";
static const char *usb_common_dev_type_help = "\
-t <dev-type> use Vendor / Product ID pair identified by shortcut <dev-type>\n\
";
static const char *usb_common_iface_help = "\
-%c <iface> use interface number <iface> (default: %d, -1 to disable)\n\
";
static const char *usb_common_iface_alt_help = "\
-%c <alt-iface> set alt-setting on interface (default: %d, -1 to disable)\n\
";
char * usb_common_usage(const struct usb_common *uc)
{
char buf[256];
unsigned n = 0;
n += snprintf(buf+n,sizeof(buf)-n, "[-c {<bus>.<addr> | <vid>:<pid>}]");
if (uc->n_dev_types)
n += snprintf(buf+n, sizeof(buf)-n, " [-t <dev-type>]");
if (uc->iface > -2) {
n += snprintf(buf+n, sizeof(buf)-n, " [-%c <iface>]",
uc->iface_opt);
if (uc->alt > -2)
n += snprintf(buf+n,sizeof(buf)-n, " [-%c <alt-iface>]",
uc->alt_opt);
}
return strndup(buf, n);
}
char * usb_common_help(const struct usb_common *uc)
{
unsigned n = snprintf(NULL, 0, "%s", usb_common_dev_spec_help);
if (uc->n_dev_types) {
n += snprintf(NULL, 0, "%s\
supported:", usb_common_dev_type_help);
for (unsigned i=0; i<uc->n_dev_types; i++)
n += 1 + strlen(uc->dev_types[i].name) + 1;
}
if (uc->iface >= -1) {
n += snprintf(NULL, 0, usb_common_iface_help,
uc->iface_opt, uc->iface);
if (uc->alt >= -1)
n += snprintf(NULL,0,usb_common_iface_alt_help,
uc->alt_opt, uc->alt);
}
char *s = malloc(n + 1);
unsigned at = snprintf(s, n+1, "%s", usb_common_dev_spec_help);
if (uc->n_dev_types) {
at += snprintf(s+at, n+1-at, "%s\
supported:", usb_common_dev_type_help);
for (unsigned i=0; i<uc->n_dev_types; i++)
at += snprintf(s+at, n+1-at, " %s%c",
uc->dev_types[i].name,
i+1 < uc->n_dev_types ? ',' : '\n');
}
if (uc->iface >= -1) {
at += snprintf(s+at, n+1-at, usb_common_iface_help,
uc->iface_opt, uc->iface);
if (uc->alt >= -1)
at += snprintf(s+at, n+1-at, usb_common_iface_alt_help,
uc->alt_opt, uc->alt);
}
return s;
}
static long opt_parse_long(int argc, char **argv, int default_val, char c)
{
long v = default_val;
int opt;
char *end;
char shortopts[] = ":x:";
shortopts[1] = c;
while ((opt = getopt(argc, argv, shortopts)) != -1)
if (opt == c) {
v = strtol(optarg, &end, 0);
if (*end)
FATAL(1,
"invalid argument for option '-%c': '%s'\n",
c,optarg);
} else if (opt == ':') {
FATAL(1,"argument expected for option '-%c'\n",optopt);
} else if (opt == '?') {
optind--;
break;
}
return v;
}
int usb_common_parse_opts(struct usb_common *uc, int argc, char **argv)
{
const char *dev_addr = getenv(ENV_DEV_ADDR);
const char *dev_type = NULL;
char *end;
int opt, r;
int iface = 0;
int alt_iface = -1;
while ((opt = getopt(argc, argv, ":c:t:")) != -1)
switch (opt) {
case 'c': dev_addr = optarg; break;
case 't': dev_type = optarg; break;
case ':': FATAL(1,"argument expected for option '-%c'\n",optopt);
case '?': optind--; goto done_opt_parsing;
}
done_opt_parsing:
if (dev_addr && (r = parse_dev_spec(&uc->spec, dev_addr)))
return r;
if (dev_type) {
const struct dev_type *t = NULL, *tt;
for (unsigned i=0; i<uc->n_dev_types; i++) {
tt = uc->dev_types + i;
if (strstr(tt->name, dev_type) != tt->name)
continue;
if (t)
FATAL(1,"ambiguous <dev-type> '%s'\n",dev_type);
t = tt;
}
if (!t)
FATAL(1,"unknown <dev-type> '%s'\n",dev_type);
uc->spec.dev_type = t;
}
if (uc->iface > -2)
uc->iface = opt_parse_long(argc, argv, uc->iface, uc->iface_opt);
if (uc->alt > -2)
uc->alt = opt_parse_long(argc, argv, uc->alt, uc->alt_opt);
return 0;
}
int usb_common_setup(struct usb_common *uc)
{
int r = 0;
/* init libusb */
r = libusb_init(&uc->ctx);
if (r) {
fprintf(stderr, "error initializing libusb: %s\n",
libusb_error_name(r));
r = 1;
goto err;
}
/* find specified USB device */
uc->hdev = usb_common_find_device(uc->ctx, &uc->spec,
uc->dev_types, uc->n_dev_types);
if (!uc->hdev) {
r = 2;
goto err;
}
if (uc->iface > -1) {
if ((r = libusb_claim_interface(uc->hdev, uc->iface))) {
fprintf(stderr, "error claiming interface %d: %s\n",
uc->iface, libusb_error_name(r));
uc->iface = -1;
r = 3;
goto err;
}
if (uc->alt > -1 &&
(r = libusb_set_interface_alt_setting(uc->hdev, uc->iface,
uc->alt))) {
fprintf(stderr,
"error setting alt-setting %d on interface %d: "
"%s\n", uc->alt, uc->iface,
libusb_error_name(r));
r = 4;
goto err;
}
}
return 0;
err:
usb_common_teardown(uc);
return r;
}
void usb_common_teardown(struct usb_common *uc)
{
if (uc->hdev && uc->iface > -1)
libusb_release_interface(uc->hdev, uc->iface);
if (uc->hdev)
libusb_close(uc->hdev);
if (uc->ctx)
libusb_exit(uc->ctx);
}
/*
int main(int argc, char **argv)
{
struct dev_spec spec;
libusb_context *ctx = NULL;
libusb_device **devs = NULL, *dev = NULL;
libusb_device_handle *hdev = NULL;
int r = 0;
memset(&spec, 0, sizeof(spec));
progname = argv[0];
done_opt_parsing:
if (argc - optind < min_argc || argc - optind > max_argc)
USAGE(1);
r = libusb_init(&ctx);
if (r)
FATAL(1,"error initializing libusb: %s\n", libusb_error_name(r));
hdev = usb_find_device(ctx, &spec);
if (!hdev)
goto out;
r = run_usb(ctx, hdev, argc - optind, argv + optind);
if (r == 1)
fprintf(stderr, "usage: %s <bus> <addr> %s\n", argv[0], usage);
else if (r)
fprintf(stderr, "run_usb failed with %d\n", r);
out:
if (hdev)
libusb_close(hdev);
if (ctx)
libusb_exit(ctx);
return r;
}*/