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

📄 usb.c

📁 讲述linux的初始化过程
💻 C
📖 第 1 页 / 共 5 页
字号:
	memset(urb, 0, sizeof(*urb));	spin_lock_init(&urb->lock);	return urb;}/** *	usb_free_urb - frees the memory used by a urb *	@urb: pointer to the urb to free * *	If an urb is created with a call to usb_create_urb() it should be *	cleaned up with a call to usb_free_urb() when the driver is finished *	with it. */void usb_free_urb(urb_t* urb){	if (urb)		kfree(urb);}/*-------------------------------------------------------------------*/int usb_submit_urb(urb_t *urb){	if (urb && urb->dev)		return urb->dev->bus->op->submit_urb(urb);	else		return -ENODEV;}/*-------------------------------------------------------------------*/int usb_unlink_urb(urb_t *urb){	if (urb && urb->dev)		return urb->dev->bus->op->unlink_urb(urb);	else		return -ENODEV;}/*-------------------------------------------------------------------* *                     COMPLETION HANDLERS                           * *-------------------------------------------------------------------*//*-------------------------------------------------------------------* * completion handler for compatibility wrappers (sync control/bulk) * *-------------------------------------------------------------------*/static void usb_api_blocking_completion(urb_t *urb){	api_wrapper_data *awd = (api_wrapper_data *)urb->context;	if (waitqueue_active(awd->wakeup))		wake_up(awd->wakeup);#if 0	else		dbg("(blocking_completion): waitqueue empty!"); 		// even occurs if urb was unlinked by timeout...#endif}/*-------------------------------------------------------------------* *                         COMPATIBILITY STUFF                       * *-------------------------------------------------------------------*/// Starts urb and waits for completion or timeoutstatic int usb_start_wait_urb(urb_t *urb, int timeout, int* actual_length){ 	DECLARE_WAITQUEUE(wait, current);	DECLARE_WAIT_QUEUE_HEAD(wqh);	api_wrapper_data awd;	int status;  	awd.wakeup = &wqh;	init_waitqueue_head(&wqh); 		current->state = TASK_INTERRUPTIBLE;	add_wait_queue(&wqh, &wait);	urb->context = &awd;	status = usb_submit_urb(urb);	if (status) {		// something went wrong		usb_free_urb(urb);		current->state = TASK_RUNNING;		remove_wait_queue(&wqh, &wait);		return status;	}	if (urb->status == -EINPROGRESS) {		while (timeout && urb->status == -EINPROGRESS)			status = timeout = schedule_timeout(timeout);	} else		status = 1;	current->state = TASK_RUNNING;	remove_wait_queue(&wqh, &wait);	if (!status) {		// timeout		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, 			    devrequest *cmd,  void *data, int len, int timeout){	urb_t *urb;	int retv;	int length;	urb = usb_alloc_urb(0);	if (!urb)		return -ENOMEM;  	FILL_CONTROL_URB(urb, usb_dev, pipe, (unsigned char*)cmd, data, len,    /* build urb */  		   (usb_complete_t)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 to wait for the message to complete before timing out (if 0 the wait is forever) * *	This function sends a simple control message to a specified endpoint *	and waits for the message to complete, or timeout. *	 *	If successful, it returns 0, othwise a negative error number. * *	Don't use this function from within an interrupt context, like a *	bottom half handler.  If you need a asyncronous 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){	devrequest *dr = kmalloc(sizeof(devrequest), GFP_KERNEL);	int ret;		if (!dr)		return -ENOMEM;	dr->requesttype = requesttype;	dr->request = request;	dr->value = cpu_to_le16p(&value);	dr->index = cpu_to_le16p(&index);	dr->length = 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 transfered in bytes *	@timeout: time to wait for the message to complete before timing out (if 0 the wait is forever) * *	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, othwise a negative error number. *	The number of actual bytes transferred will be plaed in the  *	actual_timeout paramater. * *	Don't use this function from within an interrupt context, like a *	bottom half handler.  If you need a asyncronous 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){	urb_t *urb;	if (len < 0)		return -EINVAL;	urb=usb_alloc_urb(0);	if (!urb)		return -ENOMEM;	FILL_BULK_URB(urb,usb_dev,pipe,(unsigned char*)data,len,   /* build urb */			(usb_complete_t)usb_api_blocking_completion,0);	return usb_start_wait_urb(urb,timeout,actual_length);}/* * usb_get_current_frame_number() * * returns the current frame number for the parent USB bus/controller * of the given USB device. */int usb_get_current_frame_number(struct usb_device *usb_dev){	return usb_dev->bus->op->get_frame_number (usb_dev);}/*-------------------------------------------------------------------*/static int usb_parse_endpoint(struct usb_device *dev, struct usb_endpoint_descriptor *endpoint, unsigned char *buffer, int size){	struct usb_descriptor_header *header;	unsigned char *begin;	int parsed = 0, len, numskipped;	header = (struct usb_descriptor_header *)buffer;	/* Everything should be fine being passed into here, but we sanity */	/*  check JIC */	if (header->bLength > size) {		err("ran out of descriptors parsing");		return -1;	}			if (header->bDescriptorType != USB_DT_ENDPOINT) {		warn("unexpected descriptor 0x%X, expecting endpoint descriptor, type 0x%X",			endpoint->bDescriptorType, USB_DT_ENDPOINT);		return parsed;	}	if (header->bLength == USB_DT_ENDPOINT_AUDIO_SIZE)		memcpy(endpoint, buffer, USB_DT_ENDPOINT_AUDIO_SIZE);	else		memcpy(endpoint, buffer, USB_DT_ENDPOINT_SIZE);		le16_to_cpus(&endpoint->wMaxPacketSize);	buffer += header->bLength;	size -= header->bLength;	parsed += header->bLength;	/* Skip over the rest of the Class Specific or Vendor Specific */	/*  descriptors */	begin = buffer;	numskipped = 0;	while (size >= sizeof(struct usb_descriptor_header)) {		header = (struct usb_descriptor_header *)buffer;		if (header->bLength < 2) {			err("invalid descriptor length of %d", header->bLength);			return -1;		}		/* If we find another descriptor which is at or below us */		/*  in the descriptor heirarchy then we're done  */		if ((header->bDescriptorType == USB_DT_ENDPOINT) ||		    (header->bDescriptorType == USB_DT_INTERFACE) ||		    (header->bDescriptorType == USB_DT_CONFIG) ||		    (header->bDescriptorType == USB_DT_DEVICE))			break;		dbg("skipping descriptor 0x%X",			header->bDescriptorType);		numskipped++;		buffer += header->bLength;		size -= header->bLength;		parsed += header->bLength;	}	if (numskipped)		dbg("skipped %d class/vendor specific endpoint descriptors", numskipped);	/* Copy any unknown descriptors into a storage area for drivers */	/*  to later parse */	len = (int)(buffer - begin);	if (!len) {		endpoint->extra = NULL;		endpoint->extralen = 0;		return parsed;	}	endpoint->extra = kmalloc(len, GFP_KERNEL);	if (!endpoint->extra) {		err("couldn't allocate memory for endpoint extra descriptors");		endpoint->extralen = 0;		return parsed;	}	memcpy(endpoint->extra, begin, len);	endpoint->extralen = len;	return parsed;}static int usb_parse_interface(struct usb_device *dev, struct usb_interface *interface, unsigned char *buffer, int size){	int i, len, numskipped, retval, parsed = 0;	struct usb_descriptor_header *header;	struct usb_interface_descriptor *ifp;	unsigned char *begin;	interface->act_altsetting = 0;	interface->num_altsetting = 0;	interface->max_altsetting = USB_ALTSETTINGALLOC;	interface->altsetting = kmalloc(sizeof(struct usb_interface_descriptor) * interface->max_altsetting, GFP_KERNEL);		if (!interface->altsetting) {		err("couldn't kmalloc interface->altsetting");		return -1;	}	while (size > 0) {		if (interface->num_altsetting >= interface->max_altsetting) {			void *ptr;			int oldmas;			oldmas = interface->max_altsetting;			interface->max_altsetting += USB_ALTSETTINGALLOC;			if (interface->max_altsetting > USB_MAXALTSETTING) {				warn("too many alternate settings (max %d)",					USB_MAXALTSETTING);				return -1;			}			ptr = interface->altsetting;			interface->altsetting = kmalloc(sizeof(struct usb_interface_descriptor) * interface->max_altsetting, GFP_KERNEL);			if (!interface->altsetting) {				err("couldn't kmalloc interface->altsetting");				interface->altsetting = ptr;				return -1;			}			memcpy(interface->altsetting, ptr, sizeof(struct usb_interface_descriptor) * oldmas);			kfree(ptr);		}		ifp = interface->altsetting + interface->num_altsetting;		interface->num_altsetting++;		memcpy(ifp, buffer, USB_DT_INTERFACE_SIZE);		/* Skip over the interface */		buffer += ifp->bLength;		parsed += ifp->bLength;		size -= ifp->bLength;		begin = buffer;		numskipped = 0;		/* Skip over any interface, class or vendor descriptors */		while (size >= sizeof(struct usb_descriptor_header)) {			header = (struct usb_descriptor_header *)buffer;			if (header->bLength < 2) {				err("invalid descriptor length of %d", header->bLength);				return -1;			}			/* If we find another descriptor which is at or below */			/*  us in the descriptor heirarchy then return */			if ((header->bDescriptorType == USB_DT_INTERFACE) ||			    (header->bDescriptorType == USB_DT_ENDPOINT) ||			    (header->bDescriptorType == USB_DT_CONFIG) ||			    (header->bDescriptorType == USB_DT_DEVICE))				break;			numskipped++;			buffer += header->bLength;			parsed += header->bLength;			size -= header->bLength;		}		if (numskipped)			dbg("skipped %d class/vendor specific interface descriptors", numskipped);		/* Copy any unknown descriptors into a storage area for */		/*  drivers to later parse */		len = (int)(buffer - begin);		if (!len) {			ifp->extra = NULL;			ifp->extralen = 0;		} else {			ifp->extra = kmalloc(len, GFP_KERNEL);			if (!ifp->extra) {				err("couldn't allocate memory for interface extra descriptors");				ifp->extralen = 0;				return -1;			}			memcpy(ifp->extra, begin, len);			ifp->extralen = len;		}		/* Did we hit an unexpected descriptor? */		header = (struct usb_descriptor_header *)buffer;		if ((size >= sizeof(struct usb_descriptor_header)) &&		    ((header->bDescriptorType == USB_DT_CONFIG) ||		     (header->bDescriptorType == USB_DT_DEVICE)))			return parsed;		if (ifp->bNumEndpoints > USB_MAXENDPOINTS) {			warn("too many endpoints");			return -1;		}		ifp->endpoint = (struct usb_endpoint_descriptor *)			kmalloc(ifp->bNumEndpoints *			sizeof(struct usb_endpoint_descriptor), GFP_KERNEL);		if (!ifp->endpoint) {			err("out of memory");			return -1;			}		memset(ifp->endpoint, 0, ifp->bNumEndpoints *			sizeof(struct usb_endpoint_descriptor));			for (i = 0; i < ifp->bNumEndpoints; i++) {			header = (struct usb_descriptor_header *)buffer;			if (header->bLength > size) {				err("ran out of descriptors parsing");				return -1;			}					retval = usb_parse_endpoint(dev, ifp->endpoint + i, buffer, size);			if (retval < 0)				return retval;			buffer += retval;			parsed += retval;			size -= retval;		}		/* We check to see if it's an alternate to this one */		ifp = (struct usb_interface_descriptor *)buffer;		if (size < USB_DT_INTERFACE_SIZE ||		    ifp->bDescriptorType != USB_DT_INTERFACE ||		    !ifp->bAlternateSetting)			return parsed;	}	return parsed;}

⌨️ 快捷键说明

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