dump.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
  1. /*-
  2. * SPDX-License-Identifier: BSD-2-Clause
  3. *
  4. * Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
  5. * Copyright (c) 2024 Baptiste Daroussin <bapt@FreeBSD.org>
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  17. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  20. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  22. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  23. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  24. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  25. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  26. * SUCH DAMAGE.
  27. */
  28. #include <sys/queue.h>
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <stdint.h>
  32. #include <err.h>
  33. #include <string.h>
  34. #include <pwd.h>
  35. #include <grp.h>
  36. #include <ctype.h>
  37. #include <libusb20.h>
  38. #include <libusb20_desc.h>
  39. #include "dump.h"
  40. #include "pathnames.h"
  41. #define DUMP0(n,type,field,...) dump_field(pdev, " ", #field, n->field);
  42. #define DUMP1(n,type,field,...) dump_field(pdev, " ", #field, n->field);
  43. #define DUMP2(n,type,field,...) dump_field(pdev, " ", #field, n->field);
  44. #define DUMP3(n,type,field,...) dump_field(pdev, " ", #field, n->field);
  45. struct usb_product_info {
  46. STAILQ_ENTRY(usb_product_info) link;
  47. int id;
  48. char *desc;
  49. };
  50. struct usb_vendor_info {
  51. STAILQ_ENTRY(usb_vendor_info) link;
  52. STAILQ_HEAD(,usb_product_info) devs;
  53. int id;
  54. char *desc;
  55. };
  56. STAILQ_HEAD(usb_vendors, usb_vendor_info);
  57. const char *
  58. dump_mode(uint8_t value)
  59. {
  60. if (value == LIBUSB20_MODE_HOST)
  61. return ("HOST");
  62. return ("DEVICE");
  63. }
  64. const char *
  65. dump_speed(uint8_t value)
  66. {
  67. ; /* style fix */
  68. switch (value) {
  69. case LIBUSB20_SPEED_LOW:
  70. return ("LOW (1.5Mbps)");
  71. case LIBUSB20_SPEED_FULL:
  72. return ("FULL (12Mbps)");
  73. case LIBUSB20_SPEED_HIGH:
  74. return ("HIGH (480Mbps)");
  75. case LIBUSB20_SPEED_VARIABLE:
  76. return ("VARIABLE (52-480Mbps)");
  77. case LIBUSB20_SPEED_SUPER:
  78. return ("SUPER (5.0Gbps)");
  79. default:
  80. break;
  81. }
  82. return ("UNKNOWN ()");
  83. }
  84. const char *
  85. dump_power_mode(uint8_t value)
  86. {
  87. ; /* style fix */
  88. switch (value) {
  89. case LIBUSB20_POWER_OFF:
  90. return ("OFF");
  91. case LIBUSB20_POWER_ON:
  92. return ("ON");
  93. case LIBUSB20_POWER_SAVE:
  94. return ("SAVE");
  95. case LIBUSB20_POWER_SUSPEND:
  96. return ("SUSPEND");
  97. case LIBUSB20_POWER_RESUME:
  98. return ("RESUME");
  99. default:
  100. return ("UNKNOWN");
  101. }
  102. }
  103. static void
  104. dump_field(struct libusb20_device *pdev, const char *plevel,
  105. const char *field, uint32_t value)
  106. {
  107. uint8_t temp_string[256];
  108. printf("%s%s = 0x%04x ", plevel, field, value);
  109. if (strlen(plevel) == 8) {
  110. /* Endpoint Descriptor */
  111. if (strcmp(field, "bEndpointAddress") == 0) {
  112. if (value & 0x80)
  113. printf(" <IN>\n");
  114. else
  115. printf(" <OUT>\n");
  116. return;
  117. }
  118. if (strcmp(field, "bmAttributes") == 0) {
  119. switch (value & 0x03) {
  120. case 0:
  121. printf(" <CONTROL>\n");
  122. break;
  123. case 1:
  124. switch (value & 0x0C) {
  125. case 0x00:
  126. printf(" <ISOCHRONOUS>\n");
  127. break;
  128. case 0x04:
  129. printf(" <ASYNC-ISOCHRONOUS>\n");
  130. break;
  131. case 0x08:
  132. printf(" <ADAPT-ISOCHRONOUS>\n");
  133. break;
  134. default:
  135. printf(" <SYNC-ISOCHRONOUS>\n");
  136. break;
  137. }
  138. break;
  139. case 2:
  140. printf(" <BULK>\n");
  141. break;
  142. default:
  143. printf(" <INTERRUPT>\n");
  144. break;
  145. }
  146. return;
  147. }
  148. }
  149. if ((field[0] == 'i') && (field[1] != 'd')) {
  150. /* Indirect String Descriptor */
  151. if (value == 0) {
  152. printf(" <no string>\n");
  153. return;
  154. }
  155. if (libusb20_dev_req_string_simple_sync(pdev, value,
  156. temp_string, sizeof(temp_string))) {
  157. printf(" <retrieving string failed>\n");
  158. return;
  159. }
  160. printf(" <%s>\n", temp_string);
  161. return;
  162. }
  163. if (strlen(plevel) == 2 || strlen(plevel) == 6) {
  164. /* Device and Interface Descriptor class codes */
  165. if (strcmp(field, "bInterfaceClass") == 0 ||
  166. strcmp(field, "bDeviceClass") == 0) {
  167. switch (value) {
  168. case 0x00:
  169. printf(" <Probed by interface class>\n");
  170. break;
  171. case 0x01:
  172. printf(" <Audio device>\n");
  173. break;
  174. case 0x02:
  175. printf(" <Communication device>\n");
  176. break;
  177. case 0x03:
  178. printf(" <HID device>\n");
  179. break;
  180. case 0x05:
  181. printf(" <Physical device>\n");
  182. break;
  183. case 0x06:
  184. printf(" <Still imaging>\n");
  185. break;
  186. case 0x07:
  187. printf(" <Printer device>\n");
  188. break;
  189. case 0x08:
  190. printf(" <Mass storage>\n");
  191. break;
  192. case 0x09:
  193. printf(" <HUB>\n");
  194. break;
  195. case 0x0A:
  196. printf(" <CDC-data>\n");
  197. break;
  198. case 0x0B:
  199. printf(" <Smart card>\n");
  200. break;
  201. case 0x0D:
  202. printf(" <Content security>\n");
  203. break;
  204. case 0x0E:
  205. printf(" <Video device>\n");
  206. break;
  207. case 0x0F:
  208. printf(" <Personal healthcare>\n");
  209. break;
  210. case 0x10:
  211. printf(" <Audio and video device>\n");
  212. break;
  213. case 0x11:
  214. printf(" <Billboard device>\n");
  215. break;
  216. case 0xDC:
  217. printf(" <Diagnostic device>\n");
  218. break;
  219. case 0xE0:
  220. printf(" <Wireless controller>\n");
  221. break;
  222. case 0xEF:
  223. printf(" <Miscellaneous device>\n");
  224. break;
  225. case 0xFE:
  226. printf(" <Application specific>\n");
  227. break;
  228. case 0xFF:
  229. printf(" <Vendor specific>\n");
  230. break;
  231. default:
  232. printf(" <Unknown>\n");
  233. break;
  234. }
  235. return;
  236. }
  237. }
  238. /* No additional information */
  239. printf("\n");
  240. }
  241. static void
  242. dump_extra(struct libusb20_me_struct *str, const char *plevel)
  243. {
  244. const uint8_t *ptr;
  245. uint8_t x;
  246. ptr = NULL;
  247. while ((ptr = libusb20_desc_foreach(str, ptr))) {
  248. printf("\n" "%sAdditional Descriptor\n\n", plevel);
  249. printf("%sbLength = 0x%02x\n", plevel, ptr[0]);
  250. printf("%sbDescriptorType = 0x%02x\n", plevel, ptr[1]);
  251. if (ptr[0] > 1)
  252. printf("%sbDescriptorSubType = 0x%02x\n",
  253. plevel, ptr[2]);
  254. printf("%s RAW dump: ", plevel);
  255. for (x = 0; x != ptr[0]; x++) {
  256. if ((x % 8) == 0) {
  257. printf("\n%s 0x%02x | ", plevel, x);
  258. }
  259. printf("0x%02x%s", ptr[x],
  260. (x != (ptr[0] - 1)) ? ", " : (x % 8) ? "\n" : "");
  261. }
  262. printf("\n");
  263. }
  264. }
  265. static void
  266. dump_endpoint(struct libusb20_device *pdev,
  267. struct libusb20_endpoint *ep)
  268. {
  269. struct LIBUSB20_ENDPOINT_DESC_DECODED *edesc;
  270. edesc = &ep->desc;
  271. LIBUSB20_ENDPOINT_DESC(DUMP3, edesc);
  272. dump_extra(&ep->extra, " " " " " ");
  273. }
  274. static void
  275. dump_iface(struct libusb20_device *pdev,
  276. struct libusb20_interface *iface)
  277. {
  278. struct LIBUSB20_INTERFACE_DESC_DECODED *idesc;
  279. uint8_t z;
  280. idesc = &iface->desc;
  281. LIBUSB20_INTERFACE_DESC(DUMP2, idesc);
  282. dump_extra(&iface->extra, " " " " " ");
  283. for (z = 0; z != iface->num_endpoints; z++) {
  284. printf("\n Endpoint %u\n", z);
  285. dump_endpoint(pdev, iface->endpoints + z);
  286. }
  287. }
  288. static struct usb_vendors *
  289. load_vendors(void)
  290. {
  291. const char *dbf;
  292. FILE *db = NULL;
  293. struct usb_vendor_info *cv;
  294. struct usb_product_info *cd;
  295. struct usb_vendors *usb_vendors;
  296. char buf[1024], str[1024];
  297. char *ch;
  298. int id;
  299. usb_vendors = malloc(sizeof(*usb_vendors));
  300. if (usb_vendors == NULL)
  301. err(1, "out of memory");
  302. STAILQ_INIT(usb_vendors);
  303. if ((dbf = getenv("USB_VENDOR_DATABASE")) != NULL)
  304. db = fopen(dbf, "r");
  305. if (db == NULL) {
  306. dbf = _PATH_LUSBVDB;
  307. if ((db = fopen(dbf, "r")) == NULL) {
  308. dbf = _PATH_USBVDB;
  309. if ((db = fopen(dbf, "r")) == NULL)
  310. return (usb_vendors);
  311. }
  312. }
  313. cv = NULL;
  314. cd = NULL;
  315. for (;;) {
  316. if (fgets(buf, sizeof(buf), db) == NULL)
  317. break;
  318. if ((ch = strchr(buf, '#')) != NULL)
  319. *ch = '\0';
  320. if (ch == buf)
  321. continue;
  322. ch = strchr(buf, '\0') - 1;
  323. while (ch > buf && isspace(*ch))
  324. *ch-- = '\0';
  325. if (ch <= buf)
  326. continue;
  327. /* Can't handle subvendor / subdevice entries yet */
  328. if (buf[0] == '\t' && buf[1] == '\t')
  329. continue;
  330. /* Check for vendor entry */
  331. if (buf[0] != '\t' && sscanf(buf, "%04x %[^\n]", &id, str) == 2) {
  332. if ((id == 0) || (strlen(str) < 1))
  333. continue;
  334. if ((cv = malloc(sizeof(struct usb_vendor_info))) == NULL)
  335. err(1, "out of memory");
  336. if ((cv->desc = strdup(str)) == NULL)
  337. err(1, "out of memory");
  338. cv->id = id;
  339. STAILQ_INIT(&cv->devs);
  340. STAILQ_INSERT_TAIL(usb_vendors, cv, link);
  341. continue;
  342. }
  343. /* Check for device entry */
  344. if (buf[0] == '\t' && sscanf(buf + 1, "%04x %[^\n]", &id, str) == 2) {
  345. if ((id == 0) || (strlen(str) < 1))
  346. continue;
  347. if (cv == NULL)
  348. continue;
  349. if ((cd = malloc(sizeof(struct usb_product_info))) == NULL)
  350. err(1, "out of memory");
  351. if ((cd->desc = strdup(str)) == NULL)
  352. err(1, "out of memory");
  353. cd->id = id;
  354. STAILQ_INSERT_TAIL(&cv->devs, cd, link);
  355. continue;
  356. }
  357. }
  358. if (ferror(db))
  359. err(1, "error reading the usb id db");
  360. fclose(db);
  361. /* cleanup */
  362. return (usb_vendors);
  363. }
  364. static char *
  365. _device_desc(struct libusb20_device *pdev)
  366. {
  367. static struct usb_vendors *usb_vendors = NULL;
  368. char *desc = NULL;
  369. const char *vendor = NULL, *product = NULL;
  370. uint16_t vid = libusb20_dev_get_device_desc(pdev)->idVendor;
  371. uint16_t pid = libusb20_dev_get_device_desc(pdev)->idProduct;
  372. struct usb_vendor_info *vi;
  373. struct usb_product_info *pi;
  374. if (usb_vendors == NULL)
  375. usb_vendors = load_vendors();
  376. STAILQ_FOREACH(vi, usb_vendors, link) {
  377. if (vi->id == vid) {
  378. vendor = vi->desc;
  379. break;
  380. }
  381. }
  382. if (vi != NULL) {
  383. STAILQ_FOREACH(pi, &vi->devs, link) {
  384. if (pi->id == pid) {
  385. product = pi->desc;
  386. break;
  387. }
  388. }
  389. }
  390. if (vendor == NULL || product == NULL)
  391. return (NULL);
  392. asprintf(&desc, "ugen%u.%u: <%s %s> at usbus%u",
  393. libusb20_dev_get_bus_number(pdev),
  394. libusb20_dev_get_address(pdev),
  395. product, vendor,
  396. libusb20_dev_get_bus_number(pdev));
  397. return (desc);
  398. }
  399. void
  400. dump_device_info(struct libusb20_device *pdev, uint8_t show_ifdrv)
  401. {
  402. char buf[128];
  403. uint8_t n;
  404. unsigned int usage;
  405. char *desc;
  406. usage = libusb20_dev_get_power_usage(pdev);
  407. desc = _device_desc(pdev);
  408. printf("%s, cfg=%u md=%s spd=%s pwr=%s (%umA)\n",
  409. desc ? desc : libusb20_dev_get_desc(pdev),
  410. libusb20_dev_get_config_index(pdev),
  411. dump_mode(libusb20_dev_get_mode(pdev)),
  412. dump_speed(libusb20_dev_get_speed(pdev)),
  413. dump_power_mode(libusb20_dev_get_power_mode(pdev)),
  414. usage);
  415. free(desc);
  416. if (!show_ifdrv)
  417. return;
  418. for (n = 0; n != 255; n++) {
  419. if (libusb20_dev_get_iface_desc(pdev, n, buf, sizeof(buf)))
  420. break;
  421. if (buf[0] == 0)
  422. continue;
  423. printf("ugen%u.%u.%u: %s\n",
  424. libusb20_dev_get_bus_number(pdev),
  425. libusb20_dev_get_address(pdev), n, buf);
  426. }
  427. }
  428. void
  429. dump_be_quirk_names(struct libusb20_backend *pbe)
  430. {
  431. struct libusb20_quirk q;
  432. uint16_t x;
  433. int error;
  434. memset(&q, 0, sizeof(q));
  435. printf("\nDumping list of supported quirks:\n\n");
  436. for (x = 0; x != 0xFFFF; x++) {
  437. error = libusb20_be_get_quirk_name(pbe, x, &q);
  438. if (error) {
  439. if (x == 0) {
  440. printf("No quirk names - maybe the USB quirk "
  441. "module has not been loaded.\n");
  442. }
  443. break;
  444. }
  445. if (strcmp(q.quirkname, "UQ_NONE"))
  446. printf("%s\n", q.quirkname);
  447. }
  448. printf("\n");
  449. }
  450. void
  451. dump_be_dev_quirks(struct libusb20_backend *pbe)
  452. {
  453. struct libusb20_quirk q;
  454. uint16_t x;
  455. int error;
  456. memset(&q, 0, sizeof(q));
  457. printf("\nDumping current device quirks:\n\n");
  458. for (x = 0; x != 0xFFFF; x++) {
  459. error = libusb20_be_get_dev_quirk(pbe, x, &q);
  460. if (error) {
  461. if (x == 0) {
  462. printf("No device quirks - maybe the USB quirk "
  463. "module has not been loaded.\n");
  464. }
  465. break;
  466. }
  467. if (strcmp(q.quirkname, "UQ_NONE")) {
  468. printf("VID=0x%04x PID=0x%04x REVLO=0x%04x "
  469. "REVHI=0x%04x QUIRK=%s\n",
  470. q.vid, q.pid, q.bcdDeviceLow,
  471. q.bcdDeviceHigh, q.quirkname);
  472. }
  473. }
  474. printf("\n");
  475. }
  476. void
  477. dump_device_desc(struct libusb20_device *pdev)
  478. {
  479. struct LIBUSB20_DEVICE_DESC_DECODED *ddesc;
  480. ddesc = libusb20_dev_get_device_desc(pdev);
  481. LIBUSB20_DEVICE_DESC(DUMP0, ddesc);
  482. }
  483. void
  484. dump_config(struct libusb20_device *pdev, uint8_t all_cfg)
  485. {
  486. struct LIBUSB20_CONFIG_DESC_DECODED *cdesc;
  487. struct LIBUSB20_DEVICE_DESC_DECODED *ddesc;
  488. struct libusb20_config *pcfg = NULL;
  489. uint8_t cfg_index;
  490. uint8_t cfg_index_end;
  491. uint8_t x;
  492. uint8_t y;
  493. ddesc = libusb20_dev_get_device_desc(pdev);
  494. if (all_cfg) {
  495. cfg_index = 0;
  496. cfg_index_end = ddesc->bNumConfigurations;
  497. } else {
  498. cfg_index = libusb20_dev_get_config_index(pdev);
  499. cfg_index_end = cfg_index + 1;
  500. }
  501. for (; cfg_index != cfg_index_end; cfg_index++) {
  502. pcfg = libusb20_dev_alloc_config(pdev, cfg_index);
  503. if (!pcfg) {
  504. continue;
  505. }
  506. printf("\n Configuration index %u\n\n", cfg_index);
  507. cdesc = &(pcfg->desc);
  508. LIBUSB20_CONFIG_DESC(DUMP1, cdesc);
  509. dump_extra(&(pcfg->extra), " " " ");
  510. for (x = 0; x != pcfg->num_interface; x++) {
  511. printf("\n Interface %u\n", x);
  512. dump_iface(pdev, pcfg->interface + x);
  513. printf("\n");
  514. for (y = 0; y != (pcfg->interface + x)->num_altsetting; y++) {
  515. printf("\n Interface %u Alt %u\n", x, y + 1);
  516. dump_iface(pdev,
  517. (pcfg->interface + x)->altsetting + y);
  518. printf("\n");
  519. }
  520. }
  521. printf("\n");
  522. free(pcfg);
  523. }
  524. }
  525. void
  526. dump_string_by_index(struct libusb20_device *pdev, uint8_t str_index)
  527. {
  528. char *pbuf;
  529. uint8_t n;
  530. uint8_t len;
  531. pbuf = malloc(256);
  532. if (pbuf == NULL)
  533. err(1, "out of memory");
  534. if (str_index == 0) {
  535. /* language table */
  536. if (libusb20_dev_req_string_sync(pdev,
  537. str_index, 0, pbuf, 256)) {
  538. printf("STRING_0x%02x = <read error>\n", str_index);
  539. } else {
  540. printf("STRING_0x%02x = ", str_index);
  541. len = (uint8_t)pbuf[0];
  542. for (n = 0; n != len; n++) {
  543. printf("0x%02x%s", (uint8_t)pbuf[n],
  544. (n != (len - 1)) ? ", " : "");
  545. }
  546. printf("\n");
  547. }
  548. } else {
  549. /* ordinary string */
  550. if (libusb20_dev_req_string_simple_sync(pdev,
  551. str_index, pbuf, 256)) {
  552. printf("STRING_0x%02x = <read error>\n", str_index);
  553. } else {
  554. printf("STRING_0x%02x = <%s>\n", str_index, pbuf);
  555. }
  556. }
  557. free(pbuf);
  558. }
  559. void
  560. dump_device_stats(struct libusb20_device *pdev)
  561. {
  562. struct libusb20_device_stats st;
  563. if (libusb20_dev_get_stats(pdev, &st)) {
  564. printf("{}\n");
  565. } else {
  566. printf("{\n"
  567. " UE_CONTROL_OK : %llu\n"
  568. " UE_ISOCHRONOUS_OK : %llu\n"
  569. " UE_BULK_OK : %llu\n"
  570. " UE_INTERRUPT_OK : %llu\n"
  571. " UE_CONTROL_FAIL : %llu\n"
  572. " UE_ISOCHRONOUS_FAIL : %llu\n"
  573. " UE_BULK_FAIL : %llu\n"
  574. " UE_INTERRUPT_FAIL : %llu\n"
  575. "}\n",
  576. (unsigned long long)st.xfer_ok[0],
  577. (unsigned long long)st.xfer_ok[1],
  578. (unsigned long long)st.xfer_ok[2],
  579. (unsigned long long)st.xfer_ok[3],
  580. (unsigned long long)st.xfer_fail[0],
  581. (unsigned long long)st.xfer_fail[1],
  582. (unsigned long long)st.xfer_fail[2],
  583. (unsigned long long)st.xfer_fail[3]);
  584. }
  585. }