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