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