nwbutton.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * NetWinder Button Driver-
  4. * Copyright (C) Alex Holden <alex@linuxhacker.org> 1998, 1999.
  5. *
  6. */
  7. #include <linux/module.h>
  8. #include <linux/kernel.h>
  9. #include <linux/sched/signal.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/time.h>
  12. #include <linux/timer.h>
  13. #include <linux/fs.h>
  14. #include <linux/miscdevice.h>
  15. #include <linux/string.h>
  16. #include <linux/errno.h>
  17. #include <linux/init.h>
  18. #include <linux/uaccess.h>
  19. #include <asm/irq.h>
  20. #include <asm/mach-types.h>
  21. #define __NWBUTTON_C /* Tell the header file who we are */
  22. #include "nwbutton.h"
  23. static void button_sequence_finished(struct timer_list *unused);
  24. static int button_press_count; /* The count of button presses */
  25. /* Times for the end of a sequence */
  26. static DEFINE_TIMER(button_timer, button_sequence_finished);
  27. static DECLARE_WAIT_QUEUE_HEAD(button_wait_queue); /* Used for blocking read */
  28. static char button_output_buffer[32]; /* Stores data to write out of device */
  29. static int bcount; /* The number of bytes in the buffer */
  30. static int bdelay = BUTTON_DELAY; /* The delay, in jiffies */
  31. static struct button_callback button_callback_list[32]; /* The callback list */
  32. static int callback_count; /* The number of callbacks registered */
  33. static int reboot_count = NUM_PRESSES_REBOOT; /* Number of presses to reboot */
  34. /*
  35. * This function is called by other drivers to register a callback function
  36. * to be called when a particular number of button presses occurs.
  37. * The callback list is a static array of 32 entries (I somehow doubt many
  38. * people are ever going to want to register more than 32 different actions
  39. * to be performed by the kernel on different numbers of button presses ;).
  40. * However, if an attempt to register a 33rd entry (perhaps a stuck loop
  41. * somewhere registering the same entry over and over?) it will fail to
  42. * do so and return -ENOMEM. If an attempt is made to register a null pointer,
  43. * it will fail to do so and return -EINVAL.
  44. * Because callbacks can be unregistered at random the list can become
  45. * fragmented, so we need to search through the list until we find the first
  46. * free entry.
  47. *
  48. * FIXME: Has anyone spotted any locking functions int his code recently ??
  49. */
  50. int button_add_callback (void (*callback) (void), int count)
  51. {
  52. int lp = 0;
  53. if (callback_count == 32) {
  54. return -ENOMEM;
  55. }
  56. if (!callback) {
  57. return -EINVAL;
  58. }
  59. callback_count++;
  60. for (; (button_callback_list [lp].callback); lp++);
  61. button_callback_list [lp].callback = callback;
  62. button_callback_list [lp].count = count;
  63. return 0;
  64. }
  65. /*
  66. * This function is called by other drivers to deregister a callback function.
  67. * If you attempt to unregister a callback which does not exist, it will fail
  68. * with -EINVAL. If there is more than one entry with the same address,
  69. * because it searches the list from end to beginning, it will unregister the
  70. * last one to be registered first (FILO- First In Last Out).
  71. * Note that this is not necessarily true if the entries are not submitted
  72. * at the same time, because another driver could have unregistered a callback
  73. * between the submissions creating a gap earlier in the list, which would
  74. * be filled first at submission time.
  75. */
  76. int button_del_callback (void (*callback) (void))
  77. {
  78. int lp = 31;
  79. if (!callback) {
  80. return -EINVAL;
  81. }
  82. while (lp >= 0) {
  83. if ((button_callback_list [lp].callback) == callback) {
  84. button_callback_list [lp].callback = NULL;
  85. button_callback_list [lp].count = 0;
  86. callback_count--;
  87. return 0;
  88. }
  89. lp--;
  90. }
  91. return -EINVAL;
  92. }
  93. /*
  94. * This function is called by button_sequence_finished to search through the
  95. * list of callback functions, and call any of them whose count argument
  96. * matches the current count of button presses. It starts at the beginning
  97. * of the list and works up to the end. It will refuse to follow a null
  98. * pointer (which should never happen anyway).
  99. */
  100. static void button_consume_callbacks (int bpcount)
  101. {
  102. int lp = 0;
  103. for (; lp <= 31; lp++) {
  104. if ((button_callback_list [lp].count) == bpcount) {
  105. if (button_callback_list [lp].callback) {
  106. button_callback_list[lp].callback();
  107. }
  108. }
  109. }
  110. }
  111. /*
  112. * This function is called when the button_timer times out.
  113. * ie. When you don't press the button for bdelay jiffies, this is taken to
  114. * mean you have ended the sequence of key presses, and this function is
  115. * called to wind things up (write the press_count out to /dev/button, call
  116. * any matching registered function callbacks, initiate reboot, etc.).
  117. */
  118. static void button_sequence_finished(struct timer_list *unused)
  119. {
  120. if (IS_ENABLED(CONFIG_NWBUTTON_REBOOT) &&
  121. button_press_count == reboot_count)
  122. kill_cad_pid(SIGINT, 1); /* Ask init to reboot us */
  123. button_consume_callbacks (button_press_count);
  124. bcount = sprintf (button_output_buffer, "%d\n", button_press_count);
  125. button_press_count = 0; /* Reset the button press counter */
  126. wake_up_interruptible (&button_wait_queue);
  127. }
  128. /*
  129. * This handler is called when the orange button is pressed (GPIO 10 of the
  130. * SuperIO chip, which maps to logical IRQ 26). If the press_count is 0,
  131. * this is the first press, so it starts a timer and increments the counter.
  132. * If it is higher than 0, it deletes the old timer, starts a new one, and
  133. * increments the counter.
  134. */
  135. static irqreturn_t button_handler (int irq, void *dev_id)
  136. {
  137. button_press_count++;
  138. mod_timer(&button_timer, jiffies + bdelay);
  139. return IRQ_HANDLED;
  140. }
  141. /*
  142. * This function is called when a user space program attempts to read
  143. * /dev/nwbutton. It puts the device to sleep on the wait queue until
  144. * button_sequence_finished writes some data to the buffer and flushes
  145. * the queue, at which point it writes the data out to the device and
  146. * returns the number of characters it has written. This function is
  147. * reentrant, so that many processes can be attempting to read from the
  148. * device at any one time.
  149. */
  150. static int button_read (struct file *filp, char __user *buffer,
  151. size_t count, loff_t *ppos)
  152. {
  153. DEFINE_WAIT(wait);
  154. prepare_to_wait(&button_wait_queue, &wait, TASK_INTERRUPTIBLE);
  155. schedule();
  156. finish_wait(&button_wait_queue, &wait);
  157. return (copy_to_user (buffer, &button_output_buffer, bcount))
  158. ? -EFAULT : bcount;
  159. }
  160. /*
  161. * This structure is the file operations structure, which specifies what
  162. * callbacks functions the kernel should call when a user mode process
  163. * attempts to perform these operations on the device.
  164. */
  165. static const struct file_operations button_fops = {
  166. .owner = THIS_MODULE,
  167. .read = button_read,
  168. .llseek = noop_llseek,
  169. };
  170. /*
  171. * This structure is the misc device structure, which specifies the minor
  172. * device number (158 in this case), the name of the device (for /proc/misc),
  173. * and the address of the above file operations structure.
  174. */
  175. static struct miscdevice button_misc_device = {
  176. BUTTON_MINOR,
  177. "nwbutton",
  178. &button_fops,
  179. };
  180. /*
  181. * This function is called to initialise the driver, either from misc.c at
  182. * bootup if the driver is compiled into the kernel, or from init_module
  183. * below at module insert time. It attempts to register the device node
  184. * and the IRQ and fails with a warning message if either fails, though
  185. * neither ever should because the device number and IRQ are unique to
  186. * this driver.
  187. */
  188. static int __init nwbutton_init(void)
  189. {
  190. if (!machine_is_netwinder())
  191. return -ENODEV;
  192. printk (KERN_INFO "NetWinder Button Driver Version %s (C) Alex Holden "
  193. "<alex@linuxhacker.org> 1998.\n", VERSION);
  194. if (misc_register (&button_misc_device)) {
  195. printk (KERN_WARNING "nwbutton: Couldn't register device 10, "
  196. "%d.\n", BUTTON_MINOR);
  197. return -EBUSY;
  198. }
  199. if (request_irq (IRQ_NETWINDER_BUTTON, button_handler, 0,
  200. "nwbutton", NULL)) {
  201. printk (KERN_WARNING "nwbutton: IRQ %d is not free.\n",
  202. IRQ_NETWINDER_BUTTON);
  203. misc_deregister (&button_misc_device);
  204. return -EIO;
  205. }
  206. return 0;
  207. }
  208. static void __exit nwbutton_exit (void)
  209. {
  210. free_irq (IRQ_NETWINDER_BUTTON, NULL);
  211. misc_deregister (&button_misc_device);
  212. }
  213. MODULE_AUTHOR("Alex Holden");
  214. MODULE_LICENSE("GPL");
  215. module_init(nwbutton_init);
  216. module_exit(nwbutton_exit);