blob: d8d74bd04bb4c1d5ada5aaf7981328c020260a77 [file] [log] [blame]
Simon Glass9b82eeb2015-03-25 12:21:59 -06001/*
2 * (C) Copyright 2015 Google, Inc
3 * Written by Simon Glass <sjg@chromium.org>
4 *
Simon Glassfc03a552015-03-25 12:22:30 -06005 * usb_match_device() modified from Linux kernel v4.0.
6 *
Simon Glass9b82eeb2015-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>
Simon Glass2dd337a2015-09-02 17:24:58 -060013#include <memalign.h>
Simon Glass9b82eeb2015-03-25 12:21:59 -060014#include <usb.h>
15#include <dm/device-internal.h>
16#include <dm/lists.h>
Simon Glass9b82eeb2015-03-25 12:21:59 -060017#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
Hans de Goedecebc5b72015-05-10 14:10:21 +020024struct usb_uclass_priv {
25 int companion_device_count;
26};
27
Simon Glass9b82eeb2015-03-25 12:21:59 -060028int usb_disable_asynch(int disable)
29{
30 int old_value = asynch_allowed;
31
32 asynch_allowed = !disable;
33 return old_value;
34}
35
36int submit_int_msg(struct usb_device *udev, unsigned long pipe, void *buffer,
37 int length, int interval)
38{
39 struct udevice *bus = udev->controller_dev;
40 struct dm_usb_ops *ops = usb_get_ops(bus);
41
42 if (!ops->interrupt)
43 return -ENOSYS;
44
45 return ops->interrupt(bus, udev, pipe, buffer, length, interval);
46}
47
48int submit_control_msg(struct usb_device *udev, unsigned long pipe,
49 void *buffer, int length, struct devrequest *setup)
50{
51 struct udevice *bus = udev->controller_dev;
52 struct dm_usb_ops *ops = usb_get_ops(bus);
Hans de Goedecebc5b72015-05-10 14:10:21 +020053 struct usb_uclass_priv *uc_priv = bus->uclass->priv;
54 int err;
Simon Glass9b82eeb2015-03-25 12:21:59 -060055
56 if (!ops->control)
57 return -ENOSYS;
58
Hans de Goedecebc5b72015-05-10 14:10:21 +020059 err = ops->control(bus, udev, pipe, buffer, length, setup);
60 if (setup->request == USB_REQ_SET_FEATURE &&
61 setup->requesttype == USB_RT_PORT &&
62 setup->value == cpu_to_le16(USB_PORT_FEAT_RESET) &&
63 err == -ENXIO) {
64 /* Device handed over to companion after port reset */
65 uc_priv->companion_device_count++;
66 }
67
68 return err;
Simon Glass9b82eeb2015-03-25 12:21:59 -060069}
70
71int submit_bulk_msg(struct usb_device *udev, unsigned long pipe, void *buffer,
72 int length)
73{
74 struct udevice *bus = udev->controller_dev;
75 struct dm_usb_ops *ops = usb_get_ops(bus);
76
77 if (!ops->bulk)
78 return -ENOSYS;
79
80 return ops->bulk(bus, udev, pipe, buffer, length);
81}
82
Hans de Goede0a7fa272015-05-10 14:10:18 +020083struct int_queue *create_int_queue(struct usb_device *udev,
84 unsigned long pipe, int queuesize, int elementsize,
85 void *buffer, int interval)
86{
87 struct udevice *bus = udev->controller_dev;
88 struct dm_usb_ops *ops = usb_get_ops(bus);
89
90 if (!ops->create_int_queue)
91 return NULL;
92
93 return ops->create_int_queue(bus, udev, pipe, queuesize, elementsize,
94 buffer, interval);
95}
96
97void *poll_int_queue(struct usb_device *udev, struct int_queue *queue)
98{
99 struct udevice *bus = udev->controller_dev;
100 struct dm_usb_ops *ops = usb_get_ops(bus);
101
102 if (!ops->poll_int_queue)
103 return NULL;
104
105 return ops->poll_int_queue(bus, udev, queue);
106}
107
108int destroy_int_queue(struct usb_device *udev, struct int_queue *queue)
109{
110 struct udevice *bus = udev->controller_dev;
111 struct dm_usb_ops *ops = usb_get_ops(bus);
112
113 if (!ops->destroy_int_queue)
114 return -ENOSYS;
115
116 return ops->destroy_int_queue(bus, udev, queue);
117}
118
Simon Glass9b82eeb2015-03-25 12:21:59 -0600119int usb_alloc_device(struct usb_device *udev)
120{
121 struct udevice *bus = udev->controller_dev;
122 struct dm_usb_ops *ops = usb_get_ops(bus);
123
124 /* This is only requird by some controllers - current XHCI */
125 if (!ops->alloc_device)
126 return 0;
127
128 return ops->alloc_device(bus, udev);
129}
130
Hans de Goedea9e41f82015-06-17 21:33:52 +0200131int usb_reset_root_port(struct usb_device *udev)
132{
133 struct udevice *bus = udev->controller_dev;
134 struct dm_usb_ops *ops = usb_get_ops(bus);
135
136 if (!ops->reset_root_port)
137 return -ENOSYS;
138
139 return ops->reset_root_port(bus, udev);
140}
141
Simon Glass9b82eeb2015-03-25 12:21:59 -0600142int usb_stop(void)
143{
144 struct udevice *bus;
145 struct uclass *uc;
Hans de Goedecebc5b72015-05-10 14:10:21 +0200146 struct usb_uclass_priv *uc_priv;
Simon Glass9b82eeb2015-03-25 12:21:59 -0600147 int err = 0, ret;
148
149 /* De-activate any devices that have been activated */
150 ret = uclass_get(UCLASS_USB, &uc);
151 if (ret)
152 return ret;
Hans de Goedecebc5b72015-05-10 14:10:21 +0200153
154 uc_priv = uc->priv;
155
Simon Glass9b82eeb2015-03-25 12:21:59 -0600156 uclass_foreach_dev(bus, uc) {
Stefan Roese80b5bc92017-03-20 12:51:48 +0100157 ret = device_remove(bus, DM_REMOVE_NORMAL);
Simon Glass9b82eeb2015-03-25 12:21:59 -0600158 if (ret && !err)
159 err = ret;
160 }
Simon Glassca4b2152016-03-13 08:22:33 -0600161#ifdef CONFIG_BLK
162 ret = blk_unbind_all(IF_TYPE_USB);
163 if (ret && !err)
164 err = ret;
165#endif
Simon Glass41477392015-03-25 12:22:38 -0600166#ifdef CONFIG_SANDBOX
167 struct udevice *dev;
168
169 /* Reset all enulation devices */
170 ret = uclass_get(UCLASS_USB_EMUL, &uc);
171 if (ret)
172 return ret;
173
174 uclass_foreach_dev(dev, uc)
175 usb_emul_reset(dev);
176#endif
Paul Kocialkowskid449dd32015-08-04 17:04:12 +0200177#ifdef CONFIG_USB_STORAGE
Simon Glass9b82eeb2015-03-25 12:21:59 -0600178 usb_stor_reset();
Paul Kocialkowskid449dd32015-08-04 17:04:12 +0200179#endif
Hans de Goedecebc5b72015-05-10 14:10:21 +0200180 uc_priv->companion_device_count = 0;
Simon Glass9b82eeb2015-03-25 12:21:59 -0600181 usb_started = 0;
182
183 return err;
184}
185
Hans de Goedef8762f92015-05-10 14:10:19 +0200186static void usb_scan_bus(struct udevice *bus, bool recurse)
Simon Glass9b82eeb2015-03-25 12:21:59 -0600187{
188 struct usb_bus_priv *priv;
189 struct udevice *dev;
190 int ret;
191
192 priv = dev_get_uclass_priv(bus);
193
194 assert(recurse); /* TODO: Support non-recusive */
195
Hans de Goedef8762f92015-05-10 14:10:19 +0200196 printf("scanning bus %d for devices... ", bus->seq);
197 debug("\n");
Simon Glass9b82eeb2015-03-25 12:21:59 -0600198 ret = usb_scan_device(bus, 0, USB_SPEED_FULL, &dev);
199 if (ret)
Hans de Goedef8762f92015-05-10 14:10:19 +0200200 printf("failed, error %d\n", ret);
201 else if (priv->next_addr == 0)
202 printf("No USB Device found\n");
203 else
204 printf("%d USB Device(s) found\n", priv->next_addr);
Simon Glass9b82eeb2015-03-25 12:21:59 -0600205}
206
Simon Glass35787d22015-11-08 23:48:00 -0700207static void remove_inactive_children(struct uclass *uc, struct udevice *bus)
208{
209 uclass_foreach_dev(bus, uc) {
210 struct udevice *dev, *next;
211
212 if (!device_active(bus))
213 continue;
214 device_foreach_child_safe(dev, next, bus) {
215 if (!device_active(dev))
216 device_unbind(dev);
217 }
218 }
219}
220
Simon Glass9b82eeb2015-03-25 12:21:59 -0600221int usb_init(void)
222{
223 int controllers_initialized = 0;
Hans de Goedecebc5b72015-05-10 14:10:21 +0200224 struct usb_uclass_priv *uc_priv;
Hans de Goede59a0dfc2015-05-10 14:10:20 +0200225 struct usb_bus_priv *priv;
Simon Glass9b82eeb2015-03-25 12:21:59 -0600226 struct udevice *bus;
227 struct uclass *uc;
228 int count = 0;
229 int ret;
230
231 asynch_allowed = 1;
Simon Glass9b82eeb2015-03-25 12:21:59 -0600232
233 ret = uclass_get(UCLASS_USB, &uc);
234 if (ret)
235 return ret;
236
Hans de Goedecebc5b72015-05-10 14:10:21 +0200237 uc_priv = uc->priv;
238
Simon Glass9b82eeb2015-03-25 12:21:59 -0600239 uclass_foreach_dev(bus, uc) {
240 /* init low_level USB */
Hans de Goede8679b3a2015-05-04 21:33:26 +0200241 printf("USB%d: ", count);
Simon Glass9b82eeb2015-03-25 12:21:59 -0600242 count++;
Simon Glass9b82eeb2015-03-25 12:21:59 -0600243 ret = device_probe(bus);
244 if (ret == -ENODEV) { /* No such device. */
245 puts("Port not available.\n");
246 controllers_initialized++;
247 continue;
248 }
249
250 if (ret) { /* Other error. */
251 printf("probe failed, error %d\n", ret);
252 continue;
253 }
Simon Glass9b82eeb2015-03-25 12:21:59 -0600254 controllers_initialized++;
Simon Glass9b82eeb2015-03-25 12:21:59 -0600255 usb_started = true;
256 }
257
Hans de Goede59a0dfc2015-05-10 14:10:20 +0200258 /*
259 * lowlevel init done, now scan the bus for devices i.e. search HUBs
260 * and configure them, first scan primary controllers.
261 */
262 uclass_foreach_dev(bus, uc) {
263 if (!device_active(bus))
264 continue;
265
266 priv = dev_get_uclass_priv(bus);
267 if (!priv->companion)
268 usb_scan_bus(bus, true);
269 }
270
271 /*
272 * Now that the primary controllers have been scanned and have handed
273 * over any devices they do not understand to their companions, scan
Hans de Goedecebc5b72015-05-10 14:10:21 +0200274 * the companions if necessary.
Hans de Goede59a0dfc2015-05-10 14:10:20 +0200275 */
Hans de Goedecebc5b72015-05-10 14:10:21 +0200276 if (uc_priv->companion_device_count) {
277 uclass_foreach_dev(bus, uc) {
278 if (!device_active(bus))
279 continue;
Hans de Goede59a0dfc2015-05-10 14:10:20 +0200280
Hans de Goedecebc5b72015-05-10 14:10:21 +0200281 priv = dev_get_uclass_priv(bus);
282 if (priv->companion)
283 usb_scan_bus(bus, true);
284 }
Hans de Goede59a0dfc2015-05-10 14:10:20 +0200285 }
286
Simon Glass9b82eeb2015-03-25 12:21:59 -0600287 debug("scan end\n");
Simon Glass35787d22015-11-08 23:48:00 -0700288
289 /* Remove any devices that were not found on this scan */
290 remove_inactive_children(uc, bus);
291
292 ret = uclass_get(UCLASS_USB_HUB, &uc);
293 if (ret)
294 return ret;
295 remove_inactive_children(uc, bus);
296
Simon Glass9b82eeb2015-03-25 12:21:59 -0600297 /* if we were not able to find at least one working bus, bail out */
298 if (!count)
299 printf("No controllers found\n");
300 else if (controllers_initialized == 0)
301 printf("USB error: all controllers failed lowlevel init\n");
302
303 return usb_started ? 0 : -1;
304}
305
Simon Glass5082c692015-11-08 23:47:59 -0700306/*
307 * TODO(sjg@chromium.org): Remove this legacy function. At present it is needed
308 * to support boards which use driver model for USB but not Ethernet, and want
309 * to use USB Ethernet.
310 *
311 * The #if clause is here to ensure that remains the only case.
312 */
313#if !defined(CONFIG_DM_ETH) && defined(CONFIG_USB_HOST_ETHER)
Simon Glass9b82eeb2015-03-25 12:21:59 -0600314static struct usb_device *find_child_devnum(struct udevice *parent, int devnum)
315{
316 struct usb_device *udev;
317 struct udevice *dev;
318
319 if (!device_active(parent))
320 return NULL;
Simon Glassde44acf2015-09-28 23:32:01 -0600321 udev = dev_get_parent_priv(parent);
Simon Glass9b82eeb2015-03-25 12:21:59 -0600322 if (udev->devnum == devnum)
323 return udev;
324
325 for (device_find_first_child(parent, &dev);
326 dev;
327 device_find_next_child(&dev)) {
328 udev = find_child_devnum(dev, devnum);
329 if (udev)
330 return udev;
331 }
332
333 return NULL;
334}
335
336struct usb_device *usb_get_dev_index(struct udevice *bus, int index)
337{
Hans de Goede9c203592015-06-17 21:33:53 +0200338 struct udevice *dev;
Simon Glass9b82eeb2015-03-25 12:21:59 -0600339 int devnum = index + 1; /* Addresses are allocated from 1 on USB */
340
Hans de Goede9c203592015-06-17 21:33:53 +0200341 device_find_first_child(bus, &dev);
342 if (!dev)
343 return NULL;
Simon Glass9b82eeb2015-03-25 12:21:59 -0600344
Hans de Goede9c203592015-06-17 21:33:53 +0200345 return find_child_devnum(dev, devnum);
Simon Glass9b82eeb2015-03-25 12:21:59 -0600346}
Simon Glass5082c692015-11-08 23:47:59 -0700347#endif
Simon Glass9b82eeb2015-03-25 12:21:59 -0600348
Simon Glass444b1e02015-03-25 12:22:32 -0600349int usb_setup_ehci_gadget(struct ehci_ctrl **ctlrp)
350{
351 struct usb_platdata *plat;
352 struct udevice *dev;
353 int ret;
354
355 /* Find the old device and remove it */
356 ret = uclass_find_device_by_seq(UCLASS_USB, 0, true, &dev);
357 if (ret)
358 return ret;
Stefan Roese80b5bc92017-03-20 12:51:48 +0100359 ret = device_remove(dev, DM_REMOVE_NORMAL);
Simon Glass444b1e02015-03-25 12:22:32 -0600360 if (ret)
361 return ret;
362
363 plat = dev_get_platdata(dev);
364 plat->init_type = USB_INIT_DEVICE;
365 ret = device_probe(dev);
366 if (ret)
367 return ret;
368 *ctlrp = dev_get_priv(dev);
369
370 return 0;
371}
372
Simon Glassfc03a552015-03-25 12:22:30 -0600373/* returns 0 if no match, 1 if match */
Masahiro Yamada6d8e4332017-06-22 16:35:14 +0900374static int usb_match_device(const struct usb_device_descriptor *desc,
375 const struct usb_device_id *id)
Simon Glassfc03a552015-03-25 12:22:30 -0600376{
377 if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
378 id->idVendor != le16_to_cpu(desc->idVendor))
379 return 0;
380
381 if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
382 id->idProduct != le16_to_cpu(desc->idProduct))
383 return 0;
384
385 /* No need to test id->bcdDevice_lo != 0, since 0 is never
386 greater than any unsigned number. */
387 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) &&
388 (id->bcdDevice_lo > le16_to_cpu(desc->bcdDevice)))
389 return 0;
390
391 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) &&
392 (id->bcdDevice_hi < le16_to_cpu(desc->bcdDevice)))
393 return 0;
394
395 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) &&
396 (id->bDeviceClass != desc->bDeviceClass))
397 return 0;
398
399 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) &&
400 (id->bDeviceSubClass != desc->bDeviceSubClass))
401 return 0;
402
403 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) &&
404 (id->bDeviceProtocol != desc->bDeviceProtocol))
405 return 0;
406
407 return 1;
408}
409
410/* returns 0 if no match, 1 if match */
Masahiro Yamada6d8e4332017-06-22 16:35:14 +0900411static int usb_match_one_id_intf(const struct usb_device_descriptor *desc,
412 const struct usb_interface_descriptor *int_desc,
413 const struct usb_device_id *id)
Simon Glassfc03a552015-03-25 12:22:30 -0600414{
415 /* The interface class, subclass, protocol and number should never be
416 * checked for a match if the device class is Vendor Specific,
417 * unless the match record specifies the Vendor ID. */
418 if (desc->bDeviceClass == USB_CLASS_VENDOR_SPEC &&
419 !(id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
420 (id->match_flags & (USB_DEVICE_ID_MATCH_INT_CLASS |
421 USB_DEVICE_ID_MATCH_INT_SUBCLASS |
422 USB_DEVICE_ID_MATCH_INT_PROTOCOL |
423 USB_DEVICE_ID_MATCH_INT_NUMBER)))
424 return 0;
425
426 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) &&
427 (id->bInterfaceClass != int_desc->bInterfaceClass))
428 return 0;
429
430 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) &&
431 (id->bInterfaceSubClass != int_desc->bInterfaceSubClass))
432 return 0;
433
434 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) &&
435 (id->bInterfaceProtocol != int_desc->bInterfaceProtocol))
436 return 0;
437
438 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_NUMBER) &&
439 (id->bInterfaceNumber != int_desc->bInterfaceNumber))
440 return 0;
441
442 return 1;
443}
444
445/* returns 0 if no match, 1 if match */
Masahiro Yamada6d8e4332017-06-22 16:35:14 +0900446static int usb_match_one_id(struct usb_device_descriptor *desc,
447 struct usb_interface_descriptor *int_desc,
448 const struct usb_device_id *id)
Simon Glassfc03a552015-03-25 12:22:30 -0600449{
450 if (!usb_match_device(desc, id))
451 return 0;
452
453 return usb_match_one_id_intf(desc, int_desc, id);
454}
455
456/**
457 * usb_find_and_bind_driver() - Find and bind the right USB driver
458 *
459 * This only looks at certain fields in the descriptor.
460 */
461static int usb_find_and_bind_driver(struct udevice *parent,
462 struct usb_device_descriptor *desc,
463 struct usb_interface_descriptor *iface,
464 int bus_seq, int devnum,
465 struct udevice **devp)
466{
467 struct usb_driver_entry *start, *entry;
468 int n_ents;
469 int ret;
470 char name[30], *str;
471
472 *devp = NULL;
473 debug("%s: Searching for driver\n", __func__);
474 start = ll_entry_start(struct usb_driver_entry, usb_driver_entry);
475 n_ents = ll_entry_count(struct usb_driver_entry, usb_driver_entry);
476 for (entry = start; entry != start + n_ents; entry++) {
477 const struct usb_device_id *id;
478 struct udevice *dev;
479 const struct driver *drv;
480 struct usb_dev_platdata *plat;
481
482 for (id = entry->match; id->match_flags; id++) {
483 if (!usb_match_one_id(desc, iface, id))
484 continue;
485
486 drv = entry->driver;
487 /*
488 * We could pass the descriptor to the driver as
489 * platdata (instead of NULL) and allow its bind()
490 * method to return -ENOENT if it doesn't support this
491 * device. That way we could continue the search to
492 * find another driver. For now this doesn't seem
493 * necesssary, so just bind the first match.
494 */
495 ret = device_bind(parent, drv, drv->name, NULL, -1,
496 &dev);
497 if (ret)
498 goto error;
499 debug("%s: Match found: %s\n", __func__, drv->name);
500 dev->driver_data = id->driver_info;
501 plat = dev_get_parent_platdata(dev);
502 plat->id = *id;
503 *devp = dev;
504 return 0;
505 }
506 }
507
Simon Glassc79173e2015-03-25 12:22:31 -0600508 /* Bind a generic driver so that the device can be used */
509 snprintf(name, sizeof(name), "generic_bus_%x_dev_%x", bus_seq, devnum);
510 str = strdup(name);
511 if (!str)
512 return -ENOMEM;
513 ret = device_bind_driver(parent, "usb_dev_generic_drv", str, devp);
514
Simon Glassfc03a552015-03-25 12:22:30 -0600515error:
516 debug("%s: No match found: %d\n", __func__, ret);
517 return ret;
518}
519
520/**
Simon Glassd43d7e92015-11-08 23:47:56 -0700521 * usb_find_child() - Find an existing device which matches our needs
522 *
523 *
Simon Glassfc03a552015-03-25 12:22:30 -0600524 */
Simon Glassd43d7e92015-11-08 23:47:56 -0700525static int usb_find_child(struct udevice *parent,
526 struct usb_device_descriptor *desc,
527 struct usb_interface_descriptor *iface,
528 struct udevice **devp)
Simon Glassfc03a552015-03-25 12:22:30 -0600529{
530 struct udevice *dev;
531
532 *devp = NULL;
533 for (device_find_first_child(parent, &dev);
534 dev;
535 device_find_next_child(&dev)) {
536 struct usb_dev_platdata *plat = dev_get_parent_platdata(dev);
537
538 /* If this device is already in use, skip it */
539 if (device_active(dev))
540 continue;
541 debug(" %s: name='%s', plat=%d, desc=%d\n", __func__,
542 dev->name, plat->id.bDeviceClass, desc->bDeviceClass);
543 if (usb_match_one_id(desc, iface, &plat->id)) {
544 *devp = dev;
545 return 0;
546 }
547 }
Simon Glassd43d7e92015-11-08 23:47:56 -0700548
Simon Glassfc03a552015-03-25 12:22:30 -0600549 return -ENOENT;
550}
551
Simon Glass9b82eeb2015-03-25 12:21:59 -0600552int usb_scan_device(struct udevice *parent, int port,
553 enum usb_device_speed speed, struct udevice **devp)
554{
555 struct udevice *dev;
556 bool created = false;
557 struct usb_dev_platdata *plat;
558 struct usb_bus_priv *priv;
559 struct usb_device *parent_udev;
560 int ret;
561 ALLOC_CACHE_ALIGN_BUFFER(struct usb_device, udev, 1);
562 struct usb_interface_descriptor *iface = &udev->config.if_desc[0].desc;
563
564 *devp = NULL;
565 memset(udev, '\0', sizeof(*udev));
Hans de Goede3b5ddd32015-05-05 11:54:31 +0200566 udev->controller_dev = usb_get_bus(parent);
Simon Glass9b82eeb2015-03-25 12:21:59 -0600567 priv = dev_get_uclass_priv(udev->controller_dev);
568
569 /*
570 * Somewhat nasty, this. We create a local device and use the normal
571 * USB stack to read its descriptor. Then we know what type of device
572 * to create for real.
573 *
574 * udev->dev is set to the parent, since we don't have a real device
575 * yet. The USB stack should not access udev.dev anyway, except perhaps
576 * to find the controller, and the controller will either be @parent,
577 * or some parent of @parent.
578 *
579 * Another option might be to create the device as a generic USB
580 * device, then morph it into the correct one when we know what it
581 * should be. This means that a generic USB device would morph into
582 * a network controller, or a USB flash stick, for example. However,
583 * we don't support such morphing and it isn't clear that it would
584 * be easy to do.
585 *
586 * Yet another option is to split out the USB stack parts of udev
587 * into something like a 'struct urb' (as Linux does) which can exist
588 * independently of any device. This feels cleaner, but calls for quite
589 * a big change to the USB stack.
590 *
591 * For now, the approach is to set up an empty udev, read its
592 * descriptor and assign it an address, then bind a real device and
593 * stash the resulting information into the device's parent
594 * platform data. Then when we probe it, usb_child_pre_probe() is called
595 * and it will pull the information out of the stash.
596 */
597 udev->dev = parent;
598 udev->speed = speed;
599 udev->devnum = priv->next_addr + 1;
600 udev->portnr = port;
601 debug("Calling usb_setup_device(), portnr=%d\n", udev->portnr);
602 parent_udev = device_get_uclass_id(parent) == UCLASS_USB_HUB ?
Simon Glassde44acf2015-09-28 23:32:01 -0600603 dev_get_parent_priv(parent) : NULL;
Hans de Goede778dc1c2015-06-17 21:33:46 +0200604 ret = usb_setup_device(udev, priv->desc_before_addr, parent_udev);
Simon Glass9b82eeb2015-03-25 12:21:59 -0600605 debug("read_descriptor for '%s': ret=%d\n", parent->name, ret);
606 if (ret)
607 return ret;
Simon Glassd43d7e92015-11-08 23:47:56 -0700608 ret = usb_find_child(parent, &udev->descriptor, iface, &dev);
609 debug("** usb_find_child returns %d\n", ret);
Simon Glassfc03a552015-03-25 12:22:30 -0600610 if (ret) {
611 if (ret != -ENOENT)
612 return ret;
613 ret = usb_find_and_bind_driver(parent, &udev->descriptor, iface,
614 udev->controller_dev->seq,
615 udev->devnum, &dev);
616 if (ret)
617 return ret;
618 created = true;
619 }
620 plat = dev_get_parent_platdata(dev);
621 debug("%s: Probing '%s', plat=%p\n", __func__, dev->name, plat);
622 plat->devnum = udev->devnum;
Hans de Goede8a0b4c22015-05-05 11:54:32 +0200623 plat->udev = udev;
Simon Glassfc03a552015-03-25 12:22:30 -0600624 priv->next_addr++;
625 ret = device_probe(dev);
626 if (ret) {
627 debug("%s: Device '%s' probe failed\n", __func__, dev->name);
628 priv->next_addr--;
629 if (created)
630 device_unbind(dev);
631 return ret;
632 }
633 *devp = dev;
Simon Glass9b82eeb2015-03-25 12:21:59 -0600634
Simon Glassfc03a552015-03-25 12:22:30 -0600635 return 0;
Simon Glass9b82eeb2015-03-25 12:21:59 -0600636}
637
Simon Glassfbb14942015-05-13 07:02:23 -0600638/*
639 * Detect if a USB device has been plugged or unplugged.
640 */
641int usb_detect_change(void)
642{
643 struct udevice *hub;
644 struct uclass *uc;
645 int change = 0;
646 int ret;
647
648 ret = uclass_get(UCLASS_USB_HUB, &uc);
649 if (ret)
650 return ret;
651
652 uclass_foreach_dev(hub, uc) {
653 struct usb_device *udev;
654 struct udevice *dev;
655
656 if (!device_active(hub))
657 continue;
658 for (device_find_first_child(hub, &dev);
659 dev;
660 device_find_next_child(&dev)) {
661 struct usb_port_status status;
662
663 if (!device_active(dev))
664 continue;
665
Simon Glassde44acf2015-09-28 23:32:01 -0600666 udev = dev_get_parent_priv(dev);
Simon Glassfbb14942015-05-13 07:02:23 -0600667 if (usb_get_port_status(udev, udev->portnr, &status)
668 < 0)
669 /* USB request failed */
670 continue;
671
672 if (le16_to_cpu(status.wPortChange) &
673 USB_PORT_STAT_C_CONNECTION)
674 change++;
675 }
676 }
677
678 return change;
679}
680
Masahiro Yamada6d8e4332017-06-22 16:35:14 +0900681static int usb_child_post_bind(struct udevice *dev)
Simon Glass9b82eeb2015-03-25 12:21:59 -0600682{
683 struct usb_dev_platdata *plat = dev_get_parent_platdata(dev);
Simon Glass9b82eeb2015-03-25 12:21:59 -0600684 int val;
685
Simon Glass6deb8e22017-05-18 20:09:38 -0600686 if (!dev_of_valid(dev))
Simon Glass9b82eeb2015-03-25 12:21:59 -0600687 return 0;
688
689 /* We only support matching a few things */
Simon Glass6deb8e22017-05-18 20:09:38 -0600690 val = dev_read_u32_default(dev, "usb,device-class", -1);
Simon Glass9b82eeb2015-03-25 12:21:59 -0600691 if (val != -1) {
692 plat->id.match_flags |= USB_DEVICE_ID_MATCH_DEV_CLASS;
693 plat->id.bDeviceClass = val;
694 }
Simon Glass6deb8e22017-05-18 20:09:38 -0600695 val = dev_read_u32_default(dev, "usb,interface-class", -1);
Simon Glass9b82eeb2015-03-25 12:21:59 -0600696 if (val != -1) {
697 plat->id.match_flags |= USB_DEVICE_ID_MATCH_INT_CLASS;
698 plat->id.bInterfaceClass = val;
699 }
700
701 return 0;
702}
703
Hans de Goede3b5ddd32015-05-05 11:54:31 +0200704struct udevice *usb_get_bus(struct udevice *dev)
Simon Glass9b82eeb2015-03-25 12:21:59 -0600705{
706 struct udevice *bus;
707
Simon Glass9b82eeb2015-03-25 12:21:59 -0600708 for (bus = dev; bus && device_get_uclass_id(bus) != UCLASS_USB; )
709 bus = bus->parent;
710 if (!bus) {
711 /* By design this cannot happen */
712 assert(bus);
713 debug("USB HUB '%s' does not have a controller\n", dev->name);
Simon Glass9b82eeb2015-03-25 12:21:59 -0600714 }
Simon Glass9b82eeb2015-03-25 12:21:59 -0600715
Hans de Goede3b5ddd32015-05-05 11:54:31 +0200716 return bus;
Simon Glass9b82eeb2015-03-25 12:21:59 -0600717}
718
719int usb_child_pre_probe(struct udevice *dev)
720{
Simon Glassde44acf2015-09-28 23:32:01 -0600721 struct usb_device *udev = dev_get_parent_priv(dev);
Simon Glass9b82eeb2015-03-25 12:21:59 -0600722 struct usb_dev_platdata *plat = dev_get_parent_platdata(dev);
723 int ret;
724
Hans de Goede8a0b4c22015-05-05 11:54:32 +0200725 if (plat->udev) {
726 /*
727 * Copy over all the values set in the on stack struct
728 * usb_device in usb_scan_device() to our final struct
729 * usb_device for this dev.
730 */
731 *udev = *(plat->udev);
732 /* And clear plat->udev as it will not be valid for long */
733 plat->udev = NULL;
734 udev->dev = dev;
735 } else {
736 /*
737 * This happens with devices which are explicitly bound
738 * instead of being discovered through usb_scan_device()
739 * such as sandbox emul devices.
740 */
741 udev->dev = dev;
742 udev->controller_dev = usb_get_bus(dev);
743 udev->devnum = plat->devnum;
Simon Glass9b82eeb2015-03-25 12:21:59 -0600744
Hans de Goede8a0b4c22015-05-05 11:54:32 +0200745 /*
746 * udev did not go through usb_scan_device(), so we need to
747 * select the config and read the config descriptors.
748 */
749 ret = usb_select_config(udev);
750 if (ret)
751 return ret;
752 }
Simon Glass9b82eeb2015-03-25 12:21:59 -0600753
754 return 0;
755}
756
757UCLASS_DRIVER(usb) = {
758 .id = UCLASS_USB,
759 .name = "usb",
760 .flags = DM_UC_FLAG_SEQ_ALIAS,
Simon Glass18230342016-07-05 17:10:10 -0600761 .post_bind = dm_scan_fdt_dev,
Hans de Goedecebc5b72015-05-10 14:10:21 +0200762 .priv_auto_alloc_size = sizeof(struct usb_uclass_priv),
Simon Glass9b82eeb2015-03-25 12:21:59 -0600763 .per_child_auto_alloc_size = sizeof(struct usb_device),
764 .per_device_auto_alloc_size = sizeof(struct usb_bus_priv),
765 .child_post_bind = usb_child_post_bind,
766 .child_pre_probe = usb_child_pre_probe,
767 .per_child_platdata_auto_alloc_size = sizeof(struct usb_dev_platdata),
768};
Simon Glassc79173e2015-03-25 12:22:31 -0600769
770UCLASS_DRIVER(usb_dev_generic) = {
771 .id = UCLASS_USB_DEV_GENERIC,
772 .name = "usb_dev_generic",
773};
774
775U_BOOT_DRIVER(usb_dev_generic_drv) = {
776 .id = UCLASS_USB_DEV_GENERIC,
777 .name = "usb_dev_generic_drv",
778};