xref: /openbmc/linux/drivers/char/nwbutton.c (revision 0f2ed4c6)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * 	NetWinder Button Driver-
31da177e4SLinus Torvalds  *	Copyright (C) Alex Holden <alex@linuxhacker.org> 1998, 1999.
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  */
61da177e4SLinus Torvalds 
71da177e4SLinus Torvalds #include <linux/module.h>
81da177e4SLinus Torvalds #include <linux/kernel.h>
91da177e4SLinus Torvalds #include <linux/sched.h>
101da177e4SLinus Torvalds #include <linux/interrupt.h>
111da177e4SLinus Torvalds #include <linux/time.h>
121da177e4SLinus Torvalds #include <linux/timer.h>
131da177e4SLinus Torvalds #include <linux/fs.h>
141da177e4SLinus Torvalds #include <linux/miscdevice.h>
151da177e4SLinus Torvalds #include <linux/string.h>
161da177e4SLinus Torvalds #include <linux/errno.h>
171da177e4SLinus Torvalds #include <linux/init.h>
181da177e4SLinus Torvalds 
191da177e4SLinus Torvalds #include <asm/uaccess.h>
201da177e4SLinus Torvalds #include <asm/irq.h>
211da177e4SLinus Torvalds #include <asm/mach-types.h>
221da177e4SLinus Torvalds 
231da177e4SLinus Torvalds #define __NWBUTTON_C		/* Tell the header file who we are */
241da177e4SLinus Torvalds #include "nwbutton.h"
251da177e4SLinus Torvalds 
261da177e4SLinus Torvalds static int button_press_count;		/* The count of button presses */
271da177e4SLinus Torvalds static struct timer_list button_timer;	/* Times for the end of a sequence */
281da177e4SLinus Torvalds static DECLARE_WAIT_QUEUE_HEAD(button_wait_queue); /* Used for blocking read */
291da177e4SLinus Torvalds static char button_output_buffer[32];	/* Stores data to write out of device */
301da177e4SLinus Torvalds static int bcount;			/* The number of bytes in the buffer */
311da177e4SLinus Torvalds static int bdelay = BUTTON_DELAY;	/* The delay, in jiffies */
321da177e4SLinus Torvalds static struct button_callback button_callback_list[32]; /* The callback list */
331da177e4SLinus Torvalds static int callback_count;		/* The number of callbacks registered */
341da177e4SLinus Torvalds static int reboot_count = NUM_PRESSES_REBOOT; /* Number of presses to reboot */
351da177e4SLinus Torvalds 
361da177e4SLinus Torvalds /*
371da177e4SLinus Torvalds  * This function is called by other drivers to register a callback function
381da177e4SLinus Torvalds  * to be called when a particular number of button presses occurs.
391da177e4SLinus Torvalds  * The callback list is a static array of 32 entries (I somehow doubt many
401da177e4SLinus Torvalds  * people are ever going to want to register more than 32 different actions
411da177e4SLinus Torvalds  * to be performed by the kernel on different numbers of button presses ;).
421da177e4SLinus Torvalds  * However, if an attempt to register a 33rd entry (perhaps a stuck loop
431da177e4SLinus Torvalds  * somewhere registering the same entry over and over?) it will fail to
441da177e4SLinus Torvalds  * do so and return -ENOMEM. If an attempt is made to register a null pointer,
451da177e4SLinus Torvalds  * it will fail to do so and return -EINVAL.
461da177e4SLinus Torvalds  * Because callbacks can be unregistered at random the list can become
471da177e4SLinus Torvalds  * fragmented, so we need to search through the list until we find the first
481da177e4SLinus Torvalds  * free entry.
491da177e4SLinus Torvalds  *
501da177e4SLinus Torvalds  * FIXME: Has anyone spotted any locking functions int his code recently ??
511da177e4SLinus Torvalds  */
521da177e4SLinus Torvalds 
531da177e4SLinus Torvalds int button_add_callback (void (*callback) (void), int count)
541da177e4SLinus Torvalds {
551da177e4SLinus Torvalds 	int lp = 0;
561da177e4SLinus Torvalds 	if (callback_count == 32) {
571da177e4SLinus Torvalds 		return -ENOMEM;
581da177e4SLinus Torvalds 	}
591da177e4SLinus Torvalds 	if (!callback) {
601da177e4SLinus Torvalds 		return -EINVAL;
611da177e4SLinus Torvalds 	}
621da177e4SLinus Torvalds 	callback_count++;
631da177e4SLinus Torvalds 	for (; (button_callback_list [lp].callback); lp++);
641da177e4SLinus Torvalds 	button_callback_list [lp].callback = callback;
651da177e4SLinus Torvalds 	button_callback_list [lp].count = count;
661da177e4SLinus Torvalds 	return 0;
671da177e4SLinus Torvalds }
681da177e4SLinus Torvalds 
691da177e4SLinus Torvalds /*
701da177e4SLinus Torvalds  * This function is called by other drivers to deregister a callback function.
711da177e4SLinus Torvalds  * If you attempt to unregister a callback which does not exist, it will fail
721da177e4SLinus Torvalds  * with -EINVAL. If there is more than one entry with the same address,
731da177e4SLinus Torvalds  * because it searches the list from end to beginning, it will unregister the
741da177e4SLinus Torvalds  * last one to be registered first (FILO- First In Last Out).
751da177e4SLinus Torvalds  * Note that this is not neccessarily true if the entries are not submitted
761da177e4SLinus Torvalds  * at the same time, because another driver could have unregistered a callback
771da177e4SLinus Torvalds  * between the submissions creating a gap earlier in the list, which would
781da177e4SLinus Torvalds  * be filled first at submission time.
791da177e4SLinus Torvalds  */
801da177e4SLinus Torvalds 
811da177e4SLinus Torvalds int button_del_callback (void (*callback) (void))
821da177e4SLinus Torvalds {
831da177e4SLinus Torvalds 	int lp = 31;
841da177e4SLinus Torvalds 	if (!callback) {
851da177e4SLinus Torvalds 		return -EINVAL;
861da177e4SLinus Torvalds 	}
871da177e4SLinus Torvalds 	while (lp >= 0) {
881da177e4SLinus Torvalds 		if ((button_callback_list [lp].callback) == callback) {
891da177e4SLinus Torvalds 			button_callback_list [lp].callback = NULL;
901da177e4SLinus Torvalds 			button_callback_list [lp].count = 0;
911da177e4SLinus Torvalds 			callback_count--;
921da177e4SLinus Torvalds 			return 0;
931da177e4SLinus Torvalds 		};
941da177e4SLinus Torvalds 		lp--;
951da177e4SLinus Torvalds 	};
961da177e4SLinus Torvalds 	return -EINVAL;
971da177e4SLinus Torvalds }
981da177e4SLinus Torvalds 
991da177e4SLinus Torvalds /*
1001da177e4SLinus Torvalds  * This function is called by button_sequence_finished to search through the
1011da177e4SLinus Torvalds  * list of callback functions, and call any of them whose count argument
1021da177e4SLinus Torvalds  * matches the current count of button presses. It starts at the beginning
1031da177e4SLinus Torvalds  * of the list and works up to the end. It will refuse to follow a null
1041da177e4SLinus Torvalds  * pointer (which should never happen anyway).
1051da177e4SLinus Torvalds  */
1061da177e4SLinus Torvalds 
1071da177e4SLinus Torvalds static void button_consume_callbacks (int bpcount)
1081da177e4SLinus Torvalds {
1091da177e4SLinus Torvalds 	int lp = 0;
1101da177e4SLinus Torvalds 	for (; lp <= 31; lp++) {
1111da177e4SLinus Torvalds 		if ((button_callback_list [lp].count) == bpcount) {
1121da177e4SLinus Torvalds 			if (button_callback_list [lp].callback) {
1131da177e4SLinus Torvalds 				button_callback_list[lp].callback();
1141da177e4SLinus Torvalds 			}
1151da177e4SLinus Torvalds 		}
1161da177e4SLinus Torvalds 	}
1171da177e4SLinus Torvalds }
1181da177e4SLinus Torvalds 
1191da177e4SLinus Torvalds /*
1201da177e4SLinus Torvalds  * This function is called when the button_timer times out.
1211da177e4SLinus Torvalds  * ie. When you don't press the button for bdelay jiffies, this is taken to
1221da177e4SLinus Torvalds  * mean you have ended the sequence of key presses, and this function is
1231da177e4SLinus Torvalds  * called to wind things up (write the press_count out to /dev/button, call
1241da177e4SLinus Torvalds  * any matching registered function callbacks, initiate reboot, etc.).
1251da177e4SLinus Torvalds  */
1261da177e4SLinus Torvalds 
1271da177e4SLinus Torvalds static void button_sequence_finished (unsigned long parameters)
1281da177e4SLinus Torvalds {
1291da177e4SLinus Torvalds #ifdef CONFIG_NWBUTTON_REBOOT		/* Reboot using button is enabled */
1301da177e4SLinus Torvalds 	if (button_press_count == reboot_count) {
1311da177e4SLinus Torvalds 		kill_proc (1, SIGINT, 1);	/* Ask init to reboot us */
1321da177e4SLinus Torvalds 	}
1331da177e4SLinus Torvalds #endif /* CONFIG_NWBUTTON_REBOOT */
1341da177e4SLinus Torvalds 	button_consume_callbacks (button_press_count);
1351da177e4SLinus Torvalds 	bcount = sprintf (button_output_buffer, "%d\n", button_press_count);
1361da177e4SLinus Torvalds 	button_press_count = 0;		/* Reset the button press counter */
1371da177e4SLinus Torvalds 	wake_up_interruptible (&button_wait_queue);
1381da177e4SLinus Torvalds }
1391da177e4SLinus Torvalds 
1401da177e4SLinus Torvalds /*
1411da177e4SLinus Torvalds  *  This handler is called when the orange button is pressed (GPIO 10 of the
1421da177e4SLinus Torvalds  *  SuperIO chip, which maps to logical IRQ 26). If the press_count is 0,
1431da177e4SLinus Torvalds  *  this is the first press, so it starts a timer and increments the counter.
1441da177e4SLinus Torvalds  *  If it is higher than 0, it deletes the old timer, starts a new one, and
1451da177e4SLinus Torvalds  *  increments the counter.
1461da177e4SLinus Torvalds  */
1471da177e4SLinus Torvalds 
1481da177e4SLinus Torvalds static irqreturn_t button_handler (int irq, void *dev_id, struct pt_regs *regs)
1491da177e4SLinus Torvalds {
1501da177e4SLinus Torvalds 	if (button_press_count) {
1511da177e4SLinus Torvalds 		del_timer (&button_timer);
1521da177e4SLinus Torvalds 	}
1531da177e4SLinus Torvalds 	button_press_count++;
1541da177e4SLinus Torvalds 	init_timer (&button_timer);
1551da177e4SLinus Torvalds 	button_timer.function = button_sequence_finished;
1561da177e4SLinus Torvalds 	button_timer.expires = (jiffies + bdelay);
1571da177e4SLinus Torvalds 	add_timer (&button_timer);
1581da177e4SLinus Torvalds 
1591da177e4SLinus Torvalds 	return IRQ_HANDLED;
1601da177e4SLinus Torvalds }
1611da177e4SLinus Torvalds 
1621da177e4SLinus Torvalds /*
1631da177e4SLinus Torvalds  * This function is called when a user space program attempts to read
1641da177e4SLinus Torvalds  * /dev/nwbutton. It puts the device to sleep on the wait queue until
1651da177e4SLinus Torvalds  * button_sequence_finished writes some data to the buffer and flushes
1661da177e4SLinus Torvalds  * the queue, at which point it writes the data out to the device and
1671da177e4SLinus Torvalds  * returns the number of characters it has written. This function is
1681da177e4SLinus Torvalds  * reentrant, so that many processes can be attempting to read from the
1691da177e4SLinus Torvalds  * device at any one time.
1701da177e4SLinus Torvalds  */
1711da177e4SLinus Torvalds 
1721da177e4SLinus Torvalds static int button_read (struct file *filp, char __user *buffer,
1731da177e4SLinus Torvalds 			size_t count, loff_t *ppos)
1741da177e4SLinus Torvalds {
1751da177e4SLinus Torvalds 	interruptible_sleep_on (&button_wait_queue);
1761da177e4SLinus Torvalds 	return (copy_to_user (buffer, &button_output_buffer, bcount))
1771da177e4SLinus Torvalds 		 ? -EFAULT : bcount;
1781da177e4SLinus Torvalds }
1791da177e4SLinus Torvalds 
1801da177e4SLinus Torvalds /*
1811da177e4SLinus Torvalds  * This structure is the file operations structure, which specifies what
1821da177e4SLinus Torvalds  * callbacks functions the kernel should call when a user mode process
1831da177e4SLinus Torvalds  * attempts to perform these operations on the device.
1841da177e4SLinus Torvalds  */
1851da177e4SLinus Torvalds 
1861da177e4SLinus Torvalds static struct file_operations button_fops = {
1871da177e4SLinus Torvalds 	.owner		= THIS_MODULE,
1881da177e4SLinus Torvalds 	.read		= button_read,
1891da177e4SLinus Torvalds };
1901da177e4SLinus Torvalds 
1911da177e4SLinus Torvalds /*
1921da177e4SLinus Torvalds  * This structure is the misc device structure, which specifies the minor
1931da177e4SLinus Torvalds  * device number (158 in this case), the name of the device (for /proc/misc),
1941da177e4SLinus Torvalds  * and the address of the above file operations structure.
1951da177e4SLinus Torvalds  */
1961da177e4SLinus Torvalds 
1971da177e4SLinus Torvalds static struct miscdevice button_misc_device = {
1981da177e4SLinus Torvalds 	BUTTON_MINOR,
1991da177e4SLinus Torvalds 	"nwbutton",
2001da177e4SLinus Torvalds 	&button_fops,
2011da177e4SLinus Torvalds };
2021da177e4SLinus Torvalds 
2031da177e4SLinus Torvalds /*
2041da177e4SLinus Torvalds  * This function is called to initialise the driver, either from misc.c at
2051da177e4SLinus Torvalds  * bootup if the driver is compiled into the kernel, or from init_module
2061da177e4SLinus Torvalds  * below at module insert time. It attempts to register the device node
2071da177e4SLinus Torvalds  * and the IRQ and fails with a warning message if either fails, though
2081da177e4SLinus Torvalds  * neither ever should because the device number and IRQ are unique to
2091da177e4SLinus Torvalds  * this driver.
2101da177e4SLinus Torvalds  */
2111da177e4SLinus Torvalds 
2121da177e4SLinus Torvalds static int __init nwbutton_init(void)
2131da177e4SLinus Torvalds {
2141da177e4SLinus Torvalds 	if (!machine_is_netwinder())
2151da177e4SLinus Torvalds 		return -ENODEV;
2161da177e4SLinus Torvalds 
2171da177e4SLinus Torvalds 	printk (KERN_INFO "NetWinder Button Driver Version %s (C) Alex Holden "
2181da177e4SLinus Torvalds 			"<alex@linuxhacker.org> 1998.\n", VERSION);
2191da177e4SLinus Torvalds 
2201da177e4SLinus Torvalds 	if (misc_register (&button_misc_device)) {
2211da177e4SLinus Torvalds 		printk (KERN_WARNING "nwbutton: Couldn't register device 10, "
2221da177e4SLinus Torvalds 				"%d.\n", BUTTON_MINOR);
2231da177e4SLinus Torvalds 		return -EBUSY;
2241da177e4SLinus Torvalds 	}
2251da177e4SLinus Torvalds 
2260f2ed4c6SThomas Gleixner 	if (request_irq (IRQ_NETWINDER_BUTTON, button_handler, IRQF_DISABLED,
2271da177e4SLinus Torvalds 			"nwbutton", NULL)) {
2281da177e4SLinus Torvalds 		printk (KERN_WARNING "nwbutton: IRQ %d is not free.\n",
2291da177e4SLinus Torvalds 				IRQ_NETWINDER_BUTTON);
2301da177e4SLinus Torvalds 		misc_deregister (&button_misc_device);
2311da177e4SLinus Torvalds 		return -EIO;
2321da177e4SLinus Torvalds 	}
2331da177e4SLinus Torvalds 	return 0;
2341da177e4SLinus Torvalds }
2351da177e4SLinus Torvalds 
2361da177e4SLinus Torvalds static void __exit nwbutton_exit (void)
2371da177e4SLinus Torvalds {
2381da177e4SLinus Torvalds 	free_irq (IRQ_NETWINDER_BUTTON, NULL);
2391da177e4SLinus Torvalds 	misc_deregister (&button_misc_device);
2401da177e4SLinus Torvalds }
2411da177e4SLinus Torvalds 
2421da177e4SLinus Torvalds 
2431da177e4SLinus Torvalds MODULE_AUTHOR("Alex Holden");
2441da177e4SLinus Torvalds MODULE_LICENSE("GPL");
2451da177e4SLinus Torvalds 
2461da177e4SLinus Torvalds module_init(nwbutton_init);
2471da177e4SLinus Torvalds module_exit(nwbutton_exit);
248