blob: fd11cc6f6cfb287a2b8e9c3d4b08d805b231bd63 [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
131int usb_stop(void)
132{
133 struct udevice *bus;
134 struct uclass *uc;
Hans de Goedee2536372015-05-10 14:10:21 +0200135 struct usb_uclass_priv *uc_priv;
Simon Glassde312132015-03-25 12:21:59 -0600136 int err = 0, ret;
137
138 /* De-activate any devices that have been activated */
139 ret = uclass_get(UCLASS_USB, &uc);
140 if (ret)
141 return ret;
Hans de Goedee2536372015-05-10 14:10:21 +0200142
143 uc_priv = uc->priv;
144
Simon Glassde312132015-03-25 12:21:59 -0600145 uclass_foreach_dev(bus, uc) {
146 ret = device_remove(bus);
147 if (ret && !err)
148 err = ret;
Hans de Goede6cda3692015-07-01 20:53:00 +0200149 ret = device_unbind_children(bus);
150 if (ret && !err)
151 err = ret;
Simon Glassde312132015-03-25 12:21:59 -0600152 }
153
Simon Glass095fdef2015-03-25 12:22:38 -0600154#ifdef CONFIG_SANDBOX
155 struct udevice *dev;
156
157 /* Reset all enulation devices */
158 ret = uclass_get(UCLASS_USB_EMUL, &uc);
159 if (ret)
160 return ret;
161
162 uclass_foreach_dev(dev, uc)
163 usb_emul_reset(dev);
164#endif
Simon Glassde312132015-03-25 12:21:59 -0600165 usb_stor_reset();
166 usb_hub_reset();
Hans de Goedee2536372015-05-10 14:10:21 +0200167 uc_priv->companion_device_count = 0;
Simon Glassde312132015-03-25 12:21:59 -0600168 usb_started = 0;
169
170 return err;
171}
172
Hans de Goedea24a0e92015-05-10 14:10:19 +0200173static void usb_scan_bus(struct udevice *bus, bool recurse)
Simon Glassde312132015-03-25 12:21:59 -0600174{
175 struct usb_bus_priv *priv;
176 struct udevice *dev;
177 int ret;
178
179 priv = dev_get_uclass_priv(bus);
180
181 assert(recurse); /* TODO: Support non-recusive */
182
Hans de Goedea24a0e92015-05-10 14:10:19 +0200183 printf("scanning bus %d for devices... ", bus->seq);
184 debug("\n");
Simon Glassde312132015-03-25 12:21:59 -0600185 ret = usb_scan_device(bus, 0, USB_SPEED_FULL, &dev);
186 if (ret)
Hans de Goedea24a0e92015-05-10 14:10:19 +0200187 printf("failed, error %d\n", ret);
188 else if (priv->next_addr == 0)
189 printf("No USB Device found\n");
190 else
191 printf("%d USB Device(s) found\n", priv->next_addr);
Simon Glassde312132015-03-25 12:21:59 -0600192}
193
194int usb_init(void)
195{
196 int controllers_initialized = 0;
Hans de Goedee2536372015-05-10 14:10:21 +0200197 struct usb_uclass_priv *uc_priv;
Hans de Goedeb6de4d12015-05-10 14:10:20 +0200198 struct usb_bus_priv *priv;
Simon Glassde312132015-03-25 12:21:59 -0600199 struct udevice *bus;
200 struct uclass *uc;
201 int count = 0;
202 int ret;
203
204 asynch_allowed = 1;
205 usb_hub_reset();
206
207 ret = uclass_get(UCLASS_USB, &uc);
208 if (ret)
209 return ret;
210
Hans de Goedee2536372015-05-10 14:10:21 +0200211 uc_priv = uc->priv;
212
Simon Glassde312132015-03-25 12:21:59 -0600213 uclass_foreach_dev(bus, uc) {
214 /* init low_level USB */
Hans de Goede134692a2015-05-04 21:33:26 +0200215 printf("USB%d: ", count);
Simon Glassde312132015-03-25 12:21:59 -0600216 count++;
Simon Glassde312132015-03-25 12:21:59 -0600217 ret = device_probe(bus);
218 if (ret == -ENODEV) { /* No such device. */
219 puts("Port not available.\n");
220 controllers_initialized++;
221 continue;
222 }
223
224 if (ret) { /* Other error. */
225 printf("probe failed, error %d\n", ret);
226 continue;
227 }
Simon Glassde312132015-03-25 12:21:59 -0600228 controllers_initialized++;
Simon Glassde312132015-03-25 12:21:59 -0600229 usb_started = true;
230 }
231
Hans de Goedeb6de4d12015-05-10 14:10:20 +0200232 /*
233 * lowlevel init done, now scan the bus for devices i.e. search HUBs
234 * and configure them, first scan primary controllers.
235 */
236 uclass_foreach_dev(bus, uc) {
237 if (!device_active(bus))
238 continue;
239
240 priv = dev_get_uclass_priv(bus);
241 if (!priv->companion)
242 usb_scan_bus(bus, true);
243 }
244
245 /*
246 * Now that the primary controllers have been scanned and have handed
247 * over any devices they do not understand to their companions, scan
Hans de Goedee2536372015-05-10 14:10:21 +0200248 * the companions if necessary.
Hans de Goedeb6de4d12015-05-10 14:10:20 +0200249 */
Hans de Goedee2536372015-05-10 14:10:21 +0200250 if (uc_priv->companion_device_count) {
251 uclass_foreach_dev(bus, uc) {
252 if (!device_active(bus))
253 continue;
Hans de Goedeb6de4d12015-05-10 14:10:20 +0200254
Hans de Goedee2536372015-05-10 14:10:21 +0200255 priv = dev_get_uclass_priv(bus);
256 if (priv->companion)
257 usb_scan_bus(bus, true);
258 }
Hans de Goedeb6de4d12015-05-10 14:10:20 +0200259 }
260
Simon Glassde312132015-03-25 12:21:59 -0600261 debug("scan end\n");
262 /* if we were not able to find at least one working bus, bail out */
263 if (!count)
264 printf("No controllers found\n");
265 else if (controllers_initialized == 0)
266 printf("USB error: all controllers failed lowlevel init\n");
267
268 return usb_started ? 0 : -1;
269}
270
Hans de Goede8802f562015-06-17 21:33:48 +0200271int usb_reset_root_port(struct usb_device *udev)
Simon Glassde312132015-03-25 12:21:59 -0600272{
273 return -ENOSYS;
274}
275
276static struct usb_device *find_child_devnum(struct udevice *parent, int devnum)
277{
278 struct usb_device *udev;
279 struct udevice *dev;
280
281 if (!device_active(parent))
282 return NULL;
283 udev = dev_get_parentdata(parent);
284 if (udev->devnum == devnum)
285 return udev;
286
287 for (device_find_first_child(parent, &dev);
288 dev;
289 device_find_next_child(&dev)) {
290 udev = find_child_devnum(dev, devnum);
291 if (udev)
292 return udev;
293 }
294
295 return NULL;
296}
297
298struct usb_device *usb_get_dev_index(struct udevice *bus, int index)
299{
300 struct udevice *hub;
301 int devnum = index + 1; /* Addresses are allocated from 1 on USB */
302
303 device_find_first_child(bus, &hub);
304 if (device_get_uclass_id(hub) == UCLASS_USB_HUB)
305 return find_child_devnum(hub, devnum);
306
307 return NULL;
308}
309
310int usb_post_bind(struct udevice *dev)
311{
312 /* Scan the bus for devices */
313 return dm_scan_fdt_node(dev, gd->fdt_blob, dev->of_offset, false);
314}
315
Simon Glassfbeceb22015-03-25 12:22:32 -0600316int usb_setup_ehci_gadget(struct ehci_ctrl **ctlrp)
317{
318 struct usb_platdata *plat;
319 struct udevice *dev;
320 int ret;
321
322 /* Find the old device and remove it */
323 ret = uclass_find_device_by_seq(UCLASS_USB, 0, true, &dev);
324 if (ret)
325 return ret;
326 ret = device_remove(dev);
327 if (ret)
328 return ret;
329
330 plat = dev_get_platdata(dev);
331 plat->init_type = USB_INIT_DEVICE;
332 ret = device_probe(dev);
333 if (ret)
334 return ret;
335 *ctlrp = dev_get_priv(dev);
336
337 return 0;
338}
339
Simon Glass0566e242015-03-25 12:22:30 -0600340/* returns 0 if no match, 1 if match */
341int usb_match_device(const struct usb_device_descriptor *desc,
342 const struct usb_device_id *id)
343{
344 if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
345 id->idVendor != le16_to_cpu(desc->idVendor))
346 return 0;
347
348 if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
349 id->idProduct != le16_to_cpu(desc->idProduct))
350 return 0;
351
352 /* No need to test id->bcdDevice_lo != 0, since 0 is never
353 greater than any unsigned number. */
354 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) &&
355 (id->bcdDevice_lo > le16_to_cpu(desc->bcdDevice)))
356 return 0;
357
358 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) &&
359 (id->bcdDevice_hi < le16_to_cpu(desc->bcdDevice)))
360 return 0;
361
362 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) &&
363 (id->bDeviceClass != desc->bDeviceClass))
364 return 0;
365
366 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) &&
367 (id->bDeviceSubClass != desc->bDeviceSubClass))
368 return 0;
369
370 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) &&
371 (id->bDeviceProtocol != desc->bDeviceProtocol))
372 return 0;
373
374 return 1;
375}
376
377/* returns 0 if no match, 1 if match */
378int usb_match_one_id_intf(const struct usb_device_descriptor *desc,
379 const struct usb_interface_descriptor *int_desc,
380 const struct usb_device_id *id)
381{
382 /* The interface class, subclass, protocol and number should never be
383 * checked for a match if the device class is Vendor Specific,
384 * unless the match record specifies the Vendor ID. */
385 if (desc->bDeviceClass == USB_CLASS_VENDOR_SPEC &&
386 !(id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
387 (id->match_flags & (USB_DEVICE_ID_MATCH_INT_CLASS |
388 USB_DEVICE_ID_MATCH_INT_SUBCLASS |
389 USB_DEVICE_ID_MATCH_INT_PROTOCOL |
390 USB_DEVICE_ID_MATCH_INT_NUMBER)))
391 return 0;
392
393 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) &&
394 (id->bInterfaceClass != int_desc->bInterfaceClass))
395 return 0;
396
397 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) &&
398 (id->bInterfaceSubClass != int_desc->bInterfaceSubClass))
399 return 0;
400
401 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) &&
402 (id->bInterfaceProtocol != int_desc->bInterfaceProtocol))
403 return 0;
404
405 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_NUMBER) &&
406 (id->bInterfaceNumber != int_desc->bInterfaceNumber))
407 return 0;
408
409 return 1;
410}
411
412/* returns 0 if no match, 1 if match */
413int usb_match_one_id(struct usb_device_descriptor *desc,
414 struct usb_interface_descriptor *int_desc,
415 const struct usb_device_id *id)
416{
417 if (!usb_match_device(desc, id))
418 return 0;
419
420 return usb_match_one_id_intf(desc, int_desc, id);
421}
422
423/**
424 * usb_find_and_bind_driver() - Find and bind the right USB driver
425 *
426 * This only looks at certain fields in the descriptor.
427 */
428static int usb_find_and_bind_driver(struct udevice *parent,
429 struct usb_device_descriptor *desc,
430 struct usb_interface_descriptor *iface,
431 int bus_seq, int devnum,
432 struct udevice **devp)
433{
434 struct usb_driver_entry *start, *entry;
435 int n_ents;
436 int ret;
437 char name[30], *str;
438
439 *devp = NULL;
440 debug("%s: Searching for driver\n", __func__);
441 start = ll_entry_start(struct usb_driver_entry, usb_driver_entry);
442 n_ents = ll_entry_count(struct usb_driver_entry, usb_driver_entry);
443 for (entry = start; entry != start + n_ents; entry++) {
444 const struct usb_device_id *id;
445 struct udevice *dev;
446 const struct driver *drv;
447 struct usb_dev_platdata *plat;
448
449 for (id = entry->match; id->match_flags; id++) {
450 if (!usb_match_one_id(desc, iface, id))
451 continue;
452
453 drv = entry->driver;
454 /*
455 * We could pass the descriptor to the driver as
456 * platdata (instead of NULL) and allow its bind()
457 * method to return -ENOENT if it doesn't support this
458 * device. That way we could continue the search to
459 * find another driver. For now this doesn't seem
460 * necesssary, so just bind the first match.
461 */
462 ret = device_bind(parent, drv, drv->name, NULL, -1,
463 &dev);
464 if (ret)
465 goto error;
466 debug("%s: Match found: %s\n", __func__, drv->name);
467 dev->driver_data = id->driver_info;
468 plat = dev_get_parent_platdata(dev);
469 plat->id = *id;
470 *devp = dev;
471 return 0;
472 }
473 }
474
Simon Glass449230f2015-03-25 12:22:31 -0600475 /* Bind a generic driver so that the device can be used */
476 snprintf(name, sizeof(name), "generic_bus_%x_dev_%x", bus_seq, devnum);
477 str = strdup(name);
478 if (!str)
479 return -ENOMEM;
480 ret = device_bind_driver(parent, "usb_dev_generic_drv", str, devp);
481
Simon Glass0566e242015-03-25 12:22:30 -0600482error:
483 debug("%s: No match found: %d\n", __func__, ret);
484 return ret;
485}
486
487/**
Hans de Goede9b510df2015-07-01 20:53:01 +0200488 * usb_find_emul_child() - Find an existing device for emulated devices
Simon Glass0566e242015-03-25 12:22:30 -0600489 */
Hans de Goede9b510df2015-07-01 20:53:01 +0200490static int usb_find_emul_child(struct udevice *parent,
491 struct usb_device_descriptor *desc,
492 struct usb_interface_descriptor *iface,
493 struct udevice **devp)
Simon Glass0566e242015-03-25 12:22:30 -0600494{
Hans de Goede9b510df2015-07-01 20:53:01 +0200495#ifdef CONFIG_SANDBOX
Simon Glass0566e242015-03-25 12:22:30 -0600496 struct udevice *dev;
497
498 *devp = NULL;
499 for (device_find_first_child(parent, &dev);
500 dev;
501 device_find_next_child(&dev)) {
502 struct usb_dev_platdata *plat = dev_get_parent_platdata(dev);
503
504 /* If this device is already in use, skip it */
505 if (device_active(dev))
506 continue;
507 debug(" %s: name='%s', plat=%d, desc=%d\n", __func__,
508 dev->name, plat->id.bDeviceClass, desc->bDeviceClass);
509 if (usb_match_one_id(desc, iface, &plat->id)) {
510 *devp = dev;
511 return 0;
512 }
513 }
Hans de Goede9b510df2015-07-01 20:53:01 +0200514#endif
Simon Glass0566e242015-03-25 12:22:30 -0600515 return -ENOENT;
516}
517
Simon Glassde312132015-03-25 12:21:59 -0600518int usb_scan_device(struct udevice *parent, int port,
519 enum usb_device_speed speed, struct udevice **devp)
520{
521 struct udevice *dev;
522 bool created = false;
523 struct usb_dev_platdata *plat;
524 struct usb_bus_priv *priv;
525 struct usb_device *parent_udev;
526 int ret;
527 ALLOC_CACHE_ALIGN_BUFFER(struct usb_device, udev, 1);
528 struct usb_interface_descriptor *iface = &udev->config.if_desc[0].desc;
529
530 *devp = NULL;
531 memset(udev, '\0', sizeof(*udev));
Hans de Goedef78a5c02015-05-05 11:54:31 +0200532 udev->controller_dev = usb_get_bus(parent);
Simon Glassde312132015-03-25 12:21:59 -0600533 priv = dev_get_uclass_priv(udev->controller_dev);
534
535 /*
536 * Somewhat nasty, this. We create a local device and use the normal
537 * USB stack to read its descriptor. Then we know what type of device
538 * to create for real.
539 *
540 * udev->dev is set to the parent, since we don't have a real device
541 * yet. The USB stack should not access udev.dev anyway, except perhaps
542 * to find the controller, and the controller will either be @parent,
543 * or some parent of @parent.
544 *
545 * Another option might be to create the device as a generic USB
546 * device, then morph it into the correct one when we know what it
547 * should be. This means that a generic USB device would morph into
548 * a network controller, or a USB flash stick, for example. However,
549 * we don't support such morphing and it isn't clear that it would
550 * be easy to do.
551 *
552 * Yet another option is to split out the USB stack parts of udev
553 * into something like a 'struct urb' (as Linux does) which can exist
554 * independently of any device. This feels cleaner, but calls for quite
555 * a big change to the USB stack.
556 *
557 * For now, the approach is to set up an empty udev, read its
558 * descriptor and assign it an address, then bind a real device and
559 * stash the resulting information into the device's parent
560 * platform data. Then when we probe it, usb_child_pre_probe() is called
561 * and it will pull the information out of the stash.
562 */
563 udev->dev = parent;
564 udev->speed = speed;
565 udev->devnum = priv->next_addr + 1;
566 udev->portnr = port;
567 debug("Calling usb_setup_device(), portnr=%d\n", udev->portnr);
568 parent_udev = device_get_uclass_id(parent) == UCLASS_USB_HUB ?
569 dev_get_parentdata(parent) : NULL;
Hans de Goede9eb72dd2015-06-17 21:33:46 +0200570 ret = usb_setup_device(udev, priv->desc_before_addr, parent_udev);
Simon Glassde312132015-03-25 12:21:59 -0600571 debug("read_descriptor for '%s': ret=%d\n", parent->name, ret);
572 if (ret)
573 return ret;
Hans de Goede9b510df2015-07-01 20:53:01 +0200574 ret = usb_find_emul_child(parent, &udev->descriptor, iface, &dev);
575 debug("** usb_find_emul_child returns %d\n", ret);
Simon Glass0566e242015-03-25 12:22:30 -0600576 if (ret) {
577 if (ret != -ENOENT)
578 return ret;
579 ret = usb_find_and_bind_driver(parent, &udev->descriptor, iface,
580 udev->controller_dev->seq,
581 udev->devnum, &dev);
582 if (ret)
583 return ret;
584 created = true;
585 }
586 plat = dev_get_parent_platdata(dev);
587 debug("%s: Probing '%s', plat=%p\n", __func__, dev->name, plat);
588 plat->devnum = udev->devnum;
Hans de Goede7f1a0752015-05-05 11:54:32 +0200589 plat->udev = udev;
Simon Glass0566e242015-03-25 12:22:30 -0600590 priv->next_addr++;
591 ret = device_probe(dev);
592 if (ret) {
593 debug("%s: Device '%s' probe failed\n", __func__, dev->name);
594 priv->next_addr--;
595 if (created)
596 device_unbind(dev);
597 return ret;
598 }
599 *devp = dev;
Simon Glassde312132015-03-25 12:21:59 -0600600
Simon Glass0566e242015-03-25 12:22:30 -0600601 return 0;
Simon Glassde312132015-03-25 12:21:59 -0600602}
603
Simon Glass0c5dd9a2015-05-13 07:02:23 -0600604/*
605 * Detect if a USB device has been plugged or unplugged.
606 */
607int usb_detect_change(void)
608{
609 struct udevice *hub;
610 struct uclass *uc;
611 int change = 0;
612 int ret;
613
614 ret = uclass_get(UCLASS_USB_HUB, &uc);
615 if (ret)
616 return ret;
617
618 uclass_foreach_dev(hub, uc) {
619 struct usb_device *udev;
620 struct udevice *dev;
621
622 if (!device_active(hub))
623 continue;
624 for (device_find_first_child(hub, &dev);
625 dev;
626 device_find_next_child(&dev)) {
627 struct usb_port_status status;
628
629 if (!device_active(dev))
630 continue;
631
632 udev = dev_get_parentdata(dev);
633 if (usb_get_port_status(udev, udev->portnr, &status)
634 < 0)
635 /* USB request failed */
636 continue;
637
638 if (le16_to_cpu(status.wPortChange) &
639 USB_PORT_STAT_C_CONNECTION)
640 change++;
641 }
642 }
643
644 return change;
645}
646
Simon Glassde312132015-03-25 12:21:59 -0600647int usb_child_post_bind(struct udevice *dev)
648{
649 struct usb_dev_platdata *plat = dev_get_parent_platdata(dev);
650 const void *blob = gd->fdt_blob;
651 int val;
652
653 if (dev->of_offset == -1)
654 return 0;
655
656 /* We only support matching a few things */
657 val = fdtdec_get_int(blob, dev->of_offset, "usb,device-class", -1);
658 if (val != -1) {
659 plat->id.match_flags |= USB_DEVICE_ID_MATCH_DEV_CLASS;
660 plat->id.bDeviceClass = val;
661 }
662 val = fdtdec_get_int(blob, dev->of_offset, "usb,interface-class", -1);
663 if (val != -1) {
664 plat->id.match_flags |= USB_DEVICE_ID_MATCH_INT_CLASS;
665 plat->id.bInterfaceClass = val;
666 }
667
668 return 0;
669}
670
Hans de Goedef78a5c02015-05-05 11:54:31 +0200671struct udevice *usb_get_bus(struct udevice *dev)
Simon Glassde312132015-03-25 12:21:59 -0600672{
673 struct udevice *bus;
674
Simon Glassde312132015-03-25 12:21:59 -0600675 for (bus = dev; bus && device_get_uclass_id(bus) != UCLASS_USB; )
676 bus = bus->parent;
677 if (!bus) {
678 /* By design this cannot happen */
679 assert(bus);
680 debug("USB HUB '%s' does not have a controller\n", dev->name);
Simon Glassde312132015-03-25 12:21:59 -0600681 }
Simon Glassde312132015-03-25 12:21:59 -0600682
Hans de Goedef78a5c02015-05-05 11:54:31 +0200683 return bus;
Simon Glassde312132015-03-25 12:21:59 -0600684}
685
686int usb_child_pre_probe(struct udevice *dev)
687{
Simon Glassde312132015-03-25 12:21:59 -0600688 struct usb_device *udev = dev_get_parentdata(dev);
689 struct usb_dev_platdata *plat = dev_get_parent_platdata(dev);
690 int ret;
691
Hans de Goede7f1a0752015-05-05 11:54:32 +0200692 if (plat->udev) {
693 /*
694 * Copy over all the values set in the on stack struct
695 * usb_device in usb_scan_device() to our final struct
696 * usb_device for this dev.
697 */
698 *udev = *(plat->udev);
699 /* And clear plat->udev as it will not be valid for long */
700 plat->udev = NULL;
701 udev->dev = dev;
702 } else {
703 /*
704 * This happens with devices which are explicitly bound
705 * instead of being discovered through usb_scan_device()
706 * such as sandbox emul devices.
707 */
708 udev->dev = dev;
709 udev->controller_dev = usb_get_bus(dev);
710 udev->devnum = plat->devnum;
Simon Glassde312132015-03-25 12:21:59 -0600711
Hans de Goede7f1a0752015-05-05 11:54:32 +0200712 /*
713 * udev did not go through usb_scan_device(), so we need to
714 * select the config and read the config descriptors.
715 */
716 ret = usb_select_config(udev);
717 if (ret)
718 return ret;
719 }
Simon Glassde312132015-03-25 12:21:59 -0600720
721 return 0;
722}
723
724UCLASS_DRIVER(usb) = {
725 .id = UCLASS_USB,
726 .name = "usb",
727 .flags = DM_UC_FLAG_SEQ_ALIAS,
728 .post_bind = usb_post_bind,
Hans de Goedee2536372015-05-10 14:10:21 +0200729 .priv_auto_alloc_size = sizeof(struct usb_uclass_priv),
Simon Glassde312132015-03-25 12:21:59 -0600730 .per_child_auto_alloc_size = sizeof(struct usb_device),
731 .per_device_auto_alloc_size = sizeof(struct usb_bus_priv),
732 .child_post_bind = usb_child_post_bind,
733 .child_pre_probe = usb_child_pre_probe,
734 .per_child_platdata_auto_alloc_size = sizeof(struct usb_dev_platdata),
735};
Simon Glass449230f2015-03-25 12:22:31 -0600736
737UCLASS_DRIVER(usb_dev_generic) = {
738 .id = UCLASS_USB_DEV_GENERIC,
739 .name = "usb_dev_generic",
740};
741
742U_BOOT_DRIVER(usb_dev_generic_drv) = {
743 .id = UCLASS_USB_DEV_GENERIC,
744 .name = "usb_dev_generic_drv",
745};