Device Drivers in Linux  T S PRADEEP KUMAR  Lecturer, SCS, VIT
Key points <ul><li>History </li></ul><ul><li>Why Linux </li></ul><ul><li>Linux Kernel </li></ul><ul><li>Device Drivers </l...
History <ul><li>Linux is a modern, free operating system based on UNIX standards. </li></ul><ul><li>First developed as a s...
What Linux in Embedded?
Linux Kernel/System/Distribution <ul><li>Kernel </li></ul><ul><ul><li>The OS Code that runs on privileged mode </li></ul><...
Red Hat Directory Tree
User mode and kernel mode
User Space <ul><li>At any given time, the CPU executes in </li></ul><ul><li>either User Mode or Kernel Mode </li></ul><ul>...
Kernel Space <ul><li>Full privileges, Full access </li></ul><ul><li>Any subroutines or functions forming part of the kerne...
Kernel Modules or Device Drivers <ul><li>Multiple Processes trying to access a particular hardware </li></ul><ul><li>Execu...
Everything is File <ul><li>You have probably heard the saying that in UNIX, everything is a file. </li></ul><ul><li>This i...
Prerequisites <ul><li>Basic knowledge of kernel compilation </li></ul><ul><li>A good deal of programming experience in C u...
Major & Minor Numbers <ul><li>Devices require unique identification in the system  </li></ul><ul><li>The major number iden...
Blocks & Characters <ul><li>Devices within the kernel can be separated between character and block devices. </li></ul><ul>...
Device Drivers <ul><li>Option 1 </li></ul><ul><li>Build device driver into the kernel </li></ul><ul><ul><li>Advantage – Dr...
Device Drivers as a Module <ul><li>If a device is provided as a module, then it must be registered and unregistered with t...
Accessing the Device <ul><li>Once created, the device is accessible through a standard interface. </li></ul><ul><ul><li>Fu...
Device Driver Events Cleanup_module() rmmod Remove Device file_operations:release fclose Close Device file_operations:writ...
Demos <ul><li>To list currently loaded kernel modules </li></ul><ul><ul><li>/sbin/lsmod </li></ul></ul><ul><li>Example cha...
Hello World Device Driver <ul><li>#include <linux/module.h> </li></ul><ul><li>#include <linux/kernel.h> </li></ul><ul><li>...
<ul><li>static void cleanup_module(void) </li></ul><ul><li>{ </li></ul><ul><li>unregister_chrdev(major,”mychr”); </li></ul...
Makefile (2.6 Kernel) <ul><li>objm := hellokm.o </li></ul><ul><li>KDIR := /lib/modules/$(shell uname ­r) </li></ul><ul><li...
Compilation (2.4 KERNEL) <ul><li>root#  gcc -c hello.c </li></ul><ul><li>root#  insmod ./hello.o </li></ul><ul><li>Hello, ...
<ul><li>Thank you </li></ul>
Upcoming SlideShare
Loading in …5
×

Device drivers tsp

3,377 views

Published on

Published in: Education
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
3,377
On SlideShare
0
From Embeds
0
Number of Embeds
609
Actions
Shares
0
Downloads
151
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Device drivers tsp

  1. 1. Device Drivers in Linux T S PRADEEP KUMAR Lecturer, SCS, VIT
  2. 2. Key points <ul><li>History </li></ul><ul><li>Why Linux </li></ul><ul><li>Linux Kernel </li></ul><ul><li>Device Drivers </li></ul>
  3. 3. History <ul><li>Linux is a modern, free operating system based on UNIX standards. </li></ul><ul><li>First developed as a small but self-contained kernel in 1991 by Linus Torvalds, with the major design goal of UNIX compatibility. </li></ul><ul><li>Collaboration by many users all around the world, corresponding almost exclusively over the Internet. </li></ul>
  4. 4. What Linux in Embedded?
  5. 5. Linux Kernel/System/Distribution <ul><li>Kernel </li></ul><ul><ul><li>The OS Code that runs on privileged mode </li></ul></ul><ul><li>System </li></ul><ul><ul><li>Essential system components, but runs in user mode </li></ul></ul><ul><ul><li>Compilers, system libraries </li></ul></ul><ul><li>Linux Distribution </li></ul><ul><ul><li>Precompiled and ready to install tools and packages </li></ul></ul><ul><ul><li>Popular distributions: redHat, SuSE, Debain, Mandrake,etc. </li></ul></ul>
  6. 6. Red Hat Directory Tree
  7. 7. User mode and kernel mode
  8. 8. User Space <ul><li>At any given time, the CPU executes in </li></ul><ul><li>either User Mode or Kernel Mode </li></ul><ul><li>User Space </li></ul><ul><ul><li>Cannot execute privileged instructions </li></ul></ul><ul><ul><li>Cannot access kernel code and data </li></ul></ul><ul><ul><li>Cannot directly access hardware resources </li></ul></ul><ul><ul><li>Shell or other applications like Open Office,etc. </li></ul></ul>
  9. 9. Kernel Space <ul><li>Full privileges, Full access </li></ul><ul><li>Any subroutines or functions forming part of the kernel (modules and device drivers, etc) are considered to be part of kernel space. </li></ul>
  10. 10. Kernel Modules or Device Drivers <ul><li>Multiple Processes trying to access a particular hardware </li></ul><ul><li>Executed as part of the system kernel </li></ul><ul><li>Drivers are either part of the kernel or loaded as a module </li></ul>
  11. 11. Everything is File <ul><li>You have probably heard the saying that in UNIX, everything is a file. </li></ul><ul><li>This is true, because ALL devices are provided as a file in the UNIX OS. </li></ul><ul><li>The user can then perform basic file operations on the device </li></ul><ul><ul><li>read, write, open, close, ... </li></ul></ul>
  12. 12. Prerequisites <ul><li>Basic knowledge of kernel compilation </li></ul><ul><li>A good deal of programming experience in C under Linux </li></ul><ul><li>the right techniques of data structures, like linked list is essential along with their data types. </li></ul>
  13. 13. Major & Minor Numbers <ul><li>Devices require unique identification in the system </li></ul><ul><li>The major number identifies the type of device </li></ul><ul><li>The minor number identifies any mode or subunit of the device. </li></ul>
  14. 14. Blocks & Characters <ul><li>Devices within the kernel can be separated between character and block devices. </li></ul><ul><li>A block device is something that can host a filesystem such as a disk. A block device can only be accessed as multiples of a block, where a block is usually 1kB of data . </li></ul><ul><li>A character device is one that can be accessed like a file. </li></ul><ul><ul><li>This driver implements the open, close, read and write system calls. </li></ul></ul><ul><ul><li>Examples: Console, Parallel Ports, Serial Ports, etc. </li></ul></ul>
  15. 15. Device Drivers <ul><li>Option 1 </li></ul><ul><li>Build device driver into the kernel </li></ul><ul><ul><li>Advantage – Driver available at boottime </li></ul></ul><ul><ul><li>Disadvantage – My need to load drivers that are rarely used </li></ul></ul><ul><li>Option 2 </li></ul><ul><li>Build device driver as a kernel module </li></ul><ul><ul><li>Advantage – Load When Needed </li></ul></ul><ul><ul><li>Advantage – Unload when not longer needed </li></ul></ul><ul><ul><li>Disadvantage – Potential attempts to load “bad” modules into the kernel </li></ul></ul>
  16. 16. Device Drivers as a Module <ul><li>If a device is provided as a module, then it must be registered and unregistered with the kernel. </li></ul><ul><li>To register a device driver </li></ul><ul><ul><li>register_chrdev() </li></ul></ul><ul><ul><li>register_blkdev() </li></ul></ul><ul><li>To unregister a device driver </li></ul><ul><ul><li>unregister_chrdev() or </li></ul></ul><ul><ul><li>unregister_blkdev() </li></ul></ul>
  17. 17. Accessing the Device <ul><li>Once created, the device is accessible through a standard interface. </li></ul><ul><ul><li>Function pointers in a structure. </li></ul></ul><ul><li>Two types of structures typically exist, one for character devices and one for block devices. </li></ul>
  18. 18. Device Driver Events Cleanup_module() rmmod Remove Device file_operations:release fclose Close Device file_operations:write fwrite Write Device file_operations:read fread Read Device file_operations:open fopen Open Device init_module() insmod Load Module Kernel Functions User Functions Events
  19. 19. Demos <ul><li>To list currently loaded kernel modules </li></ul><ul><ul><li>/sbin/lsmod </li></ul></ul><ul><li>Example character device name </li></ul><ul><ul><li>ls ­l /dev/lp0 </li></ul></ul><ul><li>Example block device name </li></ul><ul><ul><li>ls ­l /dev/hda </li></ul></ul><ul><li>Show list of registered character/block devices </li></ul><ul><ul><li>cat /proc/devices </li></ul></ul><ul><li>Show list of network interfaces </li></ul><ul><ul><li>/sbin/ifconfig ­a </li></ul></ul>
  20. 20. Hello World Device Driver <ul><li>#include <linux/module.h> </li></ul><ul><li>#include <linux/kernel.h> </li></ul><ul><li>MODULE_LICENSE(“GPL”); </li></ul><ul><li>static int major=0; </li></ul><ul><li>static struct file_operations fops={}; </li></ul><ul><li>static int init_module(void) </li></ul><ul><li>{ </li></ul><ul><li>printk(&quot;hello, worldn&quot;); </li></ul><ul><li>major=register_chrdev(major,”mychr”,&fops); </li></ul><ul><li>printk(“major=%dn”,major); </li></ul><ul><li>return 0; </li></ul><ul><li>} </li></ul>
  21. 21. <ul><li>static void cleanup_module(void) </li></ul><ul><li>{ </li></ul><ul><li>unregister_chrdev(major,”mychr”); </li></ul><ul><li>printk(“Bye”); </li></ul><ul><li>} </li></ul>
  22. 22. Makefile (2.6 Kernel) <ul><li>objm := hellokm.o </li></ul><ul><li>KDIR := /lib/modules/$(shell uname ­r) </li></ul><ul><li>PWD := $(shell pwd) </li></ul><ul><li>default: </li></ul><ul><li>make ­C $(KDIR) SUBDIRS=$(PWD) modules </li></ul>
  23. 23. Compilation (2.4 KERNEL) <ul><li>root# gcc -c hello.c </li></ul><ul><li>root# insmod ./hello.o </li></ul><ul><li>Hello, world </li></ul><ul><li>root# rmmod hello </li></ul><ul><li>Goodbye cruel world </li></ul><ul><li>root# </li></ul>
  24. 24. <ul><li>Thank you </li></ul>

×