2 * Test suite program based of libusb-0.1-compat testlibusb
3 * Copyright (c) 2013 Nathan Hjelm <hjelmn@mac.ccom>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 #if defined(_MSC_VER) && (_MSC_VER < 1900)
25 #define snprintf _snprintf
30 static void print_endpoint_comp(const struct libusb_ss_endpoint_companion_descriptor *ep_comp)
32 printf(" USB 3.0 Endpoint Companion:\n");
33 printf(" bMaxBurst: %d\n", ep_comp->bMaxBurst);
34 printf(" bmAttributes: 0x%02x\n", ep_comp->bmAttributes);
35 printf(" wBytesPerInterval: %d\n", ep_comp->wBytesPerInterval);
38 static void print_endpoint(const struct libusb_endpoint_descriptor *endpoint)
42 printf(" Endpoint:\n");
43 printf(" bEndpointAddress: %02xh\n", endpoint->bEndpointAddress);
44 printf(" bmAttributes: %02xh\n", endpoint->bmAttributes);
45 printf(" wMaxPacketSize: %d\n", endpoint->wMaxPacketSize);
46 printf(" bInterval: %d\n", endpoint->bInterval);
47 printf(" bRefresh: %d\n", endpoint->bRefresh);
48 printf(" bSynchAddress: %d\n", endpoint->bSynchAddress);
50 for (i = 0; i < endpoint->extra_length;) {
51 if (LIBUSB_DT_SS_ENDPOINT_COMPANION == endpoint->extra[i + 1]) {
52 struct libusb_ss_endpoint_companion_descriptor *ep_comp;
54 ret = libusb_get_ss_endpoint_companion_descriptor(NULL, endpoint, &ep_comp);
55 if (LIBUSB_SUCCESS != ret) {
59 print_endpoint_comp(ep_comp);
61 libusb_free_ss_endpoint_companion_descriptor(ep_comp);
64 i += endpoint->extra[i];
68 static void print_altsetting(const struct libusb_interface_descriptor *interface)
72 printf(" Interface:\n");
73 printf(" bInterfaceNumber: %d\n", interface->bInterfaceNumber);
74 printf(" bAlternateSetting: %d\n", interface->bAlternateSetting);
75 printf(" bNumEndpoints: %d\n", interface->bNumEndpoints);
76 printf(" bInterfaceClass: %d\n", interface->bInterfaceClass);
77 printf(" bInterfaceSubClass: %d\n", interface->bInterfaceSubClass);
78 printf(" bInterfaceProtocol: %d\n", interface->bInterfaceProtocol);
79 printf(" iInterface: %d\n", interface->iInterface);
81 for (i = 0; i < interface->bNumEndpoints; i++)
82 print_endpoint(&interface->endpoint[i]);
85 static void print_2_0_ext_cap(struct libusb_usb_2_0_extension_descriptor *usb_2_0_ext_cap)
87 printf(" USB 2.0 Extension Capabilities:\n");
88 printf(" bDevCapabilityType: %d\n", usb_2_0_ext_cap->bDevCapabilityType);
89 printf(" bmAttributes: 0x%x\n", usb_2_0_ext_cap->bmAttributes);
92 static void print_ss_usb_cap(struct libusb_ss_usb_device_capability_descriptor *ss_usb_cap)
94 printf(" USB 3.0 Capabilities:\n");
95 printf(" bDevCapabilityType: %d\n", ss_usb_cap->bDevCapabilityType);
96 printf(" bmAttributes: 0x%x\n", ss_usb_cap->bmAttributes);
97 printf(" wSpeedSupported: 0x%x\n", ss_usb_cap->wSpeedSupported);
98 printf(" bFunctionalitySupport: %d\n", ss_usb_cap->bFunctionalitySupport);
99 printf(" bU1devExitLat: %d\n", ss_usb_cap->bU1DevExitLat);
100 printf(" bU2devExitLat: %d\n", ss_usb_cap->bU2DevExitLat);
103 static void print_bos(libusb_device_handle *handle)
105 struct libusb_bos_descriptor *bos;
108 ret = libusb_get_bos_descriptor(handle, &bos);
113 printf(" Binary Object Store (BOS):\n");
114 printf(" wTotalLength: %d\n", bos->wTotalLength);
115 printf(" bNumDeviceCaps: %d\n", bos->bNumDeviceCaps);
117 if(bos->dev_capability[0]->bDevCapabilityType == LIBUSB_BT_USB_2_0_EXTENSION) {
119 struct libusb_usb_2_0_extension_descriptor *usb_2_0_extension;
120 ret = libusb_get_usb_2_0_extension_descriptor(NULL, bos->dev_capability[0],&usb_2_0_extension);
125 print_2_0_ext_cap(usb_2_0_extension);
126 libusb_free_usb_2_0_extension_descriptor(usb_2_0_extension);
129 if(bos->dev_capability[0]->bDevCapabilityType == LIBUSB_BT_SS_USB_DEVICE_CAPABILITY) {
131 struct libusb_ss_usb_device_capability_descriptor *dev_cap;
132 ret = libusb_get_ss_usb_device_capability_descriptor(NULL, bos->dev_capability[0],&dev_cap);
137 print_ss_usb_cap(dev_cap);
138 libusb_free_ss_usb_device_capability_descriptor(dev_cap);
141 libusb_free_bos_descriptor(bos);
144 static void print_interface(const struct libusb_interface *interface)
148 for (i = 0; i < interface->num_altsetting; i++)
149 print_altsetting(&interface->altsetting[i]);
152 static void print_configuration(struct libusb_config_descriptor *config)
156 printf(" Configuration:\n");
157 printf(" wTotalLength: %d\n", config->wTotalLength);
158 printf(" bNumInterfaces: %d\n", config->bNumInterfaces);
159 printf(" bConfigurationValue: %d\n", config->bConfigurationValue);
160 printf(" iConfiguration: %d\n", config->iConfiguration);
161 printf(" bmAttributes: %02xh\n", config->bmAttributes);
162 printf(" MaxPower: %d\n", config->MaxPower);
164 for (i = 0; i < config->bNumInterfaces; i++)
165 print_interface(&config->interface[i]);
168 static int print_device(libusb_device *dev, int level)
170 struct libusb_device_descriptor desc;
171 libusb_device_handle *handle = NULL;
172 char description[256];
177 ret = libusb_get_device_descriptor(dev, &desc);
179 fprintf(stderr, "failed to get device descriptor");
183 ret = libusb_open(dev, &handle);
184 if (LIBUSB_SUCCESS == ret) {
185 if (desc.iManufacturer) {
186 ret = libusb_get_string_descriptor_ascii(handle, desc.iManufacturer, string, sizeof(string));
188 snprintf(description, sizeof(description), "%s - ", string);
190 snprintf(description, sizeof(description), "%04X - ",
194 snprintf(description, sizeof(description), "%04X - ",
198 ret = libusb_get_string_descriptor_ascii(handle, desc.iProduct, string, sizeof(string));
200 snprintf(description + strlen(description), sizeof(description) -
201 strlen(description), "%s", string);
203 snprintf(description + strlen(description), sizeof(description) -
204 strlen(description), "%04X", desc.idProduct);
207 snprintf(description + strlen(description), sizeof(description) -
208 strlen(description), "%04X", desc.idProduct);
211 snprintf(description, sizeof(description), "%04X - %04X",
212 desc.idVendor, desc.idProduct);
215 printf("%.*sDev (bus %d, device %d): %s\n", level * 2, " ",
216 libusb_get_bus_number(dev), libusb_get_device_address(dev), description);
218 if (handle && verbose) {
219 if (desc.iSerialNumber) {
220 ret = libusb_get_string_descriptor_ascii(handle, desc.iSerialNumber, string, sizeof(string));
222 printf("%.*s - Serial Number: %s\n", level * 2,
228 for (i = 0; i < desc.bNumConfigurations; i++) {
229 struct libusb_config_descriptor *config;
230 ret = libusb_get_config_descriptor(dev, i, &config);
231 if (LIBUSB_SUCCESS != ret) {
232 printf(" Couldn't retrieve descriptors\n");
236 print_configuration(config);
238 libusb_free_config_descriptor(config);
241 if (handle && desc.bcdUSB >= 0x0201) {
247 libusb_close(handle);
252 int main(int argc, char *argv[])
254 libusb_device **devs;
258 if (argc > 1 && !strcmp(argv[1], "-v"))
261 r = libusb_init(NULL);
265 cnt = libusb_get_device_list(NULL, &devs);
269 for (i = 0; devs[i]; ++i) {
270 print_device(devs[i], 0);
273 libusb_free_device_list(devs, 1);