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