blob: 4aa92f8ee78e58e05a8e0e1c4d3474f761be3d67 [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>
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 Goedee2536372015-05-10 14:10:21 +020025struct usb_uclass_priv {
26 int companion_device_count;
27};
28
Simon Glassde312132015-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 Goedee2536372015-05-10 14:10:21 +020054 struct usb_uclass_priv *uc_priv = bus->uclass->priv;
55 int err;
Simon Glassde312132015-03-25 12:21:59 -060056
57 if (!ops->control)
58 return -ENOSYS;
59
Hans de Goedee2536372015-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 Glassde312132015-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 Goede8a5f0662015-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 Glassde312132015-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 Goedeb2f219b2015-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 Glassde312132015-03-25 12:21:59 -0600143int usb_stop(void)
144{
145 struct udevice *bus;
146 struct uclass *uc;
Hans de Goedee2536372015-05-10 14:10:21 +0200147 struct usb_uclass_priv *uc_priv;
Simon Glassde312132015-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 Goedee2536372015-05-10 14:10:21 +0200154
155 uc_priv = uc->priv;
156
Simon Glassde312132015-03-25 12:21:59 -0600157 uclass_foreach_dev(bus, uc) {
158 ret = device_remove(bus);
159 if (ret && !err)
160 err = ret;
161 }
162
Simon Glass095fdef2015-03-25 12:22:38 -0600163#ifdef CONFIG_SANDBOX
164 struct udevice *dev;
165
166 /* Reset all enulation devices */
167 ret = uclass_get(UCLASS_USB_EMUL, &uc);
168 if (ret)
169 return ret;
170
171 uclass_foreach_dev(dev, uc)
172 usb_emul_reset(dev);
173#endif
Paul Kocialkowski0fa59992015-08-04 17:04:12 +0200174#ifdef CONFIG_USB_STORAGE
Simon Glassde312132015-03-25 12:21:59 -0600175 usb_stor_reset();
Paul Kocialkowski0fa59992015-08-04 17:04:12 +0200176#endif
Simon Glassde312132015-03-25 12:21:59 -0600177 usb_hub_reset();
Hans de Goedee2536372015-05-10 14:10:21 +0200178 uc_priv->companion_device_count = 0;
Simon Glassde312132015-03-25 12:21:59 -0600179 usb_started = 0;
180
181 return err;
182}
183
Hans de Goedea24a0e92015-05-10 14:10:19 +0200184static void usb_scan_bus(struct udevice *bus, bool recurse)
Simon Glassde312132015-03-25 12:21:59 -0600185{
186 struct usb_bus_priv *priv;
187 struct udevice *dev;
188 int ret;
189
190 priv = dev_get_uclass_priv(bus);
191
192 assert(recurse); /* TODO: Support non-recusive */
193
Hans de Goedea24a0e92015-05-10 14:10:19 +0200194 printf("scanning bus %d for devices... ", bus->seq);
195 debug("\n");
Simon Glassde312132015-03-25 12:21:59 -0600196 ret = usb_scan_device(bus, 0, USB_SPEED_FULL, &dev);
197 if (ret)
Hans de Goedea24a0e92015-05-10 14:10:19 +0200198 printf("failed, error %d\n", ret);
199 else if (priv->next_addr == 0)
200 printf("No USB Device found\n");
201 else
202 printf("%d USB Device(s) found\n", priv->next_addr);
Simon Glassde312132015-03-25 12:21:59 -0600203}
204
205int usb_init(void)
206{
207 int controllers_initialized = 0;
Hans de Goedee2536372015-05-10 14:10:21 +0200208 struct usb_uclass_priv *uc_priv;
Hans de Goedeb6de4d12015-05-10 14:10:20 +0200209 struct usb_bus_priv *priv;
Simon Glassde312132015-03-25 12:21:59 -0600210 struct udevice *bus;
211 struct uclass *uc;
212 int count = 0;
213 int ret;
214
215 asynch_allowed = 1;
216 usb_hub_reset();
217
218 ret = uclass_get(UCLASS_USB, &uc);
219 if (ret)
220 return ret;
221
Hans de Goedee2536372015-05-10 14:10:21 +0200222 uc_priv = uc->priv;
223
Simon Glassde312132015-03-25 12:21:59 -0600224 uclass_foreach_dev(bus, uc) {
225 /* init low_level USB */
Hans de Goede134692a2015-05-04 21:33:26 +0200226 printf("USB%d: ", count);
Simon Glassde312132015-03-25 12:21:59 -0600227 count++;
Simon Glassde312132015-03-25 12:21:59 -0600228 ret = device_probe(bus);
229 if (ret == -ENODEV) { /* No such device. */
230 puts("Port not available.\n");
231 controllers_initialized++;
232 continue;
233 }
234
235 if (ret) { /* Other error. */
236 printf("probe failed, error %d\n", ret);
237 continue;
238 }
Simon Glassde312132015-03-25 12:21:59 -0600239 controllers_initialized++;
Simon Glassde312132015-03-25 12:21:59 -0600240 usb_started = true;
241 }
242
Hans de Goedeb6de4d12015-05-10 14:10:20 +0200243 /*
244 * lowlevel init done, now scan the bus for devices i.e. search HUBs
245 * and configure them, first scan primary controllers.
246 */
247 uclass_foreach_dev(bus, uc) {
248 if (!device_active(bus))
249 continue;
250
251 priv = dev_get_uclass_priv(bus);
252 if (!priv->companion)
253 usb_scan_bus(bus, true);
254 }
255
256 /*
257 * Now that the primary controllers have been scanned and have handed
258 * over any devices they do not understand to their companions, scan
Hans de Goedee2536372015-05-10 14:10:21 +0200259 * the companions if necessary.
Hans de Goedeb6de4d12015-05-10 14:10:20 +0200260 */
Hans de Goedee2536372015-05-10 14:10:21 +0200261 if (uc_priv->companion_device_count) {
262 uclass_foreach_dev(bus, uc) {
263 if (!device_active(bus))
264 continue;
Hans de Goedeb6de4d12015-05-10 14:10:20 +0200265
Hans de Goedee2536372015-05-10 14:10:21 +0200266 priv = dev_get_uclass_priv(bus);
267 if (priv->companion)
268 usb_scan_bus(bus, true);
269 }
Hans de Goedeb6de4d12015-05-10 14:10:20 +0200270 }
271
Simon Glassde312132015-03-25 12:21:59 -0600272 debug("scan end\n");
273 /* if we were not able to find at least one working bus, bail out */
274 if (!count)
275 printf("No controllers found\n");
276 else if (controllers_initialized == 0)
277 printf("USB error: all controllers failed lowlevel init\n");
278
279 return usb_started ? 0 : -1;
280}
281
Simon Glasse8ea5e82015-11-08 23:47:59 -0700282/*
283 * TODO(sjg@chromium.org): Remove this legacy function. At present it is needed
284 * to support boards which use driver model for USB but not Ethernet, and want
285 * to use USB Ethernet.
286 *
287 * The #if clause is here to ensure that remains the only case.
288 */
289#if !defined(CONFIG_DM_ETH) && defined(CONFIG_USB_HOST_ETHER)
Simon Glassde312132015-03-25 12:21:59 -0600290static struct usb_device *find_child_devnum(struct udevice *parent, int devnum)
291{
292 struct usb_device *udev;
293 struct udevice *dev;
294
295 if (!device_active(parent))
296 return NULL;
Simon Glassbcbe3d12015-09-28 23:32:01 -0600297 udev = dev_get_parent_priv(parent);
Simon Glassde312132015-03-25 12:21:59 -0600298 if (udev->devnum == devnum)
299 return udev;
300
301 for (device_find_first_child(parent, &dev);
302 dev;
303 device_find_next_child(&dev)) {
304 udev = find_child_devnum(dev, devnum);
305 if (udev)
306 return udev;
307 }
308
309 return NULL;
310}
311
312struct usb_device *usb_get_dev_index(struct udevice *bus, int index)
313{
Hans de Goedefd1bd212015-06-17 21:33:53 +0200314 struct udevice *dev;
Simon Glassde312132015-03-25 12:21:59 -0600315 int devnum = index + 1; /* Addresses are allocated from 1 on USB */
316
Hans de Goedefd1bd212015-06-17 21:33:53 +0200317 device_find_first_child(bus, &dev);
318 if (!dev)
319 return NULL;
Simon Glassde312132015-03-25 12:21:59 -0600320
Hans de Goedefd1bd212015-06-17 21:33:53 +0200321 return find_child_devnum(dev, devnum);
Simon Glassde312132015-03-25 12:21:59 -0600322}
Simon Glasse8ea5e82015-11-08 23:47:59 -0700323#endif
Simon Glassde312132015-03-25 12:21:59 -0600324
325int usb_post_bind(struct udevice *dev)
326{
327 /* Scan the bus for devices */
328 return dm_scan_fdt_node(dev, gd->fdt_blob, dev->of_offset, false);
329}
330
Simon Glassfbeceb22015-03-25 12:22:32 -0600331int usb_setup_ehci_gadget(struct ehci_ctrl **ctlrp)
332{
333 struct usb_platdata *plat;
334 struct udevice *dev;
335 int ret;
336
337 /* Find the old device and remove it */
338 ret = uclass_find_device_by_seq(UCLASS_USB, 0, true, &dev);
339 if (ret)
340 return ret;
341 ret = device_remove(dev);
342 if (ret)
343 return ret;
344
345 plat = dev_get_platdata(dev);
346 plat->init_type = USB_INIT_DEVICE;
347 ret = device_probe(dev);
348 if (ret)
349 return ret;
350 *ctlrp = dev_get_priv(dev);
351
352 return 0;
353}
354
Simon Glass0566e242015-03-25 12:22:30 -0600355/* returns 0 if no match, 1 if match */
356int usb_match_device(const struct usb_device_descriptor *desc,
357 const struct usb_device_id *id)
358{
359 if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
360 id->idVendor != le16_to_cpu(desc->idVendor))
361 return 0;
362
363 if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
364 id->idProduct != le16_to_cpu(desc->idProduct))
365 return 0;
366
367 /* No need to test id->bcdDevice_lo != 0, since 0 is never
368 greater than any unsigned number. */
369 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) &&
370 (id->bcdDevice_lo > le16_to_cpu(desc->bcdDevice)))
371 return 0;
372
373 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) &&
374 (id->bcdDevice_hi < le16_to_cpu(desc->bcdDevice)))
375 return 0;
376
377 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) &&
378 (id->bDeviceClass != desc->bDeviceClass))
379 return 0;
380
381 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) &&
382 (id->bDeviceSubClass != desc->bDeviceSubClass))
383 return 0;
384
385 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) &&
386 (id->bDeviceProtocol != desc->bDeviceProtocol))
387 return 0;
388
389 return 1;
390}
391
392/* returns 0 if no match, 1 if match */
393int usb_match_one_id_intf(const struct usb_device_descriptor *desc,
394 const struct usb_interface_descriptor *int_desc,
395 const struct usb_device_id *id)
396{
397 /* The interface class, subclass, protocol and number should never be
398 * checked for a match if the device class is Vendor Specific,
399 * unless the match record specifies the Vendor ID. */
400 if (desc->bDeviceClass == USB_CLASS_VENDOR_SPEC &&
401 !(id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
402 (id->match_flags & (USB_DEVICE_ID_MATCH_INT_CLASS |
403 USB_DEVICE_ID_MATCH_INT_SUBCLASS |
404 USB_DEVICE_ID_MATCH_INT_PROTOCOL |
405 USB_DEVICE_ID_MATCH_INT_NUMBER)))
406 return 0;
407
408 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) &&
409 (id->bInterfaceClass != int_desc->bInterfaceClass))
410 return 0;
411
412 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) &&
413 (id->bInterfaceSubClass != int_desc->bInterfaceSubClass))
414 return 0;
415
416 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) &&
417 (id->bInterfaceProtocol != int_desc->bInterfaceProtocol))
418 return 0;
419
420 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_NUMBER) &&
421 (id->bInterfaceNumber != int_desc->bInterfaceNumber))
422 return 0;
423
424 return 1;
425}
426
427/* returns 0 if no match, 1 if match */
428int usb_match_one_id(struct usb_device_descriptor *desc,
429 struct usb_interface_descriptor *int_desc,
430 const struct usb_device_id *id)
431{
432 if (!usb_match_device(desc, id))
433 return 0;
434
435 return usb_match_one_id_intf(desc, int_desc, id);
436}
437
438/**
439 * usb_find_and_bind_driver() - Find and bind the right USB driver
440 *
441 * This only looks at certain fields in the descriptor.
442 */
443static int usb_find_and_bind_driver(struct udevice *parent,
444 struct usb_device_descriptor *desc,
445 struct usb_interface_descriptor *iface,
446 int bus_seq, int devnum,
447 struct udevice **devp)
448{
449 struct usb_driver_entry *start, *entry;
450 int n_ents;
451 int ret;
452 char name[30], *str;
453
454 *devp = NULL;
455 debug("%s: Searching for driver\n", __func__);
456 start = ll_entry_start(struct usb_driver_entry, usb_driver_entry);
457 n_ents = ll_entry_count(struct usb_driver_entry, usb_driver_entry);
458 for (entry = start; entry != start + n_ents; entry++) {
459 const struct usb_device_id *id;
460 struct udevice *dev;
461 const struct driver *drv;
462 struct usb_dev_platdata *plat;
463
464 for (id = entry->match; id->match_flags; id++) {
465 if (!usb_match_one_id(desc, iface, id))
466 continue;
467
468 drv = entry->driver;
469 /*
470 * We could pass the descriptor to the driver as
471 * platdata (instead of NULL) and allow its bind()
472 * method to return -ENOENT if it doesn't support this
473 * device. That way we could continue the search to
474 * find another driver. For now this doesn't seem
475 * necesssary, so just bind the first match.
476 */
477 ret = device_bind(parent, drv, drv->name, NULL, -1,
478 &dev);
479 if (ret)
480 goto error;
481 debug("%s: Match found: %s\n", __func__, drv->name);
482 dev->driver_data = id->driver_info;
483 plat = dev_get_parent_platdata(dev);
484 plat->id = *id;
485 *devp = dev;
486 return 0;
487 }
488 }
489
Simon Glass449230f2015-03-25 12:22:31 -0600490 /* Bind a generic driver so that the device can be used */
491 snprintf(name, sizeof(name), "generic_bus_%x_dev_%x", bus_seq, devnum);
492 str = strdup(name);
493 if (!str)
494 return -ENOMEM;
495 ret = device_bind_driver(parent, "usb_dev_generic_drv", str, devp);
496
Simon Glass0566e242015-03-25 12:22:30 -0600497error:
498 debug("%s: No match found: %d\n", __func__, ret);
499 return ret;
500}
501
502/**
Simon Glass1b6a1df2015-11-08 23:47:56 -0700503 * usb_find_child() - Find an existing device which matches our needs
504 *
505 *
Simon Glass0566e242015-03-25 12:22:30 -0600506 */
Simon Glass1b6a1df2015-11-08 23:47:56 -0700507static int usb_find_child(struct udevice *parent,
508 struct usb_device_descriptor *desc,
509 struct usb_interface_descriptor *iface,
510 struct udevice **devp)
Simon Glass0566e242015-03-25 12:22:30 -0600511{
512 struct udevice *dev;
513
514 *devp = NULL;
515 for (device_find_first_child(parent, &dev);
516 dev;
517 device_find_next_child(&dev)) {
518 struct usb_dev_platdata *plat = dev_get_parent_platdata(dev);
519
520 /* If this device is already in use, skip it */
521 if (device_active(dev))
522 continue;
523 debug(" %s: name='%s', plat=%d, desc=%d\n", __func__,
524 dev->name, plat->id.bDeviceClass, desc->bDeviceClass);
525 if (usb_match_one_id(desc, iface, &plat->id)) {
526 *devp = dev;
527 return 0;
528 }
529 }
Simon Glass1b6a1df2015-11-08 23:47:56 -0700530
Simon Glass0566e242015-03-25 12:22:30 -0600531 return -ENOENT;
532}
533
Simon Glassde312132015-03-25 12:21:59 -0600534int usb_scan_device(struct udevice *parent, int port,
535 enum usb_device_speed speed, struct udevice **devp)
536{
537 struct udevice *dev;
538 bool created = false;
539 struct usb_dev_platdata *plat;
540 struct usb_bus_priv *priv;
541 struct usb_device *parent_udev;
542 int ret;
543 ALLOC_CACHE_ALIGN_BUFFER(struct usb_device, udev, 1);
544 struct usb_interface_descriptor *iface = &udev->config.if_desc[0].desc;
545
546 *devp = NULL;
547 memset(udev, '\0', sizeof(*udev));
Hans de Goedef78a5c02015-05-05 11:54:31 +0200548 udev->controller_dev = usb_get_bus(parent);
Simon Glassde312132015-03-25 12:21:59 -0600549 priv = dev_get_uclass_priv(udev->controller_dev);
550
551 /*
552 * Somewhat nasty, this. We create a local device and use the normal
553 * USB stack to read its descriptor. Then we know what type of device
554 * to create for real.
555 *
556 * udev->dev is set to the parent, since we don't have a real device
557 * yet. The USB stack should not access udev.dev anyway, except perhaps
558 * to find the controller, and the controller will either be @parent,
559 * or some parent of @parent.
560 *
561 * Another option might be to create the device as a generic USB
562 * device, then morph it into the correct one when we know what it
563 * should be. This means that a generic USB device would morph into
564 * a network controller, or a USB flash stick, for example. However,
565 * we don't support such morphing and it isn't clear that it would
566 * be easy to do.
567 *
568 * Yet another option is to split out the USB stack parts of udev
569 * into something like a 'struct urb' (as Linux does) which can exist
570 * independently of any device. This feels cleaner, but calls for quite
571 * a big change to the USB stack.
572 *
573 * For now, the approach is to set up an empty udev, read its
574 * descriptor and assign it an address, then bind a real device and
575 * stash the resulting information into the device's parent
576 * platform data. Then when we probe it, usb_child_pre_probe() is called
577 * and it will pull the information out of the stash.
578 */
579 udev->dev = parent;
580 udev->speed = speed;
581 udev->devnum = priv->next_addr + 1;
582 udev->portnr = port;
583 debug("Calling usb_setup_device(), portnr=%d\n", udev->portnr);
584 parent_udev = device_get_uclass_id(parent) == UCLASS_USB_HUB ?
Simon Glassbcbe3d12015-09-28 23:32:01 -0600585 dev_get_parent_priv(parent) : NULL;
Hans de Goede9eb72dd2015-06-17 21:33:46 +0200586 ret = usb_setup_device(udev, priv->desc_before_addr, parent_udev);
Simon Glassde312132015-03-25 12:21:59 -0600587 debug("read_descriptor for '%s': ret=%d\n", parent->name, ret);
588 if (ret)
589 return ret;
Simon Glass1b6a1df2015-11-08 23:47:56 -0700590 ret = usb_find_child(parent, &udev->descriptor, iface, &dev);
591 debug("** usb_find_child returns %d\n", ret);
Simon Glass0566e242015-03-25 12:22:30 -0600592 if (ret) {
593 if (ret != -ENOENT)
594 return ret;
595 ret = usb_find_and_bind_driver(parent, &udev->descriptor, iface,
596 udev->controller_dev->seq,
597 udev->devnum, &dev);
598 if (ret)
599 return ret;
600 created = true;
601 }
602 plat = dev_get_parent_platdata(dev);
603 debug("%s: Probing '%s', plat=%p\n", __func__, dev->name, plat);
604 plat->devnum = udev->devnum;
Hans de Goede7f1a0752015-05-05 11:54:32 +0200605 plat->udev = udev;
Simon Glass0566e242015-03-25 12:22:30 -0600606 priv->next_addr++;
607 ret = device_probe(dev);
608 if (ret) {
609 debug("%s: Device '%s' probe failed\n", __func__, dev->name);
610 priv->next_addr--;
611 if (created)
612 device_unbind(dev);
613 return ret;
614 }
615 *devp = dev;
Simon Glassde312132015-03-25 12:21:59 -0600616
Simon Glass0566e242015-03-25 12:22:30 -0600617 return 0;
Simon Glassde312132015-03-25 12:21:59 -0600618}
619
Simon Glass0c5dd9a2015-05-13 07:02:23 -0600620/*
621 * Detect if a USB device has been plugged or unplugged.
622 */
623int usb_detect_change(void)
624{
625 struct udevice *hub;
626 struct uclass *uc;
627 int change = 0;
628 int ret;
629
630 ret = uclass_get(UCLASS_USB_HUB, &uc);
631 if (ret)
632 return ret;
633
634 uclass_foreach_dev(hub, uc) {
635 struct usb_device *udev;
636 struct udevice *dev;
637
638 if (!device_active(hub))
639 continue;
640 for (device_find_first_child(hub, &dev);
641 dev;
642 device_find_next_child(&dev)) {
643 struct usb_port_status status;
644
645 if (!device_active(dev))
646 continue;
647
Simon Glassbcbe3d12015-09-28 23:32:01 -0600648 udev = dev_get_parent_priv(dev);
Simon Glass0c5dd9a2015-05-13 07:02:23 -0600649 if (usb_get_port_status(udev, udev->portnr, &status)
650 < 0)
651 /* USB request failed */
652 continue;
653
654 if (le16_to_cpu(status.wPortChange) &
655 USB_PORT_STAT_C_CONNECTION)
656 change++;
657 }
658 }
659
660 return change;
661}
662
Simon Glassde312132015-03-25 12:21:59 -0600663int usb_child_post_bind(struct udevice *dev)
664{
665 struct usb_dev_platdata *plat = dev_get_parent_platdata(dev);
666 const void *blob = gd->fdt_blob;
667 int val;
668
669 if (dev->of_offset == -1)
670 return 0;
671
672 /* We only support matching a few things */
673 val = fdtdec_get_int(blob, dev->of_offset, "usb,device-class", -1);
674 if (val != -1) {
675 plat->id.match_flags |= USB_DEVICE_ID_MATCH_DEV_CLASS;
676 plat->id.bDeviceClass = val;
677 }
678 val = fdtdec_get_int(blob, dev->of_offset, "usb,interface-class", -1);
679 if (val != -1) {
680 plat->id.match_flags |= USB_DEVICE_ID_MATCH_INT_CLASS;
681 plat->id.bInterfaceClass = val;
682 }
683
684 return 0;
685}
686
Hans de Goedef78a5c02015-05-05 11:54:31 +0200687struct udevice *usb_get_bus(struct udevice *dev)
Simon Glassde312132015-03-25 12:21:59 -0600688{
689 struct udevice *bus;
690
Simon Glassde312132015-03-25 12:21:59 -0600691 for (bus = dev; bus && device_get_uclass_id(bus) != UCLASS_USB; )
692 bus = bus->parent;
693 if (!bus) {
694 /* By design this cannot happen */
695 assert(bus);
696 debug("USB HUB '%s' does not have a controller\n", dev->name);
Simon Glassde312132015-03-25 12:21:59 -0600697 }
Simon Glassde312132015-03-25 12:21:59 -0600698
Hans de Goedef78a5c02015-05-05 11:54:31 +0200699 return bus;
Simon Glassde312132015-03-25 12:21:59 -0600700}
701
702int usb_child_pre_probe(struct udevice *dev)
703{
Simon Glassbcbe3d12015-09-28 23:32:01 -0600704 struct usb_device *udev = dev_get_parent_priv(dev);
Simon Glassde312132015-03-25 12:21:59 -0600705 struct usb_dev_platdata *plat = dev_get_parent_platdata(dev);
706 int ret;
707
Hans de Goede7f1a0752015-05-05 11:54:32 +0200708 if (plat->udev) {
709 /*
710 * Copy over all the values set in the on stack struct
711 * usb_device in usb_scan_device() to our final struct
712 * usb_device for this dev.
713 */
714 *udev = *(plat->udev);
715 /* And clear plat->udev as it will not be valid for long */
716 plat->udev = NULL;
717 udev->dev = dev;
718 } else {
719 /*
720 * This happens with devices which are explicitly bound
721 * instead of being discovered through usb_scan_device()
722 * such as sandbox emul devices.
723 */
724 udev->dev = dev;
725 udev->controller_dev = usb_get_bus(dev);
726 udev->devnum = plat->devnum;
Simon Glassde312132015-03-25 12:21:59 -0600727
Hans de Goede7f1a0752015-05-05 11:54:32 +0200728 /*
729 * udev did not go through usb_scan_device(), so we need to
730 * select the config and read the config descriptors.
731 */
732 ret = usb_select_config(udev);
733 if (ret)
734 return ret;
735 }
Simon Glassde312132015-03-25 12:21:59 -0600736
737 return 0;
738}
739
740UCLASS_DRIVER(usb) = {
741 .id = UCLASS_USB,
742 .name = "usb",
743 .flags = DM_UC_FLAG_SEQ_ALIAS,
744 .post_bind = usb_post_bind,
Hans de Goedee2536372015-05-10 14:10:21 +0200745 .priv_auto_alloc_size = sizeof(struct usb_uclass_priv),
Simon Glassde312132015-03-25 12:21:59 -0600746 .per_child_auto_alloc_size = sizeof(struct usb_device),
747 .per_device_auto_alloc_size = sizeof(struct usb_bus_priv),
748 .child_post_bind = usb_child_post_bind,
749 .child_pre_probe = usb_child_pre_probe,
750 .per_child_platdata_auto_alloc_size = sizeof(struct usb_dev_platdata),
751};
Simon Glass449230f2015-03-25 12:22:31 -0600752
753UCLASS_DRIVER(usb_dev_generic) = {
754 .id = UCLASS_USB_DEV_GENERIC,
755 .name = "usb_dev_generic",
756};
757
758U_BOOT_DRIVER(usb_dev_generic_drv) = {
759 .id = UCLASS_USB_DEV_GENERIC,
760 .name = "usb_dev_generic_drv",
761};