blob: 28f7ca9654db2dd1c9e05d50d44b25f1ebf7ba19 [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 -060021extern bool usb_started; /* flag for the started/stopped USB status */
22static bool asynch_allowed;
23
Hans de Goedecebc5b72015-05-10 14:10:21 +020024struct usb_uclass_priv {
25 int companion_device_count;
26};
27
Marek Vasut118a9032020-04-06 14:29:44 +020028int usb_lock_async(struct usb_device *udev, int lock)
29{
30 struct udevice *bus = udev->controller_dev;
31 struct dm_usb_ops *ops = usb_get_ops(bus);
32
33 if (!ops->lock_async)
34 return -ENOSYS;
35
36 return ops->lock_async(bus, lock);
37}
38
Simon Glass9b82eeb2015-03-25 12:21:59 -060039int usb_disable_asynch(int disable)
40{
41 int old_value = asynch_allowed;
42
43 asynch_allowed = !disable;
44 return old_value;
45}
46
47int submit_int_msg(struct usb_device *udev, unsigned long pipe, void *buffer,
Michal Suchanek1c95b9f2019-08-18 10:55:27 +020048 int length, int interval, bool nonblock)
Simon Glass9b82eeb2015-03-25 12:21:59 -060049{
50 struct udevice *bus = udev->controller_dev;
51 struct dm_usb_ops *ops = usb_get_ops(bus);
52
53 if (!ops->interrupt)
54 return -ENOSYS;
55
Michal Suchanek1c95b9f2019-08-18 10:55:27 +020056 return ops->interrupt(bus, udev, pipe, buffer, length, interval,
57 nonblock);
Simon Glass9b82eeb2015-03-25 12:21:59 -060058}
59
60int submit_control_msg(struct usb_device *udev, unsigned long pipe,
61 void *buffer, int length, struct devrequest *setup)
62{
63 struct udevice *bus = udev->controller_dev;
64 struct dm_usb_ops *ops = usb_get_ops(bus);
Simon Glass95588622020-12-22 19:30:28 -070065 struct usb_uclass_priv *uc_priv = uclass_get_priv(bus->uclass);
Hans de Goedecebc5b72015-05-10 14:10:21 +020066 int err;
Simon Glass9b82eeb2015-03-25 12:21:59 -060067
68 if (!ops->control)
69 return -ENOSYS;
70
Hans de Goedecebc5b72015-05-10 14:10:21 +020071 err = ops->control(bus, udev, pipe, buffer, length, setup);
72 if (setup->request == USB_REQ_SET_FEATURE &&
73 setup->requesttype == USB_RT_PORT &&
74 setup->value == cpu_to_le16(USB_PORT_FEAT_RESET) &&
75 err == -ENXIO) {
76 /* Device handed over to companion after port reset */
77 uc_priv->companion_device_count++;
78 }
79
80 return err;
Simon Glass9b82eeb2015-03-25 12:21:59 -060081}
82
83int submit_bulk_msg(struct usb_device *udev, unsigned long pipe, void *buffer,
84 int length)
85{
86 struct udevice *bus = udev->controller_dev;
87 struct dm_usb_ops *ops = usb_get_ops(bus);
88
89 if (!ops->bulk)
90 return -ENOSYS;
91
92 return ops->bulk(bus, udev, pipe, buffer, length);
93}
94
Hans de Goede0a7fa272015-05-10 14:10:18 +020095struct int_queue *create_int_queue(struct usb_device *udev,
96 unsigned long pipe, int queuesize, int elementsize,
97 void *buffer, int interval)
98{
99 struct udevice *bus = udev->controller_dev;
100 struct dm_usb_ops *ops = usb_get_ops(bus);
101
102 if (!ops->create_int_queue)
103 return NULL;
104
105 return ops->create_int_queue(bus, udev, pipe, queuesize, elementsize,
106 buffer, interval);
107}
108
109void *poll_int_queue(struct usb_device *udev, struct int_queue *queue)
110{
111 struct udevice *bus = udev->controller_dev;
112 struct dm_usb_ops *ops = usb_get_ops(bus);
113
114 if (!ops->poll_int_queue)
115 return NULL;
116
117 return ops->poll_int_queue(bus, udev, queue);
118}
119
120int destroy_int_queue(struct usb_device *udev, struct int_queue *queue)
121{
122 struct udevice *bus = udev->controller_dev;
123 struct dm_usb_ops *ops = usb_get_ops(bus);
124
125 if (!ops->destroy_int_queue)
126 return -ENOSYS;
127
128 return ops->destroy_int_queue(bus, udev, queue);
129}
130
Simon Glass9b82eeb2015-03-25 12:21:59 -0600131int usb_alloc_device(struct usb_device *udev)
132{
133 struct udevice *bus = udev->controller_dev;
134 struct dm_usb_ops *ops = usb_get_ops(bus);
135
136 /* This is only requird by some controllers - current XHCI */
137 if (!ops->alloc_device)
138 return 0;
139
140 return ops->alloc_device(bus, udev);
141}
142
Hans de Goedea9e41f82015-06-17 21:33:52 +0200143int usb_reset_root_port(struct usb_device *udev)
144{
145 struct udevice *bus = udev->controller_dev;
146 struct dm_usb_ops *ops = usb_get_ops(bus);
147
148 if (!ops->reset_root_port)
149 return -ENOSYS;
150
151 return ops->reset_root_port(bus, udev);
152}
153
Bin Meng1f31f5a2017-07-19 21:51:17 +0800154int usb_update_hub_device(struct usb_device *udev)
155{
156 struct udevice *bus = udev->controller_dev;
157 struct dm_usb_ops *ops = usb_get_ops(bus);
158
159 if (!ops->update_hub_device)
160 return -ENOSYS;
161
162 return ops->update_hub_device(bus, udev);
163}
164
Bin Meng6840a342017-09-07 06:13:17 -0700165int usb_get_max_xfer_size(struct usb_device *udev, size_t *size)
166{
167 struct udevice *bus = udev->controller_dev;
168 struct dm_usb_ops *ops = usb_get_ops(bus);
169
170 if (!ops->get_max_xfer_size)
171 return -ENOSYS;
172
173 return ops->get_max_xfer_size(bus, size);
174}
175
Simon Glass9b82eeb2015-03-25 12:21:59 -0600176int usb_stop(void)
177{
178 struct udevice *bus;
Bin Meng669ad842017-10-01 06:19:42 -0700179 struct udevice *rh;
Simon Glass9b82eeb2015-03-25 12:21:59 -0600180 struct uclass *uc;
Hans de Goedecebc5b72015-05-10 14:10:21 +0200181 struct usb_uclass_priv *uc_priv;
Simon Glass9b82eeb2015-03-25 12:21:59 -0600182 int err = 0, ret;
183
184 /* De-activate any devices that have been activated */
185 ret = uclass_get(UCLASS_USB, &uc);
186 if (ret)
187 return ret;
Hans de Goedecebc5b72015-05-10 14:10:21 +0200188
Simon Glass95588622020-12-22 19:30:28 -0700189 uc_priv = uclass_get_priv(uc);
Hans de Goedecebc5b72015-05-10 14:10:21 +0200190
Simon Glass9b82eeb2015-03-25 12:21:59 -0600191 uclass_foreach_dev(bus, uc) {
Stefan Roese80b5bc92017-03-20 12:51:48 +0100192 ret = device_remove(bus, DM_REMOVE_NORMAL);
Simon Glass9b82eeb2015-03-25 12:21:59 -0600193 if (ret && !err)
194 err = ret;
Bin Meng669ad842017-10-01 06:19:42 -0700195
196 /* Locate root hub device */
197 device_find_first_child(bus, &rh);
198 if (rh) {
199 /*
200 * All USB devices are children of root hub.
201 * Unbinding root hub will unbind all of its children.
202 */
203 ret = device_unbind(rh);
204 if (ret && !err)
205 err = ret;
206 }
Simon Glass9b82eeb2015-03-25 12:21:59 -0600207 }
Bin Mengdadc4c02017-10-01 06:19:43 -0700208
Paul Kocialkowskid449dd32015-08-04 17:04:12 +0200209#ifdef CONFIG_USB_STORAGE
Simon Glass9b82eeb2015-03-25 12:21:59 -0600210 usb_stor_reset();
Paul Kocialkowskid449dd32015-08-04 17:04:12 +0200211#endif
Hans de Goedecebc5b72015-05-10 14:10:21 +0200212 uc_priv->companion_device_count = 0;
Simon Glass9b82eeb2015-03-25 12:21:59 -0600213 usb_started = 0;
214
215 return err;
216}
217
Hans de Goedef8762f92015-05-10 14:10:19 +0200218static void usb_scan_bus(struct udevice *bus, bool recurse)
Simon Glass9b82eeb2015-03-25 12:21:59 -0600219{
220 struct usb_bus_priv *priv;
221 struct udevice *dev;
222 int ret;
223
224 priv = dev_get_uclass_priv(bus);
225
226 assert(recurse); /* TODO: Support non-recusive */
227
Ismael Luceno Cortesf58affe2019-03-19 09:19:44 +0000228 printf("scanning bus %s for devices... ", bus->name);
Hans de Goedef8762f92015-05-10 14:10:19 +0200229 debug("\n");
Simon Glass9b82eeb2015-03-25 12:21:59 -0600230 ret = usb_scan_device(bus, 0, USB_SPEED_FULL, &dev);
231 if (ret)
Hans de Goedef8762f92015-05-10 14:10:19 +0200232 printf("failed, error %d\n", ret);
233 else if (priv->next_addr == 0)
234 printf("No USB Device found\n");
235 else
236 printf("%d USB Device(s) found\n", priv->next_addr);
Simon Glass9b82eeb2015-03-25 12:21:59 -0600237}
238
Simon Glass35787d22015-11-08 23:48:00 -0700239static void remove_inactive_children(struct uclass *uc, struct udevice *bus)
240{
241 uclass_foreach_dev(bus, uc) {
242 struct udevice *dev, *next;
243
244 if (!device_active(bus))
245 continue;
246 device_foreach_child_safe(dev, next, bus) {
247 if (!device_active(dev))
248 device_unbind(dev);
249 }
250 }
251}
252
Simon Glass9b82eeb2015-03-25 12:21:59 -0600253int usb_init(void)
254{
255 int controllers_initialized = 0;
Hans de Goedecebc5b72015-05-10 14:10:21 +0200256 struct usb_uclass_priv *uc_priv;
Hans de Goede59a0dfc2015-05-10 14:10:20 +0200257 struct usb_bus_priv *priv;
Simon Glass9b82eeb2015-03-25 12:21:59 -0600258 struct udevice *bus;
259 struct uclass *uc;
Simon Glass9b82eeb2015-03-25 12:21:59 -0600260 int ret;
261
262 asynch_allowed = 1;
Simon Glass9b82eeb2015-03-25 12:21:59 -0600263
264 ret = uclass_get(UCLASS_USB, &uc);
265 if (ret)
266 return ret;
267
Simon Glass95588622020-12-22 19:30:28 -0700268 uc_priv = uclass_get_priv(uc);
Hans de Goedecebc5b72015-05-10 14:10:21 +0200269
Simon Glass9b82eeb2015-03-25 12:21:59 -0600270 uclass_foreach_dev(bus, uc) {
271 /* init low_level USB */
Ismael Luceno Cortesf58affe2019-03-19 09:19:44 +0000272 printf("Bus %s: ", bus->name);
Bin Meng669ad842017-10-01 06:19:42 -0700273
Bin Meng669ad842017-10-01 06:19:42 -0700274 /*
275 * For Sandbox, we need scan the device tree each time when we
276 * start the USB stack, in order to re-create the emulated USB
277 * devices and bind drivers for them before we actually do the
278 * driver probe.
Fabrice Gasniercd809b12022-12-12 11:44:35 +0100279 *
280 * For USB onboard HUB, we need to do some non-trivial init
281 * like enabling a power regulator, before enumeration.
Bin Meng669ad842017-10-01 06:19:42 -0700282 */
Fabrice Gasniercd809b12022-12-12 11:44:35 +0100283 if (IS_ENABLED(CONFIG_SANDBOX) ||
284 IS_ENABLED(CONFIG_USB_ONBOARD_HUB)) {
285 ret = dm_scan_fdt_dev(bus);
286 if (ret) {
287 printf("USB device scan from fdt failed (%d)", ret);
288 continue;
289 }
Bin Meng669ad842017-10-01 06:19:42 -0700290 }
Bin Meng669ad842017-10-01 06:19:42 -0700291
Simon Glass9b82eeb2015-03-25 12:21:59 -0600292 ret = device_probe(bus);
293 if (ret == -ENODEV) { /* No such device. */
294 puts("Port not available.\n");
295 controllers_initialized++;
296 continue;
297 }
298
299 if (ret) { /* Other error. */
300 printf("probe failed, error %d\n", ret);
301 continue;
302 }
Simon Glass9b82eeb2015-03-25 12:21:59 -0600303 controllers_initialized++;
Simon Glass9b82eeb2015-03-25 12:21:59 -0600304 usb_started = true;
305 }
306
Hans de Goede59a0dfc2015-05-10 14:10:20 +0200307 /*
308 * lowlevel init done, now scan the bus for devices i.e. search HUBs
309 * and configure them, first scan primary controllers.
310 */
311 uclass_foreach_dev(bus, uc) {
312 if (!device_active(bus))
313 continue;
314
315 priv = dev_get_uclass_priv(bus);
316 if (!priv->companion)
317 usb_scan_bus(bus, true);
318 }
319
320 /*
321 * Now that the primary controllers have been scanned and have handed
322 * over any devices they do not understand to their companions, scan
Hans de Goedecebc5b72015-05-10 14:10:21 +0200323 * the companions if necessary.
Hans de Goede59a0dfc2015-05-10 14:10:20 +0200324 */
Hans de Goedecebc5b72015-05-10 14:10:21 +0200325 if (uc_priv->companion_device_count) {
326 uclass_foreach_dev(bus, uc) {
327 if (!device_active(bus))
328 continue;
Hans de Goede59a0dfc2015-05-10 14:10:20 +0200329
Hans de Goedecebc5b72015-05-10 14:10:21 +0200330 priv = dev_get_uclass_priv(bus);
331 if (priv->companion)
332 usb_scan_bus(bus, true);
333 }
Hans de Goede59a0dfc2015-05-10 14:10:20 +0200334 }
335
Simon Glass9b82eeb2015-03-25 12:21:59 -0600336 debug("scan end\n");
Simon Glass35787d22015-11-08 23:48:00 -0700337
338 /* Remove any devices that were not found on this scan */
339 remove_inactive_children(uc, bus);
340
341 ret = uclass_get(UCLASS_USB_HUB, &uc);
342 if (ret)
343 return ret;
344 remove_inactive_children(uc, bus);
345
Simon Glass9b82eeb2015-03-25 12:21:59 -0600346 /* if we were not able to find at least one working bus, bail out */
Ismael Luceno Cortesf58affe2019-03-19 09:19:44 +0000347 if (controllers_initialized == 0)
348 printf("No working controllers found\n");
Simon Glass9b82eeb2015-03-25 12:21:59 -0600349
350 return usb_started ? 0 : -1;
351}
352
Simon Glass444b1e02015-03-25 12:22:32 -0600353int usb_setup_ehci_gadget(struct ehci_ctrl **ctlrp)
354{
Simon Glassb75b15b2020-12-03 16:55:23 -0700355 struct usb_plat *plat;
Simon Glass444b1e02015-03-25 12:22:32 -0600356 struct udevice *dev;
357 int ret;
358
359 /* Find the old device and remove it */
Sean Andersone7665702021-11-05 12:52:55 -0400360 ret = uclass_find_first_device(UCLASS_USB, &dev);
Simon Glass444b1e02015-03-25 12:22:32 -0600361 if (ret)
362 return ret;
Stefan Roese80b5bc92017-03-20 12:51:48 +0100363 ret = device_remove(dev, DM_REMOVE_NORMAL);
Simon Glass444b1e02015-03-25 12:22:32 -0600364 if (ret)
365 return ret;
366
Simon Glassfa20e932020-12-03 16:55:20 -0700367 plat = dev_get_plat(dev);
Simon Glass444b1e02015-03-25 12:22:32 -0600368 plat->init_type = USB_INIT_DEVICE;
369 ret = device_probe(dev);
370 if (ret)
371 return ret;
372 *ctlrp = dev_get_priv(dev);
373
374 return 0;
375}
376
Ye Li68801eb2020-06-29 10:12:59 +0800377int usb_remove_ehci_gadget(struct ehci_ctrl **ctlrp)
378{
379 struct udevice *dev;
380 int ret;
381
382 /* Find the old device and remove it */
Sean Andersone7665702021-11-05 12:52:55 -0400383 ret = uclass_find_first_device(UCLASS_USB, &dev);
Ye Li68801eb2020-06-29 10:12:59 +0800384 if (ret)
385 return ret;
386 ret = device_remove(dev, DM_REMOVE_NORMAL);
387 if (ret)
388 return ret;
389
390 *ctlrp = NULL;
391
392 return 0;
393}
394
Simon Glassfc03a552015-03-25 12:22:30 -0600395/* returns 0 if no match, 1 if match */
Masahiro Yamada6d8e4332017-06-22 16:35:14 +0900396static int usb_match_device(const struct usb_device_descriptor *desc,
397 const struct usb_device_id *id)
Simon Glassfc03a552015-03-25 12:22:30 -0600398{
399 if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
Stefan Roese77c83ac2020-07-21 10:46:04 +0200400 id->idVendor != desc->idVendor)
Simon Glassfc03a552015-03-25 12:22:30 -0600401 return 0;
402
403 if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
Stefan Roese77c83ac2020-07-21 10:46:04 +0200404 id->idProduct != desc->idProduct)
Simon Glassfc03a552015-03-25 12:22:30 -0600405 return 0;
406
407 /* No need to test id->bcdDevice_lo != 0, since 0 is never
408 greater than any unsigned number. */
409 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) &&
Stefan Roese77c83ac2020-07-21 10:46:04 +0200410 (id->bcdDevice_lo > desc->bcdDevice))
Simon Glassfc03a552015-03-25 12:22:30 -0600411 return 0;
412
413 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) &&
Stefan Roese77c83ac2020-07-21 10:46:04 +0200414 (id->bcdDevice_hi < desc->bcdDevice))
Simon Glassfc03a552015-03-25 12:22:30 -0600415 return 0;
416
417 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) &&
418 (id->bDeviceClass != desc->bDeviceClass))
419 return 0;
420
421 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) &&
422 (id->bDeviceSubClass != desc->bDeviceSubClass))
423 return 0;
424
425 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) &&
426 (id->bDeviceProtocol != desc->bDeviceProtocol))
427 return 0;
428
429 return 1;
430}
431
432/* returns 0 if no match, 1 if match */
Masahiro Yamada6d8e4332017-06-22 16:35:14 +0900433static int usb_match_one_id_intf(const struct usb_device_descriptor *desc,
434 const struct usb_interface_descriptor *int_desc,
435 const struct usb_device_id *id)
Simon Glassfc03a552015-03-25 12:22:30 -0600436{
437 /* The interface class, subclass, protocol and number should never be
438 * checked for a match if the device class is Vendor Specific,
439 * unless the match record specifies the Vendor ID. */
440 if (desc->bDeviceClass == USB_CLASS_VENDOR_SPEC &&
441 !(id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
442 (id->match_flags & (USB_DEVICE_ID_MATCH_INT_CLASS |
443 USB_DEVICE_ID_MATCH_INT_SUBCLASS |
444 USB_DEVICE_ID_MATCH_INT_PROTOCOL |
445 USB_DEVICE_ID_MATCH_INT_NUMBER)))
446 return 0;
447
448 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) &&
449 (id->bInterfaceClass != int_desc->bInterfaceClass))
450 return 0;
451
452 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) &&
453 (id->bInterfaceSubClass != int_desc->bInterfaceSubClass))
454 return 0;
455
456 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) &&
457 (id->bInterfaceProtocol != int_desc->bInterfaceProtocol))
458 return 0;
459
460 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_NUMBER) &&
461 (id->bInterfaceNumber != int_desc->bInterfaceNumber))
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(struct usb_device_descriptor *desc,
469 struct usb_interface_descriptor *int_desc,
470 const struct usb_device_id *id)
Simon Glassfc03a552015-03-25 12:22:30 -0600471{
472 if (!usb_match_device(desc, id))
473 return 0;
474
475 return usb_match_one_id_intf(desc, int_desc, id);
476}
477
Michael Walle7c961322020-06-02 01:47:07 +0200478static ofnode usb_get_ofnode(struct udevice *hub, int port)
479{
480 ofnode node;
481 u32 reg;
482
Simon Glass07c17772020-12-19 10:40:12 -0700483 if (!dev_has_ofnode(hub))
Michael Walle7c961322020-06-02 01:47:07 +0200484 return ofnode_null();
485
486 /*
487 * The USB controller and its USB hub are two different udevices,
488 * but the device tree has only one node for both. Thus we are
489 * assigning this node to both udevices.
490 * If port is zero, the controller scans its root hub, thus we
491 * are using the same ofnode as the controller here.
492 */
493 if (!port)
494 return dev_ofnode(hub);
495
496 ofnode_for_each_subnode(node, dev_ofnode(hub)) {
497 if (ofnode_read_u32(node, "reg", &reg))
498 continue;
499
500 if (reg == port)
501 return node;
502 }
503
504 return ofnode_null();
505}
506
Simon Glassfc03a552015-03-25 12:22:30 -0600507/**
508 * usb_find_and_bind_driver() - Find and bind the right USB driver
509 *
510 * This only looks at certain fields in the descriptor.
511 */
512static int usb_find_and_bind_driver(struct udevice *parent,
513 struct usb_device_descriptor *desc,
514 struct usb_interface_descriptor *iface,
Michael Walle7c961322020-06-02 01:47:07 +0200515 int bus_seq, int devnum, int port,
Simon Glassfc03a552015-03-25 12:22:30 -0600516 struct udevice **devp)
517{
518 struct usb_driver_entry *start, *entry;
519 int n_ents;
520 int ret;
Marek Vasut6e76b0f2022-11-26 13:57:53 +0100521 char name[34], *str;
Michael Walle7c961322020-06-02 01:47:07 +0200522 ofnode node = usb_get_ofnode(parent, port);
Simon Glassfc03a552015-03-25 12:22:30 -0600523
524 *devp = NULL;
525 debug("%s: Searching for driver\n", __func__);
526 start = ll_entry_start(struct usb_driver_entry, usb_driver_entry);
527 n_ents = ll_entry_count(struct usb_driver_entry, usb_driver_entry);
528 for (entry = start; entry != start + n_ents; entry++) {
529 const struct usb_device_id *id;
530 struct udevice *dev;
531 const struct driver *drv;
Simon Glassb75b15b2020-12-03 16:55:23 -0700532 struct usb_dev_plat *plat;
Simon Glassfc03a552015-03-25 12:22:30 -0600533
534 for (id = entry->match; id->match_flags; id++) {
535 if (!usb_match_one_id(desc, iface, id))
536 continue;
537
538 drv = entry->driver;
539 /*
540 * We could pass the descriptor to the driver as
Simon Glass71fa5b42020-12-03 16:55:18 -0700541 * plat (instead of NULL) and allow its bind()
Simon Glassfc03a552015-03-25 12:22:30 -0600542 * method to return -ENOENT if it doesn't support this
543 * device. That way we could continue the search to
544 * find another driver. For now this doesn't seem
545 * necesssary, so just bind the first match.
546 */
Simon Glass884870f2020-11-28 17:50:01 -0700547 ret = device_bind(parent, drv, drv->name, NULL, node,
548 &dev);
Simon Glassfc03a552015-03-25 12:22:30 -0600549 if (ret)
550 goto error;
551 debug("%s: Match found: %s\n", __func__, drv->name);
552 dev->driver_data = id->driver_info;
Simon Glass71fa5b42020-12-03 16:55:18 -0700553 plat = dev_get_parent_plat(dev);
Simon Glassfc03a552015-03-25 12:22:30 -0600554 plat->id = *id;
555 *devp = dev;
556 return 0;
557 }
558 }
559
Simon Glassc79173e2015-03-25 12:22:31 -0600560 /* Bind a generic driver so that the device can be used */
561 snprintf(name, sizeof(name), "generic_bus_%x_dev_%x", bus_seq, devnum);
562 str = strdup(name);
563 if (!str)
564 return -ENOMEM;
565 ret = device_bind_driver(parent, "usb_dev_generic_drv", str, devp);
Simon Glass4d2c3592023-01-17 10:47:35 -0700566 if (!ret)
567 device_set_name_alloced(*devp);
Simon Glassc79173e2015-03-25 12:22:31 -0600568
Simon Glassfc03a552015-03-25 12:22:30 -0600569error:
570 debug("%s: No match found: %d\n", __func__, ret);
571 return ret;
572}
573
574/**
Simon Glassd43d7e92015-11-08 23:47:56 -0700575 * usb_find_child() - Find an existing device which matches our needs
576 *
577 *
Simon Glassfc03a552015-03-25 12:22:30 -0600578 */
Simon Glassd43d7e92015-11-08 23:47:56 -0700579static int usb_find_child(struct udevice *parent,
580 struct usb_device_descriptor *desc,
581 struct usb_interface_descriptor *iface,
582 struct udevice **devp)
Simon Glassfc03a552015-03-25 12:22:30 -0600583{
584 struct udevice *dev;
585
586 *devp = NULL;
587 for (device_find_first_child(parent, &dev);
588 dev;
589 device_find_next_child(&dev)) {
Simon Glassb75b15b2020-12-03 16:55:23 -0700590 struct usb_dev_plat *plat = dev_get_parent_plat(dev);
Simon Glassfc03a552015-03-25 12:22:30 -0600591
592 /* If this device is already in use, skip it */
593 if (device_active(dev))
594 continue;
595 debug(" %s: name='%s', plat=%d, desc=%d\n", __func__,
596 dev->name, plat->id.bDeviceClass, desc->bDeviceClass);
597 if (usb_match_one_id(desc, iface, &plat->id)) {
598 *devp = dev;
599 return 0;
600 }
601 }
Simon Glassd43d7e92015-11-08 23:47:56 -0700602
Simon Glassfc03a552015-03-25 12:22:30 -0600603 return -ENOENT;
604}
605
Simon Glass9b82eeb2015-03-25 12:21:59 -0600606int usb_scan_device(struct udevice *parent, int port,
607 enum usb_device_speed speed, struct udevice **devp)
608{
609 struct udevice *dev;
610 bool created = false;
Simon Glassb75b15b2020-12-03 16:55:23 -0700611 struct usb_dev_plat *plat;
Simon Glass9b82eeb2015-03-25 12:21:59 -0600612 struct usb_bus_priv *priv;
613 struct usb_device *parent_udev;
614 int ret;
615 ALLOC_CACHE_ALIGN_BUFFER(struct usb_device, udev, 1);
616 struct usb_interface_descriptor *iface = &udev->config.if_desc[0].desc;
617
618 *devp = NULL;
619 memset(udev, '\0', sizeof(*udev));
Hans de Goede3b5ddd32015-05-05 11:54:31 +0200620 udev->controller_dev = usb_get_bus(parent);
Simon Glass9b82eeb2015-03-25 12:21:59 -0600621 priv = dev_get_uclass_priv(udev->controller_dev);
622
623 /*
624 * Somewhat nasty, this. We create a local device and use the normal
625 * USB stack to read its descriptor. Then we know what type of device
626 * to create for real.
627 *
628 * udev->dev is set to the parent, since we don't have a real device
629 * yet. The USB stack should not access udev.dev anyway, except perhaps
630 * to find the controller, and the controller will either be @parent,
631 * or some parent of @parent.
632 *
633 * Another option might be to create the device as a generic USB
634 * device, then morph it into the correct one when we know what it
635 * should be. This means that a generic USB device would morph into
636 * a network controller, or a USB flash stick, for example. However,
637 * we don't support such morphing and it isn't clear that it would
638 * be easy to do.
639 *
640 * Yet another option is to split out the USB stack parts of udev
641 * into something like a 'struct urb' (as Linux does) which can exist
642 * independently of any device. This feels cleaner, but calls for quite
643 * a big change to the USB stack.
644 *
645 * For now, the approach is to set up an empty udev, read its
646 * descriptor and assign it an address, then bind a real device and
647 * stash the resulting information into the device's parent
648 * platform data. Then when we probe it, usb_child_pre_probe() is called
649 * and it will pull the information out of the stash.
650 */
651 udev->dev = parent;
652 udev->speed = speed;
653 udev->devnum = priv->next_addr + 1;
654 udev->portnr = port;
655 debug("Calling usb_setup_device(), portnr=%d\n", udev->portnr);
656 parent_udev = device_get_uclass_id(parent) == UCLASS_USB_HUB ?
Simon Glassde44acf2015-09-28 23:32:01 -0600657 dev_get_parent_priv(parent) : NULL;
Hans de Goede778dc1c2015-06-17 21:33:46 +0200658 ret = usb_setup_device(udev, priv->desc_before_addr, parent_udev);
Simon Glass9b82eeb2015-03-25 12:21:59 -0600659 debug("read_descriptor for '%s': ret=%d\n", parent->name, ret);
660 if (ret)
661 return ret;
Simon Glassd43d7e92015-11-08 23:47:56 -0700662 ret = usb_find_child(parent, &udev->descriptor, iface, &dev);
663 debug("** usb_find_child returns %d\n", ret);
Simon Glassfc03a552015-03-25 12:22:30 -0600664 if (ret) {
665 if (ret != -ENOENT)
666 return ret;
Michael Walle7c961322020-06-02 01:47:07 +0200667 ret = usb_find_and_bind_driver(parent, &udev->descriptor,
668 iface,
Simon Glass75e534b2020-12-16 21:20:07 -0700669 dev_seq(udev->controller_dev),
Michael Walle7c961322020-06-02 01:47:07 +0200670 udev->devnum, port, &dev);
Simon Glassfc03a552015-03-25 12:22:30 -0600671 if (ret)
672 return ret;
673 created = true;
674 }
Simon Glass71fa5b42020-12-03 16:55:18 -0700675 plat = dev_get_parent_plat(dev);
Simon Glassfc03a552015-03-25 12:22:30 -0600676 debug("%s: Probing '%s', plat=%p\n", __func__, dev->name, plat);
677 plat->devnum = udev->devnum;
Hans de Goede8a0b4c22015-05-05 11:54:32 +0200678 plat->udev = udev;
Simon Glassfc03a552015-03-25 12:22:30 -0600679 priv->next_addr++;
680 ret = device_probe(dev);
681 if (ret) {
682 debug("%s: Device '%s' probe failed\n", __func__, dev->name);
683 priv->next_addr--;
684 if (created)
685 device_unbind(dev);
686 return ret;
687 }
688 *devp = dev;
Simon Glass9b82eeb2015-03-25 12:21:59 -0600689
Simon Glassfc03a552015-03-25 12:22:30 -0600690 return 0;
Simon Glass9b82eeb2015-03-25 12:21:59 -0600691}
692
Simon Glassfbb14942015-05-13 07:02:23 -0600693/*
694 * Detect if a USB device has been plugged or unplugged.
695 */
696int usb_detect_change(void)
697{
698 struct udevice *hub;
699 struct uclass *uc;
700 int change = 0;
701 int ret;
702
703 ret = uclass_get(UCLASS_USB_HUB, &uc);
704 if (ret)
705 return ret;
706
707 uclass_foreach_dev(hub, uc) {
708 struct usb_device *udev;
709 struct udevice *dev;
710
711 if (!device_active(hub))
712 continue;
713 for (device_find_first_child(hub, &dev);
714 dev;
715 device_find_next_child(&dev)) {
716 struct usb_port_status status;
717
718 if (!device_active(dev))
719 continue;
720
Simon Glassde44acf2015-09-28 23:32:01 -0600721 udev = dev_get_parent_priv(dev);
Simon Glassfbb14942015-05-13 07:02:23 -0600722 if (usb_get_port_status(udev, udev->portnr, &status)
723 < 0)
724 /* USB request failed */
725 continue;
726
727 if (le16_to_cpu(status.wPortChange) &
728 USB_PORT_STAT_C_CONNECTION)
729 change++;
730 }
731 }
732
733 return change;
734}
735
Masahiro Yamada6d8e4332017-06-22 16:35:14 +0900736static int usb_child_post_bind(struct udevice *dev)
Simon Glass9b82eeb2015-03-25 12:21:59 -0600737{
Simon Glassb75b15b2020-12-03 16:55:23 -0700738 struct usb_dev_plat *plat = dev_get_parent_plat(dev);
Simon Glass9b82eeb2015-03-25 12:21:59 -0600739 int val;
740
Simon Glassf1d50f72020-12-19 10:40:13 -0700741 if (!dev_has_ofnode(dev))
Simon Glass9b82eeb2015-03-25 12:21:59 -0600742 return 0;
743
744 /* We only support matching a few things */
Simon Glass6deb8e22017-05-18 20:09:38 -0600745 val = dev_read_u32_default(dev, "usb,device-class", -1);
Simon Glass9b82eeb2015-03-25 12:21:59 -0600746 if (val != -1) {
747 plat->id.match_flags |= USB_DEVICE_ID_MATCH_DEV_CLASS;
748 plat->id.bDeviceClass = val;
749 }
Simon Glass6deb8e22017-05-18 20:09:38 -0600750 val = dev_read_u32_default(dev, "usb,interface-class", -1);
Simon Glass9b82eeb2015-03-25 12:21:59 -0600751 if (val != -1) {
752 plat->id.match_flags |= USB_DEVICE_ID_MATCH_INT_CLASS;
753 plat->id.bInterfaceClass = val;
754 }
755
756 return 0;
757}
758
Hans de Goede3b5ddd32015-05-05 11:54:31 +0200759struct udevice *usb_get_bus(struct udevice *dev)
Simon Glass9b82eeb2015-03-25 12:21:59 -0600760{
761 struct udevice *bus;
762
Simon Glass9b82eeb2015-03-25 12:21:59 -0600763 for (bus = dev; bus && device_get_uclass_id(bus) != UCLASS_USB; )
764 bus = bus->parent;
765 if (!bus) {
766 /* By design this cannot happen */
767 assert(bus);
768 debug("USB HUB '%s' does not have a controller\n", dev->name);
Simon Glass9b82eeb2015-03-25 12:21:59 -0600769 }
Simon Glass9b82eeb2015-03-25 12:21:59 -0600770
Hans de Goede3b5ddd32015-05-05 11:54:31 +0200771 return bus;
Simon Glass9b82eeb2015-03-25 12:21:59 -0600772}
773
774int usb_child_pre_probe(struct udevice *dev)
775{
Simon Glassde44acf2015-09-28 23:32:01 -0600776 struct usb_device *udev = dev_get_parent_priv(dev);
Simon Glassb75b15b2020-12-03 16:55:23 -0700777 struct usb_dev_plat *plat = dev_get_parent_plat(dev);
Simon Glass9b82eeb2015-03-25 12:21:59 -0600778 int ret;
779
Hans de Goede8a0b4c22015-05-05 11:54:32 +0200780 if (plat->udev) {
781 /*
782 * Copy over all the values set in the on stack struct
783 * usb_device in usb_scan_device() to our final struct
784 * usb_device for this dev.
785 */
786 *udev = *(plat->udev);
787 /* And clear plat->udev as it will not be valid for long */
788 plat->udev = NULL;
789 udev->dev = dev;
790 } else {
791 /*
792 * This happens with devices which are explicitly bound
793 * instead of being discovered through usb_scan_device()
794 * such as sandbox emul devices.
795 */
796 udev->dev = dev;
797 udev->controller_dev = usb_get_bus(dev);
798 udev->devnum = plat->devnum;
Simon Glass9b82eeb2015-03-25 12:21:59 -0600799
Hans de Goede8a0b4c22015-05-05 11:54:32 +0200800 /*
801 * udev did not go through usb_scan_device(), so we need to
802 * select the config and read the config descriptors.
803 */
804 ret = usb_select_config(udev);
805 if (ret)
806 return ret;
807 }
Simon Glass9b82eeb2015-03-25 12:21:59 -0600808
809 return 0;
810}
811
812UCLASS_DRIVER(usb) = {
813 .id = UCLASS_USB,
814 .name = "usb",
815 .flags = DM_UC_FLAG_SEQ_ALIAS,
Simon Glass18230342016-07-05 17:10:10 -0600816 .post_bind = dm_scan_fdt_dev,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700817 .priv_auto = sizeof(struct usb_uclass_priv),
818 .per_child_auto = sizeof(struct usb_device),
819 .per_device_auto = sizeof(struct usb_bus_priv),
Simon Glass9b82eeb2015-03-25 12:21:59 -0600820 .child_post_bind = usb_child_post_bind,
821 .child_pre_probe = usb_child_pre_probe,
Simon Glassb75b15b2020-12-03 16:55:23 -0700822 .per_child_plat_auto = sizeof(struct usb_dev_plat),
Simon Glass9b82eeb2015-03-25 12:21:59 -0600823};
Simon Glassc79173e2015-03-25 12:22:31 -0600824
825UCLASS_DRIVER(usb_dev_generic) = {
826 .id = UCLASS_USB_DEV_GENERIC,
827 .name = "usb_dev_generic",
828};
829
830U_BOOT_DRIVER(usb_dev_generic_drv) = {
831 .id = UCLASS_USB_DEV_GENERIC,
832 .name = "usb_dev_generic_drv",
833};