Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * NetWinder Button Driver- |
| 3 | * Copyright (C) Alex Holden <alex@linuxhacker.org> 1998, 1999. |
| 4 | * |
| 5 | */ |
| 6 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 /* Tell the header file who we are */ |
| 24 | #include "nwbutton.h" |
| 25 | |
| 26 | static int button_press_count; /* The count of button presses */ |
| 27 | static struct timer_list button_timer; /* Times for the end of a sequence */ |
| 28 | static DECLARE_WAIT_QUEUE_HEAD(button_wait_queue); /* Used for blocking read */ |
| 29 | static char button_output_buffer[32]; /* Stores data to write out of device */ |
| 30 | static int bcount; /* The number of bytes in the buffer */ |
| 31 | static int bdelay = BUTTON_DELAY; /* The delay, in jiffies */ |
| 32 | static struct button_callback button_callback_list[32]; /* The callback list */ |
| 33 | static int callback_count; /* The number of callbacks registered */ |
| 34 | static int reboot_count = NUM_PRESSES_REBOOT; /* Number of presses to reboot */ |
| 35 | |
| 36 | /* |
| 37 | * This function is called by other drivers to register a callback function |
| 38 | * to be called when a particular number of button presses occurs. |
| 39 | * The callback list is a static array of 32 entries (I somehow doubt many |
| 40 | * people are ever going to want to register more than 32 different actions |
| 41 | * to be performed by the kernel on different numbers of button presses ;). |
| 42 | * However, if an attempt to register a 33rd entry (perhaps a stuck loop |
| 43 | * somewhere registering the same entry over and over?) it will fail to |
| 44 | * do so and return -ENOMEM. If an attempt is made to register a null pointer, |
| 45 | * it will fail to do so and return -EINVAL. |
| 46 | * Because callbacks can be unregistered at random the list can become |
| 47 | * fragmented, so we need to search through the list until we find the first |
| 48 | * free entry. |
| 49 | * |
| 50 | * FIXME: Has anyone spotted any locking functions int his code recently ?? |
| 51 | */ |
| 52 | |
| 53 | int button_add_callback (void (*callback) (void), int count) |
| 54 | { |
| 55 | int lp = 0; |
| 56 | if (callback_count == 32) { |
| 57 | return -ENOMEM; |
| 58 | } |
| 59 | if (!callback) { |
| 60 | return -EINVAL; |
| 61 | } |
| 62 | callback_count++; |
| 63 | for (; (button_callback_list [lp].callback); lp++); |
| 64 | button_callback_list [lp].callback = callback; |
| 65 | button_callback_list [lp].count = count; |
| 66 | return 0; |
| 67 | } |
| 68 | |
| 69 | /* |
| 70 | * This function is called by other drivers to deregister a callback function. |
| 71 | * If you attempt to unregister a callback which does not exist, it will fail |
| 72 | * with -EINVAL. If there is more than one entry with the same address, |
| 73 | * because it searches the list from end to beginning, it will unregister the |
| 74 | * last one to be registered first (FILO- First In Last Out). |
| 75 | * Note that this is not neccessarily true if the entries are not submitted |
| 76 | * at the same time, because another driver could have unregistered a callback |
| 77 | * between the submissions creating a gap earlier in the list, which would |
| 78 | * be filled first at submission time. |
| 79 | */ |
| 80 | |
| 81 | int button_del_callback (void (*callback) (void)) |
| 82 | { |
| 83 | int lp = 31; |
| 84 | if (!callback) { |
| 85 | return -EINVAL; |
| 86 | } |
| 87 | while (lp >= 0) { |
| 88 | if ((button_callback_list [lp].callback) == callback) { |
| 89 | button_callback_list [lp].callback = NULL; |
| 90 | button_callback_list [lp].count = 0; |
| 91 | callback_count--; |
| 92 | return 0; |
| 93 | }; |
| 94 | lp--; |
| 95 | }; |
| 96 | return -EINVAL; |
| 97 | } |
| 98 | |
| 99 | /* |
| 100 | * This function is called by button_sequence_finished to search through the |
| 101 | * list of callback functions, and call any of them whose count argument |
| 102 | * matches the current count of button presses. It starts at the beginning |
| 103 | * of the list and works up to the end. It will refuse to follow a null |
| 104 | * pointer (which should never happen anyway). |
| 105 | */ |
| 106 | |
| 107 | static void button_consume_callbacks (int bpcount) |
| 108 | { |
| 109 | int lp = 0; |
| 110 | for (; lp <= 31; lp++) { |
| 111 | if ((button_callback_list [lp].count) == bpcount) { |
| 112 | if (button_callback_list [lp].callback) { |
| 113 | button_callback_list[lp].callback(); |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | /* |
| 120 | * This function is called when the button_timer times out. |
| 121 | * ie. When you don't press the button for bdelay jiffies, this is taken to |
| 122 | * mean you have ended the sequence of key presses, and this function is |
| 123 | * called to wind things up (write the press_count out to /dev/button, call |
| 124 | * any matching registered function callbacks, initiate reboot, etc.). |
| 125 | */ |
| 126 | |
| 127 | static void button_sequence_finished (unsigned long parameters) |
| 128 | { |
| 129 | #ifdef CONFIG_NWBUTTON_REBOOT /* Reboot using button is enabled */ |
| 130 | if (button_press_count == reboot_count) { |
| 131 | kill_proc (1, SIGINT, 1); /* Ask init to reboot us */ |
| 132 | } |
| 133 | #endif /* CONFIG_NWBUTTON_REBOOT */ |
| 134 | button_consume_callbacks (button_press_count); |
| 135 | bcount = sprintf (button_output_buffer, "%d\n", button_press_count); |
| 136 | button_press_count = 0; /* Reset the button press counter */ |
| 137 | wake_up_interruptible (&button_wait_queue); |
| 138 | } |
| 139 | |
| 140 | /* |
| 141 | * This handler is called when the orange button is pressed (GPIO 10 of the |
| 142 | * SuperIO chip, which maps to logical IRQ 26). If the press_count is 0, |
| 143 | * this is the first press, so it starts a timer and increments the counter. |
| 144 | * If it is higher than 0, it deletes the old timer, starts a new one, and |
| 145 | * increments the counter. |
| 146 | */ |
| 147 | |
| 148 | static irqreturn_t button_handler (int irq, void *dev_id, struct pt_regs *regs) |
| 149 | { |
| 150 | if (button_press_count) { |
| 151 | del_timer (&button_timer); |
| 152 | } |
| 153 | button_press_count++; |
| 154 | init_timer (&button_timer); |
| 155 | button_timer.function = button_sequence_finished; |
| 156 | button_timer.expires = (jiffies + bdelay); |
| 157 | add_timer (&button_timer); |
| 158 | |
| 159 | return IRQ_HANDLED; |
| 160 | } |
| 161 | |
| 162 | /* |
| 163 | * This function is called when a user space program attempts to read |
| 164 | * /dev/nwbutton. It puts the device to sleep on the wait queue until |
| 165 | * button_sequence_finished writes some data to the buffer and flushes |
| 166 | * the queue, at which point it writes the data out to the device and |
| 167 | * returns the number of characters it has written. This function is |
| 168 | * reentrant, so that many processes can be attempting to read from the |
| 169 | * device at any one time. |
| 170 | */ |
| 171 | |
| 172 | static int button_read (struct file *filp, char __user *buffer, |
| 173 | size_t count, loff_t *ppos) |
| 174 | { |
| 175 | interruptible_sleep_on (&button_wait_queue); |
| 176 | return (copy_to_user (buffer, &button_output_buffer, bcount)) |
| 177 | ? -EFAULT : bcount; |
| 178 | } |
| 179 | |
| 180 | /* |
| 181 | * This structure is the file operations structure, which specifies what |
| 182 | * callbacks functions the kernel should call when a user mode process |
| 183 | * attempts to perform these operations on the device. |
| 184 | */ |
| 185 | |
Arjan van de Ven | 62322d2 | 2006-07-03 00:24:21 -0700 | [diff] [blame] | 186 | static const struct file_operations button_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | .owner = THIS_MODULE, |
| 188 | .read = button_read, |
| 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 | |
Thomas Gleixner | 0f2ed4c | 2006-07-01 19:29:33 -0700 | [diff] [blame] | 226 | if (request_irq (IRQ_NETWINDER_BUTTON, button_handler, IRQF_DISABLED, |
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); |