当前位置: 首页 > news >正文

嵌入式Linux驱动开发基础-1 hello驱动

1:APP打开的文件在内核中如何表示

1.1

        APP 打开文件时,可以得到一个整数,这个整数被称为文件句柄。对于 APP 的每一个文件句柄,在内核里面都有一个“struct file ”与之对应

        当我们使用 open 打开文件时,传入的 flags mode 等参数会被记 录在内核中对应的 struct file 结构体里 (f_flags f_mode)
int open(const char *pathname, int flags, mode_t mode);
例如: fd = open(argv[1], O_RDWR);
       读写文件时,文件的当前偏移地址也会保存在 struct file 结构体的 f_pos 成员里。

1.2 打开字符设备节点时,内核中也有对应的 struct file

        这个结构体中的结构体:struct file_operations *f_op,这是由驱动程序提供的。

        结构体 struct file_operations 的定义如下:包含了open,write,read等文件函数。

2:如何编写驱动程序

1:确定主设备号,也可以让内核分配

2:定义自己的 file_operations 结构体

3:实现对应的 drv_open/drv_read/drv_write 等函数,填入 file_operations 结构体

4:把 file_operations 结构体告诉内核:register_chrdev

5:谁来注册驱动程序啊?得有一个入口函数:安装驱动程序时,就会去调用这个入口函数

6:有入口函数就应该有出口函数:卸载驱动程序时,出口函数调用unregister_chrdev

7:其他完善:提供设备信息,自动创建设备节点:class_create, device_create

3:代码编写

参考 driver/char 中的程序,包含头文件,写框架,传输数据:
驱动中实现 open, read, write, release APP 调用这些函数时,都打印内核信息
APP 调用 write 函数时,传入的数据保存在驱动中
APP 调用 read 函数时,把驱动中保存的数据返回给 APP

3.1:主要代码分析

3.1.1:确定主设备号。

        /* 1. 确定主设备号*/

static int major = 0;       

        //后续在入口函数中,使用下列代码:注册函数进行分配

major = register_chrdev(0, "hello", &hello_drv);

3.1.2:定义自己的 file_operations 结构体

/* 2. 定义自己的file_operations结构体                                              */

static struct file_operations hello_drv = {

    .owner = THIS_MODULE,    //必须存在

    .open = hello_drv_open,     //hello驱动程序的open函数,后续我们需要自己实现这个函数

    .read = hello_drv_read,    //hello驱动程序的read函数,后续我们需要自己实现这个函数

    .write = hello_drv_write,    //hello驱动程序的write函数,后续我们需要自己实现这个函数 

    .release = hello_drv_close, //hello驱动程序的clos函数,后续我们需要自己实现这个函数

};

3.1.3:实现对应的 drv_open/drv_read/drv_write 等函数,  

注:我们现在编写的函数属于驱动函数,那么我们在串口掉用这些函数时,

        串口那边,我们属于APP

        而这些驱动属于 “内核”

        我们串口调用read函数  len = read(fd, buf, 1024);时,我们是需要把kernel_buf的数据读到buf中。所以在这read代码中,我们使用  err = copy_to_user(buf, kernel_buf, MIN(1024, size));这行代码,将kernel_buf写入到buf中。

        我们串口调用write函数 write(fd, argv[2], len);时,是把argv[2]的数据写入到kernel_buf中。所以在这write代码中,我们使用  err = copy_from_user(kernel_buf, buf, MIN(1024, size));这行代码,将buf也就是argv[2]写入到kernel_buf中。

        open函数和close函数只是打印一下内核信息,不做数据处理。

/* 3. 实现对应的open/read/write等函数,填入file_operations结构体                   */

static char kernel_buf[1024];

static ssize_t hello_drv_read(struct file *file, char __user *buf, size_t size, loff_t *offset)

{

    int err;

    printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);

    err = copy_to_user(buf, kernel_buf, MIN(1024, size));

    return MIN(1024, size);

}

static ssize_t hello_drv_write(struct file *file, const char __user *buf, size_t size, loff_t *offset)

{

    int err;

    printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);

    err = copy_from_user(kernel_buf, buf, MIN(1024, size));

    return MIN(1024, size);

}

static int hello_drv_open(struct inode *node, struct file *file)

{

    printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);

    return 0;

}

static int hello_drv_close(struct inode *node, struct file *file)

{

    printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);

    return 0;

}

3.1.4:把 file_operations 结构体告诉内核:register_chrdev

3.1.5. 谁来注册驱动程序啊?得有一个入口函数: 

        3.4和3.5可同时在入口函数中实现,安装驱动程序时,系统去调用这个入口函数,这是直接在入口函数中直接将结构体告诉内核

static int __init hello_init(void)

{

    int err;

    printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);

//3.4也就是这行代码。在入口函数中进行 注册驱动程序

    major = register_chrdev(0, "hello", &hello_drv); /* /dev/hello */

//  下面的代码属于自动创建设备节点,这里先抄着使用

    hello_class = class_create(THIS_MODULE, "hello_class");

    err = PTR_ERR(hello_class);

    if (IS_ERR(hello_class))

    {

        printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);

        unregister_chrdev(major, "hello");

        return -1;

    }

    device_create(hello_class, NULL, MKDEV(major, 0), NULL, "hello"); /* /dev/hello */

    return 0;

}

3.1.6:有入口函数就应该有出口函数:卸载驱动程序时,出口函数调用unregister_chrdev

static void __exit hello_exit(void)

{

    printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);

//销毁创建的节点,以及卸载驱动程序

    device_destroy(hello_class, MKDEV(major, 0));

    class_destroy(hello_class);

    unregister_chrdev(major, "hello");

}

3.1.7:其他完善:提供设备信息,自动创建设备节点:class_create, device_create

/* 7. 其他完善:提供设备信息,自动创建设备节点 */

module_init(hello_init);

module_exit(hello_exit);

MODULE_LICENSE("GPL");

3.2:全部代码

3.2.1:hello_drv.c

#include <linux/module.h>#include <linux/fs.h>
#include <linux/errno.h>
#include <linux/miscdevice.h>
#include <linux/kernel.h>
#include <linux/major.h>
#include <linux/mutex.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
#include <linux/stat.h>
#include <linux/init.h>
#include <linux/device.h>
#include <linux/tty.h>
#include <linux/kmod.h>
#include <linux/gfp.h>/* 1. 确定主设备号                                                                 */
static int major = 0;
static char kernel_buf[1024];
static struct class *hello_class;#define MIN(a, b) (a < b ? a : b)/* 3. 实现对应的open/read/write等函数,填入file_operations结构体                   */
static ssize_t hello_drv_read(struct file *file, char __user *buf, size_t size, loff_t *offset)
{int err;printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);err = copy_to_user(buf, kernel_buf, MIN(1024, size));return MIN(1024, size);
}static ssize_t hello_drv_write(struct file *file, const char __user *buf, size_t size, loff_t *offset)
{int err;printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);err = copy_from_user(kernel_buf, buf, MIN(1024, size));return MIN(1024, size);
}static int hello_drv_open(struct inode *node, struct file *file)
{printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);return 0;
}static int hello_drv_close(struct inode *node, struct file *file)
{printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);return 0;
}/* 2. 定义自己的file_operations结构体                                              */
static struct file_operations hello_drv = {.owner = THIS_MODULE,.open = hello_drv_open,.read = hello_drv_read,.write = hello_drv_write,.release = hello_drv_close,
};/* 4. 把file_operations结构体告诉内核:注册驱动程序                                */
/* 5. 谁来注册驱动程序啊?得有一个入口函数:安装驱动程序时,就会去调用这个入口函数 */
static int __init hello_init(void)
{int err;printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);major = register_chrdev(0, "hello", &hello_drv); /* /dev/hello */hello_class = class_create(THIS_MODULE, "hello_class");err = PTR_ERR(hello_class);if (IS_ERR(hello_class)){printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);unregister_chrdev(major, "hello");return -1;}device_create(hello_class, NULL, MKDEV(major, 0), NULL, "hello"); /* /dev/hello */return 0;
}/* 6. 有入口函数就应该有出口函数:卸载驱动程序时,就会去调用这个出口函数           */
static void __exit hello_exit(void)
{printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);device_destroy(hello_class, MKDEV(major, 0));class_destroy(hello_class);unregister_chrdev(major, "hello");
}/* 7. 其他完善:提供设备信息,自动创建设备节点 */module_init(hello_init);
module_exit(hello_exit);MODULE_LICENSE("GPL");

3.2.2 hello_drv_test.c


#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>/** ./hello_drv_test -w abc* ./hello_drv_test -r*/
int main(int argc, char **argv)
{int fd;char buf[1024];int len;/* 1. 判断参数 */if (argc < 2) {printf("Usage: %s -w <string>\n", argv[0]);printf("       %s -r\n", argv[0]);return -1;}/* 2. 打开文件 */fd = open("/dev/hello", O_RDWR);if (fd == -1){printf("can not open file /dev/hello\n");return -1;}/* 3. 写文件或读文件 */if ((0 == strcmp(argv[1], "-w")) && (argc == 3)){len = strlen(argv[2]) + 1;len = len < 1024 ? len : 1024;write(fd, argv[2], len);}else{len = read(fd, buf, 1024);		buf[1023] = '\0';printf("APP read : %s\n", buf);}close(fd);return 0;
}

3.3:测试

./hello_drv_test -w www.100ask.net // 把字符串“www.100ask.net”发给驱动程序
./hello_drv_test -r // 把驱动中保存的字符串读回

http://www.lqws.cn/news/500833.html

相关文章:

  • 【大模型问题】ms-swift微调时,显存持续增长原因分析与解决方案
  • 【CS创世SD NAND征文】基于全志V3S与CS创世SD NAND的物联网智能路灯网关数据存储方案
  • Nginx负载均衡
  • Docker 数据持久化完全指南:Volume、Bind Mount 与匿名卷
  • OpenCV CUDA模块设备层-----创建一个“常量指针访问器” 的工具函数constantPtr()
  • Docker Compose与私有仓库部署
  • Vue3+TypeScript移动端H5播放器选型指南:M3U8与主流播放器深度解析
  • 聚宽量化——股票时间序列函数
  • 传统消防演练与 VR 消防演练的区别有哪些
  • Unreal5从入门到精通之如何录制360°VR全景视频
  • Python-3-数据结构(列表)
  • Android edge-to-edge兼容适配
  • 监管报送面试回答思路和示例
  • Learning Dynamic Prompts for All-in-One Image Restoration
  • 利用 Python 脚本批量查找并删除指定 IP 的 AWS Lightsail 实例
  • 数据采集合规安全是品牌控价基石
  • 【unitrix】 4.3 左移运算(<<)的实现(shl.rs)
  • Jupyter Notebook 完全指南:从入门到生产力工具
  • 【格与代数系统】示例2
  • 在训练词编码模型使用mask还是自回归,在训练生成大模型采用mask还是自回归?
  • 【格与代数系统】示例
  • linux 下 Doris 单点部署
  • 优化 ArcPy 脚本性能
  • 华为云 Flexus+DeepSeek 征文|基于 CCE 集群部署 Dify 平台工作流:科研论文翻译与 SEO 优化工具的全流程设计实践
  • python中学物理实验模拟:平抛运动和抛物运动
  • 基于 SpringBoot+JSP 的医疗预约与诊断系统设计与实现
  • JavaWeb学习——day8(MVC模式与session、cookie)
  • Node.js特训专栏-实战进阶:7.Express模板引擎选型与使用
  • Java SE - 图书管理系统模拟实现
  • Python csv 模块