Old Interface : register_chrdev // register the device driver (implementation of the registration device number and corresponding file_operation operating structure)
// Disadvantages: Only major number, no (not supported) minor number
of new interfaces :
register_chrdev_region (equipment registered number) / alloc_chrdev_region (automatic dispensing device number) + cdev
cdev structure and related functions
Structure: struct the cdev { struct the kobject Kobj; struct Module1 * owner; const struct the file_operations * OPS; // include specific operation functions available struct the list_head List; the dev_t dev; // major and minor device number unsigned int COUNT; };
Correlation function:
cdev_alloc (cdev allocate memory space for the structure), cdev_init,
cdev_add (registration to the driving function), cdev_del
The device number associated macros: MKDEV, MAJOR, MINOR
New registration character device driver interface Step:
1, register / major and minor number automatically assigned
register_chrdev_region (registered device number) / alloc_chrdev_region (automatic dispensing device number)
2, registration character device driver
cdev_init
cdev_add
new character device driver interface cancellation steps:
1, cancellation of character device drivers
cdev_del
2, cancellation of the application of major and minor device numbers
unregister_chrdev_region
Reflection type error handling error halfway
(1). Many kernel function contains a lot of operations which each step may be wrong, and error
is necessary to step back to a no go.
(2) The error-prone flag sequence are made, once the error, error handling in order to roll back. For example, the third step wrong, using goto jump to the error flag Flag, the
process things produced after successful execution of the first two steps (such as the cancellation of the assigned device number)
Use cdev_alloc function:
Function: Use kalloc cdev assigned a heap memory structure for a pointer variable cdev structure
Objective: to reduce the size of the .data segment (segment with only a pointer) to facilitate the release of the memory demand
(structure only the size of a pointer variable with a pointer, but only by the size of the structure of a variable structure)
using a program memory:
global variables .data segment
local variable stack
malloc heap
Note: when using cdev_alloc function, the function may be used cdev_init Alternatively a Code: The
pcdev = cdev_alloc(); pcdev->owner = THIS_MODULE //cdev_init(pcdev,&test_fops); pcdev->ops = &test_fops;