Thomas Gleixner | 09c434b | 2019-05-19 13:08:20 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * NetWinder Button Driver- |
| 4 | * Copyright (C) Alex Holden <alex@linuxhacker.org> 1998, 1999. |
| 5 | * |
| 6 | */ |
| 7 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | #include <linux/module.h> |
| 9 | #include <linux/kernel.h> |
Guenter Roeck | bb35e45 | 2017-03-05 17:13:31 -0800 | [diff] [blame] | 10 | #include <linux/sched/signal.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 20 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Kees Cook | 24ed960 | 2017-08-28 11:28:21 -0700 | [diff] [blame] | 27 | static void button_sequence_finished(struct timer_list *unused); |
Jiri Slaby | 40565f1 | 2007-02-12 00:52:31 -0800 | [diff] [blame] | 28 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | static int button_press_count; /* The count of button presses */ |
Jiri Slaby | 40565f1 | 2007-02-12 00:52:31 -0800 | [diff] [blame] | 30 | /* Times for the end of a sequence */ |
Kees Cook | 1d27e3e | 2017-10-04 16:27:04 -0700 | [diff] [blame] | 31 | static DEFINE_TIMER(button_timer, button_sequence_finished); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | static DECLARE_WAIT_QUEUE_HEAD(button_wait_queue); /* Used for blocking read */ |
| 33 | static char button_output_buffer[32]; /* Stores data to write out of device */ |
| 34 | static int bcount; /* The number of bytes in the buffer */ |
| 35 | static int bdelay = BUTTON_DELAY; /* The delay, in jiffies */ |
| 36 | static struct button_callback button_callback_list[32]; /* The callback list */ |
| 37 | static int callback_count; /* The number of callbacks registered */ |
| 38 | static int reboot_count = NUM_PRESSES_REBOOT; /* Number of presses to reboot */ |
| 39 | |
| 40 | /* |
| 41 | * This function is called by other drivers to register a callback function |
| 42 | * to be called when a particular number of button presses occurs. |
| 43 | * The callback list is a static array of 32 entries (I somehow doubt many |
| 44 | * people are ever going to want to register more than 32 different actions |
| 45 | * to be performed by the kernel on different numbers of button presses ;). |
| 46 | * However, if an attempt to register a 33rd entry (perhaps a stuck loop |
| 47 | * somewhere registering the same entry over and over?) it will fail to |
| 48 | * do so and return -ENOMEM. If an attempt is made to register a null pointer, |
| 49 | * it will fail to do so and return -EINVAL. |
| 50 | * Because callbacks can be unregistered at random the list can become |
| 51 | * fragmented, so we need to search through the list until we find the first |
| 52 | * free entry. |
| 53 | * |
| 54 | * FIXME: Has anyone spotted any locking functions int his code recently ?? |
| 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 | * This function is called by other drivers to deregister a callback function. |
| 75 | * If you attempt to unregister a callback which does not exist, it will fail |
| 76 | * with -EINVAL. If there is more than one entry with the same address, |
| 77 | * because it searches the list from end to beginning, it will unregister the |
| 78 | * last one to be registered first (FILO- First In Last Out). |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 79 | * Note that this is not necessarily true if the entries are not submitted |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | * at the same time, because another driver could have unregistered a callback |
| 81 | * between the submissions creating a gap earlier in the list, which would |
| 82 | * be filled first at submission time. |
| 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; |
Peter Senna Tschudin | f8885c2 | 2012-09-12 19:03:18 +0200 | [diff] [blame] | 97 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | lp--; |
Peter Senna Tschudin | f8885c2 | 2012-09-12 19:03:18 +0200 | [diff] [blame] | 99 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | return -EINVAL; |
| 101 | } |
| 102 | |
| 103 | /* |
| 104 | * This function is called by button_sequence_finished to search through the |
| 105 | * list of callback functions, and call any of them whose count argument |
| 106 | * matches the current count of button presses. It starts at the beginning |
| 107 | * of the list and works up to the end. It will refuse to follow a null |
| 108 | * pointer (which should never happen anyway). |
| 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 | * This function is called when the button_timer times out. |
| 125 | * ie. When you don't press the button for bdelay jiffies, this is taken to |
| 126 | * mean you have ended the sequence of key presses, and this function is |
| 127 | * called to wind things up (write the press_count out to /dev/button, call |
| 128 | * any matching registered function callbacks, initiate reboot, etc.). |
| 129 | */ |
| 130 | |
Kees Cook | 24ed960 | 2017-08-28 11:28:21 -0700 | [diff] [blame] | 131 | static void button_sequence_finished(struct timer_list *unused) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | { |
Arnd Bergmann | 990162f | 2016-01-25 16:54:25 +0100 | [diff] [blame] | 133 | if (IS_ENABLED(CONFIG_NWBUTTON_REBOOT) && |
| 134 | button_press_count == reboot_count) |
Cedric Le Goater | 9ec5209 | 2006-10-02 02:19:00 -0700 | [diff] [blame] | 135 | kill_cad_pid(SIGINT, 1); /* Ask init to reboot us */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | button_consume_callbacks (button_press_count); |
| 137 | bcount = sprintf (button_output_buffer, "%d\n", button_press_count); |
| 138 | button_press_count = 0; /* Reset the button press counter */ |
| 139 | wake_up_interruptible (&button_wait_queue); |
| 140 | } |
| 141 | |
| 142 | /* |
| 143 | * This handler is called when the orange button is pressed (GPIO 10 of the |
| 144 | * SuperIO chip, which maps to logical IRQ 26). If the press_count is 0, |
| 145 | * this is the first press, so it starts a timer and increments the counter. |
| 146 | * If it is higher than 0, it deletes the old timer, starts a new one, and |
| 147 | * increments the counter. |
| 148 | */ |
| 149 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 150 | static irqreturn_t button_handler (int irq, void *dev_id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | button_press_count++; |
Jiri Slaby | 40565f1 | 2007-02-12 00:52:31 -0800 | [diff] [blame] | 153 | mod_timer(&button_timer, jiffies + bdelay); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | |
| 155 | return IRQ_HANDLED; |
| 156 | } |
| 157 | |
| 158 | /* |
| 159 | * This function is called when a user space program attempts to read |
| 160 | * /dev/nwbutton. It puts the device to sleep on the wait queue until |
| 161 | * button_sequence_finished writes some data to the buffer and flushes |
| 162 | * the queue, at which point it writes the data out to the device and |
| 163 | * returns the number of characters it has written. This function is |
| 164 | * reentrant, so that many processes can be attempting to read from the |
| 165 | * device at any one time. |
| 166 | */ |
| 167 | |
| 168 | static int button_read (struct file *filp, char __user *buffer, |
| 169 | size_t count, loff_t *ppos) |
| 170 | { |
Arnd Bergmann | eb83174 | 2014-01-02 13:07:53 +0100 | [diff] [blame] | 171 | DEFINE_WAIT(wait); |
| 172 | prepare_to_wait(&button_wait_queue, &wait, TASK_INTERRUPTIBLE); |
| 173 | schedule(); |
| 174 | finish_wait(&button_wait_queue, &wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | return (copy_to_user (buffer, &button_output_buffer, bcount)) |
| 176 | ? -EFAULT : bcount; |
| 177 | } |
| 178 | |
| 179 | /* |
| 180 | * This structure is the file operations structure, which specifies what |
| 181 | * callbacks functions the kernel should call when a user mode process |
| 182 | * attempts to perform these operations on the device. |
| 183 | */ |
| 184 | |
Arjan van de Ven | 62322d2 | 2006-07-03 00:24:21 -0700 | [diff] [blame] | 185 | static const struct file_operations button_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | .owner = THIS_MODULE, |
| 187 | .read = button_read, |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 188 | .llseek = noop_llseek, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | }; |
| 190 | |
| 191 | /* |
| 192 | * This structure is the misc device structure, which specifies the minor |
| 193 | * device number (158 in this case), the name of the device (for /proc/misc), |
| 194 | * and the address of the above file operations structure. |
| 195 | */ |
| 196 | |
| 197 | static struct miscdevice button_misc_device = { |
| 198 | BUTTON_MINOR, |
| 199 | "nwbutton", |
| 200 | &button_fops, |
| 201 | }; |
| 202 | |
| 203 | /* |
| 204 | * This function is called to initialise the driver, either from misc.c at |
| 205 | * bootup if the driver is compiled into the kernel, or from init_module |
| 206 | * below at module insert time. It attempts to register the device node |
| 207 | * and the IRQ and fails with a warning message if either fails, though |
| 208 | * neither ever should because the device number and IRQ are unique to |
| 209 | * this driver. |
| 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 | |
Michael Opdenacker | d88ed62 | 2013-10-13 06:14:42 +0200 | [diff] [blame] | 226 | if (request_irq (IRQ_NETWINDER_BUTTON, button_handler, 0, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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); |