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