Character device driver framework
1. test functions
1) open (), enable character devices;
2) IOCTL (), device with Operation characters;
3) Close (), close the character device;
2. character device functions
1) smartchip _ Init (), module loading; (insmod smartchip. Ko)
A. register_chrdev_region (). Apply for the device number;
B. alloc_chrdev_region (): Request the device number;
C. smartchip_setup_cdev ();
A. cdev_init (): Initialize the cdev member;
B. cdev_add (): registers character devices;
2) smartchip_open (): Open the device;
3) smartchip_ioctl (), module operation;
A. get_user (), from user space to kernel space;
B. put_user (): kernel space to user space;
4) smartchip_release (), disable the device;
5) smartchip_cleanup (), uninstall the module; (rmmod smartchip. Ko)
A. cdev_del (): cancels character devices;
B. unregister_chrdev_region (). Release the device number;
3. I2C Device Access
1) smartchip_i2c_read ();
A. get_fs ();
B. set_fs ();
C. sys_open (): Enable the I2C device;
D. sys_ioctl () to operate I2C devices;
E, sys_read (), read I2C devices;
F, sys_close (), disable the I2C device;
2) smartchip_i2c_write ();
A. get_fs ();
B. set_fs ();
C. sys_open (): Enable the I2C device;
D. sys_ioctl () to operate I2C devices;
E, sys_write (), write I2C device;
F, sys_close (), disable the I2C device;
Details:
1. The cdev struct describes a character device;
2. mkdev (intmajor, int minor) generates dev_t Based on the master and secondary device numbers;
Major (dev_t Dev), get the master device number from dev_t;
Minor (dev_t Dev), get the device number from dev_t;
3. The cdev_init () function is used to initialize cdev members and establish connections between cdev and file_operations;
The cdev_add () function adds a cdev to the system to register the character device;
The cdev_del () function deletes a cdev from the system to cancel the character device;
The register_chrdev_region () or alloc_chrdev_region () function applies to the system for a device number. The former is known as the device Number of the starting device, the latter is unknown, and the system dynamically applies for an unused device number;
The unregister_chrdev_region () function is used to release the originally applied device number;
4. The member functions in the file_operations struct are the main content of the character device driver, the interface between the character device driver and the kernel, and the final implementer of Linux system calls by user space;
5. When the user space calls the Linux API function open () to open the device file, the open () function of the device driver is finally called. The driver may not implement this function. In this case, the open operation of the device will always succeed;
6. filp is the object structure pointer;
7. Private Data private_data: In the open () function, point the private data private_data of the file to the device struct, and then access the device struct through private_data In The IOCTL () function;
8. In the open () function, container_of () is used to find the pointer of the corresponding struct through the pointer of the struct member. The first parameter is the pointer of the struct member, the second parameter is the type of the entire struct, the third parameter is the type of the struct member of the first input parameter, and the container_of () return value is the pointer of the entire struct;
9. kernel space and user space access functions:
A. copy_from_user () copies the user space to the kernel space, and copy_to_user () copies the kernel space to the user space. Both functions return the number of bytes that cannot be copied, if the copy is successful, the returned value is 0;
B. If the memory to be copied is of the simple type, such as char, Int, and long, you can use simple put_user (), get_user (), get_user (Val, (int *) arg), from the user space to the kernel space, Arg is the address of the user space;
10. The cmd parameters of the I/O control function are pre-defined I/O control commands, and Arg corresponds to the commands;
Three important data structures
1. file_operations Structure
2. File structure:
A. the file structure is not associated with the file in the user space program. The former is a kernel structure and will not appear in the user program. The latter is defined in the C library and will not appear in the kernel code.
B. The file structure represents an open file, which is created by the kernel at open and passed to all functions operated on the file until the final close function. After all instances of the file are closed, the kernel releases the data structure.
3. inode structure:
The kernel uses the inode structure to internally represent the file. Therefore, it has a different structure than the file structure, and the latter indicates the opened file descriptor. For a single file, there may be many file structures that represent the file descriptor opened by the file, but they all point to a single inode structure.
Open Function
INT (* open) (struct inode * inode, struct file * filp );
The inode parameter contains the required information in its I _cdev field, that is, the cdev structure we previously set. The only problem is that we usually don't need the cdev structure, but want to get the smartchip_dev structure containing the cdev structure.
Container_of (pointer, container_type, container_field );
This macro requires a pointer to the container_field field, that is, pointer. This field is included in the container_type structure and returns the structure pointer containing this field.
Doubt:
What is the purpose of class_create?
Create a class, create a device node under the/dev/directory, and then create a device node through device_create.