blob: 69c9a504ebb8ad8489523868b8991d871115c3d4 [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>
17#include <dm/root.h>
18#include <dm/uclass-internal.h>
19
20DECLARE_GLOBAL_DATA_PTR;
21
22extern bool usb_started; /* flag for the started/stopped USB status */
23static bool asynch_allowed;
24
Hans de Goedecebc5b72015-05-10 14:10:21 +020025struct usb_uclass_priv {
26 int companion_device_count;
27};
28
Simon Glass9b82eeb2015-03-25 12:21:59 -060029int usb_disable_asynch(int disable)
30{
31 int old_value = asynch_allowed;
32
33 asynch_allowed = !disable;
34 return old_value;
35}
36
37int submit_int_msg(struct usb_device *udev, unsigned long pipe, void *buffer,
38 int length, int interval)
39{
40 struct udevice *bus = udev->controller_dev;
41 struct dm_usb_ops *ops = usb_get_ops(bus);
42
43 if (!ops->interrupt)
44 return -ENOSYS;
45
46 return ops->interrupt(bus, udev, pipe, buffer, length, interval);
47}
48
49int submit_control_msg(struct usb_device *udev, unsigned long pipe,
50 void *buffer, int length, struct devrequest *setup)
51{
52 struct udevice *bus = udev->controller_dev;
53 struct dm_usb_ops *ops = usb_get_ops(bus);
Hans de Goedecebc5b72015-05-10 14:10:21 +020054 struct usb_uclass_priv *uc_priv = bus->uclass->priv;
55 int err;
Simon Glass9b82eeb2015-03-25 12:21:59 -060056
57 if (!ops->control)
58 return -ENOSYS;
59
Hans de Goedecebc5b72015-05-10 14:10:21 +020060 err = ops->control(bus, udev, pipe, buffer, length, setup);
61 if (setup->request == USB_REQ_SET_FEATURE &&
62 setup->requesttype == USB_RT_PORT &&
63 setup->value == cpu_to_le16(USB_PORT_FEAT_RESET) &&
64 err == -ENXIO) {
65 /* Device handed over to companion after port reset */
66 uc_priv->companion_device_count++;
67 }
68
69 return err;
Simon Glass9b82eeb2015-03-25 12:21:59 -060070}
71
72int submit_bulk_msg(struct usb_device *udev, unsigned long pipe, void *buffer,
73 int length)
74{
75 struct udevice *bus = udev->controller_dev;
76 struct dm_usb_ops *ops = usb_get_ops(bus);
77
78 if (!ops->bulk)
79 return -ENOSYS;
80
81 return ops->bulk(bus, udev, pipe, buffer, length);
82}
83
Hans de Goede0a7fa272015-05-10 14:10:18 +020084struct int_queue *create_int_queue(struct usb_device *udev,
85 unsigned long pipe, int queuesize, int elementsize,
86 void *buffer, int interval)
87{
88 struct udevice *bus = udev->controller_dev;
89 struct dm_usb_ops *ops = usb_get_ops(bus);
90
91 if (!ops->create_int_queue)
92 return NULL;
93
94 return ops->create_int_queue(bus, udev, pipe, queuesize, elementsize,
95 buffer, interval);
96}
97
98void *poll_int_queue(struct usb_device *udev, struct int_queue *queue)
99{
100 struct udevice *bus = udev->controller_dev;
101 struct dm_usb_ops *ops = usb_get_ops(bus);
102
103 if (!ops->poll_int_queue)
104 return NULL;
105
106 return ops->poll_int_queue(bus, udev, queue);
107}
108
109int destroy_int_queue(struct usb_device *udev, struct int_queue *queue)
110{
111 struct udevice *bus = udev->controller_dev;
112 struct dm_usb_ops *ops = usb_get_ops(bus);
113
114 if (!ops->destroy_int_queue)
115 return -ENOSYS;
116
117 return ops->destroy_int_queue(bus, udev, queue);
118}
119
Simon Glass9b82eeb2015-03-25 12:21:59 -0600120int usb_alloc_device(struct usb_device *udev)
121{
122 struct udevice *bus = udev->controller_dev;
123 struct dm_usb_ops *ops = usb_get_ops(bus);
124
125 /* This is only requird by some controllers - current XHCI */
126 if (!ops->alloc_device)
127 return 0;
128
129 return ops->alloc_device(bus, udev);
130}
131
Hans de Goedea9e41f82015-06-17 21:33:52 +0200132int usb_reset_root_port(struct usb_device *udev)
133{
134 struct udevice *bus = udev->controller_dev;
135 struct dm_usb_ops *ops = usb_get_ops(bus);
136
137 if (!ops->reset_root_port)
138 return -ENOSYS;
139
140 return ops->reset_root_port(bus, udev);
141}
142
Simon Glass9b82eeb2015-03-25 12:21:59 -0600143int usb_stop(void)
144{
145 struct udevice *bus;
146 struct uclass *uc;
Hans de Goedecebc5b72015-05-10 14:10:21 +0200147 struct usb_uclass_priv *uc_priv;
Simon Glass9b82eeb2015-03-25 12:21:59 -0600148 int err = 0, ret;
149
150 /* De-activate any devices that have been activated */
151 ret = uclass_get(UCLASS_USB, &uc);
152 if (ret)
153 return ret;
Hans de Goedecebc5b72015-05-10 14:10:21 +0200154
155 uc_priv = uc->priv;
156
Simon Glass9b82eeb2015-03-25 12:21:59 -0600157 uclass_foreach_dev(bus, uc) {
158 ret = device_remove(bus);
159 if (ret && !err)
160 err = ret;
161 }
Simon Glassca4b2152016-03-13 08:22:33 -0600162#ifdef CONFIG_BLK
163 ret = blk_unbind_all(IF_TYPE_USB);
164 if (ret && !err)
165 err = ret;
166#endif
Simon Glass41477392015-03-25 12:22:38 -0600167#ifdef CONFIG_SANDBOX
168 struct udevice *dev;
169
170 /* Reset all enulation devices */
171 ret = uclass_get(UCLASS_USB_EMUL, &uc);
172 if (ret)
173 return ret;
174
175 uclass_foreach_dev(dev, uc)
176 usb_emul_reset(dev);
177#endif
Paul Kocialkowskid449dd32015-08-04 17:04:12 +0200178#ifdef CONFIG_USB_STORAGE
Simon Glass9b82eeb2015-03-25 12:21:59 -0600179 usb_stor_reset();
Paul Kocialkowskid449dd32015-08-04 17:04:12 +0200180#endif
Simon Glass9b82eeb2015-03-25 12:21:59 -0600181 usb_hub_reset();
Hans de Goedecebc5b72015-05-10 14:10:21 +0200182 uc_priv->companion_device_count = 0;
Simon Glass9b82eeb2015-03-25 12:21:59 -0600183 usb_started = 0;
184
185 return err;
186}
187
Hans de Goedef8762f92015-05-10 14:10:19 +0200188static void usb_scan_bus(struct udevice *bus, bool recurse)
Simon Glass9b82eeb2015-03-25 12:21:59 -0600189{
190 struct usb_bus_priv *priv;
191 struct udevice *dev;
192 int ret;
193
194 priv = dev_get_uclass_priv(bus);
195
196 assert(recurse); /* TODO: Support non-recusive */
197
Hans de Goedef8762f92015-05-10 14:10:19 +0200198 printf("scanning bus %d for devices... ", bus->seq);
199 debug("\n");
Simon Glass9b82eeb2015-03-25 12:21:59 -0600200 ret = usb_scan_device(bus, 0, USB_SPEED_FULL, &dev);
201 if (ret)
Hans de Goedef8762f92015-05-10 14:10:19 +0200202 printf("failed, error %d\n", ret);
203 else if (priv->next_addr == 0)
204 printf("No USB Device found\n");
205 else
206 printf("%d USB Device(s) found\n", priv->next_addr);
Simon Glass9b82eeb2015-03-25 12:21:59 -0600207}
208
Simon Glass35787d22015-11-08 23:48:00 -0700209static void remove_inactive_children(struct uclass *uc, struct udevice *bus)
210{
211 uclass_foreach_dev(bus, uc) {
212 struct udevice *dev, *next;
213
214 if (!device_active(bus))
215 continue;
216 device_foreach_child_safe(dev, next, bus) {
217 if (!device_active(dev))
218 device_unbind(dev);
219 }
220 }
221}
222
Simon Glass9b82eeb2015-03-25 12:21:59 -0600223int usb_init(void)
224{
225 int controllers_initialized = 0;
Hans de Goedecebc5b72015-05-10 14:10:21 +0200226 struct usb_uclass_priv *uc_priv;
Hans de Goede59a0dfc2015-05-10 14:10:20 +0200227 struct usb_bus_priv *priv;
Simon Glass9b82eeb2015-03-25 12:21:59 -0600228 struct udevice *bus;
229 struct uclass *uc;
230 int count = 0;
231 int ret;
232
233 asynch_allowed = 1;
234 usb_hub_reset();
235
236 ret = uclass_get(UCLASS_USB, &uc);
237 if (ret)
238 return ret;
239
Hans de Goedecebc5b72015-05-10 14:10:21 +0200240 uc_priv = uc->priv;
241
Simon Glass9b82eeb2015-03-25 12:21:59 -0600242 uclass_foreach_dev(bus, uc) {
243 /* init low_level USB */
Hans de Goede8679b3a2015-05-04 21:33:26 +0200244 printf("USB%d: ", count);
Simon Glass9b82eeb2015-03-25 12:21:59 -0600245 count++;
Simon Glass9b82eeb2015-03-25 12:21:59 -0600246 ret = device_probe(bus);
247 if (ret == -ENODEV) { /* No such device. */
248 puts("Port not available.\n");
249 controllers_initialized++;
250 continue;
251 }
252
253 if (ret) { /* Other error. */
254 printf("probe failed, error %d\n", ret);
255 continue;
256 }
Simon Glass9b82eeb2015-03-25 12:21:59 -0600257 controllers_initialized++;
Simon Glass9b82eeb2015-03-25 12:21:59 -0600258 usb_started = true;
259 }
260
Hans de Goede59a0dfc2015-05-10 14:10:20 +0200261 /*
262 * lowlevel init done, now scan the bus for devices i.e. search HUBs
263 * and configure them, first scan primary controllers.
264 */
265 uclass_foreach_dev(bus, uc) {
266 if (!device_active(bus))
267 continue;
268
269 priv = dev_get_uclass_priv(bus);
270 if (!priv->companion)
271 usb_scan_bus(bus, true);
272 }
273
274 /*
275 * Now that the primary controllers have been scanned and have handed
276 * over any devices they do not understand to their companions, scan
Hans de Goedecebc5b72015-05-10 14:10:21 +0200277 * the companions if necessary.
Hans de Goede59a0dfc2015-05-10 14:10:20 +0200278 */
Hans de Goedecebc5b72015-05-10 14:10:21 +0200279 if (uc_priv->companion_device_count) {
280 uclass_foreach_dev(bus, uc) {
281 if (!device_active(bus))
282 continue;
Hans de Goede59a0dfc2015-05-10 14:10:20 +0200283
Hans de Goedecebc5b72015-05-10 14:10:21 +0200284 priv = dev_get_uclass_priv(bus);
285 if (priv->companion)
286 usb_scan_bus(bus, true);
287 }
Hans de Goede59a0dfc2015-05-10 14:10:20 +0200288 }
289
Simon Glass9b82eeb2015-03-25 12:21:59 -0600290 debug("scan end\n");
Simon Glass35787d22015-11-08 23:48:00 -0700291
292 /* Remove any devices that were not found on this scan */
293 remove_inactive_children(uc, bus);
294
295 ret = uclass_get(UCLASS_USB_HUB, &uc);
296 if (ret)
297 return ret;
298 remove_inactive_children(uc, bus);
299
Simon Glass9b82eeb2015-03-25 12:21:59 -0600300 /* if we were not able to find at least one working bus, bail out */
301 if (!count)
302 printf("No controllers found\n");
303 else if (controllers_initialized == 0)
304 printf("USB error: all controllers failed lowlevel init\n");
305
306 return usb_started ? 0 : -1;
307}
308
Simon Glass5082c692015-11-08 23:47:59 -0700309/*
310 * TODO(sjg@chromium.org): Remove this legacy function. At present it is needed
311 * to support boards which use driver model for USB but not Ethernet, and want
312 * to use USB Ethernet.
313 *
314 * The #if clause is here to ensure that remains the only case.
315 */
316#if !defined(CONFIG_DM_ETH) && defined(CONFIG_USB_HOST_ETHER)
Simon Glass9b82eeb2015-03-25 12:21:59 -0600317static struct usb_device *find_child_devnum(struct udevice *parent, int devnum)
318{
319 struct usb_device *udev;
320 struct udevice *dev;
321
322 if (!device_active(parent))
323 return NULL;
Simon Glassde44acf2015-09-28 23:32:01 -0600324 udev = dev_get_parent_priv(parent);
Simon Glass9b82eeb2015-03-25 12:21:59 -0600325 if (udev->devnum == devnum)
326 return udev;
327
328 for (device_find_first_child(parent, &dev);
329 dev;
330 device_find_next_child(&dev)) {
331 udev = find_child_devnum(dev, devnum);
332 if (udev)
333 return udev;
334 }
335
336 return NULL;
337}
338
339struct usb_device *usb_get_dev_index(struct udevice *bus, int index)
340{
Hans de Goede9c203592015-06-17 21:33:53 +0200341 struct udevice *dev;
Simon Glass9b82eeb2015-03-25 12:21:59 -0600342 int devnum = index + 1; /* Addresses are allocated from 1 on USB */
343
Hans de Goede9c203592015-06-17 21:33:53 +0200344 device_find_first_child(bus, &dev);
345 if (!dev)
346 return NULL;
Simon Glass9b82eeb2015-03-25 12:21:59 -0600347
Hans de Goede9c203592015-06-17 21:33:53 +0200348 return find_child_devnum(dev, devnum);
Simon Glass9b82eeb2015-03-25 12:21:59 -0600349}
Simon Glass5082c692015-11-08 23:47:59 -0700350#endif
Simon Glass9b82eeb2015-03-25 12:21:59 -0600351
352int usb_post_bind(struct udevice *dev)
353{
354 /* Scan the bus for devices */
355 return dm_scan_fdt_node(dev, gd->fdt_blob, dev->of_offset, false);
356}
357
Simon Glass444b1e02015-03-25 12:22:32 -0600358int usb_setup_ehci_gadget(struct ehci_ctrl **ctlrp)
359{
360 struct usb_platdata *plat;
361 struct udevice *dev;
362 int ret;
363
364 /* Find the old device and remove it */
365 ret = uclass_find_device_by_seq(UCLASS_USB, 0, true, &dev);
366 if (ret)
367 return ret;
368 ret = device_remove(dev);
369 if (ret)
370 return ret;
371
372 plat = dev_get_platdata(dev);
373 plat->init_type = USB_INIT_DEVICE;
374 ret = device_probe(dev);
375 if (ret)
376 return ret;
377 *ctlrp = dev_get_priv(dev);
378
379 return 0;
380}
381
Simon Glassfc03a552015-03-25 12:22:30 -0600382/* returns 0 if no match, 1 if match */
383int usb_match_device(const struct usb_device_descriptor *desc,
384 const struct usb_device_id *id)
385{
386 if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
387 id->idVendor != le16_to_cpu(desc->idVendor))
388 return 0;
389
390 if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
391 id->idProduct != le16_to_cpu(desc->idProduct))
392 return 0;
393
394 /* No need to test id->bcdDevice_lo != 0, since 0 is never
395 greater than any unsigned number. */
396 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) &&
397 (id->bcdDevice_lo > le16_to_cpu(desc->bcdDevice)))
398 return 0;
399
400 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) &&
401 (id->bcdDevice_hi < le16_to_cpu(desc->bcdDevice)))
402 return 0;
403
404 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) &&
405 (id->bDeviceClass != desc->bDeviceClass))
406 return 0;
407
408 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) &&
409 (id->bDeviceSubClass != desc->bDeviceSubClass))
410 return 0;
411
412 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) &&
413 (id->bDeviceProtocol != desc->bDeviceProtocol))
414 return 0;
415
416 return 1;
417}
418
419/* returns 0 if no match, 1 if match */
420int usb_match_one_id_intf(const struct usb_device_descriptor *desc,
421 const struct usb_interface_descriptor *int_desc,
422 const struct usb_device_id *id)
423{
424 /* The interface class, subclass, protocol and number should never be
425 * checked for a match if the device class is Vendor Specific,
426 * unless the match record specifies the Vendor ID. */
427 if (desc->bDeviceClass == USB_CLASS_VENDOR_SPEC &&
428 !(id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
429 (id->match_flags & (USB_DEVICE_ID_MATCH_INT_CLASS |
430 USB_DEVICE_ID_MATCH_INT_SUBCLASS |
431 USB_DEVICE_ID_MATCH_INT_PROTOCOL |
432 USB_DEVICE_ID_MATCH_INT_NUMBER)))
433 return 0;
434
435 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) &&
436 (id->bInterfaceClass != int_desc->bInterfaceClass))
437 return 0;
438
439 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) &&
440 (id->bInterfaceSubClass != int_desc->bInterfaceSubClass))
441 return 0;
442
443 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) &&
444 (id->bInterfaceProtocol != int_desc->bInterfaceProtocol))
445 return 0;
446
447 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_NUMBER) &&
448 (id->bInterfaceNumber != int_desc->bInterfaceNumber))
449 return 0;
450
451 return 1;
452}
453
454/* returns 0 if no match, 1 if match */
455int usb_match_one_id(struct usb_device_descriptor *desc,
456 struct usb_interface_descriptor *int_desc,
457 const struct usb_device_id *id)
458{
459 if (!usb_match_device(desc, id))
460 return 0;
461
462 return usb_match_one_id_intf(desc, int_desc, id);
463}
464
465/**
466 * usb_find_and_bind_driver() - Find and bind the right USB driver
467 *
468 * This only looks at certain fields in the descriptor.
469 */
470static int usb_find_and_bind_driver(struct udevice *parent,
471 struct usb_device_descriptor *desc,
472 struct usb_interface_descriptor *iface,
473 int bus_seq, int devnum,
474 struct udevice **devp)
475{
476 struct usb_driver_entry *start, *entry;
477 int n_ents;
478 int ret;
479 char name[30], *str;
480
481 *devp = NULL;
482 debug("%s: Searching for driver\n", __func__);
483 start = ll_entry_start(struct usb_driver_entry, usb_driver_entry);
484 n_ents = ll_entry_count(struct usb_driver_entry, usb_driver_entry);
485 for (entry = start; entry != start + n_ents; entry++) {
486 const struct usb_device_id *id;
487 struct udevice *dev;
488 const struct driver *drv;
489 struct usb_dev_platdata *plat;
490
491 for (id = entry->match; id->match_flags; id++) {
492 if (!usb_match_one_id(desc, iface, id))
493 continue;
494
495 drv = entry->driver;
496 /*
497 * We could pass the descriptor to the driver as
498 * platdata (instead of NULL) and allow its bind()
499 * method to return -ENOENT if it doesn't support this
500 * device. That way we could continue the search to
501 * find another driver. For now this doesn't seem
502 * necesssary, so just bind the first match.
503 */
504 ret = device_bind(parent, drv, drv->name, NULL, -1,
505 &dev);
506 if (ret)
507 goto error;
508 debug("%s: Match found: %s\n", __func__, drv->name);
509 dev->driver_data = id->driver_info;
510 plat = dev_get_parent_platdata(dev);
511 plat->id = *id;
512 *devp = dev;
513 return 0;
514 }
515 }
516
Simon Glassc79173e2015-03-25 12:22:31 -0600517 /* Bind a generic driver so that the device can be used */
518 snprintf(name, sizeof(name), "generic_bus_%x_dev_%x", bus_seq, devnum);
519 str = strdup(name);
520 if (!str)
521 return -ENOMEM;
522 ret = device_bind_driver(parent, "usb_dev_generic_drv", str, devp);
523
Simon Glassfc03a552015-03-25 12:22:30 -0600524error:
525 debug("%s: No match found: %d\n", __func__, ret);
526 return ret;
527}
528
529/**
Simon Glassd43d7e92015-11-08 23:47:56 -0700530 * usb_find_child() - Find an existing device which matches our needs
531 *
532 *
Simon Glassfc03a552015-03-25 12:22:30 -0600533 */
Simon Glassd43d7e92015-11-08 23:47:56 -0700534static int usb_find_child(struct udevice *parent,
535 struct usb_device_descriptor *desc,
536 struct usb_interface_descriptor *iface,
537 struct udevice **devp)
Simon Glassfc03a552015-03-25 12:22:30 -0600538{
539 struct udevice *dev;
540
541 *devp = NULL;
542 for (device_find_first_child(parent, &dev);
543 dev;
544 device_find_next_child(&dev)) {
545 struct usb_dev_platdata *plat = dev_get_parent_platdata(dev);
546
547 /* If this device is already in use, skip it */
548 if (device_active(dev))
549 continue;
550 debug(" %s: name='%s', plat=%d, desc=%d\n", __func__,
551 dev->name, plat->id.bDeviceClass, desc->bDeviceClass);
552 if (usb_match_one_id(desc, iface, &plat->id)) {
553 *devp = dev;
554 return 0;
555 }
556 }
Simon Glassd43d7e92015-11-08 23:47:56 -0700557
Simon Glassfc03a552015-03-25 12:22:30 -0600558 return -ENOENT;
559}
560
Simon Glass9b82eeb2015-03-25 12:21:59 -0600561int usb_scan_device(struct udevice *parent, int port,
562 enum usb_device_speed speed, struct udevice **devp)
563{
564 struct udevice *dev;
565 bool created = false;
566 struct usb_dev_platdata *plat;
567 struct usb_bus_priv *priv;
568 struct usb_device *parent_udev;
569 int ret;
570 ALLOC_CACHE_ALIGN_BUFFER(struct usb_device, udev, 1);
571 struct usb_interface_descriptor *iface = &udev->config.if_desc[0].desc;
572
573 *devp = NULL;
574 memset(udev, '\0', sizeof(*udev));
Hans de Goede3b5ddd32015-05-05 11:54:31 +0200575 udev->controller_dev = usb_get_bus(parent);
Simon Glass9b82eeb2015-03-25 12:21:59 -0600576 priv = dev_get_uclass_priv(udev->controller_dev);
577
578 /*
579 * Somewhat nasty, this. We create a local device and use the normal
580 * USB stack to read its descriptor. Then we know what type of device
581 * to create for real.
582 *
583 * udev->dev is set to the parent, since we don't have a real device
584 * yet. The USB stack should not access udev.dev anyway, except perhaps
585 * to find the controller, and the controller will either be @parent,
586 * or some parent of @parent.
587 *
588 * Another option might be to create the device as a generic USB
589 * device, then morph it into the correct one when we know what it
590 * should be. This means that a generic USB device would morph into
591 * a network controller, or a USB flash stick, for example. However,
592 * we don't support such morphing and it isn't clear that it would
593 * be easy to do.
594 *
595 * Yet another option is to split out the USB stack parts of udev
596 * into something like a 'struct urb' (as Linux does) which can exist
597 * independently of any device. This feels cleaner, but calls for quite
598 * a big change to the USB stack.
599 *
600 * For now, the approach is to set up an empty udev, read its
601 * descriptor and assign it an address, then bind a real device and
602 * stash the resulting information into the device's parent
603 * platform data. Then when we probe it, usb_child_pre_probe() is called
604 * and it will pull the information out of the stash.
605 */
606 udev->dev = parent;
607 udev->speed = speed;
608 udev->devnum = priv->next_addr + 1;
609 udev->portnr = port;
610 debug("Calling usb_setup_device(), portnr=%d\n", udev->portnr);
611 parent_udev = device_get_uclass_id(parent) == UCLASS_USB_HUB ?
Simon Glassde44acf2015-09-28 23:32:01 -0600612 dev_get_parent_priv(parent) : NULL;
Hans de Goede778dc1c2015-06-17 21:33:46 +0200613 ret = usb_setup_device(udev, priv->desc_before_addr, parent_udev);
Simon Glass9b82eeb2015-03-25 12:21:59 -0600614 debug("read_descriptor for '%s': ret=%d\n", parent->name, ret);
615 if (ret)
616 return ret;
Simon Glassd43d7e92015-11-08 23:47:56 -0700617 ret = usb_find_child(parent, &udev->descriptor, iface, &dev);
618 debug("** usb_find_child returns %d\n", ret);
Simon Glassfc03a552015-03-25 12:22:30 -0600619 if (ret) {
620 if (ret != -ENOENT)
621 return ret;
622 ret = usb_find_and_bind_driver(parent, &udev->descriptor, iface,
623 udev->controller_dev->seq,
624 udev->devnum, &dev);
625 if (ret)
626 return ret;
627 created = true;
628 }
629 plat = dev_get_parent_platdata(dev);
630 debug("%s: Probing '%s', plat=%p\n", __func__, dev->name, plat);
631 plat->devnum = udev->devnum;
Hans de Goede8a0b4c22015-05-05 11:54:32 +0200632 plat->udev = udev;
Simon Glassfc03a552015-03-25 12:22:30 -0600633 priv->next_addr++;
634 ret = device_probe(dev);
635 if (ret) {
636 debug("%s: Device '%s' probe failed\n", __func__, dev->name);
637 priv->next_addr--;
638 if (created)
639 device_unbind(dev);
640 return ret;
641 }
642 *devp = dev;
Simon Glass9b82eeb2015-03-25 12:21:59 -0600643
Simon Glassfc03a552015-03-25 12:22:30 -0600644 return 0;
Simon Glass9b82eeb2015-03-25 12:21:59 -0600645}
646
Simon Glassfbb14942015-05-13 07:02:23 -0600647/*
648 * Detect if a USB device has been plugged or unplugged.
649 */
650int usb_detect_change(void)
651{
652 struct udevice *hub;
653 struct uclass *uc;
654 int change = 0;
655 int ret;
656
657 ret = uclass_get(UCLASS_USB_HUB, &uc);
658 if (ret)
659 return ret;
660
661 uclass_foreach_dev(hub, uc) {
662 struct usb_device *udev;
663 struct udevice *dev;
664
665 if (!device_active(hub))
666 continue;
667 for (device_find_first_child(hub, &dev);
668 dev;
669 device_find_next_child(&dev)) {
670 struct usb_port_status status;
671
672 if (!device_active(dev))
673 continue;
674
Simon Glassde44acf2015-09-28 23:32:01 -0600675 udev = dev_get_parent_priv(dev);
Simon Glassfbb14942015-05-13 07:02:23 -0600676 if (usb_get_port_status(udev, udev->portnr, &status)
677 < 0)
678 /* USB request failed */
679 continue;
680
681 if (le16_to_cpu(status.wPortChange) &
682 USB_PORT_STAT_C_CONNECTION)
683 change++;
684 }
685 }
686
687 return change;
688}
689
Simon Glass9b82eeb2015-03-25 12:21:59 -0600690int usb_child_post_bind(struct udevice *dev)
691{
692 struct usb_dev_platdata *plat = dev_get_parent_platdata(dev);
693 const void *blob = gd->fdt_blob;
694 int val;
695
696 if (dev->of_offset == -1)
697 return 0;
698
699 /* We only support matching a few things */
700 val = fdtdec_get_int(blob, dev->of_offset, "usb,device-class", -1);
701 if (val != -1) {
702 plat->id.match_flags |= USB_DEVICE_ID_MATCH_DEV_CLASS;
703 plat->id.bDeviceClass = val;
704 }
705 val = fdtdec_get_int(blob, dev->of_offset, "usb,interface-class", -1);
706 if (val != -1) {
707 plat->id.match_flags |= USB_DEVICE_ID_MATCH_INT_CLASS;
708 plat->id.bInterfaceClass = val;
709 }
710
711 return 0;
712}
713
Hans de Goede3b5ddd32015-05-05 11:54:31 +0200714struct udevice *usb_get_bus(struct udevice *dev)
Simon Glass9b82eeb2015-03-25 12:21:59 -0600715{
716 struct udevice *bus;
717
Simon Glass9b82eeb2015-03-25 12:21:59 -0600718 for (bus = dev; bus && device_get_uclass_id(bus) != UCLASS_USB; )
719 bus = bus->parent;
720 if (!bus) {
721 /* By design this cannot happen */
722 assert(bus);
723 debug("USB HUB '%s' does not have a controller\n", dev->name);
Simon Glass9b82eeb2015-03-25 12:21:59 -0600724 }
Simon Glass9b82eeb2015-03-25 12:21:59 -0600725
Hans de Goede3b5ddd32015-05-05 11:54:31 +0200726 return bus;
Simon Glass9b82eeb2015-03-25 12:21:59 -0600727}
728
729int usb_child_pre_probe(struct udevice *dev)
730{
Simon Glassde44acf2015-09-28 23:32:01 -0600731 struct usb_device *udev = dev_get_parent_priv(dev);
Simon Glass9b82eeb2015-03-25 12:21:59 -0600732 struct usb_dev_platdata *plat = dev_get_parent_platdata(dev);
733 int ret;
734
Hans de Goede8a0b4c22015-05-05 11:54:32 +0200735 if (plat->udev) {
736 /*
737 * Copy over all the values set in the on stack struct
738 * usb_device in usb_scan_device() to our final struct
739 * usb_device for this dev.
740 */
741 *udev = *(plat->udev);
742 /* And clear plat->udev as it will not be valid for long */
743 plat->udev = NULL;
744 udev->dev = dev;
745 } else {
746 /*
747 * This happens with devices which are explicitly bound
748 * instead of being discovered through usb_scan_device()
749 * such as sandbox emul devices.
750 */
751 udev->dev = dev;
752 udev->controller_dev = usb_get_bus(dev);
753 udev->devnum = plat->devnum;
Simon Glass9b82eeb2015-03-25 12:21:59 -0600754
Hans de Goede8a0b4c22015-05-05 11:54:32 +0200755 /*
756 * udev did not go through usb_scan_device(), so we need to
757 * select the config and read the config descriptors.
758 */
759 ret = usb_select_config(udev);
760 if (ret)
761 return ret;
762 }
Simon Glass9b82eeb2015-03-25 12:21:59 -0600763
764 return 0;
765}
766
767UCLASS_DRIVER(usb) = {
768 .id = UCLASS_USB,
769 .name = "usb",
770 .flags = DM_UC_FLAG_SEQ_ALIAS,
771 .post_bind = usb_post_bind,
Hans de Goedecebc5b72015-05-10 14:10:21 +0200772 .priv_auto_alloc_size = sizeof(struct usb_uclass_priv),
Simon Glass9b82eeb2015-03-25 12:21:59 -0600773 .per_child_auto_alloc_size = sizeof(struct usb_device),
774 .per_device_auto_alloc_size = sizeof(struct usb_bus_priv),
775 .child_post_bind = usb_child_post_bind,
776 .child_pre_probe = usb_child_pre_probe,
777 .per_child_platdata_auto_alloc_size = sizeof(struct usb_dev_platdata),
778};
Simon Glassc79173e2015-03-25 12:22:31 -0600779
780UCLASS_DRIVER(usb_dev_generic) = {
781 .id = UCLASS_USB_DEV_GENERIC,
782 .name = "usb_dev_generic",
783};
784
785U_BOOT_DRIVER(usb_dev_generic_drv) = {
786 .id = UCLASS_USB_DEV_GENERIC,
787 .name = "usb_dev_generic_drv",
788};