1.file_operations 结构体
2. file 结构体
3. inode结构体
4.open方法:
a.检查设备特定的错误
b.如果设备是首次打开,对其进行初始化
c.如有需求,跟新f_ops指针
d.分配并填写置于filp->private-data里的数据结构
release方法
a.释放由open分配的,保存在flip->private_data里的数据结构
b.在最后一次关闭操作时关闭设备。
read&write
ssize_t read(struct file * filp , char __user *buff ,size_t count , loff_t *offp);
ssize_t write(struct file * filp , const char __user *buff ,size_t count , loff_t *offp);
read和write函数要做的就是在用户地址空间和内核地址空间之间进行整段数据的copy,这种能力是由下面的内核函数提供:
unsigned long copy_to_user(void __user *to, const void *from, unsigned long n)
unsigned long copy_from_user(void __user *to, const void *from, unsigned long n)
在打印一条可能重复的log 前
int printk_ratelimit(void)
example:
if(printk_ratelimit())
printk(KERNEL_INFO “xxxxxxx\n”);
printk_ratelimit通过跟踪发送到控制台的消息数量工作。如果输出的速度超过一个阈值,printk_ratelimit将返回零从而避免发送重复消息。
.text
这个代码包含了模块的可执行代码
.bss
任何编译时未初始化的变量保存在此段
.data
其他经过初始化的变量保存在 .data段