linux2.6字符设备驱动开发模板

linux2.6字符设备驱动开发模板

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. #include <linux/init.h>  
  2. #include <linux/module.h>  
  3. #include <linux/fs.h>  
  4. #include <linux/cdev.h>  
  5. #include <linux/device.h>  
  6.   
  7. //=======================字符设备驱动模板开始 ===========================//  
  8. #define CHAR_DEV_DEVICE_NAME   "char_dev"   // 是应当连接到这个编号范围的设备的名字,出现在/proc/devices和sysfs中  
  9. #define CHAR_DEV_NODE_NAME    "char_dev"   // 节点名,出现在/dev中  
  10. #define CHAR_DEV_CLASS_NAME   "char_dev_class"   //出现在/sys/devices/virtual/和/sys/class/中  
  11. struct class *char_dev_class;  // class结构用于自动创建设备结点   
  12. static int major = 0;<span style="white-space:pre">   </span>       // 0表示动态分配主设备号,可以设置成未被系统分配的具体的数字。  
  13. static struct cdev char_dev_cdev;// 定义一个cdev结构  
  14.   
  15. // 进行初始化设置,打开设备,对应应用空间的open 系统调用   
  16. int char_dev_open(struct inode *inode, struct file *filp)  
  17. {  
  18.     //  这里可以进行一些初始化  
  19.     printk("char_dev device open.\n");  
  20.     return 0;  
  21. }  
  22.    
  23. // 释放设备,关闭设备,对应应用空间的close 系统调用  
  24. static int char_dev_release (struct inode *node, struct file *file)  
  25. {  
  26.     //  这里可以进行一些资源的释放  
  27.     printk("char_dev device release.\n");  
  28.     return 0;  
  29. }  
  30. // 实现读功能,读设备,对应应用空间的read 系统调用  
  31. /*__user. 这种注解是一种文档形式, 注意, 一个指针是一个不能被直接解引用的 
  32. 用户空间地址. 对于正常的编译, __user 没有效果, 但是它可被外部检查软件使 
  33. 用来找出对用户空间地址的错误使用.*/  
  34. ssize_t char_dev_read(struct file *file,char __user *buff,size_t count,loff_t *offp)  
  35. {  
  36.     printk("char_dev device read.\n");  
  37.     return 0;  
  38. }  
  39. // 实现写功能,写设备,对应应用空间的write 系统调用  
  40. ssize_t char_dev_write(struct file *file,const char __user *buff,size_t count,loff_t *offp)  
  41. {  
  42.     printk("char_dev device write.\n");  
  43.     return 0;  
  44. }  
  45.    
  46. // 实现主要控制功能,控制设备,对应应用空间的ioctl系统调用  
  47. static int char_dev_ioctl(struct inode *inode,struct file *file,unsigned int cmd,unsigned long arg)  
  48. {    
  49.     printk("char_dev device ioctl.\n");  
  50.     return 0;  
  51. }  
  52.   
  53. //  file_operations 结构体设置,该设备的所有对外接口在这里明确,此处只写出了几常用的  
  54. static struct file_operations char_dev_fops =   
  55. {  
  56.     .owner = THIS_MODULE,  
  57.     .open  = char_dev_open,      // 打开设备   
  58.     .release = char_dev_release, // 关闭设备   
  59.     .read  = char_dev_read,      // 实现设备读功能   
  60.     .write = char_dev_write,     // 实现设备写功能   
  61.     .ioctl = char_dev_ioctl,     // 实现设备控制功能   
  62. };  
  63.   
  64. // 设备建立子函数,被char_dev_init函数调用    
  65. static void char_dev_setup_cdev(struct cdev *dev, int minor, struct file_operations *fops)  
  66. {  
  67.     int err, devno = MKDEV(major, minor);  
  68.     cdev_init(dev, fops);//对cdev结构体进行初始化  
  69.     dev->owner = THIS_MODULE;  
  70.     dev->ops = fops;  
  71.     err = cdev_add(dev, devno, 1);//参数1是应当关联到设备的设备号的数目. 常常是1  
  72.     if(err)  
  73.     {  
  74.         printk(KERN_NOTICE "Error %d adding char_dev %d.\n", err, minor);  
  75.     }  
  76.     printk("char_dev device setup.\n");  
  77. }  
  78.   
  79. //   设备初始化   
  80. static int char_dev_init(void)  
  81. {  
  82.     int result;  
  83.     dev_t dev = MKDEV(major, 0);//将主次编号转换为一个dev_t类型  
  84.     if(major)  
  85.     {  
  86.         // 给定设备号注册  
  87.         result = register_chrdev_region(dev, 1, CHAR_DEV_DEVICE_NAME);//1是你请求的连续设备编号的总数  
  88.         printk("char_dev register_chrdev_region.\n");  
  89.     }  
  90.     else  
  91.     {  
  92.         // 动态分配设备号   
  93.         result = alloc_chrdev_region(&dev, 0, 1, CHAR_DEV_DEVICE_NAME);//0是请求的第一个要用的次编号,它常常是 0  
  94.         printk("char_dev alloc_chrdev_region.\n");  
  95.         major = MAJOR(dev);  
  96.     }  
  97.     if(result < 0)//获取设备号失败返回  
  98.     {  
  99.         printk(KERN_WARNING "char_dev region fail.\n");  
  100.         return result;  
  101.     }  
  102.     char_dev_setup_cdev(&char_dev_cdev, 0, &char_dev_fops);  
  103.     printk("The major of the char_dev device is %d.\n", major);  
  104.     //==== 有中断的可以在此注册中断:request_irq,并要实现中断服务程序 ===//  
  105.     // 创建设备节点  
  106.     char_dev_class = class_create(THIS_MODULE,CHAR_DEV_CLASS_NAME);  
  107.     if (IS_ERR(char_dev_class))  
  108.     {  
  109.         printk("Err: failed in creating char_dev class.\n");  
  110.         return 0;  
  111.     }  
  112.     device_create(char_dev_class, NULL, dev, NULL, CHAR_DEV_NODE_NAME);  
  113.     printk("char_dev device installed.\n");  
  114.     return 0;  
  115. }  
  116.   
  117. // 设备注销   
  118. static void char_dev_cleanup(void)  
  119. {  
  120.     device_destroy(char_dev_class,MKDEV(major, 0));  
  121.     class_destroy(char_dev_class);  
  122.     cdev_del(&char_dev_cdev);//字符设备的注销  
  123.     unregister_chrdev_region(MKDEV(major, 0), 1);//设备号的注销  
  124.     //========  有中断的可以在此注销中断:free_irq  ======//  
  125.     printk("char_dev device uninstalled.\n");  
  126. }  
  127.   
  128. module_init(char_dev_init);//模块初始化接口  
  129. module_exit(char_dev_cleanup);//模块注销接口  
  130. //所有模块代码都应该指定所使用的许可证,该句不能省略,否则模块加载会报错  
  131. MODULE_LICENSE("Dual BSD/GPL");  
  132. MODULE_AUTHOR("Author");  
  133. MODULE_DESCRIPTION("Driver Description");  

Makefile

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. #  
  2. # Makefile for kernel helloworld drivers  
  3. #  
  4. # If KERNELRELEASE is defined, we've been invoked from the  
  5. # kernel build system and can use its language.  
  6. ifneq ($(KERNELRELEASE),)  
  7. obj-m += chardev.o  
  8. # Otherwise we were called directly from the command  
  9. # line; invoke the kernel build system.  
  10. else  
  11.     KERNEL_DIR ?= /lib/modules/$(shell uname -r)/build  
  12.     ANDROID_KERNEL_DIR ?= /home/wzf/amlogic_m1_0427/kernel/  
  13.     PWD := $(shell pwd)  
  14. default:  
  15.     $(MAKE) -C $(ANDROID_KERNEL_DIR) M=$(PWD) modules  
  16. modules:  
  17.     $(MAKE) -C $(KERNEL_DIR) M=$(PWD) modules  
  18. clean:  
  19.     $(MAKE) -C $(KERNEL_DIR) M=$(PWD) clean  
  20. endif  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值