forked from gregkh/usbutils
/
usbmisc.c
546 lines (467 loc) · 14.6 KB
/
usbmisc.c
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
/*****************************************************************************/
/*
* usbmisc.c -- Misc USB routines
*
* Copyright (C) 2003 Aurelien Jarno (aurelien@aurel32.net)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*
*/
/*****************************************************************************/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <limits.h>
#include <fcntl.h>
#ifdef HAVE_ICONV
#include <iconv.h>
#endif
#ifdef HAVE_NL_LANGINFO
#include <langinfo.h>
#endif
#include "usbmisc.h"
#ifdef OS_LINUX
/* ---------------------------------------------------------------------- */
static const char *devbususb = "/dev/bus/usb";
/* ---------------------------------------------------------------------- */
static int readlink_recursive(const char *path, char *buf, size_t bufsize)
{
char temp[PATH_MAX + 1];
char *ptemp;
int ret;
ret = readlink(path, buf, bufsize-1);
if (ret > 0) {
buf[ret] = 0;
if (*buf != '/') {
strncpy(temp, path, sizeof(temp));
ptemp = temp + strlen(temp);
while (*ptemp != '/' && ptemp != temp)
ptemp--;
ptemp++;
strncpy(ptemp, buf, bufsize + temp - ptemp);
} else
strncpy(temp, buf, sizeof(temp));
return readlink_recursive(temp, buf, bufsize);
} else {
strncpy(buf, path, bufsize);
return strlen(buf);
}
}
static char *get_absolute_path(const char *path, char *result,
size_t result_size)
{
const char *ppath; /* pointer on the input string */
char *presult; /* pointer on the output string */
ppath = path;
presult = result;
result[0] = 0;
if (path == NULL)
return result;
if (*ppath != '/') {
result = getcwd(result, result_size);
presult += strlen(result);
result_size -= strlen(result);
*presult++ = '/';
result_size--;
}
while (*ppath != 0 && result_size > 1) {
if (*ppath == '/') {
do
ppath++;
while (*ppath == '/');
*presult++ = '/';
result_size--;
} else if (*ppath == '.' && *(ppath + 1) == '.' &&
*(ppath + 2) == '/' && *(presult - 1) == '/') {
if ((presult - 1) != result) {
/* go one directory upper */
do {
presult--;
result_size++;
} while (*(presult - 1) != '/');
}
ppath += 3;
} else if (*ppath == '.' &&
*(ppath + 1) == '/' &&
*(presult - 1) == '/') {
ppath += 2;
} else {
*presult++ = *ppath++;
result_size--;
}
}
/* Don't forget to mark the end of the string! */
*presult = 0;
return result;
}
int linux_get_usb_device(libusb_device *dev, libusb_context *ctx, const char *path)
{
libusb_device **list;
ssize_t num_devs, i;
char device_path[PATH_MAX + 1];
char absolute_path[PATH_MAX + 1];
readlink_recursive(path, device_path, sizeof(device_path));
get_absolute_path(device_path, absolute_path, sizeof(absolute_path));
dev = NULL;
num_devs = libusb_get_device_list(ctx, &list);
for (i = 0; i < num_devs; ++i) {
uint8_t bnum = libusb_get_bus_number(list[i]);
uint8_t dnum = libusb_get_device_address(list[i]);
snprintf(device_path, sizeof(device_path), "%s/%03u/%03u",
devbususb, bnum, dnum);
if (!strcmp(device_path, absolute_path)) {
dev = list[i];
break;
}
}
libusb_free_device_list(list, 0);
return LIBUSB_SUCCESS;
}
int linux_get_device_info_path(char *path, size_t size, unsigned int location_id)
{
unsigned int mask;
unsigned int busnum;
int fd;
ssize_t r;
int i = 0;
if (size < strlen(SBUD))
return 0;
snprintf(path, size, "%s", SBUD);
busnum = location_id >> 24;
mask = 0x00ffffff;
location_id &= mask;
if ( location_id == 0 )
{
/* root hub is special case */
snprintf(path + strlen(path), size - strlen(path), "%s", "usb");
}
snprintf(path + strlen(path), size - strlen(path), "%d", busnum);
for (i=0; i<6; i++) {
int next = location_id >> (20 - i*4);
if (!next)
break;
snprintf(path + strlen(path), size - strlen(path), "%s%d", ((i>0) ? "." : "-" ), next);
mask >>= 4;
location_id &= mask;
}
return LIBUSB_SUCCESS;
}
#endif
/*
This call is to get a cached descriptor string, and thus is OS-dependent.
It is not for use in two special cases that are not in the descriptors:
libusb_get_device_speed
libusb_get_kernel_driver_name
*/
#ifdef OS_LINUX
int get_string_from_cache(char *buf, size_t size, libusb_device *dev, unsigned int referrer)
{
/* referrer is something like LIBUSB_DEVICE_BCDUSB */
/* return is length of string in buf */
const char *cacheID;
int fd, r;
unsigned int location_id;
char path[MY_PATH_MAX];
if (size < 1)
return LIBUSB_SUCCESS;
*buf = 0;
path[0] = '\0';
location_id = get_location_id(dev);
linux_get_device_info_path(path, sizeof(path), location_id);
snprintf(path + strlen(path), size - strlen(path), "%s", "/");
switch (referrer) {
case LIBUSB_HUB_N_NBRPORTS:
cacheID = "/maxchild"; break;
case LIBUSB_DEVICE_BCD_DEVICE:
cacheID = "/bcdDevice"; break;
case LIBUSB_CONFIG_B_CONFIGURATIONVALUE:
cacheID = "/bConfigurationValue"; break;
case LIBUSB_DEVICE_B_DEVICECLASS:
cacheID = "/bDeviceClass"; break;
case LIBUSB_DEVICE_B_DEVICEPROTOCOL:
cacheID = "/bDeviceProtocol"; break;
case LIBUSB_DEVICE_B_DEVICESUBCLASS:
cacheID = "/bDeviceSubClass"; break;
case LIBUSB_CONFIG_BM_ATTRIBUTES:
cacheID = "/bmAttributes"; break;
case LIBUSB_DEVICE_B_MAXPACKETSIZE0:
cacheID = "/bMaxPacketSize0"; break;
case LIBUSB_CONFIG_B_MAXPOWER:
cacheID = "/bMaxPower"; break;
case LIBUSB_DEVICE_B_NUMCONFIGURATIONS:
cacheID = "/bNumConfigurations"; break;
case LIBUSB_CONFIG_B_NUMINTERFACES:
cacheID = "/bNumInterfaces"; break;
case LIBUSB_CONFIG_S_CONFIGURATION:
cacheID = "/configuration"; break;
case LIBUSB_DEVICE_ID_PRODUCT:
cacheID = "/idProduct"; break;
case LIBUSB_DEVICE_ID_VENDOR:
cacheID = "/idVendor"; break;
case LIBUSB_DEVICE_BCD_USB:
cacheID = "/version"; break;
case LIBUSB_DEVICE_S_MANUFACTURER:
cacheID = "/manufacturer"; break;
case LIBUSB_DEVICE_S_PRODUCT:
cacheID = "/product"; break;
case LIBUSB_DEVICE_S_SERIALNUMBER:
cacheID = "/serial"; break;
case LIBUSB_ENDPOINT_B_ENDPOINTADDRESS:
cacheID = "/ep00/bEndpointAddress"; break;
case LIBUSB_ENDPOINT_B_INTERVAL:
cacheID = "/ep00/bInterval"; break;
case LIBUSB_ENDPOINT_B_LENGTH:
cacheID = "/ep00/bLength"; break;
case LIBUSB_ENDPOINT_BM_ATTRIBUTES:
cacheID = "/ep00/bmAttributes"; break;
case LIBUSB_ENDPOINT_W_MAXPACKETSIZE:
cacheID = "/ep00/wMaxPacketSize"; break; /* hex */
}
strncat(path, cacheID, sizeof(path) - strlen(path) - 1);
fd = open(path, O_RDONLY);
if (fd < 0)
return 0;
r = read(fd, buf, size - 1);
close(fd);
if (r < 0)
return 0;
/* buf has \n appended */
if (r < 2)
return 0;
buf[r - 1] = '\0';
return strlen(buf);
}
#endif
static void get_dev_string_ascii(char *buf, size_t size, libusb_device_handle *dev,
u_int8_t id)
{
int ret = libusb_get_string_descriptor_ascii(dev, id,
(unsigned char *) buf,
size);
if (ret < 0) {
snprintf(buf, size, "%s", "(error)");
}
}
#if defined(HAVE_NL_LANGINFO) && defined(HAVE_ICONV)
static u_int16_t get_any_langid(libusb_device_handle *dev)
{
unsigned char buf[4];
int ret = libusb_get_string_descriptor(dev, 0, 0, buf, sizeof buf);
if (ret != sizeof buf) return 0;
return buf[2] | (buf[3] << 8);
}
static char *usb_string_to_native(char * str, size_t len)
{
size_t num_converted;
iconv_t conv;
char *result, *result_end;
size_t in_bytes_left, out_bytes_left;
conv = iconv_open(nl_langinfo(CODESET), "UTF-16LE");
if (conv == (iconv_t) -1)
return NULL;
in_bytes_left = len * 2;
out_bytes_left = len * MB_CUR_MAX;
result = result_end = malloc(out_bytes_left + 1);
num_converted = iconv(conv, &str, &in_bytes_left,
&result_end, &out_bytes_left);
iconv_close(conv);
if (num_converted == (size_t) -1) {
free(result);
return NULL;
}
*result_end = 0;
return result;
}
#endif
int get_dev_string(char *buf, size_t size, libusb_device_handle *hdev, u_int8_t id)
{
#if defined(HAVE_NL_LANGINFO) && defined(HAVE_ICONV)
int ret;
unsigned char unicode_buf[254];
u_int16_t langid;
#endif
if (!hdev || !id) {
return 0;
}
#if defined(HAVE_NL_LANGINFO) && defined(HAVE_ICONV)
langid = get_any_langid(hdev);
if (!langid) {
return snprintf(buf, size, "%s", "(error)");
}
ret = libusb_get_string_descriptor(hdev, id, langid,
(unsigned char *) unicode_buf,
sizeof unicode_buf);
if (ret < 2) {
return snprintf(buf, size, "%s", "(error)");
}
char *tmp = usb_string_to_native(unicode_buf + 2,
((unsigned char) unicode_buf[0] - 2) / 2);
if (tmp) {
snprintf(buf, size, "%s", tmp);
free(tmp);
} else {
get_dev_string_ascii(buf, size, hdev, id);
}
return strlen(buf);
#else
get_dev_string_ascii(buf, size, hdev, id);
return strlen(buf);
#endif
}
unsigned int get_location_id(libusb_device *dev)
{
uint8_t port_numbers[7];
int j;
int count = libusb_get_port_numbers(dev,port_numbers, 7);
unsigned int location_id = 0;
for (j = 0; j < count; j++) {
location_id |= (port_numbers[j] & 0xf) << (20 - 4*j);
}
location_id |= (libusb_get_bus_number(dev) << 24);
return location_id;
}
#ifdef OS_DARWIN
SInt32 GetSInt32CFProperty(io_service_t obj, CFStringRef key)
{
SInt32 value = 0;
CFNumberRef valueRef = IORegistryEntryCreateCFProperty(obj, key, kCFAllocatorDefault, 0);
if (valueRef) {
CFNumberGetValue(valueRef, kCFNumberSInt32Type, &value);
CFRelease(valueRef);
}
return value;
}
UInt32 GetLocationID(io_service_t obj);
UInt32 GetLocationID(io_service_t obj) {
return (UInt32) GetSInt32CFProperty(obj, CFSTR(kUSBDevicePropertyLocationID));
}
IOReturn darwin_get_service_from_location_id ( unsigned int location_id, io_service_t *service )
{
io_iterator_t deviceIterator;
unsigned int found_location_id;
CFMutableDictionaryRef matchingDict = IOServiceMatching(kIOUSBDeviceClassName);
if (!matchingDict)
return kIOReturnError;
IOServiceGetMatchingServices(kIOMasterPortDefault, matchingDict, &deviceIterator);
while ((*service = IOIteratorNext (deviceIterator))) {
/* get the location from the i/o registry */
found_location_id = GetLocationID(*service);
if (location_id == found_location_id) {
found_location_id = GetLocationID(*service);
IOObjectRelease(deviceIterator);
return kIOReturnSuccess;
}
}
/* not found, shouldn't happen */
IOObjectRelease(deviceIterator);
return kIOReturnError;
}
int get_string_from_cache(char *buf, size_t size, libusb_device *dev, unsigned int referrer)
{
CFStringRef property;
unsigned int location_id;
io_service_t service;
unsigned int typeID; /* either the base of the number, or 0 for string */
unsigned int child; /* if it's a child property, indicate here */
if (size < 1)
return LIBUSB_SUCCESS;
*buf = 0;
child = 0;
typeID = 0;
switch (referrer) {
case LIBUSB_DEVICE_B_MAXPACKETSIZE0:
typeID = 16; property = CFSTR("bMaxPacketSize0"); break;
case LIBUSB_DEVICE_ID_PRODUCT:
typeID = 16; property = CFSTR("idProduct"); break;
case LIBUSB_DEVICE_ID_VENDOR:
typeID = 16; property = CFSTR("idVendor"); break;
case LIBUSB_DEVICE_BCD_USB:
typeID = 16; property = CFSTR("bcdUSB"); break;
case LIBUSB_DEVICE_S_MANUFACTURER:
typeID = 0; property = CFSTR("USB Vendor Name"); break;
case LIBUSB_DEVICE_S_PRODUCT:
typeID = 0; property = CFSTR("USB Product Name"); break;
case LIBUSB_DEVICE_S_SERIALNUMBER:
typeID = 0; property = CFSTR("USB Serial Number"); break;
case LIBUSB_DEVICE_BCD_DEVICE:
typeID = 16; property = CFSTR("bcdDevice"); break;
case LIBUSB_CONFIG_B_CONFIGURATIONVALUE:
child = 1; typeID = 16; property = CFSTR("bConfigurationValue"); break;
case LIBUSB_INTERFACE_B_INTERFACENUMBER:
child = 1; typeID = 16; property = CFSTR("bInterfaceNumber"); break;
case LIBUSB_INTERFACE_B_ALTERNATESETTING:
child = 1; typeID = 16; property = CFSTR("bAlternateSetting"); break;
case LIBUSB_INTERFACE_B_NUMENDPOINTS:
child = 1; typeID = 16; property = CFSTR("bNumEndpoints"); break;
case LIBUSB_INTERFACE_B_INTERFACECLASS:
child = 1; typeID = 16; property = CFSTR("bInterfaceClass"); break;
case LIBUSB_INTERFACE_B_INTERFACESUBCLASS:
child = 1; typeID = 16; property = CFSTR("bInterfaceSubClass"); break;
case LIBUSB_INTERFACE_B_INTERFACEPROTOCOL:
child = 1; typeID = 16; property = CFSTR("bInterfaceProtocol"); break;
case LIBUSB_DEVICE_B_DEVICECLASS:
typeID = 16; property = CFSTR("bDeviceClass"); break;
case LIBUSB_DEVICE_B_DEVICEPROTOCOL:
typeID = 16; property = CFSTR("bDeviceProtocol"); break;
case LIBUSB_DEVICE_B_DEVICESUBCLASS:
typeID = 16; property = CFSTR("bDeviceSubClass"); break;
case LIBUSB_CONFIG_BM_ATTRIBUTES:
typeID = 16; property = CFSTR("bmAttributes"); break;
case LIBUSB_CONFIG_B_MAXPOWER:
typeID = 16; property = CFSTR("Requested Power"); break;
case LIBUSB_DEVICE_B_NUMCONFIGURATIONS:
typeID = 16; property = CFSTR("bNumConfigurations"); break;
case LIBUSB_CONFIG_B_NUMINTERFACES:
typeID = 16; property = CFSTR("bNumInterfaces"); break;
case LIBUSB_CONFIG_S_CONFIGURATION:
typeID = 0; property = CFSTR("Configuration"); break;
case LIBUSB_HUB_N_NBRPORTS:
typeID = 16; property = CFSTR("Ports"); break;
case LIBUSB_HUB_B_HUBCONTRCURRENT:
typeID = 16; property = CFSTR("Bus Power Available"); break;
}
location_id = get_location_id(dev);
darwin_get_service_from_location_id (location_id, &service);
if (typeID == 0) {
darwin_get_ioreg_string(buf, size, service, property);
} else {
if (typeID == 10) {
snprintf(buf, size, "%d", (UInt32) GetSInt32CFProperty(service, property));
} else if (typeID == 16) {
snprintf(buf, size,"%x", (UInt32) GetSInt32CFProperty(service, property));
}
}
return LIBUSB_SUCCESS;
}
int darwin_get_ioreg_string(char *buff, size_t len, io_service_t service, CFStringRef property) {
CFIndex length;
CFIndex maxSize;
if (len < 1)
return 0;
buff[0] = '\0';
CFStringRef cfBuff = (CFStringRef)IORegistryEntryCreateCFProperty (service, property, kCFAllocatorDefault, 0);
if (cfBuff) {
length = CFStringGetLength(cfBuff);
maxSize = CFStringGetMaximumSizeForEncoding(length, kCFStringEncodingUTF8);
char *utf_buff = malloc(maxSize);
CFStringGetCString(cfBuff, utf_buff, maxSize, kCFStringEncodingUTF8);
CFRelease(cfBuff);
strncpy(buff, utf_buff, len);
free(utf_buff);
}
return strlen(buff);
}
#endif /* OS_DARWIN */