如何编写一个Linux字符设备驱动?
2017-08-16 16:46
573 查看
感觉学好驱动的基础,起码要了解驱动编写过程。。。所以就小小的整理了一下。
字符设备驱动的编写
编写字符设备驱动主要完成以下动作:
1. 定义一个结构体static struct file_operations变量,其内定义一些设备的open read write close等控制函数
2. 在结构体外分别实现结构体中定义的这些函数
3. 向结构体中注册或删除驱动模块
注册字符设备:
1. 分配cdev: struct cdev *cdev_alloc(void);
2. 初始化cdev: void cdev_init(struct cdev *cdev, const struct file_operations *fops);
3. 添加cdev: int cdev_add(struct cdev *p, dev_t dev, unsigned count)
初始化时
MKDEV 创建设备号
注册设备号 通过调用register_chrdev_region()函数实现
初始化设备号 调用cdev_init()
注册设备到内核 调用cdev_add()
退出时
删除设备 调用cdev_del()
注销设备号 调用unregister_chrdev_region()
代码如下,亲测有效
字符设备驱动的编写
编写字符设备驱动主要完成以下动作:
1. 定义一个结构体static struct file_operations变量,其内定义一些设备的open read write close等控制函数
2. 在结构体外分别实现结构体中定义的这些函数
3. 向结构体中注册或删除驱动模块
注册字符设备:
1. 分配cdev: struct cdev *cdev_alloc(void);
2. 初始化cdev: void cdev_init(struct cdev *cdev, const struct file_operations *fops);
3. 添加cdev: int cdev_add(struct cdev *p, dev_t dev, unsigned count)
初始化时
MKDEV 创建设备号
注册设备号 通过调用register_chrdev_region()函数实现
初始化设备号 调用cdev_init()
注册设备到内核 调用cdev_add()
退出时
删除设备 调用cdev_del()
注销设备号 调用unregister_chrdev_region()
代码如下,亲测有效
#include <linux/kernel.h> #include <linux/module.h> #include <linux/init.h> #include <linux/fs.h> //struct file_operations #include <linux/cdev.h> //struct cdev #define NUM_OF_MINORS 1 #define HELLO_MAJOR 250 #define DEV_NAME "hello_char_dev" static struct cdev hello_cdev; dev_t hello_devno; static int hello_open(struct inode *pinode, struct file *pfile) { printk(KERN_INFO "[kernel info]%s \n", __func__); return 0; } static struct file_operations hello_ops = { .open = hello_open, }; static int __init hello_init(void) { int ret; //组装设备号 hello_devno = MKDEV(HELLO_MAJOR, 0);//(HELLO_MAJOR << 20 | 0); ret = register_chrdev_region(hello_devno, NUM_OF_MINORS, DEV_NAME); if (ret < 0) { printk(KERN_ERR "[kernel err]%s: register_chrdev_region failed! \n", __func__); return ret; } //初始化设备 //hello_cdev.ops = &hello_ops; cdev_init(&hello_cdev, &hello_ops); //注册设备到内核 ret = cdev_add(&hello_cdev, hello_devno, NUM_OF_MINORS); if (ret < 0) { printk(KERN_ERR "[kernel err]%s: cdev_add failed! \n", __func__); unregister_chrdev_region(hello_devno, NUM_OF_MINORS); return ret; } printk(KERN_INFO "[kernel info]%s \n", __func__); return 0; } static void __exit hello_exit(void) { cdev_del(&hello_cdev); unregister_chrdev_region(hello_devno, NUM_OF_MINORS); printk(KERN_INFO "[kernel info]%s \n", __func__); } module_init(hello_init); module_exit(hello_exit); MODULE_LICENSE("GPL"); MODULE_AUTHOR("jason"); MODULE_DESCRIPTION("this is the simplest module");
相关文章推荐
- linux设备驱动第三篇:如何写一个简单的字符设备驱动?
- Linux字符设备驱动编写基本流程
- 嵌入式Linux字符设备驱动LED驱动编写
- Linux 驱动学习笔记05--字符驱动实例,实现一个共享内存设备的驱动
- linux 一个简单的字符设备驱动例子
- linux驱动编写(虚拟字符设备编写)
- Linux 字符设备驱动开发基础(一)—— 编写简单 LED 设备驱动
- 代码:编写一个简单的字符设备驱动
- 如何搭建一个Linux驱动编写环境(centos)
- 编写Linux并行接口字符设备驱动
- Linux下如何加载一个字符驱动(GPIO)
- Linux字符设备驱动编写流程
- 嵌入式Linux字符设备驱动LED驱动编写
- 利用linux 内核所提供的input子系统编写字符设备驱动的步骤
- linux驱动编写(字符设备编写框架)
- Linux字符设备驱动编写流程
- 如何查看LINUX下的一个USB设备使用的驱动模块
- linux字符设备及其驱动编写简介
- linux设备驱动第三篇:如何实现一个简单的字符设备驱动
- 如何编写linux设备驱动