blob: fa1af8f555a8d409f099c586c4149dd135189d33 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass9b82eeb2015-03-25 12:21:59 -06002/*
3 * (C) Copyright 2015 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
5 *
Simon Glassfc03a552015-03-25 12:22:30 -06006 * usb_match_device() modified from Linux kernel v4.0.
Simon Glass9b82eeb2015-03-25 12:21:59 -06007 */
8
Patrick Delaunay81313352021-04-27 11:02:19 +02009#define LOG_CATEGORY UCLASS_USB
10
Simon Glass62b03cc2023-09-20 07:29:49 -060011#include <bootdev.h>
Simon Glass9b82eeb2015-03-25 12:21:59 -060012#include <dm.h>
13#include <errno.h>
Simon Glass0f2af882020-05-10 11:40:05 -060014#include <log.h>
Simon Glass2dd337a2015-09-02 17:24:58 -060015#include <memalign.h>
Simon Glass9b82eeb2015-03-25 12:21:59 -060016#include <usb.h>
17#include <dm/device-internal.h>
18#include <dm/lists.h>
Simon Glass9b82eeb2015-03-25 12:21:59 -060019#include <dm/uclass-internal.h>
20
Simon Glass9b82eeb2015-03-25 12:21:59 -060021static bool asynch_allowed;
22
Hans de Goedecebc5b72015-05-10 14:10:21 +020023struct usb_uclass_priv {
24 int companion_device_count;
25};
26
Marek Vasut118a9032020-04-06 14:29:44 +020027int 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 Glass9b82eeb2015-03-25 12:21:59 -060038int usb_disable_asynch(int disable)
39{
40 int old_value = asynch_allowed;
41
42 asynch_allowed = !disable;
43 return old_value;
44}
45
46int submit_int_msg(struct usb_device *udev, unsigned long pipe, void *buffer,
Michal Suchanek1c95b9f2019-08-18 10:55:27 +020047 int length, int interval, bool nonblock)
Simon Glass9b82eeb2015-03-25 12:21:59 -060048{
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 Suchanek1c95b9f2019-08-18 10:55:27 +020055 return ops->interrupt(bus, udev, pipe, buffer, length, interval,
56 nonblock);
Simon Glass9b82eeb2015-03-25 12:21:59 -060057}
58
59int 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 Glass95588622020-12-22 19:30:28 -070064 struct usb_uclass_priv *uc_priv = uclass_get_priv(bus->uclass);
Hans de Goedecebc5b72015-05-10 14:10:21 +020065 int err;
Simon Glass9b82eeb2015-03-25 12:21:59 -060066
67 if (!ops->control)
68 return -ENOSYS;
69
Hans de Goedecebc5b72015-05-10 14:10:21 +020070 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 Glass9b82eeb2015-03-25 12:21:59 -060080}
81
82int 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 Goede0a7fa272015-05-10 14:10:18 +020094struct 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
108void *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
119int 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 Glass9b82eeb2015-03-25 12:21:59 -0600130int 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 Goedea9e41f82015-06-17 21:33:52 +0200142int 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 Meng1f31f5a2017-07-19 21:51:17 +0800153int 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 Meng6840a342017-09-07 06:13:17 -0700164int 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 Glass9b82eeb2015-03-25 12:21:59 -0600175int usb_stop(void)
176{
177 struct udevice *bus;
Bin Meng669ad842017-10-01 06:19:42 -0700178 struct udevice *rh;
Simon Glass9b82eeb2015-03-25 12:21:59 -0600179 struct uclass *uc;
Hans de Goedecebc5b72015-05-10 14:10:21 +0200180 struct usb_uclass_priv *uc_priv;
Simon Glass9b82eeb2015-03-25 12:21:59 -0600181 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 Goedecebc5b72015-05-10 14:10:21 +0200187
Simon Glass95588622020-12-22 19:30:28 -0700188 uc_priv = uclass_get_priv(uc);
Hans de Goedecebc5b72015-05-10 14:10:21 +0200189
Simon Glass9b82eeb2015-03-25 12:21:59 -0600190 uclass_foreach_dev(bus, uc) {
Stefan Roese80b5bc92017-03-20 12:51:48 +0100191 ret = device_remove(bus, DM_REMOVE_NORMAL);
Simon Glass9b82eeb2015-03-25 12:21:59 -0600192 if (ret && !err)
193 err = ret;
Bin Meng669ad842017-10-01 06:19:42 -0700194
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 Glass9b82eeb2015-03-25 12:21:59 -0600206 }
Bin Mengdadc4c02017-10-01 06:19:43 -0700207
Paul Kocialkowskid449dd32015-08-04 17:04:12 +0200208#ifdef CONFIG_USB_STORAGE
Simon Glass9b82eeb2015-03-25 12:21:59 -0600209 usb_stor_reset();
Paul Kocialkowskid449dd32015-08-04 17:04:12 +0200210#endif
Simon Glass62b03cc2023-09-20 07:29:49 -0600211 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 Goedecebc5b72015-05-10 14:10:21 +0200218 uc_priv->companion_device_count = 0;
Simon Glass9b82eeb2015-03-25 12:21:59 -0600219 usb_started = 0;
220
221 return err;
222}
223
Hans de Goedef8762f92015-05-10 14:10:19 +0200224static void usb_scan_bus(struct udevice *bus, bool recurse)
Simon Glass9b82eeb2015-03-25 12:21:59 -0600225{
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 Cortesf58affe2019-03-19 09:19:44 +0000234 printf("scanning bus %s for devices... ", bus->name);
Hans de Goedef8762f92015-05-10 14:10:19 +0200235 debug("\n");
Simon Glass9b82eeb2015-03-25 12:21:59 -0600236 ret = usb_scan_device(bus, 0, USB_SPEED_FULL, &dev);
237 if (ret)
Hans de Goedef8762f92015-05-10 14:10:19 +0200238 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 Glass9b82eeb2015-03-25 12:21:59 -0600243}
244
Simon Glass35787d22015-11-08 23:48:00 -0700245static 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 Gasnier7785aee2023-09-01 11:52:01 +0200259static 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
Jerome Forissier7530dc72025-04-18 16:09:40 +0200290static void usb_init_bus(struct udevice *bus)
291{
292 int ret;
293
294 /* init low_level USB */
295 printf("Bus %s: ", bus->name);
296
297 /*
298 * For Sandbox, we need scan the device tree each time when we
299 * start the USB stack, in order to re-create the emulated USB
300 * devices and bind drivers for them before we actually do the
301 * driver probe.
302 *
303 * For USB onboard HUB, we need to do some non-trivial init
304 * like enabling a power regulator, before enumeration.
305 */
306 if (IS_ENABLED(CONFIG_SANDBOX) ||
307 IS_ENABLED(CONFIG_USB_ONBOARD_HUB)) {
308 ret = dm_scan_fdt_dev(bus);
309 if (ret) {
310 printf("USB device scan from fdt failed (%d)", ret);
311 return;
312 }
313 }
314
315 ret = device_probe(bus);
316 if (ret == -ENODEV) { /* No such device. */
317 puts("Port not available.\n");
318 return;
319 }
320
321 if (ret) { /* Other error. */
322 printf("probe failed, error %d\n", ret);
323 return;
324 }
325
326 usb_probe_companion(bus);
327}
328
Simon Glass9b82eeb2015-03-25 12:21:59 -0600329int usb_init(void)
330{
331 int controllers_initialized = 0;
Hans de Goedecebc5b72015-05-10 14:10:21 +0200332 struct usb_uclass_priv *uc_priv;
Hans de Goede59a0dfc2015-05-10 14:10:20 +0200333 struct usb_bus_priv *priv;
Simon Glass9b82eeb2015-03-25 12:21:59 -0600334 struct udevice *bus;
335 struct uclass *uc;
Simon Glass9b82eeb2015-03-25 12:21:59 -0600336 int ret;
337
338 asynch_allowed = 1;
Simon Glass9b82eeb2015-03-25 12:21:59 -0600339
340 ret = uclass_get(UCLASS_USB, &uc);
341 if (ret)
342 return ret;
343
Simon Glass95588622020-12-22 19:30:28 -0700344 uc_priv = uclass_get_priv(uc);
Hans de Goedecebc5b72015-05-10 14:10:21 +0200345
Simon Glass9b82eeb2015-03-25 12:21:59 -0600346 uclass_foreach_dev(bus, uc) {
Jerome Forissier7530dc72025-04-18 16:09:40 +0200347 usb_init_bus(bus);
Simon Glass9b82eeb2015-03-25 12:21:59 -0600348 }
349
Jerome Forissier7530dc72025-04-18 16:09:40 +0200350 usb_started = true;
351
Hans de Goede59a0dfc2015-05-10 14:10:20 +0200352 /*
353 * lowlevel init done, now scan the bus for devices i.e. search HUBs
354 * and configure them, first scan primary controllers.
355 */
356 uclass_foreach_dev(bus, uc) {
357 if (!device_active(bus))
358 continue;
359
Jerome Forissier7530dc72025-04-18 16:09:40 +0200360 controllers_initialized++;
361
Hans de Goede59a0dfc2015-05-10 14:10:20 +0200362 priv = dev_get_uclass_priv(bus);
363 if (!priv->companion)
364 usb_scan_bus(bus, true);
365 }
366
367 /*
368 * Now that the primary controllers have been scanned and have handed
369 * over any devices they do not understand to their companions, scan
Hans de Goedecebc5b72015-05-10 14:10:21 +0200370 * the companions if necessary.
Hans de Goede59a0dfc2015-05-10 14:10:20 +0200371 */
Hans de Goedecebc5b72015-05-10 14:10:21 +0200372 if (uc_priv->companion_device_count) {
373 uclass_foreach_dev(bus, uc) {
374 if (!device_active(bus))
375 continue;
Hans de Goede59a0dfc2015-05-10 14:10:20 +0200376
Hans de Goedecebc5b72015-05-10 14:10:21 +0200377 priv = dev_get_uclass_priv(bus);
378 if (priv->companion)
379 usb_scan_bus(bus, true);
380 }
Hans de Goede59a0dfc2015-05-10 14:10:20 +0200381 }
382
Simon Glass9b82eeb2015-03-25 12:21:59 -0600383 debug("scan end\n");
Simon Glass35787d22015-11-08 23:48:00 -0700384
385 /* Remove any devices that were not found on this scan */
386 remove_inactive_children(uc, bus);
387
388 ret = uclass_get(UCLASS_USB_HUB, &uc);
389 if (ret)
390 return ret;
391 remove_inactive_children(uc, bus);
392
Simon Glass9b82eeb2015-03-25 12:21:59 -0600393 /* if we were not able to find at least one working bus, bail out */
Ismael Luceno Cortesf58affe2019-03-19 09:19:44 +0000394 if (controllers_initialized == 0)
Heinrich Schuchardtbf2d56e2024-06-18 21:07:29 +0200395 printf("No USB controllers found\n");
Simon Glass9b82eeb2015-03-25 12:21:59 -0600396
Simon Glassac746082023-07-30 11:15:12 -0600397 return usb_started ? 0 : -ENOENT;
Simon Glass9b82eeb2015-03-25 12:21:59 -0600398}
399
Simon Glass444b1e02015-03-25 12:22:32 -0600400int usb_setup_ehci_gadget(struct ehci_ctrl **ctlrp)
401{
Simon Glassb75b15b2020-12-03 16:55:23 -0700402 struct usb_plat *plat;
Simon Glass444b1e02015-03-25 12:22:32 -0600403 struct udevice *dev;
404 int ret;
405
406 /* Find the old device and remove it */
Sean Andersone7665702021-11-05 12:52:55 -0400407 ret = uclass_find_first_device(UCLASS_USB, &dev);
Simon Glass444b1e02015-03-25 12:22:32 -0600408 if (ret)
409 return ret;
Stefan Roese80b5bc92017-03-20 12:51:48 +0100410 ret = device_remove(dev, DM_REMOVE_NORMAL);
Simon Glass444b1e02015-03-25 12:22:32 -0600411 if (ret)
412 return ret;
413
Simon Glassfa20e932020-12-03 16:55:20 -0700414 plat = dev_get_plat(dev);
Simon Glass444b1e02015-03-25 12:22:32 -0600415 plat->init_type = USB_INIT_DEVICE;
416 ret = device_probe(dev);
417 if (ret)
418 return ret;
419 *ctlrp = dev_get_priv(dev);
420
421 return 0;
422}
423
Ye Li68801eb2020-06-29 10:12:59 +0800424int usb_remove_ehci_gadget(struct ehci_ctrl **ctlrp)
425{
426 struct udevice *dev;
427 int ret;
428
429 /* Find the old device and remove it */
Sean Andersone7665702021-11-05 12:52:55 -0400430 ret = uclass_find_first_device(UCLASS_USB, &dev);
Ye Li68801eb2020-06-29 10:12:59 +0800431 if (ret)
432 return ret;
433 ret = device_remove(dev, DM_REMOVE_NORMAL);
434 if (ret)
435 return ret;
436
437 *ctlrp = NULL;
438
439 return 0;
440}
441
Simon Glassfc03a552015-03-25 12:22:30 -0600442/* returns 0 if no match, 1 if match */
Masahiro Yamada6d8e4332017-06-22 16:35:14 +0900443static int usb_match_device(const struct usb_device_descriptor *desc,
444 const struct usb_device_id *id)
Simon Glassfc03a552015-03-25 12:22:30 -0600445{
446 if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
Stefan Roese77c83ac2020-07-21 10:46:04 +0200447 id->idVendor != desc->idVendor)
Simon Glassfc03a552015-03-25 12:22:30 -0600448 return 0;
449
450 if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
Stefan Roese77c83ac2020-07-21 10:46:04 +0200451 id->idProduct != desc->idProduct)
Simon Glassfc03a552015-03-25 12:22:30 -0600452 return 0;
453
454 /* No need to test id->bcdDevice_lo != 0, since 0 is never
455 greater than any unsigned number. */
456 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) &&
Stefan Roese77c83ac2020-07-21 10:46:04 +0200457 (id->bcdDevice_lo > desc->bcdDevice))
Simon Glassfc03a552015-03-25 12:22:30 -0600458 return 0;
459
460 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) &&
Stefan Roese77c83ac2020-07-21 10:46:04 +0200461 (id->bcdDevice_hi < desc->bcdDevice))
Simon Glassfc03a552015-03-25 12:22:30 -0600462 return 0;
463
464 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) &&
465 (id->bDeviceClass != desc->bDeviceClass))
466 return 0;
467
468 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) &&
469 (id->bDeviceSubClass != desc->bDeviceSubClass))
470 return 0;
471
472 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) &&
473 (id->bDeviceProtocol != desc->bDeviceProtocol))
474 return 0;
475
476 return 1;
477}
478
479/* returns 0 if no match, 1 if match */
Masahiro Yamada6d8e4332017-06-22 16:35:14 +0900480static int usb_match_one_id_intf(const struct usb_device_descriptor *desc,
481 const struct usb_interface_descriptor *int_desc,
482 const struct usb_device_id *id)
Simon Glassfc03a552015-03-25 12:22:30 -0600483{
484 /* The interface class, subclass, protocol and number should never be
485 * checked for a match if the device class is Vendor Specific,
486 * unless the match record specifies the Vendor ID. */
487 if (desc->bDeviceClass == USB_CLASS_VENDOR_SPEC &&
488 !(id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
489 (id->match_flags & (USB_DEVICE_ID_MATCH_INT_CLASS |
490 USB_DEVICE_ID_MATCH_INT_SUBCLASS |
491 USB_DEVICE_ID_MATCH_INT_PROTOCOL |
492 USB_DEVICE_ID_MATCH_INT_NUMBER)))
493 return 0;
494
495 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) &&
496 (id->bInterfaceClass != int_desc->bInterfaceClass))
497 return 0;
498
499 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) &&
500 (id->bInterfaceSubClass != int_desc->bInterfaceSubClass))
501 return 0;
502
503 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) &&
504 (id->bInterfaceProtocol != int_desc->bInterfaceProtocol))
505 return 0;
506
507 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_NUMBER) &&
508 (id->bInterfaceNumber != int_desc->bInterfaceNumber))
509 return 0;
510
511 return 1;
512}
513
514/* returns 0 if no match, 1 if match */
Masahiro Yamada6d8e4332017-06-22 16:35:14 +0900515static int usb_match_one_id(struct usb_device_descriptor *desc,
516 struct usb_interface_descriptor *int_desc,
517 const struct usb_device_id *id)
Simon Glassfc03a552015-03-25 12:22:30 -0600518{
519 if (!usb_match_device(desc, id))
520 return 0;
521
522 return usb_match_one_id_intf(desc, int_desc, id);
523}
524
Michael Walle7c961322020-06-02 01:47:07 +0200525static ofnode usb_get_ofnode(struct udevice *hub, int port)
526{
527 ofnode node;
528 u32 reg;
529
Simon Glass07c17772020-12-19 10:40:12 -0700530 if (!dev_has_ofnode(hub))
Michael Walle7c961322020-06-02 01:47:07 +0200531 return ofnode_null();
532
533 /*
534 * The USB controller and its USB hub are two different udevices,
535 * but the device tree has only one node for both. Thus we are
536 * assigning this node to both udevices.
537 * If port is zero, the controller scans its root hub, thus we
538 * are using the same ofnode as the controller here.
539 */
540 if (!port)
541 return dev_ofnode(hub);
542
543 ofnode_for_each_subnode(node, dev_ofnode(hub)) {
544 if (ofnode_read_u32(node, "reg", &reg))
545 continue;
546
547 if (reg == port)
548 return node;
549 }
550
551 return ofnode_null();
552}
553
Simon Glassfc03a552015-03-25 12:22:30 -0600554/**
555 * usb_find_and_bind_driver() - Find and bind the right USB driver
556 *
557 * This only looks at certain fields in the descriptor.
558 */
559static int usb_find_and_bind_driver(struct udevice *parent,
560 struct usb_device_descriptor *desc,
561 struct usb_interface_descriptor *iface,
Michael Walle7c961322020-06-02 01:47:07 +0200562 int bus_seq, int devnum, int port,
Simon Glassfc03a552015-03-25 12:22:30 -0600563 struct udevice **devp)
564{
565 struct usb_driver_entry *start, *entry;
566 int n_ents;
567 int ret;
Marek Vasut6e76b0f2022-11-26 13:57:53 +0100568 char name[34], *str;
Michael Walle7c961322020-06-02 01:47:07 +0200569 ofnode node = usb_get_ofnode(parent, port);
Simon Glassfc03a552015-03-25 12:22:30 -0600570
571 *devp = NULL;
572 debug("%s: Searching for driver\n", __func__);
573 start = ll_entry_start(struct usb_driver_entry, usb_driver_entry);
574 n_ents = ll_entry_count(struct usb_driver_entry, usb_driver_entry);
575 for (entry = start; entry != start + n_ents; entry++) {
576 const struct usb_device_id *id;
577 struct udevice *dev;
578 const struct driver *drv;
Simon Glassb75b15b2020-12-03 16:55:23 -0700579 struct usb_dev_plat *plat;
Simon Glassfc03a552015-03-25 12:22:30 -0600580
581 for (id = entry->match; id->match_flags; id++) {
582 if (!usb_match_one_id(desc, iface, id))
583 continue;
584
585 drv = entry->driver;
586 /*
587 * We could pass the descriptor to the driver as
Simon Glass71fa5b42020-12-03 16:55:18 -0700588 * plat (instead of NULL) and allow its bind()
Simon Glassfc03a552015-03-25 12:22:30 -0600589 * method to return -ENOENT if it doesn't support this
590 * device. That way we could continue the search to
591 * find another driver. For now this doesn't seem
592 * necesssary, so just bind the first match.
593 */
Simon Glass884870f2020-11-28 17:50:01 -0700594 ret = device_bind(parent, drv, drv->name, NULL, node,
595 &dev);
Simon Glassfc03a552015-03-25 12:22:30 -0600596 if (ret)
597 goto error;
598 debug("%s: Match found: %s\n", __func__, drv->name);
599 dev->driver_data = id->driver_info;
Simon Glass71fa5b42020-12-03 16:55:18 -0700600 plat = dev_get_parent_plat(dev);
Simon Glassfc03a552015-03-25 12:22:30 -0600601 plat->id = *id;
602 *devp = dev;
603 return 0;
604 }
605 }
606
Simon Glassc79173e2015-03-25 12:22:31 -0600607 /* Bind a generic driver so that the device can be used */
608 snprintf(name, sizeof(name), "generic_bus_%x_dev_%x", bus_seq, devnum);
609 str = strdup(name);
610 if (!str)
611 return -ENOMEM;
612 ret = device_bind_driver(parent, "usb_dev_generic_drv", str, devp);
Simon Glass4d2c3592023-01-17 10:47:35 -0700613 if (!ret)
614 device_set_name_alloced(*devp);
Simon Glassc79173e2015-03-25 12:22:31 -0600615
Simon Glassfc03a552015-03-25 12:22:30 -0600616error:
617 debug("%s: No match found: %d\n", __func__, ret);
618 return ret;
619}
620
621/**
Simon Glassd43d7e92015-11-08 23:47:56 -0700622 * usb_find_child() - Find an existing device which matches our needs
623 *
624 *
Simon Glassfc03a552015-03-25 12:22:30 -0600625 */
Simon Glassd43d7e92015-11-08 23:47:56 -0700626static int usb_find_child(struct udevice *parent,
627 struct usb_device_descriptor *desc,
628 struct usb_interface_descriptor *iface,
629 struct udevice **devp)
Simon Glassfc03a552015-03-25 12:22:30 -0600630{
631 struct udevice *dev;
632
633 *devp = NULL;
634 for (device_find_first_child(parent, &dev);
635 dev;
636 device_find_next_child(&dev)) {
Simon Glassb75b15b2020-12-03 16:55:23 -0700637 struct usb_dev_plat *plat = dev_get_parent_plat(dev);
Simon Glassfc03a552015-03-25 12:22:30 -0600638
639 /* If this device is already in use, skip it */
640 if (device_active(dev))
641 continue;
642 debug(" %s: name='%s', plat=%d, desc=%d\n", __func__,
643 dev->name, plat->id.bDeviceClass, desc->bDeviceClass);
644 if (usb_match_one_id(desc, iface, &plat->id)) {
645 *devp = dev;
646 return 0;
647 }
648 }
Simon Glassd43d7e92015-11-08 23:47:56 -0700649
Simon Glassfc03a552015-03-25 12:22:30 -0600650 return -ENOENT;
651}
652
Simon Glass9b82eeb2015-03-25 12:21:59 -0600653int usb_scan_device(struct udevice *parent, int port,
654 enum usb_device_speed speed, struct udevice **devp)
655{
656 struct udevice *dev;
657 bool created = false;
Simon Glassb75b15b2020-12-03 16:55:23 -0700658 struct usb_dev_plat *plat;
Simon Glass9b82eeb2015-03-25 12:21:59 -0600659 struct usb_bus_priv *priv;
660 struct usb_device *parent_udev;
661 int ret;
662 ALLOC_CACHE_ALIGN_BUFFER(struct usb_device, udev, 1);
663 struct usb_interface_descriptor *iface = &udev->config.if_desc[0].desc;
664
665 *devp = NULL;
666 memset(udev, '\0', sizeof(*udev));
Hans de Goede3b5ddd32015-05-05 11:54:31 +0200667 udev->controller_dev = usb_get_bus(parent);
Simon Glass9b82eeb2015-03-25 12:21:59 -0600668 priv = dev_get_uclass_priv(udev->controller_dev);
669
670 /*
671 * Somewhat nasty, this. We create a local device and use the normal
672 * USB stack to read its descriptor. Then we know what type of device
673 * to create for real.
674 *
675 * udev->dev is set to the parent, since we don't have a real device
676 * yet. The USB stack should not access udev.dev anyway, except perhaps
677 * to find the controller, and the controller will either be @parent,
678 * or some parent of @parent.
679 *
680 * Another option might be to create the device as a generic USB
681 * device, then morph it into the correct one when we know what it
682 * should be. This means that a generic USB device would morph into
683 * a network controller, or a USB flash stick, for example. However,
684 * we don't support such morphing and it isn't clear that it would
685 * be easy to do.
686 *
687 * Yet another option is to split out the USB stack parts of udev
688 * into something like a 'struct urb' (as Linux does) which can exist
689 * independently of any device. This feels cleaner, but calls for quite
690 * a big change to the USB stack.
691 *
692 * For now, the approach is to set up an empty udev, read its
693 * descriptor and assign it an address, then bind a real device and
694 * stash the resulting information into the device's parent
695 * platform data. Then when we probe it, usb_child_pre_probe() is called
696 * and it will pull the information out of the stash.
697 */
698 udev->dev = parent;
699 udev->speed = speed;
700 udev->devnum = priv->next_addr + 1;
701 udev->portnr = port;
702 debug("Calling usb_setup_device(), portnr=%d\n", udev->portnr);
703 parent_udev = device_get_uclass_id(parent) == UCLASS_USB_HUB ?
Simon Glassde44acf2015-09-28 23:32:01 -0600704 dev_get_parent_priv(parent) : NULL;
Hans de Goede778dc1c2015-06-17 21:33:46 +0200705 ret = usb_setup_device(udev, priv->desc_before_addr, parent_udev);
Simon Glass9b82eeb2015-03-25 12:21:59 -0600706 debug("read_descriptor for '%s': ret=%d\n", parent->name, ret);
707 if (ret)
708 return ret;
Simon Glassd43d7e92015-11-08 23:47:56 -0700709 ret = usb_find_child(parent, &udev->descriptor, iface, &dev);
710 debug("** usb_find_child returns %d\n", ret);
Simon Glassfc03a552015-03-25 12:22:30 -0600711 if (ret) {
712 if (ret != -ENOENT)
713 return ret;
Michael Walle7c961322020-06-02 01:47:07 +0200714 ret = usb_find_and_bind_driver(parent, &udev->descriptor,
715 iface,
Simon Glass75e534b2020-12-16 21:20:07 -0700716 dev_seq(udev->controller_dev),
Michael Walle7c961322020-06-02 01:47:07 +0200717 udev->devnum, port, &dev);
Simon Glassfc03a552015-03-25 12:22:30 -0600718 if (ret)
719 return ret;
720 created = true;
721 }
Simon Glass71fa5b42020-12-03 16:55:18 -0700722 plat = dev_get_parent_plat(dev);
Simon Glassfc03a552015-03-25 12:22:30 -0600723 debug("%s: Probing '%s', plat=%p\n", __func__, dev->name, plat);
724 plat->devnum = udev->devnum;
Hans de Goede8a0b4c22015-05-05 11:54:32 +0200725 plat->udev = udev;
Simon Glassfc03a552015-03-25 12:22:30 -0600726 priv->next_addr++;
727 ret = device_probe(dev);
728 if (ret) {
729 debug("%s: Device '%s' probe failed\n", __func__, dev->name);
730 priv->next_addr--;
731 if (created)
732 device_unbind(dev);
733 return ret;
734 }
735 *devp = dev;
Simon Glass9b82eeb2015-03-25 12:21:59 -0600736
Simon Glassfc03a552015-03-25 12:22:30 -0600737 return 0;
Simon Glass9b82eeb2015-03-25 12:21:59 -0600738}
739
Simon Glassfbb14942015-05-13 07:02:23 -0600740/*
741 * Detect if a USB device has been plugged or unplugged.
742 */
743int usb_detect_change(void)
744{
745 struct udevice *hub;
746 struct uclass *uc;
747 int change = 0;
748 int ret;
749
750 ret = uclass_get(UCLASS_USB_HUB, &uc);
751 if (ret)
752 return ret;
753
754 uclass_foreach_dev(hub, uc) {
755 struct usb_device *udev;
756 struct udevice *dev;
757
758 if (!device_active(hub))
759 continue;
760 for (device_find_first_child(hub, &dev);
761 dev;
762 device_find_next_child(&dev)) {
763 struct usb_port_status status;
764
765 if (!device_active(dev))
766 continue;
767
Simon Glassde44acf2015-09-28 23:32:01 -0600768 udev = dev_get_parent_priv(dev);
Simon Glassfbb14942015-05-13 07:02:23 -0600769 if (usb_get_port_status(udev, udev->portnr, &status)
770 < 0)
771 /* USB request failed */
772 continue;
773
774 if (le16_to_cpu(status.wPortChange) &
775 USB_PORT_STAT_C_CONNECTION)
776 change++;
777 }
778 }
779
780 return change;
781}
782
Masahiro Yamada6d8e4332017-06-22 16:35:14 +0900783static int usb_child_post_bind(struct udevice *dev)
Simon Glass9b82eeb2015-03-25 12:21:59 -0600784{
Simon Glassb75b15b2020-12-03 16:55:23 -0700785 struct usb_dev_plat *plat = dev_get_parent_plat(dev);
Simon Glass9b82eeb2015-03-25 12:21:59 -0600786 int val;
787
Simon Glassf1d50f72020-12-19 10:40:13 -0700788 if (!dev_has_ofnode(dev))
Simon Glass9b82eeb2015-03-25 12:21:59 -0600789 return 0;
790
791 /* We only support matching a few things */
Simon Glass6deb8e22017-05-18 20:09:38 -0600792 val = dev_read_u32_default(dev, "usb,device-class", -1);
Simon Glass9b82eeb2015-03-25 12:21:59 -0600793 if (val != -1) {
794 plat->id.match_flags |= USB_DEVICE_ID_MATCH_DEV_CLASS;
795 plat->id.bDeviceClass = val;
796 }
Simon Glass6deb8e22017-05-18 20:09:38 -0600797 val = dev_read_u32_default(dev, "usb,interface-class", -1);
Simon Glass9b82eeb2015-03-25 12:21:59 -0600798 if (val != -1) {
799 plat->id.match_flags |= USB_DEVICE_ID_MATCH_INT_CLASS;
800 plat->id.bInterfaceClass = val;
801 }
802
803 return 0;
804}
805
Hans de Goede3b5ddd32015-05-05 11:54:31 +0200806struct udevice *usb_get_bus(struct udevice *dev)
Simon Glass9b82eeb2015-03-25 12:21:59 -0600807{
808 struct udevice *bus;
809
Simon Glass9b82eeb2015-03-25 12:21:59 -0600810 for (bus = dev; bus && device_get_uclass_id(bus) != UCLASS_USB; )
811 bus = bus->parent;
812 if (!bus) {
813 /* By design this cannot happen */
814 assert(bus);
815 debug("USB HUB '%s' does not have a controller\n", dev->name);
Simon Glass9b82eeb2015-03-25 12:21:59 -0600816 }
Simon Glass9b82eeb2015-03-25 12:21:59 -0600817
Hans de Goede3b5ddd32015-05-05 11:54:31 +0200818 return bus;
Simon Glass9b82eeb2015-03-25 12:21:59 -0600819}
820
821int usb_child_pre_probe(struct udevice *dev)
822{
Simon Glassde44acf2015-09-28 23:32:01 -0600823 struct usb_device *udev = dev_get_parent_priv(dev);
Simon Glassb75b15b2020-12-03 16:55:23 -0700824 struct usb_dev_plat *plat = dev_get_parent_plat(dev);
Simon Glass9b82eeb2015-03-25 12:21:59 -0600825 int ret;
826
Hans de Goede8a0b4c22015-05-05 11:54:32 +0200827 if (plat->udev) {
828 /*
829 * Copy over all the values set in the on stack struct
830 * usb_device in usb_scan_device() to our final struct
831 * usb_device for this dev.
832 */
833 *udev = *(plat->udev);
834 /* And clear plat->udev as it will not be valid for long */
835 plat->udev = NULL;
836 udev->dev = dev;
837 } else {
838 /*
839 * This happens with devices which are explicitly bound
840 * instead of being discovered through usb_scan_device()
841 * such as sandbox emul devices.
842 */
843 udev->dev = dev;
844 udev->controller_dev = usb_get_bus(dev);
845 udev->devnum = plat->devnum;
Simon Glass9b82eeb2015-03-25 12:21:59 -0600846
Hans de Goede8a0b4c22015-05-05 11:54:32 +0200847 /*
848 * udev did not go through usb_scan_device(), so we need to
849 * select the config and read the config descriptors.
850 */
851 ret = usb_select_config(udev);
852 if (ret)
853 return ret;
854 }
Simon Glass9b82eeb2015-03-25 12:21:59 -0600855
856 return 0;
857}
858
859UCLASS_DRIVER(usb) = {
860 .id = UCLASS_USB,
861 .name = "usb",
862 .flags = DM_UC_FLAG_SEQ_ALIAS,
Simon Glass18230342016-07-05 17:10:10 -0600863 .post_bind = dm_scan_fdt_dev,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700864 .priv_auto = sizeof(struct usb_uclass_priv),
865 .per_child_auto = sizeof(struct usb_device),
866 .per_device_auto = sizeof(struct usb_bus_priv),
Simon Glass9b82eeb2015-03-25 12:21:59 -0600867 .child_post_bind = usb_child_post_bind,
868 .child_pre_probe = usb_child_pre_probe,
Simon Glassb75b15b2020-12-03 16:55:23 -0700869 .per_child_plat_auto = sizeof(struct usb_dev_plat),
Simon Glass9b82eeb2015-03-25 12:21:59 -0600870};
Simon Glassc79173e2015-03-25 12:22:31 -0600871
872UCLASS_DRIVER(usb_dev_generic) = {
873 .id = UCLASS_USB_DEV_GENERIC,
874 .name = "usb_dev_generic",
875};
876
877U_BOOT_DRIVER(usb_dev_generic_drv) = {
878 .id = UCLASS_USB_DEV_GENERIC,
879 .name = "usb_dev_generic_drv",
880};