0001
0002
0003
0004
0005
0006
0007
0008 #include <linux/module.h>
0009 #include <linux/kernel.h>
0010 #include <linux/sched/signal.h>
0011 #include <linux/interrupt.h>
0012 #include <linux/time.h>
0013 #include <linux/timer.h>
0014 #include <linux/fs.h>
0015 #include <linux/miscdevice.h>
0016 #include <linux/string.h>
0017 #include <linux/errno.h>
0018 #include <linux/init.h>
0019
0020 #include <linux/uaccess.h>
0021 #include <asm/irq.h>
0022 #include <asm/mach-types.h>
0023
0024 #define __NWBUTTON_C
0025 #include "nwbutton.h"
0026
0027 static void button_sequence_finished(struct timer_list *unused);
0028
0029 static int button_press_count;
0030
0031 static DEFINE_TIMER(button_timer, button_sequence_finished);
0032 static DECLARE_WAIT_QUEUE_HEAD(button_wait_queue);
0033 static char button_output_buffer[32];
0034 static int bcount;
0035 static int bdelay = BUTTON_DELAY;
0036 static struct button_callback button_callback_list[32];
0037 static int callback_count;
0038 static int reboot_count = NUM_PRESSES_REBOOT;
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057 int button_add_callback (void (*callback) (void), int count)
0058 {
0059 int lp = 0;
0060 if (callback_count == 32) {
0061 return -ENOMEM;
0062 }
0063 if (!callback) {
0064 return -EINVAL;
0065 }
0066 callback_count++;
0067 for (; (button_callback_list [lp].callback); lp++);
0068 button_callback_list [lp].callback = callback;
0069 button_callback_list [lp].count = count;
0070 return 0;
0071 }
0072
0073
0074
0075
0076
0077
0078
0079
0080
0081
0082
0083
0084
0085 int button_del_callback (void (*callback) (void))
0086 {
0087 int lp = 31;
0088 if (!callback) {
0089 return -EINVAL;
0090 }
0091 while (lp >= 0) {
0092 if ((button_callback_list [lp].callback) == callback) {
0093 button_callback_list [lp].callback = NULL;
0094 button_callback_list [lp].count = 0;
0095 callback_count--;
0096 return 0;
0097 }
0098 lp--;
0099 }
0100 return -EINVAL;
0101 }
0102
0103
0104
0105
0106
0107
0108
0109
0110
0111 static void button_consume_callbacks (int bpcount)
0112 {
0113 int lp = 0;
0114 for (; lp <= 31; lp++) {
0115 if ((button_callback_list [lp].count) == bpcount) {
0116 if (button_callback_list [lp].callback) {
0117 button_callback_list[lp].callback();
0118 }
0119 }
0120 }
0121 }
0122
0123
0124
0125
0126
0127
0128
0129
0130
0131 static void button_sequence_finished(struct timer_list *unused)
0132 {
0133 if (IS_ENABLED(CONFIG_NWBUTTON_REBOOT) &&
0134 button_press_count == reboot_count)
0135 kill_cad_pid(SIGINT, 1);
0136 button_consume_callbacks (button_press_count);
0137 bcount = sprintf (button_output_buffer, "%d\n", button_press_count);
0138 button_press_count = 0;
0139 wake_up_interruptible (&button_wait_queue);
0140 }
0141
0142
0143
0144
0145
0146
0147
0148
0149
0150 static irqreturn_t button_handler (int irq, void *dev_id)
0151 {
0152 button_press_count++;
0153 mod_timer(&button_timer, jiffies + bdelay);
0154
0155 return IRQ_HANDLED;
0156 }
0157
0158
0159
0160
0161
0162
0163
0164
0165
0166
0167
0168 static int button_read (struct file *filp, char __user *buffer,
0169 size_t count, loff_t *ppos)
0170 {
0171 DEFINE_WAIT(wait);
0172 prepare_to_wait(&button_wait_queue, &wait, TASK_INTERRUPTIBLE);
0173 schedule();
0174 finish_wait(&button_wait_queue, &wait);
0175 return (copy_to_user (buffer, &button_output_buffer, bcount))
0176 ? -EFAULT : bcount;
0177 }
0178
0179
0180
0181
0182
0183
0184
0185 static const struct file_operations button_fops = {
0186 .owner = THIS_MODULE,
0187 .read = button_read,
0188 .llseek = noop_llseek,
0189 };
0190
0191
0192
0193
0194
0195
0196
0197 static struct miscdevice button_misc_device = {
0198 BUTTON_MINOR,
0199 "nwbutton",
0200 &button_fops,
0201 };
0202
0203
0204
0205
0206
0207
0208
0209
0210
0211
0212 static int __init nwbutton_init(void)
0213 {
0214 if (!machine_is_netwinder())
0215 return -ENODEV;
0216
0217 printk (KERN_INFO "NetWinder Button Driver Version %s (C) Alex Holden "
0218 "<alex@linuxhacker.org> 1998.\n", VERSION);
0219
0220 if (misc_register (&button_misc_device)) {
0221 printk (KERN_WARNING "nwbutton: Couldn't register device 10, "
0222 "%d.\n", BUTTON_MINOR);
0223 return -EBUSY;
0224 }
0225
0226 if (request_irq (IRQ_NETWINDER_BUTTON, button_handler, 0,
0227 "nwbutton", NULL)) {
0228 printk (KERN_WARNING "nwbutton: IRQ %d is not free.\n",
0229 IRQ_NETWINDER_BUTTON);
0230 misc_deregister (&button_misc_device);
0231 return -EIO;
0232 }
0233 return 0;
0234 }
0235
0236 static void __exit nwbutton_exit (void)
0237 {
0238 free_irq (IRQ_NETWINDER_BUTTON, NULL);
0239 misc_deregister (&button_misc_device);
0240 }
0241
0242
0243 MODULE_AUTHOR("Alex Holden");
0244 MODULE_LICENSE("GPL");
0245
0246 module_init(nwbutton_init);
0247 module_exit(nwbutton_exit);