blob: 02c0138a2065a824f12c8def75556cb5bfc06153 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glassde312132015-03-25 12:21:59 -06002/*
3 * (C) Copyright 2015 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
5 *
Simon Glass0566e242015-03-25 12:22:30 -06006 * usb_match_device() modified from Linux kernel v4.0.
Simon Glassde312132015-03-25 12:21:59 -06007 */
8
Patrick Delaunayb953ec22021-04-27 11:02:19 +02009#define LOG_CATEGORY UCLASS_USB
10
Simon Glassde312132015-03-25 12:21:59 -060011#include <common.h>
12#include <dm.h>
13#include <errno.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060014#include <log.h>
Simon Glasscf92e052015-09-02 17:24:58 -060015#include <memalign.h>
Simon Glassde312132015-03-25 12:21:59 -060016#include <usb.h>
17#include <dm/device-internal.h>
18#include <dm/lists.h>
Simon Glassde312132015-03-25 12:21:59 -060019#include <dm/uclass-internal.h>
20
Simon Glassde312132015-03-25 12:21:59 -060021static bool asynch_allowed;
22
Hans de Goedee2536372015-05-10 14:10:21 +020023struct usb_uclass_priv {
24 int companion_device_count;
25};
26
Marek Vasut31232de2020-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 Glassde312132015-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 Suchanek34371212019-08-18 10:55:27 +020047 int length, int interval, bool nonblock)
Simon Glassde312132015-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 Suchanek34371212019-08-18 10:55:27 +020055 return ops->interrupt(bus, udev, pipe, buffer, length, interval,
56 nonblock);
Simon Glassde312132015-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 Glass0fd3d912020-12-22 19:30:28 -070064 struct usb_uclass_priv *uc_priv = uclass_get_priv(bus->uclass);
Hans de Goedee2536372015-05-10 14:10:21 +020065 int err;
Simon Glassde312132015-03-25 12:21:59 -060066
67 if (!ops->control)
68 return -ENOSYS;
69
Hans de Goedee2536372015-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 Glassde312132015-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 Goede8a5f0662015-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 Glassde312132015-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 Goedeb2f219b2015-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 Meng9ca1b4b2017-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 Meng3e59f592017-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 Glassde312132015-03-25 12:21:59 -0600175int usb_stop(void)
176{
177 struct udevice *bus;
Bin Mengd4efefe2017-10-01 06:19:42 -0700178 struct udevice *rh;
Simon Glassde312132015-03-25 12:21:59 -0600179 struct uclass *uc;
Hans de Goedee2536372015-05-10 14:10:21 +0200180 struct usb_uclass_priv *uc_priv;
Simon Glassde312132015-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 Goedee2536372015-05-10 14:10:21 +0200187
Simon Glass0fd3d912020-12-22 19:30:28 -0700188 uc_priv = uclass_get_priv(uc);
Hans de Goedee2536372015-05-10 14:10:21 +0200189
Simon Glassde312132015-03-25 12:21:59 -0600190 uclass_foreach_dev(bus, uc) {
Stefan Roese706865a2017-03-20 12:51:48 +0100191 ret = device_remove(bus, DM_REMOVE_NORMAL);
Simon Glassde312132015-03-25 12:21:59 -0600192 if (ret && !err)
193 err = ret;
Bin Mengd4efefe2017-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 Glassde312132015-03-25 12:21:59 -0600206 }
Bin Mengad0a9372017-10-01 06:19:43 -0700207
Paul Kocialkowski0fa59992015-08-04 17:04:12 +0200208#ifdef CONFIG_USB_STORAGE
Simon Glassde312132015-03-25 12:21:59 -0600209 usb_stor_reset();
Paul Kocialkowski0fa59992015-08-04 17:04:12 +0200210#endif
Hans de Goedee2536372015-05-10 14:10:21 +0200211 uc_priv->companion_device_count = 0;
Simon Glassde312132015-03-25 12:21:59 -0600212 usb_started = 0;
213
214 return err;
215}
216
Hans de Goedea24a0e92015-05-10 14:10:19 +0200217static void usb_scan_bus(struct udevice *bus, bool recurse)
Simon Glassde312132015-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 Cortes89aea232019-03-19 09:19:44 +0000227 printf("scanning bus %s for devices... ", bus->name);
Hans de Goedea24a0e92015-05-10 14:10:19 +0200228 debug("\n");
Simon Glassde312132015-03-25 12:21:59 -0600229 ret = usb_scan_device(bus, 0, USB_SPEED_FULL, &dev);
230 if (ret)
Hans de Goedea24a0e92015-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 Glassde312132015-03-25 12:21:59 -0600236}
237
Simon Glasseae11be2015-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
Simon Glassde312132015-03-25 12:21:59 -0600252int usb_init(void)
253{
254 int controllers_initialized = 0;
Hans de Goedee2536372015-05-10 14:10:21 +0200255 struct usb_uclass_priv *uc_priv;
Hans de Goedeb6de4d12015-05-10 14:10:20 +0200256 struct usb_bus_priv *priv;
Simon Glassde312132015-03-25 12:21:59 -0600257 struct udevice *bus;
258 struct uclass *uc;
Simon Glassde312132015-03-25 12:21:59 -0600259 int ret;
260
261 asynch_allowed = 1;
Simon Glassde312132015-03-25 12:21:59 -0600262
263 ret = uclass_get(UCLASS_USB, &uc);
264 if (ret)
265 return ret;
266
Simon Glass0fd3d912020-12-22 19:30:28 -0700267 uc_priv = uclass_get_priv(uc);
Hans de Goedee2536372015-05-10 14:10:21 +0200268
Simon Glassde312132015-03-25 12:21:59 -0600269 uclass_foreach_dev(bus, uc) {
270 /* init low_level USB */
Ismael Luceno Cortes89aea232019-03-19 09:19:44 +0000271 printf("Bus %s: ", bus->name);
Bin Mengd4efefe2017-10-01 06:19:42 -0700272
Bin Mengd4efefe2017-10-01 06:19:42 -0700273 /*
274 * For Sandbox, we need scan the device tree each time when we
275 * start the USB stack, in order to re-create the emulated USB
276 * devices and bind drivers for them before we actually do the
277 * driver probe.
Fabrice Gasnierba1fa2a2022-12-12 11:44:35 +0100278 *
279 * For USB onboard HUB, we need to do some non-trivial init
280 * like enabling a power regulator, before enumeration.
Bin Mengd4efefe2017-10-01 06:19:42 -0700281 */
Fabrice Gasnierba1fa2a2022-12-12 11:44:35 +0100282 if (IS_ENABLED(CONFIG_SANDBOX) ||
283 IS_ENABLED(CONFIG_USB_ONBOARD_HUB)) {
284 ret = dm_scan_fdt_dev(bus);
285 if (ret) {
286 printf("USB device scan from fdt failed (%d)", ret);
287 continue;
288 }
Bin Mengd4efefe2017-10-01 06:19:42 -0700289 }
Bin Mengd4efefe2017-10-01 06:19:42 -0700290
Simon Glassde312132015-03-25 12:21:59 -0600291 ret = device_probe(bus);
292 if (ret == -ENODEV) { /* No such device. */
293 puts("Port not available.\n");
294 controllers_initialized++;
295 continue;
296 }
297
298 if (ret) { /* Other error. */
299 printf("probe failed, error %d\n", ret);
300 continue;
301 }
Simon Glassde312132015-03-25 12:21:59 -0600302 controllers_initialized++;
Simon Glassde312132015-03-25 12:21:59 -0600303 usb_started = true;
304 }
305
Hans de Goedeb6de4d12015-05-10 14:10:20 +0200306 /*
307 * lowlevel init done, now scan the bus for devices i.e. search HUBs
308 * and configure them, first scan primary controllers.
309 */
310 uclass_foreach_dev(bus, uc) {
311 if (!device_active(bus))
312 continue;
313
314 priv = dev_get_uclass_priv(bus);
315 if (!priv->companion)
316 usb_scan_bus(bus, true);
317 }
318
319 /*
320 * Now that the primary controllers have been scanned and have handed
321 * over any devices they do not understand to their companions, scan
Hans de Goedee2536372015-05-10 14:10:21 +0200322 * the companions if necessary.
Hans de Goedeb6de4d12015-05-10 14:10:20 +0200323 */
Hans de Goedee2536372015-05-10 14:10:21 +0200324 if (uc_priv->companion_device_count) {
325 uclass_foreach_dev(bus, uc) {
326 if (!device_active(bus))
327 continue;
Hans de Goedeb6de4d12015-05-10 14:10:20 +0200328
Hans de Goedee2536372015-05-10 14:10:21 +0200329 priv = dev_get_uclass_priv(bus);
330 if (priv->companion)
331 usb_scan_bus(bus, true);
332 }
Hans de Goedeb6de4d12015-05-10 14:10:20 +0200333 }
334
Simon Glassde312132015-03-25 12:21:59 -0600335 debug("scan end\n");
Simon Glasseae11be2015-11-08 23:48:00 -0700336
337 /* Remove any devices that were not found on this scan */
338 remove_inactive_children(uc, bus);
339
340 ret = uclass_get(UCLASS_USB_HUB, &uc);
341 if (ret)
342 return ret;
343 remove_inactive_children(uc, bus);
344
Simon Glassde312132015-03-25 12:21:59 -0600345 /* if we were not able to find at least one working bus, bail out */
Ismael Luceno Cortes89aea232019-03-19 09:19:44 +0000346 if (controllers_initialized == 0)
347 printf("No working controllers found\n");
Simon Glassde312132015-03-25 12:21:59 -0600348
349 return usb_started ? 0 : -1;
350}
351
Simon Glassfbeceb22015-03-25 12:22:32 -0600352int usb_setup_ehci_gadget(struct ehci_ctrl **ctlrp)
353{
Simon Glass8a8d24b2020-12-03 16:55:23 -0700354 struct usb_plat *plat;
Simon Glassfbeceb22015-03-25 12:22:32 -0600355 struct udevice *dev;
356 int ret;
357
358 /* Find the old device and remove it */
Sean Anderson821ca602021-11-05 12:52:55 -0400359 ret = uclass_find_first_device(UCLASS_USB, &dev);
Simon Glassfbeceb22015-03-25 12:22:32 -0600360 if (ret)
361 return ret;
Stefan Roese706865a2017-03-20 12:51:48 +0100362 ret = device_remove(dev, DM_REMOVE_NORMAL);
Simon Glassfbeceb22015-03-25 12:22:32 -0600363 if (ret)
364 return ret;
365
Simon Glassc69cda22020-12-03 16:55:20 -0700366 plat = dev_get_plat(dev);
Simon Glassfbeceb22015-03-25 12:22:32 -0600367 plat->init_type = USB_INIT_DEVICE;
368 ret = device_probe(dev);
369 if (ret)
370 return ret;
371 *ctlrp = dev_get_priv(dev);
372
373 return 0;
374}
375
Ye Li1468a1c2020-06-29 10:12:59 +0800376int usb_remove_ehci_gadget(struct ehci_ctrl **ctlrp)
377{
378 struct udevice *dev;
379 int ret;
380
381 /* Find the old device and remove it */
Sean Anderson821ca602021-11-05 12:52:55 -0400382 ret = uclass_find_first_device(UCLASS_USB, &dev);
Ye Li1468a1c2020-06-29 10:12:59 +0800383 if (ret)
384 return ret;
385 ret = device_remove(dev, DM_REMOVE_NORMAL);
386 if (ret)
387 return ret;
388
389 *ctlrp = NULL;
390
391 return 0;
392}
393
Simon Glass0566e242015-03-25 12:22:30 -0600394/* returns 0 if no match, 1 if match */
Masahiro Yamada121a4d12017-06-22 16:35:14 +0900395static int usb_match_device(const struct usb_device_descriptor *desc,
396 const struct usb_device_id *id)
Simon Glass0566e242015-03-25 12:22:30 -0600397{
398 if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
Stefan Roesed96f6e12020-07-21 10:46:04 +0200399 id->idVendor != desc->idVendor)
Simon Glass0566e242015-03-25 12:22:30 -0600400 return 0;
401
402 if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
Stefan Roesed96f6e12020-07-21 10:46:04 +0200403 id->idProduct != desc->idProduct)
Simon Glass0566e242015-03-25 12:22:30 -0600404 return 0;
405
406 /* No need to test id->bcdDevice_lo != 0, since 0 is never
407 greater than any unsigned number. */
408 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) &&
Stefan Roesed96f6e12020-07-21 10:46:04 +0200409 (id->bcdDevice_lo > desc->bcdDevice))
Simon Glass0566e242015-03-25 12:22:30 -0600410 return 0;
411
412 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) &&
Stefan Roesed96f6e12020-07-21 10:46:04 +0200413 (id->bcdDevice_hi < desc->bcdDevice))
Simon Glass0566e242015-03-25 12:22:30 -0600414 return 0;
415
416 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) &&
417 (id->bDeviceClass != desc->bDeviceClass))
418 return 0;
419
420 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) &&
421 (id->bDeviceSubClass != desc->bDeviceSubClass))
422 return 0;
423
424 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) &&
425 (id->bDeviceProtocol != desc->bDeviceProtocol))
426 return 0;
427
428 return 1;
429}
430
431/* returns 0 if no match, 1 if match */
Masahiro Yamada121a4d12017-06-22 16:35:14 +0900432static int usb_match_one_id_intf(const struct usb_device_descriptor *desc,
433 const struct usb_interface_descriptor *int_desc,
434 const struct usb_device_id *id)
Simon Glass0566e242015-03-25 12:22:30 -0600435{
436 /* The interface class, subclass, protocol and number should never be
437 * checked for a match if the device class is Vendor Specific,
438 * unless the match record specifies the Vendor ID. */
439 if (desc->bDeviceClass == USB_CLASS_VENDOR_SPEC &&
440 !(id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
441 (id->match_flags & (USB_DEVICE_ID_MATCH_INT_CLASS |
442 USB_DEVICE_ID_MATCH_INT_SUBCLASS |
443 USB_DEVICE_ID_MATCH_INT_PROTOCOL |
444 USB_DEVICE_ID_MATCH_INT_NUMBER)))
445 return 0;
446
447 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) &&
448 (id->bInterfaceClass != int_desc->bInterfaceClass))
449 return 0;
450
451 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) &&
452 (id->bInterfaceSubClass != int_desc->bInterfaceSubClass))
453 return 0;
454
455 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) &&
456 (id->bInterfaceProtocol != int_desc->bInterfaceProtocol))
457 return 0;
458
459 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_NUMBER) &&
460 (id->bInterfaceNumber != int_desc->bInterfaceNumber))
461 return 0;
462
463 return 1;
464}
465
466/* returns 0 if no match, 1 if match */
Masahiro Yamada121a4d12017-06-22 16:35:14 +0900467static int usb_match_one_id(struct usb_device_descriptor *desc,
468 struct usb_interface_descriptor *int_desc,
469 const struct usb_device_id *id)
Simon Glass0566e242015-03-25 12:22:30 -0600470{
471 if (!usb_match_device(desc, id))
472 return 0;
473
474 return usb_match_one_id_intf(desc, int_desc, id);
475}
476
Michael Wallec03b7612020-06-02 01:47:07 +0200477static ofnode usb_get_ofnode(struct udevice *hub, int port)
478{
479 ofnode node;
480 u32 reg;
481
Simon Glassc23405f2020-12-19 10:40:12 -0700482 if (!dev_has_ofnode(hub))
Michael Wallec03b7612020-06-02 01:47:07 +0200483 return ofnode_null();
484
485 /*
486 * The USB controller and its USB hub are two different udevices,
487 * but the device tree has only one node for both. Thus we are
488 * assigning this node to both udevices.
489 * If port is zero, the controller scans its root hub, thus we
490 * are using the same ofnode as the controller here.
491 */
492 if (!port)
493 return dev_ofnode(hub);
494
495 ofnode_for_each_subnode(node, dev_ofnode(hub)) {
496 if (ofnode_read_u32(node, "reg", &reg))
497 continue;
498
499 if (reg == port)
500 return node;
501 }
502
503 return ofnode_null();
504}
505
Simon Glass0566e242015-03-25 12:22:30 -0600506/**
507 * usb_find_and_bind_driver() - Find and bind the right USB driver
508 *
509 * This only looks at certain fields in the descriptor.
510 */
511static int usb_find_and_bind_driver(struct udevice *parent,
512 struct usb_device_descriptor *desc,
513 struct usb_interface_descriptor *iface,
Michael Wallec03b7612020-06-02 01:47:07 +0200514 int bus_seq, int devnum, int port,
Simon Glass0566e242015-03-25 12:22:30 -0600515 struct udevice **devp)
516{
517 struct usb_driver_entry *start, *entry;
518 int n_ents;
519 int ret;
Marek Vasut55a95f82022-11-26 13:57:53 +0100520 char name[34], *str;
Michael Wallec03b7612020-06-02 01:47:07 +0200521 ofnode node = usb_get_ofnode(parent, port);
Simon Glass0566e242015-03-25 12:22:30 -0600522
523 *devp = NULL;
524 debug("%s: Searching for driver\n", __func__);
525 start = ll_entry_start(struct usb_driver_entry, usb_driver_entry);
526 n_ents = ll_entry_count(struct usb_driver_entry, usb_driver_entry);
527 for (entry = start; entry != start + n_ents; entry++) {
528 const struct usb_device_id *id;
529 struct udevice *dev;
530 const struct driver *drv;
Simon Glass8a8d24b2020-12-03 16:55:23 -0700531 struct usb_dev_plat *plat;
Simon Glass0566e242015-03-25 12:22:30 -0600532
533 for (id = entry->match; id->match_flags; id++) {
534 if (!usb_match_one_id(desc, iface, id))
535 continue;
536
537 drv = entry->driver;
538 /*
539 * We could pass the descriptor to the driver as
Simon Glasscaa4daa2020-12-03 16:55:18 -0700540 * plat (instead of NULL) and allow its bind()
Simon Glass0566e242015-03-25 12:22:30 -0600541 * method to return -ENOENT if it doesn't support this
542 * device. That way we could continue the search to
543 * find another driver. For now this doesn't seem
544 * necesssary, so just bind the first match.
545 */
Simon Glass734206d2020-11-28 17:50:01 -0700546 ret = device_bind(parent, drv, drv->name, NULL, node,
547 &dev);
Simon Glass0566e242015-03-25 12:22:30 -0600548 if (ret)
549 goto error;
550 debug("%s: Match found: %s\n", __func__, drv->name);
551 dev->driver_data = id->driver_info;
Simon Glasscaa4daa2020-12-03 16:55:18 -0700552 plat = dev_get_parent_plat(dev);
Simon Glass0566e242015-03-25 12:22:30 -0600553 plat->id = *id;
554 *devp = dev;
555 return 0;
556 }
557 }
558
Simon Glass449230f2015-03-25 12:22:31 -0600559 /* Bind a generic driver so that the device can be used */
560 snprintf(name, sizeof(name), "generic_bus_%x_dev_%x", bus_seq, devnum);
561 str = strdup(name);
562 if (!str)
563 return -ENOMEM;
564 ret = device_bind_driver(parent, "usb_dev_generic_drv", str, devp);
Simon Glassecdf3ab2023-01-17 10:47:35 -0700565 if (!ret)
566 device_set_name_alloced(*devp);
Simon Glass449230f2015-03-25 12:22:31 -0600567
Simon Glass0566e242015-03-25 12:22:30 -0600568error:
569 debug("%s: No match found: %d\n", __func__, ret);
570 return ret;
571}
572
573/**
Simon Glass1b6a1df2015-11-08 23:47:56 -0700574 * usb_find_child() - Find an existing device which matches our needs
575 *
576 *
Simon Glass0566e242015-03-25 12:22:30 -0600577 */
Simon Glass1b6a1df2015-11-08 23:47:56 -0700578static int usb_find_child(struct udevice *parent,
579 struct usb_device_descriptor *desc,
580 struct usb_interface_descriptor *iface,
581 struct udevice **devp)
Simon Glass0566e242015-03-25 12:22:30 -0600582{
583 struct udevice *dev;
584
585 *devp = NULL;
586 for (device_find_first_child(parent, &dev);
587 dev;
588 device_find_next_child(&dev)) {
Simon Glass8a8d24b2020-12-03 16:55:23 -0700589 struct usb_dev_plat *plat = dev_get_parent_plat(dev);
Simon Glass0566e242015-03-25 12:22:30 -0600590
591 /* If this device is already in use, skip it */
592 if (device_active(dev))
593 continue;
594 debug(" %s: name='%s', plat=%d, desc=%d\n", __func__,
595 dev->name, plat->id.bDeviceClass, desc->bDeviceClass);
596 if (usb_match_one_id(desc, iface, &plat->id)) {
597 *devp = dev;
598 return 0;
599 }
600 }
Simon Glass1b6a1df2015-11-08 23:47:56 -0700601
Simon Glass0566e242015-03-25 12:22:30 -0600602 return -ENOENT;
603}
604
Simon Glassde312132015-03-25 12:21:59 -0600605int usb_scan_device(struct udevice *parent, int port,
606 enum usb_device_speed speed, struct udevice **devp)
607{
608 struct udevice *dev;
609 bool created = false;
Simon Glass8a8d24b2020-12-03 16:55:23 -0700610 struct usb_dev_plat *plat;
Simon Glassde312132015-03-25 12:21:59 -0600611 struct usb_bus_priv *priv;
612 struct usb_device *parent_udev;
613 int ret;
614 ALLOC_CACHE_ALIGN_BUFFER(struct usb_device, udev, 1);
615 struct usb_interface_descriptor *iface = &udev->config.if_desc[0].desc;
616
617 *devp = NULL;
618 memset(udev, '\0', sizeof(*udev));
Hans de Goedef78a5c02015-05-05 11:54:31 +0200619 udev->controller_dev = usb_get_bus(parent);
Simon Glassde312132015-03-25 12:21:59 -0600620 priv = dev_get_uclass_priv(udev->controller_dev);
621
622 /*
623 * Somewhat nasty, this. We create a local device and use the normal
624 * USB stack to read its descriptor. Then we know what type of device
625 * to create for real.
626 *
627 * udev->dev is set to the parent, since we don't have a real device
628 * yet. The USB stack should not access udev.dev anyway, except perhaps
629 * to find the controller, and the controller will either be @parent,
630 * or some parent of @parent.
631 *
632 * Another option might be to create the device as a generic USB
633 * device, then morph it into the correct one when we know what it
634 * should be. This means that a generic USB device would morph into
635 * a network controller, or a USB flash stick, for example. However,
636 * we don't support such morphing and it isn't clear that it would
637 * be easy to do.
638 *
639 * Yet another option is to split out the USB stack parts of udev
640 * into something like a 'struct urb' (as Linux does) which can exist
641 * independently of any device. This feels cleaner, but calls for quite
642 * a big change to the USB stack.
643 *
644 * For now, the approach is to set up an empty udev, read its
645 * descriptor and assign it an address, then bind a real device and
646 * stash the resulting information into the device's parent
647 * platform data. Then when we probe it, usb_child_pre_probe() is called
648 * and it will pull the information out of the stash.
649 */
650 udev->dev = parent;
651 udev->speed = speed;
652 udev->devnum = priv->next_addr + 1;
653 udev->portnr = port;
654 debug("Calling usb_setup_device(), portnr=%d\n", udev->portnr);
655 parent_udev = device_get_uclass_id(parent) == UCLASS_USB_HUB ?
Simon Glassbcbe3d12015-09-28 23:32:01 -0600656 dev_get_parent_priv(parent) : NULL;
Hans de Goede9eb72dd2015-06-17 21:33:46 +0200657 ret = usb_setup_device(udev, priv->desc_before_addr, parent_udev);
Simon Glassde312132015-03-25 12:21:59 -0600658 debug("read_descriptor for '%s': ret=%d\n", parent->name, ret);
659 if (ret)
660 return ret;
Simon Glass1b6a1df2015-11-08 23:47:56 -0700661 ret = usb_find_child(parent, &udev->descriptor, iface, &dev);
662 debug("** usb_find_child returns %d\n", ret);
Simon Glass0566e242015-03-25 12:22:30 -0600663 if (ret) {
664 if (ret != -ENOENT)
665 return ret;
Michael Wallec03b7612020-06-02 01:47:07 +0200666 ret = usb_find_and_bind_driver(parent, &udev->descriptor,
667 iface,
Simon Glass8b85dfc2020-12-16 21:20:07 -0700668 dev_seq(udev->controller_dev),
Michael Wallec03b7612020-06-02 01:47:07 +0200669 udev->devnum, port, &dev);
Simon Glass0566e242015-03-25 12:22:30 -0600670 if (ret)
671 return ret;
672 created = true;
673 }
Simon Glasscaa4daa2020-12-03 16:55:18 -0700674 plat = dev_get_parent_plat(dev);
Simon Glass0566e242015-03-25 12:22:30 -0600675 debug("%s: Probing '%s', plat=%p\n", __func__, dev->name, plat);
676 plat->devnum = udev->devnum;
Hans de Goede7f1a0752015-05-05 11:54:32 +0200677 plat->udev = udev;
Simon Glass0566e242015-03-25 12:22:30 -0600678 priv->next_addr++;
679 ret = device_probe(dev);
680 if (ret) {
681 debug("%s: Device '%s' probe failed\n", __func__, dev->name);
682 priv->next_addr--;
683 if (created)
684 device_unbind(dev);
685 return ret;
686 }
687 *devp = dev;
Simon Glassde312132015-03-25 12:21:59 -0600688
Simon Glass0566e242015-03-25 12:22:30 -0600689 return 0;
Simon Glassde312132015-03-25 12:21:59 -0600690}
691
Simon Glass0c5dd9a2015-05-13 07:02:23 -0600692/*
693 * Detect if a USB device has been plugged or unplugged.
694 */
695int usb_detect_change(void)
696{
697 struct udevice *hub;
698 struct uclass *uc;
699 int change = 0;
700 int ret;
701
702 ret = uclass_get(UCLASS_USB_HUB, &uc);
703 if (ret)
704 return ret;
705
706 uclass_foreach_dev(hub, uc) {
707 struct usb_device *udev;
708 struct udevice *dev;
709
710 if (!device_active(hub))
711 continue;
712 for (device_find_first_child(hub, &dev);
713 dev;
714 device_find_next_child(&dev)) {
715 struct usb_port_status status;
716
717 if (!device_active(dev))
718 continue;
719
Simon Glassbcbe3d12015-09-28 23:32:01 -0600720 udev = dev_get_parent_priv(dev);
Simon Glass0c5dd9a2015-05-13 07:02:23 -0600721 if (usb_get_port_status(udev, udev->portnr, &status)
722 < 0)
723 /* USB request failed */
724 continue;
725
726 if (le16_to_cpu(status.wPortChange) &
727 USB_PORT_STAT_C_CONNECTION)
728 change++;
729 }
730 }
731
732 return change;
733}
734
Masahiro Yamada121a4d12017-06-22 16:35:14 +0900735static int usb_child_post_bind(struct udevice *dev)
Simon Glassde312132015-03-25 12:21:59 -0600736{
Simon Glass8a8d24b2020-12-03 16:55:23 -0700737 struct usb_dev_plat *plat = dev_get_parent_plat(dev);
Simon Glassde312132015-03-25 12:21:59 -0600738 int val;
739
Simon Glass7d14ee42020-12-19 10:40:13 -0700740 if (!dev_has_ofnode(dev))
Simon Glassde312132015-03-25 12:21:59 -0600741 return 0;
742
743 /* We only support matching a few things */
Simon Glassd20fd272017-05-18 20:09:38 -0600744 val = dev_read_u32_default(dev, "usb,device-class", -1);
Simon Glassde312132015-03-25 12:21:59 -0600745 if (val != -1) {
746 plat->id.match_flags |= USB_DEVICE_ID_MATCH_DEV_CLASS;
747 plat->id.bDeviceClass = val;
748 }
Simon Glassd20fd272017-05-18 20:09:38 -0600749 val = dev_read_u32_default(dev, "usb,interface-class", -1);
Simon Glassde312132015-03-25 12:21:59 -0600750 if (val != -1) {
751 plat->id.match_flags |= USB_DEVICE_ID_MATCH_INT_CLASS;
752 plat->id.bInterfaceClass = val;
753 }
754
755 return 0;
756}
757
Hans de Goedef78a5c02015-05-05 11:54:31 +0200758struct udevice *usb_get_bus(struct udevice *dev)
Simon Glassde312132015-03-25 12:21:59 -0600759{
760 struct udevice *bus;
761
Simon Glassde312132015-03-25 12:21:59 -0600762 for (bus = dev; bus && device_get_uclass_id(bus) != UCLASS_USB; )
763 bus = bus->parent;
764 if (!bus) {
765 /* By design this cannot happen */
766 assert(bus);
767 debug("USB HUB '%s' does not have a controller\n", dev->name);
Simon Glassde312132015-03-25 12:21:59 -0600768 }
Simon Glassde312132015-03-25 12:21:59 -0600769
Hans de Goedef78a5c02015-05-05 11:54:31 +0200770 return bus;
Simon Glassde312132015-03-25 12:21:59 -0600771}
772
773int usb_child_pre_probe(struct udevice *dev)
774{
Simon Glassbcbe3d12015-09-28 23:32:01 -0600775 struct usb_device *udev = dev_get_parent_priv(dev);
Simon Glass8a8d24b2020-12-03 16:55:23 -0700776 struct usb_dev_plat *plat = dev_get_parent_plat(dev);
Simon Glassde312132015-03-25 12:21:59 -0600777 int ret;
778
Hans de Goede7f1a0752015-05-05 11:54:32 +0200779 if (plat->udev) {
780 /*
781 * Copy over all the values set in the on stack struct
782 * usb_device in usb_scan_device() to our final struct
783 * usb_device for this dev.
784 */
785 *udev = *(plat->udev);
786 /* And clear plat->udev as it will not be valid for long */
787 plat->udev = NULL;
788 udev->dev = dev;
789 } else {
790 /*
791 * This happens with devices which are explicitly bound
792 * instead of being discovered through usb_scan_device()
793 * such as sandbox emul devices.
794 */
795 udev->dev = dev;
796 udev->controller_dev = usb_get_bus(dev);
797 udev->devnum = plat->devnum;
Simon Glassde312132015-03-25 12:21:59 -0600798
Hans de Goede7f1a0752015-05-05 11:54:32 +0200799 /*
800 * udev did not go through usb_scan_device(), so we need to
801 * select the config and read the config descriptors.
802 */
803 ret = usb_select_config(udev);
804 if (ret)
805 return ret;
806 }
Simon Glassde312132015-03-25 12:21:59 -0600807
808 return 0;
809}
810
811UCLASS_DRIVER(usb) = {
812 .id = UCLASS_USB,
813 .name = "usb",
814 .flags = DM_UC_FLAG_SEQ_ALIAS,
Simon Glass91195482016-07-05 17:10:10 -0600815 .post_bind = dm_scan_fdt_dev,
Simon Glass41575d82020-12-03 16:55:17 -0700816 .priv_auto = sizeof(struct usb_uclass_priv),
817 .per_child_auto = sizeof(struct usb_device),
818 .per_device_auto = sizeof(struct usb_bus_priv),
Simon Glassde312132015-03-25 12:21:59 -0600819 .child_post_bind = usb_child_post_bind,
820 .child_pre_probe = usb_child_pre_probe,
Simon Glass8a8d24b2020-12-03 16:55:23 -0700821 .per_child_plat_auto = sizeof(struct usb_dev_plat),
Simon Glassde312132015-03-25 12:21:59 -0600822};
Simon Glass449230f2015-03-25 12:22:31 -0600823
824UCLASS_DRIVER(usb_dev_generic) = {
825 .id = UCLASS_USB_DEV_GENERIC,
826 .name = "usb_dev_generic",
827};
828
829U_BOOT_DRIVER(usb_dev_generic_drv) = {
830 .id = UCLASS_USB_DEV_GENERIC,
831 .name = "usb_dev_generic_drv",
832};