【Linux内核驱动】字符设备驱动框架模板
2017-09-05 11:34
459 查看
#include <linux/init.h> #include <linux/module.h> #include <linux/fs.h> #include <linux/cdev.h> #include <linux/kdev_t.h> #include <linux/slab.h> #include <linux/device.h> #define DEVICE_NAME "chrdev_test" #define DEVICE_MAJOR 0 #define MINOR_BASE 0 #define MINOR_NUM 1 MODULE_LICENSE("Dual BSD/GPL"); MODULE_AUTHOR("colorfulshark@hotmail.com"); static int major = DEVICE_MAJOR; static int minor_base = MINOR_BASE; static int minor_num = MINOR_NUM; static dev_t devno; static struct cdev *pcdev; static struct class *test_class; static struct device *test_device; static struct file_operations fops = { .owner = THIS_MODULE, }; #if 0 module_param(major, int, S_IRWXU); module_param(minor_base, int, S_IRWXU); module_param(minor_num, int, S_IRWXU); #endif static void request_devno(void) //申请设备号 { int ret; if(major) { printk(KERN_ALERT "alloc device number static\n"); devno = MKDEV(major, minor_base); ret = register_chrdev_region(devno, minor_num, DEVICE_NAME); } else { printk(KERN_ALERT "alloc device number dynamic\n"); ret = alloc_chrdev_region(&devno, minor_base, minor_num, DEVICE_NAME); printk(KERN_ALERT "alloc major : %d\n", MAJOR(devno)); } if(ret < 0) { printk(KERN_ALERT "register chrdev region failed\n"); } } static void free_devno(void) //释放设备号 { unregister_chrdev_region(devno, minor_num); } static int add_cdev(void) //添加字符设备 { int error; pcdev = cdev_alloc(); if(pcdev == NULL) { goto err_no_mem; } cdev_init(pcdev, &fops); pcdev->owner = THIS_MODULE; error = cdev_add(pcdev, devno, minor_num); if(error) goto err_cdev_add; printk(KERN_ALERT "success to add cdev\n"); return 0; err_no_mem: printk(KERN_ALERT "no enough memory\n"); free_devno(); return -ENOMEM; err_cdev_add: printk(KERN_ALERT "fail to add cdev\n"); free_devno(); return error; } static void del_cdev(void) //删除字符设备 { cdev_del(pcdev); } static void add_dev_node(void) //创建设备节点 { test_class = class_create(THIS_MODULE, "test_class"); test_device = device_create(test_class, NULL, devno, NULL, "chrdev_test"); } static void del_dev_node(void) //删除设备节点 { device_destroy(test_class, devno); class_destroy(test_class); } static int chrdev_init(void) { printk(KERN_ALERT "chrdev init\n"); request_devno(); add_cdev(); add_dev_node(); return 0; } static void chrdev_exit(void) { del_dev_node(); del_cdev(); free_devno(); printk(KERN_ALERT "chrdev exit\n"); } module_init(chrdev_init); module_exit(chrdev_exit);
相关文章推荐
- linux 字符设备驱动框架
- vxWorks字符设备驱动模板
- LINUX内核字符设备驱动模型
- 内核字符设备驱动框架
- Linux内核开发之简单字符设备驱动(上)
- 标准的字符设备驱动的模板
- 一步步理解linux字符设备驱动框架
- rtc驱动框架 与 新的字符设备驱动注册方式
- linux字符设备驱动一般模板
- Linux内核之字符设备驱动
- 11-S3C2440驱动学习(七)嵌入式linux-字符设备的另一种写法及RTC驱动程序分析和字符设备驱动框架总结
- linux字符设备驱动框架理解
- 字符设备驱动-Linux内核异常处理体系结构
- Linux字符设备驱动框架
- linux驱动开发之字符设备框架 -调用过程分析
- Linux驱动(三)字符设备驱动框架
- 标准的字符设备驱动框架
- 第16章 驱动开发之字符设备驱动程序框架
- SylixOS字符设备驱动框架
- linux驱动开发之字符设备框架 - 实例