Download presentation
Presentation is loading. Please wait.
1
Introduction to Linux Device Drivers
Class 2: Linux Character Device Drivers August 5, 2014 Khem Raj
2
Introduction What is a Device Driver
Operates and controls a given device type Part of kernel runs with same privileges Abstracts out interfaces to be common read/write like files
3
Introduction Device Driver Types
Character Device drivers e.g. tty drivers Block Device drivers Network Device drivers User-space device drivers
4
Character Device Drivers
Drivers for byte-oriented operations Serial driver Audio/video drivers Camera drivers
5
Character Device Files
Used to communicate with device Drivers Uses Virtual File system Applications write/read usual way on device files File operations translated by VFS to device operations Even though application operations are normal output may not be same E.g. an immediate read after write may not be same
6
Character Device Drivers
User Space kernel space device space App1 Character Device Files Virtual File System Char Device Driver Char Device Driver Char device A Char device B
7
Character Device Operations
Application e.g. minicom Device File e.g. /dev/ttyUSB0 Device Driver Device
8
Major and Minor Numbers
Application talks to device file via file name /dev/console Connection between Device File and Device Driver Device file number <major, minor> Defined in linux/types.h MAJOR(dev_t dev), MINOR(dev_t dev) MKDEV(int major, int minor) – Creates device
9
Major and Minor Numbers
Connecting device file to device driver Register for given major,minor device files register_chrdev_region () Static allocation alloc_chrdev_region() Assign the device number dynamically Link device file ops to device driver functions
10
Simple serial driver module
11
Compiling module Needs prepared kernel source tree Now Build module
zcat /proc/config.gz > .config make prepare make scripts Make Now Build module make –C <path-to-kernel-srcs> M=$PWD modules
12
Installing and running
sudo insmod –f fakemod.ko Lsmod | grep fakemod rmmod fakemod
13
Running the driver
14
File Creation and operations
Automatic creation of devices Using devfs APIs struct class *class = class_create(THIS_MODULE, "<device class name>"); Populate device major/minor Device_create(class, NULL, mydevice, NULL, "<device name format>", ...)
15
File Creation and operations
Destroy device and class Calls should be made in reverse order device_destroy(class, mydevice); class_destroy(class);
16
File Operations File operations use same APIs from userspace
VFS decodes file types and transfers file operations to right bus/channel Driver should register file operations with VFS Fill fileops struct with desired operations (Struct file_operations ) dev_open, dev_close, dev_read, dev_write Initialize character device struct cdev_init() Hand over to VFS cdev_add()
17
File Operations
18
Thanks Questions?
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.