blob: c66ebb6678dee1d1340a7b9010fc4d96ae0c642c [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;
Hans de Goede6cda3692015-07-01 20:53:00 +0200161 ret = device_unbind_children(bus);
162 if (ret && !err)
163 err = ret;
Simon Glassde312132015-03-25 12:21:59 -0600164 }
165
Simon Glass095fdef2015-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 Kocialkowski0fa59992015-08-04 17:04:12 +0200177#ifdef CONFIG_USB_STORAGE
Simon Glassde312132015-03-25 12:21:59 -0600178 usb_stor_reset();
Paul Kocialkowski0fa59992015-08-04 17:04:12 +0200179#endif
Simon Glassde312132015-03-25 12:21:59 -0600180 usb_hub_reset();
Hans de Goedee2536372015-05-10 14:10:21 +0200181 uc_priv->companion_device_count = 0;
Simon Glassde312132015-03-25 12:21:59 -0600182 usb_started = 0;
183
184 return err;
185}
186
Hans de Goedea24a0e92015-05-10 14:10:19 +0200187static void usb_scan_bus(struct udevice *bus, bool recurse)
Simon Glassde312132015-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 Goedea24a0e92015-05-10 14:10:19 +0200197 printf("scanning bus %d for devices... ", bus->seq);
198 debug("\n");
Simon Glassde312132015-03-25 12:21:59 -0600199 ret = usb_scan_device(bus, 0, USB_SPEED_FULL, &dev);
200 if (ret)
Hans de Goedea24a0e92015-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 Glassde312132015-03-25 12:21:59 -0600206}
207
208int usb_init(void)
209{
210 int controllers_initialized = 0;
Hans de Goedee2536372015-05-10 14:10:21 +0200211 struct usb_uclass_priv *uc_priv;
Hans de Goedeb6de4d12015-05-10 14:10:20 +0200212 struct usb_bus_priv *priv;
Simon Glassde312132015-03-25 12:21:59 -0600213 struct udevice *bus;
214 struct uclass *uc;
215 int count = 0;
216 int ret;
217
218 asynch_allowed = 1;
219 usb_hub_reset();
220
221 ret = uclass_get(UCLASS_USB, &uc);
222 if (ret)
223 return ret;
224
Hans de Goedee2536372015-05-10 14:10:21 +0200225 uc_priv = uc->priv;
226
Simon Glassde312132015-03-25 12:21:59 -0600227 uclass_foreach_dev(bus, uc) {
228 /* init low_level USB */
Hans de Goede134692a2015-05-04 21:33:26 +0200229 printf("USB%d: ", count);
Simon Glassde312132015-03-25 12:21:59 -0600230 count++;
Simon Glassde312132015-03-25 12:21:59 -0600231 ret = device_probe(bus);
232 if (ret == -ENODEV) { /* No such device. */
233 puts("Port not available.\n");
234 controllers_initialized++;
235 continue;
236 }
237
238 if (ret) { /* Other error. */
239 printf("probe failed, error %d\n", ret);
240 continue;
241 }
Simon Glassde312132015-03-25 12:21:59 -0600242 controllers_initialized++;
Simon Glassde312132015-03-25 12:21:59 -0600243 usb_started = true;
244 }
245
Hans de Goedeb6de4d12015-05-10 14:10:20 +0200246 /*
247 * lowlevel init done, now scan the bus for devices i.e. search HUBs
248 * and configure them, first scan primary controllers.
249 */
250 uclass_foreach_dev(bus, uc) {
251 if (!device_active(bus))
252 continue;
253
254 priv = dev_get_uclass_priv(bus);
255 if (!priv->companion)
256 usb_scan_bus(bus, true);
257 }
258
259 /*
260 * Now that the primary controllers have been scanned and have handed
261 * over any devices they do not understand to their companions, scan
Hans de Goedee2536372015-05-10 14:10:21 +0200262 * the companions if necessary.
Hans de Goedeb6de4d12015-05-10 14:10:20 +0200263 */
Hans de Goedee2536372015-05-10 14:10:21 +0200264 if (uc_priv->companion_device_count) {
265 uclass_foreach_dev(bus, uc) {
266 if (!device_active(bus))
267 continue;
Hans de Goedeb6de4d12015-05-10 14:10:20 +0200268
Hans de Goedee2536372015-05-10 14:10:21 +0200269 priv = dev_get_uclass_priv(bus);
270 if (priv->companion)
271 usb_scan_bus(bus, true);
272 }
Hans de Goedeb6de4d12015-05-10 14:10:20 +0200273 }
274
Simon Glassde312132015-03-25 12:21:59 -0600275 debug("scan end\n");
276 /* if we were not able to find at least one working bus, bail out */
277 if (!count)
278 printf("No controllers found\n");
279 else if (controllers_initialized == 0)
280 printf("USB error: all controllers failed lowlevel init\n");
281
282 return usb_started ? 0 : -1;
283}
284
Simon Glassde312132015-03-25 12:21:59 -0600285static struct usb_device *find_child_devnum(struct udevice *parent, int devnum)
286{
287 struct usb_device *udev;
288 struct udevice *dev;
289
290 if (!device_active(parent))
291 return NULL;
292 udev = dev_get_parentdata(parent);
293 if (udev->devnum == devnum)
294 return udev;
295
296 for (device_find_first_child(parent, &dev);
297 dev;
298 device_find_next_child(&dev)) {
299 udev = find_child_devnum(dev, devnum);
300 if (udev)
301 return udev;
302 }
303
304 return NULL;
305}
306
307struct usb_device *usb_get_dev_index(struct udevice *bus, int index)
308{
Hans de Goedefd1bd212015-06-17 21:33:53 +0200309 struct udevice *dev;
Simon Glassde312132015-03-25 12:21:59 -0600310 int devnum = index + 1; /* Addresses are allocated from 1 on USB */
311
Hans de Goedefd1bd212015-06-17 21:33:53 +0200312 device_find_first_child(bus, &dev);
313 if (!dev)
314 return NULL;
Simon Glassde312132015-03-25 12:21:59 -0600315
Hans de Goedefd1bd212015-06-17 21:33:53 +0200316 return find_child_devnum(dev, devnum);
Simon Glassde312132015-03-25 12:21:59 -0600317}
318
319int usb_post_bind(struct udevice *dev)
320{
321 /* Scan the bus for devices */
322 return dm_scan_fdt_node(dev, gd->fdt_blob, dev->of_offset, false);
323}
324
Simon Glassfbeceb22015-03-25 12:22:32 -0600325int usb_setup_ehci_gadget(struct ehci_ctrl **ctlrp)
326{
327 struct usb_platdata *plat;
328 struct udevice *dev;
329 int ret;
330
331 /* Find the old device and remove it */
332 ret = uclass_find_device_by_seq(UCLASS_USB, 0, true, &dev);
333 if (ret)
334 return ret;
335 ret = device_remove(dev);
336 if (ret)
337 return ret;
338
339 plat = dev_get_platdata(dev);
340 plat->init_type = USB_INIT_DEVICE;
341 ret = device_probe(dev);
342 if (ret)
343 return ret;
344 *ctlrp = dev_get_priv(dev);
345
346 return 0;
347}
348
Simon Glass0566e242015-03-25 12:22:30 -0600349/* returns 0 if no match, 1 if match */
350int usb_match_device(const struct usb_device_descriptor *desc,
351 const struct usb_device_id *id)
352{
353 if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
354 id->idVendor != le16_to_cpu(desc->idVendor))
355 return 0;
356
357 if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
358 id->idProduct != le16_to_cpu(desc->idProduct))
359 return 0;
360
361 /* No need to test id->bcdDevice_lo != 0, since 0 is never
362 greater than any unsigned number. */
363 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) &&
364 (id->bcdDevice_lo > le16_to_cpu(desc->bcdDevice)))
365 return 0;
366
367 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) &&
368 (id->bcdDevice_hi < le16_to_cpu(desc->bcdDevice)))
369 return 0;
370
371 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) &&
372 (id->bDeviceClass != desc->bDeviceClass))
373 return 0;
374
375 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) &&
376 (id->bDeviceSubClass != desc->bDeviceSubClass))
377 return 0;
378
379 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) &&
380 (id->bDeviceProtocol != desc->bDeviceProtocol))
381 return 0;
382
383 return 1;
384}
385
386/* returns 0 if no match, 1 if match */
387int usb_match_one_id_intf(const struct usb_device_descriptor *desc,
388 const struct usb_interface_descriptor *int_desc,
389 const struct usb_device_id *id)
390{
391 /* The interface class, subclass, protocol and number should never be
392 * checked for a match if the device class is Vendor Specific,
393 * unless the match record specifies the Vendor ID. */
394 if (desc->bDeviceClass == USB_CLASS_VENDOR_SPEC &&
395 !(id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
396 (id->match_flags & (USB_DEVICE_ID_MATCH_INT_CLASS |
397 USB_DEVICE_ID_MATCH_INT_SUBCLASS |
398 USB_DEVICE_ID_MATCH_INT_PROTOCOL |
399 USB_DEVICE_ID_MATCH_INT_NUMBER)))
400 return 0;
401
402 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) &&
403 (id->bInterfaceClass != int_desc->bInterfaceClass))
404 return 0;
405
406 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) &&
407 (id->bInterfaceSubClass != int_desc->bInterfaceSubClass))
408 return 0;
409
410 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) &&
411 (id->bInterfaceProtocol != int_desc->bInterfaceProtocol))
412 return 0;
413
414 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_NUMBER) &&
415 (id->bInterfaceNumber != int_desc->bInterfaceNumber))
416 return 0;
417
418 return 1;
419}
420
421/* returns 0 if no match, 1 if match */
422int usb_match_one_id(struct usb_device_descriptor *desc,
423 struct usb_interface_descriptor *int_desc,
424 const struct usb_device_id *id)
425{
426 if (!usb_match_device(desc, id))
427 return 0;
428
429 return usb_match_one_id_intf(desc, int_desc, id);
430}
431
432/**
433 * usb_find_and_bind_driver() - Find and bind the right USB driver
434 *
435 * This only looks at certain fields in the descriptor.
436 */
437static int usb_find_and_bind_driver(struct udevice *parent,
438 struct usb_device_descriptor *desc,
439 struct usb_interface_descriptor *iface,
440 int bus_seq, int devnum,
441 struct udevice **devp)
442{
443 struct usb_driver_entry *start, *entry;
444 int n_ents;
445 int ret;
446 char name[30], *str;
447
448 *devp = NULL;
449 debug("%s: Searching for driver\n", __func__);
450 start = ll_entry_start(struct usb_driver_entry, usb_driver_entry);
451 n_ents = ll_entry_count(struct usb_driver_entry, usb_driver_entry);
452 for (entry = start; entry != start + n_ents; entry++) {
453 const struct usb_device_id *id;
454 struct udevice *dev;
455 const struct driver *drv;
456 struct usb_dev_platdata *plat;
457
458 for (id = entry->match; id->match_flags; id++) {
459 if (!usb_match_one_id(desc, iface, id))
460 continue;
461
462 drv = entry->driver;
463 /*
464 * We could pass the descriptor to the driver as
465 * platdata (instead of NULL) and allow its bind()
466 * method to return -ENOENT if it doesn't support this
467 * device. That way we could continue the search to
468 * find another driver. For now this doesn't seem
469 * necesssary, so just bind the first match.
470 */
471 ret = device_bind(parent, drv, drv->name, NULL, -1,
472 &dev);
473 if (ret)
474 goto error;
475 debug("%s: Match found: %s\n", __func__, drv->name);
476 dev->driver_data = id->driver_info;
477 plat = dev_get_parent_platdata(dev);
478 plat->id = *id;
479 *devp = dev;
480 return 0;
481 }
482 }
483
Simon Glass449230f2015-03-25 12:22:31 -0600484 /* Bind a generic driver so that the device can be used */
485 snprintf(name, sizeof(name), "generic_bus_%x_dev_%x", bus_seq, devnum);
486 str = strdup(name);
487 if (!str)
488 return -ENOMEM;
489 ret = device_bind_driver(parent, "usb_dev_generic_drv", str, devp);
490
Simon Glass0566e242015-03-25 12:22:30 -0600491error:
492 debug("%s: No match found: %d\n", __func__, ret);
493 return ret;
494}
495
496/**
Hans de Goede9b510df2015-07-01 20:53:01 +0200497 * usb_find_emul_child() - Find an existing device for emulated devices
Simon Glass0566e242015-03-25 12:22:30 -0600498 */
Hans de Goede9b510df2015-07-01 20:53:01 +0200499static int usb_find_emul_child(struct udevice *parent,
500 struct usb_device_descriptor *desc,
501 struct usb_interface_descriptor *iface,
502 struct udevice **devp)
Simon Glass0566e242015-03-25 12:22:30 -0600503{
Hans de Goede9b510df2015-07-01 20:53:01 +0200504#ifdef CONFIG_SANDBOX
Simon Glass0566e242015-03-25 12:22:30 -0600505 struct udevice *dev;
506
507 *devp = NULL;
508 for (device_find_first_child(parent, &dev);
509 dev;
510 device_find_next_child(&dev)) {
511 struct usb_dev_platdata *plat = dev_get_parent_platdata(dev);
512
513 /* If this device is already in use, skip it */
514 if (device_active(dev))
515 continue;
516 debug(" %s: name='%s', plat=%d, desc=%d\n", __func__,
517 dev->name, plat->id.bDeviceClass, desc->bDeviceClass);
518 if (usb_match_one_id(desc, iface, &plat->id)) {
519 *devp = dev;
520 return 0;
521 }
522 }
Hans de Goede9b510df2015-07-01 20:53:01 +0200523#endif
Simon Glass0566e242015-03-25 12:22:30 -0600524 return -ENOENT;
525}
526
Simon Glassde312132015-03-25 12:21:59 -0600527int usb_scan_device(struct udevice *parent, int port,
528 enum usb_device_speed speed, struct udevice **devp)
529{
530 struct udevice *dev;
531 bool created = false;
532 struct usb_dev_platdata *plat;
533 struct usb_bus_priv *priv;
534 struct usb_device *parent_udev;
535 int ret;
536 ALLOC_CACHE_ALIGN_BUFFER(struct usb_device, udev, 1);
537 struct usb_interface_descriptor *iface = &udev->config.if_desc[0].desc;
538
539 *devp = NULL;
540 memset(udev, '\0', sizeof(*udev));
Hans de Goedef78a5c02015-05-05 11:54:31 +0200541 udev->controller_dev = usb_get_bus(parent);
Simon Glassde312132015-03-25 12:21:59 -0600542 priv = dev_get_uclass_priv(udev->controller_dev);
543
544 /*
545 * Somewhat nasty, this. We create a local device and use the normal
546 * USB stack to read its descriptor. Then we know what type of device
547 * to create for real.
548 *
549 * udev->dev is set to the parent, since we don't have a real device
550 * yet. The USB stack should not access udev.dev anyway, except perhaps
551 * to find the controller, and the controller will either be @parent,
552 * or some parent of @parent.
553 *
554 * Another option might be to create the device as a generic USB
555 * device, then morph it into the correct one when we know what it
556 * should be. This means that a generic USB device would morph into
557 * a network controller, or a USB flash stick, for example. However,
558 * we don't support such morphing and it isn't clear that it would
559 * be easy to do.
560 *
561 * Yet another option is to split out the USB stack parts of udev
562 * into something like a 'struct urb' (as Linux does) which can exist
563 * independently of any device. This feels cleaner, but calls for quite
564 * a big change to the USB stack.
565 *
566 * For now, the approach is to set up an empty udev, read its
567 * descriptor and assign it an address, then bind a real device and
568 * stash the resulting information into the device's parent
569 * platform data. Then when we probe it, usb_child_pre_probe() is called
570 * and it will pull the information out of the stash.
571 */
572 udev->dev = parent;
573 udev->speed = speed;
574 udev->devnum = priv->next_addr + 1;
575 udev->portnr = port;
576 debug("Calling usb_setup_device(), portnr=%d\n", udev->portnr);
577 parent_udev = device_get_uclass_id(parent) == UCLASS_USB_HUB ?
578 dev_get_parentdata(parent) : NULL;
Hans de Goede9eb72dd2015-06-17 21:33:46 +0200579 ret = usb_setup_device(udev, priv->desc_before_addr, parent_udev);
Simon Glassde312132015-03-25 12:21:59 -0600580 debug("read_descriptor for '%s': ret=%d\n", parent->name, ret);
581 if (ret)
582 return ret;
Hans de Goede9b510df2015-07-01 20:53:01 +0200583 ret = usb_find_emul_child(parent, &udev->descriptor, iface, &dev);
584 debug("** usb_find_emul_child returns %d\n", ret);
Simon Glass0566e242015-03-25 12:22:30 -0600585 if (ret) {
586 if (ret != -ENOENT)
587 return ret;
588 ret = usb_find_and_bind_driver(parent, &udev->descriptor, iface,
589 udev->controller_dev->seq,
590 udev->devnum, &dev);
591 if (ret)
592 return ret;
593 created = true;
594 }
595 plat = dev_get_parent_platdata(dev);
596 debug("%s: Probing '%s', plat=%p\n", __func__, dev->name, plat);
597 plat->devnum = udev->devnum;
Hans de Goede7f1a0752015-05-05 11:54:32 +0200598 plat->udev = udev;
Simon Glass0566e242015-03-25 12:22:30 -0600599 priv->next_addr++;
600 ret = device_probe(dev);
601 if (ret) {
602 debug("%s: Device '%s' probe failed\n", __func__, dev->name);
603 priv->next_addr--;
604 if (created)
605 device_unbind(dev);
606 return ret;
607 }
608 *devp = dev;
Simon Glassde312132015-03-25 12:21:59 -0600609
Simon Glass0566e242015-03-25 12:22:30 -0600610 return 0;
Simon Glassde312132015-03-25 12:21:59 -0600611}
612
Simon Glass0c5dd9a2015-05-13 07:02:23 -0600613/*
614 * Detect if a USB device has been plugged or unplugged.
615 */
616int usb_detect_change(void)
617{
618 struct udevice *hub;
619 struct uclass *uc;
620 int change = 0;
621 int ret;
622
623 ret = uclass_get(UCLASS_USB_HUB, &uc);
624 if (ret)
625 return ret;
626
627 uclass_foreach_dev(hub, uc) {
628 struct usb_device *udev;
629 struct udevice *dev;
630
631 if (!device_active(hub))
632 continue;
633 for (device_find_first_child(hub, &dev);
634 dev;
635 device_find_next_child(&dev)) {
636 struct usb_port_status status;
637
638 if (!device_active(dev))
639 continue;
640
641 udev = dev_get_parentdata(dev);
642 if (usb_get_port_status(udev, udev->portnr, &status)
643 < 0)
644 /* USB request failed */
645 continue;
646
647 if (le16_to_cpu(status.wPortChange) &
648 USB_PORT_STAT_C_CONNECTION)
649 change++;
650 }
651 }
652
653 return change;
654}
655
Simon Glassde312132015-03-25 12:21:59 -0600656int usb_child_post_bind(struct udevice *dev)
657{
658 struct usb_dev_platdata *plat = dev_get_parent_platdata(dev);
659 const void *blob = gd->fdt_blob;
660 int val;
661
662 if (dev->of_offset == -1)
663 return 0;
664
665 /* We only support matching a few things */
666 val = fdtdec_get_int(blob, dev->of_offset, "usb,device-class", -1);
667 if (val != -1) {
668 plat->id.match_flags |= USB_DEVICE_ID_MATCH_DEV_CLASS;
669 plat->id.bDeviceClass = val;
670 }
671 val = fdtdec_get_int(blob, dev->of_offset, "usb,interface-class", -1);
672 if (val != -1) {
673 plat->id.match_flags |= USB_DEVICE_ID_MATCH_INT_CLASS;
674 plat->id.bInterfaceClass = val;
675 }
676
677 return 0;
678}
679
Hans de Goedef78a5c02015-05-05 11:54:31 +0200680struct udevice *usb_get_bus(struct udevice *dev)
Simon Glassde312132015-03-25 12:21:59 -0600681{
682 struct udevice *bus;
683
Simon Glassde312132015-03-25 12:21:59 -0600684 for (bus = dev; bus && device_get_uclass_id(bus) != UCLASS_USB; )
685 bus = bus->parent;
686 if (!bus) {
687 /* By design this cannot happen */
688 assert(bus);
689 debug("USB HUB '%s' does not have a controller\n", dev->name);
Simon Glassde312132015-03-25 12:21:59 -0600690 }
Simon Glassde312132015-03-25 12:21:59 -0600691
Hans de Goedef78a5c02015-05-05 11:54:31 +0200692 return bus;
Simon Glassde312132015-03-25 12:21:59 -0600693}
694
695int usb_child_pre_probe(struct udevice *dev)
696{
Simon Glassde312132015-03-25 12:21:59 -0600697 struct usb_device *udev = dev_get_parentdata(dev);
698 struct usb_dev_platdata *plat = dev_get_parent_platdata(dev);
699 int ret;
700
Hans de Goede7f1a0752015-05-05 11:54:32 +0200701 if (plat->udev) {
702 /*
703 * Copy over all the values set in the on stack struct
704 * usb_device in usb_scan_device() to our final struct
705 * usb_device for this dev.
706 */
707 *udev = *(plat->udev);
708 /* And clear plat->udev as it will not be valid for long */
709 plat->udev = NULL;
710 udev->dev = dev;
711 } else {
712 /*
713 * This happens with devices which are explicitly bound
714 * instead of being discovered through usb_scan_device()
715 * such as sandbox emul devices.
716 */
717 udev->dev = dev;
718 udev->controller_dev = usb_get_bus(dev);
719 udev->devnum = plat->devnum;
Simon Glassde312132015-03-25 12:21:59 -0600720
Hans de Goede7f1a0752015-05-05 11:54:32 +0200721 /*
722 * udev did not go through usb_scan_device(), so we need to
723 * select the config and read the config descriptors.
724 */
725 ret = usb_select_config(udev);
726 if (ret)
727 return ret;
728 }
Simon Glassde312132015-03-25 12:21:59 -0600729
730 return 0;
731}
732
733UCLASS_DRIVER(usb) = {
734 .id = UCLASS_USB,
735 .name = "usb",
736 .flags = DM_UC_FLAG_SEQ_ALIAS,
737 .post_bind = usb_post_bind,
Hans de Goedee2536372015-05-10 14:10:21 +0200738 .priv_auto_alloc_size = sizeof(struct usb_uclass_priv),
Simon Glassde312132015-03-25 12:21:59 -0600739 .per_child_auto_alloc_size = sizeof(struct usb_device),
740 .per_device_auto_alloc_size = sizeof(struct usb_bus_priv),
741 .child_post_bind = usb_child_post_bind,
742 .child_pre_probe = usb_child_pre_probe,
743 .per_child_platdata_auto_alloc_size = sizeof(struct usb_dev_platdata),
744};
Simon Glass449230f2015-03-25 12:22:31 -0600745
746UCLASS_DRIVER(usb_dev_generic) = {
747 .id = UCLASS_USB_DEV_GENERIC,
748 .name = "usb_dev_generic",
749};
750
751U_BOOT_DRIVER(usb_dev_generic_drv) = {
752 .id = UCLASS_USB_DEV_GENERIC,
753 .name = "usb_dev_generic_drv",
754};