SlideShare a Scribd company logo
1 of 4
Download to read offline
Study of USB device drivers under Linux
Gamesh Kamath,
MS EWT Student
ganeshkamath89@gmail.com
MCIS
Manipal University
Manipal, India
Abstract — The Universal Serial Bus (USB) Implementation
Forum was created by joint venture of 7 companies namely Intel,
Compaq, NEC, DEC, Nortel, IBM and Microsoft in 1994. The
main idea behind the device was universality in a bus system
which also supported hot-pluggability/hot-swappability. The goal
was reached and the resulting device is continuing to breach
newer and newer applications everyday and has become a sort of
icon in the design of custom hardware peripheral .In this paper
we have attempted to summarize our findings on the chain of
events that take place between the host and the peripheral. The
study began by first learning the fundamental of device drivers,
followed by executing the USB skeleton which contained all the
essential parts of USB device drivers in it. Then the study was
extended to working USB modules which showed us how exactly
each module behaved and what the role of each module and
data-structures in the USB driver was. And the mechanism
behind how data exchange happens in a USB device was verified.
This paper presents the study, the relevance of various modules
mapped to the hardware and the function of several code
snippets available in the skeleton module to understand USB
better as a character driver.
Keywords— USB, Device Drivers, Universal Serial Bus,
Descriptors,
I. INTRODUCTION
USB was aimed at being low-power to operate at 5V power
supply and to be of low cost. Even if the system does not have
the necessary drivers to address the device requirements,
when an USB device is connected, the OS still detects the
device.
Essentially the idea behind universality was to have a single
device bus system which supported all 3 kinds of device
speeds:
• Low speed (1.5 Mbps) - necessary for user interface
devices like mouse and keyboard.
• Full speed (12 Mbps) - bulk transfer devices like
Printers and scanner.
• High speed (480 Mbps) - devices like broadband and
mass storage devices.
As soon as a device is plugged in into the USB port, the
system collects all associated descriptors and then after the
device is given an End Point ID, transaction takes place. The
USB devices are also capable of operating in 4 modes of
transfer namely:
• Control Transfer:
This is a bi-directional transfer which uses
both an IN and an OUT endpoint. Each control transfer
is made up of from 2 to several transactions.
• Bulk Transfer:
These are designed to transfer large amounts
of data with error-free delivery, but with no
guarantee of bandwidth. The host will schedule bulk
transfers after the other transfer types have been
allocated.
• Interrupt Transfer:
Interrupt transfers have nothing to do with
interrupts. The name is chosen because they are used
for the sort of purpose where an interrupt would have
been used in earlier connection types.
• Isochronous Transfer:
Isochronous transfers have a guaranteed
bandwidth, but error-free delivery is not guaranteed.
II. DATA EXCHANGE IN THE USB
USB devices operate based on descriptor exchange between
the host (computer) and the USB device. The host initially
gives a USB device an End Point number 0. And after the host
has decided on a free end point number to the device, the host
uses the end point number to select specific descriptors related
to the device. It uses the descriptors to identify the speed type
of the device. Also, it uses fields like the transfer data size and
USB version.
III. DESCRIPTORS
Descriptor – It is a defined structure and format in which the
data is transferred. Descriptors are necessary to find out the
type of device driver to be loaded. When a device is first
detected it starts with an address 0. Through a quiz it finds
out as many details as it feels necessary. Descriptors have a
hierarchical structure with four levels
Device level
Configuration level
Interface level
Endpoint level
Fig. 1 Overview of the USB implementation at the kernel
IV.STRUCTURE OF THE USB DEVICE DRIVER
Though a Host computer can be looked at as 3 level system
(figure 2) with a user-interface, kernel and a hardware, the
kernel provides several abstractions which enable good user
experience. However for a thorough study, the function of the
USB Core and USB Host Controller Interface will need to be
understood before any real attempt to study the descriptors are
made; because, it is at this level that the descriptors are
fetched using special buffers called URBs (USB receive
Buffers).
From figure 2 it can also be seen that the USB core lies
between the USB Host-Controller which interfaces the
hardware, while the device driver layer which contains the
USB device driver lies above the core. After the Host
Controller identifies the type of USB peripheral, the Core
fetches the appropriate Device driver associated with it.
Fig. 2 Overview of the USB implementation at the kernel
V. ASSOCIATED DATA STRUCTURES
While trying to study the device in Linux, the first thing
one needs to know is the structure of USB skeleton, made of
328 lines. It essentially consists of the following fields and
data structures (table, fops, class, usb_driver), which enables a
proper study of the device and categorizing will help us study
modules better.
Fig. 3 Example of an unacceptable low-resolution image
From figure 3 we can learn that:
• table consists of the VendorID and ProductID fields
which are necessary for initially registering the device.
static struct usb_device_id skel_table [] = {
{ USB_DEVICE(USB_SKEL_VENDOR_ID,
USB_SKEL_PRODUCT_ID) },{ }
};
• fops (file operations) tells the systems the different file
operation functions of the USB that the device drivers
over-rides over the existing system functions related to
the corresponding devices.
static struct file_operations skel_fops = {
.owner = THIS_MODULE,
.read = skel_read,
.write = skel_write,
.open = skel_open,
.release = skel_release,
};
• class gives the details of the data-structures and fields like
name which may be required for specific applications.
static struct usb_class_driver skel_class = {
.name = "usb/skel%d",
.fops = &skel_fops,
.mode = S_IFCHR | S_IRUSR | S_IWUSR |
S_IRGRP | S_IWGRP | S_IROTH,
.minor_base = USB_SKEL_MINOR_BASE,
};
• usb_driver also gives details of the data-structures but
additionally it tells about probe which tells the host that is
needs to use the USB modules version of host to probe
and report when and what kind of USB device activities
are going on.
static struct usb_driver skel_driver = {
.owner = THIS_MODULE,
.name = "skeleton",
.id_table = skel_table,
.probe = skel_probe,
.disconnect = skel_disconnect,
};
VI.CLASSIFICATION BASED ON RETURN TYPE
While studying the USB driver, the return type of the
functions tells a lot about the functionality of the device.
Based on this classification figure 4 can be arrived:
Fig. 4 Various functions defined in the skeleton.
Where:
• int return - functions which on failure need to notify the
system so that necessary actions can be taken or the
function call needs to be made again, depending on the
functionally. A positive value indicates partial success. A
0 indicates success and a negative value indicates a
failure in the function operation.
• void return - functions don’t return anything to the
calling function because they are either the last functions
called, or the actions to be done by such functions is done
by default at boot-up.
• ssize return - functions namely read and write, tell the
number of bytes successfully sent between the host and
the device.
VII. USB UNDER LINUX
Linux being open source OS, enables us to study the
operations of the device driver. More over, several of the
modules were readily available as system calls and the user
has the freedom of inserting and removing modules even
while the OS continues to execute other activities. Linux
devices can be studied with the help of the following
commands:
• lsusb shows a basic listing of the detected USB devices.
• Like any other Kernel module it follows the sequence
o make - to create the object and the insertable
module
o insmod - to insert the module
o lsmod - to check if the device driver has been
inserted
o rmmod - to remove the module from the kernel
process tree
VIII. SEQUENCE OF FUNCTION CALLS
Fig. 5 Life-cycle of the USB driver module
When the life-cycle (figure 5) begins, it calls the init
module that initiates all the necessary data structures fills the
table with VendorID, ProductID, identifies the various
permissions, and then probes the Host Controller for the
device actions and registers the devices connected to the
system. Calls register module after which the operating
system takes over:
static int __init pen_init(void)
{
return usb_register(&pen_driver);
}
The C code contained in the skeleton probes all end points
and calls necessary function from the skeleton whenever an
action needs to take place, such as open, close, read, write and
release. Additionally the module describes a callback function
which checks how many bytes had been successfully read
from or written to the device.
The read function also calls the copy_to_user() function,
which essentially checks if there has been any fault while
reading and returns fault if there is an error:
if(copy_to_user(buf, bulk_buf, MIN(cnt, read_cnt)))
return -EFAULT;
The write function similarly checks for error using the
function copy_from_user to check for error:
if(copy_from_user(bulk_buf, buf, MIN(cnt,
MAX_PKT_SIZE)))
return -EFAULT;
The probe function contains the following line in it:
for(i=0;i<iface_desc->desc.bNumEndpoints; i++)
endpoint = &iface_desc->endpoint[i].desc;
In the code above, the for loop is used to assign the end
point with each of the peripheral connected to the system. The
loop starts with i = 0 and goes till desc.bNumEndpoints, a
variable which holds the values about the number of endpoints
assigned to the USB devices. This field directly points to
descriptors obtained from the USB device and is accessed
using the iface_desc variable, which is initialized as
iface_desc = interface->cur_altsetting;
The variable is described as of type usb_host_interface in
the program because it is the host controller that gets the
corresponding descriptors.
struct usb_host_interface *iface_desc;
All activities like deregistering and cleanup of the variables
is done in the exit (clean-up) function. But before this is done,
an instance is destroyed using the delete function. And the
device disconnect module needs to be called so that the driver
can be successfully disconnected so disconnect is always
called before the exit function. Even if the USB device is
disconnected from the system, the driver calls disconnect and
only then calls exit.
static void __exit pen_exit(void)
{
usb_deregister(&pen_driver);
}
IX.CONCLUSIONS
The study of USB device drivers helped us to better
understand character drivers. USB has become a very popular
bus because of its simple yet elegant characters mentioned in
the introduction. Each function in the device driver was
studied and its functionality was compared with working
modules. The similarity of the skeleton with the working
module was studied and documented
REFERENCES
[1] USB Complete, Third Edition, Jan Axelson, Lakeview Research LLC
[2] USB Specification 2.0, Joh Leuker et. al. (25 authors)
[3] Jungo WinDriver User’s Manual, Version 9.2X
[4] http://usbmadesimple.co.uk/
[5] LinuxForU October, November, December 2011
[6] Linux Device Drivers, 3rd
Edition, Jonathan Corbet, Alessandro Rubini
& Greg Kroah-Hartman, O’Reilly Publishers 2011

More Related Content

What's hot

System programming note
System programming noteSystem programming note
System programming noteSANTOSH RATH
 
Operating Systems FYBSC IT UNIT I- Introduction to Operating Systems
Operating Systems FYBSC IT UNIT I- Introduction to Operating SystemsOperating Systems FYBSC IT UNIT I- Introduction to Operating Systems
Operating Systems FYBSC IT UNIT I- Introduction to Operating SystemsArti Parab Academics
 
Windows 7 Architecture
Windows 7 ArchitectureWindows 7 Architecture
Windows 7 ArchitectureDaniyal Khan
 
fundamental of computer-u-1-computer hardware system
fundamental of  computer-u-1-computer hardware systemfundamental of  computer-u-1-computer hardware system
fundamental of computer-u-1-computer hardware systemRai University
 
Operating systems11 9-07 (1)
Operating systems11 9-07 (1)Operating systems11 9-07 (1)
Operating systems11 9-07 (1)vattikuti_sarada
 
Driver development – memory management
Driver development – memory managementDriver development – memory management
Driver development – memory managementVandana Salve
 
Itk rawa t____operatingsystems2
Itk rawa t____operatingsystems2Itk rawa t____operatingsystems2
Itk rawa t____operatingsystems2KapiL RawaT
 
Operating System DOS and Windows
Operating System DOS and WindowsOperating System DOS and Windows
Operating System DOS and WindowsYasirKhan357
 
Introduction to Operating Systems
Introduction to Operating SystemsIntroduction to Operating Systems
Introduction to Operating SystemsDamian T. Gordon
 
Learn about computer hardware and software
Learn about computer hardware and softwareLearn about computer hardware and software
Learn about computer hardware and softwarefarrukh ishaq choudhary
 
Computer Organization (Unit-1)
Computer Organization (Unit-1)Computer Organization (Unit-1)
Computer Organization (Unit-1)Harsh Pandya
 
Operating systems
Operating systemsOperating systems
Operating systemsArdit Meti
 

What's hot (20)

System programming note
System programming noteSystem programming note
System programming note
 
Operating Systems FYBSC IT UNIT I- Introduction to Operating Systems
Operating Systems FYBSC IT UNIT I- Introduction to Operating SystemsOperating Systems FYBSC IT UNIT I- Introduction to Operating Systems
Operating Systems FYBSC IT UNIT I- Introduction to Operating Systems
 
Windows 7 Architecture
Windows 7 ArchitectureWindows 7 Architecture
Windows 7 Architecture
 
fundamental of computer-u-1-computer hardware system
fundamental of  computer-u-1-computer hardware systemfundamental of  computer-u-1-computer hardware system
fundamental of computer-u-1-computer hardware system
 
Operating systems11 9-07 (1)
Operating systems11 9-07 (1)Operating systems11 9-07 (1)
Operating systems11 9-07 (1)
 
Driver development – memory management
Driver development – memory managementDriver development – memory management
Driver development – memory management
 
Itk rawa t____operatingsystems2
Itk rawa t____operatingsystems2Itk rawa t____operatingsystems2
Itk rawa t____operatingsystems2
 
Linux kernel
Linux kernelLinux kernel
Linux kernel
 
Operating System DOS and Windows
Operating System DOS and WindowsOperating System DOS and Windows
Operating System DOS and Windows
 
Operating system basics
Operating system basicsOperating system basics
Operating system basics
 
Introduction to Operating Systems
Introduction to Operating SystemsIntroduction to Operating Systems
Introduction to Operating Systems
 
Case study windows
Case study windowsCase study windows
Case study windows
 
Windows xp
Windows xpWindows xp
Windows xp
 
Window architecture
Window architecture Window architecture
Window architecture
 
Learn about computer hardware and software
Learn about computer hardware and softwareLearn about computer hardware and software
Learn about computer hardware and software
 
OSCh3
OSCh3OSCh3
OSCh3
 
Computer Organization (Unit-1)
Computer Organization (Unit-1)Computer Organization (Unit-1)
Computer Organization (Unit-1)
 
Operating systems
Operating systemsOperating systems
Operating systems
 
Information sheet 1
Information sheet 1Information sheet 1
Information sheet 1
 
I/O Management
I/O ManagementI/O Management
I/O Management
 

Viewers also liked

Stepwise interpretation ECG #4
Stepwise interpretation ECG #4Stepwise interpretation ECG #4
Stepwise interpretation ECG #4Anas Nader
 
Interconstruct 09 B&V_Paper
Interconstruct 09 B&V_PaperInterconstruct 09 B&V_Paper
Interconstruct 09 B&V_PaperJohn Tucker
 
A Paratore
A ParatoreA Paratore
A Paratoremk456
 
Dp69 eyewear made in Italy
Dp69 eyewear made in Italy Dp69 eyewear made in Italy
Dp69 eyewear made in Italy Daniele Padovani
 
愉快的性格是成功的靈魂
愉快的性格是成功的靈魂愉快的性格是成功的靈魂
愉快的性格是成功的靈魂Jaing Lai
 
家是溫暖的港灣
家是溫暖的港灣家是溫暖的港灣
家是溫暖的港灣Jaing Lai
 
documentc24 - Intel Internship Certificate.PDF
documentc24 - Intel Internship Certificate.PDFdocumentc24 - Intel Internship Certificate.PDF
documentc24 - Intel Internship Certificate.PDFGanesh Kamath
 
L a p l a c 1
L a p l a c 1L a p l a c 1
L a p l a c 1su hendro
 
Presidenta de diputación de león 13.11.12
Presidenta de diputación de león 13.11.12Presidenta de diputación de león 13.11.12
Presidenta de diputación de león 13.11.12Isabel Carrasco
 
Navigate: A Fjord Incubator Project
Navigate: A Fjord Incubator Project Navigate: A Fjord Incubator Project
Navigate: A Fjord Incubator Project Fjord
 

Viewers also liked (19)

Stepwise interpretation ECG #4
Stepwise interpretation ECG #4Stepwise interpretation ECG #4
Stepwise interpretation ECG #4
 
Interconstruct 09 B&V_Paper
Interconstruct 09 B&V_PaperInterconstruct 09 B&V_Paper
Interconstruct 09 B&V_Paper
 
A Paratore
A ParatoreA Paratore
A Paratore
 
Dp69 eyewear made in Italy
Dp69 eyewear made in Italy Dp69 eyewear made in Italy
Dp69 eyewear made in Italy
 
愉快的性格是成功的靈魂
愉快的性格是成功的靈魂愉快的性格是成功的靈魂
愉快的性格是成功的靈魂
 
ep_members_uk
ep_members_ukep_members_uk
ep_members_uk
 
Planning: props
Planning: propsPlanning: props
Planning: props
 
Code of Conduct
Code of ConductCode of Conduct
Code of Conduct
 
Weekly news
Weekly newsWeekly news
Weekly news
 
Analisis usaha agroindustri keripik
Analisis usaha agroindustri keripikAnalisis usaha agroindustri keripik
Analisis usaha agroindustri keripik
 
93372801 aspek-global-kewirausahaan
93372801 aspek-global-kewirausahaan93372801 aspek-global-kewirausahaan
93372801 aspek-global-kewirausahaan
 
Internet dmlr
Internet dmlrInternet dmlr
Internet dmlr
 
家是溫暖的港灣
家是溫暖的港灣家是溫暖的港灣
家是溫暖的港灣
 
documentc24 - Intel Internship Certificate.PDF
documentc24 - Intel Internship Certificate.PDFdocumentc24 - Intel Internship Certificate.PDF
documentc24 - Intel Internship Certificate.PDF
 
L a p l a c 1
L a p l a c 1L a p l a c 1
L a p l a c 1
 
Presidenta de diputación de león 13.11.12
Presidenta de diputación de león 13.11.12Presidenta de diputación de león 13.11.12
Presidenta de diputación de león 13.11.12
 
Parkinson's Sample
Parkinson's SampleParkinson's Sample
Parkinson's Sample
 
Sonia yiscela martínez
Sonia  yiscela  martínezSonia  yiscela  martínez
Sonia yiscela martínez
 
Navigate: A Fjord Incubator Project
Navigate: A Fjord Incubator Project Navigate: A Fjord Incubator Project
Navigate: A Fjord Incubator Project
 

Similar to Study of USB Device Drivers under Linux _1_

CS403: Operating System : Unit I _merged.pdf
CS403: Operating System :  Unit I _merged.pdfCS403: Operating System :  Unit I _merged.pdf
CS403: Operating System : Unit I _merged.pdfAsst.prof M.Gokilavani
 
brief intro to Linux device drivers
brief intro to Linux device driversbrief intro to Linux device drivers
brief intro to Linux device driversAlexandre Moreno
 
NE223_chapter 1_Overview of operating systems.ppt
NE223_chapter 1_Overview of operating systems.pptNE223_chapter 1_Overview of operating systems.ppt
NE223_chapter 1_Overview of operating systems.pptMemMem25
 
Computer independent device for usb data transfe
Computer independent device for usb data transfeComputer independent device for usb data transfe
Computer independent device for usb data transfeIAEME Publication
 
Operating System-Introduction
Operating System-IntroductionOperating System-Introduction
Operating System-IntroductionShipra Swati
 
Linux Operating System Resembles Unix Operating. System
Linux Operating System Resembles Unix Operating. SystemLinux Operating System Resembles Unix Operating. System
Linux Operating System Resembles Unix Operating. SystemOlga Bautista
 
unixlinux - kernelexplain yield in user spaceexplain yield in k.pdf
unixlinux - kernelexplain yield in user spaceexplain yield in k.pdfunixlinux - kernelexplain yield in user spaceexplain yield in k.pdf
unixlinux - kernelexplain yield in user spaceexplain yield in k.pdfPRATIKSINHA7304
 
OPERATING SYSTEM - SHORT NOTES
OPERATING SYSTEM - SHORT NOTESOPERATING SYSTEM - SHORT NOTES
OPERATING SYSTEM - SHORT NOTESsuthi
 
Chapter 1- Introduction.ppt
Chapter 1- Introduction.pptChapter 1- Introduction.ppt
Chapter 1- Introduction.pptmiki304759
 
CS403: Operating System : Lec 1 Introduction.pptx
CS403: Operating System : Lec 1 Introduction.pptxCS403: Operating System : Lec 1 Introduction.pptx
CS403: Operating System : Lec 1 Introduction.pptxAsst.prof M.Gokilavani
 
COMPUTER ORGNAIZATION NOTES
COMPUTER ORGNAIZATION NOTESCOMPUTER ORGNAIZATION NOTES
COMPUTER ORGNAIZATION NOTESDr.MAYA NAYAK
 
Introduction to embedded linux device driver and firmware
Introduction to embedded linux device driver and firmwareIntroduction to embedded linux device driver and firmware
Introduction to embedded linux device driver and firmwaredefinecareer
 
Unit 6 Operating System TEIT Savitribai Phule Pune University by Tushar B Kute
Unit 6 Operating System TEIT Savitribai Phule Pune University by Tushar B KuteUnit 6 Operating System TEIT Savitribai Phule Pune University by Tushar B Kute
Unit 6 Operating System TEIT Savitribai Phule Pune University by Tushar B KuteTushar B Kute
 
please answer these questions number by numberSolution1) An.pdf
please answer these questions number by numberSolution1) An.pdfplease answer these questions number by numberSolution1) An.pdf
please answer these questions number by numberSolution1) An.pdfarishaenterprises12
 

Similar to Study of USB Device Drivers under Linux _1_ (20)

CS403: Operating System : Unit I _merged.pdf
CS403: Operating System :  Unit I _merged.pdfCS403: Operating System :  Unit I _merged.pdf
CS403: Operating System : Unit I _merged.pdf
 
OS_Intro_Chap_1.ppt
OS_Intro_Chap_1.pptOS_Intro_Chap_1.ppt
OS_Intro_Chap_1.ppt
 
brief intro to Linux device drivers
brief intro to Linux device driversbrief intro to Linux device drivers
brief intro to Linux device drivers
 
Device Drivers
Device DriversDevice Drivers
Device Drivers
 
NE223_chapter 1_Overview of operating systems.ppt
NE223_chapter 1_Overview of operating systems.pptNE223_chapter 1_Overview of operating systems.ppt
NE223_chapter 1_Overview of operating systems.ppt
 
Computer independent device for usb data transfe
Computer independent device for usb data transfeComputer independent device for usb data transfe
Computer independent device for usb data transfe
 
operating system lecture notes
operating system lecture notesoperating system lecture notes
operating system lecture notes
 
Operating System-Introduction
Operating System-IntroductionOperating System-Introduction
Operating System-Introduction
 
Linux Operating System Resembles Unix Operating. System
Linux Operating System Resembles Unix Operating. SystemLinux Operating System Resembles Unix Operating. System
Linux Operating System Resembles Unix Operating. System
 
unixlinux - kernelexplain yield in user spaceexplain yield in k.pdf
unixlinux - kernelexplain yield in user spaceexplain yield in k.pdfunixlinux - kernelexplain yield in user spaceexplain yield in k.pdf
unixlinux - kernelexplain yield in user spaceexplain yield in k.pdf
 
OPERATING SYSTEM - SHORT NOTES
OPERATING SYSTEM - SHORT NOTESOPERATING SYSTEM - SHORT NOTES
OPERATING SYSTEM - SHORT NOTES
 
System structure
System structureSystem structure
System structure
 
Operating system
Operating systemOperating system
Operating system
 
Chapter 1- Introduction.ppt
Chapter 1- Introduction.pptChapter 1- Introduction.ppt
Chapter 1- Introduction.ppt
 
CS403: Operating System : Lec 1 Introduction.pptx
CS403: Operating System : Lec 1 Introduction.pptxCS403: Operating System : Lec 1 Introduction.pptx
CS403: Operating System : Lec 1 Introduction.pptx
 
COMPUTER ORGNAIZATION NOTES
COMPUTER ORGNAIZATION NOTESCOMPUTER ORGNAIZATION NOTES
COMPUTER ORGNAIZATION NOTES
 
Introduction to embedded linux device driver and firmware
Introduction to embedded linux device driver and firmwareIntroduction to embedded linux device driver and firmware
Introduction to embedded linux device driver and firmware
 
Studienarb linux kernel-dev
Studienarb linux kernel-devStudienarb linux kernel-dev
Studienarb linux kernel-dev
 
Unit 6 Operating System TEIT Savitribai Phule Pune University by Tushar B Kute
Unit 6 Operating System TEIT Savitribai Phule Pune University by Tushar B KuteUnit 6 Operating System TEIT Savitribai Phule Pune University by Tushar B Kute
Unit 6 Operating System TEIT Savitribai Phule Pune University by Tushar B Kute
 
please answer these questions number by numberSolution1) An.pdf
please answer these questions number by numberSolution1) An.pdfplease answer these questions number by numberSolution1) An.pdf
please answer these questions number by numberSolution1) An.pdf
 

More from Ganesh Kamath

Web Application Development using PHP and MySQL
Web Application Development using PHP and MySQLWeb Application Development using PHP and MySQL
Web Application Development using PHP and MySQLGanesh Kamath
 
documentc43 - AutoDesk inventor.PDF
documentc43 - AutoDesk inventor.PDFdocumentc43 - AutoDesk inventor.PDF
documentc43 - AutoDesk inventor.PDFGanesh Kamath
 
documentc19 - AutoCad Training Certificate.PDF
documentc19 - AutoCad Training Certificate.PDFdocumentc19 - AutoCad Training Certificate.PDF
documentc19 - AutoCad Training Certificate.PDFGanesh Kamath
 
documentc23 E Surveying Solutions Student Trainee Certifiate.PDF
documentc23 E Surveying Solutions Student Trainee Certifiate.PDFdocumentc23 E Surveying Solutions Student Trainee Certifiate.PDF
documentc23 E Surveying Solutions Student Trainee Certifiate.PDFGanesh Kamath
 
documentc21 - BSNL Summer Training Course.PDF
documentc21 - BSNL Summer Training Course.PDFdocumentc21 - BSNL Summer Training Course.PDF
documentc21 - BSNL Summer Training Course.PDFGanesh Kamath
 
document11 - 10th Standard Marks Card.PDF
document11 - 10th Standard Marks Card.PDFdocument11 - 10th Standard Marks Card.PDF
document11 - 10th Standard Marks Card.PDFGanesh Kamath
 
document12 - 12th Standard Marks Card.PDF
document12 - 12th Standard Marks Card.PDFdocument12 - 12th Standard Marks Card.PDF
document12 - 12th Standard Marks Card.PDFGanesh Kamath
 
BTech University Certificate
BTech University CertificateBTech University Certificate
BTech University CertificateGanesh Kamath
 
BTech Transcript - Electronics and Communication
BTech Transcript - Electronics and CommunicationBTech Transcript - Electronics and Communication
BTech Transcript - Electronics and CommunicationGanesh Kamath
 
MS Degree Certificate
MS Degree CertificateMS Degree Certificate
MS Degree CertificateGanesh Kamath
 
MS Gold Medal Certificate from Manipal University
MS Gold Medal Certificate from Manipal UniversityMS Gold Medal Certificate from Manipal University
MS Gold Medal Certificate from Manipal UniversityGanesh Kamath
 
MS Transcript - Embedded and Wireless Technology
MS Transcript - Embedded and Wireless TechnologyMS Transcript - Embedded and Wireless Technology
MS Transcript - Embedded and Wireless TechnologyGanesh Kamath
 
Ganesh Kamath's Résumé (3)
Ganesh Kamath's Résumé (3)Ganesh Kamath's Résumé (3)
Ganesh Kamath's Résumé (3)Ganesh Kamath
 

More from Ganesh Kamath (14)

Web Application Development using PHP and MySQL
Web Application Development using PHP and MySQLWeb Application Development using PHP and MySQL
Web Application Development using PHP and MySQL
 
Ganesh
GaneshGanesh
Ganesh
 
documentc43 - AutoDesk inventor.PDF
documentc43 - AutoDesk inventor.PDFdocumentc43 - AutoDesk inventor.PDF
documentc43 - AutoDesk inventor.PDF
 
documentc19 - AutoCad Training Certificate.PDF
documentc19 - AutoCad Training Certificate.PDFdocumentc19 - AutoCad Training Certificate.PDF
documentc19 - AutoCad Training Certificate.PDF
 
documentc23 E Surveying Solutions Student Trainee Certifiate.PDF
documentc23 E Surveying Solutions Student Trainee Certifiate.PDFdocumentc23 E Surveying Solutions Student Trainee Certifiate.PDF
documentc23 E Surveying Solutions Student Trainee Certifiate.PDF
 
documentc21 - BSNL Summer Training Course.PDF
documentc21 - BSNL Summer Training Course.PDFdocumentc21 - BSNL Summer Training Course.PDF
documentc21 - BSNL Summer Training Course.PDF
 
document11 - 10th Standard Marks Card.PDF
document11 - 10th Standard Marks Card.PDFdocument11 - 10th Standard Marks Card.PDF
document11 - 10th Standard Marks Card.PDF
 
document12 - 12th Standard Marks Card.PDF
document12 - 12th Standard Marks Card.PDFdocument12 - 12th Standard Marks Card.PDF
document12 - 12th Standard Marks Card.PDF
 
BTech University Certificate
BTech University CertificateBTech University Certificate
BTech University Certificate
 
BTech Transcript - Electronics and Communication
BTech Transcript - Electronics and CommunicationBTech Transcript - Electronics and Communication
BTech Transcript - Electronics and Communication
 
MS Degree Certificate
MS Degree CertificateMS Degree Certificate
MS Degree Certificate
 
MS Gold Medal Certificate from Manipal University
MS Gold Medal Certificate from Manipal UniversityMS Gold Medal Certificate from Manipal University
MS Gold Medal Certificate from Manipal University
 
MS Transcript - Embedded and Wireless Technology
MS Transcript - Embedded and Wireless TechnologyMS Transcript - Embedded and Wireless Technology
MS Transcript - Embedded and Wireless Technology
 
Ganesh Kamath's Résumé (3)
Ganesh Kamath's Résumé (3)Ganesh Kamath's Résumé (3)
Ganesh Kamath's Résumé (3)
 

Study of USB Device Drivers under Linux _1_

  • 1. Study of USB device drivers under Linux Gamesh Kamath, MS EWT Student ganeshkamath89@gmail.com MCIS Manipal University Manipal, India Abstract — The Universal Serial Bus (USB) Implementation Forum was created by joint venture of 7 companies namely Intel, Compaq, NEC, DEC, Nortel, IBM and Microsoft in 1994. The main idea behind the device was universality in a bus system which also supported hot-pluggability/hot-swappability. The goal was reached and the resulting device is continuing to breach newer and newer applications everyday and has become a sort of icon in the design of custom hardware peripheral .In this paper we have attempted to summarize our findings on the chain of events that take place between the host and the peripheral. The study began by first learning the fundamental of device drivers, followed by executing the USB skeleton which contained all the essential parts of USB device drivers in it. Then the study was extended to working USB modules which showed us how exactly each module behaved and what the role of each module and data-structures in the USB driver was. And the mechanism behind how data exchange happens in a USB device was verified. This paper presents the study, the relevance of various modules mapped to the hardware and the function of several code snippets available in the skeleton module to understand USB better as a character driver. Keywords— USB, Device Drivers, Universal Serial Bus, Descriptors, I. INTRODUCTION USB was aimed at being low-power to operate at 5V power supply and to be of low cost. Even if the system does not have the necessary drivers to address the device requirements, when an USB device is connected, the OS still detects the device. Essentially the idea behind universality was to have a single device bus system which supported all 3 kinds of device speeds: • Low speed (1.5 Mbps) - necessary for user interface devices like mouse and keyboard. • Full speed (12 Mbps) - bulk transfer devices like Printers and scanner. • High speed (480 Mbps) - devices like broadband and mass storage devices. As soon as a device is plugged in into the USB port, the system collects all associated descriptors and then after the device is given an End Point ID, transaction takes place. The USB devices are also capable of operating in 4 modes of transfer namely: • Control Transfer: This is a bi-directional transfer which uses both an IN and an OUT endpoint. Each control transfer is made up of from 2 to several transactions. • Bulk Transfer: These are designed to transfer large amounts of data with error-free delivery, but with no guarantee of bandwidth. The host will schedule bulk transfers after the other transfer types have been allocated. • Interrupt Transfer: Interrupt transfers have nothing to do with interrupts. The name is chosen because they are used for the sort of purpose where an interrupt would have been used in earlier connection types. • Isochronous Transfer: Isochronous transfers have a guaranteed bandwidth, but error-free delivery is not guaranteed. II. DATA EXCHANGE IN THE USB USB devices operate based on descriptor exchange between the host (computer) and the USB device. The host initially gives a USB device an End Point number 0. And after the host has decided on a free end point number to the device, the host uses the end point number to select specific descriptors related to the device. It uses the descriptors to identify the speed type of the device. Also, it uses fields like the transfer data size and USB version. III. DESCRIPTORS Descriptor – It is a defined structure and format in which the data is transferred. Descriptors are necessary to find out the type of device driver to be loaded. When a device is first detected it starts with an address 0. Through a quiz it finds out as many details as it feels necessary. Descriptors have a hierarchical structure with four levels Device level Configuration level Interface level Endpoint level
  • 2. Fig. 1 Overview of the USB implementation at the kernel IV.STRUCTURE OF THE USB DEVICE DRIVER Though a Host computer can be looked at as 3 level system (figure 2) with a user-interface, kernel and a hardware, the kernel provides several abstractions which enable good user experience. However for a thorough study, the function of the USB Core and USB Host Controller Interface will need to be understood before any real attempt to study the descriptors are made; because, it is at this level that the descriptors are fetched using special buffers called URBs (USB receive Buffers). From figure 2 it can also be seen that the USB core lies between the USB Host-Controller which interfaces the hardware, while the device driver layer which contains the USB device driver lies above the core. After the Host Controller identifies the type of USB peripheral, the Core fetches the appropriate Device driver associated with it. Fig. 2 Overview of the USB implementation at the kernel V. ASSOCIATED DATA STRUCTURES While trying to study the device in Linux, the first thing one needs to know is the structure of USB skeleton, made of 328 lines. It essentially consists of the following fields and data structures (table, fops, class, usb_driver), which enables a proper study of the device and categorizing will help us study modules better. Fig. 3 Example of an unacceptable low-resolution image From figure 3 we can learn that: • table consists of the VendorID and ProductID fields which are necessary for initially registering the device. static struct usb_device_id skel_table [] = { { USB_DEVICE(USB_SKEL_VENDOR_ID, USB_SKEL_PRODUCT_ID) },{ } }; • fops (file operations) tells the systems the different file operation functions of the USB that the device drivers over-rides over the existing system functions related to the corresponding devices. static struct file_operations skel_fops = { .owner = THIS_MODULE, .read = skel_read, .write = skel_write, .open = skel_open, .release = skel_release, }; • class gives the details of the data-structures and fields like name which may be required for specific applications. static struct usb_class_driver skel_class = { .name = "usb/skel%d", .fops = &skel_fops, .mode = S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH, .minor_base = USB_SKEL_MINOR_BASE, }; • usb_driver also gives details of the data-structures but additionally it tells about probe which tells the host that is needs to use the USB modules version of host to probe and report when and what kind of USB device activities are going on. static struct usb_driver skel_driver = { .owner = THIS_MODULE, .name = "skeleton", .id_table = skel_table, .probe = skel_probe, .disconnect = skel_disconnect, };
  • 3. VI.CLASSIFICATION BASED ON RETURN TYPE While studying the USB driver, the return type of the functions tells a lot about the functionality of the device. Based on this classification figure 4 can be arrived: Fig. 4 Various functions defined in the skeleton. Where: • int return - functions which on failure need to notify the system so that necessary actions can be taken or the function call needs to be made again, depending on the functionally. A positive value indicates partial success. A 0 indicates success and a negative value indicates a failure in the function operation. • void return - functions don’t return anything to the calling function because they are either the last functions called, or the actions to be done by such functions is done by default at boot-up. • ssize return - functions namely read and write, tell the number of bytes successfully sent between the host and the device. VII. USB UNDER LINUX Linux being open source OS, enables us to study the operations of the device driver. More over, several of the modules were readily available as system calls and the user has the freedom of inserting and removing modules even while the OS continues to execute other activities. Linux devices can be studied with the help of the following commands: • lsusb shows a basic listing of the detected USB devices. • Like any other Kernel module it follows the sequence o make - to create the object and the insertable module o insmod - to insert the module o lsmod - to check if the device driver has been inserted o rmmod - to remove the module from the kernel process tree VIII. SEQUENCE OF FUNCTION CALLS Fig. 5 Life-cycle of the USB driver module When the life-cycle (figure 5) begins, it calls the init module that initiates all the necessary data structures fills the table with VendorID, ProductID, identifies the various permissions, and then probes the Host Controller for the device actions and registers the devices connected to the system. Calls register module after which the operating system takes over: static int __init pen_init(void) { return usb_register(&pen_driver); } The C code contained in the skeleton probes all end points and calls necessary function from the skeleton whenever an action needs to take place, such as open, close, read, write and release. Additionally the module describes a callback function which checks how many bytes had been successfully read from or written to the device. The read function also calls the copy_to_user() function, which essentially checks if there has been any fault while reading and returns fault if there is an error: if(copy_to_user(buf, bulk_buf, MIN(cnt, read_cnt))) return -EFAULT; The write function similarly checks for error using the function copy_from_user to check for error: if(copy_from_user(bulk_buf, buf, MIN(cnt, MAX_PKT_SIZE))) return -EFAULT; The probe function contains the following line in it: for(i=0;i<iface_desc->desc.bNumEndpoints; i++) endpoint = &iface_desc->endpoint[i].desc; In the code above, the for loop is used to assign the end point with each of the peripheral connected to the system. The loop starts with i = 0 and goes till desc.bNumEndpoints, a variable which holds the values about the number of endpoints
  • 4. assigned to the USB devices. This field directly points to descriptors obtained from the USB device and is accessed using the iface_desc variable, which is initialized as iface_desc = interface->cur_altsetting; The variable is described as of type usb_host_interface in the program because it is the host controller that gets the corresponding descriptors. struct usb_host_interface *iface_desc; All activities like deregistering and cleanup of the variables is done in the exit (clean-up) function. But before this is done, an instance is destroyed using the delete function. And the device disconnect module needs to be called so that the driver can be successfully disconnected so disconnect is always called before the exit function. Even if the USB device is disconnected from the system, the driver calls disconnect and only then calls exit. static void __exit pen_exit(void) { usb_deregister(&pen_driver); } IX.CONCLUSIONS The study of USB device drivers helped us to better understand character drivers. USB has become a very popular bus because of its simple yet elegant characters mentioned in the introduction. Each function in the device driver was studied and its functionality was compared with working modules. The similarity of the skeleton with the working module was studied and documented REFERENCES [1] USB Complete, Third Edition, Jan Axelson, Lakeview Research LLC [2] USB Specification 2.0, Joh Leuker et. al. (25 authors) [3] Jungo WinDriver User’s Manual, Version 9.2X [4] http://usbmadesimple.co.uk/ [5] LinuxForU October, November, December 2011 [6] Linux Device Drivers, 3rd Edition, Jonathan Corbet, Alessandro Rubini & Greg Kroah-Hartman, O’Reilly Publishers 2011