SlideShare a Scribd company logo
5
Most read
11
Most read
14
Most read
@ 2021-21 Embitude Trainings <info@embitude.in>
All Rights Reserved
Character Driver
@ 2021-21 Embitude Trainings <info@embitude.in>
All Rights Reserved
What to Expect
●
What is Character driver
●
Major & Minor Number
●
Registering the character driver
●
Exchanging data with user space
●
Udev & netlink socket
●
Dynamic device file creation
●
IOCTL
@ 2021-21 Embitude Trainings <info@embitude.in>
All Rights Reserved
What is character driver?
●
The drivers which allow byte oriented
transaction.
●
Character device special files
– ls -l /dev | grep ^c
●
Two attributes for files
– Name (For application space)
– Number (For Kernel specific)
@ 2021-21 Embitude Trainings <info@embitude.in>
All Rights Reserved
Device file number
●
Represented by type dev_t. Is split it into 2
parts
– MSB 12 bits define the category of driver
– LSB 20 bits defines the functionality
●
Macros
– MAJOR(dev)
– MINOR(dev)
– MKDEV(major, minor)
@ 2021-21 Embitude Trainings <info@embitude.in>
All Rights Reserved
Character Driver Flow
VFS
/dev/abc
Applicaton
Driver
HW
Kernel
Space
User
Space
Hardware
Space
open()
open()
@ 2021-21 Embitude Trainings <info@embitude.in>
All Rights Reserved
Registering/Deregistering the driver
●
Registration
– register_char_dev_region(dev_t dev, unsigned int
count, char *name)
●
register_char_dev_region(MKDEV(250, 0), 3, “abc”);
– alloc_char_dev_region(dev_t *dev, unsigned int
first, unsigned int count, char *name)
●
alloc_char_dev_region(&dev, 0, 3, “abc);
●
Deregistration
– unregister_char_dev_region(dev_t dev, unsinged int
count);
@ 2021-21 Embitude Trainings <info@embitude.in>
All Rights Reserved
File operations
●
Callback handlers to be invoked by VFS
●
struct file_operations
– struct module_owner
– int (*open)(struct inode *, struct file *)
– int (*release)(struct inode , struct file *)
– int ssize_t (*read)(struct file *, char __user *, size_t, loff_t *);
– ssize_t (*write)(struct file *, const char __user *, size_t, loff_t
*);
– loff_t (*llseek)(struct file *, loff_t, int);
– int (*unlocked_ioctl)(struct file *, unsigned int, unsigned
long);
@ 2021-21 Embitude Trainings <info@embitude.in>
All Rights Reserved
●
Declare & initialize the struct file_operations
– struct file_operations fops
●
Declare & initialize the variable of type struct cdev
– struct cdev c_dev
– cdev_init(&c_dev, &fops)
●
The registration
– int cdev_add(struct dev *cdev, dev_t num, unsigned int
cound)
●
The Unregistration
– Int cdev_del(struct cdev *cdev)
(Un)Registering the file operations
@ 2021-21 Embitude Trainings <info@embitude.in>
All Rights Reserved
Read & write callbacks
●
ssize_t my_read(struct file *f, char __user *buf, size_t cnt, loff_t *off)
●
{
●
...
●
return read_cnt;
●
}
●
ssize_t my_write(struct file *f, char __user *buf, size_t cnt, loff_t *off)
●
{
●
...
●
return wrote_cnt;
●
}
@ 2021-21 Embitude Trainings <info@embitude.in>
All Rights Reserved
Exchanging the Data with User space
●
A single value
– get_user(v, p)
●
The kernel variable v gets the value pointed by user space pointer p
– put_user(v, p)
●
The value pointed by p is set to the contents of kernel variable v
●
A buffer
– unsigned long copy_to_user(void __user *to, const void
*from, unsigned long n)
– Unsigned logn copy_from_user(void *to, const void __user
*from, unsigned long n)
●
Returns 0 on success, non-zero on failure
@ 2021-21 Embitude Trainings <info@embitude.in>
All Rights Reserved
udev
●
Udev (user space dev) – a device manager
●
Runs in user space
●
Automatically creates/removes device enteries
in dev according to inserted/removed drivers
●
Listens on the netlink socket for uvents
●
Kernel shares the Major/Minor numbers through
/sys interface
@ 2021-21 Embitude Trainings <info@embitude.in>
All Rights Reserved
udev files
●
Udev configuration file
– /etc/udev/udev.conf
●
Standard udev event matching rules
– /lib/udev/rules.d
●
Custom udev event matching rules
– /etc/udev/rules.d/*.rules
●
dev/*
– Device files creation
@ 2021-21 Embitude Trainings <info@embitude.in>
All Rights Reserved
Udev Operation
Kernel driver core
(usb, pci etc)
udevd
uevent
Udev event process
Matches event to rules
Creates/removes
device files
@ 2021-21 Embitude Trainings <info@embitude.in>
All Rights Reserved
Dynamic device file creation
●
Class create & destroy
– struct class *class_create(struct module *owner, char
*name);
– void class_destroy(struct class *cl);
●
Device create & destroy
– struct class_device *device_create(struct class *cl, NULL,
dev_t devnum, NULL, const char *fmt, ...);
– void device_destroy(struct class *cl, dev_t devnum);
@ 2021-21 Embitude Trainings <info@embitude.in>
All Rights Reserved
IOCTL
●
All the miscellenious IO operations
●
long unlocked_ioctl(struct file *f, unsigned int cmd,
unsigned long arg)
– Associated to the ioctl system call
– Extends the driver capabilities beyond the limited read/
write API
●
Changing the speed of the serial port, setting video format,
quering the device serial number
– cmd is the number identifying the operation to perform
– arg is the argument passed to the command. Can be an
integer, an address
@ 2021-21 Embitude Trainings <info@embitude.in>
All Rights Reserved
IOCTL ...
●
Command is split as follows
●
Macros
●
_IO, _IOW, _IOR, _IOWR
●
Parameters
●
type (character) [15:8]
●
number (index) [7:0]
●
size (param type) [29:16]
size[29:16] type[15:8] num[7:0]
dir[31:30]
@ 2021-21 Embitude Trainings <info@embitude.in>
All Rights Reserved
IOCTL
●
All the miscellenious IO operations
●
long unlocked_ioctl(struct file *f, unsigned int cmd,
unsigned long arg)
– Associated to the ioctl system call
– Extends the driver capabilities beyond the limited read/
write API
●
Changing the speed of the serial port, setting video format,
quering the device serial number
– cmd is the number identifying the operation to perform
– arg is the argument passed to the command. Can be an
integer, an address
@ 2021-21 Embitude Trainings <info@embitude.in>
All Rights Reserved
What all did we learn?
●
What is Character driver
●
Major & Minor Number
●
Registering the character driver
●
Exchanging data with user space
●
Udev & netlink socket
●
Dynamic device file creation
●
IOCTL

More Related Content

What's hot (20)

PPTX
Introduction Linux Device Drivers
NEEVEE Technologies
 
PPTX
Linux device drivers
Abhishek Sagar
 
PDF
USB Drivers
Anil Kumar Pugalia
 
PDF
Introduction to Modern U-Boot
GlobalLogic Ukraine
 
PDF
I2C Subsystem In Linux-2.6.24
Varun Mahajan
 
PDF
Spi drivers
pradeep_tewani
 
PDF
Linux Internals - Part II
Emertxe Information Technologies Pvt Ltd
 
PDF
Embedded Linux BSP Training (Intro)
RuggedBoardGroup
 
PDF
Qemu JIT Code Generator and System Emulation
National Cheng Kung University
 
PDF
Embedded Linux Kernel - Build your custom kernel
Emertxe Information Technologies Pvt Ltd
 
PDF
Process Address Space: The way to create virtual address (page table) of user...
Adrian Huang
 
PDF
Linux Internals - Part I
Emertxe Information Technologies Pvt Ltd
 
PPT
U Boot or Universal Bootloader
Satpal Parmar
 
PDF
BusyBox for Embedded Linux
Emertxe Information Technologies Pvt Ltd
 
PPTX
U-Boot Porting on New Hardware
RuggedBoardGroup
 
PDF
Linux dma engine
pradeep_tewani
 
PPTX
Linux Initialization Process (2)
shimosawa
 
PDF
Part 02 Linux Kernel Module Programming
Tushar B Kute
 
PDF
Kernel Recipes 2019 - ftrace: Where modifying a running kernel all started
Anne Nicolas
 
Introduction Linux Device Drivers
NEEVEE Technologies
 
Linux device drivers
Abhishek Sagar
 
USB Drivers
Anil Kumar Pugalia
 
Introduction to Modern U-Boot
GlobalLogic Ukraine
 
I2C Subsystem In Linux-2.6.24
Varun Mahajan
 
Spi drivers
pradeep_tewani
 
Linux Internals - Part II
Emertxe Information Technologies Pvt Ltd
 
Embedded Linux BSP Training (Intro)
RuggedBoardGroup
 
Qemu JIT Code Generator and System Emulation
National Cheng Kung University
 
Embedded Linux Kernel - Build your custom kernel
Emertxe Information Technologies Pvt Ltd
 
Process Address Space: The way to create virtual address (page table) of user...
Adrian Huang
 
U Boot or Universal Bootloader
Satpal Parmar
 
BusyBox for Embedded Linux
Emertxe Information Technologies Pvt Ltd
 
U-Boot Porting on New Hardware
RuggedBoardGroup
 
Linux dma engine
pradeep_tewani
 
Linux Initialization Process (2)
shimosawa
 
Part 02 Linux Kernel Module Programming
Tushar B Kute
 
Kernel Recipes 2019 - ftrace: Where modifying a running kernel all started
Anne Nicolas
 

Similar to Character drivers (20)

PDF
Linux Char Device Driver
Gary Yeh
 
PPTX
Char Drivers And Debugging Techniques
YourHelper1
 
PDF
Embedded Android : System Development - Part II (Linux device drivers)
Emertxe Information Technologies Pvt Ltd
 
PDF
Character Drivers
Anil Kumar Pugalia
 
PPTX
Writing Character driver (loadable module) in linux
RajKumar Rampelli
 
PPTX
Device_drivers_copy_to_user_copy_from_user.pptx
siddu85
 
PDF
Character_Device_drvier_pc
Rashila Rr
 
PPT
Linuxdd[1]
mcganesh
 
PPT
“black boxes” that make a particular p..
JcRaajab1
 
PPT
Linux Device Driver,LDD,
Rahul Batra
 
PPT
Linux Device Driver for Writing a real world driver for embedded Linux
AchyuthShettigar2
 
PPT
Basic Linux Internals
mukul bhardwaj
 
PDF
brief intro to Linux device drivers
Alexandre Moreno
 
PPT
Embedded system - embedded system programming
Vibrant Technologies & Computers
 
PDF
A character device typically transfers data to and from a user appli.pdf
aptind
 
PPT
device drives in electronics and communication
gawthamanna
 
PDF
Kernel Recipes 2016 -
Anne Nicolas
 
PPT
LINUX Device Drivers
Partha Bhattacharya
 
PPT
DeviceDriverNov18.ppt
TerrenceRamirez1
 
PPTX
Introduction to Kernel and Device Drivers
RajKumar Rampelli
 
Linux Char Device Driver
Gary Yeh
 
Char Drivers And Debugging Techniques
YourHelper1
 
Embedded Android : System Development - Part II (Linux device drivers)
Emertxe Information Technologies Pvt Ltd
 
Character Drivers
Anil Kumar Pugalia
 
Writing Character driver (loadable module) in linux
RajKumar Rampelli
 
Device_drivers_copy_to_user_copy_from_user.pptx
siddu85
 
Character_Device_drvier_pc
Rashila Rr
 
Linuxdd[1]
mcganesh
 
“black boxes” that make a particular p..
JcRaajab1
 
Linux Device Driver,LDD,
Rahul Batra
 
Linux Device Driver for Writing a real world driver for embedded Linux
AchyuthShettigar2
 
Basic Linux Internals
mukul bhardwaj
 
brief intro to Linux device drivers
Alexandre Moreno
 
Embedded system - embedded system programming
Vibrant Technologies & Computers
 
A character device typically transfers data to and from a user appli.pdf
aptind
 
device drives in electronics and communication
gawthamanna
 
Kernel Recipes 2016 -
Anne Nicolas
 
LINUX Device Drivers
Partha Bhattacharya
 
DeviceDriverNov18.ppt
TerrenceRamirez1
 
Introduction to Kernel and Device Drivers
RajKumar Rampelli
 
Ad

Recently uploaded (20)

PDF
SAP GUI Installation Guide for Windows | Step-by-Step Setup for SAP Access
SAP Vista, an A L T Z E N Company
 
PPTX
Farrell__10e_ch04_PowerPoint.pptx Programming Logic and Design slides
bashnahara11
 
PDF
How Agentic AI Networks are Revolutionizing Collaborative AI Ecosystems in 2025
ronakdubey419
 
PPTX
Presentation about Database and Database Administrator
abhishekchauhan86963
 
PDF
Why Are More Businesses Choosing Partners Over Freelancers for Salesforce.pdf
Cymetrix Software
 
PPT
Activate_Methodology_Summary presentatio
annapureddyn
 
PDF
AWS_Agentic_AI_in_Indian_BFSI_A_Strategic_Blueprint_for_Customer.pdf
siddharthnetsavvies
 
PDF
Enhancing Healthcare RPM Platforms with Contextual AI Integration
Cadabra Studio
 
PDF
On Software Engineers' Productivity - Beyond Misleading Metrics
Romén Rodríguez-Gil
 
PPTX
Role Of Python In Programing Language.pptx
jaykoshti048
 
PDF
ChatPharo: an Open Architecture for Understanding How to Talk Live to LLMs
ESUG
 
PDF
Infrastructure planning and resilience - Keith Hastings.pptx.pdf
Safe Software
 
PDF
10 posting ideas for community engagement with AI prompts
Pankaj Taneja
 
PDF
Applitools Platform Pulse: What's New and What's Coming - July 2025
Applitools
 
PDF
SAP GUI Installation Guide for macOS (iOS) | Connect to SAP Systems on Mac
SAP Vista, an A L T Z E N Company
 
PDF
System Center 2025 vs. 2022; What’s new, what’s next_PDF.pdf
Q-Advise
 
PDF
Virtual Threads in Java: A New Dimension of Scalability and Performance
Tier1 app
 
PPTX
classification of computer and basic part of digital computer
ravisinghrajpurohit3
 
PPTX
Employee salary prediction using Machine learning Project template.ppt
bhanuk27082004
 
PDF
New Download MiniTool Partition Wizard Crack Latest Version 2025
imang66g
 
SAP GUI Installation Guide for Windows | Step-by-Step Setup for SAP Access
SAP Vista, an A L T Z E N Company
 
Farrell__10e_ch04_PowerPoint.pptx Programming Logic and Design slides
bashnahara11
 
How Agentic AI Networks are Revolutionizing Collaborative AI Ecosystems in 2025
ronakdubey419
 
Presentation about Database and Database Administrator
abhishekchauhan86963
 
Why Are More Businesses Choosing Partners Over Freelancers for Salesforce.pdf
Cymetrix Software
 
Activate_Methodology_Summary presentatio
annapureddyn
 
AWS_Agentic_AI_in_Indian_BFSI_A_Strategic_Blueprint_for_Customer.pdf
siddharthnetsavvies
 
Enhancing Healthcare RPM Platforms with Contextual AI Integration
Cadabra Studio
 
On Software Engineers' Productivity - Beyond Misleading Metrics
Romén Rodríguez-Gil
 
Role Of Python In Programing Language.pptx
jaykoshti048
 
ChatPharo: an Open Architecture for Understanding How to Talk Live to LLMs
ESUG
 
Infrastructure planning and resilience - Keith Hastings.pptx.pdf
Safe Software
 
10 posting ideas for community engagement with AI prompts
Pankaj Taneja
 
Applitools Platform Pulse: What's New and What's Coming - July 2025
Applitools
 
SAP GUI Installation Guide for macOS (iOS) | Connect to SAP Systems on Mac
SAP Vista, an A L T Z E N Company
 
System Center 2025 vs. 2022; What’s new, what’s next_PDF.pdf
Q-Advise
 
Virtual Threads in Java: A New Dimension of Scalability and Performance
Tier1 app
 
classification of computer and basic part of digital computer
ravisinghrajpurohit3
 
Employee salary prediction using Machine learning Project template.ppt
bhanuk27082004
 
New Download MiniTool Partition Wizard Crack Latest Version 2025
imang66g
 
Ad

Character drivers

  • 1. @ 2021-21 Embitude Trainings <[email protected]> All Rights Reserved Character Driver
  • 2. @ 2021-21 Embitude Trainings <[email protected]> All Rights Reserved What to Expect ● What is Character driver ● Major & Minor Number ● Registering the character driver ● Exchanging data with user space ● Udev & netlink socket ● Dynamic device file creation ● IOCTL
  • 3. @ 2021-21 Embitude Trainings <[email protected]> All Rights Reserved What is character driver? ● The drivers which allow byte oriented transaction. ● Character device special files – ls -l /dev | grep ^c ● Two attributes for files – Name (For application space) – Number (For Kernel specific)
  • 4. @ 2021-21 Embitude Trainings <[email protected]> All Rights Reserved Device file number ● Represented by type dev_t. Is split it into 2 parts – MSB 12 bits define the category of driver – LSB 20 bits defines the functionality ● Macros – MAJOR(dev) – MINOR(dev) – MKDEV(major, minor)
  • 5. @ 2021-21 Embitude Trainings <[email protected]> All Rights Reserved Character Driver Flow VFS /dev/abc Applicaton Driver HW Kernel Space User Space Hardware Space open() open()
  • 6. @ 2021-21 Embitude Trainings <[email protected]> All Rights Reserved Registering/Deregistering the driver ● Registration – register_char_dev_region(dev_t dev, unsigned int count, char *name) ● register_char_dev_region(MKDEV(250, 0), 3, “abc”); – alloc_char_dev_region(dev_t *dev, unsigned int first, unsigned int count, char *name) ● alloc_char_dev_region(&dev, 0, 3, “abc); ● Deregistration – unregister_char_dev_region(dev_t dev, unsinged int count);
  • 7. @ 2021-21 Embitude Trainings <[email protected]> All Rights Reserved File operations ● Callback handlers to be invoked by VFS ● struct file_operations – struct module_owner – int (*open)(struct inode *, struct file *) – int (*release)(struct inode , struct file *) – int ssize_t (*read)(struct file *, char __user *, size_t, loff_t *); – ssize_t (*write)(struct file *, const char __user *, size_t, loff_t *); – loff_t (*llseek)(struct file *, loff_t, int); – int (*unlocked_ioctl)(struct file *, unsigned int, unsigned long);
  • 8. @ 2021-21 Embitude Trainings <[email protected]> All Rights Reserved ● Declare & initialize the struct file_operations – struct file_operations fops ● Declare & initialize the variable of type struct cdev – struct cdev c_dev – cdev_init(&c_dev, &fops) ● The registration – int cdev_add(struct dev *cdev, dev_t num, unsigned int cound) ● The Unregistration – Int cdev_del(struct cdev *cdev) (Un)Registering the file operations
  • 9. @ 2021-21 Embitude Trainings <[email protected]> All Rights Reserved Read & write callbacks ● ssize_t my_read(struct file *f, char __user *buf, size_t cnt, loff_t *off) ● { ● ... ● return read_cnt; ● } ● ssize_t my_write(struct file *f, char __user *buf, size_t cnt, loff_t *off) ● { ● ... ● return wrote_cnt; ● }
  • 10. @ 2021-21 Embitude Trainings <[email protected]> All Rights Reserved Exchanging the Data with User space ● A single value – get_user(v, p) ● The kernel variable v gets the value pointed by user space pointer p – put_user(v, p) ● The value pointed by p is set to the contents of kernel variable v ● A buffer – unsigned long copy_to_user(void __user *to, const void *from, unsigned long n) – Unsigned logn copy_from_user(void *to, const void __user *from, unsigned long n) ● Returns 0 on success, non-zero on failure
  • 11. @ 2021-21 Embitude Trainings <[email protected]> All Rights Reserved udev ● Udev (user space dev) – a device manager ● Runs in user space ● Automatically creates/removes device enteries in dev according to inserted/removed drivers ● Listens on the netlink socket for uvents ● Kernel shares the Major/Minor numbers through /sys interface
  • 12. @ 2021-21 Embitude Trainings <[email protected]> All Rights Reserved udev files ● Udev configuration file – /etc/udev/udev.conf ● Standard udev event matching rules – /lib/udev/rules.d ● Custom udev event matching rules – /etc/udev/rules.d/*.rules ● dev/* – Device files creation
  • 13. @ 2021-21 Embitude Trainings <[email protected]> All Rights Reserved Udev Operation Kernel driver core (usb, pci etc) udevd uevent Udev event process Matches event to rules Creates/removes device files
  • 14. @ 2021-21 Embitude Trainings <[email protected]> All Rights Reserved Dynamic device file creation ● Class create & destroy – struct class *class_create(struct module *owner, char *name); – void class_destroy(struct class *cl); ● Device create & destroy – struct class_device *device_create(struct class *cl, NULL, dev_t devnum, NULL, const char *fmt, ...); – void device_destroy(struct class *cl, dev_t devnum);
  • 15. @ 2021-21 Embitude Trainings <[email protected]> All Rights Reserved IOCTL ● All the miscellenious IO operations ● long unlocked_ioctl(struct file *f, unsigned int cmd, unsigned long arg) – Associated to the ioctl system call – Extends the driver capabilities beyond the limited read/ write API ● Changing the speed of the serial port, setting video format, quering the device serial number – cmd is the number identifying the operation to perform – arg is the argument passed to the command. Can be an integer, an address
  • 16. @ 2021-21 Embitude Trainings <[email protected]> All Rights Reserved IOCTL ... ● Command is split as follows ● Macros ● _IO, _IOW, _IOR, _IOWR ● Parameters ● type (character) [15:8] ● number (index) [7:0] ● size (param type) [29:16] size[29:16] type[15:8] num[7:0] dir[31:30]
  • 17. @ 2021-21 Embitude Trainings <[email protected]> All Rights Reserved IOCTL ● All the miscellenious IO operations ● long unlocked_ioctl(struct file *f, unsigned int cmd, unsigned long arg) – Associated to the ioctl system call – Extends the driver capabilities beyond the limited read/ write API ● Changing the speed of the serial port, setting video format, quering the device serial number – cmd is the number identifying the operation to perform – arg is the argument passed to the command. Can be an integer, an address
  • 18. @ 2021-21 Embitude Trainings <[email protected]> All Rights Reserved What all did we learn? ● What is Character driver ● Major & Minor Number ● Registering the character driver ● Exchanging data with user space ● Udev & netlink socket ● Dynamic device file creation ● IOCTL