usbtest.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /* usbtest.c - test module for USB */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2008 Free Software Foundation, Inc.
  5. *
  6. * GRUB is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * GRUB is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <grub/types.h>
  20. #include <grub/misc.h>
  21. #include <grub/charset.h>
  22. #include <grub/mm.h>
  23. #include <grub/err.h>
  24. #include <grub/dl.h>
  25. #include <grub/usb.h>
  26. #include <grub/command.h>
  27. #include <grub/i18n.h>
  28. GRUB_MOD_LICENSE ("GPLv3+");
  29. static const char *usb_classes[] =
  30. {
  31. "Unknown",
  32. "Audio",
  33. "Communication Interface",
  34. "HID",
  35. "Unknown",
  36. "Physical",
  37. "Image",
  38. "Printer",
  39. "Mass Storage",
  40. "Hub",
  41. "Data Interface",
  42. "Smart Card",
  43. "Content Security",
  44. "Video"
  45. };
  46. static const char *usb_endp_type[] =
  47. {
  48. "Control",
  49. "Isochronous",
  50. "Bulk",
  51. "Interrupt"
  52. };
  53. static const char *usb_devspeed[] =
  54. {
  55. "",
  56. "Low",
  57. "Full",
  58. "High"
  59. };
  60. static grub_usb_err_t
  61. grub_usb_get_string (grub_usb_device_t dev, grub_uint8_t index, int langid,
  62. char **string)
  63. {
  64. struct grub_usb_desc_str descstr;
  65. struct grub_usb_desc_str *descstrp;
  66. grub_usb_err_t err;
  67. /* Only get the length. */
  68. err = grub_usb_control_msg (dev, 1 << 7,
  69. 0x06, (3 << 8) | index,
  70. langid, 1, (char *) &descstr);
  71. if (err)
  72. return err;
  73. descstrp = grub_malloc (descstr.length);
  74. if (! descstrp)
  75. return GRUB_USB_ERR_INTERNAL;
  76. err = grub_usb_control_msg (dev, 1 << 7,
  77. 0x06, (3 << 8) | index,
  78. langid, descstr.length, (char *) descstrp);
  79. if (descstrp->length == 0)
  80. {
  81. grub_free (descstrp);
  82. *string = grub_strdup ("");
  83. if (! *string)
  84. return GRUB_USB_ERR_INTERNAL;
  85. return GRUB_USB_ERR_NONE;
  86. }
  87. *string = grub_malloc (descstr.length * 2 + 1);
  88. if (! *string)
  89. {
  90. grub_free (descstrp);
  91. return GRUB_USB_ERR_INTERNAL;
  92. }
  93. *grub_utf16_to_utf8 ((grub_uint8_t *) *string, descstrp->str,
  94. descstrp->length / 2 - 1) = 0;
  95. grub_free (descstrp);
  96. return GRUB_USB_ERR_NONE;
  97. }
  98. static void
  99. usb_print_str (const char *description, grub_usb_device_t dev, int idx)
  100. {
  101. char *name = NULL;
  102. grub_usb_err_t err;
  103. /* XXX: LANGID */
  104. if (! idx)
  105. return;
  106. err = grub_usb_get_string (dev, idx, 0x0409, &name);
  107. if (err)
  108. grub_printf ("Error %d retrieving %s\n", err, description);
  109. else
  110. {
  111. grub_printf ("%s: `%s'\n", description, name);
  112. grub_free (name);
  113. }
  114. }
  115. static int
  116. usb_iterate (grub_usb_device_t dev, void *data __attribute__ ((unused)))
  117. {
  118. struct grub_usb_desc_device *descdev;
  119. int i;
  120. descdev = &dev->descdev;
  121. usb_print_str ("Product", dev, descdev->strprod);
  122. usb_print_str ("Vendor", dev, descdev->strvendor);
  123. usb_print_str ("Serial", dev, descdev->strserial);
  124. grub_printf ("Class: (0x%02x) %s, Subclass: 0x%02x, Protocol: 0x%02x\n",
  125. descdev->class, descdev->class < ARRAY_SIZE (usb_classes)
  126. ? usb_classes[descdev->class] : "Unknown",
  127. descdev->subclass, descdev->protocol);
  128. grub_printf ("USB version %d.%d, VendorID: 0x%02x, ProductID: 0x%02x, #conf: %d\n",
  129. descdev->usbrel >> 8, (descdev->usbrel >> 4) & 0x0F,
  130. descdev->vendorid, descdev->prodid, descdev->configcnt);
  131. grub_printf ("%s speed device\n", usb_devspeed[dev->speed]);
  132. for (i = 0; i < descdev->configcnt; i++)
  133. {
  134. struct grub_usb_desc_config *config;
  135. config = dev->config[i].descconf;
  136. usb_print_str ("Configuration:", dev, config->strconfig);
  137. }
  138. for (i = 0; i < dev->config[0].descconf->numif; i++)
  139. {
  140. int j;
  141. struct grub_usb_desc_if *interf;
  142. interf = dev->config[0].interf[i].descif;
  143. grub_printf ("Interface #%d: #Endpoints: %d ",
  144. i, interf->endpointcnt);
  145. grub_printf ("Class: (0x%02x) %s, Subclass: 0x%02x, Protocol: 0x%02x\n",
  146. interf->class, interf->class < ARRAY_SIZE (usb_classes)
  147. ? usb_classes[interf->class] : "Unknown",
  148. interf->subclass, interf->protocol);
  149. usb_print_str ("Interface", dev, interf->strif);
  150. for (j = 0; j < interf->endpointcnt; j++)
  151. {
  152. struct grub_usb_desc_endp *endp;
  153. endp = &dev->config[0].interf[i].descendp[j];
  154. grub_printf ("Endpoint #%d: %s, max packed size: %d, transfer type: %s, latency: %d\n",
  155. endp->endp_addr & 15,
  156. (endp->endp_addr & 128) ? "IN" : "OUT",
  157. endp->maxpacket, usb_endp_type[endp->attrib & 3],
  158. endp->interval);
  159. }
  160. }
  161. grub_printf("\n");
  162. return 0;
  163. }
  164. static grub_err_t
  165. grub_cmd_usbtest (grub_command_t cmd __attribute__ ((unused)),
  166. int argc __attribute__ ((unused)),
  167. char **args __attribute__ ((unused)))
  168. {
  169. grub_usb_poll_devices (1);
  170. grub_printf ("USB devices:\n\n");
  171. grub_usb_iterate (usb_iterate, NULL);
  172. return 0;
  173. }
  174. static grub_command_t cmd;
  175. GRUB_MOD_INIT(usbtest)
  176. {
  177. cmd = grub_register_command ("usb", grub_cmd_usbtest,
  178. 0, N_("Test USB support."));
  179. }
  180. GRUB_MOD_FINI(usbtest)
  181. {
  182. grub_unregister_command (cmd);
  183. }