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