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