《Linux那些事儿之我是USB》我是U盘(4)想到达明天现在就要启程

来源:岁月联盟 编辑:exp 时间:2011-10-20

既然知道了编写模块的方法,那么编写设备驱动程序自然也就不难了。我相信,每一个会写模块的人都不会觉得写设备驱动有困难。
真的,我没说假话,写驱动不是什么难事,你完全可以很自信地说,你已经可以写 设备驱动 了。对,没错,“飘柔,就这么自信。”
前面说了每一个模块都是以 module_init 开始,以 module_exit 结束的,那么我们就来看一下 U 盘驱动的这个模块。在茫茫人海中,我们很容易找到这个文件 drivers/usb/storage/usb.c ,在这个文件中又不难发现下面这段:
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);

其实, module_init/module_exit 只是一个宏,通常写模块的人为了彰显自己的个性,会给自己的初始化函数和注销函数另外取名字,比如这里 module_init(usb_stor_init) 以及 module_exit(usb_stor_exit) 实际上就是告诉这个世界,真正的函数是 usb_stor_init 和 usb_stor_exit 。这种伎俩在 Linux 内核代码中屡见不鲜,见多了也就不必大惊小怪了,天要下雨娘要嫁人,随她去吧。下面就从 usb_stor_init 正式开始我们的探索之旅。

 

*****************************

摘自《Linux那些事儿之我是USB》