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