]> git.ipfire.org Git - people/ms/u-boot.git/blob - drivers/usb/host/usb-uclass.c
dm: usb: Make usb_get_bus easier to use for callers
[people/ms/u-boot.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 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 #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
107 usb_stor_reset();
108 usb_hub_reset();
109 usb_started = 0;
110
111 return err;
112 }
113
114 static 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
131 int 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 printf("USB%d: ", count);
149 count++;
150 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
188 int usb_reset_root_port(void)
189 {
190 return -ENOSYS;
191 }
192
193 static 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
215 struct 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
227 int 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
233 int 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
257 int usb_legacy_port_reset(struct usb_device *parent, int portnr)
258 {
259 return usb_port_reset(parent, portnr);
260 }
261
262 int 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
286 /* returns 0 if no match, 1 if match */
287 int 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 */
324 int 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 */
359 int 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 */
374 static 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
421 /* 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
428 error:
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 */
438 static 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
465 int 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 udev->controller_dev = usb_get_bus(parent);
480 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);
523 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;
536 plat->speed = udev->speed;
537 plat->slot_id = udev->slot_id;
538 plat->portnr = port;
539 debug("** device '%s': stashing slot_id=%d\n", dev->name,
540 plat->slot_id);
541 priv->next_addr++;
542 ret = device_probe(dev);
543 if (ret) {
544 debug("%s: Device '%s' probe failed\n", __func__, dev->name);
545 priv->next_addr--;
546 if (created)
547 device_unbind(dev);
548 return ret;
549 }
550 *devp = dev;
551
552 return 0;
553 }
554
555 int usb_child_post_bind(struct udevice *dev)
556 {
557 struct usb_dev_platdata *plat = dev_get_parent_platdata(dev);
558 const void *blob = gd->fdt_blob;
559 int val;
560
561 if (dev->of_offset == -1)
562 return 0;
563
564 /* We only support matching a few things */
565 val = fdtdec_get_int(blob, dev->of_offset, "usb,device-class", -1);
566 if (val != -1) {
567 plat->id.match_flags |= USB_DEVICE_ID_MATCH_DEV_CLASS;
568 plat->id.bDeviceClass = val;
569 }
570 val = fdtdec_get_int(blob, dev->of_offset, "usb,interface-class", -1);
571 if (val != -1) {
572 plat->id.match_flags |= USB_DEVICE_ID_MATCH_INT_CLASS;
573 plat->id.bInterfaceClass = val;
574 }
575
576 return 0;
577 }
578
579 struct udevice *usb_get_bus(struct udevice *dev)
580 {
581 struct udevice *bus;
582
583 for (bus = dev; bus && device_get_uclass_id(bus) != UCLASS_USB; )
584 bus = bus->parent;
585 if (!bus) {
586 /* By design this cannot happen */
587 assert(bus);
588 debug("USB HUB '%s' does not have a controller\n", dev->name);
589 }
590
591 return bus;
592 }
593
594 int usb_child_pre_probe(struct udevice *dev)
595 {
596 struct usb_device *udev = dev_get_parentdata(dev);
597 struct usb_dev_platdata *plat = dev_get_parent_platdata(dev);
598 int ret;
599
600 udev->controller_dev = usb_get_bus(dev);
601 udev->dev = dev;
602 udev->devnum = plat->devnum;
603 udev->slot_id = plat->slot_id;
604 udev->portnr = plat->portnr;
605 udev->speed = plat->speed;
606 debug("** device '%s': getting slot_id=%d\n", dev->name, plat->slot_id);
607
608 ret = usb_select_config(udev);
609 if (ret)
610 return ret;
611
612 return 0;
613 }
614
615 UCLASS_DRIVER(usb) = {
616 .id = UCLASS_USB,
617 .name = "usb",
618 .flags = DM_UC_FLAG_SEQ_ALIAS,
619 .post_bind = usb_post_bind,
620 .per_child_auto_alloc_size = sizeof(struct usb_device),
621 .per_device_auto_alloc_size = sizeof(struct usb_bus_priv),
622 .child_post_bind = usb_child_post_bind,
623 .child_pre_probe = usb_child_pre_probe,
624 .per_child_platdata_auto_alloc_size = sizeof(struct usb_dev_platdata),
625 };
626
627 UCLASS_DRIVER(usb_dev_generic) = {
628 .id = UCLASS_USB_DEV_GENERIC,
629 .name = "usb_dev_generic",
630 };
631
632 U_BOOT_DRIVER(usb_dev_generic_drv) = {
633 .id = UCLASS_USB_DEV_GENERIC,
634 .name = "usb_dev_generic_drv",
635 };