blob: 10d47127b851b81d0a7f8a01063ea3c0cefec9bc [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
Hans de Goedee2536372015-05-10 14:10:21 +020024struct usb_uclass_priv {
25 int companion_device_count;
26};
27
Simon Glassde312132015-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 Goedee2536372015-05-10 14:10:21 +020053 struct usb_uclass_priv *uc_priv = bus->uclass->priv;
54 int err;
Simon Glassde312132015-03-25 12:21:59 -060055
56 if (!ops->control)
57 return -ENOSYS;
58
Hans de Goedee2536372015-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 Glassde312132015-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 Goede8a5f0662015-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 Glassde312132015-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
131int usb_stop(void)
132{
133 struct udevice *bus;
134 struct uclass *uc;
Hans de Goedee2536372015-05-10 14:10:21 +0200135 struct usb_uclass_priv *uc_priv;
Simon Glassde312132015-03-25 12:21:59 -0600136 int err = 0, ret;
137
138 /* De-activate any devices that have been activated */
139 ret = uclass_get(UCLASS_USB, &uc);
140 if (ret)
141 return ret;
Hans de Goedee2536372015-05-10 14:10:21 +0200142
143 uc_priv = uc->priv;
144
Simon Glassde312132015-03-25 12:21:59 -0600145 uclass_foreach_dev(bus, uc) {
146 ret = device_remove(bus);
147 if (ret && !err)
148 err = ret;
149 }
150
Simon Glass095fdef2015-03-25 12:22:38 -0600151#ifdef CONFIG_SANDBOX
152 struct udevice *dev;
153
154 /* Reset all enulation devices */
155 ret = uclass_get(UCLASS_USB_EMUL, &uc);
156 if (ret)
157 return ret;
158
159 uclass_foreach_dev(dev, uc)
160 usb_emul_reset(dev);
161#endif
Simon Glassde312132015-03-25 12:21:59 -0600162 usb_stor_reset();
163 usb_hub_reset();
Hans de Goedee2536372015-05-10 14:10:21 +0200164 uc_priv->companion_device_count = 0;
Simon Glassde312132015-03-25 12:21:59 -0600165 usb_started = 0;
166
167 return err;
168}
169
Hans de Goedea24a0e92015-05-10 14:10:19 +0200170static void usb_scan_bus(struct udevice *bus, bool recurse)
Simon Glassde312132015-03-25 12:21:59 -0600171{
172 struct usb_bus_priv *priv;
173 struct udevice *dev;
174 int ret;
175
176 priv = dev_get_uclass_priv(bus);
177
178 assert(recurse); /* TODO: Support non-recusive */
179
Hans de Goedea24a0e92015-05-10 14:10:19 +0200180 printf("scanning bus %d for devices... ", bus->seq);
181 debug("\n");
Simon Glassde312132015-03-25 12:21:59 -0600182 ret = usb_scan_device(bus, 0, USB_SPEED_FULL, &dev);
183 if (ret)
Hans de Goedea24a0e92015-05-10 14:10:19 +0200184 printf("failed, error %d\n", ret);
185 else if (priv->next_addr == 0)
186 printf("No USB Device found\n");
187 else
188 printf("%d USB Device(s) found\n", priv->next_addr);
Simon Glassde312132015-03-25 12:21:59 -0600189}
190
191int usb_init(void)
192{
193 int controllers_initialized = 0;
Hans de Goedee2536372015-05-10 14:10:21 +0200194 struct usb_uclass_priv *uc_priv;
Hans de Goedeb6de4d12015-05-10 14:10:20 +0200195 struct usb_bus_priv *priv;
Simon Glassde312132015-03-25 12:21:59 -0600196 struct udevice *bus;
197 struct uclass *uc;
198 int count = 0;
199 int ret;
200
201 asynch_allowed = 1;
202 usb_hub_reset();
203
204 ret = uclass_get(UCLASS_USB, &uc);
205 if (ret)
206 return ret;
207
Hans de Goedee2536372015-05-10 14:10:21 +0200208 uc_priv = uc->priv;
209
Simon Glassde312132015-03-25 12:21:59 -0600210 uclass_foreach_dev(bus, uc) {
211 /* init low_level USB */
Hans de Goede134692a2015-05-04 21:33:26 +0200212 printf("USB%d: ", count);
Simon Glassde312132015-03-25 12:21:59 -0600213 count++;
Simon Glassde312132015-03-25 12:21:59 -0600214 ret = device_probe(bus);
215 if (ret == -ENODEV) { /* No such device. */
216 puts("Port not available.\n");
217 controllers_initialized++;
218 continue;
219 }
220
221 if (ret) { /* Other error. */
222 printf("probe failed, error %d\n", ret);
223 continue;
224 }
Simon Glassde312132015-03-25 12:21:59 -0600225 controllers_initialized++;
Simon Glassde312132015-03-25 12:21:59 -0600226 usb_started = true;
227 }
228
Hans de Goedeb6de4d12015-05-10 14:10:20 +0200229 /*
230 * lowlevel init done, now scan the bus for devices i.e. search HUBs
231 * and configure them, first scan primary controllers.
232 */
233 uclass_foreach_dev(bus, uc) {
234 if (!device_active(bus))
235 continue;
236
237 priv = dev_get_uclass_priv(bus);
238 if (!priv->companion)
239 usb_scan_bus(bus, true);
240 }
241
242 /*
243 * Now that the primary controllers have been scanned and have handed
244 * over any devices they do not understand to their companions, scan
Hans de Goedee2536372015-05-10 14:10:21 +0200245 * the companions if necessary.
Hans de Goedeb6de4d12015-05-10 14:10:20 +0200246 */
Hans de Goedee2536372015-05-10 14:10:21 +0200247 if (uc_priv->companion_device_count) {
248 uclass_foreach_dev(bus, uc) {
249 if (!device_active(bus))
250 continue;
Hans de Goedeb6de4d12015-05-10 14:10:20 +0200251
Hans de Goedee2536372015-05-10 14:10:21 +0200252 priv = dev_get_uclass_priv(bus);
253 if (priv->companion)
254 usb_scan_bus(bus, true);
255 }
Hans de Goedeb6de4d12015-05-10 14:10:20 +0200256 }
257
Simon Glassde312132015-03-25 12:21:59 -0600258 debug("scan end\n");
259 /* if we were not able to find at least one working bus, bail out */
260 if (!count)
261 printf("No controllers found\n");
262 else if (controllers_initialized == 0)
263 printf("USB error: all controllers failed lowlevel init\n");
264
265 return usb_started ? 0 : -1;
266}
267
268int usb_reset_root_port(void)
269{
270 return -ENOSYS;
271}
272
273static struct usb_device *find_child_devnum(struct udevice *parent, int devnum)
274{
275 struct usb_device *udev;
276 struct udevice *dev;
277
278 if (!device_active(parent))
279 return NULL;
280 udev = dev_get_parentdata(parent);
281 if (udev->devnum == devnum)
282 return udev;
283
284 for (device_find_first_child(parent, &dev);
285 dev;
286 device_find_next_child(&dev)) {
287 udev = find_child_devnum(dev, devnum);
288 if (udev)
289 return udev;
290 }
291
292 return NULL;
293}
294
295struct usb_device *usb_get_dev_index(struct udevice *bus, int index)
296{
297 struct udevice *hub;
298 int devnum = index + 1; /* Addresses are allocated from 1 on USB */
299
300 device_find_first_child(bus, &hub);
301 if (device_get_uclass_id(hub) == UCLASS_USB_HUB)
302 return find_child_devnum(hub, devnum);
303
304 return NULL;
305}
306
307int usb_post_bind(struct udevice *dev)
308{
309 /* Scan the bus for devices */
310 return dm_scan_fdt_node(dev, gd->fdt_blob, dev->of_offset, false);
311}
312
Simon Glassfbeceb22015-03-25 12:22:32 -0600313int usb_setup_ehci_gadget(struct ehci_ctrl **ctlrp)
314{
315 struct usb_platdata *plat;
316 struct udevice *dev;
317 int ret;
318
319 /* Find the old device and remove it */
320 ret = uclass_find_device_by_seq(UCLASS_USB, 0, true, &dev);
321 if (ret)
322 return ret;
323 ret = device_remove(dev);
324 if (ret)
325 return ret;
326
327 plat = dev_get_platdata(dev);
328 plat->init_type = USB_INIT_DEVICE;
329 ret = device_probe(dev);
330 if (ret)
331 return ret;
332 *ctlrp = dev_get_priv(dev);
333
334 return 0;
335}
336
Simon Glass0566e242015-03-25 12:22:30 -0600337/* returns 0 if no match, 1 if match */
338int usb_match_device(const struct usb_device_descriptor *desc,
339 const struct usb_device_id *id)
340{
341 if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
342 id->idVendor != le16_to_cpu(desc->idVendor))
343 return 0;
344
345 if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
346 id->idProduct != le16_to_cpu(desc->idProduct))
347 return 0;
348
349 /* No need to test id->bcdDevice_lo != 0, since 0 is never
350 greater than any unsigned number. */
351 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) &&
352 (id->bcdDevice_lo > le16_to_cpu(desc->bcdDevice)))
353 return 0;
354
355 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) &&
356 (id->bcdDevice_hi < le16_to_cpu(desc->bcdDevice)))
357 return 0;
358
359 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) &&
360 (id->bDeviceClass != desc->bDeviceClass))
361 return 0;
362
363 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) &&
364 (id->bDeviceSubClass != desc->bDeviceSubClass))
365 return 0;
366
367 if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) &&
368 (id->bDeviceProtocol != desc->bDeviceProtocol))
369 return 0;
370
371 return 1;
372}
373
374/* returns 0 if no match, 1 if match */
375int usb_match_one_id_intf(const struct usb_device_descriptor *desc,
376 const struct usb_interface_descriptor *int_desc,
377 const struct usb_device_id *id)
378{
379 /* The interface class, subclass, protocol and number should never be
380 * checked for a match if the device class is Vendor Specific,
381 * unless the match record specifies the Vendor ID. */
382 if (desc->bDeviceClass == USB_CLASS_VENDOR_SPEC &&
383 !(id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
384 (id->match_flags & (USB_DEVICE_ID_MATCH_INT_CLASS |
385 USB_DEVICE_ID_MATCH_INT_SUBCLASS |
386 USB_DEVICE_ID_MATCH_INT_PROTOCOL |
387 USB_DEVICE_ID_MATCH_INT_NUMBER)))
388 return 0;
389
390 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) &&
391 (id->bInterfaceClass != int_desc->bInterfaceClass))
392 return 0;
393
394 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) &&
395 (id->bInterfaceSubClass != int_desc->bInterfaceSubClass))
396 return 0;
397
398 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) &&
399 (id->bInterfaceProtocol != int_desc->bInterfaceProtocol))
400 return 0;
401
402 if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_NUMBER) &&
403 (id->bInterfaceNumber != int_desc->bInterfaceNumber))
404 return 0;
405
406 return 1;
407}
408
409/* returns 0 if no match, 1 if match */
410int usb_match_one_id(struct usb_device_descriptor *desc,
411 struct usb_interface_descriptor *int_desc,
412 const struct usb_device_id *id)
413{
414 if (!usb_match_device(desc, id))
415 return 0;
416
417 return usb_match_one_id_intf(desc, int_desc, id);
418}
419
420/**
421 * usb_find_and_bind_driver() - Find and bind the right USB driver
422 *
423 * This only looks at certain fields in the descriptor.
424 */
425static int usb_find_and_bind_driver(struct udevice *parent,
426 struct usb_device_descriptor *desc,
427 struct usb_interface_descriptor *iface,
428 int bus_seq, int devnum,
429 struct udevice **devp)
430{
431 struct usb_driver_entry *start, *entry;
432 int n_ents;
433 int ret;
434 char name[30], *str;
435
436 *devp = NULL;
437 debug("%s: Searching for driver\n", __func__);
438 start = ll_entry_start(struct usb_driver_entry, usb_driver_entry);
439 n_ents = ll_entry_count(struct usb_driver_entry, usb_driver_entry);
440 for (entry = start; entry != start + n_ents; entry++) {
441 const struct usb_device_id *id;
442 struct udevice *dev;
443 const struct driver *drv;
444 struct usb_dev_platdata *plat;
445
446 for (id = entry->match; id->match_flags; id++) {
447 if (!usb_match_one_id(desc, iface, id))
448 continue;
449
450 drv = entry->driver;
451 /*
452 * We could pass the descriptor to the driver as
453 * platdata (instead of NULL) and allow its bind()
454 * method to return -ENOENT if it doesn't support this
455 * device. That way we could continue the search to
456 * find another driver. For now this doesn't seem
457 * necesssary, so just bind the first match.
458 */
459 ret = device_bind(parent, drv, drv->name, NULL, -1,
460 &dev);
461 if (ret)
462 goto error;
463 debug("%s: Match found: %s\n", __func__, drv->name);
464 dev->driver_data = id->driver_info;
465 plat = dev_get_parent_platdata(dev);
466 plat->id = *id;
467 *devp = dev;
468 return 0;
469 }
470 }
471
Simon Glass449230f2015-03-25 12:22:31 -0600472 /* Bind a generic driver so that the device can be used */
473 snprintf(name, sizeof(name), "generic_bus_%x_dev_%x", bus_seq, devnum);
474 str = strdup(name);
475 if (!str)
476 return -ENOMEM;
477 ret = device_bind_driver(parent, "usb_dev_generic_drv", str, devp);
478
Simon Glass0566e242015-03-25 12:22:30 -0600479error:
480 debug("%s: No match found: %d\n", __func__, ret);
481 return ret;
482}
483
484/**
485 * usb_find_child() - Find an existing device which matches our needs
486 *
487 *
488 */
489static int usb_find_child(struct udevice *parent,
490 struct usb_device_descriptor *desc,
491 struct usb_interface_descriptor *iface,
492 struct udevice **devp)
493{
494 struct udevice *dev;
495
496 *devp = NULL;
497 for (device_find_first_child(parent, &dev);
498 dev;
499 device_find_next_child(&dev)) {
500 struct usb_dev_platdata *plat = dev_get_parent_platdata(dev);
501
502 /* If this device is already in use, skip it */
503 if (device_active(dev))
504 continue;
505 debug(" %s: name='%s', plat=%d, desc=%d\n", __func__,
506 dev->name, plat->id.bDeviceClass, desc->bDeviceClass);
507 if (usb_match_one_id(desc, iface, &plat->id)) {
508 *devp = dev;
509 return 0;
510 }
511 }
512
513 return -ENOENT;
514}
515
Simon Glassde312132015-03-25 12:21:59 -0600516int usb_scan_device(struct udevice *parent, int port,
517 enum usb_device_speed speed, struct udevice **devp)
518{
519 struct udevice *dev;
520 bool created = false;
521 struct usb_dev_platdata *plat;
522 struct usb_bus_priv *priv;
523 struct usb_device *parent_udev;
524 int ret;
525 ALLOC_CACHE_ALIGN_BUFFER(struct usb_device, udev, 1);
526 struct usb_interface_descriptor *iface = &udev->config.if_desc[0].desc;
527
528 *devp = NULL;
529 memset(udev, '\0', sizeof(*udev));
Hans de Goedef78a5c02015-05-05 11:54:31 +0200530 udev->controller_dev = usb_get_bus(parent);
Simon Glassde312132015-03-25 12:21:59 -0600531 priv = dev_get_uclass_priv(udev->controller_dev);
532
533 /*
534 * Somewhat nasty, this. We create a local device and use the normal
535 * USB stack to read its descriptor. Then we know what type of device
536 * to create for real.
537 *
538 * udev->dev is set to the parent, since we don't have a real device
539 * yet. The USB stack should not access udev.dev anyway, except perhaps
540 * to find the controller, and the controller will either be @parent,
541 * or some parent of @parent.
542 *
543 * Another option might be to create the device as a generic USB
544 * device, then morph it into the correct one when we know what it
545 * should be. This means that a generic USB device would morph into
546 * a network controller, or a USB flash stick, for example. However,
547 * we don't support such morphing and it isn't clear that it would
548 * be easy to do.
549 *
550 * Yet another option is to split out the USB stack parts of udev
551 * into something like a 'struct urb' (as Linux does) which can exist
552 * independently of any device. This feels cleaner, but calls for quite
553 * a big change to the USB stack.
554 *
555 * For now, the approach is to set up an empty udev, read its
556 * descriptor and assign it an address, then bind a real device and
557 * stash the resulting information into the device's parent
558 * platform data. Then when we probe it, usb_child_pre_probe() is called
559 * and it will pull the information out of the stash.
560 */
561 udev->dev = parent;
562 udev->speed = speed;
563 udev->devnum = priv->next_addr + 1;
564 udev->portnr = port;
565 debug("Calling usb_setup_device(), portnr=%d\n", udev->portnr);
566 parent_udev = device_get_uclass_id(parent) == UCLASS_USB_HUB ?
567 dev_get_parentdata(parent) : NULL;
568 ret = usb_setup_device(udev, priv->desc_before_addr, parent_udev, port);
569 debug("read_descriptor for '%s': ret=%d\n", parent->name, ret);
570 if (ret)
571 return ret;
572 ret = usb_find_child(parent, &udev->descriptor, iface, &dev);
573 debug("** usb_find_child returns %d\n", ret);
Simon Glass0566e242015-03-25 12:22:30 -0600574 if (ret) {
575 if (ret != -ENOENT)
576 return ret;
577 ret = usb_find_and_bind_driver(parent, &udev->descriptor, iface,
578 udev->controller_dev->seq,
579 udev->devnum, &dev);
580 if (ret)
581 return ret;
582 created = true;
583 }
584 plat = dev_get_parent_platdata(dev);
585 debug("%s: Probing '%s', plat=%p\n", __func__, dev->name, plat);
586 plat->devnum = udev->devnum;
Hans de Goede7f1a0752015-05-05 11:54:32 +0200587 plat->udev = udev;
Simon Glass0566e242015-03-25 12:22:30 -0600588 priv->next_addr++;
589 ret = device_probe(dev);
590 if (ret) {
591 debug("%s: Device '%s' probe failed\n", __func__, dev->name);
592 priv->next_addr--;
593 if (created)
594 device_unbind(dev);
595 return ret;
596 }
597 *devp = dev;
Simon Glassde312132015-03-25 12:21:59 -0600598
Simon Glass0566e242015-03-25 12:22:30 -0600599 return 0;
Simon Glassde312132015-03-25 12:21:59 -0600600}
601
Simon Glass0c5dd9a2015-05-13 07:02:23 -0600602/*
603 * Detect if a USB device has been plugged or unplugged.
604 */
605int usb_detect_change(void)
606{
607 struct udevice *hub;
608 struct uclass *uc;
609 int change = 0;
610 int ret;
611
612 ret = uclass_get(UCLASS_USB_HUB, &uc);
613 if (ret)
614 return ret;
615
616 uclass_foreach_dev(hub, uc) {
617 struct usb_device *udev;
618 struct udevice *dev;
619
620 if (!device_active(hub))
621 continue;
622 for (device_find_first_child(hub, &dev);
623 dev;
624 device_find_next_child(&dev)) {
625 struct usb_port_status status;
626
627 if (!device_active(dev))
628 continue;
629
630 udev = dev_get_parentdata(dev);
631 if (usb_get_port_status(udev, udev->portnr, &status)
632 < 0)
633 /* USB request failed */
634 continue;
635
636 if (le16_to_cpu(status.wPortChange) &
637 USB_PORT_STAT_C_CONNECTION)
638 change++;
639 }
640 }
641
642 return change;
643}
644
Simon Glassde312132015-03-25 12:21:59 -0600645int usb_child_post_bind(struct udevice *dev)
646{
647 struct usb_dev_platdata *plat = dev_get_parent_platdata(dev);
648 const void *blob = gd->fdt_blob;
649 int val;
650
651 if (dev->of_offset == -1)
652 return 0;
653
654 /* We only support matching a few things */
655 val = fdtdec_get_int(blob, dev->of_offset, "usb,device-class", -1);
656 if (val != -1) {
657 plat->id.match_flags |= USB_DEVICE_ID_MATCH_DEV_CLASS;
658 plat->id.bDeviceClass = val;
659 }
660 val = fdtdec_get_int(blob, dev->of_offset, "usb,interface-class", -1);
661 if (val != -1) {
662 plat->id.match_flags |= USB_DEVICE_ID_MATCH_INT_CLASS;
663 plat->id.bInterfaceClass = val;
664 }
665
666 return 0;
667}
668
Hans de Goedef78a5c02015-05-05 11:54:31 +0200669struct udevice *usb_get_bus(struct udevice *dev)
Simon Glassde312132015-03-25 12:21:59 -0600670{
671 struct udevice *bus;
672
Simon Glassde312132015-03-25 12:21:59 -0600673 for (bus = dev; bus && device_get_uclass_id(bus) != UCLASS_USB; )
674 bus = bus->parent;
675 if (!bus) {
676 /* By design this cannot happen */
677 assert(bus);
678 debug("USB HUB '%s' does not have a controller\n", dev->name);
Simon Glassde312132015-03-25 12:21:59 -0600679 }
Simon Glassde312132015-03-25 12:21:59 -0600680
Hans de Goedef78a5c02015-05-05 11:54:31 +0200681 return bus;
Simon Glassde312132015-03-25 12:21:59 -0600682}
683
684int usb_child_pre_probe(struct udevice *dev)
685{
Simon Glassde312132015-03-25 12:21:59 -0600686 struct usb_device *udev = dev_get_parentdata(dev);
687 struct usb_dev_platdata *plat = dev_get_parent_platdata(dev);
688 int ret;
689
Hans de Goede7f1a0752015-05-05 11:54:32 +0200690 if (plat->udev) {
691 /*
692 * Copy over all the values set in the on stack struct
693 * usb_device in usb_scan_device() to our final struct
694 * usb_device for this dev.
695 */
696 *udev = *(plat->udev);
697 /* And clear plat->udev as it will not be valid for long */
698 plat->udev = NULL;
699 udev->dev = dev;
700 } else {
701 /*
702 * This happens with devices which are explicitly bound
703 * instead of being discovered through usb_scan_device()
704 * such as sandbox emul devices.
705 */
706 udev->dev = dev;
707 udev->controller_dev = usb_get_bus(dev);
708 udev->devnum = plat->devnum;
Simon Glassde312132015-03-25 12:21:59 -0600709
Hans de Goede7f1a0752015-05-05 11:54:32 +0200710 /*
711 * udev did not go through usb_scan_device(), so we need to
712 * select the config and read the config descriptors.
713 */
714 ret = usb_select_config(udev);
715 if (ret)
716 return ret;
717 }
Simon Glassde312132015-03-25 12:21:59 -0600718
719 return 0;
720}
721
722UCLASS_DRIVER(usb) = {
723 .id = UCLASS_USB,
724 .name = "usb",
725 .flags = DM_UC_FLAG_SEQ_ALIAS,
726 .post_bind = usb_post_bind,
Hans de Goedee2536372015-05-10 14:10:21 +0200727 .priv_auto_alloc_size = sizeof(struct usb_uclass_priv),
Simon Glassde312132015-03-25 12:21:59 -0600728 .per_child_auto_alloc_size = sizeof(struct usb_device),
729 .per_device_auto_alloc_size = sizeof(struct usb_bus_priv),
730 .child_post_bind = usb_child_post_bind,
731 .child_pre_probe = usb_child_pre_probe,
732 .per_child_platdata_auto_alloc_size = sizeof(struct usb_dev_platdata),
733};
Simon Glass449230f2015-03-25 12:22:31 -0600734
735UCLASS_DRIVER(usb_dev_generic) = {
736 .id = UCLASS_USB_DEV_GENERIC,
737 .name = "usb_dev_generic",
738};
739
740U_BOOT_DRIVER(usb_dev_generic_drv) = {
741 .id = UCLASS_USB_DEV_GENERIC,
742 .name = "usb_dev_generic_drv",
743};