通过/ dev / input / eventX

时间:2018-04-25 17:11:31

标签: c linux arm gpio device-tree

我曾经用/ sys / class / gpio处理用户空间中的gpio。问题是我需要在内核驱动程序中使用此gpio。由于来自内核驱动程序的gpio请求,我无法通过/ sys / class / gpio操作此gpio。

然而,在将gpio声明为设备树中的gpio键之后,我能够通过/ dev / input / eventX使用它。问题是我只能通过此界面获取事件(新状态),但不能获得初始事件。所以直到第一个事件,我才知道我的gpio的当前状态。

我试图查找解决方案/ sys / class / input / eventX和/ sys / class / input / inputX但没有成功。

此处提供更完整的问题:https://www.mail-archive.com/kernelnewbies@kernelnewbies.org/msg18588.html

我使用的当前脏方法:Char device + ioctl

diff --git a/drivers/input/keyboard/gpio_keys.c b/drivers/input/keyboard/gpio_keys.c
index 77d7b07..99df807 100644
--- a/drivers/input/keyboard/gpio_keys.c
+++ b/drivers/input/keyboard/gpio_keys.c
@@ -31,6 +31,16 @@
 #include <linux/of_gpio.h>
 #include <linux/of_irq.h>
 #include <linux/spinlock.h>
+#include <linux/cdev.h>
+#include <linux/uaccess.h>
+
+struct keys_dev {
+       struct cdev dev;
+       dev_t dev_no;
+       struct class *cl;
+};
+
+static struct keys_dev keys_inst;

 struct gpio_button_data {
        const struct gpio_keys_button *button;
@@ -55,6 +65,27 @@ struct gpio_keys_drvdata {
        struct gpio_button_data data[0];
 };

+
+#define KEYS_DEVICE_NAME "plug_status"
+#define GPIO_USB 1
+
+static long gpio_keys_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
+{
+       int value = -1;
+       int err;
+
+       value = gpio_get_value (GPIO_USB);
+       value = !value;
+       err = copy_to_user((char*)arg, &value, sizeof(int));
+       return value;
+}
+
+static const struct file_operations gpio_keys_fops = {
+       .owner = THIS_MODULE,
+       .unlocked_ioctl = gpio_keys_ioctl,
+};
+
+
 /*
  * SYSFS interface for enabling/disabling keys and switches:
  *
@@ -851,11 +882,40 @@ static struct platform_driver gpio_keys_device_driver = {

 static int __init gpio_keys_init(void)
 {
+       int r;
+
+       r = alloc_chrdev_region(&keys_inst.dev_no , 0, 1, KEYS_DEVICE_NAME);
+       if (r < 0) {
+               printk(KERN_ERR "alloc_chrdev_region failed!\n");
+               return -ENOMEM;
+       }
+       keys_inst.cl = class_create(THIS_MODULE, KEYS_DEVICE_NAME);
+       if (keys_inst.cl == NULL) {
+               printk(KERN_ERR "Could not create class!\n");
+               return -ENOMEM;
+       }
+
+       if (device_create(keys_inst.cl, NULL, keys_inst.dev_no, NULL, KEYS_DEVICE_NAME) == NULL) {
+               printk(KERN_ERR "Could not create character device!\n");
+               return -ENOMEM;
+       }
+
+       cdev_init(&keys_inst.dev, &gpio_keys_fops );
+       r = cdev_add(&keys_inst.dev, keys_inst.dev_no, 1);
+       if (r < 0) {
+               printk(KERN_ERR "Could not add character device!\n");
+               return -ENOMEM;
+       }
+
        return platform_driver_register(&gpio_keys_device_driver);
 }

 static void __exit gpio_keys_exit(void)
 {
+
+       device_destroy(keys_inst.cl, keys_inst.dev_no);
+       class_destroy(keys_inst.cl);
+       unregister_chrdev_region(keys_inst.dev_no, 1);
        platform_driver_unregister(&gpio_keys_device_driver);
 }

1 个答案:

答案 0 :(得分:0)

我也读了你的另一篇文章。不确定这是否可行但是您可以在运行时(通过insmodmodprobe)动态加载 gpio-keys.c 驱动程序作为可加载的内核模块。

因此,在将 gpio-keys.ko 加载到内核之前,您可以先启动系统。那么,在那时,您可以先读取您的gpio以确定其初始值:cat /sys/class/gpio/gpio1/value。然后,一旦确定该值,您就可以将内核模块 gpio-keys.ko 插入到内核中。希望在那时,您的gpio密钥变得可用,因此您可以将它们用作中断以将您的电路板从挂起模式唤醒。