-
Notifications
You must be signed in to change notification settings - Fork 61
/
sysfs.c
775 lines (623 loc) · 17.1 KB
/
sysfs.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
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
// SPDX-License-Identifier: GPL-2.0-only
/*
* sysfs.c for USBView - a USB device viewer
* Copyright (c) 1999, 2000, 2022 by Greg Kroah-Hartman, <[email protected]>
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#define _GNU_SOURCE
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <unistd.h>
#include <gtk/gtk.h>
#include "usbtree.h"
#include "sysfs.h"
#include "ccan/list/list.h"
struct Device *rootDevice = NULL;
static struct DeviceBandwidth *currentBandwidth = NULL;
/* taken from all-io.h from util-linux repo */
static inline ssize_t read_all(int fd, char *buf, size_t count)
{
ssize_t ret;
ssize_t c = 0;
int tries = 0;
while (count > 0) {
ret = read(fd, buf, count);
if (ret <= 0) {
if (ret < 0 && (errno == EAGAIN || errno == EINTR) &&
(tries++ < 5)) {
usleep(250000);
continue;
}
return c ? c : -1;
}
tries = 0;
count -= ret;
buf += ret;
c += ret;
}
return c;
}
static int openreadclose(const char *filename, char *buffer, size_t bufsize)
{
ssize_t count;
int fd;
fd = openat(0, filename, O_RDONLY);
if (fd < 0) {
printf("error opening %s\n", filename);
return fd;
}
count = read_all(fd, buffer, bufsize);
if (count < 0) {
printf("Error %ld reading from %s\n", count, filename);
}
close(fd);
return count;
}
static int check_file_present(const char *filename)
{
struct stat sb;
int retval;
retval = stat(filename, &sb);
if (retval == -1) {
//fprintf(stderr,
// "filename %s is not present\n", filename);
return retval;
}
if ((sb.st_mode & S_IFMT) != S_IFREG) {
fprintf(stderr,
"filename %s must be a real file, not anything else.\n",
filename);
return -1;
}
return 0;
}
static int check_dir_present(const char *filename)
{
struct stat sb;
int retval;
retval = stat(filename, &sb);
if (retval == -1) {
// fprintf(stderr,
// "filename %s is not present\n", filename);
return retval;
}
if ((sb.st_mode & S_IFMT) != S_IFDIR) {
fprintf(stderr,
"filename %s must be a directory, not anything else.\n",
filename);
return -1;
}
return 0;
}
static char *sysfs_string(const char *directory, const char *filename)
{
char full_filename[PATH_MAX];
char buffer[256];
char *temp;
int retval;
snprintf(full_filename, PATH_MAX, "%s/%s", directory, filename);
if (check_file_present(full_filename))
return NULL;
retval = openreadclose(full_filename, buffer, sizeof(buffer));
if (retval <= 0)
return NULL;
/* strip trailing \n off */
buffer[retval-1] = 0x00;
temp = g_strdup(buffer);
return temp;
}
static int sysfs_int(const char *dir, const char *filename, int base)
{
char *string = sysfs_string(dir, filename);
if (!string)
return 0;
int value = strtol(string, NULL, base);
g_free(string);
return value;
}
static void DestroyEndpoint (struct DeviceEndpoint *endpoint)
{
if (endpoint == NULL)
return;
g_free (endpoint->type);
g_free (endpoint->interval);
g_free (endpoint);
return;
}
static void DestroyInterface (struct DeviceInterface *interface)
{
int i;
if (interface == NULL)
return;
for (i = 0; i < MAX_ENDPOINTS; ++i)
DestroyEndpoint (interface->endpoint[i]);
g_free (interface->name);
g_free (interface->class);
g_free (interface);
return;
}
static void DestroyConfig (struct DeviceConfig *config)
{
int i;
if (config == NULL)
return;
for (i = 0; i < MAX_INTERFACES; ++i)
DestroyInterface (config->interface[i]);
g_free (config->maxPower);
g_free (config);
return;
}
static void DestroyBandwidth (struct DeviceBandwidth *bandwidth)
{
/* nothing dynamic in the bandwidth structure yet. */
return;
}
static void DestroyDevice(struct Device *device)
{
int i;
if (device == NULL)
return;
for (i = 0; i < MAX_CHILDREN; ++i)
DestroyDevice (device->child[i]);
for (i = 0; i < MAX_CONFIGS; ++i)
DestroyConfig (device->config[i]);
if (device->bandwidth != NULL) {
DestroyBandwidth (device->bandwidth);
g_free (device->bandwidth);
}
g_free (device->name);
g_free (device->version);
g_free (device->class);
g_free (device->subClass);
g_free (device->protocol);
g_free (device->revisionNumber);
g_free (device->manufacturer);
g_free (device->product);
g_free (device->serialNumber);
g_free (device);
return;
}
static struct Device *FindDeviceNode(struct Device *device,
int deviceNumber, int busNumber)
{
int i;
struct Device *result;
if (device == NULL)
return(NULL);
if ((device->deviceNumber == deviceNumber) &&
(device->busNumber == busNumber))
return(device);
for (i = 0; i < MAX_CHILDREN; ++i) {
result = FindDeviceNode (device->child[i], deviceNumber, busNumber);
if (result != NULL)
return(result);
}
return(NULL);
}
struct Device *usb_find_device (int deviceNumber, int busNumber)
{
int i;
struct Device *result;
/* search through the tree to try to find a device */
for (i = 0; i < MAX_CHILDREN; ++i) {
result = FindDeviceNode (rootDevice->child[i], deviceNumber, busNumber);
if (result != NULL)
return(result);
}
return(NULL);
}
/* Build all of the names of the devices */
static void NameDevice (struct Device *device)
{
int configNum;
int interfaceNum;
int i;
if (device == NULL)
return;
/* build the name for this device */
if (device != rootDevice) {
if (device->name)
g_free (device->name);
device->name = (gchar *)g_malloc0 (DEVICE_STRING_MAXSIZE*sizeof (gchar));
/* see if this device has a product name */
if (device->product != NULL) {
strcpy (device->name, device->product);
goto create_children_names;
}
/* see if this device is a root hub */
if (device->level == 0) {
strcpy (device->name, "root hub");
goto create_children_names;
}
/* look through all of the interfaces of this device, adding them all up to form a name */
for (configNum = 0; configNum < MAX_CONFIGS; ++configNum) {
if (device->config[configNum]) {
struct DeviceConfig *config = device->config[configNum];
for (interfaceNum = 0; interfaceNum < MAX_INTERFACES; ++interfaceNum) {
if (config->interface[interfaceNum]) {
struct DeviceInterface *interface = config->interface[interfaceNum];
if (interface->name != NULL) {
if (strstr (interface->name, "none") == NULL) {
if (strcmp (interface->name, "hid") == 0) {
if (interface->subClass == 1) {
switch (interface->protocol) {
case 1 :
strcat (device->name, "keyboard");
continue;
case 2 :
strcat (device->name, "mouse");
continue;
}
}
}
if (strstr (device->name, interface->name) == NULL) {
if (strlen (device->name) > 0) {
strcat (device->name, " / ");
}
strcat (device->name, interface->name);
}
}
}
}
}
}
}
if (strlen (device->name) == 0) {
strcat (device->name, "Unknown Device");
}
}
create_children_names:
/* create all of the children's names */
for (i = 0; i < MAX_CHILDREN; ++i) {
NameDevice (device->child[i]);
}
return;
}
void usb_initialize_list (void)
{
if (rootDevice != NULL) {
DestroyDevice (rootDevice);
rootDevice = NULL;
}
rootDevice = (struct Device *)g_malloc0 (sizeof(struct Device));
/* clean up any bandwidth devices */
if (currentBandwidth != NULL) {
DestroyBandwidth (currentBandwidth);
g_free (currentBandwidth);
}
return;
}
struct entity {
struct list_node list;
char *name;
};
static struct entity *entity_create(char *name)
{
struct entity *e;
e = malloc(sizeof(*e));
memset(e, 0x00, sizeof(*e));
e->name = g_strdup(name);
return e;
}
static void entity_destroy(struct entity *e)
{
list_del(&e->list);
g_free(e->name);
g_free(e);
}
static void entity_add(struct list_head *list, struct entity *e)
{
struct entity *iter;
list_for_each(list, iter, list) {
if (strcmp(iter->name, e->name) > 0) {
list_add_before(list, &iter->list, &e->list);
goto exit;
}
}
list_add_tail(list, &e->list);
exit:
#ifdef DEBUG_LIST
printf("list = ");
list_for_each(list, iter, list) {
printf("%s ", iter->name);
}
printf("\n");
#endif
return;
}
static void endpoint_parse(struct DeviceInterface *interface, const char *dir)
{
struct DeviceEndpoint *endpoint;
int i;
/* find a place in this interface to place the endpoint */
for (i = 0; i < MAX_ENDPOINTS; ++i) {
if (interface->endpoint[i] == NULL) {
break;
}
}
if (i >= MAX_ENDPOINTS) {
/* ran out of room to hold this endpoint */
g_warning ("Too many endpoints for this device.\n");
return;
}
endpoint = g_malloc0(sizeof(struct DeviceEndpoint));
endpoint->attribute = sysfs_int(dir, "bmAttributes", 16);
endpoint->maxPacketSize = sysfs_int(dir, "wMaxPacketSize", 16);
char *epdir = sysfs_string(dir, "direction");
if (!strcmp(epdir, "in"))
endpoint->in = TRUE;
else
endpoint->in = FALSE;
g_free(epdir);
endpoint->type = sysfs_string(dir, "type");
endpoint->interval = sysfs_string(dir, "interval");
endpoint->address = sysfs_int(dir, "bEndpointAddress", 16);
/* point the interface to the endpoint */
interface->endpoint[i] = endpoint;
// FIXME check max packet size
#if 0
char *maxps_hex = sysfs_read(dir, "wMaxPacketSize");
int maxps_num = strtol(maxps_hex, NULL, 16);
/* max packet size is bits 0-10 with multiplicity values in bits 11 and 12 */
maxps_num = (maxps_num & 0x7ff) * (1 + ((maxps_num >> 11) & 0x03));
printf("E: Ad=%s(%s) Atr=%s(%s) MxPS=%4d Ivl=%s\n",
addr, direction, attr, type, maxps_num, interval);
#endif
}
static void endpoints_parse(struct DeviceInterface *interface, const char *dir)
{
DIR *d;
struct dirent *de;
LIST_HEAD(endpoint_list);
struct entity *temp;
struct entity *e;
d = opendir(dir);
if (!d) {
fprintf(stderr, "Can not open %s directory\n", dir);
return;
}
while ((de = readdir(d))) {
if (de->d_type == DT_DIR) {
/* See if this directory is an interface or not */
char endpoint[PATH_MAX];
snprintf(endpoint, PATH_MAX, "%s/%s", dir, de->d_name);
char *endpointaddr = sysfs_string(endpoint, "bEndpointAddress");
if (endpointaddr) {
e = entity_create(endpoint);
entity_add(&endpoint_list, e);
g_free(endpointaddr);
}
}
}
closedir(d);
list_for_each_safe(&endpoint_list, e, temp, list) {
endpoint_parse(interface, e->name);
entity_destroy(e);
}
}
static void interface_parse(struct Device *device, const char *dir)
{
struct DeviceConfig *config;
struct DeviceInterface *interface;
int i;
int configNum;
/* find the LAST config in the device */
configNum = -1;
for (i = 0; i < MAX_CONFIGS; ++i)
if (device->config[i])
configNum = i;
if (configNum == -1) {
/* no config to put this interface at, not good */
g_warning ("No config to put an interface at for this device.\n");
return;
}
config = device->config[configNum];
/* now find a place in this config to place the interface */
for (i = 0; i < MAX_INTERFACES; ++i)
if (config->interface[i] == NULL)
break;
if (i >= MAX_INTERFACES) {
/* ran out of room to hold this interface */
g_warning ("Too many interfaces for this device.\n");
return;
}
interface = g_malloc0(sizeof(struct DeviceInterface));
interface->interfaceNumber = sysfs_int(dir, "bInterfaceNumber", 10);
interface->alternateNumber = sysfs_int(dir, "bAlternateSetting", 10);
interface->numEndpoints = sysfs_int(dir, "bNumEndpoints", 10);
interface->subClass = sysfs_int(dir, "bInterfaceSubClass", 16);
interface->protocol = sysfs_int(dir, "bInterfaceProtocol", 16);
interface->class = sysfs_string(dir, "bInterfaceClass");
char drivername[PATH_MAX];
char link[PATH_MAX];
struct stat sb;
int retval;
char *driver = "(none)";
/*
* find the driver name by looking at the basename of the driver
* symbolic link.
* In bash this would just be:
* driver=`readlink $ifpath/driver`
* driver=`basename "$driver"`
*/
snprintf(drivername, PATH_MAX, "%s/driver", dir);
retval = stat(drivername, &sb);
memset(link, 0x00, sizeof(link));
if (!retval) {
retval = readlink(drivername, link, sizeof(link));
if (retval > 0) {
/*
* crude 'basename', go to the end of the string and
* walk back to find a '/' and then go forward one.
*/
driver = &link[strlen(link)];
while (driver[0] != '/')
driver--;
driver++;
}
}
interface->name = g_strdup(driver);
/* if this interface does not have a driver attached to it, save that info for later */
if (strncmp(interface->name, INTERFACE_DRIVERNAME_NODRIVER_STRING,
INTERFACE_DRIVERNAME_STRING_MAXLENGTH) == 0) {
interface->driverAttached = FALSE;
} else {
interface->driverAttached = TRUE;
}
/* now point the config to this interface */
config->interface[i] = interface;
endpoints_parse(interface, dir);
}
static void interfaces_parse(struct Device *device, const char *dir)
{
DIR *d;
struct dirent *de;
LIST_HEAD(interface_list);
struct entity *temp;
struct entity *e;
d = opendir(dir);
if (!d) {
fprintf(stderr, "Can not open %s directory\n", dir);
return;
}
while ((de = readdir(d))) {
if (de->d_type == DT_DIR) {
/* See if this directory is an interface or not */
char interface[PATH_MAX];
snprintf(interface, PATH_MAX, "%s/%s", dir, de->d_name);
char *interfacenum = sysfs_string(interface, "bInterfaceNumber");
if (interfacenum) {
e = entity_create(interface);
entity_add(&interface_list, e);
g_free(interfacenum);
}
}
}
closedir(d);
list_for_each_safe(&interface_list, e, temp, list) {
interface_parse(device, e->name);
entity_destroy(e);
}
}
static void device_parse(struct Device *parent, const char *directory);
static void children_parse(struct Device *parent, const char *dir)
{
int devcount = 0;
DIR *d;
struct dirent *de;
d = opendir(dir);
if (!d) {
fprintf(stderr, "Can not open %s directory\n", dir);
return;
}
while ((de = readdir(d))) {
if (de->d_type == DT_DIR) {
if (de->d_name[0] == '.')
continue;
/* See if this directory is an interface or not */
char device[PATH_MAX];
snprintf(device, PATH_MAX, "%s/%s", dir, de->d_name);
char *device_class = sysfs_string(device, "bDeviceClass");
if (device_class) {
++devcount;
device_parse(parent, device);
}
g_free(device_class);
}
}
closedir(d);
}
static void device_parse(struct Device *parent, const char *dir)
{
struct Device *device;
if (check_dir_present(dir))
return;
device = (struct Device *)(g_malloc0 (sizeof(struct Device)));
if (parent == rootDevice)
device->level = 0;
else
device->level = 1;
int portnum = 0;
if (parent != rootDevice) {
/*
* The port number is the last number of the file (if present)
* before the '.' or '-' - 1
*/
const char *temp = &dir[strlen(dir)];
while ((*temp != '.') && (*temp != '-'))
--temp;
temp++;
portnum = strtol(temp, NULL, 10) - 1;
if (portnum < 0)
portnum = 0;
}
device->portNumber = portnum;
device->busNumber = sysfs_int(dir, "busnum", 10);
device->deviceNumber = sysfs_int(dir, "devnum", 10);
device->speed = sysfs_int(dir, "speed", 10);
device->maxChildren = sysfs_int(dir, "maxchild", 10);
device->parent = parent;
if (parent == rootDevice) {
++rootDevice->maxChildren;
rootDevice->child[rootDevice->maxChildren-1] = device;
} else {
parent->child[portnum] = device;
}
device->vendorId = sysfs_int(dir, "idVendor", 16);
device->productId = sysfs_int(dir, "idProduct", 16);
device->version = sysfs_string(dir, "version");
device->class = sysfs_string(dir, "bDeviceClass");
device->subClass = sysfs_string(dir, "bDeviceSubClass");
device->protocol = sysfs_string(dir, "bDeviceProtocol");
device->maxPacketSize = sysfs_int(dir, "bMaxPacketSize0", 10);
device->numConfigs = sysfs_int(dir, "bNumConfigurations", 10);
device->manufacturer = sysfs_string(dir, "manufacturer");
device->product = sysfs_string(dir, "product");
device->serialNumber = sysfs_string(dir, "serial");
char *bcddevice = sysfs_string(dir, "bcdDevice");
device->revisionNumber = (char *)g_malloc0 ((DEVICE_REVISION_NUMBER_SIZE) * sizeof(char));
device->revisionNumber[0] = bcddevice[0];
device->revisionNumber[1] = bcddevice[1];
device->revisionNumber[2] = '.';
device->revisionNumber[3] = bcddevice[2];
device->revisionNumber[4] = bcddevice[3];
g_free(bcddevice);
struct DeviceConfig *config;
config = g_malloc0(sizeof(struct DeviceConfig));
config->maxPower = sysfs_string(dir, "bMaxPower");
config->numInterfaces = sysfs_int(dir, "bNumInterfaces", 10);
config->configNumber = sysfs_int(dir, "bConfigurationValue", 10);
config->attributes = sysfs_int(dir, "bmAttributes", 16);
/* have the device now point to this config */
device->config[0] = config;
interfaces_parse(device, dir);
children_parse(device, dir);
}
void sysfs_parse(void)
{
char filename[PATH_MAX];
int i;
snprintf(filename, PATH_MAX, "/sys/bus/usb/devices/");
if (check_dir_present(filename)) {
fprintf(stderr, "%s must be present, exiting...\n", filename);
return;
}
for (i = 1; i < 100; ++i) {
snprintf(filename, PATH_MAX, "/sys/bus/usb/devices/usb%d/", i);
device_parse(rootDevice, filename);
}
}
void usb_name_devices (void)
{
NameDevice (rootDevice);
}