usb.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2008 Free Software Foundation, Inc.
  4. *
  5. * GRUB is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * GRUB 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
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #ifndef GRUB_USB_H
  19. #define GRUB_USB_H 1
  20. #include <grub/err.h>
  21. #include <grub/usbdesc.h>
  22. #include <grub/usbtrans.h>
  23. typedef struct grub_usb_device *grub_usb_device_t;
  24. typedef struct grub_usb_controller *grub_usb_controller_t;
  25. typedef struct grub_usb_controller_dev *grub_usb_controller_dev_t;
  26. typedef enum
  27. {
  28. GRUB_USB_ERR_NONE,
  29. GRUB_USB_ERR_WAIT,
  30. GRUB_USB_ERR_INTERNAL,
  31. GRUB_USB_ERR_STALL,
  32. GRUB_USB_ERR_DATA,
  33. GRUB_USB_ERR_NAK,
  34. GRUB_USB_ERR_BABBLE,
  35. GRUB_USB_ERR_TIMEOUT,
  36. GRUB_USB_ERR_BITSTUFF,
  37. GRUB_USB_ERR_UNRECOVERABLE,
  38. GRUB_USB_ERR_BADDEVICE
  39. } grub_usb_err_t;
  40. typedef enum
  41. {
  42. GRUB_USB_SPEED_NONE,
  43. GRUB_USB_SPEED_LOW,
  44. GRUB_USB_SPEED_FULL,
  45. GRUB_USB_SPEED_HIGH
  46. } grub_usb_speed_t;
  47. typedef int (*grub_usb_iterate_hook_t) (grub_usb_device_t dev, void *data);
  48. typedef int (*grub_usb_controller_iterate_hook_t) (grub_usb_controller_t dev,
  49. void *data);
  50. /* Call HOOK with each device, until HOOK returns non-zero. */
  51. int grub_usb_iterate (grub_usb_iterate_hook_t hook, void *hook_data);
  52. grub_usb_err_t grub_usb_device_initialize (grub_usb_device_t dev);
  53. grub_usb_err_t grub_usb_get_descriptor (grub_usb_device_t dev,
  54. grub_uint8_t type, grub_uint8_t index,
  55. grub_size_t size, char *data);
  56. grub_usb_err_t grub_usb_clear_halt (grub_usb_device_t dev, int endpoint);
  57. grub_usb_err_t grub_usb_set_configuration (grub_usb_device_t dev,
  58. int configuration);
  59. void grub_usb_controller_dev_register (grub_usb_controller_dev_t usb);
  60. void grub_usb_controller_dev_unregister (grub_usb_controller_dev_t usb);
  61. int grub_usb_controller_iterate (grub_usb_controller_iterate_hook_t hook,
  62. void *hook_data);
  63. grub_usb_err_t grub_usb_control_msg (grub_usb_device_t dev, grub_uint8_t reqtype,
  64. grub_uint8_t request, grub_uint16_t value,
  65. grub_uint16_t index, grub_size_t size,
  66. char *data);
  67. grub_usb_err_t
  68. grub_usb_bulk_read (grub_usb_device_t dev,
  69. struct grub_usb_desc_endp *endpoint,
  70. grub_size_t size, char *data);
  71. grub_usb_err_t
  72. grub_usb_bulk_write (grub_usb_device_t dev,
  73. struct grub_usb_desc_endp *endpoint,
  74. grub_size_t size, char *data);
  75. grub_usb_err_t
  76. grub_usb_root_hub (grub_usb_controller_t controller);
  77. /* XXX: All handled by libusb for now. */
  78. struct grub_usb_controller_dev
  79. {
  80. /* The device name. */
  81. const char *name;
  82. int (*iterate) (grub_usb_controller_iterate_hook_t hook, void *hook_data);
  83. grub_usb_err_t (*setup_transfer) (grub_usb_controller_t dev,
  84. grub_usb_transfer_t transfer);
  85. grub_usb_err_t (*check_transfer) (grub_usb_controller_t dev,
  86. grub_usb_transfer_t transfer,
  87. grub_size_t *actual);
  88. grub_usb_err_t (*cancel_transfer) (grub_usb_controller_t dev,
  89. grub_usb_transfer_t transfer);
  90. int (*hubports) (grub_usb_controller_t dev);
  91. grub_usb_err_t (*portstatus) (grub_usb_controller_t dev, unsigned int port,
  92. unsigned int enable);
  93. grub_usb_speed_t (*detect_dev) (grub_usb_controller_t dev, int port, int *changed);
  94. /* Per controller flag - port reset pending, don't do another reset */
  95. grub_uint64_t pending_reset;
  96. /* Max. number of transfer descriptors used per one bulk transfer */
  97. /* The reason is to prevent "exhausting" of TD by large bulk */
  98. /* transfer - number of TD is limited in USB host driver */
  99. /* Value is calculated/estimated in driver - some TDs should be */
  100. /* reserved for posible concurrent control or "interrupt" transfers */
  101. grub_size_t max_bulk_tds;
  102. /* The next host controller. */
  103. struct grub_usb_controller_dev *next;
  104. };
  105. struct grub_usb_controller
  106. {
  107. /* The underlying USB Host Controller device. */
  108. grub_usb_controller_dev_t dev;
  109. /* Data used by the USB Host Controller Driver. */
  110. void *data;
  111. };
  112. struct grub_usb_interface
  113. {
  114. struct grub_usb_desc_if *descif;
  115. struct grub_usb_desc_endp *descendp;
  116. /* A driver is handling this interface. Do we need to support multiple drivers
  117. for single interface?
  118. */
  119. int attached;
  120. void (*detach_hook) (struct grub_usb_device *dev, int config, int interface);
  121. void *detach_data;
  122. };
  123. struct grub_usb_configuration
  124. {
  125. /* Configuration descriptors . */
  126. struct grub_usb_desc_config *descconf;
  127. /* Interfaces associated to this configuration. */
  128. struct grub_usb_interface interf[32];
  129. };
  130. struct grub_usb_hub_port
  131. {
  132. grub_uint64_t soft_limit_time;
  133. grub_uint64_t hard_limit_time;
  134. enum {
  135. PORT_STATE_NORMAL = 0,
  136. PORT_STATE_WAITING_FOR_STABLE_POWER = 1,
  137. PORT_STATE_FAILED_DEVICE = 2,
  138. PORT_STATE_STABLE_POWER = 3,
  139. } state;
  140. };
  141. struct grub_usb_device
  142. {
  143. /* The device descriptor of this device. */
  144. struct grub_usb_desc_device descdev;
  145. /* The controller the device is connected to. */
  146. struct grub_usb_controller controller;
  147. /* Device configurations (after opening the device). */
  148. struct grub_usb_configuration config[8];
  149. /* Device address. */
  150. int addr;
  151. /* Device speed. */
  152. grub_usb_speed_t speed;
  153. /* All descriptors are read if this is set to 1. */
  154. int initialized;
  155. /* Data toggle values (used for bulk transfers only). */
  156. int toggle[256];
  157. /* Used by libusb wrapper. Schedulded for removal. */
  158. void *data;
  159. /* Hub information. */
  160. /* Array of children for a hub. */
  161. grub_usb_device_t *children;
  162. /* Number of hub ports. */
  163. unsigned nports;
  164. struct grub_usb_hub_port *ports;
  165. grub_usb_transfer_t hub_transfer;
  166. grub_uint32_t statuschange;
  167. struct grub_usb_desc_endp *hub_endpoint;
  168. /* EHCI Split Transfer information */
  169. int split_hubport;
  170. int split_hubaddr;
  171. };
  172. typedef enum grub_usb_ep_type
  173. {
  174. GRUB_USB_EP_CONTROL,
  175. GRUB_USB_EP_ISOCHRONOUS,
  176. GRUB_USB_EP_BULK,
  177. GRUB_USB_EP_INTERRUPT
  178. } grub_usb_ep_type_t;
  179. static inline enum grub_usb_ep_type
  180. grub_usb_get_ep_type (struct grub_usb_desc_endp *ep)
  181. {
  182. return ep->attrib & 3;
  183. }
  184. typedef enum
  185. {
  186. GRUB_USB_CLASS_NOTHERE,
  187. GRUB_USB_CLASS_AUDIO,
  188. GRUB_USB_CLASS_COMMUNICATION,
  189. GRUB_USB_CLASS_HID,
  190. GRUB_USB_CLASS_XXX,
  191. GRUB_USB_CLASS_PHYSICAL,
  192. GRUB_USB_CLASS_IMAGE,
  193. GRUB_USB_CLASS_PRINTER,
  194. GRUB_USB_CLASS_MASS_STORAGE,
  195. GRUB_USB_CLASS_HUB,
  196. GRUB_USB_CLASS_DATA_INTERFACE,
  197. GRUB_USB_CLASS_SMART_CARD,
  198. GRUB_USB_CLASS_CONTENT_SECURITY,
  199. GRUB_USB_CLASS_VIDEO
  200. } grub_usb_classes_t;
  201. typedef enum
  202. {
  203. GRUB_USBMS_SUBCLASS_BULK = 0x06,
  204. /* Experimental support for non-pure SCSI devices */
  205. GRUB_USBMS_SUBCLASS_RBC = 0x01,
  206. GRUB_USBMS_SUBCLASS_MMC2 = 0x02,
  207. GRUB_USBMS_SUBCLASS_UFI = 0x04,
  208. GRUB_USBMS_SUBCLASS_SFF8070 = 0x05
  209. } grub_usbms_subclass_t;
  210. typedef enum
  211. {
  212. GRUB_USBMS_PROTOCOL_BULK = 0x50,
  213. /* Experimental support for Control/Bulk/Interrupt (CBI) devices */
  214. GRUB_USBMS_PROTOCOL_CBI = 0x00, /* CBI with interrupt */
  215. GRUB_USBMS_PROTOCOL_CB = 0x01 /* CBI wthout interrupt */
  216. } grub_usbms_protocol_t;
  217. static inline struct grub_usb_desc_if *
  218. grub_usb_get_config_interface (struct grub_usb_desc_config *config)
  219. {
  220. struct grub_usb_desc_if *interf;
  221. interf = (struct grub_usb_desc_if *) (sizeof (*config) + (char *) config);
  222. return interf;
  223. }
  224. typedef int (*grub_usb_attach_hook_class) (grub_usb_device_t usbdev,
  225. int configno, int interfno);
  226. struct grub_usb_attach_desc
  227. {
  228. struct grub_usb_attach_desc *next;
  229. struct grub_usb_attach_desc **prev;
  230. int class;
  231. grub_usb_attach_hook_class hook;
  232. };
  233. void grub_usb_register_attach_hook_class (struct grub_usb_attach_desc *desc);
  234. void grub_usb_unregister_attach_hook_class (struct grub_usb_attach_desc *desc);
  235. void grub_usb_poll_devices (int wait_for_completion);
  236. void grub_usb_device_attach (grub_usb_device_t dev);
  237. grub_usb_err_t
  238. grub_usb_bulk_read_extended (grub_usb_device_t dev,
  239. struct grub_usb_desc_endp *endpoint,
  240. grub_size_t size, char *data,
  241. int timeout, grub_size_t *actual);
  242. grub_usb_transfer_t
  243. grub_usb_bulk_read_background (grub_usb_device_t dev,
  244. struct grub_usb_desc_endp *endpoint,
  245. grub_size_t size, void *data);
  246. grub_usb_err_t
  247. grub_usb_check_transfer (grub_usb_transfer_t trans, grub_size_t *actual);
  248. void
  249. grub_usb_cancel_transfer (grub_usb_transfer_t trans);
  250. void
  251. grub_ehci_init_device (volatile void *regs);
  252. void
  253. grub_ehci_pci_scan (void);
  254. #endif /* GRUB_USB_H */