Linux驱动学习之路二
上次我们写了一个最简单的驱动程序,但是那个驱动程序中没有接口可供linux上层应用程序调用
这次让我们添加一个linux调用驱动的接口,让上层应用可以调用
1.首先让咱们来了解一下linux 中file_operations结构体,这个结构体在\include\linux\hs.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 char __user *, size_t, loff_t *);
ssize_t (*aio_read) (struct kiocb *, const struct iovec *, unsigned long, loff_t);
ssize_t (*aio_write) (struct kiocb *, const struct iovec *, unsigned long, loff_t);
int (*readdir) (struct file *, void *, filldir_t);
unsigned int (*poll) (struct file *, struct poll_table_struct *);
long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);
long (*compat_ioctl) (struct file *, unsigned int, unsigned long);
int (*mmap) (struct file *, struct vm_area_struct *);
int (*open) (struct inode *, struct file *);
int (*flush) (struct file *, fl_owner_t id);
int (*release) (struct inode *, struct file *);
int (*fsync) (struct file *, loff_t, loff_t, int datasync);
int (*aio_fsync) (struct kiocb *, int datasync);
int (*fasync) (int, struct file *, int);
int (*lock) (struct file *, int, struct file_lock *);
ssize_t (*sendpage) (struct file *, struct page *, int, size_t, loff_t *, int);
unsigned long (*get_unmapped_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);
int (*check_flags)(int);
int (*flock) (struct file *, int, struct file_lock *);
ssize_t (*splice_write)(struct pipe_inode_info *, struct file *, loff_t *, size_t, unsigned int);
ssize_t (*splice_read)(struct file *, loff_t *, struct pipe_inode_info *, size_t, unsigned int);
int (*setlease)(struct file *, long, struct file_lock **);
long (*fallocate)(struct file *file, int mode, loff_t offset,loff_t len);
int (*show_fdinfo)(struct seq_file *m, struct file *f);
};
可见这个结构体都是函数指针,所以我们驱动要想向应用层提供读写接口的时候,必须把这个结构体的指针填充(用到的填充就行),让指针指向我们驱动的函数,然后应用层调用open、close、write、read等实际上感觉就是操控驱动的读写函数
我们现在把上次的代码加点东西
#include <linux/module.h>// LInux很重要的头文件之一包含 module_init module_exit 等宏定义
#include// 在module_init宏调用的函数中去注册字符设备驱动
ret = register_chrdev(MYMAJOR, MYNAME, &test_fops);
if (ret)
{
printk(KERN_ERR "register_chrdev fail\n");
return -EINVAL;
}
printk(KERN_INFO "register_chrdev success...\n"); <linux/init.h> // __init __exit
#define MYMAJOR 200
#define MYNAME "testchar"
static int test_chrdev_open(struct inode *inode, struct file *file)
{
printk(KERN_INFO "test_chrdev_open\n");
return 0;
}
static int test_chrdev_release(struct inode *inode, struct file *file)
{
printk(KERN_INFO "test_chrdev_release\n");
return 0;
}
// 自定义一个file_operations结构体变量,并且去填充
static const struct file_operations test_fops = {
.owner = THIS_MODULE, // 惯例,直接写即可
.open = test_chrdev_open, // 将来应用open打开这个设备时实际调用的
.release = test_chrdev_release, // 就是这个这个就是close函数对应的
};
// 模块安装函数
static int __init chrdev_init(void)
{
// 在module_init宏调用的函数中去注册字符设备驱动
ret = register_chrdev(MYMAJOR, MYNAME, &test_fops); //向内核注册该驱动 这里的test_fops就是我们刚刚填充的结构体
if (ret)
{
printk(KERN_ERR "register_chrdev fail\n");
return -EINVAL;
}
printk(KERN_INFO "register_chrdev success...\n");
printk(KERN_INFO "chrdev_init helloworld init\n");
return 0;
}
// 模块卸载函数
static void __exit chrdev_exit(void)
{
printk(KERN_INFO "chrdev_exit helloworld exit\n"); //printk是内核提供的,和普通应用程序的print区别在于printfk可以有打印不同消息的登记
unregister_chrdev(MYMAJOR, MYNAME); //向内核卸载该驱动
} //注意:内核中不能用printf
module_init(chrdev_init); //当在linux中命令行输入insmod的时候进行会自动跳到chrdev_init中加载函数
module_exit(chrdev_exit); //当在linux中命令行输入rmmod的时候进行会自动跳到chrdev_exit中卸载函数
// MODULE_xxx这种宏作用是用来添加模块描述信息
MODULE_LICENSE("GPL"); // 描述模块的许可证
MODULE_AUTHOR("NexyHo"); // 描述模块的作者
MODULE_DESCRIPTION("module test"); // 描述模块的介绍信息
MODULE_ALIAS("NexyHoxxx");
然后用上次我们编写的Makefile编译即可。。。。安装完驱动后使用"mknod /dev/xxxx c 主设备好 次设备号"创建驱动文件,然后自己编写一个应用程序调用open打开该驱动文件试一下