blob: ba00f9061b2874d9e29c57b5116e1ca8c3e22e22 [file] [log] [blame]
Ludovic Rousseaua1cf2062017-07-12 16:54:44 +02001/*
2* Test suite program based of libusb-0.1-compat testlibusb
3* Copyright (c) 2013 Nathan Hjelm <hjelmn@mac.ccom>
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
24int verbose = 0;
25
26static void print_endpoint_comp(const struct libusb_ss_endpoint_companion_descriptor *ep_comp)
27{
28 printf(" USB 3.0 Endpoint Companion:\n");
Chris Dickens88ded792020-01-13 12:23:02 -080029 printf(" bMaxBurst: %u\n", ep_comp->bMaxBurst);
30 printf(" bmAttributes: %02xh\n", ep_comp->bmAttributes);
31 printf(" wBytesPerInterval: %u\n", ep_comp->wBytesPerInterval);
Ludovic Rousseaua1cf2062017-07-12 16:54:44 +020032}
33
34static void print_endpoint(const struct libusb_endpoint_descriptor *endpoint)
35{
36 int i, ret;
37
38 printf(" Endpoint:\n");
Chris Dickens88ded792020-01-13 12:23:02 -080039 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);
Ludovic Rousseaua1cf2062017-07-12 16:54:44 +020045
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);
Chris Dickens88ded792020-01-13 12:23:02 -080051 if (LIBUSB_SUCCESS != ret)
Ludovic Rousseaua1cf2062017-07-12 16:54:44 +020052 continue;
Ludovic Rousseaua1cf2062017-07-12 16:54:44 +020053
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
63static void print_altsetting(const struct libusb_interface_descriptor *interface)
64{
Chris Dickensf6925522018-03-24 17:42:46 -070065 uint8_t i;
Ludovic Rousseaua1cf2062017-07-12 16:54:44 +020066
67 printf(" Interface:\n");
Chris Dickens88ded792020-01-13 12:23:02 -080068 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);
Ludovic Rousseaua1cf2062017-07-12 16:54:44 +020075
76 for (i = 0; i < interface->bNumEndpoints; i++)
77 print_endpoint(&interface->endpoint[i]);
78}
79
80static 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");
Chris Dickens88ded792020-01-13 12:23:02 -080083 printf(" bDevCapabilityType: %u\n", usb_2_0_ext_cap->bDevCapabilityType);
84 printf(" bmAttributes: %08xh\n", usb_2_0_ext_cap->bmAttributes);
Ludovic Rousseaua1cf2062017-07-12 16:54:44 +020085}
86
87static void print_ss_usb_cap(struct libusb_ss_usb_device_capability_descriptor *ss_usb_cap)
88{
89 printf(" USB 3.0 Capabilities:\n");
Chris Dickens88ded792020-01-13 12:23:02 -080090 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);
Ludovic Rousseaua1cf2062017-07-12 16:54:44 +020096}
97
98static void print_bos(libusb_device_handle *handle)
99{
100 struct libusb_bos_descriptor *bos;
Chris Dickens88ded792020-01-13 12:23:02 -0800101 uint8_t i;
Ludovic Rousseaua1cf2062017-07-12 16:54:44 +0200102 int ret;
103
104 ret = libusb_get_bos_descriptor(handle, &bos);
Chris Dickens88ded792020-01-13 12:23:02 -0800105 if (ret < 0)
Ludovic Rousseaua1cf2062017-07-12 16:54:44 +0200106 return;
Ludovic Rousseaua1cf2062017-07-12 16:54:44 +0200107
108 printf(" Binary Object Store (BOS):\n");
Chris Dickens88ded792020-01-13 12:23:02 -0800109 printf(" wTotalLength: %u\n", bos->wTotalLength);
110 printf(" bNumDeviceCaps: %u\n", bos->bNumDeviceCaps);
Ludovic Rousseaua1cf2062017-07-12 16:54:44 +0200111
Chris Dickens88ded792020-01-13 12:23:02 -0800112 for (i = 0; i < bos->bNumDeviceCaps; i++) {
113 struct libusb_bos_dev_capability_descriptor *dev_cap = bos->dev_capability[i];
Ludovic Rousseaua1cf2062017-07-12 16:54:44 +0200114
Chris Dickens88ded792020-01-13 12:23:02 -0800115 if (dev_cap->bDevCapabilityType == LIBUSB_BT_USB_2_0_EXTENSION) {
116 struct libusb_usb_2_0_extension_descriptor *usb_2_0_extension;
Ludovic Rousseaua1cf2062017-07-12 16:54:44 +0200117
Chris Dickens88ded792020-01-13 12:23:02 -0800118 ret = libusb_get_usb_2_0_extension_descriptor(NULL, dev_cap, &usb_2_0_extension);
119 if (ret < 0)
120 return;
Ludovic Rousseaua1cf2062017-07-12 16:54:44 +0200121
Chris Dickens88ded792020-01-13 12:23:02 -0800122 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;
Ludovic Rousseaua1cf2062017-07-12 16:54:44 +0200126
Chris Dickens88ded792020-01-13 12:23:02 -0800127 ret = libusb_get_ss_usb_device_capability_descriptor(NULL, dev_cap, &ss_dev_cap);
128 if (ret < 0)
129 return;
Ludovic Rousseaua1cf2062017-07-12 16:54:44 +0200130
Chris Dickens88ded792020-01-13 12:23:02 -0800131 print_ss_usb_cap(ss_dev_cap);
132 libusb_free_ss_usb_device_capability_descriptor(ss_dev_cap);
133 }
134 }
Ludovic Rousseaua1cf2062017-07-12 16:54:44 +0200135
136 libusb_free_bos_descriptor(bos);
137}
138
139static 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
147static void print_configuration(struct libusb_config_descriptor *config)
148{
Chris Dickensf6925522018-03-24 17:42:46 -0700149 uint8_t i;
Ludovic Rousseaua1cf2062017-07-12 16:54:44 +0200150
151 printf(" Configuration:\n");
Chris Dickens88ded792020-01-13 12:23:02 -0800152 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);
Ludovic Rousseaua1cf2062017-07-12 16:54:44 +0200158
159 for (i = 0; i < config->bNumInterfaces; i++)
160 print_interface(&config->interface[i]);
161}
162
Hans de Goededca3e7e2020-09-07 11:12:16 +0200163static void print_device(libusb_device *dev, libusb_device_handle *handle)
Ludovic Rousseaua1cf2062017-07-12 16:54:44 +0200164{
165 struct libusb_device_descriptor desc;
Chris Dickens9a1bc8c2020-03-30 12:28:11 -0700166 unsigned char string[256];
Hans de Goede1be063f2020-09-07 11:25:54 +0200167 const char *speed;
Chris Dickensf6925522018-03-24 17:42:46 -0700168 int ret;
169 uint8_t i;
Ludovic Rousseaua1cf2062017-07-12 16:54:44 +0200170
Hans de Goede1be063f2020-09-07 11:25:54 +0200171 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 default: speed = "Unknown";
178 }
179
Ludovic Rousseaua1cf2062017-07-12 16:54:44 +0200180 ret = libusb_get_device_descriptor(dev, &desc);
181 if (ret < 0) {
182 fprintf(stderr, "failed to get device descriptor");
Chris Dickens88ded792020-01-13 12:23:02 -0800183 return;
Ludovic Rousseaua1cf2062017-07-12 16:54:44 +0200184 }
185
Hans de Goede1be063f2020-09-07 11:25:54 +0200186 printf("Dev (bus %u, device %u): %04X - %04X speed: %s\n",
Chris Dickenscb95e3a2020-01-13 12:50:40 -0800187 libusb_get_bus_number(dev), libusb_get_device_address(dev),
Hans de Goede1be063f2020-09-07 11:25:54 +0200188 desc.idVendor, desc.idProduct, speed);
Chris Dickens88ded792020-01-13 12:23:02 -0800189
Hans de Goededca3e7e2020-09-07 11:12:16 +0200190 if (!handle)
191 libusb_open(dev, &handle);
192
193 if (handle) {
Chris Dickenscb95e3a2020-01-13 12:50:40 -0800194 if (desc.iManufacturer) {
Ludovic Rousseaua1cf2062017-07-12 16:54:44 +0200195 ret = libusb_get_string_descriptor_ascii(handle, desc.iManufacturer, string, sizeof(string));
Chris Dickenscb95e3a2020-01-13 12:50:40 -0800196 if (ret > 0)
Chris Dickens9a1bc8c2020-03-30 12:28:11 -0700197 printf(" Manufacturer: %s\n", (char *)string);
Chris Dickenscb95e3a2020-01-13 12:50:40 -0800198 }
Ludovic Rousseaua1cf2062017-07-12 16:54:44 +0200199
Chris Dickenscb95e3a2020-01-13 12:50:40 -0800200 if (desc.iProduct) {
Ludovic Rousseaua1cf2062017-07-12 16:54:44 +0200201 ret = libusb_get_string_descriptor_ascii(handle, desc.iProduct, string, sizeof(string));
Chris Dickenscb95e3a2020-01-13 12:50:40 -0800202 if (ret > 0)
Chris Dickens9a1bc8c2020-03-30 12:28:11 -0700203 printf(" Product: %s\n", (char *)string);
Chris Dickenscb95e3a2020-01-13 12:50:40 -0800204 }
Ludovic Rousseaua1cf2062017-07-12 16:54:44 +0200205
Chris Dickenscb95e3a2020-01-13 12:50:40 -0800206 if (desc.iSerialNumber && verbose) {
Ludovic Rousseaua1cf2062017-07-12 16:54:44 +0200207 ret = libusb_get_string_descriptor_ascii(handle, desc.iSerialNumber, string, sizeof(string));
Chris Dickenscb95e3a2020-01-13 12:50:40 -0800208 if (ret > 0)
Chris Dickens9a1bc8c2020-03-30 12:28:11 -0700209 printf(" Serial Number: %s\n", (char *)string);
Chris Dickenscb95e3a2020-01-13 12:50:40 -0800210 }
Ludovic Rousseaua1cf2062017-07-12 16:54:44 +0200211 }
212
213 if (verbose) {
214 for (i = 0; i < desc.bNumConfigurations; i++) {
215 struct libusb_config_descriptor *config;
Chris Dickens88ded792020-01-13 12:23:02 -0800216
Ludovic Rousseaua1cf2062017-07-12 16:54:44 +0200217 ret = libusb_get_config_descriptor(dev, i, &config);
218 if (LIBUSB_SUCCESS != ret) {
219 printf(" Couldn't retrieve descriptors\n");
220 continue;
221 }
222
223 print_configuration(config);
224
225 libusb_free_config_descriptor(config);
226 }
227
Chris Dickens88ded792020-01-13 12:23:02 -0800228 if (handle && desc.bcdUSB >= 0x0201)
Ludovic Rousseaua1cf2062017-07-12 16:54:44 +0200229 print_bos(handle);
Ludovic Rousseaua1cf2062017-07-12 16:54:44 +0200230 }
231
232 if (handle)
233 libusb_close(handle);
Ludovic Rousseaua1cf2062017-07-12 16:54:44 +0200234}
235
Hans de Goededca3e7e2020-09-07 11:12:16 +0200236#ifdef __linux__
237#include <errno.h>
238#include <fcntl.h>
239#include <unistd.h>
240
241static int test_wrapped_device(const char *device_name)
242{
243 libusb_device_handle *handle;
244 int r, fd;
245
246 fd = open(device_name, O_RDWR);
247 if (fd < 0) {
248 printf("Error could not open %s: %s\n", device_name, strerror(errno));
249 return 1;
250 }
251 r = libusb_wrap_sys_device(NULL, fd, &handle);
252 if (r) {
253 printf("Error wrapping device: %s: %s\n", device_name, libusb_strerror(r));
254 close(fd);
255 return 1;
256 }
257 print_device(libusb_get_device(handle), handle);
258 close(fd);
259 return 0;
260}
261#else
262static int test_wrapped_device(const char *device_name)
263{
Chris Dickens15668082020-11-09 15:51:44 -0800264 (void)device_name;
Hans de Goededca3e7e2020-09-07 11:12:16 +0200265 printf("Testing wrapped devices is not supported on your platform\n");
266 return 1;
267}
268#endif
269
Ludovic Rousseaua1cf2062017-07-12 16:54:44 +0200270int main(int argc, char *argv[])
271{
Hans de Goededca3e7e2020-09-07 11:12:16 +0200272 const char *device_name = NULL;
Ludovic Rousseaua1cf2062017-07-12 16:54:44 +0200273 libusb_device **devs;
274 ssize_t cnt;
275 int r, i;
276
Hans de Goededca3e7e2020-09-07 11:12:16 +0200277 for (i = 1; i < argc; i++) {
278 if (!strcmp(argv[i], "-v")) {
279 verbose = 1;
280 } else if (!strcmp(argv[i], "-d") && (i + 1) < argc) {
281 i++;
282 device_name = argv[i];
283 } else {
284 printf("Usage %s [-v] [-d </dev/bus/usb/...>]\n", argv[0]);
285 printf("Note use -d to test libusb_wrap_sys_device()\n");
286 return 1;
287 }
288 }
Ludovic Rousseaua1cf2062017-07-12 16:54:44 +0200289
290 r = libusb_init(NULL);
291 if (r < 0)
292 return r;
293
Hans de Goededca3e7e2020-09-07 11:12:16 +0200294 if (device_name) {
295 r = test_wrapped_device(device_name);
296 } else {
297 cnt = libusb_get_device_list(NULL, &devs);
298 if (cnt < 0) {
299 libusb_exit(NULL);
300 return 1;
301 }
Ludovic Rousseaua1cf2062017-07-12 16:54:44 +0200302
Hans de Goededca3e7e2020-09-07 11:12:16 +0200303 for (i = 0; devs[i]; i++)
304 print_device(devs[i], NULL);
Ludovic Rousseaua1cf2062017-07-12 16:54:44 +0200305
Hans de Goededca3e7e2020-09-07 11:12:16 +0200306 libusb_free_device_list(devs, 1);
307 }
Ludovic Rousseaua1cf2062017-07-12 16:54:44 +0200308
309 libusb_exit(NULL);
Hans de Goededca3e7e2020-09-07 11:12:16 +0200310 return r;
Ludovic Rousseaua1cf2062017-07-12 16:54:44 +0200311}