First article
reprinted from Csdn VIPCLX
Eight steps to writing Linux drivers
I. Build Linux driver framework (load, unload Linux drivers)
The Linux kernel uses the driver to load the driver, perform some initialization actions during the loading process (build the device file, allocate memory, etc.), in the driver must provide the corresponding function to handle the driver initialization work, the function must use the MODULE_INIT macro to specify Linux system in the exit is to uninstall the Linux driver, the uninstallation process to do some exit work (delete device files, free memory, etc.), in the driver should provide the appropriate function to handle the exit work, the function must be specified using the MODULE_EXIT macro. Linux drivers generally want these two macros to specify these two functions, so the C program containing the two macros and the two functions they specify can be considered a Linux-driven framework.
II. registration and cancellation of equipment documents
Any Linux driver needs to have a device file to interact with the application. The work of establishing a device file is generally done in the previous step module_init the function specified by the macro, you can use the Misc_register function to create a device file; the removal of a device file is generally done in the previous step module_exit the function specified by the macro, and can be used MISC_ The deregister function deletes the device file.
Iii. specifying driver-related information
The driver is self-describing, and the driver information needs to be specified in the driver source code. Use macros such as Module_author (author name), Module_license (open source protocol used), Module_alias (alias), Module_description (driver description) to specify driver-related information, These macros are generally written at the end of the drive source file. This information can be obtained through the Modinfo command.
Iv. Specifying callback functions
The Linux driver contains a number of actions, also known as events, such as "read" "Write" events, when triggering the corresponding event, the Linux system will automatically invoke the corresponding callback function for the driver. A driver does not necessarily have to specify a callback function. The callback function is registered through the relevant mechanism. If the callback function associated with the device file is registered using the Misc_register function.
V. Writing business logic
There is nothing to say, always can't register some empty callback function, do nothing.
VI. Preparation of Makefile documents
The Linux kernel source code compilation rules are defined by the makefile file, and each Linux driver must have a makefile file.
Vii. Compiling Linux drivers
Linux drivers can be compiled directly into the kernel (using obj-y compilation), or they can be compiled separately as modules (using OBJ-M compilation).
Viii. Installing and uninstalling Linux drivers
If the driver is compiled into the kernel, the driver will automatically load as soon as Linux uses the kernel. If the Linux driver exists separately in the module, you need to load the Linux driver module with the Insmod or Modprobe command, and uninstall the module using the Rmmod command.
Second article
Linux Driver Development Steps
The Linux kernel is made up of various drivers, and about 85% of the kernel source code is a variety of driver codes. The wide range of drivers in the kernel can be modified on the same type-driven basis to suit specific veneer
The difficulty of writing the drive is not the exact operation of the hardware, but the understanding of the architecture of the existing driver and the addition of this hardware to the architecture. The place to spend energy is to understand the drive architecture of a device and find the location of the change.
also, consider driver concurrency issues.
write the Linux approximate process:
1. View schematic, data sheet, how to operate the device
2. Find a similar driver in the kernel to develop it for the template, sometimes starting from 0
3. Implement the initialization of the driver, such as registering the driver with the kernel, so that the kernel can find the appropriate driver when the application passes in the file name.
4. Design the operations to be implemented, such as Open,close,read,write functions.
5. Implement interrupt Service (interrupts are not required for each device driver)
6. Compile the driver into the kernel or load it with the Insmod command
7. Test the driver.
loading and unloading of driversThe
driver can be statically compiled into the kernel, or it can be loaded as a module when it is used. When configuring the kernel, if a configuration item is set to M, it means that he is compiled into a module
insmod load rmmod uninstall lsmod see which modules have been loaded in the kernel
when using Insmod to load a module, the module's initialization function is called, which is used to register the driver with the kernel.
when the module is unloaded using Rmmod, the module's purge function is called
Assuming that the initialization function is my_init, the purge function is My_cleanup
then Module_init (my_init);
Module_exit (my_cleanup);
One writing character device driver
writing character device drivers is the function of writing individual functions for the file_operations structure of a specific hardware.
So, when an application accesses a device file through a system call such as Open,read,write, how does the Linux system know the members of the Open,read,write in the file_operator structure to which driver to invoke?
1. When the module is initialized, register the main device number with file_operations
There is an initialization function in the driver that is called when the driver is installed. In the initialization function, the driver file_operation structure is registered with the kernel along with its master device number. for character devices, use int register_chrdev In
this way, when the application operates the device file, the Linux system locates the file_operation structure registered in the kernel based on the device file type, the main device number (for the block device is the block_device_operations structure), The secondary device number is used only by the driver itself to distinguish him from the first of its kind.
the approximate procedure for writing a driver:
1. Write the driver initialization function.
perform the necessary initialization, including hardware initialization (which can also be placed elsewhere), registering drivers with the kernel, and so on.
2. Construction File_operation
Advanced driver programming also involves interrupts, select mechanisms, and fasync asynchronous notification mechanisms. Wait
Driver Compilation
put the xxx.c driver file written on the CD into the kernel Drivers/char subdirectory, add the following line in Drivers/char/makefile
obj-m + = XXX.O
You can then create a. ko file by executing "make modules" in the kernel root directory
Finally, to build the device file in the Development Board root file system
mknod/dev/leds C 231 0