blob: b17a7d762e03350fd01bb1225d59a5ce4f4a5701 [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>
13#include <usb.h>
14#include <dm/device-internal.h>
15#include <dm/lists.h>
16#include <dm/root.h>
17#include <dm/uclass-internal.h>
18
19DECLARE_GLOBAL_DATA_PTR;
20
21extern bool usb_started; /* flag for the started/stopped USB status */
22static bool asynch_allowed;
23
Hans de Goedee2536372015-05-10 14:10:21 +020024struct usb_uclass_priv {
25 int companion_device_count;
26};
27
Simon Glassde312132015-03-25 12:21:59 -060028int usb_disable_asynch(int disable)
29{
30 int old_value = asynch_allowed;
31
32 asynch_allowed = !disable;
33 return old_value;
34}
35
36int submit_int_msg(struct usb_device *udev, unsigned long pipe, void *buffer,
37 int length, int interval)
38{
39 struct udevice *bus = udev->controller_dev;
40 struct dm_usb_ops *ops = usb_get_ops(bus);
41
42 if (!ops->interrupt)
43 return -ENOSYS;
44
45 return ops->interrupt(bus, udev, pipe, buffer, length, interval);
46}
47
48int submit_control_msg(struct usb_device *udev, unsigned long pipe,
49 void *buffer, int length, struct devrequest *setup)
50{
51 struct udevice *bus = udev->controller_dev;
52 struct dm_usb_ops *ops = usb_get_ops(bus);
Hans de Goedee2536372015-05-10 14:10:21 +020053 struct usb_uclass_priv *uc_priv = bus->uclass->priv;
54 int err;
Simon Glassde312132015-03-25 12:21:59 -060055
56 if (!ops->control)
57 return -ENOSYS;
58
Hans de Goedee2536372015-05-10 14:10:21 +020059 err = ops->control(bus, udev, pipe, buffer, length, setup);
60 if (setup->request == USB_REQ_SET_FEATURE &&
61 setup->requesttype == USB_RT_PORT &&
62 setup->value == cpu_to_le16(USB_PORT_FEAT_RESET) &&
63 err == -ENXIO) {
64 /* Device handed over to companion after port reset */
65 uc_priv->companion_device_count++;
66 }
67
68 return err;
Simon Glassde312132015-03-25 12:21:59 -060069}
70
71int submit_bulk_msg(struct usb_device *udev, unsigned long pipe, void *buffer,
72 int length)
73{
74 struct udevice *bus = udev->controller_dev;
75 struct dm_usb_ops *ops = usb_get_ops(bus);
76
77 if (!ops->bulk)
78 return -ENOSYS;
79
80 return ops->bulk(bus, udev, pipe, buffer, length);
81}
82
Hans de Goede8a5f0662015-05-10 14:10:18 +020083struct int_queue *create_int_queue(struct usb_device *udev,
84 unsigned long pipe, int queuesize, int elementsize,
85 void *buffer, int interval)
86{
87 struct udevice *bus = udev->controller_dev;
88 struct dm_usb_ops *ops = usb_get_ops(bus);
89
90 if (!ops->create_int_queue)
91 return NULL;
92
93 return ops->create_int_queue(bus, udev, pipe, queuesize, elementsize,
94 buffer, interval);
95}
96
97void *poll_int_queue(struct usb_device *udev, struct int_queue *queue)
98{
99 struct udevice *bus = udev->controller_dev;
100 struct dm_usb_ops *ops = usb_get_ops(bus);
101
102 if (!ops->poll_int_queue)
103 return NULL;
104
105 return ops->poll_int_queue(bus, udev, queue);
106}
107
108int destroy_int_queue(struct usb_device *udev, struct int_queue *queue)
109{
110 struct udevice *bus = udev->controller_dev;
111 struct dm_usb_ops *ops = usb_get_ops(bus);
112
113 if (!ops->destroy_int_queue)
114 return -ENOSYS;
115
116 return ops->destroy_int_queue(bus, udev, queue);
117}
118
Simon Glassde312132015-03-25 12:21:59 -0600119int usb_alloc_device(struct usb_device *udev)
120{
121 struct udevice *bus = udev->controller_dev;
122 struct dm_usb_ops *ops = usb_get_ops(bus);
123
124 /* This is only requird by some controllers - current XHCI */
125 if (!ops->alloc_device)
126 return 0;
127
128 return ops->alloc_device(bus, udev);
129}
130
Hans de Goedeb2f219b2015-06-17 21:33:52 +0200131int usb_reset_root_port(struct usb_device *udev)
132{
133 struct udevice *bus = udev->controller_dev;
134 struct dm_usb_ops *ops = usb_get_ops(bus);
135
136 if (!ops->reset_root_port)
137 return -ENOSYS;
138
139 return ops->reset_root_port(bus, udev);
140}
141
Simon Glassde312132015-03-25 12:21:59 -0600142int usb_stop(void)
143{
144 struct udevice *bus;
145 struct uclass *uc;
Hans de Goedee2536372015-05-10 14:10:21 +0200146 struct usb_uclass_priv *uc_priv;
Simon Glassde312132015-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 Goedee2536372015-05-10 14:10:21 +0200153
154 uc_priv = uc->priv;
155
Simon Glassde312132015-03-25 12:21:59 -0600156 uclass_foreach_dev(bus, uc) {
157 ret = device_remove(bus);
158 if (ret && !err)
159 err = ret;
Hans de Goede6cda3692015-07-01 20:53:00 +0200160 ret = device_unbind_children(bus);
161 if (ret && !err)
162 err = ret;
Simon Glassde312132015-03-25 12:21:59 -0600163 }
164
Simon Glass095fdef2015-03-25 12:22:38 -0600165#ifdef CONFIG_SANDBOX
166 struct udevice *dev;
167
168 /* Reset all enulation devices */
169 ret = uclass_get(UCLASS_USB_EMUL, &uc);
170 if (ret)
171 return ret;
172
173 uclass_foreach_dev(dev, uc)
174 usb_emul_reset(dev);
175#endif
Paul Kocialkowski0fa59992015-08-04 17:04:12 +0200176#ifdef CONFIG_USB_STORAGE
Simon Glassde312132015-03-25 12:21:59 -0600177 usb_stor_reset();
Paul Kocialkowski0fa59992015-08-04 17:04:12 +0200178#endif
Simon Glassde312132015-03-25 12:21:59 -0600179 usb_hub_reset();
Hans de Goedee2536372015-05-10 14:10:21 +0200180 uc_priv->companion_device_count = 0;
Simon Glassde312132015-03-25 12:21:59 -0600181 usb_started = 0;
182
183 return err;
184}
185
Hans de Goedea24a0e92015-05-10 14:10:19 +0200186static void usb_scan_bus(struct udevice *bus, bool recurse)
Simon Glassde312132015-03-25 12:21:59 -0600187{
188 struct usb_bus_priv *priv;
189 struct udevice *dev;
190 int ret;
191
192 priv = dev_get_uclass_priv(bus);
193
194 assert(recurse); /* TODO: Support non-recusive */
195
Hans de Goedea24a0e92015-05-10 14:10:19 +0200196 printf("scanning bus %d for devices... ", bus->seq);
197 debug("\n");
Simon Glassde312132015-03-25 12:21:59 -0600198 ret = usb_scan_device(bus, 0, USB_SPEED_FULL, &dev);
199 if (ret)
Hans de Goedea24a0e92015-05-10 14:10:19 +0200200 printf("failed, error %d\n", ret);
201 else if (priv->next_addr == 0)
202 printf("No USB Device found\n");
203 else
204 printf("%d USB Device(s) found\n", priv->next_addr);
Simon Glassde312132015-03-25 12:21:59 -0600205}
206
207int usb_init(void)
208{
209 int controllers_initialized = 0;
Hans de Goedee2536372015-05-10 14:10:21 +0200210 struct usb_uclass_priv *uc_priv;
Hans de Goedeb6de4d12015-05-10 14:10:20 +0200211 struct usb_bus_priv *priv;
Simon Glassde312132015-03-25 12:21:59 -0600212 struct udevice *bus;
213 struct uclass *uc;
214 int count = 0;
215 int ret;
216
217 asynch_allowed = 1;
218 usb_hub_reset();
219
220 ret = uclass_get(UCLASS_USB, &uc);
221 if (ret)
222 return ret;
223
Hans de Goedee2536372015-05-10 14:10:21 +0200224 uc_priv = uc->priv;
225
Simon Glassde312132015-03-25 12:21:59 -0600226 uclass_foreach_dev(bus, uc) {
227 /* init low_level USB */
Hans de Goede134692a2015-05-04 21:33:26 +0200228 printf("USB%d: ", count);
Simon Glassde312132015-03-25 12:21:59 -0600229 count++;
Simon Glassde312132015-03-25 12:21:59 -0600230 ret = device_probe(bus);
231 if (ret == -ENODEV) { /* No such device. */
232 puts("Port not available.\n");
233 controllers_initialized++;
234 continue;
235 }
236
237 if (ret) { /* Other error. */
238 printf("probe failed, error %d\n", ret);
239 continue;
240 }
Simon Glassde312132015-03-25 12:21:59 -0600241 controllers_initialized++;
Simon Glassde312132015-03-25 12:21:59 -0600242 usb_started = true;
243 }
244
Hans de Goedeb6de4d12015-05-10 14:10:20 +0200245 /*
246 * lowlevel init done, now scan the bus for devices i.e. search HUBs
247 * and configure them, first scan primary controllers.
248 */
249 uclass_foreach_dev(bus, uc) {
250 if (!device_active(bus))
251 continue;
252
253 priv = dev_get_uclass_priv(bus);
254 if (!priv->companion)
255 usb_scan_bus(bus, true);
256 }
257
258 /*
259 * Now that the primary controllers have been scanned and have handed
260 * over any devices they do not understand to their companions, scan
Hans de Goedee2536372015-05-10 14:10:21 +0200261 * the companions if necessary.
Hans de Goedeb6de4d12015-05-10 14:10:20 +0200262 */
Hans de Goedee2536372015-05-10 14:10:21 +0200263 if (uc_priv->companion_device_count) {
264 uclass_foreach_dev(bus, uc) {
265 if (!device_active(bus))
266 continue;
Hans de Goedeb6de4d12015-05-10 14:10:20 +0200267
Hans de Goedee2536372015-05-10 14:10:21 +0200268 priv = dev_get_uclass_priv(bus);
269 if (priv->companion)
270 usb_scan_bus(bus, true);
271 }
Hans de Goedeb6de4d12015-05-10 14:10:20 +0200272 }
273
Simon Glassde312132015-03-25 12:21:59 -0600274 debug("scan end\n");
275 /* if we were not able to find at least one working bus, bail out */
276 if (!count)
277 printf("No controllers found\n");
278 else if (controllers_initialized == 0)
279 printf("USB error: all controllers failed lowlevel init\n");
280
281 return usb_started ? 0 : -1;
282}
283
Simon Glassde312132015-03-25 12:21:59 -0600284static struct usb_device *find_child_devnum(struct udevice *parent, int devnum)
285{
286 struct usb_device *udev;
287 struct udevice *dev;
288
289 if (!device_active(parent))
290 return NULL;
291 udev = dev_get_parentdata(parent);
292 if (udev->devnum == devnum)
293 return udev;
294
295 for (device_find_first_child(parent, &dev);
296 dev;
297 device_find_next_child(&dev)) {
298 udev = find_child_devnum(dev, devnum);
299 if (udev)
300 return udev;
301 }
302
303 return NULL;
304}
305
306struct usb_device *usb_get_dev_index(struct udevice *bus, int index)
307{
Hans de Goedefd1bd212015-06-17 21:33:53 +0200308 struct udevice *dev;
Simon Glassde312132015-03-25 12:21:59 -0600309 int devnum = index + 1; /* Addresses are allocated from 1 on USB */
310
Hans de Goedefd1bd212015-06-17 21:33:53 +0200311 device_find_first_child(bus, &dev);
312 if (!dev)
313 return NULL;
Simon Glassde312132015-03-25 12:21:59 -0600314
Hans de Goedefd1bd212015-06-17 21:33:53 +0200315 return find_child_devnum(dev, devnum);
Simon Glassde312132015-03-25 12:21:59 -0600316}
317
318int usb_post_bind(struct udevice *dev)
319{
320 /* Scan the bus for devices */
321 return dm_scan_fdt_node(dev, gd->fdt_blob, dev->of_offset, false);
322}
323
Simon Glassfbeceb22015-03-25 12:22:32 -0600324int usb_setup_ehci_gadget(struct ehci_ctrl **ctlrp)
325{
326 struct usb_platdata *plat;
327 struct udevice *dev;
328 int ret;
329
330 /* Find the old device and remove it */
331 ret = uclass_find_device_by_seq(UCLASS_USB, 0, true, &dev);
332 if (ret)
333 return ret;
334 ret = device_remove(dev);
335 if (ret)
336 return ret;
337
338 plat = dev_get_platdata(dev);
339 plat->init_type = USB_INIT_DEVICE;
340 ret = device_probe(dev);
341 if (ret)
342 return ret;
343 *ctlrp = dev_get_priv(dev);
344
345 return 0;
346}
347
Simon Glass0566e242015-03-25 12:22:30 -0600348/* returns 0 if no match, 1 if match */
349int usb_match_device(const struct usb_device_descriptor *desc,
350 const struct usb_device_id *id)
351{
352 if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
353 id->idVendor != le16_to_cpu(desc->idVendor))
354 return 0;
355
356 if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
357 id->idProduct != le16_to_cpu(desc->idProduct))
358 return 0;
359
360 /* No need to test id->bcdDevice_lo != 0, since 0 is never
361 greater than any unsigned number. */
362 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) &&
363 (id->bcdDevice_lo > le16_to_cpu(desc->bcdDevice)))
364 return 0;
365
366 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) &&
367 (id->bcdDevice_hi < le16_to_cpu(desc->bcdDevice)))
368 return 0;
369
370 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) &&
371 (id->bDeviceClass != desc->bDeviceClass))
372 return 0;
373
374 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) &&
375 (id->bDeviceSubClass != desc->bDeviceSubClass))
376 return 0;
377
378 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) &&
379 (id->bDeviceProtocol != desc->bDeviceProtocol))
380 return 0;
381
382 return 1;
383}
384
385/* returns 0 if no match, 1 if match */
386int usb_match_one_id_intf(const struct usb_device_descriptor *desc,
387 const struct usb_interface_descriptor *int_desc,
388 const struct usb_device_id *id)
389{
390 /* The interface class, subclass, protocol and number should never be
391 * checked for a match if the device class is Vendor Specific,
392 * unless the match record specifies the Vendor ID. */
393 if (desc->bDeviceClass == USB_CLASS_VENDOR_SPEC &&
394 !(id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
395 (id->match_flags & (USB_DEVICE_ID_MATCH_INT_CLASS |
396 USB_DEVICE_ID_MATCH_INT_SUBCLASS |
397 USB_DEVICE_ID_MATCH_INT_PROTOCOL |
398 USB_DEVICE_ID_MATCH_INT_NUMBER)))
399 return 0;
400
401 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) &&
402 (id->bInterfaceClass != int_desc->bInterfaceClass))
403 return 0;
404
405 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) &&
406 (id->bInterfaceSubClass != int_desc->bInterfaceSubClass))
407 return 0;
408
409 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) &&
410 (id->bInterfaceProtocol != int_desc->bInterfaceProtocol))
411 return 0;
412
413 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_NUMBER) &&
414 (id->bInterfaceNumber != int_desc->bInterfaceNumber))
415 return 0;
416
417 return 1;
418}
419
420/* returns 0 if no match, 1 if match */
421int usb_match_one_id(struct usb_device_descriptor *desc,
422 struct usb_interface_descriptor *int_desc,
423 const struct usb_device_id *id)
424{
425 if (!usb_match_device(desc, id))
426 return 0;
427
428 return usb_match_one_id_intf(desc, int_desc, id);
429}
430
431/**
432 * usb_find_and_bind_driver() - Find and bind the right USB driver
433 *
434 * This only looks at certain fields in the descriptor.
435 */
436static int usb_find_and_bind_driver(struct udevice *parent,
437 struct usb_device_descriptor *desc,
438 struct usb_interface_descriptor *iface,
439 int bus_seq, int devnum,
440 struct udevice **devp)
441{
442 struct usb_driver_entry *start, *entry;
443 int n_ents;
444 int ret;
445 char name[30], *str;
446
447 *devp = NULL;
448 debug("%s: Searching for driver\n", __func__);
449 start = ll_entry_start(struct usb_driver_entry, usb_driver_entry);
450 n_ents = ll_entry_count(struct usb_driver_entry, usb_driver_entry);
451 for (entry = start; entry != start + n_ents; entry++) {
452 const struct usb_device_id *id;
453 struct udevice *dev;
454 const struct driver *drv;
455 struct usb_dev_platdata *plat;
456
457 for (id = entry->match; id->match_flags; id++) {
458 if (!usb_match_one_id(desc, iface, id))
459 continue;
460
461 drv = entry->driver;
462 /*
463 * We could pass the descriptor to the driver as
464 * platdata (instead of NULL) and allow its bind()
465 * method to return -ENOENT if it doesn't support this
466 * device. That way we could continue the search to
467 * find another driver. For now this doesn't seem
468 * necesssary, so just bind the first match.
469 */
470 ret = device_bind(parent, drv, drv->name, NULL, -1,
471 &dev);
472 if (ret)
473 goto error;
474 debug("%s: Match found: %s\n", __func__, drv->name);
475 dev->driver_data = id->driver_info;
476 plat = dev_get_parent_platdata(dev);
477 plat->id = *id;
478 *devp = dev;
479 return 0;
480 }
481 }
482
Simon Glass449230f2015-03-25 12:22:31 -0600483 /* Bind a generic driver so that the device can be used */
484 snprintf(name, sizeof(name), "generic_bus_%x_dev_%x", bus_seq, devnum);
485 str = strdup(name);
486 if (!str)
487 return -ENOMEM;
488 ret = device_bind_driver(parent, "usb_dev_generic_drv", str, devp);
489
Simon Glass0566e242015-03-25 12:22:30 -0600490error:
491 debug("%s: No match found: %d\n", __func__, ret);
492 return ret;
493}
494
495/**
Hans de Goede9b510df2015-07-01 20:53:01 +0200496 * usb_find_emul_child() - Find an existing device for emulated devices
Simon Glass0566e242015-03-25 12:22:30 -0600497 */
Hans de Goede9b510df2015-07-01 20:53:01 +0200498static int usb_find_emul_child(struct udevice *parent,
499 struct usb_device_descriptor *desc,
500 struct usb_interface_descriptor *iface,
501 struct udevice **devp)
Simon Glass0566e242015-03-25 12:22:30 -0600502{
Hans de Goede9b510df2015-07-01 20:53:01 +0200503#ifdef CONFIG_SANDBOX
Simon Glass0566e242015-03-25 12:22:30 -0600504 struct udevice *dev;
505
506 *devp = NULL;
507 for (device_find_first_child(parent, &dev);
508 dev;
509 device_find_next_child(&dev)) {
510 struct usb_dev_platdata *plat = dev_get_parent_platdata(dev);
511
512 /* If this device is already in use, skip it */
513 if (device_active(dev))
514 continue;
515 debug(" %s: name='%s', plat=%d, desc=%d\n", __func__,
516 dev->name, plat->id.bDeviceClass, desc->bDeviceClass);
517 if (usb_match_one_id(desc, iface, &plat->id)) {
518 *devp = dev;
519 return 0;
520 }
521 }
Hans de Goede9b510df2015-07-01 20:53:01 +0200522#endif
Simon Glass0566e242015-03-25 12:22:30 -0600523 return -ENOENT;
524}
525
Simon Glassde312132015-03-25 12:21:59 -0600526int usb_scan_device(struct udevice *parent, int port,
527 enum usb_device_speed speed, struct udevice **devp)
528{
529 struct udevice *dev;
530 bool created = false;
531 struct usb_dev_platdata *plat;
532 struct usb_bus_priv *priv;
533 struct usb_device *parent_udev;
534 int ret;
535 ALLOC_CACHE_ALIGN_BUFFER(struct usb_device, udev, 1);
536 struct usb_interface_descriptor *iface = &udev->config.if_desc[0].desc;
537
538 *devp = NULL;
539 memset(udev, '\0', sizeof(*udev));
Hans de Goedef78a5c02015-05-05 11:54:31 +0200540 udev->controller_dev = usb_get_bus(parent);
Simon Glassde312132015-03-25 12:21:59 -0600541 priv = dev_get_uclass_priv(udev->controller_dev);
542
543 /*
544 * Somewhat nasty, this. We create a local device and use the normal
545 * USB stack to read its descriptor. Then we know what type of device
546 * to create for real.
547 *
548 * udev->dev is set to the parent, since we don't have a real device
549 * yet. The USB stack should not access udev.dev anyway, except perhaps
550 * to find the controller, and the controller will either be @parent,
551 * or some parent of @parent.
552 *
553 * Another option might be to create the device as a generic USB
554 * device, then morph it into the correct one when we know what it
555 * should be. This means that a generic USB device would morph into
556 * a network controller, or a USB flash stick, for example. However,
557 * we don't support such morphing and it isn't clear that it would
558 * be easy to do.
559 *
560 * Yet another option is to split out the USB stack parts of udev
561 * into something like a 'struct urb' (as Linux does) which can exist
562 * independently of any device. This feels cleaner, but calls for quite
563 * a big change to the USB stack.
564 *
565 * For now, the approach is to set up an empty udev, read its
566 * descriptor and assign it an address, then bind a real device and
567 * stash the resulting information into the device's parent
568 * platform data. Then when we probe it, usb_child_pre_probe() is called
569 * and it will pull the information out of the stash.
570 */
571 udev->dev = parent;
572 udev->speed = speed;
573 udev->devnum = priv->next_addr + 1;
574 udev->portnr = port;
575 debug("Calling usb_setup_device(), portnr=%d\n", udev->portnr);
576 parent_udev = device_get_uclass_id(parent) == UCLASS_USB_HUB ?
577 dev_get_parentdata(parent) : NULL;
Hans de Goede9eb72dd2015-06-17 21:33:46 +0200578 ret = usb_setup_device(udev, priv->desc_before_addr, parent_udev);
Simon Glassde312132015-03-25 12:21:59 -0600579 debug("read_descriptor for '%s': ret=%d\n", parent->name, ret);
580 if (ret)
581 return ret;
Hans de Goede9b510df2015-07-01 20:53:01 +0200582 ret = usb_find_emul_child(parent, &udev->descriptor, iface, &dev);
583 debug("** usb_find_emul_child returns %d\n", ret);
Simon Glass0566e242015-03-25 12:22:30 -0600584 if (ret) {
585 if (ret != -ENOENT)
586 return ret;
587 ret = usb_find_and_bind_driver(parent, &udev->descriptor, iface,
588 udev->controller_dev->seq,
589 udev->devnum, &dev);
590 if (ret)
591 return ret;
592 created = true;
593 }
594 plat = dev_get_parent_platdata(dev);
595 debug("%s: Probing '%s', plat=%p\n", __func__, dev->name, plat);
596 plat->devnum = udev->devnum;
Hans de Goede7f1a0752015-05-05 11:54:32 +0200597 plat->udev = udev;
Simon Glass0566e242015-03-25 12:22:30 -0600598 priv->next_addr++;
599 ret = device_probe(dev);
600 if (ret) {
601 debug("%s: Device '%s' probe failed\n", __func__, dev->name);
602 priv->next_addr--;
603 if (created)
604 device_unbind(dev);
605 return ret;
606 }
607 *devp = dev;
Simon Glassde312132015-03-25 12:21:59 -0600608
Simon Glass0566e242015-03-25 12:22:30 -0600609 return 0;
Simon Glassde312132015-03-25 12:21:59 -0600610}
611
Simon Glass0c5dd9a2015-05-13 07:02:23 -0600612/*
613 * Detect if a USB device has been plugged or unplugged.
614 */
615int usb_detect_change(void)
616{
617 struct udevice *hub;
618 struct uclass *uc;
619 int change = 0;
620 int ret;
621
622 ret = uclass_get(UCLASS_USB_HUB, &uc);
623 if (ret)
624 return ret;
625
626 uclass_foreach_dev(hub, uc) {
627 struct usb_device *udev;
628 struct udevice *dev;
629
630 if (!device_active(hub))
631 continue;
632 for (device_find_first_child(hub, &dev);
633 dev;
634 device_find_next_child(&dev)) {
635 struct usb_port_status status;
636
637 if (!device_active(dev))
638 continue;
639
640 udev = dev_get_parentdata(dev);
641 if (usb_get_port_status(udev, udev->portnr, &status)
642 < 0)
643 /* USB request failed */
644 continue;
645
646 if (le16_to_cpu(status.wPortChange) &
647 USB_PORT_STAT_C_CONNECTION)
648 change++;
649 }
650 }
651
652 return change;
653}
654
Simon Glassde312132015-03-25 12:21:59 -0600655int usb_child_post_bind(struct udevice *dev)
656{
657 struct usb_dev_platdata *plat = dev_get_parent_platdata(dev);
658 const void *blob = gd->fdt_blob;
659 int val;
660
661 if (dev->of_offset == -1)
662 return 0;
663
664 /* We only support matching a few things */
665 val = fdtdec_get_int(blob, dev->of_offset, "usb,device-class", -1);
666 if (val != -1) {
667 plat->id.match_flags |= USB_DEVICE_ID_MATCH_DEV_CLASS;
668 plat->id.bDeviceClass = val;
669 }
670 val = fdtdec_get_int(blob, dev->of_offset, "usb,interface-class", -1);
671 if (val != -1) {
672 plat->id.match_flags |= USB_DEVICE_ID_MATCH_INT_CLASS;
673 plat->id.bInterfaceClass = val;
674 }
675
676 return 0;
677}
678
Hans de Goedef78a5c02015-05-05 11:54:31 +0200679struct udevice *usb_get_bus(struct udevice *dev)
Simon Glassde312132015-03-25 12:21:59 -0600680{
681 struct udevice *bus;
682
Simon Glassde312132015-03-25 12:21:59 -0600683 for (bus = dev; bus && device_get_uclass_id(bus) != UCLASS_USB; )
684 bus = bus->parent;
685 if (!bus) {
686 /* By design this cannot happen */
687 assert(bus);
688 debug("USB HUB '%s' does not have a controller\n", dev->name);
Simon Glassde312132015-03-25 12:21:59 -0600689 }
Simon Glassde312132015-03-25 12:21:59 -0600690
Hans de Goedef78a5c02015-05-05 11:54:31 +0200691 return bus;
Simon Glassde312132015-03-25 12:21:59 -0600692}
693
694int usb_child_pre_probe(struct udevice *dev)
695{
Simon Glassde312132015-03-25 12:21:59 -0600696 struct usb_device *udev = dev_get_parentdata(dev);
697 struct usb_dev_platdata *plat = dev_get_parent_platdata(dev);
698 int ret;
699
Hans de Goede7f1a0752015-05-05 11:54:32 +0200700 if (plat->udev) {
701 /*
702 * Copy over all the values set in the on stack struct
703 * usb_device in usb_scan_device() to our final struct
704 * usb_device for this dev.
705 */
706 *udev = *(plat->udev);
707 /* And clear plat->udev as it will not be valid for long */
708 plat->udev = NULL;
709 udev->dev = dev;
710 } else {
711 /*
712 * This happens with devices which are explicitly bound
713 * instead of being discovered through usb_scan_device()
714 * such as sandbox emul devices.
715 */
716 udev->dev = dev;
717 udev->controller_dev = usb_get_bus(dev);
718 udev->devnum = plat->devnum;
Simon Glassde312132015-03-25 12:21:59 -0600719
Hans de Goede7f1a0752015-05-05 11:54:32 +0200720 /*
721 * udev did not go through usb_scan_device(), so we need to
722 * select the config and read the config descriptors.
723 */
724 ret = usb_select_config(udev);
725 if (ret)
726 return ret;
727 }
Simon Glassde312132015-03-25 12:21:59 -0600728
729 return 0;
730}
731
732UCLASS_DRIVER(usb) = {
733 .id = UCLASS_USB,
734 .name = "usb",
735 .flags = DM_UC_FLAG_SEQ_ALIAS,
736 .post_bind = usb_post_bind,
Hans de Goedee2536372015-05-10 14:10:21 +0200737 .priv_auto_alloc_size = sizeof(struct usb_uclass_priv),
Simon Glassde312132015-03-25 12:21:59 -0600738 .per_child_auto_alloc_size = sizeof(struct usb_device),
739 .per_device_auto_alloc_size = sizeof(struct usb_bus_priv),
740 .child_post_bind = usb_child_post_bind,
741 .child_pre_probe = usb_child_pre_probe,
742 .per_child_platdata_auto_alloc_size = sizeof(struct usb_dev_platdata),
743};
Simon Glass449230f2015-03-25 12:22:31 -0600744
745UCLASS_DRIVER(usb_dev_generic) = {
746 .id = UCLASS_USB_DEV_GENERIC,
747 .name = "usb_dev_generic",
748};
749
750U_BOOT_DRIVER(usb_dev_generic_drv) = {
751 .id = UCLASS_USB_DEV_GENERIC,
752 .name = "usb_dev_generic_drv",
753};