1294 lines · 36.6 KB · C
RawDownload
// SPDX-License-Identifier: GPL-2.0/* * drivers/usb/core/usb.c * * (C) Copyright Linus Torvalds 1999 * (C) Copyright Johannes Erdfelt 1999-2001 * (C) Copyright Andreas Gal 1999 * (C) Copyright Gregory P. Smith 1999 * (C) Copyright Deti Fliegl 1999 (new USB architecture) * (C) Copyright Randy Dunlap 2000 * (C) Copyright David Brownell 2000-2004 * (C) Copyright Yggdrasil Computing, Inc. 2000 *     (usb_device_id matching changes by Adam J. Richter) * (C) Copyright Greg Kroah-Hartman 2002-2003 * * Released under the GPLv2 only. * * NOTE! This is not actually a driver at all, rather this is * just a collection of helper routines that implement the * generic USB things that the real drivers can use.. * * Think of this as a "USB library" rather than anything else, * with no callbacks.  Callbacks are evil. */ #include <linux/module.h>#include <linux/moduleparam.h>#include <linux/of.h>#include <linux/string.h>#include <linux/bitops.h>#include <linux/slab.h>#include <linux/kmod.h>#include <linux/init.h>#include <linux/spinlock.h>#include <linux/errno.h>#include <linux/usb.h>#include <linux/usb/hcd.h>#include <linux/mutex.h>#include <linux/workqueue.h>#include <linux/debugfs.h>#include <linux/usb/of.h> #include <asm/io.h>#include <linux/scatterlist.h>#include <linux/mm.h>#include <linux/dma-mapping.h> #include "hub.h"#include "trace.h" const char *usbcore_name = "usbcore"; static bool nousb;	/* Disable USB when built into kernel image */ module_param(nousb, bool, 0444); /* * for external read access to <nousb> */int usb_disabled(void){	return nousb;}EXPORT_SYMBOL_GPL(usb_disabled); #ifdef	CONFIG_PM/* Default delay value, in seconds */static int usb_autosuspend_delay = CONFIG_USB_AUTOSUSPEND_DELAY;module_param_named(autosuspend, usb_autosuspend_delay, int, 0644);MODULE_PARM_DESC(autosuspend, "default autosuspend delay"); #else#define usb_autosuspend_delay		0#endif static bool match_endpoint(struct usb_endpoint_descriptor *epd,		struct usb_endpoint_descriptor **bulk_in,		struct usb_endpoint_descriptor **bulk_out,		struct usb_endpoint_descriptor **int_in,		struct usb_endpoint_descriptor **int_out){	switch (usb_endpoint_type(epd)) {	case USB_ENDPOINT_XFER_BULK:		if (usb_endpoint_dir_in(epd)) {			if (bulk_in && !*bulk_in) {				*bulk_in = epd;				break;			}		} else {			if (bulk_out && !*bulk_out) {				*bulk_out = epd;				break;			}		} 		return false;	case USB_ENDPOINT_XFER_INT:		if (usb_endpoint_dir_in(epd)) {			if (int_in && !*int_in) {				*int_in = epd;				break;			}		} else {			if (int_out && !*int_out) {				*int_out = epd;				break;			}		} 		return false;	default:		return false;	} 	return (!bulk_in || *bulk_in) && (!bulk_out || *bulk_out) &&			(!int_in || *int_in) && (!int_out || *int_out);} /** * usb_find_common_endpoints() -- look up common endpoint descriptors * @alt:	alternate setting to search * @bulk_in:	pointer to descriptor pointer, or NULL * @bulk_out:	pointer to descriptor pointer, or NULL * @int_in:	pointer to descriptor pointer, or NULL * @int_out:	pointer to descriptor pointer, or NULL * * Search the alternate setting's endpoint descriptors for the first bulk-in, * bulk-out, interrupt-in and interrupt-out endpoints and return them in the * provided pointers (unless they are NULL). * * If a requested endpoint is not found, the corresponding pointer is set to * NULL. * * Return: Zero if all requested descriptors were found, or -ENXIO otherwise. */int usb_find_common_endpoints(struct usb_host_interface *alt,		struct usb_endpoint_descriptor **bulk_in,		struct usb_endpoint_descriptor **bulk_out,		struct usb_endpoint_descriptor **int_in,		struct usb_endpoint_descriptor **int_out){	struct usb_endpoint_descriptor *epd;	int i; 	if (bulk_in)		*bulk_in = NULL;	if (bulk_out)		*bulk_out = NULL;	if (int_in)		*int_in = NULL;	if (int_out)		*int_out = NULL; 	for (i = 0; i < alt->desc.bNumEndpoints; ++i) {		epd = &alt->endpoint[i].desc; 		if (match_endpoint(epd, bulk_in, bulk_out, int_in, int_out))			return 0;	} 	return -ENXIO;}EXPORT_SYMBOL_GPL(usb_find_common_endpoints); /** * usb_find_common_endpoints_reverse() -- look up common endpoint descriptors * @alt:	alternate setting to search * @bulk_in:	pointer to descriptor pointer, or NULL * @bulk_out:	pointer to descriptor pointer, or NULL * @int_in:	pointer to descriptor pointer, or NULL * @int_out:	pointer to descriptor pointer, or NULL * * Search the alternate setting's endpoint descriptors for the last bulk-in, * bulk-out, interrupt-in and interrupt-out endpoints and return them in the * provided pointers (unless they are NULL). * * If a requested endpoint is not found, the corresponding pointer is set to * NULL. * * Return: Zero if all requested descriptors were found, or -ENXIO otherwise. */int usb_find_common_endpoints_reverse(struct usb_host_interface *alt,		struct usb_endpoint_descriptor **bulk_in,		struct usb_endpoint_descriptor **bulk_out,		struct usb_endpoint_descriptor **int_in,		struct usb_endpoint_descriptor **int_out){	struct usb_endpoint_descriptor *epd;	int i; 	if (bulk_in)		*bulk_in = NULL;	if (bulk_out)		*bulk_out = NULL;	if (int_in)		*int_in = NULL;	if (int_out)		*int_out = NULL; 	for (i = alt->desc.bNumEndpoints - 1; i >= 0; --i) {		epd = &alt->endpoint[i].desc; 		if (match_endpoint(epd, bulk_in, bulk_out, int_in, int_out))			return 0;	} 	return -ENXIO;}EXPORT_SYMBOL_GPL(usb_find_common_endpoints_reverse); /** * usb_find_endpoint() - Given an endpoint address, search for the endpoint's * usb_host_endpoint structure in an interface's current altsetting. * @intf: the interface whose current altsetting should be searched * @ep_addr: the endpoint address (number and direction) to find * * Search the altsetting's list of endpoints for one with the specified address. * * Return: Pointer to the usb_host_endpoint if found, %NULL otherwise. */static const struct usb_host_endpoint *usb_find_endpoint(		const struct usb_interface *intf, unsigned int ep_addr){	int n;	const struct usb_host_endpoint *ep; 	n = intf->cur_altsetting->desc.bNumEndpoints;	ep = intf->cur_altsetting->endpoint;	for (; n > 0; (--n, ++ep)) {		if (ep->desc.bEndpointAddress == ep_addr)			return ep;	}	return NULL;} /** * usb_check_bulk_endpoints - Check whether an interface's current altsetting * contains a set of bulk endpoints with the given addresses. * @intf: the interface whose current altsetting should be searched * @ep_addrs: 0-terminated array of the endpoint addresses (number and * direction) to look for * * Search for endpoints with the specified addresses and check their types. * * Return: %true if all the endpoints are found and are bulk, %false otherwise. */bool usb_check_bulk_endpoints(		const struct usb_interface *intf, const u8 *ep_addrs){	const struct usb_host_endpoint *ep; 	for (; *ep_addrs; ++ep_addrs) {		ep = usb_find_endpoint(intf, *ep_addrs);		if (!ep || !usb_endpoint_xfer_bulk(&ep->desc))			return false;	}	return true;}EXPORT_SYMBOL_GPL(usb_check_bulk_endpoints); /** * usb_check_int_endpoints - Check whether an interface's current altsetting * contains a set of interrupt endpoints with the given addresses. * @intf: the interface whose current altsetting should be searched * @ep_addrs: 0-terminated array of the endpoint addresses (number and * direction) to look for * * Search for endpoints with the specified addresses and check their types. * * Return: %true if all the endpoints are found and are interrupt, * %false otherwise. */bool usb_check_int_endpoints(		const struct usb_interface *intf, const u8 *ep_addrs){	const struct usb_host_endpoint *ep; 	for (; *ep_addrs; ++ep_addrs) {		ep = usb_find_endpoint(intf, *ep_addrs);		if (!ep || !usb_endpoint_xfer_int(&ep->desc))			return false;	}	return true;}EXPORT_SYMBOL_GPL(usb_check_int_endpoints); /** * usb_find_alt_setting() - Given a configuration, find the alternate setting * for the given interface. * @config: the configuration to search (not necessarily the current config). * @iface_num: interface number to search in * @alt_num: alternate interface setting number to search for. * * Search the configuration's interface cache for the given alt setting. * * Return: The alternate setting, if found. %NULL otherwise. */struct usb_host_interface *usb_find_alt_setting(		struct usb_host_config *config,		unsigned int iface_num,		unsigned int alt_num){	struct usb_interface_cache *intf_cache = NULL;	int i; 	if (!config)		return NULL;	for (i = 0; i < config->desc.bNumInterfaces; i++) {		if (config->intf_cache[i]->altsetting[0].desc.bInterfaceNumber				== iface_num) {			intf_cache = config->intf_cache[i];			break;		}	}	if (!intf_cache)		return NULL;	for (i = 0; i < intf_cache->num_altsetting; i++)		if (intf_cache->altsetting[i].desc.bAlternateSetting == alt_num)			return &intf_cache->altsetting[i]; 	printk(KERN_DEBUG "Did not find alt setting %u for intf %u, "			"config %u\n", alt_num, iface_num,			config->desc.bConfigurationValue);	return NULL;}EXPORT_SYMBOL_GPL(usb_find_alt_setting); /** * usb_ifnum_to_if - get the interface object with a given interface number * @dev: the device whose current configuration is considered * @ifnum: the desired interface * * This walks the device descriptor for the currently active configuration * to find the interface object with the particular interface number. * * Note that configuration descriptors are not required to assign interface * numbers sequentially, so that it would be incorrect to assume that * the first interface in that descriptor corresponds to interface zero. * This routine helps device drivers avoid such mistakes. * However, you should make sure that you do the right thing with any * alternate settings available for this interfaces. * * Don't call this function unless you are bound to one of the interfaces * on this device or you have locked the device! * * Return: A pointer to the interface that has @ifnum as interface number, * if found. %NULL otherwise. */struct usb_interface *usb_ifnum_to_if(const struct usb_device *dev,				      unsigned ifnum){	struct usb_host_config *config = dev->actconfig;	int i; 	if (!config)		return NULL;	for (i = 0; i < config->desc.bNumInterfaces; i++)		if (config->interface[i]->altsetting[0]				.desc.bInterfaceNumber == ifnum)			return config->interface[i]; 	return NULL;}EXPORT_SYMBOL_GPL(usb_ifnum_to_if); /** * usb_altnum_to_altsetting - get the altsetting structure with a given alternate setting number. * @intf: the interface containing the altsetting in question * @altnum: the desired alternate setting number * * This searches the altsetting array of the specified interface for * an entry with the correct bAlternateSetting value. * * Note that altsettings need not be stored sequentially by number, so * it would be incorrect to assume that the first altsetting entry in * the array corresponds to altsetting zero.  This routine helps device * drivers avoid such mistakes. * * Don't call this function unless you are bound to the intf interface * or you have locked the device! * * Return: A pointer to the entry of the altsetting array of @intf that * has @altnum as the alternate setting number. %NULL if not found. */struct usb_host_interface *usb_altnum_to_altsetting(					const struct usb_interface *intf,					unsigned int altnum){	int i; 	for (i = 0; i < intf->num_altsetting; i++) {		if (intf->altsetting[i].desc.bAlternateSetting == altnum)			return &intf->altsetting[i];	}	return NULL;}EXPORT_SYMBOL_GPL(usb_altnum_to_altsetting); struct find_interface_arg {	int minor;	struct device_driver *drv;}; static int __find_interface(struct device *dev, const void *data){	const struct find_interface_arg *arg = data;	struct usb_interface *intf; 	if (!is_usb_interface(dev))		return 0; 	if (dev->driver != arg->drv)		return 0;	intf = to_usb_interface(dev);	return intf->minor == arg->minor;} /** * usb_find_interface - find usb_interface pointer for driver and device * @drv: the driver whose current configuration is considered * @minor: the minor number of the desired device * * This walks the bus device list and returns a pointer to the interface * with the matching minor and driver.  Note, this only works for devices * that share the USB major number. * * Return: A pointer to the interface with the matching major and @minor. */struct usb_interface *usb_find_interface(struct usb_driver *drv, int minor){	struct find_interface_arg argb;	struct device *dev; 	argb.minor = minor;	argb.drv = &drv->driver; 	dev = bus_find_device(&usb_bus_type, NULL, &argb, __find_interface); 	/* Drop reference count from bus_find_device */	put_device(dev); 	return dev ? to_usb_interface(dev) : NULL;}EXPORT_SYMBOL_GPL(usb_find_interface); struct each_dev_arg {	void *data;	int (*fn)(struct usb_device *, void *);}; static int __each_dev(struct device *dev, void *data){	struct each_dev_arg *arg = (struct each_dev_arg *)data; 	/* There are struct usb_interface on the same bus, filter them out */	if (!is_usb_device(dev))		return 0; 	return arg->fn(to_usb_device(dev), arg->data);} /** * usb_for_each_dev - iterate over all USB devices in the system * @data: data pointer that will be handed to the callback function * @fn: callback function to be called for each USB device * * Iterate over all USB devices and call @fn for each, passing it @data. If it * returns anything other than 0, we break the iteration prematurely and return * that value. */int usb_for_each_dev(void *data, int (*fn)(struct usb_device *, void *)){	struct each_dev_arg arg = {data, fn}; 	return bus_for_each_dev(&usb_bus_type, NULL, &arg, __each_dev);}EXPORT_SYMBOL_GPL(usb_for_each_dev); /** * usb_release_dev - free a usb device structure when all users of it are finished. * @dev: device that's been disconnected * * Will be called only by the device core when all users of this usb device are * done. */static void usb_release_dev(struct device *dev){	struct usb_device *udev;	struct usb_hcd *hcd; 	udev = to_usb_device(dev);	hcd = bus_to_hcd(udev->bus); 	usb_destroy_configuration(udev);	usb_release_bos_descriptor(udev);	of_node_put(dev->of_node);	usb_put_hcd(hcd);	kfree(udev->product);	kfree(udev->manufacturer);	kfree(udev->serial);

Lines 1–500 of 1294.