Character Device Drivers in deep

Linux Kernel Programming - Character Device Drivers
Instructor:
Linux Trainer
7 students enrolled
English [Auto]
Write character device driver from scratch
Understand the various file operations provided by character driver

Updated on Sep 9: Added file_operation ioctl, access control section

What will you learn from this course?

  • Introduction to character drivers
  • What is device number and device file
  • Allocating device number – statically and dynamically
  • Creating device file – Manually(mknod) and automatically (udev)
  • Registering character device and its file operation with Kernel
  • Copying data from user space to kernel space and vice versa
  • Understanding the various structures – struct file and struct inode
  • Implementation of open, release, read, write, llseek, ioctl file operations
  • How to create multiple device nodes and add support for private data
  • Flow of write system call from kernel system call entry point to driver write file operations
  • Handling all the error cases in ioctl implementation
  • Sending a signal to user space from kernel space
  • Various access control mechanisms and capabilities
  • Misc Driver

API’s covered in this course:

  • MAJOR
  • MINOR
  • MKDEV
  • register_chrdev_region
  • alloc_chrdev_region
  • class_create
  • device_create
  • class_destroy
  • device_destroy
  • unregister_chrdev_region
  • imajor
  • iminor
  • copy_from_user
  • copy_to_user
  • put_user
  • get_user
  • print_dev_t
  • format_dev_t
  • strnlen_user
  • container_of
  • access_ok

Commands used in this course:

  • mknod
  • udevadm monitor

Introduction

1
Introduction to character device drivers
2
List Character and Block Devices
3
Steps in Registering a character driver
4
Device Number = Major + Minor
5
How to choose a device number
6
Data type for device number dev_t
7
Example of MAJOR,MINOR,MKDEV macros
8
/proc/devices file
9
Static Allocation of Device Numbers
10
Example of static allocation of device numbers
11
Test cases on Previous Example
12
What happens if i try to register same major number
13
What happens if i try to register same name
14
Registering Maximum Minor Numbers
15
What is Maximum Major Number
16
Dynamic Allocation of Device Number
17
Example of dynamic allocation of device number
18
Calling alloc_chrdev_region in loop
19
Automatic Device Node Creation - class_create
20
Automatic Device Node Creation - device_create
21
Example of automatic device node creation

File Operations

1
Introduction
2
Registering file_operations with Linux Kernel
3
cdev_init vs cdev_alloc
4
Linux Kernel Module example of file operations registrations - cdev_alloc
5
Linux Kernel Module example of file operations registrations - cdev_init
6
Pseudo Devices - /dev/null, /dev/zero, /dev/urandom
7
Walkthrough of drivers/char/mem.c
8
How many times open and release will be called in case of fork
9
How many times open and release will be called in case of multiple processes
10
struct file
11
Example of struct file
12
struct inode
13
Example of struct inode
14
Example of struct inode vs struct file
15
What happens when i print user space buffer in write function
16
Accessing user space buffer in Kernel space
17
copy_from_user
18
What happens when i pass NULL ptr in write system call
19
copy_to_user
20
put_user
21
get_user
22
Passing structure from user space to kernel space and vice versa
23
Can i pass user variables on heap to kernel driver
24
Passing structure with one member from stack and other in heap
25
Kernel utility to print dev_t - print_dev_t, format_dev_t
26
Can i use strlen in kernel space
27
strnlen_user
28
Solution to passing structure with one member in stack and other in heap
29
Problem in character driver when we read character by character
30
Adding offset support in read and write implementation
31
Updated logic to read/write bytes less than requested/available
32
Adding lseek support
33
Creating multiple device nodes
34
container_of macro
35
Adding support for private data
36
What happens when i call write from user space

IOCTL

1
Introduction
2
Ioctl example - BLKGETSIZE
3
Ioctl example - BLKGETSIZE64
4
Ioctl driver method - unlocked_ioctl
5
Adding three ioctls in kernel driver
6
Symbolic names to ioctls commands
7
Defining ioctl commands
8
C Example of ioctl macros
9
Macros to decode information from ioctl command
10
Updated driver code using ioctl macros
11
What should be the return value if unsupported command is sent from user space
12
access_ok macro
13
Updated driver code using access_ok macro
14
Fail scenarios of access_ok
15
What happens when we pass a different size in ioctl command
16
compat_ioctl
17
Issues when 32-bit process trying to perform ioctl command
18
Sending a signal from module to process

Access Control and Capabilities

1
How many times a device driver is being used by application
2
Allow only one process to use the device file at a time
3
Restricting access to a single user at a time
4
Capabilities
5
Example of using capabilities - cap_dac_override
6
Example of using capabilities - cap_sys_module
7
Example of using capabilities - cap_sys_admin
8
Open Flags
9
What happens if I don't call close and process terminates

Misc Driver

1
Introduction
2
Example of Misc Driver
You can view and review the lecture materials indefinitely, like an on-demand channel.
Definitely! If you have an internet connection, courses on Udemy are available on any device at any time. If you don't have an internet connection, some instructors also let their students download course lectures. That's up to the instructor though, so make sure you get on their good side!

Be the first to add a review.

Please, login to leave a review
3ffca1c192ffa1915288a8818d14dc90
30-Day Money-Back Guarantee

Includes

4 hours on-demand video
Full lifetime access
Access on mobile and TV
Certificate of Completion