PROGRAM FOR BLOCK DEVICE DRIVER OF DEVFS TYPE
對linux的devfs類型的驅(qū)動程序的編寫可以從以下幾大內(nèi)容理解和入手:
通過分析驅(qū)動程序源代碼可以發(fā)現(xiàn)驅(qū)動程序一般可分三部分:
核心數(shù)據(jù)結(jié)構(gòu);核心數(shù)據(jù)和資源的初始化,注冊以及注消,釋放;底層設(shè)備操作函數(shù);
A.核心數(shù)據(jù)結(jié)構(gòu)
struct file_operations fops 設(shè)備驅(qū)動程序接口
struct file_operations {
struct module *owner;
loff_t (*llseek) (struct file *, loff_t, int);
ssize_t (*read) (struct file *, char *, size_t, loff_t *);
ssize_t (*write) (struct file *, const char *, size_t, 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);
int (*mmap) (struct file *, struct vm_area_struct *);
int (*open) (struct inode *, struct file *);
int (*flush) (struct file *);
int (*release) (struct inode *, struct file *);
int (*fsync) (struct file *, struct dentry *, int datasync);
int (*fasync) (int, struct file *, int);
int (*lock) (struct file *, int, struct file_lock *);
ssize_t (*readv) (struct file *, const struct iovec *, unsigned long, loff_t *);
ssize_t (*writev) (struct file *, const struct iovec *, unsigned long, loff_t *);
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);
};
block_device_operations 塊設(shè)備驅(qū)動程序接口
{ int (*open) (struct inode *, struct file *);
int (*release) (struct inode *, struct file *);
int (*ioctl) (struct inode *, struct file *, unsigned, unsigned long);
int (*check_media_change) (kdev_t);
int (*revalidate) (kdev_t);
struct module *owner;
};塊設(shè)備的READ().WRITE()不在這里注冊,而是在設(shè)備的讀寫請求隊列里注冊,內(nèi)核在這里將調(diào)用通用的blk_read(),blk_write().向讀寫隊列
發(fā)出讀寫請求.
Linux 利用這些數(shù)據(jù)結(jié)構(gòu)向內(nèi)核注冊open(),release(),ioctl(),check_media_change(),rvalidate()等函數(shù)的入口句柄.
我們將要編寫的open(),release(),ioctl(),check_media_change(),revalidate()等函數(shù),將在驅(qū)動初始化的時候,
通過一個此結(jié)構(gòu)類型的變量向內(nèi)核提供函數(shù)的 入口.
struct request_queue_t 設(shè)備請求隊列的數(shù)據(jù)結(jié)構(gòu)
struct request_list {
unsigned int count;
unsigned int pending[2];
struct list_head free;
};
struct request {
struct list_head queue;
int elevator_sequence;
kdev_t rq_dev;
int cmd; /* READ or WRITE */
int errors;
unsigned long start_time;
unsigned long sector;
unsigned long nr_sectors;
unsigned long hard_sector, hard_nr_sectors;
unsigned int nr_segments;
unsigned int nr_hw_segments;
unsigned long current_nr_sectors, hard_cur_sectors;
void * special;
char * buffer;
struct completion * waiting;
struct buffer_head * bh;
struct buffer_head * bhtail;
request_queue_t *q;
};
struct request_queue
{
/*
* the queue request freelist, one for reads and one for writes
*/
struct request_list rq;
/*
* The total number of requests on each queue
*/
int nr_requests;
/*
* Batching threshold for sleep/wakeup decisions
*/
int batch_requests;
/*
* The total number of 512byte blocks on each queue
*/
atomic_t nr_sectors;
/*
* Batching threshold for sleep/wakeup decisions
*/
int batch_sectors;
/*
* The max number of 512byte blocks on each queue
*/
int max_queue_sectors;
/*
* Together with queue_head for cacheline sharing
*/
struct list_head queue_head;
elevator_t elevator;
request_fn_proc * request_fn;
merge_request_fn * back_merge_fn;
merge_request_fn * front_merge_fn;
merge_requests_fn * merge_requests_fn;
make_request_fn * make_request_fn;
plug_device_fn * plug_device_fn;
/*
* The queue owner gets to use this for whatever they like.
* ll_rw_blk doesn‘t touch it.
*/
void * queuedata;
/*
* This is used to remove the plug when tq_disk runs.
*/
struct tq_struct plug_tq;
/*
* Boolean that indicates whether this queue is plugged or not.
*/
int plugged:1;
/*
* Boolean that indicates whether current_request is active or
* not.
*/
int head_active:1;
/*
* Boolean that indicates you will use blk_started_sectors
* and blk_finished_sectors in addition to blk_started_io
* and blk_finished_io. It enables the throttling code to
* help keep the sectors in flight to a reasonable value
*/
int can_throttle:1;
unsigned long bounce_pfn;
/*
* Is meant to protect the queue in the future instead of
* io_request_lock
*/
spinlock_t queue_lock;
/*
* Tasks wait here for free read and write requests
*/
wait_queue_head_t wait_for_requests;
struct request *last_request;
};
緩沖區(qū)和對緩沖區(qū)相應(yīng)的I/O操作在此任務(wù)隊列中相關(guān)聯(lián),等待內(nèi)核的調(diào)度.如果是字符設(shè)備就不需要此數(shù)據(jù)結(jié)構(gòu).而
塊設(shè)備的read(),write()函數(shù)則在buffer_queue的initize和設(shè)備請求隊列進行處理請求焙虼莞鴕equest_fn().
struct request_queue_t{}設(shè)備請求隊列的變量類型,驅(qū)動程序在初始化的時候需要填寫request_fn().
其他的數(shù)據(jù)結(jié)構(gòu)還有 I/O port,Irq,DMA 資源分配,符合POSIX標(biāo)準(zhǔn)的ioctl的cmd的構(gòu)造和定義,以及描述設(shè)備自身的
相關(guān)數(shù)據(jù)結(jié)構(gòu)定義-如設(shè)備的控制寄存器的相關(guān)數(shù)據(jù)結(jié)構(gòu)定義,BIOS里的參數(shù)定義,設(shè)備類型定義等.
B.初始化和注冊和注消,模塊方式驅(qū)動程序的加載和卸載.
設(shè)備驅(qū)動程序在定義了數(shù)據(jù)結(jié)構(gòu)后 ,首先開始初始化:
如I/O 端口的檢查和登記,內(nèi)核對 I/O PORT的檢查和登記提供了兩個 函數(shù)check_region(int io_port, int off_set)
和request_region(int io_port, int off_set,char *devname).I/O Port登記后,就可以用inb()和outb()進行操作了 .
還有DMA和Irq的初始化檢查和 登記,
int request_irq(unsigned int irq ,void(*handle)(int,void *,struct pt_regs *),unsigned int long flags,
const char *device);
irq: 是要申請的中斷。
handle:中斷處理函數(shù)指針。
flags:SA_INTERRUPT 請求一個快速中斷,0 正常中斷。
device:設(shè)備名。
如果登記成功,返回0,這時在/proc/interrupts文件中可以看你請求的中斷。
DMA主要是在內(nèi)存中分配交換內(nèi)存空間.還有緩沖區(qū),設(shè)備請求隊列的初始化.
還有設(shè)備控制寄存器的檢查和初始化,還有對設(shè)備自身相關(guān)的數(shù)據(jù)結(jié)構(gòu)的初始化,填寫一些設(shè)備特定的數(shù)據(jù)等.
然后,開始注冊
devfs_register()向VFS注冊統(tǒng)一的設(shè)備操作函數(shù).
static struct file_operations XXX_fops = {
owner: THIS_MODULE, XXX_fops所屬的設(shè)備模塊
read: XXX_read, 讀設(shè)備操作
write: XXX_write, 寫設(shè)備操作
ioctl: XXX_ioctl, 控制設(shè)備操作
mmap: XXX_mmap, 內(nèi)存重映射操作
open: XXX_open, 打開設(shè)備操作
release: XXX_release 釋放設(shè)備操作
/* ... */
};
blk_init_queue()隊列初始化函數(shù).
request_irq()中斷注冊函數(shù)
相應(yīng)的注消函數(shù):
devfs_unregister (devfs_handle_t de){};
free_irq()釋放中斷,I/O資源,釋放緩沖區(qū),釋放設(shè)備,請求隊列,VFS節(jié)點等.
模塊方式驅(qū)動程序的加載和卸載.
static int __init _init_module (void)
{
/* ... */
}
static void __exit _cleanup_module (void)
{
}
/* 加載驅(qū)動程序模塊入口 */
module_init(_init_module);
/* 卸載驅(qū)動程序模塊入口 */
module_exit(_cleanup_module);
_intrrupt()
設(shè)備發(fā)生中斷時的處理程序.
{
1.對共享中斷的處理;
2.對spinlock以及其他的事務(wù)的處理;
}
C. 底層設(shè)備操作函數(shù)的編寫
read().write(),open(),release(),check_media_change(),revalidate()等.
open()和release()
打開設(shè)備是通過調(diào)用file_operations結(jié)構(gòu)中的函數(shù)open( )來完成的,它是驅(qū)動程序用來為今后的操作完成初始化準(zhǔn)備工作的。在大部分驅(qū)動程序中,open( )通常需要完成下列工作:
1. 檢查設(shè)備相關(guān)錯誤,如設(shè)備尚未準(zhǔn)備好等。
2. 如果是第一次打開,則初始化硬件設(shè)備。
3. 識別次設(shè)備號,如果有必要則更新讀寫操作的當(dāng)前位置指針f_ops。
4. 分配和填寫要放在file->private_data里的數(shù)據(jù)結(jié)構(gòu)。
5. 使用計數(shù)增1。
釋放設(shè)備是通過調(diào)用file_operations結(jié)構(gòu)中的函數(shù)release( )來完成的,這個設(shè)備方法有時也被稱為close( ),它的作用正好與open( )相反,通常要完成下列工作:
1. 使用計數(shù)減1。
2. 釋放在file->private_data中分配的內(nèi)存。
3. 如果使用計算為0,則關(guān)閉設(shè)備。
read()和 write()
字符設(shè)備的讀寫操作相對比較簡單,直接使用函數(shù)read( )和write( )就可以了。但如果是塊設(shè)備的話,則需要調(diào)用函數(shù)block_read( )和block_write( )來進行數(shù)據(jù)讀寫,這兩個函數(shù)將向設(shè)備請求表中增加讀寫請求,以便Linux內(nèi)核可以對請求順序進行優(yōu)化。由于是對內(nèi)存緩沖區(qū)而不是直接對設(shè)備進行操作的,因此能很大程度上加快讀寫速度。如果內(nèi)存緩沖區(qū)中沒有所要讀入的數(shù)據(jù),或者需要執(zhí)行寫操作將數(shù)據(jù)寫入設(shè)備,那么就要執(zhí)行真正的數(shù)據(jù)傳輸,這是通過調(diào)用數(shù)據(jù)結(jié)構(gòu)blk_dev_struct中的函數(shù)request_fn( )來完成的。
ioctl()--將cmd進行解釋,并送到設(shè)備的控制寄存器.事實上,read()和write()也要通過ioctl()來完成操作的 .
ioctl(){
CASE CMD{
SWITCH CASE1:{...};
SWITCH CASE2:{...};
SWITCH CASE N:{...};
.
.
DEFAULT : {...};
}
END CASE
總結(jié):
我們可以看出一個linux的驅(qū)動程序通常包含如下:
初始化設(shè)備模塊、
{I/O port ,DMA.Irq,內(nèi)存 buffer,初始化并且填寫具體設(shè)備數(shù)據(jù)結(jié)構(gòu),注冊 fops的具體函數(shù)等等 }
中斷處理模塊、設(shè)備釋放模塊、設(shè)備卸載模塊
設(shè)備打開模塊、數(shù)據(jù)讀寫和控制模塊、
驅(qū)動裝載模塊、驅(qū)動釋放模塊.