3 * Copyright (C) 2008-2010 coresystems GmbH
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <libpayload-config.h>
35 #define DR_DESC gen_bmRequestType(device_to_host, standard_type, dev_recp)
42 hci_t
*controller
= xzalloc(sizeof(hci_t
));
43 controller
->next
= usb_hcs
;
49 detach_controller(hci_t
*controller
)
51 if (controller
== NULL
)
54 usb_detach_device(controller
, 0); /* tear down root hub tree */
56 if (usb_hcs
== controller
) {
57 usb_hcs
= controller
->next
;
61 if (it
->next
== controller
) {
62 it
->next
= controller
->next
;
71 * Shut down all controllers
76 while (usb_hcs
!= NULL
) {
77 usb_hcs
->shutdown(usb_hcs
);
83 * Polls all hubs on all USB controllers, to find out about device changes
94 hci_t
*controller
= usb_hcs
;
95 while (controller
!= NULL
) {
97 for (i
= 0; i
< 128; i
++) {
98 if (controller
->devices
[i
] != 0) {
99 controller
->devices
[i
]->poll(controller
->devices
[i
]);
102 controller
= controller
->next
;
107 init_device_entry(hci_t
*controller
, int i
)
109 usbdev_t
*dev
= calloc(1, sizeof(usbdev_t
));
111 usb_debug("no memory to allocate device structure\n");
114 if (controller
->devices
[i
] != 0)
115 usb_debug("warning: device %d reassigned?\n", i
);
116 controller
->devices
[i
] = dev
;
117 dev
->controller
= controller
;
121 dev
->init
= usb_nop_init
;
122 dev
->init(controller
->devices
[i
]);
127 set_feature(usbdev_t
*dev
, int endp
, int feature
, int rtype
)
131 dr
.bmRequestType
= rtype
;
132 dr
.data_dir
= host_to_device
;
133 dr
.bRequest
= SET_FEATURE
;
138 return dev
->controller
->control(dev
, OUT
, sizeof(dr
), &dr
, 0, 0);
142 get_status(usbdev_t
*dev
, int intf
, int rtype
, int len
, void *data
)
146 dr
.bmRequestType
= rtype
;
147 dr
.data_dir
= device_to_host
;
148 dr
.bRequest
= GET_STATUS
;
153 return dev
->controller
->control(dev
, IN
, sizeof(dr
), &dr
, len
, data
);
157 * Certain Lexar / Micron USB 2.0 disks will fail the get_descriptor(DT_CFG)
158 * call due to timing issues. Work around this by making extra attempts on
161 #define GET_DESCRIPTOR_TRIES 3
164 get_descriptor(usbdev_t
*dev
, int rtype
, int desc_type
, int desc_idx
,
165 void *data
, size_t len
)
171 while (fail_tries
++ < GET_DESCRIPTOR_TRIES
) {
172 dr
.bmRequestType
= rtype
;
173 dr
.bRequest
= GET_DESCRIPTOR
;
174 dr
.wValue
= desc_type
<< 8 | desc_idx
;
178 ret
= dev
->controller
->control(dev
, IN
,
179 sizeof(dr
), &dr
, len
, data
);
189 set_configuration(usbdev_t
*dev
)
193 dr
.bmRequestType
= 0;
194 dr
.bRequest
= SET_CONFIGURATION
;
195 dr
.wValue
= dev
->configuration
->bConfigurationValue
;
199 return dev
->controller
->control(dev
, OUT
, sizeof(dr
), &dr
, 0, 0);
203 clear_feature(usbdev_t
*dev
, int endp
, int feature
, int rtype
)
207 dr
.bmRequestType
= rtype
;
208 dr
.data_dir
= host_to_device
;
209 dr
.bRequest
= CLEAR_FEATURE
;
214 return dev
->controller
->control(dev
, OUT
, sizeof(dr
), &dr
, 0, 0) < 0;
218 clear_stall(endpoint_t
*ep
)
220 int ret
= clear_feature(ep
->dev
, ep
->endpoint
, ENDPOINT_HALT
,
221 gen_bmRequestType(host_to_device
, standard_type
, endp_recp
));
226 /* returns free address or -1 */
228 get_free_address(hci_t
*controller
)
230 int i
= controller
->latest_address
+ 1;
231 for (; i
!= controller
->latest_address
; i
++) {
232 if (i
>= ARRAY_SIZE(controller
->devices
) || i
< 1) {
233 usb_debug("WARNING: Device addresses for controller %#" PRIxPTR
234 " wrapped around!\n", controller
->reg_base
);
238 if (controller
->devices
[i
] == 0) {
239 controller
->latest_address
= i
;
243 usb_debug("no free address found\n");
244 return -1; // no free address
248 usb_decode_mps0(usb_speed speed
, u8 bMaxPacketSize0
)
252 if (bMaxPacketSize0
!= 8) {
253 usb_debug("Invalid MPS0: 0x%02x\n", bMaxPacketSize0
);
256 return bMaxPacketSize0
;
258 switch (bMaxPacketSize0
) {
259 case 8: case 16: case 32: case 64:
260 return bMaxPacketSize0
;
262 usb_debug("Invalid MPS0: 0x%02x\n", bMaxPacketSize0
);
266 if (bMaxPacketSize0
!= 64) {
267 usb_debug("Invalid MPS0: 0x%02x\n", bMaxPacketSize0
);
268 bMaxPacketSize0
= 64;
270 return bMaxPacketSize0
;
272 /* Intentional fallthrough */
273 case SUPER_SPEED_PLUS
:
274 if (bMaxPacketSize0
!= 9) {
275 usb_debug("Invalid MPS0: 0x%02x\n", bMaxPacketSize0
);
278 return 1 << bMaxPacketSize0
;
279 default: /* GCC is stupid and cannot deal with enums correctly */
284 int speed_to_default_mps(usb_speed speed
)
293 /* Intentional fallthrough */
294 case SUPER_SPEED_PLUS
:
300 /* Normalize bInterval to log2 of microframes */
302 usb_decode_interval(usb_speed speed
, const endpoint_type type
, const unsigned char bInterval
)
304 #define LOG2(a) ((sizeof(unsigned) << 3) - __builtin_clz(a) - 1)
308 case ISOCHRONOUS
: case INTERRUPT
:
309 return LOG2(bInterval
) + 3;
316 return (bInterval
- 1) + 3;
318 return LOG2(bInterval
) + 3;
324 case ISOCHRONOUS
: case INTERRUPT
:
325 return bInterval
- 1;
327 return LOG2(bInterval
);
330 /* Intentional fallthrough */
331 case SUPER_SPEED_PLUS
:
333 case ISOCHRONOUS
: case INTERRUPT
:
334 return bInterval
- 1;
345 generic_set_address(hci_t
*controller
, usb_speed speed
,
346 int hubport
, int hubaddr
)
348 int adr
= get_free_address(controller
); // address to set
353 memset(&dr
, 0, sizeof(dr
));
354 dr
.data_dir
= host_to_device
;
355 dr
.req_type
= standard_type
;
356 dr
.req_recp
= dev_recp
;
357 dr
.bRequest
= SET_ADDRESS
;
362 usbdev_t
*dev
= init_device_entry(controller
, adr
);
366 // dummy values for registering the address
371 dev
->endpoints
[0].dev
= dev
;
372 dev
->endpoints
[0].endpoint
= 0;
373 dev
->endpoints
[0].maxpacketsize
= 8;
374 dev
->endpoints
[0].toggle
= 0;
375 dev
->endpoints
[0].direction
= SETUP
;
376 dev
->endpoints
[0].type
= CONTROL
;
377 if (dev
->controller
->control(dev
, OUT
, sizeof(dr
), &dr
, 0, 0) < 0) {
378 usb_debug("set_address failed\n");
379 usb_detach_device(controller
, adr
);
382 mdelay(SET_ADDRESS_MDELAY
);
386 if (get_descriptor(dev
, DR_DESC
, DT_DEV
, 0, buf
, sizeof(buf
))
388 usb_debug("first get_descriptor(DT_DEV) failed\n");
389 usb_detach_device(controller
, adr
);
392 dev
->endpoints
[0].maxpacketsize
= usb_decode_mps0(speed
, buf
[7]);
398 set_address(hci_t
*controller
, usb_speed speed
, int hubport
, int hubaddr
)
400 usbdev_t
*dev
= controller
->set_address(controller
, speed
,
403 usb_debug("set_address failed\n");
407 dev
->descriptor
= malloc(sizeof(*dev
->descriptor
));
408 if (!dev
->descriptor
|| get_descriptor(dev
, DR_DESC
, DT_DEV
, 0,
409 dev
->descriptor
, sizeof(*dev
->descriptor
))
410 != sizeof(*dev
->descriptor
)) {
411 usb_debug("get_descriptor(DT_DEV) failed\n");
412 usb_detach_device(controller
, dev
->address
);
416 usb_debug("* found device (0x%04x:0x%04x, USB %x.%x, MPS0: %d)\n",
417 dev
->descriptor
->idVendor
, dev
->descriptor
->idProduct
,
418 dev
->descriptor
->bcdUSB
>> 8, dev
->descriptor
->bcdUSB
& 0xff,
419 dev
->endpoints
[0].maxpacketsize
);
420 dev
->quirks
= usb_quirk_check(dev
->descriptor
->idVendor
,
421 dev
->descriptor
->idProduct
);
423 usb_debug("device has %d configurations\n",
424 dev
->descriptor
->bNumConfigurations
);
425 if (dev
->descriptor
->bNumConfigurations
== 0) {
426 /* device isn't usable */
427 usb_debug("... no usable configuration!\n");
428 usb_detach_device(controller
, dev
->address
);
433 if (get_descriptor(dev
, DR_DESC
, DT_CFG
, 0, buf
, sizeof(buf
))
435 usb_debug("first get_descriptor(DT_CFG) failed\n");
436 usb_detach_device(controller
, dev
->address
);
439 /* workaround for some USB devices: wait until they're ready, or
440 * they send a NAK when they're not allowed to do. 1ms is enough */
442 dev
->configuration
= malloc(buf
[1]);
443 if (!dev
->configuration
) {
444 usb_debug("could not allocate %d bytes for DT_CFG\n", buf
[1]);
445 usb_detach_device(controller
, dev
->address
);
448 if (get_descriptor(dev
, DR_DESC
, DT_CFG
, 0, dev
->configuration
,
450 usb_debug("get_descriptor(DT_CFG) failed\n");
451 usb_detach_device(controller
, dev
->address
);
454 configuration_descriptor_t
*cd
= dev
->configuration
;
455 if (cd
->wTotalLength
!= buf
[1]) {
456 usb_debug("configuration descriptor size changed, aborting\n");
457 usb_detach_device(controller
, dev
->address
);
462 * If the device is not well known (ifnum == -1), we use the first
463 * interface we encounter, as there was no need to implement something
464 * else for the time being. If you need it, see the SetInterface and
465 * GetInterface functions in the USB specification and set it yourself.
467 usb_debug("device has %x interfaces\n", cd
->bNumInterfaces
);
468 int ifnum
= usb_interface_check(dev
->descriptor
->idVendor
,
469 dev
->descriptor
->idProduct
);
470 if (cd
->bNumInterfaces
> 1 && ifnum
< 0)
471 usb_debug("NOTICE: Your device has multiple interfaces and\n"
472 "this driver will only use the first one. That may\n"
473 "be the wrong choice and cause the device to not\n"
474 "work correctly. Please report this case\n"
475 "(including the above debugging output) to\n"
476 "coreboot@coreboot.org to have the device added to\n"
477 "the list of well-known quirks.\n");
479 u8
*end
= (void *)dev
->configuration
+ cd
->wTotalLength
;
480 interface_descriptor_t
*intf
;
483 /* Find our interface (or the first good one if we don't know) */
484 for (ptr
= (void *)dev
->configuration
+ sizeof(*cd
); ; ptr
+= ptr
[0]) {
485 if (ptr
+ 2 > end
|| !ptr
[0] || ptr
+ ptr
[0] > end
) {
486 usb_debug("Couldn't find usable DT_INTF\n");
487 usb_detach_device(controller
, dev
->address
);
490 if (ptr
[1] != DT_INTF
)
493 if (intf
->bLength
!= sizeof(*intf
)) {
494 usb_debug("Skipping broken DT_INTF\n");
497 if (ifnum
>= 0 && intf
->bInterfaceNumber
!= ifnum
)
499 usb_debug("Interface %d: class 0x%x, sub 0x%x. proto 0x%x\n",
500 intf
->bInterfaceNumber
, intf
->bInterfaceClass
,
501 intf
->bInterfaceSubClass
, intf
->bInterfaceProtocol
);
502 ptr
+= sizeof(*intf
);
506 /* Gather up all endpoints belonging to this interface */
508 for (; ptr
+ 2 <= end
&& ptr
[0] && ptr
+ ptr
[0] <= end
; ptr
+= ptr
[0]) {
509 if (ptr
[1] == DT_INTF
|| ptr
[1] == DT_CFG
||
510 dev
->num_endp
>= ARRAY_SIZE(dev
->endpoints
))
512 if (ptr
[1] != DT_ENDP
)
515 endpoint_descriptor_t
*desc
= (void *)ptr
;
516 static const char *transfertypes
[4] = {
517 "control", "isochronous", "bulk", "interrupt"
519 usb_debug(" #Endpoint %d (%s), max packet size %x, type %s\n",
520 desc
->bEndpointAddress
& 0x7f,
521 (desc
->bEndpointAddress
& 0x80) ? "in" : "out",
522 desc
->wMaxPacketSize
,
523 transfertypes
[desc
->bmAttributes
& 0x3]);
525 endpoint_t
*ep
= &dev
->endpoints
[dev
->num_endp
++];
527 ep
->endpoint
= desc
->bEndpointAddress
;
529 ep
->maxpacketsize
= desc
->wMaxPacketSize
;
530 ep
->direction
= (desc
->bEndpointAddress
& 0x80) ? IN
: OUT
;
531 ep
->type
= desc
->bmAttributes
& 0x3;
532 ep
->interval
= usb_decode_interval(dev
->speed
, ep
->type
,
536 if ((controller
->finish_device_config
&&
537 controller
->finish_device_config(dev
)) ||
538 set_configuration(dev
) < 0) {
539 usb_debug("Could not finalize device configuration\n");
540 usb_detach_device(controller
, dev
->address
);
544 int class = dev
->descriptor
->bDeviceClass
;
546 class = intf
->bInterfaceClass
;
552 physical_device
= 0x05,
553 imaging_device
= 0x06,
554 printer_device
= 0x07,
559 security_device
= 0x0d,
561 healthcare_device
= 0x0f,
562 diagnostic_device
= 0xdc,
563 wireless_device
= 0xe0,
566 usb_debug("Class: ");
569 usb_debug("audio\n");
572 usb_debug("communication\n");
576 #if CONFIG(LP_USB_HID)
577 dev
->init
= usb_hid_init
;
580 usb_debug("NOTICE: USB HID support not compiled in\n");
583 case physical_device
:
584 usb_debug("physical\n");
587 usb_debug("camera\n");
590 usb_debug("printer\n");
594 #if CONFIG(LP_USB_MSC)
595 dev
->init
= usb_msc_init
;
598 usb_debug("NOTICE: USB MSC support not compiled in\n");
603 #if CONFIG(LP_USB_HUB)
604 dev
->init
= usb_hub_init
;
607 usb_debug("NOTICE: USB hub support not compiled in\n");
614 usb_debug("smartcard / CCID\n");
616 case security_device
:
617 usb_debug("content security\n");
620 usb_debug("video\n");
622 case healthcare_device
:
623 usb_debug("healthcare\n");
625 case diagnostic_device
:
626 usb_debug("diagnostic\n");
628 case wireless_device
:
629 usb_debug("wireless\n");
632 usb_debug("unsupported class %x\n", class);
635 dev
->init
= usb_generic_init
;
640 * Should be called by the hub drivers whenever a physical detach occurs
641 * and can be called by USB class drivers if they are unsatisfied with a
642 * malfunctioning device.
645 usb_detach_device(hci_t
*controller
, int devno
)
647 /* check if device exists, as we may have
648 been called yet by the USB class driver */
649 if (controller
->devices
[devno
]) {
650 controller
->devices
[devno
]->destroy(controller
->devices
[devno
]);
652 if (controller
->destroy_device
)
653 controller
->destroy_device(controller
, devno
);
655 free(controller
->devices
[devno
]->descriptor
);
656 controller
->devices
[devno
]->descriptor
= NULL
;
657 free(controller
->devices
[devno
]->configuration
);
658 controller
->devices
[devno
]->configuration
= NULL
;
660 /* Tear down the device itself *after* destroy_device()
661 * has had a chance to interrogate it. */
662 free(controller
->devices
[devno
]);
663 controller
->devices
[devno
] = NULL
;
668 usb_attach_device(hci_t
*controller
, int hubaddress
, int port
, usb_speed speed
)
670 static const char *speeds
[] = { "full", "low", "high", "super", "ultra" };
671 usb_debug("%sspeed device\n", (speed
< sizeof(speeds
) / sizeof(char*))
672 ? speeds
[speed
] : "invalid value - no");
673 int newdev
= set_address(controller
, speed
, port
, hubaddress
);
676 usbdev_t
*newdev_t
= controller
->devices
[newdev
];
677 // determine responsible driver - current done in set_address
678 newdev_t
->init(newdev_t
);
679 /* init() may have called usb_detach_device() yet, so check */
680 return controller
->devices
[newdev
] ? newdev
: -1;
684 usb_generic_destroy(usbdev_t
*dev
)
686 if (usb_generic_remove
)
687 usb_generic_remove(dev
);
691 usb_generic_init(usbdev_t
*dev
)
694 dev
->destroy
= usb_generic_destroy
;
696 if (usb_generic_create
)
697 usb_generic_create(dev
);
699 if (dev
->data
== NULL
) {
700 usb_debug("Detaching device not used by payload\n");
701 usb_detach_device(dev
->controller
, dev
->address
);
706 * returns the speed is above SUPER_SPEED or not
708 _Bool
is_usb_speed_ss(usb_speed speed
)
710 return (speed
== SUPER_SPEED
|| speed
== SUPER_SPEED_PLUS
);
714 * returns the address of the closest USB2.0 hub, which is responsible for
715 * split transactions, along with the number of the used downstream port
717 int closest_usb2_hub(const usbdev_t
*dev
, int *const addr
, int *const port
)
719 const usbdev_t
*usb1dev
;
723 if ((dev
->hub
>= 0) && (dev
->hub
< 128))
724 dev
= dev
->controller
->devices
[dev
->hub
];
727 } while (dev
&& (dev
->speed
< 2));
730 *addr
= usb1dev
->hub
;
731 *port
= usb1dev
->port
;
735 usb_debug("Couldn't find closest USB2.0 hub.\n");