blob: 611ea97a724929903d3ba028b53a42f894c223be [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,
34 int length, int interval)
35{
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
42 return ops->interrupt(bus, udev, pipe, buffer, length, interval);
43}
44
45int submit_control_msg(struct usb_device *udev, unsigned long pipe,
46 void *buffer, int length, struct devrequest *setup)
47{
48 struct udevice *bus = udev->controller_dev;
49 struct dm_usb_ops *ops = usb_get_ops(bus);
Hans de Goedee2536372015-05-10 14:10:21 +020050 struct usb_uclass_priv *uc_priv = bus->uclass->priv;
51 int err;
Simon Glassde312132015-03-25 12:21:59 -060052
53 if (!ops->control)
54 return -ENOSYS;
55
Hans de Goedee2536372015-05-10 14:10:21 +020056 err = ops->control(bus, udev, pipe, buffer, length, setup);
57 if (setup->request == USB_REQ_SET_FEATURE &&
58 setup->requesttype == USB_RT_PORT &&
59 setup->value == cpu_to_le16(USB_PORT_FEAT_RESET) &&
60 err == -ENXIO) {
61 /* Device handed over to companion after port reset */
62 uc_priv->companion_device_count++;
63 }
64
65 return err;
Simon Glassde312132015-03-25 12:21:59 -060066}
67
68int submit_bulk_msg(struct usb_device *udev, unsigned long pipe, void *buffer,
69 int length)
70{
71 struct udevice *bus = udev->controller_dev;
72 struct dm_usb_ops *ops = usb_get_ops(bus);
73
74 if (!ops->bulk)
75 return -ENOSYS;
76
77 return ops->bulk(bus, udev, pipe, buffer, length);
78}
79
Hans de Goede8a5f0662015-05-10 14:10:18 +020080struct int_queue *create_int_queue(struct usb_device *udev,
81 unsigned long pipe, int queuesize, int elementsize,
82 void *buffer, int interval)
83{
84 struct udevice *bus = udev->controller_dev;
85 struct dm_usb_ops *ops = usb_get_ops(bus);
86
87 if (!ops->create_int_queue)
88 return NULL;
89
90 return ops->create_int_queue(bus, udev, pipe, queuesize, elementsize,
91 buffer, interval);
92}
93
94void *poll_int_queue(struct usb_device *udev, struct int_queue *queue)
95{
96 struct udevice *bus = udev->controller_dev;
97 struct dm_usb_ops *ops = usb_get_ops(bus);
98
99 if (!ops->poll_int_queue)
100 return NULL;
101
102 return ops->poll_int_queue(bus, udev, queue);
103}
104
105int destroy_int_queue(struct usb_device *udev, struct int_queue *queue)
106{
107 struct udevice *bus = udev->controller_dev;
108 struct dm_usb_ops *ops = usb_get_ops(bus);
109
110 if (!ops->destroy_int_queue)
111 return -ENOSYS;
112
113 return ops->destroy_int_queue(bus, udev, queue);
114}
115
Simon Glassde312132015-03-25 12:21:59 -0600116int usb_alloc_device(struct usb_device *udev)
117{
118 struct udevice *bus = udev->controller_dev;
119 struct dm_usb_ops *ops = usb_get_ops(bus);
120
121 /* This is only requird by some controllers - current XHCI */
122 if (!ops->alloc_device)
123 return 0;
124
125 return ops->alloc_device(bus, udev);
126}
127
Hans de Goedeb2f219b2015-06-17 21:33:52 +0200128int usb_reset_root_port(struct usb_device *udev)
129{
130 struct udevice *bus = udev->controller_dev;
131 struct dm_usb_ops *ops = usb_get_ops(bus);
132
133 if (!ops->reset_root_port)
134 return -ENOSYS;
135
136 return ops->reset_root_port(bus, udev);
137}
138
Bin Meng9ca1b4b2017-07-19 21:51:17 +0800139int usb_update_hub_device(struct usb_device *udev)
140{
141 struct udevice *bus = udev->controller_dev;
142 struct dm_usb_ops *ops = usb_get_ops(bus);
143
144 if (!ops->update_hub_device)
145 return -ENOSYS;
146
147 return ops->update_hub_device(bus, udev);
148}
149
Bin Meng3e59f592017-09-07 06:13:17 -0700150int usb_get_max_xfer_size(struct usb_device *udev, size_t *size)
151{
152 struct udevice *bus = udev->controller_dev;
153 struct dm_usb_ops *ops = usb_get_ops(bus);
154
155 if (!ops->get_max_xfer_size)
156 return -ENOSYS;
157
158 return ops->get_max_xfer_size(bus, size);
159}
160
Simon Glassde312132015-03-25 12:21:59 -0600161int usb_stop(void)
162{
163 struct udevice *bus;
Bin Mengd4efefe2017-10-01 06:19:42 -0700164 struct udevice *rh;
Simon Glassde312132015-03-25 12:21:59 -0600165 struct uclass *uc;
Hans de Goedee2536372015-05-10 14:10:21 +0200166 struct usb_uclass_priv *uc_priv;
Simon Glassde312132015-03-25 12:21:59 -0600167 int err = 0, ret;
168
169 /* De-activate any devices that have been activated */
170 ret = uclass_get(UCLASS_USB, &uc);
171 if (ret)
172 return ret;
Hans de Goedee2536372015-05-10 14:10:21 +0200173
174 uc_priv = uc->priv;
175
Simon Glassde312132015-03-25 12:21:59 -0600176 uclass_foreach_dev(bus, uc) {
Stefan Roese706865a2017-03-20 12:51:48 +0100177 ret = device_remove(bus, DM_REMOVE_NORMAL);
Simon Glassde312132015-03-25 12:21:59 -0600178 if (ret && !err)
179 err = ret;
Bin Mengd4efefe2017-10-01 06:19:42 -0700180
181 /* Locate root hub device */
182 device_find_first_child(bus, &rh);
183 if (rh) {
184 /*
185 * All USB devices are children of root hub.
186 * Unbinding root hub will unbind all of its children.
187 */
188 ret = device_unbind(rh);
189 if (ret && !err)
190 err = ret;
191 }
Simon Glassde312132015-03-25 12:21:59 -0600192 }
Bin Mengad0a9372017-10-01 06:19:43 -0700193
Paul Kocialkowski0fa59992015-08-04 17:04:12 +0200194#ifdef CONFIG_USB_STORAGE
Simon Glassde312132015-03-25 12:21:59 -0600195 usb_stor_reset();
Paul Kocialkowski0fa59992015-08-04 17:04:12 +0200196#endif
Hans de Goedee2536372015-05-10 14:10:21 +0200197 uc_priv->companion_device_count = 0;
Simon Glassde312132015-03-25 12:21:59 -0600198 usb_started = 0;
199
200 return err;
201}
202
Hans de Goedea24a0e92015-05-10 14:10:19 +0200203static void usb_scan_bus(struct udevice *bus, bool recurse)
Simon Glassde312132015-03-25 12:21:59 -0600204{
205 struct usb_bus_priv *priv;
206 struct udevice *dev;
207 int ret;
208
209 priv = dev_get_uclass_priv(bus);
210
211 assert(recurse); /* TODO: Support non-recusive */
212
Hans de Goedea24a0e92015-05-10 14:10:19 +0200213 printf("scanning bus %d for devices... ", bus->seq);
214 debug("\n");
Simon Glassde312132015-03-25 12:21:59 -0600215 ret = usb_scan_device(bus, 0, USB_SPEED_FULL, &dev);
216 if (ret)
Hans de Goedea24a0e92015-05-10 14:10:19 +0200217 printf("failed, error %d\n", ret);
218 else if (priv->next_addr == 0)
219 printf("No USB Device found\n");
220 else
221 printf("%d USB Device(s) found\n", priv->next_addr);
Simon Glassde312132015-03-25 12:21:59 -0600222}
223
Simon Glasseae11be2015-11-08 23:48:00 -0700224static void remove_inactive_children(struct uclass *uc, struct udevice *bus)
225{
226 uclass_foreach_dev(bus, uc) {
227 struct udevice *dev, *next;
228
229 if (!device_active(bus))
230 continue;
231 device_foreach_child_safe(dev, next, bus) {
232 if (!device_active(dev))
233 device_unbind(dev);
234 }
235 }
236}
237
Simon Glassde312132015-03-25 12:21:59 -0600238int usb_init(void)
239{
240 int controllers_initialized = 0;
Hans de Goedee2536372015-05-10 14:10:21 +0200241 struct usb_uclass_priv *uc_priv;
Hans de Goedeb6de4d12015-05-10 14:10:20 +0200242 struct usb_bus_priv *priv;
Simon Glassde312132015-03-25 12:21:59 -0600243 struct udevice *bus;
244 struct uclass *uc;
245 int count = 0;
246 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 */
Hans de Goede134692a2015-05-04 21:33:26 +0200258 printf("USB%d: ", count);
Simon Glassde312132015-03-25 12:21:59 -0600259 count++;
Bin Mengd4efefe2017-10-01 06:19:42 -0700260
261#ifdef CONFIG_SANDBOX
262 /*
263 * For Sandbox, we need scan the device tree each time when we
264 * start the USB stack, in order to re-create the emulated USB
265 * devices and bind drivers for them before we actually do the
266 * driver probe.
267 */
268 ret = dm_scan_fdt_dev(bus);
269 if (ret) {
270 printf("Sandbox USB device scan failed (%d)\n", ret);
271 continue;
272 }
273#endif
274
Simon Glassde312132015-03-25 12:21:59 -0600275 ret = device_probe(bus);
276 if (ret == -ENODEV) { /* No such device. */
277 puts("Port not available.\n");
278 controllers_initialized++;
279 continue;
280 }
281
282 if (ret) { /* Other error. */
283 printf("probe failed, error %d\n", ret);
284 continue;
285 }
Simon Glassde312132015-03-25 12:21:59 -0600286 controllers_initialized++;
Simon Glassde312132015-03-25 12:21:59 -0600287 usb_started = true;
288 }
289
Hans de Goedeb6de4d12015-05-10 14:10:20 +0200290 /*
291 * lowlevel init done, now scan the bus for devices i.e. search HUBs
292 * and configure them, first scan primary controllers.
293 */
294 uclass_foreach_dev(bus, uc) {
295 if (!device_active(bus))
296 continue;
297
298 priv = dev_get_uclass_priv(bus);
299 if (!priv->companion)
300 usb_scan_bus(bus, true);
301 }
302
303 /*
304 * Now that the primary controllers have been scanned and have handed
305 * over any devices they do not understand to their companions, scan
Hans de Goedee2536372015-05-10 14:10:21 +0200306 * the companions if necessary.
Hans de Goedeb6de4d12015-05-10 14:10:20 +0200307 */
Hans de Goedee2536372015-05-10 14:10:21 +0200308 if (uc_priv->companion_device_count) {
309 uclass_foreach_dev(bus, uc) {
310 if (!device_active(bus))
311 continue;
Hans de Goedeb6de4d12015-05-10 14:10:20 +0200312
Hans de Goedee2536372015-05-10 14:10:21 +0200313 priv = dev_get_uclass_priv(bus);
314 if (priv->companion)
315 usb_scan_bus(bus, true);
316 }
Hans de Goedeb6de4d12015-05-10 14:10:20 +0200317 }
318
Simon Glassde312132015-03-25 12:21:59 -0600319 debug("scan end\n");
Simon Glasseae11be2015-11-08 23:48:00 -0700320
321 /* Remove any devices that were not found on this scan */
322 remove_inactive_children(uc, bus);
323
324 ret = uclass_get(UCLASS_USB_HUB, &uc);
325 if (ret)
326 return ret;
327 remove_inactive_children(uc, bus);
328
Simon Glassde312132015-03-25 12:21:59 -0600329 /* if we were not able to find at least one working bus, bail out */
330 if (!count)
331 printf("No controllers found\n");
332 else if (controllers_initialized == 0)
333 printf("USB error: all controllers failed lowlevel init\n");
334
335 return usb_started ? 0 : -1;
336}
337
Simon Glasse8ea5e82015-11-08 23:47:59 -0700338/*
339 * TODO(sjg@chromium.org): Remove this legacy function. At present it is needed
340 * to support boards which use driver model for USB but not Ethernet, and want
341 * to use USB Ethernet.
342 *
343 * The #if clause is here to ensure that remains the only case.
344 */
345#if !defined(CONFIG_DM_ETH) && defined(CONFIG_USB_HOST_ETHER)
Simon Glassde312132015-03-25 12:21:59 -0600346static struct usb_device *find_child_devnum(struct udevice *parent, int devnum)
347{
348 struct usb_device *udev;
349 struct udevice *dev;
350
351 if (!device_active(parent))
352 return NULL;
Simon Glassbcbe3d12015-09-28 23:32:01 -0600353 udev = dev_get_parent_priv(parent);
Simon Glassde312132015-03-25 12:21:59 -0600354 if (udev->devnum == devnum)
355 return udev;
356
357 for (device_find_first_child(parent, &dev);
358 dev;
359 device_find_next_child(&dev)) {
360 udev = find_child_devnum(dev, devnum);
361 if (udev)
362 return udev;
363 }
364
365 return NULL;
366}
367
368struct usb_device *usb_get_dev_index(struct udevice *bus, int index)
369{
Hans de Goedefd1bd212015-06-17 21:33:53 +0200370 struct udevice *dev;
Simon Glassde312132015-03-25 12:21:59 -0600371 int devnum = index + 1; /* Addresses are allocated from 1 on USB */
372
Hans de Goedefd1bd212015-06-17 21:33:53 +0200373 device_find_first_child(bus, &dev);
374 if (!dev)
375 return NULL;
Simon Glassde312132015-03-25 12:21:59 -0600376
Hans de Goedefd1bd212015-06-17 21:33:53 +0200377 return find_child_devnum(dev, devnum);
Simon Glassde312132015-03-25 12:21:59 -0600378}
Simon Glasse8ea5e82015-11-08 23:47:59 -0700379#endif
Simon Glassde312132015-03-25 12:21:59 -0600380
Simon Glassfbeceb22015-03-25 12:22:32 -0600381int usb_setup_ehci_gadget(struct ehci_ctrl **ctlrp)
382{
383 struct usb_platdata *plat;
384 struct udevice *dev;
385 int ret;
386
387 /* Find the old device and remove it */
388 ret = uclass_find_device_by_seq(UCLASS_USB, 0, true, &dev);
389 if (ret)
390 return ret;
Stefan Roese706865a2017-03-20 12:51:48 +0100391 ret = device_remove(dev, DM_REMOVE_NORMAL);
Simon Glassfbeceb22015-03-25 12:22:32 -0600392 if (ret)
393 return ret;
394
395 plat = dev_get_platdata(dev);
396 plat->init_type = USB_INIT_DEVICE;
397 ret = device_probe(dev);
398 if (ret)
399 return ret;
400 *ctlrp = dev_get_priv(dev);
401
402 return 0;
403}
404
Simon Glass0566e242015-03-25 12:22:30 -0600405/* returns 0 if no match, 1 if match */
Masahiro Yamada121a4d12017-06-22 16:35:14 +0900406static int usb_match_device(const struct usb_device_descriptor *desc,
407 const struct usb_device_id *id)
Simon Glass0566e242015-03-25 12:22:30 -0600408{
409 if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
410 id->idVendor != le16_to_cpu(desc->idVendor))
411 return 0;
412
413 if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
414 id->idProduct != le16_to_cpu(desc->idProduct))
415 return 0;
416
417 /* No need to test id->bcdDevice_lo != 0, since 0 is never
418 greater than any unsigned number. */
419 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) &&
420 (id->bcdDevice_lo > le16_to_cpu(desc->bcdDevice)))
421 return 0;
422
423 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) &&
424 (id->bcdDevice_hi < le16_to_cpu(desc->bcdDevice)))
425 return 0;
426
427 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) &&
428 (id->bDeviceClass != desc->bDeviceClass))
429 return 0;
430
431 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) &&
432 (id->bDeviceSubClass != desc->bDeviceSubClass))
433 return 0;
434
435 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) &&
436 (id->bDeviceProtocol != desc->bDeviceProtocol))
437 return 0;
438
439 return 1;
440}
441
442/* returns 0 if no match, 1 if match */
Masahiro Yamada121a4d12017-06-22 16:35:14 +0900443static int usb_match_one_id_intf(const struct usb_device_descriptor *desc,
444 const struct usb_interface_descriptor *int_desc,
445 const struct usb_device_id *id)
Simon Glass0566e242015-03-25 12:22:30 -0600446{
447 /* The interface class, subclass, protocol and number should never be
448 * checked for a match if the device class is Vendor Specific,
449 * unless the match record specifies the Vendor ID. */
450 if (desc->bDeviceClass == USB_CLASS_VENDOR_SPEC &&
451 !(id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
452 (id->match_flags & (USB_DEVICE_ID_MATCH_INT_CLASS |
453 USB_DEVICE_ID_MATCH_INT_SUBCLASS |
454 USB_DEVICE_ID_MATCH_INT_PROTOCOL |
455 USB_DEVICE_ID_MATCH_INT_NUMBER)))
456 return 0;
457
458 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) &&
459 (id->bInterfaceClass != int_desc->bInterfaceClass))
460 return 0;
461
462 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) &&
463 (id->bInterfaceSubClass != int_desc->bInterfaceSubClass))
464 return 0;
465
466 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) &&
467 (id->bInterfaceProtocol != int_desc->bInterfaceProtocol))
468 return 0;
469
470 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_NUMBER) &&
471 (id->bInterfaceNumber != int_desc->bInterfaceNumber))
472 return 0;
473
474 return 1;
475}
476
477/* returns 0 if no match, 1 if match */
Masahiro Yamada121a4d12017-06-22 16:35:14 +0900478static int usb_match_one_id(struct usb_device_descriptor *desc,
479 struct usb_interface_descriptor *int_desc,
480 const struct usb_device_id *id)
Simon Glass0566e242015-03-25 12:22:30 -0600481{
482 if (!usb_match_device(desc, id))
483 return 0;
484
485 return usb_match_one_id_intf(desc, int_desc, id);
486}
487
488/**
489 * usb_find_and_bind_driver() - Find and bind the right USB driver
490 *
491 * This only looks at certain fields in the descriptor.
492 */
493static int usb_find_and_bind_driver(struct udevice *parent,
494 struct usb_device_descriptor *desc,
495 struct usb_interface_descriptor *iface,
496 int bus_seq, int devnum,
497 struct udevice **devp)
498{
499 struct usb_driver_entry *start, *entry;
500 int n_ents;
501 int ret;
502 char name[30], *str;
503
504 *devp = NULL;
505 debug("%s: Searching for driver\n", __func__);
506 start = ll_entry_start(struct usb_driver_entry, usb_driver_entry);
507 n_ents = ll_entry_count(struct usb_driver_entry, usb_driver_entry);
508 for (entry = start; entry != start + n_ents; entry++) {
509 const struct usb_device_id *id;
510 struct udevice *dev;
511 const struct driver *drv;
512 struct usb_dev_platdata *plat;
513
514 for (id = entry->match; id->match_flags; id++) {
515 if (!usb_match_one_id(desc, iface, id))
516 continue;
517
518 drv = entry->driver;
519 /*
520 * We could pass the descriptor to the driver as
521 * platdata (instead of NULL) and allow its bind()
522 * method to return -ENOENT if it doesn't support this
523 * device. That way we could continue the search to
524 * find another driver. For now this doesn't seem
525 * necesssary, so just bind the first match.
526 */
527 ret = device_bind(parent, drv, drv->name, NULL, -1,
528 &dev);
529 if (ret)
530 goto error;
531 debug("%s: Match found: %s\n", __func__, drv->name);
532 dev->driver_data = id->driver_info;
533 plat = dev_get_parent_platdata(dev);
534 plat->id = *id;
535 *devp = dev;
536 return 0;
537 }
538 }
539
Simon Glass449230f2015-03-25 12:22:31 -0600540 /* Bind a generic driver so that the device can be used */
541 snprintf(name, sizeof(name), "generic_bus_%x_dev_%x", bus_seq, devnum);
542 str = strdup(name);
543 if (!str)
544 return -ENOMEM;
545 ret = device_bind_driver(parent, "usb_dev_generic_drv", str, devp);
546
Simon Glass0566e242015-03-25 12:22:30 -0600547error:
548 debug("%s: No match found: %d\n", __func__, ret);
549 return ret;
550}
551
552/**
Simon Glass1b6a1df2015-11-08 23:47:56 -0700553 * usb_find_child() - Find an existing device which matches our needs
554 *
555 *
Simon Glass0566e242015-03-25 12:22:30 -0600556 */
Simon Glass1b6a1df2015-11-08 23:47:56 -0700557static int usb_find_child(struct udevice *parent,
558 struct usb_device_descriptor *desc,
559 struct usb_interface_descriptor *iface,
560 struct udevice **devp)
Simon Glass0566e242015-03-25 12:22:30 -0600561{
562 struct udevice *dev;
563
564 *devp = NULL;
565 for (device_find_first_child(parent, &dev);
566 dev;
567 device_find_next_child(&dev)) {
568 struct usb_dev_platdata *plat = dev_get_parent_platdata(dev);
569
570 /* If this device is already in use, skip it */
571 if (device_active(dev))
572 continue;
573 debug(" %s: name='%s', plat=%d, desc=%d\n", __func__,
574 dev->name, plat->id.bDeviceClass, desc->bDeviceClass);
575 if (usb_match_one_id(desc, iface, &plat->id)) {
576 *devp = dev;
577 return 0;
578 }
579 }
Simon Glass1b6a1df2015-11-08 23:47:56 -0700580
Simon Glass0566e242015-03-25 12:22:30 -0600581 return -ENOENT;
582}
583
Simon Glassde312132015-03-25 12:21:59 -0600584int usb_scan_device(struct udevice *parent, int port,
585 enum usb_device_speed speed, struct udevice **devp)
586{
587 struct udevice *dev;
588 bool created = false;
589 struct usb_dev_platdata *plat;
590 struct usb_bus_priv *priv;
591 struct usb_device *parent_udev;
592 int ret;
593 ALLOC_CACHE_ALIGN_BUFFER(struct usb_device, udev, 1);
594 struct usb_interface_descriptor *iface = &udev->config.if_desc[0].desc;
595
596 *devp = NULL;
597 memset(udev, '\0', sizeof(*udev));
Hans de Goedef78a5c02015-05-05 11:54:31 +0200598 udev->controller_dev = usb_get_bus(parent);
Simon Glassde312132015-03-25 12:21:59 -0600599 priv = dev_get_uclass_priv(udev->controller_dev);
600
601 /*
602 * Somewhat nasty, this. We create a local device and use the normal
603 * USB stack to read its descriptor. Then we know what type of device
604 * to create for real.
605 *
606 * udev->dev is set to the parent, since we don't have a real device
607 * yet. The USB stack should not access udev.dev anyway, except perhaps
608 * to find the controller, and the controller will either be @parent,
609 * or some parent of @parent.
610 *
611 * Another option might be to create the device as a generic USB
612 * device, then morph it into the correct one when we know what it
613 * should be. This means that a generic USB device would morph into
614 * a network controller, or a USB flash stick, for example. However,
615 * we don't support such morphing and it isn't clear that it would
616 * be easy to do.
617 *
618 * Yet another option is to split out the USB stack parts of udev
619 * into something like a 'struct urb' (as Linux does) which can exist
620 * independently of any device. This feels cleaner, but calls for quite
621 * a big change to the USB stack.
622 *
623 * For now, the approach is to set up an empty udev, read its
624 * descriptor and assign it an address, then bind a real device and
625 * stash the resulting information into the device's parent
626 * platform data. Then when we probe it, usb_child_pre_probe() is called
627 * and it will pull the information out of the stash.
628 */
629 udev->dev = parent;
630 udev->speed = speed;
631 udev->devnum = priv->next_addr + 1;
632 udev->portnr = port;
633 debug("Calling usb_setup_device(), portnr=%d\n", udev->portnr);
634 parent_udev = device_get_uclass_id(parent) == UCLASS_USB_HUB ?
Simon Glassbcbe3d12015-09-28 23:32:01 -0600635 dev_get_parent_priv(parent) : NULL;
Hans de Goede9eb72dd2015-06-17 21:33:46 +0200636 ret = usb_setup_device(udev, priv->desc_before_addr, parent_udev);
Simon Glassde312132015-03-25 12:21:59 -0600637 debug("read_descriptor for '%s': ret=%d\n", parent->name, ret);
638 if (ret)
639 return ret;
Simon Glass1b6a1df2015-11-08 23:47:56 -0700640 ret = usb_find_child(parent, &udev->descriptor, iface, &dev);
641 debug("** usb_find_child returns %d\n", ret);
Simon Glass0566e242015-03-25 12:22:30 -0600642 if (ret) {
643 if (ret != -ENOENT)
644 return ret;
645 ret = usb_find_and_bind_driver(parent, &udev->descriptor, iface,
646 udev->controller_dev->seq,
647 udev->devnum, &dev);
648 if (ret)
649 return ret;
650 created = true;
651 }
652 plat = dev_get_parent_platdata(dev);
653 debug("%s: Probing '%s', plat=%p\n", __func__, dev->name, plat);
654 plat->devnum = udev->devnum;
Hans de Goede7f1a0752015-05-05 11:54:32 +0200655 plat->udev = udev;
Simon Glass0566e242015-03-25 12:22:30 -0600656 priv->next_addr++;
657 ret = device_probe(dev);
658 if (ret) {
659 debug("%s: Device '%s' probe failed\n", __func__, dev->name);
660 priv->next_addr--;
661 if (created)
662 device_unbind(dev);
663 return ret;
664 }
665 *devp = dev;
Simon Glassde312132015-03-25 12:21:59 -0600666
Simon Glass0566e242015-03-25 12:22:30 -0600667 return 0;
Simon Glassde312132015-03-25 12:21:59 -0600668}
669
Simon Glass0c5dd9a2015-05-13 07:02:23 -0600670/*
671 * Detect if a USB device has been plugged or unplugged.
672 */
673int usb_detect_change(void)
674{
675 struct udevice *hub;
676 struct uclass *uc;
677 int change = 0;
678 int ret;
679
680 ret = uclass_get(UCLASS_USB_HUB, &uc);
681 if (ret)
682 return ret;
683
684 uclass_foreach_dev(hub, uc) {
685 struct usb_device *udev;
686 struct udevice *dev;
687
688 if (!device_active(hub))
689 continue;
690 for (device_find_first_child(hub, &dev);
691 dev;
692 device_find_next_child(&dev)) {
693 struct usb_port_status status;
694
695 if (!device_active(dev))
696 continue;
697
Simon Glassbcbe3d12015-09-28 23:32:01 -0600698 udev = dev_get_parent_priv(dev);
Simon Glass0c5dd9a2015-05-13 07:02:23 -0600699 if (usb_get_port_status(udev, udev->portnr, &status)
700 < 0)
701 /* USB request failed */
702 continue;
703
704 if (le16_to_cpu(status.wPortChange) &
705 USB_PORT_STAT_C_CONNECTION)
706 change++;
707 }
708 }
709
710 return change;
711}
712
Masahiro Yamada121a4d12017-06-22 16:35:14 +0900713static int usb_child_post_bind(struct udevice *dev)
Simon Glassde312132015-03-25 12:21:59 -0600714{
715 struct usb_dev_platdata *plat = dev_get_parent_platdata(dev);
Simon Glassde312132015-03-25 12:21:59 -0600716 int val;
717
Simon Glassd20fd272017-05-18 20:09:38 -0600718 if (!dev_of_valid(dev))
Simon Glassde312132015-03-25 12:21:59 -0600719 return 0;
720
721 /* We only support matching a few things */
Simon Glassd20fd272017-05-18 20:09:38 -0600722 val = dev_read_u32_default(dev, "usb,device-class", -1);
Simon Glassde312132015-03-25 12:21:59 -0600723 if (val != -1) {
724 plat->id.match_flags |= USB_DEVICE_ID_MATCH_DEV_CLASS;
725 plat->id.bDeviceClass = val;
726 }
Simon Glassd20fd272017-05-18 20:09:38 -0600727 val = dev_read_u32_default(dev, "usb,interface-class", -1);
Simon Glassde312132015-03-25 12:21:59 -0600728 if (val != -1) {
729 plat->id.match_flags |= USB_DEVICE_ID_MATCH_INT_CLASS;
730 plat->id.bInterfaceClass = val;
731 }
732
733 return 0;
734}
735
Hans de Goedef78a5c02015-05-05 11:54:31 +0200736struct udevice *usb_get_bus(struct udevice *dev)
Simon Glassde312132015-03-25 12:21:59 -0600737{
738 struct udevice *bus;
739
Simon Glassde312132015-03-25 12:21:59 -0600740 for (bus = dev; bus && device_get_uclass_id(bus) != UCLASS_USB; )
741 bus = bus->parent;
742 if (!bus) {
743 /* By design this cannot happen */
744 assert(bus);
745 debug("USB HUB '%s' does not have a controller\n", dev->name);
Simon Glassde312132015-03-25 12:21:59 -0600746 }
Simon Glassde312132015-03-25 12:21:59 -0600747
Hans de Goedef78a5c02015-05-05 11:54:31 +0200748 return bus;
Simon Glassde312132015-03-25 12:21:59 -0600749}
750
751int usb_child_pre_probe(struct udevice *dev)
752{
Simon Glassbcbe3d12015-09-28 23:32:01 -0600753 struct usb_device *udev = dev_get_parent_priv(dev);
Simon Glassde312132015-03-25 12:21:59 -0600754 struct usb_dev_platdata *plat = dev_get_parent_platdata(dev);
755 int ret;
756
Hans de Goede7f1a0752015-05-05 11:54:32 +0200757 if (plat->udev) {
758 /*
759 * Copy over all the values set in the on stack struct
760 * usb_device in usb_scan_device() to our final struct
761 * usb_device for this dev.
762 */
763 *udev = *(plat->udev);
764 /* And clear plat->udev as it will not be valid for long */
765 plat->udev = NULL;
766 udev->dev = dev;
767 } else {
768 /*
769 * This happens with devices which are explicitly bound
770 * instead of being discovered through usb_scan_device()
771 * such as sandbox emul devices.
772 */
773 udev->dev = dev;
774 udev->controller_dev = usb_get_bus(dev);
775 udev->devnum = plat->devnum;
Simon Glassde312132015-03-25 12:21:59 -0600776
Hans de Goede7f1a0752015-05-05 11:54:32 +0200777 /*
778 * udev did not go through usb_scan_device(), so we need to
779 * select the config and read the config descriptors.
780 */
781 ret = usb_select_config(udev);
782 if (ret)
783 return ret;
784 }
Simon Glassde312132015-03-25 12:21:59 -0600785
786 return 0;
787}
788
789UCLASS_DRIVER(usb) = {
790 .id = UCLASS_USB,
791 .name = "usb",
792 .flags = DM_UC_FLAG_SEQ_ALIAS,
Simon Glass91195482016-07-05 17:10:10 -0600793 .post_bind = dm_scan_fdt_dev,
Hans de Goedee2536372015-05-10 14:10:21 +0200794 .priv_auto_alloc_size = sizeof(struct usb_uclass_priv),
Simon Glassde312132015-03-25 12:21:59 -0600795 .per_child_auto_alloc_size = sizeof(struct usb_device),
796 .per_device_auto_alloc_size = sizeof(struct usb_bus_priv),
797 .child_post_bind = usb_child_post_bind,
798 .child_pre_probe = usb_child_pre_probe,
799 .per_child_platdata_auto_alloc_size = sizeof(struct usb_dev_platdata),
800};
Simon Glass449230f2015-03-25 12:22:31 -0600801
802UCLASS_DRIVER(usb_dev_generic) = {
803 .id = UCLASS_USB_DEV_GENERIC,
804 .name = "usb_dev_generic",
805};
806
807U_BOOT_DRIVER(usb_dev_generic_drv) = {
808 .id = UCLASS_USB_DEV_GENERIC,
809 .name = "usb_dev_generic_drv",
810};