blob: bfc0556d7d660a6215475356848340db63c65350 [file] [log] [blame]
Simon Glassde312132015-03-25 12:21:59 -06001/*
2 * (C) Copyright 2015 Google, Inc
3 * Written by Simon Glass <sjg@chromium.org>
4 *
Simon Glass0566e242015-03-25 12:22:30 -06005 * usb_match_device() modified from Linux kernel v4.0.
6 *
Simon Glassde312132015-03-25 12:21:59 -06007 * SPDX-License-Identifier: GPL-2.0+
8 */
9
10#include <common.h>
11#include <dm.h>
12#include <errno.h>
Simon Glasscf92e052015-09-02 17:24:58 -060013#include <memalign.h>
Simon Glassde312132015-03-25 12:21:59 -060014#include <usb.h>
15#include <dm/device-internal.h>
16#include <dm/lists.h>
Simon Glassde312132015-03-25 12:21:59 -060017#include <dm/uclass-internal.h>
18
19DECLARE_GLOBAL_DATA_PTR;
20
21extern bool usb_started; /* flag for the started/stopped USB status */
22static bool asynch_allowed;
23
Hans de Goedee2536372015-05-10 14:10:21 +020024struct usb_uclass_priv {
25 int companion_device_count;
26};
27
Simon Glassde312132015-03-25 12:21:59 -060028int usb_disable_asynch(int disable)
29{
30 int old_value = asynch_allowed;
31
32 asynch_allowed = !disable;
33 return old_value;
34}
35
36int submit_int_msg(struct usb_device *udev, unsigned long pipe, void *buffer,
37 int length, int interval)
38{
39 struct udevice *bus = udev->controller_dev;
40 struct dm_usb_ops *ops = usb_get_ops(bus);
41
42 if (!ops->interrupt)
43 return -ENOSYS;
44
45 return ops->interrupt(bus, udev, pipe, buffer, length, interval);
46}
47
48int submit_control_msg(struct usb_device *udev, unsigned long pipe,
49 void *buffer, int length, struct devrequest *setup)
50{
51 struct udevice *bus = udev->controller_dev;
52 struct dm_usb_ops *ops = usb_get_ops(bus);
Hans de Goedee2536372015-05-10 14:10:21 +020053 struct usb_uclass_priv *uc_priv = bus->uclass->priv;
54 int err;
Simon Glassde312132015-03-25 12:21:59 -060055
56 if (!ops->control)
57 return -ENOSYS;
58
Hans de Goedee2536372015-05-10 14:10:21 +020059 err = ops->control(bus, udev, pipe, buffer, length, setup);
60 if (setup->request == USB_REQ_SET_FEATURE &&
61 setup->requesttype == USB_RT_PORT &&
62 setup->value == cpu_to_le16(USB_PORT_FEAT_RESET) &&
63 err == -ENXIO) {
64 /* Device handed over to companion after port reset */
65 uc_priv->companion_device_count++;
66 }
67
68 return err;
Simon Glassde312132015-03-25 12:21:59 -060069}
70
71int submit_bulk_msg(struct usb_device *udev, unsigned long pipe, void *buffer,
72 int length)
73{
74 struct udevice *bus = udev->controller_dev;
75 struct dm_usb_ops *ops = usb_get_ops(bus);
76
77 if (!ops->bulk)
78 return -ENOSYS;
79
80 return ops->bulk(bus, udev, pipe, buffer, length);
81}
82
Hans de Goede8a5f0662015-05-10 14:10:18 +020083struct int_queue *create_int_queue(struct usb_device *udev,
84 unsigned long pipe, int queuesize, int elementsize,
85 void *buffer, int interval)
86{
87 struct udevice *bus = udev->controller_dev;
88 struct dm_usb_ops *ops = usb_get_ops(bus);
89
90 if (!ops->create_int_queue)
91 return NULL;
92
93 return ops->create_int_queue(bus, udev, pipe, queuesize, elementsize,
94 buffer, interval);
95}
96
97void *poll_int_queue(struct usb_device *udev, struct int_queue *queue)
98{
99 struct udevice *bus = udev->controller_dev;
100 struct dm_usb_ops *ops = usb_get_ops(bus);
101
102 if (!ops->poll_int_queue)
103 return NULL;
104
105 return ops->poll_int_queue(bus, udev, queue);
106}
107
108int destroy_int_queue(struct usb_device *udev, struct int_queue *queue)
109{
110 struct udevice *bus = udev->controller_dev;
111 struct dm_usb_ops *ops = usb_get_ops(bus);
112
113 if (!ops->destroy_int_queue)
114 return -ENOSYS;
115
116 return ops->destroy_int_queue(bus, udev, queue);
117}
118
Simon Glassde312132015-03-25 12:21:59 -0600119int usb_alloc_device(struct usb_device *udev)
120{
121 struct udevice *bus = udev->controller_dev;
122 struct dm_usb_ops *ops = usb_get_ops(bus);
123
124 /* This is only requird by some controllers - current XHCI */
125 if (!ops->alloc_device)
126 return 0;
127
128 return ops->alloc_device(bus, udev);
129}
130
Hans de Goedeb2f219b2015-06-17 21:33:52 +0200131int usb_reset_root_port(struct usb_device *udev)
132{
133 struct udevice *bus = udev->controller_dev;
134 struct dm_usb_ops *ops = usb_get_ops(bus);
135
136 if (!ops->reset_root_port)
137 return -ENOSYS;
138
139 return ops->reset_root_port(bus, udev);
140}
141
Bin Meng9ca1b4b2017-07-19 21:51:17 +0800142int usb_update_hub_device(struct usb_device *udev)
143{
144 struct udevice *bus = udev->controller_dev;
145 struct dm_usb_ops *ops = usb_get_ops(bus);
146
147 if (!ops->update_hub_device)
148 return -ENOSYS;
149
150 return ops->update_hub_device(bus, udev);
151}
152
Bin Meng3e59f592017-09-07 06:13:17 -0700153int usb_get_max_xfer_size(struct usb_device *udev, size_t *size)
154{
155 struct udevice *bus = udev->controller_dev;
156 struct dm_usb_ops *ops = usb_get_ops(bus);
157
158 if (!ops->get_max_xfer_size)
159 return -ENOSYS;
160
161 return ops->get_max_xfer_size(bus, size);
162}
163
Simon Glassde312132015-03-25 12:21:59 -0600164int usb_stop(void)
165{
166 struct udevice *bus;
Bin Mengd4efefe2017-10-01 06:19:42 -0700167 struct udevice *rh;
Simon Glassde312132015-03-25 12:21:59 -0600168 struct uclass *uc;
Hans de Goedee2536372015-05-10 14:10:21 +0200169 struct usb_uclass_priv *uc_priv;
Simon Glassde312132015-03-25 12:21:59 -0600170 int err = 0, ret;
171
172 /* De-activate any devices that have been activated */
173 ret = uclass_get(UCLASS_USB, &uc);
174 if (ret)
175 return ret;
Hans de Goedee2536372015-05-10 14:10:21 +0200176
177 uc_priv = uc->priv;
178
Simon Glassde312132015-03-25 12:21:59 -0600179 uclass_foreach_dev(bus, uc) {
Stefan Roese706865a2017-03-20 12:51:48 +0100180 ret = device_remove(bus, DM_REMOVE_NORMAL);
Simon Glassde312132015-03-25 12:21:59 -0600181 if (ret && !err)
182 err = ret;
Bin Mengd4efefe2017-10-01 06:19:42 -0700183
184 /* Locate root hub device */
185 device_find_first_child(bus, &rh);
186 if (rh) {
187 /*
188 * All USB devices are children of root hub.
189 * Unbinding root hub will unbind all of its children.
190 */
191 ret = device_unbind(rh);
192 if (ret && !err)
193 err = ret;
194 }
Simon Glassde312132015-03-25 12:21:59 -0600195 }
Bin Mengad0a9372017-10-01 06:19:43 -0700196
Simon Glass095fdef2015-03-25 12:22:38 -0600197#ifdef CONFIG_SANDBOX
198 struct udevice *dev;
199
200 /* Reset all enulation devices */
201 ret = uclass_get(UCLASS_USB_EMUL, &uc);
202 if (ret)
203 return ret;
204
205 uclass_foreach_dev(dev, uc)
206 usb_emul_reset(dev);
207#endif
Paul Kocialkowski0fa59992015-08-04 17:04:12 +0200208#ifdef CONFIG_USB_STORAGE
Simon Glassde312132015-03-25 12:21:59 -0600209 usb_stor_reset();
Paul Kocialkowski0fa59992015-08-04 17:04:12 +0200210#endif
Hans de Goedee2536372015-05-10 14:10:21 +0200211 uc_priv->companion_device_count = 0;
Simon Glassde312132015-03-25 12:21:59 -0600212 usb_started = 0;
213
214 return err;
215}
216
Hans de Goedea24a0e92015-05-10 14:10:19 +0200217static void usb_scan_bus(struct udevice *bus, bool recurse)
Simon Glassde312132015-03-25 12:21:59 -0600218{
219 struct usb_bus_priv *priv;
220 struct udevice *dev;
221 int ret;
222
223 priv = dev_get_uclass_priv(bus);
224
225 assert(recurse); /* TODO: Support non-recusive */
226
Hans de Goedea24a0e92015-05-10 14:10:19 +0200227 printf("scanning bus %d for devices... ", bus->seq);
228 debug("\n");
Simon Glassde312132015-03-25 12:21:59 -0600229 ret = usb_scan_device(bus, 0, USB_SPEED_FULL, &dev);
230 if (ret)
Hans de Goedea24a0e92015-05-10 14:10:19 +0200231 printf("failed, error %d\n", ret);
232 else if (priv->next_addr == 0)
233 printf("No USB Device found\n");
234 else
235 printf("%d USB Device(s) found\n", priv->next_addr);
Simon Glassde312132015-03-25 12:21:59 -0600236}
237
Simon Glasseae11be2015-11-08 23:48:00 -0700238static void remove_inactive_children(struct uclass *uc, struct udevice *bus)
239{
240 uclass_foreach_dev(bus, uc) {
241 struct udevice *dev, *next;
242
243 if (!device_active(bus))
244 continue;
245 device_foreach_child_safe(dev, next, bus) {
246 if (!device_active(dev))
247 device_unbind(dev);
248 }
249 }
250}
251
Simon Glassde312132015-03-25 12:21:59 -0600252int usb_init(void)
253{
254 int controllers_initialized = 0;
Hans de Goedee2536372015-05-10 14:10:21 +0200255 struct usb_uclass_priv *uc_priv;
Hans de Goedeb6de4d12015-05-10 14:10:20 +0200256 struct usb_bus_priv *priv;
Simon Glassde312132015-03-25 12:21:59 -0600257 struct udevice *bus;
258 struct uclass *uc;
259 int count = 0;
260 int ret;
261
262 asynch_allowed = 1;
Simon Glassde312132015-03-25 12:21:59 -0600263
264 ret = uclass_get(UCLASS_USB, &uc);
265 if (ret)
266 return ret;
267
Hans de Goedee2536372015-05-10 14:10:21 +0200268 uc_priv = uc->priv;
269
Simon Glassde312132015-03-25 12:21:59 -0600270 uclass_foreach_dev(bus, uc) {
271 /* init low_level USB */
Hans de Goede134692a2015-05-04 21:33:26 +0200272 printf("USB%d: ", count);
Simon Glassde312132015-03-25 12:21:59 -0600273 count++;
Bin Mengd4efefe2017-10-01 06:19:42 -0700274
275#ifdef CONFIG_SANDBOX
276 /*
277 * For Sandbox, we need scan the device tree each time when we
278 * start the USB stack, in order to re-create the emulated USB
279 * devices and bind drivers for them before we actually do the
280 * driver probe.
281 */
282 ret = dm_scan_fdt_dev(bus);
283 if (ret) {
284 printf("Sandbox USB device scan failed (%d)\n", ret);
285 continue;
286 }
287#endif
288
Simon Glassde312132015-03-25 12:21:59 -0600289 ret = device_probe(bus);
290 if (ret == -ENODEV) { /* No such device. */
291 puts("Port not available.\n");
292 controllers_initialized++;
293 continue;
294 }
295
296 if (ret) { /* Other error. */
297 printf("probe failed, error %d\n", ret);
298 continue;
299 }
Simon Glassde312132015-03-25 12:21:59 -0600300 controllers_initialized++;
Simon Glassde312132015-03-25 12:21:59 -0600301 usb_started = true;
302 }
303
Hans de Goedeb6de4d12015-05-10 14:10:20 +0200304 /*
305 * lowlevel init done, now scan the bus for devices i.e. search HUBs
306 * and configure them, first scan primary controllers.
307 */
308 uclass_foreach_dev(bus, uc) {
309 if (!device_active(bus))
310 continue;
311
312 priv = dev_get_uclass_priv(bus);
313 if (!priv->companion)
314 usb_scan_bus(bus, true);
315 }
316
317 /*
318 * Now that the primary controllers have been scanned and have handed
319 * over any devices they do not understand to their companions, scan
Hans de Goedee2536372015-05-10 14:10:21 +0200320 * the companions if necessary.
Hans de Goedeb6de4d12015-05-10 14:10:20 +0200321 */
Hans de Goedee2536372015-05-10 14:10:21 +0200322 if (uc_priv->companion_device_count) {
323 uclass_foreach_dev(bus, uc) {
324 if (!device_active(bus))
325 continue;
Hans de Goedeb6de4d12015-05-10 14:10:20 +0200326
Hans de Goedee2536372015-05-10 14:10:21 +0200327 priv = dev_get_uclass_priv(bus);
328 if (priv->companion)
329 usb_scan_bus(bus, true);
330 }
Hans de Goedeb6de4d12015-05-10 14:10:20 +0200331 }
332
Simon Glassde312132015-03-25 12:21:59 -0600333 debug("scan end\n");
Simon Glasseae11be2015-11-08 23:48:00 -0700334
335 /* Remove any devices that were not found on this scan */
336 remove_inactive_children(uc, bus);
337
338 ret = uclass_get(UCLASS_USB_HUB, &uc);
339 if (ret)
340 return ret;
341 remove_inactive_children(uc, bus);
342
Simon Glassde312132015-03-25 12:21:59 -0600343 /* if we were not able to find at least one working bus, bail out */
344 if (!count)
345 printf("No controllers found\n");
346 else if (controllers_initialized == 0)
347 printf("USB error: all controllers failed lowlevel init\n");
348
349 return usb_started ? 0 : -1;
350}
351
Simon Glasse8ea5e82015-11-08 23:47:59 -0700352/*
353 * TODO(sjg@chromium.org): Remove this legacy function. At present it is needed
354 * to support boards which use driver model for USB but not Ethernet, and want
355 * to use USB Ethernet.
356 *
357 * The #if clause is here to ensure that remains the only case.
358 */
359#if !defined(CONFIG_DM_ETH) && defined(CONFIG_USB_HOST_ETHER)
Simon Glassde312132015-03-25 12:21:59 -0600360static struct usb_device *find_child_devnum(struct udevice *parent, int devnum)
361{
362 struct usb_device *udev;
363 struct udevice *dev;
364
365 if (!device_active(parent))
366 return NULL;
Simon Glassbcbe3d12015-09-28 23:32:01 -0600367 udev = dev_get_parent_priv(parent);
Simon Glassde312132015-03-25 12:21:59 -0600368 if (udev->devnum == devnum)
369 return udev;
370
371 for (device_find_first_child(parent, &dev);
372 dev;
373 device_find_next_child(&dev)) {
374 udev = find_child_devnum(dev, devnum);
375 if (udev)
376 return udev;
377 }
378
379 return NULL;
380}
381
382struct usb_device *usb_get_dev_index(struct udevice *bus, int index)
383{
Hans de Goedefd1bd212015-06-17 21:33:53 +0200384 struct udevice *dev;
Simon Glassde312132015-03-25 12:21:59 -0600385 int devnum = index + 1; /* Addresses are allocated from 1 on USB */
386
Hans de Goedefd1bd212015-06-17 21:33:53 +0200387 device_find_first_child(bus, &dev);
388 if (!dev)
389 return NULL;
Simon Glassde312132015-03-25 12:21:59 -0600390
Hans de Goedefd1bd212015-06-17 21:33:53 +0200391 return find_child_devnum(dev, devnum);
Simon Glassde312132015-03-25 12:21:59 -0600392}
Simon Glasse8ea5e82015-11-08 23:47:59 -0700393#endif
Simon Glassde312132015-03-25 12:21:59 -0600394
Simon Glassfbeceb22015-03-25 12:22:32 -0600395int usb_setup_ehci_gadget(struct ehci_ctrl **ctlrp)
396{
397 struct usb_platdata *plat;
398 struct udevice *dev;
399 int ret;
400
401 /* Find the old device and remove it */
402 ret = uclass_find_device_by_seq(UCLASS_USB, 0, true, &dev);
403 if (ret)
404 return ret;
Stefan Roese706865a2017-03-20 12:51:48 +0100405 ret = device_remove(dev, DM_REMOVE_NORMAL);
Simon Glassfbeceb22015-03-25 12:22:32 -0600406 if (ret)
407 return ret;
408
409 plat = dev_get_platdata(dev);
410 plat->init_type = USB_INIT_DEVICE;
411 ret = device_probe(dev);
412 if (ret)
413 return ret;
414 *ctlrp = dev_get_priv(dev);
415
416 return 0;
417}
418
Simon Glass0566e242015-03-25 12:22:30 -0600419/* returns 0 if no match, 1 if match */
Masahiro Yamada121a4d12017-06-22 16:35:14 +0900420static int usb_match_device(const struct usb_device_descriptor *desc,
421 const struct usb_device_id *id)
Simon Glass0566e242015-03-25 12:22:30 -0600422{
423 if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
424 id->idVendor != le16_to_cpu(desc->idVendor))
425 return 0;
426
427 if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
428 id->idProduct != le16_to_cpu(desc->idProduct))
429 return 0;
430
431 /* No need to test id->bcdDevice_lo != 0, since 0 is never
432 greater than any unsigned number. */
433 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) &&
434 (id->bcdDevice_lo > le16_to_cpu(desc->bcdDevice)))
435 return 0;
436
437 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) &&
438 (id->bcdDevice_hi < le16_to_cpu(desc->bcdDevice)))
439 return 0;
440
441 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) &&
442 (id->bDeviceClass != desc->bDeviceClass))
443 return 0;
444
445 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) &&
446 (id->bDeviceSubClass != desc->bDeviceSubClass))
447 return 0;
448
449 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) &&
450 (id->bDeviceProtocol != desc->bDeviceProtocol))
451 return 0;
452
453 return 1;
454}
455
456/* returns 0 if no match, 1 if match */
Masahiro Yamada121a4d12017-06-22 16:35:14 +0900457static int usb_match_one_id_intf(const struct usb_device_descriptor *desc,
458 const struct usb_interface_descriptor *int_desc,
459 const struct usb_device_id *id)
Simon Glass0566e242015-03-25 12:22:30 -0600460{
461 /* The interface class, subclass, protocol and number should never be
462 * checked for a match if the device class is Vendor Specific,
463 * unless the match record specifies the Vendor ID. */
464 if (desc->bDeviceClass == USB_CLASS_VENDOR_SPEC &&
465 !(id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
466 (id->match_flags & (USB_DEVICE_ID_MATCH_INT_CLASS |
467 USB_DEVICE_ID_MATCH_INT_SUBCLASS |
468 USB_DEVICE_ID_MATCH_INT_PROTOCOL |
469 USB_DEVICE_ID_MATCH_INT_NUMBER)))
470 return 0;
471
472 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) &&
473 (id->bInterfaceClass != int_desc->bInterfaceClass))
474 return 0;
475
476 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) &&
477 (id->bInterfaceSubClass != int_desc->bInterfaceSubClass))
478 return 0;
479
480 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) &&
481 (id->bInterfaceProtocol != int_desc->bInterfaceProtocol))
482 return 0;
483
484 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_NUMBER) &&
485 (id->bInterfaceNumber != int_desc->bInterfaceNumber))
486 return 0;
487
488 return 1;
489}
490
491/* returns 0 if no match, 1 if match */
Masahiro Yamada121a4d12017-06-22 16:35:14 +0900492static int usb_match_one_id(struct usb_device_descriptor *desc,
493 struct usb_interface_descriptor *int_desc,
494 const struct usb_device_id *id)
Simon Glass0566e242015-03-25 12:22:30 -0600495{
496 if (!usb_match_device(desc, id))
497 return 0;
498
499 return usb_match_one_id_intf(desc, int_desc, id);
500}
501
502/**
503 * usb_find_and_bind_driver() - Find and bind the right USB driver
504 *
505 * This only looks at certain fields in the descriptor.
506 */
507static int usb_find_and_bind_driver(struct udevice *parent,
508 struct usb_device_descriptor *desc,
509 struct usb_interface_descriptor *iface,
510 int bus_seq, int devnum,
511 struct udevice **devp)
512{
513 struct usb_driver_entry *start, *entry;
514 int n_ents;
515 int ret;
516 char name[30], *str;
517
518 *devp = NULL;
519 debug("%s: Searching for driver\n", __func__);
520 start = ll_entry_start(struct usb_driver_entry, usb_driver_entry);
521 n_ents = ll_entry_count(struct usb_driver_entry, usb_driver_entry);
522 for (entry = start; entry != start + n_ents; entry++) {
523 const struct usb_device_id *id;
524 struct udevice *dev;
525 const struct driver *drv;
526 struct usb_dev_platdata *plat;
527
528 for (id = entry->match; id->match_flags; id++) {
529 if (!usb_match_one_id(desc, iface, id))
530 continue;
531
532 drv = entry->driver;
533 /*
534 * We could pass the descriptor to the driver as
535 * platdata (instead of NULL) and allow its bind()
536 * method to return -ENOENT if it doesn't support this
537 * device. That way we could continue the search to
538 * find another driver. For now this doesn't seem
539 * necesssary, so just bind the first match.
540 */
541 ret = device_bind(parent, drv, drv->name, NULL, -1,
542 &dev);
543 if (ret)
544 goto error;
545 debug("%s: Match found: %s\n", __func__, drv->name);
546 dev->driver_data = id->driver_info;
547 plat = dev_get_parent_platdata(dev);
548 plat->id = *id;
549 *devp = dev;
550 return 0;
551 }
552 }
553
Simon Glass449230f2015-03-25 12:22:31 -0600554 /* Bind a generic driver so that the device can be used */
555 snprintf(name, sizeof(name), "generic_bus_%x_dev_%x", bus_seq, devnum);
556 str = strdup(name);
557 if (!str)
558 return -ENOMEM;
559 ret = device_bind_driver(parent, "usb_dev_generic_drv", str, devp);
560
Simon Glass0566e242015-03-25 12:22:30 -0600561error:
562 debug("%s: No match found: %d\n", __func__, ret);
563 return ret;
564}
565
566/**
Simon Glass1b6a1df2015-11-08 23:47:56 -0700567 * usb_find_child() - Find an existing device which matches our needs
568 *
569 *
Simon Glass0566e242015-03-25 12:22:30 -0600570 */
Simon Glass1b6a1df2015-11-08 23:47:56 -0700571static int usb_find_child(struct udevice *parent,
572 struct usb_device_descriptor *desc,
573 struct usb_interface_descriptor *iface,
574 struct udevice **devp)
Simon Glass0566e242015-03-25 12:22:30 -0600575{
576 struct udevice *dev;
577
578 *devp = NULL;
579 for (device_find_first_child(parent, &dev);
580 dev;
581 device_find_next_child(&dev)) {
582 struct usb_dev_platdata *plat = dev_get_parent_platdata(dev);
583
584 /* If this device is already in use, skip it */
585 if (device_active(dev))
586 continue;
587 debug(" %s: name='%s', plat=%d, desc=%d\n", __func__,
588 dev->name, plat->id.bDeviceClass, desc->bDeviceClass);
589 if (usb_match_one_id(desc, iface, &plat->id)) {
590 *devp = dev;
591 return 0;
592 }
593 }
Simon Glass1b6a1df2015-11-08 23:47:56 -0700594
Simon Glass0566e242015-03-25 12:22:30 -0600595 return -ENOENT;
596}
597
Simon Glassde312132015-03-25 12:21:59 -0600598int usb_scan_device(struct udevice *parent, int port,
599 enum usb_device_speed speed, struct udevice **devp)
600{
601 struct udevice *dev;
602 bool created = false;
603 struct usb_dev_platdata *plat;
604 struct usb_bus_priv *priv;
605 struct usb_device *parent_udev;
606 int ret;
607 ALLOC_CACHE_ALIGN_BUFFER(struct usb_device, udev, 1);
608 struct usb_interface_descriptor *iface = &udev->config.if_desc[0].desc;
609
610 *devp = NULL;
611 memset(udev, '\0', sizeof(*udev));
Hans de Goedef78a5c02015-05-05 11:54:31 +0200612 udev->controller_dev = usb_get_bus(parent);
Simon Glassde312132015-03-25 12:21:59 -0600613 priv = dev_get_uclass_priv(udev->controller_dev);
614
615 /*
616 * Somewhat nasty, this. We create a local device and use the normal
617 * USB stack to read its descriptor. Then we know what type of device
618 * to create for real.
619 *
620 * udev->dev is set to the parent, since we don't have a real device
621 * yet. The USB stack should not access udev.dev anyway, except perhaps
622 * to find the controller, and the controller will either be @parent,
623 * or some parent of @parent.
624 *
625 * Another option might be to create the device as a generic USB
626 * device, then morph it into the correct one when we know what it
627 * should be. This means that a generic USB device would morph into
628 * a network controller, or a USB flash stick, for example. However,
629 * we don't support such morphing and it isn't clear that it would
630 * be easy to do.
631 *
632 * Yet another option is to split out the USB stack parts of udev
633 * into something like a 'struct urb' (as Linux does) which can exist
634 * independently of any device. This feels cleaner, but calls for quite
635 * a big change to the USB stack.
636 *
637 * For now, the approach is to set up an empty udev, read its
638 * descriptor and assign it an address, then bind a real device and
639 * stash the resulting information into the device's parent
640 * platform data. Then when we probe it, usb_child_pre_probe() is called
641 * and it will pull the information out of the stash.
642 */
643 udev->dev = parent;
644 udev->speed = speed;
645 udev->devnum = priv->next_addr + 1;
646 udev->portnr = port;
647 debug("Calling usb_setup_device(), portnr=%d\n", udev->portnr);
648 parent_udev = device_get_uclass_id(parent) == UCLASS_USB_HUB ?
Simon Glassbcbe3d12015-09-28 23:32:01 -0600649 dev_get_parent_priv(parent) : NULL;
Hans de Goede9eb72dd2015-06-17 21:33:46 +0200650 ret = usb_setup_device(udev, priv->desc_before_addr, parent_udev);
Simon Glassde312132015-03-25 12:21:59 -0600651 debug("read_descriptor for '%s': ret=%d\n", parent->name, ret);
652 if (ret)
653 return ret;
Simon Glass1b6a1df2015-11-08 23:47:56 -0700654 ret = usb_find_child(parent, &udev->descriptor, iface, &dev);
655 debug("** usb_find_child returns %d\n", ret);
Simon Glass0566e242015-03-25 12:22:30 -0600656 if (ret) {
657 if (ret != -ENOENT)
658 return ret;
659 ret = usb_find_and_bind_driver(parent, &udev->descriptor, iface,
660 udev->controller_dev->seq,
661 udev->devnum, &dev);
662 if (ret)
663 return ret;
664 created = true;
665 }
666 plat = dev_get_parent_platdata(dev);
667 debug("%s: Probing '%s', plat=%p\n", __func__, dev->name, plat);
668 plat->devnum = udev->devnum;
Hans de Goede7f1a0752015-05-05 11:54:32 +0200669 plat->udev = udev;
Simon Glass0566e242015-03-25 12:22:30 -0600670 priv->next_addr++;
671 ret = device_probe(dev);
672 if (ret) {
673 debug("%s: Device '%s' probe failed\n", __func__, dev->name);
674 priv->next_addr--;
675 if (created)
676 device_unbind(dev);
677 return ret;
678 }
679 *devp = dev;
Simon Glassde312132015-03-25 12:21:59 -0600680
Simon Glass0566e242015-03-25 12:22:30 -0600681 return 0;
Simon Glassde312132015-03-25 12:21:59 -0600682}
683
Simon Glass0c5dd9a2015-05-13 07:02:23 -0600684/*
685 * Detect if a USB device has been plugged or unplugged.
686 */
687int usb_detect_change(void)
688{
689 struct udevice *hub;
690 struct uclass *uc;
691 int change = 0;
692 int ret;
693
694 ret = uclass_get(UCLASS_USB_HUB, &uc);
695 if (ret)
696 return ret;
697
698 uclass_foreach_dev(hub, uc) {
699 struct usb_device *udev;
700 struct udevice *dev;
701
702 if (!device_active(hub))
703 continue;
704 for (device_find_first_child(hub, &dev);
705 dev;
706 device_find_next_child(&dev)) {
707 struct usb_port_status status;
708
709 if (!device_active(dev))
710 continue;
711
Simon Glassbcbe3d12015-09-28 23:32:01 -0600712 udev = dev_get_parent_priv(dev);
Simon Glass0c5dd9a2015-05-13 07:02:23 -0600713 if (usb_get_port_status(udev, udev->portnr, &status)
714 < 0)
715 /* USB request failed */
716 continue;
717
718 if (le16_to_cpu(status.wPortChange) &
719 USB_PORT_STAT_C_CONNECTION)
720 change++;
721 }
722 }
723
724 return change;
725}
726
Masahiro Yamada121a4d12017-06-22 16:35:14 +0900727static int usb_child_post_bind(struct udevice *dev)
Simon Glassde312132015-03-25 12:21:59 -0600728{
729 struct usb_dev_platdata *plat = dev_get_parent_platdata(dev);
Simon Glassde312132015-03-25 12:21:59 -0600730 int val;
731
Simon Glassd20fd272017-05-18 20:09:38 -0600732 if (!dev_of_valid(dev))
Simon Glassde312132015-03-25 12:21:59 -0600733 return 0;
734
735 /* We only support matching a few things */
Simon Glassd20fd272017-05-18 20:09:38 -0600736 val = dev_read_u32_default(dev, "usb,device-class", -1);
Simon Glassde312132015-03-25 12:21:59 -0600737 if (val != -1) {
738 plat->id.match_flags |= USB_DEVICE_ID_MATCH_DEV_CLASS;
739 plat->id.bDeviceClass = val;
740 }
Simon Glassd20fd272017-05-18 20:09:38 -0600741 val = dev_read_u32_default(dev, "usb,interface-class", -1);
Simon Glassde312132015-03-25 12:21:59 -0600742 if (val != -1) {
743 plat->id.match_flags |= USB_DEVICE_ID_MATCH_INT_CLASS;
744 plat->id.bInterfaceClass = val;
745 }
746
747 return 0;
748}
749
Hans de Goedef78a5c02015-05-05 11:54:31 +0200750struct udevice *usb_get_bus(struct udevice *dev)
Simon Glassde312132015-03-25 12:21:59 -0600751{
752 struct udevice *bus;
753
Simon Glassde312132015-03-25 12:21:59 -0600754 for (bus = dev; bus && device_get_uclass_id(bus) != UCLASS_USB; )
755 bus = bus->parent;
756 if (!bus) {
757 /* By design this cannot happen */
758 assert(bus);
759 debug("USB HUB '%s' does not have a controller\n", dev->name);
Simon Glassde312132015-03-25 12:21:59 -0600760 }
Simon Glassde312132015-03-25 12:21:59 -0600761
Hans de Goedef78a5c02015-05-05 11:54:31 +0200762 return bus;
Simon Glassde312132015-03-25 12:21:59 -0600763}
764
765int usb_child_pre_probe(struct udevice *dev)
766{
Simon Glassbcbe3d12015-09-28 23:32:01 -0600767 struct usb_device *udev = dev_get_parent_priv(dev);
Simon Glassde312132015-03-25 12:21:59 -0600768 struct usb_dev_platdata *plat = dev_get_parent_platdata(dev);
769 int ret;
770
Hans de Goede7f1a0752015-05-05 11:54:32 +0200771 if (plat->udev) {
772 /*
773 * Copy over all the values set in the on stack struct
774 * usb_device in usb_scan_device() to our final struct
775 * usb_device for this dev.
776 */
777 *udev = *(plat->udev);
778 /* And clear plat->udev as it will not be valid for long */
779 plat->udev = NULL;
780 udev->dev = dev;
781 } else {
782 /*
783 * This happens with devices which are explicitly bound
784 * instead of being discovered through usb_scan_device()
785 * such as sandbox emul devices.
786 */
787 udev->dev = dev;
788 udev->controller_dev = usb_get_bus(dev);
789 udev->devnum = plat->devnum;
Simon Glassde312132015-03-25 12:21:59 -0600790
Hans de Goede7f1a0752015-05-05 11:54:32 +0200791 /*
792 * udev did not go through usb_scan_device(), so we need to
793 * select the config and read the config descriptors.
794 */
795 ret = usb_select_config(udev);
796 if (ret)
797 return ret;
798 }
Simon Glassde312132015-03-25 12:21:59 -0600799
800 return 0;
801}
802
803UCLASS_DRIVER(usb) = {
804 .id = UCLASS_USB,
805 .name = "usb",
806 .flags = DM_UC_FLAG_SEQ_ALIAS,
Simon Glass91195482016-07-05 17:10:10 -0600807 .post_bind = dm_scan_fdt_dev,
Hans de Goedee2536372015-05-10 14:10:21 +0200808 .priv_auto_alloc_size = sizeof(struct usb_uclass_priv),
Simon Glassde312132015-03-25 12:21:59 -0600809 .per_child_auto_alloc_size = sizeof(struct usb_device),
810 .per_device_auto_alloc_size = sizeof(struct usb_bus_priv),
811 .child_post_bind = usb_child_post_bind,
812 .child_pre_probe = usb_child_pre_probe,
813 .per_child_platdata_auto_alloc_size = sizeof(struct usb_dev_platdata),
814};
Simon Glass449230f2015-03-25 12:22:31 -0600815
816UCLASS_DRIVER(usb_dev_generic) = {
817 .id = UCLASS_USB_DEV_GENERIC,
818 .name = "usb_dev_generic",
819};
820
821U_BOOT_DRIVER(usb_dev_generic_drv) = {
822 .id = UCLASS_USB_DEV_GENERIC,
823 .name = "usb_dev_generic_drv",
824};