blob: 883c60895c5654f4366f7acd4928a9390ffcf925 [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;
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 u8 type;
39 u8 maxaddr;
Kevin O'Connor114592f2009-09-28 21:32:08 -040040};
41
Kevin O'Connord28b0fe2010-03-28 15:11:19 -040042// Information for enumerating USB hubs
43struct usbhub_s {
44 struct usbhub_op_s *op;
Kevin O'Connorea274782012-03-08 07:49:09 -050045 struct usbdevice_s *usbdev;
Kevin O'Connord28b0fe2010-03-28 15:11:19 -040046 struct usb_s *cntl;
47 struct mutex_s lock;
48 u32 powerwait;
49 u32 port;
50 u32 threads;
51 u32 portcount;
52 u32 devcount;
53};
54
55// Hub callback (32bit) info
56struct usbhub_op_s {
57 int (*detect)(struct usbhub_s *hub, u32 port);
58 int (*reset)(struct usbhub_s *hub, u32 port);
59 void (*disconnect)(struct usbhub_s *hub, u32 port);
60};
61
Gerd Hoffmanne144bb72013-06-03 16:30:18 +020062#define USB_TYPE_UHCI 1
63#define USB_TYPE_OHCI 2
64#define USB_TYPE_EHCI 3
65#define USB_TYPE_XHCI 4
Kevin O'Connor190cc622010-03-09 19:43:52 -050066
Gerd Hoffmanne144bb72013-06-03 16:30:18 +020067#define USB_FULLSPEED 0
68#define USB_LOWSPEED 1
69#define USB_HIGHSPEED 2
70#define USB_SUPERSPEED 3
Kevin O'Connor59f02832009-10-12 10:09:15 -040071
Gerd Hoffmanne144bb72013-06-03 16:30:18 +020072#define USB_MAXADDR 127
Kevin O'Connor114592f2009-09-28 21:32:08 -040073
Kevin O'Connor114592f2009-09-28 21:32:08 -040074
75/****************************************************************
76 * usb structs and flags
77 ****************************************************************/
78
Kevin O'Connor8bbc79c2010-02-14 12:16:32 -050079// USB mandated timings (in ms)
80#define USB_TIME_SIGATT 100
81#define USB_TIME_ATTDB 100
82#define USB_TIME_DRST 10
83#define USB_TIME_DRSTR 50
84#define USB_TIME_RSTRCY 10
85
86#define USB_TIME_SETADDR_RECOVERY 2
87
Kevin O'Connor114592f2009-09-28 21:32:08 -040088#define USB_PID_OUT 0xe1
89#define USB_PID_IN 0x69
90#define USB_PID_SETUP 0x2d
91
92#define USB_DIR_OUT 0 /* to device */
93#define USB_DIR_IN 0x80 /* to host */
94
95#define USB_TYPE_MASK (0x03 << 5)
96#define USB_TYPE_STANDARD (0x00 << 5)
97#define USB_TYPE_CLASS (0x01 << 5)
98#define USB_TYPE_VENDOR (0x02 << 5)
99#define USB_TYPE_RESERVED (0x03 << 5)
100
101#define USB_RECIP_MASK 0x1f
102#define USB_RECIP_DEVICE 0x00
103#define USB_RECIP_INTERFACE 0x01
104#define USB_RECIP_ENDPOINT 0x02
105#define USB_RECIP_OTHER 0x03
106
107#define USB_REQ_GET_STATUS 0x00
108#define USB_REQ_CLEAR_FEATURE 0x01
109#define USB_REQ_SET_FEATURE 0x03
110#define USB_REQ_SET_ADDRESS 0x05
111#define USB_REQ_GET_DESCRIPTOR 0x06
112#define USB_REQ_SET_DESCRIPTOR 0x07
113#define USB_REQ_GET_CONFIGURATION 0x08
114#define USB_REQ_SET_CONFIGURATION 0x09
115#define USB_REQ_GET_INTERFACE 0x0A
116#define USB_REQ_SET_INTERFACE 0x0B
117#define USB_REQ_SYNCH_FRAME 0x0C
118
119struct usb_ctrlrequest {
120 u8 bRequestType;
121 u8 bRequest;
122 u16 wValue;
123 u16 wIndex;
124 u16 wLength;
125} PACKED;
126
127#define USB_DT_DEVICE 0x01
128#define USB_DT_CONFIG 0x02
129#define USB_DT_STRING 0x03
130#define USB_DT_INTERFACE 0x04
131#define USB_DT_ENDPOINT 0x05
132#define USB_DT_DEVICE_QUALIFIER 0x06
133#define USB_DT_OTHER_SPEED_CONFIG 0x07
Gerd Hoffmannee9b84f2013-06-13 14:23:08 +0200134#define USB_DT_ENDPOINT_COMPANION 0x30
Kevin O'Connor114592f2009-09-28 21:32:08 -0400135
136struct usb_device_descriptor {
137 u8 bLength;
138 u8 bDescriptorType;
139
140 u16 bcdUSB;
141 u8 bDeviceClass;
142 u8 bDeviceSubClass;
143 u8 bDeviceProtocol;
144 u8 bMaxPacketSize0;
145 u16 idVendor;
146 u16 idProduct;
147 u16 bcdDevice;
148 u8 iManufacturer;
149 u8 iProduct;
150 u8 iSerialNumber;
151 u8 bNumConfigurations;
152} PACKED;
153
154#define USB_CLASS_PER_INTERFACE 0 /* for DeviceClass */
155#define USB_CLASS_AUDIO 1
156#define USB_CLASS_COMM 2
157#define USB_CLASS_HID 3
158#define USB_CLASS_PHYSICAL 5
159#define USB_CLASS_STILL_IMAGE 6
160#define USB_CLASS_PRINTER 7
161#define USB_CLASS_MASS_STORAGE 8
162#define USB_CLASS_HUB 9
163
164struct usb_config_descriptor {
165 u8 bLength;
166 u8 bDescriptorType;
167
168 u16 wTotalLength;
169 u8 bNumInterfaces;
170 u8 bConfigurationValue;
171 u8 iConfiguration;
172 u8 bmAttributes;
173 u8 bMaxPower;
174} PACKED;
175
176struct usb_interface_descriptor {
177 u8 bLength;
178 u8 bDescriptorType;
179
180 u8 bInterfaceNumber;
181 u8 bAlternateSetting;
182 u8 bNumEndpoints;
183 u8 bInterfaceClass;
184 u8 bInterfaceSubClass;
185 u8 bInterfaceProtocol;
186 u8 iInterface;
187} PACKED;
188
189struct usb_endpoint_descriptor {
190 u8 bLength;
191 u8 bDescriptorType;
192
193 u8 bEndpointAddress;
194 u8 bmAttributes;
195 u16 wMaxPacketSize;
196 u8 bInterval;
197} PACKED;
198
199#define USB_ENDPOINT_NUMBER_MASK 0x0f /* in bEndpointAddress */
200#define USB_ENDPOINT_DIR_MASK 0x80
201
202#define USB_ENDPOINT_XFERTYPE_MASK 0x03 /* in bmAttributes */
203#define USB_ENDPOINT_XFER_CONTROL 0
204#define USB_ENDPOINT_XFER_ISOC 1
205#define USB_ENDPOINT_XFER_BULK 2
206#define USB_ENDPOINT_XFER_INT 3
207#define USB_ENDPOINT_MAX_ADJUSTABLE 0x80
208
Kevin O'Connor4547eb92010-02-28 02:17:28 -0500209
210/****************************************************************
Gerd Hoffmann5208d932012-07-20 10:59:23 +0200211 * usb mass storage flags
212 ****************************************************************/
213
214#define US_SC_ATAPI_8020 0x02
215#define US_SC_ATAPI_8070 0x05
216#define US_SC_SCSI 0x06
217
Gerd Hoffmanne53e30d2012-07-20 10:59:24 +0200218#define US_PR_BULK 0x50 /* bulk-only transport */
219#define US_PR_UAS 0x62 /* usb attached scsi */
Gerd Hoffmann5208d932012-07-20 10:59:23 +0200220
221/****************************************************************
Kevin O'Connor4547eb92010-02-28 02:17:28 -0500222 * function defs
223 ****************************************************************/
224
225// usb.c
Kevin O'Connorc3d96c22012-03-10 09:03:25 -0500226struct usb_pipe *usb_alloc_pipe(struct usbdevice_s *usbdev
227 , struct usb_endpoint_descriptor *epdesc);
Kevin O'Connorfbf66f12012-03-10 09:13:58 -0500228int usb_send_bulk(struct usb_pipe *pipe, int dir, void *data, int datasize);
229int usb_poll_intr(struct usb_pipe *pipe, void *data);
230int send_default_control(struct usb_pipe *pipe, const struct usb_ctrlrequest *req
231 , void *data);
232void free_pipe(struct usb_pipe *pipe);
233struct usb_pipe *usb_getFreePipe(struct usb_s *cntl, u8 eptype);
234void usb_desc2pipe(struct usb_pipe *pipe, struct usbdevice_s *usbdev
235 , struct usb_endpoint_descriptor *epdesc);
Kevin O'Connoreb50eae2012-03-10 08:48:31 -0500236int usb_getFrameExp(struct usbdevice_s *usbdev
237 , struct usb_endpoint_descriptor *epdesc);
Kevin O'Connor6a8e8952012-03-08 07:20:30 -0500238struct usb_endpoint_descriptor *findEndPointDesc(struct usbdevice_s *usbdev
239 , int type, int dir);
Kevin O'Connorfbf66f12012-03-10 09:13:58 -0500240void usb_enumerate(struct usbhub_s *hub);
241void usb_setup(void);
Kevin O'Connor4547eb92010-02-28 02:17:28 -0500242
Kevin O'Connor114592f2009-09-28 21:32:08 -0400243#endif // usb.h