@ 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

Character drivers

  • 1.
    @ 2021-21 EmbitudeTrainings <info@embitude.in> All Rights Reserved Character Driver
  • 2.
    @ 2021-21 EmbitudeTrainings <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 EmbitudeTrainings <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 EmbitudeTrainings <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 EmbitudeTrainings <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 EmbitudeTrainings <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 EmbitudeTrainings <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 EmbitudeTrainings <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 EmbitudeTrainings <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 EmbitudeTrainings <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 EmbitudeTrainings <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 EmbitudeTrainings <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 EmbitudeTrainings <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 EmbitudeTrainings <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 EmbitudeTrainings <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 EmbitudeTrainings <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 EmbitudeTrainings <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 EmbitudeTrainings <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