• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2015 Google, Inc
4  * Written by Simon Glass <sjg@chromium.org>
5  *
6  * usb_match_device() modified from Linux kernel v4.0.
7  */
8 
9 #include <common.h>
10 #include <dm.h>
11 #include <errno.h>
12 #include <memalign.h>
13 #include <usb.h>
14 #include <dm/device-internal.h>
15 #include <dm/lists.h>
16 #include <dm/uclass-internal.h>
17 
18 extern bool usb_started; /* flag for the started/stopped USB status */
19 static bool asynch_allowed;
20 
21 struct usb_uclass_priv {
22 	int companion_device_count;
23 };
24 
usb_disable_asynch(int disable)25 int usb_disable_asynch(int disable)
26 {
27 	int old_value = asynch_allowed;
28 
29 	asynch_allowed = !disable;
30 	return old_value;
31 }
32 
submit_int_msg(struct usb_device * udev,unsigned long pipe,void * buffer,int length,int interval)33 int submit_int_msg(struct usb_device *udev, unsigned long pipe, void *buffer,
34 		   int length, int interval)
35 {
36 	struct udevice *bus = udev->controller_dev;
37 	struct dm_usb_ops *ops = usb_get_ops(bus);
38 
39 	if (!ops->interrupt)
40 		return -ENOSYS;
41 
42 	return ops->interrupt(bus, udev, pipe, buffer, length, interval);
43 }
44 
submit_control_msg(struct usb_device * udev,unsigned long pipe,void * buffer,int length,struct devrequest * setup)45 int submit_control_msg(struct usb_device *udev, unsigned long pipe,
46 		       void *buffer, int length, struct devrequest *setup)
47 {
48 	struct udevice *bus = udev->controller_dev;
49 	struct dm_usb_ops *ops = usb_get_ops(bus);
50 	struct usb_uclass_priv *uc_priv = bus->uclass->priv;
51 	int err;
52 
53 	if (!ops->control)
54 		return -ENOSYS;
55 
56 	err = ops->control(bus, udev, pipe, buffer, length, setup);
57 	if (setup->request == USB_REQ_SET_FEATURE &&
58 	    setup->requesttype == USB_RT_PORT &&
59 	    setup->value == cpu_to_le16(USB_PORT_FEAT_RESET) &&
60 	    err == -ENXIO) {
61 		/* Device handed over to companion after port reset */
62 		uc_priv->companion_device_count++;
63 	}
64 
65 	return err;
66 }
67 
submit_bulk_msg(struct usb_device * udev,unsigned long pipe,void * buffer,int length)68 int submit_bulk_msg(struct usb_device *udev, unsigned long pipe, void *buffer,
69 		    int length)
70 {
71 	struct udevice *bus = udev->controller_dev;
72 	struct dm_usb_ops *ops = usb_get_ops(bus);
73 
74 	if (!ops->bulk)
75 		return -ENOSYS;
76 
77 	return ops->bulk(bus, udev, pipe, buffer, length);
78 }
79 
create_int_queue(struct usb_device * udev,unsigned long pipe,int queuesize,int elementsize,void * buffer,int interval)80 struct int_queue *create_int_queue(struct usb_device *udev,
81 		unsigned long pipe, int queuesize, int elementsize,
82 		void *buffer, int interval)
83 {
84 	struct udevice *bus = udev->controller_dev;
85 	struct dm_usb_ops *ops = usb_get_ops(bus);
86 
87 	if (!ops->create_int_queue)
88 		return NULL;
89 
90 	return ops->create_int_queue(bus, udev, pipe, queuesize, elementsize,
91 				     buffer, interval);
92 }
93 
poll_int_queue(struct usb_device * udev,struct int_queue * queue)94 void *poll_int_queue(struct usb_device *udev, struct int_queue *queue)
95 {
96 	struct udevice *bus = udev->controller_dev;
97 	struct dm_usb_ops *ops = usb_get_ops(bus);
98 
99 	if (!ops->poll_int_queue)
100 		return NULL;
101 
102 	return ops->poll_int_queue(bus, udev, queue);
103 }
104 
destroy_int_queue(struct usb_device * udev,struct int_queue * queue)105 int destroy_int_queue(struct usb_device *udev, struct int_queue *queue)
106 {
107 	struct udevice *bus = udev->controller_dev;
108 	struct dm_usb_ops *ops = usb_get_ops(bus);
109 
110 	if (!ops->destroy_int_queue)
111 		return -ENOSYS;
112 
113 	return ops->destroy_int_queue(bus, udev, queue);
114 }
115 
usb_alloc_device(struct usb_device * udev)116 int usb_alloc_device(struct usb_device *udev)
117 {
118 	struct udevice *bus = udev->controller_dev;
119 	struct dm_usb_ops *ops = usb_get_ops(bus);
120 
121 	/* This is only requird by some controllers - current XHCI */
122 	if (!ops->alloc_device)
123 		return 0;
124 
125 	return ops->alloc_device(bus, udev);
126 }
127 
usb_reset_root_port(struct usb_device * udev)128 int usb_reset_root_port(struct usb_device *udev)
129 {
130 	struct udevice *bus = udev->controller_dev;
131 	struct dm_usb_ops *ops = usb_get_ops(bus);
132 
133 	if (!ops->reset_root_port)
134 		return -ENOSYS;
135 
136 	return ops->reset_root_port(bus, udev);
137 }
138 
usb_update_hub_device(struct usb_device * udev)139 int usb_update_hub_device(struct usb_device *udev)
140 {
141 	struct udevice *bus = udev->controller_dev;
142 	struct dm_usb_ops *ops = usb_get_ops(bus);
143 
144 	if (!ops->update_hub_device)
145 		return -ENOSYS;
146 
147 	return ops->update_hub_device(bus, udev);
148 }
149 
usb_get_max_xfer_size(struct usb_device * udev,size_t * size)150 int usb_get_max_xfer_size(struct usb_device *udev, size_t *size)
151 {
152 	struct udevice *bus = udev->controller_dev;
153 	struct dm_usb_ops *ops = usb_get_ops(bus);
154 
155 	if (!ops->get_max_xfer_size)
156 		return -ENOSYS;
157 
158 	return ops->get_max_xfer_size(bus, size);
159 }
160 
usb_stop(void)161 int usb_stop(void)
162 {
163 	struct udevice *bus;
164 	struct udevice *rh;
165 	struct uclass *uc;
166 	struct usb_uclass_priv *uc_priv;
167 	int err = 0, ret;
168 
169 	/* De-activate any devices that have been activated */
170 	ret = uclass_get(UCLASS_USB, &uc);
171 	if (ret)
172 		return ret;
173 
174 	uc_priv = uc->priv;
175 
176 	uclass_foreach_dev(bus, uc) {
177 		ret = device_remove(bus, DM_REMOVE_NORMAL);
178 		if (ret && !err)
179 			err = ret;
180 
181 		/* Locate root hub device */
182 		device_find_first_child(bus, &rh);
183 		if (rh) {
184 			/*
185 			 * All USB devices are children of root hub.
186 			 * Unbinding root hub will unbind all of its children.
187 			 */
188 			ret = device_unbind(rh);
189 			if (ret && !err)
190 				err = ret;
191 		}
192 	}
193 
194 #ifdef CONFIG_USB_STORAGE
195 	usb_stor_reset();
196 #endif
197 	uc_priv->companion_device_count = 0;
198 	usb_started = 0;
199 
200 	return err;
201 }
202 
usb_scan_bus(struct udevice * bus,bool recurse)203 static void usb_scan_bus(struct udevice *bus, bool recurse)
204 {
205 	struct usb_bus_priv *priv;
206 	struct udevice *dev;
207 	int ret;
208 
209 	priv = dev_get_uclass_priv(bus);
210 
211 	assert(recurse);	/* TODO: Support non-recusive */
212 
213 	printf("scanning bus %d for devices... ", bus->seq);
214 	debug("\n");
215 	ret = usb_scan_device(bus, 0, USB_SPEED_FULL, &dev);
216 	if (ret)
217 		printf("failed, error %d\n", ret);
218 	else if (priv->next_addr == 0)
219 		printf("No USB Device found\n");
220 	else
221 		printf("%d USB Device(s) found\n", priv->next_addr);
222 }
223 
remove_inactive_children(struct uclass * uc,struct udevice * bus)224 static void remove_inactive_children(struct uclass *uc, struct udevice *bus)
225 {
226 	uclass_foreach_dev(bus, uc) {
227 		struct udevice *dev, *next;
228 
229 		if (!device_active(bus))
230 			continue;
231 		device_foreach_child_safe(dev, next, bus) {
232 			if (!device_active(dev))
233 				device_unbind(dev);
234 		}
235 	}
236 }
237 
usb_init(void)238 int usb_init(void)
239 {
240 	int controllers_initialized = 0;
241 	struct usb_uclass_priv *uc_priv;
242 	struct usb_bus_priv *priv;
243 	struct udevice *bus;
244 	struct uclass *uc;
245 	int count = 0;
246 	int ret;
247 
248 	asynch_allowed = 1;
249 
250 	ret = uclass_get(UCLASS_USB, &uc);
251 	if (ret)
252 		return ret;
253 
254 	uc_priv = uc->priv;
255 
256 	uclass_foreach_dev(bus, uc) {
257 		/* init low_level USB */
258 		printf("USB%d:   ", count);
259 		count++;
260 
261 #ifdef CONFIG_SANDBOX
262 		/*
263 		 * For Sandbox, we need scan the device tree each time when we
264 		 * start the USB stack, in order to re-create the emulated USB
265 		 * devices and bind drivers for them before we actually do the
266 		 * driver probe.
267 		 */
268 		ret = dm_scan_fdt_dev(bus);
269 		if (ret) {
270 			printf("Sandbox USB device scan failed (%d)\n", ret);
271 			continue;
272 		}
273 #endif
274 
275 		ret = device_probe(bus);
276 		if (ret == -ENODEV) {	/* No such device. */
277 			puts("Port not available.\n");
278 			controllers_initialized++;
279 			continue;
280 		}
281 
282 		if (ret) {		/* Other error. */
283 			printf("probe failed, error %d\n", ret);
284 			continue;
285 		}
286 		controllers_initialized++;
287 		usb_started = true;
288 	}
289 
290 	/*
291 	 * lowlevel init done, now scan the bus for devices i.e. search HUBs
292 	 * and configure them, first scan primary controllers.
293 	 */
294 	uclass_foreach_dev(bus, uc) {
295 		if (!device_active(bus))
296 			continue;
297 
298 		priv = dev_get_uclass_priv(bus);
299 		if (!priv->companion)
300 			usb_scan_bus(bus, true);
301 	}
302 
303 	/*
304 	 * Now that the primary controllers have been scanned and have handed
305 	 * over any devices they do not understand to their companions, scan
306 	 * the companions if necessary.
307 	 */
308 	if (uc_priv->companion_device_count) {
309 		uclass_foreach_dev(bus, uc) {
310 			if (!device_active(bus))
311 				continue;
312 
313 			priv = dev_get_uclass_priv(bus);
314 			if (priv->companion)
315 				usb_scan_bus(bus, true);
316 		}
317 	}
318 
319 	debug("scan end\n");
320 
321 	/* Remove any devices that were not found on this scan */
322 	remove_inactive_children(uc, bus);
323 
324 	ret = uclass_get(UCLASS_USB_HUB, &uc);
325 	if (ret)
326 		return ret;
327 	remove_inactive_children(uc, bus);
328 
329 	/* if we were not able to find at least one working bus, bail out */
330 	if (!count)
331 		printf("No controllers found\n");
332 	else if (controllers_initialized == 0)
333 		printf("USB error: all controllers failed lowlevel init\n");
334 
335 	return usb_started ? 0 : -1;
336 }
337 
338 /*
339  * TODO(sjg@chromium.org): Remove this legacy function. At present it is needed
340  * to support boards which use driver model for USB but not Ethernet, and want
341  * to use USB Ethernet.
342  *
343  * The #if clause is here to ensure that remains the only case.
344  */
345 #if !defined(CONFIG_DM_ETH) && defined(CONFIG_USB_HOST_ETHER)
find_child_devnum(struct udevice * parent,int devnum)346 static struct usb_device *find_child_devnum(struct udevice *parent, int devnum)
347 {
348 	struct usb_device *udev;
349 	struct udevice *dev;
350 
351 	if (!device_active(parent))
352 		return NULL;
353 	udev = dev_get_parent_priv(parent);
354 	if (udev->devnum == devnum)
355 		return udev;
356 
357 	for (device_find_first_child(parent, &dev);
358 	     dev;
359 	     device_find_next_child(&dev)) {
360 		udev = find_child_devnum(dev, devnum);
361 		if (udev)
362 			return udev;
363 	}
364 
365 	return NULL;
366 }
367 
usb_get_dev_index(struct udevice * bus,int index)368 struct usb_device *usb_get_dev_index(struct udevice *bus, int index)
369 {
370 	struct udevice *dev;
371 	int devnum = index + 1; /* Addresses are allocated from 1 on USB */
372 
373 	device_find_first_child(bus, &dev);
374 	if (!dev)
375 		return NULL;
376 
377 	return find_child_devnum(dev, devnum);
378 }
379 #endif
380 
usb_setup_ehci_gadget(struct ehci_ctrl ** ctlrp)381 int usb_setup_ehci_gadget(struct ehci_ctrl **ctlrp)
382 {
383 	struct usb_platdata *plat;
384 	struct udevice *dev;
385 	int ret;
386 
387 	/* Find the old device and remove it */
388 	ret = uclass_find_device_by_seq(UCLASS_USB, 0, true, &dev);
389 	if (ret)
390 		return ret;
391 	ret = device_remove(dev, DM_REMOVE_NORMAL);
392 	if (ret)
393 		return ret;
394 
395 	plat = dev_get_platdata(dev);
396 	plat->init_type = USB_INIT_DEVICE;
397 	ret = device_probe(dev);
398 	if (ret)
399 		return ret;
400 	*ctlrp = dev_get_priv(dev);
401 
402 	return 0;
403 }
404 
405 /* returns 0 if no match, 1 if match */
usb_match_device(const struct usb_device_descriptor * desc,const struct usb_device_id * id)406 static int usb_match_device(const struct usb_device_descriptor *desc,
407 			    const struct usb_device_id *id)
408 {
409 	if ((id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
410 	    id->idVendor != le16_to_cpu(desc->idVendor))
411 		return 0;
412 
413 	if ((id->match_flags & USB_DEVICE_ID_MATCH_PRODUCT) &&
414 	    id->idProduct != le16_to_cpu(desc->idProduct))
415 		return 0;
416 
417 	/* No need to test id->bcdDevice_lo != 0, since 0 is never
418 	   greater than any unsigned number. */
419 	if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_LO) &&
420 	    (id->bcdDevice_lo > le16_to_cpu(desc->bcdDevice)))
421 		return 0;
422 
423 	if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_HI) &&
424 	    (id->bcdDevice_hi < le16_to_cpu(desc->bcdDevice)))
425 		return 0;
426 
427 	if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_CLASS) &&
428 	    (id->bDeviceClass != desc->bDeviceClass))
429 		return 0;
430 
431 	if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_SUBCLASS) &&
432 	    (id->bDeviceSubClass != desc->bDeviceSubClass))
433 		return 0;
434 
435 	if ((id->match_flags & USB_DEVICE_ID_MATCH_DEV_PROTOCOL) &&
436 	    (id->bDeviceProtocol != desc->bDeviceProtocol))
437 		return 0;
438 
439 	return 1;
440 }
441 
442 /* returns 0 if no match, 1 if match */
usb_match_one_id_intf(const struct usb_device_descriptor * desc,const struct usb_interface_descriptor * int_desc,const struct usb_device_id * id)443 static int usb_match_one_id_intf(const struct usb_device_descriptor *desc,
444 			const struct usb_interface_descriptor *int_desc,
445 			const struct usb_device_id *id)
446 {
447 	/* The interface class, subclass, protocol and number should never be
448 	 * checked for a match if the device class is Vendor Specific,
449 	 * unless the match record specifies the Vendor ID. */
450 	if (desc->bDeviceClass == USB_CLASS_VENDOR_SPEC &&
451 	    !(id->match_flags & USB_DEVICE_ID_MATCH_VENDOR) &&
452 	    (id->match_flags & (USB_DEVICE_ID_MATCH_INT_CLASS |
453 				USB_DEVICE_ID_MATCH_INT_SUBCLASS |
454 				USB_DEVICE_ID_MATCH_INT_PROTOCOL |
455 				USB_DEVICE_ID_MATCH_INT_NUMBER)))
456 		return 0;
457 
458 	if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_CLASS) &&
459 	    (id->bInterfaceClass != int_desc->bInterfaceClass))
460 		return 0;
461 
462 	if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_SUBCLASS) &&
463 	    (id->bInterfaceSubClass != int_desc->bInterfaceSubClass))
464 		return 0;
465 
466 	if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_PROTOCOL) &&
467 	    (id->bInterfaceProtocol != int_desc->bInterfaceProtocol))
468 		return 0;
469 
470 	if ((id->match_flags & USB_DEVICE_ID_MATCH_INT_NUMBER) &&
471 	    (id->bInterfaceNumber != int_desc->bInterfaceNumber))
472 		return 0;
473 
474 	return 1;
475 }
476 
477 /* returns 0 if no match, 1 if match */
usb_match_one_id(struct usb_device_descriptor * desc,struct usb_interface_descriptor * int_desc,const struct usb_device_id * id)478 static int usb_match_one_id(struct usb_device_descriptor *desc,
479 			    struct usb_interface_descriptor *int_desc,
480 			    const struct usb_device_id *id)
481 {
482 	if (!usb_match_device(desc, id))
483 		return 0;
484 
485 	return usb_match_one_id_intf(desc, int_desc, id);
486 }
487 
488 /**
489  * usb_find_and_bind_driver() - Find and bind the right USB driver
490  *
491  * This only looks at certain fields in the descriptor.
492  */
usb_find_and_bind_driver(struct udevice * parent,struct usb_device_descriptor * desc,struct usb_interface_descriptor * iface,int bus_seq,int devnum,struct udevice ** devp)493 static int usb_find_and_bind_driver(struct udevice *parent,
494 				    struct usb_device_descriptor *desc,
495 				    struct usb_interface_descriptor *iface,
496 				    int bus_seq, int devnum,
497 				    struct udevice **devp)
498 {
499 	struct usb_driver_entry *start, *entry;
500 	int n_ents;
501 	int ret;
502 	char name[30], *str;
503 
504 	*devp = NULL;
505 	debug("%s: Searching for driver\n", __func__);
506 	start = ll_entry_start(struct usb_driver_entry, usb_driver_entry);
507 	n_ents = ll_entry_count(struct usb_driver_entry, usb_driver_entry);
508 	for (entry = start; entry != start + n_ents; entry++) {
509 		const struct usb_device_id *id;
510 		struct udevice *dev;
511 		const struct driver *drv;
512 		struct usb_dev_platdata *plat;
513 
514 		for (id = entry->match; id->match_flags; id++) {
515 			if (!usb_match_one_id(desc, iface, id))
516 				continue;
517 
518 			drv = entry->driver;
519 			/*
520 			 * We could pass the descriptor to the driver as
521 			 * platdata (instead of NULL) and allow its bind()
522 			 * method to return -ENOENT if it doesn't support this
523 			 * device. That way we could continue the search to
524 			 * find another driver. For now this doesn't seem
525 			 * necesssary, so just bind the first match.
526 			 */
527 			ret = device_bind(parent, drv, drv->name, NULL, -1,
528 					  &dev);
529 			if (ret)
530 				goto error;
531 			debug("%s: Match found: %s\n", __func__, drv->name);
532 			dev->driver_data = id->driver_info;
533 			plat = dev_get_parent_platdata(dev);
534 			plat->id = *id;
535 			*devp = dev;
536 			return 0;
537 		}
538 	}
539 
540 	/* Bind a generic driver so that the device can be used */
541 	snprintf(name, sizeof(name), "generic_bus_%x_dev_%x", bus_seq, devnum);
542 	str = strdup(name);
543 	if (!str)
544 		return -ENOMEM;
545 	ret = device_bind_driver(parent, "usb_dev_generic_drv", str, devp);
546 
547 error:
548 	debug("%s: No match found: %d\n", __func__, ret);
549 	return ret;
550 }
551 
552 /**
553  * usb_find_child() - Find an existing device which matches our needs
554  *
555  *
556  */
usb_find_child(struct udevice * parent,struct usb_device_descriptor * desc,struct usb_interface_descriptor * iface,struct udevice ** devp)557 static int usb_find_child(struct udevice *parent,
558 			  struct usb_device_descriptor *desc,
559 			  struct usb_interface_descriptor *iface,
560 			  struct udevice **devp)
561 {
562 	struct udevice *dev;
563 
564 	*devp = NULL;
565 	for (device_find_first_child(parent, &dev);
566 	     dev;
567 	     device_find_next_child(&dev)) {
568 		struct usb_dev_platdata *plat = dev_get_parent_platdata(dev);
569 
570 		/* If this device is already in use, skip it */
571 		if (device_active(dev))
572 			continue;
573 		debug("   %s: name='%s', plat=%d, desc=%d\n", __func__,
574 		      dev->name, plat->id.bDeviceClass, desc->bDeviceClass);
575 		if (usb_match_one_id(desc, iface, &plat->id)) {
576 			*devp = dev;
577 			return 0;
578 		}
579 	}
580 
581 	return -ENOENT;
582 }
583 
usb_scan_device(struct udevice * parent,int port,enum usb_device_speed speed,struct udevice ** devp)584 int usb_scan_device(struct udevice *parent, int port,
585 		    enum usb_device_speed speed, struct udevice **devp)
586 {
587 	struct udevice *dev;
588 	bool created = false;
589 	struct usb_dev_platdata *plat;
590 	struct usb_bus_priv *priv;
591 	struct usb_device *parent_udev;
592 	int ret;
593 	ALLOC_CACHE_ALIGN_BUFFER(struct usb_device, udev, 1);
594 	struct usb_interface_descriptor *iface = &udev->config.if_desc[0].desc;
595 
596 	*devp = NULL;
597 	memset(udev, '\0', sizeof(*udev));
598 	udev->controller_dev = usb_get_bus(parent);
599 	priv = dev_get_uclass_priv(udev->controller_dev);
600 
601 	/*
602 	 * Somewhat nasty, this. We create a local device and use the normal
603 	 * USB stack to read its descriptor. Then we know what type of device
604 	 * to create for real.
605 	 *
606 	 * udev->dev is set to the parent, since we don't have a real device
607 	 * yet. The USB stack should not access udev.dev anyway, except perhaps
608 	 * to find the controller, and the controller will either be @parent,
609 	 * or some parent of @parent.
610 	 *
611 	 * Another option might be to create the device as a generic USB
612 	 * device, then morph it into the correct one when we know what it
613 	 * should be. This means that a generic USB device would morph into
614 	 * a network controller, or a USB flash stick, for example. However,
615 	 * we don't support such morphing and it isn't clear that it would
616 	 * be easy to do.
617 	 *
618 	 * Yet another option is to split out the USB stack parts of udev
619 	 * into something like a 'struct urb' (as Linux does) which can exist
620 	 * independently of any device. This feels cleaner, but calls for quite
621 	 * a big change to the USB stack.
622 	 *
623 	 * For now, the approach is to set up an empty udev, read its
624 	 * descriptor and assign it an address, then bind a real device and
625 	 * stash the resulting information into the device's parent
626 	 * platform data. Then when we probe it, usb_child_pre_probe() is called
627 	 * and it will pull the information out of the stash.
628 	 */
629 	udev->dev = parent;
630 	udev->speed = speed;
631 	udev->devnum = priv->next_addr + 1;
632 	udev->portnr = port;
633 	debug("Calling usb_setup_device(), portnr=%d\n", udev->portnr);
634 	parent_udev = device_get_uclass_id(parent) == UCLASS_USB_HUB ?
635 		dev_get_parent_priv(parent) : NULL;
636 	ret = usb_setup_device(udev, priv->desc_before_addr, parent_udev);
637 	debug("read_descriptor for '%s': ret=%d\n", parent->name, ret);
638 	if (ret)
639 		return ret;
640 	ret = usb_find_child(parent, &udev->descriptor, iface, &dev);
641 	debug("** usb_find_child returns %d\n", ret);
642 	if (ret) {
643 		if (ret != -ENOENT)
644 			return ret;
645 		ret = usb_find_and_bind_driver(parent, &udev->descriptor, iface,
646 					       udev->controller_dev->seq,
647 					       udev->devnum, &dev);
648 		if (ret)
649 			return ret;
650 		created = true;
651 	}
652 	plat = dev_get_parent_platdata(dev);
653 	debug("%s: Probing '%s', plat=%p\n", __func__, dev->name, plat);
654 	plat->devnum = udev->devnum;
655 	plat->udev = udev;
656 	priv->next_addr++;
657 	ret = device_probe(dev);
658 	if (ret) {
659 		debug("%s: Device '%s' probe failed\n", __func__, dev->name);
660 		priv->next_addr--;
661 		if (created)
662 			device_unbind(dev);
663 		return ret;
664 	}
665 	*devp = dev;
666 
667 	return 0;
668 }
669 
670 /*
671  * Detect if a USB device has been plugged or unplugged.
672  */
usb_detect_change(void)673 int usb_detect_change(void)
674 {
675 	struct udevice *hub;
676 	struct uclass *uc;
677 	int change = 0;
678 	int ret;
679 
680 	ret = uclass_get(UCLASS_USB_HUB, &uc);
681 	if (ret)
682 		return ret;
683 
684 	uclass_foreach_dev(hub, uc) {
685 		struct usb_device *udev;
686 		struct udevice *dev;
687 
688 		if (!device_active(hub))
689 			continue;
690 		for (device_find_first_child(hub, &dev);
691 		     dev;
692 		     device_find_next_child(&dev)) {
693 			struct usb_port_status status;
694 
695 			if (!device_active(dev))
696 				continue;
697 
698 			udev = dev_get_parent_priv(dev);
699 			if (usb_get_port_status(udev, udev->portnr, &status)
700 					< 0)
701 				/* USB request failed */
702 				continue;
703 
704 			if (le16_to_cpu(status.wPortChange) &
705 			    USB_PORT_STAT_C_CONNECTION)
706 				change++;
707 		}
708 	}
709 
710 	return change;
711 }
712 
usb_child_post_bind(struct udevice * dev)713 static int usb_child_post_bind(struct udevice *dev)
714 {
715 	struct usb_dev_platdata *plat = dev_get_parent_platdata(dev);
716 	int val;
717 
718 	if (!dev_of_valid(dev))
719 		return 0;
720 
721 	/* We only support matching a few things */
722 	val = dev_read_u32_default(dev, "usb,device-class", -1);
723 	if (val != -1) {
724 		plat->id.match_flags |= USB_DEVICE_ID_MATCH_DEV_CLASS;
725 		plat->id.bDeviceClass = val;
726 	}
727 	val = dev_read_u32_default(dev, "usb,interface-class", -1);
728 	if (val != -1) {
729 		plat->id.match_flags |= USB_DEVICE_ID_MATCH_INT_CLASS;
730 		plat->id.bInterfaceClass = val;
731 	}
732 
733 	return 0;
734 }
735 
usb_get_bus(struct udevice * dev)736 struct udevice *usb_get_bus(struct udevice *dev)
737 {
738 	struct udevice *bus;
739 
740 	for (bus = dev; bus && device_get_uclass_id(bus) != UCLASS_USB; )
741 		bus = bus->parent;
742 	if (!bus) {
743 		/* By design this cannot happen */
744 		assert(bus);
745 		debug("USB HUB '%s' does not have a controller\n", dev->name);
746 	}
747 
748 	return bus;
749 }
750 
usb_child_pre_probe(struct udevice * dev)751 int usb_child_pre_probe(struct udevice *dev)
752 {
753 	struct usb_device *udev = dev_get_parent_priv(dev);
754 	struct usb_dev_platdata *plat = dev_get_parent_platdata(dev);
755 	int ret;
756 
757 	if (plat->udev) {
758 		/*
759 		 * Copy over all the values set in the on stack struct
760 		 * usb_device in usb_scan_device() to our final struct
761 		 * usb_device for this dev.
762 		 */
763 		*udev = *(plat->udev);
764 		/* And clear plat->udev as it will not be valid for long */
765 		plat->udev = NULL;
766 		udev->dev = dev;
767 	} else {
768 		/*
769 		 * This happens with devices which are explicitly bound
770 		 * instead of being discovered through usb_scan_device()
771 		 * such as sandbox emul devices.
772 		 */
773 		udev->dev = dev;
774 		udev->controller_dev = usb_get_bus(dev);
775 		udev->devnum = plat->devnum;
776 
777 		/*
778 		 * udev did not go through usb_scan_device(), so we need to
779 		 * select the config and read the config descriptors.
780 		 */
781 		ret = usb_select_config(udev);
782 		if (ret)
783 			return ret;
784 	}
785 
786 	return 0;
787 }
788 
789 UCLASS_DRIVER(usb) = {
790 	.id		= UCLASS_USB,
791 	.name		= "usb",
792 	.flags		= DM_UC_FLAG_SEQ_ALIAS,
793 	.post_bind	= dm_scan_fdt_dev,
794 	.priv_auto_alloc_size = sizeof(struct usb_uclass_priv),
795 	.per_child_auto_alloc_size = sizeof(struct usb_device),
796 	.per_device_auto_alloc_size = sizeof(struct usb_bus_priv),
797 	.child_post_bind = usb_child_post_bind,
798 	.child_pre_probe = usb_child_pre_probe,
799 	.per_child_platdata_auto_alloc_size = sizeof(struct usb_dev_platdata),
800 };
801 
802 UCLASS_DRIVER(usb_dev_generic) = {
803 	.id		= UCLASS_USB_DEV_GENERIC,
804 	.name		= "usb_dev_generic",
805 };
806 
807 U_BOOT_DRIVER(usb_dev_generic_drv) = {
808 	.id		= UCLASS_USB_DEV_GENERIC,
809 	.name		= "usb_dev_generic_drv",
810 };
811