forked from gregkh/usbutils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlsusb-t.c
367 lines (332 loc) · 9.65 KB
/
lsusb-t.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
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <dirent.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <stddef.h>
#include <libusb.h>
#include "config.h"
#include "list.h"
#include "lsusb.h"
#include "names.h"
#include "usbmisc.h"
#define MY_STRING_MAX 256
#define MY_PATH_MAX 4096
#define MY_PARAM_MAX 64
#ifdef OS_LINUX
#define SBUD "/sys/bus/usb/devices/"
#endif
static void sort_dev_strings(char *dev_strings[], int cnt )
{
if ( cnt < 2 )
return;
int sorted = 0;
int i = 0;
while (!(sorted)) {
sorted = 1;
for (i=0; i<cnt-1; i++) {
if ( strtoul(dev_strings[i],NULL,16) > strtoul(dev_strings[i+1],NULL,16) ) {
char tmp[MY_PATH_MAX];
sorted = 0;
strncpy(tmp,dev_strings[i],MY_PATH_MAX-1);
strncpy(dev_strings[i],dev_strings[i+1],MY_PATH_MAX-1);
strncpy(dev_strings[i+1],tmp,MY_PATH_MAX-1);
break;
}
}
}
}
static unsigned int get_maxchild(unsigned int location_id)
{
/* nNbrPorts in Hub Descriptor */
unsigned int maxchild;
#ifdef OS_DARWIN
io_service_t service;
darwin_get_service_from_location_id(location_id, &service);
maxchild = (UInt32) GetSInt32CFProperty(service, CFSTR("Ports"));
return maxchild;
#endif
#ifdef OS_LINUX
/* build path */
unsigned int mask;
unsigned int busnum;
char buf[MY_PARAM_MAX];
char path[MY_PATH_MAX];
int fd, i;
ssize_t r;
strcpy(path, SBUD);
busnum = location_id >> 24;
mask = 0x00ffffff;
location_id &= mask;
if ( location_id == 0 )
{
/* root hub is special case */
snprintf(path + strlen(path), sizeof(path) - strlen(path), "%s", "usb");
}
snprintf(path + strlen(path), sizeof(path) - strlen(path), "%d", busnum);
for (i=0; i<6; i++) {
int next = location_id >> (20 - i*4);
if (!next)
break;
snprintf(path + strlen(path), sizeof(path) - strlen(path), "%s%d", ((i>0) ? "." : "-" ), next);
mask >>= 4;
location_id &= mask;
}
snprintf(path + strlen(path), sizeof(path) - strlen(path), "%s", "/maxchild");
fd = open(path, O_RDONLY);
if (fd < 0) {
perror(path);
return 0;
}
r = read(fd, buf, sizeof(buf) - 1);
close(fd);
if (r < 0) {
perror(path);
return 0;
}
buf[sizeof(buf) - 1] = '\0';
maxchild = (unsigned int)strtoul(buf, NULL, 10);
return maxchild;
#endif
}
static void get_driver(char **driver, unsigned int location_id, int ifnum)
{
#ifdef OS_DARWIN
io_service_t service;
io_registry_entry_t child;
kern_return_t kr;
CFIndex length;
CFIndex maxSize;
CFStringRef cfBuff;
strcpy(*driver, "Unknown");
kr = darwin_get_service_from_location_id(location_id, &service);
if (kr) {
fprintf(stderr, "Failed to find device 0x%08x\n",location_id);
return;
}
kr = IORegistryEntryGetChildEntry(service, kIOServicePlane, &child);
if (kr) {
fprintf(stderr, "Failed to find driver\n");
return;
}
cfBuff = (CFStringRef)IORegistryEntryCreateCFProperty (child, CFSTR("IOClass"), 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(*driver, utf_buff, MY_STRING_MAX);
free(utf_buff);
}
#endif
#ifdef OS_LINUX
/* build path */
unsigned int mask;
unsigned int busnum;
int l;
char path[PATH_MAX];
char newpath[PATH_MAX];
int i;
strcpy(path, SBUD);
strcpy(*driver, "Unknown");
busnum = location_id >> 24;
mask = 0x00ffffff;
location_id &= mask;
if ( location_id == 0 ) {
/* root hub is special case */
snprintf(path + strlen(path), sizeof(path) - strlen(path), "usb%d", busnum);
if (strlen(path) < PATH_MAX) {
l = readlink(path, newpath, PATH_MAX);
if (l >= 0) {
if (l < PATH_MAX - 1)
newpath[l] = '\0';
else
newpath[0] = '\0';
char *p = strrchr(newpath, '/');
if (p)
*p = '\0';
snprintf(path, sizeof(path), "%s/%s/driver", "/sys/bus/usb/devices/", newpath);
goto read_driver;
}
}
}
/* not root hub */
snprintf(path + strlen(path), sizeof(path) - strlen(path), "%d", busnum);
for (i=0; i<6; i++) {
int next = location_id >> (20 - i*4);
if (!next)
break;
snprintf(path + strlen(path), sizeof(path) - strlen(path), "%s%d", ((i>0) ? "." : "-" ), next);
mask >>= 4;
location_id &= mask;
}
snprintf(path + strlen(path), sizeof(path) - strlen(path), ":1.%d/driver", ifnum);
read_driver:
if (strlen(path) < PATH_MAX) {
l = readlink(path, newpath, PATH_MAX);
if (l >= 0) {
if (l < PATH_MAX - 1)
newpath[l] = '\0';
else
newpath[0] = '\0';
char *p = strrchr(newpath, '/');
if (p)
snprintf(*driver, MY_STRING_MAX, "%s", p + 1);
}
}
#endif
}
static void build_dev_strings(libusb_device **devs, char *dev_strings[], int cnt )
{
/*
* Build a formatted line to be sorted for the tree.
* The LocationID has the tree structure (0xbbdddddd):
* 0x -- always
* bb -- bus number in hexadecimal (use ones complement to sort)
* dddddd -- up to six levels for the tree, each digit represents its
* position on that level
*
* So we start each line with the LocationID for sorting purposes, then append the rest of the line as we want it
* Later, we will do the sort, then strip off the LocationID for display purposes
*/
libusb_device *dev;
unsigned int location_id;
int i = 0, j = 0;
for (i=0;i<cnt;i++) {
struct libusb_device_descriptor desc;
dev = devs[i];
location_id = get_location_id(dev);
char portnum[2];
unsigned int busnum;
unsigned int devnum;
unsigned int maxchild;
char * driver = malloc(MY_STRING_MAX);
char speed[MY_PARAM_MAX]; /* '1.5','12','480','5000' + '\n' */
char spaces[MY_STRING_MAX];
char tmp[12];
int scmp;
sprintf(tmp, "%06x", location_id & 0x00ffffff);
scmp = strchr(tmp,'0') - tmp;
if (scmp < 0)
scmp = strlen(tmp);
strcpy(spaces,"");
if (scmp == 0) {
strcpy(portnum,"1");
strcpy(spaces,"/: ");
} else {
strncpy(portnum,&tmp[scmp-1],1);
portnum[1] = '\0';
for (j=0; j<scmp; j++) {
snprintf(spaces + strlen(spaces), sizeof(spaces) - strlen(spaces), "%s", " ");
}
snprintf(spaces + strlen(spaces), sizeof(spaces) - strlen(spaces), "%s", "|__ ");
}
int r = libusb_get_device_descriptor(dev, &desc);
if (r < 0) {
fprintf(stderr, "failed to get device descriptor");
return;
}
busnum = libusb_get_bus_number(dev);
devnum = libusb_get_device_address(dev);
switch (libusb_get_device_speed(dev)) {
case LIBUSB_SPEED_LOW: strcpy(speed, "1.5"); break;
case LIBUSB_SPEED_FULL: strcpy(speed, "12"); break;
case LIBUSB_SPEED_HIGH: strcpy(speed, "480"); break;
case LIBUSB_SPEED_SUPER: strcpy(speed, "5000"); break;
default: strcpy(speed, "Unknown"); break;
}
if (scmp == 0) {
/* for root hub */
maxchild = get_maxchild(location_id);
get_driver(&driver, location_id, 0);
/* invert busnum part of location_id for sorting purposes */
snprintf(dev_strings[i], MY_PATH_MAX,
"0x%08x %sBus %02u.Port %s: Dev %u, Class=root_hub, Driver=%s/%up, %sM\n",
location_id ^ 0xff000000, spaces, busnum, portnum, devnum, driver, maxchild, speed);
} else {
for (j = 0; j < desc.bNumConfigurations; ++j) {
struct libusb_config_descriptor *config;
int ifnum;
int ret = libusb_get_config_descriptor(dev, j, &config);
if (ret) {
fprintf(stderr, "Couldn't get configuration "
"descriptor %d, some information will "
"be missing\n", j);
continue;
}
for (ifnum = 0 ; ifnum < config->bNumInterfaces ; ifnum++) {
const struct libusb_interface *intf;
intf = &config->interface[ifnum];
const struct libusb_interface_descriptor *alt;
alt = &intf->altsetting[0];
char ifcls[MY_PARAM_MAX];
get_class_string(ifcls, sizeof(ifcls), alt->bInterfaceClass);
maxchild = get_maxchild(location_id);
get_driver(&driver, location_id, ifnum);
if(ifnum == 0) {
/* invert busnum part of location_id for sorting purposes */
sprintf(tmp,"0x%08x ", location_id ^ 0xff000000);
strcpy(dev_strings[i], tmp);
}
/* put all the interfaces together into one long line for
* sorting purposes, and leave room for the final newline */
if (alt->bInterfaceClass == 9) {
snprintf(dev_strings[i] + strlen(dev_strings[i]), MY_PATH_MAX - strlen(dev_strings[i]),
"%sPort %s: Dev %u, If %u, Class=%s, Driver=%s"
"/%up"
", %sM%s",
spaces, portnum, devnum, ifnum, ifcls, driver,
maxchild,
speed, ((ifnum < config->bNumInterfaces - 1 ) ? "\n" : "" ));
} else {
snprintf(dev_strings[i] + strlen(dev_strings[i]), MY_PATH_MAX - strlen(dev_strings[i]),
"%sPort %s: Dev %u, If %u, Class=%s, Driver=%s"
", %sM%s",
spaces, portnum, devnum, ifnum, ifcls, driver,
speed, ((ifnum < config->bNumInterfaces - 1 ) ? "\n" : "" ));
}
}
snprintf(dev_strings[i] + strlen(dev_strings[i]), MY_PATH_MAX - strlen(dev_strings[i]), "%s", "\n");
libusb_free_config_descriptor(config);
}
}
free(driver);
}
return;
}
int lsusb_t(void)
{
libusb_device **devs;
int r, i;
ssize_t cnt;
r = libusb_init(NULL);
if (r < 0)
return r;
cnt = libusb_get_device_list(NULL, &devs);
if (cnt < 0)
return cnt;
char *strings[cnt];
for (i=0; i<cnt; i++) {
strings[i] = malloc(MY_PATH_MAX);
strings[i][0] = '\0';
}
build_dev_strings(devs, strings, cnt);
/* Sort by Location ID */
sort_dev_strings(strings, cnt);
/* Now strip off leading Location ID and print to stdout */
for (i=0;i<cnt;i++) {
char *p = strchr(strings[i],' ');
if (p)
snprintf(strings[i], MY_PATH_MAX, "%s", p + 1);
printf("%s",strings[i]);
free(strings[i]);
}
libusb_free_device_list(devs, 1);
libusb_exit(NULL);
return 0;
}