1 /*
2 * Test suite program based of libusb-0.1-compat testlibusb
3 * Copyright (c) 2013 Nathan Hjelm <[email protected]>
4 *
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.
9 *
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.
14 *
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
18 */
19
20 #include <stdio.h>
21 #include <string.h>
22 #include "libusb.h"
23
24 int verbose = 0;
25
print_endpoint_comp(const struct libusb_ss_endpoint_companion_descriptor * ep_comp)26 static void print_endpoint_comp(const struct libusb_ss_endpoint_companion_descriptor *ep_comp)
27 {
28 printf(" USB 3.0 Endpoint Companion:\n");
29 printf(" bMaxBurst: %u\n", ep_comp->bMaxBurst);
30 printf(" bmAttributes: %02xh\n", ep_comp->bmAttributes);
31 printf(" wBytesPerInterval: %u\n", ep_comp->wBytesPerInterval);
32 }
33
print_endpoint(const struct libusb_endpoint_descriptor * endpoint)34 static void print_endpoint(const struct libusb_endpoint_descriptor *endpoint)
35 {
36 int i, ret;
37
38 printf(" Endpoint:\n");
39 printf(" bEndpointAddress: %02xh\n", endpoint->bEndpointAddress);
40 printf(" bmAttributes: %02xh\n", endpoint->bmAttributes);
41 printf(" wMaxPacketSize: %u\n", endpoint->wMaxPacketSize);
42 printf(" bInterval: %u\n", endpoint->bInterval);
43 printf(" bRefresh: %u\n", endpoint->bRefresh);
44 printf(" bSynchAddress: %u\n", endpoint->bSynchAddress);
45
46 for (i = 0; i < endpoint->extra_length;) {
47 if (LIBUSB_DT_SS_ENDPOINT_COMPANION == endpoint->extra[i + 1]) {
48 struct libusb_ss_endpoint_companion_descriptor *ep_comp;
49
50 ret = libusb_get_ss_endpoint_companion_descriptor(NULL, endpoint, &ep_comp);
51 if (LIBUSB_SUCCESS != ret)
52 continue;
53
54 print_endpoint_comp(ep_comp);
55
56 libusb_free_ss_endpoint_companion_descriptor(ep_comp);
57 }
58
59 i += endpoint->extra[i];
60 }
61 }
62
print_altsetting(const struct libusb_interface_descriptor * interface)63 static void print_altsetting(const struct libusb_interface_descriptor *interface)
64 {
65 uint8_t i;
66
67 printf(" Interface:\n");
68 printf(" bInterfaceNumber: %u\n", interface->bInterfaceNumber);
69 printf(" bAlternateSetting: %u\n", interface->bAlternateSetting);
70 printf(" bNumEndpoints: %u\n", interface->bNumEndpoints);
71 printf(" bInterfaceClass: %u\n", interface->bInterfaceClass);
72 printf(" bInterfaceSubClass: %u\n", interface->bInterfaceSubClass);
73 printf(" bInterfaceProtocol: %u\n", interface->bInterfaceProtocol);
74 printf(" iInterface: %u\n", interface->iInterface);
75
76 for (i = 0; i < interface->bNumEndpoints; i++)
77 print_endpoint(&interface->endpoint[i]);
78 }
79
print_2_0_ext_cap(struct libusb_usb_2_0_extension_descriptor * usb_2_0_ext_cap)80 static void print_2_0_ext_cap(struct libusb_usb_2_0_extension_descriptor *usb_2_0_ext_cap)
81 {
82 printf(" USB 2.0 Extension Capabilities:\n");
83 printf(" bDevCapabilityType: %u\n", usb_2_0_ext_cap->bDevCapabilityType);
84 printf(" bmAttributes: %08xh\n", usb_2_0_ext_cap->bmAttributes);
85 }
86
print_ss_usb_cap(struct libusb_ss_usb_device_capability_descriptor * ss_usb_cap)87 static void print_ss_usb_cap(struct libusb_ss_usb_device_capability_descriptor *ss_usb_cap)
88 {
89 printf(" USB 3.0 Capabilities:\n");
90 printf(" bDevCapabilityType: %u\n", ss_usb_cap->bDevCapabilityType);
91 printf(" bmAttributes: %02xh\n", ss_usb_cap->bmAttributes);
92 printf(" wSpeedSupported: %u\n", ss_usb_cap->wSpeedSupported);
93 printf(" bFunctionalitySupport: %u\n", ss_usb_cap->bFunctionalitySupport);
94 printf(" bU1devExitLat: %u\n", ss_usb_cap->bU1DevExitLat);
95 printf(" bU2devExitLat: %u\n", ss_usb_cap->bU2DevExitLat);
96 }
97
print_bos(libusb_device_handle * handle)98 static void print_bos(libusb_device_handle *handle)
99 {
100 struct libusb_bos_descriptor *bos;
101 uint8_t i;
102 int ret;
103
104 ret = libusb_get_bos_descriptor(handle, &bos);
105 if (ret < 0)
106 return;
107
108 printf(" Binary Object Store (BOS):\n");
109 printf(" wTotalLength: %u\n", bos->wTotalLength);
110 printf(" bNumDeviceCaps: %u\n", bos->bNumDeviceCaps);
111
112 for (i = 0; i < bos->bNumDeviceCaps; i++) {
113 struct libusb_bos_dev_capability_descriptor *dev_cap = bos->dev_capability[i];
114
115 if (dev_cap->bDevCapabilityType == LIBUSB_BT_USB_2_0_EXTENSION) {
116 struct libusb_usb_2_0_extension_descriptor *usb_2_0_extension;
117
118 ret = libusb_get_usb_2_0_extension_descriptor(NULL, dev_cap, &usb_2_0_extension);
119 if (ret < 0)
120 return;
121
122 print_2_0_ext_cap(usb_2_0_extension);
123 libusb_free_usb_2_0_extension_descriptor(usb_2_0_extension);
124 } else if (dev_cap->bDevCapabilityType == LIBUSB_BT_SS_USB_DEVICE_CAPABILITY) {
125 struct libusb_ss_usb_device_capability_descriptor *ss_dev_cap;
126
127 ret = libusb_get_ss_usb_device_capability_descriptor(NULL, dev_cap, &ss_dev_cap);
128 if (ret < 0)
129 return;
130
131 print_ss_usb_cap(ss_dev_cap);
132 libusb_free_ss_usb_device_capability_descriptor(ss_dev_cap);
133 }
134 }
135
136 libusb_free_bos_descriptor(bos);
137 }
138
print_interface(const struct libusb_interface * interface)139 static void print_interface(const struct libusb_interface *interface)
140 {
141 int i;
142
143 for (i = 0; i < interface->num_altsetting; i++)
144 print_altsetting(&interface->altsetting[i]);
145 }
146
print_configuration(struct libusb_config_descriptor * config)147 static void print_configuration(struct libusb_config_descriptor *config)
148 {
149 uint8_t i;
150
151 printf(" Configuration:\n");
152 printf(" wTotalLength: %u\n", config->wTotalLength);
153 printf(" bNumInterfaces: %u\n", config->bNumInterfaces);
154 printf(" bConfigurationValue: %u\n", config->bConfigurationValue);
155 printf(" iConfiguration: %u\n", config->iConfiguration);
156 printf(" bmAttributes: %02xh\n", config->bmAttributes);
157 printf(" MaxPower: %u\n", config->MaxPower);
158
159 for (i = 0; i < config->bNumInterfaces; i++)
160 print_interface(&config->interface[i]);
161 }
162
print_device(libusb_device * dev,libusb_device_handle * handle)163 static void print_device(libusb_device *dev, libusb_device_handle *handle)
164 {
165 struct libusb_device_descriptor desc;
166 unsigned char string[256];
167 const char *speed;
168 int ret;
169 uint8_t i;
170
171 switch (libusb_get_device_speed(dev)) {
172 case LIBUSB_SPEED_LOW: speed = "1.5M"; break;
173 case LIBUSB_SPEED_FULL: speed = "12M"; break;
174 case LIBUSB_SPEED_HIGH: speed = "480M"; break;
175 case LIBUSB_SPEED_SUPER: speed = "5G"; break;
176 case LIBUSB_SPEED_SUPER_PLUS: speed = "10G"; break;
177 case LIBUSB_SPEED_SUPER_PLUS_X2: speed = "20G"; break;
178 default: speed = "Unknown";
179 }
180
181 ret = libusb_get_device_descriptor(dev, &desc);
182 if (ret < 0) {
183 fprintf(stderr, "failed to get device descriptor");
184 return;
185 }
186
187 printf("Dev (bus %u, device %u): %04X - %04X speed: %s\n",
188 libusb_get_bus_number(dev), libusb_get_device_address(dev),
189 desc.idVendor, desc.idProduct, speed);
190
191 if (!handle)
192 libusb_open(dev, &handle);
193
194 if (handle) {
195 if (desc.iManufacturer) {
196 ret = libusb_get_string_descriptor_ascii(handle, desc.iManufacturer, string, sizeof(string));
197 if (ret > 0)
198 printf(" Manufacturer: %s\n", (char *)string);
199 }
200
201 if (desc.iProduct) {
202 ret = libusb_get_string_descriptor_ascii(handle, desc.iProduct, string, sizeof(string));
203 if (ret > 0)
204 printf(" Product: %s\n", (char *)string);
205 }
206
207 if (desc.iSerialNumber && verbose) {
208 ret = libusb_get_string_descriptor_ascii(handle, desc.iSerialNumber, string, sizeof(string));
209 if (ret > 0)
210 printf(" Serial Number: %s\n", (char *)string);
211 }
212 }
213
214 if (verbose) {
215 for (i = 0; i < desc.bNumConfigurations; i++) {
216 struct libusb_config_descriptor *config;
217
218 ret = libusb_get_config_descriptor(dev, i, &config);
219 if (LIBUSB_SUCCESS != ret) {
220 printf(" Couldn't retrieve descriptors\n");
221 continue;
222 }
223
224 print_configuration(config);
225
226 libusb_free_config_descriptor(config);
227 }
228
229 if (handle && desc.bcdUSB >= 0x0201)
230 print_bos(handle);
231 }
232
233 if (handle)
234 libusb_close(handle);
235 }
236
237 #ifdef __linux__
238 #include <errno.h>
239 #include <fcntl.h>
240 #include <unistd.h>
241
test_wrapped_device(const char * device_name)242 static int test_wrapped_device(const char *device_name)
243 {
244 libusb_device_handle *handle;
245 int r, fd;
246
247 fd = open(device_name, O_RDWR);
248 if (fd < 0) {
249 printf("Error could not open %s: %s\n", device_name, strerror(errno));
250 return 1;
251 }
252 r = libusb_wrap_sys_device(NULL, fd, &handle);
253 if (r) {
254 printf("Error wrapping device: %s: %s\n", device_name, libusb_strerror(r));
255 close(fd);
256 return 1;
257 }
258 print_device(libusb_get_device(handle), handle);
259 close(fd);
260 return 0;
261 }
262 #else
test_wrapped_device(const char * device_name)263 static int test_wrapped_device(const char *device_name)
264 {
265 (void)device_name;
266 printf("Testing wrapped devices is not supported on your platform\n");
267 return 1;
268 }
269 #endif
270
main(int argc,char * argv[])271 int main(int argc, char *argv[])
272 {
273 const char *device_name = NULL;
274 libusb_device **devs;
275 ssize_t cnt;
276 int r, i;
277
278 for (i = 1; i < argc; i++) {
279 if (!strcmp(argv[i], "-v")) {
280 verbose = 1;
281 } else if (!strcmp(argv[i], "-d") && (i + 1) < argc) {
282 i++;
283 device_name = argv[i];
284 } else {
285 printf("Usage %s [-v] [-d </dev/bus/usb/...>]\n", argv[0]);
286 printf("Note use -d to test libusb_wrap_sys_device()\n");
287 return 1;
288 }
289 }
290
291 r = libusb_init_context(/*ctx=*/NULL, /*options=*/NULL, /*num_options=*/0);
292 if (r < 0)
293 return r;
294
295 if (device_name) {
296 r = test_wrapped_device(device_name);
297 } else {
298 cnt = libusb_get_device_list(NULL, &devs);
299 if (cnt < 0) {
300 libusb_exit(NULL);
301 return 1;
302 }
303
304 for (i = 0; devs[i]; i++)
305 print_device(devs[i], NULL);
306
307 libusb_free_device_list(devs, 1);
308 }
309
310 libusb_exit(NULL);
311 return r;
312 }
313