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

Linux Char Device Driver
Linux Char Device DriverLinux Char Device Driver
Linux Char Device DriverGary Yeh
 
linux device driver
linux device driverlinux device driver
linux device driverRahul Batra
 
Embedded_Linux_Booting
Embedded_Linux_BootingEmbedded_Linux_Booting
Embedded_Linux_BootingRashila Rr
 
U boot porting guide for SoC
U boot porting guide for SoCU boot porting guide for SoC
U boot porting guide for SoCMacpaul Lin
 
Bootloaders (U-Boot)
Bootloaders (U-Boot) Bootloaders (U-Boot)
Bootloaders (U-Boot) Omkar Rane
 
Basics of Operating System
Basics of Operating SystemBasics of Operating System
Basics of Operating SystemSoumit Ghosh
 
Bootstrap process of u boot (NDS32 RISC CPU)
Bootstrap process of u boot (NDS32 RISC CPU)Bootstrap process of u boot (NDS32 RISC CPU)
Bootstrap process of u boot (NDS32 RISC CPU)Macpaul Lin
 
Kernel module programming
Kernel module programmingKernel module programming
Kernel module programmingVandana Salve
 
U-Boot presentation 2013
U-Boot presentation  2013U-Boot presentation  2013
U-Boot presentation 2013Wave Digitech
 
Q4.11: Introduction to eMMC
Q4.11: Introduction to eMMCQ4.11: Introduction to eMMC
Q4.11: Introduction to eMMCLinaro
 
Kernel Module Programming
Kernel Module ProgrammingKernel Module Programming
Kernel Module ProgrammingSaurabh Bangad
 
Linux commands and file structure
Linux commands and file structureLinux commands and file structure
Linux commands and file structureSreenatha Reddy K R
 
Linux basics part 1
Linux basics part 1Linux basics part 1
Linux basics part 1Lilesh Pathe
 
Root file system for embedded systems
Root file system for embedded systemsRoot file system for embedded systems
Root file system for embedded systemsalok pal
 

What's hot (20)

Linux Char Device Driver
Linux Char Device DriverLinux Char Device Driver
Linux Char Device Driver
 
linux device driver
linux device driverlinux device driver
linux device driver
 
Embedded_Linux_Booting
Embedded_Linux_BootingEmbedded_Linux_Booting
Embedded_Linux_Booting
 
U boot porting guide for SoC
U boot porting guide for SoCU boot porting guide for SoC
U boot porting guide for SoC
 
Shell scripting
Shell scriptingShell scripting
Shell scripting
 
Bootloaders (U-Boot)
Bootloaders (U-Boot) Bootloaders (U-Boot)
Bootloaders (U-Boot)
 
Basics of Operating System
Basics of Operating SystemBasics of Operating System
Basics of Operating System
 
Bootstrap process of u boot (NDS32 RISC CPU)
Bootstrap process of u boot (NDS32 RISC CPU)Bootstrap process of u boot (NDS32 RISC CPU)
Bootstrap process of u boot (NDS32 RISC CPU)
 
Kernel module programming
Kernel module programmingKernel module programming
Kernel module programming
 
Shell Scripting
Shell ScriptingShell Scripting
Shell Scripting
 
U-Boot presentation 2013
U-Boot presentation  2013U-Boot presentation  2013
U-Boot presentation 2013
 
Q4.11: Introduction to eMMC
Q4.11: Introduction to eMMCQ4.11: Introduction to eMMC
Q4.11: Introduction to eMMC
 
Embedded linux network device driver development
Embedded linux network device driver developmentEmbedded linux network device driver development
Embedded linux network device driver development
 
Shell programming
Shell programmingShell programming
Shell programming
 
Kernel Module Programming
Kernel Module ProgrammingKernel Module Programming
Kernel Module Programming
 
Linux commands and file structure
Linux commands and file structureLinux commands and file structure
Linux commands and file structure
 
Linux basics part 1
Linux basics part 1Linux basics part 1
Linux basics part 1
 
Filepermissions in linux
Filepermissions in linuxFilepermissions in linux
Filepermissions in linux
 
Linux DMA Engine
Linux DMA EngineLinux DMA Engine
Linux DMA Engine
 
Root file system for embedded systems
Root file system for embedded systemsRoot file system for embedded systems
Root file system for embedded systems
 

Similar to Writing Character driver (loadable module) in linux

Char Drivers And Debugging Techniques
Char Drivers And Debugging TechniquesChar Drivers And Debugging Techniques
Char Drivers And Debugging TechniquesYourHelper1
 
Linuxdd[1]
Linuxdd[1]Linuxdd[1]
Linuxdd[1]mcganesh
 
Character_Device_drvier_pc
Character_Device_drvier_pcCharacter_Device_drvier_pc
Character_Device_drvier_pcRashila Rr
 
17 Linux Basics #burningkeyboards
17 Linux Basics #burningkeyboards17 Linux Basics #burningkeyboards
17 Linux Basics #burningkeyboardsDenis Ristic
 
Linux Device Driver,LDD,
Linux Device Driver,LDD,Linux Device Driver,LDD,
Linux Device Driver,LDD,Rahul Batra
 
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.pdfaptind
 
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.pdfaptind
 
brief intro to Linux device drivers
brief intro to Linux device driversbrief intro to Linux device drivers
brief intro to Linux device driversAlexandre Moreno
 
Memory forensics cheat sheet
Memory forensics cheat sheetMemory forensics cheat sheet
Memory forensics cheat sheetMartin Cabrera
 
Introduction to Operating Systems.pptx
Introduction to Operating Systems.pptxIntroduction to Operating Systems.pptx
Introduction to Operating Systems.pptxMohamedSaied877003
 

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
 
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
 
Introduction to Operating Systems.pptx
Introduction to Operating Systems.pptxIntroduction to Operating Systems.pptx
Introduction to Operating Systems.pptx
 

More from RajKumar Rampelli

Introduction to Python - Running Notes
Introduction to Python - Running NotesIntroduction to Python - Running Notes
Introduction to Python - Running NotesRajKumar Rampelli
 
Linux Kernel MMC Storage driver Overview
Linux Kernel MMC Storage driver OverviewLinux Kernel MMC Storage driver Overview
Linux Kernel MMC Storage driver OverviewRajKumar Rampelli
 
Learn python - for beginners - part-2
Learn python - for beginners - part-2Learn python - for beginners - part-2
Learn python - for beginners - part-2RajKumar Rampelli
 
Learn python – for beginners
Learn python – for beginnersLearn python – for beginners
Learn python – for beginnersRajKumar Rampelli
 
System Booting Process overview
System Booting Process overviewSystem Booting Process overview
System Booting Process overviewRajKumar Rampelli
 
Network security and cryptography
Network security and cryptographyNetwork security and cryptography
Network security and cryptographyRajKumar Rampelli
 
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)RajKumar Rampelli
 

More from RajKumar Rampelli (14)

Introduction to Python - Running Notes
Introduction to Python - Running NotesIntroduction to Python - Running Notes
Introduction to Python - Running Notes
 
Linux Kernel MMC Storage driver Overview
Linux Kernel MMC Storage driver OverviewLinux Kernel MMC Storage driver Overview
Linux Kernel MMC Storage driver Overview
 
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

ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.arsicmarija21
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...Nguyen Thanh Tu Collection
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptxSherlyMaeNeri
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Romantic Opera MUSIC FOR GRADE NINE pptx
Romantic Opera MUSIC FOR GRADE NINE pptxRomantic Opera MUSIC FOR GRADE NINE pptx
Romantic Opera MUSIC FOR GRADE NINE pptxsqpmdrvczh
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxAnupkumar Sharma
 
Quarter 4 Peace-education.pptx Catch Up Friday
Quarter 4 Peace-education.pptx Catch Up FridayQuarter 4 Peace-education.pptx Catch Up Friday
Quarter 4 Peace-education.pptx Catch Up FridayMakMakNepo
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxDr.Ibrahim Hassaan
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementmkooblal
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfUjwalaBharambe
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomnelietumpap1
 
ROOT CAUSE ANALYSIS PowerPoint Presentation
ROOT CAUSE ANALYSIS PowerPoint PresentationROOT CAUSE ANALYSIS PowerPoint Presentation
ROOT CAUSE ANALYSIS PowerPoint PresentationAadityaSharma884161
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 

Recently uploaded (20)

OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptx
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Romantic Opera MUSIC FOR GRADE NINE pptx
Romantic Opera MUSIC FOR GRADE NINE pptxRomantic Opera MUSIC FOR GRADE NINE pptx
Romantic Opera MUSIC FOR GRADE NINE pptx
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
 
Quarter 4 Peace-education.pptx Catch Up Friday
Quarter 4 Peace-education.pptx Catch Up FridayQuarter 4 Peace-education.pptx Catch Up Friday
Quarter 4 Peace-education.pptx Catch Up Friday
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptx
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of management
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choom
 
ROOT CAUSE ANALYSIS PowerPoint Presentation
ROOT CAUSE ANALYSIS PowerPoint PresentationROOT CAUSE ANALYSIS PowerPoint Presentation
ROOT CAUSE ANALYSIS PowerPoint Presentation
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 

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/