Now that you know how to write the module, writing the device driver is naturally not difficult. I believe that no one who writes a module will find it difficult to write a device driver.
Really, it's not difficult for me to write drivers. You can say with confidence that you can write
Device Driver
. Yes, that's right. "Rejoice, so confident ."
As mentioned above, every module uses
Module_init
Start
Module_exit
Let's take a look.
U
Disk Driver. In the vast sea of people, we can easily find this file
Drivers/USB/storage/USB. c
In this file, it is not difficult to find the following section:
1068 static int _ init usb_stor_init (void)
1069 {
1070
Int
Retval;
1071
Printk (kern_info "Initializing USB Mass
Storage driver.../N ");
1072
1073
/* Register
The driver, return usb_register return code if err
Or */
1074
Retval =
Usb_register (& usb_storage_driver );
1075
If (retval = 0 ){
1076
Printk (kern_info
"USB Mass Storage support registered./N ");
1077
Usb_usual_set_present (usb_us_type_stor );
1078
}
1079
Return retval;
1080}
1081
1082 static void _ exit usb_stor_exit (void)
1083 {
1084
Us_debugp ("usb_stor_exit ()
Called/N ");
1085
1086
/* Deregister the driver
1087
* This will cause disconnect () to be called for each
1088
* Attached Unit
1089
*/
1090
Us_debugp ("--
Calling usb_deregister ()/n ");
1091
Usb_deregister (& usb_storage_driver)
;
1092
1093
/* Don't
Return until all of our control and scanning threads
1094
* Have exited.
Since each thread signals threads_gone as its
1095
* Last act, we have to call wait_for_completion the right
Number
1096
* Of times.
1097
*/
1098
While
(Atomic_read (& total_threads)> 0 ){
1099
Wait_for_completion (& threads_gone );
1100
Atomic_dec (& total_threads );
1101
}
1102
1103
Usb_usual_clear_present (usb_us_type_stor );
1104}
1105
1106 module_init (usb_stor_init );
1107 module_exit (usb_stor_exit );
In fact,
Module_init/module_exit
It's just a macro. Generally, a module writer will give his own initialization function and logout function a different name to show his personality. For example, here
Module_init (usb_stor_init)
And
Module_exit (usb_stor_exit)
Actually, it is to tell the world that the real function is
Usb_stor_init
And
Usb_stor_exit
. This trick is
Linux
The kernel code is not uncommon, so you don't have to worry about it. If it rains, you need to marry and let her go. From
Usb_stor_init
Officially start our exploration journey.
*****************************
From Linux, I am USB.
I am USB in Linux
Link: China-Pub
Dangdang
, Excellence
Linux
Core cultivation path: excellence
Dangdang
,
China-Pub