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