Each character device in Linux has a struct cdev
Structure, so writing the character device driver is to fill in the structure.
The struct is as follows:
Linux-2.6.22/include/Linux/cdev. h
Struct cdev {
Struct kobject kobj ;//
Each cdev
All are kobject
Struct module * owner ;//
Pointing to implementation-driven modules
Const struct file_operations * OPS ;//
How to manipulate this character Device File
Struct list_head list ;//
And cdev
Inode-> I _devices of the corresponding character Device File
Linked List Header
Dev_t dev ;//
Start device ID
Unsigned int count ;//
Device range number size
};
General process:
Register a character device:
- Apply for a device number
|
Register_chrdev_region/alloc_chrdev_region |
- Initialize the cdev structure of the device.
|
Cdev_init, cdev. Owner, cdev_ops |
- Register character Devices
|
Cdev_add (); |
- Allocate memory for the device description Structure
|
Kmalloc |
- Allocate memory for devices
|
Mem_devp.data = kmalloc (); |
Detach a character device:
Cancel a device |
Cdev_del (& cdev ); |
Release Device struct |
Kfree (mem_devp ); |
Release Device number |
Unregister_chrdev_region |