1 /*
2  * testlibusb.c
3  *
4  *  Test suite program
5  */
6 
7 #include <stdio.h>
8 #include <string.h>
9 #include <usb.h>
10 
11 int verbose = 0;
12 
print_endpoint(struct usb_endpoint_descriptor * endpoint)13 void print_endpoint(struct usb_endpoint_descriptor *endpoint)
14 {
15   printf("      bEndpointAddress: %02xh\n", endpoint->bEndpointAddress);
16   printf("      bmAttributes:     %02xh\n", endpoint->bmAttributes);
17   printf("      wMaxPacketSize:   %d\n", endpoint->wMaxPacketSize);
18   printf("      bInterval:        %d\n", endpoint->bInterval);
19   printf("      bRefresh:         %d\n", endpoint->bRefresh);
20   printf("      bSynchAddress:    %d\n", endpoint->bSynchAddress);
21 }
22 
print_altsetting(struct usb_interface_descriptor * interface)23 void print_altsetting(struct usb_interface_descriptor *interface)
24 {
25   int i;
26 
27   printf("    bInterfaceNumber:   %d\n", interface->bInterfaceNumber);
28   printf("    bAlternateSetting:  %d\n", interface->bAlternateSetting);
29   printf("    bNumEndpoints:      %d\n", interface->bNumEndpoints);
30   printf("    bInterfaceClass:    %d\n", interface->bInterfaceClass);
31   printf("    bInterfaceSubClass: %d\n", interface->bInterfaceSubClass);
32   printf("    bInterfaceProtocol: %d\n", interface->bInterfaceProtocol);
33   printf("    iInterface:         %d\n", interface->iInterface);
34 
35   for (i = 0; i < interface->bNumEndpoints; i++)
36     print_endpoint(&interface->endpoint[i]);
37 }
38 
print_interface(struct usb_interface * interface)39 void print_interface(struct usb_interface *interface)
40 {
41   int i;
42 
43   for (i = 0; i < interface->num_altsetting; i++)
44     print_altsetting(&interface->altsetting[i]);
45 }
46 
print_configuration(struct usb_config_descriptor * config)47 void print_configuration(struct usb_config_descriptor *config)
48 {
49   int i;
50 
51   printf("  wTotalLength:         %d\n", config->wTotalLength);
52   printf("  bNumInterfaces:       %d\n", config->bNumInterfaces);
53   printf("  bConfigurationValue:  %d\n", config->bConfigurationValue);
54   printf("  iConfiguration:       %d\n", config->iConfiguration);
55   printf("  bmAttributes:         %02xh\n", config->bmAttributes);
56   printf("  MaxPower:             %d\n", config->MaxPower);
57 
58   for (i = 0; i < config->bNumInterfaces; i++)
59     print_interface(&config->interface[i]);
60 }
61 
print_device(struct usb_device * dev,int level)62 int print_device(struct usb_device *dev, int level)
63 {
64   usb_dev_handle *udev;
65   char description[256];
66   char string[256];
67   int ret, i;
68 
69   udev = usb_open(dev);
70   if (udev) {
71     if (dev->descriptor.iManufacturer) {
72       ret = usb_get_string_simple(udev, dev->descriptor.iManufacturer, string, sizeof(string));
73       if (ret > 0)
74         snprintf(description, sizeof(description), "%s - ", string);
75       else
76         snprintf(description, sizeof(description), "%04X - ",
77                  dev->descriptor.idVendor);
78     } else
79       snprintf(description, sizeof(description), "%04X - ",
80                dev->descriptor.idVendor);
81 
82     if (dev->descriptor.iProduct) {
83       ret = usb_get_string_simple(udev, dev->descriptor.iProduct, string, sizeof(string));
84       if (ret > 0)
85         snprintf(description + strlen(description), sizeof(description) -
86                  strlen(description), "%s", string);
87       else
88         snprintf(description + strlen(description), sizeof(description) -
89                  strlen(description), "%04X", dev->descriptor.idProduct);
90     } else
91       snprintf(description + strlen(description), sizeof(description) -
92                strlen(description), "%04X", dev->descriptor.idProduct);
93 
94   } else
95     snprintf(description, sizeof(description), "%04X - %04X",
96              dev->descriptor.idVendor, dev->descriptor.idProduct);
97 
98   printf("%.*sDev #%d: %s\n", level * 2, "                    ", dev->devnum,
99          description);
100 
101   if (udev && verbose) {
102     if (dev->descriptor.iSerialNumber) {
103       ret = usb_get_string_simple(udev, dev->descriptor.iSerialNumber, string, sizeof(string));
104       if (ret > 0)
105         printf("%.*s  - Serial Number: %s\n", level * 2,
106                "                    ", string);
107     }
108   }
109 
110   if (udev)
111     usb_close(udev);
112 
113   if (verbose) {
114     if (!dev->config) {
115       printf("  Couldn't retrieve descriptors\n");
116       return 0;
117     }
118 
119     for (i = 0; i < dev->descriptor.bNumConfigurations; i++)
120       print_configuration(&dev->config[i]);
121   } else {
122     for (i = 0; i < dev->num_children; i++)
123       print_device(dev->children[i], level + 1);
124   }
125 
126   return 0;
127 }
128 
main(int argc,char * argv[])129 int main(int argc, char *argv[])
130 {
131   struct usb_bus *bus;
132 
133   if (argc > 1 && !strcmp(argv[1], "-v"))
134     verbose = 1;
135 
136   usb_init();
137 
138   usb_find_busses();
139   usb_find_devices();
140 
141   for (bus = usb_busses; bus; bus = bus->next) {
142     if (bus->root_dev && !verbose)
143       print_device(bus->root_dev, 0);
144     else {
145       struct usb_device *dev;
146 
147       for (dev = bus->devices; dev; dev = dev->next)
148         print_device(dev, 0);
149     }
150   }
151 
152   return 0;
153 }
154 
155