2.3 handlers文件
static const struct file_operations input_handlers_fileops = {
.owner = THIS_MODULE,
.open = input_proc_handlers_open,
.read = seq_read,
.llseek = seq_lseek,
.release = seq_release,
};
2.3.1
这里当我们去cat /proc/bus/input/handlers时候,会调用input_proc_handlers_open函数,接着调用seq_open(file, &input_handlers_seq_ops),捆绑了input_handlers_seq_ops操作函数集,
其seq_operations函数集中声明了.show方法为input_handlers_seq_show,该方法打印了些信息到seq文件,接着cat命令会调用read方法,read方法会调用.show方法,
接着把打印到文件的信息复制用户空间的缓冲区中.这里主要看看.show方法吧
static int input_handlers_seq_show(struct seq_file *seq, void *v)
{
struct input_handler *handler = container_of(v, struct input_handler, node); //获得输入处理器
union input_seq_state *state = (union input_seq_state *)&seq->private;
seq_printf(seq, "N: Number=%u Name=%s", state->pos, handler->name); //打印N:Number=序号 name=设备名
if (handler->filter)
seq_puts(seq, " (filter)");
if (handler->fops)
seq_printf(seq, " Minor=%d", handler->minor); //打印Minor=次设备号
seq_putc(seq, '\n');
return 0;
}
我的打印是
cat handlers
N: Number=0 Name=rfkill
N: Number=1 Name=kbd
N: Number=2 Name=mousedev Minor=32
N: Number=3 Name=evdev Minor=64
3. 字符设备接口
3.1 主设备号
#define INPUT_MAJOR 13
3.2 input_fops
static const struct file_operations input_fops = {
.owner = THIS_MODULE,
.open = input_open_file, //打开方法
.llseek = noop_llseek,
};
3.2.1 input_open_file
static int input_open_file(struct inode *inode, struct file *file)
{
struct input_handler *handler;
const struct file_operations *old_fops, *new_fops = NULL;
int err;
err = mutex_lock_interruptible(&input_mutex);
if (err)
return err;
/* No load-on-demand here? */
handler = input_table[iminor(inode) >> 5]; //根据节点算出次设备号,并在全局input_table找到输入处理器
if (handler)
new_fops = fops_get(handler->fops); //获取输入操作函数集指针
mutex_unlock(&input_mutex);
/*
* That's _really_ odd. Usually NULL ->open means "nothing special",
* not "no device". Oh, well...
*/
if (!new_fops || !new_fops->open) { //判断输入操作函数集的存在且存在open方法
fops_put(new_fops);
err = -ENODEV;
goto out;
}
old_fops = file->f_op; //获取文件的操作函数集指针
file->f_op = new_fops; //替换为输入操作函数集指针
err = new_fops->open(inode, file); //调用输入操作函数集的open方法
if (err) {
fops_put(file->f_op);
file->f_op = fops_get(old_fops);
}
fops_put(old_fops);
out:
return err;
}
