SlideShare a Scribd company logo
4
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

PDF
Spi drivers
PDF
I2C Subsystem In Linux-2.6.24
PDF
Arm device tree and linux device drivers
PPTX
Linux Kernel Booting Process (1) - For NLKB
PPTX
Linux Kernel MMC Storage driver Overview
PDF
Fun with Network Interfaces
PDF
I2c drivers
PPTX
U-Boot presentation 2013
Spi drivers
I2C Subsystem In Linux-2.6.24
Arm device tree and linux device drivers
Linux Kernel Booting Process (1) - For NLKB
Linux Kernel MMC Storage driver Overview
Fun with Network Interfaces
I2c drivers
U-Boot presentation 2013

What's hot (20)

PDF
Vmlinux: anatomy of bzimage and how x86 64 processor is booted
PDF
Embedded linux network device driver development
PDF
Memory Management with Page Folios
PDF
Process Address Space: The way to create virtual address (page table) of user...
PPTX
Linux device drivers
PPTX
Introduction Linux Device Drivers
PPTX
Linux Initialization Process (1)
PDF
Physical Memory Models.pdf
PPTX
Linux Device Tree
PPTX
Slab Allocator in Linux Kernel
PDF
spinlock.pdf
PDF
Kdump and the kernel crash dump analysis
PDF
kdump: usage and_internals
PDF
Memory Mapping Implementation (mmap) in Linux Kernel
PDF
Reverse Mapping (rmap) in Linux Kernel
PDF
Hands-on ethernet driver
PDF
semaphore & mutex.pdf
PPTX
Linux Network Stack
Vmlinux: anatomy of bzimage and how x86 64 processor is booted
Embedded linux network device driver development
Memory Management with Page Folios
Process Address Space: The way to create virtual address (page table) of user...
Linux device drivers
Introduction Linux Device Drivers
Linux Initialization Process (1)
Physical Memory Models.pdf
Linux Device Tree
Slab Allocator in Linux Kernel
spinlock.pdf
Kdump and the kernel crash dump analysis
kdump: usage and_internals
Memory Mapping Implementation (mmap) in Linux Kernel
Reverse Mapping (rmap) in Linux Kernel
Hands-on ethernet driver
semaphore & mutex.pdf
Linux Network Stack
Ad

Similar to Character drivers (20)

PDF
Character Drivers
PDF
Embedded Android : System Development - Part II (Linux device drivers)
PDF
Hunting and Exploiting Bugs in Kernel Drivers - DefCamp 2012
PPT
Unix.system.calls
PPT
Optimizing Direct X On Multi Core Architectures
PPT
Linux Device Driver for Writing a real world driver for embedded Linux
PDF
Kernel Debugging & Profiling
PPT
Cuda 2011
PDF
BKK16-211 Internet of Tiny Linux (io tl)- Status and Progress
PDF
Kernel Process Management
PDF
I2c drivers
PDF
Kernel Debugging & Profiling
PPTX
Rsockets ofa12
PDF
Embedded Android
PDF
LAS16-300: Mini Conference 2 Cortex-M Software - Device Configuration
PPT
Lecture 04
PPTX
Track c-High speed transaction-based hw-sw coverification -eve
PDF
Block Drivers
PDF
Linux dma engine
PDF
Details on Platform Drivers in Embedded Linux
Character Drivers
Embedded Android : System Development - Part II (Linux device drivers)
Hunting and Exploiting Bugs in Kernel Drivers - DefCamp 2012
Unix.system.calls
Optimizing Direct X On Multi Core Architectures
Linux Device Driver for Writing a real world driver for embedded Linux
Kernel Debugging & Profiling
Cuda 2011
BKK16-211 Internet of Tiny Linux (io tl)- Status and Progress
Kernel Process Management
I2c drivers
Kernel Debugging & Profiling
Rsockets ofa12
Embedded Android
LAS16-300: Mini Conference 2 Cortex-M Software - Device Configuration
Lecture 04
Track c-High speed transaction-based hw-sw coverification -eve
Block Drivers
Linux dma engine
Details on Platform Drivers in Embedded Linux
Ad

Recently uploaded (20)

PDF
medical staffing services at VALiNTRY
PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PDF
Digital Strategies for Manufacturing Companies
PDF
Design an Analysis of Algorithms II-SECS-1021-03
PPT
Introduction Database Management System for Course Database
PDF
Understanding Forklifts - TECH EHS Solution
PDF
System and Network Administration Chapter 2
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PPTX
L1 - Introduction to python Backend.pptx
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PDF
System and Network Administraation Chapter 3
PPTX
CHAPTER 12 - CYBER SECURITY AND FUTURE SKILLS (1) (1).pptx
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
PPTX
Transform Your Business with a Software ERP System
medical staffing services at VALiNTRY
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
Digital Strategies for Manufacturing Companies
Design an Analysis of Algorithms II-SECS-1021-03
Introduction Database Management System for Course Database
Understanding Forklifts - TECH EHS Solution
System and Network Administration Chapter 2
Odoo Companies in India – Driving Business Transformation.pdf
L1 - Introduction to python Backend.pptx
Design an Analysis of Algorithms I-SECS-1021-03
Which alternative to Crystal Reports is best for small or large businesses.pdf
Navsoft: AI-Powered Business Solutions & Custom Software Development
Upgrade and Innovation Strategies for SAP ERP Customers
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
System and Network Administraation Chapter 3
CHAPTER 12 - CYBER SECURITY AND FUTURE SKILLS (1) (1).pptx
How to Migrate SBCGlobal Email to Yahoo Easily
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
Transform Your Business with a Software ERP System

Character drivers

  • 1. @ 2021-21 Embitude Trainings <info@embitude.in> All Rights Reserved Character Driver
  • 2. @ 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
  • 3. @ 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)
  • 4. @ 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)
  • 5. @ 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()
  • 6. @ 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);
  • 7. @ 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);
  • 8. @ 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
  • 9. @ 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; ● }
  • 10. @ 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
  • 11. @ 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
  • 12. @ 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
  • 13. @ 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
  • 14. @ 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);
  • 15. @ 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
  • 16. @ 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]
  • 17. @ 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
  • 18. @ 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