USB Device Driver Summary

Source: Internet
Author: User

1 USB Device Driver Summary
2
3 1. Write the main work of USB driver
4 First, it is clear that the main task of writing a USB drive is to complete the probe () and disconnect () functions. They are called when the device is plugged in and disconnected.
5 These two functions primarily complete initialization and release of hardware and software resources.
6
7 2. The main role of Usb_driver
8 and Platform_driver Similar, usb_driver just played the role of strings. The drive structure of the device to which the USB itself belongs does not hang on the USB bus
9, but there has been a lot of change in the way it was accessed, with no access to I/O and I ports, in exchange for the URB request block.
10 Therefore, Usb_driver only has a job to find a USB device, manage connections, and disconnect connections. The specific data communication function is depend on the corresponding equipment class
11 drive to achieve.
12
13 3. The driver is how to find the appropriate device.
14 The answer is by Vender ID and Product ID.
15
* * Define These values to match your devices * *
#define USB_SKEL_VENDOR_ID 0x061a
#define USB_SKEL_PRODUCT_ID 0x0200
19
/* Table of devices that work with this driver * *
21stStaticstructusb_device_id skel_table[] = {
{Usb_device (usb_skel_vendor_id, usb_skel_product_id)},
{}/* Terminating entry * *
24};
Module_device_table (USB, skel_table);
26
27 4. How to set the minor number
/* Get a minor range for your devices from the USB maintainer * *
#define USB_SKEL_MINOR_BASE 240
30
31StaticstructUsb_class_driver Skel_class = {
Name = "Skel%d",
FoPs = &skel_fops,
Minor_base = Usb_skel_minor_base,
35};
36..
Panax Notoginseng retval = Usb_register_dev (interface, &skel_class);
38..
39
40 5. The processing process of URB
41 First, manually set, the entire process to go through, the transmission of a large number of information used
Usb_alloc_urb (); Application
Usb_fill_bulk_urb (); Initialization parameters
Usb_submit_urb (); Submit
45
46 The second type, simple batch and control URB
(1) usb_bulk_msg ()
48 This function creates a USB bulk URB and sends it to a specific device, the function is synchronized, and it waits until URB
49 is finished before returning.
50intUsb_bulk_msg (structUsb_device *usb_dev,
51unsignedintPipe
52void*data,//buffer
53intLen,//[in], data length
54int*actual_length,//[out], actual data length of send or receive
55intTimeout);
(2) Usb_control_msg ()
57 has a similar function to usb_bulk_msg, but it provides the ability to drive to send and end URB control information rather than bulk information.
58intUsb_control_msg (structUsb_device *dev,
59unsignedintPipe
__U8 request,
__u8 RequestType,
__u16 value,
__u16 Index,
64void*data,
__u16 size,
66intTimeout);
67
68 6. How to read a register
* *--------------------------------------------------------------------------/*
70/**
* @Brief: Fps200_usb_readregister
72 *
@Param: Usbdev
@Param: Reg
* @Param: Data
76 *
* @Returns:
78 */
* *--------------------------------------------------------------------------/*
80StaticintFps200_usb_readregister (structUsb_device *usbdev,
81intRegunsignedChar*data) {
82 returnUsb_control_msg (Usbdev, * the device * *
Usb_rcvctrlpipe (Usbdev, 0),/* Pipe * *
Fps200_usb_read_request, * REQUEST * *
Usb_type_vendor | usb_dir_in |
usb_recip_device,/* Bmrequesttype * *
Reg,/* Value * *
0,/* Index * *
Data,
1,//the length of data
Fps200_usb_timeout);
92}
93
94 7. How to write a register
* *--------------------------------------------------------------------------* *
96/**
Fps200_usb_writeregister * @Brief:
98 *
Usbdev * @Param:
* @Param: Reg
@Param: Data
102 *
* @Returns:
104 */
I/*--------------------------------------------------------------------------* *
106StaticintFps200_usb_writeregister (structUsb_device *usbdev,
107intRegunsignedCharData) {
108 returnUsb_control_msg (Usbdev, * the device * *
109 Usb_sndctrlpipe (Usbdev, 0),/* Pipe *
Fps200_usb_write_request, * REQUEST * *
Usb_type_vendor | Usb_dir_out |
112 usb_recip_device,/* Bmrequesttype *
113 (Data << 8) | Reg,/* value, 16bits * *
114 0,/* Index * *
//data NULL, buffer
116 0,//size, 0
117 fps200_usb_timeout);
118}
119

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.