嵌入式linux/鸿蒙开发板(IMX6ULL)开发(二十一)第一次写驱动程序

简介: 嵌入式linux/鸿蒙开发板(IMX6ULL)开发(二十一)第一次写驱动程序

1.Hello驱动(不涉及硬件操作)


我们选用的内核都是4.x版本,操作都是类似的:

rk3399   linux 4.4.154
rk3288   linux 4.4.154
imx6ul   linux 4.9.88
am3358  linux 4.9.168


也就是说你要用sourceinsight,打开内核源码,在内核源码的工程下继续编写程序。因为在这个编写驱动的过程中要用到很多关于内核的函数。你可以直接去用source工程去看。不要用其他的软件。

把之前的过程重新复现一遍。


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


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

1670934246276.jpg

可以猜测,我们使用open打开文件时,传入的flags、mode等参数会被记录在内核中对应的struct file结构体里(f_flags、f_mode):

int open(const char *pathname, int flags, mode_t mode);


去读写文件时,文件的当前偏移地址也会保存在struct file结构体的f_pos成员里。

1670934264529.jpg


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


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


1670934286484.jpg

1670934293820.jpg

open一个设备节点以后,会返回一个句柄,这个的句柄会在内核中有一个struct file与其对应。flags,以及mode会传参数到struct file中去,并且当使用open打开以后,根据主设备号会在一个叫chadevs[]的数组上找到file_operation,以及其上对应的open,read,write等函数,当app使用read/write的时候,会调用驱动中的write以及read。


结构体struct file_operations的定义如下:

1670934302507.jpg



1.3 请猜猜怎么编写驱动程序


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

② 定义自己的file_operations结构体

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

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

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

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

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


1.4 请不要啰嗦,表演你的代码吧


1.4.1 写驱动程序


参考driver/char中的程序,包含头文件,写框架,传输数据:

A. 驱动中实现open, read, write, release,APP调用这些函数时,都打印内核信息

B. APP调用write函数时,传入的数据保存在驱动中

C.APP调用read函数时,把驱动中保存的数据返回给APP


使用GIT下载所有源码后,本节源码位于如下目录:

01_all_series_quickstart\
05_嵌入式Linux驱动开发基础知识\source\01_hello_drv\hello_drv.c


hello_drv.c源码如下:

01 #include <linux/module.h>
02
03 #include <linux/fs.h>
04 #include <linux/errno.h>
05 #include <linux/miscdevice.h>
06 #include <linux/kernel.h>
07 #include <linux/major.h>
08 #include <linux/mutex.h>
09 #include <linux/proc_fs.h>
10 #include <linux/seq_file.h>
11 #include <linux/stat.h>
12 #include <linux/init.h>
13 #include <linux/device.h>
14 #include <linux/tty.h>
15 #include <linux/kmod.h>
16 #include <linux/gfp.h>
17
18 /* 1. 确定主设备号 */
19 static int major = 0;//
20 static char kernel_buf[1024];
21 static struct class *hello_class;
22
23
24 #define MIN(a, b) (a < b ? a : b)
25
26 /* 3. 实现对应的open/read/write等函数,填入file_operations结构体 */ 
27 static ssize_t hello_drv_read (struct file *file, char __user *buf, size_t size, loff_t *offset)
28 {
29      int err;
30      printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
31      err = copy_to_user(buf, kernel_buf, MIN(1024, size));
32      return MIN(1024, size);
33 }
34
35 static ssize_t hello_drv_write (struct file *file, const char __user *buf, size_t size, loff_t *offset)
36 {
37      int err;
38      printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
39      err = copy_from_user(kernel_buf, buf, MIN(1024, size));
40      return MIN(1024, size);
41 }
42
43 static int hello_drv_open (struct inode *node, struct file *file)
44 {
45      printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
46      return 0;
47 }
48
49 static int hello_drv_close (struct inode *node, struct file *file)
50 {
51      printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
52      return 0;
53 }
54
55 /* 2. 定义自己的file_operations结构体 */
56 static struct file_operations hello_drv = {
57      .owner   = THIS_MODULE,
58      .open    = hello_drv_open,
59      .read    = hello_drv_read,
60      .write   = hello_drv_write,
61      .release = hello_drv_close,
62 };
63
64 /* 4. 把file_operations结构体告诉内核:注册驱动程序 */
65 /* 5. 谁来注册驱动程序啊?得有一个入口函数:安装驱动程序时,就会去调用这个入口函数 */
66 static int __init hello_init(void)
67 {
68      int err;
69
70      printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
71      major = register_chrdev(0, "hello", &hello_drv);  /* /dev/hello */
72
73
74      hello_class = class_create(THIS_MODULE, "hello_class");
75      err = PTR_ERR(hello_class);
76      if (IS_ERR(hello_class)) {
77              printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
78              unregister_chrdev(major, "hello");
79              return -1;
80      }
81
82      device_create(hello_class, NULL, MKDEV(major, 0), NULL, "hello"); /* /dev/hello */
83
84      return 0;
85 }
86
87 /* 6. 有入口函数就有出口函数:卸载驱动程序时就会去调用这个出口函数 */
88 static void __exit hello_exit(void)
89 {
90      printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
91      device_destroy(hello_class, MKDEV(major, 0));
92      class_destroy(hello_class);
93      unregister_chrdev(major, "hello");
94 }
95
96
97 /* 7. 其他完善:提供设备信息,自动创建设备节点 */
98
99 module_init(hello_init);
100 module_exit(hello_exit);
101
102 MODULE_LICENSE("GPL");
103


阅读一个驱动程序,从它的入口函数开始,第66行就是入口函数。它的主要工作就是第71行,向内核注册一个file_operations结构体:hello_drv,这就是字符设备驱动程序的核心。

file_operations结构体hello_drv在第56行定义,里面提供了open/read/write/release成员,应用程序调用open/read/write/close时就会导致这些成员函数被调用。

file_operations结构体hello_drv中的成员函数都比较简单,大多数只是打印而已。要注意的是,驱动程序和应用程序之间传递数据要使用copy_from_user/copy_to_user函数。


1.4.2 写测试程序


测试程序要实现写、读功能:

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


使用GIT下载所有源码后,本节源码位于如下目录:

01_all_series_quickstart\
05_嵌入式Linux驱动开发基础知识\source\01_hello_drv\hello_drv_test.c


hello_drv_test.c源码如下:

01
02 #include <sys/types.h>
03 #include <sys/stat.h>
04 #include <fcntl.h>
05 #include <unistd.h>
06 #include <stdio.h>
07 #include <string.h>
08
09 /*
10  * ./hello_drv_test -w abc
11  * ./hello_drv_test -r
12  */
13 int main(int argc, char **argv)
14 {
15      int fd;
16      char buf[1024];
17      int len;
18
19      /* 1. 判断参数 */
20      if (argc < 2)
21      {
22              printf("Usage: %s -w <string>\n", argv[0]);
23              printf("       %s -r\n", argv[0]);
24              return -1;
25      }
26
27      /* 2. 打开文件 */
28      fd = open("/dev/hello", O_RDWR);
29      if (fd == -1)
30      {
31              printf("can not open file /dev/hello\n");
32              return -1;
33      }
34
35      /* 3. 写文件或读文件 */
36      if ((0 == strcmp(argv[1], "-w")) && (argc == 3))
37      {
38              len = strlen(argv[2]) + 1;
39              len = len < 1024 ? len : 1024;
40              write(fd, argv[2], len);
41      }
42      else
43      {
44              len = read(fd, buf, 1024);
45              buf[1023] = '\0';
46              printf("APP read : %s\n", buf);
47      }
48
49      close(fd);
50
51      return 0;
52 }
53


1.4.3 编写makefile


A. 编写驱动程序的Makefile


驱动程序中包含了很多头文件,这些头文件来自内核,不同的ARM板它的某些头文件可能不同。所以编译驱动程序时,需要指定板子所用的内核的源码路径。

要编译哪个文件?这也需要指定,设置obj-m变量即可 怎么把.c文件编译为驱动程序.ko?这要借助内核的顶层Makefile。


本驱动程序的Makefile内容如下:

01
02 # 1. 使用不同的开发板内核时, 一定要修改KERN_DIR
03 # 2. KERN_DIR中的内核要事先配置、编译, 为了能编译内核, 要先设置下列环境变量:
04 # 2.1 ARCH,          比如: export ARCH=arm64
05 # 2.2 CROSS_COMPILE, 比如: export CROSS_COMPILE=aarch64-linux-gnu-
06 # 2.3 PATH,          比如: export PATH=$PATH:/home/book/100ask_roc-rk3399-pc/ToolChain-6.3.1/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin
07 # 注意: 不同的开发板不同的编译器上述3个环境变量不一定相同,
08 #       请参考各开发板的高级用户使用手册
09
10 KERN_DIR = /home/book/100ask_roc-rk3399-pc/linux-4.4
11
12 all:
13      make -C $(KERN_DIR) M=`pwd` modules
14      $(CROSS_COMPILE)gcc -o hello_drv_test hello_drv_test.c
15
16 clean:
17      make -C $(KERN_DIR) M=`pwd` modules clean
18      rm -rf modules.order
19      rm -f hello_drv_test
20
21 obj-m        += hello_drv.o


先设置好交叉编译工具链,编译好你的板子所用的内核,然后修改Makefile指定内核源码路径,最后即可执行make命令编译驱动程序和测试程序。


B. 上机实验


注意:我们是在Ubuntu中编译程序,但是需要在ARM板子上测试。所以需要把程序放到ARM板子上。

启动单板后,可以通过NFS挂载Ubuntu的某个目录,访问该目录中的程序。


测试示例:

① 在Ubuntu上编译好驱动,并它复制到NFS目录:

$ cp *.ko hello_drv_test ~/nfs_rootfs/


② 在ARM板上测试:

# echo "7 4 1 7" > /proc/sys/kernel/printk  // 打开内核的打印信息,有些板子默认打开了
# ifconfig eth0 192.168.1.10   // 配置ARM板IP,下面是挂载NFS文件系统
// 2.如果使用VMware桥接网络,假设Ubuntu IP为192.168.1.100,使用下面命令挂载NFS
# mount -t nfs -o nolock,vers=3  192.168.1.100:/home/book/nfs_rootfs  /mnt
# cd  /mnt
# insmod hello_drv.ko    // 安装驱动程序
[  293.594910] hello_drv: loading out-of-tree module taints kernel.
[  293.616051] /home/book/source/01_hello_drv/hello_drv.c hello_init line 70
# ls /dev/hello -l        // 驱动程序会生成设备节点
crw-------    1 root     root      236,   0 Jan 18 08:55 /dev/hello
# ./hello_drv_test        // 查看测试程序的用法
Usage: ./hello_drv_test -w <string>
       ./hello_drv_test -r
# ./hello_drv_test -w wiki.100ask.net    // 往驱动程序中写入字符串
[  318.360800] /home/book/source/01_hello_drv/hello_drv.c hello_drv_open line 45
[  318.372570] /home/book/source/01_hello_drv/hello_drv.c hello_drv_write line 38
[  318.382854] /home/book/source/01_hello_drv/hello_drv.c hello_drv_close line 51
# ./hello_drv_test -r                  // 从驱动程序中读出字符串
[  326.177890] /home/book/source/01_hello_drv/hello_drv.c hello_drv_open line 45
[  326.198304] /home/book/source/01_hello_drv/hello_drv.c hello_drv_read line 30
APP read : wiki.100ask.net
[  326.214782] /home/book/source/01_hello_drv/hello_drv.c hello_drv_close line 51


注意:如果安装驱动时提示version magic不匹配,或是污染内核(taint),请参考这些章节更新内核:


第2篇 环境搭建、Linux基本操作、工具使用


《第九章 开发板的第1个驱动程序》


Hello驱动中的一些补充知识

module_init/module_exit的实现

register_chrdev的内部实现

class_destroy/device_create浅析

相关文章
|
1月前
|
Linux Android开发
嵌入式linux中Framebuffer 驱动程序框架分析
嵌入式linux中Framebuffer 驱动程序框架分析
27 0
|
2月前
|
Linux 芯片
Linux 驱动开发基础知识——认识LED驱动程序 (二)
Linux 驱动开发基础知识——认识LED驱动程序 (二)
35 0
Linux 驱动开发基础知识——认识LED驱动程序 (二)
|
2月前
|
Ubuntu Linux 开发工具
Linux下的IMX6ULL——环境搭建与软件安装(一)
Linux下的IMX6ULL——环境搭建与软件安装(一)
67 0
Linux下的IMX6ULL——环境搭建与软件安装(一)
|
2月前
|
Ubuntu Linux 虚拟化
Linux下的IMX6ULL——构建bootloader、内核、文件系统(四)
Linux下的IMX6ULL——构建bootloader、内核、文件系统(四)
76 0
Linux下的IMX6ULL——构建bootloader、内核、文件系统(四)
|
1月前
|
存储 缓存 Linux
探秘Linux块设备驱动程序:成为内核开发大师的第一步
探秘Linux块设备驱动程序:成为内核开发大师的第一步
92 0
|
1月前
|
Linux 编译器 测试技术
探索Linux设备树:硬件描述与驱动程序的桥梁
探索Linux设备树:硬件描述与驱动程序的桥梁
66 0
|
1月前
|
存储 缓存 安全
掌握Linux字符设备驱动程序的核心要点
掌握Linux字符设备驱动程序的核心要点
58 0
|
2月前
|
Linux 编译器 芯片
Linux 驱动开发基础知识—— 具体单板的 LED 驱动程序(五)
Linux 驱动开发基础知识—— 具体单板的 LED 驱动程序(五)
50 0
Linux 驱动开发基础知识—— 具体单板的 LED 驱动程序(五)
|
2月前
|
Linux
Linux 驱动开发基础知识—— LED 驱动程序框架(四)
Linux 驱动开发基础知识—— LED 驱动程序框架(四)
58 0
Linux 驱动开发基础知识—— LED 驱动程序框架(四)
|
2月前
|
Linux
Linux 驱动开发基础知识——编写LED驱动程序(三)
Linux 驱动开发基础知识——编写LED驱动程序(三)
42 0
Linux 驱动开发基础知识——编写LED驱动程序(三)