gpio-keys-polled 驱动裁剪注意事项
内核模块的 “gpio-keys-polled”,驱动位于drivers/input/keyboard/ 路径下,
路由系统的 “gpio-keys-polled”,驱动位于 openWrt源码的 package/kernel/gpio-button-hotplug/路径下,
由此得出 内核模块与路由系统的"gpio-keys-polled"是互斥,因此我们选择路由系统的gpio-keys-polled驱动,
保证路由系统功能完整性。
gpio-keys-polled 事件输出关系
-
路由系统驱动代码走读
-
此驱动包含 “gpio-keys” 和 ‘gpio-keys-polled’ 驱动注册
//
static int __init gpio_button_init(void)
{
int ret;
ret = platform_driver_register(&gpio_keys_driver); // 注册 "gpio-keys" 模块驱动
if (ret)
return ret;
ret = platform_driver_register(&gpio_keys_polled_driver); // 注册 ‘gpio-keys-polled’ 模块驱动
if (ret)
platform_driver_unregister(&gpio_keys_driver);
return ret;
}
“gpio-keys” 模块驱动注册
// 注册 "gpio-keys" 模块驱动
static int gpio_keys_probe(struct platform_device *pdev)
{
struct gpio_keys_platform_data *pdata;
struct gpio_keys_button_dev *bdev;
int ret, i;
ret = gpio_keys_button_probe(pdev, &bdev, 0);
if (ret)
return ret;
pdata = bdev->pdata;
for (i = 0; i < pdata->nbuttons; i++) {
const struct gpio_keys_button *button = &pdata->buttons[i];
struct gpio_keys_button_data *bdata = &bdev->data[i];
unsigned long irqflags = IRQF_ONESHOT;
INIT_DELAYED_WORK(&bdata->work, gpio_keys_irq_work_func); // 注册中断下半部分回调函数
if (!bdata->gpiod)
continue;
if (!button->irq) {
bdata->irq = gpio_to_irq(button->gpio);
if (bdata->irq < 0) {
dev_err(&pdev->dev, "failed to get irq for gpio:%d\n",
button->gpio);
continue;
}
irqflags |= IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING;
} else {
bdata->irq = button->irq;
}
schedule_delayed_work(&bdata->work,
msecs_to_jiffies(bdata->software_debounce));
ret = devm_request_threaded_irq(&pdev->dev,
bdata->irq, NULL, button_handle_irq,
irqflags, dev_name(&pdev->dev), bdata);
if (ret < 0) {
bdata->irq = 0;
dev_err(&pdev->dev, "failed to request irq:%d for gpio:%d\n",
bdata->irq, button->gpio);
continue;
} else {
dev_dbg(&pdev->dev, "gpio:%d has irq:%d\n",
button->gpio, bdata->irq);
}
}
return 0;
}
// 1.回调此函数,中断下半部分回调函数
static void gpio_keys_irq_work_func(struct work_struct *work)
{
struct gpio_keys_button_data *bdata = container_of(work,
struct gpio_keys_button_data, work.work);
gpio_keys_handle_button(bdata);
}
// 2.调用此函数
static void gpio_keys_handle_button(struct gpio_keys_button_data *bdata)
{
unsigned int type = bdata->b->type ?: EV_KEY;
int state = gpio_button_get_value(bdata);
unsigned long seen = jiffies;
pr_debug(PFX "event type=%u, code=%u, pressed=%d\n",
type, bdata->b->code, state);
/* is this the initialization state? */
if (bdata->last_state == -1) {
/*
* Don't advertise unpressed buttons on initialization.
* Just save their state and continue otherwise this
* can cause OpenWrt to enter failsafe.
*/
if (type == EV_KEY && state == 0)
goto set_state;
/*
* But we are very interested in pressed buttons and
* initial switch state. These will be reported to
* userland.
*/
} else if (bdata->last_state == state) {
/* reset asserted counter (only relevant for polled keys) */
bdata->count = 0;
return;
}
if (bdata->count < bdata->threshold) {
bdata->count++;
return;
}
if (bdata->seen == 0)
bdata->seen = seen;
button_hotplug_create_event(button_map[bdata->map_entry].name, type,
(seen - bdata->seen) / HZ, state);
bdata->seen = seen;
set_state:
bdata->last_state = state;
bdata->count = 0;
}
// 3.调用此函数
static int button_hotplug_create_event(const char *name, unsigned int type,
unsigned long seen, int pressed)
{
struct bh_event *event;
pr_debug(PFX "create event, name=%s, seen=%lu, pressed=%d\n",
name, seen, pressed);
event = kzalloc(sizeof(*event), GFP_KERNEL);
if (!event)
return -ENOMEM;
event->name = name;
event->type = type;
event->seen = seen;
event->action = pressed ? "pressed" : "released";
INIT_WORK(&event->work, (void *)(void *)button_hotplug_work);
schedule_work(&event->work);
return 0;
}
// 4.调用此函数
static void button_hotplug_work(struct work_struct *work)
{
struct bh_event *event = container_of(work, struct bh_event, work);
int ret = 0;
event->skb = alloc_skb(BH_SKB_SIZE, GFP_KERNEL);
if (!event->skb)
goto out_free_event;
ret = bh_event_add_var(event, 0, "%s@", event->action);
if (ret)
goto out_free_skb;
ret = button_hotplug_fill_event(event);
if (ret)
goto out_free_skb;
printk(KERN_DEBUG " %s,line:%d \n",__FILE__,__LINE__);
NETLINK_CB(event->skb).dst_group = 1;
broadcast_uevent(event->skb, 0, 1, GFP_KERNEL); //调用 event 发送程序
out_free_skb:
if (ret) {
pr_err(PFX "work error %d\n", ret);
kfree_skb(event->skb);
}
out_free_event:
kfree(event);
}
‘gpio-keys-polled’ 模块驱动注册
// 1.注册 ‘gpio-keys-polled’ 模块驱动
static int gpio_keys_polled_probe(struct platform_device *pdev)
{
struct gpio_keys_platform_data *pdata;
struct gpio_keys_button_dev *bdev;
int ret;
ret = gpio_keys_button_probe(pdev, &bdev, 1);
if (ret)
return ret;
INIT_DELAYED_WORK(&bdev->work, gpio_keys_polled_poll);
pdata = bdev->pdata;
if (pdata->enable)
pdata->enable(bdev->dev);
gpio_keys_polled_queue_work(bdev);
return ret;
}
// 2.回调此函数
static void button_hotplug_work(struct work_struct *work)
{
struct bh_event *event = container_of(work, struct bh_event, work);
int ret = 0;
event->skb = alloc_skb(BH_SKB_SIZE, GFP_KERNEL);
if (!event->skb)
goto out_free_event;
ret = bh_event_add_var(event, 0, "%s@", event->action);
if (ret)
goto out_free_skb;
ret = button_hotplug_fill_event(event);
if (ret)
goto out_free_skb;
printk(KERN_DEBUG " %s,line:%d \n",__FILE__,__LINE__);
NETLINK_CB(event->skb).dst_group = 1;
broadcast_uevent(event->skb, 0, 1, GFP_KERNEL); //调用 event 发送程序
out_free_skb:
if (ret) {
pr_err(PFX "work error %d\n", ret);
kfree_skb(event->skb);
}
out_free_event:
kfree(event);
}
// 3.kobject-uevent 接口API封装
#if defined(CONFIG_NET)
int broadcast_uevent(struct sk_buff *skb, __u32 pid, __u32 group,
gfp_t allocation)
{
struct uevent_sock *ue_sk;
int err = 0;
/* send netlink message */
mutex_lock(&uevent_sock_mutex);
list_for_each_entry(ue_sk, &uevent_sock_list, list) {
struct sock *uevent_sock = ue_sk->sk;
struct sk_buff *skb2;
skb2 = skb_clone(skb, allocation);
if (!skb2)
break;
err = netlink_broadcast(uevent_sock, skb2, pid, group,
allocation); //最终调用 netlink_broadcast 函数,把事件发送到用户空间
if (err)
break;
}
mutex_unlock(&uevent_sock_mutex);
kfree_skb(skb);
return err;
}
#else
int broadcast_uevent(struct sk_buff *skb, __u32 pid, __u32 group,
gfp_t allocation)
{
kfree_skb(skb);
return 0;
}
#endif
EXPORT_SYMBOL_GPL(broadcast_uevent);
对比分析结果
openWRT的 “gpio-keys” 和 ‘gpio-keys-polled’ 驱动程序,最后都调用函数 button_hotplug_work() 函数,
把键盘检测结果,通过 netlink_broadcast() 接口API,把事件发送到用户空间,因此在 openwrt 路由系统中,
如果使用 linux 内核的 gpio-keys 做为内核事件的话,需要设备树中 gpio 设备命名,同时修改内核中gpio-key驱动程序
static struct platform_driver gpio_keys_device_driver = {
.probe = gpio_keys_probe,
.driver = {
.name = "gpio-keys",
.pm = &gpio_keys_pm_ops,
.of_match_table = gpio_keys_of_match,
}
};
设备驱动名称,避免与 openWrt 中的 gpio-button-hotplug 模块驱动名称相同。