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