Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Device Drivers, Part 11: USB Drivers in Linux - LINUX For You
Subscribe to Print Edition Search
HOME
REVIEWS
HOW-TOS
CODING
INTERVIEWS
FEATURES
OVERVIEW
BLOGS
SERIES
IT ADMIN
Search for:
Search
This article, which is part of the series on Linux device drivers, gets you started with writing your first USB driver in Linux.
Pugs pen drive was the device Shweta was playing with, when both of them sat down to explore the world of USB drivers in Linux. The fastest way to get the hang of it, and Pugs usual way, was to pick up a USB device, and write a driver for it, to experiment with. So they chose a pen drive (a.k.a. USB stick) that was at hand a JetFlash from Transcend, with vendor ID 0 x 0 5 8 fand product ID 0 x 6 3 8 7 .
Follow
+2,490
www.linuxforu.com/2011/10/usb-drivers-in-linux-1/
1/6
8/13/13
Device Drivers, Part 11: USB Drivers in Linux - LINUX For You
Find us on Facebook
Popular
Comments
Tag cloud
A basic listing of all detected USB devices can be obtained using the l s u s bcommand, as root. Figure 2 shows this, with and without the pen drive plugged in. A voption to l s u s b provides detailed information.
In many Linux distributions like Mandriva, Fedora, the u s b f sdriver is loaded as part of the default configuration. This enables the detected USB device details to be viewed in a more techno-friendly way through the / p r o cwindow, using c a t/ p r o c / b u s / u s b / d e v i c e s . Figure 3 shows a typical snippet of the same, clipped around the pen drive-specific section. The listing basically contains one such section for each valid USB device detected on the system.
www.linuxforu.com/2011/10/usb-drivers-in-linux-1/
2/6
8/13/13
Device Drivers, Part 11: USB Drivers in Linux - LINUX For You
www.linuxforu.com/2011/10/usb-drivers-in-linux-1/
3/6
8/13/13
Device Drivers, Part 11: USB Drivers in Linux - LINUX For You
Coming back to the USB device sections (Figure 3), the first letter on each line represents the various parts of the USB device specification just explained. For example, D for device, C for configuration, I for interface, E for endpoint, etc. Details about these and various others are available in the kernel source, in D o c u m e n t a t i o n / u s b / p r o c _ u s b _ i n f o . t x t .
As part of the u s b _ d r i v e rstructure, the fields to be provided are the drivers name, ID table for auto-detecting the particular device, and the two callback functions to be invoked by the USB core during a hot plugging and a hot removal of the device, respectively. Putting it all together, p e n _ r e g i s t e r . cwould look like what follows:
1 2 3 4 5 6 7 8 9 1 0 1 1 1 2 1 3 1 4 1 5 1 6 1 7 1 8 1 9 2 0 2 1 2 2 2 3 2 4 2 5 2 6 2 7 2 8 # i n c l u d e< l i n u x / m o d u l e . h > # i n c l u d e< l i n u x / k e r n e l . h > # i n c l u d e< l i n u x / u s b . h > s t a t i ci n tp e n _ p r o b e ( s t r u c tu s b _ i n t e r f a c e* i n t e r f a c e ,c o n s ts t r u c tu s b _ d e v i c e _ i d* i d ) { p r i n t k ( K E R N _ I N F O" P e nd r i v e( % 0 4 X : % 0 4 X )p l u g g e d \ n " ,i d > i d V e n d o r ,i d > i d P r o d u c t ) ; r e t u r n0 ; } s t a t i cv o i dp e n _ d i s c o n n e c t ( s t r u c tu s b _ i n t e r f a c e* i n t e r f a c e ) { p r i n t k ( K E R N _ I N F O" P e nd r i v er e m o v e d \ n " ) ; } s t a t i cs t r u c tu s b _ d e v i c e _ i dp e n _ t a b l e [ ]= { {U S B _ D E V I C E ( 0 x 0 5 8 F ,0 x 6 3 8 7 )} , { }/ *T e r m i n a t i n ge n t r y* / } ; M O D U L E _ D E V I C E _ T A B L E( u s b ,p e n _ t a b l e ) ; s t a t i cs t r u c tu s b _ d r i v e rp e n _ d r i v e r= { . n a m e=" p e n _ d r i v e r " , . i d _ t a b l e=p e n _ t a b l e , . p r o b e=p e n _ p r o b e , . d i s c o n n e c t=p e n _ d i s c o n n e c t ,
www.linuxforu.com/2011/10/usb-drivers-in-linux-1/
4/6
8/13/13
2 9 3 0 3 1 3 2 3 3 3 4 3 5 3 6 3 7 3 8 3 9 4 0 4 1 4 2 4 3 4 4 4 5 4 6 } ;
Device Drivers, Part 11: USB Drivers in Linux - LINUX For You
s t a t i ci n t_ _ i n i tp e n _ i n i t ( v o i d ) { r e t u r nu s b _ r e g i s t e r ( & p e n _ d r i v e r ) ; } s t a t i cv o i d_ _ e x i tp e n _ e x i t ( v o i d ) { u s b _ d e r e g i s t e r ( & p e n _ d r i v e r ) ; } m o d u l e _ i n i t ( p e n _ i n i t ) ; m o d u l e _ e x i t ( p e n _ e x i t ) ; M O D U L E _ L I C E N S E ( " G P L " ) ; M O D U L E _ A U T H O R ( " A n i lK u m a rP u g a l i a< e m a i l _ a t _ s a r i k a p u g s _ d o t _ c o m > " ) ; M O D U L E _ D E S C R I P T I O N ( " U S BP e nR e g i s t r a t i o nD r i v e r " ) ;
Then, the usual steps for any Linux device driver may be repeated: Build the driver (. k ofile) by running m a k e . Load the driver using i n s m o d . List the loaded modules using l s m o d . Unload the driver using r m m o d . But surprisingly, the results wouldnt be as expected. Check d m e s gand the p r o cwindow to see the various logs and details. This is not because a USB driver is different from a character driver but theres a catch. Figure 3 shows that the pen drive has one interface (numbered 0), which is already associated with the usual usb-storage driver. Now, in order to get our driver associated with that interface, we need to unload the usb-storage driver (i.e., r m m o du s b s t o r a g e ) and replug the pen drive. Once thats done, the results would be as expected. Figure 5 shows a glimpse of the possible logs and a p r o c window snippet. Repeat hot-plugging in and hot-plugging out the pen drive to observe the probe and disconnect calls in action.
Summing up
Finally! Something in action! a relieved Shweta said. But it seems like there are so many things (like the device ID table, probe, disconnect, etc.), yet to be understood to get a complete USB device driver in place. Yes, you are right. Lets take them up, one by one, with breaks, replied Pugs, taking a break himself.
Related Posts:
Device Drivers, Part 12: USB Drivers in Linux Continued Device Drivers, Part 13: Data Transfer to and from USB Devices Device Drivers, Part 4: Linux Character Drivers
www.linuxforu.com/2011/10/usb-drivers-in-linux-1/
5/6
8/13/13
Device Drivers, Part 11: USB Drivers in Linux - LINUX For You
Device Drivers, Part 5: Character Device Files Creation & Operations Device Drivers, Part 1: Linux Device Drivers for Your Girl Friend
Tags: API, generic usb, Hardware Support, jetflash, kernel space, LFY October 2011, linux device drivers, Linux Device Drivers Series, multi-function device, pci device, pen drive, protocol specifications, Universal Serial Bus, USB, USB device, usb devices, usb drivers, USB flash drive, usb host controller, usb protocol, usb stick, vendor id
Previous Post
Next Post
Reviews
How-Tos
Coding
Interviews
Features
Overview
Blogs
Search
Popular tags
Linux , ubuntu, Java, MySQL, Google, python, Fedora, Android, PHP, C, html, w eb applications , India, Microsoft, unix , Window s , Red Hat, Oracle, Security , Apache, xml, LFY April 2012, FOSS, GNOME, http, JavaScript, LFY June 2011, open source, RAM, operating systems
All published articles are released under Creative Commons Attribution-NonCommercial 3.0 Unported License, unless otherw ise noted. LINUX For You is pow ered by WordPress, w hich gladly sits on top of a CentOS-based LEMP stack.
www.linuxforu.com/2011/10/usb-drivers-in-linux-1/
6/6