| 1 | /* |
| 2 | * GPIO Button Hotplug driver |
| 3 | * |
| 4 | * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org> |
| 5 | * Copyright (C) 2008-2010 Gabor Juhos <juhosg@openwrt.org> |
| 6 | * |
| 7 | * Based on the diag.c - GPIO interface driver for Broadcom boards |
| 8 | * Copyright (C) 2006 Mike Baker <mbm@openwrt.org>, |
| 9 | * Copyright (C) 2006-2007 Felix Fietkau <nbd@openwrt.org> |
| 10 | * Copyright (C) 2008 Andy Boyett <agb@openwrt.org> |
| 11 | * |
| 12 | * This program is free software; you can redistribute it and/or modify it |
| 13 | * under the terms of the GNU General Public License version 2 as published |
| 14 | * by the Free Software Foundation. |
| 15 | */ |
| 16 | |
| 17 | #include <linux/module.h> |
| 18 | #include <linux/version.h> |
| 19 | #include <linux/kmod.h> |
| 20 | |
| 21 | #include <linux/workqueue.h> |
| 22 | #include <linux/skbuff.h> |
| 23 | #include <linux/netlink.h> |
| 24 | #include <linux/kobject.h> |
| 25 | #include <linux/input.h> |
| 26 | #include <linux/platform_device.h> |
| 27 | #include <linux/of_gpio.h> |
| 28 | #include <linux/gpio_keys.h> |
| 29 | |
| 30 | #define DRV_NAME "gpio-keys-polled" |
| 31 | |
| 32 | #define BH_SKB_SIZE 2048 |
| 33 | |
| 34 | #define PFX DRV_NAME ": " |
| 35 | |
| 36 | #undef BH_DEBUG |
| 37 | |
| 38 | #ifdef BH_DEBUG |
| 39 | #define BH_DBG(fmt, args...) printk(KERN_DEBUG "%s: " fmt, DRV_NAME, ##args ) |
| 40 | #else |
| 41 | #define BH_DBG(fmt, args...) do {} while (0) |
| 42 | #endif |
| 43 | |
| 44 | #define BH_ERR(fmt, args...) printk(KERN_ERR "%s: " fmt, DRV_NAME, ##args ) |
| 45 | |
| 46 | struct bh_priv { |
| 47 | unsigned long seen; |
| 48 | }; |
| 49 | |
| 50 | struct bh_event { |
| 51 | const char *name; |
| 52 | char *action; |
| 53 | unsigned long seen; |
| 54 | |
| 55 | struct sk_buff *skb; |
| 56 | struct work_struct work; |
| 57 | }; |
| 58 | |
| 59 | struct bh_map { |
| 60 | unsigned int code; |
| 61 | const char *name; |
| 62 | }; |
| 63 | |
| 64 | struct gpio_keys_button_data { |
| 65 | struct delayed_work work; |
| 66 | struct bh_priv bh; |
| 67 | int last_state; |
| 68 | int count; |
| 69 | int threshold; |
| 70 | int can_sleep; |
| 71 | }; |
| 72 | |
| 73 | extern u64 uevent_next_seqnum(void); |
| 74 | |
| 75 | #define BH_MAP(_code, _name) \ |
| 76 | { \ |
| 77 | .code = (_code), \ |
| 78 | .name = (_name), \ |
| 79 | } |
| 80 | |
| 81 | static struct bh_map button_map[] = { |
| 82 | BH_MAP(BTN_0, "BTN_0"), |
| 83 | BH_MAP(BTN_1, "BTN_1"), |
| 84 | BH_MAP(BTN_2, "BTN_2"), |
| 85 | BH_MAP(BTN_3, "BTN_3"), |
| 86 | BH_MAP(BTN_4, "BTN_4"), |
| 87 | BH_MAP(BTN_5, "BTN_5"), |
| 88 | BH_MAP(BTN_6, "BTN_6"), |
| 89 | BH_MAP(BTN_7, "BTN_7"), |
| 90 | BH_MAP(BTN_8, "BTN_8"), |
| 91 | BH_MAP(BTN_9, "BTN_9"), |
| 92 | BH_MAP(KEY_RESTART, "reset"), |
| 93 | BH_MAP(KEY_RFKILL, "rfkill"), |
| 94 | #ifdef KEY_WPS_BUTTON |
| 95 | BH_MAP(KEY_WPS_BUTTON, "wps"), |
| 96 | #endif /* KEY_WPS_BUTTON */ |
| 97 | }; |
| 98 | |
| 99 | /* -------------------------------------------------------------------------*/ |
| 100 | |
| 101 | static int bh_event_add_var(struct bh_event *event, int argv, |
| 102 | const char *format, ...) |
| 103 | { |
| 104 | static char buf[128]; |
| 105 | char *s; |
| 106 | va_list args; |
| 107 | int len; |
| 108 | |
| 109 | if (argv) |
| 110 | return 0; |
| 111 | |
| 112 | va_start(args, format); |
| 113 | len = vsnprintf(buf, sizeof(buf), format, args); |
| 114 | va_end(args); |
| 115 | |
| 116 | if (len >= sizeof(buf)) { |
| 117 | BH_ERR("buffer size too small\n"); |
| 118 | WARN_ON(1); |
| 119 | return -ENOMEM; |
| 120 | } |
| 121 | |
| 122 | s = skb_put(event->skb, len + 1); |
| 123 | strcpy(s, buf); |
| 124 | |
| 125 | BH_DBG("added variable '%s'\n", s); |
| 126 | |
| 127 | return 0; |
| 128 | } |
| 129 | |
| 130 | static int button_hotplug_fill_event(struct bh_event *event) |
| 131 | { |
| 132 | int ret; |
| 133 | |
| 134 | ret = bh_event_add_var(event, 0, "HOME=%s", "/"); |
| 135 | if (ret) |
| 136 | return ret; |
| 137 | |
| 138 | ret = bh_event_add_var(event, 0, "PATH=%s", |
| 139 | "/sbin:/bin:/usr/sbin:/usr/bin"); |
| 140 | if (ret) |
| 141 | return ret; |
| 142 | |
| 143 | ret = bh_event_add_var(event, 0, "SUBSYSTEM=%s", "button"); |
| 144 | if (ret) |
| 145 | return ret; |
| 146 | |
| 147 | ret = bh_event_add_var(event, 0, "ACTION=%s", event->action); |
| 148 | if (ret) |
| 149 | return ret; |
| 150 | |
| 151 | ret = bh_event_add_var(event, 0, "BUTTON=%s", event->name); |
| 152 | if (ret) |
| 153 | return ret; |
| 154 | |
| 155 | ret = bh_event_add_var(event, 0, "SEEN=%ld", event->seen); |
| 156 | if (ret) |
| 157 | return ret; |
| 158 | |
| 159 | ret = bh_event_add_var(event, 0, "SEQNUM=%llu", uevent_next_seqnum()); |
| 160 | |
| 161 | return ret; |
| 162 | } |
| 163 | |
| 164 | static void button_hotplug_work(struct work_struct *work) |
| 165 | { |
| 166 | struct bh_event *event = container_of(work, struct bh_event, work); |
| 167 | int ret = 0; |
| 168 | |
| 169 | event->skb = alloc_skb(BH_SKB_SIZE, GFP_KERNEL); |
| 170 | if (!event->skb) |
| 171 | goto out_free_event; |
| 172 | |
| 173 | ret = bh_event_add_var(event, 0, "%s@", event->action); |
| 174 | if (ret) |
| 175 | goto out_free_skb; |
| 176 | |
| 177 | ret = button_hotplug_fill_event(event); |
| 178 | if (ret) |
| 179 | goto out_free_skb; |
| 180 | |
| 181 | NETLINK_CB(event->skb).dst_group = 1; |
| 182 | broadcast_uevent(event->skb, 0, 1, GFP_KERNEL); |
| 183 | |
| 184 | out_free_skb: |
| 185 | if (ret) { |
| 186 | BH_ERR("work error %d\n", ret); |
| 187 | kfree_skb(event->skb); |
| 188 | } |
| 189 | out_free_event: |
| 190 | kfree(event); |
| 191 | } |
| 192 | |
| 193 | static int button_hotplug_create_event(const char *name, unsigned long seen, |
| 194 | int pressed) |
| 195 | { |
| 196 | struct bh_event *event; |
| 197 | |
| 198 | BH_DBG("create event, name=%s, seen=%lu, pressed=%d\n", |
| 199 | name, seen, pressed); |
| 200 | |
| 201 | event = kzalloc(sizeof(*event), GFP_KERNEL); |
| 202 | if (!event) |
| 203 | return -ENOMEM; |
| 204 | |
| 205 | event->name = name; |
| 206 | event->seen = seen; |
| 207 | event->action = pressed ? "pressed" : "released"; |
| 208 | |
| 209 | INIT_WORK(&event->work, (void *)(void *)button_hotplug_work); |
| 210 | schedule_work(&event->work); |
| 211 | |
| 212 | return 0; |
| 213 | } |
| 214 | |
| 215 | /* -------------------------------------------------------------------------*/ |
| 216 | |
| 217 | #ifdef CONFIG_HOTPLUG |
| 218 | static int button_get_index(unsigned int code) |
| 219 | { |
| 220 | int i; |
| 221 | |
| 222 | for (i = 0; i < ARRAY_SIZE(button_map); i++) |
| 223 | if (button_map[i].code == code) |
| 224 | return i; |
| 225 | |
| 226 | return -1; |
| 227 | } |
| 228 | static void button_hotplug_event(struct gpio_keys_button_data *data, |
| 229 | unsigned int type, unsigned int code, int value) |
| 230 | { |
| 231 | struct bh_priv *priv = &data->bh; |
| 232 | unsigned long seen = jiffies; |
| 233 | int btn; |
| 234 | |
| 235 | BH_DBG("event type=%u, code=%u, value=%d\n", type, code, value); |
| 236 | |
| 237 | if (type != EV_KEY) |
| 238 | return; |
| 239 | |
| 240 | btn = button_get_index(code); |
| 241 | if (btn < 0) |
| 242 | return; |
| 243 | |
| 244 | button_hotplug_create_event(button_map[btn].name, |
| 245 | (seen - priv->seen) / HZ, value); |
| 246 | priv->seen = seen; |
| 247 | } |
| 248 | #else |
| 249 | static void button_hotplug_event(struct gpio_keys_button_data *data, |
| 250 | unsigned int type, unsigned int code, int value) |
| 251 | { |
| 252 | } |
| 253 | #endif /* CONFIG_HOTPLUG */ |
| 254 | |
| 255 | struct gpio_keys_polled_dev { |
| 256 | struct delayed_work work; |
| 257 | |
| 258 | struct device *dev; |
| 259 | struct gpio_keys_platform_data *pdata; |
| 260 | struct gpio_keys_button_data data[0]; |
| 261 | }; |
| 262 | |
| 263 | static void gpio_keys_polled_check_state(struct gpio_keys_button *button, |
| 264 | struct gpio_keys_button_data *bdata) |
| 265 | { |
| 266 | int state; |
| 267 | |
| 268 | if (bdata->can_sleep) |
| 269 | state = !!gpio_get_value_cansleep(button->gpio); |
| 270 | else |
| 271 | state = !!gpio_get_value(button->gpio); |
| 272 | |
| 273 | state = !!(state ^ button->active_low); |
| 274 | if (state != bdata->last_state) { |
| 275 | unsigned int type = button->type ?: EV_KEY; |
| 276 | |
| 277 | button_hotplug_event(bdata, type, button->code, state); |
| 278 | bdata->count = 0; |
| 279 | bdata->last_state = state; |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | static void gpio_keys_polled_queue_work(struct gpio_keys_polled_dev *bdev) |
| 284 | { |
| 285 | struct gpio_keys_platform_data *pdata = bdev->pdata; |
| 286 | unsigned long delay = msecs_to_jiffies(pdata->poll_interval); |
| 287 | |
| 288 | if (delay >= HZ) |
| 289 | delay = round_jiffies_relative(delay); |
| 290 | schedule_delayed_work(&bdev->work, delay); |
| 291 | } |
| 292 | |
| 293 | static void gpio_keys_polled_poll(struct work_struct *work) |
| 294 | { |
| 295 | struct gpio_keys_polled_dev *bdev = |
| 296 | container_of(work, struct gpio_keys_polled_dev, work.work); |
| 297 | struct gpio_keys_platform_data *pdata = bdev->pdata; |
| 298 | int i; |
| 299 | |
| 300 | for (i = 0; i < bdev->pdata->nbuttons; i++) { |
| 301 | struct gpio_keys_button_data *bdata = &bdev->data[i]; |
| 302 | |
| 303 | if (bdata->count < bdata->threshold) |
| 304 | bdata->count++; |
| 305 | else |
| 306 | gpio_keys_polled_check_state(&pdata->buttons[i], bdata); |
| 307 | } |
| 308 | gpio_keys_polled_queue_work(bdev); |
| 309 | } |
| 310 | |
| 311 | static void __devinit gpio_keys_polled_open(struct gpio_keys_polled_dev *bdev) |
| 312 | { |
| 313 | struct gpio_keys_platform_data *pdata = bdev->pdata; |
| 314 | int i; |
| 315 | |
| 316 | if (pdata->enable) |
| 317 | pdata->enable(bdev->dev); |
| 318 | |
| 319 | /* report initial state of the buttons */ |
| 320 | for (i = 0; i < pdata->nbuttons; i++) |
| 321 | gpio_keys_polled_check_state(&pdata->buttons[i], &bdev->data[i]); |
| 322 | |
| 323 | gpio_keys_polled_queue_work(bdev); |
| 324 | } |
| 325 | |
| 326 | #ifdef CONFIG_OF |
| 327 | static struct gpio_keys_platform_data * __devinit |
| 328 | gpio_keys_polled_get_devtree_pdata(struct device *dev) |
| 329 | { |
| 330 | struct device_node *node, *pp; |
| 331 | struct gpio_keys_platform_data *pdata; |
| 332 | struct gpio_keys_button *button; |
| 333 | int error; |
| 334 | int nbuttons; |
| 335 | int i; |
| 336 | |
| 337 | node = dev->of_node; |
| 338 | if (!node) |
| 339 | return NULL; |
| 340 | |
| 341 | nbuttons = of_get_child_count(node); |
| 342 | if (nbuttons == 0) |
| 343 | return NULL; |
| 344 | |
| 345 | pdata = kzalloc(sizeof(*pdata) + nbuttons * (sizeof *button), |
| 346 | GFP_KERNEL); |
| 347 | if (!pdata) { |
| 348 | error = -ENOMEM; |
| 349 | goto err_out; |
| 350 | } |
| 351 | |
| 352 | pdata->buttons = (struct gpio_keys_button *)(pdata + 1); |
| 353 | pdata->nbuttons = nbuttons; |
| 354 | |
| 355 | pdata->rep = !!of_get_property(node, "autorepeat", NULL); |
| 356 | of_property_read_u32(node, "poll-interval", &pdata->poll_interval); |
| 357 | |
| 358 | i = 0; |
| 359 | for_each_child_of_node(node, pp) { |
| 360 | enum of_gpio_flags flags; |
| 361 | |
| 362 | if (!of_find_property(pp, "gpios", NULL)) { |
| 363 | pdata->nbuttons--; |
| 364 | dev_warn(dev, "Found button without gpios\n"); |
| 365 | continue; |
| 366 | } |
| 367 | |
| 368 | button = &pdata->buttons[i++]; |
| 369 | |
| 370 | button->gpio = of_get_gpio_flags(pp, 0, &flags); |
| 371 | button->active_low = flags & OF_GPIO_ACTIVE_LOW; |
| 372 | |
| 373 | if (of_property_read_u32(pp, "linux,code", &button->code)) { |
| 374 | dev_err(dev, "Button without keycode: 0x%x\n", |
| 375 | button->gpio); |
| 376 | error = -EINVAL; |
| 377 | goto err_free_pdata; |
| 378 | } |
| 379 | |
| 380 | button->desc = of_get_property(pp, "label", NULL); |
| 381 | |
| 382 | if (of_property_read_u32(pp, "linux,input-type", &button->type)) |
| 383 | button->type = EV_KEY; |
| 384 | |
| 385 | button->wakeup = !!of_get_property(pp, "gpio-key,wakeup", NULL); |
| 386 | |
| 387 | if (of_property_read_u32(pp, "debounce-interval", |
| 388 | &button->debounce_interval)) |
| 389 | button->debounce_interval = 5; |
| 390 | } |
| 391 | |
| 392 | if (pdata->nbuttons == 0) { |
| 393 | error = -EINVAL; |
| 394 | goto err_free_pdata; |
| 395 | } |
| 396 | |
| 397 | return pdata; |
| 398 | |
| 399 | err_free_pdata: |
| 400 | kfree(pdata); |
| 401 | err_out: |
| 402 | return ERR_PTR(error); |
| 403 | } |
| 404 | |
| 405 | static struct of_device_id gpio_keys_polled_of_match[] = { |
| 406 | { .compatible = "gpio-keys-polled", }, |
| 407 | { }, |
| 408 | }; |
| 409 | MODULE_DEVICE_TABLE(of, gpio_keys_polled_of_match); |
| 410 | |
| 411 | #else |
| 412 | |
| 413 | static inline struct gpio_keys_platform_data * |
| 414 | gpio_keys_polled_get_devtree_pdata(struct device *dev) |
| 415 | { |
| 416 | return NULL; |
| 417 | } |
| 418 | #endif |
| 419 | |
| 420 | static void __devexit gpio_keys_polled_close(struct gpio_keys_polled_dev *bdev) |
| 421 | { |
| 422 | struct gpio_keys_platform_data *pdata = bdev->pdata; |
| 423 | |
| 424 | cancel_delayed_work_sync(&bdev->work); |
| 425 | |
| 426 | if (pdata->disable) |
| 427 | pdata->disable(bdev->dev); |
| 428 | } |
| 429 | |
| 430 | static int __devinit gpio_keys_polled_probe(struct platform_device *pdev) |
| 431 | { |
| 432 | struct gpio_keys_platform_data *pdata = pdev->dev.platform_data; |
| 433 | struct device *dev = &pdev->dev; |
| 434 | struct gpio_keys_polled_dev *bdev; |
| 435 | int error; |
| 436 | int i; |
| 437 | |
| 438 | if (!pdata) { |
| 439 | pdata = gpio_keys_polled_get_devtree_pdata(dev); |
| 440 | if (IS_ERR(pdata)) |
| 441 | return PTR_ERR(pdata); |
| 442 | if (!pdata) { |
| 443 | dev_err(dev, "missing platform data\n"); |
| 444 | return -EINVAL; |
| 445 | } |
| 446 | } |
| 447 | |
| 448 | if (!pdata->poll_interval) { |
| 449 | dev_err(dev, "missing poll_interval value\n"); |
| 450 | error = -EINVAL; |
| 451 | goto err_free_pdata; |
| 452 | } |
| 453 | |
| 454 | bdev = kzalloc(sizeof(struct gpio_keys_polled_dev) + |
| 455 | pdata->nbuttons * sizeof(struct gpio_keys_button_data), |
| 456 | GFP_KERNEL); |
| 457 | if (!bdev) { |
| 458 | dev_err(dev, "no memory for private data\n"); |
| 459 | return -ENOMEM; |
| 460 | } |
| 461 | |
| 462 | for (i = 0; i < pdata->nbuttons; i++) { |
| 463 | struct gpio_keys_button *button = &pdata->buttons[i]; |
| 464 | struct gpio_keys_button_data *bdata = &bdev->data[i]; |
| 465 | unsigned int gpio = button->gpio; |
| 466 | |
| 467 | if (button->wakeup) { |
| 468 | dev_err(dev, DRV_NAME " does not support wakeup\n"); |
| 469 | error = -EINVAL; |
| 470 | goto err_free_gpio; |
| 471 | } |
| 472 | |
| 473 | error = gpio_request(gpio, |
| 474 | button->desc ? button->desc : DRV_NAME); |
| 475 | if (error) { |
| 476 | dev_err(dev, "unable to claim gpio %u, err=%d\n", |
| 477 | gpio, error); |
| 478 | goto err_free_gpio; |
| 479 | } |
| 480 | |
| 481 | error = gpio_direction_input(gpio); |
| 482 | if (error) { |
| 483 | dev_err(dev, |
| 484 | "unable to set direction on gpio %u, err=%d\n", |
| 485 | gpio, error); |
| 486 | goto err_free_gpio; |
| 487 | } |
| 488 | |
| 489 | bdata->can_sleep = gpio_cansleep(gpio); |
| 490 | bdata->last_state = 0; |
| 491 | bdata->threshold = DIV_ROUND_UP(button->debounce_interval, |
| 492 | pdata->poll_interval); |
| 493 | } |
| 494 | |
| 495 | bdev->dev = &pdev->dev; |
| 496 | bdev->pdata = pdata; |
| 497 | platform_set_drvdata(pdev, bdev); |
| 498 | |
| 499 | INIT_DELAYED_WORK(&bdev->work, gpio_keys_polled_poll); |
| 500 | |
| 501 | gpio_keys_polled_open(bdev); |
| 502 | |
| 503 | return 0; |
| 504 | |
| 505 | err_free_gpio: |
| 506 | while (--i >= 0) |
| 507 | gpio_free(pdata->buttons[i].gpio); |
| 508 | |
| 509 | kfree(bdev); |
| 510 | platform_set_drvdata(pdev, NULL); |
| 511 | |
| 512 | err_free_pdata: |
| 513 | /* If we have no platform_data, we allocated pdata dynamically. */ |
| 514 | if (!dev_get_platdata(&pdev->dev)) |
| 515 | kfree(pdata); |
| 516 | |
| 517 | return error; |
| 518 | } |
| 519 | |
| 520 | static int __devexit gpio_keys_polled_remove(struct platform_device *pdev) |
| 521 | { |
| 522 | struct gpio_keys_polled_dev *bdev = platform_get_drvdata(pdev); |
| 523 | struct gpio_keys_platform_data *pdata = bdev->pdata; |
| 524 | int i = pdata->nbuttons; |
| 525 | |
| 526 | gpio_keys_polled_close(bdev); |
| 527 | |
| 528 | while (--i >= 0) |
| 529 | gpio_free(pdata->buttons[i].gpio); |
| 530 | |
| 531 | kfree(bdev); |
| 532 | platform_set_drvdata(pdev, NULL); |
| 533 | |
| 534 | return 0; |
| 535 | } |
| 536 | |
| 537 | static struct platform_driver gpio_keys_polled_driver = { |
| 538 | .probe = gpio_keys_polled_probe, |
| 539 | .remove = __devexit_p(gpio_keys_polled_remove), |
| 540 | .driver = { |
| 541 | .name = DRV_NAME, |
| 542 | .owner = THIS_MODULE, |
| 543 | .of_match_table = of_match_ptr(gpio_keys_polled_of_match), |
| 544 | }, |
| 545 | }; |
| 546 | |
| 547 | static int __init gpio_keys_polled_init(void) |
| 548 | { |
| 549 | return platform_driver_register(&gpio_keys_polled_driver); |
| 550 | } |
| 551 | |
| 552 | static void __exit gpio_keys_polled_exit(void) |
| 553 | { |
| 554 | platform_driver_unregister(&gpio_keys_polled_driver); |
| 555 | } |
| 556 | |
| 557 | module_init(gpio_keys_polled_init); |
| 558 | module_exit(gpio_keys_polled_exit); |
| 559 | |
| 560 | MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>"); |
| 561 | MODULE_AUTHOR("Felix Fietkau <nbd@openwrt.org>"); |
| 562 | MODULE_DESCRIPTION("Polled GPIO Buttons hotplug driver"); |
| 563 | MODULE_LICENSE("GPL v2"); |
| 564 | MODULE_ALIAS("platform:" DRV_NAME); |
| 565 | |