blob: 852165158869f7031c48b76d11281f3e5d519aef [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
Simon Glassde312132015-03-25 12:21:59 -060025int usb_disable_asynch(int disable)
26{
27 int old_value = asynch_allowed;
28
29 asynch_allowed = !disable;
30 return old_value;
31}
32
33int submit_int_msg(struct usb_device *udev, unsigned long pipe, void *buffer,
Michal Suchanek34371212019-08-18 10:55:27 +020034 int length, int interval, bool nonblock)
Simon Glassde312132015-03-25 12:21:59 -060035{
36 struct udevice *bus = udev->controller_dev;
37 struct dm_usb_ops *ops = usb_get_ops(bus);
38
39 if (!ops->interrupt)
40 return -ENOSYS;
41
Michal Suchanek34371212019-08-18 10:55:27 +020042 return ops->interrupt(bus, udev, pipe, buffer, length, interval,
43 nonblock);
Simon Glassde312132015-03-25 12:21:59 -060044}
45
46int submit_control_msg(struct usb_device *udev, unsigned long pipe,
47 void *buffer, int length, struct devrequest *setup)
48{
49 struct udevice *bus = udev->controller_dev;
50 struct dm_usb_ops *ops = usb_get_ops(bus);
Hans de Goedee2536372015-05-10 14:10:21 +020051 struct usb_uclass_priv *uc_priv = bus->uclass->priv;
52 int err;
Simon Glassde312132015-03-25 12:21:59 -060053
54 if (!ops->control)
55 return -ENOSYS;
56
Hans de Goedee2536372015-05-10 14:10:21 +020057 err = ops->control(bus, udev, pipe, buffer, length, setup);
58 if (setup->request == USB_REQ_SET_FEATURE &&
59 setup->requesttype == USB_RT_PORT &&
60 setup->value == cpu_to_le16(USB_PORT_FEAT_RESET) &&
61 err == -ENXIO) {
62 /* Device handed over to companion after port reset */
63 uc_priv->companion_device_count++;
64 }
65
66 return err;
Simon Glassde312132015-03-25 12:21:59 -060067}
68
69int submit_bulk_msg(struct usb_device *udev, unsigned long pipe, void *buffer,
70 int length)
71{
72 struct udevice *bus = udev->controller_dev;
73 struct dm_usb_ops *ops = usb_get_ops(bus);
74
75 if (!ops->bulk)
76 return -ENOSYS;
77
78 return ops->bulk(bus, udev, pipe, buffer, length);
79}
80
Hans de Goede8a5f0662015-05-10 14:10:18 +020081struct int_queue *create_int_queue(struct usb_device *udev,
82 unsigned long pipe, int queuesize, int elementsize,
83 void *buffer, int interval)
84{
85 struct udevice *bus = udev->controller_dev;
86 struct dm_usb_ops *ops = usb_get_ops(bus);
87
88 if (!ops->create_int_queue)
89 return NULL;
90
91 return ops->create_int_queue(bus, udev, pipe, queuesize, elementsize,
92 buffer, interval);
93}
94
95void *poll_int_queue(struct usb_device *udev, struct int_queue *queue)
96{
97 struct udevice *bus = udev->controller_dev;
98 struct dm_usb_ops *ops = usb_get_ops(bus);
99
100 if (!ops->poll_int_queue)
101 return NULL;
102
103 return ops->poll_int_queue(bus, udev, queue);
104}
105
106int destroy_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->destroy_int_queue)
112 return -ENOSYS;
113
114 return ops->destroy_int_queue(bus, udev, queue);
115}
116
Simon Glassde312132015-03-25 12:21:59 -0600117int usb_alloc_device(struct usb_device *udev)
118{
119 struct udevice *bus = udev->controller_dev;
120 struct dm_usb_ops *ops = usb_get_ops(bus);
121
122 /* This is only requird by some controllers - current XHCI */
123 if (!ops->alloc_device)
124 return 0;
125
126 return ops->alloc_device(bus, udev);
127}
128
Hans de Goedeb2f219b2015-06-17 21:33:52 +0200129int usb_reset_root_port(struct usb_device *udev)
130{
131 struct udevice *bus = udev->controller_dev;
132 struct dm_usb_ops *ops = usb_get_ops(bus);
133
134 if (!ops->reset_root_port)
135 return -ENOSYS;
136
137 return ops->reset_root_port(bus, udev);
138}
139
Bin Meng9ca1b4b2017-07-19 21:51:17 +0800140int usb_update_hub_device(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->update_hub_device)
146 return -ENOSYS;
147
148 return ops->update_hub_device(bus, udev);
149}
150
Bin Meng3e59f592017-09-07 06:13:17 -0700151int usb_get_max_xfer_size(struct usb_device *udev, size_t *size)
152{
153 struct udevice *bus = udev->controller_dev;
154 struct dm_usb_ops *ops = usb_get_ops(bus);
155
156 if (!ops->get_max_xfer_size)
157 return -ENOSYS;
158
159 return ops->get_max_xfer_size(bus, size);
160}
161
Simon Glassde312132015-03-25 12:21:59 -0600162int usb_stop(void)
163{
164 struct udevice *bus;
Bin Mengd4efefe2017-10-01 06:19:42 -0700165 struct udevice *rh;
Simon Glassde312132015-03-25 12:21:59 -0600166 struct uclass *uc;
Hans de Goedee2536372015-05-10 14:10:21 +0200167 struct usb_uclass_priv *uc_priv;
Simon Glassde312132015-03-25 12:21:59 -0600168 int err = 0, ret;
169
170 /* De-activate any devices that have been activated */
171 ret = uclass_get(UCLASS_USB, &uc);
172 if (ret)
173 return ret;
Hans de Goedee2536372015-05-10 14:10:21 +0200174
175 uc_priv = uc->priv;
176
Simon Glassde312132015-03-25 12:21:59 -0600177 uclass_foreach_dev(bus, uc) {
Stefan Roese706865a2017-03-20 12:51:48 +0100178 ret = device_remove(bus, DM_REMOVE_NORMAL);
Simon Glassde312132015-03-25 12:21:59 -0600179 if (ret && !err)
180 err = ret;
Bin Mengd4efefe2017-10-01 06:19:42 -0700181
182 /* Locate root hub device */
183 device_find_first_child(bus, &rh);
184 if (rh) {
185 /*
186 * All USB devices are children of root hub.
187 * Unbinding root hub will unbind all of its children.
188 */
189 ret = device_unbind(rh);
190 if (ret && !err)
191 err = ret;
192 }
Simon Glassde312132015-03-25 12:21:59 -0600193 }
Bin Mengad0a9372017-10-01 06:19:43 -0700194
Paul Kocialkowski0fa59992015-08-04 17:04:12 +0200195#ifdef CONFIG_USB_STORAGE
Simon Glassde312132015-03-25 12:21:59 -0600196 usb_stor_reset();
Paul Kocialkowski0fa59992015-08-04 17:04:12 +0200197#endif
Hans de Goedee2536372015-05-10 14:10:21 +0200198 uc_priv->companion_device_count = 0;
Simon Glassde312132015-03-25 12:21:59 -0600199 usb_started = 0;
200
201 return err;
202}
203
Hans de Goedea24a0e92015-05-10 14:10:19 +0200204static void usb_scan_bus(struct udevice *bus, bool recurse)
Simon Glassde312132015-03-25 12:21:59 -0600205{
206 struct usb_bus_priv *priv;
207 struct udevice *dev;
208 int ret;
209
210 priv = dev_get_uclass_priv(bus);
211
212 assert(recurse); /* TODO: Support non-recusive */
213
Ismael Luceno Cortes89aea232019-03-19 09:19:44 +0000214 printf("scanning bus %s for devices... ", bus->name);
Hans de Goedea24a0e92015-05-10 14:10:19 +0200215 debug("\n");
Simon Glassde312132015-03-25 12:21:59 -0600216 ret = usb_scan_device(bus, 0, USB_SPEED_FULL, &dev);
217 if (ret)
Hans de Goedea24a0e92015-05-10 14:10:19 +0200218 printf("failed, error %d\n", ret);
219 else if (priv->next_addr == 0)
220 printf("No USB Device found\n");
221 else
222 printf("%d USB Device(s) found\n", priv->next_addr);
Simon Glassde312132015-03-25 12:21:59 -0600223}
224
Simon Glasseae11be2015-11-08 23:48:00 -0700225static void remove_inactive_children(struct uclass *uc, struct udevice *bus)
226{
227 uclass_foreach_dev(bus, uc) {
228 struct udevice *dev, *next;
229
230 if (!device_active(bus))
231 continue;
232 device_foreach_child_safe(dev, next, bus) {
233 if (!device_active(dev))
234 device_unbind(dev);
235 }
236 }
237}
238
Simon Glassde312132015-03-25 12:21:59 -0600239int usb_init(void)
240{
241 int controllers_initialized = 0;
Hans de Goedee2536372015-05-10 14:10:21 +0200242 struct usb_uclass_priv *uc_priv;
Hans de Goedeb6de4d12015-05-10 14:10:20 +0200243 struct usb_bus_priv *priv;
Simon Glassde312132015-03-25 12:21:59 -0600244 struct udevice *bus;
245 struct uclass *uc;
Simon Glassde312132015-03-25 12:21:59 -0600246 int ret;
247
248 asynch_allowed = 1;
Simon Glassde312132015-03-25 12:21:59 -0600249
250 ret = uclass_get(UCLASS_USB, &uc);
251 if (ret)
252 return ret;
253
Hans de Goedee2536372015-05-10 14:10:21 +0200254 uc_priv = uc->priv;
255
Simon Glassde312132015-03-25 12:21:59 -0600256 uclass_foreach_dev(bus, uc) {
257 /* init low_level USB */
Ismael Luceno Cortes89aea232019-03-19 09:19:44 +0000258 printf("Bus %s: ", bus->name);
Bin Mengd4efefe2017-10-01 06:19:42 -0700259
260#ifdef CONFIG_SANDBOX
261 /*
262 * For Sandbox, we need scan the device tree each time when we
263 * start the USB stack, in order to re-create the emulated USB
264 * devices and bind drivers for them before we actually do the
265 * driver probe.
266 */
267 ret = dm_scan_fdt_dev(bus);
268 if (ret) {
269 printf("Sandbox USB device scan failed (%d)\n", ret);
270 continue;
271 }
272#endif
273
Simon Glassde312132015-03-25 12:21:59 -0600274 ret = device_probe(bus);
275 if (ret == -ENODEV) { /* No such device. */
276 puts("Port not available.\n");
277 controllers_initialized++;
278 continue;
279 }
280
281 if (ret) { /* Other error. */
282 printf("probe failed, error %d\n", ret);
283 continue;
284 }
Simon Glassde312132015-03-25 12:21:59 -0600285 controllers_initialized++;
Simon Glassde312132015-03-25 12:21:59 -0600286 usb_started = true;
287 }
288
Hans de Goedeb6de4d12015-05-10 14:10:20 +0200289 /*
290 * lowlevel init done, now scan the bus for devices i.e. search HUBs
291 * and configure them, first scan primary controllers.
292 */
293 uclass_foreach_dev(bus, uc) {
294 if (!device_active(bus))
295 continue;
296
297 priv = dev_get_uclass_priv(bus);
298 if (!priv->companion)
299 usb_scan_bus(bus, true);
300 }
301
302 /*
303 * Now that the primary controllers have been scanned and have handed
304 * over any devices they do not understand to their companions, scan
Hans de Goedee2536372015-05-10 14:10:21 +0200305 * the companions if necessary.
Hans de Goedeb6de4d12015-05-10 14:10:20 +0200306 */
Hans de Goedee2536372015-05-10 14:10:21 +0200307 if (uc_priv->companion_device_count) {
308 uclass_foreach_dev(bus, uc) {
309 if (!device_active(bus))
310 continue;
Hans de Goedeb6de4d12015-05-10 14:10:20 +0200311
Hans de Goedee2536372015-05-10 14:10:21 +0200312 priv = dev_get_uclass_priv(bus);
313 if (priv->companion)
314 usb_scan_bus(bus, true);
315 }
Hans de Goedeb6de4d12015-05-10 14:10:20 +0200316 }
317
Simon Glassde312132015-03-25 12:21:59 -0600318 debug("scan end\n");
Simon Glasseae11be2015-11-08 23:48:00 -0700319
320 /* Remove any devices that were not found on this scan */
321 remove_inactive_children(uc, bus);
322
323 ret = uclass_get(UCLASS_USB_HUB, &uc);
324 if (ret)
325 return ret;
326 remove_inactive_children(uc, bus);
327
Simon Glassde312132015-03-25 12:21:59 -0600328 /* if we were not able to find at least one working bus, bail out */
Ismael Luceno Cortes89aea232019-03-19 09:19:44 +0000329 if (controllers_initialized == 0)
330 printf("No working controllers found\n");
Simon Glassde312132015-03-25 12:21:59 -0600331
332 return usb_started ? 0 : -1;
333}
334
Simon Glasse8ea5e82015-11-08 23:47:59 -0700335/*
336 * TODO(sjg@chromium.org): Remove this legacy function. At present it is needed
337 * to support boards which use driver model for USB but not Ethernet, and want
338 * to use USB Ethernet.
339 *
340 * The #if clause is here to ensure that remains the only case.
341 */
342#if !defined(CONFIG_DM_ETH) && defined(CONFIG_USB_HOST_ETHER)
Simon Glassde312132015-03-25 12:21:59 -0600343static struct usb_device *find_child_devnum(struct udevice *parent, int devnum)
344{
345 struct usb_device *udev;
346 struct udevice *dev;
347
348 if (!device_active(parent))
349 return NULL;
Simon Glassbcbe3d12015-09-28 23:32:01 -0600350 udev = dev_get_parent_priv(parent);
Simon Glassde312132015-03-25 12:21:59 -0600351 if (udev->devnum == devnum)
352 return udev;
353
354 for (device_find_first_child(parent, &dev);
355 dev;
356 device_find_next_child(&dev)) {
357 udev = find_child_devnum(dev, devnum);
358 if (udev)
359 return udev;
360 }
361
362 return NULL;
363}
364
365struct usb_device *usb_get_dev_index(struct udevice *bus, int index)
366{
Hans de Goedefd1bd212015-06-17 21:33:53 +0200367 struct udevice *dev;
Simon Glassde312132015-03-25 12:21:59 -0600368 int devnum = index + 1; /* Addresses are allocated from 1 on USB */
369
Hans de Goedefd1bd212015-06-17 21:33:53 +0200370 device_find_first_child(bus, &dev);
371 if (!dev)
372 return NULL;
Simon Glassde312132015-03-25 12:21:59 -0600373
Hans de Goedefd1bd212015-06-17 21:33:53 +0200374 return find_child_devnum(dev, devnum);
Simon Glassde312132015-03-25 12:21:59 -0600375}
Simon Glasse8ea5e82015-11-08 23:47:59 -0700376#endif
Simon Glassde312132015-03-25 12:21:59 -0600377
Simon Glassfbeceb22015-03-25 12:22:32 -0600378int usb_setup_ehci_gadget(struct ehci_ctrl **ctlrp)
379{
380 struct usb_platdata *plat;
381 struct udevice *dev;
382 int ret;
383
384 /* Find the old device and remove it */
385 ret = uclass_find_device_by_seq(UCLASS_USB, 0, true, &dev);
386 if (ret)
387 return ret;
Stefan Roese706865a2017-03-20 12:51:48 +0100388 ret = device_remove(dev, DM_REMOVE_NORMAL);
Simon Glassfbeceb22015-03-25 12:22:32 -0600389 if (ret)
390 return ret;
391
392 plat = dev_get_platdata(dev);
393 plat->init_type = USB_INIT_DEVICE;
394 ret = device_probe(dev);
395 if (ret)
396 return ret;
397 *ctlrp = dev_get_priv(dev);
398
399 return 0;
400}
401
Simon Glass0566e242015-03-25 12:22:30 -0600402/* returns 0 if no match, 1 if match */
Masahiro Yamada121a4d12017-06-22 16:35:14 +0900403static int usb_match_device(const struct usb_device_descriptor *desc,
404 const struct usb_device_id *id)
Simon Glass0566e242015-03-25 12:22:30 -0600405{
406 if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
407 id->idVendor != le16_to_cpu(desc->idVendor))
408 return 0;
409
410 if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
411 id->idProduct != le16_to_cpu(desc->idProduct))
412 return 0;
413
414 /* No need to test id->bcdDevice_lo != 0, since 0 is never
415 greater than any unsigned number. */
416 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) &&
417 (id->bcdDevice_lo > le16_to_cpu(desc->bcdDevice)))
418 return 0;
419
420 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) &&
421 (id->bcdDevice_hi < le16_to_cpu(desc->bcdDevice)))
422 return 0;
423
424 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) &&
425 (id->bDeviceClass != desc->bDeviceClass))
426 return 0;
427
428 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) &&
429 (id->bDeviceSubClass != desc->bDeviceSubClass))
430 return 0;
431
432 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) &&
433 (id->bDeviceProtocol != desc->bDeviceProtocol))
434 return 0;
435
436 return 1;
437}
438
439/* returns 0 if no match, 1 if match */
Masahiro Yamada121a4d12017-06-22 16:35:14 +0900440static int usb_match_one_id_intf(const struct usb_device_descriptor *desc,
441 const struct usb_interface_descriptor *int_desc,
442 const struct usb_device_id *id)
Simon Glass0566e242015-03-25 12:22:30 -0600443{
444 /* The interface class, subclass, protocol and number should never be
445 * checked for a match if the device class is Vendor Specific,
446 * unless the match record specifies the Vendor ID. */
447 if (desc->bDeviceClass == USB_CLASS_VENDOR_SPEC &&
448 !(id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
449 (id->match_flags & (USB_DEVICE_ID_MATCH_INT_CLASS |
450 USB_DEVICE_ID_MATCH_INT_SUBCLASS |
451 USB_DEVICE_ID_MATCH_INT_PROTOCOL |
452 USB_DEVICE_ID_MATCH_INT_NUMBER)))
453 return 0;
454
455 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) &&
456 (id->bInterfaceClass != int_desc->bInterfaceClass))
457 return 0;
458
459 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) &&
460 (id->bInterfaceSubClass != int_desc->bInterfaceSubClass))
461 return 0;
462
463 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) &&
464 (id->bInterfaceProtocol != int_desc->bInterfaceProtocol))
465 return 0;
466
467 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_NUMBER) &&
468 (id->bInterfaceNumber != int_desc->bInterfaceNumber))
469 return 0;
470
471 return 1;
472}
473
474/* returns 0 if no match, 1 if match */
Masahiro Yamada121a4d12017-06-22 16:35:14 +0900475static int usb_match_one_id(struct usb_device_descriptor *desc,
476 struct usb_interface_descriptor *int_desc,
477 const struct usb_device_id *id)
Simon Glass0566e242015-03-25 12:22:30 -0600478{
479 if (!usb_match_device(desc, id))
480 return 0;
481
482 return usb_match_one_id_intf(desc, int_desc, id);
483}
484
485/**
486 * usb_find_and_bind_driver() - Find and bind the right USB driver
487 *
488 * This only looks at certain fields in the descriptor.
489 */
490static int usb_find_and_bind_driver(struct udevice *parent,
491 struct usb_device_descriptor *desc,
492 struct usb_interface_descriptor *iface,
493 int bus_seq, int devnum,
494 struct udevice **devp)
495{
496 struct usb_driver_entry *start, *entry;
497 int n_ents;
498 int ret;
499 char name[30], *str;
500
501 *devp = NULL;
502 debug("%s: Searching for driver\n", __func__);
503 start = ll_entry_start(struct usb_driver_entry, usb_driver_entry);
504 n_ents = ll_entry_count(struct usb_driver_entry, usb_driver_entry);
505 for (entry = start; entry != start + n_ents; entry++) {
506 const struct usb_device_id *id;
507 struct udevice *dev;
508 const struct driver *drv;
509 struct usb_dev_platdata *plat;
510
511 for (id = entry->match; id->match_flags; id++) {
512 if (!usb_match_one_id(desc, iface, id))
513 continue;
514
515 drv = entry->driver;
516 /*
517 * We could pass the descriptor to the driver as
518 * platdata (instead of NULL) and allow its bind()
519 * method to return -ENOENT if it doesn't support this
520 * device. That way we could continue the search to
521 * find another driver. For now this doesn't seem
522 * necesssary, so just bind the first match.
523 */
524 ret = device_bind(parent, drv, drv->name, NULL, -1,
525 &dev);
526 if (ret)
527 goto error;
528 debug("%s: Match found: %s\n", __func__, drv->name);
529 dev->driver_data = id->driver_info;
530 plat = dev_get_parent_platdata(dev);
531 plat->id = *id;
532 *devp = dev;
533 return 0;
534 }
535 }
536
Simon Glass449230f2015-03-25 12:22:31 -0600537 /* Bind a generic driver so that the device can be used */
538 snprintf(name, sizeof(name), "generic_bus_%x_dev_%x", bus_seq, devnum);
539 str = strdup(name);
540 if (!str)
541 return -ENOMEM;
542 ret = device_bind_driver(parent, "usb_dev_generic_drv", str, devp);
543
Simon Glass0566e242015-03-25 12:22:30 -0600544error:
545 debug("%s: No match found: %d\n", __func__, ret);
546 return ret;
547}
548
549/**
Simon Glass1b6a1df2015-11-08 23:47:56 -0700550 * usb_find_child() - Find an existing device which matches our needs
551 *
552 *
Simon Glass0566e242015-03-25 12:22:30 -0600553 */
Simon Glass1b6a1df2015-11-08 23:47:56 -0700554static int usb_find_child(struct udevice *parent,
555 struct usb_device_descriptor *desc,
556 struct usb_interface_descriptor *iface,
557 struct udevice **devp)
Simon Glass0566e242015-03-25 12:22:30 -0600558{
559 struct udevice *dev;
560
561 *devp = NULL;
562 for (device_find_first_child(parent, &dev);
563 dev;
564 device_find_next_child(&dev)) {
565 struct usb_dev_platdata *plat = dev_get_parent_platdata(dev);
566
567 /* If this device is already in use, skip it */
568 if (device_active(dev))
569 continue;
570 debug(" %s: name='%s', plat=%d, desc=%d\n", __func__,
571 dev->name, plat->id.bDeviceClass, desc->bDeviceClass);
572 if (usb_match_one_id(desc, iface, &plat->id)) {
573 *devp = dev;
574 return 0;
575 }
576 }
Simon Glass1b6a1df2015-11-08 23:47:56 -0700577
Simon Glass0566e242015-03-25 12:22:30 -0600578 return -ENOENT;
579}
580
Simon Glassde312132015-03-25 12:21:59 -0600581int usb_scan_device(struct udevice *parent, int port,
582 enum usb_device_speed speed, struct udevice **devp)
583{
584 struct udevice *dev;
585 bool created = false;
586 struct usb_dev_platdata *plat;
587 struct usb_bus_priv *priv;
588 struct usb_device *parent_udev;
589 int ret;
590 ALLOC_CACHE_ALIGN_BUFFER(struct usb_device, udev, 1);
591 struct usb_interface_descriptor *iface = &udev->config.if_desc[0].desc;
592
593 *devp = NULL;
594 memset(udev, '\0', sizeof(*udev));
Hans de Goedef78a5c02015-05-05 11:54:31 +0200595 udev->controller_dev = usb_get_bus(parent);
Simon Glassde312132015-03-25 12:21:59 -0600596 priv = dev_get_uclass_priv(udev->controller_dev);
597
598 /*
599 * Somewhat nasty, this. We create a local device and use the normal
600 * USB stack to read its descriptor. Then we know what type of device
601 * to create for real.
602 *
603 * udev->dev is set to the parent, since we don't have a real device
604 * yet. The USB stack should not access udev.dev anyway, except perhaps
605 * to find the controller, and the controller will either be @parent,
606 * or some parent of @parent.
607 *
608 * Another option might be to create the device as a generic USB
609 * device, then morph it into the correct one when we know what it
610 * should be. This means that a generic USB device would morph into
611 * a network controller, or a USB flash stick, for example. However,
612 * we don't support such morphing and it isn't clear that it would
613 * be easy to do.
614 *
615 * Yet another option is to split out the USB stack parts of udev
616 * into something like a 'struct urb' (as Linux does) which can exist
617 * independently of any device. This feels cleaner, but calls for quite
618 * a big change to the USB stack.
619 *
620 * For now, the approach is to set up an empty udev, read its
621 * descriptor and assign it an address, then bind a real device and
622 * stash the resulting information into the device's parent
623 * platform data. Then when we probe it, usb_child_pre_probe() is called
624 * and it will pull the information out of the stash.
625 */
626 udev->dev = parent;
627 udev->speed = speed;
628 udev->devnum = priv->next_addr + 1;
629 udev->portnr = port;
630 debug("Calling usb_setup_device(), portnr=%d\n", udev->portnr);
631 parent_udev = device_get_uclass_id(parent) == UCLASS_USB_HUB ?
Simon Glassbcbe3d12015-09-28 23:32:01 -0600632 dev_get_parent_priv(parent) : NULL;
Hans de Goede9eb72dd2015-06-17 21:33:46 +0200633 ret = usb_setup_device(udev, priv->desc_before_addr, parent_udev);
Simon Glassde312132015-03-25 12:21:59 -0600634 debug("read_descriptor for '%s': ret=%d\n", parent->name, ret);
635 if (ret)
636 return ret;
Simon Glass1b6a1df2015-11-08 23:47:56 -0700637 ret = usb_find_child(parent, &udev->descriptor, iface, &dev);
638 debug("** usb_find_child returns %d\n", ret);
Simon Glass0566e242015-03-25 12:22:30 -0600639 if (ret) {
640 if (ret != -ENOENT)
641 return ret;
642 ret = usb_find_and_bind_driver(parent, &udev->descriptor, iface,
643 udev->controller_dev->seq,
644 udev->devnum, &dev);
645 if (ret)
646 return ret;
647 created = true;
648 }
649 plat = dev_get_parent_platdata(dev);
650 debug("%s: Probing '%s', plat=%p\n", __func__, dev->name, plat);
651 plat->devnum = udev->devnum;
Hans de Goede7f1a0752015-05-05 11:54:32 +0200652 plat->udev = udev;
Simon Glass0566e242015-03-25 12:22:30 -0600653 priv->next_addr++;
654 ret = device_probe(dev);
655 if (ret) {
656 debug("%s: Device '%s' probe failed\n", __func__, dev->name);
657 priv->next_addr--;
658 if (created)
659 device_unbind(dev);
660 return ret;
661 }
662 *devp = dev;
Simon Glassde312132015-03-25 12:21:59 -0600663
Simon Glass0566e242015-03-25 12:22:30 -0600664 return 0;
Simon Glassde312132015-03-25 12:21:59 -0600665}
666
Simon Glass0c5dd9a2015-05-13 07:02:23 -0600667/*
668 * Detect if a USB device has been plugged or unplugged.
669 */
670int usb_detect_change(void)
671{
672 struct udevice *hub;
673 struct uclass *uc;
674 int change = 0;
675 int ret;
676
677 ret = uclass_get(UCLASS_USB_HUB, &uc);
678 if (ret)
679 return ret;
680
681 uclass_foreach_dev(hub, uc) {
682 struct usb_device *udev;
683 struct udevice *dev;
684
685 if (!device_active(hub))
686 continue;
687 for (device_find_first_child(hub, &dev);
688 dev;
689 device_find_next_child(&dev)) {
690 struct usb_port_status status;
691
692 if (!device_active(dev))
693 continue;
694
Simon Glassbcbe3d12015-09-28 23:32:01 -0600695 udev = dev_get_parent_priv(dev);
Simon Glass0c5dd9a2015-05-13 07:02:23 -0600696 if (usb_get_port_status(udev, udev->portnr, &status)
697 < 0)
698 /* USB request failed */
699 continue;
700
701 if (le16_to_cpu(status.wPortChange) &
702 USB_PORT_STAT_C_CONNECTION)
703 change++;
704 }
705 }
706
707 return change;
708}
709
Masahiro Yamada121a4d12017-06-22 16:35:14 +0900710static int usb_child_post_bind(struct udevice *dev)
Simon Glassde312132015-03-25 12:21:59 -0600711{
712 struct usb_dev_platdata *plat = dev_get_parent_platdata(dev);
Simon Glassde312132015-03-25 12:21:59 -0600713 int val;
714
Simon Glassd20fd272017-05-18 20:09:38 -0600715 if (!dev_of_valid(dev))
Simon Glassde312132015-03-25 12:21:59 -0600716 return 0;
717
718 /* We only support matching a few things */
Simon Glassd20fd272017-05-18 20:09:38 -0600719 val = dev_read_u32_default(dev, "usb,device-class", -1);
Simon Glassde312132015-03-25 12:21:59 -0600720 if (val != -1) {
721 plat->id.match_flags |= USB_DEVICE_ID_MATCH_DEV_CLASS;
722 plat->id.bDeviceClass = val;
723 }
Simon Glassd20fd272017-05-18 20:09:38 -0600724 val = dev_read_u32_default(dev, "usb,interface-class", -1);
Simon Glassde312132015-03-25 12:21:59 -0600725 if (val != -1) {
726 plat->id.match_flags |= USB_DEVICE_ID_MATCH_INT_CLASS;
727 plat->id.bInterfaceClass = val;
728 }
729
730 return 0;
731}
732
Hans de Goedef78a5c02015-05-05 11:54:31 +0200733struct udevice *usb_get_bus(struct udevice *dev)
Simon Glassde312132015-03-25 12:21:59 -0600734{
735 struct udevice *bus;
736
Simon Glassde312132015-03-25 12:21:59 -0600737 for (bus = dev; bus && device_get_uclass_id(bus) != UCLASS_USB; )
738 bus = bus->parent;
739 if (!bus) {
740 /* By design this cannot happen */
741 assert(bus);
742 debug("USB HUB '%s' does not have a controller\n", dev->name);
Simon Glassde312132015-03-25 12:21:59 -0600743 }
Simon Glassde312132015-03-25 12:21:59 -0600744
Hans de Goedef78a5c02015-05-05 11:54:31 +0200745 return bus;
Simon Glassde312132015-03-25 12:21:59 -0600746}
747
748int usb_child_pre_probe(struct udevice *dev)
749{
Simon Glassbcbe3d12015-09-28 23:32:01 -0600750 struct usb_device *udev = dev_get_parent_priv(dev);
Simon Glassde312132015-03-25 12:21:59 -0600751 struct usb_dev_platdata *plat = dev_get_parent_platdata(dev);
752 int ret;
753
Hans de Goede7f1a0752015-05-05 11:54:32 +0200754 if (plat->udev) {
755 /*
756 * Copy over all the values set in the on stack struct
757 * usb_device in usb_scan_device() to our final struct
758 * usb_device for this dev.
759 */
760 *udev = *(plat->udev);
761 /* And clear plat->udev as it will not be valid for long */
762 plat->udev = NULL;
763 udev->dev = dev;
764 } else {
765 /*
766 * This happens with devices which are explicitly bound
767 * instead of being discovered through usb_scan_device()
768 * such as sandbox emul devices.
769 */
770 udev->dev = dev;
771 udev->controller_dev = usb_get_bus(dev);
772 udev->devnum = plat->devnum;
Simon Glassde312132015-03-25 12:21:59 -0600773
Hans de Goede7f1a0752015-05-05 11:54:32 +0200774 /*
775 * udev did not go through usb_scan_device(), so we need to
776 * select the config and read the config descriptors.
777 */
778 ret = usb_select_config(udev);
779 if (ret)
780 return ret;
781 }
Simon Glassde312132015-03-25 12:21:59 -0600782
783 return 0;
784}
785
786UCLASS_DRIVER(usb) = {
787 .id = UCLASS_USB,
788 .name = "usb",
789 .flags = DM_UC_FLAG_SEQ_ALIAS,
Simon Glass91195482016-07-05 17:10:10 -0600790 .post_bind = dm_scan_fdt_dev,
Hans de Goedee2536372015-05-10 14:10:21 +0200791 .priv_auto_alloc_size = sizeof(struct usb_uclass_priv),
Simon Glassde312132015-03-25 12:21:59 -0600792 .per_child_auto_alloc_size = sizeof(struct usb_device),
793 .per_device_auto_alloc_size = sizeof(struct usb_bus_priv),
794 .child_post_bind = usb_child_post_bind,
795 .child_pre_probe = usb_child_pre_probe,
796 .per_child_platdata_auto_alloc_size = sizeof(struct usb_dev_platdata),
797};
Simon Glass449230f2015-03-25 12:22:31 -0600798
799UCLASS_DRIVER(usb_dev_generic) = {
800 .id = UCLASS_USB_DEV_GENERIC,
801 .name = "usb_dev_generic",
802};
803
804U_BOOT_DRIVER(usb_dev_generic_drv) = {
805 .id = UCLASS_USB_DEV_GENERIC,
806 .name = "usb_dev_generic_drv",
807};