xref: /openbmc/linux/drivers/char/nwbutton.c (revision 24ed960a)
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>
9bb35e451SGuenter Roeck #include <linux/sched/signal.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 
197c0f6ba6SLinus Torvalds #include <linux/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 
2624ed960aSKees Cook static void button_sequence_finished(struct timer_list *unused);
2740565f19SJiri Slaby 
281da177e4SLinus Torvalds static int button_press_count;		/* The count of button presses */
2940565f19SJiri Slaby /* Times for the end of a sequence */
301d27e3e2SKees Cook static DEFINE_TIMER(button_timer, button_sequence_finished);
311da177e4SLinus Torvalds static DECLARE_WAIT_QUEUE_HEAD(button_wait_queue); /* Used for blocking read */
321da177e4SLinus Torvalds static char button_output_buffer[32];	/* Stores data to write out of device */
331da177e4SLinus Torvalds static int bcount;			/* The number of bytes in the buffer */
341da177e4SLinus Torvalds static int bdelay = BUTTON_DELAY;	/* The delay, in jiffies */
351da177e4SLinus Torvalds static struct button_callback button_callback_list[32]; /* The callback list */
361da177e4SLinus Torvalds static int callback_count;		/* The number of callbacks registered */
371da177e4SLinus Torvalds static int reboot_count = NUM_PRESSES_REBOOT; /* Number of presses to reboot */
381da177e4SLinus Torvalds 
391da177e4SLinus Torvalds /*
401da177e4SLinus Torvalds  * This function is called by other drivers to register a callback function
411da177e4SLinus Torvalds  * to be called when a particular number of button presses occurs.
421da177e4SLinus Torvalds  * The callback list is a static array of 32 entries (I somehow doubt many
431da177e4SLinus Torvalds  * people are ever going to want to register more than 32 different actions
441da177e4SLinus Torvalds  * to be performed by the kernel on different numbers of button presses ;).
451da177e4SLinus Torvalds  * However, if an attempt to register a 33rd entry (perhaps a stuck loop
461da177e4SLinus Torvalds  * somewhere registering the same entry over and over?) it will fail to
471da177e4SLinus Torvalds  * do so and return -ENOMEM. If an attempt is made to register a null pointer,
481da177e4SLinus Torvalds  * it will fail to do so and return -EINVAL.
491da177e4SLinus Torvalds  * Because callbacks can be unregistered at random the list can become
501da177e4SLinus Torvalds  * fragmented, so we need to search through the list until we find the first
511da177e4SLinus Torvalds  * free entry.
521da177e4SLinus Torvalds  *
531da177e4SLinus Torvalds  * FIXME: Has anyone spotted any locking functions int his code recently ??
541da177e4SLinus Torvalds  */
551da177e4SLinus Torvalds 
561da177e4SLinus Torvalds int button_add_callback (void (*callback) (void), int count)
571da177e4SLinus Torvalds {
581da177e4SLinus Torvalds 	int lp = 0;
591da177e4SLinus Torvalds 	if (callback_count == 32) {
601da177e4SLinus Torvalds 		return -ENOMEM;
611da177e4SLinus Torvalds 	}
621da177e4SLinus Torvalds 	if (!callback) {
631da177e4SLinus Torvalds 		return -EINVAL;
641da177e4SLinus Torvalds 	}
651da177e4SLinus Torvalds 	callback_count++;
661da177e4SLinus Torvalds 	for (; (button_callback_list [lp].callback); lp++);
671da177e4SLinus Torvalds 	button_callback_list [lp].callback = callback;
681da177e4SLinus Torvalds 	button_callback_list [lp].count = count;
691da177e4SLinus Torvalds 	return 0;
701da177e4SLinus Torvalds }
711da177e4SLinus Torvalds 
721da177e4SLinus Torvalds /*
731da177e4SLinus Torvalds  * This function is called by other drivers to deregister a callback function.
741da177e4SLinus Torvalds  * If you attempt to unregister a callback which does not exist, it will fail
751da177e4SLinus Torvalds  * with -EINVAL. If there is more than one entry with the same address,
761da177e4SLinus Torvalds  * because it searches the list from end to beginning, it will unregister the
771da177e4SLinus Torvalds  * last one to be registered first (FILO- First In Last Out).
7825985edcSLucas De Marchi  * Note that this is not necessarily true if the entries are not submitted
791da177e4SLinus Torvalds  * at the same time, because another driver could have unregistered a callback
801da177e4SLinus Torvalds  * between the submissions creating a gap earlier in the list, which would
811da177e4SLinus Torvalds  * be filled first at submission time.
821da177e4SLinus Torvalds  */
831da177e4SLinus Torvalds 
841da177e4SLinus Torvalds int button_del_callback (void (*callback) (void))
851da177e4SLinus Torvalds {
861da177e4SLinus Torvalds 	int lp = 31;
871da177e4SLinus Torvalds 	if (!callback) {
881da177e4SLinus Torvalds 		return -EINVAL;
891da177e4SLinus Torvalds 	}
901da177e4SLinus Torvalds 	while (lp >= 0) {
911da177e4SLinus Torvalds 		if ((button_callback_list [lp].callback) == callback) {
921da177e4SLinus Torvalds 			button_callback_list [lp].callback = NULL;
931da177e4SLinus Torvalds 			button_callback_list [lp].count = 0;
941da177e4SLinus Torvalds 			callback_count--;
951da177e4SLinus Torvalds 			return 0;
96f8885c26SPeter Senna Tschudin 		}
971da177e4SLinus Torvalds 		lp--;
98f8885c26SPeter Senna Tschudin 	}
991da177e4SLinus Torvalds 	return -EINVAL;
1001da177e4SLinus Torvalds }
1011da177e4SLinus Torvalds 
1021da177e4SLinus Torvalds /*
1031da177e4SLinus Torvalds  * This function is called by button_sequence_finished to search through the
1041da177e4SLinus Torvalds  * list of callback functions, and call any of them whose count argument
1051da177e4SLinus Torvalds  * matches the current count of button presses. It starts at the beginning
1061da177e4SLinus Torvalds  * of the list and works up to the end. It will refuse to follow a null
1071da177e4SLinus Torvalds  * pointer (which should never happen anyway).
1081da177e4SLinus Torvalds  */
1091da177e4SLinus Torvalds 
1101da177e4SLinus Torvalds static void button_consume_callbacks (int bpcount)
1111da177e4SLinus Torvalds {
1121da177e4SLinus Torvalds 	int lp = 0;
1131da177e4SLinus Torvalds 	for (; lp <= 31; lp++) {
1141da177e4SLinus Torvalds 		if ((button_callback_list [lp].count) == bpcount) {
1151da177e4SLinus Torvalds 			if (button_callback_list [lp].callback) {
1161da177e4SLinus Torvalds 				button_callback_list[lp].callback();
1171da177e4SLinus Torvalds 			}
1181da177e4SLinus Torvalds 		}
1191da177e4SLinus Torvalds 	}
1201da177e4SLinus Torvalds }
1211da177e4SLinus Torvalds 
1221da177e4SLinus Torvalds /*
1231da177e4SLinus Torvalds  * This function is called when the button_timer times out.
1241da177e4SLinus Torvalds  * ie. When you don't press the button for bdelay jiffies, this is taken to
1251da177e4SLinus Torvalds  * mean you have ended the sequence of key presses, and this function is
1261da177e4SLinus Torvalds  * called to wind things up (write the press_count out to /dev/button, call
1271da177e4SLinus Torvalds  * any matching registered function callbacks, initiate reboot, etc.).
1281da177e4SLinus Torvalds  */
1291da177e4SLinus Torvalds 
13024ed960aSKees Cook static void button_sequence_finished(struct timer_list *unused)
1311da177e4SLinus Torvalds {
132990162f0SArnd Bergmann 	if (IS_ENABLED(CONFIG_NWBUTTON_REBOOT) &&
133990162f0SArnd Bergmann 	    button_press_count == reboot_count)
1349ec52099SCedric Le Goater 		kill_cad_pid(SIGINT, 1);	/* Ask init to reboot us */
1351da177e4SLinus Torvalds 	button_consume_callbacks (button_press_count);
1361da177e4SLinus Torvalds 	bcount = sprintf (button_output_buffer, "%d\n", button_press_count);
1371da177e4SLinus Torvalds 	button_press_count = 0;		/* Reset the button press counter */
1381da177e4SLinus Torvalds 	wake_up_interruptible (&button_wait_queue);
1391da177e4SLinus Torvalds }
1401da177e4SLinus Torvalds 
1411da177e4SLinus Torvalds /*
1421da177e4SLinus Torvalds  *  This handler is called when the orange button is pressed (GPIO 10 of the
1431da177e4SLinus Torvalds  *  SuperIO chip, which maps to logical IRQ 26). If the press_count is 0,
1441da177e4SLinus Torvalds  *  this is the first press, so it starts a timer and increments the counter.
1451da177e4SLinus Torvalds  *  If it is higher than 0, it deletes the old timer, starts a new one, and
1461da177e4SLinus Torvalds  *  increments the counter.
1471da177e4SLinus Torvalds  */
1481da177e4SLinus Torvalds 
1497d12e780SDavid Howells static irqreturn_t button_handler (int irq, void *dev_id)
1501da177e4SLinus Torvalds {
1511da177e4SLinus Torvalds 	button_press_count++;
15240565f19SJiri Slaby 	mod_timer(&button_timer, jiffies + bdelay);
1531da177e4SLinus Torvalds 
1541da177e4SLinus Torvalds 	return IRQ_HANDLED;
1551da177e4SLinus Torvalds }
1561da177e4SLinus Torvalds 
1571da177e4SLinus Torvalds /*
1581da177e4SLinus Torvalds  * This function is called when a user space program attempts to read
1591da177e4SLinus Torvalds  * /dev/nwbutton. It puts the device to sleep on the wait queue until
1601da177e4SLinus Torvalds  * button_sequence_finished writes some data to the buffer and flushes
1611da177e4SLinus Torvalds  * the queue, at which point it writes the data out to the device and
1621da177e4SLinus Torvalds  * returns the number of characters it has written. This function is
1631da177e4SLinus Torvalds  * reentrant, so that many processes can be attempting to read from the
1641da177e4SLinus Torvalds  * device at any one time.
1651da177e4SLinus Torvalds  */
1661da177e4SLinus Torvalds 
1671da177e4SLinus Torvalds static int button_read (struct file *filp, char __user *buffer,
1681da177e4SLinus Torvalds 			size_t count, loff_t *ppos)
1691da177e4SLinus Torvalds {
170eb831743SArnd Bergmann 	DEFINE_WAIT(wait);
171eb831743SArnd Bergmann 	prepare_to_wait(&button_wait_queue, &wait, TASK_INTERRUPTIBLE);
172eb831743SArnd Bergmann 	schedule();
173eb831743SArnd Bergmann 	finish_wait(&button_wait_queue, &wait);
1741da177e4SLinus Torvalds 	return (copy_to_user (buffer, &button_output_buffer, bcount))
1751da177e4SLinus Torvalds 		 ? -EFAULT : bcount;
1761da177e4SLinus Torvalds }
1771da177e4SLinus Torvalds 
1781da177e4SLinus Torvalds /*
1791da177e4SLinus Torvalds  * This structure is the file operations structure, which specifies what
1801da177e4SLinus Torvalds  * callbacks functions the kernel should call when a user mode process
1811da177e4SLinus Torvalds  * attempts to perform these operations on the device.
1821da177e4SLinus Torvalds  */
1831da177e4SLinus Torvalds 
18462322d25SArjan van de Ven static const struct file_operations button_fops = {
1851da177e4SLinus Torvalds 	.owner		= THIS_MODULE,
1861da177e4SLinus Torvalds 	.read		= button_read,
1876038f373SArnd Bergmann 	.llseek		= noop_llseek,
1881da177e4SLinus Torvalds };
1891da177e4SLinus Torvalds 
1901da177e4SLinus Torvalds /*
1911da177e4SLinus Torvalds  * This structure is the misc device structure, which specifies the minor
1921da177e4SLinus Torvalds  * device number (158 in this case), the name of the device (for /proc/misc),
1931da177e4SLinus Torvalds  * and the address of the above file operations structure.
1941da177e4SLinus Torvalds  */
1951da177e4SLinus Torvalds 
1961da177e4SLinus Torvalds static struct miscdevice button_misc_device = {
1971da177e4SLinus Torvalds 	BUTTON_MINOR,
1981da177e4SLinus Torvalds 	"nwbutton",
1991da177e4SLinus Torvalds 	&button_fops,
2001da177e4SLinus Torvalds };
2011da177e4SLinus Torvalds 
2021da177e4SLinus Torvalds /*
2031da177e4SLinus Torvalds  * This function is called to initialise the driver, either from misc.c at
2041da177e4SLinus Torvalds  * bootup if the driver is compiled into the kernel, or from init_module
2051da177e4SLinus Torvalds  * below at module insert time. It attempts to register the device node
2061da177e4SLinus Torvalds  * and the IRQ and fails with a warning message if either fails, though
2071da177e4SLinus Torvalds  * neither ever should because the device number and IRQ are unique to
2081da177e4SLinus Torvalds  * this driver.
2091da177e4SLinus Torvalds  */
2101da177e4SLinus Torvalds 
2111da177e4SLinus Torvalds static int __init nwbutton_init(void)
2121da177e4SLinus Torvalds {
2131da177e4SLinus Torvalds 	if (!machine_is_netwinder())
2141da177e4SLinus Torvalds 		return -ENODEV;
2151da177e4SLinus Torvalds 
2161da177e4SLinus Torvalds 	printk (KERN_INFO "NetWinder Button Driver Version %s (C) Alex Holden "
2171da177e4SLinus Torvalds 			"<alex@linuxhacker.org> 1998.\n", VERSION);
2181da177e4SLinus Torvalds 
2191da177e4SLinus Torvalds 	if (misc_register (&button_misc_device)) {
2201da177e4SLinus Torvalds 		printk (KERN_WARNING "nwbutton: Couldn't register device 10, "
2211da177e4SLinus Torvalds 				"%d.\n", BUTTON_MINOR);
2221da177e4SLinus Torvalds 		return -EBUSY;
2231da177e4SLinus Torvalds 	}
2241da177e4SLinus Torvalds 
225d88ed628SMichael Opdenacker 	if (request_irq (IRQ_NETWINDER_BUTTON, button_handler, 0,
2261da177e4SLinus Torvalds 			"nwbutton", NULL)) {
2271da177e4SLinus Torvalds 		printk (KERN_WARNING "nwbutton: IRQ %d is not free.\n",
2281da177e4SLinus Torvalds 				IRQ_NETWINDER_BUTTON);
2291da177e4SLinus Torvalds 		misc_deregister (&button_misc_device);
2301da177e4SLinus Torvalds 		return -EIO;
2311da177e4SLinus Torvalds 	}
2321da177e4SLinus Torvalds 	return 0;
2331da177e4SLinus Torvalds }
2341da177e4SLinus Torvalds 
2351da177e4SLinus Torvalds static void __exit nwbutton_exit (void)
2361da177e4SLinus Torvalds {
2371da177e4SLinus Torvalds 	free_irq (IRQ_NETWINDER_BUTTON, NULL);
2381da177e4SLinus Torvalds 	misc_deregister (&button_misc_device);
2391da177e4SLinus Torvalds }
2401da177e4SLinus Torvalds 
2411da177e4SLinus Torvalds 
2421da177e4SLinus Torvalds MODULE_AUTHOR("Alex Holden");
2431da177e4SLinus Torvalds MODULE_LICENSE("GPL");
2441da177e4SLinus Torvalds 
2451da177e4SLinus Torvalds module_init(nwbutton_init);
2461da177e4SLinus Torvalds module_exit(nwbutton_exit);
247