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