以gpio-key.c开始分析
drivers/input/keyboard/gpio_keys.c
这里说一下环境:
rv1126 linux4.19
如果手里没有源码,可以看这里 Linux source code (v4.19.111) - Bootlin
gpio_keys_probe()
static int gpio_keys_probe(struct platform_device *pdev)
{
const struct gpio_keys_platform_data *pdata = dev_get_platdata(dev);
...
if (!pdata) {
pdata = gpio_keys_get_devtree_pdata(dev);
if (IS_ERR(pdata))
return PTR_ERR(pdata);
}
...
}
gpio_keys_probe() -> gpio_keys_ger_devtree_pdata()
结合dts分析
user_buttons {
compatible = "gpio-keys";
pinctrl-names = "default";
pinctrl-0 = <&user_buttons_pins>;
button@0 {
label = "home";
linux,code = <KEY_HOME>;
gpios = <&gpio3 7 GPIO_ACTIVE_HIGH>;
wakeup-source;
};
button@1 {
label = "menu";
linux,code = <KEY_MENU>;
gpios = <&gpio3 8 GPIO_ACTIVE_HIGH>;
wakeup-source;
};
};
static struct gpio_keys_platform_data *
gpio_keys_get_devtree_pdata(struct device *dev)
{
struct gpio_keys_platform_data *pdata;
struct gpio_keys_button *button;
struct fwnode_handle *child;
int nbuttons;
/*
* 计算child_node的数量
* 每个child node对应一个button
*/
nbuttons = device_get_child_node_count(dev);
if (nbuttons == 0)
return ERR_PTR(-ENODEV);
/*
* 申请空间用于存储button信息
* 注意申请的大小是pdata+button*nbuttons
*/
pdata = devm_kzalloc(dev,
sizeof(*pdata) + nbuttons * sizeof(*button),
GFP_KERNEL);
if (!pdata)
return ERR_PTR(-ENOMEM);
/*
* 指向button的偏移地址
*/
button = (struct gpio_keys_button *)(pdata + 1);
/*
* 更新pdata的信息
*/
pdata->buttons = button;
pdata->nbuttons = nbuttons;
/*
* rep表示的是自动重复
* 意思就是按键按下,如果不松手的话是不是自动重复上报键值
* 为什么放在这里呢?这里对应的是所以的button
* 如果单个button需要自动上报如何处理???
*/
pdata->rep = device_property_read_bool(dev, "autorepeat");
/*
* 全局的label
*/
device_property_read_string(dev, "label", &pdata->name);
/*
* 解析每一个button
*/
device_for_each_child_node(dev, child) {
if (is_of_node(child))
button->irq =
irq_of_parse_and_map(to_of_node(child), 0);
/*
* 获取键值
*/
if (fwnode_property_read_u32(child, "linux,code",
&button->code)) {
dev_err(dev, "Button without keycode\n");
fwnode_handle_put(child);
return ERR_PTR(-EINVAL);
}
/*
* 获取button的label
*/
fwnode_property_read_string(child, "label", &button->desc);
if (fwnode_property_read_u32(child, "linux,input-type",
&button->type))
button->type = EV_KEY;
/*
* 判断当前button是不是一个唤醒源
*/
button->wakeup =
fwnode_property_read_bool(child, "wakeup-source") ||
/* legacy name */
fwnode_property_read_bool(child, "gpio-key,wakeup");
fwnode_property_read_u32(child, "wakeup-event-action",
&button->wakeup_event_action);
button->can_disable =
fwnode_property_read_bool(child, "linux,can-disable");
if (fwnode_property_read_u32(child, "debounce-interval",
&button->debounce_interval))
/*
* 去抖动间隔 单位ms
*/
button->debounce_interval = 5;
button++;
}
return pdata;
}
可以看到上面主要解析了dts中button的配置,获取键值等信息
以上的信息其实和我们input的关系不大,但是要实例分析input,上面部分的代码还不能少。
回到gpio_keys_probe()
static int gpio_keys_probe(struct platform_device *pdev)
{
...
struct input_dev *input;
...
input = devm_input_allocate_device(dev);
if (!input) {
dev_err(dev, "failed to allocate input device\n");
return -ENOMEM;
}
...
}
gpio_keys_probe() -> devm_input_allocate_device()
struct input_dev *devm_input_allocate_device(struct device *dev)
{
struct input_dev *input;
...
input = input_allocate_device();
if (!input) {
devres_free(devres);
return NULL;
}
input->dev.parent = dev;
...
return input;
}
struct input_dev *input_allocate_device(void)
{
static atomic_t input_no = ATOMIC_INIT(-1);
struct input_dev *dev;
dev = kzalloc(sizeof(*dev), GFP_KERNEL);
if (dev) {
dev->dev.type = &input_dev_type;
dev->dev.class = &input_class;
device_initialize(&dev->dev);
mutex_init(&dev->mutex);
spin_lock_init(&dev->event_lock);
//注意这里有个定时器
timer_setup(&dev->timer, NULL, 0);
INIT_LIST_HEAD(&dev->h_list);
INIT_LIST_HEAD(&dev->node);
//input独一无二的编号
dev_set_name(&dev->dev, "input%lu",
(unsigned long)atomic_inc_return(&input_no));
__module_get(THIS_MODULE);
}
return dev;
}
对于input的创建并没有什么特别要关心的。
回到gpio_keys_probe()
input->name = pdata->name ? : pdev->name;
input->phys = "gpio-keys/input0";
input->dev.parent = dev;
input->open = gpio_keys_open;
input->close = gpio_keys_close;
input->id.bustype = BUS_HOST;
input->id.vendor = 0x0001;
input->id.product = 0x0001;
input->id.version = 0x0100;
input->keycode = ddata->keymap;
input->keycodesize = sizeof(ddata->keymap[0]);
input->keycodemax = pdata->nbuttons;
填充input的一些成员,对于这里的bustype,vendor,product,version这里先不用关心,后面遇到再解释
keycode主要用于键值的映射
回到gpio_keys_probe()
if (pdata->rep)
__set_bit(EV_REP, input->evbit);
对于支持自动重复的情况,这里会标记支持EV_REP事件。
这里说一下evbit,定义如下:
#define EV_SYN 0x00 #define EV_KEY 0x01 #define EV_REL 0x02 #define EV_ABS 0x03 #define EV_MSC 0x04 #define EV_SW 0x05 #define EV_LED 0x11 #define EV_SND 0x12 #define EV_REP 0x14 #define EV_FF 0x15 #define EV_PWR 0x16 #define EV_FF_STATUS 0x17 #define EV_MAX 0x1f #define EV_CNT (EV_MAX+1)unsigned long evbit[BITS_TO_LONGS(EV_CNT)];
上面的EV_SYN,EV_KEY都是input支持的事件,对于input支持某个事件,则会将evbit这个变量某个位置1,后面我们有机会看到
回到gpio_keys_probe()
下面对每一个button进行解析,因为是gpio-keys,所以还有申请中断的操作,这里不进去分析
for (i = 0; i < pdata->nbuttons; i++) {
const struct gpio_keys_button *button = &pdata->buttons[i];
if (!dev_get_platdata(dev)) {
child = device_get_next_child_node(dev, child);
if (!child) {
dev_err(dev,
"missing child device node for entry %d\n",
i);
return -EINVAL;
}
}
error = gpio_keys_setup_key(pdev, input, ddata,
button, i, child);
if (error) {
fwnode_handle_put(child);
return error;
}
if (button->wakeup)
wakeup = 1;
}
gpio_keys_setup_key中有3行非常重要的代码这里要贴出来
bdata->code = &ddata->keymap[idx];
*bdata->code = button->code;
input_set_capability(input, button->type ?: EV_KEY, *bdata->code);
因为没有设置button->type,所以这里会设置事件为EV_KEY
gpio_keys_probe() ->gpio_keys_setup_key() -> input_set_capability()
void input_set_capability(struct input_dev *dev, unsigned int type, unsigned int code)
{
switch (type) {
case EV_KEY:
__set_bit(code, dev->keybit);
break;
...
__set_bit(type, dev->evbit);
}
上面代码中出现了一个新的变量keybit,其实和evbit一样
既然evbit中支持EV_KEY事件,那么支持哪些KEY,这个是不是要记录下来?所以就出现了keybit用于记录支持的KEY
其他的事件比如EV_ABS,那么就对应一个absbit记录ABS事件中,一些ABS相关的信息
回到gpio_keys_probe()
input_register_device(input);
gpio_keys_probe() -> input_register_device()
int input_register_device(struct input_dev *dev)
{
struct input_devres *devres = NULL;
struct input_handler *handler;
unsigned int packet_size;
const char *path;
int error;
/*
* 对于ABS事件,这里会判断absinfo是否为空
* 判断的原因是ABS事件,需要有最大值和最小值这些数据
* 这些数据存储在absinfo中
* ABS事件后面会分析,这里先跳过
*/
if (test_bit(EV_ABS, dev->evbit) && !dev->absinfo) {
dev_err(&dev->dev,
"Absolute device without dev->absinfo, refusing to register\n");
return -EINVAL;
}
/*
* 使用devm_input_allocate_device申请的input设备
* devres_managed 值为 true
* 这里if满足
*/
if (dev->devres_managed) {
devres = devres_alloc(devm_input_device_unregister,
sizeof(*devres), GFP_KERNEL);
if (!devres)
return -ENOMEM;
devres->input = dev;
}
/*
* 每个input设备都会通过EV_SYN事件上报数据
* 所以默认支持EV_SYN
*/
/* 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. */
/*
* 对于不支持的事件,其事件对应的数据的信息都要被清空
* 比如不支持EV_KEY,那么keybit就会被清空
*/
input_cleanse_bitmasks(dev);
/*
* 计算每个包中events的数量
* 包是什么?
* 对应EV_KEY来说,这个值是7
* 对于ABS,REL及多点触摸这个值需要计算
* hint_events_per_packet 这个值当前驱动中没有设置,
* 为0,所以会更新
*/
packet_size = input_estimate_events_per_packet(dev);
if (dev->hint_events_per_packet < packet_size)
dev->hint_events_per_packet = packet_size;
/*
* 为什么要加2呢?
*/
dev->max_vals = dev->hint_events_per_packet + 2;
/*
* 为缓冲区申请空间
* 不知道大家有没有想过,数据上报,上报到哪里了?
* 数据上报到这个申请的缓冲区了!
*/
dev->vals = kcalloc(dev->max_vals, sizeof(*dev->vals), GFP_KERNEL);
if (!dev->vals) {
error = -ENOMEM;
goto err_devres_free;
}
/*
* 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.
*/
/*
* rep[REP_DELAY ] 和rep[REP_PERIOD] 。
* 这2个值和重复按键有关。
* 比如一个键被按下3秒开始算重复按键,
* 3秒之后每隔1秒检测一次如果还被按下就算重复按下。
* 这个3秒就是rep[REP_DELAY]
* 这个1秒就是rep[REP_PERIOD]
*/
if (!dev->rep[REP_DELAY] && !dev->rep[REP_PERIOD])
input_enable_softrepeat(dev, 250, 33);
/*
* getkeycode 和 setkeycode都是比较老的方法
* 现在程序中基本不会赋值,这里会给默认方法
*/
if (!dev->getkeycode)
dev->getkeycode = input_default_getkeycode;
if (!dev->setkeycode)
dev->setkeycode = input_default_setkeycode;
error = device_add(&dev->dev);
if (error)
goto err_free_vals;
path = kobject_get_path(&dev->dev.kobj, GFP_KERNEL);
pr_info("%s as %s\n",
dev->name ? dev->name : "Unspecified device",
path ? path : "N/A");
kfree(path);
error = mutex_lock_interruptible(&input_mutex);
if (error)
goto err_device_del;
/*
* 将input设备挂载到input_dev_list链表上
*/
list_add_tail(&dev->node, &input_dev_list);
/*
* 便利input_handler_list链表,取出每一个handler
* 通过input_attach_handler进行匹配
* 由于现在还没有分析handler,所以暂时不分析
*/
list_for_each_entry(handler, &input_handler_list, node)
input_attach_handler(dev, handler);
input_wakeup_procfs_readers();
mutex_unlock(&input_mutex);
if (dev->devres_managed) {
dev_dbg(dev->dev.parent, "%s: registering %s with devres.\n",
__func__, dev_name(&dev->dev));
devres_add(dev->dev.parent, devres);
}
return 0;
err_device_del:
device_del(&dev->dev);
err_free_vals:
kfree(dev->vals);
dev->vals = NULL;
err_devres_free:
devres_free(devres);
return error;
}
以上就是一个input设备的注册过程
本文深入分析了Linux内核中gpio_keys驱动的工作原理,从gpio_keys_probe函数开始,详细阐述了如何从设备树中解析GPIO按键配置,包括获取键值、设置自动重复等。接着介绍了input设备的注册过程,涉及input_allocate_device、input_register_device等函数,讲解了输入设备的初始化和事件支持。整个过程中,重点关注了设备树配置、按键信息解析以及input设备的关键属性设置。


被折叠的 条评论
为什么被折叠?



