usbtest.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. static const char *usb_classes[] =
  29. {
  30. "",
  31. "Audio",
  32. "Communication Interface",
  33. "HID",
  34. "",
  35. "Physical",
  36. "Image",
  37. "Printer",
  38. "Mass Storage",
  39. "Hub",
  40. "Data Interface",
  41. "Smart Card",
  42. "Content Security",
  43. "Video"
  44. };
  45. static const char *usb_endp_type[] =
  46. {
  47. "Control",
  48. "Isochronous",
  49. "Bulk",
  50. "Interrupt"
  51. };
  52. static const char *usb_devspeed[] =
  53. {
  54. "",
  55. "Low",
  56. "Full",
  57. "High"
  58. };
  59. static grub_usb_err_t
  60. grub_usb_get_string (grub_usb_device_t dev, grub_uint8_t index, int langid,
  61. char **string)
  62. {
  63. struct grub_usb_desc_str descstr;
  64. struct grub_usb_desc_str *descstrp;
  65. grub_usb_err_t err;
  66. /* Only get the length. */
  67. err = grub_usb_control_msg (dev, 1 << 7,
  68. 0x06, (3 << 8) | index,
  69. langid, 1, (char *) &descstr);
  70. if (err)
  71. return err;
  72. descstrp = grub_malloc (descstr.length);
  73. if (! descstrp)
  74. return GRUB_USB_ERR_INTERNAL;
  75. err = grub_usb_control_msg (dev, 1 << 7,
  76. 0x06, (3 << 8) | index,
  77. langid, descstr.length, (char *) descstrp);
  78. *string = grub_malloc (descstr.length / 2);
  79. if (! *string)
  80. {
  81. grub_free (descstrp);
  82. return GRUB_USB_ERR_INTERNAL;
  83. }
  84. grub_utf16_to_utf8 ((grub_uint8_t *) *string, descstrp->str, descstrp->length / 2 - 1);
  85. (*string)[descstr.length / 2 - 1] = '\0';
  86. grub_free (descstrp);
  87. return GRUB_USB_ERR_NONE;
  88. }
  89. static void
  90. usb_print_str (const char *description, grub_usb_device_t dev, int idx)
  91. {
  92. char *name;
  93. grub_usb_err_t err;
  94. /* XXX: LANGID */
  95. if (! idx)
  96. return;
  97. err = grub_usb_get_string (dev, idx, 0x0409, &name);
  98. if (err)
  99. grub_printf ("Error %d retrieving %s\n", err, description);
  100. else
  101. {
  102. grub_printf ("%s: `%s'\n", description, name);
  103. grub_free (name);
  104. }
  105. }
  106. static int
  107. usb_iterate (grub_usb_device_t dev, void *closure __attribute__ ((unused)))
  108. {
  109. struct grub_usb_desc_device *descdev;
  110. int i;
  111. descdev = &dev->descdev;
  112. usb_print_str ("Product", dev, descdev->strprod);
  113. usb_print_str ("Vendor", dev, descdev->strvendor);
  114. usb_print_str ("Serial", dev, descdev->strserial);
  115. if (descdev->class > 0 && descdev->class <= 0x0E)
  116. grub_printf ("Class: (0x%02x) %s, Subclass: 0x%02x, Protocol: 0x%02x\n",
  117. descdev->class, usb_classes[descdev->class],
  118. descdev->subclass, descdev->protocol);
  119. grub_printf ("USB version %d.%d, VendorID: 0x%02x, ProductID: 0x%02x, #conf: %d\n",
  120. descdev->usbrel >> 8, (descdev->usbrel >> 4) & 0x0F,
  121. descdev->vendorid, descdev->prodid, descdev->configcnt);
  122. grub_printf ("%s speed device\n", usb_devspeed[dev->speed]);
  123. for (i = 0; i < descdev->configcnt; i++)
  124. {
  125. struct grub_usb_desc_config *config;
  126. config = dev->config[i].descconf;
  127. usb_print_str ("Configuration:", dev, config->strconfig);
  128. }
  129. for (i = 0; i < dev->config[0].descconf->numif; i++)
  130. {
  131. int j;
  132. struct grub_usb_desc_if *interf;
  133. interf = dev->config[0].interf[i].descif;
  134. grub_printf ("Interface #%d: #Endpoints: %d ",
  135. i, interf->endpointcnt);
  136. if (interf->class > 0 && interf->class <= 0x0E)
  137. grub_printf ("Class: (0x%02x) %s, Subclass: 0x%02x, Protocol: 0x%02x\n",
  138. interf->class, usb_classes[interf->class],
  139. interf->subclass, interf->protocol);
  140. usb_print_str ("Interface", dev, interf->strif);
  141. for (j = 0; j < interf->endpointcnt; j++)
  142. {
  143. struct grub_usb_desc_endp *endp;
  144. endp = &dev->config[0].interf[i].descendp[j];
  145. grub_printf ("Endpoint #%d: %s, max packed size: %d, transfer type: %s, latency: %d\n",
  146. endp->endp_addr & 15,
  147. (endp->endp_addr & 128) ? "IN" : "OUT",
  148. endp->maxpacket, usb_endp_type[endp->attrib & 3],
  149. endp->interval);
  150. }
  151. }
  152. grub_printf("\n");
  153. return 0;
  154. }
  155. static grub_err_t
  156. grub_cmd_usbtest (grub_command_t cmd __attribute__ ((unused)),
  157. int argc __attribute__ ((unused)),
  158. char **args __attribute__ ((unused)))
  159. {
  160. grub_printf ("USB devices:\n\n");
  161. grub_usb_iterate (usb_iterate, 0);
  162. return 0;
  163. }
  164. static grub_command_t cmd;
  165. GRUB_MOD_INIT(usbtest)
  166. {
  167. cmd = grub_register_command ("usb", grub_cmd_usbtest,
  168. 0, N_("Test USB support."));
  169. }
  170. GRUB_MOD_FINI(usbtest)
  171. {
  172. grub_unregister_command (cmd);
  173. }