blob: 6e118b5a8ffa408fd8b9c7f5595e0ae47b200b58 [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
9#include <common.h>
10#include <dm.h>
11#include <errno.h>
Simon Glass2dd337a2015-09-02 17:24:58 -060012#include <memalign.h>
Simon Glass9b82eeb2015-03-25 12:21:59 -060013#include <usb.h>
14#include <dm/device-internal.h>
15#include <dm/lists.h>
Simon Glass9b82eeb2015-03-25 12:21:59 -060016#include <dm/uclass-internal.h>
17
Simon Glass9b82eeb2015-03-25 12:21:59 -060018extern bool usb_started; /* flag for the started/stopped USB status */
19static bool asynch_allowed;
20
Hans de Goedecebc5b72015-05-10 14:10:21 +020021struct usb_uclass_priv {
22 int companion_device_count;
23};
24
Simon Glass9b82eeb2015-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 Goedecebc5b72015-05-10 14:10:21 +020050 struct usb_uclass_priv *uc_priv = bus->uclass->priv;
51 int err;
Simon Glass9b82eeb2015-03-25 12:21:59 -060052
53 if (!ops->control)
54 return -ENOSYS;
55
Hans de Goedecebc5b72015-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 Glass9b82eeb2015-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 Goede0a7fa272015-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 Glass9b82eeb2015-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 Goedea9e41f82015-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 Meng1f31f5a2017-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 Meng6840a342017-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 Glass9b82eeb2015-03-25 12:21:59 -0600161int usb_stop(void)
162{
163 struct udevice *bus;
Bin Meng669ad842017-10-01 06:19:42 -0700164 struct udevice *rh;
Simon Glass9b82eeb2015-03-25 12:21:59 -0600165 struct uclass *uc;
Hans de Goedecebc5b72015-05-10 14:10:21 +0200166 struct usb_uclass_priv *uc_priv;
Simon Glass9b82eeb2015-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 Goedecebc5b72015-05-10 14:10:21 +0200173
174 uc_priv = uc->priv;
175
Simon Glass9b82eeb2015-03-25 12:21:59 -0600176 uclass_foreach_dev(bus, uc) {
Stefan Roese80b5bc92017-03-20 12:51:48 +0100177 ret = device_remove(bus, DM_REMOVE_NORMAL);
Simon Glass9b82eeb2015-03-25 12:21:59 -0600178 if (ret && !err)
179 err = ret;
Bin Meng669ad842017-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 Glass9b82eeb2015-03-25 12:21:59 -0600192 }
Bin Mengdadc4c02017-10-01 06:19:43 -0700193
Paul Kocialkowskid449dd32015-08-04 17:04:12 +0200194#ifdef CONFIG_USB_STORAGE
Simon Glass9b82eeb2015-03-25 12:21:59 -0600195 usb_stor_reset();
Paul Kocialkowskid449dd32015-08-04 17:04:12 +0200196#endif
Hans de Goedecebc5b72015-05-10 14:10:21 +0200197 uc_priv->companion_device_count = 0;
Simon Glass9b82eeb2015-03-25 12:21:59 -0600198 usb_started = 0;
199
200 return err;
201}
202
Hans de Goedef8762f92015-05-10 14:10:19 +0200203static void usb_scan_bus(struct udevice *bus, bool recurse)
Simon Glass9b82eeb2015-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
Ismael Luceno Cortesf58affe2019-03-19 09:19:44 +0000213 printf("scanning bus %s for devices... ", bus->name);
Hans de Goedef8762f92015-05-10 14:10:19 +0200214 debug("\n");
Simon Glass9b82eeb2015-03-25 12:21:59 -0600215 ret = usb_scan_device(bus, 0, USB_SPEED_FULL, &dev);
216 if (ret)
Hans de Goedef8762f92015-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 Glass9b82eeb2015-03-25 12:21:59 -0600222}
223
Simon Glass35787d22015-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 Glass9b82eeb2015-03-25 12:21:59 -0600238int usb_init(void)
239{
240 int controllers_initialized = 0;
Hans de Goedecebc5b72015-05-10 14:10:21 +0200241 struct usb_uclass_priv *uc_priv;
Hans de Goede59a0dfc2015-05-10 14:10:20 +0200242 struct usb_bus_priv *priv;
Simon Glass9b82eeb2015-03-25 12:21:59 -0600243 struct udevice *bus;
244 struct uclass *uc;
Simon Glass9b82eeb2015-03-25 12:21:59 -0600245 int ret;
246
247 asynch_allowed = 1;
Simon Glass9b82eeb2015-03-25 12:21:59 -0600248
249 ret = uclass_get(UCLASS_USB, &uc);
250 if (ret)
251 return ret;
252
Hans de Goedecebc5b72015-05-10 14:10:21 +0200253 uc_priv = uc->priv;
254
Simon Glass9b82eeb2015-03-25 12:21:59 -0600255 uclass_foreach_dev(bus, uc) {
256 /* init low_level USB */
Ismael Luceno Cortesf58affe2019-03-19 09:19:44 +0000257 printf("Bus %s: ", bus->name);
Bin Meng669ad842017-10-01 06:19:42 -0700258
259#ifdef CONFIG_SANDBOX
260 /*
261 * For Sandbox, we need scan the device tree each time when we
262 * start the USB stack, in order to re-create the emulated USB
263 * devices and bind drivers for them before we actually do the
264 * driver probe.
265 */
266 ret = dm_scan_fdt_dev(bus);
267 if (ret) {
268 printf("Sandbox USB device scan failed (%d)\n", ret);
269 continue;
270 }
271#endif
272
Simon Glass9b82eeb2015-03-25 12:21:59 -0600273 ret = device_probe(bus);
274 if (ret == -ENODEV) { /* No such device. */
275 puts("Port not available.\n");
276 controllers_initialized++;
277 continue;
278 }
279
280 if (ret) { /* Other error. */
281 printf("probe failed, error %d\n", ret);
282 continue;
283 }
Simon Glass9b82eeb2015-03-25 12:21:59 -0600284 controllers_initialized++;
Simon Glass9b82eeb2015-03-25 12:21:59 -0600285 usb_started = true;
286 }
287
Hans de Goede59a0dfc2015-05-10 14:10:20 +0200288 /*
289 * lowlevel init done, now scan the bus for devices i.e. search HUBs
290 * and configure them, first scan primary controllers.
291 */
292 uclass_foreach_dev(bus, uc) {
293 if (!device_active(bus))
294 continue;
295
296 priv = dev_get_uclass_priv(bus);
297 if (!priv->companion)
298 usb_scan_bus(bus, true);
299 }
300
301 /*
302 * Now that the primary controllers have been scanned and have handed
303 * over any devices they do not understand to their companions, scan
Hans de Goedecebc5b72015-05-10 14:10:21 +0200304 * the companions if necessary.
Hans de Goede59a0dfc2015-05-10 14:10:20 +0200305 */
Hans de Goedecebc5b72015-05-10 14:10:21 +0200306 if (uc_priv->companion_device_count) {
307 uclass_foreach_dev(bus, uc) {
308 if (!device_active(bus))
309 continue;
Hans de Goede59a0dfc2015-05-10 14:10:20 +0200310
Hans de Goedecebc5b72015-05-10 14:10:21 +0200311 priv = dev_get_uclass_priv(bus);
312 if (priv->companion)
313 usb_scan_bus(bus, true);
314 }
Hans de Goede59a0dfc2015-05-10 14:10:20 +0200315 }
316
Simon Glass9b82eeb2015-03-25 12:21:59 -0600317 debug("scan end\n");
Simon Glass35787d22015-11-08 23:48:00 -0700318
319 /* Remove any devices that were not found on this scan */
320 remove_inactive_children(uc, bus);
321
322 ret = uclass_get(UCLASS_USB_HUB, &uc);
323 if (ret)
324 return ret;
325 remove_inactive_children(uc, bus);
326
Simon Glass9b82eeb2015-03-25 12:21:59 -0600327 /* if we were not able to find at least one working bus, bail out */
Ismael Luceno Cortesf58affe2019-03-19 09:19:44 +0000328 if (controllers_initialized == 0)
329 printf("No working controllers found\n");
Simon Glass9b82eeb2015-03-25 12:21:59 -0600330
331 return usb_started ? 0 : -1;
332}
333
Simon Glass5082c692015-11-08 23:47:59 -0700334/*
335 * TODO(sjg@chromium.org): Remove this legacy function. At present it is needed
336 * to support boards which use driver model for USB but not Ethernet, and want
337 * to use USB Ethernet.
338 *
339 * The #if clause is here to ensure that remains the only case.
340 */
341#if !defined(CONFIG_DM_ETH) && defined(CONFIG_USB_HOST_ETHER)
Simon Glass9b82eeb2015-03-25 12:21:59 -0600342static struct usb_device *find_child_devnum(struct udevice *parent, int devnum)
343{
344 struct usb_device *udev;
345 struct udevice *dev;
346
347 if (!device_active(parent))
348 return NULL;
Simon Glassde44acf2015-09-28 23:32:01 -0600349 udev = dev_get_parent_priv(parent);
Simon Glass9b82eeb2015-03-25 12:21:59 -0600350 if (udev->devnum == devnum)
351 return udev;
352
353 for (device_find_first_child(parent, &dev);
354 dev;
355 device_find_next_child(&dev)) {
356 udev = find_child_devnum(dev, devnum);
357 if (udev)
358 return udev;
359 }
360
361 return NULL;
362}
363
364struct usb_device *usb_get_dev_index(struct udevice *bus, int index)
365{
Hans de Goede9c203592015-06-17 21:33:53 +0200366 struct udevice *dev;
Simon Glass9b82eeb2015-03-25 12:21:59 -0600367 int devnum = index + 1; /* Addresses are allocated from 1 on USB */
368
Hans de Goede9c203592015-06-17 21:33:53 +0200369 device_find_first_child(bus, &dev);
370 if (!dev)
371 return NULL;
Simon Glass9b82eeb2015-03-25 12:21:59 -0600372
Hans de Goede9c203592015-06-17 21:33:53 +0200373 return find_child_devnum(dev, devnum);
Simon Glass9b82eeb2015-03-25 12:21:59 -0600374}
Simon Glass5082c692015-11-08 23:47:59 -0700375#endif
Simon Glass9b82eeb2015-03-25 12:21:59 -0600376
Simon Glass444b1e02015-03-25 12:22:32 -0600377int usb_setup_ehci_gadget(struct ehci_ctrl **ctlrp)
378{
379 struct usb_platdata *plat;
380 struct udevice *dev;
381 int ret;
382
383 /* Find the old device and remove it */
384 ret = uclass_find_device_by_seq(UCLASS_USB, 0, true, &dev);
385 if (ret)
386 return ret;
Stefan Roese80b5bc92017-03-20 12:51:48 +0100387 ret = device_remove(dev, DM_REMOVE_NORMAL);
Simon Glass444b1e02015-03-25 12:22:32 -0600388 if (ret)
389 return ret;
390
391 plat = dev_get_platdata(dev);
392 plat->init_type = USB_INIT_DEVICE;
393 ret = device_probe(dev);
394 if (ret)
395 return ret;
396 *ctlrp = dev_get_priv(dev);
397
398 return 0;
399}
400
Simon Glassfc03a552015-03-25 12:22:30 -0600401/* returns 0 if no match, 1 if match */
Masahiro Yamada6d8e4332017-06-22 16:35:14 +0900402static int usb_match_device(const struct usb_device_descriptor *desc,
403 const struct usb_device_id *id)
Simon Glassfc03a552015-03-25 12:22:30 -0600404{
405 if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
406 id->idVendor != le16_to_cpu(desc->idVendor))
407 return 0;
408
409 if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
410 id->idProduct != le16_to_cpu(desc->idProduct))
411 return 0;
412
413 /* No need to test id->bcdDevice_lo != 0, since 0 is never
414 greater than any unsigned number. */
415 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) &&
416 (id->bcdDevice_lo > le16_to_cpu(desc->bcdDevice)))
417 return 0;
418
419 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) &&
420 (id->bcdDevice_hi < le16_to_cpu(desc->bcdDevice)))
421 return 0;
422
423 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) &&
424 (id->bDeviceClass != desc->bDeviceClass))
425 return 0;
426
427 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) &&
428 (id->bDeviceSubClass != desc->bDeviceSubClass))
429 return 0;
430
431 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) &&
432 (id->bDeviceProtocol != desc->bDeviceProtocol))
433 return 0;
434
435 return 1;
436}
437
438/* returns 0 if no match, 1 if match */
Masahiro Yamada6d8e4332017-06-22 16:35:14 +0900439static int usb_match_one_id_intf(const struct usb_device_descriptor *desc,
440 const struct usb_interface_descriptor *int_desc,
441 const struct usb_device_id *id)
Simon Glassfc03a552015-03-25 12:22:30 -0600442{
443 /* The interface class, subclass, protocol and number should never be
444 * checked for a match if the device class is Vendor Specific,
445 * unless the match record specifies the Vendor ID. */
446 if (desc->bDeviceClass == USB_CLASS_VENDOR_SPEC &&
447 !(id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
448 (id->match_flags & (USB_DEVICE_ID_MATCH_INT_CLASS |
449 USB_DEVICE_ID_MATCH_INT_SUBCLASS |
450 USB_DEVICE_ID_MATCH_INT_PROTOCOL |
451 USB_DEVICE_ID_MATCH_INT_NUMBER)))
452 return 0;
453
454 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) &&
455 (id->bInterfaceClass != int_desc->bInterfaceClass))
456 return 0;
457
458 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) &&
459 (id->bInterfaceSubClass != int_desc->bInterfaceSubClass))
460 return 0;
461
462 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) &&
463 (id->bInterfaceProtocol != int_desc->bInterfaceProtocol))
464 return 0;
465
466 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_NUMBER) &&
467 (id->bInterfaceNumber != int_desc->bInterfaceNumber))
468 return 0;
469
470 return 1;
471}
472
473/* returns 0 if no match, 1 if match */
Masahiro Yamada6d8e4332017-06-22 16:35:14 +0900474static int usb_match_one_id(struct usb_device_descriptor *desc,
475 struct usb_interface_descriptor *int_desc,
476 const struct usb_device_id *id)
Simon Glassfc03a552015-03-25 12:22:30 -0600477{
478 if (!usb_match_device(desc, id))
479 return 0;
480
481 return usb_match_one_id_intf(desc, int_desc, id);
482}
483
484/**
485 * usb_find_and_bind_driver() - Find and bind the right USB driver
486 *
487 * This only looks at certain fields in the descriptor.
488 */
489static int usb_find_and_bind_driver(struct udevice *parent,
490 struct usb_device_descriptor *desc,
491 struct usb_interface_descriptor *iface,
492 int bus_seq, int devnum,
493 struct udevice **devp)
494{
495 struct usb_driver_entry *start, *entry;
496 int n_ents;
497 int ret;
498 char name[30], *str;
499
500 *devp = NULL;
501 debug("%s: Searching for driver\n", __func__);
502 start = ll_entry_start(struct usb_driver_entry, usb_driver_entry);
503 n_ents = ll_entry_count(struct usb_driver_entry, usb_driver_entry);
504 for (entry = start; entry != start + n_ents; entry++) {
505 const struct usb_device_id *id;
506 struct udevice *dev;
507 const struct driver *drv;
508 struct usb_dev_platdata *plat;
509
510 for (id = entry->match; id->match_flags; id++) {
511 if (!usb_match_one_id(desc, iface, id))
512 continue;
513
514 drv = entry->driver;
515 /*
516 * We could pass the descriptor to the driver as
517 * platdata (instead of NULL) and allow its bind()
518 * method to return -ENOENT if it doesn't support this
519 * device. That way we could continue the search to
520 * find another driver. For now this doesn't seem
521 * necesssary, so just bind the first match.
522 */
523 ret = device_bind(parent, drv, drv->name, NULL, -1,
524 &dev);
525 if (ret)
526 goto error;
527 debug("%s: Match found: %s\n", __func__, drv->name);
528 dev->driver_data = id->driver_info;
529 plat = dev_get_parent_platdata(dev);
530 plat->id = *id;
531 *devp = dev;
532 return 0;
533 }
534 }
535
Simon Glassc79173e2015-03-25 12:22:31 -0600536 /* Bind a generic driver so that the device can be used */
537 snprintf(name, sizeof(name), "generic_bus_%x_dev_%x", bus_seq, devnum);
538 str = strdup(name);
539 if (!str)
540 return -ENOMEM;
541 ret = device_bind_driver(parent, "usb_dev_generic_drv", str, devp);
542
Simon Glassfc03a552015-03-25 12:22:30 -0600543error:
544 debug("%s: No match found: %d\n", __func__, ret);
545 return ret;
546}
547
548/**
Simon Glassd43d7e92015-11-08 23:47:56 -0700549 * usb_find_child() - Find an existing device which matches our needs
550 *
551 *
Simon Glassfc03a552015-03-25 12:22:30 -0600552 */
Simon Glassd43d7e92015-11-08 23:47:56 -0700553static int usb_find_child(struct udevice *parent,
554 struct usb_device_descriptor *desc,
555 struct usb_interface_descriptor *iface,
556 struct udevice **devp)
Simon Glassfc03a552015-03-25 12:22:30 -0600557{
558 struct udevice *dev;
559
560 *devp = NULL;
561 for (device_find_first_child(parent, &dev);
562 dev;
563 device_find_next_child(&dev)) {
564 struct usb_dev_platdata *plat = dev_get_parent_platdata(dev);
565
566 /* If this device is already in use, skip it */
567 if (device_active(dev))
568 continue;
569 debug(" %s: name='%s', plat=%d, desc=%d\n", __func__,
570 dev->name, plat->id.bDeviceClass, desc->bDeviceClass);
571 if (usb_match_one_id(desc, iface, &plat->id)) {
572 *devp = dev;
573 return 0;
574 }
575 }
Simon Glassd43d7e92015-11-08 23:47:56 -0700576
Simon Glassfc03a552015-03-25 12:22:30 -0600577 return -ENOENT;
578}
579
Simon Glass9b82eeb2015-03-25 12:21:59 -0600580int usb_scan_device(struct udevice *parent, int port,
581 enum usb_device_speed speed, struct udevice **devp)
582{
583 struct udevice *dev;
584 bool created = false;
585 struct usb_dev_platdata *plat;
586 struct usb_bus_priv *priv;
587 struct usb_device *parent_udev;
588 int ret;
589 ALLOC_CACHE_ALIGN_BUFFER(struct usb_device, udev, 1);
590 struct usb_interface_descriptor *iface = &udev->config.if_desc[0].desc;
591
592 *devp = NULL;
593 memset(udev, '\0', sizeof(*udev));
Hans de Goede3b5ddd32015-05-05 11:54:31 +0200594 udev->controller_dev = usb_get_bus(parent);
Simon Glass9b82eeb2015-03-25 12:21:59 -0600595 priv = dev_get_uclass_priv(udev->controller_dev);
596
597 /*
598 * Somewhat nasty, this. We create a local device and use the normal
599 * USB stack to read its descriptor. Then we know what type of device
600 * to create for real.
601 *
602 * udev->dev is set to the parent, since we don't have a real device
603 * yet. The USB stack should not access udev.dev anyway, except perhaps
604 * to find the controller, and the controller will either be @parent,
605 * or some parent of @parent.
606 *
607 * Another option might be to create the device as a generic USB
608 * device, then morph it into the correct one when we know what it
609 * should be. This means that a generic USB device would morph into
610 * a network controller, or a USB flash stick, for example. However,
611 * we don't support such morphing and it isn't clear that it would
612 * be easy to do.
613 *
614 * Yet another option is to split out the USB stack parts of udev
615 * into something like a 'struct urb' (as Linux does) which can exist
616 * independently of any device. This feels cleaner, but calls for quite
617 * a big change to the USB stack.
618 *
619 * For now, the approach is to set up an empty udev, read its
620 * descriptor and assign it an address, then bind a real device and
621 * stash the resulting information into the device's parent
622 * platform data. Then when we probe it, usb_child_pre_probe() is called
623 * and it will pull the information out of the stash.
624 */
625 udev->dev = parent;
626 udev->speed = speed;
627 udev->devnum = priv->next_addr + 1;
628 udev->portnr = port;
629 debug("Calling usb_setup_device(), portnr=%d\n", udev->portnr);
630 parent_udev = device_get_uclass_id(parent) == UCLASS_USB_HUB ?
Simon Glassde44acf2015-09-28 23:32:01 -0600631 dev_get_parent_priv(parent) : NULL;
Hans de Goede778dc1c2015-06-17 21:33:46 +0200632 ret = usb_setup_device(udev, priv->desc_before_addr, parent_udev);
Simon Glass9b82eeb2015-03-25 12:21:59 -0600633 debug("read_descriptor for '%s': ret=%d\n", parent->name, ret);
634 if (ret)
635 return ret;
Simon Glassd43d7e92015-11-08 23:47:56 -0700636 ret = usb_find_child(parent, &udev->descriptor, iface, &dev);
637 debug("** usb_find_child returns %d\n", ret);
Simon Glassfc03a552015-03-25 12:22:30 -0600638 if (ret) {
639 if (ret != -ENOENT)
640 return ret;
641 ret = usb_find_and_bind_driver(parent, &udev->descriptor, iface,
642 udev->controller_dev->seq,
643 udev->devnum, &dev);
644 if (ret)
645 return ret;
646 created = true;
647 }
648 plat = dev_get_parent_platdata(dev);
649 debug("%s: Probing '%s', plat=%p\n", __func__, dev->name, plat);
650 plat->devnum = udev->devnum;
Hans de Goede8a0b4c22015-05-05 11:54:32 +0200651 plat->udev = udev;
Simon Glassfc03a552015-03-25 12:22:30 -0600652 priv->next_addr++;
653 ret = device_probe(dev);
654 if (ret) {
655 debug("%s: Device '%s' probe failed\n", __func__, dev->name);
656 priv->next_addr--;
657 if (created)
658 device_unbind(dev);
659 return ret;
660 }
661 *devp = dev;
Simon Glass9b82eeb2015-03-25 12:21:59 -0600662
Simon Glassfc03a552015-03-25 12:22:30 -0600663 return 0;
Simon Glass9b82eeb2015-03-25 12:21:59 -0600664}
665
Simon Glassfbb14942015-05-13 07:02:23 -0600666/*
667 * Detect if a USB device has been plugged or unplugged.
668 */
669int usb_detect_change(void)
670{
671 struct udevice *hub;
672 struct uclass *uc;
673 int change = 0;
674 int ret;
675
676 ret = uclass_get(UCLASS_USB_HUB, &uc);
677 if (ret)
678 return ret;
679
680 uclass_foreach_dev(hub, uc) {
681 struct usb_device *udev;
682 struct udevice *dev;
683
684 if (!device_active(hub))
685 continue;
686 for (device_find_first_child(hub, &dev);
687 dev;
688 device_find_next_child(&dev)) {
689 struct usb_port_status status;
690
691 if (!device_active(dev))
692 continue;
693
Simon Glassde44acf2015-09-28 23:32:01 -0600694 udev = dev_get_parent_priv(dev);
Simon Glassfbb14942015-05-13 07:02:23 -0600695 if (usb_get_port_status(udev, udev->portnr, &status)
696 < 0)
697 /* USB request failed */
698 continue;
699
700 if (le16_to_cpu(status.wPortChange) &
701 USB_PORT_STAT_C_CONNECTION)
702 change++;
703 }
704 }
705
706 return change;
707}
708
Masahiro Yamada6d8e4332017-06-22 16:35:14 +0900709static int usb_child_post_bind(struct udevice *dev)
Simon Glass9b82eeb2015-03-25 12:21:59 -0600710{
711 struct usb_dev_platdata *plat = dev_get_parent_platdata(dev);
Simon Glass9b82eeb2015-03-25 12:21:59 -0600712 int val;
713
Simon Glass6deb8e22017-05-18 20:09:38 -0600714 if (!dev_of_valid(dev))
Simon Glass9b82eeb2015-03-25 12:21:59 -0600715 return 0;
716
717 /* We only support matching a few things */
Simon Glass6deb8e22017-05-18 20:09:38 -0600718 val = dev_read_u32_default(dev, "usb,device-class", -1);
Simon Glass9b82eeb2015-03-25 12:21:59 -0600719 if (val != -1) {
720 plat->id.match_flags |= USB_DEVICE_ID_MATCH_DEV_CLASS;
721 plat->id.bDeviceClass = val;
722 }
Simon Glass6deb8e22017-05-18 20:09:38 -0600723 val = dev_read_u32_default(dev, "usb,interface-class", -1);
Simon Glass9b82eeb2015-03-25 12:21:59 -0600724 if (val != -1) {
725 plat->id.match_flags |= USB_DEVICE_ID_MATCH_INT_CLASS;
726 plat->id.bInterfaceClass = val;
727 }
728
729 return 0;
730}
731
Hans de Goede3b5ddd32015-05-05 11:54:31 +0200732struct udevice *usb_get_bus(struct udevice *dev)
Simon Glass9b82eeb2015-03-25 12:21:59 -0600733{
734 struct udevice *bus;
735
Simon Glass9b82eeb2015-03-25 12:21:59 -0600736 for (bus = dev; bus && device_get_uclass_id(bus) != UCLASS_USB; )
737 bus = bus->parent;
738 if (!bus) {
739 /* By design this cannot happen */
740 assert(bus);
741 debug("USB HUB '%s' does not have a controller\n", dev->name);
Simon Glass9b82eeb2015-03-25 12:21:59 -0600742 }
Simon Glass9b82eeb2015-03-25 12:21:59 -0600743
Hans de Goede3b5ddd32015-05-05 11:54:31 +0200744 return bus;
Simon Glass9b82eeb2015-03-25 12:21:59 -0600745}
746
747int usb_child_pre_probe(struct udevice *dev)
748{
Simon Glassde44acf2015-09-28 23:32:01 -0600749 struct usb_device *udev = dev_get_parent_priv(dev);
Simon Glass9b82eeb2015-03-25 12:21:59 -0600750 struct usb_dev_platdata *plat = dev_get_parent_platdata(dev);
751 int ret;
752
Hans de Goede8a0b4c22015-05-05 11:54:32 +0200753 if (plat->udev) {
754 /*
755 * Copy over all the values set in the on stack struct
756 * usb_device in usb_scan_device() to our final struct
757 * usb_device for this dev.
758 */
759 *udev = *(plat->udev);
760 /* And clear plat->udev as it will not be valid for long */
761 plat->udev = NULL;
762 udev->dev = dev;
763 } else {
764 /*
765 * This happens with devices which are explicitly bound
766 * instead of being discovered through usb_scan_device()
767 * such as sandbox emul devices.
768 */
769 udev->dev = dev;
770 udev->controller_dev = usb_get_bus(dev);
771 udev->devnum = plat->devnum;
Simon Glass9b82eeb2015-03-25 12:21:59 -0600772
Hans de Goede8a0b4c22015-05-05 11:54:32 +0200773 /*
774 * udev did not go through usb_scan_device(), so we need to
775 * select the config and read the config descriptors.
776 */
777 ret = usb_select_config(udev);
778 if (ret)
779 return ret;
780 }
Simon Glass9b82eeb2015-03-25 12:21:59 -0600781
782 return 0;
783}
784
785UCLASS_DRIVER(usb) = {
786 .id = UCLASS_USB,
787 .name = "usb",
788 .flags = DM_UC_FLAG_SEQ_ALIAS,
Simon Glass18230342016-07-05 17:10:10 -0600789 .post_bind = dm_scan_fdt_dev,
Hans de Goedecebc5b72015-05-10 14:10:21 +0200790 .priv_auto_alloc_size = sizeof(struct usb_uclass_priv),
Simon Glass9b82eeb2015-03-25 12:21:59 -0600791 .per_child_auto_alloc_size = sizeof(struct usb_device),
792 .per_device_auto_alloc_size = sizeof(struct usb_bus_priv),
793 .child_post_bind = usb_child_post_bind,
794 .child_pre_probe = usb_child_pre_probe,
795 .per_child_platdata_auto_alloc_size = sizeof(struct usb_dev_platdata),
796};
Simon Glassc79173e2015-03-25 12:22:31 -0600797
798UCLASS_DRIVER(usb_dev_generic) = {
799 .id = UCLASS_USB_DEV_GENERIC,
800 .name = "usb_dev_generic",
801};
802
803U_BOOT_DRIVER(usb_dev_generic_drv) = {
804 .id = UCLASS_USB_DEV_GENERIC,
805 .name = "usb_dev_generic_drv",
806};