ba701819484857a23abc06b2217c925692b0fde6
[seabios.git] / src / usb.c
1 // Main code for handling USB controllers and devices.
2 //
3 // Copyright (C) 2009  Kevin O'Connor <kevin@koconnor.net>
4 //
5 // This file may be distributed under the terms of the GNU LGPLv3 license.
6
7 #include "util.h" // dprintf
8 #include "pci.h" // foreachpci
9 #include "config.h" // CONFIG_*
10 #include "pci_regs.h" // PCI_CLASS_REVISION
11 #include "pci_ids.h" // PCI_CLASS_SERIAL_USB_UHCI
12 #include "usb-uhci.h" // uhci_init
13 #include "usb-ohci.h" // ohci_init
14 #include "usb-hid.h" // usb_keyboard_setup
15 #include "usb-hub.h" // usb_hub_init
16 #include "usb-msc.h" // usb_msc_init
17 #include "usb.h" // struct usb_s
18 #include "biosvar.h" // GET_GLOBAL
19
20 struct usb_s USBControllers[16] VAR16VISIBLE;
21
22
23 /****************************************************************
24  * Controller function wrappers
25  ****************************************************************/
26
27 // Free an allocated control or bulk pipe.
28 void
29 free_pipe(struct usb_pipe *pipe)
30 {
31     ASSERT32FLAT();
32     if (!pipe)
33         return;
34     struct usb_s *cntl = endp2cntl(pipe->endp);
35     switch (cntl->type) {
36     default:
37     case USB_TYPE_UHCI:
38         return uhci_free_pipe(pipe);
39     case USB_TYPE_OHCI:
40         return ohci_free_pipe(pipe);
41     }
42 }
43
44 // Allocate a control pipe (which can only be used by 32bit code)
45 static struct usb_pipe *
46 alloc_control_pipe(u32 endp)
47 {
48     struct usb_s *cntl = endp2cntl(endp);
49     switch (cntl->type) {
50     default:
51     case USB_TYPE_UHCI:
52         return uhci_alloc_control_pipe(endp);
53     case USB_TYPE_OHCI:
54         return ohci_alloc_control_pipe(endp);
55     }
56 }
57
58 // Send a message on a control pipe using the default control descriptor.
59 static int
60 send_control(struct usb_pipe *pipe, int dir, const void *cmd, int cmdsize
61              , void *data, int datasize)
62 {
63     ASSERT32FLAT();
64     struct usb_s *cntl = endp2cntl(pipe->endp);
65     switch (cntl->type) {
66     default:
67     case USB_TYPE_UHCI:
68         return uhci_control(pipe, dir, cmd, cmdsize, data, datasize);
69     case USB_TYPE_OHCI:
70         return ohci_control(pipe, dir, cmd, cmdsize, data, datasize);
71     }
72 }
73
74 struct usb_pipe *
75 alloc_bulk_pipe(u32 endp)
76 {
77     struct usb_s *cntl = endp2cntl(endp);
78     switch (cntl->type) {
79     default:
80     case USB_TYPE_UHCI:
81         return uhci_alloc_bulk_pipe(endp);
82     case USB_TYPE_OHCI:
83         return NULL;
84     }
85 }
86
87 int
88 usb_send_bulk(struct usb_pipe *pipe, int dir, void *data, int datasize)
89 {
90     u32 endp = GET_FLATPTR(pipe->endp);
91     struct usb_s *cntl = endp2cntl(endp);
92     switch (cntl->type) {
93     default:
94     case USB_TYPE_UHCI:
95         return uhci_send_bulk(pipe, dir, data, datasize);
96     case USB_TYPE_OHCI:
97         return -1;
98     }
99 }
100
101 struct usb_pipe *
102 alloc_intr_pipe(u32 endp, int period)
103 {
104     struct usb_s *cntl = endp2cntl(endp);
105     // Find the exponential period of the requested time.
106     if (period <= 0)
107         period = 1;
108     int frameexp = __fls(period);
109     switch (cntl->type) {
110     default:
111     case USB_TYPE_UHCI:
112         return uhci_alloc_intr_pipe(endp, frameexp);
113     case USB_TYPE_OHCI:
114         return ohci_alloc_intr_pipe(endp, frameexp);
115     }
116 }
117
118 int noinline
119 usb_poll_intr(struct usb_pipe *pipe, void *data)
120 {
121     u32 endp = GET_FLATPTR(pipe->endp);
122     struct usb_s *cntl = endp2cntl(endp);
123     switch (GET_GLOBAL(cntl->type)) {
124     default:
125     case USB_TYPE_UHCI:
126         return uhci_poll_intr(pipe, data);
127     case USB_TYPE_OHCI:
128         return ohci_poll_intr(pipe, data);
129     }
130 }
131
132
133 /****************************************************************
134  * Helper functions
135  ****************************************************************/
136
137 // Find the first endpoing of a given type in an interface description.
138 struct usb_endpoint_descriptor *
139 findEndPointDesc(struct usb_interface_descriptor *iface, int imax
140                  , int type, int dir)
141 {
142     struct usb_endpoint_descriptor *epdesc = (void*)&iface[1];
143     for (;;) {
144         if ((void*)epdesc >= (void*)iface + imax
145             || epdesc->bDescriptorType == USB_DT_INTERFACE) {
146             return NULL;
147         }
148         if (epdesc->bDescriptorType == USB_DT_ENDPOINT
149             && (epdesc->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == dir
150             && (epdesc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == type)
151             return epdesc;
152         epdesc = (void*)epdesc + epdesc->bLength;
153     }
154 }
155
156 // Change endpoint characteristics of the default control pipe.
157 static void
158 usb_alter_control(struct usb_pipe *pipe, u32 endp)
159 {
160     pipe->endp = endp;
161 }
162
163 // Build an encoded "endp" from an endpoint descriptor.
164 u32
165 mkendpFromDesc(struct usb_pipe *pipe, struct usb_endpoint_descriptor *epdesc)
166 {
167     u32 endp = pipe->endp;
168     return mkendp(endp2cntl(endp), endp2devaddr(endp)
169                   , epdesc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK
170                   , endp2speed(endp), epdesc->wMaxPacketSize);
171 }
172
173 // Send a message to the default control pipe of a device.
174 int
175 send_default_control(struct usb_pipe *pipe, const struct usb_ctrlrequest *req
176                      , void *data)
177 {
178     return send_control(pipe, req->bRequestType & USB_DIR_IN
179                         , req, sizeof(*req), data, req->wLength);
180 }
181
182 // Get the first 8 bytes of the device descriptor.
183 static int
184 get_device_info8(struct usb_pipe *pipe, struct usb_device_descriptor *dinfo)
185 {
186     struct usb_ctrlrequest req;
187     req.bRequestType = USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_DEVICE;
188     req.bRequest = USB_REQ_GET_DESCRIPTOR;
189     req.wValue = USB_DT_DEVICE<<8;
190     req.wIndex = 0;
191     req.wLength = 8;
192     return send_default_control(pipe, &req, dinfo);
193 }
194
195 static struct usb_config_descriptor *
196 get_device_config(struct usb_pipe *pipe)
197 {
198     struct usb_config_descriptor cfg;
199
200     struct usb_ctrlrequest req;
201     req.bRequestType = USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_DEVICE;
202     req.bRequest = USB_REQ_GET_DESCRIPTOR;
203     req.wValue = USB_DT_CONFIG<<8;
204     req.wIndex = 0;
205     req.wLength = sizeof(cfg);
206     int ret = send_default_control(pipe, &req, &cfg);
207     if (ret)
208         return NULL;
209
210     void *config = malloc_tmphigh(cfg.wTotalLength);
211     if (!config)
212         return NULL;
213     req.wLength = cfg.wTotalLength;
214     ret = send_default_control(pipe, &req, config);
215     if (ret)
216         return NULL;
217     //hexdump(config, cfg.wTotalLength);
218     return config;
219 }
220
221 static int
222 set_configuration(struct usb_pipe *pipe, u16 val)
223 {
224     struct usb_ctrlrequest req;
225     req.bRequestType = USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE;
226     req.bRequest = USB_REQ_SET_CONFIGURATION;
227     req.wValue = val;
228     req.wIndex = 0;
229     req.wLength = 0;
230     return send_default_control(pipe, &req, NULL);
231 }
232
233
234 /****************************************************************
235  * Initialization and enumeration
236  ****************************************************************/
237
238 // Assign an address to a device in the default state on the given
239 // controller.
240 struct usb_pipe *
241 usb_set_address(struct usb_s *cntl, int lowspeed)
242 {
243     ASSERT32FLAT();
244     dprintf(3, "set_address %p\n", cntl);
245     if (cntl->maxaddr >= USB_MAXADDR)
246         return NULL;
247
248     struct usb_pipe *defpipe = cntl->defaultpipe;
249     u32 endp = mkendp(cntl, 0, 0, lowspeed, 8);
250     if (!defpipe) {
251         cntl->defaultpipe = defpipe = alloc_control_pipe(endp);
252         if (!defpipe)
253             return NULL;
254     }
255     usb_alter_control(defpipe, endp);
256
257     msleep(USB_TIME_RSTRCY);
258
259     struct usb_ctrlrequest req;
260     req.bRequestType = USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE;
261     req.bRequest = USB_REQ_SET_ADDRESS;
262     req.wValue = cntl->maxaddr + 1;
263     req.wIndex = 0;
264     req.wLength = 0;
265     int ret = send_default_control(defpipe, &req, NULL);
266     if (ret)
267         return NULL;
268
269     msleep(USB_TIME_SETADDR_RECOVERY);
270
271     cntl->maxaddr++;
272     endp = mkendp(cntl, cntl->maxaddr, 0, lowspeed, 8);
273     return alloc_control_pipe(endp);
274 }
275
276 // Called for every found device - see if a driver is available for
277 // this device and do setup if so.
278 int
279 configure_usb_device(struct usb_pipe *pipe)
280 {
281     ASSERT32FLAT();
282     struct usb_s *cntl = endp2cntl(pipe->endp);
283     dprintf(3, "config_usb: %p\n", cntl);
284
285     // Set the max packet size for endpoint 0 of this device.
286     struct usb_device_descriptor dinfo;
287     int ret = get_device_info8(pipe, &dinfo);
288     if (ret)
289         return 0;
290     dprintf(3, "device rev=%04x cls=%02x sub=%02x proto=%02x size=%02x\n"
291             , dinfo.bcdUSB, dinfo.bDeviceClass, dinfo.bDeviceSubClass
292             , dinfo.bDeviceProtocol, dinfo.bMaxPacketSize0);
293     if (dinfo.bMaxPacketSize0 < 8 || dinfo.bMaxPacketSize0 > 64)
294         return 0;
295     u32 endp = mkendp(cntl, endp2devaddr(pipe->endp), 0
296                       , endp2speed(pipe->endp), dinfo.bMaxPacketSize0);
297     usb_alter_control(pipe, endp);
298
299     // Get configuration
300     struct usb_config_descriptor *config = get_device_config(pipe);
301     if (!config)
302         return 0;
303
304     // Determine if a driver exists for this device - only look at the
305     // first interface of the first configuration.
306     struct usb_interface_descriptor *iface = (void*)(&config[1]);
307     if ((iface->bInterfaceClass != USB_CLASS_HID
308          || iface->bInterfaceSubClass != USB_INTERFACE_SUBCLASS_BOOT
309          || iface->bInterfaceProtocol != USB_INTERFACE_PROTOCOL_KEYBOARD)
310         && (iface->bInterfaceClass != USB_CLASS_MASS_STORAGE)
311         && (iface->bInterfaceClass != USB_CLASS_HUB))
312         // Not a supported device.
313         goto fail;
314
315     // Set the configuration.
316     ret = set_configuration(pipe, config->bConfigurationValue);
317     if (ret)
318         goto fail;
319
320     // Configure driver.
321     int imax = (void*)config + config->wTotalLength - (void*)iface;
322     if (iface->bInterfaceClass == USB_CLASS_HUB)
323         ret = usb_hub_init(pipe);
324     else if (iface->bInterfaceClass == USB_CLASS_MASS_STORAGE)
325         ret = usb_msc_init(pipe, iface, imax);
326     else
327         ret = usb_keyboard_init(pipe, iface, imax);
328     if (ret)
329         goto fail;
330
331     free(config);
332     return 1;
333 fail:
334     free(config);
335     return 0;
336 }
337
338 void
339 usb_setup(void)
340 {
341     ASSERT32FLAT();
342     if (! CONFIG_USB)
343         return;
344
345     dprintf(3, "init usb\n");
346
347     memset(&USBControllers, 0, sizeof(USBControllers));
348     usb_keyboard_setup();
349
350     // Look for USB controllers
351     int count = 0;
352     int bdf, max;
353     foreachpci(bdf, max) {
354         u32 code = pci_config_readl(bdf, PCI_CLASS_REVISION) >> 8;
355
356         if (code >> 8 != PCI_CLASS_SERIAL_USB)
357             continue;
358
359         struct usb_s *cntl = &USBControllers[count];
360         cntl->bdf = bdf;
361
362         if (code == PCI_CLASS_SERIAL_USB_UHCI)
363             run_thread(uhci_init, cntl);
364         else if (code == PCI_CLASS_SERIAL_USB_OHCI)
365             run_thread(ohci_init, cntl);
366         else
367             continue;
368
369         count++;
370         if (count >= ARRAY_SIZE(USBControllers))
371             break;
372     }
373 }