一部分驱动要和上层通信,都需要生成设备节点,上层应用通过一套标准的接口函数调用设备节点就可以控制底层以及和底层通信。最简单易用的杂项设备节点如何生成。
为什么引入杂项设备
在exynos4412开发板,在超级终端中输入命令“cat /proc/misc”可以查看对应的杂项设备。主设备号只有256 个,设备又非常多,所以引入了子设备号。其中杂项设备的主设备号是10,在任何Linux 系统中它都是固定的。一般将Linux驱动分为字符设备、块设备、网络设备,但是这个分类不能包含所有的设备,所以将无法归类的设备统称为杂项设备,杂项设备可能用到字符设备、快设备、网络设备中的一项或者多项设备。
在linux虚拟机上在内核源码文件下,ls drivers/char可以看到misc.c和misc.o,可见它被编译进内核,使用vim drivers/char/Makefile打开杂项设备文件的编译文件。
obj-y +=misc.o,该文件被强制编译进内核
杂项设备设备部分完全制作好了,只需要添加子设备,非常方便。
这样杂项设备的引入即解决了设备号数量少的问题,又降低了使用难度,还能防止碎片化,一举多得。
杂项设备注册函数以及结构体
杂项设备的头文件在“include/linux/miscdevice.h”
struct miscdevice {
int minor;//设备号,赋值为MISC_DYNAMIC_MINOR,这个宏定义可以查到为10
const char *name;//设备名称
const struct file_operations *fops;//file_operations 结构体
struct list_head list;
struct device *parent;
struct device *this_device;
const char *nodename;
mode_t mode;
};
extern int misc_register(struct miscdevice * misc);
extern int misc_deregister(struct miscdevice *misc);
Cextern int misc_register(struct miscdevice * misc);
杂项设备注册函数;一般在probe 中调用,参数是miscdevice
Cextern int misc_deregister(struct miscdevice *misc);
杂项设备卸载函数;一般是在hello_remove 中用于卸载驱动。
file_operations结构体
file_operations 结构体的成员函数属于驱动开发的主体内容,里面的函数和Linux 系统给应用程序提供系统接口一一对应。
file_operations结构体在内核源码“include/linux/fs.h”中,使用命令vim /include/linux/fs.h打开
struct file_operations {
struct module *owner;
loff_t (*llseek) (struct file *, loff_t, int);
ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);
ssize_t (*write) (struct file *, const