2 * $Id: input.c,v 1.7 2000/05/28 17:31:36 vojtech Exp $
4 * Copyright (c) 1999-2000 Vojtech Pavlik
6 * The input layer module itself
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 * Should you need to contact me, the author, you can do so either by
27 * e-mail - mail your message to <vojtech@suse.cz>, or by paper mail:
28 * Vojtech Pavlik, Ucitelska 1576, Prague 8, 182 00 Czech Republic
31 #include <linux/init.h>
32 #include <linux/input.h>
33 #include <linux/module.h>
34 #include <linux/random.h>
36 MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>");
38 EXPORT_SYMBOL(input_register_device
);
39 EXPORT_SYMBOL(input_unregister_device
);
40 EXPORT_SYMBOL(input_register_handler
);
41 EXPORT_SYMBOL(input_unregister_handler
);
42 EXPORT_SYMBOL(input_register_minor
);
43 EXPORT_SYMBOL(input_unregister_minor
);
44 EXPORT_SYMBOL(input_open_device
);
45 EXPORT_SYMBOL(input_close_device
);
46 EXPORT_SYMBOL(input_event
);
48 #define INPUT_MAJOR 13
49 #define INPUT_DEVICES 256
51 static struct input_dev
*input_dev
= NULL
;
52 static struct input_handler
*input_handler
= NULL
;
53 static struct input_handler
*input_table
[8] = { NULL
, /* ... */ };
54 static devfs_handle_t input_devfs_handle
= NULL
;
55 static int input_number
= 0;
56 static long input_devices
[NBITS(INPUT_DEVICES
)] = { 0, /* ... */ };
58 void input_event(struct input_dev
*dev
, unsigned int type
, unsigned int code
, int value
)
60 struct input_handle
*handle
= dev
->handle
;
63 * Filter non-events, and bad input values out.
66 if (type
> EV_MAX
|| !test_bit(type
, dev
->evbit
))
73 if (code
> KEY_MAX
|| !test_bit(code
, dev
->keybit
) || !!test_bit(code
, dev
->key
) == value
)
76 if (value
== 2) break;
78 change_bit(code
, dev
->key
);
80 if (test_bit(EV_REP
, dev
->evbit
) && dev
->timer
.function
) {
82 mod_timer(&dev
->timer
, jiffies
+ dev
->rep
[REP_DELAY
]);
83 dev
->repeat_key
= code
;
86 if (dev
->repeat_key
== code
)
87 del_timer(&dev
->timer
);
94 if (code
> ABS_MAX
|| !test_bit(code
, dev
->absbit
))
97 if (dev
->absfuzz
[code
]) {
98 if ((value
> dev
->abs
[code
] - (dev
->absfuzz
[code
] >> 1)) &&
99 (value
< dev
->abs
[code
] + (dev
->absfuzz
[code
] >> 1)))
102 if ((value
> dev
->abs
[code
] - dev
->absfuzz
[code
]) &&
103 (value
< dev
->abs
[code
] + dev
->absfuzz
[code
]))
104 value
= (dev
->abs
[code
] * 3 + value
) >> 2;
106 if ((value
> dev
->abs
[code
] - (dev
->absfuzz
[code
] << 1)) &&
107 (value
< dev
->abs
[code
] + (dev
->absfuzz
[code
] << 1)))
108 value
= (dev
->abs
[code
] + value
) >> 1;
111 if (dev
->abs
[code
] == value
)
114 dev
->abs
[code
] = value
;
119 if (code
> REL_MAX
|| !test_bit(code
, dev
->relbit
) || (value
== 0))
126 if (code
> LED_MAX
|| !test_bit(code
, dev
->ledbit
) || !!test_bit(code
, dev
->led
) == value
)
129 change_bit(code
, dev
->led
);
130 if (dev
->event
) dev
->event(dev
, type
, code
, value
);
136 if (code
> SND_MAX
|| !test_bit(code
, dev
->sndbit
) || !!test_bit(code
, dev
->snd
) == value
)
139 change_bit(code
, dev
->snd
);
140 if (dev
->event
) dev
->event(dev
, type
, code
, value
);
146 if (code
> REP_MAX
|| dev
->rep
[code
] == value
) return;
148 dev
->rep
[code
] = value
;
149 if (dev
->event
) dev
->event(dev
, type
, code
, value
);
158 add_input_randomness(((unsigned long) dev
) ^ (type
<< 24) ^ (code
<< 16) ^ value
);
162 * Distribute the event to handler modules.
167 handle
->handler
->event(handle
, type
, code
, value
);
168 handle
= handle
->dnext
;
172 static void input_repeat_key(unsigned long data
)
174 struct input_dev
*dev
= (void *) data
;
175 input_event(dev
, EV_KEY
, dev
->repeat_key
, 2);
176 mod_timer(&dev
->timer
, jiffies
+ dev
->rep
[REP_PERIOD
]);
179 int input_open_device(struct input_handle
*handle
)
182 if (handle
->dev
->open
)
183 return handle
->dev
->open(handle
->dev
);
187 void input_close_device(struct input_handle
*handle
)
189 if (handle
->dev
->close
)
190 handle
->dev
->close(handle
->dev
);
194 static void input_link_handle(struct input_handle
*handle
)
196 handle
->dnext
= handle
->dev
->handle
;
197 handle
->hnext
= handle
->handler
->handle
;
198 handle
->dev
->handle
= handle
;
199 handle
->handler
->handle
= handle
;
202 static void input_unlink_handle(struct input_handle
*handle
)
204 struct input_handle
**handleptr
;
206 handleptr
= &handle
->dev
->handle
;
207 while (*handleptr
&& (*handleptr
!= handle
))
208 handleptr
= &((*handleptr
)->dnext
);
209 *handleptr
= (*handleptr
)->dnext
;
211 handleptr
= &handle
->handler
->handle
;
212 while (*handleptr
&& (*handleptr
!= handle
))
213 handleptr
= &((*handleptr
)->hnext
);
214 *handleptr
= (*handleptr
)->hnext
;
217 void input_register_device(struct input_dev
*dev
)
219 struct input_handler
*handler
= input_handler
;
220 struct input_handle
*handle
;
223 * Initialize repeat timer to default values.
226 init_timer(&dev
->timer
);
227 dev
->timer
.data
= (long) dev
;
228 dev
->timer
.function
= input_repeat_key
;
229 dev
->rep
[REP_DELAY
] = HZ
/4;
230 dev
->rep
[REP_PERIOD
] = HZ
/33;
236 if (input_number
>= INPUT_DEVICES
) {
237 printk(KERN_WARNING
"input: ran out of input device numbers!\n");
238 dev
->number
= input_number
;
240 dev
->number
= find_first_zero_bit(input_devices
, INPUT_DEVICES
);
241 set_bit(dev
->number
, input_devices
);
244 dev
->next
= input_dev
;
253 if ((handle
= handler
->connect(handler
, dev
)))
254 input_link_handle(handle
);
255 handler
= handler
->next
;
259 void input_unregister_device(struct input_dev
*dev
)
261 struct input_handle
*handle
= dev
->handle
;
262 struct input_dev
**devptr
= &input_dev
;
263 struct input_handle
*dnext
;
266 * Kill any pending repeat timers.
269 del_timer(&dev
->timer
);
276 dnext
= handle
->dnext
;
277 input_unlink_handle(handle
);
278 handle
->handler
->disconnect(handle
);
286 while (*devptr
&& (*devptr
!= dev
))
287 devptr
= &((*devptr
)->next
);
288 *devptr
= (*devptr
)->next
;
292 if (dev
->number
< INPUT_DEVICES
)
293 clear_bit(dev
->number
, input_devices
);
296 void input_register_handler(struct input_handler
*handler
)
298 struct input_dev
*dev
= input_dev
;
299 struct input_handle
*handle
;
302 * Add minors if needed.
305 if (handler
->fops
!= NULL
)
306 input_table
[handler
->minor
>> 5] = handler
;
312 handler
->next
= input_handler
;
313 input_handler
= handler
;
316 * Notify it about all existing devices.
320 if ((handle
= handler
->connect(handler
, dev
)))
321 input_link_handle(handle
);
326 void input_unregister_handler(struct input_handler
*handler
)
328 struct input_handler
**handlerptr
= &input_handler
;
329 struct input_handle
*handle
= handler
->handle
;
330 struct input_handle
*hnext
;
333 * Tell the handler to disconnect from all devices it keeps open.
337 hnext
= handle
->hnext
;
338 input_unlink_handle(handle
);
339 handler
->disconnect(handle
);
347 while (*handlerptr
&& (*handlerptr
!= handler
))
348 handlerptr
= &((*handlerptr
)->next
);
350 *handlerptr
= (*handlerptr
)->next
;
356 if (handler
->fops
!= NULL
)
357 input_table
[handler
->minor
>> 5] = NULL
;
360 static int input_open_file(struct inode
*inode
, struct file
*file
)
362 struct input_handler
*handler
= input_table
[MINOR(inode
->i_rdev
) >> 5];
363 struct file_operations
*old_fops
, *new_fops
= NULL
;
366 /* No load-on-demand here? */
367 if (!handler
|| !(new_fops
= fops_get(handler
->fops
)))
371 * That's _really_ odd. Usually NULL ->open means "nothing special",
372 * not "no device". Oh, well...
374 if (!new_fops
->open
) {
378 old_fops
= file
->f_op
;
379 file
->f_op
= new_fops
;
380 err
= new_fops
->open(inode
, file
);
382 fops_put(file
->f_op
);
383 file
->f_op
= fops_get(old_fops
);
389 static struct file_operations input_fops
= {
391 open
: input_open_file
,
394 devfs_handle_t
input_register_minor(char *name
, int minor
, int minor_base
)
397 sprintf(devfs_name
, name
, minor
);
398 return devfs_register(input_devfs_handle
, devfs_name
, DEVFS_FL_DEFAULT
, INPUT_MAJOR
, minor
+ minor_base
,
399 S_IFCHR
| S_IRUGO
| S_IWUSR
, &input_fops
, NULL
);
402 void input_unregister_minor(devfs_handle_t handle
)
404 devfs_unregister(handle
);
407 static int __init
input_init(void)
409 if (devfs_register_chrdev(INPUT_MAJOR
, "input", &input_fops
)) {
410 printk(KERN_ERR
"input: unable to register char major %d", INPUT_MAJOR
);
413 input_devfs_handle
= devfs_mk_dir(NULL
, "input", NULL
);
417 static void __exit
input_exit(void)
419 devfs_unregister(input_devfs_handle
);
420 if (devfs_unregister_chrdev(INPUT_MAJOR
, "input"))
421 printk(KERN_ERR
"input: can't unregister char major %d", INPUT_MAJOR
);
424 module_init(input_init
);
425 module_exit(input_exit
);