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