| 1 | #include <linux/module.h> |
| 2 | #include <linux/device.h> |
| 3 | #include <linux/platform_device.h> |
| 4 | #include <linux/gpio_event.h> |
| 5 | |
| 6 | #define PREFIX "debounce: " |
| 7 | |
| 8 | static unsigned old_flags = 0; |
| 9 | ktime_t old_debounce_delay; |
| 10 | ktime_t old_settle_time; |
| 11 | ktime_t old_poll_time; |
| 12 | static struct gpio_event_matrix_info *gpio_evmi = NULL; |
| 13 | |
| 14 | static int find_ms2_dev(struct device *dev, void *data) |
| 15 | { |
| 16 | if (!strncmp((char*)data, dev_name(dev), strlen((char*)data))) { |
| 17 | printk(KERN_INFO PREFIX "Found it\n"); |
| 18 | return 1; |
| 19 | } |
| 20 | return 0; |
| 21 | } |
| 22 | |
| 23 | static ssize_t show_debounce_delay(struct device *dev, struct device_attribute *attr, char *buf) |
| 24 | { |
| 25 | if (!gpio_evmi) |
| 26 | return -ENODEV; |
| 27 | |
| 28 | return snprintf(buf, PAGE_SIZE, "%ld\n", (gpio_evmi->debounce_delay.tv.nsec / NSEC_PER_MSEC)); |
| 29 | } |
| 30 | |
| 31 | static void set_debounce_delay(long delay) |
| 32 | { |
| 33 | if (gpio_evmi->debounce_delay.tv.nsec != delay * NSEC_PER_MSEC) { |
| 34 | printk(KERN_INFO PREFIX "Changing debounce_delay\n"); |
| 35 | gpio_evmi->debounce_delay.tv.nsec = delay * NSEC_PER_MSEC; |
| 36 | printk(KERN_INFO PREFIX "debounce_delay: %u\n", gpio_evmi->debounce_delay.tv.nsec); |
| 37 | } |
| 38 | |
| 39 | #if 0 |
| 40 | if (gpio_evmi->debounce_delay.tv.nsec != 0) { |
| 41 | if (!(gpio_evmi->flags & GPIOKPF_DEBOUNCE)) { |
| 42 | printk(KERN_INFO PREFIX "Activating debounce\n"); |
| 43 | gpio_evmi->flags |= GPIOKPF_DEBOUNCE; |
| 44 | } |
| 45 | } else { |
| 46 | if (gpio_evmi->flags & GPIOKPF_DEBOUNCE) { |
| 47 | printk(KERN_INFO PREFIX "Deactivating debounce\n"); |
| 48 | gpio_evmi->flags &= ~GPIOKPF_DEBOUNCE; |
| 49 | } |
| 50 | } |
| 51 | #endif |
| 52 | } |
| 53 | |
| 54 | static ssize_t store_debounce_delay(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) |
| 55 | { |
| 56 | long int delay; |
| 57 | |
| 58 | if (!gpio_evmi) |
| 59 | return -ENODEV; |
| 60 | |
| 61 | sscanf(buf, "%ld", &delay); |
| 62 | set_debounce_delay(delay); |
| 63 | |
| 64 | return count; |
| 65 | } |
| 66 | |
| 67 | static ssize_t show_settle_time(struct device *dev, struct device_attribute *attr, char *buf) |
| 68 | { |
| 69 | if (!gpio_evmi) |
| 70 | return -ENODEV; |
| 71 | |
| 72 | return snprintf(buf, PAGE_SIZE, "%ld\n", (gpio_evmi->settle_time.tv.nsec / NSEC_PER_USEC)); |
| 73 | } |
| 74 | |
| 75 | static ssize_t store_settle_time(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) |
| 76 | { |
| 77 | long int delay; |
| 78 | |
| 79 | if (!gpio_evmi) |
| 80 | return -ENODEV; |
| 81 | |
| 82 | sscanf(buf, "%ld", &delay); |
| 83 | gpio_evmi->settle_time.tv.nsec = delay * NSEC_PER_USEC; |
| 84 | |
| 85 | return count; |
| 86 | } |
| 87 | |
| 88 | static ssize_t show_poll_time(struct device *dev, struct device_attribute *attr, char *buf) |
| 89 | { |
| 90 | if (!gpio_evmi) |
| 91 | return -ENODEV; |
| 92 | |
| 93 | return snprintf(buf, PAGE_SIZE, "%ld\n", (gpio_evmi->poll_time.tv.nsec / NSEC_PER_MSEC)); |
| 94 | } |
| 95 | |
| 96 | static ssize_t store_poll_time(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) |
| 97 | { |
| 98 | long int delay; |
| 99 | |
| 100 | if (!gpio_evmi) |
| 101 | return -ENODEV; |
| 102 | |
| 103 | sscanf(buf, "%ld", &delay); |
| 104 | gpio_evmi->poll_time.tv.nsec = delay * NSEC_PER_MSEC; |
| 105 | |
| 106 | return count; |
| 107 | } |
| 108 | |
| 109 | static ssize_t show_flags(struct device *dev, struct device_attribute *attr, char *buf) |
| 110 | { |
| 111 | if (!gpio_evmi) |
| 112 | return -ENODEV; |
| 113 | |
| 114 | return snprintf(buf, PAGE_SIZE, "0x%x\n", gpio_evmi->flags); |
| 115 | } |
| 116 | |
| 117 | static ssize_t store_flags(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) |
| 118 | { |
| 119 | unsigned flags; |
| 120 | |
| 121 | if (!gpio_evmi) |
| 122 | return -ENODEV; |
| 123 | |
| 124 | sscanf(buf, "0x%x", &flags); |
| 125 | |
| 126 | printk(KERN_INFO PREFIX "flags: 0x%x\n", flags); |
| 127 | |
| 128 | if (flags & GPIOKPF_DRIVE_INACTIVE) |
| 129 | return count; |
| 130 | |
| 131 | gpio_evmi->flags = flags; |
| 132 | |
| 133 | return count; |
| 134 | } |
| 135 | |
| 136 | static ssize_t show_debounce_flag(struct device *dev, struct device_attribute *attr, char *buf) |
| 137 | { |
| 138 | if (!gpio_evmi) |
| 139 | return -ENODEV; |
| 140 | |
| 141 | return snprintf(buf, PAGE_SIZE, "%u\n", (gpio_evmi->flags & GPIOKPF_DEBOUNCE) ? 1 : 0); |
| 142 | } |
| 143 | |
| 144 | static ssize_t store_debounce_flag(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) |
| 145 | { |
| 146 | unsigned flag; |
| 147 | |
| 148 | if (!gpio_evmi) |
| 149 | return -ENODEV; |
| 150 | |
| 151 | sscanf(buf, "%u", &flag); |
| 152 | |
| 153 | if (flag) { |
| 154 | gpio_evmi->flags |= GPIOKPF_DEBOUNCE; |
| 155 | } else { |
| 156 | gpio_evmi->flags &= ~GPIOKPF_DEBOUNCE; |
| 157 | } |
| 158 | |
| 159 | return count; |
| 160 | } |
| 161 | |
| 162 | static ssize_t show_remove_some_phantom_keys_flag(struct device *dev, struct device_attribute *attr, char *buf) |
| 163 | { |
| 164 | if (!gpio_evmi) |
| 165 | return -ENODEV; |
| 166 | |
| 167 | return snprintf(buf, PAGE_SIZE, "%u\n", (gpio_evmi->flags & GPIOKPF_REMOVE_SOME_PHANTOM_KEYS) ? 1 : 0); |
| 168 | } |
| 169 | |
| 170 | static ssize_t store_remove_some_phantom_keys_flag(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) |
| 171 | { |
| 172 | unsigned flag; |
| 173 | |
| 174 | if (!gpio_evmi) |
| 175 | return -ENODEV; |
| 176 | |
| 177 | sscanf(buf, "%u", &flag); |
| 178 | |
| 179 | if (flag) { |
| 180 | gpio_evmi->flags |= GPIOKPF_REMOVE_SOME_PHANTOM_KEYS; |
| 181 | } else { |
| 182 | gpio_evmi->flags &= ~GPIOKPF_REMOVE_SOME_PHANTOM_KEYS; |
| 183 | } |
| 184 | |
| 185 | return count; |
| 186 | } |
| 187 | |
| 188 | static ssize_t show_print_unmapped_keys_flag(struct device *dev, struct device_attribute *attr, char *buf) |
| 189 | { |
| 190 | if (!gpio_evmi) |
| 191 | return -ENODEV; |
| 192 | |
| 193 | return snprintf(buf, PAGE_SIZE, "%u\n", (gpio_evmi->flags & GPIOKPF_PRINT_UNMAPPED_KEYS) ? 1 : 0); |
| 194 | } |
| 195 | |
| 196 | static ssize_t store_print_unmapped_keys_flag(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) |
| 197 | { |
| 198 | unsigned flag; |
| 199 | |
| 200 | if (!gpio_evmi) |
| 201 | return -ENODEV; |
| 202 | |
| 203 | sscanf(buf, "%u", &flag); |
| 204 | |
| 205 | if (flag) { |
| 206 | gpio_evmi->flags |= GPIOKPF_PRINT_UNMAPPED_KEYS; |
| 207 | } else { |
| 208 | gpio_evmi->flags &= ~GPIOKPF_PRINT_UNMAPPED_KEYS; |
| 209 | } |
| 210 | |
| 211 | return count; |
| 212 | } |
| 213 | |
| 214 | static ssize_t show_print_mapped_keys_flag(struct device *dev, struct device_attribute *attr, char *buf) |
| 215 | { |
| 216 | if (!gpio_evmi) |
| 217 | return -ENODEV; |
| 218 | |
| 219 | return snprintf(buf, PAGE_SIZE, "%u\n", (gpio_evmi->flags & GPIOKPF_PRINT_MAPPED_KEYS) ? 1 : 0); |
| 220 | } |
| 221 | |
| 222 | static ssize_t store_print_mapped_keys_flag(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) |
| 223 | { |
| 224 | unsigned flag; |
| 225 | |
| 226 | if (!gpio_evmi) |
| 227 | return -ENODEV; |
| 228 | |
| 229 | sscanf(buf, "%u", &flag); |
| 230 | |
| 231 | if (flag) { |
| 232 | gpio_evmi->flags |= GPIOKPF_PRINT_MAPPED_KEYS; |
| 233 | } else { |
| 234 | gpio_evmi->flags &= ~GPIOKPF_PRINT_MAPPED_KEYS; |
| 235 | } |
| 236 | |
| 237 | return count; |
| 238 | } |
| 239 | |
| 240 | static ssize_t show_print_phantom_keys_flag(struct device *dev, struct device_attribute *attr, char *buf) |
| 241 | { |
| 242 | if (!gpio_evmi) |
| 243 | return -ENODEV; |
| 244 | |
| 245 | return snprintf(buf, PAGE_SIZE, "%u\n", (gpio_evmi->flags & GPIOKPF_PRINT_PHANTOM_KEYS) ? 1 : 0); |
| 246 | } |
| 247 | |
| 248 | static ssize_t store_print_phantom_keys_flag(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) |
| 249 | { |
| 250 | unsigned flag; |
| 251 | |
| 252 | if (!gpio_evmi) |
| 253 | return -ENODEV; |
| 254 | |
| 255 | sscanf(buf, "%u", &flag); |
| 256 | |
| 257 | if (flag) { |
| 258 | gpio_evmi->flags |= GPIOKPF_PRINT_PHANTOM_KEYS; |
| 259 | } else { |
| 260 | gpio_evmi->flags &= ~GPIOKPF_PRINT_PHANTOM_KEYS; |
| 261 | } |
| 262 | |
| 263 | return count; |
| 264 | } |
| 265 | |
| 266 | static DEVICE_ATTR(debounce_delay, (S_IRUGO | S_IWUGO), show_debounce_delay, store_debounce_delay); |
| 267 | static DEVICE_ATTR(settle_time, (S_IRUGO | S_IWUGO), show_settle_time, store_settle_time); |
| 268 | static DEVICE_ATTR(poll_time, (S_IRUGO | S_IWUGO), show_poll_time, store_poll_time); |
| 269 | static DEVICE_ATTR(flags, (S_IRUGO), show_flags, store_flags); |
| 270 | static DEVICE_ATTR(debounce_flag, (S_IRUGO | S_IWUGO), show_debounce_flag, store_debounce_flag); |
| 271 | static DEVICE_ATTR(remove_some_phantom_keys_flag, (S_IRUGO | S_IWUGO), show_remove_some_phantom_keys_flag, store_remove_some_phantom_keys_flag); |
| 272 | static DEVICE_ATTR(print_unmapped_keys_flag, (S_IRUGO | S_IWUGO), show_print_unmapped_keys_flag, store_print_unmapped_keys_flag); |
| 273 | static DEVICE_ATTR(print_mapped_keys_flag, (S_IRUGO | S_IWUGO), show_print_mapped_keys_flag, store_print_mapped_keys_flag); |
| 274 | static DEVICE_ATTR(print_phantom_keys_flag, (S_IRUGO | S_IWUGO), show_print_phantom_keys_flag, store_print_phantom_keys_flag); |
| 275 | |
| 276 | static void debounce_release(struct device *dev) |
| 277 | { |
| 278 | } |
| 279 | |
| 280 | static struct device debounce_device = { |
| 281 | .init_name = "debounce", |
| 282 | .release = debounce_release, |
| 283 | }; |
| 284 | |
| 285 | static int __init debounce_init(void) |
| 286 | { |
| 287 | struct device *event_dev = NULL; |
| 288 | struct gpio_event_platform_data *gpio_epd; |
| 289 | struct gpio_event_info *gpio_ei; |
| 290 | int err = 0; |
| 291 | |
| 292 | printk(KERN_INFO PREFIX "Searching for " GPIO_EVENT_DEV_NAME "...\n"); |
| 293 | |
| 294 | event_dev = device_find_child(&platform_bus, GPIO_EVENT_DEV_NAME, find_ms2_dev); |
| 295 | if (event_dev == NULL) |
| 296 | return -ENODEV; |
| 297 | |
| 298 | gpio_epd = (struct gpio_event_platform_data*)event_dev->platform_data; |
| 299 | printk(KERN_INFO PREFIX "And there is a %s connected...\n", gpio_epd->name); |
| 300 | if (strcmp(gpio_epd->name, "sholes-keypad")) |
| 301 | return -ENODEV; |
| 302 | |
| 303 | gpio_ei = (struct gpio_event_info*)gpio_epd->info[0]; |
| 304 | gpio_evmi = container_of(gpio_ei, struct gpio_event_matrix_info, info); |
| 305 | |
| 306 | err = device_register(&debounce_device); |
| 307 | if (err) { |
| 308 | return err; |
| 309 | } |
| 310 | |
| 311 | err = device_create_file(&debounce_device, &dev_attr_debounce_delay); |
| 312 | err = device_create_file(&debounce_device, &dev_attr_settle_time); |
| 313 | err = device_create_file(&debounce_device, &dev_attr_poll_time); |
| 314 | err = device_create_file(&debounce_device, &dev_attr_flags); |
| 315 | err = device_create_file(&debounce_device, &dev_attr_debounce_flag); |
| 316 | err = device_create_file(&debounce_device, &dev_attr_remove_some_phantom_keys_flag); |
| 317 | err = device_create_file(&debounce_device, &dev_attr_print_unmapped_keys_flag); |
| 318 | err = device_create_file(&debounce_device, &dev_attr_print_mapped_keys_flag); |
| 319 | err = device_create_file(&debounce_device, &dev_attr_print_phantom_keys_flag); |
| 320 | |
| 321 | printk(KERN_INFO PREFIX "settle_time: %u\n", gpio_evmi->settle_time.tv.nsec); |
| 322 | printk(KERN_INFO PREFIX "poll_time: %u\n", gpio_evmi->poll_time.tv.nsec); |
| 323 | printk(KERN_INFO PREFIX "debounce_delay: %u\n", gpio_evmi->debounce_delay.tv.nsec); |
| 324 | printk(KERN_INFO PREFIX "flags: 0x%x\n", gpio_evmi->flags); |
| 325 | |
| 326 | old_debounce_delay = gpio_evmi->debounce_delay; |
| 327 | old_settle_time = gpio_evmi->settle_time; |
| 328 | old_poll_time = gpio_evmi->poll_time; |
| 329 | old_flags = gpio_evmi->flags; |
| 330 | |
| 331 | printk(KERN_INFO PREFIX "flags: 0x%x\n", gpio_evmi->flags); |
| 332 | |
| 333 | return 0; |
| 334 | } |
| 335 | |
| 336 | static void __exit debounce_exit(void) |
| 337 | { |
| 338 | if (gpio_evmi) { |
| 339 | if (gpio_evmi->debounce_delay.tv.nsec != old_debounce_delay.tv.nsec) { |
| 340 | printk(KERN_INFO PREFIX "Restoring debounce_delay\n"); |
| 341 | gpio_evmi->debounce_delay = old_debounce_delay; |
| 342 | printk(KERN_INFO PREFIX "debounce_delay: %u\n", gpio_evmi->debounce_delay.tv.nsec); |
| 343 | } |
| 344 | if (gpio_evmi->flags != old_flags) { |
| 345 | printk(KERN_INFO PREFIX "Restoring flags\n"); |
| 346 | gpio_evmi->flags = old_flags; |
| 347 | printk(KERN_INFO PREFIX "flags: 0x%x\n", gpio_evmi->flags); |
| 348 | } |
| 349 | gpio_evmi->settle_time = old_settle_time; |
| 350 | gpio_evmi->poll_time = old_poll_time; |
| 351 | } |
| 352 | device_remove_file(&debounce_device, &dev_attr_debounce_delay); |
| 353 | device_remove_file(&debounce_device, &dev_attr_settle_time); |
| 354 | device_remove_file(&debounce_device, &dev_attr_poll_time); |
| 355 | device_remove_file(&debounce_device, &dev_attr_flags); |
| 356 | device_remove_file(&debounce_device, &dev_attr_debounce_flag); |
| 357 | device_remove_file(&debounce_device, &dev_attr_remove_some_phantom_keys_flag); |
| 358 | device_remove_file(&debounce_device, &dev_attr_print_unmapped_keys_flag); |
| 359 | device_remove_file(&debounce_device, &dev_attr_print_mapped_keys_flag); |
| 360 | device_remove_file(&debounce_device, &dev_attr_print_phantom_keys_flag); |
| 361 | device_unregister(&debounce_device); |
| 362 | } |
| 363 | |
| 364 | module_init(debounce_init); |
| 365 | module_exit(debounce_exit); |
| 366 | |
| 367 | MODULE_LICENSE("GPL"); |
| 368 | MODULE_AUTHOR("Michael Gernoth <michael@gernoth.net>"); |