⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 usb.c

📁 usb driver for 2.6.17
💻 C
📖 第 1 页 / 共 3 页
字号:
/* * 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;}#elsestatic 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 */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -