Linux USB mouse

Source: Internet
Author: User
4.2.1 usb_endpoint_is_int_in
Determine whether the endpoint is of the interrupt in Type
Static inline int usb_endpoint_is_int_in (const struct usb_endpoint_descriptor * EPD)
{
Return (usb_endpoint_xfer_int (EPD) & usb_endpoint_dir_in (EPD ));
}
Static inline int usb_endpoint_xfer_int (const struct usb_endpoint_descriptor * EPD)
{
Return (EPD-> bmattributes & usb_endpoint_xfertype_mask) = usb_endpoint_xfer_int );
}
The lower two bmattributes In the endpoint descriptor is used to indicate the endpoint transmission type, which is divided into four types:
# Define usb_endpoint_xfertype_mask 0x03/* In bmattributes */
# Define usb_endpoint_xfer_control 0 -- Control Transmission
# Define usb_endpoint_xfer_isoc 1 -- same time Transmission
# Define usb_endpoint_xfer_bulk 2 -- block transfer
# Define usb_endpoint_xfer_int 3 -- interrupt transmission static inline int usb_endpoint_dir_in (const struct usb_endpoint_descriptor * EPD)
{
Return (EPD-> bendpointaddress & usb_endpoint_dir_mask) = usb_dir_in );
}
The highest bit of bendpointaddress is used to indicate the transmission direction. It is located on the host. In, only the device to the host is used, and out indicates the host to the device. The lower four bits are used to indicate the device address. Therefore, each hub has the most
31 devices are mounted.
At the same time, the detection of other endpoint types is similar, for example, whether the detection endpoint is block in type: usb_endpoint_is_bulk_in. 4.2.2 usb_rcvintpipe
Set the endpoint information. pipe is actually an int type data. The endpoint information of the specific target struct usb_device sent by urb.
# Define usb_rcvintpipe (Dev, endpoint) (pipe_interrupt <30) | _ create_pipe (Dev, endpoint) | usb_dir_in)
Static inline unsigned int _ create_pipe (struct usb_device * Dev, unsigned int endpoint)
{
Return (Dev-> devnum <8) | (endpoint <15 );
}
Pipe consists of four parts. The highest two bits are used to indicate the pipe type. The BITs between 15th and 23 are used to indicate the endpoint-> bendpointaddress information, and the second to 8th bits are used to indicate the number of the device, 7bit
Used to indicate the direction of the data stream.
I personally think that the endpoint here is very easy to misunderstand. If it is written as endpointaddress, it will be better understood, so that when you see this API, you will know what the input parameter is.
Create various pipelines:
# Define usb_sndctrlpipe (Dev, endpoint) -- set the specified port of the specified USB device to a control out endpoint
# Define usb_rcvctrlpipe (Dev, endpoint) -- set the specified port of the specified USB device to a control in endpoint
# Define usb_sndisocpipe (Dev, endpoint) -- set the specified port of the specified USB device to an out endpoint
# Define usb_rcvisocpipe (Dev, endpoint) -- set the specified port of the specified USB device to an in endpoint
# Define usb_sndbulkpipe (Dev, endpoint) -- set the specified port of the specified USB device to an out endpoint.
# Define usb_rcvbulkpipe (Dev, endpoint) -- set the specified port of the specified USB device to an in endpoint.
# Define usb_sndintpipe (Dev, endpoint) -- set the specified port of the specified USB device to an out-of-interruption endpoint.
# Define usb_rcvintpipe (Dev, endpoint) -- set the specified port of the specified USB device to an in-interrupt endpoint.
PIPE is easy to understand. It is to set the specified port of the specified USB device to a type of port, that is, to save the endpoint information. 4.2.3 usb_maxpacket
Obtains the maximum number of data supported by the endpoint.
Static inline _ 2010usb_maxpacket (struct usb_device * udev, int pipe, int is_out)
{
Struct usb_host_endpoint * EP;
Unsigned epnum = usb_pipeendpoint (PIPE); -- get the device number (pipe> 15) & 0x0f

If (is_out ){
Warn_on (usb_pipein (PIPE ));
Ep = udev-> ep_out [epnum]; -- get the endpoint
} Else {
Warn_on (usb_pipeout (PIPE ));
Ep = udev-> ep_in [epnum];
}
If (! EP)
Return 0;/* Note: Only 0x07ff bits are for packet size ...*/
Return le16_to_cpu (EP-> DESC. wmaxpacketsize); -- get endpoint Information
} 4.2.4 usb_buffer_alloc
A buffer zone can be understood literally. The parameter is easy to understand. Generally, mem_flags is gfp_kernel, but it cannot be blocked in the interrupt context and is set to gfp_atomic.
Void * usb_buffer_alloc (struct usb_device * Dev, size_t size, gfp_t mem_flags, dma_addr_t * DMA)
{
If (! Dev |! Dev-> Bus)
Return NULL;
Return hcd_buffer_alloc (Dev-> bus, size, mem_flags, DMA );
} Void * hcd_buffer_alloc (struct usb_bus * bus, size_t size, gfp_t mem_flags, dma_addr_t * DMA)
{
Struct usb_hcd * HCD = bus_to_hcd (bus );
Int I;/* Some USB hosts just use Pio */
If (! Bus> controller> dma_mask &&! (HCD-> driver-> flags & hcd_local_mem )){
* DMA = ~ (Dma_addr_t) 0;
Return kmalloc (size, mem_flags );
} For (I = 0; I If (size <= pool_max [I]) -- when the size is within hcd_buffer_pools, it is directly allocated in the pool.
Return dma_pool_alloc (HCD-> pool [I], mem_flags, DMA );
}
Return dma_alloc_coherent (HCD-> self. Controller, size, DMA, 0); -- allocate DMA Space
} 4.2.5 usb_alloc_urb
In the USB system, urb is very important. Its kernel communicates with the USB device through urb. This type is the same as a liaison officer, connecting the driver and the device.
Urb is a huge struct, but for driver engineers, we need to understand the following members:
Struct usb_device * dev; pointer of the target USB device sent by urb.
Unsigned int pipe; this is not to mention. This is the specific Endpoint information sent to a specific target. As for how to set pipe.
Unsigned int transfer_flags; transmission identifier
Urb_short_not_ OK -- read any possible in endpoint should be err
Urb_iso_asap -- If urb is of the same time, this will be viewed later when urb is waiting.
Urb_no_transfer_dma_map -- this bit should be set when urb contains a buffer that is about to transmit the DMA buffer. The USB core uses the buffer directed by transfer_dma instead of the address pointed by transfer_buffer.
Urb_no_setup_dma_map -- similar to the above. USB core uses the setup_dma variable to point to the buffer, rather than setup_packet.s
Urb_no_fsbr
Urb_zero_packet
Urb_no_interrupt
Void * transfer_buffer refers to the buffer used to send data to the device (Out urb) or receive data (in urb) from the device. The buffer must be created using kmalloc instead of in the stack or static memory.
Dma_addr_t transfer_dma is used to transmit data to the buffer zone of the USB device in DMA mode.
Int transfer_buffer_length the size of the buffer to which the two parameters point
Unsigned char * setup_packet points to the pointer to the set data packet that controls the urb
Dma_addr_t setup_dma
Usb_complete_t complete end processing function pointer, called when urb is completely transmitted or an error occurs.
Void * context is similar to the private data of a urb.
The actual length of the message sent or received by the int actual_length urb.
Int status urb end or called status
Int start_frame is used to set or return the number of initial frames.
Int interval urb polling Interval
The above members are important. Struct urb * usb_alloc_urb (INT iso_packets, gfp_t mem_flags)
{
Struct urb * urb;
Urb = kmalloc (sizeof (struct urb) + iso_packets * sizeof (struct usb_iso_packet_descriptor), mem_flags );
If (! Urb ){
Printk (kern_err "alloc_urb: kmalloc failed \ n ");
Return NULL;
}
Usb_init_urb (urb );
Return urb;
} Void usb_init_urb (struct urb * urb)
{
If (urb ){
Memset (urb, 0, sizeof (* urb ));
Kref_init (& urb-> kref );
Init_list_head (& urb-> anchor_list );
}
} 4.2.6 usb_fill_int_urb
After applying for urb, you must assign values to the urb parameters. For interrupt, control, and batch urb, there are initialization functions for urb.
Take usb_fill_int_urb as an example:
Static inline 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_fn, void * context, int interval)
{
Urb-> Dev = dev;
Urb-> pipe = pipe;
Urb-> transfer_buffer = transfer_buffer;
Urb-> transfer_buffer_length = buffer_length;
Urb-> complete = complete_fn;
Urb-> context = context;
If (Dev-> speed = usb_speed_high)
Urb-> interval = 1 <(interval-1 );
Else
Urb-> interval = interval;
Urb-> start_frame =-1;
}
There are similar initialization functions for controlling both urb and batch urb.
Static inline 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_fn, void * context)
Static inline void encode (struct urb * urb, struct usb_device * Dev, unsigned int pipe, unsigned char * setup_packet, void * transfer_buffer, int buffer_length, complete complete_fn, void * context)
For urb filling, values are assigned to some urb members. 4.2.7 usb_submit_urb
Int usb_submit_urb (struct urb * urb, gfp_t mem_flags)
The urb parameter is the previous urb.
Mem_flags has three values available
Gfp_atomic
In an urb end processing function, the caller terminates the processing function at the bottom half. In the tasklet or timer callback function.
The caller is holding a spin lock or read/write lock.
In fact, the settings of this identifier are actually to avoid sleep.
Gfp_noio
This value should be used when the driver is in the block I/O path.
Gfp_kernel
Summary
A Linux USB driver is generally a driver for a certain interface of the device. Here, we mainly apply for and submit urb.

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.