Ludovic Rousseau | a1cf206 | 2017-07-12 16:54:44 +0200 | [diff] [blame] | 1 | /* |
| 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 | |
| 24 | int verbose = 0; |
| 25 | |
| 26 | static void print_endpoint_comp(const struct libusb_ss_endpoint_companion_descriptor *ep_comp) |
| 27 | { |
| 28 | printf(" USB 3.0 Endpoint Companion:\n"); |
Chris Dickens | 88ded79 | 2020-01-13 12:23:02 -0800 | [diff] [blame] | 29 | printf(" bMaxBurst: %u\n", ep_comp->bMaxBurst); |
| 30 | printf(" bmAttributes: %02xh\n", ep_comp->bmAttributes); |
| 31 | printf(" wBytesPerInterval: %u\n", ep_comp->wBytesPerInterval); |
Ludovic Rousseau | a1cf206 | 2017-07-12 16:54:44 +0200 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | static void print_endpoint(const struct libusb_endpoint_descriptor *endpoint) |
| 35 | { |
| 36 | int i, ret; |
| 37 | |
| 38 | printf(" Endpoint:\n"); |
Chris Dickens | 88ded79 | 2020-01-13 12:23:02 -0800 | [diff] [blame] | 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); |
Ludovic Rousseau | a1cf206 | 2017-07-12 16:54:44 +0200 | [diff] [blame] | 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); |
Chris Dickens | 88ded79 | 2020-01-13 12:23:02 -0800 | [diff] [blame] | 51 | if (LIBUSB_SUCCESS != ret) |
Ludovic Rousseau | a1cf206 | 2017-07-12 16:54:44 +0200 | [diff] [blame] | 52 | continue; |
Ludovic Rousseau | a1cf206 | 2017-07-12 16:54:44 +0200 | [diff] [blame] | 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 | |
| 63 | static void print_altsetting(const struct libusb_interface_descriptor *interface) |
| 64 | { |
Chris Dickens | f692552 | 2018-03-24 17:42:46 -0700 | [diff] [blame] | 65 | uint8_t i; |
Ludovic Rousseau | a1cf206 | 2017-07-12 16:54:44 +0200 | [diff] [blame] | 66 | |
| 67 | printf(" Interface:\n"); |
Chris Dickens | 88ded79 | 2020-01-13 12:23:02 -0800 | [diff] [blame] | 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); |
Ludovic Rousseau | a1cf206 | 2017-07-12 16:54:44 +0200 | [diff] [blame] | 75 | |
| 76 | for (i = 0; i < interface->bNumEndpoints; i++) |
| 77 | print_endpoint(&interface->endpoint[i]); |
| 78 | } |
| 79 | |
| 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"); |
Chris Dickens | 88ded79 | 2020-01-13 12:23:02 -0800 | [diff] [blame] | 83 | printf(" bDevCapabilityType: %u\n", usb_2_0_ext_cap->bDevCapabilityType); |
| 84 | printf(" bmAttributes: %08xh\n", usb_2_0_ext_cap->bmAttributes); |
Ludovic Rousseau | a1cf206 | 2017-07-12 16:54:44 +0200 | [diff] [blame] | 85 | } |
| 86 | |
| 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"); |
Chris Dickens | 88ded79 | 2020-01-13 12:23:02 -0800 | [diff] [blame] | 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); |
Ludovic Rousseau | a1cf206 | 2017-07-12 16:54:44 +0200 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | static void print_bos(libusb_device_handle *handle) |
| 99 | { |
| 100 | struct libusb_bos_descriptor *bos; |
Chris Dickens | 88ded79 | 2020-01-13 12:23:02 -0800 | [diff] [blame] | 101 | uint8_t i; |
Ludovic Rousseau | a1cf206 | 2017-07-12 16:54:44 +0200 | [diff] [blame] | 102 | int ret; |
| 103 | |
| 104 | ret = libusb_get_bos_descriptor(handle, &bos); |
Chris Dickens | 88ded79 | 2020-01-13 12:23:02 -0800 | [diff] [blame] | 105 | if (ret < 0) |
Ludovic Rousseau | a1cf206 | 2017-07-12 16:54:44 +0200 | [diff] [blame] | 106 | return; |
Ludovic Rousseau | a1cf206 | 2017-07-12 16:54:44 +0200 | [diff] [blame] | 107 | |
| 108 | printf(" Binary Object Store (BOS):\n"); |
Chris Dickens | 88ded79 | 2020-01-13 12:23:02 -0800 | [diff] [blame] | 109 | printf(" wTotalLength: %u\n", bos->wTotalLength); |
| 110 | printf(" bNumDeviceCaps: %u\n", bos->bNumDeviceCaps); |
Ludovic Rousseau | a1cf206 | 2017-07-12 16:54:44 +0200 | [diff] [blame] | 111 | |
Chris Dickens | 88ded79 | 2020-01-13 12:23:02 -0800 | [diff] [blame] | 112 | for (i = 0; i < bos->bNumDeviceCaps; i++) { |
| 113 | struct libusb_bos_dev_capability_descriptor *dev_cap = bos->dev_capability[i]; |
Ludovic Rousseau | a1cf206 | 2017-07-12 16:54:44 +0200 | [diff] [blame] | 114 | |
Chris Dickens | 88ded79 | 2020-01-13 12:23:02 -0800 | [diff] [blame] | 115 | if (dev_cap->bDevCapabilityType == LIBUSB_BT_USB_2_0_EXTENSION) { |
| 116 | struct libusb_usb_2_0_extension_descriptor *usb_2_0_extension; |
Ludovic Rousseau | a1cf206 | 2017-07-12 16:54:44 +0200 | [diff] [blame] | 117 | |
Chris Dickens | 88ded79 | 2020-01-13 12:23:02 -0800 | [diff] [blame] | 118 | ret = libusb_get_usb_2_0_extension_descriptor(NULL, dev_cap, &usb_2_0_extension); |
| 119 | if (ret < 0) |
| 120 | return; |
Ludovic Rousseau | a1cf206 | 2017-07-12 16:54:44 +0200 | [diff] [blame] | 121 | |
Chris Dickens | 88ded79 | 2020-01-13 12:23:02 -0800 | [diff] [blame] | 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; |
Ludovic Rousseau | a1cf206 | 2017-07-12 16:54:44 +0200 | [diff] [blame] | 126 | |
Chris Dickens | 88ded79 | 2020-01-13 12:23:02 -0800 | [diff] [blame] | 127 | ret = libusb_get_ss_usb_device_capability_descriptor(NULL, dev_cap, &ss_dev_cap); |
| 128 | if (ret < 0) |
| 129 | return; |
Ludovic Rousseau | a1cf206 | 2017-07-12 16:54:44 +0200 | [diff] [blame] | 130 | |
Chris Dickens | 88ded79 | 2020-01-13 12:23:02 -0800 | [diff] [blame] | 131 | print_ss_usb_cap(ss_dev_cap); |
| 132 | libusb_free_ss_usb_device_capability_descriptor(ss_dev_cap); |
| 133 | } |
| 134 | } |
Ludovic Rousseau | a1cf206 | 2017-07-12 16:54:44 +0200 | [diff] [blame] | 135 | |
| 136 | libusb_free_bos_descriptor(bos); |
| 137 | } |
| 138 | |
| 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 | |
| 147 | static void print_configuration(struct libusb_config_descriptor *config) |
| 148 | { |
Chris Dickens | f692552 | 2018-03-24 17:42:46 -0700 | [diff] [blame] | 149 | uint8_t i; |
Ludovic Rousseau | a1cf206 | 2017-07-12 16:54:44 +0200 | [diff] [blame] | 150 | |
| 151 | printf(" Configuration:\n"); |
Chris Dickens | 88ded79 | 2020-01-13 12:23:02 -0800 | [diff] [blame] | 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); |
Ludovic Rousseau | a1cf206 | 2017-07-12 16:54:44 +0200 | [diff] [blame] | 158 | |
| 159 | for (i = 0; i < config->bNumInterfaces; i++) |
| 160 | print_interface(&config->interface[i]); |
| 161 | } |
| 162 | |
Chris Dickens | 88ded79 | 2020-01-13 12:23:02 -0800 | [diff] [blame] | 163 | static void print_device(libusb_device *dev) |
Ludovic Rousseau | a1cf206 | 2017-07-12 16:54:44 +0200 | [diff] [blame] | 164 | { |
| 165 | struct libusb_device_descriptor desc; |
| 166 | libusb_device_handle *handle = NULL; |
Chris Dickens | 9a1bc8c | 2020-03-30 12:28:11 -0700 | [diff] [blame] | 167 | unsigned char string[256]; |
Chris Dickens | f692552 | 2018-03-24 17:42:46 -0700 | [diff] [blame] | 168 | int ret; |
| 169 | uint8_t i; |
Ludovic Rousseau | a1cf206 | 2017-07-12 16:54:44 +0200 | [diff] [blame] | 170 | |
| 171 | ret = libusb_get_device_descriptor(dev, &desc); |
| 172 | if (ret < 0) { |
| 173 | fprintf(stderr, "failed to get device descriptor"); |
Chris Dickens | 88ded79 | 2020-01-13 12:23:02 -0800 | [diff] [blame] | 174 | return; |
Ludovic Rousseau | a1cf206 | 2017-07-12 16:54:44 +0200 | [diff] [blame] | 175 | } |
| 176 | |
Chris Dickens | cb95e3a | 2020-01-13 12:50:40 -0800 | [diff] [blame] | 177 | printf("Dev (bus %u, device %u): %04X - %04X\n", |
| 178 | libusb_get_bus_number(dev), libusb_get_device_address(dev), |
| 179 | desc.idVendor, desc.idProduct); |
Chris Dickens | 88ded79 | 2020-01-13 12:23:02 -0800 | [diff] [blame] | 180 | |
Ludovic Rousseau | a1cf206 | 2017-07-12 16:54:44 +0200 | [diff] [blame] | 181 | ret = libusb_open(dev, &handle); |
| 182 | if (LIBUSB_SUCCESS == ret) { |
Chris Dickens | cb95e3a | 2020-01-13 12:50:40 -0800 | [diff] [blame] | 183 | if (desc.iManufacturer) { |
Ludovic Rousseau | a1cf206 | 2017-07-12 16:54:44 +0200 | [diff] [blame] | 184 | ret = libusb_get_string_descriptor_ascii(handle, desc.iManufacturer, string, sizeof(string)); |
Chris Dickens | cb95e3a | 2020-01-13 12:50:40 -0800 | [diff] [blame] | 185 | if (ret > 0) |
Chris Dickens | 9a1bc8c | 2020-03-30 12:28:11 -0700 | [diff] [blame] | 186 | printf(" Manufacturer: %s\n", (char *)string); |
Chris Dickens | cb95e3a | 2020-01-13 12:50:40 -0800 | [diff] [blame] | 187 | } |
Ludovic Rousseau | a1cf206 | 2017-07-12 16:54:44 +0200 | [diff] [blame] | 188 | |
Chris Dickens | cb95e3a | 2020-01-13 12:50:40 -0800 | [diff] [blame] | 189 | if (desc.iProduct) { |
Ludovic Rousseau | a1cf206 | 2017-07-12 16:54:44 +0200 | [diff] [blame] | 190 | ret = libusb_get_string_descriptor_ascii(handle, desc.iProduct, string, sizeof(string)); |
Chris Dickens | cb95e3a | 2020-01-13 12:50:40 -0800 | [diff] [blame] | 191 | if (ret > 0) |
Chris Dickens | 9a1bc8c | 2020-03-30 12:28:11 -0700 | [diff] [blame] | 192 | printf(" Product: %s\n", (char *)string); |
Chris Dickens | cb95e3a | 2020-01-13 12:50:40 -0800 | [diff] [blame] | 193 | } |
Ludovic Rousseau | a1cf206 | 2017-07-12 16:54:44 +0200 | [diff] [blame] | 194 | |
Chris Dickens | cb95e3a | 2020-01-13 12:50:40 -0800 | [diff] [blame] | 195 | if (desc.iSerialNumber && verbose) { |
Ludovic Rousseau | a1cf206 | 2017-07-12 16:54:44 +0200 | [diff] [blame] | 196 | ret = libusb_get_string_descriptor_ascii(handle, desc.iSerialNumber, string, sizeof(string)); |
Chris Dickens | cb95e3a | 2020-01-13 12:50:40 -0800 | [diff] [blame] | 197 | if (ret > 0) |
Chris Dickens | 9a1bc8c | 2020-03-30 12:28:11 -0700 | [diff] [blame] | 198 | printf(" Serial Number: %s\n", (char *)string); |
Chris Dickens | cb95e3a | 2020-01-13 12:50:40 -0800 | [diff] [blame] | 199 | } |
Ludovic Rousseau | a1cf206 | 2017-07-12 16:54:44 +0200 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | if (verbose) { |
| 203 | for (i = 0; i < desc.bNumConfigurations; i++) { |
| 204 | struct libusb_config_descriptor *config; |
Chris Dickens | 88ded79 | 2020-01-13 12:23:02 -0800 | [diff] [blame] | 205 | |
Ludovic Rousseau | a1cf206 | 2017-07-12 16:54:44 +0200 | [diff] [blame] | 206 | ret = libusb_get_config_descriptor(dev, i, &config); |
| 207 | if (LIBUSB_SUCCESS != ret) { |
| 208 | printf(" Couldn't retrieve descriptors\n"); |
| 209 | continue; |
| 210 | } |
| 211 | |
| 212 | print_configuration(config); |
| 213 | |
| 214 | libusb_free_config_descriptor(config); |
| 215 | } |
| 216 | |
Chris Dickens | 88ded79 | 2020-01-13 12:23:02 -0800 | [diff] [blame] | 217 | if (handle && desc.bcdUSB >= 0x0201) |
Ludovic Rousseau | a1cf206 | 2017-07-12 16:54:44 +0200 | [diff] [blame] | 218 | print_bos(handle); |
Ludovic Rousseau | a1cf206 | 2017-07-12 16:54:44 +0200 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | if (handle) |
| 222 | libusb_close(handle); |
Ludovic Rousseau | a1cf206 | 2017-07-12 16:54:44 +0200 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | int main(int argc, char *argv[]) |
| 226 | { |
| 227 | libusb_device **devs; |
| 228 | ssize_t cnt; |
| 229 | int r, i; |
| 230 | |
| 231 | if (argc > 1 && !strcmp(argv[1], "-v")) |
| 232 | verbose = 1; |
| 233 | |
| 234 | r = libusb_init(NULL); |
| 235 | if (r < 0) |
| 236 | return r; |
| 237 | |
| 238 | cnt = libusb_get_device_list(NULL, &devs); |
| 239 | if (cnt < 0) |
| 240 | return (int)cnt; |
| 241 | |
Chris Dickens | 88ded79 | 2020-01-13 12:23:02 -0800 | [diff] [blame] | 242 | for (i = 0; devs[i]; i++) |
| 243 | print_device(devs[i]); |
Ludovic Rousseau | a1cf206 | 2017-07-12 16:54:44 +0200 | [diff] [blame] | 244 | |
| 245 | libusb_free_device_list(devs, 1); |
| 246 | |
| 247 | libusb_exit(NULL); |
| 248 | return 0; |
| 249 | } |