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