USB Device---URB request fast

Source: Internet
Author: User

1. URB Structural Body
USB Request block (USB requestor block,urb) is a USB device driver used to describe the basic carrier and core data structure for communication with USB devices, very similar to the SK_BUFF structure in network device drivers.

struct URB {/* Private: Fields that can only be accessed by the USB core and host controller */struct kref kref;/*urb reference count */void *hcpriv;/* Host controller private data */atomic_t Use_count; /* Concurrent transfer count */u8 reject; /* Transmission will fail */int unlink; /* Unlink Error code */* Public: The field that can be driven to use */struct list_head urb_list; /* Chain head */struct usb_anchor *anchor; struct Usb_device *dev; /* Associated USB device */struct usb_host_endpoint *ep;unsigned int pipe; /* Pipe information */INT status; /* Current Status of URB */unsigned int transfer_flags; /* URB_SHORT_NOT_OK | ... */void *transfer_buffer; /* buffer to send data to the device or receive data from the device */dma_addr_t TRANSFER_DMA; /* The buffer used to transmit data to the device in DMA */int transfer_buffer_length;/*transfer_buffer or TRANSFER_DMA points to the buffer size */int actual_length; /* URB The actual length of the data sent or received after the end of */unsigned char *setup_packet; /* Pointer to the set packet that controls URB */dma_addr_t SETUP_DMA; /* Control URB Set the DMA buffer for the packet */int start_frame; /* for setting or returning the initial frame */int number_of_packets; /* Equal time transmission medium Time Buffer number */int interval; /* URB the interval to be polled (valid for interrupts and URB) */int error_count; /* Number of transmission errors */void *context; /* Completion Function Context */usb_complete_t complete; /* When URB is fully 

  

2. URB Processing Process
Each endpoint in a USB device processes a urb queue, a typical life cycle of a URB before the queue is emptied
As follows.
(1) created by a USB device driver.
The functions for creating a URB struct are:
struct URB *usb_alloc_urb (int iso_packets, int mem_flags);
Iso_packets is the number of equal-time packets that this URB should contain, and if 0 indicates that no wait-time packets are created.
The Mem_flags parameter is the flag that allocates memory, and the Kmalloc () function assigns a flag parameter that has the same meaning. If the assignment succeeds, the function returns a URB struct pointer, otherwise 0 is returned.
URB structures cannot be created statically in a drive, as this may corrupt the reference counting method used by the USB core to Urb.

Usb_alloc_urb () "Inverse function" is:
void Usb_free_urb (struct urb *urb);
This function is used to release the URB struct that is allocated by Usb_alloc_urb ().

The

(2) is initialized and is assigned to a specific endpoint of a particular USB device.
for interrupt Urb, use the Usb_fill_int_urb () function to initialize the URB as follows:
void usb_fill_int_urb (struct urb *urb, struct Usb_device *dev, unsigned int pipe, void *transfer_buffer,int buffer_length, usb_complete_t complete,void *context, int interval); The
Urb parameter points to the pointer to the URB to be initialized, and dev points to the USB device to which the URB is to be sent, the pipe is the specific endpoint of the USB device to which the URB is to be sent, and Transfer_buffer is a pointer to the buffer that sends or receives data. Like URB, it cannot be a static buffer, it must be allocated using Kmalloc (), Buffer_length is the size of the buffer that the Transfer_buffer pointer points to, and the complete pointer points to the completion handler that is called when the URB is completed The context is the "contexts" in which the processing function is completed; interval is the interval at which this urb should be dispatched. The pipe in the
above function parameter is created using Usb_sndintpipe () or usb_rcvintpipe ().
for bulk URB, use the Usb_fill_bulk_urb () function to initialize the URB as follows:
void usb_fill_bulk_urb (struct urb *urb, struct Usb_device *dev, unsigned int pipe, void *transfer_buffer,int buffer_length, usb_complete_t complete,void *context); The
parameter of the function is the same as the parameter of the Usb_fill_int_urb () function, except that there is no interval parameter corresponding to the dispatch interval. The pipe in the
above function parameter is created using the Usb_sndbulkpipe () or usb_rcvbulkpipe () function.


For control URB, use the Usb_fill_control_urb () function to initialize the URB, as follows:
void Usb_fill_control_urb (struct urb *urb, struct usb_device *dev,unsigned int pipe, unsigned char *setup_packet,void *tra Nsfer_buffer, int buffer_length,usb_complete_t complete, void *context);
In addition to adding a new Setup_packet parameter, the parameters of the function are the same as the parameters of the Usb_fill_bulk_urb () function. The Setup_packet parameter points to the provisioning packet that is about to be sent to the endpoint.
The pipe in the above function parameter is created using the Usb_sndctrlpipe () or usb_rcvictrlpipe () function.

(3) The USB device driver is submitted to the USB core.
After completing the creation and initialization of URB (1), (2), the URB can be submitted to the USB core via the Usb_submit_urb () function, as follows:
int usb_submit_urb (struct URB *urb, int mem_flags);
The URB parameter is a pointer to URB, and the Mem_flags parameter is the same as the one passed to the Kmalloc () function parameter, which tells the USB core how to allocate a memory buffer at this point.
After committing URB to the USB core, do not access any members of URB until the completion function is called.


Usb_submit_urb () can be called in both the atomic context and the process context, and the mem_flags variable needs to be based on the call ring
Settings as shown below.
Gfp_atomic: This flag should be used in interrupt handling functions, bottom halves, tasklet, timer handlers, and URB completion functions when the caller holds a spin lock or read-write lock and when the driver modifies current→state to non-task_running.
Gfp_noio: This flag should be used in the block I/O and error handling paths of storage devices;
Gfp_kernel: If there is no reason to use gfp_atomic and Gfp_noio, use Gfp_kernel.

If the Usb_submit_urb () call succeeds, that is, the control of URB is handed over to the USB core, and the function returns 0;
Returns the error number.
(4) Submit the USB host controller driver specified by the USB core.
(5) by the USB host controller processing, once to the USB device transmission.
The steps (4) ~ (5) are completed by the USB core and the host controller and are not controlled by USB device drivers.
(6) When URB completes, USB host controller driver notifies USB device driver.

In the following 3 cases, the URB will end, and the URB completion function will be called.
1, URB was successfully sent to the device, and the device returned the correct confirmation. If Urb→status is 0, the data is sent successfully for an output URB, and the requested data is received successfully for an input URB.
2. If an error occurs when sending data to the device or receiving data from the device, Urb→status will record the error value.
3. URB is removed from the USB core "connection", which occurs when the driver cancels Urb via the usb_unlink_urb () or usb_kill_urb () function, or if the URB has been submitted while the USB device is unplugged.

When URB Life ends (processing is done or unlinked), the reason is known by the status member of the URB struct,
If 0 means the transmission succeeds,-enoent is killed by Usb_kill_urb (),-econnreset is Usb_unlink_urb ()
Kill,-eproto indicates that a bitstuff error occurred in the transmission or the hardware failed to receive the response packet in a timely manner-enodev
Indicates that the USB device has been removed,-exdev indicates that the transmission is only partially completed, and so on.

A summary of the above URB processing steps, figure 20.5 gives a urb the entire processing flow, the dashed box usb_unlink_urb () and usb_kill_urb () does not necessarily occur, it is only in the URB is being the USB core and host controller When the driver is canceled.

USB Device---URB request fast

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.