usb.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /* usb.c -- libusb USB support for GRUB. */
  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 <config.h>
  20. #include <grub/misc.h>
  21. #include <grub/mm.h>
  22. #include <usb.h>
  23. #include <grub/usb.h>
  24. #include <grub/dl.h>
  25. static struct grub_usb_controller_dev usb_controller =
  26. {
  27. .name = "libusb"
  28. };
  29. static struct grub_usb_device *grub_usb_devs[128];
  30. struct usb_bus *busses;
  31. static grub_err_t
  32. grub_libusb_devices (void)
  33. {
  34. struct usb_bus *bus;
  35. int last = 0;
  36. busses = usb_get_busses();
  37. for (bus = busses; bus; bus = bus->next)
  38. {
  39. struct usb_device *usbdev;
  40. struct grub_usb_device *dev;
  41. for (usbdev = bus->devices; usbdev; usbdev = usbdev->next)
  42. {
  43. struct usb_device_descriptor *desc = &usbdev->descriptor;
  44. grub_err_t err;
  45. if (! desc->bcdUSB)
  46. continue;
  47. dev = grub_malloc (sizeof (*dev));
  48. if (! dev)
  49. return grub_errno;
  50. dev->data = usbdev;
  51. /* Fill in all descriptors. */
  52. err = grub_usb_device_initialize (dev);
  53. if (err)
  54. {
  55. grub_errno = GRUB_ERR_NONE;
  56. continue;
  57. }
  58. /* Register the device. */
  59. grub_usb_devs[last++] = dev;
  60. }
  61. }
  62. return GRUB_USB_ERR_NONE;
  63. }
  64. int
  65. grub_usb_iterate (int (*hook) (grub_usb_device_t dev))
  66. {
  67. int i;
  68. for (i = 0; i < 128; i++)
  69. {
  70. if (grub_usb_devs[i])
  71. {
  72. if (hook (grub_usb_devs[i]))
  73. return 1;
  74. }
  75. }
  76. return 0;
  77. }
  78. grub_usb_err_t
  79. grub_usb_root_hub (grub_usb_controller_t controller __attribute__((unused)))
  80. {
  81. return GRUB_USB_ERR_NONE;
  82. }
  83. grub_usb_err_t
  84. grub_usb_control_msg (grub_usb_device_t dev, grub_uint8_t reqtype,
  85. grub_uint8_t request, grub_uint16_t value,
  86. grub_uint16_t index, grub_size_t size, char *data)
  87. {
  88. usb_dev_handle *devh;
  89. struct usb_device *d = dev->data;
  90. devh = usb_open (d);
  91. if (usb_control_msg (devh, reqtype, request,
  92. value, index, data, size, 20) < 0)
  93. {
  94. usb_close (devh);
  95. return GRUB_USB_ERR_STALL;
  96. }
  97. usb_close (devh);
  98. return GRUB_USB_ERR_NONE;
  99. }
  100. grub_usb_err_t
  101. grub_usb_bulk_read (grub_usb_device_t dev,
  102. int endpoint, grub_size_t size, char *data)
  103. {
  104. usb_dev_handle *devh;
  105. struct usb_device *d = dev->data;
  106. devh = usb_open (d);
  107. if (usb_claim_interface (devh, 0) < 1)
  108. {
  109. usb_close (devh);
  110. return GRUB_USB_ERR_STALL;
  111. }
  112. if (usb_bulk_read (devh, endpoint, data, size, 20) < 1)
  113. {
  114. usb_close (devh);
  115. return GRUB_USB_ERR_STALL;
  116. }
  117. usb_release_interface (devh, 0);
  118. usb_close (devh);
  119. return GRUB_USB_ERR_NONE;
  120. }
  121. grub_usb_err_t
  122. grub_usb_bulk_write (grub_usb_device_t dev,
  123. int endpoint, grub_size_t size, char *data)
  124. {
  125. usb_dev_handle *devh;
  126. struct usb_device *d = dev->data;
  127. devh = usb_open (d);
  128. if (usb_claim_interface (devh, 0) < 0)
  129. goto fail;
  130. if (usb_bulk_write (devh, endpoint, data, size, 20) < 0)
  131. goto fail;
  132. if (usb_release_interface (devh, 0) < 0)
  133. goto fail;
  134. usb_close (devh);
  135. return GRUB_USB_ERR_NONE;
  136. fail:
  137. usb_close (devh);
  138. return GRUB_USB_ERR_STALL;
  139. }
  140. GRUB_MOD_INIT (libusb)
  141. {
  142. usb_init();
  143. usb_find_busses();
  144. usb_find_devices();
  145. if (grub_libusb_devices ())
  146. return;
  147. grub_usb_controller_dev_register (&usb_controller);
  148. return;
  149. }
  150. GRUB_MOD_FINI (libusb)
  151. {
  152. return;
  153. }