Portability cleanup as required by Linus.
[linux-2.6/linux-mips.git] / drivers / char / nwbutton.c
blob8cf8a15a7cd33cc4d5897ac23ac6620cd54dc40f
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 #define __NWBUTTON_C /* Tell the header file who we are */
23 #include "nwbutton.h"
25 static int button_press_count; /* The count of button presses */
26 static struct timer_list button_timer; /* Times for the end of a sequence */
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 */
36 * This function is called by other drivers to register a callback function
37 * to be called when a particular number of button presses occurs.
38 * The callback list is a static array of 32 entries (I somehow doubt many
39 * people are ever going to want to register more than 32 different actions
40 * to be performed by the kernel on different numbers of button presses ;).
41 * However, if an attempt to register a 33rd entry (perhaps a stuck loop
42 * somewhere registering the same entry over and over?) it will fail to
43 * do so and return -ENOMEM. If an attempt is made to register a null pointer,
44 * it will fail to do so and return -EINVAL.
45 * Because callbacks can be unregistered at random the list can become
46 * fragmented, so we need to search through the list until we find the first
47 * free entry.
50 int button_add_callback (void (*callback) (void), int count)
52 int lp = 0;
53 if (callback_count == 32) {
54 return -ENOMEM;
56 if (!callback) {
57 return -EINVAL;
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;
67 * This function is called by other drivers to deregister a callback function.
68 * If you attempt to unregister a callback which does not exist, it will fail
69 * with -EINVAL. If there is more than one entry with the same address,
70 * because it searches the list from end to beginning, it will unregister the
71 * last one to be registered first (FILO- First In Last Out).
72 * Note that this is not neccessarily true if the entries are not submitted
73 * at the same time, because another driver could have unregistered a callback
74 * between the submissions creating a gap earlier in the list, which would
75 * be filled first at submission time.
78 int button_del_callback (void (*callback) (void))
80 int lp = 31;
81 if (!callback) {
82 return -EINVAL;
84 while (lp >= 0) {
85 if ((button_callback_list [lp].callback) == callback) {
86 button_callback_list [lp].callback = NULL;
87 button_callback_list [lp].count = 0;
88 callback_count--;
89 return 0;
91 lp--;
93 return -EINVAL;
97 * This function is called by button_sequence_finished to search through the
98 * list of callback functions, and call any of them whose count argument
99 * matches the current count of button presses. It starts at the beginning
100 * of the list and works up to the end. It will refuse to follow a null
101 * pointer (which should never happen anyway).
104 static void button_consume_callbacks (int bpcount)
106 int lp = 0;
107 for (; lp <= 31; lp++) {
108 if ((button_callback_list [lp].count) == bpcount) {
109 if (button_callback_list [lp].callback) {
110 button_callback_list[lp].callback();
117 * This function is called when the button_timer times out.
118 * ie. When you don't press the button for bdelay jiffies, this is taken to
119 * mean you have ended the sequence of key presses, and this function is
120 * called to wind things up (write the press_count out to /dev/button, call
121 * any matching registered function callbacks, initiate reboot, etc.).
124 static void button_sequence_finished (unsigned long parameters)
126 #ifdef CONFIG_NWBUTTON_REBOOT /* Reboot using button is enabled */
127 if (button_press_count == reboot_count) {
128 kill_proc (1, SIGINT, 1); /* Ask init to reboot us */
130 #endif /* CONFIG_NWBUTTON_REBOOT */
131 button_consume_callbacks (button_press_count);
132 bcount = sprintf (button_output_buffer, "%d\n", button_press_count);
133 button_press_count = 0; /* Reset the button press counter */
134 wake_up_interruptible (&button_wait_queue);
138 * This handler is called when the orange button is pressed (GPIO 10 of the
139 * SuperIO chip, which maps to logical IRQ 26). If the press_count is 0,
140 * this is the first press, so it starts a timer and increments the counter.
141 * If it is higher than 0, it deletes the old timer, starts a new one, and
142 * increments the counter.
145 static void button_handler (int irq, void *dev_id, struct pt_regs *regs)
147 if (button_press_count) {
148 del_timer (&button_timer);
150 button_press_count++;
151 init_timer (&button_timer);
152 button_timer.function = button_sequence_finished;
153 button_timer.expires = (jiffies + bdelay);
154 add_timer (&button_timer);
158 * This function is called when a user space program attempts to read
159 * /dev/nwbutton. It puts the device to sleep on the wait queue until
160 * button_sequence_finished writes some data to the buffer and flushes
161 * the queue, at which point it writes the data out to the device and
162 * returns the number of characters it has written. This function is
163 * reentrant, so that many processes can be attempting to read from the
164 * device at any one time.
167 static int button_read (struct file *filp, char *buffer,
168 size_t count, loff_t *ppos)
170 interruptible_sleep_on (&button_wait_queue);
171 return (copy_to_user (buffer, &button_output_buffer, bcount))
172 ? -EFAULT : bcount;
176 * This structure is the file operations structure, which specifies what
177 * callbacks functions the kernel should call when a user mode process
178 * attempts to perform these operations on the device.
181 static struct file_operations button_fops = {
182 owner: THIS_MODULE,
183 read: button_read,
187 * This structure is the misc device structure, which specifies the minor
188 * device number (158 in this case), the name of the device (for /proc/misc),
189 * and the address of the above file operations structure.
192 static struct miscdevice button_misc_device = {
193 BUTTON_MINOR,
194 "nwbutton",
195 &button_fops,
199 * This function is called to initialise the driver, either from misc.c at
200 * bootup if the driver is compiled into the kernel, or from init_module
201 * below at module insert time. It attempts to register the device node
202 * and the IRQ and fails with a warning message if either fails, though
203 * neither ever should because the device number and IRQ are unique to
204 * this driver.
207 static int __init nwbutton_init(void)
209 if (!machine_is_netwinder())
210 return -ENODEV;
212 printk (KERN_INFO "NetWinder Button Driver Version %s (C) Alex Holden "
213 "<alex@linuxhacker.org> 1998.\n", VERSION);
215 if (misc_register (&button_misc_device)) {
216 printk (KERN_WARNING "nwbutton: Couldn't register device 10, "
217 "%d.\n", BUTTON_MINOR);
218 return -EBUSY;
221 if (request_irq (IRQ_NETWINDER_BUTTON, button_handler, SA_INTERRUPT,
222 "nwbutton", NULL)) {
223 printk (KERN_WARNING "nwbutton: IRQ %d is not free.\n",
224 IRQ_NETWINDER_BUTTON);
225 misc_deregister (&button_misc_device);
226 return -EIO;
228 return 0;
231 static void __exit nwbutton_exit (void)
233 free_irq (IRQ_NETWINDER_BUTTON, NULL);
234 misc_deregister (&button_misc_device);
237 EXPORT_NO_SYMBOLS;
239 module_init(nwbutton_init);
240 module_exit(nwbutton_exit);