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