Char Drivers
Char Drivers
– Char Drivers
Agenda
User Application
● Interaction with user apps
● Interaction with hardware
● Interaction with the kernel
● Logic Management
Kernel Device Driver ● Buffer Management
● Concurrency issues
Hardware
Classification of Device Drivers
● Device Numbers
● Allocating and Freeing Device Numbers
● Char Device Registration
● Important Data Structures
● File Operations
Anatomy of Char Drivers
User Application
Accessed using the
name of the file
Device node in
/dev
Accessed using the
device number
Hardware
Device Numbers
● Device numbers :
– Major number : Identifies the driver associated with the device
– Minor number : Distinguishes amongst devices of the driver
● ls -l /dev
Device Number Representation
● 'dev_t' holds the device number
Major : a Major : a ● It is a 32-bit quatity :
Minor : x Minor : y
– Major : 12-bits
– Minor : 20-bits
/dev/cdev0 /dev/cdev1
● Device no. to :
– Major : MAJOR(dev_t dev);
– Minor : MINOR(dev_t dev);
Major:Minor to dev_t :
Device Driver
●
● struct file
● struct inode
● struct file_operations
struct file
<linux/fs.h>
struct file {
...
unsigned int f_flags;
struct file_operations *f_op;
void *private_data;
};
struct inode
<linux/fs.h>
struct inode {
...
dev_t i_rdev;
struct cdev *i_cdev;
};
struct inode Vs struct file
open()
open()
open()
<linux/fs.h>
struct file_operations {
...
int (*open) (struct inode *, struct file *);
ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);
ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);
int (*release) (struct inode *, struct file *);
};
struct file_operations : open()
User Application
write() read()
Device node in
/dev
write() read()
{ {
copy_from_user(); copy_to_user();
} }
Device Driver
References