blob: d94789156dc7d5f72992ca56bb43915bf1771862 [file] [log] [blame]
Kevin O'Connor114592f2009-09-28 21:32:08 -04001// USB functions and data.
2#ifndef __USB_H
3#define __USB_H
4
5// Local information for a usb controller.
6struct usb_s {
Kevin O'Connor59f02832009-10-12 10:09:15 -04007 u8 type;
Kevin O'Connor114592f2009-09-28 21:32:08 -04008 u8 maxaddr;
Kevin O'Connor59f02832009-10-12 10:09:15 -04009 u16 bdf;
10
11 union {
12 struct {
13 u16 iobase;
Kevin O'Connor991eaff2010-02-13 21:51:47 -050014 void *qh, *framelist;
Kevin O'Connor59f02832009-10-12 10:09:15 -040015 } uhci;
16 struct {
17 struct ohci_regs *regs;
Kevin O'Connor1c46a542009-10-17 23:53:32 -040018 void *control_ed;
Kevin O'Connor59f02832009-10-12 10:09:15 -040019 } ohci;
20 };
Kevin O'Connor114592f2009-09-28 21:32:08 -040021};
22
Kevin O'Connor59f02832009-10-12 10:09:15 -040023#define USB_TYPE_UHCI 1
24#define USB_TYPE_OHCI 2
25
Kevin O'Connor114592f2009-09-28 21:32:08 -040026extern struct usb_s USBControllers[];
27
Kevin O'Connor59f02832009-10-12 10:09:15 -040028struct usb_pipe {
29 u32 endp;
30};
31
Kevin O'Connor114592f2009-09-28 21:32:08 -040032#define USB_MAXADDR 127
33
34// usb.c
Kevin O'Connor1ca05b02010-01-03 17:43:37 -050035void usb_setup(void);
Kevin O'Connor114592f2009-09-28 21:32:08 -040036int configure_usb_device(struct usb_s *cntl, int lowspeed);
37struct usb_ctrlrequest;
38int send_default_control(u32 endp, const struct usb_ctrlrequest *req
39 , void *data);
Kevin O'Connor7149fc82010-02-17 23:24:42 -050040int usb_send_bulk(struct usb_pipe *pipe, int dir, void *data, int datasize);
41struct usb_pipe *alloc_bulk_pipe(u32 endp);
Kevin O'Connor59f02832009-10-12 10:09:15 -040042struct usb_pipe *alloc_intr_pipe(u32 endp, int period);
43int usb_poll_intr(struct usb_pipe *pipe, void *data);
Kevin O'Connor3c160dd2010-02-17 23:06:52 -050044struct usb_interface_descriptor;
45struct usb_endpoint_descriptor *findEndPointDesc(
46 struct usb_interface_descriptor *iface, int imax, int type, int dir);
47u32 mkendpFromDesc(u32 endp, struct usb_endpoint_descriptor *epdesc);
Kevin O'Connor114592f2009-09-28 21:32:08 -040048
49
50/****************************************************************
51 * endpoint definition
52 ****************************************************************/
53
54static inline u32
55mkendp(struct usb_s *cntl, u8 devaddr, u8 ep, u8 lowspeed, u8 maxsize)
56{
57 u8 bus = cntl-USBControllers;
58 u8 size = __ffs(maxsize);
59 return (size<<25) | (lowspeed<<24) | (bus<<16) | (devaddr<<8) | ep;
60}
61
62static inline u8 endp2ep(u32 endp) {
63 return endp;
64}
65static inline u8 endp2devaddr(u32 endp) {
66 return endp>>8;
67}
68static inline struct usb_s *endp2cntl(u32 endp) {
69 u8 bus = endp>>16;
70 return &USBControllers[bus];
71}
72static inline u8 endp2speed(u32 endp) {
73 return (endp>>24) & 1;
74}
75static inline u8 endp2maxsize(u32 endp) {
76 return 1 << (endp>>25);
77}
78
79
80/****************************************************************
81 * usb structs and flags
82 ****************************************************************/
83
Kevin O'Connor8bbc79c2010-02-14 12:16:32 -050084// USB mandated timings (in ms)
85#define USB_TIME_SIGATT 100
86#define USB_TIME_ATTDB 100
87#define USB_TIME_DRST 10
88#define USB_TIME_DRSTR 50
89#define USB_TIME_RSTRCY 10
90
91#define USB_TIME_SETADDR_RECOVERY 2
92
Kevin O'Connor114592f2009-09-28 21:32:08 -040093#define USB_PID_OUT 0xe1
94#define USB_PID_IN 0x69
95#define USB_PID_SETUP 0x2d
96
97#define USB_DIR_OUT 0 /* to device */
98#define USB_DIR_IN 0x80 /* to host */
99
100#define USB_TYPE_MASK (0x03 << 5)
101#define USB_TYPE_STANDARD (0x00 << 5)
102#define USB_TYPE_CLASS (0x01 << 5)
103#define USB_TYPE_VENDOR (0x02 << 5)
104#define USB_TYPE_RESERVED (0x03 << 5)
105
106#define USB_RECIP_MASK 0x1f
107#define USB_RECIP_DEVICE 0x00
108#define USB_RECIP_INTERFACE 0x01
109#define USB_RECIP_ENDPOINT 0x02
110#define USB_RECIP_OTHER 0x03
111
112#define USB_REQ_GET_STATUS 0x00
113#define USB_REQ_CLEAR_FEATURE 0x01
114#define USB_REQ_SET_FEATURE 0x03
115#define USB_REQ_SET_ADDRESS 0x05
116#define USB_REQ_GET_DESCRIPTOR 0x06
117#define USB_REQ_SET_DESCRIPTOR 0x07
118#define USB_REQ_GET_CONFIGURATION 0x08
119#define USB_REQ_SET_CONFIGURATION 0x09
120#define USB_REQ_GET_INTERFACE 0x0A
121#define USB_REQ_SET_INTERFACE 0x0B
122#define USB_REQ_SYNCH_FRAME 0x0C
123
124struct usb_ctrlrequest {
125 u8 bRequestType;
126 u8 bRequest;
127 u16 wValue;
128 u16 wIndex;
129 u16 wLength;
130} PACKED;
131
132#define USB_DT_DEVICE 0x01
133#define USB_DT_CONFIG 0x02
134#define USB_DT_STRING 0x03
135#define USB_DT_INTERFACE 0x04
136#define USB_DT_ENDPOINT 0x05
137#define USB_DT_DEVICE_QUALIFIER 0x06
138#define USB_DT_OTHER_SPEED_CONFIG 0x07
139
140struct usb_device_descriptor {
141 u8 bLength;
142 u8 bDescriptorType;
143
144 u16 bcdUSB;
145 u8 bDeviceClass;
146 u8 bDeviceSubClass;
147 u8 bDeviceProtocol;
148 u8 bMaxPacketSize0;
149 u16 idVendor;
150 u16 idProduct;
151 u16 bcdDevice;
152 u8 iManufacturer;
153 u8 iProduct;
154 u8 iSerialNumber;
155 u8 bNumConfigurations;
156} PACKED;
157
158#define USB_CLASS_PER_INTERFACE 0 /* for DeviceClass */
159#define USB_CLASS_AUDIO 1
160#define USB_CLASS_COMM 2
161#define USB_CLASS_HID 3
162#define USB_CLASS_PHYSICAL 5
163#define USB_CLASS_STILL_IMAGE 6
164#define USB_CLASS_PRINTER 7
165#define USB_CLASS_MASS_STORAGE 8
166#define USB_CLASS_HUB 9
167
168struct usb_config_descriptor {
169 u8 bLength;
170 u8 bDescriptorType;
171
172 u16 wTotalLength;
173 u8 bNumInterfaces;
174 u8 bConfigurationValue;
175 u8 iConfiguration;
176 u8 bmAttributes;
177 u8 bMaxPower;
178} PACKED;
179
180struct usb_interface_descriptor {
181 u8 bLength;
182 u8 bDescriptorType;
183
184 u8 bInterfaceNumber;
185 u8 bAlternateSetting;
186 u8 bNumEndpoints;
187 u8 bInterfaceClass;
188 u8 bInterfaceSubClass;
189 u8 bInterfaceProtocol;
190 u8 iInterface;
191} PACKED;
192
193struct usb_endpoint_descriptor {
194 u8 bLength;
195 u8 bDescriptorType;
196
197 u8 bEndpointAddress;
198 u8 bmAttributes;
199 u16 wMaxPacketSize;
200 u8 bInterval;
201} PACKED;
202
203#define USB_ENDPOINT_NUMBER_MASK 0x0f /* in bEndpointAddress */
204#define USB_ENDPOINT_DIR_MASK 0x80
205
206#define USB_ENDPOINT_XFERTYPE_MASK 0x03 /* in bmAttributes */
207#define USB_ENDPOINT_XFER_CONTROL 0
208#define USB_ENDPOINT_XFER_ISOC 1
209#define USB_ENDPOINT_XFER_BULK 2
210#define USB_ENDPOINT_XFER_INT 3
211#define USB_ENDPOINT_MAX_ADJUSTABLE 0x80
212
213#endif // usb.h