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