testlibusb.c 9.2 KB

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