SlideShare a Scribd company logo
Linux Device Drivers [email_address]
A software component that controls a hardware device interacts with user programs better provide basic hardware controls  only leave high level decision to user programs e.g.) floppy driver provides only a view of bytes sequence A layer between hardware and user programs defines how the device appears to user applications there can be several drivers for a single hardware a single driver can handle several similar devices Devices? memory, disk, memory, CPU, …. What is a device driver?
A programming module with interfaces Communication Medium between application/user and hardware In Unix, Kernel module device driver interface = file interface What are normal operations? Block vs. character Device Driver
Kernel View
Kernel Components Process management –  Creating and destroying processes –  Handling their connection to the outside world (input and output). –  Communication among different processes (through signals, pipes, or Inter Process communication primitives) –  Scheduling Memory management –  Provides virtual addressing space for any and all processes –  The different parts of the kernel interact with the memory-management subsystem
Kernel Components File systems –  Unix is heavily based on the file system concept;  almost everything in Unix can be treated as a file. –  The kernel builds a structured file system on top of unstructured hardware. –  The resulting file abstraction is heavily used throughout the whole system. –  In addition, Linux supports multiple file system types, that is, different ways of organizing data on the physical medium.
Kernel Components Device control –  Almost every system operation eventually maps to a physical device. –  With the exception of the processor, memory, and a very few other entities, any and all device control operations are performed by code that is specific to the device being addressed.  That code is called a  device driver . –  The kernel must have embedded in it a device driver for every peripheral present on a system, from the hard drive to the keyboard and the tape drive.
Kernel Components Networking –  Networking must be managed by the operating system, because most network operations are not specific to a process: incoming packets are asynchronous events. –  The packets must be collected, identified, and dispatched before a process takes care of them. –  The system is in charge of delivering data packets across program and network interfaces, and it must control the execution of programs according to their network activity. –  Routing and address resolution are implemented in the Kernel
What are device drivers in Linux? From the User‘s point of view: files From the Kernel‘s point of view: a set of  VFS  functions (read, write, open) plus some  register  functions Part of the Kernel -> run in kernel mode Either loadable or statically build in the kernel Two different kinds of access: Sequential and random char and block devices
Linux Device Drivers A set of API subroutines (typically system calls) interface to hardware Hide implementation and hardware-specific details from a user program Typically use a file interface metaphor Device is a special file Manage data flow between a user program and devices A self-contained component (add/remove from kernel) A user can access the device via file name in /dev , e.g. /dev/lp0
Kernel Modules Versus Applications •  Modules are event-driven –  Every kernel module registers itself in order to serve future requests –  It’s initialization function terminates immediately –  Exit function of a module must carefully undo everything the init function built up •  User-level applications can call functions they don’t define –  Linking stage resolves external references using libraries •  Module is linked only to the kernel,  the only functions it can call are the ones exported by the kernel –  No libraries to link to –  Example:  printk  is the version of  printf  defined within the kernel and exported to the modules •  Don’t include typical user-level header files  (like <stdio.h>, etc.) –  Only functions that are actually part of the kernel may be used in kernel modules –  Anything related to the kernel is declared in headers found in the kernel source tree
 
User Space Versus Kernel Space •  Module  runs in  kernel space ,  whereas  Applications  run in  user space •  Unix transfers execution from user space to kernel space whenever an application issues a system call or is suspended by a hardware interrupt •  Role of a module is to extend kernel functionality –  Some functions in the module are executed as part of system calls –  Some are in charge of interrupt handling •  Linux driver code must be reentrant –  Must be capable of running in more than one context at the same time –  Must avoid race conditions => Linux 2.6 is a preemptive kernel!
User program  & Kernel  interface
Block I/O on Linux
Device Driver Types Character Devices –  Accessed as a stream of bytes (like a file) –  Typically just data channels, which allow only sequential access Some char devices look like data areas and allow moving back and forth in them (example: frame grabbers) –  A char driver is in charge of implementing this behavior –  Char devices are accessed by means of file system nodes Example:  /dev/tty1  and  /dev/lp0 –  Driver needs to implement at least the  open ,  close ,  read , and write  system calls Examples: –  Text console  (/dev/console ) –  Serial ports  (/dev/ttyS0 )
Device Driver Types Block Devices –  In some Unix systems, block devices can only handle I/O operations that transfer one or more whole blocks, which are usually 512 bytes (or a larger power of 2). Linux, instead, allows applications to read and write a block device like a char device –  Like char devices accessed through file system nodes in the /dev directory –  Char and block devices differ in the kernel/driver interface –  Difference between char and block devices is transparent to users
Device Driver Types Network Interfaces –  Network transactions made through an interface •  Hardware device •  Pure software device (loop back) –  Network interfaces usually designed around the transmission and receipt of packets •  Network driver knows nothing about individual connections; it only handles packets –  Char device? Block device? •  not easily mapped to file system nodes •  Network interfaces don’t have entries in the file system •  Communication between the kernel and network device driver completely different from that used with char and block drivers
Block Versus  Character devices
Loadable Kernel Module (LKM) A new kernel module can be added on the fly (while OS is still running) LKMs are often called “kernel modules” They are not user program
Types of LKM Device drivers  File system driver (one for ext2, MSDOS FAT16, 32, NFS) System calls Network Drivers TTY line disciplines. special terminal devices.  Executable interpreters.
Basic LKM (program) Every LKM consist of two basic functions (minimum)  : int init_module(void)  /*used for all initialition stuff*/  {  ...  }  void cleanup_module(void)  /*used for a clean shutdown*/  {  ...  }  Loading a module - normally restricted to root - is managed by issuing the following command:  # insmod module.o
LKM Utilities cmd insmod   Insert an LKM into the kernel. rmmod  Remove an LKM from the kernel. depmod  Determine interdependencies between LKMs. kerneld  Kerneld daemon program ksyms   Display symbols that are exported by the kernel for use by new LKMs. lsmod   List currently loaded LKMs. modinfo  Display contents of .modinfo section in an LKM object file. modprobe   Insert or remove an LKM or set of LKMs intelligently. For example, if you must load A before loading B, Modprobe will automatically load A when you tell it to load B.
Common LKM util cmd Create a special device file % mknode /dev/driver c 40 0  Insert a new module % insmod modname Remove a module %rmmod modname List module % lsmod or % more /proc/modules audio  37840  0 cmpci  24544  0 soundcore  4208  4 [audio cmpci] nfsd  70464  8 (autoclean)
General implementation steps Understand the device characteristic and supported commands. Map device specific operations to UNIX file operation Select the device name (user interface) Namespace (2-3 characters, /dev/lp0) (optional) select a major number and minor (a device special file creation) for VFS interface Mapping the number to right device sub-routines Implement file interface subroutines Compile the device driver Install the device driver module with loadable kernel module (LKM) or Rebuild (compile) the kernel
Read/write (I/O) IO-Operations have unpredictable termination time -waiting for positioning the head of a hard disk -waiting for keyboard input Two strategies -polling mode -interrupt mode
Polling mode To poll (befragen) Read the status register repeatedly until it changes   -> spin locks (busy waits) Inefficient, if duration in the order of milliseconds schedule inside the loop interrupt mode I/O-Controller not capable of signaling Fastest way to communicate with hardware
Interrupt mode An interrupt handling routine is registered with the kernel After triggering the operation, process is suspended when finished, an interrupt is issued process is awaken
Device Driver Process I/O Device read device wake up process suspend process issue interrupt and read data issue read command  Interrupt mode
Interrupt mode
Device special file Device number Major (used to VFS mapping to right functions) Minor (sub-devices) mknod  /dev/stk c 38 0 ls –l /dev/tty crw-rw-rw-  1 root  root  5,  0 Apr 21 18:33 /dev/tty
Register and unregister device int init_module(void)  /*used for all initialition stuff*/  {  /* Register the character device (atleast try) */ Major = register_chrdev(0, DEVICE_NAME, &Fops); : }  void cleanup_module(void)  /*used for a clean shutdown*/  {ret = unregister_chrdev(Major, DEVICE_NAME); ...  }
Register and unregister device compile -Wall -DMODULE -D__KERNEL__ -DLINUX –DDEBUG -I /usr/include/linux/version.h  -I/lib/modules/`uname -r`/build/include Install the module %insmod module.o  List the module %lsmod If you let the system pick Major number, you can find the major number (for special creation) by % more /proc/devices Make a special file  % mknod /dev/device_name c major minor
Implementation Assuming that your device name is Xxx Xxx_init() initialize the device when OS is booted Xxx_open() open a device  Xxx_read() read from kernel memory  Xxx_write() write  Xxx_release() clean-up (close) init_module() cleanup_module()
kernel functions add_timer()  Causes a function to be executed when a given amount of time has passed  cli()   Prevents interrupts from being acknowledged  end_request()   Called when a request has been satisfied or aborted  free_irq()   Frees an IRQ previously acquired with request_irq() or irqaction()  get_user*()   Allows a driver to access data in user space, a memory area distinct from the kernel  inb(), inb_p()   Reads a byte from a port. Here, inb() goes as fast as it can, while inb_p() pauses before returning.  irqaction()   Registers an interrupt like a signal.  IS_*(inode)   Tests if inode is on a file system mounted with the corresponding flag.  kfree*()   Frees memory previously allocated with kmalloc()  kmalloc()   Allocates a chu nk of memory no larger than 4096 bytes.  MAJOR()   Reports the major device number for a device.  MINOR()  Reports the minor device number for a device.
kernel functions memcpy_*fs()   Copies chunks of memory between user space and kernel space  outb(), outb_p()   Writes a byte to a port. Here, outb() goes as fast as it can, while outb_p() pauses before returning.  printk()   A version of printf() for the kernel.  put_user*()   Allows a driver to write data in user space.  register_*dev()   Registers a device with the kernel.  request_irq()   Requests an IRQ from the kernel, and, if successful, installs an IRQ interrupt handler.  select_wait()   Adds a process to the proper select_wait queue.  *sleep_on()  Sleeps on an event, puts a wait_queue entry in the list so that the process can be awakened on that event.  sti()   Allows interrupts to be acknowledged.  sys_get*()   System calls used to get information regarding the process, user, or group.  wake_up*()   Wakes up a process that has been put to sleep by the matching *sleep_on() function.
Pitfalls Using standard libraries : can only use kernel functions, which are the functions you can see in /proc/ksyms.        Disabling interrupts  You might need to do this for a short time and that is OK, but if you don't enable them afterwards, your system will be stuck  Changes from version to version

More Related Content

What's hot (20)

PDF
Introduction to char device driver
Vandana Salve
 
PDF
Jagan Teki - U-boot from scratch
linuxlab_conf
 
PDF
Arm device tree and linux device drivers
Houcheng Lin
 
PPT
U Boot or Universal Bootloader
Satpal Parmar
 
PDF
Embedded Operating System - Linux
Emertxe Information Technologies Pvt Ltd
 
PDF
Embedded linux network device driver development
Amr Ali (ISTQB CTAL Full, CSM, ITIL Foundation)
 
PDF
Kernel Module Programming
Saurabh Bangad
 
PPTX
Linux device drivers
Abhishek Sagar
 
PDF
Part 02 Linux Kernel Module Programming
Tushar B Kute
 
PPTX
U-Boot presentation 2013
Wave Digitech
 
PDF
Introduction to Modern U-Boot
GlobalLogic Ukraine
 
PDF
Introduction To Linux Kernel Modules
dibyajyotig
 
PPTX
Bootloaders (U-Boot)
Omkar Rane
 
PPTX
Linux Kernel MMC Storage driver Overview
RajKumar Rampelli
 
PDF
Embedded Android : System Development - Part IV
Emertxe Information Technologies Pvt Ltd
 
PDF
Uboot startup sequence
Houcheng Lin
 
PPTX
Linux Kernel Tour
samrat das
 
PPTX
Linux Ethernet device driver
艾鍗科技
 
PDF
Lesson 2 Understanding Linux File System
Sadia Bashir
 
Introduction to char device driver
Vandana Salve
 
Jagan Teki - U-boot from scratch
linuxlab_conf
 
Arm device tree and linux device drivers
Houcheng Lin
 
U Boot or Universal Bootloader
Satpal Parmar
 
Embedded Operating System - Linux
Emertxe Information Technologies Pvt Ltd
 
Embedded linux network device driver development
Amr Ali (ISTQB CTAL Full, CSM, ITIL Foundation)
 
Kernel Module Programming
Saurabh Bangad
 
Linux device drivers
Abhishek Sagar
 
Part 02 Linux Kernel Module Programming
Tushar B Kute
 
U-Boot presentation 2013
Wave Digitech
 
Introduction to Modern U-Boot
GlobalLogic Ukraine
 
Introduction To Linux Kernel Modules
dibyajyotig
 
Bootloaders (U-Boot)
Omkar Rane
 
Linux Kernel MMC Storage driver Overview
RajKumar Rampelli
 
Embedded Android : System Development - Part IV
Emertxe Information Technologies Pvt Ltd
 
Uboot startup sequence
Houcheng Lin
 
Linux Kernel Tour
samrat das
 
Linux Ethernet device driver
艾鍗科技
 
Lesson 2 Understanding Linux File System
Sadia Bashir
 

Similar to linux device driver (20)

PPTX
Device Drivers
Kushal Modi
 
PPT
Linuxdd[1]
mcganesh
 
PPT
Embedded system - embedded system programming
Vibrant Technologies & Computers
 
PPT
Driver_linux
Sayanton Vhaduri
 
PPT
Device drivers tsp
Pradeep Kumar TS
 
PPT
Linux io
Sagar Janagonda
 
PDF
brief intro to Linux device drivers
Alexandre Moreno
 
PDF
Driver Programming Report
Shivek Khurana
 
PPT
“black boxes” that make a particular p..
JcRaajab1
 
PPTX
Device Drivers and Running Modules
YourHelper1
 
PPT
lecture_1_introduction_linux_1234567.ppt
ubalearchana6
 
PPT
lecture_1_introduction.ppt
RandyGaray
 
PPTX
Linux Device Driver’s
Rashmi Warghade
 
PPTX
Writing Character driver (loadable module) in linux
RajKumar Rampelli
 
PDF
Embedded Android : System Development - Part II (Linux device drivers)
Emertxe Information Technologies Pvt Ltd
 
PPT
device drives in electronics and communication
gawthamanna
 
PPT
Linux Device Driver for Writing a real world driver for embedded Linux
AchyuthShettigar2
 
PDF
Unit 5.2 Device Driver.pdf (Device Driver)
AnilkumarBrahmane2
 
PDF
Studienarb linux kernel-dev
murali_purushothaman
 
PPT
LINUX Device Drivers
Partha Bhattacharya
 
Device Drivers
Kushal Modi
 
Linuxdd[1]
mcganesh
 
Embedded system - embedded system programming
Vibrant Technologies & Computers
 
Driver_linux
Sayanton Vhaduri
 
Device drivers tsp
Pradeep Kumar TS
 
Linux io
Sagar Janagonda
 
brief intro to Linux device drivers
Alexandre Moreno
 
Driver Programming Report
Shivek Khurana
 
“black boxes” that make a particular p..
JcRaajab1
 
Device Drivers and Running Modules
YourHelper1
 
lecture_1_introduction_linux_1234567.ppt
ubalearchana6
 
lecture_1_introduction.ppt
RandyGaray
 
Linux Device Driver’s
Rashmi Warghade
 
Writing Character driver (loadable module) in linux
RajKumar Rampelli
 
Embedded Android : System Development - Part II (Linux device drivers)
Emertxe Information Technologies Pvt Ltd
 
device drives in electronics and communication
gawthamanna
 
Linux Device Driver for Writing a real world driver for embedded Linux
AchyuthShettigar2
 
Unit 5.2 Device Driver.pdf (Device Driver)
AnilkumarBrahmane2
 
Studienarb linux kernel-dev
murali_purushothaman
 
LINUX Device Drivers
Partha Bhattacharya
 
Ad

Recently uploaded (20)

PDF
ARAL-Orientation_Morning-Session_Day-11.pdf
JoelVilloso1
 
PPTX
2025 Winter SWAYAM NPTEL & A Student.pptx
Utsav Yagnik
 
PDF
ARAL_Orientation_Day-2-Sessions_ARAL-Readung ARAL-Mathematics ARAL-Sciencev2.pdf
JoelVilloso1
 
PPTX
PATIENT ASSIGNMENTS AND NURSING CARE RESPONSIBILITIES.pptx
PRADEEP ABOTHU
 
PPTX
Views on Education of Indian Thinkers Mahatma Gandhi.pptx
ShrutiMahanta1
 
PDF
LAW OF CONTRACT (5 YEAR LLB & UNITARY LLB )- MODULE - 1.& 2 - LEARN THROUGH P...
APARNA T SHAIL KUMAR
 
PPTX
THE TAME BIRD AND THE FREE BIRD.pptxxxxx
MarcChristianNicolas
 
PPTX
Stereochemistry-Optical Isomerism in organic compoundsptx
Tarannum Nadaf-Mansuri
 
PDF
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
PDF
CEREBRAL PALSY: NURSING MANAGEMENT .pdf
PRADEEP ABOTHU
 
PPTX
grade 5 lesson ENGLISH 5_Q1_PPT_WEEK3.pptx
SireQuinn
 
PPTX
Mathematics 5 - Time Measurement: Time Zone
menchreo
 
PPTX
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
PPTX
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
PPSX
Health Planning in india - Unit 03 - CHN 2 - GNM 3RD YEAR.ppsx
Priyanshu Anand
 
PPTX
SPINA BIFIDA: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
PPTX
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
PPTX
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
PPTX
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
PDF
The Different Types of Non-Experimental Research
Thelma Villaflores
 
ARAL-Orientation_Morning-Session_Day-11.pdf
JoelVilloso1
 
2025 Winter SWAYAM NPTEL & A Student.pptx
Utsav Yagnik
 
ARAL_Orientation_Day-2-Sessions_ARAL-Readung ARAL-Mathematics ARAL-Sciencev2.pdf
JoelVilloso1
 
PATIENT ASSIGNMENTS AND NURSING CARE RESPONSIBILITIES.pptx
PRADEEP ABOTHU
 
Views on Education of Indian Thinkers Mahatma Gandhi.pptx
ShrutiMahanta1
 
LAW OF CONTRACT (5 YEAR LLB & UNITARY LLB )- MODULE - 1.& 2 - LEARN THROUGH P...
APARNA T SHAIL KUMAR
 
THE TAME BIRD AND THE FREE BIRD.pptxxxxx
MarcChristianNicolas
 
Stereochemistry-Optical Isomerism in organic compoundsptx
Tarannum Nadaf-Mansuri
 
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
CEREBRAL PALSY: NURSING MANAGEMENT .pdf
PRADEEP ABOTHU
 
grade 5 lesson ENGLISH 5_Q1_PPT_WEEK3.pptx
SireQuinn
 
Mathematics 5 - Time Measurement: Time Zone
menchreo
 
Universal immunization Programme (UIP).pptx
Vishal Chanalia
 
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
Health Planning in india - Unit 03 - CHN 2 - GNM 3RD YEAR.ppsx
Priyanshu Anand
 
SPINA BIFIDA: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
The Different Types of Non-Experimental Research
Thelma Villaflores
 
Ad

linux device driver

  • 1. Linux Device Drivers [email_address]
  • 2. A software component that controls a hardware device interacts with user programs better provide basic hardware controls only leave high level decision to user programs e.g.) floppy driver provides only a view of bytes sequence A layer between hardware and user programs defines how the device appears to user applications there can be several drivers for a single hardware a single driver can handle several similar devices Devices? memory, disk, memory, CPU, …. What is a device driver?
  • 3. A programming module with interfaces Communication Medium between application/user and hardware In Unix, Kernel module device driver interface = file interface What are normal operations? Block vs. character Device Driver
  • 5. Kernel Components Process management – Creating and destroying processes – Handling their connection to the outside world (input and output). – Communication among different processes (through signals, pipes, or Inter Process communication primitives) – Scheduling Memory management – Provides virtual addressing space for any and all processes – The different parts of the kernel interact with the memory-management subsystem
  • 6. Kernel Components File systems – Unix is heavily based on the file system concept; almost everything in Unix can be treated as a file. – The kernel builds a structured file system on top of unstructured hardware. – The resulting file abstraction is heavily used throughout the whole system. – In addition, Linux supports multiple file system types, that is, different ways of organizing data on the physical medium.
  • 7. Kernel Components Device control – Almost every system operation eventually maps to a physical device. – With the exception of the processor, memory, and a very few other entities, any and all device control operations are performed by code that is specific to the device being addressed. That code is called a device driver . – The kernel must have embedded in it a device driver for every peripheral present on a system, from the hard drive to the keyboard and the tape drive.
  • 8. Kernel Components Networking – Networking must be managed by the operating system, because most network operations are not specific to a process: incoming packets are asynchronous events. – The packets must be collected, identified, and dispatched before a process takes care of them. – The system is in charge of delivering data packets across program and network interfaces, and it must control the execution of programs according to their network activity. – Routing and address resolution are implemented in the Kernel
  • 9. What are device drivers in Linux? From the User‘s point of view: files From the Kernel‘s point of view: a set of VFS functions (read, write, open) plus some register functions Part of the Kernel -> run in kernel mode Either loadable or statically build in the kernel Two different kinds of access: Sequential and random char and block devices
  • 10. Linux Device Drivers A set of API subroutines (typically system calls) interface to hardware Hide implementation and hardware-specific details from a user program Typically use a file interface metaphor Device is a special file Manage data flow between a user program and devices A self-contained component (add/remove from kernel) A user can access the device via file name in /dev , e.g. /dev/lp0
  • 11. Kernel Modules Versus Applications • Modules are event-driven – Every kernel module registers itself in order to serve future requests – It’s initialization function terminates immediately – Exit function of a module must carefully undo everything the init function built up • User-level applications can call functions they don’t define – Linking stage resolves external references using libraries • Module is linked only to the kernel, the only functions it can call are the ones exported by the kernel – No libraries to link to – Example: printk is the version of printf defined within the kernel and exported to the modules • Don’t include typical user-level header files (like <stdio.h>, etc.) – Only functions that are actually part of the kernel may be used in kernel modules – Anything related to the kernel is declared in headers found in the kernel source tree
  • 12.  
  • 13. User Space Versus Kernel Space • Module runs in kernel space , whereas Applications run in user space • Unix transfers execution from user space to kernel space whenever an application issues a system call or is suspended by a hardware interrupt • Role of a module is to extend kernel functionality – Some functions in the module are executed as part of system calls – Some are in charge of interrupt handling • Linux driver code must be reentrant – Must be capable of running in more than one context at the same time – Must avoid race conditions => Linux 2.6 is a preemptive kernel!
  • 14. User program & Kernel interface
  • 15. Block I/O on Linux
  • 16. Device Driver Types Character Devices – Accessed as a stream of bytes (like a file) – Typically just data channels, which allow only sequential access Some char devices look like data areas and allow moving back and forth in them (example: frame grabbers) – A char driver is in charge of implementing this behavior – Char devices are accessed by means of file system nodes Example: /dev/tty1 and /dev/lp0 – Driver needs to implement at least the open , close , read , and write system calls Examples: – Text console (/dev/console ) – Serial ports (/dev/ttyS0 )
  • 17. Device Driver Types Block Devices – In some Unix systems, block devices can only handle I/O operations that transfer one or more whole blocks, which are usually 512 bytes (or a larger power of 2). Linux, instead, allows applications to read and write a block device like a char device – Like char devices accessed through file system nodes in the /dev directory – Char and block devices differ in the kernel/driver interface – Difference between char and block devices is transparent to users
  • 18. Device Driver Types Network Interfaces – Network transactions made through an interface • Hardware device • Pure software device (loop back) – Network interfaces usually designed around the transmission and receipt of packets • Network driver knows nothing about individual connections; it only handles packets – Char device? Block device? • not easily mapped to file system nodes • Network interfaces don’t have entries in the file system • Communication between the kernel and network device driver completely different from that used with char and block drivers
  • 19. Block Versus Character devices
  • 20. Loadable Kernel Module (LKM) A new kernel module can be added on the fly (while OS is still running) LKMs are often called “kernel modules” They are not user program
  • 21. Types of LKM Device drivers File system driver (one for ext2, MSDOS FAT16, 32, NFS) System calls Network Drivers TTY line disciplines. special terminal devices. Executable interpreters.
  • 22. Basic LKM (program) Every LKM consist of two basic functions (minimum) : int init_module(void) /*used for all initialition stuff*/ { ... } void cleanup_module(void) /*used for a clean shutdown*/ { ... } Loading a module - normally restricted to root - is managed by issuing the following command: # insmod module.o
  • 23. LKM Utilities cmd insmod Insert an LKM into the kernel. rmmod Remove an LKM from the kernel. depmod Determine interdependencies between LKMs. kerneld Kerneld daemon program ksyms Display symbols that are exported by the kernel for use by new LKMs. lsmod List currently loaded LKMs. modinfo Display contents of .modinfo section in an LKM object file. modprobe Insert or remove an LKM or set of LKMs intelligently. For example, if you must load A before loading B, Modprobe will automatically load A when you tell it to load B.
  • 24. Common LKM util cmd Create a special device file % mknode /dev/driver c 40 0 Insert a new module % insmod modname Remove a module %rmmod modname List module % lsmod or % more /proc/modules audio 37840 0 cmpci 24544 0 soundcore 4208 4 [audio cmpci] nfsd 70464 8 (autoclean)
  • 25. General implementation steps Understand the device characteristic and supported commands. Map device specific operations to UNIX file operation Select the device name (user interface) Namespace (2-3 characters, /dev/lp0) (optional) select a major number and minor (a device special file creation) for VFS interface Mapping the number to right device sub-routines Implement file interface subroutines Compile the device driver Install the device driver module with loadable kernel module (LKM) or Rebuild (compile) the kernel
  • 26. Read/write (I/O) IO-Operations have unpredictable termination time -waiting for positioning the head of a hard disk -waiting for keyboard input Two strategies -polling mode -interrupt mode
  • 27. Polling mode To poll (befragen) Read the status register repeatedly until it changes -> spin locks (busy waits) Inefficient, if duration in the order of milliseconds schedule inside the loop interrupt mode I/O-Controller not capable of signaling Fastest way to communicate with hardware
  • 28. Interrupt mode An interrupt handling routine is registered with the kernel After triggering the operation, process is suspended when finished, an interrupt is issued process is awaken
  • 29. Device Driver Process I/O Device read device wake up process suspend process issue interrupt and read data issue read command Interrupt mode
  • 31. Device special file Device number Major (used to VFS mapping to right functions) Minor (sub-devices) mknod /dev/stk c 38 0 ls –l /dev/tty crw-rw-rw- 1 root root 5, 0 Apr 21 18:33 /dev/tty
  • 32. Register and unregister device int init_module(void) /*used for all initialition stuff*/ { /* Register the character device (atleast try) */ Major = register_chrdev(0, DEVICE_NAME, &Fops); : } void cleanup_module(void) /*used for a clean shutdown*/ {ret = unregister_chrdev(Major, DEVICE_NAME); ... }
  • 33. Register and unregister device compile -Wall -DMODULE -D__KERNEL__ -DLINUX –DDEBUG -I /usr/include/linux/version.h -I/lib/modules/`uname -r`/build/include Install the module %insmod module.o List the module %lsmod If you let the system pick Major number, you can find the major number (for special creation) by % more /proc/devices Make a special file % mknod /dev/device_name c major minor
  • 34. Implementation Assuming that your device name is Xxx Xxx_init() initialize the device when OS is booted Xxx_open() open a device Xxx_read() read from kernel memory Xxx_write() write Xxx_release() clean-up (close) init_module() cleanup_module()
  • 35. kernel functions add_timer() Causes a function to be executed when a given amount of time has passed cli() Prevents interrupts from being acknowledged end_request() Called when a request has been satisfied or aborted free_irq() Frees an IRQ previously acquired with request_irq() or irqaction() get_user*() Allows a driver to access data in user space, a memory area distinct from the kernel inb(), inb_p() Reads a byte from a port. Here, inb() goes as fast as it can, while inb_p() pauses before returning. irqaction() Registers an interrupt like a signal. IS_*(inode) Tests if inode is on a file system mounted with the corresponding flag. kfree*() Frees memory previously allocated with kmalloc() kmalloc() Allocates a chu nk of memory no larger than 4096 bytes. MAJOR() Reports the major device number for a device. MINOR() Reports the minor device number for a device.
  • 36. kernel functions memcpy_*fs() Copies chunks of memory between user space and kernel space outb(), outb_p() Writes a byte to a port. Here, outb() goes as fast as it can, while outb_p() pauses before returning. printk() A version of printf() for the kernel. put_user*() Allows a driver to write data in user space. register_*dev() Registers a device with the kernel. request_irq() Requests an IRQ from the kernel, and, if successful, installs an IRQ interrupt handler. select_wait() Adds a process to the proper select_wait queue. *sleep_on() Sleeps on an event, puts a wait_queue entry in the list so that the process can be awakened on that event. sti() Allows interrupts to be acknowledged. sys_get*() System calls used to get information regarding the process, user, or group. wake_up*() Wakes up a process that has been put to sleep by the matching *sleep_on() function.
  • 37. Pitfalls Using standard libraries : can only use kernel functions, which are the functions you can see in /proc/ksyms.       Disabling interrupts You might need to do this for a short time and that is OK, but if you don't enable them afterwards, your system will be stuck Changes from version to version