SlideShare a Scribd company logo
Linux Char Device Driver
Gary 2013/03/20
Outline
• Introduction
• Module
• Major Number and Minor Number
• Data Structure
• Registration
• Open and Release
• Read and Write
• Future Work
Introduction
• Device driver is a bridge between physical
devices and programs, and it’s part of kernel.
User program can manage physical devices via
device driver.
• Driver can be roughly divided into Block
device driver and Character device driver. The
former transfer a fixed amount of data each
time, and Character device driver transfer no-
fixed amount of data.
Module
Module
Advantage:
• Reduce kernel image space
• Speed up the boot time
• Facilitate the development of the kernel
function
Module
Linking a module to a kernel
Major Number and Minor Number
• Major number(0-255)
When the kernel receives open() system call,
it selects the driver based on major number.
• Minor number(0-255)
Identify individuals of similar devices.
Meaningless to kernel. Only the driver itself
knows the significance of the minor number.
Major Number and Minor Number
• “c” represents special file of char driver.
• “b” represents device file of block driver.
Major number Minor number
Major Number and Minor Number
• Use mknod command to create device node.
Needs superuser priviledges and four
arguments.
- <name> <device type> <major> <minor>
$mknod /dev/ant c 252 0
• Use rm command to delete device node.
$rm /dev/ant
Data Structure
• Struct file represents an opened-device.
• Struct file_operations is used for kernel to
access the method in driver.
- defined in <linux/fs.h>
• Struct file has field f_op, which is the pointer
point to struct file_operations
File_operations
File_operations
• struct module *owner;
Not a function pointer. For kernel to maintain
the usage count of module.
• loff_t (*llseek) (struct file *, loff_t, int);
Change the position of current file read write
point.
File
• The file mentioned here has no concerned
with the file in normal application.
• For every file which is opened, there is a
correspond struct file.
• The pointer point to file is named filp.
File
Old Registration Method
• Call register_chrdev()
- define in <linux/fs.h>
- /usr/src/<kernel version>/include/linux/fs.h
Old Registration Method
• $cat /proc/devices
New Registration Method
• Kernel uses struct cdev to represent char
device driver. You need to include
<linux/cdev.h>
• Use cdev_alloc() to configure struct cdev
• If you have your own designed struct, you
need to use cdev_init()
Struct cdev *my_cdev = cdev_alloc();
My_cdev->ops = &my_fops;
Void cdev_init(struct cdev *dev, struct file_operations *fops);
New Registration Method
• No matter how to initialize struct cdev, the
owner field must be set
• The last step, use cdev_add() to add to kernel
struct cdev my_cdev;
my_cdev.owner = THIS_MODULE;
int cdev_add(struct cdev *dev, dev_t num, unsigned int count);
Struct cdev you set
Major number
Total amount of device number
New Registration Method
• Destroy cdev
void cdev_del(struct cdev *dev);
Open and Release
• Open operation offers driver initialization, and
increase usage count.
• Release operation decrease usage count.
- Defined in <linux/module.h>
Open
• Most of the open operation of driver should do the
following jobs.
- increase usage count
- check for device specific errors
(ex : no CD in CD-ROM)
- if the target device is opened the first time,
do initializtion
- Identify the minor number and update the f_op
pointer
- allocate and fill data structure in filp->private data
Open
• The first step of open operation is to check the
target device’s minor number.
Release
• Release anything that open operation allocate
to flip->private_data.
• Shut down the target device on the last close.
• Decrease usage count
Read and Write
• flip : file pointer
• buff : argument to the buffer in user-space
• count : transfer data amount
• offp : the file location
ssize_t read(struct file *filp, char *buff, size_t count, loff_t *offp);
ssize_t write(struct file *filp, const char *buff, size_t count, loff_t *offp);
Read and Write
• Data transfer between kernel space and user
space.
• Use the function defined in <asm/uaccess.h>
Read and Write
• The arguments to read
Future Work
• Read more
• Ioctl

More Related Content

PDF
Introduction to char device driver
PDF
Linux Internals - Part II
PPTX
Linux device drivers
PPTX
Bootloaders (U-Boot)
PDF
Linux systems - Linux Commands and Shell Scripting
ODP
Linux commands
Introduction to char device driver
Linux Internals - Part II
Linux device drivers
Bootloaders (U-Boot)
Linux systems - Linux Commands and Shell Scripting
Linux commands

What's hot (20)

PDF
Embedded Linux from Scratch to Yocto
PDF
Q2.12: Debugging with GDB
PDF
PDF
Part 03 File System Implementation in Linux
PPTX
Introduction Linux Device Drivers
PPT
linux device driver
PPT
Basic Linux Internals
PPTX
Basic commands of linux
PPT
U boot porting guide for SoC
PDF
Linux kernel architecture
PDF
Arm device tree and linux device drivers
PDF
PDF
Character drivers
PDF
Linux Internals - Part III
PDF
Shell scripting
PPTX
U-Boot Porting on New Hardware
PPTX
Basics of-linux
PPT
Kernel module programming
PPTX
Bash shell scripting
PPTX
Linux basics
Embedded Linux from Scratch to Yocto
Q2.12: Debugging with GDB
Part 03 File System Implementation in Linux
Introduction Linux Device Drivers
linux device driver
Basic Linux Internals
Basic commands of linux
U boot porting guide for SoC
Linux kernel architecture
Arm device tree and linux device drivers
Character drivers
Linux Internals - Part III
Shell scripting
U-Boot Porting on New Hardware
Basics of-linux
Kernel module programming
Bash shell scripting
Linux basics
Ad

Viewers also liked (9)

PDF
Mosquito Attack
PDF
Kernel overview
PDF
10分で分かるLinuxブロックレイヤ
PDF
Linux device driver for dummies
PPTX
Yocto bspを作ってみた
PDF
オペレーティングシステム 第1回-公開用
PDF
Yocto Project ハンズオン プレゼン用資料
PDF
Ethernetの受信処理
PDF
10GbE時代のネットワークI/O高速化
Mosquito Attack
Kernel overview
10分で分かるLinuxブロックレイヤ
Linux device driver for dummies
Yocto bspを作ってみた
オペレーティングシステム 第1回-公開用
Yocto Project ハンズオン プレゼン用資料
Ethernetの受信処理
10GbE時代のネットワークI/O高速化
Ad

Similar to Linux Char Device Driver (20)

PPTX
Char Drivers And Debugging Techniques
PPT
Linuxdd[1]
PDF
Embedded Android : System Development - Part II (Linux device drivers)
PPT
Linux Device Driver,LDD,
PDF
brief intro to Linux device drivers
PPT
Device drivers tsp
PPTX
Device Drivers
PPT
“black boxes” that make a particular p..
PPTX
Writing Character driver (loadable module) in linux
PDF
Character Drivers
PDF
Unit 5.2 Device Driver.pdf (Device Driver)
PPT
Linux Device Driver for Writing a real world driver for embedded Linux
PPTX
Device_drivers_copy_to_user_copy_from_user.pptx
PPT
device drives in electronics and communication
PPT
lecture_1_introduction_linux_1234567.ppt
PPT
lecture_1_introduction.ppt
PPT
DeviceDriverNov18.ppt
PPT
Embedded system - embedded system programming
PPT
Driver_linux
PDF
Driver Programming Report
Char Drivers And Debugging Techniques
Linuxdd[1]
Embedded Android : System Development - Part II (Linux device drivers)
Linux Device Driver,LDD,
brief intro to Linux device drivers
Device drivers tsp
Device Drivers
“black boxes” that make a particular p..
Writing Character driver (loadable module) in linux
Character Drivers
Unit 5.2 Device Driver.pdf (Device Driver)
Linux Device Driver for Writing a real world driver for embedded Linux
Device_drivers_copy_to_user_copy_from_user.pptx
device drives in electronics and communication
lecture_1_introduction_linux_1234567.ppt
lecture_1_introduction.ppt
DeviceDriverNov18.ppt
Embedded system - embedded system programming
Driver_linux
Driver Programming Report

More from Gary Yeh (10)

PDF
Servlet and JSP
PDF
Introduction of openGL
PDF
Basic Understanding and Implement of Node.js
PDF
Run-time of Node.js : V8 JavaScript Engine
PDF
JQuery UI
PDF
jQuery Mobile and JavaScript
PDF
JQuery mobile
PDF
Database and Java Database Connectivity
PDF
Html5 canvas
PDF
Git Workflow
Servlet and JSP
Introduction of openGL
Basic Understanding and Implement of Node.js
Run-time of Node.js : V8 JavaScript Engine
JQuery UI
jQuery Mobile and JavaScript
JQuery mobile
Database and Java Database Connectivity
Html5 canvas
Git Workflow

Recently uploaded (20)

PDF
Complete React Javascript Course Syllabus.pdf
PDF
System and Network Administraation Chapter 3
PDF
PTS Company Brochure 2025 (1).pdf.......
PDF
Softaken Excel to vCard Converter Software.pdf
PDF
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
PPTX
Materi_Pemrograman_Komputer-Looping.pptx
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PPTX
CHAPTER 12 - CYBER SECURITY AND FUTURE SKILLS (1) (1).pptx
PPTX
history of c programming in notes for students .pptx
PDF
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PPTX
Online Work Permit System for Fast Permit Processing
PDF
Understanding Forklifts - TECH EHS Solution
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PPTX
Materi-Enum-and-Record-Data-Type (1).pptx
PDF
top salesforce developer skills in 2025.pdf
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
PPTX
Transform Your Business with a Software ERP System
PPTX
ISO 45001 Occupational Health and Safety Management System
Complete React Javascript Course Syllabus.pdf
System and Network Administraation Chapter 3
PTS Company Brochure 2025 (1).pdf.......
Softaken Excel to vCard Converter Software.pdf
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
Materi_Pemrograman_Komputer-Looping.pptx
How to Choose the Right IT Partner for Your Business in Malaysia
CHAPTER 12 - CYBER SECURITY AND FUTURE SKILLS (1) (1).pptx
history of c programming in notes for students .pptx
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
Wondershare Filmora 15 Crack With Activation Key [2025
Internet Downloader Manager (IDM) Crack 6.42 Build 41
Online Work Permit System for Fast Permit Processing
Understanding Forklifts - TECH EHS Solution
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
Materi-Enum-and-Record-Data-Type (1).pptx
top salesforce developer skills in 2025.pdf
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
Transform Your Business with a Software ERP System
ISO 45001 Occupational Health and Safety Management System

Linux Char Device Driver

  • 1. Linux Char Device Driver Gary 2013/03/20
  • 2. Outline • Introduction • Module • Major Number and Minor Number • Data Structure • Registration • Open and Release • Read and Write • Future Work
  • 3. Introduction • Device driver is a bridge between physical devices and programs, and it’s part of kernel. User program can manage physical devices via device driver. • Driver can be roughly divided into Block device driver and Character device driver. The former transfer a fixed amount of data each time, and Character device driver transfer no- fixed amount of data.
  • 5. Module Advantage: • Reduce kernel image space • Speed up the boot time • Facilitate the development of the kernel function
  • 7. Major Number and Minor Number • Major number(0-255) When the kernel receives open() system call, it selects the driver based on major number. • Minor number(0-255) Identify individuals of similar devices. Meaningless to kernel. Only the driver itself knows the significance of the minor number.
  • 8. Major Number and Minor Number • “c” represents special file of char driver. • “b” represents device file of block driver. Major number Minor number
  • 9. Major Number and Minor Number • Use mknod command to create device node. Needs superuser priviledges and four arguments. - <name> <device type> <major> <minor> $mknod /dev/ant c 252 0 • Use rm command to delete device node. $rm /dev/ant
  • 10. Data Structure • Struct file represents an opened-device. • Struct file_operations is used for kernel to access the method in driver. - defined in <linux/fs.h> • Struct file has field f_op, which is the pointer point to struct file_operations
  • 12. File_operations • struct module *owner; Not a function pointer. For kernel to maintain the usage count of module. • loff_t (*llseek) (struct file *, loff_t, int); Change the position of current file read write point.
  • 13. File • The file mentioned here has no concerned with the file in normal application. • For every file which is opened, there is a correspond struct file. • The pointer point to file is named filp.
  • 14. File
  • 15. Old Registration Method • Call register_chrdev() - define in <linux/fs.h> - /usr/src/<kernel version>/include/linux/fs.h
  • 16. Old Registration Method • $cat /proc/devices
  • 17. New Registration Method • Kernel uses struct cdev to represent char device driver. You need to include <linux/cdev.h> • Use cdev_alloc() to configure struct cdev • If you have your own designed struct, you need to use cdev_init() Struct cdev *my_cdev = cdev_alloc(); My_cdev->ops = &my_fops; Void cdev_init(struct cdev *dev, struct file_operations *fops);
  • 18. New Registration Method • No matter how to initialize struct cdev, the owner field must be set • The last step, use cdev_add() to add to kernel struct cdev my_cdev; my_cdev.owner = THIS_MODULE; int cdev_add(struct cdev *dev, dev_t num, unsigned int count); Struct cdev you set Major number Total amount of device number
  • 19. New Registration Method • Destroy cdev void cdev_del(struct cdev *dev);
  • 20. Open and Release • Open operation offers driver initialization, and increase usage count. • Release operation decrease usage count. - Defined in <linux/module.h>
  • 21. Open • Most of the open operation of driver should do the following jobs. - increase usage count - check for device specific errors (ex : no CD in CD-ROM) - if the target device is opened the first time, do initializtion - Identify the minor number and update the f_op pointer - allocate and fill data structure in filp->private data
  • 22. Open • The first step of open operation is to check the target device’s minor number.
  • 23. Release • Release anything that open operation allocate to flip->private_data. • Shut down the target device on the last close. • Decrease usage count
  • 24. Read and Write • flip : file pointer • buff : argument to the buffer in user-space • count : transfer data amount • offp : the file location ssize_t read(struct file *filp, char *buff, size_t count, loff_t *offp); ssize_t write(struct file *filp, const char *buff, size_t count, loff_t *offp);
  • 25. Read and Write • Data transfer between kernel space and user space. • Use the function defined in <asm/uaccess.h>
  • 26. Read and Write • The arguments to read
  • 27. Future Work • Read more • Ioctl