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

<!-- [if gte mso 9]><xml> <o:DocumentProperties> <o:Template>Normal.dotm</o:Template> <o:Revision>0</o:Revision> <o:TotalTime>0</o:TotalTime> <o:Pages>1</o:Pages> <o:Words>264</o:Words> <o:Characters>1509</o:Characters> <o:Company>JTU</o:Company> <o:Lines>12</o:Lines> <o:Paragraphs>3</o:Paragraphs> <o:CharactersWithSpaces>1853</o:CharactersWithSpaces> <o:Version>12.0</o:Version> </o:DocumentProperties> <o:OfficeDocumentSettings> <o:AllowPNG /> </o:OfficeDocumentSettings> </xml><![endif]--><!-- [if gte mso 9]><xml> <w:WordDocument> <w:Zoom>0</w:Zoom> <w:TrackMoves>false</w:TrackMoves> <w:TrackFormatting /> <w:PunctuationKerning /> <w:DrawingGridHorizontalSpacing>18 pt</w:DrawingGridHorizontalSpacing> <w:DrawingGridVerticalSpacing>18 pt</w:DrawingGridVerticalSpacing> <w:DisplayHorizontalDrawingGridEvery>0</w:DisplayHorizontalDrawingGridEvery> <w:DisplayVerticalDrawingGridEvery>0</w:DisplayVerticalDrawingGridEvery> <w:ValidateAgainstSchemas /> <w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid> <w:IgnoreMixedContent>false</w:IgnoreMixedContent> <w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText> <w:Compatibility> <w:BreakWrappedTables /> <w:DontGrowAutofit /> <w:DontAutofitConstrainedTables /> <w:DontVertAlignInTxbx /> <w:UseFELayout /> </w:Compatibility> </w:WordDocument> </xml><![endif]--><!-- [if gte mso 9]><xml> <w:LatentStyles DefLockedState="false" LatentStyleCount="276"> </w:LatentStyles> </xml><![endif]--> <!-- [if gte mso 10]> <mce:style><! /* Style Definitions */ table.MsoNormalTable {mso-style-name:"Table Normal"; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-parent:""; mso-padding-alt:0cm 5.4pt 0cm 5.4pt; mso-para-margin:0cm; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:12.0pt; font-family:"Times New Roman"; mso-ascii-font-family:Cambria; mso-ascii-theme-font:minor-latin; mso-fareast-font-family:"Times New Roman"; mso-fareast-theme-font:minor-fareast; mso-hansi-font-family:Cambria; mso-hansi-theme-font:minor-latin; mso-bidi-font-family:"Times New Roman"; mso-bidi-theme-font:minor-bidi;} --> <!-- [endif]--> <!-- StartFragment-->

既然知道了编写模块的方法,那么编写设备驱动程序自然也就不难了。我相信,每一个会写模块的人都不会觉得写设备驱动有困难。

真的,我没说假话,写驱动不是什么难事,你完全可以很自信地说,你已经可以写 设备驱动 了。对,没错,“飘柔,就这么自信。”

前面说了每一个模块都是以 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 正式开始我们的探索之旅。

<!-- EndFragment-->

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

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

《Linux那些事儿之我是USB》 链接为:china-pub当当卓越

Linux 内核修炼之道》链接为:卓越 当当 china-pub


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值