linux设备驱动初学(二)

来源:互联网 发布:lte网优日常优化面试 编辑:程序博客网 时间:2024/06/08 18:23

[cpp] view plaincopyprint?
  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,constchar *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. 这样就说明调用成功了 

原创粉丝点击