blob: 5e423012dfd8faa3af5e17cf2413f612c483b638 [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
9#include <common.h>
10#include <dm.h>
11#include <errno.h>
Simon Glasscf92e052015-09-02 17:24:58 -060012#include <memalign.h>
Simon Glassde312132015-03-25 12:21:59 -060013#include <usb.h>
14#include <dm/device-internal.h>
15#include <dm/lists.h>
Simon Glassde312132015-03-25 12:21:59 -060016#include <dm/uclass-internal.h>
17
Simon Glassde312132015-03-25 12:21:59 -060018extern bool usb_started; /* flag for the started/stopped USB status */
19static bool asynch_allowed;
20
Hans de Goedee2536372015-05-10 14:10:21 +020021struct usb_uclass_priv {
22 int companion_device_count;
23};
24
Marek Vasut31232de2020-04-06 14:29:44 +020025int usb_lock_async(struct usb_device *udev, int lock)
26{
27 struct udevice *bus = udev->controller_dev;
28 struct dm_usb_ops *ops = usb_get_ops(bus);
29
30 if (!ops->lock_async)
31 return -ENOSYS;
32
33 return ops->lock_async(bus, lock);
34}
35
Simon Glassde312132015-03-25 12:21:59 -060036int usb_disable_asynch(int disable)
37{
38 int old_value = asynch_allowed;
39
40 asynch_allowed = !disable;
41 return old_value;
42}
43
44int submit_int_msg(struct usb_device *udev, unsigned long pipe, void *buffer,
Michal Suchanek34371212019-08-18 10:55:27 +020045 int length, int interval, bool nonblock)
Simon Glassde312132015-03-25 12:21:59 -060046{
47 struct udevice *bus = udev->controller_dev;
48 struct dm_usb_ops *ops = usb_get_ops(bus);
49
50 if (!ops->interrupt)
51 return -ENOSYS;
52
Michal Suchanek34371212019-08-18 10:55:27 +020053 return ops->interrupt(bus, udev, pipe, buffer, length, interval,
54 nonblock);
Simon Glassde312132015-03-25 12:21:59 -060055}
56
57int submit_control_msg(struct usb_device *udev, unsigned long pipe,
58 void *buffer, int length, struct devrequest *setup)
59{
60 struct udevice *bus = udev->controller_dev;
61 struct dm_usb_ops *ops = usb_get_ops(bus);
Hans de Goedee2536372015-05-10 14:10:21 +020062 struct usb_uclass_priv *uc_priv = bus->uclass->priv;
63 int err;
Simon Glassde312132015-03-25 12:21:59 -060064
65 if (!ops->control)
66 return -ENOSYS;
67
Hans de Goedee2536372015-05-10 14:10:21 +020068 err = ops->control(bus, udev, pipe, buffer, length, setup);
69 if (setup->request == USB_REQ_SET_FEATURE &&
70 setup->requesttype == USB_RT_PORT &&
71 setup->value == cpu_to_le16(USB_PORT_FEAT_RESET) &&
72 err == -ENXIO) {
73 /* Device handed over to companion after port reset */
74 uc_priv->companion_device_count++;
75 }
76
77 return err;
Simon Glassde312132015-03-25 12:21:59 -060078}
79
80int submit_bulk_msg(struct usb_device *udev, unsigned long pipe, void *buffer,
81 int length)
82{
83 struct udevice *bus = udev->controller_dev;
84 struct dm_usb_ops *ops = usb_get_ops(bus);
85
86 if (!ops->bulk)
87 return -ENOSYS;
88
89 return ops->bulk(bus, udev, pipe, buffer, length);
90}
91
Hans de Goede8a5f0662015-05-10 14:10:18 +020092struct int_queue *create_int_queue(struct usb_device *udev,
93 unsigned long pipe, int queuesize, int elementsize,
94 void *buffer, int interval)
95{
96 struct udevice *bus = udev->controller_dev;
97 struct dm_usb_ops *ops = usb_get_ops(bus);
98
99 if (!ops->create_int_queue)
100 return NULL;
101
102 return ops->create_int_queue(bus, udev, pipe, queuesize, elementsize,
103 buffer, interval);
104}
105
106void *poll_int_queue(struct usb_device *udev, struct int_queue *queue)
107{
108 struct udevice *bus = udev->controller_dev;
109 struct dm_usb_ops *ops = usb_get_ops(bus);
110
111 if (!ops->poll_int_queue)
112 return NULL;
113
114 return ops->poll_int_queue(bus, udev, queue);
115}
116
117int destroy_int_queue(struct usb_device *udev, struct int_queue *queue)
118{
119 struct udevice *bus = udev->controller_dev;
120 struct dm_usb_ops *ops = usb_get_ops(bus);
121
122 if (!ops->destroy_int_queue)
123 return -ENOSYS;
124
125 return ops->destroy_int_queue(bus, udev, queue);
126}
127
Simon Glassde312132015-03-25 12:21:59 -0600128int usb_alloc_device(struct usb_device *udev)
129{
130 struct udevice *bus = udev->controller_dev;
131 struct dm_usb_ops *ops = usb_get_ops(bus);
132
133 /* This is only requird by some controllers - current XHCI */
134 if (!ops->alloc_device)
135 return 0;
136
137 return ops->alloc_device(bus, udev);
138}
139
Hans de Goedeb2f219b2015-06-17 21:33:52 +0200140int usb_reset_root_port(struct usb_device *udev)
141{
142 struct udevice *bus = udev->controller_dev;
143 struct dm_usb_ops *ops = usb_get_ops(bus);
144
145 if (!ops->reset_root_port)
146 return -ENOSYS;
147
148 return ops->reset_root_port(bus, udev);
149}
150
Bin Meng9ca1b4b2017-07-19 21:51:17 +0800151int usb_update_hub_device(struct usb_device *udev)
152{
153 struct udevice *bus = udev->controller_dev;
154 struct dm_usb_ops *ops = usb_get_ops(bus);
155
156 if (!ops->update_hub_device)
157 return -ENOSYS;
158
159 return ops->update_hub_device(bus, udev);
160}
161
Bin Meng3e59f592017-09-07 06:13:17 -0700162int usb_get_max_xfer_size(struct usb_device *udev, size_t *size)
163{
164 struct udevice *bus = udev->controller_dev;
165 struct dm_usb_ops *ops = usb_get_ops(bus);
166
167 if (!ops->get_max_xfer_size)
168 return -ENOSYS;
169
170 return ops->get_max_xfer_size(bus, size);
171}
172
Simon Glassde312132015-03-25 12:21:59 -0600173int usb_stop(void)
174{
175 struct udevice *bus;
Bin Mengd4efefe2017-10-01 06:19:42 -0700176 struct udevice *rh;
Simon Glassde312132015-03-25 12:21:59 -0600177 struct uclass *uc;
Hans de Goedee2536372015-05-10 14:10:21 +0200178 struct usb_uclass_priv *uc_priv;
Simon Glassde312132015-03-25 12:21:59 -0600179 int err = 0, ret;
180
181 /* De-activate any devices that have been activated */
182 ret = uclass_get(UCLASS_USB, &uc);
183 if (ret)
184 return ret;
Hans de Goedee2536372015-05-10 14:10:21 +0200185
186 uc_priv = uc->priv;
187
Simon Glassde312132015-03-25 12:21:59 -0600188 uclass_foreach_dev(bus, uc) {
Stefan Roese706865a2017-03-20 12:51:48 +0100189 ret = device_remove(bus, DM_REMOVE_NORMAL);
Simon Glassde312132015-03-25 12:21:59 -0600190 if (ret && !err)
191 err = ret;
Bin Mengd4efefe2017-10-01 06:19:42 -0700192
193 /* Locate root hub device */
194 device_find_first_child(bus, &rh);
195 if (rh) {
196 /*
197 * All USB devices are children of root hub.
198 * Unbinding root hub will unbind all of its children.
199 */
200 ret = device_unbind(rh);
201 if (ret && !err)
202 err = ret;
203 }
Simon Glassde312132015-03-25 12:21:59 -0600204 }
Bin Mengad0a9372017-10-01 06:19:43 -0700205
Paul Kocialkowski0fa59992015-08-04 17:04:12 +0200206#ifdef CONFIG_USB_STORAGE
Simon Glassde312132015-03-25 12:21:59 -0600207 usb_stor_reset();
Paul Kocialkowski0fa59992015-08-04 17:04:12 +0200208#endif
Hans de Goedee2536372015-05-10 14:10:21 +0200209 uc_priv->companion_device_count = 0;
Simon Glassde312132015-03-25 12:21:59 -0600210 usb_started = 0;
211
212 return err;
213}
214
Hans de Goedea24a0e92015-05-10 14:10:19 +0200215static void usb_scan_bus(struct udevice *bus, bool recurse)
Simon Glassde312132015-03-25 12:21:59 -0600216{
217 struct usb_bus_priv *priv;
218 struct udevice *dev;
219 int ret;
220
221 priv = dev_get_uclass_priv(bus);
222
223 assert(recurse); /* TODO: Support non-recusive */
224
Ismael Luceno Cortes89aea232019-03-19 09:19:44 +0000225 printf("scanning bus %s for devices... ", bus->name);
Hans de Goedea24a0e92015-05-10 14:10:19 +0200226 debug("\n");
Simon Glassde312132015-03-25 12:21:59 -0600227 ret = usb_scan_device(bus, 0, USB_SPEED_FULL, &dev);
228 if (ret)
Hans de Goedea24a0e92015-05-10 14:10:19 +0200229 printf("failed, error %d\n", ret);
230 else if (priv->next_addr == 0)
231 printf("No USB Device found\n");
232 else
233 printf("%d USB Device(s) found\n", priv->next_addr);
Simon Glassde312132015-03-25 12:21:59 -0600234}
235
Simon Glasseae11be2015-11-08 23:48:00 -0700236static void remove_inactive_children(struct uclass *uc, struct udevice *bus)
237{
238 uclass_foreach_dev(bus, uc) {
239 struct udevice *dev, *next;
240
241 if (!device_active(bus))
242 continue;
243 device_foreach_child_safe(dev, next, bus) {
244 if (!device_active(dev))
245 device_unbind(dev);
246 }
247 }
248}
249
Simon Glassde312132015-03-25 12:21:59 -0600250int usb_init(void)
251{
252 int controllers_initialized = 0;
Hans de Goedee2536372015-05-10 14:10:21 +0200253 struct usb_uclass_priv *uc_priv;
Hans de Goedeb6de4d12015-05-10 14:10:20 +0200254 struct usb_bus_priv *priv;
Simon Glassde312132015-03-25 12:21:59 -0600255 struct udevice *bus;
256 struct uclass *uc;
Simon Glassde312132015-03-25 12:21:59 -0600257 int ret;
258
259 asynch_allowed = 1;
Simon Glassde312132015-03-25 12:21:59 -0600260
261 ret = uclass_get(UCLASS_USB, &uc);
262 if (ret)
263 return ret;
264
Hans de Goedee2536372015-05-10 14:10:21 +0200265 uc_priv = uc->priv;
266
Simon Glassde312132015-03-25 12:21:59 -0600267 uclass_foreach_dev(bus, uc) {
268 /* init low_level USB */
Ismael Luceno Cortes89aea232019-03-19 09:19:44 +0000269 printf("Bus %s: ", bus->name);
Bin Mengd4efefe2017-10-01 06:19:42 -0700270
271#ifdef CONFIG_SANDBOX
272 /*
273 * For Sandbox, we need scan the device tree each time when we
274 * start the USB stack, in order to re-create the emulated USB
275 * devices and bind drivers for them before we actually do the
276 * driver probe.
277 */
278 ret = dm_scan_fdt_dev(bus);
279 if (ret) {
280 printf("Sandbox USB device scan failed (%d)\n", ret);
281 continue;
282 }
283#endif
284
Simon Glassde312132015-03-25 12:21:59 -0600285 ret = device_probe(bus);
286 if (ret == -ENODEV) { /* No such device. */
287 puts("Port not available.\n");
288 controllers_initialized++;
289 continue;
290 }
291
292 if (ret) { /* Other error. */
293 printf("probe failed, error %d\n", ret);
294 continue;
295 }
Simon Glassde312132015-03-25 12:21:59 -0600296 controllers_initialized++;
Simon Glassde312132015-03-25 12:21:59 -0600297 usb_started = true;
298 }
299
Hans de Goedeb6de4d12015-05-10 14:10:20 +0200300 /*
301 * lowlevel init done, now scan the bus for devices i.e. search HUBs
302 * and configure them, first scan primary controllers.
303 */
304 uclass_foreach_dev(bus, uc) {
305 if (!device_active(bus))
306 continue;
307
308 priv = dev_get_uclass_priv(bus);
309 if (!priv->companion)
310 usb_scan_bus(bus, true);
311 }
312
313 /*
314 * Now that the primary controllers have been scanned and have handed
315 * over any devices they do not understand to their companions, scan
Hans de Goedee2536372015-05-10 14:10:21 +0200316 * the companions if necessary.
Hans de Goedeb6de4d12015-05-10 14:10:20 +0200317 */
Hans de Goedee2536372015-05-10 14:10:21 +0200318 if (uc_priv->companion_device_count) {
319 uclass_foreach_dev(bus, uc) {
320 if (!device_active(bus))
321 continue;
Hans de Goedeb6de4d12015-05-10 14:10:20 +0200322
Hans de Goedee2536372015-05-10 14:10:21 +0200323 priv = dev_get_uclass_priv(bus);
324 if (priv->companion)
325 usb_scan_bus(bus, true);
326 }
Hans de Goedeb6de4d12015-05-10 14:10:20 +0200327 }
328
Simon Glassde312132015-03-25 12:21:59 -0600329 debug("scan end\n");
Simon Glasseae11be2015-11-08 23:48:00 -0700330
331 /* Remove any devices that were not found on this scan */
332 remove_inactive_children(uc, bus);
333
334 ret = uclass_get(UCLASS_USB_HUB, &uc);
335 if (ret)
336 return ret;
337 remove_inactive_children(uc, bus);
338
Simon Glassde312132015-03-25 12:21:59 -0600339 /* if we were not able to find at least one working bus, bail out */
Ismael Luceno Cortes89aea232019-03-19 09:19:44 +0000340 if (controllers_initialized == 0)
341 printf("No working controllers found\n");
Simon Glassde312132015-03-25 12:21:59 -0600342
343 return usb_started ? 0 : -1;
344}
345
Simon Glasse8ea5e82015-11-08 23:47:59 -0700346/*
347 * TODO(sjg@chromium.org): Remove this legacy function. At present it is needed
348 * to support boards which use driver model for USB but not Ethernet, and want
349 * to use USB Ethernet.
350 *
351 * The #if clause is here to ensure that remains the only case.
352 */
353#if !defined(CONFIG_DM_ETH) && defined(CONFIG_USB_HOST_ETHER)
Simon Glassde312132015-03-25 12:21:59 -0600354static struct usb_device *find_child_devnum(struct udevice *parent, int devnum)
355{
356 struct usb_device *udev;
357 struct udevice *dev;
358
359 if (!device_active(parent))
360 return NULL;
Simon Glassbcbe3d12015-09-28 23:32:01 -0600361 udev = dev_get_parent_priv(parent);
Simon Glassde312132015-03-25 12:21:59 -0600362 if (udev->devnum == devnum)
363 return udev;
364
365 for (device_find_first_child(parent, &dev);
366 dev;
367 device_find_next_child(&dev)) {
368 udev = find_child_devnum(dev, devnum);
369 if (udev)
370 return udev;
371 }
372
373 return NULL;
374}
375
376struct usb_device *usb_get_dev_index(struct udevice *bus, int index)
377{
Hans de Goedefd1bd212015-06-17 21:33:53 +0200378 struct udevice *dev;
Simon Glassde312132015-03-25 12:21:59 -0600379 int devnum = index + 1; /* Addresses are allocated from 1 on USB */
380
Hans de Goedefd1bd212015-06-17 21:33:53 +0200381 device_find_first_child(bus, &dev);
382 if (!dev)
383 return NULL;
Simon Glassde312132015-03-25 12:21:59 -0600384
Hans de Goedefd1bd212015-06-17 21:33:53 +0200385 return find_child_devnum(dev, devnum);
Simon Glassde312132015-03-25 12:21:59 -0600386}
Simon Glasse8ea5e82015-11-08 23:47:59 -0700387#endif
Simon Glassde312132015-03-25 12:21:59 -0600388
Simon Glassfbeceb22015-03-25 12:22:32 -0600389int usb_setup_ehci_gadget(struct ehci_ctrl **ctlrp)
390{
391 struct usb_platdata *plat;
392 struct udevice *dev;
393 int ret;
394
395 /* Find the old device and remove it */
396 ret = uclass_find_device_by_seq(UCLASS_USB, 0, true, &dev);
397 if (ret)
398 return ret;
Stefan Roese706865a2017-03-20 12:51:48 +0100399 ret = device_remove(dev, DM_REMOVE_NORMAL);
Simon Glassfbeceb22015-03-25 12:22:32 -0600400 if (ret)
401 return ret;
402
403 plat = dev_get_platdata(dev);
404 plat->init_type = USB_INIT_DEVICE;
405 ret = device_probe(dev);
406 if (ret)
407 return ret;
408 *ctlrp = dev_get_priv(dev);
409
410 return 0;
411}
412
Simon Glass0566e242015-03-25 12:22:30 -0600413/* returns 0 if no match, 1 if match */
Masahiro Yamada121a4d12017-06-22 16:35:14 +0900414static int usb_match_device(const struct usb_device_descriptor *desc,
415 const struct usb_device_id *id)
Simon Glass0566e242015-03-25 12:22:30 -0600416{
417 if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
418 id->idVendor != le16_to_cpu(desc->idVendor))
419 return 0;
420
421 if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
422 id->idProduct != le16_to_cpu(desc->idProduct))
423 return 0;
424
425 /* No need to test id->bcdDevice_lo != 0, since 0 is never
426 greater than any unsigned number. */
427 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) &&
428 (id->bcdDevice_lo > le16_to_cpu(desc->bcdDevice)))
429 return 0;
430
431 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) &&
432 (id->bcdDevice_hi < le16_to_cpu(desc->bcdDevice)))
433 return 0;
434
435 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) &&
436 (id->bDeviceClass != desc->bDeviceClass))
437 return 0;
438
439 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) &&
440 (id->bDeviceSubClass != desc->bDeviceSubClass))
441 return 0;
442
443 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) &&
444 (id->bDeviceProtocol != desc->bDeviceProtocol))
445 return 0;
446
447 return 1;
448}
449
450/* returns 0 if no match, 1 if match */
Masahiro Yamada121a4d12017-06-22 16:35:14 +0900451static int usb_match_one_id_intf(const struct usb_device_descriptor *desc,
452 const struct usb_interface_descriptor *int_desc,
453 const struct usb_device_id *id)
Simon Glass0566e242015-03-25 12:22:30 -0600454{
455 /* The interface class, subclass, protocol and number should never be
456 * checked for a match if the device class is Vendor Specific,
457 * unless the match record specifies the Vendor ID. */
458 if (desc->bDeviceClass == USB_CLASS_VENDOR_SPEC &&
459 !(id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
460 (id->match_flags & (USB_DEVICE_ID_MATCH_INT_CLASS |
461 USB_DEVICE_ID_MATCH_INT_SUBCLASS |
462 USB_DEVICE_ID_MATCH_INT_PROTOCOL |
463 USB_DEVICE_ID_MATCH_INT_NUMBER)))
464 return 0;
465
466 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) &&
467 (id->bInterfaceClass != int_desc->bInterfaceClass))
468 return 0;
469
470 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) &&
471 (id->bInterfaceSubClass != int_desc->bInterfaceSubClass))
472 return 0;
473
474 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) &&
475 (id->bInterfaceProtocol != int_desc->bInterfaceProtocol))
476 return 0;
477
478 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_NUMBER) &&
479 (id->bInterfaceNumber != int_desc->bInterfaceNumber))
480 return 0;
481
482 return 1;
483}
484
485/* returns 0 if no match, 1 if match */
Masahiro Yamada121a4d12017-06-22 16:35:14 +0900486static int usb_match_one_id(struct usb_device_descriptor *desc,
487 struct usb_interface_descriptor *int_desc,
488 const struct usb_device_id *id)
Simon Glass0566e242015-03-25 12:22:30 -0600489{
490 if (!usb_match_device(desc, id))
491 return 0;
492
493 return usb_match_one_id_intf(desc, int_desc, id);
494}
495
496/**
497 * usb_find_and_bind_driver() - Find and bind the right USB driver
498 *
499 * This only looks at certain fields in the descriptor.
500 */
501static int usb_find_and_bind_driver(struct udevice *parent,
502 struct usb_device_descriptor *desc,
503 struct usb_interface_descriptor *iface,
504 int bus_seq, int devnum,
505 struct udevice **devp)
506{
507 struct usb_driver_entry *start, *entry;
508 int n_ents;
509 int ret;
510 char name[30], *str;
511
512 *devp = NULL;
513 debug("%s: Searching for driver\n", __func__);
514 start = ll_entry_start(struct usb_driver_entry, usb_driver_entry);
515 n_ents = ll_entry_count(struct usb_driver_entry, usb_driver_entry);
516 for (entry = start; entry != start + n_ents; entry++) {
517 const struct usb_device_id *id;
518 struct udevice *dev;
519 const struct driver *drv;
520 struct usb_dev_platdata *plat;
521
522 for (id = entry->match; id->match_flags; id++) {
523 if (!usb_match_one_id(desc, iface, id))
524 continue;
525
526 drv = entry->driver;
527 /*
528 * We could pass the descriptor to the driver as
529 * platdata (instead of NULL) and allow its bind()
530 * method to return -ENOENT if it doesn't support this
531 * device. That way we could continue the search to
532 * find another driver. For now this doesn't seem
533 * necesssary, so just bind the first match.
534 */
535 ret = device_bind(parent, drv, drv->name, NULL, -1,
536 &dev);
537 if (ret)
538 goto error;
539 debug("%s: Match found: %s\n", __func__, drv->name);
540 dev->driver_data = id->driver_info;
541 plat = dev_get_parent_platdata(dev);
542 plat->id = *id;
543 *devp = dev;
544 return 0;
545 }
546 }
547
Simon Glass449230f2015-03-25 12:22:31 -0600548 /* Bind a generic driver so that the device can be used */
549 snprintf(name, sizeof(name), "generic_bus_%x_dev_%x", bus_seq, devnum);
550 str = strdup(name);
551 if (!str)
552 return -ENOMEM;
553 ret = device_bind_driver(parent, "usb_dev_generic_drv", str, devp);
554
Simon Glass0566e242015-03-25 12:22:30 -0600555error:
556 debug("%s: No match found: %d\n", __func__, ret);
557 return ret;
558}
559
560/**
Simon Glass1b6a1df2015-11-08 23:47:56 -0700561 * usb_find_child() - Find an existing device which matches our needs
562 *
563 *
Simon Glass0566e242015-03-25 12:22:30 -0600564 */
Simon Glass1b6a1df2015-11-08 23:47:56 -0700565static int usb_find_child(struct udevice *parent,
566 struct usb_device_descriptor *desc,
567 struct usb_interface_descriptor *iface,
568 struct udevice **devp)
Simon Glass0566e242015-03-25 12:22:30 -0600569{
570 struct udevice *dev;
571
572 *devp = NULL;
573 for (device_find_first_child(parent, &dev);
574 dev;
575 device_find_next_child(&dev)) {
576 struct usb_dev_platdata *plat = dev_get_parent_platdata(dev);
577
578 /* If this device is already in use, skip it */
579 if (device_active(dev))
580 continue;
581 debug(" %s: name='%s', plat=%d, desc=%d\n", __func__,
582 dev->name, plat->id.bDeviceClass, desc->bDeviceClass);
583 if (usb_match_one_id(desc, iface, &plat->id)) {
584 *devp = dev;
585 return 0;
586 }
587 }
Simon Glass1b6a1df2015-11-08 23:47:56 -0700588
Simon Glass0566e242015-03-25 12:22:30 -0600589 return -ENOENT;
590}
591
Simon Glassde312132015-03-25 12:21:59 -0600592int usb_scan_device(struct udevice *parent, int port,
593 enum usb_device_speed speed, struct udevice **devp)
594{
595 struct udevice *dev;
596 bool created = false;
597 struct usb_dev_platdata *plat;
598 struct usb_bus_priv *priv;
599 struct usb_device *parent_udev;
600 int ret;
601 ALLOC_CACHE_ALIGN_BUFFER(struct usb_device, udev, 1);
602 struct usb_interface_descriptor *iface = &udev->config.if_desc[0].desc;
603
604 *devp = NULL;
605 memset(udev, '\0', sizeof(*udev));
Hans de Goedef78a5c02015-05-05 11:54:31 +0200606 udev->controller_dev = usb_get_bus(parent);
Simon Glassde312132015-03-25 12:21:59 -0600607 priv = dev_get_uclass_priv(udev->controller_dev);
608
609 /*
610 * Somewhat nasty, this. We create a local device and use the normal
611 * USB stack to read its descriptor. Then we know what type of device
612 * to create for real.
613 *
614 * udev->dev is set to the parent, since we don't have a real device
615 * yet. The USB stack should not access udev.dev anyway, except perhaps
616 * to find the controller, and the controller will either be @parent,
617 * or some parent of @parent.
618 *
619 * Another option might be to create the device as a generic USB
620 * device, then morph it into the correct one when we know what it
621 * should be. This means that a generic USB device would morph into
622 * a network controller, or a USB flash stick, for example. However,
623 * we don't support such morphing and it isn't clear that it would
624 * be easy to do.
625 *
626 * Yet another option is to split out the USB stack parts of udev
627 * into something like a 'struct urb' (as Linux does) which can exist
628 * independently of any device. This feels cleaner, but calls for quite
629 * a big change to the USB stack.
630 *
631 * For now, the approach is to set up an empty udev, read its
632 * descriptor and assign it an address, then bind a real device and
633 * stash the resulting information into the device's parent
634 * platform data. Then when we probe it, usb_child_pre_probe() is called
635 * and it will pull the information out of the stash.
636 */
637 udev->dev = parent;
638 udev->speed = speed;
639 udev->devnum = priv->next_addr + 1;
640 udev->portnr = port;
641 debug("Calling usb_setup_device(), portnr=%d\n", udev->portnr);
642 parent_udev = device_get_uclass_id(parent) == UCLASS_USB_HUB ?
Simon Glassbcbe3d12015-09-28 23:32:01 -0600643 dev_get_parent_priv(parent) : NULL;
Hans de Goede9eb72dd2015-06-17 21:33:46 +0200644 ret = usb_setup_device(udev, priv->desc_before_addr, parent_udev);
Simon Glassde312132015-03-25 12:21:59 -0600645 debug("read_descriptor for '%s': ret=%d\n", parent->name, ret);
646 if (ret)
647 return ret;
Simon Glass1b6a1df2015-11-08 23:47:56 -0700648 ret = usb_find_child(parent, &udev->descriptor, iface, &dev);
649 debug("** usb_find_child returns %d\n", ret);
Simon Glass0566e242015-03-25 12:22:30 -0600650 if (ret) {
651 if (ret != -ENOENT)
652 return ret;
653 ret = usb_find_and_bind_driver(parent, &udev->descriptor, iface,
654 udev->controller_dev->seq,
655 udev->devnum, &dev);
656 if (ret)
657 return ret;
658 created = true;
659 }
660 plat = dev_get_parent_platdata(dev);
661 debug("%s: Probing '%s', plat=%p\n", __func__, dev->name, plat);
662 plat->devnum = udev->devnum;
Hans de Goede7f1a0752015-05-05 11:54:32 +0200663 plat->udev = udev;
Simon Glass0566e242015-03-25 12:22:30 -0600664 priv->next_addr++;
665 ret = device_probe(dev);
666 if (ret) {
667 debug("%s: Device '%s' probe failed\n", __func__, dev->name);
668 priv->next_addr--;
669 if (created)
670 device_unbind(dev);
671 return ret;
672 }
673 *devp = dev;
Simon Glassde312132015-03-25 12:21:59 -0600674
Simon Glass0566e242015-03-25 12:22:30 -0600675 return 0;
Simon Glassde312132015-03-25 12:21:59 -0600676}
677
Simon Glass0c5dd9a2015-05-13 07:02:23 -0600678/*
679 * Detect if a USB device has been plugged or unplugged.
680 */
681int usb_detect_change(void)
682{
683 struct udevice *hub;
684 struct uclass *uc;
685 int change = 0;
686 int ret;
687
688 ret = uclass_get(UCLASS_USB_HUB, &uc);
689 if (ret)
690 return ret;
691
692 uclass_foreach_dev(hub, uc) {
693 struct usb_device *udev;
694 struct udevice *dev;
695
696 if (!device_active(hub))
697 continue;
698 for (device_find_first_child(hub, &dev);
699 dev;
700 device_find_next_child(&dev)) {
701 struct usb_port_status status;
702
703 if (!device_active(dev))
704 continue;
705
Simon Glassbcbe3d12015-09-28 23:32:01 -0600706 udev = dev_get_parent_priv(dev);
Simon Glass0c5dd9a2015-05-13 07:02:23 -0600707 if (usb_get_port_status(udev, udev->portnr, &status)
708 < 0)
709 /* USB request failed */
710 continue;
711
712 if (le16_to_cpu(status.wPortChange) &
713 USB_PORT_STAT_C_CONNECTION)
714 change++;
715 }
716 }
717
718 return change;
719}
720
Masahiro Yamada121a4d12017-06-22 16:35:14 +0900721static int usb_child_post_bind(struct udevice *dev)
Simon Glassde312132015-03-25 12:21:59 -0600722{
723 struct usb_dev_platdata *plat = dev_get_parent_platdata(dev);
Simon Glassde312132015-03-25 12:21:59 -0600724 int val;
725
Simon Glassd20fd272017-05-18 20:09:38 -0600726 if (!dev_of_valid(dev))
Simon Glassde312132015-03-25 12:21:59 -0600727 return 0;
728
729 /* We only support matching a few things */
Simon Glassd20fd272017-05-18 20:09:38 -0600730 val = dev_read_u32_default(dev, "usb,device-class", -1);
Simon Glassde312132015-03-25 12:21:59 -0600731 if (val != -1) {
732 plat->id.match_flags |= USB_DEVICE_ID_MATCH_DEV_CLASS;
733 plat->id.bDeviceClass = val;
734 }
Simon Glassd20fd272017-05-18 20:09:38 -0600735 val = dev_read_u32_default(dev, "usb,interface-class", -1);
Simon Glassde312132015-03-25 12:21:59 -0600736 if (val != -1) {
737 plat->id.match_flags |= USB_DEVICE_ID_MATCH_INT_CLASS;
738 plat->id.bInterfaceClass = val;
739 }
740
741 return 0;
742}
743
Hans de Goedef78a5c02015-05-05 11:54:31 +0200744struct udevice *usb_get_bus(struct udevice *dev)
Simon Glassde312132015-03-25 12:21:59 -0600745{
746 struct udevice *bus;
747
Simon Glassde312132015-03-25 12:21:59 -0600748 for (bus = dev; bus && device_get_uclass_id(bus) != UCLASS_USB; )
749 bus = bus->parent;
750 if (!bus) {
751 /* By design this cannot happen */
752 assert(bus);
753 debug("USB HUB '%s' does not have a controller\n", dev->name);
Simon Glassde312132015-03-25 12:21:59 -0600754 }
Simon Glassde312132015-03-25 12:21:59 -0600755
Hans de Goedef78a5c02015-05-05 11:54:31 +0200756 return bus;
Simon Glassde312132015-03-25 12:21:59 -0600757}
758
759int usb_child_pre_probe(struct udevice *dev)
760{
Simon Glassbcbe3d12015-09-28 23:32:01 -0600761 struct usb_device *udev = dev_get_parent_priv(dev);
Simon Glassde312132015-03-25 12:21:59 -0600762 struct usb_dev_platdata *plat = dev_get_parent_platdata(dev);
763 int ret;
764
Hans de Goede7f1a0752015-05-05 11:54:32 +0200765 if (plat->udev) {
766 /*
767 * Copy over all the values set in the on stack struct
768 * usb_device in usb_scan_device() to our final struct
769 * usb_device for this dev.
770 */
771 *udev = *(plat->udev);
772 /* And clear plat->udev as it will not be valid for long */
773 plat->udev = NULL;
774 udev->dev = dev;
775 } else {
776 /*
777 * This happens with devices which are explicitly bound
778 * instead of being discovered through usb_scan_device()
779 * such as sandbox emul devices.
780 */
781 udev->dev = dev;
782 udev->controller_dev = usb_get_bus(dev);
783 udev->devnum = plat->devnum;
Simon Glassde312132015-03-25 12:21:59 -0600784
Hans de Goede7f1a0752015-05-05 11:54:32 +0200785 /*
786 * udev did not go through usb_scan_device(), so we need to
787 * select the config and read the config descriptors.
788 */
789 ret = usb_select_config(udev);
790 if (ret)
791 return ret;
792 }
Simon Glassde312132015-03-25 12:21:59 -0600793
794 return 0;
795}
796
797UCLASS_DRIVER(usb) = {
798 .id = UCLASS_USB,
799 .name = "usb",
800 .flags = DM_UC_FLAG_SEQ_ALIAS,
Simon Glass91195482016-07-05 17:10:10 -0600801 .post_bind = dm_scan_fdt_dev,
Hans de Goedee2536372015-05-10 14:10:21 +0200802 .priv_auto_alloc_size = sizeof(struct usb_uclass_priv),
Simon Glassde312132015-03-25 12:21:59 -0600803 .per_child_auto_alloc_size = sizeof(struct usb_device),
804 .per_device_auto_alloc_size = sizeof(struct usb_bus_priv),
805 .child_post_bind = usb_child_post_bind,
806 .child_pre_probe = usb_child_pre_probe,
807 .per_child_platdata_auto_alloc_size = sizeof(struct usb_dev_platdata),
808};
Simon Glass449230f2015-03-25 12:22:31 -0600809
810UCLASS_DRIVER(usb_dev_generic) = {
811 .id = UCLASS_USB_DEV_GENERIC,
812 .name = "usb_dev_generic",
813};
814
815U_BOOT_DRIVER(usb_dev_generic_drv) = {
816 .id = UCLASS_USB_DEV_GENERIC,
817 .name = "usb_dev_generic_drv",
818};