blob: 7247245a702ac013173f2cb726bd3e486fdcde23 [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>
Jerome Forissier219dade2025-04-18 16:09:41 +020012#include <uthread.h>
Simon Glass9b82eeb2015-03-25 12:21:59 -060013#include <dm.h>
14#include <errno.h>
Simon Glass0f2af882020-05-10 11:40:05 -060015#include <log.h>
Simon Glass2dd337a2015-09-02 17:24:58 -060016#include <memalign.h>
Simon Glass9b82eeb2015-03-25 12:21:59 -060017#include <usb.h>
18#include <dm/device-internal.h>
19#include <dm/lists.h>
Simon Glass9b82eeb2015-03-25 12:21:59 -060020#include <dm/uclass-internal.h>
Jerome Forissier219dade2025-04-18 16:09:41 +020021#include <time.h>
Simon Glass9b82eeb2015-03-25 12:21:59 -060022
Simon Glass9b82eeb2015-03-25 12:21:59 -060023static bool asynch_allowed;
24
Hans de Goedecebc5b72015-05-10 14:10:21 +020025struct usb_uclass_priv {
26 int companion_device_count;
27};
28
Marek Vasut118a9032020-04-06 14:29:44 +020029int usb_lock_async(struct usb_device *udev, int lock)
30{
31 struct udevice *bus = udev->controller_dev;
32 struct dm_usb_ops *ops = usb_get_ops(bus);
33
34 if (!ops->lock_async)
35 return -ENOSYS;
36
37 return ops->lock_async(bus, lock);
38}
39
Simon Glass9b82eeb2015-03-25 12:21:59 -060040int usb_disable_asynch(int disable)
41{
42 int old_value = asynch_allowed;
43
44 asynch_allowed = !disable;
45 return old_value;
46}
47
48int submit_int_msg(struct usb_device *udev, unsigned long pipe, void *buffer,
Michal Suchanek1c95b9f2019-08-18 10:55:27 +020049 int length, int interval, bool nonblock)
Simon Glass9b82eeb2015-03-25 12:21:59 -060050{
51 struct udevice *bus = udev->controller_dev;
52 struct dm_usb_ops *ops = usb_get_ops(bus);
53
54 if (!ops->interrupt)
55 return -ENOSYS;
56
Michal Suchanek1c95b9f2019-08-18 10:55:27 +020057 return ops->interrupt(bus, udev, pipe, buffer, length, interval,
58 nonblock);
Simon Glass9b82eeb2015-03-25 12:21:59 -060059}
60
61int submit_control_msg(struct usb_device *udev, unsigned long pipe,
62 void *buffer, int length, struct devrequest *setup)
63{
64 struct udevice *bus = udev->controller_dev;
65 struct dm_usb_ops *ops = usb_get_ops(bus);
Simon Glass95588622020-12-22 19:30:28 -070066 struct usb_uclass_priv *uc_priv = uclass_get_priv(bus->uclass);
Hans de Goedecebc5b72015-05-10 14:10:21 +020067 int err;
Simon Glass9b82eeb2015-03-25 12:21:59 -060068
69 if (!ops->control)
70 return -ENOSYS;
71
Hans de Goedecebc5b72015-05-10 14:10:21 +020072 err = ops->control(bus, udev, pipe, buffer, length, setup);
73 if (setup->request == USB_REQ_SET_FEATURE &&
74 setup->requesttype == USB_RT_PORT &&
75 setup->value == cpu_to_le16(USB_PORT_FEAT_RESET) &&
76 err == -ENXIO) {
77 /* Device handed over to companion after port reset */
78 uc_priv->companion_device_count++;
79 }
80
81 return err;
Simon Glass9b82eeb2015-03-25 12:21:59 -060082}
83
84int submit_bulk_msg(struct usb_device *udev, unsigned long pipe, void *buffer,
85 int length)
86{
87 struct udevice *bus = udev->controller_dev;
88 struct dm_usb_ops *ops = usb_get_ops(bus);
89
90 if (!ops->bulk)
91 return -ENOSYS;
92
93 return ops->bulk(bus, udev, pipe, buffer, length);
94}
95
Hans de Goede0a7fa272015-05-10 14:10:18 +020096struct int_queue *create_int_queue(struct usb_device *udev,
97 unsigned long pipe, int queuesize, int elementsize,
98 void *buffer, int interval)
99{
100 struct udevice *bus = udev->controller_dev;
101 struct dm_usb_ops *ops = usb_get_ops(bus);
102
103 if (!ops->create_int_queue)
104 return NULL;
105
106 return ops->create_int_queue(bus, udev, pipe, queuesize, elementsize,
107 buffer, interval);
108}
109
110void *poll_int_queue(struct usb_device *udev, struct int_queue *queue)
111{
112 struct udevice *bus = udev->controller_dev;
113 struct dm_usb_ops *ops = usb_get_ops(bus);
114
115 if (!ops->poll_int_queue)
116 return NULL;
117
118 return ops->poll_int_queue(bus, udev, queue);
119}
120
121int destroy_int_queue(struct usb_device *udev, struct int_queue *queue)
122{
123 struct udevice *bus = udev->controller_dev;
124 struct dm_usb_ops *ops = usb_get_ops(bus);
125
126 if (!ops->destroy_int_queue)
127 return -ENOSYS;
128
129 return ops->destroy_int_queue(bus, udev, queue);
130}
131
Simon Glass9b82eeb2015-03-25 12:21:59 -0600132int usb_alloc_device(struct usb_device *udev)
133{
134 struct udevice *bus = udev->controller_dev;
135 struct dm_usb_ops *ops = usb_get_ops(bus);
136
137 /* This is only requird by some controllers - current XHCI */
138 if (!ops->alloc_device)
139 return 0;
140
141 return ops->alloc_device(bus, udev);
142}
143
Hans de Goedea9e41f82015-06-17 21:33:52 +0200144int usb_reset_root_port(struct usb_device *udev)
145{
146 struct udevice *bus = udev->controller_dev;
147 struct dm_usb_ops *ops = usb_get_ops(bus);
148
149 if (!ops->reset_root_port)
150 return -ENOSYS;
151
152 return ops->reset_root_port(bus, udev);
153}
154
Bin Meng1f31f5a2017-07-19 21:51:17 +0800155int usb_update_hub_device(struct usb_device *udev)
156{
157 struct udevice *bus = udev->controller_dev;
158 struct dm_usb_ops *ops = usb_get_ops(bus);
159
160 if (!ops->update_hub_device)
161 return -ENOSYS;
162
163 return ops->update_hub_device(bus, udev);
164}
165
Bin Meng6840a342017-09-07 06:13:17 -0700166int usb_get_max_xfer_size(struct usb_device *udev, size_t *size)
167{
168 struct udevice *bus = udev->controller_dev;
169 struct dm_usb_ops *ops = usb_get_ops(bus);
170
171 if (!ops->get_max_xfer_size)
172 return -ENOSYS;
173
174 return ops->get_max_xfer_size(bus, size);
175}
176
Jerome Forissier219dade2025-04-18 16:09:41 +0200177#if CONFIG_IS_ENABLED(UTHREAD)
178static struct uthread_mutex mutex = UTHREAD_MUTEX_INITIALIZER;
179#endif
180
Simon Glass9b82eeb2015-03-25 12:21:59 -0600181int usb_stop(void)
182{
183 struct udevice *bus;
Bin Meng669ad842017-10-01 06:19:42 -0700184 struct udevice *rh;
Simon Glass9b82eeb2015-03-25 12:21:59 -0600185 struct uclass *uc;
Hans de Goedecebc5b72015-05-10 14:10:21 +0200186 struct usb_uclass_priv *uc_priv;
Simon Glass9b82eeb2015-03-25 12:21:59 -0600187 int err = 0, ret;
188
Jerome Forissier219dade2025-04-18 16:09:41 +0200189 uthread_mutex_lock(&mutex);
190
Simon Glass9b82eeb2015-03-25 12:21:59 -0600191 /* De-activate any devices that have been activated */
192 ret = uclass_get(UCLASS_USB, &uc);
Jerome Forissier219dade2025-04-18 16:09:41 +0200193 if (ret) {
194 uthread_mutex_unlock(&mutex);
Simon Glass9b82eeb2015-03-25 12:21:59 -0600195 return ret;
Jerome Forissier219dade2025-04-18 16:09:41 +0200196 }
Hans de Goedecebc5b72015-05-10 14:10:21 +0200197
Simon Glass95588622020-12-22 19:30:28 -0700198 uc_priv = uclass_get_priv(uc);
Hans de Goedecebc5b72015-05-10 14:10:21 +0200199
Simon Glass9b82eeb2015-03-25 12:21:59 -0600200 uclass_foreach_dev(bus, uc) {
Stefan Roese80b5bc92017-03-20 12:51:48 +0100201 ret = device_remove(bus, DM_REMOVE_NORMAL);
Simon Glass9b82eeb2015-03-25 12:21:59 -0600202 if (ret && !err)
203 err = ret;
Bin Meng669ad842017-10-01 06:19:42 -0700204
205 /* Locate root hub device */
206 device_find_first_child(bus, &rh);
207 if (rh) {
208 /*
209 * All USB devices are children of root hub.
210 * Unbinding root hub will unbind all of its children.
211 */
212 ret = device_unbind(rh);
213 if (ret && !err)
214 err = ret;
215 }
Simon Glass9b82eeb2015-03-25 12:21:59 -0600216 }
Bin Mengdadc4c02017-10-01 06:19:43 -0700217
Paul Kocialkowskid449dd32015-08-04 17:04:12 +0200218#ifdef CONFIG_USB_STORAGE
Simon Glass9b82eeb2015-03-25 12:21:59 -0600219 usb_stor_reset();
Paul Kocialkowskid449dd32015-08-04 17:04:12 +0200220#endif
Simon Glass62b03cc2023-09-20 07:29:49 -0600221 if (CONFIG_IS_ENABLED(BOOTSTD)) {
222 int ret;
223
224 ret = bootdev_unhunt(UCLASS_USB);
225 if (IS_ENABLED(CONFIG_BOOTSTD_FULL) && ret && ret != -EALREADY)
226 printf("failed to unhunt USB (err=%dE)\n", ret);
227 }
Hans de Goedecebc5b72015-05-10 14:10:21 +0200228 uc_priv->companion_device_count = 0;
Simon Glass9b82eeb2015-03-25 12:21:59 -0600229 usb_started = 0;
230
Jerome Forissier219dade2025-04-18 16:09:41 +0200231 uthread_mutex_unlock(&mutex);
232
Simon Glass9b82eeb2015-03-25 12:21:59 -0600233 return err;
234}
235
Jerome Forissier219dade2025-04-18 16:09:41 +0200236static void _usb_scan_bus(void *arg)
Simon Glass9b82eeb2015-03-25 12:21:59 -0600237{
Jerome Forissier219dade2025-04-18 16:09:41 +0200238 struct udevice *bus = (struct udevice *)arg;
Simon Glass9b82eeb2015-03-25 12:21:59 -0600239 struct usb_bus_priv *priv;
240 struct udevice *dev;
241 int ret;
242
243 priv = dev_get_uclass_priv(bus);
244
Simon Glass9b82eeb2015-03-25 12:21:59 -0600245 ret = usb_scan_device(bus, 0, USB_SPEED_FULL, &dev);
246 if (ret)
Jerome Forissier219dade2025-04-18 16:09:41 +0200247 printf("Scanning bus %s failed, error %d\n", bus->name, ret);
Simon Glass9b82eeb2015-03-25 12:21:59 -0600248}
249
Simon Glass35787d22015-11-08 23:48:00 -0700250static void remove_inactive_children(struct uclass *uc, struct udevice *bus)
251{
252 uclass_foreach_dev(bus, uc) {
253 struct udevice *dev, *next;
254
255 if (!device_active(bus))
256 continue;
257 device_foreach_child_safe(dev, next, bus) {
258 if (!device_active(dev))
259 device_unbind(dev);
260 }
261 }
262}
263
Fabrice Gasnier7785aee2023-09-01 11:52:01 +0200264static int usb_probe_companion(struct udevice *bus)
265{
266 struct udevice *companion_dev;
267 int ret;
268
269 /*
270 * Enforce optional companion controller is marked as such in order to
271 * 1st scan the primary controller, before the companion controller
272 * (ownership is given to companion when low or full speed devices
273 * have been detected).
274 */
275 ret = uclass_get_device_by_phandle(UCLASS_USB, bus, "companion", &companion_dev);
276 if (!ret) {
277 struct usb_bus_priv *companion_bus_priv;
278
279 debug("%s is the companion of %s\n", companion_dev->name, bus->name);
280 companion_bus_priv = dev_get_uclass_priv(companion_dev);
281 companion_bus_priv->companion = true;
282 } else if (ret && ret != -ENOENT && ret != -ENODEV) {
283 /*
284 * Treat everything else than no companion or disabled
285 * companion as an error. (It may not be enabled on boards
286 * that have a High-Speed HUB to handle FS and LS traffic).
287 */
288 printf("Failed to get companion (ret=%d)\n", ret);
289 return ret;
290 }
291
292 return 0;
293}
294
Jerome Forissier219dade2025-04-18 16:09:41 +0200295static void _usb_init_bus(void *arg)
Jerome Forissier7530dc72025-04-18 16:09:40 +0200296{
Jerome Forissier219dade2025-04-18 16:09:41 +0200297 struct udevice *bus = (struct udevice *)arg;
Jerome Forissier7530dc72025-04-18 16:09:40 +0200298 int ret;
299
300 /* init low_level USB */
Jerome Forissier7530dc72025-04-18 16:09:40 +0200301
302 /*
303 * For Sandbox, we need scan the device tree each time when we
304 * start the USB stack, in order to re-create the emulated USB
305 * devices and bind drivers for them before we actually do the
306 * driver probe.
307 *
308 * For USB onboard HUB, we need to do some non-trivial init
309 * like enabling a power regulator, before enumeration.
310 */
311 if (IS_ENABLED(CONFIG_SANDBOX) ||
312 IS_ENABLED(CONFIG_USB_ONBOARD_HUB)) {
313 ret = dm_scan_fdt_dev(bus);
314 if (ret) {
Jerome Forissier219dade2025-04-18 16:09:41 +0200315 printf("Bus %s: USB device scan from fdt failed (%d)\n",
316 bus->name, ret);
Jerome Forissier7530dc72025-04-18 16:09:40 +0200317 return;
318 }
319 }
320
321 ret = device_probe(bus);
322 if (ret == -ENODEV) { /* No such device. */
Jerome Forissier219dade2025-04-18 16:09:41 +0200323 printf("Bus %s: Port not available.\n", bus->name);
Jerome Forissier7530dc72025-04-18 16:09:40 +0200324 return;
325 }
326
327 if (ret) { /* Other error. */
Jerome Forissier219dade2025-04-18 16:09:41 +0200328 printf("Bus %s: probe failed, error %d\n", bus->name, ret);
Jerome Forissier7530dc72025-04-18 16:09:40 +0200329 return;
330 }
331
332 usb_probe_companion(bus);
333}
334
Jerome Forissier219dade2025-04-18 16:09:41 +0200335static int nthr;
336static int grp_id;
337
338static void usb_init_bus(struct udevice *bus)
339{
340 if (!grp_id)
341 grp_id = uthread_grp_new_id();
342 if (!uthread_create(NULL, _usb_init_bus, (void *)bus, 0, grp_id))
343 nthr++;
344}
345
346static void usb_scan_bus(struct udevice *bus, bool recurse)
347{
348 if (!grp_id)
349 grp_id = uthread_grp_new_id();
350 if (!uthread_create(NULL, _usb_scan_bus, (void *)bus, 0, grp_id))
351 nthr++;
352}
353
354static void usb_report_devices(struct uclass *uc)
355{
356 struct usb_bus_priv *priv;
357 struct udevice *bus;
358
359 uclass_foreach_dev(bus, uc) {
360 if (!device_active(bus))
361 continue;
362 priv = dev_get_uclass_priv(bus);
363 printf("Bus %s: ", bus->name);
364 if (priv->next_addr == 0)
365 printf("No USB Device found\n");
366 else
367 printf("%d USB Device(s) found\n", priv->next_addr);
368 }
369}
370
371static void run_threads(void)
372{
373#if CONFIG_IS_ENABLED(UTHREAD)
374 if (!nthr)
375 return;
376 while (!uthread_grp_done(grp_id))
377 uthread_schedule();
378 nthr = 0;
379 grp_id = 0;
380#endif
381}
382
Simon Glass9b82eeb2015-03-25 12:21:59 -0600383int usb_init(void)
384{
385 int controllers_initialized = 0;
Jerome Forissier219dade2025-04-18 16:09:41 +0200386 unsigned long t0 = timer_get_us();
Hans de Goedecebc5b72015-05-10 14:10:21 +0200387 struct usb_uclass_priv *uc_priv;
Hans de Goede59a0dfc2015-05-10 14:10:20 +0200388 struct usb_bus_priv *priv;
Simon Glass9b82eeb2015-03-25 12:21:59 -0600389 struct udevice *bus;
390 struct uclass *uc;
Simon Glass9b82eeb2015-03-25 12:21:59 -0600391 int ret;
392
Jerome Forissier219dade2025-04-18 16:09:41 +0200393 uthread_mutex_lock(&mutex);
394
395 if (usb_started) {
396 ret = 0;
397 goto out;
398 }
399
Simon Glass9b82eeb2015-03-25 12:21:59 -0600400 asynch_allowed = 1;
Simon Glass9b82eeb2015-03-25 12:21:59 -0600401
402 ret = uclass_get(UCLASS_USB, &uc);
403 if (ret)
Jerome Forissier219dade2025-04-18 16:09:41 +0200404 goto out;
Simon Glass9b82eeb2015-03-25 12:21:59 -0600405
Simon Glass95588622020-12-22 19:30:28 -0700406 uc_priv = uclass_get_priv(uc);
Hans de Goedecebc5b72015-05-10 14:10:21 +0200407
Simon Glass9b82eeb2015-03-25 12:21:59 -0600408 uclass_foreach_dev(bus, uc) {
Jerome Forissier7530dc72025-04-18 16:09:40 +0200409 usb_init_bus(bus);
Simon Glass9b82eeb2015-03-25 12:21:59 -0600410 }
411
Jerome Forissier219dade2025-04-18 16:09:41 +0200412 if (CONFIG_IS_ENABLED(UTHREAD))
413 run_threads();
414
Jerome Forissier7530dc72025-04-18 16:09:40 +0200415 usb_started = true;
416
Hans de Goede59a0dfc2015-05-10 14:10:20 +0200417 /*
418 * lowlevel init done, now scan the bus for devices i.e. search HUBs
419 * and configure them, first scan primary controllers.
420 */
421 uclass_foreach_dev(bus, uc) {
422 if (!device_active(bus))
423 continue;
424
Jerome Forissier7530dc72025-04-18 16:09:40 +0200425 controllers_initialized++;
426
Hans de Goede59a0dfc2015-05-10 14:10:20 +0200427 priv = dev_get_uclass_priv(bus);
428 if (!priv->companion)
429 usb_scan_bus(bus, true);
430 }
431
Jerome Forissier219dade2025-04-18 16:09:41 +0200432 if (CONFIG_IS_ENABLED(UTHREAD))
433 run_threads();
434
Hans de Goede59a0dfc2015-05-10 14:10:20 +0200435 /*
436 * Now that the primary controllers have been scanned and have handed
437 * over any devices they do not understand to their companions, scan
Hans de Goedecebc5b72015-05-10 14:10:21 +0200438 * the companions if necessary.
Hans de Goede59a0dfc2015-05-10 14:10:20 +0200439 */
Hans de Goedecebc5b72015-05-10 14:10:21 +0200440 if (uc_priv->companion_device_count) {
441 uclass_foreach_dev(bus, uc) {
442 if (!device_active(bus))
443 continue;
Hans de Goede59a0dfc2015-05-10 14:10:20 +0200444
Hans de Goedecebc5b72015-05-10 14:10:21 +0200445 priv = dev_get_uclass_priv(bus);
446 if (priv->companion)
447 usb_scan_bus(bus, true);
448 }
Hans de Goede59a0dfc2015-05-10 14:10:20 +0200449 }
450
Jerome Forissier219dade2025-04-18 16:09:41 +0200451 if (CONFIG_IS_ENABLED(UTHREAD))
452 run_threads();
453
454 usb_report_devices(uc);
Simon Glass35787d22015-11-08 23:48:00 -0700455
456 /* Remove any devices that were not found on this scan */
457 remove_inactive_children(uc, bus);
458
459 ret = uclass_get(UCLASS_USB_HUB, &uc);
460 if (ret)
Jerome Forissier219dade2025-04-18 16:09:41 +0200461 goto out;
462
Simon Glass35787d22015-11-08 23:48:00 -0700463 remove_inactive_children(uc, bus);
464
Simon Glass9b82eeb2015-03-25 12:21:59 -0600465 /* if we were not able to find at least one working bus, bail out */
Ismael Luceno Cortesf58affe2019-03-19 09:19:44 +0000466 if (controllers_initialized == 0)
Heinrich Schuchardtbf2d56e2024-06-18 21:07:29 +0200467 printf("No USB controllers found\n");
Simon Glass9b82eeb2015-03-25 12:21:59 -0600468
Jerome Forissier219dade2025-04-18 16:09:41 +0200469 debug("USB initialized in %ld ms\n",
470 (timer_get_us() - t0) / 1000);
471
472 uthread_mutex_unlock(&mutex);
473
Simon Glassac746082023-07-30 11:15:12 -0600474 return usb_started ? 0 : -ENOENT;
Jerome Forissier219dade2025-04-18 16:09:41 +0200475out:
476 uthread_mutex_unlock(&mutex);
477
478 return ret;
Simon Glass9b82eeb2015-03-25 12:21:59 -0600479}
480
Simon Glass444b1e02015-03-25 12:22:32 -0600481int usb_setup_ehci_gadget(struct ehci_ctrl **ctlrp)
482{
Simon Glassb75b15b2020-12-03 16:55:23 -0700483 struct usb_plat *plat;
Simon Glass444b1e02015-03-25 12:22:32 -0600484 struct udevice *dev;
485 int ret;
486
487 /* Find the old device and remove it */
Sean Andersone7665702021-11-05 12:52:55 -0400488 ret = uclass_find_first_device(UCLASS_USB, &dev);
Simon Glass444b1e02015-03-25 12:22:32 -0600489 if (ret)
490 return ret;
Stefan Roese80b5bc92017-03-20 12:51:48 +0100491 ret = device_remove(dev, DM_REMOVE_NORMAL);
Simon Glass444b1e02015-03-25 12:22:32 -0600492 if (ret)
493 return ret;
494
Simon Glassfa20e932020-12-03 16:55:20 -0700495 plat = dev_get_plat(dev);
Simon Glass444b1e02015-03-25 12:22:32 -0600496 plat->init_type = USB_INIT_DEVICE;
497 ret = device_probe(dev);
498 if (ret)
499 return ret;
500 *ctlrp = dev_get_priv(dev);
501
502 return 0;
503}
504
Ye Li68801eb2020-06-29 10:12:59 +0800505int usb_remove_ehci_gadget(struct ehci_ctrl **ctlrp)
506{
507 struct udevice *dev;
508 int ret;
509
510 /* Find the old device and remove it */
Sean Andersone7665702021-11-05 12:52:55 -0400511 ret = uclass_find_first_device(UCLASS_USB, &dev);
Ye Li68801eb2020-06-29 10:12:59 +0800512 if (ret)
513 return ret;
514 ret = device_remove(dev, DM_REMOVE_NORMAL);
515 if (ret)
516 return ret;
517
518 *ctlrp = NULL;
519
520 return 0;
521}
522
Simon Glassfc03a552015-03-25 12:22:30 -0600523/* returns 0 if no match, 1 if match */
Masahiro Yamada6d8e4332017-06-22 16:35:14 +0900524static int usb_match_device(const struct usb_device_descriptor *desc,
525 const struct usb_device_id *id)
Simon Glassfc03a552015-03-25 12:22:30 -0600526{
527 if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
Stefan Roese77c83ac2020-07-21 10:46:04 +0200528 id->idVendor != desc->idVendor)
Simon Glassfc03a552015-03-25 12:22:30 -0600529 return 0;
530
531 if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
Stefan Roese77c83ac2020-07-21 10:46:04 +0200532 id->idProduct != desc->idProduct)
Simon Glassfc03a552015-03-25 12:22:30 -0600533 return 0;
534
535 /* No need to test id->bcdDevice_lo != 0, since 0 is never
536 greater than any unsigned number. */
537 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) &&
Stefan Roese77c83ac2020-07-21 10:46:04 +0200538 (id->bcdDevice_lo > desc->bcdDevice))
Simon Glassfc03a552015-03-25 12:22:30 -0600539 return 0;
540
541 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) &&
Stefan Roese77c83ac2020-07-21 10:46:04 +0200542 (id->bcdDevice_hi < desc->bcdDevice))
Simon Glassfc03a552015-03-25 12:22:30 -0600543 return 0;
544
545 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) &&
546 (id->bDeviceClass != desc->bDeviceClass))
547 return 0;
548
549 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) &&
550 (id->bDeviceSubClass != desc->bDeviceSubClass))
551 return 0;
552
553 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) &&
554 (id->bDeviceProtocol != desc->bDeviceProtocol))
555 return 0;
556
557 return 1;
558}
559
560/* returns 0 if no match, 1 if match */
Masahiro Yamada6d8e4332017-06-22 16:35:14 +0900561static int usb_match_one_id_intf(const struct usb_device_descriptor *desc,
562 const struct usb_interface_descriptor *int_desc,
563 const struct usb_device_id *id)
Simon Glassfc03a552015-03-25 12:22:30 -0600564{
565 /* The interface class, subclass, protocol and number should never be
566 * checked for a match if the device class is Vendor Specific,
567 * unless the match record specifies the Vendor ID. */
568 if (desc->bDeviceClass == USB_CLASS_VENDOR_SPEC &&
569 !(id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
570 (id->match_flags & (USB_DEVICE_ID_MATCH_INT_CLASS |
571 USB_DEVICE_ID_MATCH_INT_SUBCLASS |
572 USB_DEVICE_ID_MATCH_INT_PROTOCOL |
573 USB_DEVICE_ID_MATCH_INT_NUMBER)))
574 return 0;
575
576 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) &&
577 (id->bInterfaceClass != int_desc->bInterfaceClass))
578 return 0;
579
580 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) &&
581 (id->bInterfaceSubClass != int_desc->bInterfaceSubClass))
582 return 0;
583
584 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) &&
585 (id->bInterfaceProtocol != int_desc->bInterfaceProtocol))
586 return 0;
587
588 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_NUMBER) &&
589 (id->bInterfaceNumber != int_desc->bInterfaceNumber))
590 return 0;
591
592 return 1;
593}
594
595/* returns 0 if no match, 1 if match */
Masahiro Yamada6d8e4332017-06-22 16:35:14 +0900596static int usb_match_one_id(struct usb_device_descriptor *desc,
597 struct usb_interface_descriptor *int_desc,
598 const struct usb_device_id *id)
Simon Glassfc03a552015-03-25 12:22:30 -0600599{
600 if (!usb_match_device(desc, id))
601 return 0;
602
603 return usb_match_one_id_intf(desc, int_desc, id);
604}
605
Michael Walle7c961322020-06-02 01:47:07 +0200606static ofnode usb_get_ofnode(struct udevice *hub, int port)
607{
608 ofnode node;
609 u32 reg;
610
Simon Glass07c17772020-12-19 10:40:12 -0700611 if (!dev_has_ofnode(hub))
Michael Walle7c961322020-06-02 01:47:07 +0200612 return ofnode_null();
613
614 /*
615 * The USB controller and its USB hub are two different udevices,
616 * but the device tree has only one node for both. Thus we are
617 * assigning this node to both udevices.
618 * If port is zero, the controller scans its root hub, thus we
619 * are using the same ofnode as the controller here.
620 */
621 if (!port)
622 return dev_ofnode(hub);
623
624 ofnode_for_each_subnode(node, dev_ofnode(hub)) {
625 if (ofnode_read_u32(node, "reg", &reg))
626 continue;
627
628 if (reg == port)
629 return node;
630 }
631
632 return ofnode_null();
633}
634
Simon Glassfc03a552015-03-25 12:22:30 -0600635/**
636 * usb_find_and_bind_driver() - Find and bind the right USB driver
637 *
638 * This only looks at certain fields in the descriptor.
639 */
640static int usb_find_and_bind_driver(struct udevice *parent,
641 struct usb_device_descriptor *desc,
642 struct usb_interface_descriptor *iface,
Michael Walle7c961322020-06-02 01:47:07 +0200643 int bus_seq, int devnum, int port,
Simon Glassfc03a552015-03-25 12:22:30 -0600644 struct udevice **devp)
645{
646 struct usb_driver_entry *start, *entry;
647 int n_ents;
648 int ret;
Marek Vasut6e76b0f2022-11-26 13:57:53 +0100649 char name[34], *str;
Michael Walle7c961322020-06-02 01:47:07 +0200650 ofnode node = usb_get_ofnode(parent, port);
Simon Glassfc03a552015-03-25 12:22:30 -0600651
652 *devp = NULL;
653 debug("%s: Searching for driver\n", __func__);
654 start = ll_entry_start(struct usb_driver_entry, usb_driver_entry);
655 n_ents = ll_entry_count(struct usb_driver_entry, usb_driver_entry);
656 for (entry = start; entry != start + n_ents; entry++) {
657 const struct usb_device_id *id;
658 struct udevice *dev;
659 const struct driver *drv;
Simon Glassb75b15b2020-12-03 16:55:23 -0700660 struct usb_dev_plat *plat;
Simon Glassfc03a552015-03-25 12:22:30 -0600661
662 for (id = entry->match; id->match_flags; id++) {
663 if (!usb_match_one_id(desc, iface, id))
664 continue;
665
666 drv = entry->driver;
667 /*
668 * We could pass the descriptor to the driver as
Simon Glass71fa5b42020-12-03 16:55:18 -0700669 * plat (instead of NULL) and allow its bind()
Simon Glassfc03a552015-03-25 12:22:30 -0600670 * method to return -ENOENT if it doesn't support this
671 * device. That way we could continue the search to
672 * find another driver. For now this doesn't seem
673 * necesssary, so just bind the first match.
674 */
Simon Glass884870f2020-11-28 17:50:01 -0700675 ret = device_bind(parent, drv, drv->name, NULL, node,
676 &dev);
Simon Glassfc03a552015-03-25 12:22:30 -0600677 if (ret)
678 goto error;
679 debug("%s: Match found: %s\n", __func__, drv->name);
680 dev->driver_data = id->driver_info;
Simon Glass71fa5b42020-12-03 16:55:18 -0700681 plat = dev_get_parent_plat(dev);
Simon Glassfc03a552015-03-25 12:22:30 -0600682 plat->id = *id;
683 *devp = dev;
684 return 0;
685 }
686 }
687
Simon Glassc79173e2015-03-25 12:22:31 -0600688 /* Bind a generic driver so that the device can be used */
689 snprintf(name, sizeof(name), "generic_bus_%x_dev_%x", bus_seq, devnum);
690 str = strdup(name);
691 if (!str)
692 return -ENOMEM;
693 ret = device_bind_driver(parent, "usb_dev_generic_drv", str, devp);
Simon Glass4d2c3592023-01-17 10:47:35 -0700694 if (!ret)
695 device_set_name_alloced(*devp);
Simon Glassc79173e2015-03-25 12:22:31 -0600696
Simon Glassfc03a552015-03-25 12:22:30 -0600697error:
698 debug("%s: No match found: %d\n", __func__, ret);
699 return ret;
700}
701
702/**
Simon Glassd43d7e92015-11-08 23:47:56 -0700703 * usb_find_child() - Find an existing device which matches our needs
704 *
705 *
Simon Glassfc03a552015-03-25 12:22:30 -0600706 */
Simon Glassd43d7e92015-11-08 23:47:56 -0700707static int usb_find_child(struct udevice *parent,
708 struct usb_device_descriptor *desc,
709 struct usb_interface_descriptor *iface,
710 struct udevice **devp)
Simon Glassfc03a552015-03-25 12:22:30 -0600711{
712 struct udevice *dev;
713
714 *devp = NULL;
715 for (device_find_first_child(parent, &dev);
716 dev;
717 device_find_next_child(&dev)) {
Simon Glassb75b15b2020-12-03 16:55:23 -0700718 struct usb_dev_plat *plat = dev_get_parent_plat(dev);
Simon Glassfc03a552015-03-25 12:22:30 -0600719
720 /* If this device is already in use, skip it */
721 if (device_active(dev))
722 continue;
723 debug(" %s: name='%s', plat=%d, desc=%d\n", __func__,
724 dev->name, plat->id.bDeviceClass, desc->bDeviceClass);
725 if (usb_match_one_id(desc, iface, &plat->id)) {
726 *devp = dev;
727 return 0;
728 }
729 }
Simon Glassd43d7e92015-11-08 23:47:56 -0700730
Simon Glassfc03a552015-03-25 12:22:30 -0600731 return -ENOENT;
732}
733
Simon Glass9b82eeb2015-03-25 12:21:59 -0600734int usb_scan_device(struct udevice *parent, int port,
735 enum usb_device_speed speed, struct udevice **devp)
736{
737 struct udevice *dev;
738 bool created = false;
Simon Glassb75b15b2020-12-03 16:55:23 -0700739 struct usb_dev_plat *plat;
Simon Glass9b82eeb2015-03-25 12:21:59 -0600740 struct usb_bus_priv *priv;
741 struct usb_device *parent_udev;
742 int ret;
743 ALLOC_CACHE_ALIGN_BUFFER(struct usb_device, udev, 1);
744 struct usb_interface_descriptor *iface = &udev->config.if_desc[0].desc;
745
746 *devp = NULL;
747 memset(udev, '\0', sizeof(*udev));
Hans de Goede3b5ddd32015-05-05 11:54:31 +0200748 udev->controller_dev = usb_get_bus(parent);
Simon Glass9b82eeb2015-03-25 12:21:59 -0600749 priv = dev_get_uclass_priv(udev->controller_dev);
750
751 /*
752 * Somewhat nasty, this. We create a local device and use the normal
753 * USB stack to read its descriptor. Then we know what type of device
754 * to create for real.
755 *
756 * udev->dev is set to the parent, since we don't have a real device
757 * yet. The USB stack should not access udev.dev anyway, except perhaps
758 * to find the controller, and the controller will either be @parent,
759 * or some parent of @parent.
760 *
761 * Another option might be to create the device as a generic USB
762 * device, then morph it into the correct one when we know what it
763 * should be. This means that a generic USB device would morph into
764 * a network controller, or a USB flash stick, for example. However,
765 * we don't support such morphing and it isn't clear that it would
766 * be easy to do.
767 *
768 * Yet another option is to split out the USB stack parts of udev
769 * into something like a 'struct urb' (as Linux does) which can exist
770 * independently of any device. This feels cleaner, but calls for quite
771 * a big change to the USB stack.
772 *
773 * For now, the approach is to set up an empty udev, read its
774 * descriptor and assign it an address, then bind a real device and
775 * stash the resulting information into the device's parent
776 * platform data. Then when we probe it, usb_child_pre_probe() is called
777 * and it will pull the information out of the stash.
778 */
779 udev->dev = parent;
780 udev->speed = speed;
781 udev->devnum = priv->next_addr + 1;
782 udev->portnr = port;
783 debug("Calling usb_setup_device(), portnr=%d\n", udev->portnr);
784 parent_udev = device_get_uclass_id(parent) == UCLASS_USB_HUB ?
Simon Glassde44acf2015-09-28 23:32:01 -0600785 dev_get_parent_priv(parent) : NULL;
Hans de Goede778dc1c2015-06-17 21:33:46 +0200786 ret = usb_setup_device(udev, priv->desc_before_addr, parent_udev);
Simon Glass9b82eeb2015-03-25 12:21:59 -0600787 debug("read_descriptor for '%s': ret=%d\n", parent->name, ret);
788 if (ret)
789 return ret;
Simon Glassd43d7e92015-11-08 23:47:56 -0700790 ret = usb_find_child(parent, &udev->descriptor, iface, &dev);
791 debug("** usb_find_child returns %d\n", ret);
Simon Glassfc03a552015-03-25 12:22:30 -0600792 if (ret) {
793 if (ret != -ENOENT)
794 return ret;
Michael Walle7c961322020-06-02 01:47:07 +0200795 ret = usb_find_and_bind_driver(parent, &udev->descriptor,
796 iface,
Simon Glass75e534b2020-12-16 21:20:07 -0700797 dev_seq(udev->controller_dev),
Michael Walle7c961322020-06-02 01:47:07 +0200798 udev->devnum, port, &dev);
Simon Glassfc03a552015-03-25 12:22:30 -0600799 if (ret)
800 return ret;
801 created = true;
802 }
Simon Glass71fa5b42020-12-03 16:55:18 -0700803 plat = dev_get_parent_plat(dev);
Simon Glassfc03a552015-03-25 12:22:30 -0600804 debug("%s: Probing '%s', plat=%p\n", __func__, dev->name, plat);
805 plat->devnum = udev->devnum;
Hans de Goede8a0b4c22015-05-05 11:54:32 +0200806 plat->udev = udev;
Simon Glassfc03a552015-03-25 12:22:30 -0600807 priv->next_addr++;
808 ret = device_probe(dev);
809 if (ret) {
810 debug("%s: Device '%s' probe failed\n", __func__, dev->name);
811 priv->next_addr--;
812 if (created)
813 device_unbind(dev);
814 return ret;
815 }
816 *devp = dev;
Simon Glass9b82eeb2015-03-25 12:21:59 -0600817
Simon Glassfc03a552015-03-25 12:22:30 -0600818 return 0;
Simon Glass9b82eeb2015-03-25 12:21:59 -0600819}
820
Simon Glassfbb14942015-05-13 07:02:23 -0600821/*
822 * Detect if a USB device has been plugged or unplugged.
823 */
824int usb_detect_change(void)
825{
826 struct udevice *hub;
827 struct uclass *uc;
828 int change = 0;
829 int ret;
830
831 ret = uclass_get(UCLASS_USB_HUB, &uc);
832 if (ret)
833 return ret;
834
835 uclass_foreach_dev(hub, uc) {
836 struct usb_device *udev;
837 struct udevice *dev;
838
839 if (!device_active(hub))
840 continue;
841 for (device_find_first_child(hub, &dev);
842 dev;
843 device_find_next_child(&dev)) {
844 struct usb_port_status status;
845
846 if (!device_active(dev))
847 continue;
848
Simon Glassde44acf2015-09-28 23:32:01 -0600849 udev = dev_get_parent_priv(dev);
Simon Glassfbb14942015-05-13 07:02:23 -0600850 if (usb_get_port_status(udev, udev->portnr, &status)
851 < 0)
852 /* USB request failed */
853 continue;
854
855 if (le16_to_cpu(status.wPortChange) &
856 USB_PORT_STAT_C_CONNECTION)
857 change++;
858 }
859 }
860
861 return change;
862}
863
Masahiro Yamada6d8e4332017-06-22 16:35:14 +0900864static int usb_child_post_bind(struct udevice *dev)
Simon Glass9b82eeb2015-03-25 12:21:59 -0600865{
Simon Glassb75b15b2020-12-03 16:55:23 -0700866 struct usb_dev_plat *plat = dev_get_parent_plat(dev);
Simon Glass9b82eeb2015-03-25 12:21:59 -0600867 int val;
868
Simon Glassf1d50f72020-12-19 10:40:13 -0700869 if (!dev_has_ofnode(dev))
Simon Glass9b82eeb2015-03-25 12:21:59 -0600870 return 0;
871
872 /* We only support matching a few things */
Simon Glass6deb8e22017-05-18 20:09:38 -0600873 val = dev_read_u32_default(dev, "usb,device-class", -1);
Simon Glass9b82eeb2015-03-25 12:21:59 -0600874 if (val != -1) {
875 plat->id.match_flags |= USB_DEVICE_ID_MATCH_DEV_CLASS;
876 plat->id.bDeviceClass = val;
877 }
Simon Glass6deb8e22017-05-18 20:09:38 -0600878 val = dev_read_u32_default(dev, "usb,interface-class", -1);
Simon Glass9b82eeb2015-03-25 12:21:59 -0600879 if (val != -1) {
880 plat->id.match_flags |= USB_DEVICE_ID_MATCH_INT_CLASS;
881 plat->id.bInterfaceClass = val;
882 }
883
884 return 0;
885}
886
Hans de Goede3b5ddd32015-05-05 11:54:31 +0200887struct udevice *usb_get_bus(struct udevice *dev)
Simon Glass9b82eeb2015-03-25 12:21:59 -0600888{
889 struct udevice *bus;
890
Simon Glass9b82eeb2015-03-25 12:21:59 -0600891 for (bus = dev; bus && device_get_uclass_id(bus) != UCLASS_USB; )
892 bus = bus->parent;
893 if (!bus) {
894 /* By design this cannot happen */
895 assert(bus);
896 debug("USB HUB '%s' does not have a controller\n", dev->name);
Simon Glass9b82eeb2015-03-25 12:21:59 -0600897 }
Simon Glass9b82eeb2015-03-25 12:21:59 -0600898
Hans de Goede3b5ddd32015-05-05 11:54:31 +0200899 return bus;
Simon Glass9b82eeb2015-03-25 12:21:59 -0600900}
901
902int usb_child_pre_probe(struct udevice *dev)
903{
Simon Glassde44acf2015-09-28 23:32:01 -0600904 struct usb_device *udev = dev_get_parent_priv(dev);
Simon Glassb75b15b2020-12-03 16:55:23 -0700905 struct usb_dev_plat *plat = dev_get_parent_plat(dev);
Simon Glass9b82eeb2015-03-25 12:21:59 -0600906 int ret;
907
Hans de Goede8a0b4c22015-05-05 11:54:32 +0200908 if (plat->udev) {
909 /*
910 * Copy over all the values set in the on stack struct
911 * usb_device in usb_scan_device() to our final struct
912 * usb_device for this dev.
913 */
914 *udev = *(plat->udev);
915 /* And clear plat->udev as it will not be valid for long */
916 plat->udev = NULL;
917 udev->dev = dev;
918 } else {
919 /*
920 * This happens with devices which are explicitly bound
921 * instead of being discovered through usb_scan_device()
922 * such as sandbox emul devices.
923 */
924 udev->dev = dev;
925 udev->controller_dev = usb_get_bus(dev);
926 udev->devnum = plat->devnum;
Simon Glass9b82eeb2015-03-25 12:21:59 -0600927
Hans de Goede8a0b4c22015-05-05 11:54:32 +0200928 /*
929 * udev did not go through usb_scan_device(), so we need to
930 * select the config and read the config descriptors.
931 */
932 ret = usb_select_config(udev);
933 if (ret)
934 return ret;
935 }
Simon Glass9b82eeb2015-03-25 12:21:59 -0600936
937 return 0;
938}
939
940UCLASS_DRIVER(usb) = {
941 .id = UCLASS_USB,
942 .name = "usb",
943 .flags = DM_UC_FLAG_SEQ_ALIAS,
Simon Glass18230342016-07-05 17:10:10 -0600944 .post_bind = dm_scan_fdt_dev,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700945 .priv_auto = sizeof(struct usb_uclass_priv),
946 .per_child_auto = sizeof(struct usb_device),
947 .per_device_auto = sizeof(struct usb_bus_priv),
Simon Glass9b82eeb2015-03-25 12:21:59 -0600948 .child_post_bind = usb_child_post_bind,
949 .child_pre_probe = usb_child_pre_probe,
Simon Glassb75b15b2020-12-03 16:55:23 -0700950 .per_child_plat_auto = sizeof(struct usb_dev_plat),
Simon Glass9b82eeb2015-03-25 12:21:59 -0600951};
Simon Glassc79173e2015-03-25 12:22:31 -0600952
953UCLASS_DRIVER(usb_dev_generic) = {
954 .id = UCLASS_USB_DEV_GENERIC,
955 .name = "usb_dev_generic",
956};
957
958U_BOOT_DRIVER(usb_dev_generic_drv) = {
959 .id = UCLASS_USB_DEV_GENERIC,
960 .name = "usb_dev_generic_drv",
961};