USB Device Driver Implementation
AumRaj Design Systems Pvt Ltd.
An ISO 9001:2015 Certified, VLSI, Embedded and Product Engineering Company
Introduction : When linux kernel was developed only few USB devices were supported by its kernel such as mouse, keyboard etc. The latest linux kernel 6.1.7 has been released on 18 Jan. 2023.? Linux supports almost all USB device class types viz. storage class, human?interface class, communication class etc. Standard USB devices supported are keyboards, mice,?modems, networking cards, speakers, printers etc. List of supported vendor specific devices are growing and?it includes cameras, MP3 players, ethernet devices, USB to serial converters, pendrives,?monitors etc.?
USB Basics
USB (Universal Serial Bus) is a high speed serial bus which supports different transfer rates from 1.5 Mbps to 80 Gbps as per its respective versions. List of USB versions released include USB 1.0, USB 1.1, USB 2.0,?USB 3.0, USB 3.1, USB 3.2, USB 4.0 and USB 4 V2.0. There are different connectors supported by these?USB standard versions such as type-A, mini-A, mini-AB, micro-A, micro-B, micro-AB and type-C.?
USB device descriptors :
USB device supports number of descriptors which defines its capabilities. These descriptors are as follows.??
Device descriptor : All the USB devices have one device descriptor.?It contains USB version, product ID, vendor ID and other configuration parameters.?
Configuration descriptor : It specifies power consumption of the device based on its interfaces?and power mode. A USB device can have more configurations but only one can be enabled at a time.?
Interface descriptor : A USB device can have one or more interfaces.?Each USB device can have multiple interface descriptors enabled at one time unlike?configuration descriptor. Let us consider a multi-functional device having printer/scanner/fax features.?In this device, interface descriptor #1 can describe endpoints of printer, interface descriptor #2 can?describe endpoints of scanner and so on.?
Endpoint descriptor : This descriptor functions as source of data or sink of data.?
It specifies data transfer type, its direction, polling interval, max. packet size etc.?
USB transfer types :?
There are four different ways USB host can communicate with USB device.?
There data flow types are as follows.?
Control Transfer : This type carries setup and configuration related transfers.?
Every USB device must have atleast one end point to configure device
when plugged in. This endpoint is called endpoint-0.?
Interrupt Transfer : This type is used to transfer interrupt driven data which
have lower frequency such as HID devices.?
Bulk Transfer : This type is meant for transferring large chunk of data such as communication devices,?printers, memory pen drives (as in our example).?
Isochronous Transfer : This type is used for periodic and continuous data transfers such as?audio, video and other streaming devices.??
USB device driver Implementation (Pen drive)
Figure-1 shows interfacing between USB device and its Linux kernel components.??As shown, USB subsystem consists of USB core, USB Host Controller and USB host controller driver.
As part of USB driver, USB device specific descriptors are configured?
for auto-detection. In addition two functions are invoked by core during?
hot plugging and hot removal of the USB device.?
Following steps are followed for writing USB specific device driver.?
1. Initialize USB registers and USB core APIs in USB.h file as per requirement?
2. Find out vendor ID and product ID of USB device (using lsusb -v command)?
??and modify structure by name "usb_device_id" in USB.C file.
领英推荐
? idVendor??????0x14cd Super Top
? idProduct?????0x1212 microSD card reader (SY-T18)
3. Build the driver (.ko file) (Using make command)
4. Load the driver (Using insmod command)
5. Check registration of USB device with USB core (Using dmesg), following kernel message appears.
[?153.009831] usbcore: registered new interface driver pen_driver
6. Hot plug the USB device with CPU of desktop computer, following kernel?
??messages appear on the screen.
[?164.151950] usb 1-5: new high-speed USB device number 5 using xhci_hcd
[?164.301045] usb 1-5: New USB device found, idVendor=14cd, idProduct=1212, bcdDevice= 1.00
[?164.301057] usb 1-5: New USB device strings: Mfr=1, Product=3, SerialNumber=2
[?164.301062] usb 1-5: Product: Mass Storage Device
[?164.301066] usb 1-5: Manufacturer: Generic
[?164.301070] usb 1-5: SerialNumber: 121220160204
[?164.302330] Minor obtained: 0
[?164.302334] bulk_in_add:81
[?164.302337] bulk_out_add:2
[?164.317146] usbcore: registered new interface driver usb-storage
[?164.318429] usbcore: registered new interface driver uas
7. After hot plugging, following commands can be given to check the USB device status.?
# ls /dev
# ls /sys/bus/usb/drivers
8. Remove the USB device from CPU and unload the driver using rmmod command
As explained in this article, it is possible to interface any USB device types with Linux kernel?once proper USB device specific guidelines and data sheets are followed.?
About Aumraj Design Systems Pvt. Ltd.?
Aumraj is specialized in Embedded hardware, Embedded firmware, Linux device drivers and?Chip design and verification services. Aumraj has strong team of firmware engineers and Linux kernel experts who can understand client requirements and provide appropriate solutions.??We provide skilled manpower to the clients across the globe and undertakes project execution as per client requirements?in VLSI and embedded Linux domain.
For more information visit https://aumraj.com/?
For specific requirements, be in touch with our business team at [email protected].