linux设备驱动之字符设备驱动模型(1)
一:字符设备驱动
在linux下面,应用层看到的一切皆为文件(名字)所有的设备都是文件,都可以调用open,read,write来操作,而在内核中每个中每个设备有唯一的对应一个设备号;
| APP (名字) |
| OS (设备号) |
| HW |
下面我们写一个简单的字符设备驱动,再应用层我们打开一个设备,看看它是怎么来调用内核中的函数的;
首先我们使用命令mknod 创建一个名为wangcai的设备名,在应用曾打开它;
mknod /dev/设备文件名 c 主设备号 次设备号
命令 mknod wangcai c 9 0
在注册字符设备是我们需要用到这几个struct cdev,原型如下:
struct cdev
{
struct kobject kobj; // 内嵌的kobject对象,描述设备引用计数
struct module *owner; // 所属模块,一般赋值为THIS_MODULE
struct file_operations *ops; // 文件操作结构体
struct list_head list;
dev_t dev; // 设备号
unsigned int count;
};
cdev结构体的dev_t定义了设备号,32位。高12位为主设备号,低20位为次设备号。
(1)应用层打开设备
#include <stdio.h>
#include <fcntl.h> int main()
{
int fd = ;
fd = open("wangcai", O_RDWR);
if(fd < ) {
perror("open error");
return ;
} return ;
}
(2)在内核中册了设备文件wangcai和方法ops
#include <linux/init.h>
#include <linux/module.h>
#include <linux/cdev.h>
#include <linux/fs.h> MODULE_LICENSE("GPL");
MODULE_AUTHOR("bunfly");
int file_open(struct inode *no, struct file *fp); struct cdev wangcai;//设备
struct file_operations fops;//方法
dev_t devno; int bunfly_init()
{
fops.open = file_open;//调用open
//wangcai.ops = &fops;
cdev_init(&wangcai, &fops);//初始化cdev成员,建立cdev和file_operations之间的连接
//wangcai.dev = (9, 0);
devno = MKDEV(, );//字符设备号注册
//insert_list(&wangcai);
cdev_add(&wangcai, devno, );// 向系统添加一个dev,完成字符设备的注册,常用于模块加载函数中 } int bunfly_exit()
{
printk("this is bunfly_exit\n");
} module_init(bunfly_init);
module_exit(bunfly_exit); int file_open(struct inode *no, struct file *fp)
{
printk("this is file_open\n"); return ;
}
下面代码是实现字符设备的读写操作:
(1)应用层
write:
#include <stdio.h>
#include <fcntl.h>
#include <string.h> int main(int argc, char *argv[])
{
if(argc < ) {
printf("using %s <dev> <msg>\n", argv[]);
return ;
} int fd = ;
int ret = ; fd = open(argv[], O_RDWR);
if(fd < ) {
perror("open error"); return ;
} ret = write(fd, argv[], strlen(argv[]));
if(ret < ) {
perror("write error");
return ;
}
close(fd); return ;
}
read:
#include <stdio.h>
#include <fcntl.h>
#include <string.h> int main(int argc, char *argv[])
{
if(argc != ) {
printf("using %s <dev>\n", argv[]);
return ;
} int fd = ;
int ret = ;
unsigned char data[] = {}; fd = open(argv[], O_RDWR);
if(fd < ) {
perror("open error"); return ;
} ret = read(fd, data, );
if(ret < ) {
perror("write error");
return ;
} printf("data is: %s\n", data); close(fd); return ;
}
(2)内核
#include <linux/init.h>
#include <linux/module.h>
#include <linux/cdev.h>
#include <linux/fs.h> MODULE_LICENSE("GPL");
MODULE_AUTHOR("bunfly"); int file_open(struct inode *no, struct file *fp);
ssize_t file_read(struct file *fp, char *buff, size_t size, loff_t * loff);
ssize_t file_write(struct file *fp, const char *buff, size_t size, loff_t *loff); struct cdev wangcai;//设备
struct file_operations fops;//方法
dev_t devno;
unsigned char data[] = {}; int bunfly_init()
{
fops.open = file_open;
fops.read = file_read;
fops.write = file_write; //wangcai.ops = &fops;
cdev_init(&wangcai, &fops);//初始化cdev成员,建立cdev和file_operations之间的连接
//wangcai.dev = (9, 0);
devno = MKDEV(, );//字符设备号注册
//insert_list(&wangcai);
cdev_add(&wangcai, devno, );// 向系统添加一个dev,完成字符设备的注册,常用于模块加载函数中 } int bunfly_exit()
{
cdev_del(&wangcai); /*注销设备*/
unregister_chrdev_region(MKDEV(, ), );
printk("this is bunfly_exit\n");
} module_init(bunfly_init);
module_exit(bunfly_exit); int file_open(struct inode *no, struct file *fp)
{
return ;
}
ssize_t file_read(struct file *fp, char *buff, size_t size, loff_t *loff)
{
strcpy(buff, data);
return size;
} ssize_t file_write(struct file *fp, const char *buff, size_t size, loff_t *loff)
{
memset(data, , );
strcpy(data, buff);
return size;
}
59
下面代码是通过ioctl()函数来控制灯亮灯灭:
(1)应用层
#include <stdio.h>
#include <string.h>
#include <fcntl.h> int main(int argc, char *argv[])
{
if(argc != ) {
printf("using %s <devname> 1:0\n", argv[]);
return ;
} int fd = ;
fd = open(argv[], O_RDWR);
if(fd < ) {
perror("open");
return ;
} ioctl(fd, atoi(argv[]));
close(fd);
return ;
}
(2)内核
#include <linux/init.h>
#include <linux/module.h>
#include <linux/cdev.h>
#include <linux/fs.h>
#include <linux/io.h>
#include <linux/gpio.h> MODULE_LICENSE("GPL");
MODULE_AUTHOR("bunfly"); long my_ioctl(struct file *fp, unsigned int id, unsigned long fd);
int file_open(struct inode *no, struct file *fp); struct cdev wangcai;
struct file_operations fops;
dev_t devno;
unsigned long gpio_virt;
unsigned long *gpm4con, *gpm4dat; int bunfly_init()
{
fops.open = file_open;
fops.unlocked_ioctl = my_ioctl; //wangcai.ops = &fops;
cdev_init(&wangcai, &fops);//初始化cdev成员,建立cdev和file_operations之间的连接
//wangcai.dev = (9, 0);
devno = MKDEV(, );//字符设备号注册
//insert_list(&wangcai);
cdev_add(&wangcai, devno, );// 向系统添加一个dev,完成字符设备的注册,常用于模块加载函数中 gpio_virt = ioremap(0x11000000, SZ_4K);//led物理地址到虚拟地址映射
gpm4con = gpio_virt + 0x02e0;
gpm4dat = gpio_virt + 0x02e4; return ;
} int bunfly_exit()
{
cdev_del(&wangcai); /*注销设备*/
unregister_chrdev_region(MKDEV(, ), );
printk("this is bunfly_exit\n"); return ;
} module_init(bunfly_init);
module_exit(bunfly_exit); int file_open(struct inode *no, struct file *fp)
{
return ;
} long my_ioctl(struct file *fp, unsigned int id, unsigned long fd)
{
if(id == ) {
*gpm4con = 0x1111;
*gpm4dat = 0x0;
}
if(id == ) {
*gpm4con = 0x1111;
*gpm4dat = 0xf;
}
}
通过上面的代码我们已经了解了字符设备驱动的原理,在linux下应用层看到的设备都只 是一个名字,应用层打开一个设备最终会调到内核中的file_operations方法来进行读写操作,如果我们只创建一个的设备的时候,我们可以对他正 常的读写,那如果当我们有两个设备时,我们是否还能正常的进行读写操作,明显是存在问题的,就是第二次的数据会将第一次的数据覆盖,因为我们只有一个数据存储的data;那么解决这个问题的方法就是封装;
下面是具体代码:
#include <linux/init.h>
#include <linux/module.h>
#include <linux/cdev.h>
#include <linux/fs.h> MODULE_LICENSE("GPL");
MODULE_AUTHOR("bunfly"); int file_open(struct inode *no, struct file *fp);
ssize_t file_read(struct file *fp, char *buff, size_t size, loff_t *loff);
ssize_t file_write(struct file *fp, const char *buff, size_t size, loff_t *loff); struct file_operations fops;//方法 /*封装*/
struct bunfly_cdev {
dev_t devno;//设备号
struct cdev cdev;
unsigned char data[];
}; int bunfly_init()
{
fops.open = file_open;
fops.read = file_read;
fops.write = file_write; struct bunfly_cdev wangcai;
cdev_init(&wangcai.cdev, &fops);//初始化cdev成员,建立cdev和file_operations之间的连接
wangcai.devno = MKDEV(, );//注册设备号
cdev_add(&wangcai.cdev, wangcai.devno, );// 向系统添加一个dev,完成字符设备的注册,常用于模块加载函数中 struct bunfly_cdev tugou;
cdev_init(&tugou.cdev, &fops);
tugou.devno = MKDEV(, );
cdev_add(&tugou.cdev, tugou.devno, ); return ;
} int bunfly_exit()
{
printk("this is bunfly_exit\n"); return ;
} module_init(bunfly_init);
module_exit(bunfly_exit); int file_open(struct inode *no, struct file *fp)
{
struct cdev *addr = no->i_cdev;//找到struct cdev dev 在struct bunfly_cdev中的地址
struct bunfly_cdev *this = container_of(addr, struct bunfly_cdev, cdev);
fp->private_data = this; //父类在子类中的地址 //子类类型 return ;
}
ssize_t file_read(struct file *fp, char *buff, size_t size, loff_t *loff)
{
struct bunfly_cdev *this = fp->private_data;
strcpy(buff,this-> data); return size;
} ssize_t file_write(struct file *fp, const char *buff, size_t size, loff_t *loff)
{
struct bunfly_cdev *this = fp->private_data;
memset(this->data, , );
strcpy(this->data, buff); return size;
}
代码中有看到了container_of,再次强调掌握,还需要注意的是:
(1)每一个设备文件仅有inode结构体 ;
(2)每打开一次文件就创建一个file 结构体;
linux设备驱动之字符设备驱动模型(1)的更多相关文章
- 【转】深入浅出:Linux设备驱动之字符设备驱动
深入浅出:Linux设备驱动之字符设备驱动 一.linux系统将设备分为3类:字符设备.块设备.网络设备.使用驱动程序: 字符设备:是指只能一个字节一个字节读写的设备,不能随机读取设备内存中的某一数据 ...
- 手把手教Linux驱动3-之字符设备架构详解,有这篇就够了
一.Linux设备分类 Linux系统为了管理方便,将设备分成三种基本类型: 字符设备 块设备 网络设备 字符设备: 字符(char)设备是个能够像字节流(类似文件)一样被访问的设备,由字符设备驱动程 ...
- Linux驱动设计——字符设备驱动(一)
Linux字符设别驱动结构 cdev结构体 struct cdev { struct kobject kobj; struct module *owner; const struct file_ope ...
- Linux 设备驱动之字符设备
参考转载博客:http://blog.chinaunix.net/uid-26833883-id-4369060.html https://www.cnblogs.com/xiaojiang1025/ ...
- linux中c表示字符设备文件符号
linux中c表示字符设备文件,b表示块设备文件,l表示符号链接文件,r表示可读权限,w表示可写权限.linux文件属性解读:文件类型:-:普通文件 (f)d:目录文件b:块设备文件 (block)c ...
- linux设备驱动之字符设备驱动模型(2)
在上一篇中我们已经了解了字符设备驱动的原理,也了解了应用层调用内核函数的机制,但是我们每次操作设备,都必须首先通过mknod命令创建一个设备文件名,比如说我们要打开u盘,硬盘等这些设备,难道我们还要自 ...
- 深入浅出:Linux设备驱动之字符设备驱
一.linux系统将设备分为3类:字符设备.块设备.网络设备.使用驱动程序: 字符设备:是指只能一个字节一个字节读写的设备,不能随机读取设备内存中的某一数据,读取数据需要按照先后数据.字符设备是面向流 ...
- 【Linux驱动】字符设备驱动
一.linux系统将设备分为3类:字符设备.块设备.网络设备.使用驱动程序: 1.字符设备:是指只能一个字节一个字节读写的设备,不能随机读取设备内存中的某一数据,读取数据需要按照先后数据.字符设备是面 ...
- 蜕变成蝶~Linux设备驱动之字符设备驱动
一.linux系统将设备分为3类:字符设备.块设备.网络设备.使用驱动程序: 字符设备:是指只能一个字节一个字节读写的设备,不能随机读取设备内存中的某一数据,读取数据需要按照先后数据.字符设备是面向流 ...
随机推荐
- 算法学习笔记(一)C++排序函数、映射技巧与字典树
1.头文件algorithm中有函数sort()用于排序,参数为:排序起始地址,排序结束地址,排序规则(返回bool型)例如,要将array[] = {5,7,1,2,9}升序排列,则使用: bool ...
- Linux - info
基本上,info与man的用途其实差不多,都是用来查询命令的用法或者是文件的格式.但是与man page一口气输出一堆信息不同的是,info page则是将文件数据拆成一个一个的段落,每个段落用自己的 ...
- HBase 快照操作
1.配置hbase-site.xml <property> <name>hbase.snapshot.enabled</name> <value>tru ...
- 新版MATERIAL DESIGN 官方动效指南(二)
继上一篇,本文继续第二部分,从动效的速度.动态持续时间.通用持续时间和缓动曲线4个部分,教你创建平滑一致的Material Design 动效.再系统的干货都比不上官方的动效指南,西瓜就在这,赶紧来捡 ...
- Oracle统一访问代理层方案
目标 提供一个oracle数据库统一访问代理层,统一管理所有oracle数据库用户名的连接池,让多个应用系统相同的数据库用户公用连接池以节省oracle服务器的总连接数,并且提供统一管理oracle能 ...
- 面试之路(29)-TCP流量控制和拥塞控制-滑动窗口协议详解
拥塞: 拥塞发生的主要原因在于网络能够提供的资源不足以满足用户的需求,这些资源包括缓存空间.链路带宽容量和中间节点的处理能力.由于互联网的设计机制导致其缺乏"接纳控制"能力,因此在 ...
- C语言算法---求鞍点
题目:有一个3X4矩阵,要求输出其鞍点(行列均最大的值),以及它的行号和列号. int a[3][4] = {{123,94,-10,218}, {3 ...
- Linux 系统应用编程——线程基础
传统多任务操作系统中一个可以独立调度的任务(或称之为顺序执行流)是一个进程.每个程序加载到内存后只可以唯一地对应创建一个顺序执行流,即传统意义的进程.每个进程的全部系统资源是私有的,如虚拟地址空间,文 ...
- vicoapp使用备忘
vico是一个模式编辑器,意味着没用过vi之类编辑器的童鞋用起来肯定觉得很不习惯. 模式切换 i:切至编辑模式,在光标前插入 a:切至编辑模式,在在光标后插入 I:类似于i,不过在行首插入 esc键: ...
- Eclipse RCP中超长任务单线程,异步线程处理
转自:http://www.blogjava.net/mydearvivian/articles/246028.html 在RCP程序中,常碰到某个线程执行时间比较很长的情况,若处理不好,用户体验度是 ...