grml...
[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-ehci.h" // ehci_init
15 #include "usb-hid.h" // usb_keyboard_setup
16 #include "usb-hub.h" // usb_hub_init
17 #include "usb-msc.h" // usb_msc_init
18 #include "usb.h" // struct usb_s
19 #include "biosvar.h" // GET_GLOBAL
20
21
22 /****************************************************************
23  * Controller function wrappers
24  ****************************************************************/
25
26 // Free an allocated control or bulk pipe.
27 void
28 free_pipe(struct usb_pipe *pipe)
29 {
30     ASSERT32FLAT();
31     if (!pipe)
32         return;
33     switch (pipe->type) {
34     default:
35     case USB_TYPE_UHCI:
36         return uhci_free_pipe(pipe);
37     case USB_TYPE_OHCI:
38         return ohci_free_pipe(pipe);
39     case USB_TYPE_EHCI:
40         return ehci_free_pipe(pipe);
41     }
42 }
43
44 // Allocate a control pipe to a default endpoint (which can only be
45 // used by 32bit code)
46 static struct usb_pipe *
47 alloc_default_control_pipe(struct usb_pipe *dummy)
48 {
49     switch (dummy->type) {
50     default:
51     case USB_TYPE_UHCI:
52         return uhci_alloc_control_pipe(dummy);
53     case USB_TYPE_OHCI:
54         return ohci_alloc_control_pipe(dummy);
55     case USB_TYPE_EHCI:
56         return ehci_alloc_control_pipe(dummy);
57     }
58 }
59
60 // Send a message on a control pipe using the default control descriptor.
61 static int
62 send_control(struct usb_pipe *pipe, int dir, const void *cmd, int cmdsize
63              , void *data, int datasize)
64 {
65     ASSERT32FLAT();
66     switch (pipe->type) {
67     default:
68     case USB_TYPE_UHCI:
69         return uhci_control(pipe, dir, cmd, cmdsize, data, datasize);
70     case USB_TYPE_OHCI:
71         return ohci_control(pipe, dir, cmd, cmdsize, data, datasize);
72     case USB_TYPE_EHCI:
73         return ehci_control(pipe, dir, cmd, cmdsize, data, datasize);
74     }
75 }
76
77 // Fill "pipe" endpoint info from an endpoint descriptor.
78 static void
79 desc2pipe(struct usb_pipe *newpipe, struct usb_pipe *origpipe
80           , struct usb_endpoint_descriptor *epdesc)
81 {
82     memcpy(newpipe, origpipe, sizeof(*newpipe));
83     newpipe->ep = epdesc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
84     newpipe->maxpacket = epdesc->wMaxPacketSize;
85 }
86
87 struct usb_pipe *
88 alloc_bulk_pipe(struct usb_pipe *pipe, struct usb_endpoint_descriptor *epdesc)
89 {
90     struct usb_pipe dummy;
91     desc2pipe(&dummy, pipe, epdesc);
92     switch (pipe->type) {
93     default:
94     case USB_TYPE_UHCI:
95         return uhci_alloc_bulk_pipe(&dummy);
96     case USB_TYPE_OHCI:
97         return ohci_alloc_bulk_pipe(&dummy);
98     case USB_TYPE_EHCI:
99         return ehci_alloc_bulk_pipe(&dummy);
100     }
101 }
102
103 int
104 usb_send_bulk(struct usb_pipe *pipe_fl, int dir, void *data, int datasize)
105 {
106     switch (GET_FLATPTR(pipe_fl->type)) {
107     default:
108     case USB_TYPE_UHCI:
109         return uhci_send_bulk(pipe_fl, dir, data, datasize);
110     case USB_TYPE_OHCI:
111         return ohci_send_bulk(pipe_fl, dir, data, datasize);
112     case USB_TYPE_EHCI:
113         return ehci_send_bulk(pipe_fl, dir, data, datasize);
114     }
115 }
116
117 struct usb_pipe *
118 alloc_intr_pipe(struct usb_pipe *pipe, struct usb_endpoint_descriptor *epdesc)
119 {
120     struct usb_pipe dummy;
121     desc2pipe(&dummy, pipe, epdesc);
122     // Find the exponential period of the requested time.
123     int period = epdesc->bInterval;
124     int frameexp;
125     if (pipe->speed != USB_HIGHSPEED)
126         frameexp = (period <= 0) ? 0 : __fls(period);
127     else
128         frameexp = (period <= 4) ? 0 : period - 4;
129     switch (pipe->type) {
130     default:
131     case USB_TYPE_UHCI:
132         return uhci_alloc_intr_pipe(&dummy, frameexp);
133     case USB_TYPE_OHCI:
134         return ohci_alloc_intr_pipe(&dummy, frameexp);
135     case USB_TYPE_EHCI:
136         return ehci_alloc_intr_pipe(&dummy, frameexp);
137     }
138 }
139
140 int noinline
141 usb_poll_intr(struct usb_pipe *pipe_fl, void *data)
142 {
143     switch (GET_FLATPTR(pipe_fl->type)) {
144     default:
145     case USB_TYPE_UHCI:
146         return uhci_poll_intr(pipe_fl, data);
147     case USB_TYPE_OHCI:
148         return ohci_poll_intr(pipe_fl, data);
149     case USB_TYPE_EHCI:
150         return ehci_poll_intr(pipe_fl, data);
151     }
152 }
153
154
155 /****************************************************************
156  * Helper functions
157  ****************************************************************/
158
159 // Find the first endpoing of a given type in an interface description.
160 struct usb_endpoint_descriptor *
161 findEndPointDesc(struct usb_interface_descriptor *iface, int imax
162                  , int type, int dir)
163 {
164     struct usb_endpoint_descriptor *epdesc = (void*)&iface[1];
165     for (;;) {
166         if ((void*)epdesc >= (void*)iface + imax
167             || epdesc->bDescriptorType == USB_DT_INTERFACE) {
168             return NULL;
169         }
170         if (epdesc->bDescriptorType == USB_DT_ENDPOINT
171             && (epdesc->bEndpointAddress & USB_ENDPOINT_DIR_MASK) == dir
172             && (epdesc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == type)
173             return epdesc;
174         epdesc = (void*)epdesc + epdesc->bLength;
175     }
176 }
177
178 // Send a message to the default control pipe of a device.
179 int
180 send_default_control(struct usb_pipe *pipe, const struct usb_ctrlrequest *req
181                      , void *data)
182 {
183     return send_control(pipe, req->bRequestType & USB_DIR_IN
184                         , req, sizeof(*req), data, req->wLength);
185 }
186
187 // Get the first 8 bytes of the device descriptor.
188 static int
189 get_device_info8(struct usb_pipe *pipe, struct usb_device_descriptor *dinfo)
190 {
191     struct usb_ctrlrequest req;
192     req.bRequestType = USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_DEVICE;
193     req.bRequest = USB_REQ_GET_DESCRIPTOR;
194     req.wValue = USB_DT_DEVICE<<8;
195     req.wIndex = 0;
196     req.wLength = 8;
197     return send_default_control(pipe, &req, dinfo);
198 }
199
200 static struct usb_config_descriptor *
201 get_device_config(struct usb_pipe *pipe)
202 {
203     struct usb_config_descriptor cfg;
204
205     struct usb_ctrlrequest req;
206     req.bRequestType = USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_DEVICE;
207     req.bRequest = USB_REQ_GET_DESCRIPTOR;
208     req.wValue = USB_DT_CONFIG<<8;
209     req.wIndex = 0;
210     req.wLength = sizeof(cfg);
211     int ret = send_default_control(pipe, &req, &cfg);
212     if (ret)
213         return NULL;
214
215     void *config = malloc_tmphigh(cfg.wTotalLength);
216     if (!config)
217         return NULL;
218     req.wLength = cfg.wTotalLength;
219     ret = send_default_control(pipe, &req, config);
220     if (ret)
221         return NULL;
222     //hexdump(config, cfg.wTotalLength);
223     return config;
224 }
225
226 static int
227 set_configuration(struct usb_pipe *pipe, u16 val)
228 {
229     struct usb_ctrlrequest req;
230     req.bRequestType = USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE;
231     req.bRequest = USB_REQ_SET_CONFIGURATION;
232     req.wValue = val;
233     req.wIndex = 0;
234     req.wLength = 0;
235     return send_default_control(pipe, &req, NULL);
236 }
237
238
239 /****************************************************************
240  * Initialization and enumeration
241  ****************************************************************/
242
243 // Assign an address to a device in the default state on the given
244 // controller.
245 static struct usb_pipe *
246 usb_set_address(struct usbhub_s *hub, int port, int speed)
247 {
248     ASSERT32FLAT();
249     struct usb_s *cntl = hub->cntl;
250     dprintf(3, "set_address %p\n", cntl);
251     if (cntl->maxaddr >= USB_MAXADDR)
252         return NULL;
253
254     struct usb_pipe *defpipe = cntl->defaultpipe;
255     if (!defpipe) {
256         // Create a pipe for the default address.
257         struct usb_pipe dummy;
258         memset(&dummy, 0, sizeof(dummy));
259         dummy.cntl = cntl;
260         dummy.type = cntl->type;
261         dummy.maxpacket = 8;
262         dummy.path = (u64)-1;
263         cntl->defaultpipe = defpipe = alloc_default_control_pipe(&dummy);
264         if (!defpipe)
265             return NULL;
266     }
267     defpipe->speed = speed;
268     if (hub->pipe) {
269         if (hub->pipe->speed == USB_HIGHSPEED) {
270             defpipe->tt_devaddr = hub->pipe->devaddr;
271             defpipe->tt_port = port;
272         } else {
273             defpipe->tt_devaddr = hub->pipe->tt_devaddr;
274             defpipe->tt_port = hub->pipe->tt_port;
275         }
276     } else {
277         defpipe->tt_devaddr = defpipe->tt_port = 0;
278     }
279
280     msleep(USB_TIME_RSTRCY);
281
282     struct usb_ctrlrequest req;
283     req.bRequestType = USB_DIR_OUT | USB_TYPE_STANDARD | USB_RECIP_DEVICE;
284     req.bRequest = USB_REQ_SET_ADDRESS;
285     req.wValue = cntl->maxaddr + 1;
286     req.wIndex = 0;
287     req.wLength = 0;
288     int ret = send_default_control(defpipe, &req, NULL);
289     if (ret)
290         return NULL;
291
292     msleep(USB_TIME_SETADDR_RECOVERY);
293
294     cntl->maxaddr++;
295     defpipe->devaddr = cntl->maxaddr;
296     struct usb_pipe *pipe = alloc_default_control_pipe(defpipe);
297     defpipe->devaddr = 0;
298     if (hub->pipe)
299         pipe->path = hub->pipe->path;
300     pipe->path = (pipe->path << 8) | port;
301     return pipe;
302 }
303
304 // Called for every found device - see if a driver is available for
305 // this device and do setup if so.
306 static int
307 configure_usb_device(struct usb_pipe *pipe)
308 {
309     ASSERT32FLAT();
310     dprintf(3, "config_usb: %p\n", pipe);
311
312     // Set the max packet size for endpoint 0 of this device.
313     struct usb_device_descriptor dinfo;
314     int ret = get_device_info8(pipe, &dinfo);
315     if (ret)
316         return 0;
317     dprintf(3, "device rev=%04x cls=%02x sub=%02x proto=%02x size=%02x\n"
318             , dinfo.bcdUSB, dinfo.bDeviceClass, dinfo.bDeviceSubClass
319             , dinfo.bDeviceProtocol, dinfo.bMaxPacketSize0);
320     if (dinfo.bMaxPacketSize0 < 8 || dinfo.bMaxPacketSize0 > 64)
321         return 0;
322     pipe->maxpacket = dinfo.bMaxPacketSize0;
323
324     // Get configuration
325     struct usb_config_descriptor *config = get_device_config(pipe);
326     if (!config)
327         return 0;
328
329     // Determine if a driver exists for this device - only look at the
330     // first interface of the first configuration.
331     struct usb_interface_descriptor *iface = (void*)(&config[1]);
332     if (iface->bInterfaceClass != USB_CLASS_HID
333         && iface->bInterfaceClass != USB_CLASS_MASS_STORAGE
334         && iface->bInterfaceClass != USB_CLASS_HUB)
335         // Not a supported device.
336         goto fail;
337
338     // Set the configuration.
339     ret = set_configuration(pipe, config->bConfigurationValue);
340     if (ret)
341         goto fail;
342
343     // Configure driver.
344     int imax = (void*)config + config->wTotalLength - (void*)iface;
345     if (iface->bInterfaceClass == USB_CLASS_HUB)
346         ret = usb_hub_init(pipe);
347     else if (iface->bInterfaceClass == USB_CLASS_MASS_STORAGE)
348         ret = usb_msc_init(pipe, iface, imax);
349     else
350         ret = usb_hid_init(pipe, iface, imax);
351     if (ret)
352         goto fail;
353
354     free(config);
355     return 1;
356 fail:
357     free(config);
358     return 0;
359 }
360
361 static void
362 usb_init_hub_port(void *data)
363 {
364     struct usbhub_s *hub = data;
365     u32 port = hub->port; // XXX - find better way to pass port
366
367     // Detect if device present (and possibly start reset)
368     int ret = hub->op->detect(hub, port);
369     if (ret)
370         // No device present
371         goto done;
372
373     // Reset port and determine device speed
374     mutex_lock(&hub->cntl->resetlock);
375     ret = hub->op->reset(hub, port);
376     if (ret < 0)
377         // Reset failed
378         goto resetfail;
379
380     // Set address of port
381     struct usb_pipe *pipe = usb_set_address(hub, port, ret);
382     if (!pipe) {
383         hub->op->disconnect(hub, port);
384         goto resetfail;
385     }
386     mutex_unlock(&hub->cntl->resetlock);
387
388     // Configure the device
389     int count = configure_usb_device(pipe);
390     free_pipe(pipe);
391     if (!count)
392         hub->op->disconnect(hub, port);
393     hub->devcount += count;
394 done:
395     hub->threads--;
396     return;
397
398 resetfail:
399     mutex_unlock(&hub->cntl->resetlock);
400     goto done;
401 }
402
403 void
404 usb_enumerate(struct usbhub_s *hub)
405 {
406     u32 portcount = hub->portcount;
407     hub->threads = portcount;
408
409     // Launch a thread for every port.
410     int i;
411     for (i=0; i<portcount; i++) {
412         hub->port = i;
413         run_thread(usb_init_hub_port, hub);
414     }
415
416     // Wait for threads to complete.
417     while (hub->threads)
418         yield();
419 }
420
421 void
422 usb_setup(void)
423 {
424     ASSERT32FLAT();
425     if (! CONFIG_USB)
426         return;
427
428     dprintf(3, "init usb\n");
429
430     // Look for USB controllers
431     int count = 0;
432     struct pci_device *ehcipci = PCIDevices;
433     struct pci_device *pci;
434     foreachpci(pci) {
435         if (pci->class != PCI_CLASS_SERIAL_USB)
436             continue;
437
438         if (pci->bdf >= ehcipci->bdf) {
439             // Check to see if this device has an ehci controller
440             int found = 0;
441             ehcipci = pci;
442             for (;;) {
443                 if (pci_classprog(ehcipci) == PCI_CLASS_SERIAL_USB_EHCI) {
444                     // Found an ehci controller.
445                     int ret = ehci_init(ehcipci, count++, pci);
446                     if (ret)
447                         // Error
448                         break;
449                     count += found;
450                     pci = ehcipci;
451                     break;
452                 }
453                 if (ehcipci->class == PCI_CLASS_SERIAL_USB)
454                     found++;
455                 ehcipci = ehcipci->next;
456                 if (!ehcipci || (pci_bdf_to_busdev(ehcipci->bdf)
457                                  != pci_bdf_to_busdev(pci->bdf)))
458                     // No ehci controller found.
459                     break;
460             }
461         }
462
463         if (pci_classprog(pci) == PCI_CLASS_SERIAL_USB_UHCI)
464             uhci_init(pci, count++);
465         else if (pci_classprog(pci) == PCI_CLASS_SERIAL_USB_OHCI)
466             ohci_init(pci, count++);
467     }
468 }