1 /*
2 * (C) Copyright 2015 Google, Inc
3 * Written by Simon Glass <sjg@chromium.org>
4 *
5 * usb_match_device() modified from Linux kernel v4.0.
6 *
7 * SPDX-License-Identifier: GPL-2.0+
8 */
9
10 #include <common.h>
11 #include <dm.h>
12 #include <errno.h>
13 #include <memalign.h>
14 #include <usb.h>
15 #include <dm/device-internal.h>
16 #include <dm/lists.h>
17 #include <dm/uclass-internal.h>
18
19 DECLARE_GLOBAL_DATA_PTR;
20
21 extern bool usb_started; /* flag for the started/stopped USB status */
22 static bool asynch_allowed;
23
24 struct usb_uclass_priv {
25 int companion_device_count;
26 };
27
usb_disable_asynch(int disable)28 int usb_disable_asynch(int disable)
29 {
30 int old_value = asynch_allowed;
31
32 asynch_allowed = !disable;
33 return old_value;
34 }
35
submit_int_msg(struct usb_device * udev,unsigned long pipe,void * buffer,int length,int interval,bool nonblock)36 int submit_int_msg(struct usb_device *udev, unsigned long pipe, void *buffer,
37 int length, int interval, bool nonblock)
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 nonblock);
47 }
48
submit_control_msg(struct usb_device * udev,unsigned long pipe,void * buffer,int length,struct devrequest * setup)49 int 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);
54 struct usb_uclass_priv *uc_priv = bus->uclass->priv;
55 int err;
56
57 if (!ops->control)
58 return -ENOSYS;
59
60 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;
70 }
71
submit_bulk_msg(struct usb_device * udev,unsigned long pipe,void * buffer,int length)72 int 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
create_int_queue(struct usb_device * udev,unsigned long pipe,int queuesize,int elementsize,void * buffer,int interval)84 struct 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
poll_int_queue(struct usb_device * udev,struct int_queue * queue)98 void *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
destroy_int_queue(struct usb_device * udev,struct int_queue * queue)109 int 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
usb_alloc_device(struct usb_device * udev)120 int 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
usb_reset_root_port(struct usb_device * udev)132 int 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
usb_update_hub_device(struct usb_device * udev)143 int usb_update_hub_device(struct usb_device *udev)
144 {
145 struct udevice *bus = udev->controller_dev;
146 struct dm_usb_ops *ops = usb_get_ops(bus);
147
148 if (!ops->update_hub_device)
149 return -ENOSYS;
150
151 return ops->update_hub_device(bus, udev);
152 }
153
usb_get_max_xfer_size(struct usb_device * udev,size_t * size)154 int usb_get_max_xfer_size(struct usb_device *udev, size_t *size)
155 {
156 struct udevice *bus = udev->controller_dev;
157 struct dm_usb_ops *ops = usb_get_ops(bus);
158
159 if (!ops->get_max_xfer_size)
160 return -ENOSYS;
161
162 return ops->get_max_xfer_size(bus, size);
163 }
164
usb_stop(void)165 int usb_stop(void)
166 {
167 struct udevice *bus;
168 struct udevice *rh;
169 struct uclass *uc;
170 struct usb_uclass_priv *uc_priv;
171 int err = 0, ret;
172
173 /* De-activate any devices that have been activated */
174 ret = uclass_get(UCLASS_USB, &uc);
175 if (ret)
176 return ret;
177
178 uc_priv = uc->priv;
179
180 uclass_foreach_dev(bus, uc) {
181 ret = device_remove(bus, DM_REMOVE_NORMAL);
182 if (ret && !err)
183 err = ret;
184
185 /* Locate root hub device */
186 device_find_first_child(bus, &rh);
187 if (rh) {
188 /*
189 * All USB devices are children of root hub.
190 * Unbinding root hub will unbind all of its children.
191 */
192 ret = device_unbind(rh);
193 if (ret && !err)
194 err = ret;
195 }
196 }
197
198 #ifdef CONFIG_USB_STORAGE
199 usb_stor_reset();
200 #endif
201 uc_priv->companion_device_count = 0;
202 usb_started = 0;
203
204 return err;
205 }
206
usb_scan_bus(struct udevice * bus,bool recurse)207 static void usb_scan_bus(struct udevice *bus, bool recurse)
208 {
209 struct usb_bus_priv *priv;
210 struct udevice *dev;
211 int ret;
212
213 priv = dev_get_uclass_priv(bus);
214
215 assert(recurse); /* TODO: Support non-recusive */
216
217 printf("scanning bus %s for devices... ", bus->name);
218 debug("\n");
219 ret = usb_scan_device(bus, 0, USB_SPEED_FULL, &dev);
220 if (ret)
221 printf("failed, error %d\n", ret);
222 else if (priv->next_addr == 0)
223 printf("No USB Device found\n");
224 else
225 printf("%d USB Device(s) found\n", priv->next_addr);
226 }
227
remove_inactive_children(struct uclass * uc,struct udevice * bus)228 static void remove_inactive_children(struct uclass *uc, struct udevice *bus)
229 {
230 uclass_foreach_dev(bus, uc) {
231 struct udevice *dev, *next;
232
233 if (!device_active(bus))
234 continue;
235 device_foreach_child_safe(dev, next, bus) {
236 if (!device_active(dev))
237 device_unbind(dev);
238 }
239 }
240 }
241
usb_init(void)242 int usb_init(void)
243 {
244 int controllers_initialized = 0;
245 struct usb_uclass_priv *uc_priv;
246 struct usb_bus_priv *priv;
247 struct udevice *bus;
248 struct uclass *uc;
249 int ret;
250
251 asynch_allowed = 1;
252
253 ret = uclass_get(UCLASS_USB, &uc);
254 if (ret)
255 return ret;
256
257 uc_priv = uc->priv;
258
259 uclass_foreach_dev(bus, uc) {
260 /* init low_level USB */
261 printf("Bus %s: ", bus->name);
262
263 #ifdef CONFIG_SANDBOX
264 /*
265 * For Sandbox, we need scan the device tree each time when we
266 * start the USB stack, in order to re-create the emulated USB
267 * devices and bind drivers for them before we actually do the
268 * driver probe.
269 */
270 ret = dm_scan_fdt_dev(bus);
271 if (ret) {
272 printf("Sandbox USB device scan failed (%d)\n", ret);
273 continue;
274 }
275 #endif
276
277 ret = device_probe(bus);
278 if (ret == -ENODEV) { /* No such device. */
279 puts("Port not available.\n");
280 controllers_initialized++;
281 continue;
282 }
283
284 if (ret) { /* Other error. */
285 printf("probe failed, error %d\n", ret);
286 continue;
287 }
288 controllers_initialized++;
289 usb_started = true;
290 }
291
292 /*
293 * lowlevel init done, now scan the bus for devices i.e. search HUBs
294 * and configure them, first scan primary controllers.
295 */
296 uclass_foreach_dev(bus, uc) {
297 if (!device_active(bus))
298 continue;
299
300 priv = dev_get_uclass_priv(bus);
301 if (!priv->companion)
302 usb_scan_bus(bus, true);
303 }
304
305 /*
306 * Now that the primary controllers have been scanned and have handed
307 * over any devices they do not understand to their companions, scan
308 * the companions if necessary.
309 */
310 if (uc_priv->companion_device_count) {
311 uclass_foreach_dev(bus, uc) {
312 if (!device_active(bus))
313 continue;
314
315 priv = dev_get_uclass_priv(bus);
316 if (priv->companion)
317 usb_scan_bus(bus, true);
318 }
319 }
320
321 debug("scan end\n");
322
323 /* Remove any devices that were not found on this scan */
324 remove_inactive_children(uc, bus);
325
326 ret = uclass_get(UCLASS_USB_HUB, &uc);
327 if (ret)
328 return ret;
329 remove_inactive_children(uc, bus);
330
331 /* if we were not able to find at least one working bus, bail out */
332 if (controllers_initialized == 0)
333 printf("No working controllers found\n");
334
335 return usb_started ? 0 : -1;
336 }
337
338 /*
339 * TODO(sjg@chromium.org): Remove this legacy function. At present it is needed
340 * to support boards which use driver model for USB but not Ethernet, and want
341 * to use USB Ethernet.
342 *
343 * The #if clause is here to ensure that remains the only case.
344 */
345 #if !defined(CONFIG_DM_ETH) && defined(CONFIG_USB_HOST_ETHER)
find_child_devnum(struct udevice * parent,int devnum)346 static struct usb_device *find_child_devnum(struct udevice *parent, int devnum)
347 {
348 struct usb_device *udev;
349 struct udevice *dev;
350
351 if (!device_active(parent))
352 return NULL;
353 udev = dev_get_parent_priv(parent);
354 if (udev->devnum == devnum)
355 return udev;
356
357 for (device_find_first_child(parent, &dev);
358 dev;
359 device_find_next_child(&dev)) {
360 udev = find_child_devnum(dev, devnum);
361 if (udev)
362 return udev;
363 }
364
365 return NULL;
366 }
367
usb_get_dev_index(struct udevice * bus,int index)368 struct usb_device *usb_get_dev_index(struct udevice *bus, int index)
369 {
370 struct udevice *dev;
371 int devnum = index + 1; /* Addresses are allocated from 1 on USB */
372
373 device_find_first_child(bus, &dev);
374 if (!dev)
375 return NULL;
376
377 return find_child_devnum(dev, devnum);
378 }
379 #endif
380
usb_setup_ehci_gadget(struct ehci_ctrl ** ctlrp)381 int usb_setup_ehci_gadget(struct ehci_ctrl **ctlrp)
382 {
383 struct usb_platdata *plat;
384 struct udevice *dev;
385 int ret;
386
387 /* Find the old device and remove it */
388 ret = uclass_find_device_by_seq(UCLASS_USB, 0, true, &dev);
389 if (ret)
390 return ret;
391 ret = device_remove(dev, DM_REMOVE_NORMAL);
392 if (ret)
393 return ret;
394
395 plat = dev_get_platdata(dev);
396 plat->init_type = USB_INIT_DEVICE;
397 ret = device_probe(dev);
398 if (ret)
399 return ret;
400 *ctlrp = dev_get_priv(dev);
401
402 return 0;
403 }
404
405 /* returns 0 if no match, 1 if match */
usb_match_device(const struct usb_device_descriptor * desc,const struct usb_device_id * id)406 static int usb_match_device(const struct usb_device_descriptor *desc,
407 const struct usb_device_id *id)
408 {
409 if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
410 id->idVendor != le16_to_cpu(desc->idVendor))
411 return 0;
412
413 if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
414 id->idProduct != le16_to_cpu(desc->idProduct))
415 return 0;
416
417 /* No need to test id->bcdDevice_lo != 0, since 0 is never
418 greater than any unsigned number. */
419 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) &&
420 (id->bcdDevice_lo > le16_to_cpu(desc->bcdDevice)))
421 return 0;
422
423 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) &&
424 (id->bcdDevice_hi < le16_to_cpu(desc->bcdDevice)))
425 return 0;
426
427 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) &&
428 (id->bDeviceClass != desc->bDeviceClass))
429 return 0;
430
431 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) &&
432 (id->bDeviceSubClass != desc->bDeviceSubClass))
433 return 0;
434
435 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) &&
436 (id->bDeviceProtocol != desc->bDeviceProtocol))
437 return 0;
438
439 return 1;
440 }
441
442 /* returns 0 if no match, 1 if match */
usb_match_one_id_intf(const struct usb_device_descriptor * desc,const struct usb_interface_descriptor * int_desc,const struct usb_device_id * id)443 static int usb_match_one_id_intf(const struct usb_device_descriptor *desc,
444 const struct usb_interface_descriptor *int_desc,
445 const struct usb_device_id *id)
446 {
447 /* The interface class, subclass, protocol and number should never be
448 * checked for a match if the device class is Vendor Specific,
449 * unless the match record specifies the Vendor ID. */
450 if (desc->bDeviceClass == USB_CLASS_VENDOR_SPEC &&
451 !(id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
452 (id->match_flags & (USB_DEVICE_ID_MATCH_INT_CLASS |
453 USB_DEVICE_ID_MATCH_INT_SUBCLASS |
454 USB_DEVICE_ID_MATCH_INT_PROTOCOL |
455 USB_DEVICE_ID_MATCH_INT_NUMBER)))
456 return 0;
457
458 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) &&
459 (id->bInterfaceClass != int_desc->bInterfaceClass))
460 return 0;
461
462 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) &&
463 (id->bInterfaceSubClass != int_desc->bInterfaceSubClass))
464 return 0;
465
466 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) &&
467 (id->bInterfaceProtocol != int_desc->bInterfaceProtocol))
468 return 0;
469
470 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_NUMBER) &&
471 (id->bInterfaceNumber != int_desc->bInterfaceNumber))
472 return 0;
473
474 return 1;
475 }
476
477 /* returns 0 if no match, 1 if match */
usb_match_one_id(struct usb_device_descriptor * desc,struct usb_interface_descriptor * int_desc,const struct usb_device_id * id)478 static int usb_match_one_id(struct usb_device_descriptor *desc,
479 struct usb_interface_descriptor *int_desc,
480 const struct usb_device_id *id)
481 {
482 if (!usb_match_device(desc, id))
483 return 0;
484
485 return usb_match_one_id_intf(desc, int_desc, id);
486 }
487
488 /**
489 * usb_find_and_bind_driver() - Find and bind the right USB driver
490 *
491 * This only looks at certain fields in the descriptor.
492 */
usb_find_and_bind_driver(struct udevice * parent,struct usb_device_descriptor * desc,struct usb_interface_descriptor * iface,int bus_seq,int devnum,struct udevice ** devp)493 static int usb_find_and_bind_driver(struct udevice *parent,
494 struct usb_device_descriptor *desc,
495 struct usb_interface_descriptor *iface,
496 int bus_seq, int devnum,
497 struct udevice **devp)
498 {
499 struct usb_driver_entry *start, *entry;
500 int n_ents;
501 int ret;
502 char name[30], *str;
503
504 *devp = NULL;
505 debug("%s: Searching for driver\n", __func__);
506 start = ll_entry_start(struct usb_driver_entry, usb_driver_entry);
507 n_ents = ll_entry_count(struct usb_driver_entry, usb_driver_entry);
508 for (entry = start; entry != start + n_ents; entry++) {
509 const struct usb_device_id *id;
510 struct udevice *dev;
511 const struct driver *drv;
512 struct usb_dev_platdata *plat;
513
514 for (id = entry->match; id->match_flags; id++) {
515 if (!usb_match_one_id(desc, iface, id))
516 continue;
517
518 drv = entry->driver;
519 /*
520 * We could pass the descriptor to the driver as
521 * platdata (instead of NULL) and allow its bind()
522 * method to return -ENOENT if it doesn't support this
523 * device. That way we could continue the search to
524 * find another driver. For now this doesn't seem
525 * necesssary, so just bind the first match.
526 */
527 ret = device_bind(parent, drv, drv->name, NULL, -1,
528 &dev);
529 if (ret)
530 goto error;
531 debug("%s: Match found: %s\n", __func__, drv->name);
532 dev->driver_data = id->driver_info;
533 plat = dev_get_parent_platdata(dev);
534 plat->id = *id;
535 *devp = dev;
536 return 0;
537 }
538 }
539
540 /* Bind a generic driver so that the device can be used */
541 snprintf(name, sizeof(name), "generic_bus_%x_dev_%x", bus_seq, devnum);
542 str = strdup(name);
543 if (!str)
544 return -ENOMEM;
545 ret = device_bind_driver(parent, "usb_dev_generic_drv", str, devp);
546
547 error:
548 debug("%s: No match found: %d\n", __func__, ret);
549 return ret;
550 }
551
552 /**
553 * usb_find_child() - Find an existing device which matches our needs
554 *
555 *
556 */
usb_find_child(struct udevice * parent,struct usb_device_descriptor * desc,struct usb_interface_descriptor * iface,struct udevice ** devp)557 static int usb_find_child(struct udevice *parent,
558 struct usb_device_descriptor *desc,
559 struct usb_interface_descriptor *iface,
560 struct udevice **devp)
561 {
562 struct udevice *dev;
563
564 *devp = NULL;
565 for (device_find_first_child(parent, &dev);
566 dev;
567 device_find_next_child(&dev)) {
568 struct usb_dev_platdata *plat = dev_get_parent_platdata(dev);
569
570 /* If this device is already in use, skip it */
571 if (device_active(dev))
572 continue;
573 debug(" %s: name='%s', plat=%d, desc=%d\n", __func__,
574 dev->name, plat->id.bDeviceClass, desc->bDeviceClass);
575 if (usb_match_one_id(desc, iface, &plat->id)) {
576 *devp = dev;
577 return 0;
578 }
579 }
580
581 return -ENOENT;
582 }
583
usb_scan_device(struct udevice * parent,int port,enum usb_device_speed speed,struct udevice ** devp)584 int usb_scan_device(struct udevice *parent, int port,
585 enum usb_device_speed speed, struct udevice **devp)
586 {
587 struct udevice *dev;
588 bool created = false;
589 struct usb_dev_platdata *plat;
590 struct usb_bus_priv *priv;
591 struct usb_device *parent_udev;
592 int ret;
593 ALLOC_CACHE_ALIGN_BUFFER(struct usb_device, udev, 1);
594 struct usb_interface_descriptor *iface = &udev->config.if_desc[0].desc;
595
596 *devp = NULL;
597 memset(udev, '\0', sizeof(*udev));
598 udev->controller_dev = usb_get_bus(parent);
599 priv = dev_get_uclass_priv(udev->controller_dev);
600
601 /*
602 * Somewhat nasty, this. We create a local device and use the normal
603 * USB stack to read its descriptor. Then we know what type of device
604 * to create for real.
605 *
606 * udev->dev is set to the parent, since we don't have a real device
607 * yet. The USB stack should not access udev.dev anyway, except perhaps
608 * to find the controller, and the controller will either be @parent,
609 * or some parent of @parent.
610 *
611 * Another option might be to create the device as a generic USB
612 * device, then morph it into the correct one when we know what it
613 * should be. This means that a generic USB device would morph into
614 * a network controller, or a USB flash stick, for example. However,
615 * we don't support such morphing and it isn't clear that it would
616 * be easy to do.
617 *
618 * Yet another option is to split out the USB stack parts of udev
619 * into something like a 'struct urb' (as Linux does) which can exist
620 * independently of any device. This feels cleaner, but calls for quite
621 * a big change to the USB stack.
622 *
623 * For now, the approach is to set up an empty udev, read its
624 * descriptor and assign it an address, then bind a real device and
625 * stash the resulting information into the device's parent
626 * platform data. Then when we probe it, usb_child_pre_probe() is called
627 * and it will pull the information out of the stash.
628 */
629 udev->dev = parent;
630 udev->speed = speed;
631 udev->devnum = priv->next_addr + 1;
632 udev->portnr = port;
633 debug("Calling usb_setup_device(), portnr=%d\n", udev->portnr);
634 parent_udev = device_get_uclass_id(parent) == UCLASS_USB_HUB ?
635 dev_get_parent_priv(parent) : NULL;
636 ret = usb_setup_device(udev, priv->desc_before_addr, parent_udev);
637 debug("read_descriptor for '%s': ret=%d\n", parent->name, ret);
638 if (ret)
639 return ret;
640 ret = usb_find_child(parent, &udev->descriptor, iface, &dev);
641 debug("** usb_find_child returns %d\n", ret);
642 if (ret) {
643 if (ret != -ENOENT)
644 return ret;
645 ret = usb_find_and_bind_driver(parent, &udev->descriptor, iface,
646 udev->controller_dev->seq,
647 udev->devnum, &dev);
648 if (ret)
649 return ret;
650 created = true;
651 }
652 plat = dev_get_parent_platdata(dev);
653 debug("%s: Probing '%s', plat=%p\n", __func__, dev->name, plat);
654 plat->devnum = udev->devnum;
655 plat->udev = udev;
656 priv->next_addr++;
657 ret = device_probe(dev);
658 if (ret) {
659 debug("%s: Device '%s' probe failed\n", __func__, dev->name);
660 priv->next_addr--;
661 if (created)
662 device_unbind(dev);
663 return ret;
664 }
665 *devp = dev;
666
667 return 0;
668 }
669
670 /*
671 * Detect if a USB device has been plugged or unplugged.
672 */
usb_detect_change(void)673 int usb_detect_change(void)
674 {
675 struct udevice *hub;
676 struct uclass *uc;
677 int change = 0;
678 int ret;
679
680 ret = uclass_get(UCLASS_USB_HUB, &uc);
681 if (ret)
682 return ret;
683
684 uclass_foreach_dev(hub, uc) {
685 struct usb_device *udev;
686 struct udevice *dev;
687
688 if (!device_active(hub))
689 continue;
690 for (device_find_first_child(hub, &dev);
691 dev;
692 device_find_next_child(&dev)) {
693 struct usb_port_status status;
694
695 if (!device_active(dev))
696 continue;
697
698 udev = dev_get_parent_priv(dev);
699 if (usb_get_port_status(udev, udev->portnr, &status)
700 < 0)
701 /* USB request failed */
702 continue;
703
704 if (le16_to_cpu(status.wPortChange) &
705 USB_PORT_STAT_C_CONNECTION)
706 change++;
707 }
708 }
709
710 return change;
711 }
712
usb_child_post_bind(struct udevice * dev)713 static int usb_child_post_bind(struct udevice *dev)
714 {
715 struct usb_dev_platdata *plat = dev_get_parent_platdata(dev);
716 int val;
717
718 if (!dev_of_valid(dev))
719 return 0;
720
721 /* We only support matching a few things */
722 val = dev_read_u32_default(dev, "usb,device-class", -1);
723 if (val != -1) {
724 plat->id.match_flags |= USB_DEVICE_ID_MATCH_DEV_CLASS;
725 plat->id.bDeviceClass = val;
726 }
727 val = dev_read_u32_default(dev, "usb,interface-class", -1);
728 if (val != -1) {
729 plat->id.match_flags |= USB_DEVICE_ID_MATCH_INT_CLASS;
730 plat->id.bInterfaceClass = val;
731 }
732
733 return 0;
734 }
735
usb_get_bus(struct udevice * dev)736 struct udevice *usb_get_bus(struct udevice *dev)
737 {
738 struct udevice *bus;
739
740 for (bus = dev; bus && device_get_uclass_id(bus) != UCLASS_USB; )
741 bus = bus->parent;
742 if (!bus) {
743 /* By design this cannot happen */
744 assert(bus);
745 debug("USB HUB '%s' does not have a controller\n", dev->name);
746 }
747
748 return bus;
749 }
750
usb_child_pre_probe(struct udevice * dev)751 int usb_child_pre_probe(struct udevice *dev)
752 {
753 struct usb_device *udev = dev_get_parent_priv(dev);
754 struct usb_dev_platdata *plat = dev_get_parent_platdata(dev);
755 int ret;
756
757 if (plat->udev) {
758 /*
759 * Copy over all the values set in the on stack struct
760 * usb_device in usb_scan_device() to our final struct
761 * usb_device for this dev.
762 */
763 *udev = *(plat->udev);
764 /* And clear plat->udev as it will not be valid for long */
765 plat->udev = NULL;
766 udev->dev = dev;
767 } else {
768 /*
769 * This happens with devices which are explicitly bound
770 * instead of being discovered through usb_scan_device()
771 * such as sandbox emul devices.
772 */
773 udev->dev = dev;
774 udev->controller_dev = usb_get_bus(dev);
775 udev->devnum = plat->devnum;
776
777 /*
778 * udev did not go through usb_scan_device(), so we need to
779 * select the config and read the config descriptors.
780 */
781 ret = usb_select_config(udev);
782 if (ret)
783 return ret;
784 }
785
786 return 0;
787 }
788
789 UCLASS_DRIVER(usb) = {
790 .id = UCLASS_USB,
791 .name = "usb",
792 .flags = DM_UC_FLAG_SEQ_ALIAS,
793 .post_bind = dm_scan_fdt_dev,
794 .priv_auto_alloc_size = sizeof(struct usb_uclass_priv),
795 .per_child_auto_alloc_size = sizeof(struct usb_device),
796 .per_device_auto_alloc_size = sizeof(struct usb_bus_priv),
797 .child_post_bind = usb_child_post_bind,
798 .child_pre_probe = usb_child_pre_probe,
799 .per_child_platdata_auto_alloc_size = sizeof(struct usb_dev_platdata),
800 };
801
802 UCLASS_DRIVER(usb_dev_generic) = {
803 .id = UCLASS_USB_DEV_GENERIC,
804 .name = "usb_dev_generic",
805 };
806
807 U_BOOT_DRIVER(usb_dev_generic_drv) = {
808 .id = UCLASS_USB_DEV_GENERIC,
809 .name = "usb_dev_generic_drv",
810 };
811