1
2
3
4
5
6
7#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
24#include "nwbutton.h"
25
26static void button_sequence_finished (unsigned long parameters);
27
28static int button_press_count;
29
30static DEFINE_TIMER(button_timer, button_sequence_finished, 0, 0);
31static DECLARE_WAIT_QUEUE_HEAD(button_wait_queue);
32static char button_output_buffer[32];
33static int bcount;
34static int bdelay = BUTTON_DELAY;
35static struct button_callback button_callback_list[32];
36static int callback_count;
37static int reboot_count = NUM_PRESSES_REBOOT;
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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
74
75
76
77
78
79
80
81
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
104
105
106
107
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
124
125
126
127
128
129
130static void button_sequence_finished (unsigned long parameters)
131{
132 if (IS_ENABLED(CONFIG_NWBUTTON_REBOOT) &&
133 button_press_count == reboot_count)
134 kill_cad_pid(SIGINT, 1);
135 button_consume_callbacks (button_press_count);
136 bcount = sprintf (button_output_buffer, "%d\n", button_press_count);
137 button_press_count = 0;
138 wake_up_interruptible (&button_wait_queue);
139}
140
141
142
143
144
145
146
147
148
149static irqreturn_t button_handler (int irq, void *dev_id)
150{
151 button_press_count++;
152 mod_timer(&button_timer, jiffies + bdelay);
153
154 return IRQ_HANDLED;
155}
156
157
158
159
160
161
162
163
164
165
166
167static int button_read (struct file *filp, char __user *buffer,
168 size_t count, loff_t *ppos)
169{
170 DEFINE_WAIT(wait);
171 prepare_to_wait(&button_wait_queue, &wait, TASK_INTERRUPTIBLE);
172 schedule();
173 finish_wait(&button_wait_queue, &wait);
174 return (copy_to_user (buffer, &button_output_buffer, bcount))
175 ? -EFAULT : bcount;
176}
177
178
179
180
181
182
183
184static const struct file_operations button_fops = {
185 .owner = THIS_MODULE,
186 .read = button_read,
187 .llseek = noop_llseek,
188};
189
190
191
192
193
194
195
196static struct miscdevice button_misc_device = {
197 BUTTON_MINOR,
198 "nwbutton",
199 &button_fops,
200};
201
202
203
204
205
206
207
208
209
210
211static int __init nwbutton_init(void)
212{
213 if (!machine_is_netwinder())
214 return -ENODEV;
215
216 printk (KERN_INFO "NetWinder Button Driver Version %s (C) Alex Holden "
217 "<alex@linuxhacker.org> 1998.\n", VERSION);
218
219 if (misc_register (&button_misc_device)) {
220 printk (KERN_WARNING "nwbutton: Couldn't register device 10, "
221 "%d.\n", BUTTON_MINOR);
222 return -EBUSY;
223 }
224
225 if (request_irq (IRQ_NETWINDER_BUTTON, button_handler, 0,
226 "nwbutton", NULL)) {
227 printk (KERN_WARNING "nwbutton: IRQ %d is not free.\n",
228 IRQ_NETWINDER_BUTTON);
229 misc_deregister (&button_misc_device);
230 return -EIO;
231 }
232 return 0;
233}
234
235static void __exit nwbutton_exit (void)
236{
237 free_irq (IRQ_NETWINDER_BUTTON, NULL);
238 misc_deregister (&button_misc_device);
239}
240
241
242MODULE_AUTHOR("Alex Holden");
243MODULE_LICENSE("GPL");
244
245module_init(nwbutton_init);
246module_exit(nwbutton_exit);
247