linux char device driver
TRANSCRIPT
![Page 1: Linux Char Device Driver](https://reader034.vdocument.in/reader034/viewer/2022042508/55d1d1b8bb61ebf34e8b473c/html5/thumbnails/1.jpg)
Linux Char Device Driver
Gary 2013/03/20
![Page 2: Linux Char Device Driver](https://reader034.vdocument.in/reader034/viewer/2022042508/55d1d1b8bb61ebf34e8b473c/html5/thumbnails/2.jpg)
Outline
• Introduction
• Module
• Major Number and Minor Number
• Data Structure
• Registration
• Open and Release
• Read and Write
• Future Work
![Page 3: Linux Char Device Driver](https://reader034.vdocument.in/reader034/viewer/2022042508/55d1d1b8bb61ebf34e8b473c/html5/thumbnails/3.jpg)
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.
![Page 4: Linux Char Device Driver](https://reader034.vdocument.in/reader034/viewer/2022042508/55d1d1b8bb61ebf34e8b473c/html5/thumbnails/4.jpg)
Module
![Page 5: Linux Char Device Driver](https://reader034.vdocument.in/reader034/viewer/2022042508/55d1d1b8bb61ebf34e8b473c/html5/thumbnails/5.jpg)
Module
Advantage:
• Reduce kernel image space
• Speed up the boot time
• Facilitate the development of the kernel function
![Page 6: Linux Char Device Driver](https://reader034.vdocument.in/reader034/viewer/2022042508/55d1d1b8bb61ebf34e8b473c/html5/thumbnails/6.jpg)
Module
Linking a module to a kernel
![Page 7: Linux Char Device Driver](https://reader034.vdocument.in/reader034/viewer/2022042508/55d1d1b8bb61ebf34e8b473c/html5/thumbnails/7.jpg)
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.
![Page 8: Linux Char Device Driver](https://reader034.vdocument.in/reader034/viewer/2022042508/55d1d1b8bb61ebf34e8b473c/html5/thumbnails/8.jpg)
Major Number and Minor Number
• “c” represents special file of char driver.
• “b” represents device file of block driver.
Major number Minor number
![Page 9: Linux Char Device Driver](https://reader034.vdocument.in/reader034/viewer/2022042508/55d1d1b8bb61ebf34e8b473c/html5/thumbnails/9.jpg)
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
![Page 10: Linux Char Device Driver](https://reader034.vdocument.in/reader034/viewer/2022042508/55d1d1b8bb61ebf34e8b473c/html5/thumbnails/10.jpg)
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
![Page 11: Linux Char Device Driver](https://reader034.vdocument.in/reader034/viewer/2022042508/55d1d1b8bb61ebf34e8b473c/html5/thumbnails/11.jpg)
File_operations
![Page 12: Linux Char Device Driver](https://reader034.vdocument.in/reader034/viewer/2022042508/55d1d1b8bb61ebf34e8b473c/html5/thumbnails/12.jpg)
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.
![Page 13: Linux Char Device Driver](https://reader034.vdocument.in/reader034/viewer/2022042508/55d1d1b8bb61ebf34e8b473c/html5/thumbnails/13.jpg)
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.
![Page 14: Linux Char Device Driver](https://reader034.vdocument.in/reader034/viewer/2022042508/55d1d1b8bb61ebf34e8b473c/html5/thumbnails/14.jpg)
File
![Page 15: Linux Char Device Driver](https://reader034.vdocument.in/reader034/viewer/2022042508/55d1d1b8bb61ebf34e8b473c/html5/thumbnails/15.jpg)
Old Registration Method
• Call register_chrdev()
- define in <linux/fs.h>
- /usr/src/<kernel version>/include/linux/fs.h
![Page 16: Linux Char Device Driver](https://reader034.vdocument.in/reader034/viewer/2022042508/55d1d1b8bb61ebf34e8b473c/html5/thumbnails/16.jpg)
Old Registration Method
• $cat /proc/devices
![Page 17: Linux Char Device Driver](https://reader034.vdocument.in/reader034/viewer/2022042508/55d1d1b8bb61ebf34e8b473c/html5/thumbnails/17.jpg)
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);
![Page 18: Linux Char Device Driver](https://reader034.vdocument.in/reader034/viewer/2022042508/55d1d1b8bb61ebf34e8b473c/html5/thumbnails/18.jpg)
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
![Page 19: Linux Char Device Driver](https://reader034.vdocument.in/reader034/viewer/2022042508/55d1d1b8bb61ebf34e8b473c/html5/thumbnails/19.jpg)
New Registration Method
• Destroy cdevvoid cdev_del(struct cdev *dev);
![Page 20: Linux Char Device Driver](https://reader034.vdocument.in/reader034/viewer/2022042508/55d1d1b8bb61ebf34e8b473c/html5/thumbnails/20.jpg)
Open and Release
• Open operation offers driver initialization, and increase usage count.
• Release operation decrease usage count.
- Defined in <linux/module.h>
![Page 21: Linux Char Device Driver](https://reader034.vdocument.in/reader034/viewer/2022042508/55d1d1b8bb61ebf34e8b473c/html5/thumbnails/21.jpg)
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
![Page 22: Linux Char Device Driver](https://reader034.vdocument.in/reader034/viewer/2022042508/55d1d1b8bb61ebf34e8b473c/html5/thumbnails/22.jpg)
Open
• The first step of open operation is to check the target device’s minor number.
![Page 23: Linux Char Device Driver](https://reader034.vdocument.in/reader034/viewer/2022042508/55d1d1b8bb61ebf34e8b473c/html5/thumbnails/23.jpg)
Release
• Release anything that open operation allocate to flip->private_data.
• Shut down the target device on the last close.
• Decrease usage count
![Page 24: Linux Char Device Driver](https://reader034.vdocument.in/reader034/viewer/2022042508/55d1d1b8bb61ebf34e8b473c/html5/thumbnails/24.jpg)
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);
![Page 25: Linux Char Device Driver](https://reader034.vdocument.in/reader034/viewer/2022042508/55d1d1b8bb61ebf34e8b473c/html5/thumbnails/25.jpg)
Read and Write
• Data transfer between kernel space and user space.
• Use the function defined in <asm/uaccess.h>
![Page 26: Linux Char Device Driver](https://reader034.vdocument.in/reader034/viewer/2022042508/55d1d1b8bb61ebf34e8b473c/html5/thumbnails/26.jpg)
Read and Write
• The arguments to read
![Page 27: Linux Char Device Driver](https://reader034.vdocument.in/reader034/viewer/2022042508/55d1d1b8bb61ebf34e8b473c/html5/thumbnails/27.jpg)
Future Work
• Read more
• Ioctl