blob: 6eded4abad029ac11fea795628771ea9e7e40517 [file] [log] [blame]
Simon Glass9b82eeb2015-03-25 12:21:59 -06001/*
2 * (C) Copyright 2015 Google, Inc
3 * Written by Simon Glass <sjg@chromium.org>
4 *
Simon Glassfc03a552015-03-25 12:22:30 -06005 * usb_match_device() modified from Linux kernel v4.0.
6 *
Simon Glass9b82eeb2015-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 Glass2dd337a2015-09-02 17:24:58 -060013#include <memalign.h>
Simon Glass9b82eeb2015-03-25 12:21:59 -060014#include <usb.h>
15#include <dm/device-internal.h>
16#include <dm/lists.h>
Simon Glass9b82eeb2015-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 Goedecebc5b72015-05-10 14:10:21 +020024struct usb_uclass_priv {
25 int companion_device_count;
26};
27
Simon Glass9b82eeb2015-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 Goedecebc5b72015-05-10 14:10:21 +020053 struct usb_uclass_priv *uc_priv = bus->uclass->priv;
54 int err;
Simon Glass9b82eeb2015-03-25 12:21:59 -060055
56 if (!ops->control)
57 return -ENOSYS;
58
Hans de Goedecebc5b72015-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 Glass9b82eeb2015-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 Goede0a7fa272015-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 Glass9b82eeb2015-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 Goedea9e41f82015-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
Simon Glass9b82eeb2015-03-25 12:21:59 -0600142int usb_stop(void)
143{
144 struct udevice *bus;
145 struct uclass *uc;
Hans de Goedecebc5b72015-05-10 14:10:21 +0200146 struct usb_uclass_priv *uc_priv;
Simon Glass9b82eeb2015-03-25 12:21:59 -0600147 int err = 0, ret;
148
149 /* De-activate any devices that have been activated */
150 ret = uclass_get(UCLASS_USB, &uc);
151 if (ret)
152 return ret;
Hans de Goedecebc5b72015-05-10 14:10:21 +0200153
154 uc_priv = uc->priv;
155
Simon Glass9b82eeb2015-03-25 12:21:59 -0600156 uclass_foreach_dev(bus, uc) {
Stefan Roese80b5bc92017-03-20 12:51:48 +0100157 ret = device_remove(bus, DM_REMOVE_NORMAL);
Simon Glass9b82eeb2015-03-25 12:21:59 -0600158 if (ret && !err)
159 err = ret;
160 }
Simon Glassca4b2152016-03-13 08:22:33 -0600161#ifdef CONFIG_BLK
162 ret = blk_unbind_all(IF_TYPE_USB);
163 if (ret && !err)
164 err = ret;
165#endif
Simon Glass41477392015-03-25 12:22:38 -0600166#ifdef CONFIG_SANDBOX
167 struct udevice *dev;
168
169 /* Reset all enulation devices */
170 ret = uclass_get(UCLASS_USB_EMUL, &uc);
171 if (ret)
172 return ret;
173
174 uclass_foreach_dev(dev, uc)
175 usb_emul_reset(dev);
176#endif
Paul Kocialkowskid449dd32015-08-04 17:04:12 +0200177#ifdef CONFIG_USB_STORAGE
Simon Glass9b82eeb2015-03-25 12:21:59 -0600178 usb_stor_reset();
Paul Kocialkowskid449dd32015-08-04 17:04:12 +0200179#endif
Simon Glass9b82eeb2015-03-25 12:21:59 -0600180 usb_hub_reset();
Hans de Goedecebc5b72015-05-10 14:10:21 +0200181 uc_priv->companion_device_count = 0;
Simon Glass9b82eeb2015-03-25 12:21:59 -0600182 usb_started = 0;
183
184 return err;
185}
186
Hans de Goedef8762f92015-05-10 14:10:19 +0200187static void usb_scan_bus(struct udevice *bus, bool recurse)
Simon Glass9b82eeb2015-03-25 12:21:59 -0600188{
189 struct usb_bus_priv *priv;
190 struct udevice *dev;
191 int ret;
192
193 priv = dev_get_uclass_priv(bus);
194
195 assert(recurse); /* TODO: Support non-recusive */
196
Hans de Goedef8762f92015-05-10 14:10:19 +0200197 printf("scanning bus %d for devices... ", bus->seq);
198 debug("\n");
Simon Glass9b82eeb2015-03-25 12:21:59 -0600199 ret = usb_scan_device(bus, 0, USB_SPEED_FULL, &dev);
200 if (ret)
Hans de Goedef8762f92015-05-10 14:10:19 +0200201 printf("failed, error %d\n", ret);
202 else if (priv->next_addr == 0)
203 printf("No USB Device found\n");
204 else
205 printf("%d USB Device(s) found\n", priv->next_addr);
Simon Glass9b82eeb2015-03-25 12:21:59 -0600206}
207
Simon Glass35787d22015-11-08 23:48:00 -0700208static void remove_inactive_children(struct uclass *uc, struct udevice *bus)
209{
210 uclass_foreach_dev(bus, uc) {
211 struct udevice *dev, *next;
212
213 if (!device_active(bus))
214 continue;
215 device_foreach_child_safe(dev, next, bus) {
216 if (!device_active(dev))
217 device_unbind(dev);
218 }
219 }
220}
221
Simon Glass9b82eeb2015-03-25 12:21:59 -0600222int usb_init(void)
223{
224 int controllers_initialized = 0;
Hans de Goedecebc5b72015-05-10 14:10:21 +0200225 struct usb_uclass_priv *uc_priv;
Hans de Goede59a0dfc2015-05-10 14:10:20 +0200226 struct usb_bus_priv *priv;
Simon Glass9b82eeb2015-03-25 12:21:59 -0600227 struct udevice *bus;
228 struct uclass *uc;
229 int count = 0;
230 int ret;
231
232 asynch_allowed = 1;
233 usb_hub_reset();
234
235 ret = uclass_get(UCLASS_USB, &uc);
236 if (ret)
237 return ret;
238
Hans de Goedecebc5b72015-05-10 14:10:21 +0200239 uc_priv = uc->priv;
240
Simon Glass9b82eeb2015-03-25 12:21:59 -0600241 uclass_foreach_dev(bus, uc) {
242 /* init low_level USB */
Hans de Goede8679b3a2015-05-04 21:33:26 +0200243 printf("USB%d: ", count);
Simon Glass9b82eeb2015-03-25 12:21:59 -0600244 count++;
Simon Glass9b82eeb2015-03-25 12:21:59 -0600245 ret = device_probe(bus);
246 if (ret == -ENODEV) { /* No such device. */
247 puts("Port not available.\n");
248 controllers_initialized++;
249 continue;
250 }
251
252 if (ret) { /* Other error. */
253 printf("probe failed, error %d\n", ret);
254 continue;
255 }
Simon Glass9b82eeb2015-03-25 12:21:59 -0600256 controllers_initialized++;
Simon Glass9b82eeb2015-03-25 12:21:59 -0600257 usb_started = true;
258 }
259
Hans de Goede59a0dfc2015-05-10 14:10:20 +0200260 /*
261 * lowlevel init done, now scan the bus for devices i.e. search HUBs
262 * and configure them, first scan primary controllers.
263 */
264 uclass_foreach_dev(bus, uc) {
265 if (!device_active(bus))
266 continue;
267
268 priv = dev_get_uclass_priv(bus);
269 if (!priv->companion)
270 usb_scan_bus(bus, true);
271 }
272
273 /*
274 * Now that the primary controllers have been scanned and have handed
275 * over any devices they do not understand to their companions, scan
Hans de Goedecebc5b72015-05-10 14:10:21 +0200276 * the companions if necessary.
Hans de Goede59a0dfc2015-05-10 14:10:20 +0200277 */
Hans de Goedecebc5b72015-05-10 14:10:21 +0200278 if (uc_priv->companion_device_count) {
279 uclass_foreach_dev(bus, uc) {
280 if (!device_active(bus))
281 continue;
Hans de Goede59a0dfc2015-05-10 14:10:20 +0200282
Hans de Goedecebc5b72015-05-10 14:10:21 +0200283 priv = dev_get_uclass_priv(bus);
284 if (priv->companion)
285 usb_scan_bus(bus, true);
286 }
Hans de Goede59a0dfc2015-05-10 14:10:20 +0200287 }
288
Simon Glass9b82eeb2015-03-25 12:21:59 -0600289 debug("scan end\n");
Simon Glass35787d22015-11-08 23:48:00 -0700290
291 /* Remove any devices that were not found on this scan */
292 remove_inactive_children(uc, bus);
293
294 ret = uclass_get(UCLASS_USB_HUB, &uc);
295 if (ret)
296 return ret;
297 remove_inactive_children(uc, bus);
298
Simon Glass9b82eeb2015-03-25 12:21:59 -0600299 /* if we were not able to find at least one working bus, bail out */
300 if (!count)
301 printf("No controllers found\n");
302 else if (controllers_initialized == 0)
303 printf("USB error: all controllers failed lowlevel init\n");
304
305 return usb_started ? 0 : -1;
306}
307
Simon Glass5082c692015-11-08 23:47:59 -0700308/*
309 * TODO(sjg@chromium.org): Remove this legacy function. At present it is needed
310 * to support boards which use driver model for USB but not Ethernet, and want
311 * to use USB Ethernet.
312 *
313 * The #if clause is here to ensure that remains the only case.
314 */
315#if !defined(CONFIG_DM_ETH) && defined(CONFIG_USB_HOST_ETHER)
Simon Glass9b82eeb2015-03-25 12:21:59 -0600316static struct usb_device *find_child_devnum(struct udevice *parent, int devnum)
317{
318 struct usb_device *udev;
319 struct udevice *dev;
320
321 if (!device_active(parent))
322 return NULL;
Simon Glassde44acf2015-09-28 23:32:01 -0600323 udev = dev_get_parent_priv(parent);
Simon Glass9b82eeb2015-03-25 12:21:59 -0600324 if (udev->devnum == devnum)
325 return udev;
326
327 for (device_find_first_child(parent, &dev);
328 dev;
329 device_find_next_child(&dev)) {
330 udev = find_child_devnum(dev, devnum);
331 if (udev)
332 return udev;
333 }
334
335 return NULL;
336}
337
338struct usb_device *usb_get_dev_index(struct udevice *bus, int index)
339{
Hans de Goede9c203592015-06-17 21:33:53 +0200340 struct udevice *dev;
Simon Glass9b82eeb2015-03-25 12:21:59 -0600341 int devnum = index + 1; /* Addresses are allocated from 1 on USB */
342
Hans de Goede9c203592015-06-17 21:33:53 +0200343 device_find_first_child(bus, &dev);
344 if (!dev)
345 return NULL;
Simon Glass9b82eeb2015-03-25 12:21:59 -0600346
Hans de Goede9c203592015-06-17 21:33:53 +0200347 return find_child_devnum(dev, devnum);
Simon Glass9b82eeb2015-03-25 12:21:59 -0600348}
Simon Glass5082c692015-11-08 23:47:59 -0700349#endif
Simon Glass9b82eeb2015-03-25 12:21:59 -0600350
Simon Glass444b1e02015-03-25 12:22:32 -0600351int usb_setup_ehci_gadget(struct ehci_ctrl **ctlrp)
352{
353 struct usb_platdata *plat;
354 struct udevice *dev;
355 int ret;
356
357 /* Find the old device and remove it */
358 ret = uclass_find_device_by_seq(UCLASS_USB, 0, true, &dev);
359 if (ret)
360 return ret;
Stefan Roese80b5bc92017-03-20 12:51:48 +0100361 ret = device_remove(dev, DM_REMOVE_NORMAL);
Simon Glass444b1e02015-03-25 12:22:32 -0600362 if (ret)
363 return ret;
364
365 plat = dev_get_platdata(dev);
366 plat->init_type = USB_INIT_DEVICE;
367 ret = device_probe(dev);
368 if (ret)
369 return ret;
370 *ctlrp = dev_get_priv(dev);
371
372 return 0;
373}
374
Simon Glassfc03a552015-03-25 12:22:30 -0600375/* returns 0 if no match, 1 if match */
376int usb_match_device(const struct usb_device_descriptor *desc,
377 const struct usb_device_id *id)
378{
379 if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
380 id->idVendor != le16_to_cpu(desc->idVendor))
381 return 0;
382
383 if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
384 id->idProduct != le16_to_cpu(desc->idProduct))
385 return 0;
386
387 /* No need to test id->bcdDevice_lo != 0, since 0 is never
388 greater than any unsigned number. */
389 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) &&
390 (id->bcdDevice_lo > le16_to_cpu(desc->bcdDevice)))
391 return 0;
392
393 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) &&
394 (id->bcdDevice_hi < le16_to_cpu(desc->bcdDevice)))
395 return 0;
396
397 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) &&
398 (id->bDeviceClass != desc->bDeviceClass))
399 return 0;
400
401 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) &&
402 (id->bDeviceSubClass != desc->bDeviceSubClass))
403 return 0;
404
405 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) &&
406 (id->bDeviceProtocol != desc->bDeviceProtocol))
407 return 0;
408
409 return 1;
410}
411
412/* returns 0 if no match, 1 if match */
413int usb_match_one_id_intf(const struct usb_device_descriptor *desc,
414 const struct usb_interface_descriptor *int_desc,
415 const struct usb_device_id *id)
416{
417 /* The interface class, subclass, protocol and number should never be
418 * checked for a match if the device class is Vendor Specific,
419 * unless the match record specifies the Vendor ID. */
420 if (desc->bDeviceClass == USB_CLASS_VENDOR_SPEC &&
421 !(id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
422 (id->match_flags & (USB_DEVICE_ID_MATCH_INT_CLASS |
423 USB_DEVICE_ID_MATCH_INT_SUBCLASS |
424 USB_DEVICE_ID_MATCH_INT_PROTOCOL |
425 USB_DEVICE_ID_MATCH_INT_NUMBER)))
426 return 0;
427
428 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) &&
429 (id->bInterfaceClass != int_desc->bInterfaceClass))
430 return 0;
431
432 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) &&
433 (id->bInterfaceSubClass != int_desc->bInterfaceSubClass))
434 return 0;
435
436 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) &&
437 (id->bInterfaceProtocol != int_desc->bInterfaceProtocol))
438 return 0;
439
440 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_NUMBER) &&
441 (id->bInterfaceNumber != int_desc->bInterfaceNumber))
442 return 0;
443
444 return 1;
445}
446
447/* returns 0 if no match, 1 if match */
448int usb_match_one_id(struct usb_device_descriptor *desc,
449 struct usb_interface_descriptor *int_desc,
450 const struct usb_device_id *id)
451{
452 if (!usb_match_device(desc, id))
453 return 0;
454
455 return usb_match_one_id_intf(desc, int_desc, id);
456}
457
458/**
459 * usb_find_and_bind_driver() - Find and bind the right USB driver
460 *
461 * This only looks at certain fields in the descriptor.
462 */
463static int usb_find_and_bind_driver(struct udevice *parent,
464 struct usb_device_descriptor *desc,
465 struct usb_interface_descriptor *iface,
466 int bus_seq, int devnum,
467 struct udevice **devp)
468{
469 struct usb_driver_entry *start, *entry;
470 int n_ents;
471 int ret;
472 char name[30], *str;
473
474 *devp = NULL;
475 debug("%s: Searching for driver\n", __func__);
476 start = ll_entry_start(struct usb_driver_entry, usb_driver_entry);
477 n_ents = ll_entry_count(struct usb_driver_entry, usb_driver_entry);
478 for (entry = start; entry != start + n_ents; entry++) {
479 const struct usb_device_id *id;
480 struct udevice *dev;
481 const struct driver *drv;
482 struct usb_dev_platdata *plat;
483
484 for (id = entry->match; id->match_flags; id++) {
485 if (!usb_match_one_id(desc, iface, id))
486 continue;
487
488 drv = entry->driver;
489 /*
490 * We could pass the descriptor to the driver as
491 * platdata (instead of NULL) and allow its bind()
492 * method to return -ENOENT if it doesn't support this
493 * device. That way we could continue the search to
494 * find another driver. For now this doesn't seem
495 * necesssary, so just bind the first match.
496 */
497 ret = device_bind(parent, drv, drv->name, NULL, -1,
498 &dev);
499 if (ret)
500 goto error;
501 debug("%s: Match found: %s\n", __func__, drv->name);
502 dev->driver_data = id->driver_info;
503 plat = dev_get_parent_platdata(dev);
504 plat->id = *id;
505 *devp = dev;
506 return 0;
507 }
508 }
509
Simon Glassc79173e2015-03-25 12:22:31 -0600510 /* Bind a generic driver so that the device can be used */
511 snprintf(name, sizeof(name), "generic_bus_%x_dev_%x", bus_seq, devnum);
512 str = strdup(name);
513 if (!str)
514 return -ENOMEM;
515 ret = device_bind_driver(parent, "usb_dev_generic_drv", str, devp);
516
Simon Glassfc03a552015-03-25 12:22:30 -0600517error:
518 debug("%s: No match found: %d\n", __func__, ret);
519 return ret;
520}
521
522/**
Simon Glassd43d7e92015-11-08 23:47:56 -0700523 * usb_find_child() - Find an existing device which matches our needs
524 *
525 *
Simon Glassfc03a552015-03-25 12:22:30 -0600526 */
Simon Glassd43d7e92015-11-08 23:47:56 -0700527static int usb_find_child(struct udevice *parent,
528 struct usb_device_descriptor *desc,
529 struct usb_interface_descriptor *iface,
530 struct udevice **devp)
Simon Glassfc03a552015-03-25 12:22:30 -0600531{
532 struct udevice *dev;
533
534 *devp = NULL;
535 for (device_find_first_child(parent, &dev);
536 dev;
537 device_find_next_child(&dev)) {
538 struct usb_dev_platdata *plat = dev_get_parent_platdata(dev);
539
540 /* If this device is already in use, skip it */
541 if (device_active(dev))
542 continue;
543 debug(" %s: name='%s', plat=%d, desc=%d\n", __func__,
544 dev->name, plat->id.bDeviceClass, desc->bDeviceClass);
545 if (usb_match_one_id(desc, iface, &plat->id)) {
546 *devp = dev;
547 return 0;
548 }
549 }
Simon Glassd43d7e92015-11-08 23:47:56 -0700550
Simon Glassfc03a552015-03-25 12:22:30 -0600551 return -ENOENT;
552}
553
Simon Glass9b82eeb2015-03-25 12:21:59 -0600554int usb_scan_device(struct udevice *parent, int port,
555 enum usb_device_speed speed, struct udevice **devp)
556{
557 struct udevice *dev;
558 bool created = false;
559 struct usb_dev_platdata *plat;
560 struct usb_bus_priv *priv;
561 struct usb_device *parent_udev;
562 int ret;
563 ALLOC_CACHE_ALIGN_BUFFER(struct usb_device, udev, 1);
564 struct usb_interface_descriptor *iface = &udev->config.if_desc[0].desc;
565
566 *devp = NULL;
567 memset(udev, '\0', sizeof(*udev));
Hans de Goede3b5ddd32015-05-05 11:54:31 +0200568 udev->controller_dev = usb_get_bus(parent);
Simon Glass9b82eeb2015-03-25 12:21:59 -0600569 priv = dev_get_uclass_priv(udev->controller_dev);
570
571 /*
572 * Somewhat nasty, this. We create a local device and use the normal
573 * USB stack to read its descriptor. Then we know what type of device
574 * to create for real.
575 *
576 * udev->dev is set to the parent, since we don't have a real device
577 * yet. The USB stack should not access udev.dev anyway, except perhaps
578 * to find the controller, and the controller will either be @parent,
579 * or some parent of @parent.
580 *
581 * Another option might be to create the device as a generic USB
582 * device, then morph it into the correct one when we know what it
583 * should be. This means that a generic USB device would morph into
584 * a network controller, or a USB flash stick, for example. However,
585 * we don't support such morphing and it isn't clear that it would
586 * be easy to do.
587 *
588 * Yet another option is to split out the USB stack parts of udev
589 * into something like a 'struct urb' (as Linux does) which can exist
590 * independently of any device. This feels cleaner, but calls for quite
591 * a big change to the USB stack.
592 *
593 * For now, the approach is to set up an empty udev, read its
594 * descriptor and assign it an address, then bind a real device and
595 * stash the resulting information into the device's parent
596 * platform data. Then when we probe it, usb_child_pre_probe() is called
597 * and it will pull the information out of the stash.
598 */
599 udev->dev = parent;
600 udev->speed = speed;
601 udev->devnum = priv->next_addr + 1;
602 udev->portnr = port;
603 debug("Calling usb_setup_device(), portnr=%d\n", udev->portnr);
604 parent_udev = device_get_uclass_id(parent) == UCLASS_USB_HUB ?
Simon Glassde44acf2015-09-28 23:32:01 -0600605 dev_get_parent_priv(parent) : NULL;
Hans de Goede778dc1c2015-06-17 21:33:46 +0200606 ret = usb_setup_device(udev, priv->desc_before_addr, parent_udev);
Simon Glass9b82eeb2015-03-25 12:21:59 -0600607 debug("read_descriptor for '%s': ret=%d\n", parent->name, ret);
608 if (ret)
609 return ret;
Simon Glassd43d7e92015-11-08 23:47:56 -0700610 ret = usb_find_child(parent, &udev->descriptor, iface, &dev);
611 debug("** usb_find_child returns %d\n", ret);
Simon Glassfc03a552015-03-25 12:22:30 -0600612 if (ret) {
613 if (ret != -ENOENT)
614 return ret;
615 ret = usb_find_and_bind_driver(parent, &udev->descriptor, iface,
616 udev->controller_dev->seq,
617 udev->devnum, &dev);
618 if (ret)
619 return ret;
620 created = true;
621 }
622 plat = dev_get_parent_platdata(dev);
623 debug("%s: Probing '%s', plat=%p\n", __func__, dev->name, plat);
624 plat->devnum = udev->devnum;
Hans de Goede8a0b4c22015-05-05 11:54:32 +0200625 plat->udev = udev;
Simon Glassfc03a552015-03-25 12:22:30 -0600626 priv->next_addr++;
627 ret = device_probe(dev);
628 if (ret) {
629 debug("%s: Device '%s' probe failed\n", __func__, dev->name);
630 priv->next_addr--;
631 if (created)
632 device_unbind(dev);
633 return ret;
634 }
635 *devp = dev;
Simon Glass9b82eeb2015-03-25 12:21:59 -0600636
Simon Glassfc03a552015-03-25 12:22:30 -0600637 return 0;
Simon Glass9b82eeb2015-03-25 12:21:59 -0600638}
639
Simon Glassfbb14942015-05-13 07:02:23 -0600640/*
641 * Detect if a USB device has been plugged or unplugged.
642 */
643int usb_detect_change(void)
644{
645 struct udevice *hub;
646 struct uclass *uc;
647 int change = 0;
648 int ret;
649
650 ret = uclass_get(UCLASS_USB_HUB, &uc);
651 if (ret)
652 return ret;
653
654 uclass_foreach_dev(hub, uc) {
655 struct usb_device *udev;
656 struct udevice *dev;
657
658 if (!device_active(hub))
659 continue;
660 for (device_find_first_child(hub, &dev);
661 dev;
662 device_find_next_child(&dev)) {
663 struct usb_port_status status;
664
665 if (!device_active(dev))
666 continue;
667
Simon Glassde44acf2015-09-28 23:32:01 -0600668 udev = dev_get_parent_priv(dev);
Simon Glassfbb14942015-05-13 07:02:23 -0600669 if (usb_get_port_status(udev, udev->portnr, &status)
670 < 0)
671 /* USB request failed */
672 continue;
673
674 if (le16_to_cpu(status.wPortChange) &
675 USB_PORT_STAT_C_CONNECTION)
676 change++;
677 }
678 }
679
680 return change;
681}
682
Simon Glass9b82eeb2015-03-25 12:21:59 -0600683int usb_child_post_bind(struct udevice *dev)
684{
685 struct usb_dev_platdata *plat = dev_get_parent_platdata(dev);
686 const void *blob = gd->fdt_blob;
687 int val;
688
Simon Glassdd79d6e2017-01-17 16:52:55 -0700689 if (dev_of_offset(dev) == -1)
Simon Glass9b82eeb2015-03-25 12:21:59 -0600690 return 0;
691
692 /* We only support matching a few things */
Simon Glassdd79d6e2017-01-17 16:52:55 -0700693 val = fdtdec_get_int(blob, dev_of_offset(dev), "usb,device-class", -1);
Simon Glass9b82eeb2015-03-25 12:21:59 -0600694 if (val != -1) {
695 plat->id.match_flags |= USB_DEVICE_ID_MATCH_DEV_CLASS;
696 plat->id.bDeviceClass = val;
697 }
Simon Glassdd79d6e2017-01-17 16:52:55 -0700698 val = fdtdec_get_int(blob, dev_of_offset(dev), "usb,interface-class",
699 -1);
Simon Glass9b82eeb2015-03-25 12:21:59 -0600700 if (val != -1) {
701 plat->id.match_flags |= USB_DEVICE_ID_MATCH_INT_CLASS;
702 plat->id.bInterfaceClass = val;
703 }
704
705 return 0;
706}
707
Hans de Goede3b5ddd32015-05-05 11:54:31 +0200708struct udevice *usb_get_bus(struct udevice *dev)
Simon Glass9b82eeb2015-03-25 12:21:59 -0600709{
710 struct udevice *bus;
711
Simon Glass9b82eeb2015-03-25 12:21:59 -0600712 for (bus = dev; bus && device_get_uclass_id(bus) != UCLASS_USB; )
713 bus = bus->parent;
714 if (!bus) {
715 /* By design this cannot happen */
716 assert(bus);
717 debug("USB HUB '%s' does not have a controller\n", dev->name);
Simon Glass9b82eeb2015-03-25 12:21:59 -0600718 }
Simon Glass9b82eeb2015-03-25 12:21:59 -0600719
Hans de Goede3b5ddd32015-05-05 11:54:31 +0200720 return bus;
Simon Glass9b82eeb2015-03-25 12:21:59 -0600721}
722
723int usb_child_pre_probe(struct udevice *dev)
724{
Simon Glassde44acf2015-09-28 23:32:01 -0600725 struct usb_device *udev = dev_get_parent_priv(dev);
Simon Glass9b82eeb2015-03-25 12:21:59 -0600726 struct usb_dev_platdata *plat = dev_get_parent_platdata(dev);
727 int ret;
728
Hans de Goede8a0b4c22015-05-05 11:54:32 +0200729 if (plat->udev) {
730 /*
731 * Copy over all the values set in the on stack struct
732 * usb_device in usb_scan_device() to our final struct
733 * usb_device for this dev.
734 */
735 *udev = *(plat->udev);
736 /* And clear plat->udev as it will not be valid for long */
737 plat->udev = NULL;
738 udev->dev = dev;
739 } else {
740 /*
741 * This happens with devices which are explicitly bound
742 * instead of being discovered through usb_scan_device()
743 * such as sandbox emul devices.
744 */
745 udev->dev = dev;
746 udev->controller_dev = usb_get_bus(dev);
747 udev->devnum = plat->devnum;
Simon Glass9b82eeb2015-03-25 12:21:59 -0600748
Hans de Goede8a0b4c22015-05-05 11:54:32 +0200749 /*
750 * udev did not go through usb_scan_device(), so we need to
751 * select the config and read the config descriptors.
752 */
753 ret = usb_select_config(udev);
754 if (ret)
755 return ret;
756 }
Simon Glass9b82eeb2015-03-25 12:21:59 -0600757
758 return 0;
759}
760
761UCLASS_DRIVER(usb) = {
762 .id = UCLASS_USB,
763 .name = "usb",
764 .flags = DM_UC_FLAG_SEQ_ALIAS,
Simon Glass18230342016-07-05 17:10:10 -0600765 .post_bind = dm_scan_fdt_dev,
Hans de Goedecebc5b72015-05-10 14:10:21 +0200766 .priv_auto_alloc_size = sizeof(struct usb_uclass_priv),
Simon Glass9b82eeb2015-03-25 12:21:59 -0600767 .per_child_auto_alloc_size = sizeof(struct usb_device),
768 .per_device_auto_alloc_size = sizeof(struct usb_bus_priv),
769 .child_post_bind = usb_child_post_bind,
770 .child_pre_probe = usb_child_pre_probe,
771 .per_child_platdata_auto_alloc_size = sizeof(struct usb_dev_platdata),
772};
Simon Glassc79173e2015-03-25 12:22:31 -0600773
774UCLASS_DRIVER(usb_dev_generic) = {
775 .id = UCLASS_USB_DEV_GENERIC,
776 .name = "usb_dev_generic",
777};
778
779U_BOOT_DRIVER(usb_dev_generic_drv) = {
780 .id = UCLASS_USB_DEV_GENERIC,
781 .name = "usb_dev_generic_drv",
782};