/drivers/input/power.c

https://bitbucket.org/abioy/linux · C · 174 lines · 115 code · 25 blank · 34 comment · 18 complexity · 197f75aadc4c82ef2ac3e173e0283b5c MD5 · raw file

  1. /*
  2. * $Id: power.c,v 1.10 2001/09/25 09:17:15 vojtech Exp $
  3. *
  4. * Copyright (c) 2001 "Crazy" James Simmons
  5. *
  6. * Input driver Power Management.
  7. *
  8. * Sponsored by Transvirtual Technology.
  9. */
  10. /*
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  24. *
  25. * Should you need to contact me, the author, you can do so by
  26. * e-mail - mail your message to <jsimmons@transvirtual.com>.
  27. */
  28. #include <linux/module.h>
  29. #include <linux/config.h>
  30. #include <linux/input.h>
  31. #include <linux/slab.h>
  32. #include <linux/init.h>
  33. #include <linux/tty.h>
  34. #include <linux/delay.h>
  35. #include <linux/pm.h>
  36. static struct input_handler power_handler;
  37. /*
  38. * Power management can't be done in a interrupt context. So we have to
  39. * use keventd.
  40. */
  41. static int suspend_button_pushed = 0;
  42. static void suspend_button_task_handler(void *data)
  43. {
  44. udelay(200); /* debounce */
  45. suspend_button_pushed = 0;
  46. }
  47. static DECLARE_WORK(suspend_button_task, suspend_button_task_handler, NULL);
  48. static void power_event(struct input_handle *handle, unsigned int type,
  49. unsigned int code, int down)
  50. {
  51. struct input_dev *dev = handle->dev;
  52. printk("Entering power_event\n");
  53. if (type != EV_KEY || type != EV_PWR) return;
  54. if (type == EV_PWR) {
  55. switch (code) {
  56. case KEY_SUSPEND:
  57. printk("Powering down entire device\n");
  58. if (!suspend_button_pushed) {
  59. suspend_button_pushed = 1;
  60. schedule_work(&suspend_button_task);
  61. }
  62. break;
  63. case KEY_POWER:
  64. /* Hum power down the machine. */
  65. break;
  66. default:
  67. return;
  68. }
  69. } else {
  70. switch (code) {
  71. case KEY_SUSPEND:
  72. printk("Powering down input device\n");
  73. /* This is risky. See pm.h for details. */
  74. if (dev->state != PM_RESUME)
  75. dev->state = PM_RESUME;
  76. else
  77. dev->state = PM_SUSPEND;
  78. pm_send(dev->pm_dev, dev->state, dev);
  79. break;
  80. case KEY_POWER:
  81. /* Turn the input device off completely ? */
  82. break;
  83. default:
  84. return;
  85. }
  86. }
  87. return;
  88. }
  89. static struct input_handle *power_connect(struct input_handler *handler,
  90. struct input_dev *dev,
  91. struct input_device_id *id)
  92. {
  93. struct input_handle *handle;
  94. if (!test_bit(EV_KEY, dev->evbit) || !test_bit(EV_PWR, dev->evbit))
  95. return NULL;
  96. if (!test_bit(KEY_SUSPEND, dev->keybit) || (!test_bit(KEY_POWER, dev->keybit)))
  97. return NULL;
  98. if (!(handle = kmalloc(sizeof(struct input_handle), GFP_KERNEL)))
  99. return NULL;
  100. memset(handle, 0, sizeof(struct input_handle));
  101. handle->dev = dev;
  102. handle->handler = handler;
  103. input_open_device(handle);
  104. printk(KERN_INFO "power.c: Adding power management to input layer\n");
  105. return handle;
  106. }
  107. static void power_disconnect(struct input_handle *handle)
  108. {
  109. input_close_device(handle);
  110. kfree(handle);
  111. }
  112. static struct input_device_id power_ids[] = {
  113. {
  114. .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT,
  115. .evbit = { BIT(EV_KEY) },
  116. .keybit = { [LONG(KEY_SUSPEND)] = BIT(KEY_SUSPEND) }
  117. },
  118. {
  119. .flags = INPUT_DEVICE_ID_MATCH_EVBIT | INPUT_DEVICE_ID_MATCH_KEYBIT,
  120. .evbit = { BIT(EV_KEY) },
  121. .keybit = { [LONG(KEY_POWER)] = BIT(KEY_POWER) }
  122. },
  123. {
  124. .flags = INPUT_DEVICE_ID_MATCH_EVBIT,
  125. .evbit = { BIT(EV_PWR) },
  126. },
  127. { }, /* Terminating entry */
  128. };
  129. MODULE_DEVICE_TABLE(input, power_ids);
  130. static struct input_handler power_handler = {
  131. .event = power_event,
  132. .connect = power_connect,
  133. .disconnect = power_disconnect,
  134. .name = "power",
  135. .id_table = power_ids,
  136. };
  137. static int __init power_init(void)
  138. {
  139. input_register_handler(&power_handler);
  140. return 0;
  141. }
  142. static void __exit power_exit(void)
  143. {
  144. input_unregister_handler(&power_handler);
  145. }
  146. module_init(power_init);
  147. module_exit(power_exit);
  148. MODULE_AUTHOR("James Simmons <jsimmons@transvirtual.com>");
  149. MODULE_DESCRIPTION("Input Power Management driver");