(五)Linux之设备驱动模型

(一)Linux内核驱动简介

对于刚接触linux驱动的同学来说,应该思考一个问题就是为什么要有Linux内核驱动呢?原因就是Linux内核对设备的驱动编写进行了规范。

我们在linux系统的/dev目录下可以查看设备节点文件,这些节点文件是怎么存在的也是值得思考的,本篇文章将会讲解一个杂项设备驱动模型、早期经典字符设备驱动模型,都是针对字符设备进行讲解的。

图一
通过ls选项看到,文件权限前面的符号表示该文件是字符设备文件、块设备文件或者其他的,每一个设备文件都有一个主设备号和一个次设备号

主设备号:标识设备的类型,标识具体的某一类设备,如usb设备为一类设备
次设备号:标识某一类设备中的具体的某一个设备,如我的金士顿U盘

驱动程序为程序员开发后放入内核的功能模块,所以驱动程序本身不属于内核的一部分,导致在向内核添加驱动功能的时候需要向内核提出申请,即注册操作。

(二)杂项设备驱动模型

杂项设备:主设备号固定为10 的设备称为在下个设备,Linux驱动中把无法归类的五花八门的设备定义为混杂设备(用miscdevice结构体表述)。miscdevice共享一个主设备号MISC_MAJOR(即10),但次设备号不同。

(1)相关接口

int misc_register(struct miscdevice * misc)

int:返回值,成功返回0,失败返回负数
struct miscdevice * misc:设备结构体
    struct miscdevice  {
    	int minor;	//次设备号	
        // 当minor的值为255的时候内核会自动分配次设备号,
        // 一般采用此方法,因为自己指定容易和已有的次设备号冲突		
    	const char *name;  //设备节点名	
     	const struct file_operations *fops;//文件操作指针结构体
    	struct list_head list; //杂项设备链表,使用者不用关心
    	struct device *parent; //父设备类,无需关心
    	struct device *this_device; //本设备,无需关心
    	const char *nodename; //节点名
    	umode_t mode; //权限
    };

注册杂项设备需要关心的参数:
int minor; //次设备号
const char *name; //设备节点名
const struct file_operations *fops;//文件操作指针结构体

struct file_operations结构体如下:

struct file_operations {
	struct module *owner; //一般赋值为THIS_MODULE
	loff_t (*llseek) (struct file *, loff_t, int);
	/*
    ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);
     char __user *:内核空间读取到文件中的数据,直接传递到用户空间调用的read接口中
     ssize_t read(int fd, void *buf, size_t count);
     buf中的数据即来源于  char __user *
    */
	/*
	 ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);
	 const char __user *:用户层调用的write接口向文件写入的数据
	 ssize_t write(int fd, const void *buf, size_t count);
	 buf中的数据直接传递到了const char __user *
	 size_t:数据的大小
	 loff_t *指的是写入数据的偏移量 
   */
	unsigned int (*poll) (struct file *, struct poll_table_struct *);
	long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);
	int (*open) (struct inode *, struct file *);
	int (*release) (struct inode *, struct file *);//内核中的release等同于系统的close
	int (*fsync) (struct file *, loff_t, loff_t, int datasync);
	int (*fasync) (int, struct file *, int);
	int (*lock) (struct file *, int, struct file_lock *);
};	
以int (*open) (struct inode *, struct file *);为例:
open指针指向的函数是用来接收系统层调用的open函数所传递的参数值
struct inode *:保存文件属性的
struct file *:保存文件操作中的数据
(2)杂项设备注册过程
  1. 定以struct miscdevice 并初始化
  2. 定以struct file_operations并初始化
  3. 使用misc_register(struct miscdevice * misc)进行注册
  4. 注销-------在模块程序中的卸载函数中进行注销操作

下面以一个例子讲解,重点看注册函数misc_register:
miscdevice.c

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/miscdevice.h>
#include <linux/fs.h>

#define MISCDEVICE_NAME  "miscdevice"

ssize_t misc_read(struct file * fp , char __user * buf, size_t size, loff_t * offset)
{
  printk("this is misc read\n");
  return 0;
}
ssize_t misc_write(struct file * fp , char __user * buf, size_t size, loff_t * offset)
{
  printk("this is misc write\n");
  return 0;
}

int misc_release (struct inode * node, struct file * fp)
{
  printk("this is misc release \n");
  return 0;
}
int misc_open(struct inode * node, struct file * fp)
{
  printk("this is misc open \n");
  return 0;
}
	
struct file_operations fp=
{
.read=misc_read,
.write=misc_write,
.release=misc_release,
.open =misc_open
};

struct miscdevice misc =
{
	.minor = 255,
	.name = MISCDEVICE_NAME,
	.fops=&fp
};
static int __init  miscdevice_init(void)
{
  printk("miscdevice init success\n");
   misc_register(&misc);//注册杂项设备
   return 0;
}

static void __exit miscdevice_cleanup(void)
{
  printk("miscdevice exit success\n");
  misc_deregister(&misc);//注销杂项设备
}
module_init(miscdevice_init);
module_exit(miscdevice_cleanup);
MODULE_LICENSE("GPL");

以上注册和注销都依赖于模块化编程,不懂的可以参考:Linux内核模块化编程

编写系统调用函数
misc_app.c

#include<stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int main(int argc, char *argv[])
{
  int fd= open(argv[1],O_RDWR);
  if(fd== -1)
  {
    perror("open");
    return -1;
  }
  write(fd,"hell",4);
  close(fd);
  return 0;
}

Makfile

CFLAG = -C
TARGET = miscdevice
APP=misc_app
KERNEL = /mydriver/linux-3.5
obj-m +=$(TARGET).o

all:
	make $(CFLAG)  $(KERNEL)  M=$(PWD)
	arm-linux-gcc -o $(APP) $(APP).c
clean:
	make $(CFLAG)  $(KERNEL)  M=$(PWD) clean
	rm $(APP)

使用交叉编译器编译msic_app.c后,并且刚刚编写的miscdevice.c编译成模块后
启动tiny4412开发板查看
图一
注意:

  • 1、注册成功会在根文件系统的dev目录下产生一个指定的节点文件
  • 2、设备驱动模型中的接口(文件操作接口)只有上层接口调用的时候才会有效果
  • 3、操作驱动模型对应的设备只需序打开该设备对应的节点文件名
(三)早期经典字符设备驱动模型
(1)相关接口

int register_chrdev(unsigned int major, const char *name, const struct file_operations *fops)

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: major device number or 0 for dynamic allocation
 *  主设备号或者0用来自动分配
* @baseminor: first of the requested range of minor numbers 默认为0
* 申请设备号的起始次设备号,在此处默认为0
* @count: the number of minor numbers required
*  申请次设备号的个数
* @name: name of this range of devices
* 申请设备号对应的设备名
* @fops: file operations associated with this devices
* 文件操作结构体

static inline void unregister_chrdev(unsigned int major, const char *name)

static inline void unregister_chrdev(unsigned int major, const char *name)
{
	__unregister_chrdev(major, 0, 256, name);
}
(2)杂项设备注册过程
  1. 定以并初始化struct file_operations
  2. 在模块的加载函数中对驱动模型进行注册register_chrdev
  3. 在模块的注销函数中注销早期设备驱动模型unregister_chrdev

下面以例子进行讲解,重点看注册函数register_chrdev:

char_device.c:
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/fs.h>
int major =0;
ssize_t chr_read (struct file * fp, char __user * buf, size_t size, loff_t * offset )
{
  printk(" this is read\n");
  return 0;
}
ssize_t chr_write (struct file * fp, char __user * buf, size_t size, loff_t * offset )
{
  printk(" this is write\n");
  return 0;
}
int chr_close (struct inode * node ,  struct file * file )
{
  printk(" this is close\n");
  return 0;
}
int chr_open (struct inode * node , struct file * file)
{
  printk(" this is open\n");
  return 0;
}
struct file_operations fp={
  .read =chr_read, 
  .write =chr_write,
  .release =chr_close,
  .open =chr_open, 
};	
static int __init chardev_init(void)
{
  major = register_chrdev(0, "ming", &fp);
  printk("this is module init,major =%d\n",major);
  return 0;
}
static void __exit chardev_cleanup(void)
{
  unregister_chrdev(major,"ming");
  printk("this is module exit\n");
}
module_init(chardev_init);
module_exit(chardev_cleanup);
MODULE_LICENSE("GPL");

关于测试函数同杂项设备的misc_app.c和Makefile,在此不重复写了,下面看现象
图二

(二)杂项和早期经典的区别
  • 1、杂项设备的主设备号固定位10,,早期经典设备的主设备号是0-255除10外
  • 2、杂项设备的一个设备对应一个次设备号,而早期经典的模型中,一旦申请成功,则该主设备号下的所有次设备号均对应一个设备,次设备号的范围为0-255

本文章仅供学习交流用禁止用作商业用途,文中内容来水枂编辑,如需转载请告知,谢谢合作

微信公众号:zhjj0729

微博:文艺to青年

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值