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