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

📄 usb.c

📁 是关于linux2.5.1的完全源码
💻 C
📖 第 1 页 / 共 5 页
字号:
		/* MUST go to zero here, else someone's hanging on to		 * a device that's supposed to have been cleaned up!!		 */		BUG ();	}	dev->bus->op->deallocate (dev);	usb_destroy_configuration (dev);	usb_bus_put (dev->bus);	kfree (dev);}/** * usb_alloc_urb - creates a new urb for a USB driver to use * @iso_packets: number of iso packets for this urb * @mem_flags: the type of memory to allocate, see kmalloc() for a list of *	valid options for this. * * Creates an urb for the USB driver to use, initializes a few internal * structures, incrementes the usage counter, and returns a pointer to it. * * If no memory is available, NULL is returned. * * If the driver want to use this urb for interrupt, control, or bulk * endpoints, pass '0' as the number of iso packets. * * The driver must call usb_free_urb() when it is finished with the urb. */struct urb *usb_alloc_urb(int iso_packets, int mem_flags){	struct urb *urb;	urb = (struct urb *)kmalloc(sizeof(struct urb) + 		iso_packets * sizeof(struct usb_iso_packet_descriptor),		mem_flags);	if (!urb) {		err("alloc_urb: kmalloc failed");		return NULL;	}	memset(urb, 0, sizeof(*urb));	urb->count = (atomic_t)ATOMIC_INIT(1);	spin_lock_init(&urb->lock);	return urb;}/** * usb_free_urb - frees the memory used by a urb when all users of it are finished * @urb: pointer to the urb to free * * Must be called when a user of a urb is finished with it.  When the last user * of the urb calls this function, the memory of the urb is freed. * * Note: The transfer buffer associated with the urb is not freed, that must be * done elsewhere. */void usb_free_urb(struct urb *urb){	if (urb)		if (atomic_dec_and_test(&urb->count))			kfree(urb);}/** * usb_get_urb - incrementes the reference count of the urb * @urb: pointer to the urb to modify * * This must be  called whenever a urb is transfered from a device driver to a * host controller driver.  This allows proper reference counting to happen * for urbs. * * A pointer to the urb with the incremented reference counter is returned. */struct urb * usb_get_urb(struct urb *urb){	if (urb) {		atomic_inc(&urb->count);		return urb;	} else		return NULL;}				/*-------------------------------------------------------------------*//** * usb_submit_urb - asynchronously issue a transfer request for an endpoint * @urb: pointer to the urb describing the request * @mem_flags: the type of memory to allocate, see kmalloc() for a list *	of valid options for this. * * This submits a transfer request, and transfers control of the URB * describing that request to the USB subsystem.  Request completion will * indicated later, asynchronously, by calling the completion handler. * This call may be issued in interrupt context. * * The caller must have correctly initialized the URB before submitting * it.  Functions such as usb_fill_bulk_urb() and usb_fill_control_urb() are * available to ensure that most fields are correctly initialized, for * the particular kind of transfer, although they will not initialize * any transfer flags. * * Successful submissions return 0; otherwise this routine returns a * negative error number.  If the submission is successful, the complete * fuction of the urb will be called when the USB host driver is * finished with the urb (either a successful transmission, or some * error case.) * * Unreserved Bandwidth Transfers: * * Bulk or control requests complete only once.  When the completion * function is called, control of the URB is returned to the device * driver which issued the request.  The completion handler may then * immediately free or reuse that URB. * * Bulk URBs will be queued if the USB_QUEUE_BULK transfer flag is set * in the URB.  This can be used to maximize bandwidth utilization by * letting the USB controller start work on the next URB without any * delay to report completion (scheduling and processing an interrupt) * and then submit that next request. * * For control endpoints, the synchronous usb_control_msg() call is * often used (in non-interrupt context) instead of this call. * * Reserved Bandwidth Transfers: * * Periodic URBs (interrupt or isochronous) are completed repeatedly, * until the original request is aborted.  When the completion callback * indicates the URB has been unlinked (with a special status code), * control of that URB returns to the device driver.  Otherwise, the * completion handler does not control the URB, and should not change * any of its fields. * * Note that isochronous URBs should be submitted in a "ring" data * structure (using urb->next) to ensure that they are resubmitted * appropriately. * * If the USB subsystem can't reserve sufficient bandwidth to perform * the periodic request, and bandwidth reservation is being done for * this controller, submitting such a periodic request will fail. * * Memory Flags: * * General rules for how to decide which mem_flags to use: *  * Basically the rules are the same as for kmalloc.  There are four * different possible values; GFP_KERNEL, GFP_NOFS, GFP_NOIO and * GFP_ATOMIC. * * GFP_NOFS is not ever used, as it has not been implemented yet. * * There are three situations you must use GFP_ATOMIC. *    a) you are inside a completion handler, an interrupt, bottom half, *       tasklet or timer. *    b) you are holding a spinlock or rwlock (does not apply to *       semaphores) *    c) current->state != TASK_RUNNING, this is the case only after *       you've changed it. *  * GFP_NOIO is used in the block io path and error handling of storage * devices. * * All other situations use GFP_KERNEL. * * Specfic rules for how to decide which mem_flags to use: * *    - start_xmit, timeout, and receive methods of network drivers must *      use GFP_ATOMIC (spinlock) *    - queuecommand methods of scsi drivers must use GFP_ATOMIC (spinlock) *    - If you use a kernel thread with a network driver you must use *      GFP_NOIO, unless b) or c) apply *    - After you have done a down() you use GFP_KERNEL, unless b) or c) *      apply or your are in a storage driver's block io path *    - probe and disconnect use GFP_KERNEL unless b) or c) apply *    - Changing firmware on a running storage or net device uses *      GFP_NOIO, unless b) or c) apply * */int usb_submit_urb(struct urb *urb, int mem_flags){	if (urb && urb->dev && urb->dev->bus && urb->dev->bus->op)		return urb->dev->bus->op->submit_urb(urb, mem_flags);	else		return -ENODEV;}/*-------------------------------------------------------------------*//** * usb_unlink_urb - abort/cancel a transfer request for an endpoint * @urb: pointer to urb describing a previously submitted request * * This routine cancels an in-progress request.  The requests's * completion handler will be called with a status code indicating * that the request has been canceled, and that control of the URB * has been returned to that device driver.  This is the only way * to stop an interrupt transfer, so long as the device is connected. * * When the USB_ASYNC_UNLINK transfer flag for the URB is clear, this * request is synchronous.  Success is indicated by returning zero, * at which time the urb will have been unlinked, * and the completion function will see status -ENOENT.  Failure is * indicated by any other return value.  This mode may not be used * when unlinking an urb from an interrupt context, such as a bottom * half or a completion handler, * * When the USB_ASYNC_UNLINK transfer flag for the URB is set, this * request is asynchronous.  Success is indicated by returning -EINPROGRESS, * at which time the urb will normally not have been unlinked, * and the completion function will see status -ECONNRESET.  Failure is * indicated by any other return value. */int usb_unlink_urb(struct urb *urb){	if (urb && urb->dev && urb->dev->bus && urb->dev->bus->op)		return urb->dev->bus->op->unlink_urb(urb);	else		return -ENODEV;}/*-------------------------------------------------------------------* *                         SYNCHRONOUS CALLS                         * *-------------------------------------------------------------------*/struct usb_api_data {	wait_queue_head_t wqh;	int done;};static void usb_api_blocking_completion(struct urb *urb){	struct usb_api_data *awd = (struct usb_api_data *)urb->context;	awd->done = 1;	wmb();	wake_up(&awd->wqh);}// Starts urb and waits for completion or timeoutstatic int usb_start_wait_urb(struct urb *urb, int timeout, int* actual_length){ 	DECLARE_WAITQUEUE(wait, current);	struct usb_api_data awd;	int status;	init_waitqueue_head(&awd.wqh); 		awd.done = 0;	set_current_state(TASK_UNINTERRUPTIBLE);	add_wait_queue(&awd.wqh, &wait);	urb->context = &awd;	status = usb_submit_urb(urb, GFP_KERNEL);	if (status) {		// something went wrong		usb_free_urb(urb);		set_current_state(TASK_RUNNING);		remove_wait_queue(&awd.wqh, &wait);		return status;	}	while (timeout && !awd.done)	{		timeout = schedule_timeout(timeout);		set_current_state(TASK_UNINTERRUPTIBLE);		rmb();	}	set_current_state(TASK_RUNNING);	remove_wait_queue(&awd.wqh, &wait);	if (!timeout && !awd.done) {		if (urb->status != -EINPROGRESS) {	/* No callback?!! */			printk(KERN_ERR "usb: raced timeout, "			    "pipe 0x%x status %d time left %d\n",			    urb->pipe, urb->status, timeout);			status = urb->status;		} else {			printk("usb_control/bulk_msg: timeout\n");			usb_unlink_urb(urb);  // remove urb safely			status = -ETIMEDOUT;		}	} else		status = urb->status;	if (actual_length)		*actual_length = urb->actual_length;	usb_free_urb(urb);  	return status;}/*-------------------------------------------------------------------*/// returns status (negative) or length (positive)int usb_internal_control_msg(struct usb_device *usb_dev, unsigned int pipe, 			    struct usb_ctrlrequest *cmd,  void *data, int len, int timeout){	struct urb *urb;	int retv;	int length;	urb = usb_alloc_urb(0, GFP_KERNEL);	if (!urb)		return -ENOMEM;  	FILL_CONTROL_URB(urb, usb_dev, pipe, (unsigned char*)cmd, data, len,		   usb_api_blocking_completion, 0);	retv = usb_start_wait_urb(urb, timeout, &length);	if (retv < 0)		return retv;	else		return length;}/** *	usb_control_msg - Builds a control urb, sends it off and waits for completion *	@dev: pointer to the usb device to send the message to *	@pipe: endpoint "pipe" to send the message to *	@request: USB message request value *	@requesttype: USB message request type value *	@value: USB message value *	@index: USB message index value *	@data: pointer to the data to send *	@size: length in bytes of the data to send *	@timeout: time in jiffies to wait for the message to complete before *		timing out (if 0 the wait is forever) *	Context: !in_interrupt () * *	This function sends a simple control message to a specified endpoint *	and waits for the message to complete, or timeout. *	 *	If successful, it returns the number of bytes transferred, otherwise a negative error number. * *	Don't use this function from within an interrupt context, like a *	bottom half handler.  If you need an asynchronous message, or need to send *	a message from within interrupt context, use usb_submit_urb() */int usb_control_msg(struct usb_device *dev, unsigned int pipe, __u8 request, __u8 requesttype,			 __u16 value, __u16 index, void *data, __u16 size, int timeout){	struct usb_ctrlrequest *dr = kmalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL);	int ret;		if (!dr)		return -ENOMEM;	dr->bRequestType= requesttype;	dr->bRequest = request;	dr->wValue = cpu_to_le16p(&value);	dr->wIndex = cpu_to_le16p(&index);	dr->wLength = cpu_to_le16p(&size);	//dbg("usb_control_msg");		ret = usb_internal_control_msg(dev, pipe, dr, data, size, timeout);	kfree(dr);	return ret;}/** *	usb_bulk_msg - Builds a bulk urb, sends it off and waits for completion *	@usb_dev: pointer to the usb device to send the message to *	@pipe: endpoint "pipe" to send the message to *	@data: pointer to the data to send *	@len: length in bytes of the data to send *	@actual_length: pointer to a location to put the actual length transferred in bytes *	@timeout: time in jiffies to wait for the message to complete before *		timing out (if 0 the wait is forever) *	Context: !in_interrupt () * *	This function sends a simple bulk message to a specified endpoint *	and waits for the message to complete, or timeout. *	 *	If successful, it returns 0, otherwise a negative error number. *	The number of actual bytes transferred will be stored in the  *	actual_length paramater. * *	Don't use this function from within an interrupt context, like a *	bottom half handler.  If you need an asynchronous message, or need to *	send a message from within interrupt context, use usb_submit_urb() */int usb_bulk_msg(struct usb_device *usb_dev, unsigned int pipe, 			void *data, int len, int *actual_length, int timeout){	struct urb *urb;	if (len < 0)		return -EINVAL;	urb=usb_alloc_urb(0, GFP_KERNEL);	if (!urb)		return -ENOMEM;	FILL_BULK_URB(urb, usb_dev, pipe, data, len,		    usb_api_blocking_completion, 0);	return usb_start_wait_urb(urb,timeout,actual_length);}/** * 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);}/*-------------------------------------------------------------------*/static int usb_parse_endpoint(struct usb_endpoint_descriptor *endpoint, unsigned char *buffer, int size){	struct usb_descriptor_header *header;

⌨️ 快捷键说明

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