linux驱动程序 实例,Linux驱动程序实例

62e50291a81fc54b507f33cb80033297.gif Linux驱动程序实例

(7页)

8bf4492adfadf20ad0dc9f77427123e1.png

本资源提供全文预览,点击全文预览即可全文预览,如果喜欢文档就下载吧,查找使用更方便哦!

9.9 积分

1、 最简单的设备驱动程序hcllo.c#include #inelude MODULE_LICENSE(nGPLH);static int hello_init(void){printk (KERN_ALERT ,,Hello, Hello, Linux Driver ! \nn);return 0;}static void hello_exit(void){printk(KERN_ALERT nGoodbye, Hello,Linux Driver ! \nH );}module_init(hello_init);module_exit(hello_exit);说明:(1) 前两行为所包含的头文件,位于内核源码的目录下include文件夹下的。(2) 程序中的MODULE_LICENSE ( nGPL");用于声明模块的许可证。⑶module_init()为注册加载时执行的函数,module_exit ()为/注册卸载时执彳亍的函数。传 递给这两个函数的的参数为所要进行初始化的函数的地址(函数名)。⑷模块的编译:使用gcc编译器,与编译普通程序不同的是要在参数-工后指定内核源代码 的目录,2.4的内核模块编译后将生成.o格式的文件。命令行下的编译格式如下:gcc -DMODULE -D KERNEL -I /usr/src/linux-2.4.20/include -c hello.c?其中-DMODULE -D—KERNEL—为两个宏定义,如果文件中包含这两个的宏定义,可以在gCC 编译时不用再指定。文件中宏定义的内容如下:#ifndef —KERNEL—#define —KERNEL—#endif#ifndef MODULE#define MODULE#endif(5)模块的加载与卸载模块加载:在命令行下执行:insmod hello.o»如果成功加载到内核将在终端上显水:Hello, Hello,Linux Driver ! 。查看加载的模块:通过lsmod命令町以杳看内核屮已经加载的模块。模块的卸载:rmmod hello . Oo卸载成功的话将终端显示:Goodbye, Hello, Linux Driver ! ⑹ 驱动模块运行在内核空间,运行时不能依赖于任何函数库和模块连接,所以在耳驱动时 所调用的函数只能是作为内核一部分的函数。内核代码不能实现浮点书运算。2、 -般驱动程序都会实现一个file_operation结构中的函数。如test.c程序中所实现的 file_operation 结构如下: struct, file_operations test_fops={read:read_test,write:writ,open:open_test,release:release_test,};该驱动程序所实现的功能是在内存中开辟-?段空间,write实现了往这段内屮写数据,read 正好相反,是从这段内存中读出数据。(1)头文件和全局变量定义如下:#include#includedinux/f s . h>#includevlinux/mm.h>#inc丄ude#include#include#includedinux/module . h>#include#include//设备号//存放内心空间的指针//当前内存中拥有的数据的字节数MODULE_LICENSE (f,GPLf,);unsigned int test_major = 254; char *str_test;unsigned int count_driver;const unsigned int count_driver_max=255; //容虽的最大值(2)模块初始化函数为:static int test_init_modu1e(void)int result;result = register_chrdev (test_majorz ,,test,,z &test_fops);if (result < 0){printk (KERN_INFOntest: can 11 get major nuir.ber\nn);return result;}prin tk ("registei: ok\nn );if ( (str_test= (char*) kmalloc (256, GFP_KERNEL) ) ==NULL) //屮请 256 字节fl勺内存空MJ {printk(nkmalloc error\nK);return -1;Elseprintk (,Tkamlloc success ! \nn);count_driver = 0;}return 0;}完成了两个工作:一、通过register_chrdev ()向系统注册了一个设备名为test的设备, 设备号由test_major指定。应用层对设备的操作方法由结构test.fops中的指针所指向。 二、通过函数kmallocO申请了一段256字节的内存,用来存放应用层用户所写的数据。⑶open和close的实现,只是一个让数功能:static int open_test (struet inode *inode,struet file *file){MOD_INC_USE_COUNT;return 0;}static int release_test(struct inode *inodez struct file *file){MOD_DEC_USE_COUNT;return 0;}(4) Write两数的实现:static ssize_t write_test(struct file *file,const char *buff size_t count,loff_t 壮){int result;if(councount_driver_max){if((result=copy_from_user(str_test,bu£,count))==0) count_driver=count;elsecount_driver=0;}else{if ( (result=copy_f rom_user (str_test z buf z count_driver_max) ) ==0) count_driver=count_driver_m3x;elsecount_driver=0;}retur 关 键 词: Linux 驱动程序 实例

524d6daf746efaa52c3c71bbfe7ba172.gif  天天文库所有资源均是用户自行上传分享,仅供网友学习交流,未经上传用户书面授权,请勿作他用。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值