linux设备驱动初学(二)


  1. <div><span style='color: rgb(51, 51, 51); line-height: 25px; font-family: Georgia, "Times New Roman", Times, sans-serif; font-size: 14px;'>#include <linux/init.h></span></div><div><span style='color: rgb(51, 51, 51); line-height: 25px; font-family: Georgia, "Times New Roman", Times, sans-serif; font-size: 14px;'>#include <linux/kernel.h></span></div>#include <linux/module.h> 
  2. #include <linux/types.h> 
  3. #include <linux/fs.h> 
  4. #include <linux/mm.h> 
  5. #include <linux/errno.h> 
  6. #include <asm/segment.h> 
  7. #include <linux/module.h> 
  8. #include <linux/sched.h> 
  9. #include <linux/cdev.h> 
  10. MODULE_LICENSE("Dual BSD/GPL"); 
  11. unsigned int test_major=0; 
  12. static char *book_name = "dissecting Linux Device Drviver"
  13. static int num = 4000; 
  14. static int global_major = 0; 
  15. struct cc_dev_t 
  16.   struct cdev cdev; 
  17. } cc_dev; 
  18. static int cc_read(struct inode *inode,struct file *file,char *buf,int count) 
  19.     printk(KERN_INFO "*******cc_read********\n"); 
  20.     return 0; 
  21. static int cc_write(struct inode *inode,struct file *file,const char *buf,int count) 
  22.   printk(KERN_INFO "*******cc_write*******\n"); 
  23.   return 0; 
  24. static int cc_open(struct inode *inode,struct file *file) 
  25.   printk(KERN_INFO "********cc_open*******\n"); 
  26.   return 0; 
  27. static void cc_release(struct inode *inode,struct file *file) 
  28.   printk(KERN_INFO "*******cc_release*****\n"); 
  29. struct file_operations cc_fops={ 
  30. .owner = THIS_MODULE, 
  31. .read = cc_read, 
  32. .write = cc_write, 
  33. .open = cc_open, 
  34. .release = cc_release, 
  35. }; 
  36. static int __init cc_init(void
  37.   int result,err; 
  38.   dev_t devno = MKDEV(0,0); 
  39.   result = alloc_chrdev_region(&devno,0,1,"cc"); 
  40.   global_major = MAJOR(devno); 
  41.   if(result < 0) 
  42.   { 
  43.     return result; 
  44.   } 
  45.   cdev_init(&cc_dev.cdev,&cc_fops); 
  46.   err = cdev_add(&cc_dev.cdev,devno,1); 
  47.   if(err) 
  48.     printk(KERN_NOTICE "Error"); 
  49.   return 0; 
  50. void cc_exit(void
  51.   cdev_del(&cc_dev.cdev); 
  52.   unregister_chrdev_region(MKDEV(global_major,0),1); 
  53. module_init(cc_init); 
  54. module_exit(cc_exit); 
  55. module_param(num,int,S_IRUGO); 
  56. module_param(book_name,charp,S_IRUGO); 
  57. MODULE_AUTHOR("xyl"); 
  58. MODULE_DESCRIPTION("A simple Hello world Moudle"); 
  59. MODULE_ALIAS("a simplest moudle"); 
  60. 写完驱动部分 make一下 在/proc/devices下面就会有对应的主设备号了 
  61.  
  62. makefile文件 
  63.  
  64.     PWD = $(shell pwd) 
  65.     CC=gcc 
  66.     KERNEL_SRC = /usr/src/linux-source-2.6.38/ 
  67.     obj-m := cc.o 
  68.     module-objs := cc.o 
  69. all:    
  70.     $(MAKE) -C $(KERNEL_SRC)  M=$(PWD) modules 
  71. clean: 
  72.     rm *.ko 
  73.     rm *.o 
  74. 之后在/dev下面创建设备节点  cat /proc/devices获得主设备号  mknod /dev/cc c major minor 
  75.  
  76. 在/dev下面就会有对应的设备节点了  如果想要删除的话直接rm掉 
  77.  
  78. 下面是test.c 
  79.  
  80. #include<stdio.h> 
  81. #include<fcntl.h> 
  82. int main() 
  83.   int myfile; 
  84.   char buffer[100]; 
  85.   int retval; 
  86.   myfile = open("/dev/cc",O_RDWR); 
  87.   if(myfile < 0) 
  88.     printf("Open filed /n"); 
  89.   write(myfile,"hello_world",sizeof("hello_world")); 
  90.   retval = read(myfile,buffer,100); 
  91.   buffer[retval] = 1; 
  92.   printf("redponse:%s/n",buffer); 
  93.   close(myfile); 
  94. 其中close对应驱动中的release.上面的 write read 内容就随便写啦,因为我驱动中并没有实现。 
  95.  
  96. 编译一下执行,看内核输出信息dmesg 如下 
  97.  
  98. [ 5096.422497] ********cc_open******* 
  99. [ 5096.422512] *******cc_write******* 
  100. [ 5096.422519] *******cc_read******** 
  101. [ 5096.422572] *******cc_release***** 
  102. 这样就说明调用成功了 

Python网络爬虫与推荐算法新闻推荐平台:网络爬虫:通过Python实现新浪新闻的爬取,可爬取新闻页面上的标题、文本、图片、视频链接(保留排版) 推荐算法:权重衰减+标签推荐+区域推荐+热点推荐.zip项目工程资源经过严格测试可直接运行成功且功能正常的情况才上传,可轻松复刻,拿到资料包后可轻松复现出一样的项目,本人系统开发经验充足(全领域),有任何使用问题欢迎随时与我联系,我会及时为您解惑,提供帮助。 【资源内容】:包含完整源码+工程文件+说明(如有)等。答辩评审平均分达到96分,放心下载使用!可轻松复现,设计报告也可借鉴此项目,该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的。 【提供帮助】:有任何使用问题欢迎随时与我联系,我会及时解答解惑,提供帮助 【附带帮助】:若还需要相关开发工具、学习资料等,我会提供帮助,提供资料,鼓励学习进步 【项目价值】:可用在相关项目设计中,皆可应用在项目、毕业设计、课程设计、期末/期中/大作业、工程实训、大创等学科竞赛比赛、初期项目立项、学习/练手等方面,可借鉴此优质项目实现复刻,设计报告也可借鉴此项目,也可基于此项目来扩展开发出更多功能 下载后请首先打开README文件(如有),项目工程可直接复现复刻,如果基础还行,也可在此程序基础上进行修改,以实现其它功能。供开源学习/技术交流/学习参考,勿用于商业用途。质量优质,放心下载使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值