1*09c434b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 21da177e4SLinus Torvalds /* 31da177e4SLinus Torvalds * NetWinder Button Driver- 41da177e4SLinus Torvalds * Copyright (C) Alex Holden <alex@linuxhacker.org> 1998, 1999. 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds */ 71da177e4SLinus Torvalds 81da177e4SLinus Torvalds #include <linux/module.h> 91da177e4SLinus Torvalds #include <linux/kernel.h> 10bb35e451SGuenter Roeck #include <linux/sched/signal.h> 111da177e4SLinus Torvalds #include <linux/interrupt.h> 121da177e4SLinus Torvalds #include <linux/time.h> 131da177e4SLinus Torvalds #include <linux/timer.h> 141da177e4SLinus Torvalds #include <linux/fs.h> 151da177e4SLinus Torvalds #include <linux/miscdevice.h> 161da177e4SLinus Torvalds #include <linux/string.h> 171da177e4SLinus Torvalds #include <linux/errno.h> 181da177e4SLinus Torvalds #include <linux/init.h> 191da177e4SLinus Torvalds 207c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 211da177e4SLinus Torvalds #include <asm/irq.h> 221da177e4SLinus Torvalds #include <asm/mach-types.h> 231da177e4SLinus Torvalds 241da177e4SLinus Torvalds #define __NWBUTTON_C /* Tell the header file who we are */ 251da177e4SLinus Torvalds #include "nwbutton.h" 261da177e4SLinus Torvalds 2724ed960aSKees Cook static void button_sequence_finished(struct timer_list *unused); 2840565f19SJiri Slaby 291da177e4SLinus Torvalds static int button_press_count; /* The count of button presses */ 3040565f19SJiri Slaby /* Times for the end of a sequence */ 311d27e3e2SKees Cook static DEFINE_TIMER(button_timer, button_sequence_finished); 321da177e4SLinus Torvalds static DECLARE_WAIT_QUEUE_HEAD(button_wait_queue); /* Used for blocking read */ 331da177e4SLinus Torvalds static char button_output_buffer[32]; /* Stores data to write out of device */ 341da177e4SLinus Torvalds static int bcount; /* The number of bytes in the buffer */ 351da177e4SLinus Torvalds static int bdelay = BUTTON_DELAY; /* The delay, in jiffies */ 361da177e4SLinus Torvalds static struct button_callback button_callback_list[32]; /* The callback list */ 371da177e4SLinus Torvalds static int callback_count; /* The number of callbacks registered */ 381da177e4SLinus Torvalds static int reboot_count = NUM_PRESSES_REBOOT; /* Number of presses to reboot */ 391da177e4SLinus Torvalds 401da177e4SLinus Torvalds /* 411da177e4SLinus Torvalds * This function is called by other drivers to register a callback function 421da177e4SLinus Torvalds * to be called when a particular number of button presses occurs. 431da177e4SLinus Torvalds * The callback list is a static array of 32 entries (I somehow doubt many 441da177e4SLinus Torvalds * people are ever going to want to register more than 32 different actions 451da177e4SLinus Torvalds * to be performed by the kernel on different numbers of button presses ;). 461da177e4SLinus Torvalds * However, if an attempt to register a 33rd entry (perhaps a stuck loop 471da177e4SLinus Torvalds * somewhere registering the same entry over and over?) it will fail to 481da177e4SLinus Torvalds * do so and return -ENOMEM. If an attempt is made to register a null pointer, 491da177e4SLinus Torvalds * it will fail to do so and return -EINVAL. 501da177e4SLinus Torvalds * Because callbacks can be unregistered at random the list can become 511da177e4SLinus Torvalds * fragmented, so we need to search through the list until we find the first 521da177e4SLinus Torvalds * free entry. 531da177e4SLinus Torvalds * 541da177e4SLinus Torvalds * FIXME: Has anyone spotted any locking functions int his code recently ?? 551da177e4SLinus Torvalds */ 561da177e4SLinus Torvalds 571da177e4SLinus Torvalds int button_add_callback (void (*callback) (void), int count) 581da177e4SLinus Torvalds { 591da177e4SLinus Torvalds int lp = 0; 601da177e4SLinus Torvalds if (callback_count == 32) { 611da177e4SLinus Torvalds return -ENOMEM; 621da177e4SLinus Torvalds } 631da177e4SLinus Torvalds if (!callback) { 641da177e4SLinus Torvalds return -EINVAL; 651da177e4SLinus Torvalds } 661da177e4SLinus Torvalds callback_count++; 671da177e4SLinus Torvalds for (; (button_callback_list [lp].callback); lp++); 681da177e4SLinus Torvalds button_callback_list [lp].callback = callback; 691da177e4SLinus Torvalds button_callback_list [lp].count = count; 701da177e4SLinus Torvalds return 0; 711da177e4SLinus Torvalds } 721da177e4SLinus Torvalds 731da177e4SLinus Torvalds /* 741da177e4SLinus Torvalds * This function is called by other drivers to deregister a callback function. 751da177e4SLinus Torvalds * If you attempt to unregister a callback which does not exist, it will fail 761da177e4SLinus Torvalds * with -EINVAL. If there is more than one entry with the same address, 771da177e4SLinus Torvalds * because it searches the list from end to beginning, it will unregister the 781da177e4SLinus Torvalds * last one to be registered first (FILO- First In Last Out). 7925985edcSLucas De Marchi * Note that this is not necessarily true if the entries are not submitted 801da177e4SLinus Torvalds * at the same time, because another driver could have unregistered a callback 811da177e4SLinus Torvalds * between the submissions creating a gap earlier in the list, which would 821da177e4SLinus Torvalds * be filled first at submission time. 831da177e4SLinus Torvalds */ 841da177e4SLinus Torvalds 851da177e4SLinus Torvalds int button_del_callback (void (*callback) (void)) 861da177e4SLinus Torvalds { 871da177e4SLinus Torvalds int lp = 31; 881da177e4SLinus Torvalds if (!callback) { 891da177e4SLinus Torvalds return -EINVAL; 901da177e4SLinus Torvalds } 911da177e4SLinus Torvalds while (lp >= 0) { 921da177e4SLinus Torvalds if ((button_callback_list [lp].callback) == callback) { 931da177e4SLinus Torvalds button_callback_list [lp].callback = NULL; 941da177e4SLinus Torvalds button_callback_list [lp].count = 0; 951da177e4SLinus Torvalds callback_count--; 961da177e4SLinus Torvalds return 0; 97f8885c26SPeter Senna Tschudin } 981da177e4SLinus Torvalds lp--; 99f8885c26SPeter Senna Tschudin } 1001da177e4SLinus Torvalds return -EINVAL; 1011da177e4SLinus Torvalds } 1021da177e4SLinus Torvalds 1031da177e4SLinus Torvalds /* 1041da177e4SLinus Torvalds * This function is called by button_sequence_finished to search through the 1051da177e4SLinus Torvalds * list of callback functions, and call any of them whose count argument 1061da177e4SLinus Torvalds * matches the current count of button presses. It starts at the beginning 1071da177e4SLinus Torvalds * of the list and works up to the end. It will refuse to follow a null 1081da177e4SLinus Torvalds * pointer (which should never happen anyway). 1091da177e4SLinus Torvalds */ 1101da177e4SLinus Torvalds 1111da177e4SLinus Torvalds static void button_consume_callbacks (int bpcount) 1121da177e4SLinus Torvalds { 1131da177e4SLinus Torvalds int lp = 0; 1141da177e4SLinus Torvalds for (; lp <= 31; lp++) { 1151da177e4SLinus Torvalds if ((button_callback_list [lp].count) == bpcount) { 1161da177e4SLinus Torvalds if (button_callback_list [lp].callback) { 1171da177e4SLinus Torvalds button_callback_list[lp].callback(); 1181da177e4SLinus Torvalds } 1191da177e4SLinus Torvalds } 1201da177e4SLinus Torvalds } 1211da177e4SLinus Torvalds } 1221da177e4SLinus Torvalds 1231da177e4SLinus Torvalds /* 1241da177e4SLinus Torvalds * This function is called when the button_timer times out. 1251da177e4SLinus Torvalds * ie. When you don't press the button for bdelay jiffies, this is taken to 1261da177e4SLinus Torvalds * mean you have ended the sequence of key presses, and this function is 1271da177e4SLinus Torvalds * called to wind things up (write the press_count out to /dev/button, call 1281da177e4SLinus Torvalds * any matching registered function callbacks, initiate reboot, etc.). 1291da177e4SLinus Torvalds */ 1301da177e4SLinus Torvalds 13124ed960aSKees Cook static void button_sequence_finished(struct timer_list *unused) 1321da177e4SLinus Torvalds { 133990162f0SArnd Bergmann if (IS_ENABLED(CONFIG_NWBUTTON_REBOOT) && 134990162f0SArnd Bergmann button_press_count == reboot_count) 1359ec52099SCedric Le Goater kill_cad_pid(SIGINT, 1); /* Ask init to reboot us */ 1361da177e4SLinus Torvalds button_consume_callbacks (button_press_count); 1371da177e4SLinus Torvalds bcount = sprintf (button_output_buffer, "%d\n", button_press_count); 1381da177e4SLinus Torvalds button_press_count = 0; /* Reset the button press counter */ 1391da177e4SLinus Torvalds wake_up_interruptible (&button_wait_queue); 1401da177e4SLinus Torvalds } 1411da177e4SLinus Torvalds 1421da177e4SLinus Torvalds /* 1431da177e4SLinus Torvalds * This handler is called when the orange button is pressed (GPIO 10 of the 1441da177e4SLinus Torvalds * SuperIO chip, which maps to logical IRQ 26). If the press_count is 0, 1451da177e4SLinus Torvalds * this is the first press, so it starts a timer and increments the counter. 1461da177e4SLinus Torvalds * If it is higher than 0, it deletes the old timer, starts a new one, and 1471da177e4SLinus Torvalds * increments the counter. 1481da177e4SLinus Torvalds */ 1491da177e4SLinus Torvalds 1507d12e780SDavid Howells static irqreturn_t button_handler (int irq, void *dev_id) 1511da177e4SLinus Torvalds { 1521da177e4SLinus Torvalds button_press_count++; 15340565f19SJiri Slaby mod_timer(&button_timer, jiffies + bdelay); 1541da177e4SLinus Torvalds 1551da177e4SLinus Torvalds return IRQ_HANDLED; 1561da177e4SLinus Torvalds } 1571da177e4SLinus Torvalds 1581da177e4SLinus Torvalds /* 1591da177e4SLinus Torvalds * This function is called when a user space program attempts to read 1601da177e4SLinus Torvalds * /dev/nwbutton. It puts the device to sleep on the wait queue until 1611da177e4SLinus Torvalds * button_sequence_finished writes some data to the buffer and flushes 1621da177e4SLinus Torvalds * the queue, at which point it writes the data out to the device and 1631da177e4SLinus Torvalds * returns the number of characters it has written. This function is 1641da177e4SLinus Torvalds * reentrant, so that many processes can be attempting to read from the 1651da177e4SLinus Torvalds * device at any one time. 1661da177e4SLinus Torvalds */ 1671da177e4SLinus Torvalds 1681da177e4SLinus Torvalds static int button_read (struct file *filp, char __user *buffer, 1691da177e4SLinus Torvalds size_t count, loff_t *ppos) 1701da177e4SLinus Torvalds { 171eb831743SArnd Bergmann DEFINE_WAIT(wait); 172eb831743SArnd Bergmann prepare_to_wait(&button_wait_queue, &wait, TASK_INTERRUPTIBLE); 173eb831743SArnd Bergmann schedule(); 174eb831743SArnd Bergmann finish_wait(&button_wait_queue, &wait); 1751da177e4SLinus Torvalds return (copy_to_user (buffer, &button_output_buffer, bcount)) 1761da177e4SLinus Torvalds ? -EFAULT : bcount; 1771da177e4SLinus Torvalds } 1781da177e4SLinus Torvalds 1791da177e4SLinus Torvalds /* 1801da177e4SLinus Torvalds * This structure is the file operations structure, which specifies what 1811da177e4SLinus Torvalds * callbacks functions the kernel should call when a user mode process 1821da177e4SLinus Torvalds * attempts to perform these operations on the device. 1831da177e4SLinus Torvalds */ 1841da177e4SLinus Torvalds 18562322d25SArjan van de Ven static const struct file_operations button_fops = { 1861da177e4SLinus Torvalds .owner = THIS_MODULE, 1871da177e4SLinus Torvalds .read = button_read, 1886038f373SArnd Bergmann .llseek = noop_llseek, 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 226d88ed628SMichael Opdenacker if (request_irq (IRQ_NETWINDER_BUTTON, button_handler, 0, 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