Feature 10-character device driver model

Source: Internet
Author: User

I. Compiling and installing character device driver module
1. To have makefile
Obj-m: = MEMDEV.O
Kdir: =/ok6410/linux-3.10
All
Make-c $ (Kdir) m= (PWD) modules Cross_compile=arm-linux-arch=arm
Clean
Rm-f *.ko *.o *.mod.o *.mod.c *.symvers *.bak *.order
2. To have the appropriate driver files
3.insmod lsmod rmmod command is used.
Two. Install the character device (node) file
1. Application finds device files by file name
2. The device file finds a matching driver through the main device number. (The device file and driver have the main device number, the same match)
3. Can print the driver information via cat/proc/devices (mainly the main device number, the secondary device number 0-255 can be)
4. Create a method
mknod/dev/device file name C main device number secondary device number (the main device number and driver are the same)
5. The character device here is a virtual device that simulates several registers of hardware with a memory (for example, an array)
6. Encountering not found may be a dependency is not satisfied, use the arm-linux-readelf-d file name to see the dynamic link library required by the program. To further solve the problem.
Three. Write Data program

Four. Read the data application

Five. Development Board operation

Six. Character Device description structure
1. In any drive model, the device is described by a structure in the kernel.
2. The description of the character device uses the

3. Match device files and device drivers with the master device number. Distinguish the same type of equipment with the secondary device number. Defining the device number with dev_t in the kernel is essentially the unsigned int type. High 12-bit main device number, low 20-bit device number.
4. Combine the main device number and the secondary device number into the member of the device number in the structure, using Mkdev (note device number, secondary device number). If you want to isolate the main device number and the secondary device number from the device number, use major (dev_t device number). MINOR (dev_t device number).
5. Assign the main device number:
(1) static application--register_chrdev_region. However, it is possible to fail and not work properly because there may be device number conflicts.
(2) Dynamic allocation of--alloc_chrdev_region. This will not conflict.
(3) The device number is cancelled--unregister_chrdev_region, and the device should be released later.
6. Set of operating functions
(1) Play the role of mapping, the function of the application space and the driving function through the fileoperations operation function set corresponding to
(2) Do not need to set NULL pointer
Seven. Character device driver Model
1. Driver initialization: (usually done in module_init)
(1) Assigning Cdev. can be statically assigned (defined directly) or dynamically allocated (struct Cdev *pdev = Cdev_alloc ()).
(2) initialization of Cdev. Cdev_init (struct Cdev * pdev, const struct file_operations *fops);
(3) Registration Cdev. Cdev_add (struct Cdev * Pdev, dev_t dev,unsigned count Dev_number);
(4) Hardware initialization
2. Implement device operation
(1) About struct file
In a Linux system, each file opens (to be opened) and a struct file is associated with the kernel, which is created by the Linux kernel when the file is opened. The struct file structure is released after the files are closed.
Among the important members are:
loff_t f_pos---File reading and writing pointers
The struct file_operations* f_ops----The action that the file corresponds to.
(2) About struct Inod
Each file that exists in the file system will have a INOD structure and it corresponds. It is mainly used to record the physical information of a file. Regardless of whether the file is open or not, it will be associated.
Important Members
dev_t T_rdev---equipment number
(3) Open device method
Prepare for future work and complete the initialization work. Mainly indicates the secondary device number, start the device
(4) Release: sometimes called Close. is to turn off the device.
(5) Read: Reads data from the device to the driver (kernel space), and then returns the read data to the user space. Prototypes are as follows

Note: The buff parameter is a pointer from the user space, which cannot be used directly by the kernel space Code, and must use specialized functions
int Copy_from_user (void* to, const void _user *from,int N);
int Copy_to_user (void _user* to,const void * from,int N);

(6) Write
--read data from the address provided by the application
--Write data to device (Hardware class operation)
The parameters are similar to read.
(7) Llseek: Reposition read-write pointer, corresponding system call.
3. Driver logoff
When uninstalling the driver from the kernel, use the Cdev_del function to complete the process. And finally release the device number.

Eight. For the application to use system calls, finally call the kernel space of the drive function (device method):
1. At the time of application assembly, the system call number to be called is stored in R7,
2. Execute the SVC command to complete the jump from application space to kernel space
3. Remove the system call number
4. The actual code (function) of the corresponding kernel space is obtained by checking the table.
5. Actually call the driver via the Vfs_read function





Feature 10-character device driver model

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.