testlibusb1.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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: %d\n", ep_comp->bMaxBurst);
  27. printf(" bmAttributes: 0x%02x\n", ep_comp->bmAttributes);
  28. printf(" wBytesPerInterval: %d\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: %d\n", endpoint->wMaxPacketSize);
  37. printf(" bInterval: %d\n", endpoint->bInterval);
  38. printf(" bRefresh: %d\n", endpoint->bRefresh);
  39. printf(" bSynchAddress: %d\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_parse_ss_endpoint_comp(endpoint->extra+i, endpoint->extra[0], &ep_comp);
  44. if (LIBUSB_SUCCESS != ret) {
  45. continue;
  46. }
  47. print_endpoint_comp(ep_comp);
  48. libusb_free_ss_endpoint_comp(ep_comp);
  49. }
  50. i += endpoint->extra[i];
  51. }
  52. }
  53. static void print_altsetting(const struct libusb_interface_descriptor *interface)
  54. {
  55. int i;
  56. printf(" Interface:\n");
  57. printf(" bInterfaceNumber: %d\n", interface->bInterfaceNumber);
  58. printf(" bAlternateSetting: %d\n", interface->bAlternateSetting);
  59. printf(" bNumEndpoints: %d\n", interface->bNumEndpoints);
  60. printf(" bInterfaceClass: %d\n", interface->bInterfaceClass);
  61. printf(" bInterfaceSubClass: %d\n", interface->bInterfaceSubClass);
  62. printf(" bInterfaceProtocol: %d\n", interface->bInterfaceProtocol);
  63. printf(" iInterface: %d\n", interface->iInterface);
  64. for (i = 0; i < interface->bNumEndpoints; i++)
  65. print_endpoint(&interface->endpoint[i]);
  66. }
  67. static void print_2_0_ext_cap(struct libusb_usb_2_0_device_capability_descriptor *usb_2_0_ext_cap)
  68. {
  69. printf(" USB 2.0 Extension Capabilities:\n");
  70. printf(" bDevCapabilityType: %d\n", usb_2_0_ext_cap->bDevCapabilityType);
  71. printf(" bmAttributes: 0x%x\n", usb_2_0_ext_cap->bmAttributes);
  72. }
  73. static void print_ss_usb_cap(struct libusb_ss_usb_device_capability_descriptor *ss_usb_cap)
  74. {
  75. printf(" USB 3.0 Capabilities:\n");
  76. printf(" bDevCapabilityType: %d\n", ss_usb_cap->bDevCapabilityType);
  77. printf(" bmAttributes: 0x%x\n", ss_usb_cap->bmAttributes);
  78. printf(" wSpeedSupported: 0x%x\n", ss_usb_cap->wSpeedSupported);
  79. printf(" bFunctionalitySupport: %d\n", ss_usb_cap->bFunctionalitySupport);
  80. printf(" bU1devExitLat: %d\n", ss_usb_cap->bU1DevExitLat);
  81. printf(" bU2devExitLat: %d\n", ss_usb_cap->bU2DevExitLat);
  82. }
  83. static void print_bos(libusb_device_handle *handle)
  84. {
  85. unsigned char buffer[128];
  86. struct libusb_bos_descriptor *bos;
  87. int ret;
  88. ret = libusb_get_descriptor(handle, LIBUSB_DT_BOS, 0, buffer, 128);
  89. if (0 > ret) {
  90. return;
  91. }
  92. ret = libusb_parse_bos_descriptor(buffer, 128, &bos);
  93. if (0 > ret) {
  94. return;
  95. }
  96. printf(" Binary Object Store (BOS):\n");
  97. printf(" wTotalLength: %d\n", bos->wTotalLength);
  98. printf(" bNumDeviceCaps: %d\n", bos->bNumDeviceCaps);
  99. if (bos->usb_2_0_ext_cap) {
  100. print_2_0_ext_cap(bos->usb_2_0_ext_cap);
  101. }
  102. if (bos->ss_usb_cap) {
  103. print_ss_usb_cap(bos->ss_usb_cap);
  104. }
  105. }
  106. static void print_interface(const struct libusb_interface *interface)
  107. {
  108. int i;
  109. for (i = 0; i < interface->num_altsetting; i++)
  110. print_altsetting(&interface->altsetting[i]);
  111. }
  112. static void print_configuration(struct libusb_config_descriptor *config)
  113. {
  114. int i;
  115. printf(" Configuration:\n");
  116. printf(" wTotalLength: %d\n", config->wTotalLength);
  117. printf(" bNumInterfaces: %d\n", config->bNumInterfaces);
  118. printf(" bConfigurationValue: %d\n", config->bConfigurationValue);
  119. printf(" iConfiguration: %d\n", config->iConfiguration);
  120. printf(" bmAttributes: %02xh\n", config->bmAttributes);
  121. printf(" MaxPower: %d\n", config->MaxPower);
  122. for (i = 0; i < config->bNumInterfaces; i++)
  123. print_interface(&config->interface[i]);
  124. }
  125. static int print_device(libusb_device *dev, int level)
  126. {
  127. struct libusb_device_descriptor desc;
  128. libusb_device_handle *handle = NULL;
  129. char description[256];
  130. char string[256];
  131. int ret, i;
  132. ret = libusb_get_device_descriptor(dev, &desc);
  133. if (ret < 0) {
  134. fprintf(stderr, "failed to get device descriptor");
  135. return -1;
  136. }
  137. ret = libusb_open(dev, &handle);
  138. if (LIBUSB_SUCCESS == ret) {
  139. if (desc.iManufacturer) {
  140. ret = libusb_get_string_descriptor_ascii(handle, desc.iManufacturer, string, sizeof(string));
  141. if (ret > 0)
  142. snprintf(description, sizeof(description), "%s - ", string);
  143. else
  144. snprintf(description, sizeof(description), "%04X - ",
  145. desc.idVendor);
  146. } else
  147. snprintf(description, sizeof(description), "%04X - ",
  148. desc.idVendor);
  149. if (desc.iProduct) {
  150. ret = libusb_get_string_descriptor_ascii(handle, desc.iProduct, string, sizeof(string));
  151. if (ret > 0)
  152. snprintf(description + strlen(description), sizeof(description) -
  153. strlen(description), "%s", string);
  154. else
  155. snprintf(description + strlen(description), sizeof(description) -
  156. strlen(description), "%04X", desc.idProduct);
  157. } else
  158. snprintf(description + strlen(description), sizeof(description) -
  159. strlen(description), "%04X", desc.idProduct);
  160. } else {
  161. snprintf(description, sizeof(description), "%04X - %04X",
  162. desc.idVendor, desc.idProduct);
  163. }
  164. printf("%.*sDev (bus %d, device %d): %s\n", level * 2, " ",
  165. libusb_get_bus_number(dev), libusb_get_device_address(dev), description);
  166. if (handle && verbose) {
  167. if (desc.iSerialNumber) {
  168. ret = libusb_get_string_descriptor_ascii(handle, desc.iSerialNumber, string, sizeof(string));
  169. if (ret > 0)
  170. printf("%.*s - Serial Number: %s\n", level * 2,
  171. " ", 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. }
  189. if (handle)
  190. libusb_close(handle);
  191. return 0;
  192. }
  193. int main(int argc, char *argv[])
  194. {
  195. libusb_device **devs;
  196. ssize_t cnt;
  197. int r, i;
  198. if (argc > 1 && !strcmp(argv[1], "-v"))
  199. verbose = 1;
  200. r = libusb_init(NULL);
  201. if (r < 0)
  202. return r;
  203. cnt = libusb_get_device_list(NULL, &devs);
  204. if (cnt < 0)
  205. return (int) cnt;
  206. for (i = 0 ; devs[i] ; ++i) {
  207. print_device(devs[i], 0);
  208. }
  209. libusb_free_device_list(devs, 1);
  210. libusb_exit(NULL);
  211. return 0;
  212. }