blob: a43e82995d893f8f981efea7e5361b32bad79c02 [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'Connor8ebcac02010-03-09 19:58:23 -05005#include "util.h" // struct mutex_s
6
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;
25 u32 port;
Kevin O'Connor6a8e8952012-03-08 07:20:30 -050026 struct usb_config_descriptor *config;
27 struct usb_interface_descriptor *iface;
28 int imax;
Kevin O'Connor4f74dad2012-03-06 08:20:40 -050029 u8 speed;
Kevin O'Connor77ab4e12012-03-08 08:44:32 -050030 u8 devaddr;
Kevin O'Connor4f74dad2012-03-06 08:20:40 -050031};
32
Kevin O'Connor406fad62010-02-28 02:23:19 -050033// Common information for usb controllers.
Kevin O'Connor114592f2009-09-28 21:32:08 -040034struct usb_s {
Kevin O'Connor98af3532012-02-20 12:54:49 -050035 struct usb_pipe *freelist;
Kevin O'Connor8ebcac02010-03-09 19:58:23 -050036 struct mutex_s resetlock;
Kevin O'Connor8ff8e012011-07-09 14:11:21 -040037 struct pci_device *pci;
Kevin O'Connor406fad62010-02-28 02:23:19 -050038 int busid;
39 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
Kevin O'Connor59f02832009-10-12 10:09:15 -040063#define USB_TYPE_UHCI 1
64#define USB_TYPE_OHCI 2
Kevin O'Connor190cc622010-03-09 19:43:52 -050065#define USB_TYPE_EHCI 3
66
67#define USB_FULLSPEED 0
68#define USB_LOWSPEED 1
69#define USB_HIGHSPEED 2
Kevin O'Connor59f02832009-10-12 10:09:15 -040070
Kevin O'Connor114592f2009-09-28 21:32:08 -040071#define USB_MAXADDR 127
72
Kevin O'Connor114592f2009-09-28 21:32:08 -040073
74/****************************************************************
75 * usb structs and flags
76 ****************************************************************/
77
Kevin O'Connor8bbc79c2010-02-14 12:16:32 -050078// USB mandated timings (in ms)
79#define USB_TIME_SIGATT 100
80#define USB_TIME_ATTDB 100
81#define USB_TIME_DRST 10
82#define USB_TIME_DRSTR 50
83#define USB_TIME_RSTRCY 10
84
85#define USB_TIME_SETADDR_RECOVERY 2
86
Kevin O'Connor114592f2009-09-28 21:32:08 -040087#define USB_PID_OUT 0xe1
88#define USB_PID_IN 0x69
89#define USB_PID_SETUP 0x2d
90
91#define USB_DIR_OUT 0 /* to device */
92#define USB_DIR_IN 0x80 /* to host */
93
94#define USB_TYPE_MASK (0x03 << 5)
95#define USB_TYPE_STANDARD (0x00 << 5)
96#define USB_TYPE_CLASS (0x01 << 5)
97#define USB_TYPE_VENDOR (0x02 << 5)
98#define USB_TYPE_RESERVED (0x03 << 5)
99
100#define USB_RECIP_MASK 0x1f
101#define USB_RECIP_DEVICE 0x00
102#define USB_RECIP_INTERFACE 0x01
103#define USB_RECIP_ENDPOINT 0x02
104#define USB_RECIP_OTHER 0x03
105
106#define USB_REQ_GET_STATUS 0x00
107#define USB_REQ_CLEAR_FEATURE 0x01
108#define USB_REQ_SET_FEATURE 0x03
109#define USB_REQ_SET_ADDRESS 0x05
110#define USB_REQ_GET_DESCRIPTOR 0x06
111#define USB_REQ_SET_DESCRIPTOR 0x07
112#define USB_REQ_GET_CONFIGURATION 0x08
113#define USB_REQ_SET_CONFIGURATION 0x09
114#define USB_REQ_GET_INTERFACE 0x0A
115#define USB_REQ_SET_INTERFACE 0x0B
116#define USB_REQ_SYNCH_FRAME 0x0C
117
118struct usb_ctrlrequest {
119 u8 bRequestType;
120 u8 bRequest;
121 u16 wValue;
122 u16 wIndex;
123 u16 wLength;
124} PACKED;
125
126#define USB_DT_DEVICE 0x01
127#define USB_DT_CONFIG 0x02
128#define USB_DT_STRING 0x03
129#define USB_DT_INTERFACE 0x04
130#define USB_DT_ENDPOINT 0x05
131#define USB_DT_DEVICE_QUALIFIER 0x06
132#define USB_DT_OTHER_SPEED_CONFIG 0x07
133
134struct usb_device_descriptor {
135 u8 bLength;
136 u8 bDescriptorType;
137
138 u16 bcdUSB;
139 u8 bDeviceClass;
140 u8 bDeviceSubClass;
141 u8 bDeviceProtocol;
142 u8 bMaxPacketSize0;
143 u16 idVendor;
144 u16 idProduct;
145 u16 bcdDevice;
146 u8 iManufacturer;
147 u8 iProduct;
148 u8 iSerialNumber;
149 u8 bNumConfigurations;
150} PACKED;
151
152#define USB_CLASS_PER_INTERFACE 0 /* for DeviceClass */
153#define USB_CLASS_AUDIO 1
154#define USB_CLASS_COMM 2
155#define USB_CLASS_HID 3
156#define USB_CLASS_PHYSICAL 5
157#define USB_CLASS_STILL_IMAGE 6
158#define USB_CLASS_PRINTER 7
159#define USB_CLASS_MASS_STORAGE 8
160#define USB_CLASS_HUB 9
161
162struct usb_config_descriptor {
163 u8 bLength;
164 u8 bDescriptorType;
165
166 u16 wTotalLength;
167 u8 bNumInterfaces;
168 u8 bConfigurationValue;
169 u8 iConfiguration;
170 u8 bmAttributes;
171 u8 bMaxPower;
172} PACKED;
173
174struct usb_interface_descriptor {
175 u8 bLength;
176 u8 bDescriptorType;
177
178 u8 bInterfaceNumber;
179 u8 bAlternateSetting;
180 u8 bNumEndpoints;
181 u8 bInterfaceClass;
182 u8 bInterfaceSubClass;
183 u8 bInterfaceProtocol;
184 u8 iInterface;
185} PACKED;
186
187struct usb_endpoint_descriptor {
188 u8 bLength;
189 u8 bDescriptorType;
190
191 u8 bEndpointAddress;
192 u8 bmAttributes;
193 u16 wMaxPacketSize;
194 u8 bInterval;
195} PACKED;
196
197#define USB_ENDPOINT_NUMBER_MASK 0x0f /* in bEndpointAddress */
198#define USB_ENDPOINT_DIR_MASK 0x80
199
200#define USB_ENDPOINT_XFERTYPE_MASK 0x03 /* in bmAttributes */
201#define USB_ENDPOINT_XFER_CONTROL 0
202#define USB_ENDPOINT_XFER_ISOC 1
203#define USB_ENDPOINT_XFER_BULK 2
204#define USB_ENDPOINT_XFER_INT 3
205#define USB_ENDPOINT_MAX_ADJUSTABLE 0x80
206
Kevin O'Connor4547eb92010-02-28 02:17:28 -0500207
208/****************************************************************
Gerd Hoffmann5208d932012-07-20 10:59:23 +0200209 * usb mass storage flags
210 ****************************************************************/
211
212#define US_SC_ATAPI_8020 0x02
213#define US_SC_ATAPI_8070 0x05
214#define US_SC_SCSI 0x06
215
Gerd Hoffmanne53e30d2012-07-20 10:59:24 +0200216#define US_PR_BULK 0x50 /* bulk-only transport */
217#define US_PR_UAS 0x62 /* usb attached scsi */
Gerd Hoffmann5208d932012-07-20 10:59:23 +0200218
219/****************************************************************
Kevin O'Connor4547eb92010-02-28 02:17:28 -0500220 * function defs
221 ****************************************************************/
222
223// usb.c
Kevin O'Connorc3d96c22012-03-10 09:03:25 -0500224struct usb_pipe *usb_alloc_pipe(struct usbdevice_s *usbdev
225 , struct usb_endpoint_descriptor *epdesc);
Kevin O'Connorfbf66f12012-03-10 09:13:58 -0500226int usb_send_bulk(struct usb_pipe *pipe, int dir, void *data, int datasize);
227int usb_poll_intr(struct usb_pipe *pipe, void *data);
228int send_default_control(struct usb_pipe *pipe, const struct usb_ctrlrequest *req
229 , void *data);
230void free_pipe(struct usb_pipe *pipe);
231struct usb_pipe *usb_getFreePipe(struct usb_s *cntl, u8 eptype);
232void usb_desc2pipe(struct usb_pipe *pipe, struct usbdevice_s *usbdev
233 , struct usb_endpoint_descriptor *epdesc);
Kevin O'Connoreb50eae2012-03-10 08:48:31 -0500234int usb_getFrameExp(struct usbdevice_s *usbdev
235 , struct usb_endpoint_descriptor *epdesc);
Kevin O'Connor6a8e8952012-03-08 07:20:30 -0500236struct usb_endpoint_descriptor *findEndPointDesc(struct usbdevice_s *usbdev
237 , int type, int dir);
Kevin O'Connorfbf66f12012-03-10 09:13:58 -0500238void usb_enumerate(struct usbhub_s *hub);
239void usb_setup(void);
Kevin O'Connor4547eb92010-02-28 02:17:28 -0500240
Kevin O'Connor114592f2009-09-28 21:32:08 -0400241#endif // usb.h