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.
Sixth. First Linux driver: Count the number of words