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