2 * Driver for USB ported from CoreBoot
4 * Copyright (C) 2014 BALATON Zoltan
6 * This file was part of the libpayload project.
8 * Copyright (C) 2008-2010 coresystems GmbH
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include "drivers/usb.h"
38 #include "libc/byteorder.h"
42 static void usb_nop_init (usbdev_t *dev);
45 usb_nop_destroy (usbdev_t *dev)
47 if (dev->descriptor != 0)
48 free (dev->descriptor);
56 usb_nop_poll (usbdev_t *dev)
62 usb_nop_init (usbdev_t *dev)
65 dev->destroy = usb_nop_destroy;
66 dev->poll = usb_nop_poll;
72 hci_t *controller = malloc (sizeof (hci_t));
76 controller->next = usb_hcs;
85 detach_controller (hci_t *controller)
87 if (controller == NULL)
89 if (usb_hcs == controller) {
90 usb_hcs = controller->next;
94 if (it->next == controller) {
95 it->next = controller->next;
104 * Shut down all controllers
109 while (usb_hcs != NULL) {
110 usb_hcs->shutdown(usb_hcs);
116 * Polls all hubs on all USB controllers, to find out about device changes
123 hci_t *controller = usb_hcs;
124 while (controller != NULL) {
126 for (i = 0; i < 128; i++) {
127 if (controller->devices[i] != 0) {
128 controller->devices[i]->poll (controller->devices[i]);
131 controller = controller->next;
136 init_device_entry (hci_t *controller, int i)
138 if (controller->devices[i] != 0)
139 usb_debug("warning: device %d reassigned?\n", i);
140 controller->devices[i] = malloc(sizeof(usbdev_t));
141 controller->devices[i]->controller = controller;
142 controller->devices[i]->address = -1;
143 controller->devices[i]->hub = -1;
144 controller->devices[i]->port = -1;
145 controller->devices[i]->init = usb_nop_init;
146 controller->devices[i]->init (controller->devices[i]);
150 set_feature (usbdev_t *dev, int endp, int feature, int rtype)
154 dr.bmRequestType = rtype;
155 dr.data_dir = host_to_device;
156 dr.bRequest = SET_FEATURE;
157 dr.wValue = __cpu_to_le16(feature);
158 dr.wIndex = __cpu_to_le16(endp);
160 dev->controller->control (dev, OUT, sizeof (dr), &dr, 0, 0);
164 get_status (usbdev_t *dev, int intf, int rtype, int len, void *data)
168 dr.bmRequestType = rtype;
169 dr.data_dir = device_to_host;
170 dr.bRequest = GET_STATUS;
172 dr.wIndex = __cpu_to_le16(intf);
173 dr.wLength = __cpu_to_le16(len);
174 dev->controller->control (dev, IN, sizeof (dr), &dr, len, data);
178 get_descriptor (usbdev_t *dev, unsigned char bmRequestType, int descType,
179 int descIdx, int langID)
186 dr.bmRequestType = bmRequestType;
187 dr.data_dir = device_to_host; // always like this for descriptors
188 dr.bRequest = GET_DESCRIPTOR;
189 dr.wValue = __cpu_to_le16((descType << 8) | descIdx);
190 dr.wIndex = __cpu_to_le16(langID);
191 dr.wLength = __cpu_to_le16(8);
192 if (dev->controller->control (dev, IN, sizeof (dr), &dr, 8, buf)) {
193 usb_debug ("getting descriptor size (type %x) failed\n",
198 device_descriptor_t *dd = (device_descriptor_t *) buf;
199 usb_debug ("maxPacketSize0: %x\n", dd->bMaxPacketSize0);
200 if (dd->bMaxPacketSize0 != 0)
201 dev->endpoints[0].maxpacketsize = dd->bMaxPacketSize0;
204 /* special case for configuration descriptors: they carry all their
205 subsequent descriptors with them, and keep the entire size at a
206 different location */
209 int realsize = __le16_to_cpu(((unsigned short *) (buf + 2))[0]);
212 result = malloc (size);
213 memset (result, 0, size);
214 dr.wLength = __cpu_to_le16(size);
215 if (dev->controller->
216 control (dev, IN, sizeof (dr), &dr, size, result)) {
217 usb_debug ("getting descriptor (type %x, size %x) failed\n",
225 set_configuration (usbdev_t *dev)
229 dr.bmRequestType = 0;
230 dr.bRequest = SET_CONFIGURATION;
231 dr.wValue = __cpu_to_le16(dev->configuration[5]);
234 dev->controller->control (dev, OUT, sizeof (dr), &dr, 0, 0);
238 clear_feature (usbdev_t *dev, int endp, int feature, int rtype)
242 dr.bmRequestType = rtype;
243 dr.data_dir = host_to_device;
244 dr.bRequest = CLEAR_FEATURE;
245 dr.wValue = __cpu_to_le16(feature);
246 dr.wIndex = __cpu_to_le16(endp);
248 return dev->controller->control (dev, OUT, sizeof (dr), &dr, 0, 0);
252 clear_stall (endpoint_t *ep)
254 usbdev_t *dev = ep->dev;
255 int endp = ep->endpoint;
256 int rtype = gen_bmRequestType (host_to_device, standard_type,
257 endp ? endp_recp : dev_recp);
259 int ret = clear_feature (dev, endp, ENDPOINT_HALT, rtype);
264 /* returns free address or -1 */
266 get_free_address (hci_t *controller)
269 for (i = 1; i < 128; i++) {
270 if (controller->devices[i] == 0)
273 usb_debug ("no free address found\n");
274 return -1; // no free address
278 generic_set_address (hci_t *controller, int speed, int hubport, int hubaddr)
280 int adr = get_free_address (controller); // address to set
283 memset (&dr, 0, sizeof (dr));
284 dr.data_dir = host_to_device;
285 dr.req_type = standard_type;
286 dr.req_recp = dev_recp;
287 dr.bRequest = SET_ADDRESS;
288 dr.wValue = __cpu_to_le16(adr);
292 init_device_entry(controller, adr);
293 usbdev_t *dev = controller->devices[adr];
294 // dummy values for registering the address
299 dev->endpoints[0].dev = dev;
300 dev->endpoints[0].endpoint = 0;
301 dev->endpoints[0].maxpacketsize = 8;
302 dev->endpoints[0].toggle = 0;
303 dev->endpoints[0].direction = SETUP;
305 if (dev->controller->control (dev, OUT, sizeof (dr), &dr, 0, 0)) {
313 /* Normalize bInterval to log2 of microframes */
315 usb_decode_interval(const int speed, const endpoint_type type, const unsigned char bInterval)
317 #define LOG2(a) ((sizeof(unsigned) << 3) - __builtin_clz(a) - 1)
321 case ISOCHRONOUS: case INTERRUPT:
322 return LOG2(bInterval) + 3;
329 return (bInterval - 1) + 3;
331 return LOG2(bInterval) + 3;
337 case ISOCHRONOUS: case INTERRUPT:
338 return bInterval - 1;
340 return LOG2(bInterval);
344 case ISOCHRONOUS: case INTERRUPT:
345 return bInterval - 1;
356 set_address (hci_t *controller, int speed, int hubport, int hubaddr)
358 int adr = controller->set_address(controller, speed, hubport, hubaddr);
359 if (adr < 0 || !controller->devices[adr]) {
360 usb_debug ("set_address failed\n");
363 configuration_descriptor_t *cd;
364 device_descriptor_t *dd;
366 usbdev_t *dev = controller->devices[adr];
371 dev->descriptor = get_descriptor (dev, gen_bmRequestType
372 (device_to_host, standard_type, dev_recp), 1, 0, 0);
373 dd = (device_descriptor_t *) dev->descriptor;
375 usb_debug ("* found device (0x%04x:0x%04x, USB %x.%x)",
376 __le16_to_cpu(dd->idVendor), __le16_to_cpu(dd->idProduct),
377 __le16_to_cpu(dd->bcdUSB) >> 8, __le16_to_cpu(dd->bcdUSB) & 0xff);
378 dev->quirks = USB_QUIRK_NONE;
380 usb_debug ("\ndevice has %x configurations\n", dd->bNumConfigurations);
381 if (dd->bNumConfigurations == 0) {
382 /* device isn't usable */
383 usb_debug ("... no usable configuration!\n");
388 dev->configuration = get_descriptor (dev, gen_bmRequestType
389 (device_to_host, standard_type, dev_recp), 2, 0, 0);
390 cd = (configuration_descriptor_t *) dev->configuration;
391 interface_descriptor_t *interface =
392 (interface_descriptor_t *) (((char *) cd) + cd->bLength);
395 int num = cd->bNumInterfaces;
396 interface_descriptor_t *current = interface;
397 usb_debug ("device has %x interfaces\n", num);
399 usb_debug ("\nNOTICE: This driver defaults to using the first interface.\n"
400 "This might be the wrong choice and lead to limited functionality\n"
402 /* we limit to the first interface, as there was no need to
403 * implement something else for the time being. If you need
404 * it, see the SetInterface and GetInterface functions in
405 * the USB specification, and adapt appropriately.
407 num = (num > 1) ? 1 : num;
409 for (i = 0; i < num; i++) {
411 usb_debug (" #%x has %x endpoints, interface %x:%x, protocol %x\n",
412 current->bInterfaceNumber, current->bNumEndpoints, current->bInterfaceClass, current->bInterfaceSubClass, current->bInterfaceProtocol);
413 endpoint_descriptor_t *endp =
414 (endpoint_descriptor_t *) (((char *) current)
416 /* Skip any non-endpoint descriptor */
417 if (endp->bDescriptorType != 0x05)
418 endp = (endpoint_descriptor_t *)(((char *)endp) + ((char *)endp)[0]);
420 memset (dev->endpoints, 0, sizeof (dev->endpoints));
421 dev->num_endp = 1; // 0 always exists
422 dev->endpoints[0].dev = dev;
423 dev->endpoints[0].maxpacketsize = dd->bMaxPacketSize0;
424 dev->endpoints[0].direction = SETUP;
425 dev->endpoints[0].type = CONTROL;
426 dev->endpoints[0].interval = usb_decode_interval(dev->speed, CONTROL, endp->bInterval);
427 for (j = 1; j <= current->bNumEndpoints; j++) {
428 #ifdef CONFIG_DEBUG_USB
429 static const char *transfertypes[4] = {
430 "control", "isochronous", "bulk", "interrupt"
432 usb_debug (" #%x: Endpoint %x (%s), max packet size %x, type %s\n", j, endp->bEndpointAddress & 0x7f, ((endp->bEndpointAddress & 0x80) != 0) ? "in" : "out", __le16_to_cpu(endp->wMaxPacketSize), transfertypes[endp->bmAttributes]);
435 &dev->endpoints[dev->num_endp++];
437 ep->endpoint = endp->bEndpointAddress;
439 ep->maxpacketsize = __le16_to_cpu(endp->wMaxPacketSize);
441 ((endp->bEndpointAddress & 0x80) ==
443 ep->type = endp->bmAttributes;
444 ep->interval = usb_decode_interval(dev->speed, ep->type, endp->bInterval);
445 endp = (endpoint_descriptor_t
446 *) (((char *) endp) + endp->bLength);
448 current = (interface_descriptor_t *) endp;
452 if (controller->finish_device_config &&
453 controller->finish_device_config(dev))
454 return adr; /* Device isn't configured correctly,
455 only control transfers may work. */
457 set_configuration(dev);
459 int class = dd->bDeviceClass;
461 class = interface->bInterfaceClass;
463 usb_debug(", class: ");
466 usb_debug("audio\n");
469 usb_debug("communication\n");
473 #ifdef CONFIG_USB_HID
474 controller->devices[adr]->init = usb_hid_init;
477 usb_debug ("NOTICE: USB HID support not compiled in\n");
480 case physical_device:
481 usb_debug("physical\n");
484 usb_debug("camera\n");
487 usb_debug("printer\n");
491 #ifdef CONFIG_USB_MSC
492 controller->devices[adr]->init = usb_msc_init;
495 usb_debug ("NOTICE: USB MSC support not compiled in\n");
500 #ifdef CONFIG_USB_HUB
501 controller->devices[adr]->init = usb_hub_init;
504 usb_debug ("NOTICE: USB hub support not compiled in.\n");
511 usb_debug("smartcard / CCID\n");
513 case security_device:
514 usb_debug("content security\n");
517 usb_debug("video\n");
519 case healthcare_device:
520 usb_debug("healthcare\n");
522 case diagnostic_device:
523 usb_debug("diagnostic\n");
525 case wireless_device:
526 usb_debug("wireless\n");
529 usb_debug("unsupported class %x\n", class);
532 controller->devices[adr]->init = usb_generic_init;
537 * Should be called by the hub drivers whenever a physical detach occurs
538 * and can be called by usb class drivers if they are unsatisfied with a
539 * malfunctioning device.
542 usb_detach_device(hci_t *controller, int devno)
544 /* check if device exists, as we may have
545 been called yet by the usb class driver */
546 if (controller->devices[devno]) {
547 controller->devices[devno]->destroy (controller->devices[devno]);
548 free(controller->devices[devno]);
549 controller->devices[devno] = NULL;
550 if (controller->destroy_device)
551 controller->destroy_device(controller, devno);
556 usb_attach_device(hci_t *controller, int hubaddress, int port, int speed)
558 #ifdef CONFIG_USB_DEBUG
559 static const char* speeds[] = { "full", "low", "high" };
560 usb_debug ("%sspeed device\n", (speed <= 2) ? speeds[speed] : "invalid value - no");
562 int newdev = set_address (controller, speed, port, hubaddress);
565 usbdev_t *newdev_t = controller->devices[newdev];
566 // determine responsible driver - current done in set_address
567 newdev_t->init (newdev_t);
568 /* init() may have called usb_detach_device() yet, so check */
569 return controller->devices[newdev] ? newdev : -1;
573 usb_generic_destroy (usbdev_t *dev)
575 if (usb_generic_remove)
576 usb_generic_remove(dev);
580 usb_generic_init (usbdev_t *dev)
583 dev->destroy = usb_generic_destroy;
585 if (usb_generic_create)
586 usb_generic_create(dev);