字符设备驱动程序总结.pdf
文本预览下载声明
字符设备驱动程序总结
该总结以基于JZ2440 开发板的按键驱动为例。
By yougwypf1991 2016/2/22
1 步骤
1.1 阅读参考资料
参考资料包括原理图、需要操作的器件的芯片手册。这些资料包括:JZ2440
的原理图、S3C2440 的用户手册。
首先看原理图是为了确定待控制的按键是如何连接的。如下图
图 1 按键原理图
由此可知,按键S2 与EINT0 连接即GPF0 ,按键S3 与EINT2 即GPF2,按键
S4 与EINT11 连接即GPG3 ,按键S5 与EINT19 连接即GPG11 。同时可以了解到,
如果按键在松开的状态下各个引脚的状态应该是高电平,在按下的状态下,引脚
的状态为低电平。
接着,查看S3C2440 的用户手册,确定这些引脚的控制方法。如图
注:(1)本文是基于linux-2.6.22.6 内核编写;
(2 )本文是基于JZ2440 编写(引脚连接不同,其他部分均与其他型号的开发板相同);
(2 )推荐使用sourceinsight 工具查看代码。
图 2 引脚控制方法
1.2 构建驱动程序框架
1.2.1 文件控制结构体
首先需要了解的是file_operation s 这个机构体,结构体的定义如下:
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 *);
int (*ioctl) (struct inode *, struct file *, unsigned int, unsigned long);
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 *, struct dentry *, int datasync);
int (*aio_fsync) (struct kiocb *, int datasync);
int (*fasync) (int, struct file *, int);
int (*lock) (struct file *, int, struct file_lock *);
显示全部