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