赞
踩
即新功能源码与内核其它代码一起编译进uImage文件内
即新功能源码与内核其它源码不一起编译,而是独立编译成内核的插件(被称为内核模块)文件.ko
module_param(name, type, perm); // 将指定的全局变量设置成模块参数
name:全局变量名
type:
使用符号 实际类型 传参方式
bool bool insmod xxx.ko 变量名=0 或 1
invbool bool insmod xxx.ko 变量名=0 或 1
charp char * insmod xxx.ko 变量名="字符串内容"
short short insmod xxx.ko 变量名=数值
int int insmod xxx.ko 变量名=数值
long long insmod xxx.ko 变量名=数值
ushort unsigned short insmod xxx.ko 变量名=数值
uint unsigned int insmod xxx.ko 变量名=数值
ulong unsigned long insmod xxx.ko 变量名=数值
perm:给对应文件 /sys/module/name/parameters/变量名 指定操作权限
#define S_IRWXU 00700
#define S_IRUSR 00400
#define S_IWUSR 00200
#define S_IXUSR 00100
#define S_IRWXG 00070
#define S_IRGRP 00040
#define S_IWGRP 00020
#define S_IXGRP 00010
#define S_IRWXO 00007
#define S_IROTH 00004
#define S_IWOTH 00002 //不要用 编译出错
#define S_IXOTH 00001
module_param_array(name, type, &num, perm);
name、type、perm同module_param,type指数组中元素的类型
&num:存放数组大小变量的地址,可以填NULL(确保传参个数不越界)
传参方式 insmod xxx.ko 数组名=元素值0,元素值1,...元素值num-1
- #include <linux/module.h>
- #include <linux/kernel.h>
-
- int gx = 10;
- char* gstr = "hello";
- int garr[5] = {1, 2, 3, 4, 5};
-
- module_param(gx, int, 0664);
- module_param(gstr, charp, 0664);
- module_param_array(garr, int, NULL, 0664);
-
-
- int __init testparam_init(void)
- {
- int i = 0;
- printk("gx = %d\n", gx);
- printk("gstr = %s\n", gstr);
-
- for (i = 0; i < 5; i++) {
- printk("%d ", garr[i]);
- }
- printk("\n");
- return 0;
- }
-
- void __exit testparam_exit(void)
- {
- printk("testparam will exit\n");
- }
-
- MODULE_LICENSE("GPL");
-
- module_init(testparam_init);
- module_exit(testparam_exit);
插ko:
- shrek@ubuntu16:~/share/mydrivercode$ sudo insmod ./testparam.ko
- shrek@ubuntu16:~/share/mydrivercode$ dmesg
- [ 1167.269738] gx = 10
- [ 1167.269739] gstr = hello
- [ 1167.269739] 1
- [ 1167.269739] 2
- [ 1167.269740] 3
- [ 1167.269740] 4
- [ 1167.269740] 5
- shrek@ubuntu16:~/share/mydrivercode$ sudo insmod ./testparam.ko gx=1000 gstr="hi" garr=5,6,7,8,9
- shrek@ubuntu16:~/share/mydrivercode$ sudo dmesg
- [ 1145.753620] gx = 1000
- [ 1145.753621] gstr = hi
- [ 1145.753622] 5
- [ 1145.753622] 6
- [ 1145.753622] 7
- [ 1145.753622] 8
- [ 1145.753622] 9
既然内核模块的代码与其它内核代码共用统一的运行环境,也就是说模块只是存在形式上独立,运行上其实和内核其它源码是一个整体,它们隶属于同一个程序,因此一个模块或内核其它部分源码应该可以使用另一个模块的一些全局特性。
一个模块中这些可以被其它地方使用的名称被称为导出符号,所有导出符号被填在同一个表中这个表被称为符号表。
最常用的可导出全局特性为全局变量和函数
查看符号表的命令:nm
nm查看elf格式的可执行文件或目标文件中包含的符号表,用法:
nm 文件名 (可以通过man nm查看一些字母含义)
两个用于导出模块中符号名称的宏:
EXPORT_SYMBOL(函数名或全局变量名)
EXPORT_SYMBOL_GPL(函数名或全局变量名) 需要GPL许可证协议验证
使用导出符号的地方,需要对这些符号进行extern声明后才能使用这些符号
B模块使用了A模块导出的符号,此时称B模块依赖于A模块,则:
1. 编译次序:先编译模块A,再编译模块B,当两个模块源码在不同目录时,需要:i. 先编译导出符号的模块A ii. 拷贝A模块目录中的Module.symvers到B模块目录 iii. 编译使用符号的模块B。否则编译B模块时有符号未定义错误
2. 加载次序:先插入A模块,再插入B模块,否则B模块插入失败
3. 卸载次序:先卸载B模块,在卸载A模块,否则A模块卸载失败
补充说明:
内核符号表(直接当文本文件查看)
运行时可查看 /proc/kallsyms
编译后可查看 /boot/System.map
moduleA.c:
- #include <linux/module.h>
- #include <linux/kernel.h>
-
- int gx = 10;
- EXPORT_SYMBOL(gx);
-
- int __init modulea_init(void)
- {
- printk("In moduleA init, gx = %d\n", gx);
- return 0;
- }
-
- void __exit modulea_exit(void)
- {
- printk("modulea will exit\n");
- }
-
- MODULE_LICENSE("GPL");
-
- module_init(modulea_init);
- module_exit(modulea_exit);
moduleB.c:
- #include <linux/module.h>
- #include <linux/kernel.h>
-
- extern int gx;
-
- int __init moduleb_init(void)
- {
- printk("In moduleB init, gx = %d\n", gx);
- return 0;
- }
-
- void __exit moduleb_exit(void)
- {
- printk("moduleb will exit\n");
- }
-
- MODULE_LICENSE("GPL");
-
- module_init(moduleb_init);
- module_exit(moduleb_exit);
Makefile(注意编译顺序):
- ifeq ($(KERNELRELEASE),)
-
- ifeq ($(ARCH),arm)
- KERNELDIR ?= /home/shrek/Linux_4412/kernel/linux-3.14
- ROOTFS ?= /opt/4412/rootfs
- else
- KERNELDIR ?= /lib/modules/$(shell uname -r)/build
- endif
- PWD := $(shell pwd)
-
- modules:
- $(MAKE) -C $(KERNELDIR) M=$(PWD) modules
-
- modules_install:
- $(MAKE) -C $(KERNELDIR) M=$(PWD) modules INSTALL_MOD_PATH=$(ROOTFS) modules_install
-
- clean:
- rm -rf *.o *.ko .*.cmd *.mod.* modules.order Module.symvers .tmp_versions
-
- else
- obj-m += moduleA.o
-
- obj-m += moduleB.o
-
- endif
-
插ko(注意插入顺序和卸载顺序):
- shrek@ubuntu16:~/share/mydrivercode/twomodules$ sudo insmod ./moduleA.ko
- shrek@ubuntu16:~/share/mydrivercode/twomodules$ sudo insmod ./moduleB.ko
- shrek@ubuntu16:~/share/mydrivercode/twomodules$ sudo dmesg
- [ 8145.865709] In moduleA init, gx = 10
- [ 8147.510935] In moduleB init, gx = 10
- shrek@ubuntu16:~/share/mydrivercode/twomodules$ sudo rmmod moduleB
- shrek@ubuntu16:~/share/mydrivercode/twomodules$ sudo rmmod moduleA
为了彻底解决一个应用程序出错不影响系统和其它app的运行,操作系统给每个app一个独立的假想的地址空间,这个假想的地址空间被称为虚拟地址空间(也叫逻辑地址),操作系统也占用其中固定的一部分,32位Linux的虚拟地址空间大小为4G,并将其划分两部分:
1. 0~3G 用户空间 :每个应用程序只能使用自己的这份虚拟地址空间
2. 3G~4G 内核空间:内核使用的虚拟地址空间,应用程序不能直接使用这份地址空间,但可以通过一些系统调用函数与其中的某些空间进行数据通信
实际内存操作时,需要将虚拟地址映射到实际内存的物理地址,然后才进行实际的内存读写
有开始有结束总体顺序执行的一段独立代码,又被称为代码上下文
计算机系统中的执行流的分类:
任务流--任务上下文(都参与CPU时间片轮转,都有任务五状态:就绪态 运行态 睡眠态 僵死态 暂停态)
1. 进程
2. 线程
内核线程:内核创建的线程
应用线程:应用进程创建的线程
异常流--异常上下文
1. 中断
2. 其它异常
应用编程可能涉及到的执行流:
1. 进程
2. 线程
内核编程可能涉及到的执行流:
1. 应用程序自身代码运行在用户空间,处于用户态 ----------------- 用户态app
2. 应用程序正在调用系统调用函数,运行在内核空间,处于内核态,即代码是内核代码但处于应用执行流(即属于一个应用进程或应用线程) ----------------- 内核态app
3. 一直运行于内核空间,处于内核态,属于内核内的任务上下文 ----------------- 内核线程
4. 一直运行于内核空间,处于内核态,专门用来处理各种异常 ----------------- 异常上下文
不同点 | 内核模块 | 应用程序 |
---|---|---|
API来源 | 不能使用任何库函数 | 各种库函数均可以使用 |
运行空间 | 内核空间 | 用户空间 |
运行权限 | 特权模式运行 | 非特权模式运行 |
编译方式 | 静态编译进内核镜像或编译特殊的ko文件 | elf格式的应用程序可执行文件 |
运行方式 | 模块中的函数在需要时被动调用 | 从main开始顺序执行 |
入口函数 | init_module | main |
退出方式 | cleanup_module | main函数返回或调用exit |
浮点支持 | 一般不涉及浮点运算,因此printk不支持浮点数据 | 支持浮点运算,printf可以打印浮点数据 |
并发考虑 | 需要考虑多种执行流并发的竞态情况 | 只需考虑多任务并行的竞态 |
程序出错 | 可能会导致整个系统崩溃 | 只会让自己崩溃 |
大部分API函数包含的头文件在include/linux目录下,因此:
1. 首先在 include/linux 查询指定函数:grep 名称 ./ -r -n
2. 找不到则更大范围的 include 目录下查询,命令同上
linux的文件种类:
1. -:普通文件
2. d:目录文件
3. p:管道文件
4. s:本地socket文件
5. l:链接文件
6. c:字符设备
7. b:块设备
Linux内核按驱动程序实现模型框架的不同,将设备分为三类:
1. 字符设备:按字节流形式进行数据读写的设备,一般情况下按顺序访问,数据量不大,一般不设缓存
2. 块设备:按整块进行数据读写的设备,最小的块大小为512字节(一个扇区),块的大小必须是扇区的整数倍,Linux系统的块大小一般为4096字节,随机访问,设缓存以提高效率
3. 网络设备:针对网络数据收发的设备
内核中同类设备的区分
内核用设备号来区分同类里不同的设备,设备号是一个无符号32位整数,数据类型为dev_t,设备号分为两部分:
1. 主设备号:占高12位,用来表示驱动程序相同的一类设备
2. 次设备号:占低20位,用来表示被操作的哪个具体设备
应用程序打开一个设备文件时,通过设备号来查找定位内核中管理的设备。
MKDEV宏用来将主设备号和次设备号组合成32位完整的设备号,用法:
- dev_t devno;
- int major = 251; // 主设备号
- int minor = 2; // 次设备号
- devno = MKDEV(major,minor);
MAJOR宏用来从32位设备号中分离出主设备号,用法:
- dev_t devno = MKDEV(249,1);
- int major = MAJOR(devno);
MINOR宏用来从32位设备号中分离出次设备号,用法:
- dev_t devno = MKDEV(249,1);
- int minor = MINOR(devno);
如果已知一个设备的主次设备号,应用层指定好设备文件名,那么可以用mknod命令在/dev目录创建代表这个设备的文件,即此后应用程序对此文件的操作就是对其代表的设备操作,mknod用法如下:
- @ cd /dev
- @ mknod 设备文件名 设备种类(c为字符设备,b为块设备) 主设备号 次设备号 //ubuntu下需加sudo执行
在应用程序中如果要创建设备可以调用系统调用函数mknod,其原型如下:
int mknod(const char *pathname, mode_t mode, dev_t dev);
pathname:带路径的设备文件名,无路径默认为当前目录,一般都创建在/dev下
mode:文件权限 位或 S_IFCHR/S_IFBLK
dev:32位设备号
返回值:成功为0,失败-1
字符驱动开发的第一步是通过模块的入口函数向内核添加本设备驱动的代码框架,主要完成:
1. 申请设备号
2. 定义、初始化、向内核添加代表本设备的结构体元素
int register_chrdev_region(dev_t from, unsigned count, const char *name)
功能:手动分配设备号,先验证设备号是否被占用,如果没有则申请占用该设备号
参数:
from:自己指定的设备号
count:申请的设备数量
name:/proc/devices文件中与该设备对应的名字,方便用户层查询主设备号
返回值:
成功为0,失败负数,绝对值为错误码
int alloc_chrdev_region(dev_t *dev, unsigned baseminor, unsigned count, const char *name)
功能:动态分配设备号,查询内核里未被占用的设备号,如果找到则占用该设备号
参数:
dev:分配设备号成功后用来存放分配到的设备号
baseminior:起始的次设备号,一般为0
count:申请的设备数量
name:/proc/devices文件中与该设备对应的名字,方便用户层查询主次设备号
返回值:
成功为0,失败负数,绝对值为错误码
分配成功后在/proc/devices 可以查看到申请到主设备号和对应的设备名,mknod时参数可以参考查到的此设备信息
void unregister_chrdev_region(dev_t from, unsigned count)
功能:释放设备号
参数:
from:已成功分配的设备号将被释放
count:申请成功的设备数量
释放后 /proc/devices 文件对应的记录消失
- #include <linux/module.h>
- #include <linux/kernel.h>
- #include <linux/fs.h>
-
- int major = 11;
- int minor = 0;
- int mychar_num = 1;
-
- int __init mychar_init(void)
- {
- int ret = 0;
- dev_t devno = MKDEV(major, minor);
-
- ret = register_chrdev_region(devno, mychar_num, "mychar");
- if (ret) {
- ret = alloc_chrdev_region(&devno, minor, mychar_num, "mychar");
- if (ret) {
- printk("get devno faile\n");
- return -1;
- }
- major = MAJOR(devno);
- }
-
- return 0;
- }
-
- void __exit mychar_exit(void)
- {
- dev_t devno = MKDEV(major, minor);
- unregister_chrdev_region(devno, mychar_num);
- }
-
-
- MODULE_LICENSE("GPL");
-
- module_init(mychar_init);
- module_exit(mychar_exit);
插ko之后可通过 /proc/devices 查看:
- shrek@ubuntu16:~/share/mydrivercode$ cat /proc/devices | grep mychar
- 11 mychar
void cdev_init(struct cdev *cdev, const struct file_operations *fops)
cdev结构体
- struct cdev
- {
- struct kobject kobj;//表示该类型实体是一种内核对象
- struct module *owner;//填THIS_MODULE,表示该字符设备从属于哪个内核模块
- const struct file_operations *ops;//指向空间存放着针对该设备的各种操作函数地址
- struct list_head list;//链表指针域
- dev_t dev;//设备号
- unsigned int count;//设备数量
- };
1. 直接定义:定义结构体全局变量
2. 动态申请:struct cdev * cdev_alloc()
file_operations结构体
- struct file_operations
- {
- struct module *owner; //填THIS_MODULE,表示该结构体对象从属于哪个内核模块
- int (*open) (struct inode *, struct file *); //打开设备
- int (*release) (struct inode *, struct file *); //关闭设备
- ssize_t (*read) (struct file *, char __user *, size_t, loff_t *); //读设备
- ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *); //写设备
- loff_t (*llseek) (struct file *, loff_t, int); //定位
- long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);//读写设备参数,读设备状态、控制设备
- unsigned int (*poll) (struct file *, struct poll_table_struct *); //POLL机制,实现多路复用的支持
- int (*mmap) (struct file *, struct vm_area_struct *); //映射内核空间到用户层
- int (*fasync) (int, struct file *, int); //信号驱动
- //......
- };
一般定义一个struct file_operations类型的全局变量并用自己实现各种操作函数名对其进行初始化
int cdev_add(struct cdev *p, dev_t dev, unsigned int count)
功能:将指定字符设备添加到内核
参数:
p:指向被添加的设备
dev:设备号
count:设备数量,一般填1
void cdev_del(struct cdev *p)
功能:从内核中移除一个字符设备
参数:
p:指向被移除的字符设备
字符设备驱动开发步骤:
1. 如果设备有自己的一些控制数据,则定义一个包含struct cdev cdev成员的结构体struct mydev,其它成员根据设备需求,设备简单则直接用struct cdev
2. 定义一个struct mydev或struct cdev的全局变量来表示本设备;也可以定义一个struct mydev或struct cdev的全局指针(记得在init时动态分配)
3. 定义三个全局变量分别来表示主设备号、次设备号、设备数
4. 定义一个struct file_operations结构体变量,其owner成员置成THIS_MODULE
5. module init函数流程:a. 申请设备号 b. 如果是全局设备指针则动态分配代表本设备的结构体元素 c. 初始化struct cdev成员 d. 设置struct cdev的owner成员为THIS_MODULE e. 添加字符设备到内核
6. module exit函数:a. 注销设备号 b. 从内核中移除struct cdev c. 如果如果是全局设备指针则释放其指向空间
7. 编写各个操作函数并将函数名初始化给struct file_operations结构体变量
验证操作步骤:
1. 编写驱动代码mychar.c
2. make生成ko文件
3. insmod内核模块
4. 查阅字符设备用到的设备号(主设备号):cat /proc/devices | grep 申请设备号时用的名字
5. 创建设备文件(设备节点) : mknod /dev/??? c 上一步查询到的主设备号 代码中指定初始次设备号
6. 编写app验证驱动(testmychar_app.c)
7. 编译运行app,dmesg命令查看内核打印信息
mychar.c
- #include <linux/module.h>
- #include <linux/kernel.h>
- #include <linux/fs.h>
- #include <linux/cdev.h>
-
- int major = 11;
- int minor = 0;
- int mychar_num = 1;
-
- struct cdev mydev;
-
- int mychar_open(struct inode* pnode, struct file* pfile)
- {
- printk("mychar_open is called\n");
- return 0;
- }
-
- int mychar_close(struct inode* pnode, struct file* pfile)
- {
- printk("mychar_close is called\n");
- return 0;
- }
-
- struct file_operations myops = {
- .owner = THIS_MODULE,
- .open = mychar_open,
- .release = mychar_close,
- };
-
- int __init mychar_init(void)
- {
- int ret = 0;
- dev_t devno = MKDEV(major, minor);
-
- /* 申请设备号 */
- ret = register_chrdev_region(devno, mychar_num, "mychar");
- if (ret) {
- ret = alloc_chrdev_region(&devno, minor, mychar_num, "mychar");
- if (ret) {
- printk("get devno failed\n");
- return -1;
- }
- major = MAJOR(devno);
- }
-
- /* 给struct cdev对象指定操作函数集 */
- cdev_init(&mydev, &myops);
-
- /* 给struct cdev对象添加到内核对应的数据结构里 */
- mydev.owner = THIS_MODULE;
- cdev_add(&mydev, devno, mychar_num);
-
- return 0;
- }
-
- void __exit mychar_exit(void)
- {
- dev_t devno = MKDEV(major, minor);
- cdev_del(&mydev);
- unregister_chrdev_region(devno, mychar_num);
- }
-
-
- MODULE_LICENSE("GPL");
-
- module_init(mychar_init);
- module_exit(mychar_exit);
test.c
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <unistd.h>
- #include <stdio.h>
-
- int main(int argc, char* argv[])
- {
- int fd = -1;
-
- if (argc < 2) {
- printf("The argument is too few\n");
- return 1;
- }
-
- fd = open(argv[1], O_RDONLY);
- if (fd < 0) {
- printf("open %s failed\n", argv[1]);
- return 2;
- }
-
- close(fd);
- fd = -1;
- return 0;
- }
测试结果
- shrek@ubuntu16:~/share/mydrivercode$ sudo mknod /dev/mydev c 11 0
- shrek@ubuntu16:~/share/mydrivercode$ sudo insmod ./mychar.ko
- shrek@ubuntu16:~/share/mydrivercode$ ./a.out /dev/mydev
- shrek@ubuntu16:~/share/mydrivercode$ dmesg
- [11215.776355] mychar_open is called
- [11215.776358] mychar_close is called
内核中记录文件元信息的结构体
- struct inode
- {
- //....
- dev_t i_rdev; // 设备号
- struct cdev *i_cdev; // 如果是字符设备才有此成员,指向对应设备驱动程序中的加入系统的struct cdev对象
- //....
- }
- /*
- 1. 内核中每个该结构体对象对应着一个实际文件,一对一
- 2. open一个文件时如果内核中该文件对应的inode对象已存在则不再创建,不存在才创建
- 3. 内核中用此类型对象关联到对此文件的操作函数集(对设备而言就是关联到具体驱动代码)
- */
读写文件内容过程中用到的一些控制性数据组合而成的对象------文件操作引擎(文件操控器)
- struct file
- {
- //...
- mode_t f_mode; // 不同用户的操作权限,驱动一般不用
- loff_t f_pos; // position 数据位置指示器,需要控制数据开始读写位置的设备有用
- unsigned int f_flags; // open时的第二个参数flags存放在此,驱动中常用
- struct file_operations *f_op; // open时从struct inode中i_cdev的对应成员获得地址,驱动开发中用来协助理解工作原理,内核中使用
- void *private_data; // 本次打开文件的私有数据,驱动中常来在几个操作函数间传递共用数据
- struct dentry *f_dentry; // 驱动中一般不用,除非需要访问对应文件的inode,用法flip->f_dentry->d_inode
- int refcnt; // 引用计数,保存着该对象地址的位置个数,close时发现refcnt为0才会销毁该struct file对象
- //...
- };
- /*
- 1. open函数被调用成功一次,则创建一个该对象,因此可以认为一个该类型的对象对应一次指定文件的操作
- 2. open同一个文件多次,每次open都会创建一个该类型的对象
- 3. 文件描述符数组中存放的地址指向该类型的对象
- 4. 每个文件描述符都对应一个struct file对象的地址
- */
- int (*open) (struct inode *, struct file *); //打开设备
- /*
- 指向函数一般用来对设备进行硬件上的初始化,对于一些简单的设备该函数只需要return 0,对应open系统调用,是open系统调用函数实现过程中调用的函数,
- */
-
- int (*release) (struct inode *, struct file *); //关闭设备
- /*
- ,指向函数一般用来对设备进行硬件上的关闭操作,对于一些简单的设备该函数只需要return 0,对应close系统调用,是close系统调用函数实现过程中调用的函数
- */
-
- ssize_t (*read) (struct file *, char __user *, size_t, loff_t *); //读设备
- /*
- 指向函数用来将设备产生的数据读到用户空间,对应read系统调用,是read系统调用函数实现过程中调用的函数
- */
-
- ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *); //写设备
- /*
- 指向函数用来将用户空间的数据写进设备,对应write系统调用,是write系统调用函数实现过程中调用的函数
- */
-
- loff_t (*llseek) (struct file *, loff_t, int); //数据操作位置的定位
- /*
- 指向函数用来获取或设置设备数据的开始操作位置(位置指示器),对应lseek系统调用,是lseek系统调用函数实现过程中调用的函数
- */
-
-
- long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);//读写设备参数,读设备状态、控制设备
- /*
- 指向函数用来获取、设置设备一些属性或设备的工作方式等非数据读写操作,对应ioctl系统调用,是ioctl系统调用函数实现过程中调用的函数
- */
-
- unsigned int (*poll) (struct file *, struct poll_table_struct *);//POLL机制,实现对设备的多路复用方式的访问
- /*
- 指向函数用来协助多路复用机制完成对本设备可读、可写数据的监控,对应select、poll、epoll_wait系统调用,是select、poll、epoll_wait系统调用函数实现过程中调用的函数
- */
-
- int (*fasync) (int, struct file *, int); //信号驱动
- /*
- 指向函数用来创建信号驱动机制的引擎,对应fcntl系统调用的FASYNC标记设置,是fcntl系统调用函数FASYNC标记设置过程中调用的函数
- */
ssize_t xxx_read(struct file *filp, char __user *pbuf, size_t count, loff_t *ppos);
完成功能:读取设备产生的数据
参数:
filp:指向open产生的struct file类型的对象,表示本次read对应的那次open
pbuf:指向用户空间一块内存,用来保存读到的数据
count:用户期望读取的字节数
ppos:对于需要位置指示器控制的设备操作有用,用来指示读取的起始位置,读完后也需要变更位置指示器的指示位置
返回值:
本次成功读取的字节数,失败返回-1
内核空间拷贝到用户空间
put_user(x, ptr) // 宏
x:char、int类型的简单变量名
unsigned long copy_to_user (void __user * to, const void * from, unsigned long n)
成功为返回0,失败非0
ssize_t xxx_write (struct file *filp, const char __user *pbuf, size_t count, loff_t *ppos);
完成功能:向设备写入数据
参数:
filp:指向open产生的struct file类型的对象,表示本次write对应的那次open
pbuf:指向用户空间一块内存,用来保存被写的数据
count:用户期望写入的字节数
ppos:对于需要位置指示器控制的设备操作有用,用来指示写入的起始位置,写完后也需要变更位置指示器的指示位置
返回值:
本次成功写入的字节数,失败返回-1
用户空间拷贝到内核空间
get_user(x, ptr) // 宏
x:char、int类型的简单变量名
unsigned long copy_from_user (void * to, const void __user * from, unsigned long n)
成功为返回0,失败非0
mychar.c
- #include <linux/module.h>
- #include <linux/kernel.h>
- #include <linux/fs.h>
- #include <linux/cdev.h>
- #include <linux/uaccess.h>
-
- #define BUF_LEN 100
-
- int major = 11;
- int minor = 0;
- int mychar_num = 1;
-
- struct cdev mydev;
-
- char mydev_buf[BUF_LEN];
- int curlen = 0;
-
- int mychar_open(struct inode* pnode, struct file* pfile)
- {
- printk("mychar_open is called\n");
- return 0;
- }
-
- int mychar_close(struct inode* pnode, struct file* pfile)
- {
- printk("mychar_close is called\n");
- return 0;
- }
-
- ssize_t mychar_read(struct file* pfile, char __user *puser, size_t count, loff_t* p_pos)
- {
- int size = 0;
- int ret = 0;
-
- if (count > curlen) {
- size = curlen;
- } else {
- size = count;
- }
-
- ret = copy_to_user(puser, mydev_buf, size);
- if (ret) {
- printk("copy_to_user failed\n");
- return -1;
- }
-
- memcpy(mydev_buf, mydev_buf + size, curlen - size); // 将未读走的数据前移至buf头
-
- curlen = curlen - size;
- return size;
- }
-
- ssize_t mychar_write(struct file* pfile, const char __user *puser, size_t count, loff_t* p_pos)
- {
- int size = 0;
- int ret = 0;
-
- if (count > BUF_LEN - curlen) {
- size = BUF_LEN - curlen;
- } else {
- size = count;
- }
-
- ret = copy_from_user(mydev_buf + curlen, puser, size);
- if (ret) {
- printk("copy_from_user failed\n");
- return -1;
- }
-
- curlen = curlen + size;
- return size;
- }
-
- struct file_operations myops = {
- .owner = THIS_MODULE,
- .open = mychar_open,
- .release = mychar_close,
- .read = mychar_read,
- .write = mychar_write,
- };
-
- int __init mychar_init(void)
- {
- int ret = 0;
- dev_t devno = MKDEV(major, minor);
-
- /* 申请设备号 */
- ret = register_chrdev_region(devno, mychar_num, "mychar");
- if (ret) {
- ret = alloc_chrdev_region(&devno, minor, mychar_num, "mychar");
- if (ret) {
- printk("get devno failed\n");
- return -1;
- }
- major = MAJOR(devno);
- }
-
- /* 给struct cdev对象指定操作函数集 */
- cdev_init(&mydev, &myops);
-
- /* 给struct cdev对象添加到内核对应的数据结构里 */
- mydev.owner = THIS_MODULE;
- cdev_add(&mydev, devno, mychar_num);
-
- return 0;
- }
-
- void __exit mychar_exit(void)
- {
- dev_t devno = MKDEV(major, minor);
- cdev_del(&mydev);
- unregister_chrdev_region(devno, mychar_num);
- }
-
-
- MODULE_LICENSE("GPL");
-
- module_init(mychar_init);
- module_exit(mychar_exit);
test.c
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <unistd.h>
- #include <stdio.h>
-
- int main(int argc, char* argv[])
- {
- int fd = -1;
- char buf[8] = "";
-
- if (argc < 2) {
- printf("The argument is too few\n");
- return 1;
- }
-
- fd = open(argv[1], O_RDWR);
- if (fd < 0) {
- printf("open %s failed\n", argv[1]);
- return 2;
- }
-
- write(fd, "hello", 6);
- read(fd, buf, 8);
- printf("buf = %s\n",buf);
-
- close(fd);
- fd = -1;
- return 0;
- }
测试结果
- shrek@ubuntu16:~/share/mydrivercode$ sudo mknod /dev/mydev c 11 0
- shrek@ubuntu16:~/share/mydrivercode$ sudo chmod a+w /dev/mydev
- shrek@ubuntu16:~/share/mydrivercode$ sudo insmod ./mychar.ko
- shrek@ubuntu16:~/share/mydrivercode$ ./a.out /dev/mydev
- buf = hello
已知成员的地址获得所在结构体变量的地址:container_of(成员地址, 结构体类型名, 成员在结构体中的名称)
- #include <linux/module.h>
- #include <linux/kernel.h>
- #include <linux/fs.h>
- #include <linux/cdev.h>
- #include <linux/uaccess.h>
-
- #define BUF_LEN 100
-
- int major = 11;
- int minor = 0;
- int mychar_num = 1;
-
- struct mychar_dev
- {
- struct cdev mydev;
-
- char mydev_buf[BUF_LEN];
- int curlen;
- };
-
- struct mychar_dev gmydev;
-
- int mychar_open(struct inode* pnode, struct file* pfile)
- {
- pfile->private_data = (void *)(container_of(pnode->i_cdev, struct mychar_dev, mydev));
- printk("mychar_open is called\n");
- return 0;
- }
-
- int mychar_close(struct inode* pnode, struct file* pfile)
- {
- printk("mychar_close is called\n");
- return 0;
- }
-
- ssize_t mychar_read(struct file* pfile, char __user *puser, size_t count, loff_t* p_pos)
- {
- struct mychar_dev *pmydev = (struct mychar_dev *)pfile->private_data;
- int size = 0;
- int ret = 0;
-
- if (count > pmydev->curlen) {
- size = pmydev->curlen;
- } else {
- size = count;
- }
-
- ret = copy_to_user(puser, pmydev->mydev_buf, size);
- if (ret) {
- printk("copy_to_user failed\n");
- return -1;
- }
-
- memcpy(pmydev->mydev_buf, pmydev->mydev_buf + size, pmydev->curlen - size);
-
- pmydev->curlen -= size;
- return size;
- }
-
- ssize_t mychar_write(struct file* pfile, const char __user *puser, size_t count, loff_t* p_pos)
- {
- struct mychar_dev *pmydev = (struct mychar_dev *)pfile->private_data;
- int size = 0;
- int ret = 0;
-
- if (count > BUF_LEN - pmydev->curlen) {
- size = BUF_LEN - pmydev->curlen;
- } else {
- size = count;
- }
-
- ret = copy_from_user(pmydev->mydev_buf + pmydev->curlen, puser, size);
- if (ret) {
- printk("copy_from_user failed\n");
- return -1;
- }
-
- pmydev->curlen += size;
- return size;
- }
-
- struct file_operations myops = {
- .owner = THIS_MODULE,
- .open = mychar_open,
- .release = mychar_close,
- .read = mychar_read,
- .write = mychar_write,
- };
-
- int __init mychar_init(void)
- {
- int ret = 0;
- dev_t devno = MKDEV(major, minor);
-
- /* 申请设备号 */
- ret = register_chrdev_region(devno, mychar_num, "mychar");
- if (ret) {
- ret = alloc_chrdev_region(&devno, minor, mychar_num, "mychar");
- if (ret) {
- printk("get devno faile\n");
- return -1;
- }
- major = MAJOR(devno);
- }
-
- /* 给struct cdev对象指定操作函数集 */
- cdev_init(&gmydev.mydev, &myops);
-
- /* 给struct cdev对象添加到内核对应的数据结构里 */
- gmydev.mydev.owner = THIS_MODULE;
- cdev_add(&gmydev.mydev, devno, mychar_num);
-
- return 0;
- }
-
- void __exit mychar_exit(void)
- {
- dev_t devno = MKDEV(major, minor);
- cdev_del(&gmydev.mydev);
- unregister_chrdev_region(devno, mychar_num);
- }
-
-
- MODULE_LICENSE("GPL");
-
- module_init(mychar_init);
- module_exit(mychar_exit);
long xxx_ioctl (struct file *filp, unsigned int cmd, unsigned long arg);
功能:对相应设备做指定的控制操作(各种属性的设置获取等等)
参数:
filp:指向open产生的struct file类型的对象,表示本次ioctl对应的那次open
cmd:用来表示做的是哪一个操作
arg:和cmd配合用的参数
返回值:成功为0,失败-1
1. dir(direction),ioctl 命令访问模式(属性数据传输方向),占据 2 bit,可以为 _IOC_NONE、_IOC_READ、_IOC_WRITE、_IOC_READ | _IOC_WRITE,分别指示了四种访问模式:无数据、读数据、写数据、读写数据;
2. type(device type),设备类型,占据 8 bit,在一些文献中翻译为 “幻数” 或者 “魔数”,可以为任意 char 型字符,例如
‘a’、’b’、’c’ 等等,其主要作用是使 ioctl 命令有唯一的设备标识;
3. nr(number),命令编号/序数,占据 8 bit,可以为任意 unsigned char 型数据,取值范围 0~255,如果定义了多个 ioctl 命令,通常从 0 开始编号递增;
4. size,涉及到 ioctl 函数 第三个参数 arg ,占据 13bit 或者 14bit(体系相关,arm 架构一般为 14 位),指定了 arg 的数据类型及长度,如果在驱动的 ioctl 实现中不检查,通常可以忽略该参数;
- #define _IOC(dir,type,nr,size) (((dir)<<_IOC_DIRSHIFT)| \
- ((type)<<_IOC_TYPESHIFT)| \
- ((nr)<<_IOC_NRSHIFT)| \
- ((size)<<_IOC_SIZESHIFT))
- /* used to create numbers */
-
- // 定义不带参数的 ioctl 命令
- #define _IO(type,nr) _IOC(_IOC_NONE,(type),(nr),0)
-
- //定义带读参数的ioctl命令(copy_to_user) size为类型名
- #define _IOR(type,nr,size) _IOC(_IOC_READ,(type),(nr),(_IOC_TYPECHECK(size)))
-
- //定义带写参数的 ioctl 命令(copy_from_user) size为类型名
- #define _IOW(type,nr,size) _IOC(_IOC_WRITE,(type),(nr),(_IOC_TYPECHECK(size)))
-
- //定义带读写参数的 ioctl 命令 size为类型名
- #define _IOWR(type,nr,size) _IOC(_IOC_READ|_IOC_WRITE,(type),(nr),(_IOC_TYPECHECK(size)))
-
- /* used to decode ioctl numbers */
- #define _IOC_DIR(nr) (((nr) >> _IOC_DIRSHIFT) & _IOC_DIRMASK)
- #define _IOC_TYPE(nr) (((nr) >> _IOC_TYPESHIFT) & _IOC_TYPEMASK)
- #define _IOC_NR(nr) (((nr) >> _IOC_NRSHIFT) & _IOC_NRMASK)
- #define _IOC_SIZE(nr) (((nr) >> _IOC_SIZESHIFT) & _IOC_SIZEMASK)
mychar.c
- #include <linux/module.h>
- #include <linux/kernel.h>
- #include <linux/fs.h>
- #include <linux/cdev.h>
- #include <linux/uaccess.h>
-
- #include "mychar.h"
-
- #define BUF_LEN 100
-
- int major = 11;
- int minor = 0;
- int mychar_num = 1;
-
- struct mychar_dev
- {
- struct cdev mydev;
-
- char mydev_buf[BUF_LEN];
- int curlen;
- };
-
- struct mychar_dev gmydev;
-
- int mychar_open(struct inode* pnode, struct file* pfile)
- {
- pfile->private_data = (void *)(container_of(pnode->i_cdev, struct mychar_dev, mydev));
- printk("mychar_open is called\n");
- return 0;
- }
-
- int mychar_close(struct inode* pnode, struct file* pfile)
- {
- printk("mychar_close is called\n");
- return 0;
- }
-
- ssize_t mychar_read(struct file* pfile, char __user *puser, size_t count, loff_t* p_pos)
- {
- struct mychar_dev *pmydev = (struct mychar_dev *)pfile->private_data;
- int size = 0;
- int ret = 0;
-
- if (count > pmydev->curlen) {
- size = pmydev->curlen;
- } else {
- size = count;
- }
-
- ret = copy_to_user(puser, pmydev->mydev_buf, size);
- if (ret) {
- printk("copy_to_user failed\n");
- return -1;
- }
-
- memcpy(pmydev->mydev_buf, pmydev->mydev_buf + size, pmydev->curlen - size);
-
- pmydev->curlen -= size;
- return size;
- }
-
- ssize_t mychar_write(struct file* pfile, const char __user *puser, size_t count, loff_t* p_pos)
- {
- struct mychar_dev *pmydev = (struct mychar_dev *)pfile->private_data;
- int size = 0;
- int ret = 0;
-
- if (count > BUF_LEN - pmydev->curlen) {
- size = BUF_LEN - pmydev->curlen;
- } else {
- size = count;
- }
-
- ret = copy_from_user(pmydev->mydev_buf + pmydev->curlen, puser, size);
- if (ret) {
- printk("copy_from_user failed\n");
- return -1;
- }
-
- pmydev->curlen += size;
- return size;
- }
-
- long mychar_ioctl(struct file* pfile, unsigned int cmd, unsigned long arg)
- {
- int __user *pret = (int*)arg;
- int maxlen = BUF_LEN;
- int ret = 0;
- struct mychar_dev *pmydev = (struct mychar_dev *)pfile->private_data;
-
- switch (cmd) {
- case MYCHAR_IOCTL_GET_MAXLEN:
- ret = copy_to_user(pret, &maxlen, sizeof(int));
- if (ret) {
- printk("copy_to_user MAXLEN failed\n");
- return -1;
- }
- break;
- case MYCHAR_IOCTL_GET_CURLEN:
- ret = copy_to_user(pret, &pmydev->curlen, sizeof(int));
- if (ret) {
- printk("copy_to_user CURLEN failed\n");
- return -1;
- }
- break;
- default:
- printk("The cmd is unknow\n");
- return -1;
- }
- return 0;
- }
-
- struct file_operations myops = {
- .owner = THIS_MODULE,
- .open = mychar_open,
- .release = mychar_close,
- .read = mychar_read,
- .write = mychar_write,
- .unlocked_ioctl = mychar_ioctl,
- };
-
- int __init mychar_init(void)
- {
- int ret = 0;
- dev_t devno = MKDEV(major, minor);
-
- /* 申请设备号 */
- ret = register_chrdev_region(devno, mychar_num, "mychar");
- if (ret) {
- ret = alloc_chrdev_region(&devno, minor, mychar_num, "mychar");
- if (ret) {
- printk("get devno failed\n");
- return -1;
- }
- major = MAJOR(devno);
- }
-
- /* 给struct cdev对象指定操作函数集 */
- cdev_init(&gmydev.mydev, &myops);
-
- /* 给struct cdev对象添加到内核对应的数据结构里 */
- gmydev.mydev.owner = THIS_MODULE;
- cdev_add(&gmydev.mydev, devno, mychar_num);
-
- return 0;
- }
-
- void __exit mychar_exit(void)
- {
- dev_t devno = MKDEV(major, minor);
- cdev_del(&gmydev.mydev);
- unregister_chrdev_region(devno, mychar_num);
- }
-
-
- MODULE_LICENSE("GPL");
-
- module_init(mychar_init);
- module_exit(mychar_exit);
mychar.h
- #ifndef MY_CHAR_H
- #define MY_CHAR_H
-
- #include <asm/ioctl.h>
-
- #define MY_CHAR_MAGIC 'k'
-
- #define MYCHAR_IOCTL_GET_MAXLEN _IOR(MY_CHAR_MAGIC,1,int*)
- #define MYCHAR_IOCTL_GET_CURLEN _IOR(MY_CHAR_MAGIC,2,int*)
-
-
- #endif
test.c
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <unistd.h>
- #include <stdio.h>
- #include <sys/ioctl.h>
-
- #include "mychar.h"
-
- int main(int argc, char* argv[])
- {
- int fd = -1;
- char buf[8] = "";
- int max = 0;
- int cur = 0;
-
- if (argc < 2) {
- printf("The argument is too few\n");
- return 1;
- }
-
- fd = open(argv[1], O_RDWR);
- if (fd < 0) {
- printf("open %s failed\n", argv[1]);
- return 2;
- }
-
- ioctl(fd, MYCHAR_IOCTL_GET_MAXLEN, &max);
- printf("max len is %d\n", max);
-
- write(fd, "hello", 6);
-
- ioctl(fd, MYCHAR_IOCTL_GET_CURLEN, &cur);
- printf("cur len is %d\n",cur);
-
- read(fd, buf, 8);
- printf("buf = %s\n",buf);
-
- close(fd);
- fd = -1;
- return 0;
- }
测试结果
- shrek@ubuntu16:~/share/mydrivercode$ sudo mknod /dev/mydev c 11 0
- shrek@ubuntu16:~/share/mydrivercode$ sudo insmod ./mychar.ko
- shrek@ubuntu16:~/share/mydrivercode$ ./a.out /dev/mydev
- max len is 100
- cur len is 6
- buf = hello
- //日志级别
- #define KERN_EMERG "<0>" /* system is unusable */
- #define KERN_ALERT "<1>" /* action must be taken immediately */
- #define KERN_CRIT "<2>" /* critical conditions */
- #define KERN_ERR "<3>" /* error conditions */
-
- #define KERN_WARNING "<4>" /* warning conditions */
-
- #define KERN_NOTICE "<5>" /* normal but significant condition */
- #define KERN_INFO "<6>" /* informational */
- #define KERN_DEBUG "<7>" /* debug-level messages */
用法:printk(KERN_INFO"....",....)
printk(KERN_INFO"Hello World"); =====> printk("<6>""Hello World") ====> printk("<6>Hello World")
指定 dmesg 打印的内容:dmesg --level=emerg,alert,crit,err,warn,notice,info,debug
自定义标签:
- #define HELLO_DEBUG
- #undef PDEBUG
- #ifdef HELLO_DEBUG
- #define PDEBUG(fmt, args...) printk(KERN_DEBUG fmt, ##args)
- #else
- #define PDEBUG(fmt, args...)
- #endif
应用层:
方法一:open时由 O_NONBLOCK 指示read、write时是否阻塞
方法二:open以后可以由fcntl函数来改变是否阻塞:
- flags = fcntl(fd, F_GETFL, 0);
- flags |= O_NONBLOCK;
- fcntl(fd, F_SETFL, flags);
驱动层:
通过等待队列
- wait_queue_head_t //等待队列头数据类型
-
- init_waitqueue_head(wait_queue_head_t *pwq) //初始化等待队列头
-
- wait_event_interruptible(wq,condition)
- /*
- 功能:条件不成立则让任务进入浅度睡眠,直到条件成立醒来
- wq:等待队列头
- condition:C语言表达式
- 返回:正常唤醒返回0,信号唤醒返回非0(此时读写操作函数应返回-ERESTARTSYS)
- */
-
- wait_event(wq,condition) //深度睡眠
-
- wake_up_interruptible(wait_queue_head_t *pwq)
-
- wake_up(wait_queue_head_t *pwq)
-
-
- /*
- 1. 读、写用不同的等待队列头rq、wq
- 2. 无数据可读、可写时调用wait_event_interruptible(rq、wq,条件)
- 3. 写入数据成功时唤醒rq,读出数据成功唤醒wq
- */
mychar.c
- #include <linux/module.h>
- #include <linux/kernel.h>
- #include <linux/fs.h>
- #include <linux/cdev.h>
- #include <linux/wait.h>
- #include <linux/sched.h>
- #include <linux/uaccess.h>
-
- #include "mychar.h"
-
- #define BUF_LEN 100
-
- int major = 11;
- int minor = 0;
- int mychar_num = 1;
-
- struct mychar_dev
- {
- struct cdev mydev;
-
- char mydev_buf[BUF_LEN];
- int curlen;
-
- wait_queue_head_t rq;
- wait_queue_head_t wq;
- };
-
- struct mychar_dev gmydev;
-
- int mychar_open(struct inode* pnode, struct file* pfile)
- {
- pfile->private_data = (void *)(container_of(pnode->i_cdev, struct mychar_dev, mydev));
- printk("mychar_open is called\n");
- return 0;
- }
-
- int mychar_close(struct inode* pnode, struct file* pfile)
- {
- printk("mychar_close is called\n");
- return 0;
- }
-
- ssize_t mychar_read(struct file* pfile, char __user *puser, size_t count, loff_t* p_pos)
- {
- struct mychar_dev *pmydev = (struct mychar_dev *)pfile->private_data;
- int size = 0;
- int ret = 0;
-
- if (pmydev->curlen <= 0) {
- if (pfile->f_flags & O_NONBLOCK) {
- // 非阻塞
- printk("O_NONBLOCK No Data Read\n");
- return -1;
- } else {
- // 阻塞
- ret = wait_event_interruptible(pmydev->rq, pmydev->curlen > 0);
- if (ret) {
- printk("Wake up by signal\n");
- return -ERESTARTSYS;
- }
- }
- }
-
- if (count > pmydev->curlen) {
- size = pmydev->curlen;
- } else {
- size = count;
- }
-
- ret = copy_to_user(puser, pmydev->mydev_buf, size);
- if (ret) {
- printk("copy_to_user failed\n");
- return -1;
- }
-
- memcpy(pmydev->mydev_buf, pmydev->mydev_buf + size, pmydev->curlen - size);
-
- pmydev->curlen -= size;
-
- wake_up_interruptible(&pmydev->wq);
-
- return size;
- }
-
- ssize_t mychar_write(struct file* pfile, const char __user *puser, size_t count, loff_t* p_pos)
- {
- struct mychar_dev *pmydev = (struct mychar_dev *)pfile->private_data;
- int size = 0;
- int ret = 0;
-
- if (pmydev->curlen >= BUF_LEN) {
- if (pfile->f_flags & O_NONBLOCK) {
- printk("O_NONBLOCK Can not write data\n");
- return -1;
- } else {
- ret = wait_event_interruptible(pmydev->wq, pmydev->curlen < BUF_LEN);
- if (ret) {
- printk("Wake up by signal\n");
- return -ERESTARTSYS;
- }
- }
- }
-
- if (count > BUF_LEN - pmydev->curlen) {
- size = BUF_LEN - pmydev->curlen;
- } else {
- size = count;
- }
-
- ret = copy_from_user(pmydev->mydev_buf + pmydev->curlen, puser, size);
- if (ret) {
- printk("copy_from_user failed\n");
- return -1;
- }
-
- pmydev->curlen += size;
-
- wake_up_interruptible(&pmydev->rq);
-
- return size;
- }
-
- long mychar_ioctl(struct file* pfile, unsigned int cmd, unsigned long arg)
- {
- int __user *pret = (int*)arg;
- int maxlen = BUF_LEN;
- int ret = 0;
- struct mychar_dev *pmydev = (struct mychar_dev *)pfile->private_data;
-
- switch (cmd) {
- case MYCHAR_IOCTL_GET_MAXLEN:
- ret = copy_to_user(pret, &maxlen, sizeof(int));
- if (ret) {
- printk("copy_to_user MAXLEN failed\n");
- return -1;
- }
- break;
- case MYCHAR_IOCTL_GET_CURLEN:
- ret = copy_to_user(pret, &pmydev->curlen, sizeof(int));
- if (ret) {
- printk("copy_to_user CURLEN failed\n");
- return -1;
- }
- break;
- default:
- printk("The cmd is unknow\n");
- return -1;
- }
- return 0;
- }
-
- struct file_operations myops = {
- .owner = THIS_MODULE,
- .open = mychar_open,
- .release = mychar_close,
- .read = mychar_read,
- .write = mychar_write,
- .unlocked_ioctl = mychar_ioctl,
- };
-
- int __init mychar_init(void)
- {
- int ret = 0;
- dev_t devno = MKDEV(major, minor);
-
- /* 申请设备号 */
- ret = register_chrdev_region(devno, mychar_num, "mychar");
- if (ret) {
- ret = alloc_chrdev_region(&devno, minor, mychar_num, "mychar");
- if (ret) {
- printk("get devno failed\n");
- return -1;
- }
- major = MAJOR(devno);
- }
-
- /* 给struct cdev对象指定操作函数集 */
- cdev_init(&gmydev.mydev, &myops);
-
- /* 给struct cdev对象添加到内核对应的数据结构里 */
- gmydev.mydev.owner = THIS_MODULE;
- cdev_add(&gmydev.mydev, devno, mychar_num);
-
- init_waitqueue_head(&gmydev.rq);
- init_waitqueue_head(&gmydev.wq);
- return 0;
- }
-
- void __exit mychar_exit(void)
- {
- dev_t devno = MKDEV(major, minor);
- cdev_del(&gmydev.mydev);
- unregister_chrdev_region(devno, mychar_num);
- }
-
-
- MODULE_LICENSE("GPL");
-
- module_init(mychar_init);
- module_exit(mychar_exit);
mychar.h同ioctl
test.c(测试非阻塞,如果测试阻塞则将 O_NONBLOCK 去掉即可)
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <unistd.h>
- #include <stdio.h>
- #include <sys/ioctl.h>
-
- #include "mychar.h"
-
- int main(int argc, char* argv[])
- {
- int fd = -1;
- char buf[8] = "";
- int ret = 0;
-
- if (argc < 2) {
- printf("The argument is too few\n");
- return 1;
- }
-
- fd = open(argv[1], O_RDWR | O_NONBLOCK);
- if (fd < 0) {
- printf("open %s failed\n", argv[1]);
- return 2;
- }
-
- ret = read(fd, buf, 8);
- if (ret < 0) {
- printf("read data failed\n");
- } else {
- printf("buf = %s\n",buf);
- }
-
- close(fd);
- fd = -1;
- return 0;
- }
测试结果
非阻塞:
- shrek@ubuntu16:~/share/mydrivercode$ sudo mknod /dev/mydev c 11 0
- shrek@ubuntu16:~/share/mydrivercode$ sudo chmod a+w /dev/mydev
- shrek@ubuntu16:~/share/mydrivercode$ sudo insmod ./mychar.ko
- shrek@ubuntu16:~/share/mydrivercode$ ./a.out /dev/mydev
- read data failed
阻塞:
执行测试代码一直阻塞,直至给设备写入数据结束阻塞
- shrek@ubuntu16:~/share/mydrivercode$ echo "hello" > /dev/mydev
- shrek@ubuntu16:~/share/mydrivercode$ ./a.out /dev/mydev
- buf = hello
应用层
select:位运算实现,监控的描述符数量有限(32位机1024,64位机2048),效率差
poll:链表实现,监控的描述符数量不限,效率差
epoll:效率最高,监控的描述符数量不限
select:
- int select(int nfds, fd_set *readfds, fd_set *writefds,fd_set *exceptfds, struct timeval *timeout);
- /* 功能:监听多个描述符,阻塞等待有一个或者多个文件描述符,准备就绪。
- 内核将没有准备就绪的文件描述符,从集合中清掉了。
- 参数: nfds 最大文件描述符数 ,加1
- readfds 读文件描述符集合
- writefds 写文件描述符集合
- exceptfds 其他异常的文件描述符集合
- timeout 超时时间(NULL)
- 返回值:当timeout为NULL时返回0,成功:准备好的文件描述的个数 出错:-1
- 当timeout不为NULL时,如超时设置为0,则select为非阻塞,超时设置 > 0,则无描述符可被操作的情况下阻塞指定长度的时间
- */
- void FD_CLR(int fd, fd_set *set);
- //功能:将fd 从集合中清除掉
-
- int FD_ISSET(int fd, fd_set *set);
- //功能:判断fd 是否存在于集合中
-
- void FD_SET(int fd, fd_set *set);
- //功能:将fd 添加到集合中
-
- void FD_ZERO(fd_set *set);
- //功能:将集合清零
-
- //使用模型:
-
- while(1)
- {
- /*得到最大的描述符maxfd*/
- /*FD_ZERO清空描述符集合*/
- /*将被监控描述符加到相应集合rfds里 FD_SET*/
- /*设置超时*/
- ret = select(maxfd+1,&rfds,&wfds,NULL,NULL);
- if(ret < 0)
- {
- if(errno == EINTR)//错误时信号引起的
- {
- continue;
- }
- else
- {
- break;
- }
- }
- else if(ret == 0)
- {//超时
- //.....
- }
- else
- { //> 0 ret为可被操作的描述符个数
- if(FD_ISSET(fd1,&rfds))
- {//读数据
- //....
- }
- if(FD_ISSET(fd2,&rfds))
- {//读数据
- //....
- }
- ///.....
- if(FD_ISSET(fd1,&wfds))
- {//写数据
- //....
- }
- }
- }
驱动层
- void poll_wait(struct file * filp, wait_queue_head_t * wait_address, poll_table *p);
- /*功能:将等待队列头添加至poll_table表中
- 参数:struct file :设备文件
- Wait_queue_head_t :等待队列头
- Poll_table :poll_table表
- */
-
- /*该函数与select、poll、epoll_wait函数相对应,协助这些多路监控函数判断本设备是否有数据可读写*/
- unsigned int xxx_poll(struct file *filp, poll_table *wait) //函数名初始化给struct file_operations的成员.poll
- {
- unsigned int mask = 0;
- /*
- 1. 将所有等待队列头加入poll_table表中
- 2. 判断是否可读,如可读则mask |= POLLIN | POLLRDNORM;
- 3. 判断是否可写,如可写则mask |= POLLOUT | POLLWRNORM;
- */
-
- return mask;
- }
mychar.c
- #include <linux/module.h>
- #include <linux/kernel.h>
- #include <linux/fs.h>
- #include <linux/cdev.h>
- #include <linux/wait.h>
- #include <linux/sched.h>
- #include <linux/poll.h>
- #include <linux/uaccess.h>
-
- #include "mychar.h"
-
- #define BUF_LEN 100
-
- int major = 11;
- int minor = 0;
- int mychar_num = 1;
-
- struct mychar_dev
- {
- struct cdev mydev;
-
- char mydev_buf[BUF_LEN];
- int curlen;
-
- wait_queue_head_t rq;
- wait_queue_head_t wq;
- };
-
- struct mychar_dev gmydev;
-
- int mychar_open(struct inode* pnode, struct file* pfile)
- {
- pfile->private_data = (void *)(container_of(pnode->i_cdev, struct mychar_dev, mydev));
- printk("mychar_open is called\n");
- return 0;
- }
-
- int mychar_close(struct inode* pnode, struct file* pfile)
- {
- printk("mychar_close is called\n");
- return 0;
- }
-
- ssize_t mychar_read(struct file* pfile, char __user *puser, size_t count, loff_t* p_pos)
- {
- struct mychar_dev *pmydev = (struct mychar_dev *)pfile->private_data;
- int size = 0;
- int ret = 0;
-
- if (pmydev->curlen <= 0) {
- if (pfile->f_flags & O_NONBLOCK) {
- // 非阻塞
- printk("O_NONBLOCK No Data Read\n");
- return -1;
- } else {
- // 阻塞
- ret = wait_event_interruptible(pmydev->rq, pmydev->curlen > 0);
- if (ret) {
- printk("Wake up by signal\n");
- return -ERESTARTSYS;
- }
- }
- }
-
- if (count > pmydev->curlen) {
- size = pmydev->curlen;
- } else {
- size = count;
- }
-
- ret = copy_to_user(puser, pmydev->mydev_buf, size);
- if (ret) {
- printk("copy_to_user failed\n");
- return -1;
- }
-
- memcpy(pmydev->mydev_buf, pmydev->mydev_buf + size, pmydev->curlen - size);
-
- pmydev->curlen -= size;
-
- wake_up_interruptible(&pmydev->wq);
-
- return size;
- }
-
- ssize_t mychar_write(struct file* pfile, const char __user *puser, size_t count, loff_t* p_pos)
- {
- struct mychar_dev *pmydev = (struct mychar_dev *)pfile->private_data;
- int size = 0;
- int ret = 0;
-
- if (pmydev->curlen >= BUF_LEN) {
- if (pfile->f_flags & O_NONBLOCK) {
- printk("O_NONBLOCK Can not write data\n");
- return -1;
- } else {
- ret = wait_event_interruptible(pmydev->wq, pmydev->curlen < BUF_LEN);
- if (ret) {
- printk("Wake up by signal\n");
- return -ERESTARTSYS;
- }
- }
- }
-
- if (count > BUF_LEN - pmydev->curlen) {
- size = BUF_LEN - pmydev->curlen;
- } else {
- size = count;
- }
-
- ret = copy_from_user(pmydev->mydev_buf + pmydev->curlen, puser, size);
- if (ret) {
- printk("copy_from_user failed\n");
- return -1;
- }
-
- pmydev->curlen += size;
-
- wake_up_interruptible(&pmydev->rq);
-
- return size;
- }
-
- long mychar_ioctl(struct file* pfile, unsigned int cmd, unsigned long arg)
- {
- int __user *pret = (int*)arg;
- int maxlen = BUF_LEN;
- int ret = 0;
- struct mychar_dev *pmydev = (struct mychar_dev *)pfile->private_data;
-
- switch (cmd) {
- case MYCHAR_IOCTL_GET_MAXLEN:
- ret = copy_to_user(pret, &maxlen, sizeof(int));
- if (ret) {
- printk("copy_to_user MAXLEN failed\n");
- return -1;
- }
- break;
- case MYCHAR_IOCTL_GET_CURLEN:
- ret = copy_to_user(pret, &pmydev->curlen, sizeof(int));
- if (ret) {
- printk("copy_to_user CURLEN failed\n");
- return -1;
- }
- break;
- default:
- printk("The cmd is unknow\n");
- return -1;
- }
- return 0;
- }
-
- unsigned int mychar_poll(struct file* pfile, poll_table* ptb)
- {
- struct mychar_dev *pmydev = (struct mychar_dev *)pfile->private_data;
- unsigned int mask = 0;
-
- poll_wait(pfile, &pmydev->rq, ptb);
- poll_wait(pfile, &pmydev->wq, ptb);
-
- if (pmydev->curlen > 0) {
- mask |= POLLIN | POLLRDNORM;
- }
-
- if (pmydev->curlen < BUF_LEN) {
- mask |= POLLOUT | POLLWRNORM;
- }
-
- return mask;
- }
-
- struct file_operations myops = {
- .owner = THIS_MODULE,
- .open = mychar_open,
- .release = mychar_close,
- .read = mychar_read,
- .write = mychar_write,
- .unlocked_ioctl = mychar_ioctl,
- .poll = mychar_poll,
- };
-
- int __init mychar_init(void)
- {
- int ret = 0;
- dev_t devno = MKDEV(major, minor);
-
- /* 申请设备号 */
- ret = register_chrdev_region(devno, mychar_num, "mychar");
- if (ret) {
- ret = alloc_chrdev_region(&devno, minor, mychar_num, "mychar");
- if (ret) {
- printk("get devno failed\n");
- return -1;
- }
- major = MAJOR(devno);
- }
-
- /* 给struct cdev对象指定操作函数集 */
- cdev_init(&gmydev.mydev, &myops);
-
- /* 给struct cdev对象添加到内核对应的数据结构里 */
- gmydev.mydev.owner = THIS_MODULE;
- cdev_add(&gmydev.mydev, devno, mychar_num);
-
- init_waitqueue_head(&gmydev.rq);
- init_waitqueue_head(&gmydev.wq);
- return 0;
- }
-
- void __exit mychar_exit(void)
- {
- dev_t devno = MKDEV(major, minor);
- cdev_del(&gmydev.mydev);
- unregister_chrdev_region(devno, mychar_num);
- }
-
-
- MODULE_LICENSE("GPL");
-
- module_init(mychar_init);
- module_exit(mychar_exit);
mychar.h同ioctl
test.c
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <unistd.h>
- #include <stdio.h>
- #include <sys/ioctl.h>
- #include <sys/select.h>
- #include <sys/time.h>
- #include <errno.h>
-
- #include "mychar.h"
-
- int main(int argc, char* argv[])
- {
- int fd = -1;
- char buf[8] = "";
- int ret = 0;
- fd_set rfds;
-
- if (argc < 2) {
- printf("The argument is too few\n");
- return 1;
- }
-
- fd = open(argv[1], O_RDWR);
- if (fd < 0) {
- printf("open %s failed\n", argv[1]);
- return 2;
- }
-
- while (1) {
- FD_ZERO(&rfds);
- FD_SET(fd, &rfds);
- ret = select(fd + 1, &rfds, NULL, NULL, NULL);
- if (ret < 0) {
- if (errno == EINTR) {
- continue;
- } else {
- printf("select error\n");
- break;
- }
- }
- if (FD_ISSET(fd, &rfds)) {
- read(fd, buf, 8);
- printf("buf = %s\n", buf);
- }
- }
-
- close(fd);
- fd = -1;
- return 0;
- }
测试结果
执行测试代码后一直等待是否有数据可读,有则读取数据
- shrek@ubuntu16:~/share/mydrivercode$ echo "hello" > /dev/mydev
- shrek@ubuntu16:~/share/mydrivercode$ echo "hello" > /dev/mydev
- shrek@ubuntu16:~/share/mydrivercode$ echo "hello" > /dev/mydev
- shrek@ubuntu16:~/share/mydrivercode$ echo "hello" > /dev/mydev
- shrek@ubuntu16:~/share/mydrivercode$ ./a.out /dev/mydev
- buf = hello
-
- buf = hello
-
- buf = hello
-
- buf = hello
Tips
信号驱动存在丢失信号的风险
应用层:信号注册+fcntl
- signal(SIGIO, input_handler); //注册信号处理函数
-
- fcntl(fd, F_SETOWN, getpid());//将描述符设置给对应进程,好由描述符获知PID
-
- oflags = fcntl(fd, F_GETFL);
- fcntl(fd, F_SETFL, oflags | FASYNC);//将该设备的IO模式设置成信号驱动模式
-
- void input_handler(int signum)//应用自己实现的信号处理函数,在此函数中完成读写
- {
- //读数据
- }
-
- //应用模板
- int main()
- {
- int fd = open("/dev/xxxx",O_RDONLY);
-
- fcntl(fd, F_SETOWN, getpid());
-
- oflags = fcntl(fd, F_GETFL);
- fcntl(fd, F_SETFL, oflags | FASYNC);
-
- signal(SIGIO,xxxx_handler);
-
- //......
- }
-
- void xxxx_handle(int signo)
- {//读写数据
-
- }
驱动层:实现fasync函数
- /*设备结构中添加如下成员*/
- struct fasync_struct *pasync_obj;
-
- /*应用调用fcntl设置FASYNC时调用该函数产生异步通知结构对象,并将其地址设置到设备结构成员中*/
- static int hello_fasync(int fd, struct file *filp, int mode) //函数名初始化给struct file_operations的成员.fasync
- {
- struct hello_device *dev = filp->private_data;
- return fasync_helper(fd, filp, mode, &dev->pasync_obj);
- }
-
- /*写函数中有数据可读时向应用层发信号*/
- if (dev->pasync_obj)
- kill_fasync(&dev->pasync_obj, SIGIO, POLL_IN);
-
- /*release函数中释放异步通知结构对象*/
- if (dev->pasync_obj)
- fasync_helper(-1, filp, 0, &dev->pasync_obj);
-
- int fasync_helper(int fd, struct file *filp, int mode, struct fasync_struct **pp);
- /*
- 功能:产生或释放异步通知结构对象
- 参数:
- 返回值:成功为>=0,失败负数
- */
-
- void kill_fasync(struct fasync_struct **, int, int);
- /*
- 功能:发信号
- 参数:
- struct fasync_struct ** 指向保存异步通知结构地址的指针
- int 信号 SIGIO/SIGKILL/SIGCHLD/SIGCONT/SIGSTOP
- int 读写信息POLLIN、POLLOUT
- */
mychar.c
- #include <linux/module.h>
- #include <linux/kernel.h>
- #include <linux/fs.h>
- #include <linux/cdev.h>
- #include <linux/wait.h>
- #include <linux/sched.h>
- #include <linux/poll.h>
- #include <linux/uaccess.h>
-
- #include "mychar.h"
-
- #define BUF_LEN 100
-
- int major = 11;
- int minor = 0;
- int mychar_num = 1;
-
- struct mychar_dev
- {
- struct cdev mydev;
-
- char mydev_buf[BUF_LEN];
- int curlen;
-
- wait_queue_head_t rq;
- wait_queue_head_t wq;
-
- struct fasync_struct* pasync_obj;
- };
-
- struct mychar_dev gmydev;
-
- int mychar_open(struct inode* pnode, struct file* pfile)
- {
- pfile->private_data = (void *)(container_of(pnode->i_cdev, struct mychar_dev, mydev));
- return 0;
- }
-
- int mychar_close(struct inode* pnode, struct file* pfile)
- {
- struct mychar_dev *pmydev = (struct mychar_dev *)pfile->private_data;
- if (pmydev->pasync_obj != NULL) {
- fasync_helper(-1, pfile, 0, &pmydev->pasync_obj);
- }
- return 0;
- }
-
- ssize_t mychar_read(struct file* pfile, char __user *puser, size_t count, loff_t* p_pos)
- {
- struct mychar_dev *pmydev = (struct mychar_dev *)pfile->private_data;
- int size = 0;
- int ret = 0;
-
- if (pmydev->curlen <= 0) {
- if (pfile->f_flags & O_NONBLOCK) {
- // 非阻塞
- printk("O_NONBLOCK No Data Read\n");
- return -1;
- } else {
- // 阻塞
- ret = wait_event_interruptible(pmydev->rq, pmydev->curlen > 0);
- if (ret) {
- printk("Wake up by signal\n");
- return -ERESTARTSYS;
- }
- }
- }
-
- if (count > pmydev->curlen) {
- size = pmydev->curlen;
- } else {
- size = count;
- }
-
- ret = copy_to_user(puser, pmydev->mydev_buf, size);
- if (ret) {
- printk("copy_to_user failed\n");
- return -1;
- }
-
- memcpy(pmydev->mydev_buf, pmydev->mydev_buf + size, pmydev->curlen - size);
-
- pmydev->curlen -= size;
-
- wake_up_interruptible(&pmydev->wq);
-
- return size;
- }
-
- ssize_t mychar_write(struct file* pfile, const char __user *puser, size_t count, loff_t* p_pos)
- {
- struct mychar_dev *pmydev = (struct mychar_dev *)pfile->private_data;
- int size = 0;
- int ret = 0;
-
- if (pmydev->curlen >= BUF_LEN) {
- if (pfile->f_flags & O_NONBLOCK) {
- printk("O_NONBLOCK Can not write data\n");
- return -1;
- } else {
- ret = wait_event_interruptible(pmydev->wq, pmydev->curlen < BUF_LEN);
- if (ret) {
- printk("Wake up by signal\n");
- return -ERESTARTSYS;
- }
- }
- }
-
- if (count > BUF_LEN - pmydev->curlen) {
- size = BUF_LEN - pmydev->curlen;
- } else {
- size = count;
- }
-
- ret = copy_from_user(pmydev->mydev_buf + pmydev->curlen, puser, size);
- if (ret) {
- printk("copy_from_user failed\n");
- return -1;
- }
-
- pmydev->curlen += size;
-
- wake_up_interruptible(&pmydev->rq);
-
- if (pmydev->pasync_obj != NULL) {
- kill_fasync(&pmydev->pasync_obj, SIGIO, POLL_IN);
- }
-
- return size;
- }
-
- long mychar_ioctl(struct file* pfile, unsigned int cmd, unsigned long arg)
- {
- int __user *pret = (int*)arg;
- int maxlen = BUF_LEN;
- int ret = 0;
- struct mychar_dev *pmydev = (struct mychar_dev *)pfile->private_data;
-
- switch (cmd) {
- case MYCHAR_IOCTL_GET_MAXLEN:
- ret = copy_to_user(pret, &maxlen, sizeof(int));
- if (ret) {
- printk("copy_to_user MAXLEN failed\n");
- return -1;
- }
- break;
- case MYCHAR_IOCTL_GET_CURLEN:
- ret = copy_to_user(pret, &pmydev->curlen, sizeof(int));
- if (ret) {
- printk("copy_to_user CURLEN failed\n");
- return -1;
- }
- break;
- default:
- printk("The cmd is unknow\n");
- return -1;
- }
- return 0;
- }
-
- unsigned int mychar_poll(struct file* pfile, poll_table* ptb)
- {
- struct mychar_dev *pmydev = (struct mychar_dev *)pfile->private_data;
- unsigned int mask = 0;
-
- poll_wait(pfile, &pmydev->rq, ptb);
- poll_wait(pfile, &pmydev->wq, ptb);
-
- if (pmydev->curlen > 0) {
- mask |= POLLIN | POLLRDNORM;
- }
-
- if (pmydev->curlen < BUF_LEN) {
- mask |= POLLOUT | POLLWRNORM;
- }
-
- return mask;
- }
-
- int mychar_fasync(int fd, struct file* pfile, int mode) {
- struct mychar_dev *pmydev = (struct mychar_dev *)pfile->private_data;
- return fasync_helper(fd, pfile, mode, &pmydev->pasync_obj);
- }
-
- struct file_operations myops = {
- .owner = THIS_MODULE,
- .open = mychar_open,
- .release = mychar_close,
- .read = mychar_read,
- .write = mychar_write,
- .unlocked_ioctl = mychar_ioctl,
- .poll = mychar_poll,
- .fasync = mychar_fasync,
- };
-
- int __init mychar_init(void)
- {
- int ret = 0;
- dev_t devno = MKDEV(major, minor);
-
- /* 申请设备号 */
- ret = register_chrdev_region(devno, mychar_num, "mychar");
- if (ret) {
- ret = alloc_chrdev_region(&devno, minor, mychar_num, "mychar");
- if (ret) {
- printk("get devno failed\n");
- return -1;
- }
- major = MAJOR(devno);
- }
-
- /* 给struct cdev对象指定操作函数集 */
- cdev_init(&gmydev.mydev, &myops);
-
- /* 给struct cdev对象添加到内核对应的数据结构里 */
- gmydev.mydev.owner = THIS_MODULE;
- cdev_add(&gmydev.mydev, devno, mychar_num);
-
- init_waitqueue_head(&gmydev.rq);
- init_waitqueue_head(&gmydev.wq);
- return 0;
- }
-
- void __exit mychar_exit(void)
- {
- dev_t devno = MKDEV(major, minor);
- cdev_del(&gmydev.mydev);
- unregister_chrdev_region(devno, mychar_num);
- }
-
-
- MODULE_LICENSE("GPL");
-
- module_init(mychar_init);
- module_exit(mychar_exit);
mychar.h同ioctl
test.c
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <unistd.h>
- #include <stdio.h>
- #include <sys/ioctl.h>
- #include <signal.h>
-
- #include "mychar.h"
-
- int fd = -1;
-
- void sigio_handler(int signo);
-
- int main(int argc, char* argv[])
- {
- int flg = 0;
-
- if (argc < 2) {
- printf("The argument is too few\n");
- return 1;
- }
-
- signal(SIGIO, sigio_handler);
-
- fd = open(argv[1], O_RDWR);
- if (fd < 0) {
- printf("open %s failed\n", argv[1]);
- return 2;
- }
-
- fcntl(fd, F_SETOWN, getpid());
- flg = fcntl(fd, F_GETFL);
- flg |= FASYNC;
- fcntl(fd, F_SETFL, flg);
-
- while (1) {}
-
- close(fd);
- fd = -1;
- return 0;
- }
-
- void sigio_handler(int signo)
- {
- char buf[8] = "";
- read(fd, buf, 8);
- printf("buf = %s\n", buf);
- }
测试结果同多路复用
执行流:有开始有结束总体顺序执行的一段代码 又称上下文
应用编程:任务上下文
内核编程:
1. 任务上下文:五状态 可阻塞
a. 应用进程或线程运行在用户空间
b. 应用进程或线程运行在内核空间(通过调用syscall来间接使用内核空间)
c. 内核线程始终在内核空间
2. 异常上下文:不可阻塞
中断上下文
竞态:多任务并行执行时,如果在一个时刻同时操作同一个资源,会引起资源的错乱,这种错乱情形被称为竞态
共享资源:可能会被多个任务同时使用的资源
临界区:操作共享资源的代码段
为了解决竞态,需要提供一种控制机制,来避免在同一时刻使用共享资源,这种机制被称为并发控制机制
并发控制机制分类:
1. 原子操作类
2. 忙等待类
3. 阻塞类
通用并发控制机制的一般使用套路:
- /*互斥问题:*/
- 并发控制机制初始化为可用
- P操作
-
- 临界区
-
- V操作
-
- /*同步问题:*/
- // 并发控制机制初始化为不可用
- // 先行方:
- ......
- V操作
-
- // 后行方:
- P操作
- ......
一种同步机制的辅助手段
禁止本cpu中断 使能本cpu中断
local_irq_disable(); local_irq_enable();
local_irq_save(flags); local_irq_restore(flags); 与cpu的中断位相关
local_bh_disable(); local_bh_enable(); 与中断低半部有关,关闭、打开软中断
禁止中断
临界区 //临界区代码不能占用太长时间,需要很快完成
打开中断
适用场合:中断上下文与某任务共享资源时,或多个不同优先级的中断上下文间共享资源时
原子变量:存取不可被打断的特殊整型变量
a.设置原子量的值
void atomic_set(atomic_t *v,int i); // 设置原子量的值为i
atomic_t v = ATOMIC_INIT(0); // 定义原子变量v并初始化为0
v = 10;//错误
b.获取原子量的值
atomic_read(atomic_t *v); // 返回原子量的值
c.原子变量加减
void atomic_add(int i,atomic_t *v); // 原子变量增加i
void atomic_sub(int i,atomic_t *v); // 原子变量减少i
d.原子变量自增自减
void atomic_inc(atomic_t *v); // 原子变量增加1
void atomic_dec(atomic_t *v); // 原子变量减少1
e.操作并测试:运算后结果为0则返回真,否则返回假
int atomic_inc_and_test(atomic_t *v);
int atomic_dec_and_test(atomic_t *v);
int atomic_sub_and_test(int i,atomic_t *v);
原子位操作方法:
a.设置位
void set_bit(nr, void *addr); // 设置addr的第nr位为1
b.清除位
void clear_bit(nr , void *addr); // 清除addr的第nr位为0
c.改变位
void change_bit(nr , void *addr); // 改变addr的第nr位为1
d.测试位
void test_bit(nr , void *addr); // 测试addr的第nr位是否为1
适用场合:共享资源为单个整型变量的互斥场合
openonce.c
- #include <linux/module.h>
- #include <linux/kernel.h>
- #include <linux/fs.h>
- #include <linux/cdev.h>
- #include <linux/wait.h>
- #include <linux/sched.h>
- #include <linux/poll.h>
- #include <linux/uaccess.h>
- #include <asm/atomic.h>
-
- int major = 11;
- int minor = 0;
- int openonce_num = 1;
-
- struct openonce_dev
- {
- struct cdev mydev;
-
- atomic_t openflag;
- };
-
- struct openonce_dev gmydev;
-
- int openonce_open(struct inode* pnode, struct file* pfile)
- {
- struct openonce_dev* pmydev = NULL;
- pfile->private_data = (void *)(container_of(pnode->i_cdev, struct openonce_dev, mydev));
- pmydev = (struct openonce_dev*)pfile->private_data;
-
- if (atomic_dec_and_test(&pmydev->openflag)) {
- return 0;
- } else {
- atomic_inc(&pmydev->openflag);
- printk("The device is opened already\n");
- return -1;
- }
-
- return 0;
- }
-
- int openonce_close(struct inode* pnode, struct file* pfile)
- {
- struct openonce_dev *pmydev = (struct openonce_dev *)pfile->private_data;
- atomic_set(&pmydev->openflag, 1);
- return 0;
- }
-
- struct file_operations myops = {
- .owner = THIS_MODULE,
- .open = openonce_open,
- .release = openonce_close,
- };
-
- int __init openonce_init(void)
- {
- int ret = 0;
- dev_t devno = MKDEV(major, minor);
-
- /* 申请设备号 */
- ret = register_chrdev_region(devno, openonce_num, "openonce");
- if (ret) {
- ret = alloc_chrdev_region(&devno, minor, openonce_num, "openonce");
- if (ret) {
- printk("get devno failed\n");
- return -1;
- }
- major = MAJOR(devno);
- }
-
- /* 给struct cdev对象指定操作函数集 */
- cdev_init(&gmydev.mydev, &myops);
-
- /* 给struct cdev对象添加到内核对应的数据结构里 */
- gmydev.mydev.owner = THIS_MODULE;
- cdev_add(&gmydev.mydev, devno, openonce_num);
-
- atomic_set(&gmydev.openflag, 1);
- return 0;
- }
-
- void __exit openonce_exit(void)
- {
- dev_t devno = MKDEV(major, minor);
- cdev_del(&gmydev.mydev);
- unregister_chrdev_region(devno, openonce_num);
- }
-
-
- MODULE_LICENSE("GPL");
-
- module_init(openonce_init);
- module_exit(openonce_exit);
test.c
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <unistd.h>
- #include <stdio.h>
- #include <sys/ioctl.h>
-
- int main(int argc, char* argv[])
- {
- int fd = -1;
-
- if (argc < 2) {
- printf("The argument is too few\n");
- return 1;
- }
-
- fd = open(argv[1], O_RDONLY);
- if (fd < 0) {
- printf("open %s failed\n", argv[1]);
- return 2;
- }
-
- while (1) {}
-
- close(fd);
- fd = -1;
- return 0;
- }
测试结果
第一次打开设备文件成功,第二次打开设备文件失败
a.定义自旋锁
spinlock_t lock;
b.初始化自旋锁
spin_lock_init(spinlock_t *);
c.获得自旋锁
spin_lock(spinlock_t *); //成功获得自旋锁立即返回,否则自旋在那里直到该自旋锁的保持者释放
spin_trylock(spinlock_t *); //成功获得自旋锁立即返回真,否则返回假,而不是像上一个那样"在原地打转”
d.释放自旋锁
spin_unlock(spinlock_t *);
- #include <linux/spinlock.h>
- 定义spinlock_t类型的变量lock
- spin_lock_init(&lock)后才能正常使用spinlock
-
-
- spin_lock(&lock);
- 临界区
- spin_unlock(&lock);
适用场合:
1. 异常上下文之间或异常上下文与任务上下文之间共享资源时
2. 任务上下文之间且临界区执行时间很短时
3. 互斥问题
openonce.c
- #include <linux/module.h>
- #include <linux/kernel.h>
- #include <linux/fs.h>
- #include <linux/cdev.h>
- #include <linux/wait.h>
- #include <linux/sched.h>
- #include <linux/poll.h>
- #include <linux/uaccess.h>
- #include <asm/atomic.h>
-
- int major = 11;
- int minor = 0;
- int openonce_num = 1;
-
- struct openonce_dev
- {
- struct cdev mydev;
-
- int openflag;
- spinlock_t lock;
- };
-
- struct openonce_dev gmydev;
-
- int openonce_open(struct inode* pnode, struct file* pfile)
- {
- struct openonce_dev* pmydev = NULL;
- pfile->private_data = (void *)(container_of(pnode->i_cdev, struct openonce_dev, mydev));
- pmydev = (struct openonce_dev*)pfile->private_data;
-
- spin_lock(&pmydev->lock);
- if (pmydev->openflag) {
- pmydev->openflag = 0;
- spin_unlock(&pmydev->lock);
- return 0;
- } else {
- spin_unlock(&pmydev->lock);
- printk("The device is opened already\n");
- return -1;
- }
- }
-
- int openonce_close(struct inode* pnode, struct file* pfile)
- {
- struct openonce_dev *pmydev = (struct openonce_dev *)pfile->private_data;
- spin_lock(&pmydev->lock);
- pmydev->openflag = 1;
- spin_unlock(&pmydev->lock);
- return 0;
- }
-
- struct file_operations myops = {
- .owner = THIS_MODULE,
- .open = openonce_open,
- .release = openonce_close,
- };
-
- int __init openonce_init(void)
- {
- int ret = 0;
- dev_t devno = MKDEV(major, minor);
-
- /* 申请设备号 */
- ret = register_chrdev_region(devno, openonce_num, "openonce");
- if (ret) {
- ret = alloc_chrdev_region(&devno, minor, openonce_num, "openonce");
- if (ret) {
- printk("get devno failed\n");
- return -1;
- }
- major = MAJOR(devno);
- }
-
- /* 给struct cdev对象指定操作函数集 */
- cdev_init(&gmydev.mydev, &myops);
-
- /* 给struct cdev对象添加到内核对应的数据结构里 */
- gmydev.mydev.owner = THIS_MODULE;
- cdev_add(&gmydev.mydev, devno, openonce_num);
-
- gmydev.openflag = 1;
-
- spin_lock_init(&gmydev.lock);
- return 0;
- }
-
- void __exit openonce_exit(void)
- {
- dev_t devno = MKDEV(major, minor);
- cdev_del(&gmydev.mydev);
- unregister_chrdev_region(devno, openonce_num);
- }
-
-
- MODULE_LICENSE("GPL");
-
- module_init(openonce_init);
- module_exit(openonce_exit);
test.c
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <unistd.h>
- #include <stdio.h>
- #include <sys/ioctl.h>
-
- int main(int argc, char* argv[])
- {
- int fd = -1;
-
- if (argc < 2) {
- printf("The argument is too few\n");
- return 1;
- }
-
- fd = open(argv[1], O_RDONLY);
- if (fd < 0) {
- printf("open %s failed\n", argv[1]);
- return 2;
- }
-
- while (1) {}
-
- close(fd);
- fd = -1;
- return 0;
- }
测试结果
第一次打开设备文件成功,第二次打开设备文件失败
a.定义信号量
struct semaphore sem;
b.初始化信号量
void sema_init(struct semaphore *sem, int val);
c.获得信号量P
int down(struct semaphore *sem);//深度睡眠
int down_interruptible(struct semaphore *sem);//浅度睡眠
d.释放信号量V
void up(struct semaphore *sem);
#include <linux/semaphore.h>
适用场合:任务上下文之间且临界区执行时间较长时的互斥或同步问题
mychar.c
- #include <linux/module.h>
- #include <linux/kernel.h>
- #include <linux/fs.h>
- #include <linux/cdev.h>
- #include <linux/wait.h>
- #include <linux/sched.h>
- #include <linux/poll.h>
- #include <linux/uaccess.h>
-
- #include "mychar.h"
-
- #define BUF_LEN 100
-
- int major = 11;
- int minor = 0;
- int mychar_num = 1;
-
- struct mychar_dev
- {
- struct cdev mydev;
-
- char mydev_buf[BUF_LEN];
- int curlen;
- struct semaphore sem;
-
- wait_queue_head_t rq;
- wait_queue_head_t wq;
-
- struct fasync_struct* pasync_obj;
- };
-
- struct mychar_dev gmydev;
-
- int mychar_open(struct inode* pnode, struct file* pfile)
- {
- pfile->private_data = (void *)(container_of(pnode->i_cdev, struct mychar_dev, mydev));
- return 0;
- }
-
- int mychar_close(struct inode* pnode, struct file* pfile)
- {
- struct mychar_dev *pmydev = (struct mychar_dev *)pfile->private_data;
- if (pmydev->pasync_obj != NULL) {
- fasync_helper(-1, pfile, 0, &pmydev->pasync_obj);
- }
- return 0;
- }
-
- ssize_t mychar_read(struct file* pfile, char __user *puser, size_t count, loff_t* p_pos)
- {
- struct mychar_dev *pmydev = (struct mychar_dev *)pfile->private_data;
- int size = 0;
- int ret = 0;
-
- down(&pmydev->sem);
- if (pmydev->curlen <= 0) {
- if (pfile->f_flags & O_NONBLOCK) {
- // 非阻塞
- up(&pmydev->sem);
- printk("O_NONBLOCK No Data Read\n");
- return -1;
- } else {
- // 阻塞
- up(&pmydev->sem);
- ret = wait_event_interruptible(pmydev->rq, pmydev->curlen > 0);
- if (ret) {
- printk("Wake up by signal\n");
- return -ERESTARTSYS;
- }
- down(&pmydev->sem);
- }
- }
-
- if (count > pmydev->curlen) {
- size = pmydev->curlen;
- } else {
- size = count;
- }
-
- ret = copy_to_user(puser, pmydev->mydev_buf, size);
- if (ret) {
- up(&pmydev->sem);
- printk("copy_to_user failed\n");
- return -1;
- }
-
- memcpy(pmydev->mydev_buf, pmydev->mydev_buf + size, pmydev->curlen - size);
-
- pmydev->curlen -= size;
-
- up(&pmydev->sem);
- wake_up_interruptible(&pmydev->wq);
-
- return size;
- }
-
- ssize_t mychar_write(struct file* pfile, const char __user *puser, size_t count, loff_t* p_pos)
- {
- struct mychar_dev *pmydev = (struct mychar_dev *)pfile->private_data;
- int size = 0;
- int ret = 0;
-
- down(&pmydev->sem);
- if (pmydev->curlen >= BUF_LEN) {
- if (pfile->f_flags & O_NONBLOCK) {
- up(&pmydev->sem);
- printk("O_NONBLOCK Can not write data\n");
- return -1;
- } else {
- up(&pmydev->sem);
- ret = wait_event_interruptible(pmydev->wq, pmydev->curlen < BUF_LEN);
- if (ret) {
- printk("Wake up by signal\n");
- return -ERESTARTSYS;
- }
- down(&pmydev->sem);
- }
- }
-
- if (count > BUF_LEN - pmydev->curlen) {
- size = BUF_LEN - pmydev->curlen;
- } else {
- size = count;
- }
-
- ret = copy_from_user(pmydev->mydev_buf + pmydev->curlen, puser, size);
- if (ret) {
- up(&pmydev->sem);
- printk("copy_from_user failed\n");
- return -1;
- }
-
- pmydev->curlen += size;
-
- up(&pmydev->sem);
-
- wake_up_interruptible(&pmydev->rq);
-
- if (pmydev->pasync_obj != NULL) {
- kill_fasync(&pmydev->pasync_obj, SIGIO, POLL_IN);
- }
-
- return size;
- }
-
- long mychar_ioctl(struct file* pfile, unsigned int cmd, unsigned long arg)
- {
- int __user *pret = (int*)arg;
- int maxlen = BUF_LEN;
- int ret = 0;
- struct mychar_dev *pmydev = (struct mychar_dev *)pfile->private_data;
-
- switch (cmd) {
- case MYCHAR_IOCTL_GET_MAXLEN:
- ret = copy_to_user(pret, &maxlen, sizeof(int));
- if (ret) {
- printk("copy_to_user MAXLEN failed\n");
- return -1;
- }
- break;
- case MYCHAR_IOCTL_GET_CURLEN:
- down(&pmydev->sem);
- ret = copy_to_user(pret, &pmydev->curlen, sizeof(int));
- up(&pmydev->sem);
- if (ret) {
- printk("copy_to_user CURLEN failed\n");
- return -1;
- }
- break;
- default:
- printk("The cmd is unknow\n");
- return -1;
- }
- return 0;
- }
-
- unsigned int mychar_poll(struct file* pfile, poll_table* ptb)
- {
- struct mychar_dev *pmydev = (struct mychar_dev *)pfile->private_data;
- unsigned int mask = 0;
-
- poll_wait(pfile, &pmydev->rq, ptb);
- poll_wait(pfile, &pmydev->wq, ptb);
-
- down(&pmydev->sem);
- if (pmydev->curlen > 0) {
- mask |= POLLIN | POLLRDNORM;
- }
-
- if (pmydev->curlen < BUF_LEN) {
- mask |= POLLOUT | POLLWRNORM;
- }
- up(&pmydev->sem);
-
- return mask;
- }
-
- int mychar_fasync(int fd, struct file* pfile, int mode) {
- struct mychar_dev *pmydev = (struct mychar_dev *)pfile->private_data;
- return fasync_helper(fd, pfile, mode, &pmydev->pasync_obj);
- }
-
- struct file_operations myops = {
- .owner = THIS_MODULE,
- .open = mychar_open,
- .release = mychar_close,
- .read = mychar_read,
- .write = mychar_write,
- .unlocked_ioctl = mychar_ioctl,
- .poll = mychar_poll,
- .fasync = mychar_fasync,
- };
-
- int __init mychar_init(void)
- {
- int ret = 0;
- dev_t devno = MKDEV(major, minor);
-
- /* 申请设备号 */
- ret = register_chrdev_region(devno, mychar_num, "mychar");
- if (ret) {
- ret = alloc_chrdev_region(&devno, minor, mychar_num, "mychar");
- if (ret) {
- printk("get devno failed\n");
- return -1;
- }
- major = MAJOR(devno);
- }
-
- /* 给struct cdev对象指定操作函数集 */
- cdev_init(&gmydev.mydev, &myops);
-
- /* 给struct cdev对象添加到内核对应的数据结构里 */
- gmydev.mydev.owner = THIS_MODULE;
- cdev_add(&gmydev.mydev, devno, mychar_num);
-
- init_waitqueue_head(&gmydev.rq);
- init_waitqueue_head(&gmydev.wq);
-
- sema_init(&gmydev.sem, 1);
- return 0;
- }
-
- void __exit mychar_exit(void)
- {
- dev_t devno = MKDEV(major, minor);
- cdev_del(&gmydev.mydev);
- unregister_chrdev_region(devno, mychar_num);
- }
-
-
- MODULE_LICENSE("GPL");
-
- module_init(mychar_init);
- module_exit(mychar_exit);
a.初始化
struct mutex my_mutex;
mutex_init(&my_mutex);
b.获取互斥体
void mutex_lock(struct mutex *lock);
c.释放互斥体
void mutex_unlock(struct mutex *lock);
1. 定义对应类型的变量
2. 初始化对应变量
P/加锁
临界区
V/解锁
#include <linux/mutex.h>
适用场合:任务上下文之间且临界区执行时间较长时的互斥问题
mychar.c
- #include <linux/module.h>
- #include <linux/kernel.h>
- #include <linux/fs.h>
- #include <linux/cdev.h>
- #include <linux/wait.h>
- #include <linux/sched.h>
- #include <linux/poll.h>
- #include <linux/uaccess.h>
-
- #include "mychar.h"
-
- #define BUF_LEN 100
-
- int major = 11;
- int minor = 0;
- int mychar_num = 1;
-
- struct mychar_dev
- {
- struct cdev mydev;
-
- char mydev_buf[BUF_LEN];
- int curlen;
- struct mutex lock;
-
- wait_queue_head_t rq;
- wait_queue_head_t wq;
-
- struct fasync_struct* pasync_obj;
- };
-
- struct mychar_dev gmydev;
-
- int mychar_open(struct inode* pnode, struct file* pfile)
- {
- pfile->private_data = (void *)(container_of(pnode->i_cdev, struct mychar_dev, mydev));
- return 0;
- }
-
- int mychar_close(struct inode* pnode, struct file* pfile)
- {
- struct mychar_dev *pmydev = (struct mychar_dev *)pfile->private_data;
- if (pmydev->pasync_obj != NULL) {
- fasync_helper(-1, pfile, 0, &pmydev->pasync_obj);
- }
- return 0;
- }
-
- ssize_t mychar_read(struct file* pfile, char __user *puser, size_t count, loff_t* p_pos)
- {
- struct mychar_dev *pmydev = (struct mychar_dev *)pfile->private_data;
- int size = 0;
- int ret = 0;
-
- mutex_lock(&pmydev->lock);
- if (pmydev->curlen <= 0) {
- if (pfile->f_flags & O_NONBLOCK) {
- // 非阻塞
- mutex_unlock(&pmydev->lock);
- printk("O_NONBLOCK No Data Read\n");
- return -1;
- } else {
- // 阻塞
- mutex_unlock(&pmydev->lock);
- ret = wait_event_interruptible(pmydev->rq, pmydev->curlen > 0);
- if (ret) {
- printk("Wake up by signal\n");
- return -ERESTARTSYS;
- }
- mutex_lock(&pmydev->lock);
- }
- }
-
- if (count > pmydev->curlen) {
- size = pmydev->curlen;
- } else {
- size = count;
- }
-
- ret = copy_to_user(puser, pmydev->mydev_buf, size);
- if (ret) {
- mutex_unlock(&pmydev->lock);
- printk("copy_to_user failed\n");
- return -1;
- }
-
- memcpy(pmydev->mydev_buf, pmydev->mydev_buf + size, pmydev->curlen - size);
-
- pmydev->curlen -= size;
-
- mutex_unlock(&pmydev->lock);
- wake_up_interruptible(&pmydev->wq);
-
- return size;
- }
-
- ssize_t mychar_write(struct file* pfile, const char __user *puser, size_t count, loff_t* p_pos)
- {
- struct mychar_dev *pmydev = (struct mychar_dev *)pfile->private_data;
- int size = 0;
- int ret = 0;
-
- mutex_lock(&pmydev->lock);
- if (pmydev->curlen >= BUF_LEN) {
- if (pfile->f_flags & O_NONBLOCK) {
- mutex_unlock(&pmydev->lock);
- printk("O_NONBLOCK Can not write data\n");
- return -1;
- } else {
- mutex_unlock(&pmydev->lock);
- ret = wait_event_interruptible(pmydev->wq, pmydev->curlen < BUF_LEN);
- if (ret) {
- printk("Wake up by signal\n");
- return -ERESTARTSYS;
- }
- mutex_lock(&pmydev->lock);
- }
- }
-
- if (count > BUF_LEN - pmydev->curlen) {
- size = BUF_LEN - pmydev->curlen;
- } else {
- size = count;
- }
-
- ret = copy_from_user(pmydev->mydev_buf + pmydev->curlen, puser, size);
- if (ret) {
- mutex_unlock(&pmydev->lock);
- printk("copy_from_user failed\n");
- return -1;
- }
-
- pmydev->curlen += size;
-
- mutex_unlock(&pmydev->lock);
-
- wake_up_interruptible(&pmydev->rq);
-
- if (pmydev->pasync_obj != NULL) {
- kill_fasync(&pmydev->pasync_obj, SIGIO, POLL_IN);
- }
-
- return size;
- }
-
- long mychar_ioctl(struct file* pfile, unsigned int cmd, unsigned long arg)
- {
- int __user *pret = (int*)arg;
- int maxlen = BUF_LEN;
- int ret = 0;
- struct mychar_dev *pmydev = (struct mychar_dev *)pfile->private_data;
-
- switch (cmd) {
- case MYCHAR_IOCTL_GET_MAXLEN:
- ret = copy_to_user(pret, &maxlen, sizeof(int));
- if (ret) {
- printk("copy_to_user MAXLEN failed\n");
- return -1;
- }
- break;
- case MYCHAR_IOCTL_GET_CURLEN:
- mutex_lock(&pmydev->lock);
- ret = copy_to_user(pret, &pmydev->curlen, sizeof(int));
- mutex_unlock(&pmydev->lock);
- if (ret) {
- printk("copy_to_user CURLEN failed\n");
- return -1;
- }
- break;
- default:
- printk("The cmd is unknow\n");
- return -1;
- }
- return 0;
- }
-
- unsigned int mychar_poll(struct file* pfile, poll_table* ptb)
- {
- struct mychar_dev *pmydev = (struct mychar_dev *)pfile->private_data;
- unsigned int mask = 0;
-
- poll_wait(pfile, &pmydev->rq, ptb);
- poll_wait(pfile, &pmydev->wq, ptb);
-
- mutex_lock(&pmydev->lock);
- if (pmydev->curlen > 0) {
- mask |= POLLIN | POLLRDNORM;
- }
-
- if (pmydev->curlen < BUF_LEN) {
- mask |= POLLOUT | POLLWRNORM;
- }
- mutex_unlock(&pmydev->lock);
-
- return mask;
- }
-
- int mychar_fasync(int fd, struct file* pfile, int mode) {
- struct mychar_dev *pmydev = (struct mychar_dev *)pfile->private_data;
- return fasync_helper(fd, pfile, mode, &pmydev->pasync_obj);
- }
-
- struct file_operations myops = {
- .owner = THIS_MODULE,
- .open = mychar_open,
- .release = mychar_close,
- .read = mychar_read,
- .write = mychar_write,
- .unlocked_ioctl = mychar_ioctl,
- .poll = mychar_poll,
- .fasync = mychar_fasync,
- };
-
- int __init mychar_init(void)
- {
- int ret = 0;
- dev_t devno = MKDEV(major, minor);
-
- /* 申请设备号 */
- ret = register_chrdev_region(devno, mychar_num, "mychar");
- if (ret) {
- ret = alloc_chrdev_region(&devno, minor, mychar_num, "mychar");
- if (ret) {
- printk("get devno failed\n");
- return -1;
- }
- major = MAJOR(devno);
- }
-
- /* 给struct cdev对象指定操作函数集 */
- cdev_init(&gmydev.mydev, &myops);
-
- /* 给struct cdev对象添加到内核对应的数据结构里 */
- gmydev.mydev.owner = THIS_MODULE;
- cdev_add(&gmydev.mydev, devno, mychar_num);
-
- init_waitqueue_head(&gmydev.rq);
- init_waitqueue_head(&gmydev.wq);
-
- mutex_init(&gmydev.lock);
- return 0;
- }
-
- void __exit mychar_exit(void)
- {
- dev_t devno = MKDEV(major, minor);
- cdev_del(&gmydev.mydev);
- unregister_chrdev_region(devno, mychar_num);
- }
-
-
- MODULE_LICENSE("GPL");
-
- module_init(mychar_init);
- module_exit(mychar_exit);
1. 不允许睡眠的上下文需要采用忙等待类,可以睡眠的上下文可以采用阻塞类。在异常上下文中访问的竞争资源一定采用忙等待类。
2. 临界区操作较长的应用建议采用阻塞类,临界区很短的操作建议采用忙等待类。
3. 中断屏蔽仅在有与中断上下文共享资源时使用。
4. 共享资源仅是一个简单整型量时用原子变量
硬件有一个时钟装置,该装置每隔一定时间发出一个时钟中断(称为一次时钟嘀嗒-tick),对应的中断处理程序就将全局变量jiffies_64加1
jiffies_64 是一个全局64位整型, jiffies全局变量为其低32位的全局变量,程序中一般用jiffies
HZ:可配置的宏,表示1秒钟产生的时钟中断次数,一般设为100或200
1. 短延迟:忙等待
- 1. void ndelay(unsigned long nsecs)
- 2. void udelay(unsigned long usecs)
- 3. void mdelay(unsigned long msecs)
2. 长延迟:忙等待
使用jiffies比较宏来实现
- time_after(a,b) //a > b
- time_before(a,b) //a < b
-
- // 延迟100个jiffies
- unsigned long delay = jiffies + 100;
- while(time_before(jiffies,delay))
- {
- ;
- }
-
- // 延迟2s
- unsigned long delay = jiffies + 2*HZ;
- while(time_before(jiffies,delay))
- {
- ;
- }
3. 睡眠延迟----阻塞类
- void msleep(unsigned int msecs);
- unsigned long msleep_interruptible(unsigned int msecs);
延时机制的选择原则:
1. 异常上下文中只能采用忙等待类
2. 任务上下文短延迟采用忙等待类,长延迟采用阻塞类
(1)定义定时器结构体
- struct timer_list
- {
- struct list_head entry;
- unsigned long expires; // 期望的时间值 jiffies + x * HZ
- void (*function)(unsigned long); // 时间到达后,执行的回调函数,软中断异常上下文
- unsigned long data;
- };
(2)初始化定时器
init_timer(struct timer_list *)
(3)增加定时器 ------ 定时器开始计时
void add_timer(struct timer_list *timer);
(4)删除定时器 -------定时器停止工作
int del_timer(struct timer_list * timer);
(5)修改定时器
int mod_timer(struct timer_list *timer, unsigned long expires);
参考代码模板
- 定义struct timer_list tl类型的变量
-
- init_timer(...);//模块入口函数
-
- //模块入口函数或open或希望定时器开始工作的地方
- tl.expires = jiffies + n * HZ //n秒
- tl.function = xxx_func;
- tl.data = ...;
-
- add_timer(....);
-
-
- //不想让定时器继续工作时
- del_timer(....);
-
- void xxx_func(unsigned long arg)
- {
- ......
- mod_timer(....);//如需要定时器继续隔指定时间再次调用本函数
- }
内核将物理内存等分成N块4KB,称之为一页,每页都用一个struct page来表示,采用伙伴关系算法维护
内核地址空间划分图:
3G~3G+896M:低端内存,直接映射 虚拟地址 = 3G + 物理地址
细分为:ZONE_DMA、ZONE_NORMAL
分配方式:
1. kmalloc:小内存分配,slab算法
2. get_free_page:整页分配,2的n次方页,n最大为10
大于3G+896M:高端内存
细分为:vmalloc区、持久映射区、固定映射区
分配方式:vmalloc:虚拟地址连续,物理地址不连续
函数原型:
- void *kmalloc(size_t size, gfp_t flags);
- void *kzalloc(size_t size, gfp_t flags); // 分配内存同时置零
kmalloc() 申请的内存位于直接映射区域,而且在物理上也是连续的,它们与真实的物理地址只有一个固定的偏移,因为存在较简单的转换关系,所以对申请的内存大小有限制,不能超过128KB。
较常用的 flags(分配内存的方法):
GFP_ATOMIC —— 分配内存的过程是一个原子过程,分配内存的过程不会被(高优先级进程或中断)打断;
GFP_KERNEL —— 正常分配内存;
GFP_DMA —— 给 DMA 控制器分配内存,需要使用该标志(DMA要求分配虚拟地址和物理地址连续)。
flags 的参考用法:
|– 进程上下文,可以睡眠 GFP_KERNEL
|– 异常上下文,不可以睡眠 GFP_ATOMIC
| |– 中断处理程序 GFP_ATOMIC
| |– 软中断 GFP_ATOMIC
| |– Tasklet GFP_ATOMIC
|– 用于DMA的内存,可以睡眠 GFP_DMA | GFP_KERNEL
|– 用于DMA的内存,不可以睡眠 GFP_DMA |GFP_ATOMIC
对应的内存释放函数为:
void kfree(const void *objp);
void *vmalloc(unsigned long size);
vmalloc() 函数则会在虚拟内存空间给出一块连续的内存区,但这片连续的虚拟内存在物理内存中并不一定连续。由于 vmalloc() 没有保证申请到的是连续的物理内存,因此对申请的内存大小没有限制,如果需要申请较大的内存空间就需要用此函数了。
对应的内存释放函数为:
void vfree(const void *addr);
注意:vmalloc() 和 vfree() 可以睡眠,因此不能从异常上下文调用。
kmalloc()、kzalloc()、vmalloc() 的共同特点是:
1. 用于申请内核空间的内存;
2. 内存以字节为单位进行分配;
3. 所分配的内存虚拟地址上连续;
kmalloc()、kzalloc()、vmalloc() 的区别是:
1. kzalloc 是强制清零的 kmalloc 操作;(以下描述不区分 kmalloc 和 kzalloc)
2. kmalloc 分配的内存大小有限制(128KB),而 vmalloc 没有限制;
3. kmalloc 可以保证分配的内存物理地址是连续的,但是 vmalloc 不能保证;
4. kmalloc 分配内存的过程可以是原子过程(使用 GFP_ATOMIC),而 vmalloc 分配内存时则可能产生阻塞;
5. kmalloc 分配内存的开销小,因此 kmalloc 比 vmalloc 要快;
一般情况下,内存只有在要被 DMA 访问的时候才需要物理上连续,但为了性能上的考虑,内核中一般使用 kmalloc(),而只有在需要获得大块内存时才使用 vmalloc()。
分配选择原则:
1. 小内存(< 128k)用kmalloc,大内存用vmalloc或get_free_page
2. 如果需要比较大的内存,并且要求使用效率较高时用get_free_page,否则用vmalloc
mychar.c
- #include <linux/module.h>
- #include <linux/kernel.h>
- #include <linux/fs.h>
- #include <linux/cdev.h>
- #include <linux/wait.h>
- #include <linux/sched.h>
- #include <linux/poll.h>
- #include <linux/mm.h>
- #include <linux/slab.h>
- #include <asm/uaccess.h>
-
- #include "mychar.h"
-
- #define BUF_LEN 100
-
- int major = 11;
- int minor = 0;
- int mychar_num = 1;
-
- struct mychar_dev
- {
- struct cdev mydev;
-
- char mydev_buf[BUF_LEN];
- int curlen;
- struct mutex lock;
-
- wait_queue_head_t rq;
- wait_queue_head_t wq;
-
- struct fasync_struct* pasync_obj;
- };
-
- struct mychar_dev *pgmydev = NULL;
-
- int mychar_open(struct inode* pnode, struct file* pfile)
- {
- pfile->private_data = (void *)(container_of(pnode->i_cdev, struct mychar_dev, mydev));
- return 0;
- }
-
- int mychar_close(struct inode* pnode, struct file* pfile)
- {
- struct mychar_dev *pmydev = (struct mychar_dev *)pfile->private_data;
- if (pmydev->pasync_obj != NULL) {
- fasync_helper(-1, pfile, 0, &pmydev->pasync_obj);
- }
- return 0;
- }
-
- ssize_t mychar_read(struct file* pfile, char __user *puser, size_t count, loff_t* p_pos)
- {
- struct mychar_dev *pmydev = (struct mychar_dev *)pfile->private_data;
- int size = 0;
- int ret = 0;
-
- mutex_lock(&pmydev->lock);
- if (pmydev->curlen <= 0) {
- if (pfile->f_flags & O_NONBLOCK) {
- // 非阻塞
- mutex_unlock(&pmydev->lock);
- printk("O_NONBLOCK No Data Read\n");
- return -1;
- } else {
- // 阻塞
- mutex_unlock(&pmydev->lock);
- ret = wait_event_interruptible(pmydev->rq, pmydev->curlen > 0);
- if (ret) {
- printk("Wake up by signal\n");
- return -ERESTARTSYS;
- }
- mutex_lock(&pmydev->lock);
- }
- }
-
- if (count > pmydev->curlen) {
- size = pmydev->curlen;
- } else {
- size = count;
- }
-
- ret = copy_to_user(puser, pmydev->mydev_buf, size);
- if (ret) {
- mutex_unlock(&pmydev->lock);
- printk("copy_to_user failed\n");
- return -1;
- }
-
- memcpy(pmydev->mydev_buf, pmydev->mydev_buf + size, pmydev->curlen - size);
-
- pmydev->curlen -= size;
-
- mutex_unlock(&pmydev->lock);
- wake_up_interruptible(&pmydev->wq);
-
- return size;
- }
-
- ssize_t mychar_write(struct file* pfile, const char __user *puser, size_t count, loff_t* p_pos)
- {
- struct mychar_dev *pmydev = (struct mychar_dev *)pfile->private_data;
- int size = 0;
- int ret = 0;
-
- mutex_lock(&pmydev->lock);
- if (pmydev->curlen >= BUF_LEN) {
- if (pfile->f_flags & O_NONBLOCK) {
- mutex_unlock(&pmydev->lock);
- printk("O_NONBLOCK Can not write data\n");
- return -1;
- } else {
- mutex_unlock(&pmydev->lock);
- ret = wait_event_interruptible(pmydev->wq, pmydev->curlen < BUF_LEN);
- if (ret) {
- printk("Wake up by signal\n");
- return -ERESTARTSYS;
- }
- mutex_lock(&pmydev->lock);
- }
- }
-
- if (count > BUF_LEN - pmydev->curlen) {
- size = BUF_LEN - pmydev->curlen;
- } else {
- size = count;
- }
-
- ret = copy_from_user(pmydev->mydev_buf + pmydev->curlen, puser, size);
- if (ret) {
- mutex_unlock(&pmydev->lock);
- printk("copy_from_user failed\n");
- return -1;
- }
-
- pmydev->curlen += size;
-
- mutex_unlock(&pmydev->lock);
-
- wake_up_interruptible(&pmydev->rq);
-
- if (pmydev->pasync_obj != NULL) {
- kill_fasync(&pmydev->pasync_obj, SIGIO, POLL_IN);
- }
-
- return size;
- }
-
- long mychar_ioctl(struct file* pfile, unsigned int cmd, unsigned long arg)
- {
- int __user *pret = (int*)arg;
- int maxlen = BUF_LEN;
- int ret = 0;
- struct mychar_dev *pmydev = (struct mychar_dev *)pfile->private_data;
-
- switch (cmd) {
- case MYCHAR_IOCTL_GET_MAXLEN:
- ret = copy_to_user(pret, &maxlen, sizeof(int));
- if (ret) {
- printk("copy_to_user MAXLEN failed\n");
- return -1;
- }
- break;
- case MYCHAR_IOCTL_GET_CURLEN:
- mutex_lock(&pmydev->lock);
- ret = copy_to_user(pret, &pmydev->curlen, sizeof(int));
- mutex_unlock(&pmydev->lock);
- if (ret) {
- printk("copy_to_user CURLEN failed\n");
- return -1;
- }
- break;
- default:
- printk("The cmd is unknow\n");
- return -1;
- }
- return 0;
- }
-
- unsigned int mychar_poll(struct file* pfile, poll_table* ptb)
- {
- struct mychar_dev *pmydev = (struct mychar_dev *)pfile->private_data;
- unsigned int mask = 0;
-
- poll_wait(pfile, &pmydev->rq, ptb);
- poll_wait(pfile, &pmydev->wq, ptb);
-
- mutex_lock(&pmydev->lock);
- if (pmydev->curlen > 0) {
- mask |= POLLIN | POLLRDNORM;
- }
-
- if (pmydev->curlen < BUF_LEN) {
- mask |= POLLOUT | POLLWRNORM;
- }
- mutex_unlock(&pmydev->lock);
-
- return mask;
- }
-
- int mychar_fasync(int fd, struct file* pfile, int mode) {
- struct mychar_dev *pmydev = (struct mychar_dev *)pfile->private_data;
- return fasync_helper(fd, pfile, mode, &pmydev->pasync_obj);
- }
-
- struct file_operations myops = {
- .owner = THIS_MODULE,
- .open = mychar_open,
- .release = mychar_close,
- .read = mychar_read,
- .write = mychar_write,
- .unlocked_ioctl = mychar_ioctl,
- .poll = mychar_poll,
- .fasync = mychar_fasync,
- };
-
- int __init mychar_init(void)
- {
- int ret = 0;
- dev_t devno = MKDEV(major, minor);
-
- /* 申请设备号 */
- ret = register_chrdev_region(devno, mychar_num, "mychar");
- if (ret) {
- ret = alloc_chrdev_region(&devno, minor, mychar_num, "mychar");
- if (ret) {
- printk("get devno failed\n");
- return -1;
- }
- major = MAJOR(devno);
- }
-
- pgmydev = (struct mychar_dev*)kmalloc(sizeof(struct mychar_dev), GFP_KERNEL);
- if (NULL == pgmydev) {
- unregister_chrdev_region(devno, mychar_num);
- printk("kmalloc for struct mychar_dev failed\n");
- return -1;
- }
-
- /* 给struct cdev对象指定操作函数集 */
- cdev_init(&pgmydev->mydev, &myops);
-
- /* 给struct cdev对象添加到内核对应的数据结构里 */
- pgmydev->mydev.owner = THIS_MODULE;
- cdev_add(&pgmydev->mydev, devno, mychar_num);
-
- init_waitqueue_head(&pgmydev->rq);
- init_waitqueue_head(&pgmydev->wq);
-
- mutex_init(&pgmydev->lock);
- return 0;
- }
-
- void __exit mychar_exit(void)
- {
- dev_t devno = MKDEV(major, minor);
- cdev_del(&pgmydev->mydev);
- unregister_chrdev_region(devno, mychar_num);
-
- kfree(pgmydev);
- pgmydev = NULL;
- }
-
-
- MODULE_LICENSE("GPL");
-
- module_init(mychar_init);
- module_exit(mychar_exit);
两种方式:
1. IO端口:X86上用IO指令访问
2. IO内存:外设寄存器在SOC芯片手册上都有相应物理地址
IO内存访问接口:
- static inline void __iomem *ioremap(unsigned long offset, unsigned long size)
- /*
- 功能:实现IO管脚的映射
- 参数:offset:该管脚的偏移地址
- Size:该管脚映射空间的大小
- 返回值:成功返回映射的虚拟地址,失败NULL
- */
-
- static inline void iounmap(volatile void __iomem *addr)
- /*
- 功能:解除io管脚的映射
- 参数:addr:io管脚映射的地址
- */
-
- unsigned readb(void *addr); // 1字节 或ioread8(void *addr)
- unsigned readw(void *addr); // 2字节 或ioread16(void *addr)
- unsigned readl(void *addr); // 4字节 或ioread32(void *addr)
- /*
- 功能:读取寄存器的值
- 参数:addr 地址
- 返回值:读到的数据
- */
-
- void writeb(unsigned value, void *addr); // 1字节 或iowrite8(u8 value, void *addr)
- void writew(unsigned value, void *addr); // 2字节 或iowrite16(u16 value, void *addr)
- void writel(unsigned value, void *addr); // 4字节 或iowrite32(u32 value, void *addr)
- /*
- 功能:向指定的寄存器中,写入数据。
- 参数:value:待写入寄存器中的数据
- Address:寄存器的虚拟地址
- */
原理图
SOC芯片手册
GPX2_7 led2 GPX2CON----0x11000C40---28~31-----0001 GPX2DAT----0x11000C44-----7
GPX1_0 led3 GPX1CON----0x11000C20--- 0~3-----0001 GPX1DAT----0x11000C24-----0
GPF3_4 led4 GPF3CON----0x114001E0--- 16~19-----0001 GPF3DAT----0x114001E4-----4
GPF3_5 led5 GPF3CON----0x114001E0--- 20~23-----0001 GPF3DAT----0x114001E4-----5
编写驱动步骤
a. 设计设备数据类型
b. 考虑需要支持的函数
c. 模块入口:ioremap + 设置成输出
d. 模块出口:iounmap
e. 编写关灯函数和开灯函数,实现ioctl
leddrv.c
- #include <linux/module.h>
- #include <linux/kernel.h>
- #include <linux/fs.h>
- #include <linux/cdev.h>
- #include <linux/wait.h>
- #include <linux/sched.h>
- #include <linux/poll.h>
- #include <linux/slab.h>
- #include <linux/mm.h>
- #include <linux/io.h>
- #include <asm/uaccess.h>
- #include <asm/atomic.h>
-
- #include "leddrv.h"
-
- #define GPX1CON 0x11000C20
- #define GPX1DAT 0x11000C24
-
- #define GPX2CON 0x11000C40
- #define GPX2DAT 0x11000C44
-
- #define GPF3CON 0x114001E0
- #define GPF3DAT 0x114001E4
-
- int major = 11;
- int minor = 0;
- int myled_num = 1;
-
- struct myled_dev
- {
- struct cdev mydev;
-
- volatile unsigned long *pled2_con;
- volatile unsigned long *pled2_dat;
-
- volatile unsigned long *pled3_con;
- volatile unsigned long *pled3_dat;
-
- volatile unsigned long *pled4_con;
- volatile unsigned long *pled4_dat;
-
- volatile unsigned long *pled5_con;
- volatile unsigned long *pled5_dat;
- };
-
- struct myled_dev *pgmydev = NULL;
-
- int myled_open(struct inode *pnode, struct file *pfile)
- {
- pfile->private_data =(void*)(container_of(pnode->i_cdev,struct myled_dev,mydev));
- return 0;
- }
-
- int myled_close(struct inode *pnode, struct file *pfile)
- {
- return 0;
- }
-
- void led_on(struct myled_dev *pmydev, int ledno)
- {
- switch (ledno) {
- case 2:
- writel(readl(pmydev->pled2_dat) | (0x1 << 7), pmydev->pled2_dat);
- break;
- case 3:
- writel(readl(pmydev->pled3_dat) | (0x1 << 0), pmydev->pled3_dat);
- break;
- case 4:
- writel(readl(pmydev->pled4_dat) | (0x1 << 4), pmydev->pled4_dat);
- break;
- case 5:
- writel(readl(pmydev->pled5_dat) | (0x1 << 5), pmydev->pled5_dat);
- break;
- }
- }
-
- void led_off(struct myled_dev *pmydev, int ledno)
- {
- switch (ledno) {
- case 2:
- writel(readl(pmydev->pled2_dat) & (~(0x1 << 7)), pmydev->pled2_dat);
- break;
- case 3:
- writel(readl(pmydev->pled3_dat) & (~(0x1 << 0)), pmydev->pled3_dat);
- break;
- case 4:
- writel(readl(pmydev->pled4_dat) & (~(0x1 << 4)), pmydev->pled4_dat);
- break;
- case 5:
- writel(readl(pmydev->pled5_dat) & (~(0x1 << 5)), pmydev->pled5_dat);
- break;
- }
- }
-
- long myled_ioctl(struct file *pfile, unsigned int cmd, unsigned long arg)
- {
- struct myled_dev *pmydev = (struct myled_dev*)pfile->private_data;
-
- if (arg < 2 || arg > 5) {
- return -1;
- }
-
- switch (cmd) {
- case MY_LED_ON:
- led_on(pmydev, arg);
- break;
- case MY_LED_OFF:
- led_off(pmydev, arg);
- break;
- default:
- return -1;
- }
-
- return 0;
- }
-
- struct file_operations myops = {
- .owner = THIS_MODULE,
- .open = myled_open,
- .release = myled_close,
- .unlocked_ioctl = myled_ioctl,
- };
-
- void ioremap_ledreg(struct myled_dev *pmydev)
- {
- pmydev->pled2_con = ioremap(GPX2CON, 4);
- pmydev->pled2_dat = ioremap(GPX2DAT, 4);
-
- pmydev->pled3_con = ioremap(GPX1CON, 4);
- pmydev->pled3_dat = ioremap(GPX1DAT, 4);
-
- pmydev->pled4_con = ioremap(GPF3CON, 4);
- pmydev->pled4_dat = ioremap(GPF3DAT, 4);
-
- pmydev->pled5_con = pmydev->pled4_con;
- pmydev->pled5_dat = pmydev->pled4_dat;
- }
-
- void iounmap_ledreg(struct myled_dev *pmydev)
- {
- iounmap(pmydev->pled2_con);
- iounmap(pmydev->pled2_dat);
- pmydev->pled2_con = NULL;
- pmydev->pled2_dat = NULL;
-
- iounmap(pmydev->pled3_con);
- iounmap(pmydev->pled3_dat);
- pmydev->pled3_con = NULL;
- pmydev->pled3_dat = NULL;
-
- iounmap(pmydev->pled4_con);
- iounmap(pmydev->pled4_dat);
- pmydev->pled4_con = NULL;
- pmydev->pled4_dat = NULL;
-
- pmydev->pled5_con = NULL;
- pmydev->pled5_dat = NULL;
- }
-
- void set_output_ledconreg(struct myled_dev *pmydev)
- {
- writel((readl(pmydev->pled2_con) & (~(0xF << 28))) | (0x1 << 28), pmydev->pled2_con);
- writel((readl(pmydev->pled3_con) & (~(0xF << 0))) | (0x1 << 0), pmydev->pled3_con);
- writel((readl(pmydev->pled4_con) & (~(0xF << 16))) | (0x1 << 16), pmydev->pled4_con);
- writel((readl(pmydev->pled5_con) & (~(0xF << 20))) | (0x1 << 20), pmydev->pled5_con);
-
- writel(readl(pmydev->pled2_dat) & (~(0x1 << 7)), pmydev->pled2_dat);
- writel(readl(pmydev->pled3_dat) & (~(0x1 << 0)), pmydev->pled3_dat);
- writel(readl(pmydev->pled4_dat) & (~(0x1 << 4)), pmydev->pled4_dat);
- writel(readl(pmydev->pled5_dat) & (~(0x1 << 5)), pmydev->pled5_dat);
- }
-
- int __init myled_init(void)
- {
- int ret = 0;
- dev_t devno = MKDEV(major, minor);
-
- /* 申请设备号 */
- ret = register_chrdev_region(devno, myled_num, "myled");
- if (ret) {
- ret = alloc_chrdev_region(&devno, minor, myled_num, "myled");
- if (ret) {
- printk("get devno failed\n");
- return -1;
- }
- major = MAJOR(devno);
- }
-
- pgmydev = (struct myled_dev*)kmalloc(sizeof(struct myled_dev), GFP_KERNEL);
- if (NULL == pgmydev) {
- unregister_chrdev_region(devno, myled_num);
- printk("kmalloc failed\n");
- return -1;
- }
- memset(pgmydev, 0, sizeof(struct myled_dev));
-
- /* 给struct cdev对象指定操作函数集 */
- cdev_init(&pgmydev->mydev, &myops);
-
- /* 给struct cdev对象添加到内核对应的数据结构里 */
- pgmydev->mydev.owner = THIS_MODULE;
- cdev_add(&pgmydev->mydev, devno, myled_num);
-
- /* ioremap */
- ioremap_ledreg(pgmydev);
-
- /* con-register set output */
- set_output_ledconreg(pgmydev);
-
- return 0;
- }
-
- void __exit myled_exit(void)
- {
- dev_t devno = MKDEV(major, minor);
-
- /* iounmap */
- iounmap_ledreg(pgmydev);
-
- cdev_del(&pgmydev->mydev);
- unregister_chrdev_region(devno, myled_num);
-
- kfree(pgmydev);
- pgmydev = NULL;
- }
-
-
- MODULE_LICENSE("GPL");
-
- module_init(myled_init);
- module_exit(myled_exit);
leddrv.h
- #ifndef LED_DRIVER_H
- #define LED_DRIVER_H
-
- #define LED_DEV_MAGIC 'g'
-
- #define MY_LED_OFF _IO(LED_DEV_MAGIC, 0)
- #define MY_LED_ON _IO(LED_DEV_MAGIC, 1)
-
- #endif
testled.c
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <sys/ioctl.h>
- #include <unistd.h>
- #include <stdio.h>
-
- #include "leddrv.h"
-
- int main(int argc, char *argv[])
- {
- int fd = -1;
- int onoff = 0;
- int no = 0;
-
- if (argc < 4) {
- printf("The argument is too few\n");
- return 1;
- }
-
- sscanf(argv[2], "%d", &onoff);
- sscanf(argv[3], "%d", &no);
-
- if (no < 2 || no > 5) {
- printf("led-no is invalid\n");
- return 2;
- }
-
- fd = open(argv[1], O_RDONLY);
- if (fd < 0) {
- printf("open %s failed\n", argv[1]);
- return 3;
- }
-
- if (onoff) {
- ioctl(fd, MY_LED_ON, no);
- } else {
- ioctl(fd, MY_LED_OFF, no);
- }
-
- close(fd);
- fd = -1;
- return 0;
- }
测试结果
ubuntu上将 leddrv.c 编译成 arm 架构的 ko 文件,将 testled.c 编译成 arm 架构的可执行文件,再将这两个文件拷贝到挂载的根文件系统目录下
- shrek@ubuntu16:~/share/mydrivercode$ make ARCH=arm
- shrek@ubuntu16:~/share/mydrivercode$ arm-none-linux-gnueabi-gcc ./testled.c -o testled
- shrek@ubuntu16:~/share/mydrivercode$ cp leddrv.ko /opt/4412/rootfs/
- shrek@ubuntu16:~/share/mydrivercode$ cp testled /opt/4412/rootfs/
开发板上挂载好根文件系统,执行如下命令
- [root@farsight]#insmod leddrv.ko
- [root@farsight]#cat /proc/devices | grep led
- 11 myled
- [root@farsight]#mknod /dev/leddev c 11 0
- [root@farsight]#./testled /dev/leddev 1 2
- [root@farsight]#./testled /dev/leddev 1 3
- [root@farsight]#./testled /dev/leddev 1 4
- [root@farsight]#./testled /dev/leddev 1 5
- [root@farsight]#./testled /dev/leddev 0 5
- [root@farsight]#./testled /dev/leddev 0 4
- [root@farsight]#./testled /dev/leddev 0 3
- [root@farsight]#./testled /dev/leddev 0 2
看到开发板上的LED灯可以控制亮灭
减少垃圾代码
减轻驱动开发工作量
驱动代码和设备信息分离
参考Open Firmware设计
用来记录硬件平台中各种硬件设备的属性信息
两种源文件:
1. xxxxx.dts dts是device tree source的缩写
2. xxxxx.dtsi dtsi是device tree source include的缩写,意味着这样源文件用于被dts文件包含用
dts文件主体内容由多个节点组成
每个节点可以包含0或多个子节点,形成树状关系
每个dts文件都有一个根节点,其它节点都是它的子孙
根节点一般来描述整个开发板硬件平台,其它节点用来表示具体设备、总线的属性信息
各个节点可以有多个属性,每个属性用key-value键值对来表示
节点语法:
- [label:] node-name[@unit-address] {
- [properties definitions];
- [child nodes];
- };
-
- []表示可选项
- label:可选项,节点别名,为了缩短节点访问路径,后续节点中可以使用 &label 来表示引用指定节点
- node-name:节点名
- unit-address:可选项,设备地址,一般填写该设备寄存器组或内存块的首地址
- properties definitions:可选项,属性定义
- child nodes:可选项,子节点
属性语法:
- [label:] property-name = value;
- [label:] property-name;
-
- 属性可以无值
- 有值的属性,可以有三种取值:
- 1. arrays of cells(1个或多个32位数据, 64位数据使用2个32位数据表示,空格分隔),用尖括号表示(< >)
- 2. string(字符串), 用双引号表示(" ")
- 3. bytestring(1个或多个字节,空格分隔),用方括号表示([])
- 4. 用,分隔的多值
根节点表示整块开发板的信息
- #address-cells // 在子节点的reg属性中, 使用多少个u32整数来描述地址(address)
- #size-cells // 在子节点的reg属性中, 使用多少个u32整数来描述大小(size)
- compatible // 定义一系列的字符串, 用来指定内核中哪个machine_desc可以支持本设备,即描述其兼容哪些平台
- model // 比如有2款板子配置基本一致, 它们的compatible是一样的,那么就通过model来分辨这2款板子
所有设备树文件的必需节点,它定义了系统物理内存的 layout
- device_type = "memory";
- reg //用来指定内存的地址、大小
传递内核启动时使用的参数parameter
bootargs //字符串,内核启动参数, 跟u-boot中设置的bootargs作用一样
/cpus节点下有1个或多个cpu子节点,cpu子节点中用reg属性用来标明自己是哪一个cpu
所以 /cpus 中有以下2个属性:
- #address-cells // 在它的子节点的reg属性中, 使用多少个u32整数来描述地址(address)
- #size-cells // 在它的子节点的reg属性中, 使用多少个u32整数来描述大小(size) 必须设置为0
数字形式的节点标识,在后续节点中属性值性质表示某节点时,可以引用对应节点
如:
- pic@10000000 {
- phandle = <1>;
- interrupt-controller;
- };
- another-device-node {
- interrupt-parent = <1>; // 使用phandle值为1来引用上述节点
- };
reg属性:表示内存区域region,语法:
reg = <address1 length1 [address2 length2] [address3 length3]>;
#address-cells:reg属性中, 使用多少个u32整数来描述地址(address),语法:
#address-cells = <数字>;
#size-cells:reg属性中, 使用多少个u32整数来描述大小(size),语法:
#size-cells = <数字>;
驱动和设备(设备节点)的匹配依据,compatible(兼容性)的值可以有不止一个字符串以满足不同的需求,语法:
compatible = "字符串1","字符串2",...;
a. 中断控制器节点用的属性:
interrupt-controller 一个无值空属性用来声明这个node接收中断信号,表示该节点是一个中断控制器
#interrupt-cells 这是中断控制器节点的属性,用来标识这个控制器需要几个单位做中断描述符
b. 中断源设备节点用的属性:
interrupt-parent:标识此设备节点属于哪一个中断控制器,如果没有设置这个属性,会自动依附父节点的,语法:
interrupt-parent = <引用某中断控制器节点>
interrupts 一个中断标识符列表,表示每一个中断输出信号,语法:
- interrupts = <中断号 触发方式>
-
- 1 low-to-high 上升沿触发
- 2 high-to-low 下降沿触发
- 4 high level 高电平触发
- 8 low level 低电平触发
gpio也是最常见的IO口,常用的属性有:
a. 对于GPIO控制器:
gpio-controller,无值空属性,用来说明该节点描述的是一个gpio控制器
#gpio-cells,用来表示要用几个cell描述一个 GPIO引脚
b. 对于GPIO使用者节点:
gpio使用节点的属性
- xxx-gpio = <&引用GPIO控制器 GPIO标号 工作模式>
- 工作模式:
- 1 低电平有效 GPIO_ACTIVE_HIGH
- 0 高电平有效 GPIO_ACTIVE_LOW
一般来说,每一种设备的节点属性设置都会有一些套路,比如可以设置哪些属性?属性值怎么设置?那怎么知道这些套路呢,有两种思路:
1. 抄类似的dts,比如我们自己项目的平台是4412,那么就可以抄exynos4412-tiny4412.dts、exynos4412-smdk4412.dts这类相近的dts
2. 查询内核中的文档,比如Documentation/devicetree/bindings/i2c/i2c-imx.txt就描述了imx平台的i2c属性设置方法;Documentation/devicetree/bindings/fb就描述了lcd、lvds这类属性设置方法
struct device_node 对应设备树中的一个节点
struct property 对应节点中一个属性
- /*
- include/of.h
- of_find_node_by_path - 通过路径查找指定节点
- @path - 带全路径的节点名,也可以是节点的别名
- 成功:得到节点的首地址;失败:NULL
- */
- struct device_node * of_find_node_by_path(const char *path);
- /*
- include/of.h
- of_find_property - 提取指定属性的值
- @np - 设备节点指针
- @name - 属性名称
- @lenp - 属性值的字节数
- 成功:属性值的首地址;失败:NULL
- */
- struct property *of_find_property(const struct device_node *np, const char *name, int *lenp);
- /*
- * include/of_gpio.h
- * of_get_named_gpio - 从设备树中提取gpio口
- * @np - 设备节点指针
- * @propname - 属性名
- * @index - gpio口引脚标号
- * 成功:得到GPIO口编号;失败:负数,绝对值是错误码
- */
- int of_get_named_gpio(struct device_node *np, const char *propname, int index);
- /*
- 功能:获得设备树中的中断号并进行映射
- 参数:node:设备节点
- index:序号
- 返回值:成功:中断号 失败:错误码
- */
- unsigned int irq_of_parse_and_map(struct device_node *node, int index);
- /*
- of_property_read_string - 提取字符串(属性值)
- @np - 设备节点指针
- @propname - 属性名称
- @out_string - 输出参数,指向字符串(属性值)
- 成功:0;失败:负数,绝对值是错误码
- */
- int of_property_read_string(struct device_node *np, const char *propname, const char **out_string);
- int of_property_read_u8(const struct device_node *np,const char *propname,u8 *out_value)
-
- int of_property_read_u16(const struct device_node *np,const char *propname,u16 *out_value)
-
- int of_property_read_u32(const struct device_node *np,const char *propname,u32 *out_value)
int of_property_read_bool(const struct device_node *np,const char *propname)
int of_property_read_u32_array(const struct device_node *np,const char *propname,u32 *out_value,size_t sz)
int gpio_request(unsigned gpio,const char *label)
功能:其实就是让内核检查一下该GPIO引脚是否被其它设备占用,如果没有占用则返回0并用label做一下标记,表示被本设备占用,否则返回负数
void gpio_free(unsigned gpio)
功能:去除本设备对该GPIO的占用标记,表示本设备向内核归还对该GPIO引脚的使用权,此后其它设备可占用该GPIO引脚
- int gpio_direction_input(unsigned gpio)
-
- int gpio_direction_output(unsigned gpio,int value)
- int gpio_get_value(unsigned gpio)
-
- int gpio_set_value(unsigned gpio,int value)
1. 在设备树源文件的根节点下添加本设备的节点(该节点中包含本设备用到的资源信息)
..../linux3.14/arch/arm/boot/dts/exynos4412-fs4412.dts
- fs4412-leds {
- compatible = "4412,led2-5";
- led2-gpio = <&gpx2 7 0>;
- led3-gpio = <&gpx1 0 0>;
- led4-gpio = <&gpf3 4 0>;
- led5-gpio = <&gpf3 5 0>;
- };
2. 在linux内核源码的顶层目录下执行:make dtbs (生成对应的dtb文件)
3. cp ?????.dtb /tftpboot
4. 编写驱动代码:
a. 通过本设备在设备树中的路径找到对应节点(struct device_node类型的地址值)
b. 调用 of_get_named_gpio 函数得到某个GPIO的编号
c. struct leddev结构体中记录所有用到的GPIO编号
d. 使用某个GPIO引脚前需先通过gpio_request函数向内核申请占用该引脚,不用该引脚时可通过gpio_free归还给内核
e. 通过gpio_direction_input和gpio_direction_output函数来设置某个GPIO的作用
f. 通过gpio_get_value函数可以获取某个GPIO引脚的当前电平
g. 通过gpio_set_value函数可以改变某个GPIO引脚的电平
leddrv.c
- #include <linux/module.h>
- #include <linux/kernel.h>
- #include <linux/fs.h>
- #include <linux/gpio.h>
- #include <linux/of_gpio.h>
- #include <linux/cdev.h>
- #include <linux/wait.h>
- #include <linux/sched.h>
- #include <linux/poll.h>
- #include <linux/slab.h>
- #include <linux/mm.h>
- #include <linux/io.h>
- #include <asm/uaccess.h>
- #include <asm/atomic.h>
-
- #include "leddrv.h"
-
- int major = 11;
- int minor = 0;
- int myled_num = 1;
-
- struct myled_dev
- {
- struct cdev mydev;
-
- unsigned int led2gpio;
- unsigned int led3gpio;
- unsigned int led4gpio;
- unsigned int led5gpio;
- };
-
- struct myled_dev *pgmydev = NULL;
-
- int myled_open(struct inode *pnode, struct file *pfile)
- {
- pfile->private_data =(void*)(container_of(pnode->i_cdev,struct myled_dev,mydev));
- return 0;
- }
-
- int myled_close(struct inode *pnode, struct file *pfile)
- {
- return 0;
- }
-
- void led_on(struct myled_dev *pmydev, int ledno)
- {
- switch (ledno) {
- case 2:
- gpio_set_value(pmydev->led2gpio, 1);
- break;
- case 3:
- gpio_set_value(pmydev->led3gpio, 1);
- break;
- case 4:
- gpio_set_value(pmydev->led4gpio, 1);
- break;
- case 5:
- gpio_set_value(pmydev->led5gpio, 1);
- break;
- }
- }
-
- void led_off(struct myled_dev *pmydev, int ledno)
- {
- switch (ledno) {
- case 2:
- gpio_set_value(pmydev->led2gpio, 0);
- break;
- case 3:
- gpio_set_value(pmydev->led3gpio, 0);
- break;
- case 4:
- gpio_set_value(pmydev->led4gpio, 0);
- break;
- case 5:
- gpio_set_value(pmydev->led5gpio, 0);
- break;
- }
- }
-
- long myled_ioctl(struct file *pfile, unsigned int cmd, unsigned long arg)
- {
- struct myled_dev *pmydev = (struct myled_dev*)pfile->private_data;
-
- if (arg < 2 || arg > 5) {
- return -1;
- }
-
- switch (cmd) {
- case MY_LED_ON:
- led_on(pmydev, arg);
- break;
- case MY_LED_OFF:
- led_off(pmydev, arg);
- break;
- default:
- return -1;
- }
-
- return 0;
- }
-
- struct file_operations myops = {
- .owner = THIS_MODULE,
- .open = myled_open,
- .release = myled_close,
- .unlocked_ioctl = myled_ioctl,
- };
-
- void request_leds_gpio(struct myled_dev *pmydev, struct device_node *pnode) // 申请占用设备
- {
- pmydev->led2gpio = of_get_named_gpio(pnode, "led2-gpio", 0);
- gpio_request(pmydev->led2gpio, "led2");
-
- pmydev->led3gpio = of_get_named_gpio(pnode, "led3-gpio", 0);
- gpio_request(pmydev->led3gpio, "led3");
-
- pmydev->led4gpio = of_get_named_gpio(pnode, "led4-gpio", 0);
- gpio_request(pmydev->led4gpio, "led4");
-
- pmydev->led5gpio = of_get_named_gpio(pnode, "led5-gpio", 0);
- gpio_request(pmydev->led5gpio, "led5");
-
- }
-
- void set_leds_gpio_output(struct myled_dev *pmydev) // 配置成输出
- {
- gpio_direction_output(pmydev->led2gpio, 0);
- gpio_direction_output(pmydev->led3gpio, 0);
- gpio_direction_output(pmydev->led4gpio, 0);
- gpio_direction_output(pmydev->led5gpio, 0);
- }
-
- void free_leds_gpio(struct myled_dev *pmydev) // free设备
- {
- gpio_free(pmydev->led2gpio);
- gpio_free(pmydev->led3gpio);
- gpio_free(pmydev->led4gpio);
- gpio_free(pmydev->led5gpio);
- }
-
- int __init myled_init(void)
- {
- int ret = 0;
- dev_t devno = MKDEV(major, minor);
- struct device_node *pnode = NULL;
-
- pnode = of_find_node_by_path("/fs4412-leds"); // 设备树中的节点名
- if (NULL == pnode) {
- printk("find node by path failed\n");
- return -1;
- }
-
- ret = register_chrdev_region(devno, myled_num, "myled");
- if (ret) {
- ret = alloc_chrdev_region(&devno, minor, myled_num, "myled");
- if (ret) {
- printk("get devno failed\n");
- return -1;
- }
- major = MAJOR(devno);
- }
-
- pgmydev = (struct myled_dev*)kmalloc(sizeof(struct myled_dev), GFP_KERNEL);
- if (NULL == pgmydev) {
- unregister_chrdev_region(devno, myled_num);
- printk("kmalloc failed\n");
- return -1;
- }
- memset(pgmydev, 0, sizeof(struct myled_dev));
-
- cdev_init(&pgmydev->mydev, &myops);
-
- pgmydev->mydev.owner = THIS_MODULE;
- cdev_add(&pgmydev->mydev, devno, myled_num);
-
- /* ioremap */
- request_leds_gpio(pgmydev, pnode);
-
- /* con-register set output */
- set_leds_gpio_output(pgmydev);
-
- return 0;
- }
-
- void __exit myled_exit(void)
- {
- dev_t devno = MKDEV(major, minor);
-
- /* iounmap */
- free_leds_gpio(pgmydev);
-
- cdev_del(&pgmydev->mydev);
- unregister_chrdev_region(devno, myled_num);
-
- kfree(pgmydev);
- pgmydev = NULL;
- }
-
-
- MODULE_LICENSE("GPL");
-
- module_init(myled_init);
- module_exit(myled_exit);
leddrv.h
- #ifndef LED_DRIVER_H
- #define LED_DRIVER_H
-
- #define LED_DEV_MAGIC 'g'
-
- #define MY_LED_OFF _IO(LED_DEV_MAGIC, 0)
- #define MY_LED_ON _IO(LED_DEV_MAGIC, 1)
-
- #endif
testled.c
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <sys/ioctl.h>
- #include <unistd.h>
- #include <stdio.h>
-
- #include "leddrv.h"
-
- int main(int argc, char *argv[])
- {
- int fd = -1;
- int onoff = 0;
- int no = 0;
-
- if (argc < 4) {
- printf("The argument is too few\n");
- return 1;
- }
-
- sscanf(argv[2], "%d", &onoff);
- sscanf(argv[3], "%d", &no);
-
- if (no < 2 || no > 5) {
- printf("led-no is invalid\n");
- return 2;
- }
-
- fd = open(argv[1], O_RDONLY);
- if (fd < 0) {
- printf("open %s failed\n", argv[1]);
- return 3;
- }
-
- if (onoff) {
- ioctl(fd, MY_LED_ON, no);
- } else {
- ioctl(fd, MY_LED_OFF, no);
- }
-
- close(fd);
- fd = -1;
- return 0;
- }
测试结果
ubuntu上将 leddrv.c 编译成 arm 架构的 ko 文件,将 testled.c 编译成 arm 架构的可执行文件,再将这两个文件拷贝到挂载的根文件系统目录下
- shrek@ubuntu16:~/share/mydrivercode$ make ARCH=arm
- shrek@ubuntu16:~/share/mydrivercode$ arm-none-linux-gnueabi-gcc ./testled.c -o testled
- shrek@ubuntu16:~/share/mydrivercode$ cp leddrv.ko /opt/4412/rootfs/
- shrek@ubuntu16:~/share/mydrivercode$ cp testled /opt/4412/rootfs/
开发板上挂载好根文件系统,执行如下命令
- [root@farsight]#insmod leddrv.ko
- [root@farsight]#cat /proc/devices | grep led
- 11 myled
- [root@farsight]#mknod /dev/leddev c 11 0
- [root@farsight]#./testled /dev/leddev 1 2
- [root@farsight]#./testled /dev/leddev 1 3
- [root@farsight]#./testled /dev/leddev 1 4
- [root@farsight]#./testled /dev/leddev 1 5
- [root@farsight]#./testled /dev/leddev 0 5
- [root@farsight]#./testled /dev/leddev 0 4
- [root@farsight]#./testled /dev/leddev 0 3
- [root@farsight]#./testled /dev/leddev 0 2
看到开发板上的LED灯可以控制亮灭
一种硬件上的通知机制,用来通知CPU发生了某种需要立即处理的事件
分为:
1. 内部中断 CPU执行程序的过程中,发生的一些硬件出错、运算出错事件(如分母为0、溢出等等),不可屏蔽
2. 外部中断 外设发生某种情况,通过一个引脚的高、低电平变化来通知CPU (如外设产生了数据、某种处理完毕等等)
任何一种中断产生,CPU都会暂停当前执行的程序,跳转到内存固定位置执行一段程序,该程序被称为总的中断服务程序,在该程序中区分中断源,然后进一步调用该中断源对应的处理函数。
中断源对应的处理函数被称为分中断处理程序,一般每一个分中断处理程序对应一个外设产生的中断
写驱动时,如果外设有中断,则需要编写一个函数(分中断处理程序)来处理这种中断
- int request_irq(unsigned int irq, irq_handler_t handler, unsigned long flags,const char *name, void *dev)
- /*
- 参数:
- irq:所申请的中断号
- handler:该中断号对应的中断处理函数
- flags:中断触发方式或处理方式
- 触发方式:IRQF_TRIGGER_NONE //无触发
- IRQF_TRIGGER_RISING //上升沿触发
- IRQF_TRIGGER_FALLING //下降沿触发
- IRQF_TRIGGER_HIGH //高电平触发
- IRQF_TRIGGER_LOW //低电平触发
- 处理方式:
- IRQF_DISABLED //用于快速中断,处理中屏蔽所有中断
- IRQF_SHARED //共享中断
- name:中断名 /proc/interrupts
- dev:传递给中断例程的参数,共享中断时用于区分那个设备,一般为对应设备的结构体地址,无共享中断时写NULL
- 返回值:成功:0 失败:错误码
- */
- void free_irq(unsigned int irq, void *dev_id);
- /*
- 功能:释放中断号
- 参数:
- irq:设备号
- dev_id:共享中断时用于区分那个设备一般强转成设备号,无共享中断时写NULL
- */
- typedef irqreturn_t (*irq_handler_t)(int, void *);
- /*
- 参数:
- int:中断号
- void*:对应的申请中断时的dev_id
- 返回值:
- typedef enum irqreturn irqreturn_t; //中断返回值类型
- enum irqreturn {
- IRQ_NONE = (0 << 0),
- IRQ_HANDLED = (1 << 0),
- IRQ_WAKE_THREAD = (1 << 1),
- };
- 返回IRQ_HANDLED表示处理完了,返回IRQ_NONE在共享中断表示不处理
- */
exynos4412-fs4412.dts中增加节点
- mykey2_node {
- compatible = "mykey2,key2";
- key2-gpio = <&gpx1 1 0>;
- interrupt-parent = <&gpx1>;
- interrupts = <1 3>;
- };
key.c
- #include <linux/module.h>
- #include <linux/kernel.h>
- #include <linux/fs.h>
- #include <linux/gpio.h>
- #include <linux/interrupt.h>
- #include <linux/of_gpio.h>
- #include <linux/of_irq.h>
- #include <linux/cdev.h>
- #include <linux/wait.h>
- #include <linux/sched.h>
- #include <linux/poll.h>
- #include <linux/mm.h>
- #include <linux/delay.h>
- #include <linux/slab.h>
- #include <asm/uaccess.h>
-
- #include "key.h"
-
- int major = 11;
- int minor = 0;
- int key2_num = 1;
-
- struct key2_dev
- {
- struct cdev mydev;
-
- int gpio; // GPIO号
- int irqno; // 中断号
-
- struct keyvalue data; // 按键产生的数据
- int newflag; // 按键产生标志
- spinlock_t lock;
-
- wait_queue_head_t rq;
- };
-
- struct key2_dev *pgmydev = NULL;
-
- int key2_open(struct inode* pnode, struct file* pfile)
- {
- pfile->private_data = (void *)(container_of(pnode->i_cdev, struct key2_dev, mydev));
- return 0;
- }
-
- int key2_close(struct inode* pnode, struct file* pfile)
- {
- return 0;
- }
-
- ssize_t key2_read(struct file* pfile, char __user *puser, size_t count, loff_t* p_pos)
- {
- struct key2_dev *pmydev = (struct key2_dev *)pfile->private_data;
- int size = 0;
- int ret = 0;
-
- if (count < sizeof(struct keyvalue)) {
- printk("expect read size is invalid\n");
- return -1;
- }
-
- spin_lock(&pmydev->lock);
- if (!pmydev->newflag) {
- if (pfile->f_flags & O_NONBLOCK) {
- // 非阻塞
- spin_unlock(&pmydev->lock);
- printk("O_NONBLOCK No Data Read\n");
- return -1;
- } else {
- // 阻塞
- spin_unlock(&pmydev->lock);
- ret = wait_event_interruptible(pmydev->rq, pmydev->newflag == 1);
- if (ret) {
- printk("Wake up by signal\n");
- return -ERESTARTSYS;
- }
- spin_lock(&pmydev->lock);
- }
- }
-
- if (count > sizeof(struct keyvalue)) {
- size = sizeof(struct keyvalue);
- } else {
- size = count;
- }
-
- ret = copy_to_user(puser, &pmydev->data, size);
- if (ret) {
- spin_unlock(&pmydev->lock);
- printk("copy_to_user failed\n");
- return -1;
- }
-
- pmydev->newflag = 0;
-
- spin_unlock(&pmydev->lock);
-
- return size;
- }
-
- unsigned int key2_poll(struct file* pfile, poll_table* ptb)
- {
- struct key2_dev *pmydev = (struct key2_dev *)pfile->private_data;
- unsigned int mask = 0;
-
- poll_wait(pfile, &pmydev->rq, ptb);
-
- spin_lock(&pmydev->lock);
- if (pmydev->newflag) {
- mask |= POLLIN | POLLRDNORM;
- }
- spin_unlock(&pmydev->lock);
-
- return mask;
- }
-
- struct file_operations myops = {
- .owner = THIS_MODULE,
- .open = key2_open,
- .release = key2_close,
- .read = key2_read,
- .poll = key2_poll,
- };
-
- irqreturn_t key2_irq_handle(int no, void *arg)
- {
- struct key2_dev *pmydev = (struct key2_dev*)arg;
- int status1 = 0;
- int status2 = 0;
- int status = 0;
-
- status1 = gpio_get_value(pmydev->gpio);
- mdelay(1);
- status2 = gpio_get_value(pmydev->gpio);
-
- if (status1 != status2) {
- return IRQ_NONE;
- }
-
- status = status1;
-
- spin_lock(&pmydev->lock);
- if (status == pmydev->data.status) {
- spin_unlock(&pmydev->lock);
- return IRQ_NONE;