字符设备注册方法一:
1. 确定主设备号major
2. 构造file_operations
.open
.read
.write
3. 注册字符设备 register_chrdev()
4. 入口函数
5. 出口函数
字符设备注册方法二:
1、确定主设备号major
2、构造file_operations
.open
.read
.warite
3、注册字符设备 register_chrdev_region();
cdev_init();
dev_add();
4、出口函数
5、入口函数
在分析内核input的子系统的时候,我们发现采用的是方法二,
APP:
read -> …->file->f_op ->read.
如下分析的是从app的read开始调用流程是
seq_file.c:
app read ->seq_read->seq_putc (通过copy_to_user()函数实现user与内核通信)
input.c:
file_operations input_handlers_fileops(user与内核的调用接口) > input_proc_devices_open > seq_operations input_devices_seq_ops > input_devices_seq_show() > seq_putc() >input_proc_init >
seq_file.c
ssize_t seq_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
{
struct seq_file *m = file->private_data;
mutex_lock(&m->lock);
/* if not empty - flush it first */
if (m->count) {
n = min(m->count, size);
err = copy_to_user(buf, m->buf + m->from, n);
m->count -= n;
m->from += n;
size -= n;
buf += n;
copied += n;
if (!m->count)
m->index++;
if (!size)
goto Done;
}
}
int seq_putc(struct seq_file *m, char c)
{
if (m->count < m->size) {
m->buf[m->count++] = c;
return 0;
}
return -1;
}
input.c
static<