/*1* NetWinder Button Driver-2* Copyright (C) Alex Holden <[email protected]> 1998, 1999.3*4*/56#include <linux/module.h>7#include <linux/kernel.h>8#include <linux/sched.h>9#include <linux/interrupt.h>10#include <linux/time.h>11#include <linux/timer.h>12#include <linux/fs.h>13#include <linux/miscdevice.h>14#include <linux/string.h>15#include <linux/errno.h>16#include <linux/init.h>1718#include <asm/uaccess.h>19#include <asm/irq.h>20#include <asm/mach-types.h>2122#define __NWBUTTON_C /* Tell the header file who we are */23#include "nwbutton.h"2425static void button_sequence_finished (unsigned long parameters);2627static int button_press_count; /* The count of button presses */28/* Times for the end of a sequence */29static DEFINE_TIMER(button_timer, button_sequence_finished, 0, 0);30static DECLARE_WAIT_QUEUE_HEAD(button_wait_queue); /* Used for blocking read */31static char button_output_buffer[32]; /* Stores data to write out of device */32static int bcount; /* The number of bytes in the buffer */33static int bdelay = BUTTON_DELAY; /* The delay, in jiffies */34static struct button_callback button_callback_list[32]; /* The callback list */35static int callback_count; /* The number of callbacks registered */36static int reboot_count = NUM_PRESSES_REBOOT; /* Number of presses to reboot */3738/*39* This function is called by other drivers to register a callback function40* to be called when a particular number of button presses occurs.41* The callback list is a static array of 32 entries (I somehow doubt many42* people are ever going to want to register more than 32 different actions43* to be performed by the kernel on different numbers of button presses ;).44* However, if an attempt to register a 33rd entry (perhaps a stuck loop45* somewhere registering the same entry over and over?) it will fail to46* do so and return -ENOMEM. If an attempt is made to register a null pointer,47* it will fail to do so and return -EINVAL.48* Because callbacks can be unregistered at random the list can become49* fragmented, so we need to search through the list until we find the first50* free entry.51*52* FIXME: Has anyone spotted any locking functions int his code recently ??53*/5455int button_add_callback (void (*callback) (void), int count)56{57int lp = 0;58if (callback_count == 32) {59return -ENOMEM;60}61if (!callback) {62return -EINVAL;63}64callback_count++;65for (; (button_callback_list [lp].callback); lp++);66button_callback_list [lp].callback = callback;67button_callback_list [lp].count = count;68return 0;69}7071/*72* This function is called by other drivers to deregister a callback function.73* If you attempt to unregister a callback which does not exist, it will fail74* with -EINVAL. If there is more than one entry with the same address,75* because it searches the list from end to beginning, it will unregister the76* last one to be registered first (FILO- First In Last Out).77* Note that this is not necessarily true if the entries are not submitted78* at the same time, because another driver could have unregistered a callback79* between the submissions creating a gap earlier in the list, which would80* be filled first at submission time.81*/8283int button_del_callback (void (*callback) (void))84{85int lp = 31;86if (!callback) {87return -EINVAL;88}89while (lp >= 0) {90if ((button_callback_list [lp].callback) == callback) {91button_callback_list [lp].callback = NULL;92button_callback_list [lp].count = 0;93callback_count--;94return 0;95};96lp--;97};98return -EINVAL;99}100101/*102* This function is called by button_sequence_finished to search through the103* list of callback functions, and call any of them whose count argument104* matches the current count of button presses. It starts at the beginning105* of the list and works up to the end. It will refuse to follow a null106* pointer (which should never happen anyway).107*/108109static void button_consume_callbacks (int bpcount)110{111int lp = 0;112for (; lp <= 31; lp++) {113if ((button_callback_list [lp].count) == bpcount) {114if (button_callback_list [lp].callback) {115button_callback_list[lp].callback();116}117}118}119}120121/*122* This function is called when the button_timer times out.123* ie. When you don't press the button for bdelay jiffies, this is taken to124* mean you have ended the sequence of key presses, and this function is125* called to wind things up (write the press_count out to /dev/button, call126* any matching registered function callbacks, initiate reboot, etc.).127*/128129static void button_sequence_finished (unsigned long parameters)130{131#ifdef CONFIG_NWBUTTON_REBOOT /* Reboot using button is enabled */132if (button_press_count == reboot_count)133kill_cad_pid(SIGINT, 1); /* Ask init to reboot us */134#endif /* CONFIG_NWBUTTON_REBOOT */135button_consume_callbacks (button_press_count);136bcount = sprintf (button_output_buffer, "%d\n", button_press_count);137button_press_count = 0; /* Reset the button press counter */138wake_up_interruptible (&button_wait_queue);139}140141/*142* This handler is called when the orange button is pressed (GPIO 10 of the143* SuperIO chip, which maps to logical IRQ 26). If the press_count is 0,144* this is the first press, so it starts a timer and increments the counter.145* If it is higher than 0, it deletes the old timer, starts a new one, and146* increments the counter.147*/148149static irqreturn_t button_handler (int irq, void *dev_id)150{151button_press_count++;152mod_timer(&button_timer, jiffies + bdelay);153154return IRQ_HANDLED;155}156157/*158* This function is called when a user space program attempts to read159* /dev/nwbutton. It puts the device to sleep on the wait queue until160* button_sequence_finished writes some data to the buffer and flushes161* the queue, at which point it writes the data out to the device and162* returns the number of characters it has written. This function is163* reentrant, so that many processes can be attempting to read from the164* device at any one time.165*/166167static int button_read (struct file *filp, char __user *buffer,168size_t count, loff_t *ppos)169{170interruptible_sleep_on (&button_wait_queue);171return (copy_to_user (buffer, &button_output_buffer, bcount))172? -EFAULT : bcount;173}174175/*176* This structure is the file operations structure, which specifies what177* callbacks functions the kernel should call when a user mode process178* attempts to perform these operations on the device.179*/180181static const struct file_operations button_fops = {182.owner = THIS_MODULE,183.read = button_read,184.llseek = noop_llseek,185};186187/*188* This structure is the misc device structure, which specifies the minor189* device number (158 in this case), the name of the device (for /proc/misc),190* and the address of the above file operations structure.191*/192193static struct miscdevice button_misc_device = {194BUTTON_MINOR,195"nwbutton",196&button_fops,197};198199/*200* This function is called to initialise the driver, either from misc.c at201* bootup if the driver is compiled into the kernel, or from init_module202* below at module insert time. It attempts to register the device node203* and the IRQ and fails with a warning message if either fails, though204* neither ever should because the device number and IRQ are unique to205* this driver.206*/207208static int __init nwbutton_init(void)209{210if (!machine_is_netwinder())211return -ENODEV;212213printk (KERN_INFO "NetWinder Button Driver Version %s (C) Alex Holden "214"<[email protected]> 1998.\n", VERSION);215216if (misc_register (&button_misc_device)) {217printk (KERN_WARNING "nwbutton: Couldn't register device 10, "218"%d.\n", BUTTON_MINOR);219return -EBUSY;220}221222if (request_irq (IRQ_NETWINDER_BUTTON, button_handler, IRQF_DISABLED,223"nwbutton", NULL)) {224printk (KERN_WARNING "nwbutton: IRQ %d is not free.\n",225IRQ_NETWINDER_BUTTON);226misc_deregister (&button_misc_device);227return -EIO;228}229return 0;230}231232static void __exit nwbutton_exit (void)233{234free_irq (IRQ_NETWINDER_BUTTON, NULL);235misc_deregister (&button_misc_device);236}237238239MODULE_AUTHOR("Alex Holden");240MODULE_LICENSE("GPL");241242module_init(nwbutton_init);243module_exit(nwbutton_exit);244245246