Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the libpayload project. |
| 3 | * |
| 4 | * Copyright (C) 2008 coresystems GmbH |
| 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 | |
| 30 | #include <config.h> |
Jordan Crouse | 29061a5 | 2008-09-11 17:29:00 +0000 | [diff] [blame^] | 31 | #include <usb/usb.h> |
Patrick Georgi | d21f68b | 2008-09-02 16:06:22 +0000 | [diff] [blame] | 32 | |
| 33 | hci_t *usb_hcs = 0; |
| 34 | |
| 35 | hci_t * |
| 36 | new_controller () |
| 37 | { |
| 38 | hci_t *controller = malloc (sizeof (hci_t)); |
| 39 | |
| 40 | /* atomic */ |
| 41 | controller->next = usb_hcs; |
| 42 | usb_hcs = controller; |
| 43 | /* atomic end */ |
| 44 | |
| 45 | return controller; |
| 46 | } |
| 47 | |
| 48 | void |
| 49 | detach_controller (hci_t *controller) |
| 50 | { |
| 51 | if (controller == 0) |
| 52 | return; |
| 53 | if (usb_hcs == controller) { |
| 54 | usb_hcs = controller->next; |
| 55 | } else { |
| 56 | hci_t *it = usb_hcs; |
| 57 | while (it != 0) { |
| 58 | if (it->next == controller) { |
| 59 | it->next = controller->next; |
| 60 | return; |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Polls all hubs on all USB controllers, to find out about device changes |
| 68 | */ |
| 69 | void |
| 70 | usb_poll () |
| 71 | { |
| 72 | if (usb_hcs == 0) |
| 73 | return; |
| 74 | hci_t *controller = usb_hcs; |
| 75 | while (controller != 0) { |
| 76 | int i; |
| 77 | for (i = 0; i < 128; i++) { |
| 78 | if (controller->devices[i].address != -1) { |
| 79 | controller->devices[i].poll (&controller-> |
| 80 | devices[i]); |
| 81 | } |
| 82 | } |
| 83 | controller = controller->next; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | void |
| 88 | init_device_entry (hci_t *controller, int i) |
| 89 | { |
| 90 | controller->devices[i].controller = controller; |
| 91 | controller->devices[i].address = -1; |
| 92 | controller->devices[i].hub = -1; |
| 93 | controller->devices[i].port = -1; |
| 94 | controller->devices[i].init = usb_nop_init; |
| 95 | controller->devices[i].init (&controller->devices[i]); |
| 96 | } |
| 97 | |
| 98 | void |
| 99 | set_feature (usbdev_t *dev, int endp, int feature, int rtype) |
| 100 | { |
| 101 | dev_req_t dr; |
| 102 | |
| 103 | dr.bmRequestType = rtype; |
| 104 | dr.data_dir = host_to_device; |
| 105 | dr.bRequest = SET_FEATURE; |
| 106 | dr.wValue = feature; |
| 107 | dr.wIndex = endp; |
| 108 | dr.wLength = 0; |
| 109 | dev->controller->control (dev, OUT, sizeof (dr), &dr, 0, 0); |
| 110 | } |
| 111 | |
| 112 | void |
| 113 | get_status (usbdev_t *dev, int intf, int rtype, int len, void *data) |
| 114 | { |
| 115 | dev_req_t dr; |
| 116 | |
| 117 | dr.bmRequestType = rtype; |
| 118 | dr.data_dir = device_to_host; |
| 119 | dr.bRequest = GET_STATUS; |
| 120 | dr.wValue = 0; |
| 121 | dr.wIndex = intf; |
| 122 | dr.wLength = len; |
| 123 | dev->controller->control (dev, IN, sizeof (dr), &dr, len, data); |
| 124 | } |
| 125 | |
| 126 | u8 * |
| 127 | get_descriptor (usbdev_t *dev, unsigned char bmRequestType, int descType, |
| 128 | int descIdx, int langID) |
| 129 | { |
| 130 | u8 buf[8]; |
| 131 | u8 *result; |
| 132 | dev_req_t dr; |
| 133 | int size; |
| 134 | |
| 135 | dr.bmRequestType = bmRequestType; |
| 136 | dr.data_dir = device_to_host; // always like this for descriptors |
| 137 | dr.bRequest = GET_DESCRIPTOR; |
| 138 | dr.wValue = (descType << 8) | descIdx; |
| 139 | dr.wIndex = langID; |
| 140 | dr.wLength = 8; |
| 141 | if (dev->controller->control (dev, IN, sizeof (dr), &dr, 8, buf)) { |
| 142 | printf ("getting descriptor size (type %x) failed\n", |
| 143 | descType); |
| 144 | } |
| 145 | |
| 146 | if (descType == 1) { |
| 147 | device_descriptor_t *dd = (device_descriptor_t *) buf; |
| 148 | printf ("maxPacketSize0: %x\n", dd->bMaxPacketSize0); |
| 149 | if (dd->bMaxPacketSize0 != 0) |
| 150 | dev->endpoints[0].maxpacketsize = dd->bMaxPacketSize0; |
| 151 | } |
| 152 | |
| 153 | /* special case for configuration descriptors: they carry all their |
| 154 | subsequent descriptors with them, and keep the entire size at a |
| 155 | different location */ |
| 156 | size = buf[0]; |
| 157 | if (buf[1] == 2) { |
| 158 | int realsize = ((unsigned short *) (buf + 2))[0]; |
| 159 | size = realsize; |
| 160 | } |
| 161 | result = malloc (size); |
| 162 | memset (result, 0, size); |
| 163 | dr.wLength = size; |
| 164 | if (dev->controller-> |
| 165 | control (dev, IN, sizeof (dr), &dr, size, result)) { |
| 166 | printf ("getting descriptor (type %x, size %x) failed\n", |
| 167 | descType, size); |
| 168 | } |
| 169 | |
| 170 | return result; |
| 171 | } |
| 172 | |
| 173 | void |
| 174 | set_configuration (usbdev_t *dev) |
| 175 | { |
| 176 | dev_req_t dr; |
| 177 | |
| 178 | dr.bmRequestType = 0; |
| 179 | dr.bRequest = SET_CONFIGURATION; |
| 180 | dr.wValue = dev->configuration[5]; |
| 181 | dr.wIndex = 0; |
| 182 | dr.wLength = 0; |
| 183 | dev->controller->control (dev, OUT, sizeof (dr), &dr, 0, 0); |
| 184 | } |
| 185 | |
| 186 | int |
| 187 | clear_stall (endpoint_t *ep) |
| 188 | { |
| 189 | usbdev_t *dev = ep->dev; |
| 190 | int endp = ep->endpoint; |
| 191 | dev_req_t dr; |
| 192 | |
| 193 | dr.bmRequestType = 0; |
| 194 | if (endp != 0) { |
| 195 | dr.req_recp = endp_recp; |
| 196 | } |
| 197 | dr.bRequest = CLEAR_FEATURE; |
| 198 | dr.wValue = ENDPOINT_HALT; |
| 199 | dr.wIndex = endp; |
| 200 | dr.wLength = 0; |
| 201 | dev->controller->control (dev, OUT, sizeof (dr), &dr, 0, 0); |
| 202 | return 0; |
| 203 | } |
| 204 | |
| 205 | /* returns free address or -1 */ |
| 206 | static int |
| 207 | get_free_address (hci_t *controller) |
| 208 | { |
| 209 | int i; |
| 210 | for (i = 1; i < 128; i++) { |
| 211 | if (controller->devices[i].address != i) |
| 212 | return i; |
| 213 | } |
| 214 | printf ("no free address found\n"); |
| 215 | return -1; // no free address |
| 216 | } |
| 217 | |
| 218 | int |
| 219 | set_address (hci_t *controller, int lowspeed) |
| 220 | { |
| 221 | int adr = get_free_address (controller); // address to set |
| 222 | dev_req_t dr; |
| 223 | configuration_descriptor_t *cd; |
| 224 | device_descriptor_t *dd; |
| 225 | |
| 226 | memset (&dr, 0, sizeof (dr)); |
| 227 | dr.data_dir = host_to_device; |
| 228 | dr.req_type = standard_type; |
| 229 | dr.req_recp = dev_recp; |
| 230 | dr.bRequest = SET_ADDRESS; |
| 231 | dr.wValue = adr; |
| 232 | dr.wIndex = 0; |
| 233 | dr.wLength = 0; |
| 234 | |
| 235 | usbdev_t *dev = &controller->devices[adr]; |
| 236 | // dummy values for registering the address |
| 237 | dev->address = 0; |
| 238 | dev->lowspeed = lowspeed; |
| 239 | dev->endpoints[0].dev = dev; |
| 240 | dev->endpoints[0].endpoint = 0; |
| 241 | dev->endpoints[0].maxpacketsize = 8; |
| 242 | dev->endpoints[0].toggle = 0; |
| 243 | dev->endpoints[0].direction = SETUP; |
| 244 | mdelay (50); |
| 245 | if (dev->controller->control (dev, OUT, sizeof (dr), &dr, 0, 0)) { |
| 246 | printf ("set_address failed\n"); |
| 247 | return -1; |
| 248 | } |
| 249 | mdelay (50); |
| 250 | dev->address = adr; |
| 251 | dev->descriptor = |
| 252 | get_descriptor (dev, |
| 253 | gen_bmRequestType (device_to_host, |
| 254 | standard_type, dev_recp), |
| 255 | 1, 0, 0); |
| 256 | dd = (device_descriptor_t *) dev->descriptor; |
| 257 | printf ("device version: %x.%x\n", dd->bcdUSB >> 8, |
| 258 | dd->bcdUSB & 0xff); |
| 259 | printf ("device has %x configurations\n", dd->bNumConfigurations); |
| 260 | if (dd->bNumConfigurations == 0) { |
| 261 | /* device isn't usable */ |
| 262 | printf ("no usable configuration!\n"); |
| 263 | dev->address = 0; |
| 264 | return -1; |
| 265 | } |
| 266 | dev->configuration = |
| 267 | get_descriptor (dev, |
| 268 | gen_bmRequestType (device_to_host, |
| 269 | standard_type, dev_recp), |
| 270 | 2, 0, 0); |
| 271 | cd = (configuration_descriptor_t *) dev->configuration; |
| 272 | set_configuration (dev); |
| 273 | interface_descriptor_t *interface = |
| 274 | (interface_descriptor_t *) (((char *) cd) + cd->bLength); |
| 275 | { |
| 276 | int i; |
| 277 | int num = cd->bNumInterfaces; |
| 278 | interface_descriptor_t *current = interface; |
| 279 | printf ("device has %x interfaces\n", num); |
| 280 | num = (num > 5) ? 5 : num; |
| 281 | for (i = 0; i < num; i++) { |
| 282 | int j; |
| 283 | printf (" #%x has %x endpoints, interface %x:%x, protocol %x\n", current->bInterfaceNumber, current->bNumEndpoints, current->bInterfaceClass, current->bInterfaceSubClass, current->bInterfaceProtocol); |
| 284 | endpoint_descriptor_t *endp = |
| 285 | (endpoint_descriptor_t *) (((char *) current) |
| 286 | + |
| 287 | current->bLength); |
| 288 | if (interface->bInterfaceClass == 0x3) |
| 289 | endp = (endpoint_descriptor_t *) (((char *) endp) + ((char *) endp)[0]); // ignore HID descriptor |
| 290 | memset (dev->endpoints, 0, sizeof (dev->endpoints)); |
| 291 | dev->num_endp = 1; // 0 always exists |
| 292 | dev->endpoints[0].dev = dev; |
| 293 | dev->endpoints[0].maxpacketsize = dd->bMaxPacketSize0; |
| 294 | dev->endpoints[0].direction = SETUP; |
| 295 | dev->endpoints[0].type = CONTROL; |
| 296 | for (j = 1; j <= current->bNumEndpoints; j++) { |
| 297 | static const char *transfertypes[4] = |
| 298 | { "control", "isochronous", "bulk", |
| 299 | "interrupt" |
| 300 | }; |
| 301 | printf (" #%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]); |
| 302 | endpoint_t *ep = |
| 303 | &dev->endpoints[dev->num_endp++]; |
| 304 | ep->dev = dev; |
| 305 | ep->endpoint = endp->bEndpointAddress; |
| 306 | ep->toggle = 0; |
| 307 | ep->maxpacketsize = endp->wMaxPacketSize; |
| 308 | ep->direction = |
| 309 | ((endp->bEndpointAddress & 0x80) == |
| 310 | 0) ? OUT : IN; |
| 311 | ep->type = endp->bmAttributes; |
| 312 | endp = (endpoint_descriptor_t |
| 313 | *) (((char *) endp) + endp->bLength); |
| 314 | } |
| 315 | current = (interface_descriptor_t *) endp; |
| 316 | } |
| 317 | } |
| 318 | int class = dd->bDeviceClass; |
| 319 | if (class == 0) |
| 320 | class = interface->bInterfaceClass; |
| 321 | |
| 322 | enum { hid_device = 0x3, msc_device = 0x8, hub_device = 0x9 }; |
| 323 | |
| 324 | printf ("device of class %x found\n", class); |
| 325 | if (class == hub_device) { |
| 326 | printf ("hub found\n"); |
| 327 | #ifdef CONFIG_USB_HUB |
| 328 | controller->devices[adr].init = usb_hub_init; |
| 329 | #else |
| 330 | printf ("support not compiled in\n"); |
| 331 | #endif |
| 332 | } |
| 333 | if (class == hid_device) { |
| 334 | printf ("HID found\n"); |
| 335 | #ifdef CONFIG_USB_HID |
| 336 | controller->devices[adr].init = usb_hid_init; |
| 337 | #else |
| 338 | printf ("support not compiled in\n"); |
| 339 | #endif |
| 340 | } |
| 341 | if (class == msc_device) { |
| 342 | printf ("MSC found\n"); |
| 343 | #ifdef CONFIG_USB_MSC |
| 344 | controller->devices[adr].init = usb_msc_init; |
| 345 | #else |
| 346 | printf ("support not compiled in\n"); |
| 347 | #endif |
| 348 | } |
| 349 | return adr; |
| 350 | } |