blob: 5dc361c954e2aa92a3632117bdd4575bc9f23382 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * The input core
3 *
4 * Copyright (c) 1999-2002 Vojtech Pavlik
5 */
6
7/*
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License version 2 as published by
10 * the Free Software Foundation.
11 */
12
13#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/input.h>
15#include <linux/module.h>
16#include <linux/random.h>
17#include <linux/major.h>
18#include <linux/proc_fs.h>
Dmitry Torokhov969b21c2006-04-02 00:09:34 -050019#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/interrupt.h>
21#include <linux/poll.h>
22#include <linux/device.h>
Jes Sorensene676c232006-02-19 00:21:46 -050023#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
25MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>");
26MODULE_DESCRIPTION("Input core");
27MODULE_LICENSE("GPL");
28
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#define INPUT_DEVICES 256
30
31static LIST_HEAD(input_dev_list);
32static LIST_HEAD(input_handler_list);
33
34static struct input_handler *input_table[8];
35
Dmitry Torokhov0e739d22006-07-06 00:22:43 -040036/**
37 * input_event() - report new input event
Dmitry Torokhov14471902006-11-02 23:26:55 -050038 * @dev: device that generated the event
Dmitry Torokhov0e739d22006-07-06 00:22:43 -040039 * @type: type of the event
40 * @code: event code
41 * @value: value of the event
42 *
43 * This function should be used by drivers implementing various input devices
44 * See also input_inject_event()
45 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070046void input_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
47{
48 struct input_handle *handle;
49
50 if (type > EV_MAX || !test_bit(type, dev->evbit))
51 return;
52
53 add_input_randomness(type, code, value);
54
55 switch (type) {
56
57 case EV_SYN:
58 switch (code) {
59 case SYN_CONFIG:
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -040060 if (dev->event)
61 dev->event(dev, type, code, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 break;
63
64 case SYN_REPORT:
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -040065 if (dev->sync)
66 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 dev->sync = 1;
68 break;
69 }
70 break;
71
72 case EV_KEY:
73
74 if (code > KEY_MAX || !test_bit(code, dev->keybit) || !!test_bit(code, dev->key) == value)
75 return;
76
77 if (value == 2)
78 break;
79
80 change_bit(code, dev->key);
81
82 if (test_bit(EV_REP, dev->evbit) && dev->rep[REP_PERIOD] && dev->rep[REP_DELAY] && dev->timer.data && value) {
83 dev->repeat_key = code;
84 mod_timer(&dev->timer, jiffies + msecs_to_jiffies(dev->rep[REP_DELAY]));
85 }
86
87 break;
88
Richard Purdie31581062005-09-06 15:19:06 -070089 case EV_SW:
90
91 if (code > SW_MAX || !test_bit(code, dev->swbit) || !!test_bit(code, dev->sw) == value)
92 return;
93
94 change_bit(code, dev->sw);
95
96 break;
97
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 case EV_ABS:
99
100 if (code > ABS_MAX || !test_bit(code, dev->absbit))
101 return;
102
103 if (dev->absfuzz[code]) {
104 if ((value > dev->abs[code] - (dev->absfuzz[code] >> 1)) &&
105 (value < dev->abs[code] + (dev->absfuzz[code] >> 1)))
106 return;
107
108 if ((value > dev->abs[code] - dev->absfuzz[code]) &&
109 (value < dev->abs[code] + dev->absfuzz[code]))
110 value = (dev->abs[code] * 3 + value) >> 2;
111
112 if ((value > dev->abs[code] - (dev->absfuzz[code] << 1)) &&
113 (value < dev->abs[code] + (dev->absfuzz[code] << 1)))
114 value = (dev->abs[code] + value) >> 1;
115 }
116
117 if (dev->abs[code] == value)
118 return;
119
120 dev->abs[code] = value;
121 break;
122
123 case EV_REL:
124
125 if (code > REL_MAX || !test_bit(code, dev->relbit) || (value == 0))
126 return;
127
128 break;
129
130 case EV_MSC:
131
132 if (code > MSC_MAX || !test_bit(code, dev->mscbit))
133 return;
134
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400135 if (dev->event)
136 dev->event(dev, type, code, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
138 break;
139
140 case EV_LED:
141
142 if (code > LED_MAX || !test_bit(code, dev->ledbit) || !!test_bit(code, dev->led) == value)
143 return;
144
145 change_bit(code, dev->led);
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400146
147 if (dev->event)
148 dev->event(dev, type, code, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
150 break;
151
152 case EV_SND:
153
154 if (code > SND_MAX || !test_bit(code, dev->sndbit))
155 return;
156
Dmitry Torokhov8fdc1942006-04-29 01:13:48 -0400157 if (!!test_bit(code, dev->snd) != !!value)
158 change_bit(code, dev->snd);
159
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400160 if (dev->event)
161 dev->event(dev, type, code, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
163 break;
164
165 case EV_REP:
166
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400167 if (code > REP_MAX || value < 0 || dev->rep[code] == value)
168 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
170 dev->rep[code] = value;
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400171 if (dev->event)
172 dev->event(dev, type, code, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
174 break;
175
176 case EV_FF:
Anssi Hannula509ca1a2006-07-19 01:40:22 -0400177
178 if (value < 0)
179 return;
180
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400181 if (dev->event)
182 dev->event(dev, type, code, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 break;
184 }
185
186 if (type != EV_SYN)
187 dev->sync = 0;
188
189 if (dev->grab)
190 dev->grab->handler->event(dev->grab, type, code, value);
191 else
192 list_for_each_entry(handle, &dev->h_list, d_node)
193 if (handle->open)
194 handle->handler->event(handle, type, code, value);
195}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -0400196EXPORT_SYMBOL(input_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
Dmitry Torokhov0e739d22006-07-06 00:22:43 -0400198/**
199 * input_inject_event() - send input event from input handler
200 * @handle: input handle to send event through
201 * @type: type of the event
202 * @code: event code
203 * @value: value of the event
204 *
205 * Similar to input_event() but will ignore event if device is "grabbed" and handle
206 * injecting event is not the one that owns the device.
207 */
208void input_inject_event(struct input_handle *handle, unsigned int type, unsigned int code, int value)
209{
210 if (!handle->dev->grab || handle->dev->grab == handle)
211 input_event(handle->dev, type, code, value);
212}
213EXPORT_SYMBOL(input_inject_event);
214
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215static void input_repeat_key(unsigned long data)
216{
217 struct input_dev *dev = (void *) data;
218
219 if (!test_bit(dev->repeat_key, dev->key))
220 return;
221
222 input_event(dev, EV_KEY, dev->repeat_key, 2);
223 input_sync(dev);
224
225 if (dev->rep[REP_PERIOD])
226 mod_timer(&dev->timer, jiffies + msecs_to_jiffies(dev->rep[REP_PERIOD]));
227}
228
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229int input_grab_device(struct input_handle *handle)
230{
231 if (handle->dev->grab)
232 return -EBUSY;
233
234 handle->dev->grab = handle;
235 return 0;
236}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -0400237EXPORT_SYMBOL(input_grab_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
239void input_release_device(struct input_handle *handle)
240{
Andrew Mortona2b2ed22006-07-15 01:17:38 -0400241 struct input_dev *dev = handle->dev;
Dmitry Torokhovc7e8dc62006-07-06 00:21:03 -0400242
Andrew Mortona2b2ed22006-07-15 01:17:38 -0400243 if (dev->grab == handle) {
244 dev->grab = NULL;
245
246 list_for_each_entry(handle, &dev->h_list, d_node)
Dmitry Torokhovc7e8dc62006-07-06 00:21:03 -0400247 if (handle->handler->start)
248 handle->handler->start(handle);
249 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -0400251EXPORT_SYMBOL(input_release_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
253int input_open_device(struct input_handle *handle)
254{
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500255 struct input_dev *dev = handle->dev;
256 int err;
257
Jes Sorensene676c232006-02-19 00:21:46 -0500258 err = mutex_lock_interruptible(&dev->mutex);
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500259 if (err)
260 return err;
261
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 handle->open++;
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500263
264 if (!dev->users++ && dev->open)
265 err = dev->open(dev);
266
267 if (err)
268 handle->open--;
269
Jes Sorensene676c232006-02-19 00:21:46 -0500270 mutex_unlock(&dev->mutex);
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500271
272 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -0400274EXPORT_SYMBOL(input_open_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
276int input_flush_device(struct input_handle* handle, struct file* file)
277{
278 if (handle->dev->flush)
279 return handle->dev->flush(handle->dev, file);
280
281 return 0;
282}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -0400283EXPORT_SYMBOL(input_flush_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
285void input_close_device(struct input_handle *handle)
286{
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500287 struct input_dev *dev = handle->dev;
288
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 input_release_device(handle);
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500290
Jes Sorensene676c232006-02-19 00:21:46 -0500291 mutex_lock(&dev->mutex);
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500292
293 if (!--dev->users && dev->close)
294 dev->close(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 handle->open--;
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500296
Jes Sorensene676c232006-02-19 00:21:46 -0500297 mutex_unlock(&dev->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -0400299EXPORT_SYMBOL(input_close_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
Marvin Raaijmakersc8e4c772007-03-14 22:50:42 -0400301static int input_fetch_keycode(struct input_dev *dev, int scancode)
302{
303 switch (dev->keycodesize) {
304 case 1:
305 return ((u8 *)dev->keycode)[scancode];
306
307 case 2:
308 return ((u16 *)dev->keycode)[scancode];
309
310 default:
311 return ((u32 *)dev->keycode)[scancode];
312 }
313}
314
315static int input_default_getkeycode(struct input_dev *dev,
316 int scancode, int *keycode)
317{
318 if (!dev->keycodesize)
319 return -EINVAL;
320
321 if (scancode < 0 || scancode >= dev->keycodemax)
322 return -EINVAL;
323
324 *keycode = input_fetch_keycode(dev, scancode);
325
326 return 0;
327}
328
329static int input_default_setkeycode(struct input_dev *dev,
330 int scancode, int keycode)
331{
332 int old_keycode;
333 int i;
334
335 if (scancode < 0 || scancode >= dev->keycodemax)
336 return -EINVAL;
337
338 if (keycode < 0 || keycode > KEY_MAX)
339 return -EINVAL;
340
341 if (!dev->keycodesize)
342 return -EINVAL;
343
344 if (dev->keycodesize < sizeof(keycode) && (keycode >> (dev->keycodesize * 8)))
345 return -EINVAL;
346
347 switch (dev->keycodesize) {
348 case 1: {
349 u8 *k = (u8 *)dev->keycode;
350 old_keycode = k[scancode];
351 k[scancode] = keycode;
352 break;
353 }
354 case 2: {
355 u16 *k = (u16 *)dev->keycode;
356 old_keycode = k[scancode];
357 k[scancode] = keycode;
358 break;
359 }
360 default: {
361 u32 *k = (u32 *)dev->keycode;
362 old_keycode = k[scancode];
363 k[scancode] = keycode;
364 break;
365 }
366 }
367
368 clear_bit(old_keycode, dev->keybit);
369 set_bit(keycode, dev->keybit);
370
371 for (i = 0; i < dev->keycodemax; i++) {
372 if (input_fetch_keycode(dev, i) == old_keycode) {
373 set_bit(old_keycode, dev->keybit);
374 break; /* Setting the bit twice is useless, so break */
375 }
376 }
377
378 return 0;
379}
380
381
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382#define MATCH_BIT(bit, max) \
383 for (i = 0; i < NBITS(max); i++) \
384 if ((id->bit[i] & dev->bit[i]) != id->bit[i]) \
385 break; \
386 if (i != NBITS(max)) \
387 continue;
388
Dmitry Torokhov66e66112006-09-14 01:31:59 -0400389static const struct input_device_id *input_match_device(const struct input_device_id *id,
390 struct input_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391{
392 int i;
393
394 for (; id->flags || id->driver_info; id++) {
395
396 if (id->flags & INPUT_DEVICE_ID_MATCH_BUS)
Dmitry Torokhovddc5d342006-04-26 00:14:19 -0400397 if (id->bustype != dev->id.bustype)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 continue;
399
400 if (id->flags & INPUT_DEVICE_ID_MATCH_VENDOR)
Dmitry Torokhovddc5d342006-04-26 00:14:19 -0400401 if (id->vendor != dev->id.vendor)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 continue;
403
404 if (id->flags & INPUT_DEVICE_ID_MATCH_PRODUCT)
Dmitry Torokhovddc5d342006-04-26 00:14:19 -0400405 if (id->product != dev->id.product)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 continue;
407
408 if (id->flags & INPUT_DEVICE_ID_MATCH_VERSION)
Dmitry Torokhovddc5d342006-04-26 00:14:19 -0400409 if (id->version != dev->id.version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 continue;
411
412 MATCH_BIT(evbit, EV_MAX);
413 MATCH_BIT(keybit, KEY_MAX);
414 MATCH_BIT(relbit, REL_MAX);
415 MATCH_BIT(absbit, ABS_MAX);
416 MATCH_BIT(mscbit, MSC_MAX);
417 MATCH_BIT(ledbit, LED_MAX);
418 MATCH_BIT(sndbit, SND_MAX);
419 MATCH_BIT(ffbit, FF_MAX);
Dmitry Torokhovff13f982005-09-24 02:02:29 -0500420 MATCH_BIT(swbit, SW_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
422 return id;
423 }
424
425 return NULL;
426}
427
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400428static int input_attach_handler(struct input_dev *dev, struct input_handler *handler)
429{
430 const struct input_device_id *id;
431 int error;
432
433 if (handler->blacklist && input_match_device(handler->blacklist, dev))
434 return -ENODEV;
435
436 id = input_match_device(handler->id_table, dev);
437 if (!id)
438 return -ENODEV;
439
440 error = handler->connect(handler, dev, id);
441 if (error && error != -ENODEV)
442 printk(KERN_ERR
443 "input: failed to attach handler %s to device %s, "
444 "error: %d\n",
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400445 handler->name, kobject_name(&dev->dev.kobj), error);
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400446
447 return error;
448}
449
450
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451#ifdef CONFIG_PROC_FS
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500452
453static struct proc_dir_entry *proc_bus_input_dir;
454static DECLARE_WAIT_QUEUE_HEAD(input_devices_poll_wait);
455static int input_devices_state;
456
457static inline void input_wakeup_procfs_readers(void)
458{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 input_devices_state++;
460 wake_up(&input_devices_poll_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461}
462
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500463static unsigned int input_proc_devices_poll(struct file *file, poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464{
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500465 int state = input_devices_state;
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400466
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500467 poll_wait(file, &input_devices_poll_wait, wait);
468 if (state != input_devices_state)
469 return POLLIN | POLLRDNORM;
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400470
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500471 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472}
473
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500474static void *input_devices_seq_start(struct seq_file *seq, loff_t *pos)
475{
476 /* acquire lock here ... Yes, we do need locking, I knowi, I know... */
477
Pavel Emelianovad5d9722007-07-18 00:38:32 -0400478 return seq_list_start(&input_dev_list, *pos);
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500479}
480
481static void *input_devices_seq_next(struct seq_file *seq, void *v, loff_t *pos)
482{
Pavel Emelianovad5d9722007-07-18 00:38:32 -0400483 return seq_list_next(v, &input_dev_list, pos);
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500484}
485
486static void input_devices_seq_stop(struct seq_file *seq, void *v)
487{
488 /* release lock here */
489}
490
491static void input_seq_print_bitmap(struct seq_file *seq, const char *name,
492 unsigned long *bitmap, int max)
493{
494 int i;
495
496 for (i = NBITS(max) - 1; i > 0; i--)
497 if (bitmap[i])
498 break;
499
500 seq_printf(seq, "B: %s=", name);
501 for (; i >= 0; i--)
502 seq_printf(seq, "%lx%s", bitmap[i], i > 0 ? " " : "");
503 seq_putc(seq, '\n');
504}
505
506static int input_devices_seq_show(struct seq_file *seq, void *v)
507{
508 struct input_dev *dev = container_of(v, struct input_dev, node);
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400509 const char *path = kobject_get_path(&dev->dev.kobj, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 struct input_handle *handle;
511
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500512 seq_printf(seq, "I: Bus=%04x Vendor=%04x Product=%04x Version=%04x\n",
513 dev->id.bustype, dev->id.vendor, dev->id.product, dev->id.version);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500515 seq_printf(seq, "N: Name=\"%s\"\n", dev->name ? dev->name : "");
516 seq_printf(seq, "P: Phys=%s\n", dev->phys ? dev->phys : "");
517 seq_printf(seq, "S: Sysfs=%s\n", path ? path : "");
Dmitry Torokhov15e03ae2007-03-07 23:20:17 -0500518 seq_printf(seq, "U: Uniq=%s\n", dev->uniq ? dev->uniq : "");
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500519 seq_printf(seq, "H: Handlers=");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500521 list_for_each_entry(handle, &dev->h_list, d_node)
522 seq_printf(seq, "%s ", handle->name);
523 seq_putc(seq, '\n');
Dmitry Torokhov051b2fe2005-09-15 02:01:54 -0500524
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500525 input_seq_print_bitmap(seq, "EV", dev->evbit, EV_MAX);
526 if (test_bit(EV_KEY, dev->evbit))
527 input_seq_print_bitmap(seq, "KEY", dev->keybit, KEY_MAX);
528 if (test_bit(EV_REL, dev->evbit))
529 input_seq_print_bitmap(seq, "REL", dev->relbit, REL_MAX);
530 if (test_bit(EV_ABS, dev->evbit))
531 input_seq_print_bitmap(seq, "ABS", dev->absbit, ABS_MAX);
532 if (test_bit(EV_MSC, dev->evbit))
533 input_seq_print_bitmap(seq, "MSC", dev->mscbit, MSC_MAX);
534 if (test_bit(EV_LED, dev->evbit))
535 input_seq_print_bitmap(seq, "LED", dev->ledbit, LED_MAX);
536 if (test_bit(EV_SND, dev->evbit))
537 input_seq_print_bitmap(seq, "SND", dev->sndbit, SND_MAX);
538 if (test_bit(EV_FF, dev->evbit))
539 input_seq_print_bitmap(seq, "FF", dev->ffbit, FF_MAX);
540 if (test_bit(EV_SW, dev->evbit))
541 input_seq_print_bitmap(seq, "SW", dev->swbit, SW_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500543 seq_putc(seq, '\n');
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500545 kfree(path);
546 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547}
548
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500549static struct seq_operations input_devices_seq_ops = {
550 .start = input_devices_seq_start,
551 .next = input_devices_seq_next,
552 .stop = input_devices_seq_stop,
553 .show = input_devices_seq_show,
554};
555
556static int input_proc_devices_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557{
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500558 return seq_open(file, &input_devices_seq_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559}
560
Arjan van de Ven2b8693c2007-02-12 00:55:32 -0800561static const struct file_operations input_devices_fileops = {
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500562 .owner = THIS_MODULE,
563 .open = input_proc_devices_open,
564 .poll = input_proc_devices_poll,
565 .read = seq_read,
566 .llseek = seq_lseek,
567 .release = seq_release,
568};
569
570static void *input_handlers_seq_start(struct seq_file *seq, loff_t *pos)
571{
572 /* acquire lock here ... Yes, we do need locking, I knowi, I know... */
573 seq->private = (void *)(unsigned long)*pos;
Pavel Emelianovad5d9722007-07-18 00:38:32 -0400574 return seq_list_start(&input_handler_list, *pos);
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500575}
576
577static void *input_handlers_seq_next(struct seq_file *seq, void *v, loff_t *pos)
578{
579 seq->private = (void *)(unsigned long)(*pos + 1);
Pavel Emelianovad5d9722007-07-18 00:38:32 -0400580 return seq_list_next(v, &input_handler_list, pos);
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500581}
582
583static void input_handlers_seq_stop(struct seq_file *seq, void *v)
584{
585 /* release lock here */
586}
587
588static int input_handlers_seq_show(struct seq_file *seq, void *v)
589{
590 struct input_handler *handler = container_of(v, struct input_handler, node);
591
592 seq_printf(seq, "N: Number=%ld Name=%s",
593 (unsigned long)seq->private, handler->name);
594 if (handler->fops)
595 seq_printf(seq, " Minor=%d", handler->minor);
596 seq_putc(seq, '\n');
597
598 return 0;
599}
600static struct seq_operations input_handlers_seq_ops = {
601 .start = input_handlers_seq_start,
602 .next = input_handlers_seq_next,
603 .stop = input_handlers_seq_stop,
604 .show = input_handlers_seq_show,
605};
606
607static int input_proc_handlers_open(struct inode *inode, struct file *file)
608{
609 return seq_open(file, &input_handlers_seq_ops);
610}
611
Arjan van de Ven2b8693c2007-02-12 00:55:32 -0800612static const struct file_operations input_handlers_fileops = {
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500613 .owner = THIS_MODULE,
614 .open = input_proc_handlers_open,
615 .read = seq_read,
616 .llseek = seq_lseek,
617 .release = seq_release,
618};
Luke Kosewskie334016fc2005-06-01 02:39:28 -0500619
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620static int __init input_proc_init(void)
621{
622 struct proc_dir_entry *entry;
623
624 proc_bus_input_dir = proc_mkdir("input", proc_bus);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500625 if (!proc_bus_input_dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 return -ENOMEM;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500627
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 proc_bus_input_dir->owner = THIS_MODULE;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500629
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500630 entry = create_proc_entry("devices", 0, proc_bus_input_dir);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500631 if (!entry)
632 goto fail1;
633
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 entry->owner = THIS_MODULE;
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500635 entry->proc_fops = &input_devices_fileops;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500636
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500637 entry = create_proc_entry("handlers", 0, proc_bus_input_dir);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500638 if (!entry)
639 goto fail2;
640
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 entry->owner = THIS_MODULE;
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500642 entry->proc_fops = &input_handlers_fileops;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500643
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 return 0;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500645
646 fail2: remove_proc_entry("devices", proc_bus_input_dir);
647 fail1: remove_proc_entry("input", proc_bus);
648 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649}
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500650
Andrew Mortonbeffbdc2005-07-01 23:54:30 -0500651static void input_proc_exit(void)
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500652{
653 remove_proc_entry("devices", proc_bus_input_dir);
654 remove_proc_entry("handlers", proc_bus_input_dir);
655 remove_proc_entry("input", proc_bus);
656}
657
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658#else /* !CONFIG_PROC_FS */
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500659static inline void input_wakeup_procfs_readers(void) { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660static inline int input_proc_init(void) { return 0; }
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500661static inline void input_proc_exit(void) { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662#endif
663
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400664#define INPUT_DEV_STRING_ATTR_SHOW(name) \
665static ssize_t input_dev_show_##name(struct device *dev, \
666 struct device_attribute *attr, \
667 char *buf) \
668{ \
669 struct input_dev *input_dev = to_input_dev(dev); \
670 \
671 return scnprintf(buf, PAGE_SIZE, "%s\n", \
672 input_dev->name ? input_dev->name : ""); \
673} \
674static DEVICE_ATTR(name, S_IRUGO, input_dev_show_##name, NULL)
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -0500675
676INPUT_DEV_STRING_ATTR_SHOW(name);
677INPUT_DEV_STRING_ATTR_SHOW(phys);
678INPUT_DEV_STRING_ATTR_SHOW(uniq);
679
Dmitry Torokhovac648a62006-04-02 00:09:51 -0500680static int input_print_modalias_bits(char *buf, int size,
681 char name, unsigned long *bm,
682 unsigned int min_bit, unsigned int max_bit)
Rusty Russell1d8f4302005-12-07 21:40:34 +0100683{
Dmitry Torokhovac648a62006-04-02 00:09:51 -0500684 int len = 0, i;
Rusty Russell1d8f4302005-12-07 21:40:34 +0100685
Dmitry Torokhovac648a62006-04-02 00:09:51 -0500686 len += snprintf(buf, max(size, 0), "%c", name);
687 for (i = min_bit; i < max_bit; i++)
688 if (bm[LONG(i)] & BIT(i))
689 len += snprintf(buf + len, max(size - len, 0), "%X,", i);
Kay Sieversbd37e5a2006-01-05 13:19:55 +0100690 return len;
691}
692
Dmitry Torokhov2db66872006-04-02 00:09:26 -0500693static int input_print_modalias(char *buf, int size, struct input_dev *id,
694 int add_cr)
Kay Sieversbd37e5a2006-01-05 13:19:55 +0100695{
696 int len;
697
Dmitry Torokhovac648a62006-04-02 00:09:51 -0500698 len = snprintf(buf, max(size, 0),
699 "input:b%04Xv%04Xp%04Xe%04X-",
700 id->id.bustype, id->id.vendor,
701 id->id.product, id->id.version);
Kay Sieversbd37e5a2006-01-05 13:19:55 +0100702
Dmitry Torokhovac648a62006-04-02 00:09:51 -0500703 len += input_print_modalias_bits(buf + len, size - len,
704 'e', id->evbit, 0, EV_MAX);
705 len += input_print_modalias_bits(buf + len, size - len,
706 'k', id->keybit, KEY_MIN_INTERESTING, KEY_MAX);
707 len += input_print_modalias_bits(buf + len, size - len,
708 'r', id->relbit, 0, REL_MAX);
709 len += input_print_modalias_bits(buf + len, size - len,
710 'a', id->absbit, 0, ABS_MAX);
711 len += input_print_modalias_bits(buf + len, size - len,
712 'm', id->mscbit, 0, MSC_MAX);
713 len += input_print_modalias_bits(buf + len, size - len,
714 'l', id->ledbit, 0, LED_MAX);
715 len += input_print_modalias_bits(buf + len, size - len,
716 's', id->sndbit, 0, SND_MAX);
717 len += input_print_modalias_bits(buf + len, size - len,
718 'f', id->ffbit, 0, FF_MAX);
719 len += input_print_modalias_bits(buf + len, size - len,
720 'w', id->swbit, 0, SW_MAX);
Dmitry Torokhov2db66872006-04-02 00:09:26 -0500721
722 if (add_cr)
Dmitry Torokhovac648a62006-04-02 00:09:51 -0500723 len += snprintf(buf + len, max(size - len, 0), "\n");
Dmitry Torokhov2db66872006-04-02 00:09:26 -0500724
Rusty Russell1d8f4302005-12-07 21:40:34 +0100725 return len;
726}
727
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400728static ssize_t input_dev_show_modalias(struct device *dev,
729 struct device_attribute *attr,
730 char *buf)
Rusty Russell1d8f4302005-12-07 21:40:34 +0100731{
732 struct input_dev *id = to_input_dev(dev);
Kay Sieversbd37e5a2006-01-05 13:19:55 +0100733 ssize_t len;
Rusty Russell1d8f4302005-12-07 21:40:34 +0100734
Dmitry Torokhov2db66872006-04-02 00:09:26 -0500735 len = input_print_modalias(buf, PAGE_SIZE, id, 1);
736
Richard Purdie8a3cf452006-06-26 01:48:21 -0400737 return min_t(int, len, PAGE_SIZE);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100738}
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400739static DEVICE_ATTR(modalias, S_IRUGO, input_dev_show_modalias, NULL);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100740
Greg Kroah-Hartman629b77a2005-10-27 22:25:43 -0700741static struct attribute *input_dev_attrs[] = {
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400742 &dev_attr_name.attr,
743 &dev_attr_phys.attr,
744 &dev_attr_uniq.attr,
745 &dev_attr_modalias.attr,
Greg Kroah-Hartman629b77a2005-10-27 22:25:43 -0700746 NULL
747};
748
Dmitry Torokhovbd0ef232005-11-20 00:56:31 -0500749static struct attribute_group input_dev_attr_group = {
Greg Kroah-Hartman629b77a2005-10-27 22:25:43 -0700750 .attrs = input_dev_attrs,
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -0500751};
752
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400753#define INPUT_DEV_ID_ATTR(name) \
754static ssize_t input_dev_show_id_##name(struct device *dev, \
755 struct device_attribute *attr, \
756 char *buf) \
757{ \
758 struct input_dev *input_dev = to_input_dev(dev); \
759 return scnprintf(buf, PAGE_SIZE, "%04x\n", input_dev->id.name); \
760} \
761static DEVICE_ATTR(name, S_IRUGO, input_dev_show_id_##name, NULL)
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -0500762
763INPUT_DEV_ID_ATTR(bustype);
764INPUT_DEV_ID_ATTR(vendor);
765INPUT_DEV_ID_ATTR(product);
766INPUT_DEV_ID_ATTR(version);
767
768static struct attribute *input_dev_id_attrs[] = {
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400769 &dev_attr_bustype.attr,
770 &dev_attr_vendor.attr,
771 &dev_attr_product.attr,
772 &dev_attr_version.attr,
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -0500773 NULL
774};
775
776static struct attribute_group input_dev_id_attr_group = {
777 .name = "id",
778 .attrs = input_dev_id_attrs,
779};
780
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500781static int input_print_bitmap(char *buf, int buf_size, unsigned long *bitmap,
782 int max, int add_cr)
783{
784 int i;
785 int len = 0;
786
787 for (i = NBITS(max) - 1; i > 0; i--)
788 if (bitmap[i])
789 break;
790
791 for (; i >= 0; i--)
792 len += snprintf(buf + len, max(buf_size - len, 0),
793 "%lx%s", bitmap[i], i > 0 ? " " : "");
794
795 if (add_cr)
796 len += snprintf(buf + len, max(buf_size - len, 0), "\n");
797
798 return len;
799}
800
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400801#define INPUT_DEV_CAP_ATTR(ev, bm) \
802static ssize_t input_dev_show_cap_##bm(struct device *dev, \
803 struct device_attribute *attr, \
804 char *buf) \
805{ \
806 struct input_dev *input_dev = to_input_dev(dev); \
807 int len = input_print_bitmap(buf, PAGE_SIZE, \
808 input_dev->bm##bit, ev##_MAX, 1); \
809 return min_t(int, len, PAGE_SIZE); \
810} \
811static DEVICE_ATTR(bm, S_IRUGO, input_dev_show_cap_##bm, NULL)
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -0500812
813INPUT_DEV_CAP_ATTR(EV, ev);
814INPUT_DEV_CAP_ATTR(KEY, key);
815INPUT_DEV_CAP_ATTR(REL, rel);
816INPUT_DEV_CAP_ATTR(ABS, abs);
817INPUT_DEV_CAP_ATTR(MSC, msc);
818INPUT_DEV_CAP_ATTR(LED, led);
819INPUT_DEV_CAP_ATTR(SND, snd);
820INPUT_DEV_CAP_ATTR(FF, ff);
821INPUT_DEV_CAP_ATTR(SW, sw);
822
823static struct attribute *input_dev_caps_attrs[] = {
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400824 &dev_attr_ev.attr,
825 &dev_attr_key.attr,
826 &dev_attr_rel.attr,
827 &dev_attr_abs.attr,
828 &dev_attr_msc.attr,
829 &dev_attr_led.attr,
830 &dev_attr_snd.attr,
831 &dev_attr_ff.attr,
832 &dev_attr_sw.attr,
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -0500833 NULL
834};
835
836static struct attribute_group input_dev_caps_attr_group = {
837 .name = "capabilities",
838 .attrs = input_dev_caps_attrs,
839};
840
Dmitry Torokhovcb9def42007-03-07 23:20:26 -0500841static struct attribute_group *input_dev_attr_groups[] = {
842 &input_dev_attr_group,
843 &input_dev_id_attr_group,
844 &input_dev_caps_attr_group,
845 NULL
846};
847
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400848static void input_dev_release(struct device *device)
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -0500849{
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400850 struct input_dev *dev = to_input_dev(device);
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -0500851
Anssi Hannula509ca1a2006-07-19 01:40:22 -0400852 input_ff_destroy(dev);
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -0500853 kfree(dev);
Anssi Hannula509ca1a2006-07-19 01:40:22 -0400854
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -0500855 module_put(THIS_MODULE);
856}
857
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500858/*
Kay Sievers312c0042005-11-16 09:00:00 +0100859 * Input uevent interface - loading event handlers based on
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500860 * device bitfields.
861 */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200862static int input_add_uevent_bm_var(struct kobj_uevent_env *env,
Dmitry Torokhovac648a62006-04-02 00:09:51 -0500863 const char *name, unsigned long *bitmap, int max)
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500864{
Kay Sievers7eff2e72007-08-14 15:15:12 +0200865 int len;
866
867 if (add_uevent_var(env, "%s=", name))
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500868 return -ENOMEM;
869
Kay Sievers7eff2e72007-08-14 15:15:12 +0200870 len = input_print_bitmap(&env->buf[env->buflen - 1],
871 sizeof(env->buf) - env->buflen,
872 bitmap, max, 0);
873 if (len >= (sizeof(env->buf) - env->buflen))
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500874 return -ENOMEM;
875
Kay Sievers7eff2e72007-08-14 15:15:12 +0200876 env->buflen += len;
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500877 return 0;
878}
879
Kay Sievers7eff2e72007-08-14 15:15:12 +0200880static int input_add_uevent_modalias_var(struct kobj_uevent_env *env,
Dmitry Torokhovac648a62006-04-02 00:09:51 -0500881 struct input_dev *dev)
882{
Kay Sievers7eff2e72007-08-14 15:15:12 +0200883 int len;
884
885 if (add_uevent_var(env, "MODALIAS="))
Dmitry Torokhovac648a62006-04-02 00:09:51 -0500886 return -ENOMEM;
887
Kay Sievers7eff2e72007-08-14 15:15:12 +0200888 len = input_print_modalias(&env->buf[env->buflen - 1],
889 sizeof(env->buf) - env->buflen,
890 dev, 0);
891 if (len >= (sizeof(env->buf) - env->buflen))
Dmitry Torokhovac648a62006-04-02 00:09:51 -0500892 return -ENOMEM;
893
Kay Sievers7eff2e72007-08-14 15:15:12 +0200894 env->buflen += len;
Dmitry Torokhovac648a62006-04-02 00:09:51 -0500895 return 0;
896}
897
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500898#define INPUT_ADD_HOTPLUG_VAR(fmt, val...) \
899 do { \
Kay Sievers7eff2e72007-08-14 15:15:12 +0200900 int err = add_uevent_var(env, fmt, val); \
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500901 if (err) \
902 return err; \
903 } while (0)
904
905#define INPUT_ADD_HOTPLUG_BM_VAR(name, bm, max) \
906 do { \
Kay Sievers7eff2e72007-08-14 15:15:12 +0200907 int err = input_add_uevent_bm_var(env, name, bm, max); \
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500908 if (err) \
909 return err; \
910 } while (0)
911
Dmitry Torokhovac648a62006-04-02 00:09:51 -0500912#define INPUT_ADD_HOTPLUG_MODALIAS_VAR(dev) \
913 do { \
Kay Sievers7eff2e72007-08-14 15:15:12 +0200914 int err = input_add_uevent_modalias_var(env, dev); \
Dmitry Torokhovac648a62006-04-02 00:09:51 -0500915 if (err) \
916 return err; \
917 } while (0)
918
Kay Sievers7eff2e72007-08-14 15:15:12 +0200919static int input_dev_uevent(struct device *device, struct kobj_uevent_env *env)
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500920{
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400921 struct input_dev *dev = to_input_dev(device);
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500922
923 INPUT_ADD_HOTPLUG_VAR("PRODUCT=%x/%x/%x/%x",
924 dev->id.bustype, dev->id.vendor,
925 dev->id.product, dev->id.version);
926 if (dev->name)
927 INPUT_ADD_HOTPLUG_VAR("NAME=\"%s\"", dev->name);
928 if (dev->phys)
929 INPUT_ADD_HOTPLUG_VAR("PHYS=\"%s\"", dev->phys);
Dmitry Torokhov08de1f02005-11-08 21:34:29 -0800930 if (dev->uniq)
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500931 INPUT_ADD_HOTPLUG_VAR("UNIQ=\"%s\"", dev->uniq);
932
933 INPUT_ADD_HOTPLUG_BM_VAR("EV=", dev->evbit, EV_MAX);
934 if (test_bit(EV_KEY, dev->evbit))
935 INPUT_ADD_HOTPLUG_BM_VAR("KEY=", dev->keybit, KEY_MAX);
936 if (test_bit(EV_REL, dev->evbit))
937 INPUT_ADD_HOTPLUG_BM_VAR("REL=", dev->relbit, REL_MAX);
938 if (test_bit(EV_ABS, dev->evbit))
939 INPUT_ADD_HOTPLUG_BM_VAR("ABS=", dev->absbit, ABS_MAX);
940 if (test_bit(EV_MSC, dev->evbit))
941 INPUT_ADD_HOTPLUG_BM_VAR("MSC=", dev->mscbit, MSC_MAX);
942 if (test_bit(EV_LED, dev->evbit))
943 INPUT_ADD_HOTPLUG_BM_VAR("LED=", dev->ledbit, LED_MAX);
944 if (test_bit(EV_SND, dev->evbit))
945 INPUT_ADD_HOTPLUG_BM_VAR("SND=", dev->sndbit, SND_MAX);
946 if (test_bit(EV_FF, dev->evbit))
947 INPUT_ADD_HOTPLUG_BM_VAR("FF=", dev->ffbit, FF_MAX);
948 if (test_bit(EV_SW, dev->evbit))
949 INPUT_ADD_HOTPLUG_BM_VAR("SW=", dev->swbit, SW_MAX);
950
Dmitry Torokhovac648a62006-04-02 00:09:51 -0500951 INPUT_ADD_HOTPLUG_MODALIAS_VAR(dev);
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500952
953 return 0;
954}
955
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400956static struct device_type input_dev_type = {
957 .groups = input_dev_attr_groups,
958 .release = input_dev_release,
959 .uevent = input_dev_uevent,
960};
961
Greg Kroah-Hartmanea9f2402005-10-27 22:25:43 -0700962struct class input_class = {
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400963 .name = "input",
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -0500964};
Dmitry Torokhovca56fe02006-06-26 01:49:21 -0400965EXPORT_SYMBOL_GPL(input_class);
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -0500966
Dmitry Torokhov14471902006-11-02 23:26:55 -0500967/**
968 * input_allocate_device - allocate memory for new input device
969 *
970 * Returns prepared struct input_dev or NULL.
971 *
972 * NOTE: Use input_free_device() to free devices that have not been
973 * registered; input_unregister_device() should be used for already
974 * registered devices.
975 */
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -0500976struct input_dev *input_allocate_device(void)
977{
978 struct input_dev *dev;
979
980 dev = kzalloc(sizeof(struct input_dev), GFP_KERNEL);
981 if (dev) {
Dmitry Torokhov9657d752007-06-14 23:32:24 -0400982 dev->dev.type = &input_dev_type;
983 dev->dev.class = &input_class;
984 device_initialize(&dev->dev);
Dmitry Torokhovf60d2b12006-06-26 01:48:36 -0400985 mutex_init(&dev->mutex);
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -0500986 INIT_LIST_HEAD(&dev->h_list);
987 INIT_LIST_HEAD(&dev->node);
Dmitry Torokhov655816e2006-09-14 01:32:14 -0400988
989 __module_get(THIS_MODULE);
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -0500990 }
991
992 return dev;
993}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -0400994EXPORT_SYMBOL(input_allocate_device);
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -0500995
Dmitry Torokhov14471902006-11-02 23:26:55 -0500996/**
997 * input_free_device - free memory occupied by input_dev structure
998 * @dev: input device to free
999 *
1000 * This function should only be used if input_register_device()
1001 * was not called yet or if it failed. Once device was registered
1002 * use input_unregister_device() and memory will be freed once last
1003 * refrence to the device is dropped.
1004 *
1005 * Device should be allocated by input_allocate_device().
1006 *
1007 * NOTE: If there are references to the input device then memory
1008 * will not be freed until last reference is dropped.
1009 */
Dmitry Torokhovf60d2b12006-06-26 01:48:36 -04001010void input_free_device(struct input_dev *dev)
1011{
Dmitry Torokhov54f9e362007-03-16 00:57:25 -04001012 if (dev)
Dmitry Torokhovf60d2b12006-06-26 01:48:36 -04001013 input_put_device(dev);
Dmitry Torokhovf60d2b12006-06-26 01:48:36 -04001014}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -04001015EXPORT_SYMBOL(input_free_device);
Dmitry Torokhovf60d2b12006-06-26 01:48:36 -04001016
Dmitry Torokhov534565f2007-04-25 00:53:18 -04001017/**
1018 * input_set_capability - mark device as capable of a certain event
1019 * @dev: device that is capable of emitting or accepting event
1020 * @type: type of the event (EV_KEY, EV_REL, etc...)
1021 * @code: event code
1022 *
1023 * In addition to setting up corresponding bit in appropriate capability
1024 * bitmap the function also adjusts dev->evbit.
1025 */
1026void input_set_capability(struct input_dev *dev, unsigned int type, unsigned int code)
1027{
1028 switch (type) {
1029 case EV_KEY:
1030 __set_bit(code, dev->keybit);
1031 break;
1032
1033 case EV_REL:
1034 __set_bit(code, dev->relbit);
1035 break;
1036
1037 case EV_ABS:
1038 __set_bit(code, dev->absbit);
1039 break;
1040
1041 case EV_MSC:
1042 __set_bit(code, dev->mscbit);
1043 break;
1044
1045 case EV_SW:
1046 __set_bit(code, dev->swbit);
1047 break;
1048
1049 case EV_LED:
1050 __set_bit(code, dev->ledbit);
1051 break;
1052
1053 case EV_SND:
1054 __set_bit(code, dev->sndbit);
1055 break;
1056
1057 case EV_FF:
1058 __set_bit(code, dev->ffbit);
1059 break;
1060
1061 default:
1062 printk(KERN_ERR
1063 "input_set_capability: unknown type %u (code %u)\n",
1064 type, code);
1065 dump_stack();
1066 return;
1067 }
1068
1069 __set_bit(type, dev->evbit);
1070}
1071EXPORT_SYMBOL(input_set_capability);
1072
Dmitry Torokhov5f945482005-11-02 22:51:46 -05001073int input_register_device(struct input_dev *dev)
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001074{
Dmitry Torokhovbd0ef232005-11-20 00:56:31 -05001075 static atomic_t input_no = ATOMIC_INIT(0);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001076 struct input_handler *handler;
Dmitry Torokhovbd0ef232005-11-20 00:56:31 -05001077 const char *path;
1078 int error;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001079
Dmitry Torokhov5f945482005-11-02 22:51:46 -05001080 set_bit(EV_SYN, dev->evbit);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001081
1082 /*
1083 * If delay and period are pre-set by the driver, then autorepeating
1084 * is handled by the driver itself and we don't do it in input.c.
1085 */
1086
1087 init_timer(&dev->timer);
1088 if (!dev->rep[REP_DELAY] && !dev->rep[REP_PERIOD]) {
1089 dev->timer.data = (long) dev;
1090 dev->timer.function = input_repeat_key;
1091 dev->rep[REP_DELAY] = 250;
1092 dev->rep[REP_PERIOD] = 33;
1093 }
1094
Marvin Raaijmakersc8e4c772007-03-14 22:50:42 -04001095 if (!dev->getkeycode)
1096 dev->getkeycode = input_default_getkeycode;
1097
1098 if (!dev->setkeycode)
1099 dev->setkeycode = input_default_setkeycode;
1100
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001101 list_add_tail(&dev->node, &input_dev_list);
1102
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001103 snprintf(dev->dev.bus_id, sizeof(dev->dev.bus_id),
Dmitry Torokhovbd0ef232005-11-20 00:56:31 -05001104 "input%ld", (unsigned long) atomic_inc_return(&input_no) - 1);
1105
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001106 if (dev->cdev.dev)
1107 dev->dev.parent = dev->cdev.dev;
Dmitry Torokhov88a447a2007-04-12 01:34:47 -04001108
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001109 error = device_add(&dev->dev);
Dmitry Torokhovbd0ef232005-11-20 00:56:31 -05001110 if (error)
1111 return error;
1112
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001113 path = kobject_get_path(&dev->dev.kobj, GFP_KERNEL);
Dmitry Torokhovbd0ef232005-11-20 00:56:31 -05001114 printk(KERN_INFO "input: %s as %s\n",
1115 dev->name ? dev->name : "Unspecified device", path ? path : "N/A");
1116 kfree(path);
Greg Kroah-Hartman10204022005-10-27 22:25:43 -07001117
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001118 list_for_each_entry(handler, &input_handler_list, node)
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001119 input_attach_handler(dev, handler);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001120
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001121 input_wakeup_procfs_readers();
Dmitry Torokhov5f945482005-11-02 22:51:46 -05001122
1123 return 0;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001124}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -04001125EXPORT_SYMBOL(input_register_device);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001126
1127void input_unregister_device(struct input_dev *dev)
1128{
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001129 struct input_handle *handle, *next;
Dmitry Torokhov6d2750c2006-09-10 21:56:06 -04001130 int code;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001131
Dmitry Torokhov6d2750c2006-09-10 21:56:06 -04001132 for (code = 0; code <= KEY_MAX; code++)
1133 if (test_bit(code, dev->key))
1134 input_report_key(dev, code, 0);
1135 input_sync(dev);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001136
1137 del_timer_sync(&dev->timer);
1138
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001139 list_for_each_entry_safe(handle, next, &dev->h_list, d_node)
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001140 handle->handler->disconnect(handle);
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001141 WARN_ON(!list_empty(&dev->h_list));
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001142
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001143 list_del_init(&dev->node);
1144
Dmitry Torokhov9657d752007-06-14 23:32:24 -04001145 device_unregister(&dev->dev);
Dmitry Torokhove7374e42006-06-27 08:30:31 -04001146
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001147 input_wakeup_procfs_readers();
1148}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -04001149EXPORT_SYMBOL(input_unregister_device);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001150
Dmitry Torokhov4263cf02006-09-14 01:32:39 -04001151int input_register_handler(struct input_handler *handler)
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001152{
1153 struct input_dev *dev;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001154
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001155 INIT_LIST_HEAD(&handler->h_list);
1156
Dmitry Torokhov4263cf02006-09-14 01:32:39 -04001157 if (handler->fops != NULL) {
1158 if (input_table[handler->minor >> 5])
1159 return -EBUSY;
1160
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001161 input_table[handler->minor >> 5] = handler;
Dmitry Torokhov4263cf02006-09-14 01:32:39 -04001162 }
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001163
1164 list_add_tail(&handler->node, &input_handler_list);
1165
1166 list_for_each_entry(dev, &input_dev_list, node)
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001167 input_attach_handler(dev, handler);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001168
1169 input_wakeup_procfs_readers();
Dmitry Torokhov4263cf02006-09-14 01:32:39 -04001170 return 0;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001171}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -04001172EXPORT_SYMBOL(input_register_handler);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001173
1174void input_unregister_handler(struct input_handler *handler)
1175{
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001176 struct input_handle *handle, *next;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001177
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001178 list_for_each_entry_safe(handle, next, &handler->h_list, h_node)
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001179 handler->disconnect(handle);
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001180 WARN_ON(!list_empty(&handler->h_list));
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001181
1182 list_del_init(&handler->node);
1183
1184 if (handler->fops != NULL)
1185 input_table[handler->minor >> 5] = NULL;
1186
1187 input_wakeup_procfs_readers();
1188}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -04001189EXPORT_SYMBOL(input_unregister_handler);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001190
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001191int input_register_handle(struct input_handle *handle)
1192{
1193 struct input_handler *handler = handle->handler;
1194
1195 list_add_tail(&handle->d_node, &handle->dev->h_list);
1196 list_add_tail(&handle->h_node, &handler->h_list);
1197
1198 if (handler->start)
1199 handler->start(handle);
1200
1201 return 0;
1202}
1203EXPORT_SYMBOL(input_register_handle);
1204
1205void input_unregister_handle(struct input_handle *handle)
1206{
1207 list_del_init(&handle->h_node);
1208 list_del_init(&handle->d_node);
1209}
1210EXPORT_SYMBOL(input_unregister_handle);
1211
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001212static int input_open_file(struct inode *inode, struct file *file)
1213{
1214 struct input_handler *handler = input_table[iminor(inode) >> 5];
Arjan van de Ven99ac48f2006-03-28 01:56:41 -08001215 const struct file_operations *old_fops, *new_fops = NULL;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001216 int err;
1217
1218 /* No load-on-demand here? */
1219 if (!handler || !(new_fops = fops_get(handler->fops)))
1220 return -ENODEV;
1221
1222 /*
1223 * That's _really_ odd. Usually NULL ->open means "nothing special",
1224 * not "no device". Oh, well...
1225 */
1226 if (!new_fops->open) {
1227 fops_put(new_fops);
1228 return -ENODEV;
1229 }
1230 old_fops = file->f_op;
1231 file->f_op = new_fops;
1232
1233 err = new_fops->open(inode, file);
1234
1235 if (err) {
1236 fops_put(file->f_op);
1237 file->f_op = fops_get(old_fops);
1238 }
1239 fops_put(old_fops);
1240 return err;
1241}
1242
Arjan van de Ven2b8693c2007-02-12 00:55:32 -08001243static const struct file_operations input_fops = {
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001244 .owner = THIS_MODULE,
1245 .open = input_open_file,
1246};
1247
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248static int __init input_init(void)
1249{
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001250 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
Greg Kroah-Hartmanea9f2402005-10-27 22:25:43 -07001252 err = class_register(&input_class);
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -05001253 if (err) {
1254 printk(KERN_ERR "input: unable to register input_dev class\n");
1255 return err;
1256 }
1257
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001258 err = input_proc_init();
1259 if (err)
Greg Kroah-Hartmanb0fdfeb2005-10-27 22:25:43 -07001260 goto fail1;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001261
1262 err = register_chrdev(INPUT_MAJOR, "input", &input_fops);
1263 if (err) {
1264 printk(KERN_ERR "input: unable to register char major %d", INPUT_MAJOR);
Greg Kroah-Hartmanb0fdfeb2005-10-27 22:25:43 -07001265 goto fail2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 }
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001267
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001268 return 0;
1269
Greg Kroah-Hartmanb0fdfeb2005-10-27 22:25:43 -07001270 fail2: input_proc_exit();
Greg Kroah-Hartmanea9f2402005-10-27 22:25:43 -07001271 fail1: class_unregister(&input_class);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001272 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273}
1274
1275static void __exit input_exit(void)
1276{
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001277 input_proc_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 unregister_chrdev(INPUT_MAJOR, "input");
Greg Kroah-Hartmanea9f2402005-10-27 22:25:43 -07001279 class_unregister(&input_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280}
1281
1282subsys_initcall(input_init);
1283module_exit(input_exit);