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