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