SlideShare a Scribd company logo
1 of 11
Step by step approach to
write a simple loadable
“character driver” in linux
- Raj Kumar Rampelli
Outline
• Introduction to kernel & device driver: Please refer to my previous
PPT https://www.slideshare.net/rampalliraj/kernel-device-drivers
• File types in Linux/Unix
• Device files
• Character driver
• Steps to write a loadable character driver module in linux
• Sample character device driver code
• References
File types in Linux/Unix
File Type Definition How they are indicated? How to create? How to see in ls -l command?
Regular File A readable file or a binary file
or an image file or compressed
file
Indicated with “-” in ls -l
command output
Use touch or vi or
vim command
Ex: vim 1.txt
-rw------- 1 rrajk hardware 154
Dec 4 21:15 myscript_dev_t194.sh
Directory File Contains regular files, folders or
special files stored on a physical
device
Indicated with “d” in ls -l
command output
Use mkdir
command
drwx------ 3 rrajk hardware 4096
Dec 3 23:48 obj
Block device file Behave lot like ordinary files.
They are an array of bytes,
values can be read/write
from/to given location.
Indicated with “b” in ls -l
command output. Most
of them present in /dev
directory
Use fdisk
command or
create virtual
partition
brw-rw---- 1 root disk 8, 0 Oct 29
02:07 sda
Character device
file
Provides a serial stream of
input or output
Indicated with “c” in ls -l
command output. Most
of them present in /dev
directory
Use mknod
command
crw-r--r-- 1 root root 1, 11 Oct 29
02:07 kmsg
PIPE file Fist in First out property Indicated with “p” in ls -l
command’s output
Use mkfifo
command
prw-r----- 1 root root 0 2010-02-15
09:35 /dev/.initramfs/usplash_fifo
Symbolic link file Linked files to other files. They
are either directory or regular
file. The inode for this file and
it’s parent file is same.
Indicated with “l” in ls -l
command’s ouput
Use ln command lrwxrwxrwx 1 root root 9 Jun 12
14:16 /var/lock -> /run/lock
Socket files Used to pass information
between applications for
communication purpose
Indicated with “s” in ls -l
command’s ouput
Use socket()
system call
srw-rw-rw- 1 root root 0 Oct 29
02:07 log
Device file
• Device file is a file that associates it’s name visible to the user space applications and it’s triplet (type, major,
minor) to the kernel
• Device file is a user interface to communicate with device driver (character and block driver)
• A device file can represent character devices, which emit a stream data one character at a time, or block
devices which allow random access to blocks of data.
• In Linux, every device represented as a device file
• All device files stored in the /dev directory and they are also called as device nodes.
• Example:
• for Character device file  /dev/input/mice : represents mouse, exposes the movement of mouse as a character stream.
• for block device file  /dev/sda : represents hard disk, exposes the addressable regions of memory of the device.
• Device files (or device nodes) are created by the mknod system call.
• Kernel assigns user requests to the corresponding device driver using triplet information.
• Type: character device (c) or block device (b)
• Major: represents device category
• Minor: identifier of the device.
• When data is read or written to a device file, the request is handled by the kernel driver for that device. Each
device file has an associated number (Major number) which identifies the driver to use.
Character driver
• It is a device driver which interacts with character devices.
• Character devices are keyboard, mouse and camera etc.
• Sequential Byte oriented data transfer
• Character device is a device that can be accessed as a stream of bytes.
• Character device doesn’t require buffering and it sends the data with no preconfigured size. (Block devices have a buffer
and it send/receives the data in blocks of a size configured per device)
Run command “cat /proc/devices” on terminal. Output shows all character devices (not
including devices whose modules are not loaded) and their major number associated with it.
Character devices:
1 mem
4 /dev/vc/0
4 tty
4 ttyS
5 /dev/tty
5 /dev/console
5 /dev/ptmx
5 ttyprintk
6 lp
7 vcs
10 misc
13 input
21 sg
29 fb
108 ppp
128 ptm
136 pts
180 usb
189 usb_device
248 hidraw
249 hpilo
250 ptp
251 pps
252 bsg
253 watchdog
254 rtc
Major number
Character
device name
rtc254
Top view: User application interacts with character device
Note: This PPT covers
only on how user
application interaction
with character device
driver by a device file.
Writing character driver steps
1. Write a simple loadable module in C language (simple_char_module.c)
• Add module_init() and module_exit() functions
• Use register_chrdev() unregister_chrdev() functions to register our module as a character driver.
• Implement basic struct file_operations methods – check all file operation functions at /lib/modules/3.13.0-
119-generic/build/include/linux/fs.h
• open() read()
• close() write()
2. Write a Makefile with content: obj-m := simple_char_module.o
3. Compile module with command: make -C /lib/modules/$(uname -r)/build M=$PWD modules
• simple_char_module.ko file will be generated on successful compilation
4. Create a device file using mknod command: sudo mknod -m 666 /dev/simple_char_dev c 240 0
• Here, ‘-m’ denotes permissions, char ‘c’ tells character device, number 240 is Major number, 0 is Minor
number, simple_char_dev is device file name
• Check device file created or not using “ls -l /dev/simple_char_dev” command
5. Insert simple_char_module.ko module into linux machine using insmod command
• insmod simple_char_module.ko
• Check if module successfully inserted or not using command: cat /proc/devices
• Also, check lsmod command output
• Check linux system log: tail -f /var/log/syslog
• module_init() function will be called with insmod command. Use register_chrdev() function to register our
module with kernel subsystem as a character device driver.
Writing character driver steps (contd..)
6. Reading contents of device file: cat /dev/simple_char_dev; Below file system
operations are called in sequence.
• File open() operation will be called in simple_char_module.c
• File read() operation called to read contents from char device
• File release() operation called to close the device.
7. Writing contents to the device file:
echo “hello world” > /dev/simple_char_dev;
Below file system operations are called in sequence.
• File open() operation will be called in simple_char_module.c
• File write() operation called to write contents to char device
• File release() operation called to close the device.
8. Remove simple_char_module module from linux system: rmmod simple_char_module
• module_exit() function will be called.
• Use unregister_chrdev() function to un-register our module from kernel
subsystem.
• Check linux system log at tail -f /var/log/syslog
• Also, check lsmod output if the module successfully removed from system or
not.
Sample character driver code:
#include<linux/init.h>
#include<linux/module.h>
#include<linux/fs.h> /* For the character driver support */
int char_drv_open (struct inode *pinode, struct file *pfile) {
printk(KERN_INFO "Inside the %s functionn", __FUNCTION__); return 0;
}
ssize_t char_drv_read (struct file *pfile, char __user *buffer, size_t len, loff_t *offset){
printk(KERN_INFO "Inside the %s functionn", __FUNCTION__); return 0;
}
ssize_t char_drv_write (struct file *pfile, const char __user *buffer, size_t len, loff_t *offset){
printk(KERN_INFO "Inside the %s functionn", __FUNCTION__); return len;
}
int char_drv_release (struct inode *pinode, struct file *pfile){
printk(KERN_INFO "Inside the %s functionn", __FUNCTION__); return 0;
}
struct file_operations char_drv_fops = {/* To hold the file operations on this device */
.owner = THIS_MODULE,
.open = char_drv_open,
.read = char_drv_read,
.write = char_drv_write,
.release = char_drv_release,
};
int simple_char_drv_init(void)
{ printk(KERN_INFO "Inside the %s functionn", __FUNCTION__);
/* Register with kernel and indicate that we are registering a character device driver */
register_chrdev(240 /*Major Number*/, "Simple char drv" /* Name of the driver*/, &char_drv_fops);
return 0;
}
void simple_char_drv_exit(void)
{ printk(KERN_INFO "Inside the %s functionn", __FUNCTION__);
unregister_chrdev(240 /*Major Number*/, "Simple char drv" /* Name of the driver*/);
}
module_init(simple_char_drv_init);
module_exit(simple_char_drv_exit);
Step 0) Makefile: obj-m := filename.o
Step 1) Compilation: make -C /lib/modules/$(uname -r)/build M=$PWD
modules
Output of compilation command:
make: Entering directory `/usr/src/linux-headers-3.13.0-119-generic'
CC [M] /build/rrajk/character_device_driver_module/simple_char_drv.o
Building modules, stage 2.
MODPOST 1 modules
CC
/build/rrajk/character_device_driver_module/simple_char_drv.mod.o
LD [M]
/build/rrajk/character_device_driver_module/simple_char_drv.ko
make: Leaving directory `/usr/src/linux-headers-3.13.0-119-generic'
Step 2) Insert module:
$sudo insmod ./simple_char_drv.ko
Step 3) Check if it inserted properly or not with lsmod
$lsmod | grep simple
Step 4) We registered our driver as character device driver
using register_chrdev(). Check if it is successful or not
$cat /proc/devices
Output: 240 Simple char drv
Step 5) Create device file using mknod with major# 240
$sudo mknod -m 666 /dev/simple_char_dev c 240 0
Note: check “ls –la /dev/simple_char_dev” output
Step 6) perform read (ex: cat simple_char_dev), write (ex: echo
“data” > simple_char_dev) operations on above device file and
check the linux system log simultaneously using command “tail –f
/var/log/syslog” and observe how character driver functions get
invoked with read/write requests performed on device file by
user application.
References
• Linux file types: https://www.linuxnix.com/file-types-in-linux/
• Simple character driver implementation: Linux Device Drivers Training
06, Simple Character Driver by KarthikM@youtube.com
• Introduction to char device driver by Vandana_salve@slideshare.com
THANK YOU 
Have a look at
http://www.slideshare.net/rampalliraj/
http://practicepeople.blogspot.in/

More Related Content

What's hot

U boot porting guide for SoC
U boot porting guide for SoCU boot porting guide for SoC
U boot porting guide for SoC
Macpaul Lin
 

What's hot (20)

Understanding a kernel oops and a kernel panic
Understanding a kernel oops and a kernel panicUnderstanding a kernel oops and a kernel panic
Understanding a kernel oops and a kernel panic
 
Bootloaders (U-Boot)
Bootloaders (U-Boot) Bootloaders (U-Boot)
Bootloaders (U-Boot)
 
U boot porting guide for SoC
U boot porting guide for SoCU boot porting guide for SoC
U boot porting guide for SoC
 
Character drivers
Character driversCharacter drivers
Character drivers
 
Sisteme de Operare: Analiza executabilelor și proceselor
Sisteme de Operare: Analiza executabilelor și proceselorSisteme de Operare: Analiza executabilelor și proceselor
Sisteme de Operare: Analiza executabilelor și proceselor
 
Linux Device Tree
Linux Device TreeLinux Device Tree
Linux Device Tree
 
Linux Char Device Driver
Linux Char Device DriverLinux Char Device Driver
Linux Char Device Driver
 
Process Address Space: The way to create virtual address (page table) of user...
Process Address Space: The way to create virtual address (page table) of user...Process Address Space: The way to create virtual address (page table) of user...
Process Address Space: The way to create virtual address (page table) of user...
 
Linux Kernel MMC Storage driver Overview
Linux Kernel MMC Storage driver OverviewLinux Kernel MMC Storage driver Overview
Linux Kernel MMC Storage driver Overview
 
Linux boot process
Linux boot processLinux boot process
Linux boot process
 
Linux kernel debugging
Linux kernel debuggingLinux kernel debugging
Linux kernel debugging
 
3 computer memory
3   computer memory3   computer memory
3 computer memory
 
Memory model
Memory modelMemory model
Memory model
 
semaphore & mutex.pdf
semaphore & mutex.pdfsemaphore & mutex.pdf
semaphore & mutex.pdf
 
ELF(executable and linkable format)
ELF(executable and linkable format)ELF(executable and linkable format)
ELF(executable and linkable format)
 
I2c drivers
I2c driversI2c drivers
I2c drivers
 
Introduction to Modern U-Boot
Introduction to Modern U-BootIntroduction to Modern U-Boot
Introduction to Modern U-Boot
 
Memory Mapping Implementation (mmap) in Linux Kernel
Memory Mapping Implementation (mmap) in Linux KernelMemory Mapping Implementation (mmap) in Linux Kernel
Memory Mapping Implementation (mmap) in Linux Kernel
 
U Boot or Universal Bootloader
U Boot or Universal BootloaderU Boot or Universal Bootloader
U Boot or Universal Bootloader
 
Linux kernel modules
Linux kernel modulesLinux kernel modules
Linux kernel modules
 

Similar to Writing Character driver (loadable module) in linux

Linuxdd[1]
Linuxdd[1]Linuxdd[1]
Linuxdd[1]
mcganesh
 
Character_Device_drvier_pc
Character_Device_drvier_pcCharacter_Device_drvier_pc
Character_Device_drvier_pc
Rashila Rr
 
A character device typically transfers data to and from a user appli.pdf
A character device typically transfers data to and from a user appli.pdfA character device typically transfers data to and from a user appli.pdf
A character device typically transfers data to and from a user appli.pdf
aptind
 
A character device typically transfers data to and from a user appli.pdf
A character device typically transfers data to and from a user appli.pdfA character device typically transfers data to and from a user appli.pdf
A character device typically transfers data to and from a user appli.pdf
aptind
 

Similar to Writing Character driver (loadable module) in linux (20)

Linux filesystemhierarchy
Linux filesystemhierarchyLinux filesystemhierarchy
Linux filesystemhierarchy
 
Char Drivers And Debugging Techniques
Char Drivers And Debugging TechniquesChar Drivers And Debugging Techniques
Char Drivers And Debugging Techniques
 
Device drivers tsp
Device drivers tspDevice drivers tsp
Device drivers tsp
 
Linuxdd[1]
Linuxdd[1]Linuxdd[1]
Linuxdd[1]
 
Character_Device_drvier_pc
Character_Device_drvier_pcCharacter_Device_drvier_pc
Character_Device_drvier_pc
 
Device Drivers
Device DriversDevice Drivers
Device Drivers
 
17 Linux Basics #burningkeyboards
17 Linux Basics #burningkeyboards17 Linux Basics #burningkeyboards
17 Linux Basics #burningkeyboards
 
Linux Device Driver,LDD,
Linux Device Driver,LDD,Linux Device Driver,LDD,
Linux Device Driver,LDD,
 
A character device typically transfers data to and from a user appli.pdf
A character device typically transfers data to and from a user appli.pdfA character device typically transfers data to and from a user appli.pdf
A character device typically transfers data to and from a user appli.pdf
 
A character device typically transfers data to and from a user appli.pdf
A character device typically transfers data to and from a user appli.pdfA character device typically transfers data to and from a user appli.pdf
A character device typically transfers data to and from a user appli.pdf
 
Basic Linux Internals
Basic Linux InternalsBasic Linux Internals
Basic Linux Internals
 
Linux
LinuxLinux
Linux
 
Tutorial 2
Tutorial 2Tutorial 2
Tutorial 2
 
linux installation.pdf
linux installation.pdflinux installation.pdf
linux installation.pdf
 
brief intro to Linux device drivers
brief intro to Linux device driversbrief intro to Linux device drivers
brief intro to Linux device drivers
 
Docker.io
Docker.ioDocker.io
Docker.io
 
Linux kernel modules
Linux kernel modulesLinux kernel modules
Linux kernel modules
 
Memory forensics cheat sheet
Memory forensics cheat sheetMemory forensics cheat sheet
Memory forensics cheat sheet
 
Linux basics
Linux basics Linux basics
Linux basics
 
Ch1 linux basics
Ch1 linux basicsCh1 linux basics
Ch1 linux basics
 

More from RajKumar Rampelli

More from RajKumar Rampelli (13)

Introduction to Python - Running Notes
Introduction to Python - Running NotesIntroduction to Python - Running Notes
Introduction to Python - Running Notes
 
Learn python - for beginners - part-2
Learn python - for beginners - part-2Learn python - for beginners - part-2
Learn python - for beginners - part-2
 
Learn python – for beginners
Learn python – for beginnersLearn python – for beginners
Learn python – for beginners
 
Linux watchdog timer
Linux watchdog timerLinux watchdog timer
Linux watchdog timer
 
Linux Kernel I/O Schedulers
Linux Kernel I/O SchedulersLinux Kernel I/O Schedulers
Linux Kernel I/O Schedulers
 
Linux GIT commands
Linux GIT commandsLinux GIT commands
Linux GIT commands
 
System Booting Process overview
System Booting Process overviewSystem Booting Process overview
System Booting Process overview
 
Network security and cryptography
Network security and cryptographyNetwork security and cryptography
Network security and cryptography
 
Tasklet vs work queues (Deferrable functions in linux)
Tasklet vs work queues (Deferrable functions in linux)Tasklet vs work queues (Deferrable functions in linux)
Tasklet vs work queues (Deferrable functions in linux)
 
Sql injection attack
Sql injection attackSql injection attack
Sql injection attack
 
Turing awards seminar
Turing awards seminarTuring awards seminar
Turing awards seminar
 
Higher education importance
Higher education importanceHigher education importance
Higher education importance
 
C compilation process
C compilation processC compilation process
C compilation process
 

Recently uploaded

Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
KarakKing
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 

Recently uploaded (20)

Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 

Writing Character driver (loadable module) in linux

  • 1. Step by step approach to write a simple loadable “character driver” in linux - Raj Kumar Rampelli
  • 2. Outline • Introduction to kernel & device driver: Please refer to my previous PPT https://www.slideshare.net/rampalliraj/kernel-device-drivers • File types in Linux/Unix • Device files • Character driver • Steps to write a loadable character driver module in linux • Sample character device driver code • References
  • 3. File types in Linux/Unix File Type Definition How they are indicated? How to create? How to see in ls -l command? Regular File A readable file or a binary file or an image file or compressed file Indicated with “-” in ls -l command output Use touch or vi or vim command Ex: vim 1.txt -rw------- 1 rrajk hardware 154 Dec 4 21:15 myscript_dev_t194.sh Directory File Contains regular files, folders or special files stored on a physical device Indicated with “d” in ls -l command output Use mkdir command drwx------ 3 rrajk hardware 4096 Dec 3 23:48 obj Block device file Behave lot like ordinary files. They are an array of bytes, values can be read/write from/to given location. Indicated with “b” in ls -l command output. Most of them present in /dev directory Use fdisk command or create virtual partition brw-rw---- 1 root disk 8, 0 Oct 29 02:07 sda Character device file Provides a serial stream of input or output Indicated with “c” in ls -l command output. Most of them present in /dev directory Use mknod command crw-r--r-- 1 root root 1, 11 Oct 29 02:07 kmsg PIPE file Fist in First out property Indicated with “p” in ls -l command’s output Use mkfifo command prw-r----- 1 root root 0 2010-02-15 09:35 /dev/.initramfs/usplash_fifo Symbolic link file Linked files to other files. They are either directory or regular file. The inode for this file and it’s parent file is same. Indicated with “l” in ls -l command’s ouput Use ln command lrwxrwxrwx 1 root root 9 Jun 12 14:16 /var/lock -> /run/lock Socket files Used to pass information between applications for communication purpose Indicated with “s” in ls -l command’s ouput Use socket() system call srw-rw-rw- 1 root root 0 Oct 29 02:07 log
  • 4. Device file • Device file is a file that associates it’s name visible to the user space applications and it’s triplet (type, major, minor) to the kernel • Device file is a user interface to communicate with device driver (character and block driver) • A device file can represent character devices, which emit a stream data one character at a time, or block devices which allow random access to blocks of data. • In Linux, every device represented as a device file • All device files stored in the /dev directory and they are also called as device nodes. • Example: • for Character device file  /dev/input/mice : represents mouse, exposes the movement of mouse as a character stream. • for block device file  /dev/sda : represents hard disk, exposes the addressable regions of memory of the device. • Device files (or device nodes) are created by the mknod system call. • Kernel assigns user requests to the corresponding device driver using triplet information. • Type: character device (c) or block device (b) • Major: represents device category • Minor: identifier of the device. • When data is read or written to a device file, the request is handled by the kernel driver for that device. Each device file has an associated number (Major number) which identifies the driver to use.
  • 5. Character driver • It is a device driver which interacts with character devices. • Character devices are keyboard, mouse and camera etc. • Sequential Byte oriented data transfer • Character device is a device that can be accessed as a stream of bytes. • Character device doesn’t require buffering and it sends the data with no preconfigured size. (Block devices have a buffer and it send/receives the data in blocks of a size configured per device) Run command “cat /proc/devices” on terminal. Output shows all character devices (not including devices whose modules are not loaded) and their major number associated with it. Character devices: 1 mem 4 /dev/vc/0 4 tty 4 ttyS 5 /dev/tty 5 /dev/console 5 /dev/ptmx 5 ttyprintk 6 lp 7 vcs 10 misc 13 input 21 sg 29 fb 108 ppp 128 ptm 136 pts 180 usb 189 usb_device 248 hidraw 249 hpilo 250 ptp 251 pps 252 bsg 253 watchdog 254 rtc Major number Character device name rtc254
  • 6. Top view: User application interacts with character device Note: This PPT covers only on how user application interaction with character device driver by a device file.
  • 7. Writing character driver steps 1. Write a simple loadable module in C language (simple_char_module.c) • Add module_init() and module_exit() functions • Use register_chrdev() unregister_chrdev() functions to register our module as a character driver. • Implement basic struct file_operations methods – check all file operation functions at /lib/modules/3.13.0- 119-generic/build/include/linux/fs.h • open() read() • close() write() 2. Write a Makefile with content: obj-m := simple_char_module.o 3. Compile module with command: make -C /lib/modules/$(uname -r)/build M=$PWD modules • simple_char_module.ko file will be generated on successful compilation 4. Create a device file using mknod command: sudo mknod -m 666 /dev/simple_char_dev c 240 0 • Here, ‘-m’ denotes permissions, char ‘c’ tells character device, number 240 is Major number, 0 is Minor number, simple_char_dev is device file name • Check device file created or not using “ls -l /dev/simple_char_dev” command 5. Insert simple_char_module.ko module into linux machine using insmod command • insmod simple_char_module.ko • Check if module successfully inserted or not using command: cat /proc/devices • Also, check lsmod command output • Check linux system log: tail -f /var/log/syslog • module_init() function will be called with insmod command. Use register_chrdev() function to register our module with kernel subsystem as a character device driver.
  • 8. Writing character driver steps (contd..) 6. Reading contents of device file: cat /dev/simple_char_dev; Below file system operations are called in sequence. • File open() operation will be called in simple_char_module.c • File read() operation called to read contents from char device • File release() operation called to close the device. 7. Writing contents to the device file: echo “hello world” > /dev/simple_char_dev; Below file system operations are called in sequence. • File open() operation will be called in simple_char_module.c • File write() operation called to write contents to char device • File release() operation called to close the device. 8. Remove simple_char_module module from linux system: rmmod simple_char_module • module_exit() function will be called. • Use unregister_chrdev() function to un-register our module from kernel subsystem. • Check linux system log at tail -f /var/log/syslog • Also, check lsmod output if the module successfully removed from system or not.
  • 9. Sample character driver code: #include<linux/init.h> #include<linux/module.h> #include<linux/fs.h> /* For the character driver support */ int char_drv_open (struct inode *pinode, struct file *pfile) { printk(KERN_INFO "Inside the %s functionn", __FUNCTION__); return 0; } ssize_t char_drv_read (struct file *pfile, char __user *buffer, size_t len, loff_t *offset){ printk(KERN_INFO "Inside the %s functionn", __FUNCTION__); return 0; } ssize_t char_drv_write (struct file *pfile, const char __user *buffer, size_t len, loff_t *offset){ printk(KERN_INFO "Inside the %s functionn", __FUNCTION__); return len; } int char_drv_release (struct inode *pinode, struct file *pfile){ printk(KERN_INFO "Inside the %s functionn", __FUNCTION__); return 0; } struct file_operations char_drv_fops = {/* To hold the file operations on this device */ .owner = THIS_MODULE, .open = char_drv_open, .read = char_drv_read, .write = char_drv_write, .release = char_drv_release, }; int simple_char_drv_init(void) { printk(KERN_INFO "Inside the %s functionn", __FUNCTION__); /* Register with kernel and indicate that we are registering a character device driver */ register_chrdev(240 /*Major Number*/, "Simple char drv" /* Name of the driver*/, &char_drv_fops); return 0; } void simple_char_drv_exit(void) { printk(KERN_INFO "Inside the %s functionn", __FUNCTION__); unregister_chrdev(240 /*Major Number*/, "Simple char drv" /* Name of the driver*/); } module_init(simple_char_drv_init); module_exit(simple_char_drv_exit); Step 0) Makefile: obj-m := filename.o Step 1) Compilation: make -C /lib/modules/$(uname -r)/build M=$PWD modules Output of compilation command: make: Entering directory `/usr/src/linux-headers-3.13.0-119-generic' CC [M] /build/rrajk/character_device_driver_module/simple_char_drv.o Building modules, stage 2. MODPOST 1 modules CC /build/rrajk/character_device_driver_module/simple_char_drv.mod.o LD [M] /build/rrajk/character_device_driver_module/simple_char_drv.ko make: Leaving directory `/usr/src/linux-headers-3.13.0-119-generic' Step 2) Insert module: $sudo insmod ./simple_char_drv.ko Step 3) Check if it inserted properly or not with lsmod $lsmod | grep simple Step 4) We registered our driver as character device driver using register_chrdev(). Check if it is successful or not $cat /proc/devices Output: 240 Simple char drv Step 5) Create device file using mknod with major# 240 $sudo mknod -m 666 /dev/simple_char_dev c 240 0 Note: check “ls –la /dev/simple_char_dev” output Step 6) perform read (ex: cat simple_char_dev), write (ex: echo “data” > simple_char_dev) operations on above device file and check the linux system log simultaneously using command “tail –f /var/log/syslog” and observe how character driver functions get invoked with read/write requests performed on device file by user application.
  • 10. References • Linux file types: https://www.linuxnix.com/file-types-in-linux/ • Simple character driver implementation: Linux Device Drivers Training 06, Simple Character Driver by KarthikM@youtube.com • Introduction to char device driver by Vandana_salve@slideshare.com
  • 11. THANK YOU  Have a look at http://www.slideshare.net/rampalliraj/ http://practicepeople.blogspot.in/