xref: /OK3568_Linux_fs/kernel/drivers/char/nwbutton.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * 	NetWinder Button Driver-
4*4882a593Smuzhiyun  *	Copyright (C) Alex Holden <alex@linuxhacker.org> 1998, 1999.
5*4882a593Smuzhiyun  *
6*4882a593Smuzhiyun  */
7*4882a593Smuzhiyun 
8*4882a593Smuzhiyun #include <linux/module.h>
9*4882a593Smuzhiyun #include <linux/kernel.h>
10*4882a593Smuzhiyun #include <linux/sched/signal.h>
11*4882a593Smuzhiyun #include <linux/interrupt.h>
12*4882a593Smuzhiyun #include <linux/time.h>
13*4882a593Smuzhiyun #include <linux/timer.h>
14*4882a593Smuzhiyun #include <linux/fs.h>
15*4882a593Smuzhiyun #include <linux/miscdevice.h>
16*4882a593Smuzhiyun #include <linux/string.h>
17*4882a593Smuzhiyun #include <linux/errno.h>
18*4882a593Smuzhiyun #include <linux/init.h>
19*4882a593Smuzhiyun 
20*4882a593Smuzhiyun #include <linux/uaccess.h>
21*4882a593Smuzhiyun #include <asm/irq.h>
22*4882a593Smuzhiyun #include <asm/mach-types.h>
23*4882a593Smuzhiyun 
24*4882a593Smuzhiyun #define __NWBUTTON_C		/* Tell the header file who we are */
25*4882a593Smuzhiyun #include "nwbutton.h"
26*4882a593Smuzhiyun 
27*4882a593Smuzhiyun static void button_sequence_finished(struct timer_list *unused);
28*4882a593Smuzhiyun 
29*4882a593Smuzhiyun static int button_press_count;		/* The count of button presses */
30*4882a593Smuzhiyun /* Times for the end of a sequence */
31*4882a593Smuzhiyun static DEFINE_TIMER(button_timer, button_sequence_finished);
32*4882a593Smuzhiyun static DECLARE_WAIT_QUEUE_HEAD(button_wait_queue); /* Used for blocking read */
33*4882a593Smuzhiyun static char button_output_buffer[32];	/* Stores data to write out of device */
34*4882a593Smuzhiyun static int bcount;			/* The number of bytes in the buffer */
35*4882a593Smuzhiyun static int bdelay = BUTTON_DELAY;	/* The delay, in jiffies */
36*4882a593Smuzhiyun static struct button_callback button_callback_list[32]; /* The callback list */
37*4882a593Smuzhiyun static int callback_count;		/* The number of callbacks registered */
38*4882a593Smuzhiyun static int reboot_count = NUM_PRESSES_REBOOT; /* Number of presses to reboot */
39*4882a593Smuzhiyun 
40*4882a593Smuzhiyun /*
41*4882a593Smuzhiyun  * This function is called by other drivers to register a callback function
42*4882a593Smuzhiyun  * to be called when a particular number of button presses occurs.
43*4882a593Smuzhiyun  * The callback list is a static array of 32 entries (I somehow doubt many
44*4882a593Smuzhiyun  * people are ever going to want to register more than 32 different actions
45*4882a593Smuzhiyun  * to be performed by the kernel on different numbers of button presses ;).
46*4882a593Smuzhiyun  * However, if an attempt to register a 33rd entry (perhaps a stuck loop
47*4882a593Smuzhiyun  * somewhere registering the same entry over and over?) it will fail to
48*4882a593Smuzhiyun  * do so and return -ENOMEM. If an attempt is made to register a null pointer,
49*4882a593Smuzhiyun  * it will fail to do so and return -EINVAL.
50*4882a593Smuzhiyun  * Because callbacks can be unregistered at random the list can become
51*4882a593Smuzhiyun  * fragmented, so we need to search through the list until we find the first
52*4882a593Smuzhiyun  * free entry.
53*4882a593Smuzhiyun  *
54*4882a593Smuzhiyun  * FIXME: Has anyone spotted any locking functions int his code recently ??
55*4882a593Smuzhiyun  */
56*4882a593Smuzhiyun 
button_add_callback(void (* callback)(void),int count)57*4882a593Smuzhiyun int button_add_callback (void (*callback) (void), int count)
58*4882a593Smuzhiyun {
59*4882a593Smuzhiyun 	int lp = 0;
60*4882a593Smuzhiyun 	if (callback_count == 32) {
61*4882a593Smuzhiyun 		return -ENOMEM;
62*4882a593Smuzhiyun 	}
63*4882a593Smuzhiyun 	if (!callback) {
64*4882a593Smuzhiyun 		return -EINVAL;
65*4882a593Smuzhiyun 	}
66*4882a593Smuzhiyun 	callback_count++;
67*4882a593Smuzhiyun 	for (; (button_callback_list [lp].callback); lp++);
68*4882a593Smuzhiyun 	button_callback_list [lp].callback = callback;
69*4882a593Smuzhiyun 	button_callback_list [lp].count = count;
70*4882a593Smuzhiyun 	return 0;
71*4882a593Smuzhiyun }
72*4882a593Smuzhiyun 
73*4882a593Smuzhiyun /*
74*4882a593Smuzhiyun  * This function is called by other drivers to deregister a callback function.
75*4882a593Smuzhiyun  * If you attempt to unregister a callback which does not exist, it will fail
76*4882a593Smuzhiyun  * with -EINVAL. If there is more than one entry with the same address,
77*4882a593Smuzhiyun  * because it searches the list from end to beginning, it will unregister the
78*4882a593Smuzhiyun  * last one to be registered first (FILO- First In Last Out).
79*4882a593Smuzhiyun  * Note that this is not necessarily true if the entries are not submitted
80*4882a593Smuzhiyun  * at the same time, because another driver could have unregistered a callback
81*4882a593Smuzhiyun  * between the submissions creating a gap earlier in the list, which would
82*4882a593Smuzhiyun  * be filled first at submission time.
83*4882a593Smuzhiyun  */
84*4882a593Smuzhiyun 
button_del_callback(void (* callback)(void))85*4882a593Smuzhiyun int button_del_callback (void (*callback) (void))
86*4882a593Smuzhiyun {
87*4882a593Smuzhiyun 	int lp = 31;
88*4882a593Smuzhiyun 	if (!callback) {
89*4882a593Smuzhiyun 		return -EINVAL;
90*4882a593Smuzhiyun 	}
91*4882a593Smuzhiyun 	while (lp >= 0) {
92*4882a593Smuzhiyun 		if ((button_callback_list [lp].callback) == callback) {
93*4882a593Smuzhiyun 			button_callback_list [lp].callback = NULL;
94*4882a593Smuzhiyun 			button_callback_list [lp].count = 0;
95*4882a593Smuzhiyun 			callback_count--;
96*4882a593Smuzhiyun 			return 0;
97*4882a593Smuzhiyun 		}
98*4882a593Smuzhiyun 		lp--;
99*4882a593Smuzhiyun 	}
100*4882a593Smuzhiyun 	return -EINVAL;
101*4882a593Smuzhiyun }
102*4882a593Smuzhiyun 
103*4882a593Smuzhiyun /*
104*4882a593Smuzhiyun  * This function is called by button_sequence_finished to search through the
105*4882a593Smuzhiyun  * list of callback functions, and call any of them whose count argument
106*4882a593Smuzhiyun  * matches the current count of button presses. It starts at the beginning
107*4882a593Smuzhiyun  * of the list and works up to the end. It will refuse to follow a null
108*4882a593Smuzhiyun  * pointer (which should never happen anyway).
109*4882a593Smuzhiyun  */
110*4882a593Smuzhiyun 
button_consume_callbacks(int bpcount)111*4882a593Smuzhiyun static void button_consume_callbacks (int bpcount)
112*4882a593Smuzhiyun {
113*4882a593Smuzhiyun 	int lp = 0;
114*4882a593Smuzhiyun 	for (; lp <= 31; lp++) {
115*4882a593Smuzhiyun 		if ((button_callback_list [lp].count) == bpcount) {
116*4882a593Smuzhiyun 			if (button_callback_list [lp].callback) {
117*4882a593Smuzhiyun 				button_callback_list[lp].callback();
118*4882a593Smuzhiyun 			}
119*4882a593Smuzhiyun 		}
120*4882a593Smuzhiyun 	}
121*4882a593Smuzhiyun }
122*4882a593Smuzhiyun 
123*4882a593Smuzhiyun /*
124*4882a593Smuzhiyun  * This function is called when the button_timer times out.
125*4882a593Smuzhiyun  * ie. When you don't press the button for bdelay jiffies, this is taken to
126*4882a593Smuzhiyun  * mean you have ended the sequence of key presses, and this function is
127*4882a593Smuzhiyun  * called to wind things up (write the press_count out to /dev/button, call
128*4882a593Smuzhiyun  * any matching registered function callbacks, initiate reboot, etc.).
129*4882a593Smuzhiyun  */
130*4882a593Smuzhiyun 
button_sequence_finished(struct timer_list * unused)131*4882a593Smuzhiyun static void button_sequence_finished(struct timer_list *unused)
132*4882a593Smuzhiyun {
133*4882a593Smuzhiyun 	if (IS_ENABLED(CONFIG_NWBUTTON_REBOOT) &&
134*4882a593Smuzhiyun 	    button_press_count == reboot_count)
135*4882a593Smuzhiyun 		kill_cad_pid(SIGINT, 1);	/* Ask init to reboot us */
136*4882a593Smuzhiyun 	button_consume_callbacks (button_press_count);
137*4882a593Smuzhiyun 	bcount = sprintf (button_output_buffer, "%d\n", button_press_count);
138*4882a593Smuzhiyun 	button_press_count = 0;		/* Reset the button press counter */
139*4882a593Smuzhiyun 	wake_up_interruptible (&button_wait_queue);
140*4882a593Smuzhiyun }
141*4882a593Smuzhiyun 
142*4882a593Smuzhiyun /*
143*4882a593Smuzhiyun  *  This handler is called when the orange button is pressed (GPIO 10 of the
144*4882a593Smuzhiyun  *  SuperIO chip, which maps to logical IRQ 26). If the press_count is 0,
145*4882a593Smuzhiyun  *  this is the first press, so it starts a timer and increments the counter.
146*4882a593Smuzhiyun  *  If it is higher than 0, it deletes the old timer, starts a new one, and
147*4882a593Smuzhiyun  *  increments the counter.
148*4882a593Smuzhiyun  */
149*4882a593Smuzhiyun 
button_handler(int irq,void * dev_id)150*4882a593Smuzhiyun static irqreturn_t button_handler (int irq, void *dev_id)
151*4882a593Smuzhiyun {
152*4882a593Smuzhiyun 	button_press_count++;
153*4882a593Smuzhiyun 	mod_timer(&button_timer, jiffies + bdelay);
154*4882a593Smuzhiyun 
155*4882a593Smuzhiyun 	return IRQ_HANDLED;
156*4882a593Smuzhiyun }
157*4882a593Smuzhiyun 
158*4882a593Smuzhiyun /*
159*4882a593Smuzhiyun  * This function is called when a user space program attempts to read
160*4882a593Smuzhiyun  * /dev/nwbutton. It puts the device to sleep on the wait queue until
161*4882a593Smuzhiyun  * button_sequence_finished writes some data to the buffer and flushes
162*4882a593Smuzhiyun  * the queue, at which point it writes the data out to the device and
163*4882a593Smuzhiyun  * returns the number of characters it has written. This function is
164*4882a593Smuzhiyun  * reentrant, so that many processes can be attempting to read from the
165*4882a593Smuzhiyun  * device at any one time.
166*4882a593Smuzhiyun  */
167*4882a593Smuzhiyun 
button_read(struct file * filp,char __user * buffer,size_t count,loff_t * ppos)168*4882a593Smuzhiyun static int button_read (struct file *filp, char __user *buffer,
169*4882a593Smuzhiyun 			size_t count, loff_t *ppos)
170*4882a593Smuzhiyun {
171*4882a593Smuzhiyun 	DEFINE_WAIT(wait);
172*4882a593Smuzhiyun 	prepare_to_wait(&button_wait_queue, &wait, TASK_INTERRUPTIBLE);
173*4882a593Smuzhiyun 	schedule();
174*4882a593Smuzhiyun 	finish_wait(&button_wait_queue, &wait);
175*4882a593Smuzhiyun 	return (copy_to_user (buffer, &button_output_buffer, bcount))
176*4882a593Smuzhiyun 		 ? -EFAULT : bcount;
177*4882a593Smuzhiyun }
178*4882a593Smuzhiyun 
179*4882a593Smuzhiyun /*
180*4882a593Smuzhiyun  * This structure is the file operations structure, which specifies what
181*4882a593Smuzhiyun  * callbacks functions the kernel should call when a user mode process
182*4882a593Smuzhiyun  * attempts to perform these operations on the device.
183*4882a593Smuzhiyun  */
184*4882a593Smuzhiyun 
185*4882a593Smuzhiyun static const struct file_operations button_fops = {
186*4882a593Smuzhiyun 	.owner		= THIS_MODULE,
187*4882a593Smuzhiyun 	.read		= button_read,
188*4882a593Smuzhiyun 	.llseek		= noop_llseek,
189*4882a593Smuzhiyun };
190*4882a593Smuzhiyun 
191*4882a593Smuzhiyun /*
192*4882a593Smuzhiyun  * This structure is the misc device structure, which specifies the minor
193*4882a593Smuzhiyun  * device number (158 in this case), the name of the device (for /proc/misc),
194*4882a593Smuzhiyun  * and the address of the above file operations structure.
195*4882a593Smuzhiyun  */
196*4882a593Smuzhiyun 
197*4882a593Smuzhiyun static struct miscdevice button_misc_device = {
198*4882a593Smuzhiyun 	BUTTON_MINOR,
199*4882a593Smuzhiyun 	"nwbutton",
200*4882a593Smuzhiyun 	&button_fops,
201*4882a593Smuzhiyun };
202*4882a593Smuzhiyun 
203*4882a593Smuzhiyun /*
204*4882a593Smuzhiyun  * This function is called to initialise the driver, either from misc.c at
205*4882a593Smuzhiyun  * bootup if the driver is compiled into the kernel, or from init_module
206*4882a593Smuzhiyun  * below at module insert time. It attempts to register the device node
207*4882a593Smuzhiyun  * and the IRQ and fails with a warning message if either fails, though
208*4882a593Smuzhiyun  * neither ever should because the device number and IRQ are unique to
209*4882a593Smuzhiyun  * this driver.
210*4882a593Smuzhiyun  */
211*4882a593Smuzhiyun 
nwbutton_init(void)212*4882a593Smuzhiyun static int __init nwbutton_init(void)
213*4882a593Smuzhiyun {
214*4882a593Smuzhiyun 	if (!machine_is_netwinder())
215*4882a593Smuzhiyun 		return -ENODEV;
216*4882a593Smuzhiyun 
217*4882a593Smuzhiyun 	printk (KERN_INFO "NetWinder Button Driver Version %s (C) Alex Holden "
218*4882a593Smuzhiyun 			"<alex@linuxhacker.org> 1998.\n", VERSION);
219*4882a593Smuzhiyun 
220*4882a593Smuzhiyun 	if (misc_register (&button_misc_device)) {
221*4882a593Smuzhiyun 		printk (KERN_WARNING "nwbutton: Couldn't register device 10, "
222*4882a593Smuzhiyun 				"%d.\n", BUTTON_MINOR);
223*4882a593Smuzhiyun 		return -EBUSY;
224*4882a593Smuzhiyun 	}
225*4882a593Smuzhiyun 
226*4882a593Smuzhiyun 	if (request_irq (IRQ_NETWINDER_BUTTON, button_handler, 0,
227*4882a593Smuzhiyun 			"nwbutton", NULL)) {
228*4882a593Smuzhiyun 		printk (KERN_WARNING "nwbutton: IRQ %d is not free.\n",
229*4882a593Smuzhiyun 				IRQ_NETWINDER_BUTTON);
230*4882a593Smuzhiyun 		misc_deregister (&button_misc_device);
231*4882a593Smuzhiyun 		return -EIO;
232*4882a593Smuzhiyun 	}
233*4882a593Smuzhiyun 	return 0;
234*4882a593Smuzhiyun }
235*4882a593Smuzhiyun 
nwbutton_exit(void)236*4882a593Smuzhiyun static void __exit nwbutton_exit (void)
237*4882a593Smuzhiyun {
238*4882a593Smuzhiyun 	free_irq (IRQ_NETWINDER_BUTTON, NULL);
239*4882a593Smuzhiyun 	misc_deregister (&button_misc_device);
240*4882a593Smuzhiyun }
241*4882a593Smuzhiyun 
242*4882a593Smuzhiyun 
243*4882a593Smuzhiyun MODULE_AUTHOR("Alex Holden");
244*4882a593Smuzhiyun MODULE_LICENSE("GPL");
245*4882a593Smuzhiyun 
246*4882a593Smuzhiyun module_init(nwbutton_init);
247*4882a593Smuzhiyun module_exit(nwbutton_exit);
248