usb-compat.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. #ifndef __USB_COMPAT_H__
  2. #define __USB_COMPAT_H__
  3. #include <dm.h>
  4. #include "usb.h"
  5. struct usb_hcd {
  6. void *hcd_priv;
  7. };
  8. struct usb_host_endpoint {
  9. struct usb_endpoint_descriptor desc;
  10. struct list_head urb_list;
  11. void *hcpriv;
  12. };
  13. /*
  14. * urb->transfer_flags:
  15. *
  16. * Note: URB_DIR_IN/OUT is automatically set in usb_submit_urb().
  17. */
  18. #define URB_SHORT_NOT_OK 0x0001 /* report short reads as errors */
  19. #define URB_ZERO_PACKET 0x0040 /* Finish bulk OUT with short packet */
  20. struct urb;
  21. typedef void (*usb_complete_t)(struct urb *);
  22. struct urb {
  23. void *hcpriv; /* private data for host controller */
  24. struct list_head urb_list; /* list head for use by the urb's
  25. * current owner */
  26. struct usb_device *dev; /* (in) pointer to associated device */
  27. struct usb_host_endpoint *ep; /* (internal) pointer to endpoint */
  28. unsigned int pipe; /* (in) pipe information */
  29. int status; /* (return) non-ISO status */
  30. unsigned int transfer_flags; /* (in) URB_SHORT_NOT_OK | ...*/
  31. void *transfer_buffer; /* (in) associated data buffer */
  32. dma_addr_t transfer_dma; /* (in) dma addr for transfer_buffer */
  33. u32 transfer_buffer_length; /* (in) data buffer length */
  34. u32 actual_length; /* (return) actual transfer length */
  35. unsigned char *setup_packet; /* (in) setup packet (control only) */
  36. int start_frame; /* (modify) start frame (ISO) */
  37. usb_complete_t complete; /* (in) completion routine */
  38. };
  39. #define usb_hcd_link_urb_to_ep(hcd, urb) ({ \
  40. int ret = 0; \
  41. list_add_tail(&urb->urb_list, &urb->ep->urb_list); \
  42. ret; })
  43. #define usb_hcd_unlink_urb_from_ep(hcd, urb) list_del_init(&urb->urb_list)
  44. #define usb_hcd_check_unlink_urb(hdc, urb, status) 0
  45. static inline void usb_hcd_giveback_urb(struct usb_hcd *hcd,
  46. struct urb *urb,
  47. int status)
  48. {
  49. urb->status = status;
  50. if (urb->complete)
  51. urb->complete(urb);
  52. }
  53. static inline int usb_hcd_unmap_urb_for_dma(struct usb_hcd *hcd,
  54. struct urb *urb)
  55. {
  56. /* TODO: add cache invalidation here */
  57. return 0;
  58. }
  59. #ifdef CONFIG_DM_USB
  60. static inline u16 find_tt(struct usb_device *udev)
  61. {
  62. struct udevice *parent;
  63. struct usb_device *uparent, *ttdev;
  64. /*
  65. * When called from usb-uclass.c: usb_scan_device() udev->dev points
  66. * to the parent udevice, not the actual udevice belonging to the
  67. * udev as the device is not instantiated yet. So when searching
  68. * for the first usb-2 parent start with udev->dev not
  69. * udev->dev->parent .
  70. */
  71. ttdev = udev;
  72. parent = udev->dev;
  73. uparent = dev_get_parent_priv(parent);
  74. while (uparent->speed != USB_SPEED_HIGH) {
  75. struct udevice *dev = parent;
  76. if (device_get_uclass_id(dev->parent) != UCLASS_USB_HUB) {
  77. printf("musb: Error cannot find high speed parent of usb-1 device\n");
  78. return 0;
  79. }
  80. ttdev = dev_get_parent_priv(dev);
  81. parent = dev->parent;
  82. uparent = dev_get_parent_priv(parent);
  83. }
  84. return (uparent->devnum << 8) | (ttdev->portnr - 1);
  85. }
  86. static inline struct usb_device *usb_dev_get_parent(struct usb_device *udev)
  87. {
  88. struct udevice *parent = udev->dev->parent;
  89. /*
  90. * When called from usb-uclass.c: usb_scan_device() udev->dev points
  91. * to the parent udevice, not the actual udevice belonging to the
  92. * udev as the device is not instantiated yet.
  93. *
  94. * If dev is an usb-bus, then we are called from usb_scan_device() for
  95. * an usb-device plugged directly into the root port, return NULL.
  96. */
  97. if (device_get_uclass_id(udev->dev) == UCLASS_USB)
  98. return NULL;
  99. /*
  100. * If these 2 are not the same we are being called from
  101. * usb_scan_device() and udev itself is the parent.
  102. */
  103. if (dev_get_parent_priv(udev->dev) != udev)
  104. return udev;
  105. /* We are being called normally, use the parent pointer */
  106. if (device_get_uclass_id(parent) == UCLASS_USB_HUB)
  107. return dev_get_parent_priv(parent);
  108. return NULL;
  109. }
  110. #else
  111. static inline u16 find_tt(struct usb_device *dev)
  112. {
  113. u8 chid;
  114. u8 hub;
  115. /* Find out the nearest parent which is high speed */
  116. while (dev->parent->parent != NULL)
  117. if (dev->parent->speed != USB_SPEED_HIGH)
  118. dev = dev->parent;
  119. else
  120. break;
  121. /* determine the port address at that hub */
  122. hub = dev->parent->devnum;
  123. for (chid = 0; chid < USB_MAXCHILDREN; chid++)
  124. if (dev->parent->children[chid] == dev)
  125. break;
  126. return (hub << 8) | chid;
  127. }
  128. static inline struct usb_device *usb_dev_get_parent(struct usb_device *dev)
  129. {
  130. return dev->parent;
  131. }
  132. #endif
  133. #endif /* __USB_COMPAT_H__ */