Blame view

drivers/char/nwbutton.c 7.92 KB
09c434b8a   Thomas Gleixner   treewide: Add SPD...
1
  // SPDX-License-Identifier: GPL-2.0-only
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2
3
4
5
6
  /*
   * 	NetWinder Button Driver-
   *	Copyright (C) Alex Holden <alex@linuxhacker.org> 1998, 1999.
   *
   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
7
8
  #include <linux/module.h>
  #include <linux/kernel.h>
bb35e4515   Guenter Roeck   drivers/char/nwbu...
9
  #include <linux/sched/signal.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
10
11
12
13
14
15
16
17
  #include <linux/interrupt.h>
  #include <linux/time.h>
  #include <linux/timer.h>
  #include <linux/fs.h>
  #include <linux/miscdevice.h>
  #include <linux/string.h>
  #include <linux/errno.h>
  #include <linux/init.h>
7c0f6ba68   Linus Torvalds   Replace <asm/uacc...
18
  #include <linux/uaccess.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
19
20
21
22
23
  #include <asm/irq.h>
  #include <asm/mach-types.h>
  
  #define __NWBUTTON_C		/* Tell the header file who we are */
  #include "nwbutton.h"
24ed960ab   Kees Cook   treewide: Switch ...
24
  static void button_sequence_finished(struct timer_list *unused);
40565f196   Jiri Slaby   [PATCH] Char: tim...
25

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