1232 lines · 35.0 KB · C
RawDownload
/* * drivers/usb/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 * * 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. * It should be considered a slave, with no callbacks. Callbacks * are evil. */ #include <linux/config.h>#include <linux/module.h>#include <linux/string.h>#include <linux/bitops.h>#include <linux/slab.h>#include <linux/interrupt.h>  /* for in_interrupt() */#include <linux/kmod.h>#include <linux/init.h>#include <linux/spinlock.h>#include <linux/errno.h>#include <linux/smp_lock.h>#include <linux/usb.h>#include <linux/mutex.h> #include <asm/io.h>#include <asm/scatterlist.h>#include <linux/mm.h>#include <linux/dma-mapping.h> #include "hcd.h"#include "usb.h"  const char *usbcore_name = "usbcore"; static int nousb;	/* Disable USB when built into kernel image */  /** * 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 * and returns a pointer to the interface with that particular interface * number, or null. * * 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! */struct usb_interface *usb_ifnum_to_if(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;} /** * 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 and returns a pointer * to that entry, or null. * * 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! */struct usb_host_interface *usb_altnum_to_altsetting(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;} /** * usb_driver_claim_interface - bind a driver to an interface * @driver: the driver to be bound * @iface: the interface to which it will be bound; must be in the *	usb device's active configuration * @priv: driver data associated with that interface * * This is used by usb device drivers that need to claim more than one * interface on a device when probing (audio and acm are current examples). * No device driver should directly modify internal usb_interface or * usb_device structure members. * * Few drivers should need to use this routine, since the most natural * way to bind to an interface is to return the private data from * the driver's probe() method. * * Callers must own the device lock and the driver model's usb_bus_type.subsys * writelock.  So driver probe() entries don't need extra locking, * but other call contexts may need to explicitly claim those locks. */int usb_driver_claim_interface(struct usb_driver *driver,				struct usb_interface *iface, void* priv){	struct device *dev = &iface->dev; 	if (dev->driver)		return -EBUSY; 	dev->driver = &driver->driver;	usb_set_intfdata(iface, priv);	iface->condition = USB_INTERFACE_BOUND;	mark_active(iface); 	/* if interface was already added, bind now; else let	 * the future device_add() bind it, bypassing probe()	 */	if (device_is_registered(dev))		device_bind_driver(dev); 	return 0;} /** * usb_driver_release_interface - unbind a driver from an interface * @driver: the driver to be unbound * @iface: the interface from which it will be unbound * * This can be used by drivers to release an interface without waiting * for their disconnect() methods to be called.  In typical cases this * also causes the driver disconnect() method to be called. * * This call is synchronous, and may not be used in an interrupt context. * Callers must own the device lock and the driver model's usb_bus_type.subsys * writelock.  So driver disconnect() entries don't need extra locking, * but other call contexts may need to explicitly claim those locks. */void usb_driver_release_interface(struct usb_driver *driver,					struct usb_interface *iface){	struct device *dev = &iface->dev; 	/* this should never happen, don't release something that's not ours */	if (!dev->driver || dev->driver != &driver->driver)		return; 	/* don't release from within disconnect() */	if (iface->condition != USB_INTERFACE_BOUND)		return; 	/* don't release if the interface hasn't been added yet */	if (device_is_registered(dev)) {		iface->condition = USB_INTERFACE_UNBINDING;		device_release_driver(dev);	} 	dev->driver = NULL;	usb_set_intfdata(iface, NULL);	iface->condition = USB_INTERFACE_UNBOUND;	mark_quiesced(iface);} struct find_interface_arg {	int minor;	struct usb_interface *interface;}; static int __find_interface(struct device * dev, void * data){	struct find_interface_arg *arg = data;	struct usb_interface *intf; 	/* can't look at usb devices, only interfaces */	if (dev->driver == &usb_generic_driver)		return 0; 	intf = to_usb_interface(dev);	if (intf->minor != -1 && intf->minor == arg->minor) {		arg->interface = intf;		return 1;	}	return 0;} /** * 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 driver device list and returns a pointer to the interface  * with the matching minor.  Note, this only works for devices that share the * USB major number. */struct usb_interface *usb_find_interface(struct usb_driver *drv, int minor){	struct find_interface_arg argb; 	argb.minor = minor;	argb.interface = NULL;	driver_for_each_device(&drv->driver, NULL, &argb, __find_interface);	return argb.interface;} #ifdef	CONFIG_HOTPLUG /* * This sends an uevent to userspace, typically helping to load driver * or other modules, configure the device, and more.  Drivers can provide * a MODULE_DEVICE_TABLE to help with module loading subtasks. * * We're called either from khubd (the typical case) or from root hub * (init, kapmd, modprobe, rmmod, etc), but the agents need to handle * delays in event delivery.  Use sysfs (and DEVPATH) to make sure the * device (and this configuration!) are still present. */static int usb_uevent(struct device *dev, char **envp, int num_envp,		      char *buffer, int buffer_size){	struct usb_interface *intf;	struct usb_device *usb_dev;	struct usb_host_interface *alt;	int i = 0;	int length = 0; 	if (!dev)		return -ENODEV; 	/* driver is often null here; dev_dbg() would oops */	pr_debug ("usb %s: uevent\n", dev->bus_id); 	/* Must check driver_data here, as on remove driver is always NULL */	if ((dev->driver == &usb_generic_driver) || 	    (dev->driver_data == &usb_generic_driver_data))		return 0; 	intf = to_usb_interface(dev);	usb_dev = interface_to_usbdev (intf);	alt = intf->cur_altsetting; 	if (usb_dev->devnum < 0) {		pr_debug ("usb %s: already deleted?\n", dev->bus_id);		return -ENODEV;	}	if (!usb_dev->bus) {		pr_debug ("usb %s: bus removed?\n", dev->bus_id);		return -ENODEV;	} #ifdef	CONFIG_USB_DEVICEFS	/* If this is available, userspace programs can directly read	 * all the device descriptors we don't tell them about.  Or	 * even act as usermode drivers.	 *	 * FIXME reduce hardwired intelligence here	 */	if (add_uevent_var(envp, num_envp, &i,			   buffer, buffer_size, &length,			   "DEVICE=/proc/bus/usb/%03d/%03d",			   usb_dev->bus->busnum, usb_dev->devnum))		return -ENOMEM;#endif 	/* per-device configurations are common */	if (add_uevent_var(envp, num_envp, &i,			   buffer, buffer_size, &length,			   "PRODUCT=%x/%x/%x",			   le16_to_cpu(usb_dev->descriptor.idVendor),			   le16_to_cpu(usb_dev->descriptor.idProduct),			   le16_to_cpu(usb_dev->descriptor.bcdDevice)))		return -ENOMEM; 	/* class-based driver binding models */	if (add_uevent_var(envp, num_envp, &i,			   buffer, buffer_size, &length,			   "TYPE=%d/%d/%d",			   usb_dev->descriptor.bDeviceClass,			   usb_dev->descriptor.bDeviceSubClass,			   usb_dev->descriptor.bDeviceProtocol))		return -ENOMEM; 	if (add_uevent_var(envp, num_envp, &i,			   buffer, buffer_size, &length,			   "INTERFACE=%d/%d/%d",			   alt->desc.bInterfaceClass,			   alt->desc.bInterfaceSubClass,			   alt->desc.bInterfaceProtocol))		return -ENOMEM; 	if (add_uevent_var(envp, num_envp, &i,			   buffer, buffer_size, &length,			   "MODALIAS=usb:v%04Xp%04Xd%04Xdc%02Xdsc%02Xdp%02Xic%02Xisc%02Xip%02X",			   le16_to_cpu(usb_dev->descriptor.idVendor),			   le16_to_cpu(usb_dev->descriptor.idProduct),			   le16_to_cpu(usb_dev->descriptor.bcdDevice),			   usb_dev->descriptor.bDeviceClass,			   usb_dev->descriptor.bDeviceSubClass,			   usb_dev->descriptor.bDeviceProtocol,			   alt->desc.bInterfaceClass,			   alt->desc.bInterfaceSubClass,			   alt->desc.bInterfaceProtocol))		return -ENOMEM; 	envp[i] = NULL; 	return 0;} #else static int usb_uevent(struct device *dev, char **envp,			int num_envp, char *buffer, int buffer_size){	return -ENODEV;} #endif	/* CONFIG_HOTPLUG */ /** * 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; 	udev = to_usb_device(dev); 	usb_destroy_configuration(udev);	usb_bus_put(udev->bus);	kfree(udev->product);	kfree(udev->manufacturer);	kfree(udev->serial);	kfree(udev);} /** * usb_alloc_dev - usb device constructor (usbcore-internal) * @parent: hub to which device is connected; null to allocate a root hub * @bus: bus used to access the device * @port1: one-based index of port; ignored for root hubs * Context: !in_interrupt () * * Only hub drivers (including virtual root hub drivers for host * controllers) should ever call this. * * This call may not be used in a non-sleeping context. */struct usb_device *usb_alloc_dev(struct usb_device *parent, struct usb_bus *bus, unsigned port1){	struct usb_device *dev; 	dev = kzalloc(sizeof(*dev), GFP_KERNEL);	if (!dev)		return NULL; 	bus = usb_bus_get(bus);	if (!bus) {		kfree(dev);		return NULL;	} 	device_initialize(&dev->dev);	dev->dev.bus = &usb_bus_type;	dev->dev.dma_mask = bus->controller->dma_mask;	dev->dev.driver_data = &usb_generic_driver_data;	dev->dev.driver = &usb_generic_driver;	dev->dev.release = usb_release_dev;	dev->state = USB_STATE_ATTACHED; 	INIT_LIST_HEAD(&dev->ep0.urb_list);	dev->ep0.desc.bLength = USB_DT_ENDPOINT_SIZE;	dev->ep0.desc.bDescriptorType = USB_DT_ENDPOINT;	/* ep0 maxpacket comes later, from device descriptor */	dev->ep_in[0] = dev->ep_out[0] = &dev->ep0; 	/* Save readable and stable topology id, distinguishing devices	 * by location for diagnostics, tools, driver model, etc.  The	 * string is a path along hub ports, from the root.  Each device's	 * dev->devpath will be stable until USB is re-cabled, and hubs	 * are often labeled with these port numbers.  The bus_id isn't	 * as stable:  bus->busnum changes easily from modprobe order,	 * cardbus or pci hotplugging, and so on.	 */	if (unlikely (!parent)) {		dev->devpath [0] = '0'; 		dev->dev.parent = bus->controller;		sprintf (&dev->dev.bus_id[0], "usb%d", bus->busnum);	} else {		/* match any labeling on the hubs; it's one-based */		if (parent->devpath [0] == '0')			snprintf (dev->devpath, sizeof dev->devpath,				"%d", port1);		else			snprintf (dev->devpath, sizeof dev->devpath,				"%s.%d", parent->devpath, port1); 		dev->dev.parent = &parent->dev;		sprintf (&dev->dev.bus_id[0], "%d-%s",			bus->busnum, dev->devpath); 		/* hub driver sets up TT records */	} 	dev->portnum = port1;	dev->bus = bus;	dev->parent = parent;	INIT_LIST_HEAD(&dev->filelist); 	return dev;} /** * usb_get_dev - increments the reference count of the usb device structure * @dev: the device being referenced * * Each live reference to a device should be refcounted. * * Drivers for USB interfaces should normally record such references in * their probe() methods, when they bind to an interface, and release * them by calling usb_put_dev(), in their disconnect() methods. * * A pointer to the device with the incremented reference counter is returned. */struct usb_device *usb_get_dev(struct usb_device *dev){	if (dev)		get_device(&dev->dev);	return dev;} /** * usb_put_dev - release a use of the usb device structure * @dev: device that's been disconnected * * Must be called when a user of a device is finished with it.  When the last * user of the device calls this function, the memory of the device is freed. */void usb_put_dev(struct usb_device *dev){	if (dev)		put_device(&dev->dev);} /** * usb_get_intf - increments the reference count of the usb interface structure * @intf: the interface being referenced * * Each live reference to a interface must be refcounted. * * Drivers for USB interfaces should normally record such references in * their probe() methods, when they bind to an interface, and release * them by calling usb_put_intf(), in their disconnect() methods. * * A pointer to the interface with the incremented reference counter is * returned. */struct usb_interface *usb_get_intf(struct usb_interface *intf){	if (intf)		get_device(&intf->dev);	return intf;

Lines 1–500 of 1232.