一、字元驅動程式設計模型
在linux系統中,裝置的類型非常繁多,比如:字元裝置,塊裝置,網絡接口裝置,USB裝置,PCI裝置,平台裝置,混雜裝置...,而裝置類型不同,也意味着其對應的驅動程式模型不同,這樣就導緻了我們需要去掌握衆多的驅動程式模型。那麼能不能從這些衆多的驅動模型中提煉一些具有共性的規則,則是我們能不能學好linux驅動的關鍵。
![](https://img.laitimes.com/img/9ZDMuAjOiMmIsIjOiQnIsIiN0UTOykzM5ATMyMDM0EDMy8CX0Vmbu4GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.jpg)
1、裝置描述結構cdev
在任何一種驅動模型中,裝置都會用核心中的一種結構來描述。我們的字元裝置在核心中使用struct cdev來描述。
1.1、結構定義
struct cdev
{
struct kobject kobj;
struct module *owner;
const struct file_operations *ops; //裝置操作集
struct list_head list;
dev_t dev; //裝置号
unsigned int count; //裝置數
};
1.2、裝置号
檢視/dev目錄下的裝置号;字元裝置檔案通過主裝置号與字元裝置驅動建立對應關系。
驅動程式通過次裝置号來區分同類型的裝置(主裝置号反映裝置類型)。
裝置号的操作:linux核心中使用dev_t類型來定義裝置号,dev_t這種類型其實質為32位的unsigned int,其中高12位為主裝置号,低20位為次裝置号。
問1:如果知道主裝置号,次裝置号,怎麼組合成dev_t類型
答:dev_t dev = MKDEV(主裝置号, 次裝置号)
問2:如何從dev_t中分解出主裝置号?
答:主裝置号 = MAJOR(dev_t dev)
問3:如何從dev_t中分解出次裝置号?
答:次裝置号 = MINOR(dev_t dev)
如何為裝置配置設定一個主裝置号?
靜态申請
開發者自己選擇一個數字作為主裝置号,然後通過函數register_chrdev_region向核心申請使用。缺點:如果申請使用的裝置号已經被核心中的其他驅動使用了,則申請失敗。
動态申請
使用alloc_chrdev_region由核心配置設定一個可用的主裝置号。優點:因為核心知道哪些号已經被使用了,是以不會導緻配置設定到已經被使用的号。
裝置号-登出
不論使用何種方法配置設定裝置号,都應該在驅動退出時,使用unregister_chrdev_region函數釋放這些裝置号。
1.3、裝置操作集
struct file_operations是一個函數指針的集合,定義能在裝置上進行的操作。結構中的函數指針指向驅動中的函數,這些函數實作一個針對裝置的操作,對于不支援的操作則設定函數指針為NULL。例如:
struct file_operations dev_fops =
{
.llseek = NULL,
.read = dev_read,
.write = dev_write,
.ioctl = dev_ioctl,
.open = dev_open,
.release = dev_release,
};
上面的表就實作了映射,把來自使用者系統調用轉換為核心中的函數來完成相關操作,file_operations就是這個映射表。
cdev的配置設定
cdev變量的定義可以采用靜态和動态兩種辦法
靜态配置設定
struct cdev mdev;
動态配置設定
struct cdev *pdev = cdev_alloc();
cdev的初始化
struct cdev的初始化使用cdev_init函數來完成。
cdev_init(struct cdev *cdev, const struct file_operations *fops)
參數:
cdev:待初始化的cdev結構
fops:裝置對應的操作函數集
cdev的注冊
字元裝置的注冊使用cdev_add函數來完成。
cdev_add(struct cdev *p, dev_t dev, unsigned int count)
參數:
p:待添加到核心的字元裝置結構
dev:裝置号(主裝置号、次裝置号)
count:該類裝置的裝置個數
硬體初始化:
根據相應的硬體的晶片手冊,完成初始化。
實作裝置操作:
應用程式通路驅動是通過一個映射關系表,而這張表就是file_operation。它是一個含有函數的指針的結構體
下面是核心中的映射表(file_operation):
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 *, 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);
}
裝置方法原型:
1、int (*open)(struct inode *, struct file *)
打開裝置,響應open系統
2、int (*release)(struct inode *, struct file *)
關閉裝置,響應close系統調用
3、loff_t(*llseek)(struct inode *, struct file *)
重定位讀寫指針,響應lseek系統調用
4、ssize_t(*read)(struct file *,char _user *, size_t, loff_t *)
從裝置讀取資料,響應read系統調用
5、ssize_t(*write)(struct file *, const char _user *, size_t, loff_t *)
向裝置寫入資料,響應write系統調用
下面介紹一下裝置方法中常用到的兩個結構體:
struct file
struct inode
1、struct file
在linux系統中,每一個打開的檔案,在核心中都會關聯一個struct file,它由核心在打開檔案時建立,在檔案關閉後釋放。
重要成員:
loff_t f_pos 檔案讀寫指針
struct file_operations *f_op 該檔案所對應的操作
2、struct inode
每一個存在于檔案系統裡面的檔案都會關聯一個inode結構,該結構主要用來記錄檔案實體上的資訊。是以,它和代表打開檔案的file結構是不同的。一個檔案沒有被打開時不會關聯file結構,但是卻會關聯一個inode結構。
重要成員:
dev_t i_rdev 裝置号
裝置方法:
1、open
open裝置方法是驅動程式用來為以後的操作完成初始化準備工作的。在大部分驅動程式中,open完成如下工作:
标明次裝置号
啟動裝置
2、release
release方法的作用正好與open相反。這個裝置方法有時也稱為close,它是關閉裝置的。
3、read
它通常完成兩件事情:
從裝置中讀取資料(屬于硬體通路類操作)
将讀取到的資料傳回給應用程式
ssize_t (*read)(struct file *filp, char _user *buff, size_t count, loff_t *offp)
參數分析:
filp:與字元裝置檔案關聯的file結構指針,由核心建立
buff:從裝置讀取到的資料,需要儲存到的位置。由read系統調用提供該參數。
count:請求傳輸的資料量,由read系統調用提供該參數。
offp:檔案的讀寫位置,由核心從file結構中取出後,傳遞進來。
buff參數是來自使用者空間的指針,這類指針都不能被核心代碼直接引用,必須使用專門的函數
1、int copy_from_user(void *to, const void _user *from, int n)
2、int copy_from_user(void _user *to, const void *from, int n)
4、write
write裝置方法通常完成2件事情:
從應用程式提供的位址中取出資料
将資料寫入裝置(屬于硬體通路類操作)
ssize_t (* write)(struct file *, const char _user *, size_t, loff_t *)
其參數類似于read
驅動登出:
當我們從核心中解除安裝驅動程式的時候,需要使用cdev_del函數來完成字元裝置的登出。
2、字元裝置驅動模型
3、範例驅動分析
#include <linux/module.h>
#include <linux/types.h>
#include <linux/fs.h>
#include <linux/errno.h>
#include <linux/init.h>
#include <linux/cdev.h>
#include <asm/uaccess.h>
#include <linux/slab.h>
int dev1_registers[5];
int dev2_registers[5];
struct cdev cdev;
dev_t devno;
int mem_open(struct inode *inode, struct file *filp)
{
int num = MINOR(inode->i_rdev);
if (num==0)
filp->private_data = dev1_registers;
else if(num == 1)
filp->private_data = dev2_registers;
else
return -ENODEV; //無效的次裝置号
return 0;
}
int mem_release(struct inode *inode, struct file *filp)
{
return 0;
}
static ssize_t mem_read(struct file *filp, char __user *buf, size_t size, loff_t *ppos)
{
unsigned long p = *ppos;
unsigned int count = size;
int ret = 0;
int *register_addr = filp->private_data;
if (p >= 5*sizeof(int))
return 0;
if (count > 5*sizeof(int) - p)
count = 5*sizeof(int) - p;
if (copy_to_user(buf, register_addr+p, count))
{
ret = -EFAULT;
}
else
{
*ppos += count;
ret = count;
}
return ret;
}
static ssize_t mem_write(struct file *filp, const char __user *buf, size_t size, loff_t *ppos)
{
unsigned long p = *ppos;
unsigned int count = size;
int ret = 0;
int *register_addr = filp->private_data;
if (p >= 5*sizeof(int))
return 0;
if (count > 5*sizeof(int) - p)
count = 5*sizeof(int) - p;
if (copy_from_user(register_addr + p, buf, count))
ret = -EFAULT;
else
{
*ppos += count;
ret = count;
}
return ret;
}
static loff_t mem_llseek(struct file *filp, loff_t offset, int whence)
{
loff_t newpos;
switch(whence) {
case SEEK_SET:
newpos = offset;
break;
case SEEK_CUR:
newpos = filp->f_pos + offset;
break;
case SEEK_END:
newpos = 5*sizeof(int)-1 + offset;
break;
default:
return -EINVAL;
}
if ((newpos<0) || (newpos>5*sizeof(int)))
return -EINVAL;
filp->f_pos = newpos;
return newpos;
}
static const struct file_operations mem_fops =
{
.llseek = mem_llseek,
.read = mem_read,
.write = mem_write,
.open = mem_open,
.release = mem_release,
};
static int memdev_init(void)
{
cdev_init(&cdev, &mem_fops);
alloc_chrdev_region(&devno, 0, 2, "memdev");
cdev_add(&cdev, devno, 2);
}
static void memdev_exit(void)
{
cdev_del(&cdev);
unregister_chrdev_region(devno, 2);
}
MODULE_LICENSE("GPL");
module_init(memdev_init);
module_exit(memdev_exit);