blob: 8dc18236aba8a21d929d6e4b4a1b213bb03195ab [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));
479 ret = usb_get_bus(parent, &udev->controller_dev);
480 if (ret)
481 return ret;
482 priv = dev_get_uclass_priv(udev->controller_dev);
483
484 /*
485 * Somewhat nasty, this. We create a local device and use the normal
486 * USB stack to read its descriptor. Then we know what type of device
487 * to create for real.
488 *
489 * udev->dev is set to the parent, since we don't have a real device
490 * yet. The USB stack should not access udev.dev anyway, except perhaps
491 * to find the controller, and the controller will either be @parent,
492 * or some parent of @parent.
493 *
494 * Another option might be to create the device as a generic USB
495 * device, then morph it into the correct one when we know what it
496 * should be. This means that a generic USB device would morph into
497 * a network controller, or a USB flash stick, for example. However,
498 * we don't support such morphing and it isn't clear that it would
499 * be easy to do.
500 *
501 * Yet another option is to split out the USB stack parts of udev
502 * into something like a 'struct urb' (as Linux does) which can exist
503 * independently of any device. This feels cleaner, but calls for quite
504 * a big change to the USB stack.
505 *
506 * For now, the approach is to set up an empty udev, read its
507 * descriptor and assign it an address, then bind a real device and
508 * stash the resulting information into the device's parent
509 * platform data. Then when we probe it, usb_child_pre_probe() is called
510 * and it will pull the information out of the stash.
511 */
512 udev->dev = parent;
513 udev->speed = speed;
514 udev->devnum = priv->next_addr + 1;
515 udev->portnr = port;
516 debug("Calling usb_setup_device(), portnr=%d\n", udev->portnr);
517 parent_udev = device_get_uclass_id(parent) == UCLASS_USB_HUB ?
518 dev_get_parentdata(parent) : NULL;
519 ret = usb_setup_device(udev, priv->desc_before_addr, parent_udev, port);
520 debug("read_descriptor for '%s': ret=%d\n", parent->name, ret);
521 if (ret)
522 return ret;
523 ret = usb_find_child(parent, &udev->descriptor, iface, &dev);
524 debug("** usb_find_child returns %d\n", ret);
Simon Glass0566e242015-03-25 12:22:30 -0600525 if (ret) {
526 if (ret != -ENOENT)
527 return ret;
528 ret = usb_find_and_bind_driver(parent, &udev->descriptor, iface,
529 udev->controller_dev->seq,
530 udev->devnum, &dev);
531 if (ret)
532 return ret;
533 created = true;
534 }
535 plat = dev_get_parent_platdata(dev);
536 debug("%s: Probing '%s', plat=%p\n", __func__, dev->name, plat);
537 plat->devnum = udev->devnum;
538 plat->speed = udev->speed;
539 plat->slot_id = udev->slot_id;
540 plat->portnr = port;
541 debug("** device '%s': stashing slot_id=%d\n", dev->name,
542 plat->slot_id);
543 priv->next_addr++;
544 ret = device_probe(dev);
545 if (ret) {
546 debug("%s: Device '%s' probe failed\n", __func__, dev->name);
547 priv->next_addr--;
548 if (created)
549 device_unbind(dev);
550 return ret;
551 }
552 *devp = dev;
Simon Glassde312132015-03-25 12:21:59 -0600553
Simon Glass0566e242015-03-25 12:22:30 -0600554 return 0;
Simon Glassde312132015-03-25 12:21:59 -0600555}
556
557int usb_child_post_bind(struct udevice *dev)
558{
559 struct usb_dev_platdata *plat = dev_get_parent_platdata(dev);
560 const void *blob = gd->fdt_blob;
561 int val;
562
563 if (dev->of_offset == -1)
564 return 0;
565
566 /* We only support matching a few things */
567 val = fdtdec_get_int(blob, dev->of_offset, "usb,device-class", -1);
568 if (val != -1) {
569 plat->id.match_flags |= USB_DEVICE_ID_MATCH_DEV_CLASS;
570 plat->id.bDeviceClass = val;
571 }
572 val = fdtdec_get_int(blob, dev->of_offset, "usb,interface-class", -1);
573 if (val != -1) {
574 plat->id.match_flags |= USB_DEVICE_ID_MATCH_INT_CLASS;
575 plat->id.bInterfaceClass = val;
576 }
577
578 return 0;
579}
580
581int usb_get_bus(struct udevice *dev, struct udevice **busp)
582{
583 struct udevice *bus;
584
585 *busp = NULL;
586 for (bus = dev; bus && device_get_uclass_id(bus) != UCLASS_USB; )
587 bus = bus->parent;
588 if (!bus) {
589 /* By design this cannot happen */
590 assert(bus);
591 debug("USB HUB '%s' does not have a controller\n", dev->name);
592 return -EXDEV;
593 }
594 *busp = bus;
595
596 return 0;
597}
598
599int usb_child_pre_probe(struct udevice *dev)
600{
601 struct udevice *bus;
602 struct usb_device *udev = dev_get_parentdata(dev);
603 struct usb_dev_platdata *plat = dev_get_parent_platdata(dev);
604 int ret;
605
606 ret = usb_get_bus(dev, &bus);
607 if (ret)
608 return ret;
609 udev->controller_dev = bus;
610 udev->dev = dev;
611 udev->devnum = plat->devnum;
612 udev->slot_id = plat->slot_id;
613 udev->portnr = plat->portnr;
614 udev->speed = plat->speed;
615 debug("** device '%s': getting slot_id=%d\n", dev->name, plat->slot_id);
616
617 ret = usb_select_config(udev);
618 if (ret)
619 return ret;
620
621 return 0;
622}
623
624UCLASS_DRIVER(usb) = {
625 .id = UCLASS_USB,
626 .name = "usb",
627 .flags = DM_UC_FLAG_SEQ_ALIAS,
628 .post_bind = usb_post_bind,
629 .per_child_auto_alloc_size = sizeof(struct usb_device),
630 .per_device_auto_alloc_size = sizeof(struct usb_bus_priv),
631 .child_post_bind = usb_child_post_bind,
632 .child_pre_probe = usb_child_pre_probe,
633 .per_child_platdata_auto_alloc_size = sizeof(struct usb_dev_platdata),
634};
Simon Glass449230f2015-03-25 12:22:31 -0600635
636UCLASS_DRIVER(usb_dev_generic) = {
637 .id = UCLASS_USB_DEV_GENERIC,
638 .name = "usb_dev_generic",
639};
640
641U_BOOT_DRIVER(usb_dev_generic_drv) = {
642 .id = UCLASS_USB_DEV_GENERIC,
643 .name = "usb_dev_generic_drv",
644};