blob: 956e2a4e8e4e2967bded433fe4e585f89bc9eaf5 [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
274#ifdef CONFIG_SANDBOX
275 /*
276 * For Sandbox, we need scan the device tree each time when we
277 * start the USB stack, in order to re-create the emulated USB
278 * devices and bind drivers for them before we actually do the
279 * driver probe.
280 */
281 ret = dm_scan_fdt_dev(bus);
282 if (ret) {
283 printf("Sandbox USB device scan failed (%d)\n", ret);
284 continue;
285 }
286#endif
287
Simon Glass9b82eeb2015-03-25 12:21:59 -0600288 ret = device_probe(bus);
289 if (ret == -ENODEV) { /* No such device. */
290 puts("Port not available.\n");
291 controllers_initialized++;
292 continue;
293 }
294
295 if (ret) { /* Other error. */
296 printf("probe failed, error %d\n", ret);
297 continue;
298 }
Simon Glass9b82eeb2015-03-25 12:21:59 -0600299 controllers_initialized++;
Simon Glass9b82eeb2015-03-25 12:21:59 -0600300 usb_started = true;
301 }
302
Hans de Goede59a0dfc2015-05-10 14:10:20 +0200303 /*
304 * lowlevel init done, now scan the bus for devices i.e. search HUBs
305 * and configure them, first scan primary controllers.
306 */
307 uclass_foreach_dev(bus, uc) {
308 if (!device_active(bus))
309 continue;
310
311 priv = dev_get_uclass_priv(bus);
312 if (!priv->companion)
313 usb_scan_bus(bus, true);
314 }
315
316 /*
317 * Now that the primary controllers have been scanned and have handed
318 * over any devices they do not understand to their companions, scan
Hans de Goedecebc5b72015-05-10 14:10:21 +0200319 * the companions if necessary.
Hans de Goede59a0dfc2015-05-10 14:10:20 +0200320 */
Hans de Goedecebc5b72015-05-10 14:10:21 +0200321 if (uc_priv->companion_device_count) {
322 uclass_foreach_dev(bus, uc) {
323 if (!device_active(bus))
324 continue;
Hans de Goede59a0dfc2015-05-10 14:10:20 +0200325
Hans de Goedecebc5b72015-05-10 14:10:21 +0200326 priv = dev_get_uclass_priv(bus);
327 if (priv->companion)
328 usb_scan_bus(bus, true);
329 }
Hans de Goede59a0dfc2015-05-10 14:10:20 +0200330 }
331
Simon Glass9b82eeb2015-03-25 12:21:59 -0600332 debug("scan end\n");
Simon Glass35787d22015-11-08 23:48:00 -0700333
334 /* Remove any devices that were not found on this scan */
335 remove_inactive_children(uc, bus);
336
337 ret = uclass_get(UCLASS_USB_HUB, &uc);
338 if (ret)
339 return ret;
340 remove_inactive_children(uc, bus);
341
Simon Glass9b82eeb2015-03-25 12:21:59 -0600342 /* if we were not able to find at least one working bus, bail out */
Ismael Luceno Cortesf58affe2019-03-19 09:19:44 +0000343 if (controllers_initialized == 0)
344 printf("No working controllers found\n");
Simon Glass9b82eeb2015-03-25 12:21:59 -0600345
346 return usb_started ? 0 : -1;
347}
348
Simon Glass444b1e02015-03-25 12:22:32 -0600349int usb_setup_ehci_gadget(struct ehci_ctrl **ctlrp)
350{
Simon Glassb75b15b2020-12-03 16:55:23 -0700351 struct usb_plat *plat;
Simon Glass444b1e02015-03-25 12:22:32 -0600352 struct udevice *dev;
353 int ret;
354
355 /* Find the old device and remove it */
Sean Andersone7665702021-11-05 12:52:55 -0400356 ret = uclass_find_first_device(UCLASS_USB, &dev);
Simon Glass444b1e02015-03-25 12:22:32 -0600357 if (ret)
358 return ret;
Stefan Roese80b5bc92017-03-20 12:51:48 +0100359 ret = device_remove(dev, DM_REMOVE_NORMAL);
Simon Glass444b1e02015-03-25 12:22:32 -0600360 if (ret)
361 return ret;
362
Simon Glassfa20e932020-12-03 16:55:20 -0700363 plat = dev_get_plat(dev);
Simon Glass444b1e02015-03-25 12:22:32 -0600364 plat->init_type = USB_INIT_DEVICE;
365 ret = device_probe(dev);
366 if (ret)
367 return ret;
368 *ctlrp = dev_get_priv(dev);
369
370 return 0;
371}
372
Ye Li68801eb2020-06-29 10:12:59 +0800373int usb_remove_ehci_gadget(struct ehci_ctrl **ctlrp)
374{
375 struct udevice *dev;
376 int ret;
377
378 /* Find the old device and remove it */
Sean Andersone7665702021-11-05 12:52:55 -0400379 ret = uclass_find_first_device(UCLASS_USB, &dev);
Ye Li68801eb2020-06-29 10:12:59 +0800380 if (ret)
381 return ret;
382 ret = device_remove(dev, DM_REMOVE_NORMAL);
383 if (ret)
384 return ret;
385
386 *ctlrp = NULL;
387
388 return 0;
389}
390
Simon Glassfc03a552015-03-25 12:22:30 -0600391/* returns 0 if no match, 1 if match */
Masahiro Yamada6d8e4332017-06-22 16:35:14 +0900392static int usb_match_device(const struct usb_device_descriptor *desc,
393 const struct usb_device_id *id)
Simon Glassfc03a552015-03-25 12:22:30 -0600394{
395 if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
Stefan Roese77c83ac2020-07-21 10:46:04 +0200396 id->idVendor != desc->idVendor)
Simon Glassfc03a552015-03-25 12:22:30 -0600397 return 0;
398
399 if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
Stefan Roese77c83ac2020-07-21 10:46:04 +0200400 id->idProduct != desc->idProduct)
Simon Glassfc03a552015-03-25 12:22:30 -0600401 return 0;
402
403 /* No need to test id->bcdDevice_lo != 0, since 0 is never
404 greater than any unsigned number. */
405 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) &&
Stefan Roese77c83ac2020-07-21 10:46:04 +0200406 (id->bcdDevice_lo > desc->bcdDevice))
Simon Glassfc03a552015-03-25 12:22:30 -0600407 return 0;
408
409 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) &&
Stefan Roese77c83ac2020-07-21 10:46:04 +0200410 (id->bcdDevice_hi < desc->bcdDevice))
Simon Glassfc03a552015-03-25 12:22:30 -0600411 return 0;
412
413 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) &&
414 (id->bDeviceClass != desc->bDeviceClass))
415 return 0;
416
417 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) &&
418 (id->bDeviceSubClass != desc->bDeviceSubClass))
419 return 0;
420
421 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) &&
422 (id->bDeviceProtocol != desc->bDeviceProtocol))
423 return 0;
424
425 return 1;
426}
427
428/* returns 0 if no match, 1 if match */
Masahiro Yamada6d8e4332017-06-22 16:35:14 +0900429static int usb_match_one_id_intf(const struct usb_device_descriptor *desc,
430 const struct usb_interface_descriptor *int_desc,
431 const struct usb_device_id *id)
Simon Glassfc03a552015-03-25 12:22:30 -0600432{
433 /* The interface class, subclass, protocol and number should never be
434 * checked for a match if the device class is Vendor Specific,
435 * unless the match record specifies the Vendor ID. */
436 if (desc->bDeviceClass == USB_CLASS_VENDOR_SPEC &&
437 !(id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
438 (id->match_flags & (USB_DEVICE_ID_MATCH_INT_CLASS |
439 USB_DEVICE_ID_MATCH_INT_SUBCLASS |
440 USB_DEVICE_ID_MATCH_INT_PROTOCOL |
441 USB_DEVICE_ID_MATCH_INT_NUMBER)))
442 return 0;
443
444 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) &&
445 (id->bInterfaceClass != int_desc->bInterfaceClass))
446 return 0;
447
448 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) &&
449 (id->bInterfaceSubClass != int_desc->bInterfaceSubClass))
450 return 0;
451
452 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) &&
453 (id->bInterfaceProtocol != int_desc->bInterfaceProtocol))
454 return 0;
455
456 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_NUMBER) &&
457 (id->bInterfaceNumber != int_desc->bInterfaceNumber))
458 return 0;
459
460 return 1;
461}
462
463/* returns 0 if no match, 1 if match */
Masahiro Yamada6d8e4332017-06-22 16:35:14 +0900464static int usb_match_one_id(struct usb_device_descriptor *desc,
465 struct usb_interface_descriptor *int_desc,
466 const struct usb_device_id *id)
Simon Glassfc03a552015-03-25 12:22:30 -0600467{
468 if (!usb_match_device(desc, id))
469 return 0;
470
471 return usb_match_one_id_intf(desc, int_desc, id);
472}
473
Michael Walle7c961322020-06-02 01:47:07 +0200474static ofnode usb_get_ofnode(struct udevice *hub, int port)
475{
476 ofnode node;
477 u32 reg;
478
Simon Glass07c17772020-12-19 10:40:12 -0700479 if (!dev_has_ofnode(hub))
Michael Walle7c961322020-06-02 01:47:07 +0200480 return ofnode_null();
481
482 /*
483 * The USB controller and its USB hub are two different udevices,
484 * but the device tree has only one node for both. Thus we are
485 * assigning this node to both udevices.
486 * If port is zero, the controller scans its root hub, thus we
487 * are using the same ofnode as the controller here.
488 */
489 if (!port)
490 return dev_ofnode(hub);
491
492 ofnode_for_each_subnode(node, dev_ofnode(hub)) {
493 if (ofnode_read_u32(node, "reg", &reg))
494 continue;
495
496 if (reg == port)
497 return node;
498 }
499
500 return ofnode_null();
501}
502
Simon Glassfc03a552015-03-25 12:22:30 -0600503/**
504 * usb_find_and_bind_driver() - Find and bind the right USB driver
505 *
506 * This only looks at certain fields in the descriptor.
507 */
508static int usb_find_and_bind_driver(struct udevice *parent,
509 struct usb_device_descriptor *desc,
510 struct usb_interface_descriptor *iface,
Michael Walle7c961322020-06-02 01:47:07 +0200511 int bus_seq, int devnum, int port,
Simon Glassfc03a552015-03-25 12:22:30 -0600512 struct udevice **devp)
513{
514 struct usb_driver_entry *start, *entry;
515 int n_ents;
516 int ret;
Marek Vasut6e76b0f2022-11-26 13:57:53 +0100517 char name[34], *str;
Michael Walle7c961322020-06-02 01:47:07 +0200518 ofnode node = usb_get_ofnode(parent, port);
Simon Glassfc03a552015-03-25 12:22:30 -0600519
520 *devp = NULL;
521 debug("%s: Searching for driver\n", __func__);
522 start = ll_entry_start(struct usb_driver_entry, usb_driver_entry);
523 n_ents = ll_entry_count(struct usb_driver_entry, usb_driver_entry);
524 for (entry = start; entry != start + n_ents; entry++) {
525 const struct usb_device_id *id;
526 struct udevice *dev;
527 const struct driver *drv;
Simon Glassb75b15b2020-12-03 16:55:23 -0700528 struct usb_dev_plat *plat;
Simon Glassfc03a552015-03-25 12:22:30 -0600529
530 for (id = entry->match; id->match_flags; id++) {
531 if (!usb_match_one_id(desc, iface, id))
532 continue;
533
534 drv = entry->driver;
535 /*
536 * We could pass the descriptor to the driver as
Simon Glass71fa5b42020-12-03 16:55:18 -0700537 * plat (instead of NULL) and allow its bind()
Simon Glassfc03a552015-03-25 12:22:30 -0600538 * method to return -ENOENT if it doesn't support this
539 * device. That way we could continue the search to
540 * find another driver. For now this doesn't seem
541 * necesssary, so just bind the first match.
542 */
Simon Glass884870f2020-11-28 17:50:01 -0700543 ret = device_bind(parent, drv, drv->name, NULL, node,
544 &dev);
Simon Glassfc03a552015-03-25 12:22:30 -0600545 if (ret)
546 goto error;
547 debug("%s: Match found: %s\n", __func__, drv->name);
548 dev->driver_data = id->driver_info;
Simon Glass71fa5b42020-12-03 16:55:18 -0700549 plat = dev_get_parent_plat(dev);
Simon Glassfc03a552015-03-25 12:22:30 -0600550 plat->id = *id;
551 *devp = dev;
552 return 0;
553 }
554 }
555
Simon Glassc79173e2015-03-25 12:22:31 -0600556 /* Bind a generic driver so that the device can be used */
557 snprintf(name, sizeof(name), "generic_bus_%x_dev_%x", bus_seq, devnum);
558 str = strdup(name);
559 if (!str)
560 return -ENOMEM;
561 ret = device_bind_driver(parent, "usb_dev_generic_drv", str, devp);
562
Simon Glassfc03a552015-03-25 12:22:30 -0600563error:
564 debug("%s: No match found: %d\n", __func__, ret);
565 return ret;
566}
567
568/**
Simon Glassd43d7e92015-11-08 23:47:56 -0700569 * usb_find_child() - Find an existing device which matches our needs
570 *
571 *
Simon Glassfc03a552015-03-25 12:22:30 -0600572 */
Simon Glassd43d7e92015-11-08 23:47:56 -0700573static int usb_find_child(struct udevice *parent,
574 struct usb_device_descriptor *desc,
575 struct usb_interface_descriptor *iface,
576 struct udevice **devp)
Simon Glassfc03a552015-03-25 12:22:30 -0600577{
578 struct udevice *dev;
579
580 *devp = NULL;
581 for (device_find_first_child(parent, &dev);
582 dev;
583 device_find_next_child(&dev)) {
Simon Glassb75b15b2020-12-03 16:55:23 -0700584 struct usb_dev_plat *plat = dev_get_parent_plat(dev);
Simon Glassfc03a552015-03-25 12:22:30 -0600585
586 /* If this device is already in use, skip it */
587 if (device_active(dev))
588 continue;
589 debug(" %s: name='%s', plat=%d, desc=%d\n", __func__,
590 dev->name, plat->id.bDeviceClass, desc->bDeviceClass);
591 if (usb_match_one_id(desc, iface, &plat->id)) {
592 *devp = dev;
593 return 0;
594 }
595 }
Simon Glassd43d7e92015-11-08 23:47:56 -0700596
Simon Glassfc03a552015-03-25 12:22:30 -0600597 return -ENOENT;
598}
599
Simon Glass9b82eeb2015-03-25 12:21:59 -0600600int usb_scan_device(struct udevice *parent, int port,
601 enum usb_device_speed speed, struct udevice **devp)
602{
603 struct udevice *dev;
604 bool created = false;
Simon Glassb75b15b2020-12-03 16:55:23 -0700605 struct usb_dev_plat *plat;
Simon Glass9b82eeb2015-03-25 12:21:59 -0600606 struct usb_bus_priv *priv;
607 struct usb_device *parent_udev;
608 int ret;
609 ALLOC_CACHE_ALIGN_BUFFER(struct usb_device, udev, 1);
610 struct usb_interface_descriptor *iface = &udev->config.if_desc[0].desc;
611
612 *devp = NULL;
613 memset(udev, '\0', sizeof(*udev));
Hans de Goede3b5ddd32015-05-05 11:54:31 +0200614 udev->controller_dev = usb_get_bus(parent);
Simon Glass9b82eeb2015-03-25 12:21:59 -0600615 priv = dev_get_uclass_priv(udev->controller_dev);
616
617 /*
618 * Somewhat nasty, this. We create a local device and use the normal
619 * USB stack to read its descriptor. Then we know what type of device
620 * to create for real.
621 *
622 * udev->dev is set to the parent, since we don't have a real device
623 * yet. The USB stack should not access udev.dev anyway, except perhaps
624 * to find the controller, and the controller will either be @parent,
625 * or some parent of @parent.
626 *
627 * Another option might be to create the device as a generic USB
628 * device, then morph it into the correct one when we know what it
629 * should be. This means that a generic USB device would morph into
630 * a network controller, or a USB flash stick, for example. However,
631 * we don't support such morphing and it isn't clear that it would
632 * be easy to do.
633 *
634 * Yet another option is to split out the USB stack parts of udev
635 * into something like a 'struct urb' (as Linux does) which can exist
636 * independently of any device. This feels cleaner, but calls for quite
637 * a big change to the USB stack.
638 *
639 * For now, the approach is to set up an empty udev, read its
640 * descriptor and assign it an address, then bind a real device and
641 * stash the resulting information into the device's parent
642 * platform data. Then when we probe it, usb_child_pre_probe() is called
643 * and it will pull the information out of the stash.
644 */
645 udev->dev = parent;
646 udev->speed = speed;
647 udev->devnum = priv->next_addr + 1;
648 udev->portnr = port;
649 debug("Calling usb_setup_device(), portnr=%d\n", udev->portnr);
650 parent_udev = device_get_uclass_id(parent) == UCLASS_USB_HUB ?
Simon Glassde44acf2015-09-28 23:32:01 -0600651 dev_get_parent_priv(parent) : NULL;
Hans de Goede778dc1c2015-06-17 21:33:46 +0200652 ret = usb_setup_device(udev, priv->desc_before_addr, parent_udev);
Simon Glass9b82eeb2015-03-25 12:21:59 -0600653 debug("read_descriptor for '%s': ret=%d\n", parent->name, ret);
654 if (ret)
655 return ret;
Simon Glassd43d7e92015-11-08 23:47:56 -0700656 ret = usb_find_child(parent, &udev->descriptor, iface, &dev);
657 debug("** usb_find_child returns %d\n", ret);
Simon Glassfc03a552015-03-25 12:22:30 -0600658 if (ret) {
659 if (ret != -ENOENT)
660 return ret;
Michael Walle7c961322020-06-02 01:47:07 +0200661 ret = usb_find_and_bind_driver(parent, &udev->descriptor,
662 iface,
Simon Glass75e534b2020-12-16 21:20:07 -0700663 dev_seq(udev->controller_dev),
Michael Walle7c961322020-06-02 01:47:07 +0200664 udev->devnum, port, &dev);
Simon Glassfc03a552015-03-25 12:22:30 -0600665 if (ret)
666 return ret;
667 created = true;
668 }
Simon Glass71fa5b42020-12-03 16:55:18 -0700669 plat = dev_get_parent_plat(dev);
Simon Glassfc03a552015-03-25 12:22:30 -0600670 debug("%s: Probing '%s', plat=%p\n", __func__, dev->name, plat);
671 plat->devnum = udev->devnum;
Hans de Goede8a0b4c22015-05-05 11:54:32 +0200672 plat->udev = udev;
Simon Glassfc03a552015-03-25 12:22:30 -0600673 priv->next_addr++;
674 ret = device_probe(dev);
675 if (ret) {
676 debug("%s: Device '%s' probe failed\n", __func__, dev->name);
677 priv->next_addr--;
678 if (created)
679 device_unbind(dev);
680 return ret;
681 }
682 *devp = dev;
Simon Glass9b82eeb2015-03-25 12:21:59 -0600683
Simon Glassfc03a552015-03-25 12:22:30 -0600684 return 0;
Simon Glass9b82eeb2015-03-25 12:21:59 -0600685}
686
Simon Glassfbb14942015-05-13 07:02:23 -0600687/*
688 * Detect if a USB device has been plugged or unplugged.
689 */
690int usb_detect_change(void)
691{
692 struct udevice *hub;
693 struct uclass *uc;
694 int change = 0;
695 int ret;
696
697 ret = uclass_get(UCLASS_USB_HUB, &uc);
698 if (ret)
699 return ret;
700
701 uclass_foreach_dev(hub, uc) {
702 struct usb_device *udev;
703 struct udevice *dev;
704
705 if (!device_active(hub))
706 continue;
707 for (device_find_first_child(hub, &dev);
708 dev;
709 device_find_next_child(&dev)) {
710 struct usb_port_status status;
711
712 if (!device_active(dev))
713 continue;
714
Simon Glassde44acf2015-09-28 23:32:01 -0600715 udev = dev_get_parent_priv(dev);
Simon Glassfbb14942015-05-13 07:02:23 -0600716 if (usb_get_port_status(udev, udev->portnr, &status)
717 < 0)
718 /* USB request failed */
719 continue;
720
721 if (le16_to_cpu(status.wPortChange) &
722 USB_PORT_STAT_C_CONNECTION)
723 change++;
724 }
725 }
726
727 return change;
728}
729
Masahiro Yamada6d8e4332017-06-22 16:35:14 +0900730static int usb_child_post_bind(struct udevice *dev)
Simon Glass9b82eeb2015-03-25 12:21:59 -0600731{
Simon Glassb75b15b2020-12-03 16:55:23 -0700732 struct usb_dev_plat *plat = dev_get_parent_plat(dev);
Simon Glass9b82eeb2015-03-25 12:21:59 -0600733 int val;
734
Simon Glassf1d50f72020-12-19 10:40:13 -0700735 if (!dev_has_ofnode(dev))
Simon Glass9b82eeb2015-03-25 12:21:59 -0600736 return 0;
737
738 /* We only support matching a few things */
Simon Glass6deb8e22017-05-18 20:09:38 -0600739 val = dev_read_u32_default(dev, "usb,device-class", -1);
Simon Glass9b82eeb2015-03-25 12:21:59 -0600740 if (val != -1) {
741 plat->id.match_flags |= USB_DEVICE_ID_MATCH_DEV_CLASS;
742 plat->id.bDeviceClass = val;
743 }
Simon Glass6deb8e22017-05-18 20:09:38 -0600744 val = dev_read_u32_default(dev, "usb,interface-class", -1);
Simon Glass9b82eeb2015-03-25 12:21:59 -0600745 if (val != -1) {
746 plat->id.match_flags |= USB_DEVICE_ID_MATCH_INT_CLASS;
747 plat->id.bInterfaceClass = val;
748 }
749
750 return 0;
751}
752
Hans de Goede3b5ddd32015-05-05 11:54:31 +0200753struct udevice *usb_get_bus(struct udevice *dev)
Simon Glass9b82eeb2015-03-25 12:21:59 -0600754{
755 struct udevice *bus;
756
Simon Glass9b82eeb2015-03-25 12:21:59 -0600757 for (bus = dev; bus && device_get_uclass_id(bus) != UCLASS_USB; )
758 bus = bus->parent;
759 if (!bus) {
760 /* By design this cannot happen */
761 assert(bus);
762 debug("USB HUB '%s' does not have a controller\n", dev->name);
Simon Glass9b82eeb2015-03-25 12:21:59 -0600763 }
Simon Glass9b82eeb2015-03-25 12:21:59 -0600764
Hans de Goede3b5ddd32015-05-05 11:54:31 +0200765 return bus;
Simon Glass9b82eeb2015-03-25 12:21:59 -0600766}
767
768int usb_child_pre_probe(struct udevice *dev)
769{
Simon Glassde44acf2015-09-28 23:32:01 -0600770 struct usb_device *udev = dev_get_parent_priv(dev);
Simon Glassb75b15b2020-12-03 16:55:23 -0700771 struct usb_dev_plat *plat = dev_get_parent_plat(dev);
Simon Glass9b82eeb2015-03-25 12:21:59 -0600772 int ret;
773
Hans de Goede8a0b4c22015-05-05 11:54:32 +0200774 if (plat->udev) {
775 /*
776 * Copy over all the values set in the on stack struct
777 * usb_device in usb_scan_device() to our final struct
778 * usb_device for this dev.
779 */
780 *udev = *(plat->udev);
781 /* And clear plat->udev as it will not be valid for long */
782 plat->udev = NULL;
783 udev->dev = dev;
784 } else {
785 /*
786 * This happens with devices which are explicitly bound
787 * instead of being discovered through usb_scan_device()
788 * such as sandbox emul devices.
789 */
790 udev->dev = dev;
791 udev->controller_dev = usb_get_bus(dev);
792 udev->devnum = plat->devnum;
Simon Glass9b82eeb2015-03-25 12:21:59 -0600793
Hans de Goede8a0b4c22015-05-05 11:54:32 +0200794 /*
795 * udev did not go through usb_scan_device(), so we need to
796 * select the config and read the config descriptors.
797 */
798 ret = usb_select_config(udev);
799 if (ret)
800 return ret;
801 }
Simon Glass9b82eeb2015-03-25 12:21:59 -0600802
803 return 0;
804}
805
806UCLASS_DRIVER(usb) = {
807 .id = UCLASS_USB,
808 .name = "usb",
809 .flags = DM_UC_FLAG_SEQ_ALIAS,
Simon Glass18230342016-07-05 17:10:10 -0600810 .post_bind = dm_scan_fdt_dev,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700811 .priv_auto = sizeof(struct usb_uclass_priv),
812 .per_child_auto = sizeof(struct usb_device),
813 .per_device_auto = sizeof(struct usb_bus_priv),
Simon Glass9b82eeb2015-03-25 12:21:59 -0600814 .child_post_bind = usb_child_post_bind,
815 .child_pre_probe = usb_child_pre_probe,
Simon Glassb75b15b2020-12-03 16:55:23 -0700816 .per_child_plat_auto = sizeof(struct usb_dev_plat),
Simon Glass9b82eeb2015-03-25 12:21:59 -0600817};
Simon Glassc79173e2015-03-25 12:22:31 -0600818
819UCLASS_DRIVER(usb_dev_generic) = {
820 .id = UCLASS_USB_DEV_GENERIC,
821 .name = "usb_dev_generic",
822};
823
824U_BOOT_DRIVER(usb_dev_generic_drv) = {
825 .id = UCLASS_USB_DEV_GENERIC,
826 .name = "usb_dev_generic_drv",
827};