Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  *  NetWinder Button Driver-
0004  *  Copyright (C) Alex Holden <alex@linuxhacker.org> 1998, 1999.
0005  *
0006  */
0007 
0008 #include <linux/module.h>
0009 #include <linux/kernel.h>
0010 #include <linux/sched/signal.h>
0011 #include <linux/interrupt.h>
0012 #include <linux/time.h>
0013 #include <linux/timer.h>
0014 #include <linux/fs.h>
0015 #include <linux/miscdevice.h>
0016 #include <linux/string.h>
0017 #include <linux/errno.h>
0018 #include <linux/init.h>
0019 
0020 #include <linux/uaccess.h>
0021 #include <asm/irq.h>
0022 #include <asm/mach-types.h>
0023 
0024 #define __NWBUTTON_C        /* Tell the header file who we are */
0025 #include "nwbutton.h"
0026 
0027 static void button_sequence_finished(struct timer_list *unused);
0028 
0029 static int button_press_count;      /* The count of button presses */
0030 /* Times for the end of a sequence */
0031 static DEFINE_TIMER(button_timer, button_sequence_finished);
0032 static DECLARE_WAIT_QUEUE_HEAD(button_wait_queue); /* Used for blocking read */
0033 static char button_output_buffer[32];   /* Stores data to write out of device */
0034 static int bcount;          /* The number of bytes in the buffer */
0035 static int bdelay = BUTTON_DELAY;   /* The delay, in jiffies */
0036 static struct button_callback button_callback_list[32]; /* The callback list */
0037 static int callback_count;      /* The number of callbacks registered */
0038 static int reboot_count = NUM_PRESSES_REBOOT; /* Number of presses to reboot */
0039 
0040 /*
0041  * This function is called by other drivers to register a callback function
0042  * to be called when a particular number of button presses occurs.
0043  * The callback list is a static array of 32 entries (I somehow doubt many
0044  * people are ever going to want to register more than 32 different actions
0045  * to be performed by the kernel on different numbers of button presses ;).
0046  * However, if an attempt to register a 33rd entry (perhaps a stuck loop
0047  * somewhere registering the same entry over and over?) it will fail to
0048  * do so and return -ENOMEM. If an attempt is made to register a null pointer,
0049  * it will fail to do so and return -EINVAL.
0050  * Because callbacks can be unregistered at random the list can become
0051  * fragmented, so we need to search through the list until we find the first
0052  * free entry.
0053  *
0054  * FIXME: Has anyone spotted any locking functions int his code recently ??
0055  */
0056 
0057 int button_add_callback (void (*callback) (void), int count)
0058 {
0059     int lp = 0;
0060     if (callback_count == 32) {
0061         return -ENOMEM;
0062     }
0063     if (!callback) {
0064         return -EINVAL;
0065     }
0066     callback_count++;
0067     for (; (button_callback_list [lp].callback); lp++);
0068     button_callback_list [lp].callback = callback;
0069     button_callback_list [lp].count = count;
0070     return 0;
0071 }
0072 
0073 /*
0074  * This function is called by other drivers to deregister a callback function.
0075  * If you attempt to unregister a callback which does not exist, it will fail
0076  * with -EINVAL. If there is more than one entry with the same address,
0077  * because it searches the list from end to beginning, it will unregister the
0078  * last one to be registered first (FILO- First In Last Out).
0079  * Note that this is not necessarily true if the entries are not submitted
0080  * at the same time, because another driver could have unregistered a callback
0081  * between the submissions creating a gap earlier in the list, which would
0082  * be filled first at submission time.
0083  */
0084 
0085 int button_del_callback (void (*callback) (void))
0086 {
0087     int lp = 31;
0088     if (!callback) {
0089         return -EINVAL;
0090     }
0091     while (lp >= 0) {
0092         if ((button_callback_list [lp].callback) == callback) {
0093             button_callback_list [lp].callback = NULL;
0094             button_callback_list [lp].count = 0;
0095             callback_count--;
0096             return 0;
0097         }
0098         lp--;
0099     }
0100     return -EINVAL;
0101 }
0102 
0103 /*
0104  * This function is called by button_sequence_finished to search through the
0105  * list of callback functions, and call any of them whose count argument
0106  * matches the current count of button presses. It starts at the beginning
0107  * of the list and works up to the end. It will refuse to follow a null
0108  * pointer (which should never happen anyway).
0109  */
0110 
0111 static void button_consume_callbacks (int bpcount)
0112 {
0113     int lp = 0;
0114     for (; lp <= 31; lp++) {
0115         if ((button_callback_list [lp].count) == bpcount) {
0116             if (button_callback_list [lp].callback) {
0117                 button_callback_list[lp].callback();
0118             }
0119         }
0120     }
0121 }
0122 
0123 /* 
0124  * This function is called when the button_timer times out.
0125  * ie. When you don't press the button for bdelay jiffies, this is taken to
0126  * mean you have ended the sequence of key presses, and this function is
0127  * called to wind things up (write the press_count out to /dev/button, call
0128  * any matching registered function callbacks, initiate reboot, etc.).
0129  */
0130 
0131 static void button_sequence_finished(struct timer_list *unused)
0132 {
0133     if (IS_ENABLED(CONFIG_NWBUTTON_REBOOT) &&
0134         button_press_count == reboot_count)
0135         kill_cad_pid(SIGINT, 1);    /* Ask init to reboot us */
0136     button_consume_callbacks (button_press_count);
0137     bcount = sprintf (button_output_buffer, "%d\n", button_press_count);
0138     button_press_count = 0;     /* Reset the button press counter */
0139     wake_up_interruptible (&button_wait_queue);
0140 }
0141 
0142 /* 
0143  *  This handler is called when the orange button is pressed (GPIO 10 of the
0144  *  SuperIO chip, which maps to logical IRQ 26). If the press_count is 0,
0145  *  this is the first press, so it starts a timer and increments the counter.
0146  *  If it is higher than 0, it deletes the old timer, starts a new one, and
0147  *  increments the counter.
0148  */ 
0149 
0150 static irqreturn_t button_handler (int irq, void *dev_id)
0151 {
0152     button_press_count++;
0153     mod_timer(&button_timer, jiffies + bdelay);
0154 
0155     return IRQ_HANDLED;
0156 }
0157 
0158 /*
0159  * This function is called when a user space program attempts to read
0160  * /dev/nwbutton. It puts the device to sleep on the wait queue until
0161  * button_sequence_finished writes some data to the buffer and flushes
0162  * the queue, at which point it writes the data out to the device and
0163  * returns the number of characters it has written. This function is
0164  * reentrant, so that many processes can be attempting to read from the
0165  * device at any one time.
0166  */
0167 
0168 static int button_read (struct file *filp, char __user *buffer,
0169             size_t count, loff_t *ppos)
0170 {
0171     DEFINE_WAIT(wait);
0172     prepare_to_wait(&button_wait_queue, &wait, TASK_INTERRUPTIBLE);
0173     schedule();
0174     finish_wait(&button_wait_queue, &wait);
0175     return (copy_to_user (buffer, &button_output_buffer, bcount))
0176          ? -EFAULT : bcount;
0177 }
0178 
0179 /* 
0180  * This structure is the file operations structure, which specifies what
0181  * callbacks functions the kernel should call when a user mode process
0182  * attempts to perform these operations on the device.
0183  */
0184 
0185 static const struct file_operations button_fops = {
0186     .owner      = THIS_MODULE,
0187     .read       = button_read,
0188     .llseek     = noop_llseek,
0189 };
0190 
0191 /* 
0192  * This structure is the misc device structure, which specifies the minor
0193  * device number (158 in this case), the name of the device (for /proc/misc),
0194  * and the address of the above file operations structure.
0195  */
0196 
0197 static struct miscdevice button_misc_device = {
0198     BUTTON_MINOR,
0199     "nwbutton",
0200     &button_fops,
0201 };
0202 
0203 /*
0204  * This function is called to initialise the driver, either from misc.c at
0205  * bootup if the driver is compiled into the kernel, or from init_module
0206  * below at module insert time. It attempts to register the device node
0207  * and the IRQ and fails with a warning message if either fails, though
0208  * neither ever should because the device number and IRQ are unique to
0209  * this driver.
0210  */
0211 
0212 static int __init nwbutton_init(void)
0213 {
0214     if (!machine_is_netwinder())
0215         return -ENODEV;
0216 
0217     printk (KERN_INFO "NetWinder Button Driver Version %s (C) Alex Holden "
0218             "<alex@linuxhacker.org> 1998.\n", VERSION);
0219 
0220     if (misc_register (&button_misc_device)) {
0221         printk (KERN_WARNING "nwbutton: Couldn't register device 10, "
0222                 "%d.\n", BUTTON_MINOR);
0223         return -EBUSY;
0224     }
0225 
0226     if (request_irq (IRQ_NETWINDER_BUTTON, button_handler, 0,
0227             "nwbutton", NULL)) {
0228         printk (KERN_WARNING "nwbutton: IRQ %d is not free.\n",
0229                 IRQ_NETWINDER_BUTTON);
0230         misc_deregister (&button_misc_device);
0231         return -EIO;
0232     }
0233     return 0;
0234 }
0235 
0236 static void __exit nwbutton_exit (void) 
0237 {
0238     free_irq (IRQ_NETWINDER_BUTTON, NULL);
0239     misc_deregister (&button_misc_device);
0240 }
0241 
0242 
0243 MODULE_AUTHOR("Alex Holden");
0244 MODULE_LICENSE("GPL");
0245 
0246 module_init(nwbutton_init);
0247 module_exit(nwbutton_exit);