blob: 61962964448a11deac930d3f2642ca9cdd5fad74 [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'Connor3df600b2013-09-14 19:28:55 -04005#include "stacks.h" // struct mutex_s
Kevin O'Connor8ebcac02010-03-09 19:58:23 -05006
Kevin O'Connord28b0fe2010-03-28 15:11:19 -04007// Information on a USB end point.
Kevin O'Connor357bdfa2010-02-26 08:57:13 -05008struct usb_pipe {
Kevin O'Connor98af3532012-02-20 12:54:49 -05009 union {
10 struct usb_s *cntl;
11 struct usb_pipe *freenext;
12 };
Kevin O'Connor4547eb92010-02-28 02:17:28 -050013 u8 type;
14 u8 ep;
15 u8 devaddr;
Kevin O'Connor190cc622010-03-09 19:43:52 -050016 u8 speed;
Kevin O'Connor4547eb92010-02-28 02:17:28 -050017 u16 maxpacket;
Kevin O'Connor01de9bd2012-03-05 22:41:21 -050018 u8 eptype;
Kevin O'Connor357bdfa2010-02-26 08:57:13 -050019};
20
Kevin O'Connor4f74dad2012-03-06 08:20:40 -050021// Common information for usb devices.
22struct usbdevice_s {
23 struct usbhub_s *hub;
24 struct usb_pipe *defpipe;
Kevin O'Connor81ee3bb2013-12-27 21:09:53 -050025 u32 slotid;
Kevin O'Connor4f74dad2012-03-06 08:20:40 -050026 u32 port;
Kevin O'Connor6a8e8952012-03-08 07:20:30 -050027 struct usb_config_descriptor *config;
28 struct usb_interface_descriptor *iface;
29 int imax;
Kevin O'Connor4f74dad2012-03-06 08:20:40 -050030 u8 speed;
Kevin O'Connor77ab4e12012-03-08 08:44:32 -050031 u8 devaddr;
Kevin O'Connor4f74dad2012-03-06 08:20:40 -050032};
33
Kevin O'Connor406fad62010-02-28 02:23:19 -050034// Common information for usb controllers.
Kevin O'Connor114592f2009-09-28 21:32:08 -040035struct usb_s {
Kevin O'Connor98af3532012-02-20 12:54:49 -050036 struct usb_pipe *freelist;
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050037 struct mutex_s resetlock;
Kevin O'Connor8ff8e012011-07-09 14:11:21 -040038 struct pci_device *pci;
Kevin O'Connor406fad62010-02-28 02:23:19 -050039 u8 type;
40 u8 maxaddr;
Kevin O'Connor114592f2009-09-28 21:32:08 -040041};
42
Kevin O'Connord28b0fe2010-03-28 15:11:19 -040043// Information for enumerating USB hubs
44struct usbhub_s {
45 struct usbhub_op_s *op;
Kevin O'Connorea274782012-03-08 07:49:09 -050046 struct usbdevice_s *usbdev;
Kevin O'Connord28b0fe2010-03-28 15:11:19 -040047 struct usb_s *cntl;
48 struct mutex_s lock;
49 u32 powerwait;
50 u32 port;
51 u32 threads;
52 u32 portcount;
53 u32 devcount;
54};
55
56// Hub callback (32bit) info
57struct usbhub_op_s {
58 int (*detect)(struct usbhub_s *hub, u32 port);
59 int (*reset)(struct usbhub_s *hub, u32 port);
60 void (*disconnect)(struct usbhub_s *hub, u32 port);
61};
62
Gerd Hoffmanne144bb72013-06-03 16:30:18 +020063#define USB_TYPE_UHCI 1
64#define USB_TYPE_OHCI 2
65#define USB_TYPE_EHCI 3
66#define USB_TYPE_XHCI 4
Kevin O'Connor190cc622010-03-09 19:43:52 -050067
Gerd Hoffmanne144bb72013-06-03 16:30:18 +020068#define USB_FULLSPEED 0
69#define USB_LOWSPEED 1
70#define USB_HIGHSPEED 2
71#define USB_SUPERSPEED 3
Kevin O'Connor59f02832009-10-12 10:09:15 -040072
Gerd Hoffmanne144bb72013-06-03 16:30:18 +020073#define USB_MAXADDR 127
Kevin O'Connor114592f2009-09-28 21:32:08 -040074
Kevin O'Connor114592f2009-09-28 21:32:08 -040075
76/****************************************************************
77 * usb structs and flags
78 ****************************************************************/
79
Kevin O'Connor8bbc79c2010-02-14 12:16:32 -050080// USB mandated timings (in ms)
81#define USB_TIME_SIGATT 100
82#define USB_TIME_ATTDB 100
83#define USB_TIME_DRST 10
84#define USB_TIME_DRSTR 50
85#define USB_TIME_RSTRCY 10
86
87#define USB_TIME_SETADDR_RECOVERY 2
88
Kevin O'Connor114592f2009-09-28 21:32:08 -040089#define USB_PID_OUT 0xe1
90#define USB_PID_IN 0x69
91#define USB_PID_SETUP 0x2d
92
93#define USB_DIR_OUT 0 /* to device */
94#define USB_DIR_IN 0x80 /* to host */
95
96#define USB_TYPE_MASK (0x03 << 5)
97#define USB_TYPE_STANDARD (0x00 << 5)
98#define USB_TYPE_CLASS (0x01 << 5)
99#define USB_TYPE_VENDOR (0x02 << 5)
100#define USB_TYPE_RESERVED (0x03 << 5)
101
102#define USB_RECIP_MASK 0x1f
103#define USB_RECIP_DEVICE 0x00
104#define USB_RECIP_INTERFACE 0x01
105#define USB_RECIP_ENDPOINT 0x02
106#define USB_RECIP_OTHER 0x03
107
108#define USB_REQ_GET_STATUS 0x00
109#define USB_REQ_CLEAR_FEATURE 0x01
110#define USB_REQ_SET_FEATURE 0x03
111#define USB_REQ_SET_ADDRESS 0x05
112#define USB_REQ_GET_DESCRIPTOR 0x06
113#define USB_REQ_SET_DESCRIPTOR 0x07
114#define USB_REQ_GET_CONFIGURATION 0x08
115#define USB_REQ_SET_CONFIGURATION 0x09
116#define USB_REQ_GET_INTERFACE 0x0A
117#define USB_REQ_SET_INTERFACE 0x0B
118#define USB_REQ_SYNCH_FRAME 0x0C
119
120struct usb_ctrlrequest {
121 u8 bRequestType;
122 u8 bRequest;
123 u16 wValue;
124 u16 wIndex;
125 u16 wLength;
126} PACKED;
127
128#define USB_DT_DEVICE 0x01
129#define USB_DT_CONFIG 0x02
130#define USB_DT_STRING 0x03
131#define USB_DT_INTERFACE 0x04
132#define USB_DT_ENDPOINT 0x05
133#define USB_DT_DEVICE_QUALIFIER 0x06
134#define USB_DT_OTHER_SPEED_CONFIG 0x07
Gerd Hoffmannee9b84f2013-06-13 14:23:08 +0200135#define USB_DT_ENDPOINT_COMPANION 0x30
Kevin O'Connor114592f2009-09-28 21:32:08 -0400136
137struct usb_device_descriptor {
138 u8 bLength;
139 u8 bDescriptorType;
140
141 u16 bcdUSB;
142 u8 bDeviceClass;
143 u8 bDeviceSubClass;
144 u8 bDeviceProtocol;
145 u8 bMaxPacketSize0;
146 u16 idVendor;
147 u16 idProduct;
148 u16 bcdDevice;
149 u8 iManufacturer;
150 u8 iProduct;
151 u8 iSerialNumber;
152 u8 bNumConfigurations;
153} PACKED;
154
155#define USB_CLASS_PER_INTERFACE 0 /* for DeviceClass */
156#define USB_CLASS_AUDIO 1
157#define USB_CLASS_COMM 2
158#define USB_CLASS_HID 3
159#define USB_CLASS_PHYSICAL 5
160#define USB_CLASS_STILL_IMAGE 6
161#define USB_CLASS_PRINTER 7
162#define USB_CLASS_MASS_STORAGE 8
163#define USB_CLASS_HUB 9
164
165struct usb_config_descriptor {
166 u8 bLength;
167 u8 bDescriptorType;
168
169 u16 wTotalLength;
170 u8 bNumInterfaces;
171 u8 bConfigurationValue;
172 u8 iConfiguration;
173 u8 bmAttributes;
174 u8 bMaxPower;
175} PACKED;
176
177struct usb_interface_descriptor {
178 u8 bLength;
179 u8 bDescriptorType;
180
181 u8 bInterfaceNumber;
182 u8 bAlternateSetting;
183 u8 bNumEndpoints;
184 u8 bInterfaceClass;
185 u8 bInterfaceSubClass;
186 u8 bInterfaceProtocol;
187 u8 iInterface;
188} PACKED;
189
190struct usb_endpoint_descriptor {
191 u8 bLength;
192 u8 bDescriptorType;
193
194 u8 bEndpointAddress;
195 u8 bmAttributes;
196 u16 wMaxPacketSize;
197 u8 bInterval;
198} PACKED;
199
200#define USB_ENDPOINT_NUMBER_MASK 0x0f /* in bEndpointAddress */
201#define USB_ENDPOINT_DIR_MASK 0x80
202
203#define USB_ENDPOINT_XFERTYPE_MASK 0x03 /* in bmAttributes */
204#define USB_ENDPOINT_XFER_CONTROL 0
205#define USB_ENDPOINT_XFER_ISOC 1
206#define USB_ENDPOINT_XFER_BULK 2
207#define USB_ENDPOINT_XFER_INT 3
208#define USB_ENDPOINT_MAX_ADJUSTABLE 0x80
209
Kevin O'Connor4547eb92010-02-28 02:17:28 -0500210
211/****************************************************************
Gerd Hoffmann5208d932012-07-20 10:59:23 +0200212 * usb mass storage flags
213 ****************************************************************/
214
215#define US_SC_ATAPI_8020 0x02
216#define US_SC_ATAPI_8070 0x05
217#define US_SC_SCSI 0x06
218
Gerd Hoffmanne53e30d2012-07-20 10:59:24 +0200219#define US_PR_BULK 0x50 /* bulk-only transport */
220#define US_PR_UAS 0x62 /* usb attached scsi */
Gerd Hoffmann5208d932012-07-20 10:59:23 +0200221
222/****************************************************************
Kevin O'Connor4547eb92010-02-28 02:17:28 -0500223 * function defs
224 ****************************************************************/
225
226// usb.c
Kevin O'Connorc3d96c22012-03-10 09:03:25 -0500227struct usb_pipe *usb_alloc_pipe(struct usbdevice_s *usbdev
228 , struct usb_endpoint_descriptor *epdesc);
Kevin O'Connorfbf66f12012-03-10 09:13:58 -0500229int usb_send_bulk(struct usb_pipe *pipe, int dir, void *data, int datasize);
230int usb_poll_intr(struct usb_pipe *pipe, void *data);
Kevin O'Connorde30dad2013-12-30 22:09:04 -0500231int usb_32bit_pipe(struct usb_pipe *pipe_fl);
Kevin O'Connorfbf66f12012-03-10 09:13:58 -0500232int send_default_control(struct usb_pipe *pipe, const struct usb_ctrlrequest *req
233 , void *data);
234void free_pipe(struct usb_pipe *pipe);
235struct usb_pipe *usb_getFreePipe(struct usb_s *cntl, u8 eptype);
236void usb_desc2pipe(struct usb_pipe *pipe, struct usbdevice_s *usbdev
237 , struct usb_endpoint_descriptor *epdesc);
Kevin O'Connoreb50eae2012-03-10 08:48:31 -0500238int usb_getFrameExp(struct usbdevice_s *usbdev
239 , struct usb_endpoint_descriptor *epdesc);
Kevin O'Connor6a8e8952012-03-08 07:20:30 -0500240struct usb_endpoint_descriptor *findEndPointDesc(struct usbdevice_s *usbdev
241 , int type, int dir);
Kevin O'Connorfbf66f12012-03-10 09:13:58 -0500242void usb_enumerate(struct usbhub_s *hub);
243void usb_setup(void);
Kevin O'Connor4547eb92010-02-28 02:17:28 -0500244
Kevin O'Connor114592f2009-09-28 21:32:08 -0400245#endif // usb.h