Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the libpayload project. |
| 3 | * |
Stefan Reinauer | b56f2d0 | 2010-03-25 22:17:36 +0000 | [diff] [blame] | 4 | * Copyright (C) 2008-2010 coresystems GmbH |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions |
| 8 | * are met: |
| 9 | * 1. Redistributions of source code must retain the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer. |
| 11 | * 2. Redistributions in binary form must reproduce the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer in the |
| 13 | * documentation and/or other materials provided with the distribution. |
| 14 | * 3. The name of the author may not be used to endorse or promote products |
| 15 | * derived from this software without specific prior written permission. |
| 16 | * |
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
| 18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 27 | * SUCH DAMAGE. |
| 28 | */ |
| 29 | |
Stefan Reinauer | b56f2d0 | 2010-03-25 22:17:36 +0000 | [diff] [blame] | 30 | //#define USB_DEBUG |
| 31 | |
Jordan Crouse | db8c0ab | 2008-11-24 17:54:46 +0000 | [diff] [blame] | 32 | #include <libpayload-config.h> |
Jordan Crouse | 29061a5 | 2008-09-11 17:29:00 +0000 | [diff] [blame] | 33 | #include <usb/usb.h> |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 34 | |
| 35 | hci_t *usb_hcs = 0; |
| 36 | |
| 37 | hci_t * |
Stefan Reinauer | 5fe6e23 | 2009-07-31 11:39:55 +0000 | [diff] [blame] | 38 | new_controller (void) |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 39 | { |
| 40 | hci_t *controller = malloc (sizeof (hci_t)); |
| 41 | |
Stefan Reinauer | 5fe6e23 | 2009-07-31 11:39:55 +0000 | [diff] [blame] | 42 | if (controller) { |
| 43 | /* atomic */ |
| 44 | controller->next = usb_hcs; |
| 45 | usb_hcs = controller; |
| 46 | /* atomic end */ |
| 47 | } |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 48 | |
| 49 | return controller; |
| 50 | } |
| 51 | |
| 52 | void |
| 53 | detach_controller (hci_t *controller) |
| 54 | { |
Stefan Reinauer | 5fe6e23 | 2009-07-31 11:39:55 +0000 | [diff] [blame] | 55 | if (controller == NULL) |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 56 | return; |
| 57 | if (usb_hcs == controller) { |
| 58 | usb_hcs = controller->next; |
| 59 | } else { |
| 60 | hci_t *it = usb_hcs; |
Stefan Reinauer | 5fe6e23 | 2009-07-31 11:39:55 +0000 | [diff] [blame] | 61 | while (it != NULL) { |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 62 | if (it->next == controller) { |
| 63 | it->next = controller->next; |
| 64 | return; |
| 65 | } |
Anton Kochkov | 421303a | 2012-06-20 03:57:18 +0400 | [diff] [blame] | 66 | it = it->next; |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 67 | } |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | /** |
Patrick Georgi | bbc5231 | 2011-11-04 12:06:06 +0100 | [diff] [blame] | 72 | * Shut down all controllers |
| 73 | */ |
| 74 | int |
| 75 | usb_exit (void) |
| 76 | { |
Mathias Krause | 59c020a | 2013-03-24 19:40:02 +0100 | [diff] [blame] | 77 | while (usb_hcs != NULL) { |
| 78 | usb_hcs->shutdown(usb_hcs); |
Patrick Georgi | bbc5231 | 2011-11-04 12:06:06 +0100 | [diff] [blame] | 79 | } |
| 80 | return 0; |
| 81 | } |
| 82 | |
| 83 | /** |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 84 | * Polls all hubs on all USB controllers, to find out about device changes |
| 85 | */ |
| 86 | void |
Stefan Reinauer | b56f2d0 | 2010-03-25 22:17:36 +0000 | [diff] [blame] | 87 | usb_poll (void) |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 88 | { |
| 89 | if (usb_hcs == 0) |
| 90 | return; |
| 91 | hci_t *controller = usb_hcs; |
Patrick Georgi | db89ec9 | 2011-11-18 11:56:38 +0100 | [diff] [blame] | 92 | while (controller != NULL) { |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 93 | int i; |
| 94 | for (i = 0; i < 128; i++) { |
Patrick Georgi | 4727c07 | 2008-10-16 19:20:51 +0000 | [diff] [blame] | 95 | if (controller->devices[i] != 0) { |
Stefan Reinauer | b56f2d0 | 2010-03-25 22:17:36 +0000 | [diff] [blame] | 96 | controller->devices[i]->poll (controller->devices[i]); |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 97 | } |
| 98 | } |
| 99 | controller = controller->next; |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | void |
| 104 | init_device_entry (hci_t *controller, int i) |
| 105 | { |
Patrick Georgi | 4727c07 | 2008-10-16 19:20:51 +0000 | [diff] [blame] | 106 | if (controller->devices[i] != 0) |
Gabe Black | 93ded59 | 2012-11-01 15:44:10 -0700 | [diff] [blame] | 107 | usb_debug("warning: device %d reassigned?\n", i); |
Patrick Georgi | 4727c07 | 2008-10-16 19:20:51 +0000 | [diff] [blame] | 108 | controller->devices[i] = malloc(sizeof(usbdev_t)); |
| 109 | controller->devices[i]->controller = controller; |
| 110 | controller->devices[i]->address = -1; |
| 111 | controller->devices[i]->hub = -1; |
| 112 | controller->devices[i]->port = -1; |
| 113 | controller->devices[i]->init = usb_nop_init; |
| 114 | controller->devices[i]->init (controller->devices[i]); |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | void |
| 118 | set_feature (usbdev_t *dev, int endp, int feature, int rtype) |
| 119 | { |
| 120 | dev_req_t dr; |
| 121 | |
| 122 | dr.bmRequestType = rtype; |
| 123 | dr.data_dir = host_to_device; |
| 124 | dr.bRequest = SET_FEATURE; |
| 125 | dr.wValue = feature; |
| 126 | dr.wIndex = endp; |
| 127 | dr.wLength = 0; |
| 128 | dev->controller->control (dev, OUT, sizeof (dr), &dr, 0, 0); |
| 129 | } |
| 130 | |
| 131 | void |
| 132 | get_status (usbdev_t *dev, int intf, int rtype, int len, void *data) |
| 133 | { |
| 134 | dev_req_t dr; |
| 135 | |
| 136 | dr.bmRequestType = rtype; |
| 137 | dr.data_dir = device_to_host; |
| 138 | dr.bRequest = GET_STATUS; |
| 139 | dr.wValue = 0; |
| 140 | dr.wIndex = intf; |
| 141 | dr.wLength = len; |
| 142 | dev->controller->control (dev, IN, sizeof (dr), &dr, len, data); |
| 143 | } |
| 144 | |
| 145 | u8 * |
| 146 | get_descriptor (usbdev_t *dev, unsigned char bmRequestType, int descType, |
| 147 | int descIdx, int langID) |
| 148 | { |
| 149 | u8 buf[8]; |
| 150 | u8 *result; |
| 151 | dev_req_t dr; |
| 152 | int size; |
| 153 | |
| 154 | dr.bmRequestType = bmRequestType; |
| 155 | dr.data_dir = device_to_host; // always like this for descriptors |
| 156 | dr.bRequest = GET_DESCRIPTOR; |
| 157 | dr.wValue = (descType << 8) | descIdx; |
| 158 | dr.wIndex = langID; |
| 159 | dr.wLength = 8; |
| 160 | if (dev->controller->control (dev, IN, sizeof (dr), &dr, 8, buf)) { |
Gabe Black | 93ded59 | 2012-11-01 15:44:10 -0700 | [diff] [blame] | 161 | usb_debug ("getting descriptor size (type %x) failed\n", |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 162 | descType); |
| 163 | } |
| 164 | |
| 165 | if (descType == 1) { |
| 166 | device_descriptor_t *dd = (device_descriptor_t *) buf; |
Gabe Black | 93ded59 | 2012-11-01 15:44:10 -0700 | [diff] [blame] | 167 | usb_debug ("maxPacketSize0: %x\n", dd->bMaxPacketSize0); |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 168 | if (dd->bMaxPacketSize0 != 0) |
| 169 | dev->endpoints[0].maxpacketsize = dd->bMaxPacketSize0; |
| 170 | } |
| 171 | |
| 172 | /* special case for configuration descriptors: they carry all their |
| 173 | subsequent descriptors with them, and keep the entire size at a |
| 174 | different location */ |
| 175 | size = buf[0]; |
| 176 | if (buf[1] == 2) { |
| 177 | int realsize = ((unsigned short *) (buf + 2))[0]; |
| 178 | size = realsize; |
| 179 | } |
| 180 | result = malloc (size); |
| 181 | memset (result, 0, size); |
| 182 | dr.wLength = size; |
| 183 | if (dev->controller-> |
| 184 | control (dev, IN, sizeof (dr), &dr, size, result)) { |
Gabe Black | 93ded59 | 2012-11-01 15:44:10 -0700 | [diff] [blame] | 185 | usb_debug ("getting descriptor (type %x, size %x) failed\n", |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 186 | descType, size); |
| 187 | } |
| 188 | |
| 189 | return result; |
| 190 | } |
| 191 | |
| 192 | void |
| 193 | set_configuration (usbdev_t *dev) |
| 194 | { |
| 195 | dev_req_t dr; |
| 196 | |
| 197 | dr.bmRequestType = 0; |
| 198 | dr.bRequest = SET_CONFIGURATION; |
| 199 | dr.wValue = dev->configuration[5]; |
| 200 | dr.wIndex = 0; |
| 201 | dr.wLength = 0; |
| 202 | dev->controller->control (dev, OUT, sizeof (dr), &dr, 0, 0); |
| 203 | } |
| 204 | |
Nico Huber | 79e1f2f | 2012-06-01 09:50:11 +0200 | [diff] [blame] | 205 | int |
Nico Huber | 5f595cb | 2012-05-21 16:19:05 +0200 | [diff] [blame] | 206 | clear_feature (usbdev_t *dev, int endp, int feature, int rtype) |
| 207 | { |
| 208 | dev_req_t dr; |
| 209 | |
| 210 | dr.bmRequestType = rtype; |
| 211 | dr.data_dir = host_to_device; |
| 212 | dr.bRequest = CLEAR_FEATURE; |
| 213 | dr.wValue = feature; |
| 214 | dr.wIndex = endp; |
| 215 | dr.wLength = 0; |
Nico Huber | 79e1f2f | 2012-06-01 09:50:11 +0200 | [diff] [blame] | 216 | return dev->controller->control (dev, OUT, sizeof (dr), &dr, 0, 0); |
Nico Huber | 5f595cb | 2012-05-21 16:19:05 +0200 | [diff] [blame] | 217 | } |
| 218 | |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 219 | int |
| 220 | clear_stall (endpoint_t *ep) |
| 221 | { |
| 222 | usbdev_t *dev = ep->dev; |
| 223 | int endp = ep->endpoint; |
Nico Huber | 5f595cb | 2012-05-21 16:19:05 +0200 | [diff] [blame] | 224 | int rtype = gen_bmRequestType (host_to_device, standard_type, |
| 225 | endp ? endp_recp : dev_recp); |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 226 | |
Nico Huber | 79e1f2f | 2012-06-01 09:50:11 +0200 | [diff] [blame] | 227 | int ret = clear_feature (dev, endp, ENDPOINT_HALT, rtype); |
Stefan Reinauer | b56f2d0 | 2010-03-25 22:17:36 +0000 | [diff] [blame] | 228 | ep->toggle = 0; |
Nico Huber | 79e1f2f | 2012-06-01 09:50:11 +0200 | [diff] [blame] | 229 | return ret; |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | /* returns free address or -1 */ |
| 233 | static int |
| 234 | get_free_address (hci_t *controller) |
| 235 | { |
| 236 | int i; |
| 237 | for (i = 1; i < 128; i++) { |
Patrick Georgi | 4727c07 | 2008-10-16 19:20:51 +0000 | [diff] [blame] | 238 | if (controller->devices[i] == 0) |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 239 | return i; |
| 240 | } |
Gabe Black | 93ded59 | 2012-11-01 15:44:10 -0700 | [diff] [blame] | 241 | usb_debug ("no free address found\n"); |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 242 | return -1; // no free address |
| 243 | } |
| 244 | |
Patrick Georgi | 482af6d | 2013-05-24 15:48:56 +0200 | [diff] [blame^] | 245 | int |
| 246 | generic_set_address (hci_t *controller, int speed, int hubport, int hubaddr) |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 247 | { |
| 248 | int adr = get_free_address (controller); // address to set |
| 249 | dev_req_t dr; |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 250 | |
| 251 | memset (&dr, 0, sizeof (dr)); |
| 252 | dr.data_dir = host_to_device; |
| 253 | dr.req_type = standard_type; |
| 254 | dr.req_recp = dev_recp; |
| 255 | dr.bRequest = SET_ADDRESS; |
| 256 | dr.wValue = adr; |
| 257 | dr.wIndex = 0; |
| 258 | dr.wLength = 0; |
| 259 | |
Patrick Georgi | 4727c07 | 2008-10-16 19:20:51 +0000 | [diff] [blame] | 260 | init_device_entry(controller, adr); |
| 261 | usbdev_t *dev = controller->devices[adr]; |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 262 | // dummy values for registering the address |
| 263 | dev->address = 0; |
Nico Huber | 1ab6075 | 2012-05-23 09:21:54 +0200 | [diff] [blame] | 264 | dev->hub = hubaddr; |
| 265 | dev->port = hubport; |
Stefan Reinauer | b56f2d0 | 2010-03-25 22:17:36 +0000 | [diff] [blame] | 266 | dev->speed = speed; |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 267 | dev->endpoints[0].dev = dev; |
| 268 | dev->endpoints[0].endpoint = 0; |
| 269 | dev->endpoints[0].maxpacketsize = 8; |
| 270 | dev->endpoints[0].toggle = 0; |
| 271 | dev->endpoints[0].direction = SETUP; |
| 272 | mdelay (50); |
| 273 | if (dev->controller->control (dev, OUT, sizeof (dr), &dr, 0, 0)) { |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 274 | return -1; |
| 275 | } |
| 276 | mdelay (50); |
Patrick Georgi | 482af6d | 2013-05-24 15:48:56 +0200 | [diff] [blame^] | 277 | |
| 278 | return adr; |
| 279 | } |
| 280 | |
| 281 | static int |
| 282 | set_address (hci_t *controller, int speed, int hubport, int hubaddr) |
| 283 | { |
| 284 | int adr = controller->set_address(controller, speed, hubport, hubaddr); |
| 285 | if (adr < 0 || !controller->devices[adr]) { |
| 286 | usb_debug ("set_address failed\n"); |
| 287 | return -1; |
| 288 | } |
| 289 | configuration_descriptor_t *cd; |
| 290 | device_descriptor_t *dd; |
| 291 | |
| 292 | usbdev_t *dev = controller->devices[adr]; |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 293 | dev->address = adr; |
Patrick Georgi | 482af6d | 2013-05-24 15:48:56 +0200 | [diff] [blame^] | 294 | dev->hub = hubaddr; |
| 295 | dev->port = hubport; |
| 296 | dev->speed = speed; |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 297 | dev->descriptor = get_descriptor (dev, gen_bmRequestType |
Stefan Reinauer | b56f2d0 | 2010-03-25 22:17:36 +0000 | [diff] [blame] | 298 | (device_to_host, standard_type, dev_recp), 1, 0, 0); |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 299 | dd = (device_descriptor_t *) dev->descriptor; |
Stefan Reinauer | b56f2d0 | 2010-03-25 22:17:36 +0000 | [diff] [blame] | 300 | |
Dave Frodin | 6bf11cf | 2012-12-11 13:08:07 -0700 | [diff] [blame] | 301 | usb_debug ("* found device (0x%04x:0x%04x, USB %x.%x)", |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 302 | dd->idVendor, dd->idProduct, |
Stefan Reinauer | b56f2d0 | 2010-03-25 22:17:36 +0000 | [diff] [blame] | 303 | dd->bcdUSB >> 8, dd->bcdUSB & 0xff); |
| 304 | dev->quirks = usb_quirk_check(dd->idVendor, dd->idProduct); |
| 305 | |
Gabe Black | 93ded59 | 2012-11-01 15:44:10 -0700 | [diff] [blame] | 306 | usb_debug ("\ndevice has %x configurations\n", dd->bNumConfigurations); |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 307 | if (dd->bNumConfigurations == 0) { |
| 308 | /* device isn't usable */ |
Dave Frodin | 6bf11cf | 2012-12-11 13:08:07 -0700 | [diff] [blame] | 309 | usb_debug ("... no usable configuration!\n"); |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 310 | dev->address = 0; |
| 311 | return -1; |
| 312 | } |
Stefan Reinauer | b56f2d0 | 2010-03-25 22:17:36 +0000 | [diff] [blame] | 313 | |
| 314 | dev->configuration = get_descriptor (dev, gen_bmRequestType |
| 315 | (device_to_host, standard_type, dev_recp), 2, 0, 0); |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 316 | cd = (configuration_descriptor_t *) dev->configuration; |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 317 | interface_descriptor_t *interface = |
| 318 | (interface_descriptor_t *) (((char *) cd) + cd->bLength); |
| 319 | { |
| 320 | int i; |
| 321 | int num = cd->bNumInterfaces; |
| 322 | interface_descriptor_t *current = interface; |
Gabe Black | 93ded59 | 2012-11-01 15:44:10 -0700 | [diff] [blame] | 323 | usb_debug ("device has %x interfaces\n", num); |
Stefan Reinauer | b56f2d0 | 2010-03-25 22:17:36 +0000 | [diff] [blame] | 324 | if (num > 1) { |
| 325 | int interfaces = usb_interface_check(dd->idVendor, dd->idProduct); |
| 326 | if (interfaces) { |
| 327 | /* Well known device, don't warn */ |
| 328 | num = interfaces; |
| 329 | } else { |
| 330 | |
Dave Frodin | 6bf11cf | 2012-12-11 13:08:07 -0700 | [diff] [blame] | 331 | usb_debug ("\nNOTICE: This driver defaults to using the first interface.\n" |
Stefan Reinauer | b56f2d0 | 2010-03-25 22:17:36 +0000 | [diff] [blame] | 332 | "This might be the wrong choice and lead to limited functionality\n" |
| 333 | "of the device. Please report such a case to coreboot@coreboot.org\n" |
| 334 | "as you might be the first.\n"); |
| 335 | /* we limit to the first interface, as there was no need to |
| 336 | * implement something else for the time being. If you need |
| 337 | * it, see the SetInterface and GetInterface functions in |
| 338 | * the USB specification, and adapt appropriately. |
| 339 | */ |
| 340 | num = (num > 1) ? 1 : num; |
| 341 | } |
| 342 | } |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 343 | for (i = 0; i < num; i++) { |
| 344 | int j; |
Gabe Black | 93ded59 | 2012-11-01 15:44:10 -0700 | [diff] [blame] | 345 | usb_debug (" #%x has %x endpoints, interface %x:%x, protocol %x\n", |
Stefan Reinauer | b56f2d0 | 2010-03-25 22:17:36 +0000 | [diff] [blame] | 346 | current->bInterfaceNumber, current->bNumEndpoints, current->bInterfaceClass, current->bInterfaceSubClass, current->bInterfaceProtocol); |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 347 | endpoint_descriptor_t *endp = |
| 348 | (endpoint_descriptor_t *) (((char *) current) |
Stefan Reinauer | b56f2d0 | 2010-03-25 22:17:36 +0000 | [diff] [blame] | 349 | + current->bLength); |
Nico Huber | 735f55c | 2013-06-10 13:43:29 +0200 | [diff] [blame] | 350 | /* Skip any non-endpoint descriptor */ |
| 351 | if (endp->bDescriptorType != 0x05) |
| 352 | endp = (endpoint_descriptor_t *)(((char *)endp) + ((char *)endp)[0]); |
| 353 | |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 354 | memset (dev->endpoints, 0, sizeof (dev->endpoints)); |
| 355 | dev->num_endp = 1; // 0 always exists |
| 356 | dev->endpoints[0].dev = dev; |
| 357 | dev->endpoints[0].maxpacketsize = dd->bMaxPacketSize0; |
| 358 | dev->endpoints[0].direction = SETUP; |
| 359 | dev->endpoints[0].type = CONTROL; |
| 360 | for (j = 1; j <= current->bNumEndpoints; j++) { |
Stefan Reinauer | b56f2d0 | 2010-03-25 22:17:36 +0000 | [diff] [blame] | 361 | #ifdef USB_DEBUG |
| 362 | static const char *transfertypes[4] = { |
| 363 | "control", "isochronous", "bulk", "interrupt" |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 364 | }; |
Gabe Black | 93ded59 | 2012-11-01 15:44:10 -0700 | [diff] [blame] | 365 | usb_debug (" #%x: Endpoint %x (%s), max packet size %x, type %s\n", j, endp->bEndpointAddress & 0x7f, ((endp->bEndpointAddress & 0x80) != 0) ? "in" : "out", endp->wMaxPacketSize, transfertypes[endp->bmAttributes]); |
Stefan Reinauer | b56f2d0 | 2010-03-25 22:17:36 +0000 | [diff] [blame] | 366 | #endif |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 367 | endpoint_t *ep = |
| 368 | &dev->endpoints[dev->num_endp++]; |
| 369 | ep->dev = dev; |
| 370 | ep->endpoint = endp->bEndpointAddress; |
| 371 | ep->toggle = 0; |
| 372 | ep->maxpacketsize = endp->wMaxPacketSize; |
| 373 | ep->direction = |
| 374 | ((endp->bEndpointAddress & 0x80) == |
| 375 | 0) ? OUT : IN; |
| 376 | ep->type = endp->bmAttributes; |
| 377 | endp = (endpoint_descriptor_t |
| 378 | *) (((char *) endp) + endp->bLength); |
| 379 | } |
| 380 | current = (interface_descriptor_t *) endp; |
| 381 | } |
| 382 | } |
Stefan Reinauer | b56f2d0 | 2010-03-25 22:17:36 +0000 | [diff] [blame] | 383 | |
Patrick Georgi | 482af6d | 2013-05-24 15:48:56 +0200 | [diff] [blame^] | 384 | if (controller->finish_device_config && |
| 385 | controller->finish_device_config(dev)) |
| 386 | return adr; /* Device isn't configured correctly, |
| 387 | only control transfers may work. */ |
| 388 | |
| 389 | set_configuration(dev); |
| 390 | |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 391 | int class = dd->bDeviceClass; |
| 392 | if (class == 0) |
| 393 | class = interface->bInterfaceClass; |
| 394 | |
Stefan Reinauer | b56f2d0 | 2010-03-25 22:17:36 +0000 | [diff] [blame] | 395 | enum { |
| 396 | audio_device = 0x01, |
| 397 | comm_device = 0x02, |
| 398 | hid_device = 0x03, |
| 399 | physical_device = 0x05, |
| 400 | imaging_device = 0x06, |
| 401 | printer_device = 0x07, |
| 402 | msc_device = 0x08, |
| 403 | hub_device = 0x09, |
| 404 | cdc_device = 0x0a, |
| 405 | ccid_device = 0x0b, |
| 406 | security_device = 0x0d, |
| 407 | video_device = 0x0e, |
| 408 | healthcare_device = 0x0f, |
| 409 | diagnostic_device = 0xdc, |
| 410 | wireless_device = 0xe0, |
| 411 | misc_device = 0xef, |
| 412 | }; |
Dave Frodin | 6bf11cf | 2012-12-11 13:08:07 -0700 | [diff] [blame] | 413 | usb_debug(", class: "); |
Stefan Reinauer | b56f2d0 | 2010-03-25 22:17:36 +0000 | [diff] [blame] | 414 | switch (class) { |
| 415 | case audio_device: |
Dave Frodin | 6bf11cf | 2012-12-11 13:08:07 -0700 | [diff] [blame] | 416 | usb_debug("audio\n"); |
Stefan Reinauer | b56f2d0 | 2010-03-25 22:17:36 +0000 | [diff] [blame] | 417 | break; |
| 418 | case comm_device: |
Dave Frodin | 6bf11cf | 2012-12-11 13:08:07 -0700 | [diff] [blame] | 419 | usb_debug("communication\n"); |
Stefan Reinauer | b56f2d0 | 2010-03-25 22:17:36 +0000 | [diff] [blame] | 420 | break; |
| 421 | case hid_device: |
Dave Frodin | 6bf11cf | 2012-12-11 13:08:07 -0700 | [diff] [blame] | 422 | usb_debug ("HID\n"); |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 423 | #ifdef CONFIG_USB_HID |
Patrick Georgi | 4727c07 | 2008-10-16 19:20:51 +0000 | [diff] [blame] | 424 | controller->devices[adr]->init = usb_hid_init; |
Gabe Black | dc9e77f | 2013-02-01 20:19:27 -0800 | [diff] [blame] | 425 | return adr; |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 426 | #else |
Gabe Black | 93ded59 | 2012-11-01 15:44:10 -0700 | [diff] [blame] | 427 | usb_debug ("NOTICE: USB HID support not compiled in\n"); |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 428 | #endif |
Stefan Reinauer | b56f2d0 | 2010-03-25 22:17:36 +0000 | [diff] [blame] | 429 | break; |
| 430 | case physical_device: |
Dave Frodin | 6bf11cf | 2012-12-11 13:08:07 -0700 | [diff] [blame] | 431 | usb_debug("physical\n"); |
Stefan Reinauer | b56f2d0 | 2010-03-25 22:17:36 +0000 | [diff] [blame] | 432 | break; |
| 433 | case imaging_device: |
Dave Frodin | 6bf11cf | 2012-12-11 13:08:07 -0700 | [diff] [blame] | 434 | usb_debug("camera\n"); |
Stefan Reinauer | b56f2d0 | 2010-03-25 22:17:36 +0000 | [diff] [blame] | 435 | break; |
| 436 | case printer_device: |
Dave Frodin | 6bf11cf | 2012-12-11 13:08:07 -0700 | [diff] [blame] | 437 | usb_debug("printer\n"); |
Stefan Reinauer | b56f2d0 | 2010-03-25 22:17:36 +0000 | [diff] [blame] | 438 | break; |
| 439 | case msc_device: |
Dave Frodin | 6bf11cf | 2012-12-11 13:08:07 -0700 | [diff] [blame] | 440 | usb_debug ("MSC\n"); |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 441 | #ifdef CONFIG_USB_MSC |
Patrick Georgi | 4727c07 | 2008-10-16 19:20:51 +0000 | [diff] [blame] | 442 | controller->devices[adr]->init = usb_msc_init; |
Gabe Black | dc9e77f | 2013-02-01 20:19:27 -0800 | [diff] [blame] | 443 | return adr; |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 444 | #else |
Gabe Black | 93ded59 | 2012-11-01 15:44:10 -0700 | [diff] [blame] | 445 | usb_debug ("NOTICE: USB MSC support not compiled in\n"); |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 446 | #endif |
Stefan Reinauer | b56f2d0 | 2010-03-25 22:17:36 +0000 | [diff] [blame] | 447 | break; |
| 448 | case hub_device: |
Dave Frodin | 6bf11cf | 2012-12-11 13:08:07 -0700 | [diff] [blame] | 449 | usb_debug ("hub\n"); |
Stefan Reinauer | b56f2d0 | 2010-03-25 22:17:36 +0000 | [diff] [blame] | 450 | #ifdef CONFIG_USB_HUB |
| 451 | controller->devices[adr]->init = usb_hub_init; |
Gabe Black | dc9e77f | 2013-02-01 20:19:27 -0800 | [diff] [blame] | 452 | return adr; |
Stefan Reinauer | b56f2d0 | 2010-03-25 22:17:36 +0000 | [diff] [blame] | 453 | #else |
Gabe Black | 93ded59 | 2012-11-01 15:44:10 -0700 | [diff] [blame] | 454 | usb_debug ("NOTICE: USB hub support not compiled in.\n"); |
Stefan Reinauer | b56f2d0 | 2010-03-25 22:17:36 +0000 | [diff] [blame] | 455 | #endif |
| 456 | break; |
| 457 | case cdc_device: |
Dave Frodin | 6bf11cf | 2012-12-11 13:08:07 -0700 | [diff] [blame] | 458 | usb_debug("CDC\n"); |
Stefan Reinauer | b56f2d0 | 2010-03-25 22:17:36 +0000 | [diff] [blame] | 459 | break; |
| 460 | case ccid_device: |
Dave Frodin | 6bf11cf | 2012-12-11 13:08:07 -0700 | [diff] [blame] | 461 | usb_debug("smartcard / CCID\n"); |
Stefan Reinauer | b56f2d0 | 2010-03-25 22:17:36 +0000 | [diff] [blame] | 462 | break; |
| 463 | case security_device: |
Dave Frodin | 6bf11cf | 2012-12-11 13:08:07 -0700 | [diff] [blame] | 464 | usb_debug("content security\n"); |
Stefan Reinauer | b56f2d0 | 2010-03-25 22:17:36 +0000 | [diff] [blame] | 465 | break; |
| 466 | case video_device: |
Dave Frodin | 6bf11cf | 2012-12-11 13:08:07 -0700 | [diff] [blame] | 467 | usb_debug("video\n"); |
Stefan Reinauer | b56f2d0 | 2010-03-25 22:17:36 +0000 | [diff] [blame] | 468 | break; |
| 469 | case healthcare_device: |
Dave Frodin | 6bf11cf | 2012-12-11 13:08:07 -0700 | [diff] [blame] | 470 | usb_debug("healthcare\n"); |
Stefan Reinauer | b56f2d0 | 2010-03-25 22:17:36 +0000 | [diff] [blame] | 471 | break; |
| 472 | case diagnostic_device: |
Dave Frodin | 6bf11cf | 2012-12-11 13:08:07 -0700 | [diff] [blame] | 473 | usb_debug("diagnostic\n"); |
Stefan Reinauer | b56f2d0 | 2010-03-25 22:17:36 +0000 | [diff] [blame] | 474 | break; |
| 475 | case wireless_device: |
Dave Frodin | 6bf11cf | 2012-12-11 13:08:07 -0700 | [diff] [blame] | 476 | usb_debug("wireless\n"); |
Stefan Reinauer | b56f2d0 | 2010-03-25 22:17:36 +0000 | [diff] [blame] | 477 | break; |
| 478 | default: |
Dave Frodin | 6bf11cf | 2012-12-11 13:08:07 -0700 | [diff] [blame] | 479 | usb_debug("unsupported class %x\n", class); |
Stefan Reinauer | b56f2d0 | 2010-03-25 22:17:36 +0000 | [diff] [blame] | 480 | break; |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 481 | } |
Gabe Black | dc9e77f | 2013-02-01 20:19:27 -0800 | [diff] [blame] | 482 | controller->devices[adr]->init = usb_generic_init; |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 483 | return adr; |
| 484 | } |
Patrick Georgi | 4727c07 | 2008-10-16 19:20:51 +0000 | [diff] [blame] | 485 | |
Nico Huber | 79e1f2f | 2012-06-01 09:50:11 +0200 | [diff] [blame] | 486 | /* |
| 487 | * Should be called by the hub drivers whenever a physical detach occurs |
| 488 | * and can be called by usb class drivers if they are unsatisfied with a |
| 489 | * malfunctioning device. |
| 490 | */ |
Patrick Georgi | 4727c07 | 2008-10-16 19:20:51 +0000 | [diff] [blame] | 491 | void |
| 492 | usb_detach_device(hci_t *controller, int devno) |
| 493 | { |
Nico Huber | 79e1f2f | 2012-06-01 09:50:11 +0200 | [diff] [blame] | 494 | /* check if device exists, as we may have |
| 495 | been called yet by the usb class driver */ |
| 496 | if (controller->devices[devno]) { |
| 497 | controller->devices[devno]->destroy (controller->devices[devno]); |
| 498 | free(controller->devices[devno]); |
| 499 | controller->devices[devno] = NULL; |
Patrick Georgi | 482af6d | 2013-05-24 15:48:56 +0200 | [diff] [blame^] | 500 | if (controller->destroy_device) |
| 501 | controller->destroy_device(controller, devno); |
Nico Huber | 79e1f2f | 2012-06-01 09:50:11 +0200 | [diff] [blame] | 502 | } |
Patrick Georgi | 4727c07 | 2008-10-16 19:20:51 +0000 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | int |
Stefan Reinauer | b56f2d0 | 2010-03-25 22:17:36 +0000 | [diff] [blame] | 506 | usb_attach_device(hci_t *controller, int hubaddress, int port, int speed) |
Patrick Georgi | 4727c07 | 2008-10-16 19:20:51 +0000 | [diff] [blame] | 507 | { |
Stefan Reinauer | b56f2d0 | 2010-03-25 22:17:36 +0000 | [diff] [blame] | 508 | static const char* speeds[] = { "full", "low", "high" }; |
Gabe Black | 93ded59 | 2012-11-01 15:44:10 -0700 | [diff] [blame] | 509 | usb_debug ("%sspeed device\n", (speed <= 2) ? speeds[speed] : "invalid value - no"); |
Nico Huber | 1ab6075 | 2012-05-23 09:21:54 +0200 | [diff] [blame] | 510 | int newdev = set_address (controller, speed, port, hubaddress); |
Patrick Georgi | 4727c07 | 2008-10-16 19:20:51 +0000 | [diff] [blame] | 511 | if (newdev == -1) |
| 512 | return -1; |
| 513 | usbdev_t *newdev_t = controller->devices[newdev]; |
Patrick Georgi | 4727c07 | 2008-10-16 19:20:51 +0000 | [diff] [blame] | 514 | // determine responsible driver - current done in set_address |
| 515 | newdev_t->init (newdev_t); |
Nico Huber | d8a6680 | 2012-06-21 11:21:23 +0200 | [diff] [blame] | 516 | /* init() may have called usb_detach_device() yet, so check */ |
| 517 | return controller->devices[newdev] ? newdev : -1; |
Patrick Georgi | 4727c07 | 2008-10-16 19:20:51 +0000 | [diff] [blame] | 518 | } |
Stefan Reinauer | 5fe6e23 | 2009-07-31 11:39:55 +0000 | [diff] [blame] | 519 | |
Gabe Black | dc9e77f | 2013-02-01 20:19:27 -0800 | [diff] [blame] | 520 | static void |
| 521 | usb_generic_destroy (usbdev_t *dev) |
| 522 | { |
| 523 | if (usb_generic_remove) |
| 524 | usb_generic_remove(dev); |
| 525 | } |
| 526 | |
| 527 | void |
| 528 | usb_generic_init (usbdev_t *dev) |
| 529 | { |
| 530 | dev->data = NULL; |
| 531 | dev->destroy = usb_generic_destroy; |
| 532 | |
| 533 | if (usb_generic_create) |
| 534 | usb_generic_create(dev); |
| 535 | } |