? ? ? ? 本文主要探讨210的input子系统。
input子系统
????????input子系统包含:设备驱动层,输入核心层,事件驱动层
????????事件处理层:接收核心层上报事件选择对应struct input_handler处理,每个input_handler对象处理一类事件,同类事件的设备驱动共用同一handler
????????核心层:连接设备驱动层和事件处理层,为设备驱动层提供输入设备驱动接口(struct input_dev)以及输入设备驱动注册函数(input_register_device),为事件处理层提供输入事件驱动接口,通知事件处理层对事件处理
????????设备驱动层:获取硬件设备信息,并转换为核心层定义的规范事件后提交给核心层,每个设备均被描述为struct input_dev对象
结构体
????????input_dev结构体是硬件驱动层代表input设备结构体
// include/linux/input.h?
struct input_dev {
? ? const char *name; ? ? ? ? ? ?/* 设备名称 */
? ? const char *phys; ? ? ? ? ? ?/* 设备在系统中的路径 */
? ? const char *uniq; ? ? ? ? ? ?/* 设备唯一id */
? ? struct input_id id; ? ? ? ? ?/* input设备id号 */
? ? unsigned long propbit[BITS_TO_LONGS(INPUT_PROP_CNT)];?
? ? unsigned long evbit[BITS_TO_LONGS(EV_CNT)]; ? ? ? ?/* 设备支持的事件类型,主要有EV_SYNC,EV_KEY,EV_KEY,EV_REL,EV_ABS等*/ ? ?
? ? unsigned long keybit[BITS_TO_LONGS(KEY_CNT)]; ? ? ? ?/* 按键所对应的位图 */
? ? unsigned long relbit[BITS_TO_LONGS(REL_CNT)]; ? ? ? ?/* 相对坐标对应位图 */
? ? unsigned long absbit[BITS_TO_LONGS(ABS_CNT)]; ? ? ? ?/* 决定左边对应位图 */
? ? unsigned long mscbit[BITS_TO_LONGS(MSC_CNT)]; ? ? ? ?/* 支持其他事件 */
? ? unsigned long ledbit[BITS_TO_LONGS(LED_CNT)]; ? ? ? ?/* 支持led事件 */
? ? unsigned long sndbit[BITS_TO_LONGS(SND_CNT)]; ? ? ? ?/* 支持声音事件 */
? ? unsigned long ffbit[BITS_TO_LONGS(FF_CNT)]; ? ? ? ? ? ?/* 支持受力事件 */
? ? unsigned long swbit[BITS_TO_LONGS(SW_CNT)]; ? ? ? ? ? ?/* 支持开关事件 */
? ? unsigned int hint_events_per_packet; ? ? ? ? ? ?/* ?平均事件数*/
? ? unsigned int keycodemax; ? ? ? ? ? ?/* 支持最大按键数 */
? ? unsigned int keycodesize; ? ? ? ? ? ?/* 每个键值字节数 */
? ? void *keycode; ? ? ? ? ? ?/* 存储按键值的数组的首地址 */
? ? int (*setkeycode)(struct input_dev *dev,
? ? ? ? ? ? ? const struct input_keymap_entry *ke,
? ? ? ? ? ? ? unsigned int *old_keycode);
? ? int (*getkeycode)(struct input_dev *dev,
? ? ? ? ? ? ? struct input_keymap_entry *ke);
? ? struct ff_device *ff; ? ? ? ?/* 设备关联的反馈结构,如果设备支持 */
? ? unsigned int repeat_key; ? ?/* 最近一次按键值,用于连击 */
? ? struct timer_list timer; ? ?/* 自动连击计时器 */
? ? int rep[REP_CNT]; ? ? ? ? ? ? /* 自动连击参数 */
? ? struct input_mt *mt; ? ? ? ?/* 多点触控区域 */
? ? struct input_absinfo *absinfo; ? ? ? ?/* 存放绝对值坐标的相关参数数组 */
? ? unsigned long key[BITS_TO_LONGS(KEY_CNT)]; ? ?/* 反应设备当前的按键状态 */
? ? unsigned long led[BITS_TO_LONGS(LED_CNT)]; ? ?/* 反应设备当前的led状态 */
? ? unsigned long snd[BITS_TO_LONGS(SND_CNT)]; ? ?/* 反应设备当前的声音状态 */
? ? unsigned long sw[BITS_TO_LONGS(SW_CNT)]; ? ?/* 反应设备当前的开关状态 */
? ? int (*open)(struct input_dev *dev); ? ? ? ?/* 第一次打开设备时调用,初始化设备用 */
? ? void (*close)(struct input_dev *dev); ? ? ? ?/* 最后一个应用程序释放设备事件,关闭设备 */
? ? int (*flush)(struct input_dev *dev, struct file *file); ? ? ? ?/* 用于处理传递设备的事件 */
? ? int (*event)(struct input_dev *dev, unsigned int type, unsigned int code, int value); ? ?/* 事件处理函数,主要是接收用户下发的命令,如点亮led */
? ? struct input_handle __rcu *grab; ? ? ? ?/* 当前占有设备的input_handle */
? ? spinlock_t event_lock; ? ? ? ?/* 事件锁 */
? ? struct mutex mutex; ? ? ? ? ? ?/* 互斥体 */
? ? unsigned int users; ? ? ? ?/* 打开该设备的用户数量(input_handle) */
? ? bool going_away; ? ? ? ? ? ?/* 标记正在销毁的设备 */
? ? struct device dev; ? ? ? ?/* 设备 */
? ? struct list_head ? ?h_list; ? ? ? ?/* 设备所支持的input handle */
? ? struct list_head ? ?node; ? ? ? ?/* 用于将此input_dev连接到input_dev_list */
? ? unsigned int num_vals; ? ? ? ?/* 当前帧中排队的值数 */
? ? unsigned int max_vals; ? ? ? ?/* ?队列最大的帧数*/
? ? struct input_value *vals; ? ?/* ?当前帧中排队的数组*/
? ? bool devres_managed; ? ? ? ?/* 表示设备被devres 框架管理,不需要明确取消和释放*/
};
????????input_handler结构体是事件层代表事件处理器
// include/linux/input.h?
struct input_handler {
? ? void *private; ? ? ? ?/* 存放handle数据 */
? ? void (*event)(struct input_handle *handle, unsigned int type, unsigned int code, int value);
? ? void (*events)(struct input_handle *handle,
? ? ? ? ? ? ? ?const struct input_value *vals, unsigned int count);
? ? bool (*filter)(struct input_handle *handle, unsigned int type, unsigned int code, int value);
? ? bool (*match)(struct input_handler *handler, struct input_dev *dev);
? ? int (*connect)(struct input_handler *handler, struct input_dev *dev, const struct input_device_id *id);
? ? void (*disconnect)(struct input_handle *handle);
? ? void (*start)(struct input_handle *handle);
? ? const struct file_operations *fops;
? ? bool legacy_minors;
? ? int minor;
? ? const char *name; ? ? ? ?/* 名字 */
? ? const struct input_device_id *id_table; ? ? ? ?/* input_dev匹配用的id */
? ? struct list_head ? ?h_list; ? ?/* 用于链接和handler相关的handle,input_dev与input_handler配对之后就会生成一个input_handle结构 */
? ? struct list_head ? ?node; ? ?/* 用于将该handler链入input_handler_list,链接所有注册到内核的所有注册到内核的事件处理器 */
};
????????input_handle结构体属于核心层代表配对input设备(input_dev)与input事件处理器(input_handler)
// include/linux/input.h?
struct input_handle {
? ? void *private; ? ? ? ? ? ? ? ?/* 数据指针 */
? ? int open; ? ? ? ? ? ? ? ? ? ?/* 打开标志,每个input_handle 打开后才能操作 */
? ? const char *name; ? ? ? ? ? ?/* 设备名称 */
? ? struct input_dev *dev; ? ? ? ? ? ?/* 指向所属的input_dev */
? ? struct input_handler *handler; ? ?/* 指向所属的input_handler */
? ? struct list_head ? ?d_node; ? ? ? ?/* 用于链入所指向的input_dev的handle链表 */
? ? struct list_head ? ?h_node; ? ? ? ?/* 用于链入所指向的input_handler的handle链表 */
};
????????Evdev字符设备事件结构体
/* drivers/input/evdev.c */
struct evdev {
? ? ? ? int open; ? ?/* 设备被打开的计数 */
? ? ? ? struct input_handle handle; ?/* 关联的input_handle */?
? ? ? ? wait_queue_head_t wait; ?/* 等待队列,当前进程读取设备,没有事件产生时,
进程就会sleep */
? ? ? ? struct evdev_client __rcu *grab; ?/* event响应 */
struct list_head client_list; ?/* evdev_client链表,说明evdev设备可以处理多个 evdev _client,可以有多个进程访问evdev设备 */
? ? ? ? spinlock_t client_lock;
? ? ? ? struct mutex mutex;
? ? ? ? struct device dev;
? ? ? ? struct cdev cdev;
? ? ? ? bool exist; ? /* 设备存在判断 */
};
????????evdev_client字符设备事件响应结构体
struct evdev_client {
? ? unsigned int head; ? ? ? ? ? ? ? ? /* 动态索引,每加入一个event到buffer中,head++ */
? ? unsigned int tail; ? ? ? ? ? ? ? ?/* 动态索引,每取出一个buffer中到event,tail++ */
? ? unsigned int packet_head; ? ? ? ? /* 数据包头部 */
? ? spinlock_t buffer_lock;?
? ? struct fasync_struct *fasync; ? ? /* 异步通知函数 */
? ? struct evdev *evdev;
? ? struct list_head node; ? ? ? ? ? ?/* evdev_client链表项 */
? ? int clkid;
? ? bool revoked;
? ? unsigned int bufsize;
? ? struct input_event buffer[]; ? ? ? ?/* 用来存放input_dev事件缓冲区 */
};
????????evdev_handler事件处理函数结构体
/* drivers/input/input.c */
static struct input_handler evdev_handler = {
? ? ? ? .event ? ? ? ?= evdev_event, ? /* 事件处理函数, */ ?
? ? ? ? .events ? ?= evdev_events, ?/* 事件处理函数, */
? ? ? ? .connect ? ?= evdev_connect, /* 连接函数,将事件处理和输入设备联系起来 */
? ? ? ? .disconnect ? ?= evdev_disconnect, ?/* 断开该链接 */
? ? ? ? .legacy_minors ? ?= true,
? ? ? ? .minor ? ? ? ?= EVDEV_MINOR_BASE,
? ? ? ? .name ? ? ? ?= "evdev", /* handler名称 */
? ? ? ? .id_table ? ?= evdev_ids, /* 断开该链接 */
};
????????input_event结构体(按键编码信息)
/* drivers/input/evdev.c */
struct input_event { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
? ? struct timeval time; ? /* 事件发生的时间 ?*/ ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
? ? __u16 type; ? ? ? ? ? ? /* 事件类型 */ ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
? ? __u16 code; ? ? ? ? ? ? /* 事件码 */ ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
? ? __s32 value; ? ? ? ? ? ?/* 事件值 */ ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
};
?
inputcore
subsys_initcall(input_init);
module_exit(input_exit);
static int __init input_init(void)
{
?? ?int err;
?? ?input_init_abs_bypass();
?? ?err = class_register(&input_class);
?? ?if (err) {
?? ??? ?printk(KERN_ERR "input: unable to register input_dev class\n");
?? ??? ?return err;
?? ?}
?? ?err = input_proc_init();
?? ?if (err)
?? ??? ?goto fail1;
?? ?err = register_chrdev(INPUT_MAJOR, "input", &input_fops);
?? ?if (err) {
?? ??? ?printk(KERN_ERR "input: unable to register char major %d", INPUT_MAJOR);
?? ??? ?goto fail2;
?? ?}
?? ?return 0;
?fail2:?? ?input_proc_exit();
?fail1:?? ?class_unregister(&input_class);
?? ?return err;
}
struct class input_class = {
?? ?.name?? ??? ?= "input",
?? ?.devnode?? ?= input_devnode,
};
#define INPUT_MAJOR?? ??? ?13
static const struct file_operations input_fops = {
?? ?.owner = THIS_MODULE,
?? ?.open = input_open_file,
};
????????class_register注册输入设备类/sys/class/input
????????input_proc_init注册proce文件/proc/bus/input
????????register_chrdevs注册设备注册,主设备号为13
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];
?? ?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) {
?? ??? ?fops_put(new_fops);
?? ??? ?err = -ENODEV;
?? ??? ?goto out;
?? ?}
?? ?old_fops = file->f_op;
?? ?file->f_op = new_fops;
?? ?err = new_fops->open(inode, file);
?? ?if (err) {
?? ??? ?fops_put(file->f_op);
?? ??? ?file->f_op = fops_get(old_fops);
?? ?}
?? ?fops_put(old_fops);
out:
?? ?return err;
}
static const struct file_operations input_fops = {
?? ?.owner = THIS_MODULE,
?? ?.open = input_open_file,
};
static struct input_handler *input_table[8];
handler = input_table[iminor(inode) >> 5];
????????获取子设备号并除以32的值为input驱动数组号,来获取对应input_handler中的fops
????????input_table为input_register_handler函数注册时填充
?事件处理层
int input_register_handler(struct input_handler *handler)
{
?? ?struct input_dev *dev;
?? ?int retval;
?? ?retval = mutex_lock_interruptible(&input_mutex);
?? ?if (retval)
?? ??? ?return retval;
?? ?INIT_LIST_HEAD(&handler->h_list);
?? ?if (handler->fops != NULL) {
?? ??? ?if (input_table[handler->minor >> 5]) {
?? ??? ??? ?retval = -EBUSY;
?? ??? ??? ?goto out;
?? ??? ?}
?? ??? ?input_table[handler->minor >> 5] = handler;
?? ?}
?? ?list_add_tail(&handler->node, &input_handler_list);
?? ?list_for_each_entry(dev, &input_dev_list, node)
?? ??? ?input_attach_handler(dev, handler);
?? ?input_wakeup_procfs_readers();
?out:
?? ?mutex_unlock(&input_mutex);
?? ?return retval;
}
static int __init evdev_init(void)
{
?? ?return input_register_handler(&evdev_handler);
}
????????依据evdev.c分析input_register_hndler
static struct input_handler evdev_handler = {
?? ?.event?? ??? ?= evdev_event,
?? ?.connect?? ?= evdev_connect,
?? ?.disconnect?? ?= evdev_disconnect,
?? ?.fops?? ??? ?= &evdev_fops,
?? ?.minor?? ??? ?= EVDEV_MINOR_BASE,
?? ?.name?? ??? ?= "evdev",
?? ?.id_table?? ?= evdev_ids,
};
static const struct file_operations evdev_fops = {
?? ?.owner?? ??? ?= THIS_MODULE,
?? ?.read?? ??? ?= evdev_read,
?? ?.write?? ??? ?= evdev_write,
?? ?.poll?? ??? ?= evdev_poll,
?? ?.open?? ??? ?= evdev_open,
?? ?.release?? ?= evdev_release,
?? ?.unlocked_ioctl?? ?= evdev_ioctl,
#ifdef CONFIG_COMPAT
?? ?.compat_ioctl?? ?= evdev_ioctl_compat,
#endif
?? ?.fasync?? ??? ?= evdev_fasync,
?? ?.flush?? ??? ?= evdev_flush
}
#define EVDEV_MINOR_BASE?? ?64
static const struct input_device_id evdev_ids[] = {
?? ?{ .driver_info = 1 },?? ?/* Matches all devices */
?? ?{ },?? ??? ??? ?/* Terminating zero entry */
};
static void evdev_event(struct input_handle *handle,
?? ??? ??? ?unsigned int type, unsigned int code, int value)
{
?? ?struct evdev *evdev = handle->private;
?? ?struct evdev_client *client;
?? ?struct input_event event;
?? ?struct timespec ts;
?? ?ktime_get_ts(&ts);
?? ?event.time.tv_sec = ts.tv_sec;
?? ?event.time.tv_usec = ts.tv_nsec / NSEC_PER_USEC;
?? ?event.type = type;
?? ?event.code = code;
?? ?event.value = value;
?? ?rcu_read_lock();
?? ?client = rcu_dereference(evdev->grab);
?? ?if (client)
?? ??? ?evdev_pass_event(client, &event);
?? ?else
?? ??? ?list_for_each_entry_rcu(client, &evdev->client_list, node)
?? ??? ??? ?evdev_pass_event(client, &event);
?? ?rcu_read_unlock();
?? ?wake_up_interruptible(&evdev->wait);
}
static int evdev_connect(struct input_handler *handler, struct input_dev *dev,
?? ??? ??? ? const struct input_device_id *id)
{
?? ?struct evdev *evdev;
?? ?int minor;
?? ?int error;
?? ?for (minor = 0; minor < EVDEV_MINORS; minor++)
?? ??? ?if (!evdev_table[minor])
?? ??? ??? ?break;
?? ?if (minor == EVDEV_MINORS) {
?? ??? ?printk(KERN_ERR "evdev: no more free evdev devices\n");
?? ??? ?return -ENFILE;
?? ?}
?? ?evdev = kzalloc(sizeof(struct evdev), GFP_KERNEL);
?? ?if (!evdev)
?? ??? ?return -ENOMEM;
?? ?INIT_LIST_HEAD(&evdev->client_list);
?? ?spin_lock_init(&evdev->client_lock);
?? ?mutex_init(&evdev->mutex);
?? ?init_waitqueue_head(&evdev->wait);
?? ?dev_set_name(&evdev->dev, "event%d", minor);
?? ?evdev->exist = 1;
?? ?evdev->minor = minor;
?? ?evdev->handle.dev = input_get_device(dev);
?? ?evdev->handle.name = dev_name(&evdev->dev);
?? ?evdev->handle.handler = handler;
?? ?evdev->handle.private = evdev;
?? ?evdev->dev.devt = MKDEV(INPUT_MAJOR, EVDEV_MINOR_BASE + minor);
?? ?evdev->dev.class = &input_class;
?? ?evdev->dev.parent = &dev->dev;
?? ?evdev->dev.release = evdev_free;
?? ?device_initialize(&evdev->dev);
?? ?error = input_register_handle(&evdev->handle);
?? ?if (error)
?? ??? ?goto err_free_evdev;
?? ?error = evdev_install_chrdev(evdev);
?? ?if (error)
?? ??? ?goto err_unregister_handle;
?? ?error = device_add(&evdev->dev);
?? ?if (error)
?? ??? ?goto err_cleanup_evdev;
?? ?return 0;
?err_cleanup_evdev:
?? ?evdev_cleanup(evdev);
?err_unregister_handle:
?? ?input_unregister_handle(&evdev->handle);
?err_free_evdev:
?? ?put_device(&evdev->dev);
?? ?return error;
}
int input_register_handle(struct input_handle *handle)
{
?? ?struct input_handler *handler = handle->handler;
?? ?struct input_dev *dev = handle->dev;
?? ?int error;
?? ?/*
?? ? * We take dev->mutex here to prevent race with
?? ? * input_release_device().
?? ? */
?? ?error = mutex_lock_interruptible(&dev->mutex);
?? ?if (error)
?? ??? ?return error;
?? ?/*
?? ? * Filters go to the head of the list, normal handlers
?? ? * to the tail.
?? ? */
?? ?if (handler->filter)
?? ??? ?list_add_rcu(&handle->d_node, &dev->h_list);
?? ?else
?? ??? ?list_add_tail_rcu(&handle->d_node, &dev->h_list);
?? ?mutex_unlock(&dev->mutex);
?? ?/*
?? ? * Since we are supposed to be called from ->connect()
?? ? * which is mutually exclusive with ->disconnect()
?? ? * we can't be racing with input_unregister_handle()
?? ? * and so separate lock is not needed here.
?? ? */
?? ?list_add_tail_rcu(&handle->h_node, &handler->h_list);
?? ?if (handler->start)
?? ??? ?handler->start(handle);
?? ?return 0;
}
????????input_register_handler为事件注册函数,struct input_handler为事件处理结构体
????????evdev_init调用input_register_handler和struct input_handler evdev_handler注册evdev设备事件
????????evdev_fops为edev类型设备操作函数,EVDEV_MINOR_BASE为64存在input_table[2]中
????????id_table为handler支持的输入设备(input_dev->id==input_handler->id_table,调用connect)?
设备驱动层
int input_register_device(struct input_dev *dev)
{
?? ?static atomic_t input_no = ATOMIC_INIT(0);
?? ?struct input_handler *handler;
?? ?const char *path;
?? ?int error;
?? ?/* Every input device generates EV_SYN/SYN_REPORT events. */
?? ?__set_bit(EV_SYN, dev->evbit);
?? ?/* KEY_RESERVED is not supposed to be transmitted to userspace. */
?? ?__clear_bit(KEY_RESERVED, dev->keybit);
?? ?/* Make sure that bitmasks not mentioned in dev->evbit are clean. */
?? ?input_cleanse_bitmasks(dev);
?? ?/*
?? ? * If delay and period are pre-set by the driver, then autorepeating
?? ? * is handled by the driver itself and we don't do it in input.c.
?? ? */
?? ?init_timer(&dev->timer);
?? ?if (!dev->rep[REP_DELAY] && !dev->rep[REP_PERIOD]) {
?? ??? ?dev->timer.data = (long) dev;
?? ??? ?dev->timer.function = input_repeat_key;
?? ??? ?dev->rep[REP_DELAY] = 250;
?? ??? ?dev->rep[REP_PERIOD] = 33;
?? ?}
?? ?if (!dev->getkeycode)
?? ??? ?dev->getkeycode = input_default_getkeycode;
?? ?if (!dev->setkeycode)
?? ??? ?dev->setkeycode = input_default_setkeycode;
?? ?dev_set_name(&dev->dev, "input%ld",
?? ??? ? ? ? (unsigned long) atomic_inc_return(&input_no) - 1);
?? ?error = device_add(&dev->dev);
?? ?if (error)
?? ??? ?return error;
?? ?path = kobject_get_path(&dev->dev.kobj, GFP_KERNEL);
?? ?printk(KERN_INFO "input: %s as %s\n",
?? ??? ?dev->name ? dev->name : "Unspecified device", path ? path : "N/A");
?? ?kfree(path);
?? ?error = mutex_lock_interruptible(&input_mutex);
?? ?if (error) {
?? ??? ?device_del(&dev->dev);
?? ??? ?return error;
?? ?}
?? ?list_add_tail(&dev->node, &input_dev_list);
?? ?list_for_each_entry(handler, &input_handler_list, node)
?? ??? ?input_attach_handler(dev, handler);
?? ?input_wakeup_procfs_readers();
?? ?mutex_unlock(&input_mutex);
?? ?return 0;
}
static int input_attach_handler(struct input_dev *dev, struct input_handler *handler)
{
?? ?const struct input_device_id *id;
?? ?int error;
?? ?id = input_match_device(handler, dev);
?? ?if (!id)
?? ??? ?return -ENODEV;
?? ?error = handler->connect(handler, dev, id);
?? ?if (error && error != -ENODEV)
?? ??? ?printk(KERN_ERR
?? ??? ??? ?"input: failed to attach handler %s to device %s, "
?? ??? ??? ?"error: %d\n",
?? ??? ??? ?handler->name, kobject_name(&dev->dev.kobj), error);
?? ?return error;
}
list_add_tail(&dev->node, &input_dev_list);
list_for_each_entry(handler, &input_handler_list, node)input_attach_handler(dev, handler);
????????将要注册input_dev驱动设备放在input_dev_list链表中,调用input_attach_handler函数,将每个input_handle的id_table进连接接
????????注册input_handler类同设备注册通过inputhandle将input_handler和input_dev连接(id匹配)
int input_register_handler(struct input_handler *handler)
{
? ? ......
?
? ? list_add_tail(&handler->node, &input_handler_list);
?
? ? list_for_each_entry(dev, &input_dev_list, node)
? ? ? ? input_attach_handler(dev, handler);
?
? ? ......
}
核心层提供给设备驱动层的接口函数
struct input_dev *input_allocate_device(void);
分配input_dev内存
void input_set_capability(struct input_dev *dev, unsigned int type, unsigned int code);
设置输入设备上报事件,input_set_capability(dev, EV_KEY, KEY_E);
dev:input_dev结构体,type上报事件类型,code上报具体事件
int input_register_device(struct input_dev *dev);
input核心层注册设备
demo:
? ? ? ? 210内核自带轮循方式检测按键,自写中断方式
button-x210.c
#include <linux/input.h>
#include <linux/module.h>
#include <linux/init.h>
#include <asm/irq.h>
#include <asm/io.h>
#include <mach/irqs.h>
#include <linux/interrupt.h>
#include <linux/gpio.h>
#define BUTTON_IRQ_LEFT IRQ_EINT2
static struct input_dev *button_dev;
static irqreturn_t button_interrupt(int irq, void *dummy)
{
int flag;
//中断产生,设置gpio为输入模式下获取gpio号
s3c_gpio_cfgpin(S5PV210_GPH0(2), S3C_GPIO_SFN(0x0));
flag = gpio_get_value(S5PV210_GPH0(2));
//重新置为中断模式
s3c_gpio_cfgpin(S5PV210_GPH0(2), S3C_GPIO_SFN(0x0f));
//上报按键中断事件
input_report_key(button_dev, KEY_LEFT, !flag);
//同步按键中断
input_sync(button_dev);
return IRQ_HANDLED;
}
static int __init button_init(void)
{
int error;
//申请gpio
error = gpio_request(S5PV210_GPH0(2), "GPH0_2");
if(error)
{
printk("button-x210.c: request gpio GPH0(2) fail");
return -EBUSY;
}
//配置gpio为中断模式
s3c_gpio_cfgpin(S5PV210_GPH0(2), S3C_GPIO_SFN(0x0f));
//申请中断
if (request_irq(BUTTON_IRQ_LEFT, button_interrupt, 0, "button", NULL))
{
printk(KERN_ERR "button-x210.c: Can't allocate irq %d\n", BUTTON_IRQ_LEFT);
goto err_free_gpio;
return -EBUSY;
}
//申请设备内存
button_dev = input_allocate_device();
if (!button_dev)
{
printk(KERN_ERR "button-x210.c: Not enough memory\n");
error = -ENOMEM;
goto err_free_irq;
}
//设备初始化
button_dev->name = "x210";
//设置设备上报事件
input_set_capability(button_dev, EV_KEY, KEY_LEFT);
//设备注册
error = input_register_device(button_dev);
if (error)
{
printk(KERN_ERR "button-x210.c: Failed to register device\n");
goto err_free_dev;
}
return 0;
err_free_dev:
input_free_device(button_dev);
err_free_irq:
free_irq(BUTTON_IRQ_LEFT, button_interrupt);
err_free_gpio:
gpio_free(S5PV210_GPH0(2));
return error;
}
static void __exit button_exit(void)
{
input_unregister_device(button_dev);
input_free_device(button_dev);
free_irq(BUTTON_IRQ_LEFT, NULL);
gpio_free(S5PV210_GPH0(2));
}
module_init(button_init);
module_exit(button_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("cxb");
MODULE_DESCRIPTION("button module");
MODULE_ALIAS("button");
button.c?
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <linux/input.h>
#include <string.h>
#define X210_KEY "/dev/input/event2"
int main(void)
{
int fd = -1, ret = -1;
struct input_event ev;
fd = open(X210_KEY, O_RDONLY);
if (fd < 0)
{
perror("open");
return -1;
}
while (1)
{
memset(&ev, 0, sizeof(struct input_event));
ret = read(fd, &ev, sizeof(struct input_event));
if (ret != sizeof(struct input_event))
{
perror("read");
close(fd);
return -1;
}
printf("-------------------------\n");
printf("type: %hd\n", ev.type);
printf("code: %hd\n", ev.code);
printf("value: %d\n", ev.value);
printf("\n");
}
close(fd);
return 0;
}
操作流程:?
cd /root/kernel/arch/arm/mach-s5pv210
rm button-x210.o
vim Makefile
#obj-$(CONFIG_MACH_SMDKV210) += button-x210.o
make -j8
?