usb-compat.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. #ifndef __USB_COMPAT_H__
  2. #define __USB_COMPAT_H__
  3. //#include <dm.h>
  4. #include "usb.h"
  5. #include "timer.h"
  6. struct usb_bus {
  7. int busnum; /* Bus number (in order of reg) */
  8. const char *bus_name; /* stable id (PCI slot_name etc) */
  9. u8 uses_dma; /* Does the host controller use DMA? */
  10. u8 uses_pio_for_control; /*
  11. * Does the host controller use PIO
  12. * for control transfers?
  13. */
  14. u8 otg_port; /* 0, or number of OTG/HNP port */
  15. unsigned is_b_host:1; /* true during some HNP roleswitches */
  16. unsigned b_hnp_enable:1; /* OTG: did A-Host enable HNP? */
  17. unsigned no_stop_on_short:1; /*
  18. * Quirk: some controllers don't stop
  19. * the ep queue on a short transfer
  20. * with the URB_SHORT_NOT_OK flag set.
  21. */
  22. unsigned no_sg_constraint:1; /* no sg constraint */
  23. unsigned sg_tablesize; /* 0 or largest number of sg list entries */
  24. int devnum_next; /* Next open device number in
  25. * round-robin allocation */
  26. int bandwidth_allocated; /* on this bus: how much of the time
  27. * reserved for periodic (intr/iso)
  28. * requests is used, on average?
  29. * Units: microseconds/frame.
  30. * Limits: Full/low speed reserve 90%,
  31. * while high speed reserves 80%.
  32. */
  33. int bandwidth_int_reqs; /* number of Interrupt requests */
  34. int bandwidth_isoc_reqs; /* number of Isoc. requests */
  35. unsigned resuming_ports; /* bit array: resuming root-hub ports */
  36. };
  37. struct usb_hcd {
  38. struct usb_bus self;
  39. int has_tt;
  40. void *hcd_priv;
  41. };
  42. struct usb_host_endpoint {
  43. struct usb_endpoint_descriptor desc;
  44. #ifndef NO_GNU
  45. struct list_head urb_list;
  46. #else
  47. List_t urb_list;
  48. #endif
  49. void *hcpriv;
  50. };
  51. /*
  52. * urb->transfer_flags:
  53. *
  54. * Note: URB_DIR_IN/OUT is automatically set in usb_submit_urb().
  55. */
  56. #define URB_SHORT_NOT_OK 0x0001 /* report short reads as errors */
  57. #define URB_ZERO_PACKET 0x0040 /* Finish bulk OUT with short packet */
  58. #define URB_NO_INTERRUPT 0x0080 /* HINT: no non-error interrupt*/
  59. struct urb;
  60. typedef void (*usb_complete_t)(struct urb *);
  61. struct urb {
  62. void *hcpriv; /* private data for host controller */
  63. #ifndef NO_GNU
  64. struct list_head urb_list; /* list head for use by the urb's
  65. * current owner */
  66. #else
  67. ListItem_t urb_list;
  68. void *queueHandle;
  69. #endif
  70. struct usb_device *dev; /* (in) pointer to associated device */
  71. struct usb_host_endpoint *ep; /* (internal) pointer to endpoint */
  72. unsigned int pipe; /* (in) pipe information */
  73. int status; /* (return) non-ISO status */
  74. unsigned int transfer_flags; /* (in) URB_SHORT_NOT_OK | ...*/
  75. void *transfer_buffer; /* (in) associated data buffer */
  76. dma_addr_t transfer_dma; /* (in) dma addr for transfer_buffer */
  77. u32 transfer_buffer_length; /* (in) data buffer length */
  78. u32 actual_length; /* (return) actual transfer length */
  79. unsigned char *setup_packet; /* (in) setup packet (control only) */
  80. dma_addr_t setup_dma;
  81. int start_frame; /* (modify) start frame (ISO) */
  82. void *context;
  83. usb_complete_t complete; /* (in) completion routine */
  84. int interval;
  85. int error_count; /* (return) number of ISO errors */
  86. int number_of_packets; /* (in) number of ISO packets */
  87. struct usb_iso_packet_descriptor iso_frame_desc[0];
  88. };
  89. struct hc_driver {
  90. const char *description; /* "ehci-hcd" etc */
  91. const char *product_desc; /* product/vendor string */
  92. size_t hcd_priv_size; /* size of private data */
  93. /* irq handler */
  94. irqreturn_t (*irq) (struct usb_hcd *hcd);
  95. int flags;
  96. #define HCD_MEMORY 0x0001 /* HC regs use memory (else I/O) */
  97. #define HCD_LOCAL_MEM 0x0002 /* HC needs local memory */
  98. #define HCD_SHARED 0x0004 /* Two (or more) usb_hcds share HW */
  99. #define HCD_USB11 0x0010 /* USB 1.1 */
  100. #define HCD_USB2 0x0020 /* USB 2.0 */
  101. #define HCD_USB25 0x0030 /* Wireless USB 1.0 (USB 2.5)*/
  102. #define HCD_USB3 0x0040 /* USB 3.0 */
  103. #define HCD_USB31 0x0050 /* USB 3.1 */
  104. #define HCD_MASK 0x0070
  105. #define HCD_BH 0x0100 /* URB complete in BH context */
  106. /* called to init HCD and root hub */
  107. int (*reset) (struct usb_hcd *hcd);
  108. int (*start) (struct usb_hcd *hcd);
  109. /* NOTE: these suspend/resume calls relate to the HC as
  110. * a whole, not just the root hub; they're for PCI bus glue.
  111. */
  112. /* called after suspending the hub, before entering D3 etc */
  113. int (*pci_suspend)(struct usb_hcd *hcd, bool do_wakeup);
  114. /* called after entering D0 (etc), before resuming the hub */
  115. int (*pci_resume)(struct usb_hcd *hcd, bool hibernated);
  116. /* cleanly make HCD stop writing memory and doing I/O */
  117. void (*stop) (struct usb_hcd *hcd);
  118. /* shutdown HCD */
  119. void (*shutdown) (struct usb_hcd *hcd);
  120. /* return current frame number */
  121. int (*get_frame_number) (struct usb_hcd *hcd);
  122. /* manage i/o requests, device state */
  123. int (*urb_enqueue)(struct usb_hcd *hcd,
  124. struct urb *urb, gfp_t mem_flags);
  125. int (*urb_dequeue)(struct usb_hcd *hcd,
  126. struct urb *urb, int status);
  127. /*
  128. * (optional) these hooks allow an HCD to override the default DMA
  129. * mapping and unmapping routines. In general, they shouldn't be
  130. * necessary unless the host controller has special DMA requirements,
  131. * such as alignment contraints. If these are not specified, the
  132. * general usb_hcd_(un)?map_urb_for_dma functions will be used instead
  133. * (and it may be a good idea to call these functions in your HCD
  134. * implementation)
  135. */
  136. int (*map_urb_for_dma)(struct usb_hcd *hcd, struct urb *urb,
  137. gfp_t mem_flags);
  138. void (*unmap_urb_for_dma)(struct usb_hcd *hcd, struct urb *urb);
  139. /* hw synch, freeing endpoint resources that urb_dequeue can't */
  140. void (*endpoint_disable)(struct usb_hcd *hcd,
  141. struct usb_host_endpoint *ep);
  142. /* (optional) reset any endpoint state such as sequence number
  143. and current window */
  144. void (*endpoint_reset)(struct usb_hcd *hcd,
  145. struct usb_host_endpoint *ep);
  146. /* root hub support */
  147. int (*hub_status_data) (struct usb_hcd *hcd, char *buf);
  148. int (*hub_control) (struct usb_hcd *hcd,
  149. u16 typeReq, u16 wValue, u16 wIndex,
  150. char *buf, u16 wLength);
  151. int (*bus_suspend)(struct usb_hcd *);
  152. int (*bus_resume)(struct usb_hcd *);
  153. int (*start_port_reset)(struct usb_hcd *, unsigned port_num);
  154. /* force handover of high-speed port to full-speed companion */
  155. void (*relinquish_port)(struct usb_hcd *, int);
  156. /* has a port been handed over to a companion? */
  157. int (*port_handed_over)(struct usb_hcd *, int);
  158. /* CLEAR_TT_BUFFER completion callback */
  159. void (*clear_tt_buffer_complete)(struct usb_hcd *,
  160. struct usb_host_endpoint *);
  161. /* xHCI specific functions */
  162. /* Called by usb_alloc_dev to alloc HC device structures */
  163. int (*alloc_dev)(struct usb_hcd *, struct usb_device *);
  164. /* Called by usb_disconnect to free HC device structures */
  165. void (*free_dev)(struct usb_hcd *, struct usb_device *);
  166. /* Change a group of bulk endpoints to support multiple stream IDs */
  167. int (*alloc_streams)(struct usb_hcd *hcd, struct usb_device *udev,
  168. struct usb_host_endpoint **eps, unsigned int num_eps,
  169. unsigned int num_streams, gfp_t mem_flags);
  170. /* Reverts a group of bulk endpoints back to not using stream IDs.
  171. * Can fail if we run out of memory.
  172. */
  173. int (*free_streams)(struct usb_hcd *hcd, struct usb_device *udev,
  174. struct usb_host_endpoint **eps, unsigned int num_eps,
  175. gfp_t mem_flags);
  176. /* Bandwidth computation functions */
  177. /* Note that add_endpoint() can only be called once per endpoint before
  178. * check_bandwidth() or reset_bandwidth() must be called.
  179. * drop_endpoint() can only be called once per endpoint also.
  180. * A call to xhci_drop_endpoint() followed by a call to
  181. * xhci_add_endpoint() will add the endpoint to the schedule with
  182. * possibly new parameters denoted by a different endpoint descriptor
  183. * in usb_host_endpoint. A call to xhci_add_endpoint() followed by a
  184. * call to xhci_drop_endpoint() is not allowed.
  185. */
  186. /* Allocate endpoint resources and add them to a new schedule */
  187. int (*add_endpoint)(struct usb_hcd *, struct usb_device *,
  188. struct usb_host_endpoint *);
  189. /* Drop an endpoint from a new schedule */
  190. int (*drop_endpoint)(struct usb_hcd *, struct usb_device *,
  191. struct usb_host_endpoint *);
  192. /* Check that a new hardware configuration, set using
  193. * endpoint_enable and endpoint_disable, does not exceed bus
  194. * bandwidth. This must be called before any set configuration
  195. * or set interface requests are sent to the device.
  196. */
  197. int (*check_bandwidth)(struct usb_hcd *, struct usb_device *);
  198. /* Reset the device schedule to the last known good schedule,
  199. * which was set from a previous successful call to
  200. * check_bandwidth(). This reverts any add_endpoint() and
  201. * drop_endpoint() calls since that last successful call.
  202. * Used for when a check_bandwidth() call fails due to resource
  203. * or bandwidth constraints.
  204. */
  205. void (*reset_bandwidth)(struct usb_hcd *, struct usb_device *);
  206. /* Returns the hardware-chosen device address */
  207. int (*address_device)(struct usb_hcd *, struct usb_device *udev);
  208. /* prepares the hardware to send commands to the device */
  209. int (*enable_device)(struct usb_hcd *, struct usb_device *udev);
  210. /* Notifies the HCD after a hub descriptor is fetched.
  211. * Will block.
  212. */
  213. int (*update_hub_device)(struct usb_hcd *, struct usb_device *hdev,
  214. struct usb_tt *tt, gfp_t mem_flags);
  215. int (*reset_device)(struct usb_hcd *, struct usb_device *);
  216. /* Notifies the HCD after a device is connected and its
  217. * address is set
  218. */
  219. int (*update_device)(struct usb_hcd *, struct usb_device *);
  220. int (*set_usb2_hw_lpm)(struct usb_hcd *, struct usb_device *, int);
  221. int (*find_raw_port_number)(struct usb_hcd *, int);
  222. /* Call for power on/off the port if necessary */
  223. int (*port_power)(struct usb_hcd *hcd, int portnum, bool enable);
  224. };
  225. #ifndef NO_GNU
  226. #define usb_hcd_link_urb_to_ep(hcd, urb) ({ \
  227. int ret = 0; \
  228. list_add_tail(&urb->urb_list, &urb->ep->urb_list); \
  229. ret; })
  230. #else
  231. #define usb_hcd_link_urb_to_ep(hcd, urb) do { \
  232. list_add_tail(&urb->urb_list, &urb->ep->urb_list); \
  233. } while(0)
  234. #endif
  235. #define usb_hcd_unlink_urb_from_ep(hcd, urb) list_del_init(&urb->urb_list)
  236. #define usb_hcd_check_unlink_urb(hdc, urb, status) 0
  237. static inline void usb_hcd_giveback_urb(struct usb_hcd *hcd,
  238. struct urb *urb,
  239. int status)
  240. {
  241. urb->status = status;
  242. if (urb->complete)
  243. urb->complete(urb);
  244. }
  245. static inline int usb_hcd_unmap_urb_for_dma(struct usb_hcd *hcd,
  246. struct urb *urb)
  247. {
  248. /* TODO: add cache invalidation here */
  249. return 0;
  250. }
  251. static inline struct usb_bus *hcd_to_bus(struct usb_hcd *hcd)
  252. {
  253. return &hcd->self;
  254. }
  255. static inline void usb_hcd_resume_root_hub(struct usb_hcd *hcd)
  256. {
  257. (void)hcd;
  258. return;
  259. }
  260. /*
  261. * Generic bandwidth allocation constants/support
  262. */
  263. #define FRAME_TIME_USECS 1000L
  264. #define BitTime(bytecount) (7 * 8 * bytecount / 6) /* with integer truncation */
  265. /* Trying not to use worst-case bit-stuffing
  266. * of (7/6 * 8 * bytecount) = 9.33 * bytecount */
  267. /* bytecount = data payload byte count */
  268. #define NS_TO_US(ns) DIV_ROUND_UP(ns, 1000L)
  269. /* convert nanoseconds to microseconds, rounding up */
  270. /*
  271. * Full/low speed bandwidth allocation constants/support.
  272. */
  273. #define BW_HOST_DELAY 1000L /* nanoseconds */
  274. #define BW_HUB_LS_SETUP 333L /* nanoseconds */
  275. /* 4 full-speed bit times (est.) */
  276. #define FRAME_TIME_BITS 12000L /* frame = 1 millisecond */
  277. #define FRAME_TIME_MAX_BITS_ALLOC (90L * FRAME_TIME_BITS / 100L)
  278. #define FRAME_TIME_MAX_USECS_ALLOC (90L * FRAME_TIME_USECS / 100L)
  279. /*
  280. * Ceiling [nano/micro]seconds (typical) for that many bytes at high speed
  281. * ISO is a bit less, no ACK ... from USB 2.0 spec, 5.11.3 (and needed
  282. * to preallocate bandwidth)
  283. */
  284. #define USB2_HOST_DELAY 5 /* nsec, guess */
  285. #define HS_NSECS(bytes) (((55 * 8 * 2083) \
  286. + (2083UL * (3 + BitTime(bytes))))/1000 \
  287. + USB2_HOST_DELAY)
  288. #define HS_NSECS_ISO(bytes) (((38 * 8 * 2083) \
  289. + (2083UL * (3 + BitTime(bytes))))/1000 \
  290. + USB2_HOST_DELAY)
  291. #define HS_USECS(bytes) NS_TO_US(HS_NSECS(bytes))
  292. #define HS_USECS_ISO(bytes) NS_TO_US(HS_NSECS_ISO(bytes))
  293. static inline long usb_calc_bus_time (int speed, int is_input, int isoc, int bytecount)
  294. {
  295. unsigned long tmp;
  296. switch (speed) {
  297. case USB_SPEED_LOW: /* INTR only */
  298. if (is_input) {
  299. tmp = (67667L * (31L + 10L * BitTime (bytecount))) / 1000L;
  300. return 64060L + (2 * BW_HUB_LS_SETUP) + BW_HOST_DELAY + tmp;
  301. } else {
  302. tmp = (66700L * (31L + 10L * BitTime (bytecount))) / 1000L;
  303. return 64107L + (2 * BW_HUB_LS_SETUP) + BW_HOST_DELAY + tmp;
  304. }
  305. case USB_SPEED_FULL: /* ISOC or INTR */
  306. if (isoc) {
  307. tmp = (8354L * (31L + 10L * BitTime (bytecount))) / 1000L;
  308. return ((is_input) ? 7268L : 6265L) + BW_HOST_DELAY + tmp;
  309. } else {
  310. tmp = (8354L * (31L + 10L * BitTime (bytecount))) / 1000L;
  311. return 9107L + BW_HOST_DELAY + tmp;
  312. }
  313. case USB_SPEED_HIGH: /* ISOC or INTR */
  314. /* FIXME adjust for input vs output */
  315. if (isoc)
  316. tmp = HS_NSECS_ISO (bytecount);
  317. else
  318. tmp = HS_NSECS (bytecount);
  319. return tmp;
  320. default:
  321. //pr_debug ("%s: bogus device speed!\n", usbcore_name);
  322. return -1;
  323. }
  324. }
  325. #ifdef CONFIG_DM_USB
  326. static inline struct usb_device *usb_dev_get_parent(struct usb_device *udev)
  327. {
  328. struct udevice *parent = udev->dev->parent;
  329. /*
  330. * When called from usb-uclass.c: usb_scan_device() udev->dev points
  331. * to the parent udevice, not the actual udevice belonging to the
  332. * udev as the device is not instantiated yet.
  333. *
  334. * If dev is an usb-bus, then we are called from usb_scan_device() for
  335. * an usb-device plugged directly into the root port, return NULL.
  336. */
  337. if (device_get_uclass_id(udev->dev) == UCLASS_USB)
  338. return NULL;
  339. /*
  340. * If these 2 are not the same we are being called from
  341. * usb_scan_device() and udev itself is the parent.
  342. */
  343. if (dev_get_parent_priv(udev->dev) != udev)
  344. return udev;
  345. /* We are being called normally, use the parent pointer */
  346. if (device_get_uclass_id(parent) == UCLASS_USB_HUB)
  347. return dev_get_parent_priv(parent);
  348. return NULL;
  349. }
  350. #else
  351. static inline struct usb_device *usb_dev_get_parent(struct usb_device *dev)
  352. {
  353. return dev->parent;
  354. }
  355. #endif
  356. #if 0
  357. static inline void usb_gadget_giveback_request(struct usb_ep *ep,
  358. struct usb_request *req)
  359. {
  360. req->complete(ep, req);
  361. }
  362. static inline int usb_gadget_map_request(struct usb_gadget *gadget,
  363. struct usb_request *req, int is_in)
  364. {
  365. if (req->length == 0)
  366. return 0;
  367. req->dma = dma_map_single(req->buf, req->length,
  368. is_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
  369. return 0;
  370. }
  371. static inline void usb_gadget_unmap_request(struct usb_gadget *gadget,
  372. struct usb_request *req, int is_in)
  373. {
  374. if (req->length == 0)
  375. return;
  376. dma_unmap_single((void *)req->dma, req->length,
  377. is_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
  378. }
  379. #else
  380. void usb_gadget_giveback_request(struct usb_ep *ep,
  381. struct usb_request *req);
  382. int usb_gadget_map_request(struct usb_gadget *gadget,
  383. struct usb_request *req, int is_in);
  384. void usb_gadget_unmap_request(struct usb_gadget *gadget,
  385. struct usb_request *req, int is_in);
  386. #endif
  387. #endif /* __USB_COMPAT_H__ */