blob: e5fe949f254c381fc1f1cd3cc5d1db43f7cff411 [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 Glass9b82eeb2015-03-25 12:21:59 -060011#include <common.h>
12#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
Hans de Goedecebc5b72015-05-10 14:10:21 +0200211 uc_priv->companion_device_count = 0;
Simon Glass9b82eeb2015-03-25 12:21:59 -0600212 usb_started = 0;
213
214 return err;
215}
216
Hans de Goedef8762f92015-05-10 14:10:19 +0200217static void usb_scan_bus(struct udevice *bus, bool recurse)
Simon Glass9b82eeb2015-03-25 12:21:59 -0600218{
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 Cortesf58affe2019-03-19 09:19:44 +0000227 printf("scanning bus %s for devices... ", bus->name);
Hans de Goedef8762f92015-05-10 14:10:19 +0200228 debug("\n");
Simon Glass9b82eeb2015-03-25 12:21:59 -0600229 ret = usb_scan_device(bus, 0, USB_SPEED_FULL, &dev);
230 if (ret)
Hans de Goedef8762f92015-05-10 14:10:19 +0200231 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 Glass9b82eeb2015-03-25 12:21:59 -0600236}
237
Simon Glass35787d22015-11-08 23:48:00 -0700238static 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
Fabrice Gasnier7785aee2023-09-01 11:52:01 +0200252static int usb_probe_companion(struct udevice *bus)
253{
254 struct udevice *companion_dev;
255 int ret;
256
257 /*
258 * Enforce optional companion controller is marked as such in order to
259 * 1st scan the primary controller, before the companion controller
260 * (ownership is given to companion when low or full speed devices
261 * have been detected).
262 */
263 ret = uclass_get_device_by_phandle(UCLASS_USB, bus, "companion", &companion_dev);
264 if (!ret) {
265 struct usb_bus_priv *companion_bus_priv;
266
267 debug("%s is the companion of %s\n", companion_dev->name, bus->name);
268 companion_bus_priv = dev_get_uclass_priv(companion_dev);
269 companion_bus_priv->companion = true;
270 } else if (ret && ret != -ENOENT && ret != -ENODEV) {
271 /*
272 * Treat everything else than no companion or disabled
273 * companion as an error. (It may not be enabled on boards
274 * that have a High-Speed HUB to handle FS and LS traffic).
275 */
276 printf("Failed to get companion (ret=%d)\n", ret);
277 return ret;
278 }
279
280 return 0;
281}
282
Simon Glass9b82eeb2015-03-25 12:21:59 -0600283int usb_init(void)
284{
285 int controllers_initialized = 0;
Hans de Goedecebc5b72015-05-10 14:10:21 +0200286 struct usb_uclass_priv *uc_priv;
Hans de Goede59a0dfc2015-05-10 14:10:20 +0200287 struct usb_bus_priv *priv;
Simon Glass9b82eeb2015-03-25 12:21:59 -0600288 struct udevice *bus;
289 struct uclass *uc;
Simon Glass9b82eeb2015-03-25 12:21:59 -0600290 int ret;
291
292 asynch_allowed = 1;
Simon Glass9b82eeb2015-03-25 12:21:59 -0600293
294 ret = uclass_get(UCLASS_USB, &uc);
295 if (ret)
296 return ret;
297
Simon Glass95588622020-12-22 19:30:28 -0700298 uc_priv = uclass_get_priv(uc);
Hans de Goedecebc5b72015-05-10 14:10:21 +0200299
Simon Glass9b82eeb2015-03-25 12:21:59 -0600300 uclass_foreach_dev(bus, uc) {
301 /* init low_level USB */
Ismael Luceno Cortesf58affe2019-03-19 09:19:44 +0000302 printf("Bus %s: ", bus->name);
Bin Meng669ad842017-10-01 06:19:42 -0700303
Bin Meng669ad842017-10-01 06:19:42 -0700304 /*
305 * For Sandbox, we need scan the device tree each time when we
306 * start the USB stack, in order to re-create the emulated USB
307 * devices and bind drivers for them before we actually do the
308 * driver probe.
Fabrice Gasniercd809b12022-12-12 11:44:35 +0100309 *
310 * For USB onboard HUB, we need to do some non-trivial init
311 * like enabling a power regulator, before enumeration.
Bin Meng669ad842017-10-01 06:19:42 -0700312 */
Fabrice Gasniercd809b12022-12-12 11:44:35 +0100313 if (IS_ENABLED(CONFIG_SANDBOX) ||
314 IS_ENABLED(CONFIG_USB_ONBOARD_HUB)) {
315 ret = dm_scan_fdt_dev(bus);
316 if (ret) {
317 printf("USB device scan from fdt failed (%d)", ret);
318 continue;
319 }
Bin Meng669ad842017-10-01 06:19:42 -0700320 }
Bin Meng669ad842017-10-01 06:19:42 -0700321
Simon Glass9b82eeb2015-03-25 12:21:59 -0600322 ret = device_probe(bus);
323 if (ret == -ENODEV) { /* No such device. */
324 puts("Port not available.\n");
325 controllers_initialized++;
326 continue;
327 }
328
329 if (ret) { /* Other error. */
330 printf("probe failed, error %d\n", ret);
331 continue;
332 }
Fabrice Gasnier7785aee2023-09-01 11:52:01 +0200333
334 ret = usb_probe_companion(bus);
335 if (ret)
336 continue;
337
Simon Glass9b82eeb2015-03-25 12:21:59 -0600338 controllers_initialized++;
Simon Glass9b82eeb2015-03-25 12:21:59 -0600339 usb_started = true;
340 }
341
Hans de Goede59a0dfc2015-05-10 14:10:20 +0200342 /*
343 * lowlevel init done, now scan the bus for devices i.e. search HUBs
344 * and configure them, first scan primary controllers.
345 */
346 uclass_foreach_dev(bus, uc) {
347 if (!device_active(bus))
348 continue;
349
350 priv = dev_get_uclass_priv(bus);
351 if (!priv->companion)
352 usb_scan_bus(bus, true);
353 }
354
355 /*
356 * Now that the primary controllers have been scanned and have handed
357 * over any devices they do not understand to their companions, scan
Hans de Goedecebc5b72015-05-10 14:10:21 +0200358 * the companions if necessary.
Hans de Goede59a0dfc2015-05-10 14:10:20 +0200359 */
Hans de Goedecebc5b72015-05-10 14:10:21 +0200360 if (uc_priv->companion_device_count) {
361 uclass_foreach_dev(bus, uc) {
362 if (!device_active(bus))
363 continue;
Hans de Goede59a0dfc2015-05-10 14:10:20 +0200364
Hans de Goedecebc5b72015-05-10 14:10:21 +0200365 priv = dev_get_uclass_priv(bus);
366 if (priv->companion)
367 usb_scan_bus(bus, true);
368 }
Hans de Goede59a0dfc2015-05-10 14:10:20 +0200369 }
370
Simon Glass9b82eeb2015-03-25 12:21:59 -0600371 debug("scan end\n");
Simon Glass35787d22015-11-08 23:48:00 -0700372
373 /* Remove any devices that were not found on this scan */
374 remove_inactive_children(uc, bus);
375
376 ret = uclass_get(UCLASS_USB_HUB, &uc);
377 if (ret)
378 return ret;
379 remove_inactive_children(uc, bus);
380
Simon Glass9b82eeb2015-03-25 12:21:59 -0600381 /* if we were not able to find at least one working bus, bail out */
Ismael Luceno Cortesf58affe2019-03-19 09:19:44 +0000382 if (controllers_initialized == 0)
383 printf("No working controllers found\n");
Simon Glass9b82eeb2015-03-25 12:21:59 -0600384
Simon Glassac746082023-07-30 11:15:12 -0600385 return usb_started ? 0 : -ENOENT;
Simon Glass9b82eeb2015-03-25 12:21:59 -0600386}
387
Simon Glass444b1e02015-03-25 12:22:32 -0600388int usb_setup_ehci_gadget(struct ehci_ctrl **ctlrp)
389{
Simon Glassb75b15b2020-12-03 16:55:23 -0700390 struct usb_plat *plat;
Simon Glass444b1e02015-03-25 12:22:32 -0600391 struct udevice *dev;
392 int ret;
393
394 /* Find the old device and remove it */
Sean Andersone7665702021-11-05 12:52:55 -0400395 ret = uclass_find_first_device(UCLASS_USB, &dev);
Simon Glass444b1e02015-03-25 12:22:32 -0600396 if (ret)
397 return ret;
Stefan Roese80b5bc92017-03-20 12:51:48 +0100398 ret = device_remove(dev, DM_REMOVE_NORMAL);
Simon Glass444b1e02015-03-25 12:22:32 -0600399 if (ret)
400 return ret;
401
Simon Glassfa20e932020-12-03 16:55:20 -0700402 plat = dev_get_plat(dev);
Simon Glass444b1e02015-03-25 12:22:32 -0600403 plat->init_type = USB_INIT_DEVICE;
404 ret = device_probe(dev);
405 if (ret)
406 return ret;
407 *ctlrp = dev_get_priv(dev);
408
409 return 0;
410}
411
Ye Li68801eb2020-06-29 10:12:59 +0800412int usb_remove_ehci_gadget(struct ehci_ctrl **ctlrp)
413{
414 struct udevice *dev;
415 int ret;
416
417 /* Find the old device and remove it */
Sean Andersone7665702021-11-05 12:52:55 -0400418 ret = uclass_find_first_device(UCLASS_USB, &dev);
Ye Li68801eb2020-06-29 10:12:59 +0800419 if (ret)
420 return ret;
421 ret = device_remove(dev, DM_REMOVE_NORMAL);
422 if (ret)
423 return ret;
424
425 *ctlrp = NULL;
426
427 return 0;
428}
429
Simon Glassfc03a552015-03-25 12:22:30 -0600430/* returns 0 if no match, 1 if match */
Masahiro Yamada6d8e4332017-06-22 16:35:14 +0900431static int usb_match_device(const struct usb_device_descriptor *desc,
432 const struct usb_device_id *id)
Simon Glassfc03a552015-03-25 12:22:30 -0600433{
434 if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
Stefan Roese77c83ac2020-07-21 10:46:04 +0200435 id->idVendor != desc->idVendor)
Simon Glassfc03a552015-03-25 12:22:30 -0600436 return 0;
437
438 if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
Stefan Roese77c83ac2020-07-21 10:46:04 +0200439 id->idProduct != desc->idProduct)
Simon Glassfc03a552015-03-25 12:22:30 -0600440 return 0;
441
442 /* No need to test id->bcdDevice_lo != 0, since 0 is never
443 greater than any unsigned number. */
444 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) &&
Stefan Roese77c83ac2020-07-21 10:46:04 +0200445 (id->bcdDevice_lo > desc->bcdDevice))
Simon Glassfc03a552015-03-25 12:22:30 -0600446 return 0;
447
448 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) &&
Stefan Roese77c83ac2020-07-21 10:46:04 +0200449 (id->bcdDevice_hi < desc->bcdDevice))
Simon Glassfc03a552015-03-25 12:22:30 -0600450 return 0;
451
452 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) &&
453 (id->bDeviceClass != desc->bDeviceClass))
454 return 0;
455
456 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) &&
457 (id->bDeviceSubClass != desc->bDeviceSubClass))
458 return 0;
459
460 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) &&
461 (id->bDeviceProtocol != desc->bDeviceProtocol))
462 return 0;
463
464 return 1;
465}
466
467/* returns 0 if no match, 1 if match */
Masahiro Yamada6d8e4332017-06-22 16:35:14 +0900468static int usb_match_one_id_intf(const struct usb_device_descriptor *desc,
469 const struct usb_interface_descriptor *int_desc,
470 const struct usb_device_id *id)
Simon Glassfc03a552015-03-25 12:22:30 -0600471{
472 /* The interface class, subclass, protocol and number should never be
473 * checked for a match if the device class is Vendor Specific,
474 * unless the match record specifies the Vendor ID. */
475 if (desc->bDeviceClass == USB_CLASS_VENDOR_SPEC &&
476 !(id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
477 (id->match_flags & (USB_DEVICE_ID_MATCH_INT_CLASS |
478 USB_DEVICE_ID_MATCH_INT_SUBCLASS |
479 USB_DEVICE_ID_MATCH_INT_PROTOCOL |
480 USB_DEVICE_ID_MATCH_INT_NUMBER)))
481 return 0;
482
483 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) &&
484 (id->bInterfaceClass != int_desc->bInterfaceClass))
485 return 0;
486
487 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) &&
488 (id->bInterfaceSubClass != int_desc->bInterfaceSubClass))
489 return 0;
490
491 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) &&
492 (id->bInterfaceProtocol != int_desc->bInterfaceProtocol))
493 return 0;
494
495 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_NUMBER) &&
496 (id->bInterfaceNumber != int_desc->bInterfaceNumber))
497 return 0;
498
499 return 1;
500}
501
502/* returns 0 if no match, 1 if match */
Masahiro Yamada6d8e4332017-06-22 16:35:14 +0900503static int usb_match_one_id(struct usb_device_descriptor *desc,
504 struct usb_interface_descriptor *int_desc,
505 const struct usb_device_id *id)
Simon Glassfc03a552015-03-25 12:22:30 -0600506{
507 if (!usb_match_device(desc, id))
508 return 0;
509
510 return usb_match_one_id_intf(desc, int_desc, id);
511}
512
Michael Walle7c961322020-06-02 01:47:07 +0200513static ofnode usb_get_ofnode(struct udevice *hub, int port)
514{
515 ofnode node;
516 u32 reg;
517
Simon Glass07c17772020-12-19 10:40:12 -0700518 if (!dev_has_ofnode(hub))
Michael Walle7c961322020-06-02 01:47:07 +0200519 return ofnode_null();
520
521 /*
522 * The USB controller and its USB hub are two different udevices,
523 * but the device tree has only one node for both. Thus we are
524 * assigning this node to both udevices.
525 * If port is zero, the controller scans its root hub, thus we
526 * are using the same ofnode as the controller here.
527 */
528 if (!port)
529 return dev_ofnode(hub);
530
531 ofnode_for_each_subnode(node, dev_ofnode(hub)) {
532 if (ofnode_read_u32(node, "reg", &reg))
533 continue;
534
535 if (reg == port)
536 return node;
537 }
538
539 return ofnode_null();
540}
541
Simon Glassfc03a552015-03-25 12:22:30 -0600542/**
543 * usb_find_and_bind_driver() - Find and bind the right USB driver
544 *
545 * This only looks at certain fields in the descriptor.
546 */
547static int usb_find_and_bind_driver(struct udevice *parent,
548 struct usb_device_descriptor *desc,
549 struct usb_interface_descriptor *iface,
Michael Walle7c961322020-06-02 01:47:07 +0200550 int bus_seq, int devnum, int port,
Simon Glassfc03a552015-03-25 12:22:30 -0600551 struct udevice **devp)
552{
553 struct usb_driver_entry *start, *entry;
554 int n_ents;
555 int ret;
Marek Vasut6e76b0f2022-11-26 13:57:53 +0100556 char name[34], *str;
Michael Walle7c961322020-06-02 01:47:07 +0200557 ofnode node = usb_get_ofnode(parent, port);
Simon Glassfc03a552015-03-25 12:22:30 -0600558
559 *devp = NULL;
560 debug("%s: Searching for driver\n", __func__);
561 start = ll_entry_start(struct usb_driver_entry, usb_driver_entry);
562 n_ents = ll_entry_count(struct usb_driver_entry, usb_driver_entry);
563 for (entry = start; entry != start + n_ents; entry++) {
564 const struct usb_device_id *id;
565 struct udevice *dev;
566 const struct driver *drv;
Simon Glassb75b15b2020-12-03 16:55:23 -0700567 struct usb_dev_plat *plat;
Simon Glassfc03a552015-03-25 12:22:30 -0600568
569 for (id = entry->match; id->match_flags; id++) {
570 if (!usb_match_one_id(desc, iface, id))
571 continue;
572
573 drv = entry->driver;
574 /*
575 * We could pass the descriptor to the driver as
Simon Glass71fa5b42020-12-03 16:55:18 -0700576 * plat (instead of NULL) and allow its bind()
Simon Glassfc03a552015-03-25 12:22:30 -0600577 * method to return -ENOENT if it doesn't support this
578 * device. That way we could continue the search to
579 * find another driver. For now this doesn't seem
580 * necesssary, so just bind the first match.
581 */
Simon Glass884870f2020-11-28 17:50:01 -0700582 ret = device_bind(parent, drv, drv->name, NULL, node,
583 &dev);
Simon Glassfc03a552015-03-25 12:22:30 -0600584 if (ret)
585 goto error;
586 debug("%s: Match found: %s\n", __func__, drv->name);
587 dev->driver_data = id->driver_info;
Simon Glass71fa5b42020-12-03 16:55:18 -0700588 plat = dev_get_parent_plat(dev);
Simon Glassfc03a552015-03-25 12:22:30 -0600589 plat->id = *id;
590 *devp = dev;
591 return 0;
592 }
593 }
594
Simon Glassc79173e2015-03-25 12:22:31 -0600595 /* Bind a generic driver so that the device can be used */
596 snprintf(name, sizeof(name), "generic_bus_%x_dev_%x", bus_seq, devnum);
597 str = strdup(name);
598 if (!str)
599 return -ENOMEM;
600 ret = device_bind_driver(parent, "usb_dev_generic_drv", str, devp);
Simon Glass4d2c3592023-01-17 10:47:35 -0700601 if (!ret)
602 device_set_name_alloced(*devp);
Simon Glassc79173e2015-03-25 12:22:31 -0600603
Simon Glassfc03a552015-03-25 12:22:30 -0600604error:
605 debug("%s: No match found: %d\n", __func__, ret);
606 return ret;
607}
608
609/**
Simon Glassd43d7e92015-11-08 23:47:56 -0700610 * usb_find_child() - Find an existing device which matches our needs
611 *
612 *
Simon Glassfc03a552015-03-25 12:22:30 -0600613 */
Simon Glassd43d7e92015-11-08 23:47:56 -0700614static int usb_find_child(struct udevice *parent,
615 struct usb_device_descriptor *desc,
616 struct usb_interface_descriptor *iface,
617 struct udevice **devp)
Simon Glassfc03a552015-03-25 12:22:30 -0600618{
619 struct udevice *dev;
620
621 *devp = NULL;
622 for (device_find_first_child(parent, &dev);
623 dev;
624 device_find_next_child(&dev)) {
Simon Glassb75b15b2020-12-03 16:55:23 -0700625 struct usb_dev_plat *plat = dev_get_parent_plat(dev);
Simon Glassfc03a552015-03-25 12:22:30 -0600626
627 /* If this device is already in use, skip it */
628 if (device_active(dev))
629 continue;
630 debug(" %s: name='%s', plat=%d, desc=%d\n", __func__,
631 dev->name, plat->id.bDeviceClass, desc->bDeviceClass);
632 if (usb_match_one_id(desc, iface, &plat->id)) {
633 *devp = dev;
634 return 0;
635 }
636 }
Simon Glassd43d7e92015-11-08 23:47:56 -0700637
Simon Glassfc03a552015-03-25 12:22:30 -0600638 return -ENOENT;
639}
640
Simon Glass9b82eeb2015-03-25 12:21:59 -0600641int usb_scan_device(struct udevice *parent, int port,
642 enum usb_device_speed speed, struct udevice **devp)
643{
644 struct udevice *dev;
645 bool created = false;
Simon Glassb75b15b2020-12-03 16:55:23 -0700646 struct usb_dev_plat *plat;
Simon Glass9b82eeb2015-03-25 12:21:59 -0600647 struct usb_bus_priv *priv;
648 struct usb_device *parent_udev;
649 int ret;
650 ALLOC_CACHE_ALIGN_BUFFER(struct usb_device, udev, 1);
651 struct usb_interface_descriptor *iface = &udev->config.if_desc[0].desc;
652
653 *devp = NULL;
654 memset(udev, '\0', sizeof(*udev));
Hans de Goede3b5ddd32015-05-05 11:54:31 +0200655 udev->controller_dev = usb_get_bus(parent);
Simon Glass9b82eeb2015-03-25 12:21:59 -0600656 priv = dev_get_uclass_priv(udev->controller_dev);
657
658 /*
659 * Somewhat nasty, this. We create a local device and use the normal
660 * USB stack to read its descriptor. Then we know what type of device
661 * to create for real.
662 *
663 * udev->dev is set to the parent, since we don't have a real device
664 * yet. The USB stack should not access udev.dev anyway, except perhaps
665 * to find the controller, and the controller will either be @parent,
666 * or some parent of @parent.
667 *
668 * Another option might be to create the device as a generic USB
669 * device, then morph it into the correct one when we know what it
670 * should be. This means that a generic USB device would morph into
671 * a network controller, or a USB flash stick, for example. However,
672 * we don't support such morphing and it isn't clear that it would
673 * be easy to do.
674 *
675 * Yet another option is to split out the USB stack parts of udev
676 * into something like a 'struct urb' (as Linux does) which can exist
677 * independently of any device. This feels cleaner, but calls for quite
678 * a big change to the USB stack.
679 *
680 * For now, the approach is to set up an empty udev, read its
681 * descriptor and assign it an address, then bind a real device and
682 * stash the resulting information into the device's parent
683 * platform data. Then when we probe it, usb_child_pre_probe() is called
684 * and it will pull the information out of the stash.
685 */
686 udev->dev = parent;
687 udev->speed = speed;
688 udev->devnum = priv->next_addr + 1;
689 udev->portnr = port;
690 debug("Calling usb_setup_device(), portnr=%d\n", udev->portnr);
691 parent_udev = device_get_uclass_id(parent) == UCLASS_USB_HUB ?
Simon Glassde44acf2015-09-28 23:32:01 -0600692 dev_get_parent_priv(parent) : NULL;
Hans de Goede778dc1c2015-06-17 21:33:46 +0200693 ret = usb_setup_device(udev, priv->desc_before_addr, parent_udev);
Simon Glass9b82eeb2015-03-25 12:21:59 -0600694 debug("read_descriptor for '%s': ret=%d\n", parent->name, ret);
695 if (ret)
696 return ret;
Simon Glassd43d7e92015-11-08 23:47:56 -0700697 ret = usb_find_child(parent, &udev->descriptor, iface, &dev);
698 debug("** usb_find_child returns %d\n", ret);
Simon Glassfc03a552015-03-25 12:22:30 -0600699 if (ret) {
700 if (ret != -ENOENT)
701 return ret;
Michael Walle7c961322020-06-02 01:47:07 +0200702 ret = usb_find_and_bind_driver(parent, &udev->descriptor,
703 iface,
Simon Glass75e534b2020-12-16 21:20:07 -0700704 dev_seq(udev->controller_dev),
Michael Walle7c961322020-06-02 01:47:07 +0200705 udev->devnum, port, &dev);
Simon Glassfc03a552015-03-25 12:22:30 -0600706 if (ret)
707 return ret;
708 created = true;
709 }
Simon Glass71fa5b42020-12-03 16:55:18 -0700710 plat = dev_get_parent_plat(dev);
Simon Glassfc03a552015-03-25 12:22:30 -0600711 debug("%s: Probing '%s', plat=%p\n", __func__, dev->name, plat);
712 plat->devnum = udev->devnum;
Hans de Goede8a0b4c22015-05-05 11:54:32 +0200713 plat->udev = udev;
Simon Glassfc03a552015-03-25 12:22:30 -0600714 priv->next_addr++;
715 ret = device_probe(dev);
716 if (ret) {
717 debug("%s: Device '%s' probe failed\n", __func__, dev->name);
718 priv->next_addr--;
719 if (created)
720 device_unbind(dev);
721 return ret;
722 }
723 *devp = dev;
Simon Glass9b82eeb2015-03-25 12:21:59 -0600724
Simon Glassfc03a552015-03-25 12:22:30 -0600725 return 0;
Simon Glass9b82eeb2015-03-25 12:21:59 -0600726}
727
Simon Glassfbb14942015-05-13 07:02:23 -0600728/*
729 * Detect if a USB device has been plugged or unplugged.
730 */
731int usb_detect_change(void)
732{
733 struct udevice *hub;
734 struct uclass *uc;
735 int change = 0;
736 int ret;
737
738 ret = uclass_get(UCLASS_USB_HUB, &uc);
739 if (ret)
740 return ret;
741
742 uclass_foreach_dev(hub, uc) {
743 struct usb_device *udev;
744 struct udevice *dev;
745
746 if (!device_active(hub))
747 continue;
748 for (device_find_first_child(hub, &dev);
749 dev;
750 device_find_next_child(&dev)) {
751 struct usb_port_status status;
752
753 if (!device_active(dev))
754 continue;
755
Simon Glassde44acf2015-09-28 23:32:01 -0600756 udev = dev_get_parent_priv(dev);
Simon Glassfbb14942015-05-13 07:02:23 -0600757 if (usb_get_port_status(udev, udev->portnr, &status)
758 < 0)
759 /* USB request failed */
760 continue;
761
762 if (le16_to_cpu(status.wPortChange) &
763 USB_PORT_STAT_C_CONNECTION)
764 change++;
765 }
766 }
767
768 return change;
769}
770
Masahiro Yamada6d8e4332017-06-22 16:35:14 +0900771static int usb_child_post_bind(struct udevice *dev)
Simon Glass9b82eeb2015-03-25 12:21:59 -0600772{
Simon Glassb75b15b2020-12-03 16:55:23 -0700773 struct usb_dev_plat *plat = dev_get_parent_plat(dev);
Simon Glass9b82eeb2015-03-25 12:21:59 -0600774 int val;
775
Simon Glassf1d50f72020-12-19 10:40:13 -0700776 if (!dev_has_ofnode(dev))
Simon Glass9b82eeb2015-03-25 12:21:59 -0600777 return 0;
778
779 /* We only support matching a few things */
Simon Glass6deb8e22017-05-18 20:09:38 -0600780 val = dev_read_u32_default(dev, "usb,device-class", -1);
Simon Glass9b82eeb2015-03-25 12:21:59 -0600781 if (val != -1) {
782 plat->id.match_flags |= USB_DEVICE_ID_MATCH_DEV_CLASS;
783 plat->id.bDeviceClass = val;
784 }
Simon Glass6deb8e22017-05-18 20:09:38 -0600785 val = dev_read_u32_default(dev, "usb,interface-class", -1);
Simon Glass9b82eeb2015-03-25 12:21:59 -0600786 if (val != -1) {
787 plat->id.match_flags |= USB_DEVICE_ID_MATCH_INT_CLASS;
788 plat->id.bInterfaceClass = val;
789 }
790
791 return 0;
792}
793
Hans de Goede3b5ddd32015-05-05 11:54:31 +0200794struct udevice *usb_get_bus(struct udevice *dev)
Simon Glass9b82eeb2015-03-25 12:21:59 -0600795{
796 struct udevice *bus;
797
Simon Glass9b82eeb2015-03-25 12:21:59 -0600798 for (bus = dev; bus && device_get_uclass_id(bus) != UCLASS_USB; )
799 bus = bus->parent;
800 if (!bus) {
801 /* By design this cannot happen */
802 assert(bus);
803 debug("USB HUB '%s' does not have a controller\n", dev->name);
Simon Glass9b82eeb2015-03-25 12:21:59 -0600804 }
Simon Glass9b82eeb2015-03-25 12:21:59 -0600805
Hans de Goede3b5ddd32015-05-05 11:54:31 +0200806 return bus;
Simon Glass9b82eeb2015-03-25 12:21:59 -0600807}
808
809int usb_child_pre_probe(struct udevice *dev)
810{
Simon Glassde44acf2015-09-28 23:32:01 -0600811 struct usb_device *udev = dev_get_parent_priv(dev);
Simon Glassb75b15b2020-12-03 16:55:23 -0700812 struct usb_dev_plat *plat = dev_get_parent_plat(dev);
Simon Glass9b82eeb2015-03-25 12:21:59 -0600813 int ret;
814
Hans de Goede8a0b4c22015-05-05 11:54:32 +0200815 if (plat->udev) {
816 /*
817 * Copy over all the values set in the on stack struct
818 * usb_device in usb_scan_device() to our final struct
819 * usb_device for this dev.
820 */
821 *udev = *(plat->udev);
822 /* And clear plat->udev as it will not be valid for long */
823 plat->udev = NULL;
824 udev->dev = dev;
825 } else {
826 /*
827 * This happens with devices which are explicitly bound
828 * instead of being discovered through usb_scan_device()
829 * such as sandbox emul devices.
830 */
831 udev->dev = dev;
832 udev->controller_dev = usb_get_bus(dev);
833 udev->devnum = plat->devnum;
Simon Glass9b82eeb2015-03-25 12:21:59 -0600834
Hans de Goede8a0b4c22015-05-05 11:54:32 +0200835 /*
836 * udev did not go through usb_scan_device(), so we need to
837 * select the config and read the config descriptors.
838 */
839 ret = usb_select_config(udev);
840 if (ret)
841 return ret;
842 }
Simon Glass9b82eeb2015-03-25 12:21:59 -0600843
844 return 0;
845}
846
847UCLASS_DRIVER(usb) = {
848 .id = UCLASS_USB,
849 .name = "usb",
850 .flags = DM_UC_FLAG_SEQ_ALIAS,
Simon Glass18230342016-07-05 17:10:10 -0600851 .post_bind = dm_scan_fdt_dev,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700852 .priv_auto = sizeof(struct usb_uclass_priv),
853 .per_child_auto = sizeof(struct usb_device),
854 .per_device_auto = sizeof(struct usb_bus_priv),
Simon Glass9b82eeb2015-03-25 12:21:59 -0600855 .child_post_bind = usb_child_post_bind,
856 .child_pre_probe = usb_child_pre_probe,
Simon Glassb75b15b2020-12-03 16:55:23 -0700857 .per_child_plat_auto = sizeof(struct usb_dev_plat),
Simon Glass9b82eeb2015-03-25 12:21:59 -0600858};
Simon Glassc79173e2015-03-25 12:22:31 -0600859
860UCLASS_DRIVER(usb_dev_generic) = {
861 .id = UCLASS_USB_DEV_GENERIC,
862 .name = "usb_dev_generic",
863};
864
865U_BOOT_DRIVER(usb_dev_generic_drv) = {
866 .id = UCLASS_USB_DEV_GENERIC,
867 .name = "usb_dev_generic_drv",
868};