Simon Glass | 9b82eeb | 2015-03-25 12:21:59 -0600 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2015 Google, Inc |
| 3 | * Written by Simon Glass <sjg@chromium.org> |
| 4 | * |
Simon Glass | fc03a55 | 2015-03-25 12:22:30 -0600 | [diff] [blame] | 5 | * usb_match_device() modified from Linux kernel v4.0. |
| 6 | * |
Simon Glass | 9b82eeb | 2015-03-25 12:21:59 -0600 | [diff] [blame] | 7 | * SPDX-License-Identifier: GPL-2.0+ |
| 8 | */ |
| 9 | |
| 10 | #include <common.h> |
| 11 | #include <dm.h> |
| 12 | #include <errno.h> |
| 13 | #include <usb.h> |
| 14 | #include <dm/device-internal.h> |
| 15 | #include <dm/lists.h> |
| 16 | #include <dm/root.h> |
| 17 | #include <dm/uclass-internal.h> |
| 18 | |
| 19 | DECLARE_GLOBAL_DATA_PTR; |
| 20 | |
| 21 | extern bool usb_started; /* flag for the started/stopped USB status */ |
| 22 | static bool asynch_allowed; |
| 23 | |
Hans de Goede | cebc5b7 | 2015-05-10 14:10:21 +0200 | [diff] [blame] | 24 | struct usb_uclass_priv { |
| 25 | int companion_device_count; |
| 26 | }; |
| 27 | |
Simon Glass | 9b82eeb | 2015-03-25 12:21:59 -0600 | [diff] [blame] | 28 | int usb_disable_asynch(int disable) |
| 29 | { |
| 30 | int old_value = asynch_allowed; |
| 31 | |
| 32 | asynch_allowed = !disable; |
| 33 | return old_value; |
| 34 | } |
| 35 | |
| 36 | int submit_int_msg(struct usb_device *udev, unsigned long pipe, void *buffer, |
| 37 | int length, int interval) |
| 38 | { |
| 39 | struct udevice *bus = udev->controller_dev; |
| 40 | struct dm_usb_ops *ops = usb_get_ops(bus); |
| 41 | |
| 42 | if (!ops->interrupt) |
| 43 | return -ENOSYS; |
| 44 | |
| 45 | return ops->interrupt(bus, udev, pipe, buffer, length, interval); |
| 46 | } |
| 47 | |
| 48 | int submit_control_msg(struct usb_device *udev, unsigned long pipe, |
| 49 | void *buffer, int length, struct devrequest *setup) |
| 50 | { |
| 51 | struct udevice *bus = udev->controller_dev; |
| 52 | struct dm_usb_ops *ops = usb_get_ops(bus); |
Hans de Goede | cebc5b7 | 2015-05-10 14:10:21 +0200 | [diff] [blame] | 53 | struct usb_uclass_priv *uc_priv = bus->uclass->priv; |
| 54 | int err; |
Simon Glass | 9b82eeb | 2015-03-25 12:21:59 -0600 | [diff] [blame] | 55 | |
| 56 | if (!ops->control) |
| 57 | return -ENOSYS; |
| 58 | |
Hans de Goede | cebc5b7 | 2015-05-10 14:10:21 +0200 | [diff] [blame] | 59 | err = ops->control(bus, udev, pipe, buffer, length, setup); |
| 60 | if (setup->request == USB_REQ_SET_FEATURE && |
| 61 | setup->requesttype == USB_RT_PORT && |
| 62 | setup->value == cpu_to_le16(USB_PORT_FEAT_RESET) && |
| 63 | err == -ENXIO) { |
| 64 | /* Device handed over to companion after port reset */ |
| 65 | uc_priv->companion_device_count++; |
| 66 | } |
| 67 | |
| 68 | return err; |
Simon Glass | 9b82eeb | 2015-03-25 12:21:59 -0600 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | int submit_bulk_msg(struct usb_device *udev, unsigned long pipe, void *buffer, |
| 72 | int length) |
| 73 | { |
| 74 | struct udevice *bus = udev->controller_dev; |
| 75 | struct dm_usb_ops *ops = usb_get_ops(bus); |
| 76 | |
| 77 | if (!ops->bulk) |
| 78 | return -ENOSYS; |
| 79 | |
| 80 | return ops->bulk(bus, udev, pipe, buffer, length); |
| 81 | } |
| 82 | |
Hans de Goede | 0a7fa27 | 2015-05-10 14:10:18 +0200 | [diff] [blame] | 83 | struct int_queue *create_int_queue(struct usb_device *udev, |
| 84 | unsigned long pipe, int queuesize, int elementsize, |
| 85 | void *buffer, int interval) |
| 86 | { |
| 87 | struct udevice *bus = udev->controller_dev; |
| 88 | struct dm_usb_ops *ops = usb_get_ops(bus); |
| 89 | |
| 90 | if (!ops->create_int_queue) |
| 91 | return NULL; |
| 92 | |
| 93 | return ops->create_int_queue(bus, udev, pipe, queuesize, elementsize, |
| 94 | buffer, interval); |
| 95 | } |
| 96 | |
| 97 | void *poll_int_queue(struct usb_device *udev, struct int_queue *queue) |
| 98 | { |
| 99 | struct udevice *bus = udev->controller_dev; |
| 100 | struct dm_usb_ops *ops = usb_get_ops(bus); |
| 101 | |
| 102 | if (!ops->poll_int_queue) |
| 103 | return NULL; |
| 104 | |
| 105 | return ops->poll_int_queue(bus, udev, queue); |
| 106 | } |
| 107 | |
| 108 | int destroy_int_queue(struct usb_device *udev, struct int_queue *queue) |
| 109 | { |
| 110 | struct udevice *bus = udev->controller_dev; |
| 111 | struct dm_usb_ops *ops = usb_get_ops(bus); |
| 112 | |
| 113 | if (!ops->destroy_int_queue) |
| 114 | return -ENOSYS; |
| 115 | |
| 116 | return ops->destroy_int_queue(bus, udev, queue); |
| 117 | } |
| 118 | |
Simon Glass | 9b82eeb | 2015-03-25 12:21:59 -0600 | [diff] [blame] | 119 | int usb_alloc_device(struct usb_device *udev) |
| 120 | { |
| 121 | struct udevice *bus = udev->controller_dev; |
| 122 | struct dm_usb_ops *ops = usb_get_ops(bus); |
| 123 | |
| 124 | /* This is only requird by some controllers - current XHCI */ |
| 125 | if (!ops->alloc_device) |
| 126 | return 0; |
| 127 | |
| 128 | return ops->alloc_device(bus, udev); |
| 129 | } |
| 130 | |
| 131 | int usb_stop(void) |
| 132 | { |
| 133 | struct udevice *bus; |
| 134 | struct uclass *uc; |
Hans de Goede | cebc5b7 | 2015-05-10 14:10:21 +0200 | [diff] [blame] | 135 | struct usb_uclass_priv *uc_priv; |
Simon Glass | 9b82eeb | 2015-03-25 12:21:59 -0600 | [diff] [blame] | 136 | int err = 0, ret; |
| 137 | |
| 138 | /* De-activate any devices that have been activated */ |
| 139 | ret = uclass_get(UCLASS_USB, &uc); |
| 140 | if (ret) |
| 141 | return ret; |
Hans de Goede | cebc5b7 | 2015-05-10 14:10:21 +0200 | [diff] [blame] | 142 | |
| 143 | uc_priv = uc->priv; |
| 144 | |
Simon Glass | 9b82eeb | 2015-03-25 12:21:59 -0600 | [diff] [blame] | 145 | uclass_foreach_dev(bus, uc) { |
| 146 | ret = device_remove(bus); |
| 147 | if (ret && !err) |
| 148 | err = ret; |
| 149 | } |
| 150 | |
Simon Glass | 4147739 | 2015-03-25 12:22:38 -0600 | [diff] [blame] | 151 | #ifdef CONFIG_SANDBOX |
| 152 | struct udevice *dev; |
| 153 | |
| 154 | /* Reset all enulation devices */ |
| 155 | ret = uclass_get(UCLASS_USB_EMUL, &uc); |
| 156 | if (ret) |
| 157 | return ret; |
| 158 | |
| 159 | uclass_foreach_dev(dev, uc) |
| 160 | usb_emul_reset(dev); |
| 161 | #endif |
Simon Glass | 9b82eeb | 2015-03-25 12:21:59 -0600 | [diff] [blame] | 162 | usb_stor_reset(); |
| 163 | usb_hub_reset(); |
Hans de Goede | cebc5b7 | 2015-05-10 14:10:21 +0200 | [diff] [blame] | 164 | uc_priv->companion_device_count = 0; |
Simon Glass | 9b82eeb | 2015-03-25 12:21:59 -0600 | [diff] [blame] | 165 | usb_started = 0; |
| 166 | |
| 167 | return err; |
| 168 | } |
| 169 | |
Hans de Goede | f8762f9 | 2015-05-10 14:10:19 +0200 | [diff] [blame] | 170 | static void usb_scan_bus(struct udevice *bus, bool recurse) |
Simon Glass | 9b82eeb | 2015-03-25 12:21:59 -0600 | [diff] [blame] | 171 | { |
| 172 | struct usb_bus_priv *priv; |
| 173 | struct udevice *dev; |
| 174 | int ret; |
| 175 | |
| 176 | priv = dev_get_uclass_priv(bus); |
| 177 | |
| 178 | assert(recurse); /* TODO: Support non-recusive */ |
| 179 | |
Hans de Goede | f8762f9 | 2015-05-10 14:10:19 +0200 | [diff] [blame] | 180 | printf("scanning bus %d for devices... ", bus->seq); |
| 181 | debug("\n"); |
Simon Glass | 9b82eeb | 2015-03-25 12:21:59 -0600 | [diff] [blame] | 182 | ret = usb_scan_device(bus, 0, USB_SPEED_FULL, &dev); |
| 183 | if (ret) |
Hans de Goede | f8762f9 | 2015-05-10 14:10:19 +0200 | [diff] [blame] | 184 | printf("failed, error %d\n", ret); |
| 185 | else if (priv->next_addr == 0) |
| 186 | printf("No USB Device found\n"); |
| 187 | else |
| 188 | printf("%d USB Device(s) found\n", priv->next_addr); |
Simon Glass | 9b82eeb | 2015-03-25 12:21:59 -0600 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | int usb_init(void) |
| 192 | { |
| 193 | int controllers_initialized = 0; |
Hans de Goede | cebc5b7 | 2015-05-10 14:10:21 +0200 | [diff] [blame] | 194 | struct usb_uclass_priv *uc_priv; |
Hans de Goede | 59a0dfc | 2015-05-10 14:10:20 +0200 | [diff] [blame] | 195 | struct usb_bus_priv *priv; |
Simon Glass | 9b82eeb | 2015-03-25 12:21:59 -0600 | [diff] [blame] | 196 | struct udevice *bus; |
| 197 | struct uclass *uc; |
| 198 | int count = 0; |
| 199 | int ret; |
| 200 | |
| 201 | asynch_allowed = 1; |
| 202 | usb_hub_reset(); |
| 203 | |
| 204 | ret = uclass_get(UCLASS_USB, &uc); |
| 205 | if (ret) |
| 206 | return ret; |
| 207 | |
Hans de Goede | cebc5b7 | 2015-05-10 14:10:21 +0200 | [diff] [blame] | 208 | uc_priv = uc->priv; |
| 209 | |
Simon Glass | 9b82eeb | 2015-03-25 12:21:59 -0600 | [diff] [blame] | 210 | uclass_foreach_dev(bus, uc) { |
| 211 | /* init low_level USB */ |
Hans de Goede | 8679b3a | 2015-05-04 21:33:26 +0200 | [diff] [blame] | 212 | printf("USB%d: ", count); |
Simon Glass | 9b82eeb | 2015-03-25 12:21:59 -0600 | [diff] [blame] | 213 | count++; |
Simon Glass | 9b82eeb | 2015-03-25 12:21:59 -0600 | [diff] [blame] | 214 | ret = device_probe(bus); |
| 215 | if (ret == -ENODEV) { /* No such device. */ |
| 216 | puts("Port not available.\n"); |
| 217 | controllers_initialized++; |
| 218 | continue; |
| 219 | } |
| 220 | |
| 221 | if (ret) { /* Other error. */ |
| 222 | printf("probe failed, error %d\n", ret); |
| 223 | continue; |
| 224 | } |
Simon Glass | 9b82eeb | 2015-03-25 12:21:59 -0600 | [diff] [blame] | 225 | controllers_initialized++; |
Simon Glass | 9b82eeb | 2015-03-25 12:21:59 -0600 | [diff] [blame] | 226 | usb_started = true; |
| 227 | } |
| 228 | |
Hans de Goede | 59a0dfc | 2015-05-10 14:10:20 +0200 | [diff] [blame] | 229 | /* |
| 230 | * lowlevel init done, now scan the bus for devices i.e. search HUBs |
| 231 | * and configure them, first scan primary controllers. |
| 232 | */ |
| 233 | uclass_foreach_dev(bus, uc) { |
| 234 | if (!device_active(bus)) |
| 235 | continue; |
| 236 | |
| 237 | priv = dev_get_uclass_priv(bus); |
| 238 | if (!priv->companion) |
| 239 | usb_scan_bus(bus, true); |
| 240 | } |
| 241 | |
| 242 | /* |
| 243 | * Now that the primary controllers have been scanned and have handed |
| 244 | * over any devices they do not understand to their companions, scan |
Hans de Goede | cebc5b7 | 2015-05-10 14:10:21 +0200 | [diff] [blame] | 245 | * the companions if necessary. |
Hans de Goede | 59a0dfc | 2015-05-10 14:10:20 +0200 | [diff] [blame] | 246 | */ |
Hans de Goede | cebc5b7 | 2015-05-10 14:10:21 +0200 | [diff] [blame] | 247 | if (uc_priv->companion_device_count) { |
| 248 | uclass_foreach_dev(bus, uc) { |
| 249 | if (!device_active(bus)) |
| 250 | continue; |
Hans de Goede | 59a0dfc | 2015-05-10 14:10:20 +0200 | [diff] [blame] | 251 | |
Hans de Goede | cebc5b7 | 2015-05-10 14:10:21 +0200 | [diff] [blame] | 252 | priv = dev_get_uclass_priv(bus); |
| 253 | if (priv->companion) |
| 254 | usb_scan_bus(bus, true); |
| 255 | } |
Hans de Goede | 59a0dfc | 2015-05-10 14:10:20 +0200 | [diff] [blame] | 256 | } |
| 257 | |
Simon Glass | 9b82eeb | 2015-03-25 12:21:59 -0600 | [diff] [blame] | 258 | debug("scan end\n"); |
| 259 | /* if we were not able to find at least one working bus, bail out */ |
| 260 | if (!count) |
| 261 | printf("No controllers found\n"); |
| 262 | else if (controllers_initialized == 0) |
| 263 | printf("USB error: all controllers failed lowlevel init\n"); |
| 264 | |
| 265 | return usb_started ? 0 : -1; |
| 266 | } |
| 267 | |
| 268 | int usb_reset_root_port(void) |
| 269 | { |
| 270 | return -ENOSYS; |
| 271 | } |
| 272 | |
| 273 | static struct usb_device *find_child_devnum(struct udevice *parent, int devnum) |
| 274 | { |
| 275 | struct usb_device *udev; |
| 276 | struct udevice *dev; |
| 277 | |
| 278 | if (!device_active(parent)) |
| 279 | return NULL; |
| 280 | udev = dev_get_parentdata(parent); |
| 281 | if (udev->devnum == devnum) |
| 282 | return udev; |
| 283 | |
| 284 | for (device_find_first_child(parent, &dev); |
| 285 | dev; |
| 286 | device_find_next_child(&dev)) { |
| 287 | udev = find_child_devnum(dev, devnum); |
| 288 | if (udev) |
| 289 | return udev; |
| 290 | } |
| 291 | |
| 292 | return NULL; |
| 293 | } |
| 294 | |
| 295 | struct usb_device *usb_get_dev_index(struct udevice *bus, int index) |
| 296 | { |
| 297 | struct udevice *hub; |
| 298 | int devnum = index + 1; /* Addresses are allocated from 1 on USB */ |
| 299 | |
| 300 | device_find_first_child(bus, &hub); |
| 301 | if (device_get_uclass_id(hub) == UCLASS_USB_HUB) |
| 302 | return find_child_devnum(hub, devnum); |
| 303 | |
| 304 | return NULL; |
| 305 | } |
| 306 | |
| 307 | int usb_post_bind(struct udevice *dev) |
| 308 | { |
| 309 | /* Scan the bus for devices */ |
| 310 | return dm_scan_fdt_node(dev, gd->fdt_blob, dev->of_offset, false); |
| 311 | } |
| 312 | |
| 313 | int usb_port_reset(struct usb_device *parent, int portnr) |
| 314 | { |
| 315 | unsigned short portstatus; |
| 316 | int ret; |
| 317 | |
| 318 | debug("%s: start\n", __func__); |
| 319 | |
| 320 | if (parent) { |
| 321 | /* reset the port for the second time */ |
| 322 | assert(portnr > 0); |
| 323 | debug("%s: reset %d\n", __func__, portnr - 1); |
| 324 | ret = legacy_hub_port_reset(parent, portnr - 1, &portstatus); |
| 325 | if (ret < 0) { |
| 326 | printf("\n Couldn't reset port %i\n", portnr); |
| 327 | return ret; |
| 328 | } |
| 329 | } else { |
| 330 | debug("%s: reset root\n", __func__); |
| 331 | usb_reset_root_port(); |
| 332 | } |
| 333 | |
| 334 | return 0; |
| 335 | } |
| 336 | |
| 337 | int usb_legacy_port_reset(struct usb_device *parent, int portnr) |
| 338 | { |
| 339 | return usb_port_reset(parent, portnr); |
| 340 | } |
| 341 | |
Simon Glass | 444b1e0 | 2015-03-25 12:22:32 -0600 | [diff] [blame] | 342 | int usb_setup_ehci_gadget(struct ehci_ctrl **ctlrp) |
| 343 | { |
| 344 | struct usb_platdata *plat; |
| 345 | struct udevice *dev; |
| 346 | int ret; |
| 347 | |
| 348 | /* Find the old device and remove it */ |
| 349 | ret = uclass_find_device_by_seq(UCLASS_USB, 0, true, &dev); |
| 350 | if (ret) |
| 351 | return ret; |
| 352 | ret = device_remove(dev); |
| 353 | if (ret) |
| 354 | return ret; |
| 355 | |
| 356 | plat = dev_get_platdata(dev); |
| 357 | plat->init_type = USB_INIT_DEVICE; |
| 358 | ret = device_probe(dev); |
| 359 | if (ret) |
| 360 | return ret; |
| 361 | *ctlrp = dev_get_priv(dev); |
| 362 | |
| 363 | return 0; |
| 364 | } |
| 365 | |
Simon Glass | fc03a55 | 2015-03-25 12:22:30 -0600 | [diff] [blame] | 366 | /* returns 0 if no match, 1 if match */ |
| 367 | int usb_match_device(const struct usb_device_descriptor *desc, |
| 368 | const struct usb_device_id *id) |
| 369 | { |
| 370 | if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) && |
| 371 | id->idVendor != le16_to_cpu(desc->idVendor)) |
| 372 | return 0; |
| 373 | |
| 374 | if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) && |
| 375 | id->idProduct != le16_to_cpu(desc->idProduct)) |
| 376 | return 0; |
| 377 | |
| 378 | /* No need to test id->bcdDevice_lo != 0, since 0 is never |
| 379 | greater than any unsigned number. */ |
| 380 | if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) && |
| 381 | (id->bcdDevice_lo > le16_to_cpu(desc->bcdDevice))) |
| 382 | return 0; |
| 383 | |
| 384 | if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) && |
| 385 | (id->bcdDevice_hi < le16_to_cpu(desc->bcdDevice))) |
| 386 | return 0; |
| 387 | |
| 388 | if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) && |
| 389 | (id->bDeviceClass != desc->bDeviceClass)) |
| 390 | return 0; |
| 391 | |
| 392 | if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) && |
| 393 | (id->bDeviceSubClass != desc->bDeviceSubClass)) |
| 394 | return 0; |
| 395 | |
| 396 | if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) && |
| 397 | (id->bDeviceProtocol != desc->bDeviceProtocol)) |
| 398 | return 0; |
| 399 | |
| 400 | return 1; |
| 401 | } |
| 402 | |
| 403 | /* returns 0 if no match, 1 if match */ |
| 404 | int usb_match_one_id_intf(const struct usb_device_descriptor *desc, |
| 405 | const struct usb_interface_descriptor *int_desc, |
| 406 | const struct usb_device_id *id) |
| 407 | { |
| 408 | /* The interface class, subclass, protocol and number should never be |
| 409 | * checked for a match if the device class is Vendor Specific, |
| 410 | * unless the match record specifies the Vendor ID. */ |
| 411 | if (desc->bDeviceClass == USB_CLASS_VENDOR_SPEC && |
| 412 | !(id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) && |
| 413 | (id->match_flags & (USB_DEVICE_ID_MATCH_INT_CLASS | |
| 414 | USB_DEVICE_ID_MATCH_INT_SUBCLASS | |
| 415 | USB_DEVICE_ID_MATCH_INT_PROTOCOL | |
| 416 | USB_DEVICE_ID_MATCH_INT_NUMBER))) |
| 417 | return 0; |
| 418 | |
| 419 | if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) && |
| 420 | (id->bInterfaceClass != int_desc->bInterfaceClass)) |
| 421 | return 0; |
| 422 | |
| 423 | if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) && |
| 424 | (id->bInterfaceSubClass != int_desc->bInterfaceSubClass)) |
| 425 | return 0; |
| 426 | |
| 427 | if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) && |
| 428 | (id->bInterfaceProtocol != int_desc->bInterfaceProtocol)) |
| 429 | return 0; |
| 430 | |
| 431 | if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_NUMBER) && |
| 432 | (id->bInterfaceNumber != int_desc->bInterfaceNumber)) |
| 433 | return 0; |
| 434 | |
| 435 | return 1; |
| 436 | } |
| 437 | |
| 438 | /* returns 0 if no match, 1 if match */ |
| 439 | int usb_match_one_id(struct usb_device_descriptor *desc, |
| 440 | struct usb_interface_descriptor *int_desc, |
| 441 | const struct usb_device_id *id) |
| 442 | { |
| 443 | if (!usb_match_device(desc, id)) |
| 444 | return 0; |
| 445 | |
| 446 | return usb_match_one_id_intf(desc, int_desc, id); |
| 447 | } |
| 448 | |
| 449 | /** |
| 450 | * usb_find_and_bind_driver() - Find and bind the right USB driver |
| 451 | * |
| 452 | * This only looks at certain fields in the descriptor. |
| 453 | */ |
| 454 | static int usb_find_and_bind_driver(struct udevice *parent, |
| 455 | struct usb_device_descriptor *desc, |
| 456 | struct usb_interface_descriptor *iface, |
| 457 | int bus_seq, int devnum, |
| 458 | struct udevice **devp) |
| 459 | { |
| 460 | struct usb_driver_entry *start, *entry; |
| 461 | int n_ents; |
| 462 | int ret; |
| 463 | char name[30], *str; |
| 464 | |
| 465 | *devp = NULL; |
| 466 | debug("%s: Searching for driver\n", __func__); |
| 467 | start = ll_entry_start(struct usb_driver_entry, usb_driver_entry); |
| 468 | n_ents = ll_entry_count(struct usb_driver_entry, usb_driver_entry); |
| 469 | for (entry = start; entry != start + n_ents; entry++) { |
| 470 | const struct usb_device_id *id; |
| 471 | struct udevice *dev; |
| 472 | const struct driver *drv; |
| 473 | struct usb_dev_platdata *plat; |
| 474 | |
| 475 | for (id = entry->match; id->match_flags; id++) { |
| 476 | if (!usb_match_one_id(desc, iface, id)) |
| 477 | continue; |
| 478 | |
| 479 | drv = entry->driver; |
| 480 | /* |
| 481 | * We could pass the descriptor to the driver as |
| 482 | * platdata (instead of NULL) and allow its bind() |
| 483 | * method to return -ENOENT if it doesn't support this |
| 484 | * device. That way we could continue the search to |
| 485 | * find another driver. For now this doesn't seem |
| 486 | * necesssary, so just bind the first match. |
| 487 | */ |
| 488 | ret = device_bind(parent, drv, drv->name, NULL, -1, |
| 489 | &dev); |
| 490 | if (ret) |
| 491 | goto error; |
| 492 | debug("%s: Match found: %s\n", __func__, drv->name); |
| 493 | dev->driver_data = id->driver_info; |
| 494 | plat = dev_get_parent_platdata(dev); |
| 495 | plat->id = *id; |
| 496 | *devp = dev; |
| 497 | return 0; |
| 498 | } |
| 499 | } |
| 500 | |
Simon Glass | c79173e | 2015-03-25 12:22:31 -0600 | [diff] [blame] | 501 | /* Bind a generic driver so that the device can be used */ |
| 502 | snprintf(name, sizeof(name), "generic_bus_%x_dev_%x", bus_seq, devnum); |
| 503 | str = strdup(name); |
| 504 | if (!str) |
| 505 | return -ENOMEM; |
| 506 | ret = device_bind_driver(parent, "usb_dev_generic_drv", str, devp); |
| 507 | |
Simon Glass | fc03a55 | 2015-03-25 12:22:30 -0600 | [diff] [blame] | 508 | error: |
| 509 | debug("%s: No match found: %d\n", __func__, ret); |
| 510 | return ret; |
| 511 | } |
| 512 | |
| 513 | /** |
| 514 | * usb_find_child() - Find an existing device which matches our needs |
| 515 | * |
| 516 | * |
| 517 | */ |
| 518 | static int usb_find_child(struct udevice *parent, |
| 519 | struct usb_device_descriptor *desc, |
| 520 | struct usb_interface_descriptor *iface, |
| 521 | struct udevice **devp) |
| 522 | { |
| 523 | struct udevice *dev; |
| 524 | |
| 525 | *devp = NULL; |
| 526 | for (device_find_first_child(parent, &dev); |
| 527 | dev; |
| 528 | device_find_next_child(&dev)) { |
| 529 | struct usb_dev_platdata *plat = dev_get_parent_platdata(dev); |
| 530 | |
| 531 | /* If this device is already in use, skip it */ |
| 532 | if (device_active(dev)) |
| 533 | continue; |
| 534 | debug(" %s: name='%s', plat=%d, desc=%d\n", __func__, |
| 535 | dev->name, plat->id.bDeviceClass, desc->bDeviceClass); |
| 536 | if (usb_match_one_id(desc, iface, &plat->id)) { |
| 537 | *devp = dev; |
| 538 | return 0; |
| 539 | } |
| 540 | } |
| 541 | |
| 542 | return -ENOENT; |
| 543 | } |
| 544 | |
Simon Glass | 9b82eeb | 2015-03-25 12:21:59 -0600 | [diff] [blame] | 545 | int usb_scan_device(struct udevice *parent, int port, |
| 546 | enum usb_device_speed speed, struct udevice **devp) |
| 547 | { |
| 548 | struct udevice *dev; |
| 549 | bool created = false; |
| 550 | struct usb_dev_platdata *plat; |
| 551 | struct usb_bus_priv *priv; |
| 552 | struct usb_device *parent_udev; |
| 553 | int ret; |
| 554 | ALLOC_CACHE_ALIGN_BUFFER(struct usb_device, udev, 1); |
| 555 | struct usb_interface_descriptor *iface = &udev->config.if_desc[0].desc; |
| 556 | |
| 557 | *devp = NULL; |
| 558 | memset(udev, '\0', sizeof(*udev)); |
Hans de Goede | 3b5ddd3 | 2015-05-05 11:54:31 +0200 | [diff] [blame] | 559 | udev->controller_dev = usb_get_bus(parent); |
Simon Glass | 9b82eeb | 2015-03-25 12:21:59 -0600 | [diff] [blame] | 560 | priv = dev_get_uclass_priv(udev->controller_dev); |
| 561 | |
| 562 | /* |
| 563 | * Somewhat nasty, this. We create a local device and use the normal |
| 564 | * USB stack to read its descriptor. Then we know what type of device |
| 565 | * to create for real. |
| 566 | * |
| 567 | * udev->dev is set to the parent, since we don't have a real device |
| 568 | * yet. The USB stack should not access udev.dev anyway, except perhaps |
| 569 | * to find the controller, and the controller will either be @parent, |
| 570 | * or some parent of @parent. |
| 571 | * |
| 572 | * Another option might be to create the device as a generic USB |
| 573 | * device, then morph it into the correct one when we know what it |
| 574 | * should be. This means that a generic USB device would morph into |
| 575 | * a network controller, or a USB flash stick, for example. However, |
| 576 | * we don't support such morphing and it isn't clear that it would |
| 577 | * be easy to do. |
| 578 | * |
| 579 | * Yet another option is to split out the USB stack parts of udev |
| 580 | * into something like a 'struct urb' (as Linux does) which can exist |
| 581 | * independently of any device. This feels cleaner, but calls for quite |
| 582 | * a big change to the USB stack. |
| 583 | * |
| 584 | * For now, the approach is to set up an empty udev, read its |
| 585 | * descriptor and assign it an address, then bind a real device and |
| 586 | * stash the resulting information into the device's parent |
| 587 | * platform data. Then when we probe it, usb_child_pre_probe() is called |
| 588 | * and it will pull the information out of the stash. |
| 589 | */ |
| 590 | udev->dev = parent; |
| 591 | udev->speed = speed; |
| 592 | udev->devnum = priv->next_addr + 1; |
| 593 | udev->portnr = port; |
| 594 | debug("Calling usb_setup_device(), portnr=%d\n", udev->portnr); |
| 595 | parent_udev = device_get_uclass_id(parent) == UCLASS_USB_HUB ? |
| 596 | dev_get_parentdata(parent) : NULL; |
| 597 | ret = usb_setup_device(udev, priv->desc_before_addr, parent_udev, port); |
| 598 | debug("read_descriptor for '%s': ret=%d\n", parent->name, ret); |
| 599 | if (ret) |
| 600 | return ret; |
| 601 | ret = usb_find_child(parent, &udev->descriptor, iface, &dev); |
| 602 | debug("** usb_find_child returns %d\n", ret); |
Simon Glass | fc03a55 | 2015-03-25 12:22:30 -0600 | [diff] [blame] | 603 | if (ret) { |
| 604 | if (ret != -ENOENT) |
| 605 | return ret; |
| 606 | ret = usb_find_and_bind_driver(parent, &udev->descriptor, iface, |
| 607 | udev->controller_dev->seq, |
| 608 | udev->devnum, &dev); |
| 609 | if (ret) |
| 610 | return ret; |
| 611 | created = true; |
| 612 | } |
| 613 | plat = dev_get_parent_platdata(dev); |
| 614 | debug("%s: Probing '%s', plat=%p\n", __func__, dev->name, plat); |
| 615 | plat->devnum = udev->devnum; |
Hans de Goede | 8a0b4c2 | 2015-05-05 11:54:32 +0200 | [diff] [blame] | 616 | plat->udev = udev; |
Simon Glass | fc03a55 | 2015-03-25 12:22:30 -0600 | [diff] [blame] | 617 | priv->next_addr++; |
| 618 | ret = device_probe(dev); |
| 619 | if (ret) { |
| 620 | debug("%s: Device '%s' probe failed\n", __func__, dev->name); |
| 621 | priv->next_addr--; |
| 622 | if (created) |
| 623 | device_unbind(dev); |
| 624 | return ret; |
| 625 | } |
| 626 | *devp = dev; |
Simon Glass | 9b82eeb | 2015-03-25 12:21:59 -0600 | [diff] [blame] | 627 | |
Simon Glass | fc03a55 | 2015-03-25 12:22:30 -0600 | [diff] [blame] | 628 | return 0; |
Simon Glass | 9b82eeb | 2015-03-25 12:21:59 -0600 | [diff] [blame] | 629 | } |
| 630 | |
Simon Glass | fbb1494 | 2015-05-13 07:02:23 -0600 | [diff] [blame] | 631 | /* |
| 632 | * Detect if a USB device has been plugged or unplugged. |
| 633 | */ |
| 634 | int usb_detect_change(void) |
| 635 | { |
| 636 | struct udevice *hub; |
| 637 | struct uclass *uc; |
| 638 | int change = 0; |
| 639 | int ret; |
| 640 | |
| 641 | ret = uclass_get(UCLASS_USB_HUB, &uc); |
| 642 | if (ret) |
| 643 | return ret; |
| 644 | |
| 645 | uclass_foreach_dev(hub, uc) { |
| 646 | struct usb_device *udev; |
| 647 | struct udevice *dev; |
| 648 | |
| 649 | if (!device_active(hub)) |
| 650 | continue; |
| 651 | for (device_find_first_child(hub, &dev); |
| 652 | dev; |
| 653 | device_find_next_child(&dev)) { |
| 654 | struct usb_port_status status; |
| 655 | |
| 656 | if (!device_active(dev)) |
| 657 | continue; |
| 658 | |
| 659 | udev = dev_get_parentdata(dev); |
| 660 | if (usb_get_port_status(udev, udev->portnr, &status) |
| 661 | < 0) |
| 662 | /* USB request failed */ |
| 663 | continue; |
| 664 | |
| 665 | if (le16_to_cpu(status.wPortChange) & |
| 666 | USB_PORT_STAT_C_CONNECTION) |
| 667 | change++; |
| 668 | } |
| 669 | } |
| 670 | |
| 671 | return change; |
| 672 | } |
| 673 | |
Simon Glass | 9b82eeb | 2015-03-25 12:21:59 -0600 | [diff] [blame] | 674 | int usb_child_post_bind(struct udevice *dev) |
| 675 | { |
| 676 | struct usb_dev_platdata *plat = dev_get_parent_platdata(dev); |
| 677 | const void *blob = gd->fdt_blob; |
| 678 | int val; |
| 679 | |
| 680 | if (dev->of_offset == -1) |
| 681 | return 0; |
| 682 | |
| 683 | /* We only support matching a few things */ |
| 684 | val = fdtdec_get_int(blob, dev->of_offset, "usb,device-class", -1); |
| 685 | if (val != -1) { |
| 686 | plat->id.match_flags |= USB_DEVICE_ID_MATCH_DEV_CLASS; |
| 687 | plat->id.bDeviceClass = val; |
| 688 | } |
| 689 | val = fdtdec_get_int(blob, dev->of_offset, "usb,interface-class", -1); |
| 690 | if (val != -1) { |
| 691 | plat->id.match_flags |= USB_DEVICE_ID_MATCH_INT_CLASS; |
| 692 | plat->id.bInterfaceClass = val; |
| 693 | } |
| 694 | |
| 695 | return 0; |
| 696 | } |
| 697 | |
Hans de Goede | 3b5ddd3 | 2015-05-05 11:54:31 +0200 | [diff] [blame] | 698 | struct udevice *usb_get_bus(struct udevice *dev) |
Simon Glass | 9b82eeb | 2015-03-25 12:21:59 -0600 | [diff] [blame] | 699 | { |
| 700 | struct udevice *bus; |
| 701 | |
Simon Glass | 9b82eeb | 2015-03-25 12:21:59 -0600 | [diff] [blame] | 702 | for (bus = dev; bus && device_get_uclass_id(bus) != UCLASS_USB; ) |
| 703 | bus = bus->parent; |
| 704 | if (!bus) { |
| 705 | /* By design this cannot happen */ |
| 706 | assert(bus); |
| 707 | debug("USB HUB '%s' does not have a controller\n", dev->name); |
Simon Glass | 9b82eeb | 2015-03-25 12:21:59 -0600 | [diff] [blame] | 708 | } |
Simon Glass | 9b82eeb | 2015-03-25 12:21:59 -0600 | [diff] [blame] | 709 | |
Hans de Goede | 3b5ddd3 | 2015-05-05 11:54:31 +0200 | [diff] [blame] | 710 | return bus; |
Simon Glass | 9b82eeb | 2015-03-25 12:21:59 -0600 | [diff] [blame] | 711 | } |
| 712 | |
| 713 | int usb_child_pre_probe(struct udevice *dev) |
| 714 | { |
Simon Glass | 9b82eeb | 2015-03-25 12:21:59 -0600 | [diff] [blame] | 715 | struct usb_device *udev = dev_get_parentdata(dev); |
| 716 | struct usb_dev_platdata *plat = dev_get_parent_platdata(dev); |
| 717 | int ret; |
| 718 | |
Hans de Goede | 8a0b4c2 | 2015-05-05 11:54:32 +0200 | [diff] [blame] | 719 | if (plat->udev) { |
| 720 | /* |
| 721 | * Copy over all the values set in the on stack struct |
| 722 | * usb_device in usb_scan_device() to our final struct |
| 723 | * usb_device for this dev. |
| 724 | */ |
| 725 | *udev = *(plat->udev); |
| 726 | /* And clear plat->udev as it will not be valid for long */ |
| 727 | plat->udev = NULL; |
| 728 | udev->dev = dev; |
| 729 | } else { |
| 730 | /* |
| 731 | * This happens with devices which are explicitly bound |
| 732 | * instead of being discovered through usb_scan_device() |
| 733 | * such as sandbox emul devices. |
| 734 | */ |
| 735 | udev->dev = dev; |
| 736 | udev->controller_dev = usb_get_bus(dev); |
| 737 | udev->devnum = plat->devnum; |
Simon Glass | 9b82eeb | 2015-03-25 12:21:59 -0600 | [diff] [blame] | 738 | |
Hans de Goede | 8a0b4c2 | 2015-05-05 11:54:32 +0200 | [diff] [blame] | 739 | /* |
| 740 | * udev did not go through usb_scan_device(), so we need to |
| 741 | * select the config and read the config descriptors. |
| 742 | */ |
| 743 | ret = usb_select_config(udev); |
| 744 | if (ret) |
| 745 | return ret; |
| 746 | } |
Simon Glass | 9b82eeb | 2015-03-25 12:21:59 -0600 | [diff] [blame] | 747 | |
| 748 | return 0; |
| 749 | } |
| 750 | |
| 751 | UCLASS_DRIVER(usb) = { |
| 752 | .id = UCLASS_USB, |
| 753 | .name = "usb", |
| 754 | .flags = DM_UC_FLAG_SEQ_ALIAS, |
| 755 | .post_bind = usb_post_bind, |
Hans de Goede | cebc5b7 | 2015-05-10 14:10:21 +0200 | [diff] [blame] | 756 | .priv_auto_alloc_size = sizeof(struct usb_uclass_priv), |
Simon Glass | 9b82eeb | 2015-03-25 12:21:59 -0600 | [diff] [blame] | 757 | .per_child_auto_alloc_size = sizeof(struct usb_device), |
| 758 | .per_device_auto_alloc_size = sizeof(struct usb_bus_priv), |
| 759 | .child_post_bind = usb_child_post_bind, |
| 760 | .child_pre_probe = usb_child_pre_probe, |
| 761 | .per_child_platdata_auto_alloc_size = sizeof(struct usb_dev_platdata), |
| 762 | }; |
Simon Glass | c79173e | 2015-03-25 12:22:31 -0600 | [diff] [blame] | 763 | |
| 764 | UCLASS_DRIVER(usb_dev_generic) = { |
| 765 | .id = UCLASS_USB_DEV_GENERIC, |
| 766 | .name = "usb_dev_generic", |
| 767 | }; |
| 768 | |
| 769 | U_BOOT_DRIVER(usb_dev_generic_drv) = { |
| 770 | .id = UCLASS_USB_DEV_GENERIC, |
| 771 | .name = "usb_dev_generic_drv", |
| 772 | }; |