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