字符设备驱动的工作原理:

系统整体工作原理
1.应用层->API->设备驱动->硬件
2.API:open、read、write、close等
3.驱动源码中提供真正的open、read、write、close等函数实体
应用层的open、read、write、close等最终也是调用到驱动层的。


下面我们看一下file_operations结构体,它在linux/fs.h下面
/*

  • NOTE:
  • read, write, poll, fsync, readv, writev, unlocked_ioctl and compat_ioctl
  • can be called without the big kernel lock held in all filesystems.
    */
    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 *);
    int (*ioctl) (struct inode *, struct file *, unsigned int, unsigned long);
    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 *, 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 **);
    };
    总结:
    元素主要是函数指针,用来挂接实体函数地址
    每个设备驱动都需要一个该结构体类型的变量
    设备驱动向内核注册时提供该结构体类型的变量

register_chrdev函数分析:
static inline int register_chrdev(unsigned int major, const char *name,
const struct file_operations *fops)
{
return __register_chrdev(major, 0, 256, name, fops);
}
功能:注册字符设备
参数:
major:主设备号,可以传1-254(自己指定Major),传0函数会帮我们分配主设备号
name:字符设备的名字,cat /proc/device 可以看到, 也就是存到字符设备数组的名字
fops:file_operations 结构体
返回值:成功返回0或者主设备号,失败返回负数
0:我们自己指定Major,主设备号
正数:我们自己不指定Major(注册的时候主设备号传0),这个函数自己帮我们分配的

static和inline,一般放在头文件里面的函数,需要被别的文件包含,注意宏的原地替换性。


内核如何管理字符设备驱动?
内核中有一个数组用来存储注册的字符设备驱动,register_chrdev内部将我们要注册的驱动的信息(主要是 )存储在数组中相应的位置,,major就是主设备号,其实就是对应我们这个数组的index
查看已经注册过的驱动:
cat /proc/devices查看内核中已经注册过的字符设备驱动(和块设备驱动)
注意:proc和sys文件系统的理解,vfs。


下面对应的是内核里面的错误码:
#define EPERM 1 /* Operation not permitted /
#define ENOENT 2 /
No such file or directory /
#define ESRCH 3 /
No such process /
#define EINTR 4 /
Interrupted system call /
#define EIO 5 /
I/O error /
#define ENXIO 6 /
No such device or address /
#define E2BIG 7 /
Argument list too long /
#define ENOEXEC 8 /
Exec format error /
#define EBADF 9 /
Bad file number /
#define ECHILD 10 /
No child processes /
#define EAGAIN 11 /
Try again /
#define ENOMEM 12 /
Out of memory /
#define EACCES 13 /
Permission denied /
#define EFAULT 14 /
Bad address /
#define ENOTBLK 15 /
Block device required /
#define EBUSY 16 /
Device or resource busy /
#define EEXIST 17 /
File exists /
#define EXDEV 18 /
Cross-device link /
#define ENODEV 19 /
No such device /
#define ENOTDIR 20 /
Not a directory /
#define EISDIR 21 /
Is a directory /
#define EINVAL 22 /
Invalid argument /
#define ENFILE 23 /
File table overflow /
#define EMFILE 24 /
Too many open files /
#define ENOTTY 25 /
Not a typewriter /
#define ETXTBSY 26 /
Text file busy /
#define EFBIG 27 /
File too large /
#define ENOSPC 28 /
No space left on device /
#define ESPIPE 29 /
Illegal seek /
#define EROFS 30 /
Read-only file system /
#define EMLINK 31 /
Too many links /
#define EPIPE 32 /
Broken pipe /
#define EDOM 33 /
Math argument out of domain of func /
#define ERANGE 34 /
Math result not representable */


看我们写的代码:

#include <linux/module.h>
#include <linux/init.h>
#include <linux/fs.h>

#define TEST_MAJOR 200
#define TEST_NAME “testCharDev”

static int test_char_device_open(struct inode *inode, struct file *file)
{
printk(KERN_INFO “test_char_device_open\n”);
return 0;
}

static int test_char_device_release(struct inode *inode, struct file *file)
{
printk(KERN_INFO “test_char_device_release\n”);
return 0;
}

static const struct file_operations test_ops =
{
.owner = THIS_MODULE, //do not to change
.open = test_char_device_open, //open ----- API open
.release = test_char_device_release, //close ---- API close
};

static int __init chrdev_init(void)
{
int ret = -1;
printk(KERN_INFO “chrdev_init helloworld init\n”);
#if 0
// we indicate the Major
ret = register_chrdev(TEST_MAJOR, TEST_NAME, &test_ops);
if (ret)
{
printk(KERN_INFO “register_chrdev fail\n”);
return -EINVAL;
}
#else
//let the register_chrdev function alloc the Major
ret = register_chrdev(0, TEST_NAME, &test_ops);
if (ret < 0)
{
printk(KERN_INFO “register_chrdev fail\n”);
return -EINVAL;
}
printk(KERN_INFO “Major = %d\n”, ret);
#endif

printk(KERN_INFO "register_chrdev OK!!!\n");
return 0;

}

static void __exit chrdev_exit(void)
{
unregister_chrdev(TEST_MAJOR, TEST_NAME);
printk(KERN_INFO “chrdev_exit helloworld exit\n”);
}

module_init(chrdev_init);
module_exit(chrdev_exit);

MODULE_LICENSE(“GPL”);
MODULE_AUTHOR(“Mark”);
MODULE_DESCRIPTION(“test for driver”);
MODULE_ALIAS(“alias test”);


对部分内容做解释和说明:
register_chrdev:
第一个参数Major,如果我们自己想指定主设备号,我们可以传1-254进去,传0和大于254的值,这个函数都会自己帮我们分配主设备号,不要乱传值,会出现意想不到的错误。注意:1-254,自动分配的时候是从空闲的最大值开始分配的。
第二个参数name:就是将来放到字符设备数组里面的name,用cat /proc/device可以看到的。
在init函数里面字符设备注册后,要记得在exit里面调用:
unregister_chrdev(MYMAJOR, MYNAME);
unregister_chrdev这个函数,传入主设备号和名字。
一定要记住释放资源


static const struct file_operations test_ops =
{
.owner = THIS_MODULE, //do not to change
.open = test_char_device_open, //open ----- API open
.release = test_char_device_release, //close ---- API close
};
定义为const类型,使用.member = xxx的方式进行赋值。
记得一定要定义: .owner = THIS_MODULE, //do not to change,这个是不别的,一直都是这个值。open函数将来对应的就是应用层的open函数,release函数对应的就是将来应用层的close函数。
根这个结构体相关的很多东西都放在:linux/fs.h头文件中


注意open和release函数的定义:
static int test_char_device_open(struct inode *inode, struct file *file)
{
printk(KERN_INFO “test_char_device_open\n”);
return 0;
}

static int test_char_device_release(struct inode *inode, struct file *file)
{
printk(KERN_INFO “test_char_device_release\n”);
return 0;
}
返回值:int
参数:struct inode *inode和struct file *file


在应用程序中去调用驱动:
应用程序:
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#define PATHNAME “/dev/testCharDev”

int main(void)
{
int fd = -1;
fd = open(PATHNAME, O_RDWR);
if (fd < 0)
{
perror(“open:”);
return -1;
}

close(fd);

}
现在在/dev下面还没有设备文件,我们需要用mknod去创建这个设备文件。
何为设备文件?
设备文件的关键信息是:设备号 = 主设备号 + 次设备号,使用ls -l去查看设备文件,就可以得到这个设备文件对应的主次设备号。
使用mknod创建设备文件:mknod /dev/xxx c 主设备号 次设备号。
我们运行app,open的时候就调用驱动的open函数,打印:test_char_device_open
close的时候会调用驱动的close函数,打印:test_char_device_release


在应用和驱动中添加read、write接口:
应用和驱动之间的数据交换使用两个函数:
copy_from_user,用来将数据从用户空间复制到内核空间
copy_to_user,用来将数据从内核空间复制到用户空间
下面我们看一下驱动层写的read、write接口:

static ssize_t test_char_device_read (struct file * file, char __user * ubuf,
size_t count, loff_t * ppos)
{
int ret = -1;
printk(KERN_INFO “test_char_device_read\n”);
ret = copy_to_user(ubuf, buf, count);
if (ret)
{
printk(KERN_INFO “copy_to_user fail\n”);
return -EINVAL;
}
printk(KERN_INFO “copy_to_user ok\n”);
return 0;
}
static ssize_t test_char_device_write (struct file * file , const char __user * ubuf,
size_t count, loff_t * ppos)
{
int ret = -1;
printk(KERN_INFO “test_char_device_write\n”);
ret = copy_from_user(buf, ubuf, count);
if (ret)
{
printk(KERN_INFO “copy from user fail\n”);
return -EINVAL;
}
printk(KERN_INFO “copy_from_user\n”);

return 0;

}


对部分代码进行分析:
copy_from_user,用来将数据从用户空间复制到内核空间
copy_to_user,用来将数据从内核空间复制到用户空间
copy_from_user和copy_to_user函数的返回值定义,和常规有点不同。返回值:如果成功复制则返回0,如果 不成功复制则返回尚未成功复制剩下的字节数,正数。
copy_to_user(ubuf, buf, count);
第一个参数:用户层的buf,传参过来的
第二个参数:驱动里面的buf
第三个参数:要拷贝的字节数,应用层要读的字节数

copy_from_user(buf, ubuf, count);
第一个参数:驱动里面的buf
第二个参数:用户层的buf,传参过来的。
第三个参数:要拷贝的字节数,应用层要写的字节数
所以:第一个参数是目标位置,第二个是源,第三个是字节数。


驱动里面read和write函数的原型:
static ssize_t test_char_device_read (struct file * file, char __user * ubuf, size_t count, loff_t * ppos)
第一个参数:struct file*
第二个参数:用户层传过来的buf,我们要把驱动层的数据拷贝到里面去
第三个参数:要读的字节数
第四个参数:应用里面的文件指针位置,只是猜测,后面有时间去证明

static ssize_t test_char_device_write (struct file * file , const char __user * ubuf, size_t count, loff_t * ppos)
第一个参数:struct file*
第二个参数:用户层传过来的buf,我们要把数据写到驱动的buf里面去
第三个参数:要写入的字节数
第四个参数:应用里面的文件指针位置,只是猜测,后面有时间去证明


应用层执行的打印结果:
[ 4407.411992] test_char_device_open
[ 4407.413836] test_char_device_write
[ 4407.417236] copy_from_user
[ 4407.419920] test_char_device_read
[ 4407.423185] copy_to_user ok
[ 4407.426091] test_char_device_release
123456789// 应用层写入的
到这来我们已经实现了一个简单的驱动了。只是与硬件没有任何的关系,后面我们继续把硬件添加进来

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值