文档介绍:设备驱动程序
2
设备驱动程序的作用
设备驱动程序是一个软件层,该软件层使硬件设备响应预定义好的编程接口,我们已经熟悉这些接口,它由一组控制设备的函数(open,read,ioctl等等)组成,这些函数的实际实现由设备驱动程序全权负责。
设备驱动程序(应该只是)为系统的其它部分提供各种使用设备的能力,使用设备的方法应该由应用程序决定。
3
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);
};
struct file_operations
include/linux/
设备驱动程序
则编写设备驱动程序的主要工作就是编写如上子函数,并填充file_operations的各个域
一个最简单字符驱动程序,由下面7个函数和1个结构体就可组成。Open(),Release,
static int my_open(struct inode * inode, struct file * filp)
{ 设备打开时的操作…}
static int my_release(struct inode * inode, struct file * filp)
{ 设备关闭时的操作…}
static int my_write(struct file *file, const char * buffer, size_t count,
loff_t * ppos)
{ 设备写入时的操作…}
static int my_read(struct file *file, const char * buffer, size_t count,
loff_t * ppos)
{ 设备读取时的操作…}
()Write(),Read()Ioctl()Init(),Exit()Struct file_operation
static int __init my_init(void)
{初始化硬件,注册设备,创建设备节点…}
static void __exit my_exit(void)
{删除设备节点,注销设备…}
{ 设备的控制操作……}
Static int my_i