Introduction to Linux Drivers     © 2010 Anil Kumar Pugalia <email@sarika-pugs.com>                    All Rights Reserved.
What to Expect?After this session, you would know  Ws of Linux Drivers  Ecosystem of Linux Drivers  Types of Linux Drivers...
Ws of Linux DriversWhat is a Driver?What is a Linux Driver?Is Linux Device Driver = Linux Driver?Why we need a Driver?What...
Functions of an OSProcess / Time / Processor ManagementMemory ManagementDevice I/O ManagementStorage ManagementNetwork Man...
Linux as an OSSo, Linux also has the same structureVisually, can be shown as        © 2010 Anil Kumar Pugalia <email@sarik...
Linux Driver Ecosystembash           gvim        X Server           ssh           gcc          firefox  Process          M...
Kernel Source Organization/usr/src/linux/             arch/<arch>                    mm                  drivers          ...
Ws of a Module?Hot plug-n-play DriverDynamically Loadable & UnloadableLinux – the first OS to have such a featureLater man...
Module CommandsTypically needs root permissionResides in /sbinOperates over the kernel-module i/fFoundation of Driver Deve...
Listing ModulesCommand: lsmodFields: Module, Size, Used ByKernel Window: /proc/modulesAre these listed modules static ordy...
Loading ModulesCommand: insmod <module_file>Go to modules directory and into fs/vfatTry: insmod vfat.ko        © 2010 Anil...
Unloading ModulesCommand: rmmod <module_name>Try: rmmod fat        © 2010 Anil Kumar Pugalia <email@sarika-pugs.com>   12 ...
Auto Loading ModulesCommand: modprobe <module_name>Try: modprobe vfat        © 2010 Anil Kumar Pugalia <email@sarika-pugs....
Kernel WindowsThrough virtual filesystems  /proc  /sysCommand: cat <window_file>System Logs: /var/log/messagesCommand:  ta...
Other Useful CommandsDisassemble: objdump -d <object_file>List symbols: nm <object_file>        © 2010 Anil Kumar Pugalia ...
Command Summarylsmodinsmodmodprobermmoddmesgobjdumpnm         © 2010 Anil Kumar Pugalia <email@sarika-pugs.com>   16      ...
The First Linux Driver© 2010 Anil Kumar Pugalia <email@sarika-pugs.com>   17               All Rights Reserved.
The Kernels CNormal C but without access to  Standard Headers (/usr/include)  Standard Libraries (/usr/lib)Then, what?  Ke...
The Module Constructorstatic int __init mfd_init(void){    ...    return 0;}module_init(mfd_init);                © 2010 A...
The Module Destructorstatic void __exit mfd_exit(void){    ...}module_exit(mfd_exit);            © 2010 Anil Kumar Pugalia...
printk – Kernels printfHeader: <linux/kernel.h>Arguments: Same as printfFormat Specifiers: All as in printf, except float ...
The Module Constructor                (revisited)static int __init mfd_init(void){    printk(KERN_INFO "mfd registered"); ...
The Module Destructor               (revisited)static void __exit mfd_exit(void){    printk(KERN_INFO "mfd deregistered");...
The Other Basics & OrnamentsBasic Headers  #include <linux/module.h>  #include <linux/version.h>  #include <linux/kernel.h...
Building the ModuleFor building our driver, it needs  The Kernel Headers for Prototypes  The Kernel Functions for Function...
Our Makefileifeq (${KERNELRELEASE},)       KERNEL_SOURCE := <kernel source directory path>       PWD := $(shell pwd)defaul...
Try out your First Linux Driver   © 2010 Anil Kumar Pugalia <email@sarika-pugs.com>   27                  All Rights Reser...
What all have we learnt?Ws of Linux DriversEcosystem of Linux DriversTypes of Linux DriversVertical & Horizontal Driver La...
Any Queries?© 2010 Anil Kumar Pugalia <email@sarika-pugs.com>   29               All Rights Reserved.
Upcoming SlideShare
Loading in...5
×

Introduction to Linux Drivers

22,260

Published on

Published in: Technology
4 Comments
15 Likes
Statistics
Notes
No Downloads
Views
Total Views
22,260
On Slideshare
0
From Embeds
0
Number of Embeds
20
Actions
Shares
0
Downloads
0
Comments
4
Likes
15
Embeds 0
No embeds

No notes for slide

Transcript of "Introduction to Linux Drivers"

  1. 1. Introduction to Linux Drivers © 2010 Anil Kumar Pugalia <email@sarika-pugs.com> All Rights Reserved.
  2. 2. What to Expect?After this session, you would know Ws of Linux Drivers Ecosystem of Linux Drivers Types of Linux Drivers Vertical & Horizontal Driver Layering Various Terminologies in vogue Linux Driver related Commands & Configs Using a Linux Driver Our First Linux Driver © 2010 Anil Kumar Pugalia <email@sarika-pugs.com> 2 All Rights Reserved.
  3. 3. Ws of Linux DriversWhat is a Driver?What is a Linux Driver?Is Linux Device Driver = Linux Driver?Why we need a Driver?What are the roles of Linux Driver? © 2010 Anil Kumar Pugalia <email@sarika-pugs.com> 3 All Rights Reserved.
  4. 4. Functions of an OSProcess / Time / Processor ManagementMemory ManagementDevice I/O ManagementStorage ManagementNetwork Management © 2010 Anil Kumar Pugalia <email@sarika-pugs.com> 4 All Rights Reserved.
  5. 5. Linux as an OSSo, Linux also has the same structureVisually, can be shown as © 2010 Anil Kumar Pugalia <email@sarika-pugs.com> 5 All Rights Reserved.
  6. 6. Linux Driver Ecosystembash gvim X Server ssh gcc firefox Process Memory Device File Systems NetworkingManagement Management ControlConcurrency Virtual Ttys & Files & Dirs: ConnectivityMultiTasking Memory Device Access The VFSArchitecture Character Filesystem Network MemoryDependent Drivers Layer Subsystem Manager Code & Block Layer Interface Friends & Drivers Drivers Hardware Protocol Layers like PCI, USB, I2C, RS232, ... Consoles, Disks & Network CPU Memory ` etc CDs Interfaces © 2010 Anil Kumar Pugalia <email@sarika-pugs.com> 6 All Rights Reserved.
  7. 7. Kernel Source Organization/usr/src/linux/ arch/<arch> mm drivers fs char mtd/ide net pci serial usb ... block net include linux asm-<arch> init kernel ipc lib scripts tools crypto firmware security sound ... © 2010 Anil Kumar Pugalia <email@sarika-pugs.com> 7 All Rights Reserved.
  8. 8. Ws of a Module?Hot plug-n-play DriverDynamically Loadable & UnloadableLinux – the first OS to have such a featureLater many followed suitEnables fast development cycleFile: <module>.ko (Kernel Object) <module>.o wrapped with kernel signatureStd Modules Path /lib/modules/<kernel version>/kernel/...Module Configuration: /etc/modprobe.conf © 2010 Anil Kumar Pugalia <email@sarika-pugs.com> 8 All Rights Reserved.
  9. 9. Module CommandsTypically needs root permissionResides in /sbinOperates over the kernel-module i/fFoundation of Driver DevelopmentNeed to understand thoroughly © 2010 Anil Kumar Pugalia <email@sarika-pugs.com> 9 All Rights Reserved.
  10. 10. Listing ModulesCommand: lsmodFields: Module, Size, Used ByKernel Window: /proc/modulesAre these listed modules static ordynamic? © 2010 Anil Kumar Pugalia <email@sarika-pugs.com> 10 All Rights Reserved.
  11. 11. Loading ModulesCommand: insmod <module_file>Go to modules directory and into fs/vfatTry: insmod vfat.ko © 2010 Anil Kumar Pugalia <email@sarika-pugs.com> 11 All Rights Reserved.
  12. 12. Unloading ModulesCommand: rmmod <module_name>Try: rmmod fat © 2010 Anil Kumar Pugalia <email@sarika-pugs.com> 12 All Rights Reserved.
  13. 13. Auto Loading ModulesCommand: modprobe <module_name>Try: modprobe vfat © 2010 Anil Kumar Pugalia <email@sarika-pugs.com> 13 All Rights Reserved.
  14. 14. Kernel WindowsThrough virtual filesystems /proc /sysCommand: cat <window_file>System Logs: /var/log/messagesCommand: tail /var/log/messages dmesg | tail © 2010 Anil Kumar Pugalia <email@sarika-pugs.com> 14 All Rights Reserved.
  15. 15. Other Useful CommandsDisassemble: objdump -d <object_file>List symbols: nm <object_file> © 2010 Anil Kumar Pugalia <email@sarika-pugs.com> 15 All Rights Reserved.
  16. 16. Command Summarylsmodinsmodmodprobermmoddmesgobjdumpnm © 2010 Anil Kumar Pugalia <email@sarika-pugs.com> 16 All Rights Reserved.
  17. 17. The First Linux Driver© 2010 Anil Kumar Pugalia <email@sarika-pugs.com> 17 All Rights Reserved.
  18. 18. The Kernels CNormal C but without access to Standard Headers (/usr/include) Standard Libraries (/usr/lib)Then, what? Kernel Headers @ <kernel src>/include Kernel Function Collection @ <kernel src>/kernel <kernel src>/ipc <kernel src>/libgcc need to be tuned to compile “Kernel C”Kernel C is a beautiful example of implementing objectoriented code using pure C © 2010 Anil Kumar Pugalia <email@sarika-pugs.com> 18 All Rights Reserved.
  19. 19. The Module Constructorstatic int __init mfd_init(void){ ... return 0;}module_init(mfd_init); © 2010 Anil Kumar Pugalia <email@sarika-pugs.com> 19 All Rights Reserved.
  20. 20. The Module Destructorstatic void __exit mfd_exit(void){ ...}module_exit(mfd_exit); © 2010 Anil Kumar Pugalia <email@sarika-pugs.com> 20 All Rights Reserved.
  21. 21. printk – Kernels printfHeader: <linux/kernel.h>Arguments: Same as printfFormat Specifiers: All as in printf, except float & double relatedAdditionally, a initial 3 character sequence for Log Level KERN_EMERG "<0>" /* system is unusable */ KERN_ALERT "<1>" /* action must be taken immediately */ KERN_CRIT "<2>" /* critical conditions */ KERN_ERR "<3>" /* error conditions */ KERN_WARNING "<4>" /* warning conditions */ KERN_NOTICE "<5>" /* normal but significant condition */ KERN_INFO "<6>" /* informational */ KERN_DEBUG "<7>" /* debug-level messages */ © 2010 Anil Kumar Pugalia <email@sarika-pugs.com> 21 All Rights Reserved.
  22. 22. The Module Constructor (revisited)static int __init mfd_init(void){ printk(KERN_INFO "mfd registered"); ... return 0;}module_init(mfd_init); © 2010 Anil Kumar Pugalia <email@sarika-pugs.com> 22 All Rights Reserved.
  23. 23. The Module Destructor (revisited)static void __exit mfd_exit(void){ printk(KERN_INFO "mfd deregistered"); ...}module_exit(mfd_exit); © 2010 Anil Kumar Pugalia <email@sarika-pugs.com> 23 All Rights Reserved.
  24. 24. The Other Basics & OrnamentsBasic Headers #include <linux/module.h> #include <linux/version.h> #include <linux/kernel.h>MODULE_LICENSE("GPL");MODULE_AUTHOR("Anil Kumar Pugalia");MODULE_DESCRIPTION("First Device Driver"); © 2010 Anil Kumar Pugalia <email@sarika-pugs.com> 24 All Rights Reserved.
  25. 25. Building the ModuleFor building our driver, it needs The Kernel Headers for Prototypes The Kernel Functions for Functionality The Kernel Build System & the Makefile for BuildingTwo options to Achieve 1. Building under Kernel Source Tree Put our driver appropriately under drivers folder Edit corresponding Kconfig(s) & Makefile to include our driver 2. Create our own Makefile to do the right invocation © 2010 Anil Kumar Pugalia <email@sarika-pugs.com> 25 All Rights Reserved.
  26. 26. Our Makefileifeq (${KERNELRELEASE},) KERNEL_SOURCE := <kernel source directory path> PWD := $(shell pwd)default: $(MAKE) -C ${KERNEL_SOURCE} SUBDIRS=$(PWD) modulesclean: $(MAKE) -C ${KERNEL_SOURCE} SUBDIRS=$(PWD) cleanelse obj-m += <module>.oendif © 2010 Anil Kumar Pugalia <email@sarika-pugs.com> 26 All Rights Reserved.
  27. 27. Try out your First Linux Driver © 2010 Anil Kumar Pugalia <email@sarika-pugs.com> 27 All Rights Reserved.
  28. 28. What all have we learnt?Ws of Linux DriversEcosystem of Linux DriversTypes of Linux DriversVertical & Horizontal Driver LayeringVarious Terminologies in vogueLinux Driver related Commands & ConfigsUsing a Linux DriverOur First Linux Driver © 2010 Anil Kumar Pugalia <email@sarika-pugs.com> 28 All Rights Reserved.
  29. 29. Any Queries?© 2010 Anil Kumar Pugalia <email@sarika-pugs.com> 29 All Rights Reserved.

×