]> git.karo-electronics.de Git - karo-tx-uboot.git/blob - drivers/usb/host/usb-uclass.c
dm: usb: Move printing of usb scan status to usb_scan_bus()
[karo-tx-uboot.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 udevice *bus;
175         struct uclass *uc;
176         int count = 0;
177         int ret;
178
179         asynch_allowed = 1;
180         usb_hub_reset();
181
182         ret = uclass_get(UCLASS_USB, &uc);
183         if (ret)
184                 return ret;
185
186         uclass_foreach_dev(bus, uc) {
187                 /* init low_level USB */
188                 printf("USB%d:   ", count);
189                 count++;
190                 ret = device_probe(bus);
191                 if (ret == -ENODEV) {   /* No such device. */
192                         puts("Port not available.\n");
193                         controllers_initialized++;
194                         continue;
195                 }
196
197                 if (ret) {              /* Other error. */
198                         printf("probe failed, error %d\n", ret);
199                         continue;
200                 }
201                 /*
202                  * lowlevel init is OK, now scan the bus for devices
203                  * i.e. search HUBs and configure them
204                  */
205                 controllers_initialized++;
206                 usb_scan_bus(bus, true);
207                 usb_started = true;
208         }
209
210         debug("scan end\n");
211         /* if we were not able to find at least one working bus, bail out */
212         if (!count)
213                 printf("No controllers found\n");
214         else if (controllers_initialized == 0)
215                 printf("USB error: all controllers failed lowlevel init\n");
216
217         return usb_started ? 0 : -1;
218 }
219
220 int usb_reset_root_port(void)
221 {
222         return -ENOSYS;
223 }
224
225 static struct usb_device *find_child_devnum(struct udevice *parent, int devnum)
226 {
227         struct usb_device *udev;
228         struct udevice *dev;
229
230         if (!device_active(parent))
231                 return NULL;
232         udev = dev_get_parentdata(parent);
233         if (udev->devnum == devnum)
234                 return udev;
235
236         for (device_find_first_child(parent, &dev);
237              dev;
238              device_find_next_child(&dev)) {
239                 udev = find_child_devnum(dev, devnum);
240                 if (udev)
241                         return udev;
242         }
243
244         return NULL;
245 }
246
247 struct usb_device *usb_get_dev_index(struct udevice *bus, int index)
248 {
249         struct udevice *hub;
250         int devnum = index + 1; /* Addresses are allocated from 1 on USB */
251
252         device_find_first_child(bus, &hub);
253         if (device_get_uclass_id(hub) == UCLASS_USB_HUB)
254                 return find_child_devnum(hub, devnum);
255
256         return NULL;
257 }
258
259 int usb_post_bind(struct udevice *dev)
260 {
261         /* Scan the bus for devices */
262         return dm_scan_fdt_node(dev, gd->fdt_blob, dev->of_offset, false);
263 }
264
265 int usb_port_reset(struct usb_device *parent, int portnr)
266 {
267         unsigned short portstatus;
268         int ret;
269
270         debug("%s: start\n", __func__);
271
272         if (parent) {
273                 /* reset the port for the second time */
274                 assert(portnr > 0);
275                 debug("%s: reset %d\n", __func__, portnr - 1);
276                 ret = legacy_hub_port_reset(parent, portnr - 1, &portstatus);
277                 if (ret < 0) {
278                         printf("\n     Couldn't reset port %i\n", portnr);
279                         return ret;
280                 }
281         } else {
282                 debug("%s: reset root\n", __func__);
283                 usb_reset_root_port();
284         }
285
286         return 0;
287 }
288
289 int usb_legacy_port_reset(struct usb_device *parent, int portnr)
290 {
291         return usb_port_reset(parent, portnr);
292 }
293
294 int usb_setup_ehci_gadget(struct ehci_ctrl **ctlrp)
295 {
296         struct usb_platdata *plat;
297         struct udevice *dev;
298         int ret;
299
300         /* Find the old device and remove it */
301         ret = uclass_find_device_by_seq(UCLASS_USB, 0, true, &dev);
302         if (ret)
303                 return ret;
304         ret = device_remove(dev);
305         if (ret)
306                 return ret;
307
308         plat = dev_get_platdata(dev);
309         plat->init_type = USB_INIT_DEVICE;
310         ret = device_probe(dev);
311         if (ret)
312                 return ret;
313         *ctlrp = dev_get_priv(dev);
314
315         return 0;
316 }
317
318 /* returns 0 if no match, 1 if match */
319 int usb_match_device(const struct usb_device_descriptor *desc,
320                      const struct usb_device_id *id)
321 {
322         if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
323             id->idVendor != le16_to_cpu(desc->idVendor))
324                 return 0;
325
326         if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
327             id->idProduct != le16_to_cpu(desc->idProduct))
328                 return 0;
329
330         /* No need to test id->bcdDevice_lo != 0, since 0 is never
331            greater than any unsigned number. */
332         if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) &&
333             (id->bcdDevice_lo > le16_to_cpu(desc->bcdDevice)))
334                 return 0;
335
336         if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) &&
337             (id->bcdDevice_hi < le16_to_cpu(desc->bcdDevice)))
338                 return 0;
339
340         if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) &&
341             (id->bDeviceClass != desc->bDeviceClass))
342                 return 0;
343
344         if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) &&
345             (id->bDeviceSubClass != desc->bDeviceSubClass))
346                 return 0;
347
348         if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) &&
349             (id->bDeviceProtocol != desc->bDeviceProtocol))
350                 return 0;
351
352         return 1;
353 }
354
355 /* returns 0 if no match, 1 if match */
356 int usb_match_one_id_intf(const struct usb_device_descriptor *desc,
357                           const struct usb_interface_descriptor *int_desc,
358                           const struct usb_device_id *id)
359 {
360         /* The interface class, subclass, protocol and number should never be
361          * checked for a match if the device class is Vendor Specific,
362          * unless the match record specifies the Vendor ID. */
363         if (desc->bDeviceClass == USB_CLASS_VENDOR_SPEC &&
364             !(id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
365             (id->match_flags & (USB_DEVICE_ID_MATCH_INT_CLASS |
366                                 USB_DEVICE_ID_MATCH_INT_SUBCLASS |
367                                 USB_DEVICE_ID_MATCH_INT_PROTOCOL |
368                                 USB_DEVICE_ID_MATCH_INT_NUMBER)))
369                 return 0;
370
371         if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) &&
372             (id->bInterfaceClass != int_desc->bInterfaceClass))
373                 return 0;
374
375         if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) &&
376             (id->bInterfaceSubClass != int_desc->bInterfaceSubClass))
377                 return 0;
378
379         if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) &&
380             (id->bInterfaceProtocol != int_desc->bInterfaceProtocol))
381                 return 0;
382
383         if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_NUMBER) &&
384             (id->bInterfaceNumber != int_desc->bInterfaceNumber))
385                 return 0;
386
387         return 1;
388 }
389
390 /* returns 0 if no match, 1 if match */
391 int usb_match_one_id(struct usb_device_descriptor *desc,
392                      struct usb_interface_descriptor *int_desc,
393                      const struct usb_device_id *id)
394 {
395         if (!usb_match_device(desc, id))
396                 return 0;
397
398         return usb_match_one_id_intf(desc, int_desc, id);
399 }
400
401 /**
402  * usb_find_and_bind_driver() - Find and bind the right USB driver
403  *
404  * This only looks at certain fields in the descriptor.
405  */
406 static int usb_find_and_bind_driver(struct udevice *parent,
407                                     struct usb_device_descriptor *desc,
408                                     struct usb_interface_descriptor *iface,
409                                     int bus_seq, int devnum,
410                                     struct udevice **devp)
411 {
412         struct usb_driver_entry *start, *entry;
413         int n_ents;
414         int ret;
415         char name[30], *str;
416
417         *devp = NULL;
418         debug("%s: Searching for driver\n", __func__);
419         start = ll_entry_start(struct usb_driver_entry, usb_driver_entry);
420         n_ents = ll_entry_count(struct usb_driver_entry, usb_driver_entry);
421         for (entry = start; entry != start + n_ents; entry++) {
422                 const struct usb_device_id *id;
423                 struct udevice *dev;
424                 const struct driver *drv;
425                 struct usb_dev_platdata *plat;
426
427                 for (id = entry->match; id->match_flags; id++) {
428                         if (!usb_match_one_id(desc, iface, id))
429                                 continue;
430
431                         drv = entry->driver;
432                         /*
433                          * We could pass the descriptor to the driver as
434                          * platdata (instead of NULL) and allow its bind()
435                          * method to return -ENOENT if it doesn't support this
436                          * device. That way we could continue the search to
437                          * find another driver. For now this doesn't seem
438                          * necesssary, so just bind the first match.
439                          */
440                         ret = device_bind(parent, drv, drv->name, NULL, -1,
441                                           &dev);
442                         if (ret)
443                                 goto error;
444                         debug("%s: Match found: %s\n", __func__, drv->name);
445                         dev->driver_data = id->driver_info;
446                         plat = dev_get_parent_platdata(dev);
447                         plat->id = *id;
448                         *devp = dev;
449                         return 0;
450                 }
451         }
452
453         /* Bind a generic driver so that the device can be used */
454         snprintf(name, sizeof(name), "generic_bus_%x_dev_%x", bus_seq, devnum);
455         str = strdup(name);
456         if (!str)
457                 return -ENOMEM;
458         ret = device_bind_driver(parent, "usb_dev_generic_drv", str, devp);
459
460 error:
461         debug("%s: No match found: %d\n", __func__, ret);
462         return ret;
463 }
464
465 /**
466  * usb_find_child() - Find an existing device which matches our needs
467  *
468  *
469  */
470 static int usb_find_child(struct udevice *parent,
471                           struct usb_device_descriptor *desc,
472                           struct usb_interface_descriptor *iface,
473                           struct udevice **devp)
474 {
475         struct udevice *dev;
476
477         *devp = NULL;
478         for (device_find_first_child(parent, &dev);
479              dev;
480              device_find_next_child(&dev)) {
481                 struct usb_dev_platdata *plat = dev_get_parent_platdata(dev);
482
483                 /* If this device is already in use, skip it */
484                 if (device_active(dev))
485                         continue;
486                 debug("   %s: name='%s', plat=%d, desc=%d\n", __func__,
487                       dev->name, plat->id.bDeviceClass, desc->bDeviceClass);
488                 if (usb_match_one_id(desc, iface, &plat->id)) {
489                         *devp = dev;
490                         return 0;
491                 }
492         }
493
494         return -ENOENT;
495 }
496
497 int usb_scan_device(struct udevice *parent, int port,
498                     enum usb_device_speed speed, struct udevice **devp)
499 {
500         struct udevice *dev;
501         bool created = false;
502         struct usb_dev_platdata *plat;
503         struct usb_bus_priv *priv;
504         struct usb_device *parent_udev;
505         int ret;
506         ALLOC_CACHE_ALIGN_BUFFER(struct usb_device, udev, 1);
507         struct usb_interface_descriptor *iface = &udev->config.if_desc[0].desc;
508
509         *devp = NULL;
510         memset(udev, '\0', sizeof(*udev));
511         udev->controller_dev = usb_get_bus(parent);
512         priv = dev_get_uclass_priv(udev->controller_dev);
513
514         /*
515          * Somewhat nasty, this. We create a local device and use the normal
516          * USB stack to read its descriptor. Then we know what type of device
517          * to create for real.
518          *
519          * udev->dev is set to the parent, since we don't have a real device
520          * yet. The USB stack should not access udev.dev anyway, except perhaps
521          * to find the controller, and the controller will either be @parent,
522          * or some parent of @parent.
523          *
524          * Another option might be to create the device as a generic USB
525          * device, then morph it into the correct one when we know what it
526          * should be. This means that a generic USB device would morph into
527          * a network controller, or a USB flash stick, for example. However,
528          * we don't support such morphing and it isn't clear that it would
529          * be easy to do.
530          *
531          * Yet another option is to split out the USB stack parts of udev
532          * into something like a 'struct urb' (as Linux does) which can exist
533          * independently of any device. This feels cleaner, but calls for quite
534          * a big change to the USB stack.
535          *
536          * For now, the approach is to set up an empty udev, read its
537          * descriptor and assign it an address, then bind a real device and
538          * stash the resulting information into the device's parent
539          * platform data. Then when we probe it, usb_child_pre_probe() is called
540          * and it will pull the information out of the stash.
541          */
542         udev->dev = parent;
543         udev->speed = speed;
544         udev->devnum = priv->next_addr + 1;
545         udev->portnr = port;
546         debug("Calling usb_setup_device(), portnr=%d\n", udev->portnr);
547         parent_udev = device_get_uclass_id(parent) == UCLASS_USB_HUB ?
548                 dev_get_parentdata(parent) : NULL;
549         ret = usb_setup_device(udev, priv->desc_before_addr, parent_udev, port);
550         debug("read_descriptor for '%s': ret=%d\n", parent->name, ret);
551         if (ret)
552                 return ret;
553         ret = usb_find_child(parent, &udev->descriptor, iface, &dev);
554         debug("** usb_find_child returns %d\n", ret);
555         if (ret) {
556                 if (ret != -ENOENT)
557                         return ret;
558                 ret = usb_find_and_bind_driver(parent, &udev->descriptor, iface,
559                                                udev->controller_dev->seq,
560                                                udev->devnum, &dev);
561                 if (ret)
562                         return ret;
563                 created = true;
564         }
565         plat = dev_get_parent_platdata(dev);
566         debug("%s: Probing '%s', plat=%p\n", __func__, dev->name, plat);
567         plat->devnum = udev->devnum;
568         plat->udev = udev;
569         priv->next_addr++;
570         ret = device_probe(dev);
571         if (ret) {
572                 debug("%s: Device '%s' probe failed\n", __func__, dev->name);
573                 priv->next_addr--;
574                 if (created)
575                         device_unbind(dev);
576                 return ret;
577         }
578         *devp = dev;
579
580         return 0;
581 }
582
583 int usb_child_post_bind(struct udevice *dev)
584 {
585         struct usb_dev_platdata *plat = dev_get_parent_platdata(dev);
586         const void *blob = gd->fdt_blob;
587         int val;
588
589         if (dev->of_offset == -1)
590                 return 0;
591
592         /* We only support matching a few things */
593         val = fdtdec_get_int(blob, dev->of_offset, "usb,device-class", -1);
594         if (val != -1) {
595                 plat->id.match_flags |= USB_DEVICE_ID_MATCH_DEV_CLASS;
596                 plat->id.bDeviceClass = val;
597         }
598         val = fdtdec_get_int(blob, dev->of_offset, "usb,interface-class", -1);
599         if (val != -1) {
600                 plat->id.match_flags |= USB_DEVICE_ID_MATCH_INT_CLASS;
601                 plat->id.bInterfaceClass = val;
602         }
603
604         return 0;
605 }
606
607 struct udevice *usb_get_bus(struct udevice *dev)
608 {
609         struct udevice *bus;
610
611         for (bus = dev; bus && device_get_uclass_id(bus) != UCLASS_USB; )
612                 bus = bus->parent;
613         if (!bus) {
614                 /* By design this cannot happen */
615                 assert(bus);
616                 debug("USB HUB '%s' does not have a controller\n", dev->name);
617         }
618
619         return bus;
620 }
621
622 int usb_child_pre_probe(struct udevice *dev)
623 {
624         struct usb_device *udev = dev_get_parentdata(dev);
625         struct usb_dev_platdata *plat = dev_get_parent_platdata(dev);
626         int ret;
627
628         if (plat->udev) {
629                 /*
630                  * Copy over all the values set in the on stack struct
631                  * usb_device in usb_scan_device() to our final struct
632                  * usb_device for this dev.
633                  */
634                 *udev = *(plat->udev);
635                 /* And clear plat->udev as it will not be valid for long */
636                 plat->udev = NULL;
637                 udev->dev = dev;
638         } else {
639                 /*
640                  * This happens with devices which are explicitly bound
641                  * instead of being discovered through usb_scan_device()
642                  * such as sandbox emul devices.
643                  */
644                 udev->dev = dev;
645                 udev->controller_dev = usb_get_bus(dev);
646                 udev->devnum = plat->devnum;
647
648                 /*
649                  * udev did not go through usb_scan_device(), so we need to
650                  * select the config and read the config descriptors.
651                  */
652                 ret = usb_select_config(udev);
653                 if (ret)
654                         return ret;
655         }
656
657         return 0;
658 }
659
660 UCLASS_DRIVER(usb) = {
661         .id             = UCLASS_USB,
662         .name           = "usb",
663         .flags          = DM_UC_FLAG_SEQ_ALIAS,
664         .post_bind      = usb_post_bind,
665         .per_child_auto_alloc_size = sizeof(struct usb_device),
666         .per_device_auto_alloc_size = sizeof(struct usb_bus_priv),
667         .child_post_bind = usb_child_post_bind,
668         .child_pre_probe = usb_child_pre_probe,
669         .per_child_platdata_auto_alloc_size = sizeof(struct usb_dev_platdata),
670 };
671
672 UCLASS_DRIVER(usb_dev_generic) = {
673         .id             = UCLASS_USB_DEV_GENERIC,
674         .name           = "usb_dev_generic",
675 };
676
677 U_BOOT_DRIVER(usb_dev_generic_drv) = {
678         .id             = UCLASS_USB_DEV_GENERIC,
679         .name           = "usb_dev_generic_drv",
680 };