blob: bc44fc3394dab4cda45590c52271a77d7c6d8dc5 [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;
167 struct uclass *uc;
Hans de Goedee2536372015-05-10 14:10:21 +0200168 struct usb_uclass_priv *uc_priv;
Simon Glassde312132015-03-25 12:21:59 -0600169 int err = 0, ret;
170
171 /* De-activate any devices that have been activated */
172 ret = uclass_get(UCLASS_USB, &uc);
173 if (ret)
174 return ret;
Hans de Goedee2536372015-05-10 14:10:21 +0200175
176 uc_priv = uc->priv;
177
Simon Glassde312132015-03-25 12:21:59 -0600178 uclass_foreach_dev(bus, uc) {
Stefan Roese706865a2017-03-20 12:51:48 +0100179 ret = device_remove(bus, DM_REMOVE_NORMAL);
Simon Glassde312132015-03-25 12:21:59 -0600180 if (ret && !err)
181 err = ret;
182 }
Simon Glass9b977562016-03-13 08:22:33 -0600183#ifdef CONFIG_BLK
184 ret = blk_unbind_all(IF_TYPE_USB);
185 if (ret && !err)
186 err = ret;
187#endif
Simon Glass095fdef2015-03-25 12:22:38 -0600188#ifdef CONFIG_SANDBOX
189 struct udevice *dev;
190
191 /* Reset all enulation devices */
192 ret = uclass_get(UCLASS_USB_EMUL, &uc);
193 if (ret)
194 return ret;
195
196 uclass_foreach_dev(dev, uc)
197 usb_emul_reset(dev);
198#endif
Paul Kocialkowski0fa59992015-08-04 17:04:12 +0200199#ifdef CONFIG_USB_STORAGE
Simon Glassde312132015-03-25 12:21:59 -0600200 usb_stor_reset();
Paul Kocialkowski0fa59992015-08-04 17:04:12 +0200201#endif
Hans de Goedee2536372015-05-10 14:10:21 +0200202 uc_priv->companion_device_count = 0;
Simon Glassde312132015-03-25 12:21:59 -0600203 usb_started = 0;
204
205 return err;
206}
207
Hans de Goedea24a0e92015-05-10 14:10:19 +0200208static void usb_scan_bus(struct udevice *bus, bool recurse)
Simon Glassde312132015-03-25 12:21:59 -0600209{
210 struct usb_bus_priv *priv;
211 struct udevice *dev;
212 int ret;
213
214 priv = dev_get_uclass_priv(bus);
215
216 assert(recurse); /* TODO: Support non-recusive */
217
Hans de Goedea24a0e92015-05-10 14:10:19 +0200218 printf("scanning bus %d for devices... ", bus->seq);
219 debug("\n");
Simon Glassde312132015-03-25 12:21:59 -0600220 ret = usb_scan_device(bus, 0, USB_SPEED_FULL, &dev);
221 if (ret)
Hans de Goedea24a0e92015-05-10 14:10:19 +0200222 printf("failed, error %d\n", ret);
223 else if (priv->next_addr == 0)
224 printf("No USB Device found\n");
225 else
226 printf("%d USB Device(s) found\n", priv->next_addr);
Simon Glassde312132015-03-25 12:21:59 -0600227}
228
Simon Glasseae11be2015-11-08 23:48:00 -0700229static void remove_inactive_children(struct uclass *uc, struct udevice *bus)
230{
231 uclass_foreach_dev(bus, uc) {
232 struct udevice *dev, *next;
233
234 if (!device_active(bus))
235 continue;
236 device_foreach_child_safe(dev, next, bus) {
237 if (!device_active(dev))
238 device_unbind(dev);
239 }
240 }
241}
242
Simon Glassde312132015-03-25 12:21:59 -0600243int usb_init(void)
244{
245 int controllers_initialized = 0;
Hans de Goedee2536372015-05-10 14:10:21 +0200246 struct usb_uclass_priv *uc_priv;
Hans de Goedeb6de4d12015-05-10 14:10:20 +0200247 struct usb_bus_priv *priv;
Simon Glassde312132015-03-25 12:21:59 -0600248 struct udevice *bus;
249 struct uclass *uc;
250 int count = 0;
251 int ret;
252
253 asynch_allowed = 1;
Simon Glassde312132015-03-25 12:21:59 -0600254
255 ret = uclass_get(UCLASS_USB, &uc);
256 if (ret)
257 return ret;
258
Hans de Goedee2536372015-05-10 14:10:21 +0200259 uc_priv = uc->priv;
260
Simon Glassde312132015-03-25 12:21:59 -0600261 uclass_foreach_dev(bus, uc) {
262 /* init low_level USB */
Hans de Goede134692a2015-05-04 21:33:26 +0200263 printf("USB%d: ", count);
Simon Glassde312132015-03-25 12:21:59 -0600264 count++;
Simon Glassde312132015-03-25 12:21:59 -0600265 ret = device_probe(bus);
266 if (ret == -ENODEV) { /* No such device. */
267 puts("Port not available.\n");
268 controllers_initialized++;
269 continue;
270 }
271
272 if (ret) { /* Other error. */
273 printf("probe failed, error %d\n", ret);
274 continue;
275 }
Simon Glassde312132015-03-25 12:21:59 -0600276 controllers_initialized++;
Simon Glassde312132015-03-25 12:21:59 -0600277 usb_started = true;
278 }
279
Hans de Goedeb6de4d12015-05-10 14:10:20 +0200280 /*
281 * lowlevel init done, now scan the bus for devices i.e. search HUBs
282 * and configure them, first scan primary controllers.
283 */
284 uclass_foreach_dev(bus, uc) {
285 if (!device_active(bus))
286 continue;
287
288 priv = dev_get_uclass_priv(bus);
289 if (!priv->companion)
290 usb_scan_bus(bus, true);
291 }
292
293 /*
294 * Now that the primary controllers have been scanned and have handed
295 * over any devices they do not understand to their companions, scan
Hans de Goedee2536372015-05-10 14:10:21 +0200296 * the companions if necessary.
Hans de Goedeb6de4d12015-05-10 14:10:20 +0200297 */
Hans de Goedee2536372015-05-10 14:10:21 +0200298 if (uc_priv->companion_device_count) {
299 uclass_foreach_dev(bus, uc) {
300 if (!device_active(bus))
301 continue;
Hans de Goedeb6de4d12015-05-10 14:10:20 +0200302
Hans de Goedee2536372015-05-10 14:10:21 +0200303 priv = dev_get_uclass_priv(bus);
304 if (priv->companion)
305 usb_scan_bus(bus, true);
306 }
Hans de Goedeb6de4d12015-05-10 14:10:20 +0200307 }
308
Simon Glassde312132015-03-25 12:21:59 -0600309 debug("scan end\n");
Simon Glasseae11be2015-11-08 23:48:00 -0700310
311 /* Remove any devices that were not found on this scan */
312 remove_inactive_children(uc, bus);
313
314 ret = uclass_get(UCLASS_USB_HUB, &uc);
315 if (ret)
316 return ret;
317 remove_inactive_children(uc, bus);
318
Simon Glassde312132015-03-25 12:21:59 -0600319 /* if we were not able to find at least one working bus, bail out */
320 if (!count)
321 printf("No controllers found\n");
322 else if (controllers_initialized == 0)
323 printf("USB error: all controllers failed lowlevel init\n");
324
325 return usb_started ? 0 : -1;
326}
327
Simon Glasse8ea5e82015-11-08 23:47:59 -0700328/*
329 * TODO(sjg@chromium.org): Remove this legacy function. At present it is needed
330 * to support boards which use driver model for USB but not Ethernet, and want
331 * to use USB Ethernet.
332 *
333 * The #if clause is here to ensure that remains the only case.
334 */
335#if !defined(CONFIG_DM_ETH) && defined(CONFIG_USB_HOST_ETHER)
Simon Glassde312132015-03-25 12:21:59 -0600336static struct usb_device *find_child_devnum(struct udevice *parent, int devnum)
337{
338 struct usb_device *udev;
339 struct udevice *dev;
340
341 if (!device_active(parent))
342 return NULL;
Simon Glassbcbe3d12015-09-28 23:32:01 -0600343 udev = dev_get_parent_priv(parent);
Simon Glassde312132015-03-25 12:21:59 -0600344 if (udev->devnum == devnum)
345 return udev;
346
347 for (device_find_first_child(parent, &dev);
348 dev;
349 device_find_next_child(&dev)) {
350 udev = find_child_devnum(dev, devnum);
351 if (udev)
352 return udev;
353 }
354
355 return NULL;
356}
357
358struct usb_device *usb_get_dev_index(struct udevice *bus, int index)
359{
Hans de Goedefd1bd212015-06-17 21:33:53 +0200360 struct udevice *dev;
Simon Glassde312132015-03-25 12:21:59 -0600361 int devnum = index + 1; /* Addresses are allocated from 1 on USB */
362
Hans de Goedefd1bd212015-06-17 21:33:53 +0200363 device_find_first_child(bus, &dev);
364 if (!dev)
365 return NULL;
Simon Glassde312132015-03-25 12:21:59 -0600366
Hans de Goedefd1bd212015-06-17 21:33:53 +0200367 return find_child_devnum(dev, devnum);
Simon Glassde312132015-03-25 12:21:59 -0600368}
Simon Glasse8ea5e82015-11-08 23:47:59 -0700369#endif
Simon Glassde312132015-03-25 12:21:59 -0600370
Simon Glassfbeceb22015-03-25 12:22:32 -0600371int usb_setup_ehci_gadget(struct ehci_ctrl **ctlrp)
372{
373 struct usb_platdata *plat;
374 struct udevice *dev;
375 int ret;
376
377 /* Find the old device and remove it */
378 ret = uclass_find_device_by_seq(UCLASS_USB, 0, true, &dev);
379 if (ret)
380 return ret;
Stefan Roese706865a2017-03-20 12:51:48 +0100381 ret = device_remove(dev, DM_REMOVE_NORMAL);
Simon Glassfbeceb22015-03-25 12:22:32 -0600382 if (ret)
383 return ret;
384
385 plat = dev_get_platdata(dev);
386 plat->init_type = USB_INIT_DEVICE;
387 ret = device_probe(dev);
388 if (ret)
389 return ret;
390 *ctlrp = dev_get_priv(dev);
391
392 return 0;
393}
394
Simon Glass0566e242015-03-25 12:22:30 -0600395/* returns 0 if no match, 1 if match */
Masahiro Yamada121a4d12017-06-22 16:35:14 +0900396static int usb_match_device(const struct usb_device_descriptor *desc,
397 const struct usb_device_id *id)
Simon Glass0566e242015-03-25 12:22:30 -0600398{
399 if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
400 id->idVendor != le16_to_cpu(desc->idVendor))
401 return 0;
402
403 if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
404 id->idProduct != le16_to_cpu(desc->idProduct))
405 return 0;
406
407 /* No need to test id->bcdDevice_lo != 0, since 0 is never
408 greater than any unsigned number. */
409 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) &&
410 (id->bcdDevice_lo > le16_to_cpu(desc->bcdDevice)))
411 return 0;
412
413 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) &&
414 (id->bcdDevice_hi < le16_to_cpu(desc->bcdDevice)))
415 return 0;
416
417 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) &&
418 (id->bDeviceClass != desc->bDeviceClass))
419 return 0;
420
421 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) &&
422 (id->bDeviceSubClass != desc->bDeviceSubClass))
423 return 0;
424
425 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) &&
426 (id->bDeviceProtocol != desc->bDeviceProtocol))
427 return 0;
428
429 return 1;
430}
431
432/* returns 0 if no match, 1 if match */
Masahiro Yamada121a4d12017-06-22 16:35:14 +0900433static int usb_match_one_id_intf(const struct usb_device_descriptor *desc,
434 const struct usb_interface_descriptor *int_desc,
435 const struct usb_device_id *id)
Simon Glass0566e242015-03-25 12:22:30 -0600436{
437 /* The interface class, subclass, protocol and number should never be
438 * checked for a match if the device class is Vendor Specific,
439 * unless the match record specifies the Vendor ID. */
440 if (desc->bDeviceClass == USB_CLASS_VENDOR_SPEC &&
441 !(id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
442 (id->match_flags & (USB_DEVICE_ID_MATCH_INT_CLASS |
443 USB_DEVICE_ID_MATCH_INT_SUBCLASS |
444 USB_DEVICE_ID_MATCH_INT_PROTOCOL |
445 USB_DEVICE_ID_MATCH_INT_NUMBER)))
446 return 0;
447
448 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) &&
449 (id->bInterfaceClass != int_desc->bInterfaceClass))
450 return 0;
451
452 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) &&
453 (id->bInterfaceSubClass != int_desc->bInterfaceSubClass))
454 return 0;
455
456 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) &&
457 (id->bInterfaceProtocol != int_desc->bInterfaceProtocol))
458 return 0;
459
460 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_NUMBER) &&
461 (id->bInterfaceNumber != int_desc->bInterfaceNumber))
462 return 0;
463
464 return 1;
465}
466
467/* returns 0 if no match, 1 if match */
Masahiro Yamada121a4d12017-06-22 16:35:14 +0900468static int usb_match_one_id(struct usb_device_descriptor *desc,
469 struct usb_interface_descriptor *int_desc,
470 const struct usb_device_id *id)
Simon Glass0566e242015-03-25 12:22:30 -0600471{
472 if (!usb_match_device(desc, id))
473 return 0;
474
475 return usb_match_one_id_intf(desc, int_desc, id);
476}
477
478/**
479 * usb_find_and_bind_driver() - Find and bind the right USB driver
480 *
481 * This only looks at certain fields in the descriptor.
482 */
483static int usb_find_and_bind_driver(struct udevice *parent,
484 struct usb_device_descriptor *desc,
485 struct usb_interface_descriptor *iface,
486 int bus_seq, int devnum,
487 struct udevice **devp)
488{
489 struct usb_driver_entry *start, *entry;
490 int n_ents;
491 int ret;
492 char name[30], *str;
493
494 *devp = NULL;
495 debug("%s: Searching for driver\n", __func__);
496 start = ll_entry_start(struct usb_driver_entry, usb_driver_entry);
497 n_ents = ll_entry_count(struct usb_driver_entry, usb_driver_entry);
498 for (entry = start; entry != start + n_ents; entry++) {
499 const struct usb_device_id *id;
500 struct udevice *dev;
501 const struct driver *drv;
502 struct usb_dev_platdata *plat;
503
504 for (id = entry->match; id->match_flags; id++) {
505 if (!usb_match_one_id(desc, iface, id))
506 continue;
507
508 drv = entry->driver;
509 /*
510 * We could pass the descriptor to the driver as
511 * platdata (instead of NULL) and allow its bind()
512 * method to return -ENOENT if it doesn't support this
513 * device. That way we could continue the search to
514 * find another driver. For now this doesn't seem
515 * necesssary, so just bind the first match.
516 */
517 ret = device_bind(parent, drv, drv->name, NULL, -1,
518 &dev);
519 if (ret)
520 goto error;
521 debug("%s: Match found: %s\n", __func__, drv->name);
522 dev->driver_data = id->driver_info;
523 plat = dev_get_parent_platdata(dev);
524 plat->id = *id;
525 *devp = dev;
526 return 0;
527 }
528 }
529
Simon Glass449230f2015-03-25 12:22:31 -0600530 /* Bind a generic driver so that the device can be used */
531 snprintf(name, sizeof(name), "generic_bus_%x_dev_%x", bus_seq, devnum);
532 str = strdup(name);
533 if (!str)
534 return -ENOMEM;
535 ret = device_bind_driver(parent, "usb_dev_generic_drv", str, devp);
536
Simon Glass0566e242015-03-25 12:22:30 -0600537error:
538 debug("%s: No match found: %d\n", __func__, ret);
539 return ret;
540}
541
542/**
Simon Glass1b6a1df2015-11-08 23:47:56 -0700543 * usb_find_child() - Find an existing device which matches our needs
544 *
545 *
Simon Glass0566e242015-03-25 12:22:30 -0600546 */
Simon Glass1b6a1df2015-11-08 23:47:56 -0700547static int usb_find_child(struct udevice *parent,
548 struct usb_device_descriptor *desc,
549 struct usb_interface_descriptor *iface,
550 struct udevice **devp)
Simon Glass0566e242015-03-25 12:22:30 -0600551{
552 struct udevice *dev;
553
554 *devp = NULL;
555 for (device_find_first_child(parent, &dev);
556 dev;
557 device_find_next_child(&dev)) {
558 struct usb_dev_platdata *plat = dev_get_parent_platdata(dev);
559
560 /* If this device is already in use, skip it */
561 if (device_active(dev))
562 continue;
563 debug(" %s: name='%s', plat=%d, desc=%d\n", __func__,
564 dev->name, plat->id.bDeviceClass, desc->bDeviceClass);
565 if (usb_match_one_id(desc, iface, &plat->id)) {
566 *devp = dev;
567 return 0;
568 }
569 }
Simon Glass1b6a1df2015-11-08 23:47:56 -0700570
Simon Glass0566e242015-03-25 12:22:30 -0600571 return -ENOENT;
572}
573
Simon Glassde312132015-03-25 12:21:59 -0600574int usb_scan_device(struct udevice *parent, int port,
575 enum usb_device_speed speed, struct udevice **devp)
576{
577 struct udevice *dev;
578 bool created = false;
579 struct usb_dev_platdata *plat;
580 struct usb_bus_priv *priv;
581 struct usb_device *parent_udev;
582 int ret;
583 ALLOC_CACHE_ALIGN_BUFFER(struct usb_device, udev, 1);
584 struct usb_interface_descriptor *iface = &udev->config.if_desc[0].desc;
585
586 *devp = NULL;
587 memset(udev, '\0', sizeof(*udev));
Hans de Goedef78a5c02015-05-05 11:54:31 +0200588 udev->controller_dev = usb_get_bus(parent);
Simon Glassde312132015-03-25 12:21:59 -0600589 priv = dev_get_uclass_priv(udev->controller_dev);
590
591 /*
592 * Somewhat nasty, this. We create a local device and use the normal
593 * USB stack to read its descriptor. Then we know what type of device
594 * to create for real.
595 *
596 * udev->dev is set to the parent, since we don't have a real device
597 * yet. The USB stack should not access udev.dev anyway, except perhaps
598 * to find the controller, and the controller will either be @parent,
599 * or some parent of @parent.
600 *
601 * Another option might be to create the device as a generic USB
602 * device, then morph it into the correct one when we know what it
603 * should be. This means that a generic USB device would morph into
604 * a network controller, or a USB flash stick, for example. However,
605 * we don't support such morphing and it isn't clear that it would
606 * be easy to do.
607 *
608 * Yet another option is to split out the USB stack parts of udev
609 * into something like a 'struct urb' (as Linux does) which can exist
610 * independently of any device. This feels cleaner, but calls for quite
611 * a big change to the USB stack.
612 *
613 * For now, the approach is to set up an empty udev, read its
614 * descriptor and assign it an address, then bind a real device and
615 * stash the resulting information into the device's parent
616 * platform data. Then when we probe it, usb_child_pre_probe() is called
617 * and it will pull the information out of the stash.
618 */
619 udev->dev = parent;
620 udev->speed = speed;
621 udev->devnum = priv->next_addr + 1;
622 udev->portnr = port;
623 debug("Calling usb_setup_device(), portnr=%d\n", udev->portnr);
624 parent_udev = device_get_uclass_id(parent) == UCLASS_USB_HUB ?
Simon Glassbcbe3d12015-09-28 23:32:01 -0600625 dev_get_parent_priv(parent) : NULL;
Hans de Goede9eb72dd2015-06-17 21:33:46 +0200626 ret = usb_setup_device(udev, priv->desc_before_addr, parent_udev);
Simon Glassde312132015-03-25 12:21:59 -0600627 debug("read_descriptor for '%s': ret=%d\n", parent->name, ret);
628 if (ret)
629 return ret;
Simon Glass1b6a1df2015-11-08 23:47:56 -0700630 ret = usb_find_child(parent, &udev->descriptor, iface, &dev);
631 debug("** usb_find_child returns %d\n", ret);
Simon Glass0566e242015-03-25 12:22:30 -0600632 if (ret) {
633 if (ret != -ENOENT)
634 return ret;
635 ret = usb_find_and_bind_driver(parent, &udev->descriptor, iface,
636 udev->controller_dev->seq,
637 udev->devnum, &dev);
638 if (ret)
639 return ret;
640 created = true;
641 }
642 plat = dev_get_parent_platdata(dev);
643 debug("%s: Probing '%s', plat=%p\n", __func__, dev->name, plat);
644 plat->devnum = udev->devnum;
Hans de Goede7f1a0752015-05-05 11:54:32 +0200645 plat->udev = udev;
Simon Glass0566e242015-03-25 12:22:30 -0600646 priv->next_addr++;
647 ret = device_probe(dev);
648 if (ret) {
649 debug("%s: Device '%s' probe failed\n", __func__, dev->name);
650 priv->next_addr--;
651 if (created)
652 device_unbind(dev);
653 return ret;
654 }
655 *devp = dev;
Simon Glassde312132015-03-25 12:21:59 -0600656
Simon Glass0566e242015-03-25 12:22:30 -0600657 return 0;
Simon Glassde312132015-03-25 12:21:59 -0600658}
659
Simon Glass0c5dd9a2015-05-13 07:02:23 -0600660/*
661 * Detect if a USB device has been plugged or unplugged.
662 */
663int usb_detect_change(void)
664{
665 struct udevice *hub;
666 struct uclass *uc;
667 int change = 0;
668 int ret;
669
670 ret = uclass_get(UCLASS_USB_HUB, &uc);
671 if (ret)
672 return ret;
673
674 uclass_foreach_dev(hub, uc) {
675 struct usb_device *udev;
676 struct udevice *dev;
677
678 if (!device_active(hub))
679 continue;
680 for (device_find_first_child(hub, &dev);
681 dev;
682 device_find_next_child(&dev)) {
683 struct usb_port_status status;
684
685 if (!device_active(dev))
686 continue;
687
Simon Glassbcbe3d12015-09-28 23:32:01 -0600688 udev = dev_get_parent_priv(dev);
Simon Glass0c5dd9a2015-05-13 07:02:23 -0600689 if (usb_get_port_status(udev, udev->portnr, &status)
690 < 0)
691 /* USB request failed */
692 continue;
693
694 if (le16_to_cpu(status.wPortChange) &
695 USB_PORT_STAT_C_CONNECTION)
696 change++;
697 }
698 }
699
700 return change;
701}
702
Masahiro Yamada121a4d12017-06-22 16:35:14 +0900703static int usb_child_post_bind(struct udevice *dev)
Simon Glassde312132015-03-25 12:21:59 -0600704{
705 struct usb_dev_platdata *plat = dev_get_parent_platdata(dev);
Simon Glassde312132015-03-25 12:21:59 -0600706 int val;
707
Simon Glassd20fd272017-05-18 20:09:38 -0600708 if (!dev_of_valid(dev))
Simon Glassde312132015-03-25 12:21:59 -0600709 return 0;
710
711 /* We only support matching a few things */
Simon Glassd20fd272017-05-18 20:09:38 -0600712 val = dev_read_u32_default(dev, "usb,device-class", -1);
Simon Glassde312132015-03-25 12:21:59 -0600713 if (val != -1) {
714 plat->id.match_flags |= USB_DEVICE_ID_MATCH_DEV_CLASS;
715 plat->id.bDeviceClass = val;
716 }
Simon Glassd20fd272017-05-18 20:09:38 -0600717 val = dev_read_u32_default(dev, "usb,interface-class", -1);
Simon Glassde312132015-03-25 12:21:59 -0600718 if (val != -1) {
719 plat->id.match_flags |= USB_DEVICE_ID_MATCH_INT_CLASS;
720 plat->id.bInterfaceClass = val;
721 }
722
723 return 0;
724}
725
Hans de Goedef78a5c02015-05-05 11:54:31 +0200726struct udevice *usb_get_bus(struct udevice *dev)
Simon Glassde312132015-03-25 12:21:59 -0600727{
728 struct udevice *bus;
729
Simon Glassde312132015-03-25 12:21:59 -0600730 for (bus = dev; bus && device_get_uclass_id(bus) != UCLASS_USB; )
731 bus = bus->parent;
732 if (!bus) {
733 /* By design this cannot happen */
734 assert(bus);
735 debug("USB HUB '%s' does not have a controller\n", dev->name);
Simon Glassde312132015-03-25 12:21:59 -0600736 }
Simon Glassde312132015-03-25 12:21:59 -0600737
Hans de Goedef78a5c02015-05-05 11:54:31 +0200738 return bus;
Simon Glassde312132015-03-25 12:21:59 -0600739}
740
741int usb_child_pre_probe(struct udevice *dev)
742{
Simon Glassbcbe3d12015-09-28 23:32:01 -0600743 struct usb_device *udev = dev_get_parent_priv(dev);
Simon Glassde312132015-03-25 12:21:59 -0600744 struct usb_dev_platdata *plat = dev_get_parent_platdata(dev);
745 int ret;
746
Hans de Goede7f1a0752015-05-05 11:54:32 +0200747 if (plat->udev) {
748 /*
749 * Copy over all the values set in the on stack struct
750 * usb_device in usb_scan_device() to our final struct
751 * usb_device for this dev.
752 */
753 *udev = *(plat->udev);
754 /* And clear plat->udev as it will not be valid for long */
755 plat->udev = NULL;
756 udev->dev = dev;
757 } else {
758 /*
759 * This happens with devices which are explicitly bound
760 * instead of being discovered through usb_scan_device()
761 * such as sandbox emul devices.
762 */
763 udev->dev = dev;
764 udev->controller_dev = usb_get_bus(dev);
765 udev->devnum = plat->devnum;
Simon Glassde312132015-03-25 12:21:59 -0600766
Hans de Goede7f1a0752015-05-05 11:54:32 +0200767 /*
768 * udev did not go through usb_scan_device(), so we need to
769 * select the config and read the config descriptors.
770 */
771 ret = usb_select_config(udev);
772 if (ret)
773 return ret;
774 }
Simon Glassde312132015-03-25 12:21:59 -0600775
776 return 0;
777}
778
779UCLASS_DRIVER(usb) = {
780 .id = UCLASS_USB,
781 .name = "usb",
782 .flags = DM_UC_FLAG_SEQ_ALIAS,
Simon Glass91195482016-07-05 17:10:10 -0600783 .post_bind = dm_scan_fdt_dev,
Hans de Goedee2536372015-05-10 14:10:21 +0200784 .priv_auto_alloc_size = sizeof(struct usb_uclass_priv),
Simon Glassde312132015-03-25 12:21:59 -0600785 .per_child_auto_alloc_size = sizeof(struct usb_device),
786 .per_device_auto_alloc_size = sizeof(struct usb_bus_priv),
787 .child_post_bind = usb_child_post_bind,
788 .child_pre_probe = usb_child_pre_probe,
789 .per_child_platdata_auto_alloc_size = sizeof(struct usb_dev_platdata),
790};
Simon Glass449230f2015-03-25 12:22:31 -0600791
792UCLASS_DRIVER(usb_dev_generic) = {
793 .id = UCLASS_USB_DEV_GENERIC,
794 .name = "usb_dev_generic",
795};
796
797U_BOOT_DRIVER(usb_dev_generic_drv) = {
798 .id = UCLASS_USB_DEV_GENERIC,
799 .name = "usb_dev_generic_drv",
800};