This source file includes following definitions.
- button_add_callback
- button_del_callback
- button_consume_callbacks
- button_sequence_finished
- button_handler
- button_read
- nwbutton_init
- nwbutton_exit
1
2
3
4
5
6
7
8 #include <linux/module.h>
9 #include <linux/kernel.h>
10 #include <linux/sched/signal.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 <linux/uaccess.h>
21 #include <asm/irq.h>
22 #include <asm/mach-types.h>
23
24 #define __NWBUTTON_C
25 #include "nwbutton.h"
26
27 static void button_sequence_finished(struct timer_list *unused);
28
29 static int button_press_count;
30
31 static DEFINE_TIMER(button_timer, button_sequence_finished);
32 static DECLARE_WAIT_QUEUE_HEAD(button_wait_queue);
33 static char button_output_buffer[32];
34 static int bcount;
35 static int bdelay = BUTTON_DELAY;
36 static struct button_callback button_callback_list[32];
37 static int callback_count;
38 static int reboot_count = NUM_PRESSES_REBOOT;
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57 int button_add_callback (void (*callback) (void), int count)
58 {
59 int lp = 0;
60 if (callback_count == 32) {
61 return -ENOMEM;
62 }
63 if (!callback) {
64 return -EINVAL;
65 }
66 callback_count++;
67 for (; (button_callback_list [lp].callback); lp++);
68 button_callback_list [lp].callback = callback;
69 button_callback_list [lp].count = count;
70 return 0;
71 }
72
73
74
75
76
77
78
79
80
81
82
83
84
85 int button_del_callback (void (*callback) (void))
86 {
87 int lp = 31;
88 if (!callback) {
89 return -EINVAL;
90 }
91 while (lp >= 0) {
92 if ((button_callback_list [lp].callback) == callback) {
93 button_callback_list [lp].callback = NULL;
94 button_callback_list [lp].count = 0;
95 callback_count--;
96 return 0;
97 }
98 lp--;
99 }
100 return -EINVAL;
101 }
102
103
104
105
106
107
108
109
110
111 static void button_consume_callbacks (int bpcount)
112 {
113 int lp = 0;
114 for (; lp <= 31; lp++) {
115 if ((button_callback_list [lp].count) == bpcount) {
116 if (button_callback_list [lp].callback) {
117 button_callback_list[lp].callback();
118 }
119 }
120 }
121 }
122
123
124
125
126
127
128
129
130
131 static void button_sequence_finished(struct timer_list *unused)
132 {
133 if (IS_ENABLED(CONFIG_NWBUTTON_REBOOT) &&
134 button_press_count == reboot_count)
135 kill_cad_pid(SIGINT, 1);
136 button_consume_callbacks (button_press_count);
137 bcount = sprintf (button_output_buffer, "%d\n", button_press_count);
138 button_press_count = 0;
139 wake_up_interruptible (&button_wait_queue);
140 }
141
142
143
144
145
146
147
148
149
150 static irqreturn_t button_handler (int irq, void *dev_id)
151 {
152 button_press_count++;
153 mod_timer(&button_timer, jiffies + bdelay);
154
155 return IRQ_HANDLED;
156 }
157
158
159
160
161
162
163
164
165
166
167
168 static int button_read (struct file *filp, char __user *buffer,
169 size_t count, loff_t *ppos)
170 {
171 DEFINE_WAIT(wait);
172 prepare_to_wait(&button_wait_queue, &wait, TASK_INTERRUPTIBLE);
173 schedule();
174 finish_wait(&button_wait_queue, &wait);
175 return (copy_to_user (buffer, &button_output_buffer, bcount))
176 ? -EFAULT : bcount;
177 }
178
179
180
181
182
183
184
185 static const struct file_operations button_fops = {
186 .owner = THIS_MODULE,
187 .read = button_read,
188 .llseek = noop_llseek,
189 };
190
191
192
193
194
195
196
197 static struct miscdevice button_misc_device = {
198 BUTTON_MINOR,
199 "nwbutton",
200 &button_fops,
201 };
202
203
204
205
206
207
208
209
210
211
212 static int __init nwbutton_init(void)
213 {
214 if (!machine_is_netwinder())
215 return -ENODEV;
216
217 printk (KERN_INFO "NetWinder Button Driver Version %s (C) Alex Holden "
218 "<alex@linuxhacker.org> 1998.\n", VERSION);
219
220 if (misc_register (&button_misc_device)) {
221 printk (KERN_WARNING "nwbutton: Couldn't register device 10, "
222 "%d.\n", BUTTON_MINOR);
223 return -EBUSY;
224 }
225
226 if (request_irq (IRQ_NETWINDER_BUTTON, button_handler, 0,
227 "nwbutton", NULL)) {
228 printk (KERN_WARNING "nwbutton: IRQ %d is not free.\n",
229 IRQ_NETWINDER_BUTTON);
230 misc_deregister (&button_misc_device);
231 return -EIO;
232 }
233 return 0;
234 }
235
236 static void __exit nwbutton_exit (void)
237 {
238 free_irq (IRQ_NETWINDER_BUTTON, NULL);
239 misc_deregister (&button_misc_device);
240 }
241
242
243 MODULE_AUTHOR("Alex Holden");
244 MODULE_LICENSE("GPL");
245
246 module_init(nwbutton_init);
247 module_exit(nwbutton_exit);