
制作第一个Linux驱动程序
发布日期:2021-05-08 23:07:57
浏览次数:21
分类:精选文章
本文共 2492 字,大约阅读时间需要 8 分钟。
编写一个简单的Linux驱动程序:从零开始到上线
编写一个Linux驱动程序,实现应用程序能通过系统调用,调用相应驱动函数,驱动程序中仅打印信息,不实现一些特定功能。
1. 创建一个驱动源文件 first_drv.c
#include#include #include #include #include #include #include #include #include #include #include int major; static int first_drv_open(struct inode *inode, struct file *file) { printk("first_drv_open\n"); return 0; } static ssize_t first_drv_read(struct file *file, char __user *buf, size_t count, loff_t *ppos) { printk("first_drv_read\n"); return 0; } static ssize_t first_drv_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos) { printk("first_drv_read\n"); return 0; } struct file_operations first_drv_fileop = { .owner = THIS_MODULE, .open = first_drv_open, .read = first_drv_read, .write = first_drv_write, }; static int first_drv_init(void) { major = register_chrdev(0, "first_drv", &first_drv_fileop); return 0; } static void first_drv_exit(void) { unregister_chrdev(major, "first_drv"); } module_init(first_drv_init); module_exit(first_drv_exit); MODULE_LICENSE("GPL");
2. 创建一个编译驱动的Makefile
KERN_DIR = /home/book/self_learn/01_linux_develop/02_embedded_dir/02_kernel/linux-3.4.2all: make -C $(KERN_DIR) M=`pwd` modulesclean: make -C $(KERN_DIR) M=`pwd` modules clean rm -rf modules.orderobj-m add -first_drv.o
3. 编译并加载驱动
makecp first_drv.ko /lib/modules/$(shell uname -r)/updates/depmod -ainsmod first_drv.ko
4. 手动创建设备节点
mknod /dev/first c 252 0
5. 编写测试程序
#include#include #include #include int main(int argc, char **argv) { int fd; int val = 1; fd = open("/dev/first", O_RDWR); if (fd == -1) { printf("can't open...\n"); exit(EXIT_FAILURE); } read(fd, &val, sizeof(val)); exit(EXIT_SUCCESS);}
6. 优化驱动程序(自动创建设备节点)
static struct class *first_drv_class;static struct class_device *first_drv_class_dev;static int first_drv_init(void) { first_drv_class = class_create(THIS_MODULE, "firstdrv"); if (!first_drv_class) { return -1; } first_drv_class_dev = class_device_create(first_drv_class, NULL, MKDEV(major, 0), NULL, "firstdrv"); if (!first_drv_class_dev) { return -1; } return 0;}static void first_drv_exit(void) { class_device_unregister(first_drv_class_dev); class_destroy(first_drv_class);}
7. 测试驱动程序
通过以上步骤,您可以成功编写并测试一个简单的Linux驱动程序。驱动程序实现了文件操作的基本功能,并通过自动化的设备节点创建,简化了手动操作的复杂性。
发表评论
最新留言
表示我来过!
[***.240.166.169]2025年05月08日 06时46分09秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
linux下解压命令
2023-02-03
Linux下设置Tomcat启动参数
2023-02-03
Linux下读取doc,docx文件
2023-02-03
linux下软件的安装与卸载
2023-02-03
linux下迅雷远程下载服务,在 Linux 下使用迅雷的另一种无入侵方式
2023-02-03
Linux下运行Jmeter压测
2023-02-03
Linux下进程与线程的区别及查询方法
2023-02-03
Linux下进程通信与FIFO操作详解
2023-02-03
Linux下通过ssh访问另一台内网服务器
2023-02-03
Linux下通过端口杀死进程
2023-02-03
linux下配置Java环境变量
2023-02-03
Linux下配置Mysql允许远程访问
2023-02-03
Linux下配置NFS简单步骤
2023-02-03
Linux下配置tomcat,resin,JDK
2023-02-03
Linux下配置无密码登录
2023-02-03
linux下集成脚启动本编写——Rabbitmq mysql redis apache
2023-02-03
Linux下,C++判断指定路径下,是否存在wps打开的文件
2023-02-03
Linux下,Docker出现Cannot connect to the Docker daemon. Is the docker daemon running on this host错误解决办法
2023-02-03
linux下,websocketpp实现长连接,C++代码实现
2023-02-03