allow tuning of more flags in /sys/devices/debounce
[ms2-fixes] / debounce.c
index 845adad..cefa1d9 100644 (file)
@@ -5,10 +5,16 @@
 
 #define PREFIX "debounce: "
 
-static int debounce_delay = 10;
+static int debounce_delay = 15;
+
+static unsigned old_flags = 0;
+ktime_t old_debounce_delay;
+ktime_t old_settle_time;
+ktime_t old_poll_time;
+static struct gpio_event_matrix_info *gpio_evmi = NULL;
 
 module_param(debounce_delay, int, S_IRUSR | S_IRGRP | S_IROTH);
-MODULE_PARM_DESC(debounce_delay, "debouncing delay (ms), default: 10");
+MODULE_PARM_DESC(debounce_delay, "debouncing delay (ms), default: 15");
 
 static int find_ms2_dev(struct device *dev, void *data)
 {
@@ -19,15 +25,134 @@ static int find_ms2_dev(struct device *dev, void *data)
        return 0;
 }
 
+static ssize_t show_debounce_delay(struct device *dev, struct device_attribute *attr, char *buf)
+{
+       if (!gpio_evmi) 
+               return -ENODEV;
+
+       return snprintf(buf, PAGE_SIZE, "%ld\n", (gpio_evmi->debounce_delay.tv.nsec / NSEC_PER_MSEC));
+}
+
+static void set_debounce_delay(long delay)
+{
+       if (gpio_evmi->debounce_delay.tv.nsec != delay * NSEC_PER_MSEC) {
+               printk(KERN_INFO PREFIX "Changing debounce_delay\n");
+               gpio_evmi->debounce_delay.tv.nsec = delay * NSEC_PER_MSEC;
+               debounce_delay = delay;
+               printk(KERN_INFO PREFIX "debounce_delay: %u\n", gpio_evmi->debounce_delay.tv.nsec);
+       }
+
+       if (gpio_evmi->debounce_delay.tv.nsec != 0) {
+               if (!(gpio_evmi->flags & GPIOKPF_DEBOUNCE)) {
+                       printk(KERN_INFO PREFIX "Activating debounce\n");
+                       gpio_evmi->flags |= GPIOKPF_DEBOUNCE;
+               }
+       } else {
+               if (gpio_evmi->flags & GPIOKPF_DEBOUNCE) {
+                       printk(KERN_INFO PREFIX "Deactivating debounce\n");
+                       gpio_evmi->flags &= ~GPIOKPF_DEBOUNCE;
+               }
+       }
+}
+
+static ssize_t store_debounce_delay(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
+{
+       long int delay;
+
+       if (!gpio_evmi) 
+               return -ENODEV;
+
+       sscanf(buf, "%ld", &delay);
+       set_debounce_delay(delay);
+
+       return strnlen(buf, PAGE_SIZE);
+}
+
+static ssize_t show_settle_time(struct device *dev, struct device_attribute *attr, char *buf)
+{
+       if (!gpio_evmi) 
+               return -ENODEV;
+
+       return snprintf(buf, PAGE_SIZE, "%ld\n", (gpio_evmi->settle_time.tv.nsec / NSEC_PER_USEC));
+}
+
+static ssize_t store_settle_time(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
+{
+       long int delay;
+
+       if (!gpio_evmi) 
+               return -ENODEV;
+
+       sscanf(buf, "%ld", &delay);
+       gpio_evmi->settle_time.tv.nsec = delay * NSEC_PER_USEC;
+
+       return strnlen(buf, PAGE_SIZE);
+}
+
+static ssize_t show_poll_time(struct device *dev, struct device_attribute *attr, char *buf)
+{
+       if (!gpio_evmi) 
+               return -ENODEV;
+
+       return snprintf(buf, PAGE_SIZE, "%ld\n", (gpio_evmi->poll_time.tv.nsec / NSEC_PER_MSEC));
+}
+
+static ssize_t store_poll_time(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
+{
+       long int delay;
+
+       if (!gpio_evmi) 
+               return -ENODEV;
+
+       sscanf(buf, "%ld", &delay);
+       gpio_evmi->poll_time.tv.nsec = delay * NSEC_PER_MSEC;
+
+       return strnlen(buf, PAGE_SIZE);
+}
+
+static ssize_t show_flags(struct device *dev, struct device_attribute *attr, char *buf)
+{
+       if (!gpio_evmi) 
+               return -ENODEV;
+
+       return snprintf(buf, PAGE_SIZE, "0x%x\n", gpio_evmi->flags);
+}
+
+static ssize_t store_flags(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
+{
+       unsigned flags;
+
+       if (!gpio_evmi) 
+               return -ENODEV;
+
+       sscanf(buf, "0x%x", &flags);
+       gpio_evmi->flags = flags;
+
+       return strnlen(buf, PAGE_SIZE);
+}
+
+static DEVICE_ATTR(debounce_delay, (S_IRUGO | S_IWUGO), &show_debounce_delay, store_debounce_delay);
+static DEVICE_ATTR(settle_time, (S_IRUGO | S_IWUGO), &show_settle_time, store_settle_time);
+static DEVICE_ATTR(poll_time, (S_IRUGO | S_IWUGO), &show_poll_time, store_poll_time);
+static DEVICE_ATTR(flags, (S_IRUGO | S_IWUGO), &show_flags, store_flags);
+
+static void debounce_release(struct device *dev)
+{
+}
+
+static struct device debounce_device = {
+       .init_name = "debounce",
+       .release = debounce_release,
+};
+
 static int __init debounce_init(void)
 {
        struct device *event_dev = NULL;
        struct gpio_event_platform_data *gpio_epd;
        struct gpio_event_info *gpio_ei;
-       struct gpio_event_matrix_info *gpio_evmi;
+       int err = 0;
 
        printk(KERN_INFO PREFIX "Searching for " GPIO_EVENT_DEV_NAME "...\n");
-
        
        event_dev = device_find_child(&platform_bus, GPIO_EVENT_DEV_NAME, find_ms2_dev);
        if (event_dev == NULL)
@@ -41,29 +166,56 @@ static int __init debounce_init(void)
        gpio_ei = (struct gpio_event_info*)gpio_epd->info[0];
        gpio_evmi = container_of(gpio_ei, struct gpio_event_matrix_info, info);
 
+       err = device_register(&debounce_device);
+       if (err) {
+               return err;
+       }
+
+       err = device_create_file(&debounce_device, &dev_attr_debounce_delay);
+       err = device_create_file(&debounce_device, &dev_attr_settle_time);
+       err = device_create_file(&debounce_device, &dev_attr_poll_time);
+       err = device_create_file(&debounce_device, &dev_attr_flags);
+
        printk(KERN_INFO PREFIX "settle_time: %u\n", gpio_evmi->settle_time.tv.nsec);
        printk(KERN_INFO PREFIX "poll_time: %u\n", gpio_evmi->poll_time.tv.nsec);
        printk(KERN_INFO PREFIX "debounce_delay: %u\n", gpio_evmi->debounce_delay.tv.nsec);
        printk(KERN_INFO PREFIX "flags: 0x%x\n", gpio_evmi->flags);
 
-       /* GPIOKPF_DEBOUNCE should already be set by GPIOKPF_REMOVE_PHANTOM_KEYS */
-       if (!(gpio_evmi->flags & GPIOKPF_DEBOUNCE)) {
-               printk(KERN_INFO PREFIX "Activating debounce\n");
-               gpio_evmi->flags |= GPIOKPF_DEBOUNCE;
-               printk(KERN_INFO PREFIX "flags: 0x%x\n", gpio_evmi->flags);
-       }
+       old_debounce_delay = gpio_evmi->debounce_delay;
+       old_settle_time = gpio_evmi->settle_time;
+       old_poll_time = gpio_evmi->poll_time;
+       old_flags = gpio_evmi->flags;
 
-       if (gpio_evmi->debounce_delay.tv.nsec != debounce_delay * NSEC_PER_MSEC) {
-               printk(KERN_INFO PREFIX "Changing debounce_delay\n");
-               gpio_evmi->debounce_delay.tv.nsec = debounce_delay * NSEC_PER_MSEC;
-               printk(KERN_INFO PREFIX "debounce_delay: %u\n", gpio_evmi->debounce_delay.tv.nsec);
-       }
+       set_debounce_delay(debounce_delay);
+#if 0
+       gpio_evmi->flags &= ~GPIOKPF_REMOVE_SOME_PHANTOM_KEYS;
+#endif
+       printk(KERN_INFO PREFIX "flags: 0x%x\n", gpio_evmi->flags);
 
        return 0;
 }
 
 static void __exit debounce_exit(void)
 {
+       if (gpio_evmi) {
+               if (gpio_evmi->debounce_delay.tv.nsec != old_debounce_delay.tv.nsec) {
+                       printk(KERN_INFO PREFIX "Restoring debounce_delay\n");
+                       gpio_evmi->debounce_delay = old_debounce_delay;
+                       printk(KERN_INFO PREFIX "debounce_delay: %u\n", gpio_evmi->debounce_delay.tv.nsec);
+               }
+               if (gpio_evmi->flags != old_flags) {
+                       printk(KERN_INFO PREFIX "Restoring flags\n");
+                       gpio_evmi->flags = old_flags;
+                       printk(KERN_INFO PREFIX "flags: 0x%x\n", gpio_evmi->flags);
+               }
+               gpio_evmi->settle_time = old_settle_time;
+               gpio_evmi->poll_time = old_poll_time;
+       }
+       device_remove_file(&debounce_device, &dev_attr_debounce_delay);
+       device_remove_file(&debounce_device, &dev_attr_settle_time);
+       device_remove_file(&debounce_device, &dev_attr_poll_time);
+       device_remove_file(&debounce_device, &dev_attr_flags);
+       device_unregister(&debounce_device);
 }
 
 module_init(debounce_init);
Impressum, Datenschutz