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

📄 usb.c

📁 linux-2.6.15.6
💻 C
📖 第 1 页 / 共 4 页
字号:
/*			USB device locking * * Although locking USB devices should be straightforward, it is * complicated by the way the driver-model core works.  When a new USB * driver is registered or unregistered, the core will automatically * probe or disconnect all matching interfaces on all USB devices while * holding the USB subsystem writelock.  There's no good way for us to * tell which devices will be used or to lock them beforehand; our only * option is to effectively lock all the USB devices. * * We do that by using a private rw-semaphore, usb_all_devices_rwsem. * When locking an individual device you must first acquire the rwsem's * readlock.  When a driver is registered or unregistered the writelock * must be held.  These actions are encapsulated in the subroutines * below, so all a driver needs to do is call usb_lock_device() and * usb_unlock_device(). * * Complications arise when several devices are to be locked at the same * time.  Only hub-aware drivers that are part of usbcore ever have to * do this; nobody else needs to worry about it.  The problem is that * usb_lock_device() must not be called to lock a second device since it * would acquire the rwsem's readlock reentrantly, leading to deadlock if * another thread was waiting for the writelock.  The solution is simple: * *	When locking more than one device, call usb_lock_device() *	to lock the first one.  Lock the others by calling *	down(&udev->serialize) directly. * *	When unlocking multiple devices, use up(&udev->serialize) *	to unlock all but the last one.  Unlock the last one by *	calling usb_unlock_device(). * *	When locking both a device and its parent, always lock the *	the parent first. *//** * usb_lock_device - acquire the lock for a usb device structure * @udev: device that's being locked * * Use this routine when you don't hold any other device locks; * to acquire nested inner locks call down(&udev->serialize) directly. * This is necessary for proper interaction with usb_lock_all_devices(). */void usb_lock_device(struct usb_device *udev){	down_read(&usb_all_devices_rwsem);	down(&udev->serialize);}/** * usb_trylock_device - attempt to acquire the lock for a usb device structure * @udev: device that's being locked * * Don't use this routine if you already hold a device lock; * use down_trylock(&udev->serialize) instead. * This is necessary for proper interaction with usb_lock_all_devices(). * * Returns 1 if successful, 0 if contention. */int usb_trylock_device(struct usb_device *udev){	if (!down_read_trylock(&usb_all_devices_rwsem))		return 0;	if (down_trylock(&udev->serialize)) {		up_read(&usb_all_devices_rwsem);		return 0;	}	return 1;}/** * usb_lock_device_for_reset - cautiously acquire the lock for a *	usb device structure * @udev: device that's being locked * @iface: interface bound to the driver making the request (optional) * * Attempts to acquire the device lock, but fails if the device is * NOTATTACHED or SUSPENDED, or if iface is specified and the interface * is neither BINDING nor BOUND.  Rather than sleeping to wait for the * lock, the routine polls repeatedly.  This is to prevent deadlock with * disconnect; in some drivers (such as usb-storage) the disconnect() * or suspend() method will block waiting for a device reset to complete. * * Returns a negative error code for failure, otherwise 1 or 0 to indicate * that the device will or will not have to be unlocked.  (0 can be * returned when an interface is given and is BINDING, because in that * case the driver already owns the device lock.) */int usb_lock_device_for_reset(struct usb_device *udev,		struct usb_interface *iface){	unsigned long jiffies_expire = jiffies + HZ;	if (udev->state == USB_STATE_NOTATTACHED)		return -ENODEV;	if (udev->state == USB_STATE_SUSPENDED)		return -EHOSTUNREACH;	if (iface) {		switch (iface->condition) {		  case USB_INTERFACE_BINDING:			return 0;		  case USB_INTERFACE_BOUND:			break;		  default:			return -EINTR;		}	}	while (!usb_trylock_device(udev)) {		/* If we can't acquire the lock after waiting one second,		 * we're probably deadlocked */		if (time_after(jiffies, jiffies_expire))			return -EBUSY;		msleep(15);		if (udev->state == USB_STATE_NOTATTACHED)			return -ENODEV;		if (udev->state == USB_STATE_SUSPENDED)			return -EHOSTUNREACH;		if (iface && iface->condition != USB_INTERFACE_BOUND)			return -EINTR;	}	return 1;}/** * usb_unlock_device - release the lock for a usb device structure * @udev: device that's being unlocked * * Use this routine when releasing the only device lock you hold; * to release inner nested locks call up(&udev->serialize) directly. * This is necessary for proper interaction with usb_lock_all_devices(). */void usb_unlock_device(struct usb_device *udev){	up(&udev->serialize);	up_read(&usb_all_devices_rwsem);}/** * usb_lock_all_devices - acquire the lock for all usb device structures * * This is necessary when registering a new driver or probing a bus, * since the driver-model core may try to use any usb_device. */void usb_lock_all_devices(void){	down_write(&usb_all_devices_rwsem);}/** * usb_unlock_all_devices - release the lock for all usb device structures */void usb_unlock_all_devices(void){	up_write(&usb_all_devices_rwsem);}static struct usb_device *match_device(struct usb_device *dev,				       u16 vendor_id, u16 product_id){	struct usb_device *ret_dev = NULL;	int child;	dev_dbg(&dev->dev, "check for vendor %04x, product %04x ...\n",	    le16_to_cpu(dev->descriptor.idVendor),	    le16_to_cpu(dev->descriptor.idProduct));	/* see if this device matches */	if ((vendor_id == le16_to_cpu(dev->descriptor.idVendor)) &&	    (product_id == le16_to_cpu(dev->descriptor.idProduct))) {		dev_dbg (&dev->dev, "matched this device!\n");		ret_dev = usb_get_dev(dev);		goto exit;	}	/* look through all of the children of this device */	for (child = 0; child < dev->maxchild; ++child) {		if (dev->children[child]) {			down(&dev->children[child]->serialize);			ret_dev = match_device(dev->children[child],					       vendor_id, product_id);			up(&dev->children[child]->serialize);			if (ret_dev)				goto exit;		}	}exit:	return ret_dev;}/** * usb_find_device - find a specific usb device in the system * @vendor_id: the vendor id of the device to find * @product_id: the product id of the device to find * * Returns a pointer to a struct usb_device if such a specified usb * device is present in the system currently.  The usage count of the * device will be incremented if a device is found.  Make sure to call * usb_put_dev() when the caller is finished with the device. * * If a device with the specified vendor and product id is not found, * NULL is returned. */struct usb_device *usb_find_device(u16 vendor_id, u16 product_id){	struct list_head *buslist;	struct usb_bus *bus;	struct usb_device *dev = NULL;		down(&usb_bus_list_lock);	for (buslist = usb_bus_list.next;	     buslist != &usb_bus_list; 	     buslist = buslist->next) {		bus = container_of(buslist, struct usb_bus, bus_list);		if (!bus->root_hub)			continue;		usb_lock_device(bus->root_hub);		dev = match_device(bus->root_hub, vendor_id, product_id);		usb_unlock_device(bus->root_hub);		if (dev)			goto exit;	}exit:	up(&usb_bus_list_lock);	return dev;}/** * usb_get_current_frame_number - return current bus frame number * @dev: the device whose bus is being queried * * Returns the current frame number for the USB host controller * used with the given USB device.  This can be used when scheduling * isochronous requests. * * Note that different kinds of host controller have different * "scheduling horizons".  While one type might support scheduling only * 32 frames into the future, others could support scheduling up to * 1024 frames into the future. */int usb_get_current_frame_number(struct usb_device *dev){	return dev->bus->op->get_frame_number (dev);}/*-------------------------------------------------------------------*//* * __usb_get_extra_descriptor() finds a descriptor of specific type in the * extra field of the interface and endpoint descriptor structs. */int __usb_get_extra_descriptor(char *buffer, unsigned size,	unsigned char type, void **ptr){	struct usb_descriptor_header *header;	while (size >= sizeof(struct usb_descriptor_header)) {		header = (struct usb_descriptor_header *)buffer;		if (header->bLength < 2) {			printk(KERN_ERR				"%s: bogus descriptor, type %d length %d\n",				usbcore_name,				header->bDescriptorType, 				header->bLength);			return -1;		}		if (header->bDescriptorType == type) {			*ptr = header;			return 0;		}		buffer += header->bLength;		size -= header->bLength;	}	return -1;}/** * usb_buffer_alloc - allocate dma-consistent buffer for URB_NO_xxx_DMA_MAP * @dev: device the buffer will be used with * @size: requested buffer size * @mem_flags: affect whether allocation may block * @dma: used to return DMA address of buffer * * Return value is either null (indicating no buffer could be allocated), or * the cpu-space pointer to a buffer that may be used to perform DMA to the * specified device.  Such cpu-space buffers are returned along with the DMA * address (through the pointer provided). * * These buffers are used with URB_NO_xxx_DMA_MAP set in urb->transfer_flags * to avoid behaviors like using "DMA bounce buffers", or tying down I/O * mapping hardware for long idle periods.  The implementation varies between * platforms, depending on details of how DMA will work to this device. * Using these buffers also helps prevent cacheline sharing problems on * architectures where CPU caches are not DMA-coherent. * * When the buffer is no longer used, free it with usb_buffer_free(). */void *usb_buffer_alloc (	struct usb_device *dev,	size_t size,	gfp_t mem_flags,	dma_addr_t *dma){	if (!dev || !dev->bus || !dev->bus->op || !dev->bus->op->buffer_alloc)		return NULL;	return dev->bus->op->buffer_alloc (dev->bus, size, mem_flags, dma);}/** * usb_buffer_free - free memory allocated with usb_buffer_alloc() * @dev: device the buffer was used with * @size: requested buffer size * @addr: CPU address of buffer * @dma: DMA address of buffer * * This reclaims an I/O buffer, letting it be reused.  The memory must have * been allocated using usb_buffer_alloc(), and the parameters must match * those provided in that allocation request.  */void usb_buffer_free (	struct usb_device *dev,	size_t size,	void *addr,	dma_addr_t dma){	if (!dev || !dev->bus || !dev->bus->op || !dev->bus->op->buffer_free)	    	return;	dev->bus->op->buffer_free (dev->bus, size, addr, dma);}/** * usb_buffer_map - create DMA mapping(s) for an urb * @urb: urb whose transfer_buffer/setup_packet will be mapped * * Return value is either null (indicating no buffer could be mapped), or * the parameter.  URB_NO_TRANSFER_DMA_MAP and URB_NO_SETUP_DMA_MAP are * added to urb->transfer_flags if the operation succeeds.  If the device * is connected to this system through a non-DMA controller, this operation * always succeeds. * * This call would normally be used for an urb which is reused, perhaps * as the target of a large periodic transfer, with usb_buffer_dmasync() * calls to synchronize memory and dma state. * * Reverse the effect of this call with usb_buffer_unmap(). */#if 0struct urb *usb_buffer_map (struct urb *urb){	struct usb_bus		*bus;	struct device		*controller;	if (!urb			|| !urb->dev			|| !(bus = urb->dev->bus)			|| !(controller = bus->controller))		return NULL;	if (controller->dma_mask) {		urb->transfer_dma = dma_map_single (controller,			urb->transfer_buffer, urb->transfer_buffer_length,			usb_pipein (urb->pipe)				? DMA_FROM_DEVICE : DMA_TO_DEVICE);		if (usb_pipecontrol (urb->pipe))			urb->setup_dma = dma_map_single (controller,					urb->setup_packet,					sizeof (struct usb_ctrlrequest),					DMA_TO_DEVICE);	// FIXME generic api broken like pci, can't report errors	// if (urb->transfer_dma == DMA_ADDR_INVALID) return 0;	} else		urb->transfer_dma = ~0;	urb->transfer_flags |= (URB_NO_TRANSFER_DMA_MAP				| URB_NO_SETUP_DMA_MAP);	return urb;}#endif  /*  0  *//* XXX DISABLED, no users currently.  If you wish to re-enable this * XXX please determine whether the sync is to transfer ownership of * XXX the buffer from device to cpu or vice verse, and thusly use the * XXX appropriate _for_{cpu,device}() method.  -DaveM */#if 0/** * usb_buffer_dmasync - synchronize DMA and CPU view of buffer(s) * @urb: urb whose transfer_buffer/setup_packet will be synchronized */void usb_buffer_dmasync (struct urb *urb){	struct usb_bus		*bus;	struct device		*controller;	if (!urb			|| !(urb->transfer_flags & URB_NO_TRANSFER_DMA_MAP)			|| !urb->dev			|| !(bus = urb->dev->bus)			|| !(controller = bus->controller))		return;	if (controller->dma_mask) {

⌨️ 快捷键说明

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