The process of opening a character device is from
VFS Layer Open System Call ==> def_chr_fops --> open ==> cdev-> fops-> open
==> Device specific operation
If the same type of device is used, for example, the system has multiple framebuffers, the common operations are put in cdev-> fops-> open. At this time, only this driver is registered with the system;
Put the devices with the same primary device number (for example,/dev/fb0/dev/Fb1) in an array, and then go to cdev-> fops-> open
Operations that reflect the differences between different devices are called Based on the device number as the index, that is, device specific operation. This is why hardware drivers such as framebuffer/block do not implement file_operations, these operations are related to specific devices.
This is the so-called one type of device should be one type of driver.
It can be considered that most simple devices only need to be implemented to the cdev-> fops-> open location. When they become complex, to extract the commonalities, You need to implement the [red device specific operation location.
For IOCTL operations, f_op-> unlocked_ioctl is preferentially executed. If unlocked_ioctl is not available, f_op-> IOCTL is executed.
In ARM development, I learned how to use. IOCTL in 2.6.32. There is also IOCTL in file_operatioins. When I upgraded myself to 2.6.36, when I used Vim to check the source code, I found that there was no IOCTL and only the unlocked_ioctl and compat_ioctl functions were available.
Rewrite the unlocked_ioctl function by yourself. However, I do not know how to test it with an application,
When I use the ioctl function in the kernel, I use IOCTL (FD, "on" in the application to test whether the ioctl function is called.
However, when the unlocked_ioctl function is changed to the unlocked_ioctl () function, the system prompts that this function is not available. What can I do?
Long test_ioctl (struct file * filp, unsigned int cmd, unsigned long value ){
Struct newdev * P = filp-> private_data;
Printk ("IOCTL % x \ n", P );
Switch (CMD ){
Case 0:
P-> On (); break;
Case 1:
P-> off (); break;
Default:
Printk ("unknow cmd \ n ";
Return-einval;
}
Return 0;
}
Struct file_operations fops = {
. Open = test_open,
. Unlocked_ioctl = test_ioctl,
};
Void test_on (void ){
Printk ("test_on test \ n ";
}
Void test_off (void ){
Printk ("test_off test \ n ";
}
Void initial_device (struct newdev * D ){
D-> On = test_on;
D-> off = test_off;
}
The application is as follows:
Int main (INT argc, char * argv []) {
Int FD;
Char Buf [10];
Printf ("1 \ n ";
Printf ("argc [% d] \ n", argc );
If (argc! = 2 ){
Printf ("usage % s string \ n", argv [0]);
Return-1;
}
Printf ("2 \ n ";
FD = open ("/dev/test", o_rdwr );
Printf ("3 \ n ";
If (FD <0 ){
Printf ("FD open errror \ n ";
Return-1;
}
Printf ("4 \ n ";
If (! Strncasecmp (argv [1], "on", 2 ))
Unlocked_ioctl (FD, 0 );
If (! Strncasecmp (argv [1], "off", 3 ))
Unlocked_ioctl (FD, 1 );
Return 0;
}
Root @ Xu-LAPTOP:/usr/src/linux-2.6.36/Drivers/New/10th # gcc-O app. c
/Tmp/cc4pwycc. O: In function 'main ':
App. C. Text + 0xf1): Undefined reference to 'unlocked _ ioctl'
App. C. Text + 0x12a): Undefined reference to 'unlocked _ ioctl'
Collect2: LD returned 1 exit status
Root @ Xu-LAPTOP:/usr/src/linux-2.6.36/Drivers/New/10th # To remove BKL from the kernel slowly, added unlocked_ioctl, but there are many places to use ioctl, therefore, it is retained until b19dd42f removes all IOCTL usage before removing IOCTL from file_operations.
So you can use unlocked_ioctl in the kernel.
However, unlocked_ioctl cannot be used in your application, because there is no change in the system call ioctl, or the original system call interface, but in the implementation of the System Call, vfs_ioctl () is changed to unlocked_ioctl, at the application layer, you don't need to pay attention to the changes in these implementations in the kernel. You only need to use them according to the usage of system calls.
Therefore, in the kernel module, you should use. unlocked_ioctl = test_ioctl;
During the application layer test, the system call of the application is still IOCTL ().
In this case, the middle Virtual File layer of the VFS layer calls the unlocked_ioctl I wrote, while the application layer still calls the functions in VFS through IOCTL. The work I need to do is completed by VFS.