nwbutton.c 7.9 KB

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