当前位置:Linux教程 - Linux资讯 - linux驱动程序设计

linux驱动程序设计

 PROGRAM FOR BLOCK DEVICE DRIVER OF DEVFS TYPE 对Linux的devfs类型的驱动程序的编写可以从以下几大内容理解和入手:
通过分析驱动程序源代码可以发现驱动程序一般可分三部分:
核心数据结构;核心数据和资源的初始化,注册以及注消,释放;底层设备操作函数;

A.核心数据结构
strUCt file_operations fops 设备驱动程序接口
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 块设备驱动程序接口
{ 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;
};块设备的READ().WR99vE()不在这里注册,而是在设备的读写请求队列里注册,内核在这里将调用通用的blk_read(),blk_write().向读写队列
发出读写请求.

Linux 利用这些数据结构向内核注册open(),release(),ioctl(),check_media_change(),rvalidate()等函数的入口句柄.
我们将要编写的open(),release(),ioctl(),check_media_change(),revalidate()等函数,将在驱动初始化的时候,
通过一个此结构类型的变量向内核提供函数的 入口.

struct request_queue_t 设备请求队列的数据结构
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 WR99vE */
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;

[1] [2] [3] 下一页 

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;
};

缓冲区和对缓冲区相应的I/O操作在此任务队列中相关联,等待内核的调度.如果是字符设备就不需要此数据结构.而
块设备的read(),write()函数则在buffer_queue的initize和设备请求队列进行处理请求时候传递给request_fn().
struct request_queue_t{}设备请求队列的变量类型,驱动程序在初始化的时候需要填写request_fn().

其他的数据结构还有 I/O port,Irq,DMA 资源分配,符合POSIX标准的ioctl的cmd的构造和定义,以及描述设备自身的
相关数据结构定义-如设备的控制寄存器的相关数据结构定义,BIOS里的参数定义,设备类型定义等.

B.初始化和注册和注消,模块方式驱动程序的加载和卸载.

设备驱动程序在定义了数据结构后 ,首先开始初始化:
如I/O 端口的检查和登记,内核对 I/O PORT的检查和登记提供了两个 函数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:中断处理函数指针。

flags:SA_INTERRUPT 请求一个快速中断,0 正常中断。

上一页 [1] [2] [3] 下一页 


device:设备名。

如果登记成功,返回0,这时在/proc/interrupts文件中可以看你请求的中断。

DMA主要是在内存中分配交换内存空间.还有缓冲区,设备请求队列的初始化.

还有设备控制寄存器的检查和初始化,还有对设备自身相关的数据结构的初始化,填写一些设备特定的数据等.

然后,开始注册
devfs_register()向VFS注册统一的设备操作函数.
static struct file_operations XXX_fops = {
owner: THIS_MODULE, XXX_fops所属的设备模块
read: XXX_read, 读设备操作
write: XXX_write, 写设备操作
ioctl: XXX_ioctl, 控制设备操作
mmap: XXX_mmap, 内存重映射操作
open: XXX_open, 打开设备操作
release: XXX_release 释放设备操作
/* ... */
};


blk_init_queue()队列初始化函数.
request_irq()中断注册函数
相应的注消函数:
devfs_unregister (devfs_handle_t de){};
free_irq()释放中断,I/O资源,释放缓冲区,释放设备,请求队列,VFS节点等.

模块方式驱动程序的加载和卸载.
owner: THIS_MODULE, XXX_fops所属的设备模块 read: XXX_read, 读设备操作 write: XXX_write, 写设备操作 ioctl: XXX_ioctl, 控制设备操作 mmap: XXX_mmap, 内存重映射操作 open: XXX_open, 打开设备操作 release: XXX_release 释放设备操作 /* ... */ }; blk_init_queue()队列初始化函数. request_irq()中断注册函数 相应的注消函数: devfs_unregister (devfs_handle_t de){}; free_irq()释放中断,I/O资源,释放缓冲区,释放设备,请求队列,VFS节点等. 模块方式驱动程序的加载和卸载.