blob: 915e9ab7cab0faad95ad5391b9493eb946bb1d7f [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/smp_lock.h>
15#include <linux/input.h>
16#include <linux/module.h>
17#include <linux/random.h>
18#include <linux/major.h>
19#include <linux/proc_fs.h>
Dmitry Torokhov969b21c2006-04-02 00:09:34 -050020#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/interrupt.h>
22#include <linux/poll.h>
23#include <linux/device.h>
Jes Sorensene676c232006-02-19 00:21:46 -050024#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>");
27MODULE_DESCRIPTION("Input core");
28MODULE_LICENSE("GPL");
29
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#define INPUT_DEVICES 256
31
32static LIST_HEAD(input_dev_list);
33static LIST_HEAD(input_handler_list);
34
35static struct input_handler *input_table[8];
36
Dmitry Torokhov0e739d22006-07-06 00:22:43 -040037/**
38 * input_event() - report new input event
Dmitry Torokhov14471902006-11-02 23:26:55 -050039 * @dev: device that generated the event
Dmitry Torokhov0e739d22006-07-06 00:22:43 -040040 * @type: type of the event
41 * @code: event code
42 * @value: value of the event
43 *
44 * This function should be used by drivers implementing various input devices
45 * See also input_inject_event()
46 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070047void input_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
48{
49 struct input_handle *handle;
50
51 if (type > EV_MAX || !test_bit(type, dev->evbit))
52 return;
53
54 add_input_randomness(type, code, value);
55
56 switch (type) {
57
58 case EV_SYN:
59 switch (code) {
60 case SYN_CONFIG:
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -040061 if (dev->event)
62 dev->event(dev, type, code, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 break;
64
65 case SYN_REPORT:
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -040066 if (dev->sync)
67 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 dev->sync = 1;
69 break;
70 }
71 break;
72
73 case EV_KEY:
74
75 if (code > KEY_MAX || !test_bit(code, dev->keybit) || !!test_bit(code, dev->key) == value)
76 return;
77
78 if (value == 2)
79 break;
80
81 change_bit(code, dev->key);
82
83 if (test_bit(EV_REP, dev->evbit) && dev->rep[REP_PERIOD] && dev->rep[REP_DELAY] && dev->timer.data && value) {
84 dev->repeat_key = code;
85 mod_timer(&dev->timer, jiffies + msecs_to_jiffies(dev->rep[REP_DELAY]));
86 }
87
88 break;
89
Richard Purdie31581062005-09-06 15:19:06 -070090 case EV_SW:
91
92 if (code > SW_MAX || !test_bit(code, dev->swbit) || !!test_bit(code, dev->sw) == value)
93 return;
94
95 change_bit(code, dev->sw);
96
97 break;
98
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 case EV_ABS:
100
101 if (code > ABS_MAX || !test_bit(code, dev->absbit))
102 return;
103
104 if (dev->absfuzz[code]) {
105 if ((value > dev->abs[code] - (dev->absfuzz[code] >> 1)) &&
106 (value < dev->abs[code] + (dev->absfuzz[code] >> 1)))
107 return;
108
109 if ((value > dev->abs[code] - dev->absfuzz[code]) &&
110 (value < dev->abs[code] + dev->absfuzz[code]))
111 value = (dev->abs[code] * 3 + value) >> 2;
112
113 if ((value > dev->abs[code] - (dev->absfuzz[code] << 1)) &&
114 (value < dev->abs[code] + (dev->absfuzz[code] << 1)))
115 value = (dev->abs[code] + value) >> 1;
116 }
117
118 if (dev->abs[code] == value)
119 return;
120
121 dev->abs[code] = value;
122 break;
123
124 case EV_REL:
125
126 if (code > REL_MAX || !test_bit(code, dev->relbit) || (value == 0))
127 return;
128
129 break;
130
131 case EV_MSC:
132
133 if (code > MSC_MAX || !test_bit(code, dev->mscbit))
134 return;
135
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400136 if (dev->event)
137 dev->event(dev, type, code, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
139 break;
140
141 case EV_LED:
142
143 if (code > LED_MAX || !test_bit(code, dev->ledbit) || !!test_bit(code, dev->led) == value)
144 return;
145
146 change_bit(code, dev->led);
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400147
148 if (dev->event)
149 dev->event(dev, type, code, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
151 break;
152
153 case EV_SND:
154
155 if (code > SND_MAX || !test_bit(code, dev->sndbit))
156 return;
157
Dmitry Torokhov8fdc1942006-04-29 01:13:48 -0400158 if (!!test_bit(code, dev->snd) != !!value)
159 change_bit(code, dev->snd);
160
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400161 if (dev->event)
162 dev->event(dev, type, code, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
164 break;
165
166 case EV_REP:
167
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400168 if (code > REP_MAX || value < 0 || dev->rep[code] == value)
169 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
171 dev->rep[code] = value;
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400172 if (dev->event)
173 dev->event(dev, type, code, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
175 break;
176
177 case EV_FF:
Anssi Hannula509ca1a2006-07-19 01:40:22 -0400178
179 if (value < 0)
180 return;
181
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400182 if (dev->event)
183 dev->event(dev, type, code, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 break;
185 }
186
187 if (type != EV_SYN)
188 dev->sync = 0;
189
190 if (dev->grab)
191 dev->grab->handler->event(dev->grab, type, code, value);
192 else
193 list_for_each_entry(handle, &dev->h_list, d_node)
194 if (handle->open)
195 handle->handler->event(handle, type, code, value);
196}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -0400197EXPORT_SYMBOL(input_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
Dmitry Torokhov0e739d22006-07-06 00:22:43 -0400199/**
200 * input_inject_event() - send input event from input handler
201 * @handle: input handle to send event through
202 * @type: type of the event
203 * @code: event code
204 * @value: value of the event
205 *
206 * Similar to input_event() but will ignore event if device is "grabbed" and handle
207 * injecting event is not the one that owns the device.
208 */
209void input_inject_event(struct input_handle *handle, unsigned int type, unsigned int code, int value)
210{
211 if (!handle->dev->grab || handle->dev->grab == handle)
212 input_event(handle->dev, type, code, value);
213}
214EXPORT_SYMBOL(input_inject_event);
215
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216static void input_repeat_key(unsigned long data)
217{
218 struct input_dev *dev = (void *) data;
219
220 if (!test_bit(dev->repeat_key, dev->key))
221 return;
222
223 input_event(dev, EV_KEY, dev->repeat_key, 2);
224 input_sync(dev);
225
226 if (dev->rep[REP_PERIOD])
227 mod_timer(&dev->timer, jiffies + msecs_to_jiffies(dev->rep[REP_PERIOD]));
228}
229
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230int input_grab_device(struct input_handle *handle)
231{
232 if (handle->dev->grab)
233 return -EBUSY;
234
235 handle->dev->grab = handle;
236 return 0;
237}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -0400238EXPORT_SYMBOL(input_grab_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
240void input_release_device(struct input_handle *handle)
241{
Andrew Mortona2b2ed22006-07-15 01:17:38 -0400242 struct input_dev *dev = handle->dev;
Dmitry Torokhovc7e8dc62006-07-06 00:21:03 -0400243
Andrew Mortona2b2ed22006-07-15 01:17:38 -0400244 if (dev->grab == handle) {
245 dev->grab = NULL;
246
247 list_for_each_entry(handle, &dev->h_list, d_node)
Dmitry Torokhovc7e8dc62006-07-06 00:21:03 -0400248 if (handle->handler->start)
249 handle->handler->start(handle);
250 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -0400252EXPORT_SYMBOL(input_release_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
254int input_open_device(struct input_handle *handle)
255{
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500256 struct input_dev *dev = handle->dev;
257 int err;
258
Jes Sorensene676c232006-02-19 00:21:46 -0500259 err = mutex_lock_interruptible(&dev->mutex);
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500260 if (err)
261 return err;
262
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 handle->open++;
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500264
265 if (!dev->users++ && dev->open)
266 err = dev->open(dev);
267
268 if (err)
269 handle->open--;
270
Jes Sorensene676c232006-02-19 00:21:46 -0500271 mutex_unlock(&dev->mutex);
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500272
273 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -0400275EXPORT_SYMBOL(input_open_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
277int input_flush_device(struct input_handle* handle, struct file* file)
278{
279 if (handle->dev->flush)
280 return handle->dev->flush(handle->dev, file);
281
282 return 0;
283}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -0400284EXPORT_SYMBOL(input_flush_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
286void input_close_device(struct input_handle *handle)
287{
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500288 struct input_dev *dev = handle->dev;
289
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 input_release_device(handle);
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500291
Jes Sorensene676c232006-02-19 00:21:46 -0500292 mutex_lock(&dev->mutex);
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500293
294 if (!--dev->users && dev->close)
295 dev->close(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 handle->open--;
Dmitry Torokhov0fbf87c2005-05-29 02:29:25 -0500297
Jes Sorensene676c232006-02-19 00:21:46 -0500298 mutex_unlock(&dev->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -0400300EXPORT_SYMBOL(input_close_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
Marvin Raaijmakersc8e4c772007-03-14 22:50:42 -0400302static int input_fetch_keycode(struct input_dev *dev, int scancode)
303{
304 switch (dev->keycodesize) {
305 case 1:
306 return ((u8 *)dev->keycode)[scancode];
307
308 case 2:
309 return ((u16 *)dev->keycode)[scancode];
310
311 default:
312 return ((u32 *)dev->keycode)[scancode];
313 }
314}
315
316static int input_default_getkeycode(struct input_dev *dev,
317 int scancode, int *keycode)
318{
319 if (!dev->keycodesize)
320 return -EINVAL;
321
322 if (scancode < 0 || scancode >= dev->keycodemax)
323 return -EINVAL;
324
325 *keycode = input_fetch_keycode(dev, scancode);
326
327 return 0;
328}
329
330static int input_default_setkeycode(struct input_dev *dev,
331 int scancode, int keycode)
332{
333 int old_keycode;
334 int i;
335
336 if (scancode < 0 || scancode >= dev->keycodemax)
337 return -EINVAL;
338
339 if (keycode < 0 || keycode > KEY_MAX)
340 return -EINVAL;
341
342 if (!dev->keycodesize)
343 return -EINVAL;
344
345 if (dev->keycodesize < sizeof(keycode) && (keycode >> (dev->keycodesize * 8)))
346 return -EINVAL;
347
348 switch (dev->keycodesize) {
349 case 1: {
350 u8 *k = (u8 *)dev->keycode;
351 old_keycode = k[scancode];
352 k[scancode] = keycode;
353 break;
354 }
355 case 2: {
356 u16 *k = (u16 *)dev->keycode;
357 old_keycode = k[scancode];
358 k[scancode] = keycode;
359 break;
360 }
361 default: {
362 u32 *k = (u32 *)dev->keycode;
363 old_keycode = k[scancode];
364 k[scancode] = keycode;
365 break;
366 }
367 }
368
369 clear_bit(old_keycode, dev->keybit);
370 set_bit(keycode, dev->keybit);
371
372 for (i = 0; i < dev->keycodemax; i++) {
373 if (input_fetch_keycode(dev, i) == old_keycode) {
374 set_bit(old_keycode, dev->keybit);
375 break; /* Setting the bit twice is useless, so break */
376 }
377 }
378
379 return 0;
380}
381
382
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383#define MATCH_BIT(bit, max) \
384 for (i = 0; i < NBITS(max); i++) \
385 if ((id->bit[i] & dev->bit[i]) != id->bit[i]) \
386 break; \
387 if (i != NBITS(max)) \
388 continue;
389
Dmitry Torokhov66e66112006-09-14 01:31:59 -0400390static const struct input_device_id *input_match_device(const struct input_device_id *id,
391 struct input_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392{
393 int i;
394
395 for (; id->flags || id->driver_info; id++) {
396
397 if (id->flags & INPUT_DEVICE_ID_MATCH_BUS)
Dmitry Torokhovddc5d342006-04-26 00:14:19 -0400398 if (id->bustype != dev->id.bustype)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 continue;
400
401 if (id->flags & INPUT_DEVICE_ID_MATCH_VENDOR)
Dmitry Torokhovddc5d342006-04-26 00:14:19 -0400402 if (id->vendor != dev->id.vendor)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 continue;
404
405 if (id->flags & INPUT_DEVICE_ID_MATCH_PRODUCT)
Dmitry Torokhovddc5d342006-04-26 00:14:19 -0400406 if (id->product != dev->id.product)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 continue;
408
409 if (id->flags & INPUT_DEVICE_ID_MATCH_VERSION)
Dmitry Torokhovddc5d342006-04-26 00:14:19 -0400410 if (id->version != dev->id.version)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 continue;
412
413 MATCH_BIT(evbit, EV_MAX);
414 MATCH_BIT(keybit, KEY_MAX);
415 MATCH_BIT(relbit, REL_MAX);
416 MATCH_BIT(absbit, ABS_MAX);
417 MATCH_BIT(mscbit, MSC_MAX);
418 MATCH_BIT(ledbit, LED_MAX);
419 MATCH_BIT(sndbit, SND_MAX);
420 MATCH_BIT(ffbit, FF_MAX);
Dmitry Torokhovff13f982005-09-24 02:02:29 -0500421 MATCH_BIT(swbit, SW_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
423 return id;
424 }
425
426 return NULL;
427}
428
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -0400429static int input_attach_handler(struct input_dev *dev, struct input_handler *handler)
430{
431 const struct input_device_id *id;
432 int error;
433
434 if (handler->blacklist && input_match_device(handler->blacklist, dev))
435 return -ENODEV;
436
437 id = input_match_device(handler->id_table, dev);
438 if (!id)
439 return -ENODEV;
440
441 error = handler->connect(handler, dev, id);
442 if (error && error != -ENODEV)
443 printk(KERN_ERR
444 "input: failed to attach handler %s to device %s, "
445 "error: %d\n",
446 handler->name, kobject_name(&dev->cdev.kobj), error);
447
448 return error;
449}
450
451
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452#ifdef CONFIG_PROC_FS
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500453
454static struct proc_dir_entry *proc_bus_input_dir;
455static DECLARE_WAIT_QUEUE_HEAD(input_devices_poll_wait);
456static int input_devices_state;
457
458static inline void input_wakeup_procfs_readers(void)
459{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 input_devices_state++;
461 wake_up(&input_devices_poll_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462}
463
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500464static unsigned int input_proc_devices_poll(struct file *file, poll_table *wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465{
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500466 int state = input_devices_state;
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400467
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500468 poll_wait(file, &input_devices_poll_wait, wait);
469 if (state != input_devices_state)
470 return POLLIN | POLLRDNORM;
Dmitry Torokhov1e0afb22006-06-26 01:48:47 -0400471
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500472 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473}
474
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500475static struct list_head *list_get_nth_element(struct list_head *list, loff_t *pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476{
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500477 struct list_head *node;
478 loff_t i = 0;
479
480 list_for_each(node, list)
481 if (i++ == *pos)
482 return node;
483
484 return NULL;
485}
486
487static struct list_head *list_get_next_element(struct list_head *list, struct list_head *element, loff_t *pos)
488{
489 if (element->next == list)
490 return NULL;
491
492 ++(*pos);
493 return element->next;
494}
495
496static void *input_devices_seq_start(struct seq_file *seq, loff_t *pos)
497{
498 /* acquire lock here ... Yes, we do need locking, I knowi, I know... */
499
500 return list_get_nth_element(&input_dev_list, pos);
501}
502
503static void *input_devices_seq_next(struct seq_file *seq, void *v, loff_t *pos)
504{
505 return list_get_next_element(&input_dev_list, v, pos);
506}
507
508static void input_devices_seq_stop(struct seq_file *seq, void *v)
509{
510 /* release lock here */
511}
512
513static void input_seq_print_bitmap(struct seq_file *seq, const char *name,
514 unsigned long *bitmap, int max)
515{
516 int i;
517
518 for (i = NBITS(max) - 1; i > 0; i--)
519 if (bitmap[i])
520 break;
521
522 seq_printf(seq, "B: %s=", name);
523 for (; i >= 0; i--)
524 seq_printf(seq, "%lx%s", bitmap[i], i > 0 ? " " : "");
525 seq_putc(seq, '\n');
526}
527
528static int input_devices_seq_show(struct seq_file *seq, void *v)
529{
530 struct input_dev *dev = container_of(v, struct input_dev, node);
531 const char *path = kobject_get_path(&dev->cdev.kobj, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 struct input_handle *handle;
533
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500534 seq_printf(seq, "I: Bus=%04x Vendor=%04x Product=%04x Version=%04x\n",
535 dev->id.bustype, dev->id.vendor, dev->id.product, dev->id.version);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500537 seq_printf(seq, "N: Name=\"%s\"\n", dev->name ? dev->name : "");
538 seq_printf(seq, "P: Phys=%s\n", dev->phys ? dev->phys : "");
539 seq_printf(seq, "S: Sysfs=%s\n", path ? path : "");
Dmitry Torokhov15e03ae2007-03-07 23:20:17 -0500540 seq_printf(seq, "U: Uniq=%s\n", dev->uniq ? dev->uniq : "");
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500541 seq_printf(seq, "H: Handlers=");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500543 list_for_each_entry(handle, &dev->h_list, d_node)
544 seq_printf(seq, "%s ", handle->name);
545 seq_putc(seq, '\n');
Dmitry Torokhov051b2fe2005-09-15 02:01:54 -0500546
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500547 input_seq_print_bitmap(seq, "EV", dev->evbit, EV_MAX);
548 if (test_bit(EV_KEY, dev->evbit))
549 input_seq_print_bitmap(seq, "KEY", dev->keybit, KEY_MAX);
550 if (test_bit(EV_REL, dev->evbit))
551 input_seq_print_bitmap(seq, "REL", dev->relbit, REL_MAX);
552 if (test_bit(EV_ABS, dev->evbit))
553 input_seq_print_bitmap(seq, "ABS", dev->absbit, ABS_MAX);
554 if (test_bit(EV_MSC, dev->evbit))
555 input_seq_print_bitmap(seq, "MSC", dev->mscbit, MSC_MAX);
556 if (test_bit(EV_LED, dev->evbit))
557 input_seq_print_bitmap(seq, "LED", dev->ledbit, LED_MAX);
558 if (test_bit(EV_SND, dev->evbit))
559 input_seq_print_bitmap(seq, "SND", dev->sndbit, SND_MAX);
560 if (test_bit(EV_FF, dev->evbit))
561 input_seq_print_bitmap(seq, "FF", dev->ffbit, FF_MAX);
562 if (test_bit(EV_SW, dev->evbit))
563 input_seq_print_bitmap(seq, "SW", dev->swbit, SW_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500565 seq_putc(seq, '\n');
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500567 kfree(path);
568 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569}
570
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500571static struct seq_operations input_devices_seq_ops = {
572 .start = input_devices_seq_start,
573 .next = input_devices_seq_next,
574 .stop = input_devices_seq_stop,
575 .show = input_devices_seq_show,
576};
577
578static int input_proc_devices_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579{
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500580 return seq_open(file, &input_devices_seq_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581}
582
Arjan van de Ven2b8693c2007-02-12 00:55:32 -0800583static const struct file_operations input_devices_fileops = {
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500584 .owner = THIS_MODULE,
585 .open = input_proc_devices_open,
586 .poll = input_proc_devices_poll,
587 .read = seq_read,
588 .llseek = seq_lseek,
589 .release = seq_release,
590};
591
592static void *input_handlers_seq_start(struct seq_file *seq, loff_t *pos)
593{
594 /* acquire lock here ... Yes, we do need locking, I knowi, I know... */
595 seq->private = (void *)(unsigned long)*pos;
596 return list_get_nth_element(&input_handler_list, pos);
597}
598
599static void *input_handlers_seq_next(struct seq_file *seq, void *v, loff_t *pos)
600{
601 seq->private = (void *)(unsigned long)(*pos + 1);
602 return list_get_next_element(&input_handler_list, v, pos);
603}
604
605static void input_handlers_seq_stop(struct seq_file *seq, void *v)
606{
607 /* release lock here */
608}
609
610static int input_handlers_seq_show(struct seq_file *seq, void *v)
611{
612 struct input_handler *handler = container_of(v, struct input_handler, node);
613
614 seq_printf(seq, "N: Number=%ld Name=%s",
615 (unsigned long)seq->private, handler->name);
616 if (handler->fops)
617 seq_printf(seq, " Minor=%d", handler->minor);
618 seq_putc(seq, '\n');
619
620 return 0;
621}
622static struct seq_operations input_handlers_seq_ops = {
623 .start = input_handlers_seq_start,
624 .next = input_handlers_seq_next,
625 .stop = input_handlers_seq_stop,
626 .show = input_handlers_seq_show,
627};
628
629static int input_proc_handlers_open(struct inode *inode, struct file *file)
630{
631 return seq_open(file, &input_handlers_seq_ops);
632}
633
Arjan van de Ven2b8693c2007-02-12 00:55:32 -0800634static const struct file_operations input_handlers_fileops = {
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500635 .owner = THIS_MODULE,
636 .open = input_proc_handlers_open,
637 .read = seq_read,
638 .llseek = seq_lseek,
639 .release = seq_release,
640};
Luke Kosewskie334016fc2005-06-01 02:39:28 -0500641
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642static int __init input_proc_init(void)
643{
644 struct proc_dir_entry *entry;
645
646 proc_bus_input_dir = proc_mkdir("input", proc_bus);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500647 if (!proc_bus_input_dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 return -ENOMEM;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500649
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 proc_bus_input_dir->owner = THIS_MODULE;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500651
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500652 entry = create_proc_entry("devices", 0, proc_bus_input_dir);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500653 if (!entry)
654 goto fail1;
655
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 entry->owner = THIS_MODULE;
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500657 entry->proc_fops = &input_devices_fileops;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500658
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500659 entry = create_proc_entry("handlers", 0, proc_bus_input_dir);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500660 if (!entry)
661 goto fail2;
662
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 entry->owner = THIS_MODULE;
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500664 entry->proc_fops = &input_handlers_fileops;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500665
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 return 0;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500667
668 fail2: remove_proc_entry("devices", proc_bus_input_dir);
669 fail1: remove_proc_entry("input", proc_bus);
670 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671}
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500672
Andrew Mortonbeffbdc2005-07-01 23:54:30 -0500673static void input_proc_exit(void)
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500674{
675 remove_proc_entry("devices", proc_bus_input_dir);
676 remove_proc_entry("handlers", proc_bus_input_dir);
677 remove_proc_entry("input", proc_bus);
678}
679
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680#else /* !CONFIG_PROC_FS */
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500681static inline void input_wakeup_procfs_readers(void) { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682static inline int input_proc_init(void) { return 0; }
Dmitry Torokhovf96b4342005-06-30 00:50:29 -0500683static inline void input_proc_exit(void) { }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684#endif
685
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -0500686#define INPUT_DEV_STRING_ATTR_SHOW(name) \
687static ssize_t input_dev_show_##name(struct class_device *dev, char *buf) \
688{ \
689 struct input_dev *input_dev = to_input_dev(dev); \
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -0500690 \
Dmitry Torokhov1efa7702007-02-18 01:40:37 -0500691 return scnprintf(buf, PAGE_SIZE, "%s\n", \
692 input_dev->name ? input_dev->name : ""); \
Greg Kroah-Hartman629b77a2005-10-27 22:25:43 -0700693} \
694static CLASS_DEVICE_ATTR(name, S_IRUGO, input_dev_show_##name, NULL);
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -0500695
696INPUT_DEV_STRING_ATTR_SHOW(name);
697INPUT_DEV_STRING_ATTR_SHOW(phys);
698INPUT_DEV_STRING_ATTR_SHOW(uniq);
699
Dmitry Torokhovac648a62006-04-02 00:09:51 -0500700static int input_print_modalias_bits(char *buf, int size,
701 char name, unsigned long *bm,
702 unsigned int min_bit, unsigned int max_bit)
Rusty Russell1d8f4302005-12-07 21:40:34 +0100703{
Dmitry Torokhovac648a62006-04-02 00:09:51 -0500704 int len = 0, i;
Rusty Russell1d8f4302005-12-07 21:40:34 +0100705
Dmitry Torokhovac648a62006-04-02 00:09:51 -0500706 len += snprintf(buf, max(size, 0), "%c", name);
707 for (i = min_bit; i < max_bit; i++)
708 if (bm[LONG(i)] & BIT(i))
709 len += snprintf(buf + len, max(size - len, 0), "%X,", i);
Kay Sieversbd37e5a2006-01-05 13:19:55 +0100710 return len;
711}
712
Dmitry Torokhov2db66872006-04-02 00:09:26 -0500713static int input_print_modalias(char *buf, int size, struct input_dev *id,
714 int add_cr)
Kay Sieversbd37e5a2006-01-05 13:19:55 +0100715{
716 int len;
717
Dmitry Torokhovac648a62006-04-02 00:09:51 -0500718 len = snprintf(buf, max(size, 0),
719 "input:b%04Xv%04Xp%04Xe%04X-",
720 id->id.bustype, id->id.vendor,
721 id->id.product, id->id.version);
Kay Sieversbd37e5a2006-01-05 13:19:55 +0100722
Dmitry Torokhovac648a62006-04-02 00:09:51 -0500723 len += input_print_modalias_bits(buf + len, size - len,
724 'e', id->evbit, 0, EV_MAX);
725 len += input_print_modalias_bits(buf + len, size - len,
726 'k', id->keybit, KEY_MIN_INTERESTING, KEY_MAX);
727 len += input_print_modalias_bits(buf + len, size - len,
728 'r', id->relbit, 0, REL_MAX);
729 len += input_print_modalias_bits(buf + len, size - len,
730 'a', id->absbit, 0, ABS_MAX);
731 len += input_print_modalias_bits(buf + len, size - len,
732 'm', id->mscbit, 0, MSC_MAX);
733 len += input_print_modalias_bits(buf + len, size - len,
734 'l', id->ledbit, 0, LED_MAX);
735 len += input_print_modalias_bits(buf + len, size - len,
736 's', id->sndbit, 0, SND_MAX);
737 len += input_print_modalias_bits(buf + len, size - len,
738 'f', id->ffbit, 0, FF_MAX);
739 len += input_print_modalias_bits(buf + len, size - len,
740 'w', id->swbit, 0, SW_MAX);
Dmitry Torokhov2db66872006-04-02 00:09:26 -0500741
742 if (add_cr)
Dmitry Torokhovac648a62006-04-02 00:09:51 -0500743 len += snprintf(buf + len, max(size - len, 0), "\n");
Dmitry Torokhov2db66872006-04-02 00:09:26 -0500744
Rusty Russell1d8f4302005-12-07 21:40:34 +0100745 return len;
746}
747
748static ssize_t input_dev_show_modalias(struct class_device *dev, char *buf)
749{
750 struct input_dev *id = to_input_dev(dev);
Kay Sieversbd37e5a2006-01-05 13:19:55 +0100751 ssize_t len;
Rusty Russell1d8f4302005-12-07 21:40:34 +0100752
Dmitry Torokhov2db66872006-04-02 00:09:26 -0500753 len = input_print_modalias(buf, PAGE_SIZE, id, 1);
754
Richard Purdie8a3cf452006-06-26 01:48:21 -0400755 return min_t(int, len, PAGE_SIZE);
Rusty Russell1d8f4302005-12-07 21:40:34 +0100756}
757static CLASS_DEVICE_ATTR(modalias, S_IRUGO, input_dev_show_modalias, NULL);
758
Greg Kroah-Hartman629b77a2005-10-27 22:25:43 -0700759static struct attribute *input_dev_attrs[] = {
760 &class_device_attr_name.attr,
761 &class_device_attr_phys.attr,
762 &class_device_attr_uniq.attr,
Rusty Russell1d8f4302005-12-07 21:40:34 +0100763 &class_device_attr_modalias.attr,
Greg Kroah-Hartman629b77a2005-10-27 22:25:43 -0700764 NULL
765};
766
Dmitry Torokhovbd0ef232005-11-20 00:56:31 -0500767static struct attribute_group input_dev_attr_group = {
Greg Kroah-Hartman629b77a2005-10-27 22:25:43 -0700768 .attrs = input_dev_attrs,
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -0500769};
770
771#define INPUT_DEV_ID_ATTR(name) \
772static ssize_t input_dev_show_id_##name(struct class_device *dev, char *buf) \
773{ \
774 struct input_dev *input_dev = to_input_dev(dev); \
Dmitry Torokhov2db66872006-04-02 00:09:26 -0500775 return scnprintf(buf, PAGE_SIZE, "%04x\n", input_dev->id.name); \
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -0500776} \
777static CLASS_DEVICE_ATTR(name, S_IRUGO, input_dev_show_id_##name, NULL);
778
779INPUT_DEV_ID_ATTR(bustype);
780INPUT_DEV_ID_ATTR(vendor);
781INPUT_DEV_ID_ATTR(product);
782INPUT_DEV_ID_ATTR(version);
783
784static struct attribute *input_dev_id_attrs[] = {
785 &class_device_attr_bustype.attr,
786 &class_device_attr_vendor.attr,
787 &class_device_attr_product.attr,
788 &class_device_attr_version.attr,
789 NULL
790};
791
792static struct attribute_group input_dev_id_attr_group = {
793 .name = "id",
794 .attrs = input_dev_id_attrs,
795};
796
Dmitry Torokhov969b21c2006-04-02 00:09:34 -0500797static int input_print_bitmap(char *buf, int buf_size, unsigned long *bitmap,
798 int max, int add_cr)
799{
800 int i;
801 int len = 0;
802
803 for (i = NBITS(max) - 1; i > 0; i--)
804 if (bitmap[i])
805 break;
806
807 for (; i >= 0; i--)
808 len += snprintf(buf + len, max(buf_size - len, 0),
809 "%lx%s", bitmap[i], i > 0 ? " " : "");
810
811 if (add_cr)
812 len += snprintf(buf + len, max(buf_size - len, 0), "\n");
813
814 return len;
815}
816
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -0500817#define INPUT_DEV_CAP_ATTR(ev, bm) \
818static ssize_t input_dev_show_cap_##bm(struct class_device *dev, char *buf) \
819{ \
820 struct input_dev *input_dev = to_input_dev(dev); \
Dmitry Torokhov2db66872006-04-02 00:09:26 -0500821 int len = input_print_bitmap(buf, PAGE_SIZE, \
822 input_dev->bm##bit, ev##_MAX, 1); \
823 return min_t(int, len, PAGE_SIZE); \
Dmitry Torokhov5c1e9a62005-09-15 02:01:55 -0500824} \
825static CLASS_DEVICE_ATTR(bm, S_IRUGO, input_dev_show_cap_##bm, NULL);
826
827INPUT_DEV_CAP_ATTR(EV, ev);
828INPUT_DEV_CAP_ATTR(KEY, key);
829INPUT_DEV_CAP_ATTR(REL, rel);
830INPUT_DEV_CAP_ATTR(ABS, abs);
831INPUT_DEV_CAP_ATTR(MSC, msc);
832INPUT_DEV_CAP_ATTR(LED, led);
833INPUT_DEV_CAP_ATTR(SND, snd);
834INPUT_DEV_CAP_ATTR(FF, ff);
835INPUT_DEV_CAP_ATTR(SW, sw);
836
837static struct attribute *input_dev_caps_attrs[] = {
838 &class_device_attr_ev.attr,
839 &class_device_attr_key.attr,
840 &class_device_attr_rel.attr,
841 &class_device_attr_abs.attr,
842 &class_device_attr_msc.attr,
843 &class_device_attr_led.attr,
844 &class_device_attr_snd.attr,
845 &class_device_attr_ff.attr,
846 &class_device_attr_sw.attr,
847 NULL
848};
849
850static struct attribute_group input_dev_caps_attr_group = {
851 .name = "capabilities",
852 .attrs = input_dev_caps_attrs,
853};
854
Dmitry Torokhovcb9def42007-03-07 23:20:26 -0500855static struct attribute_group *input_dev_attr_groups[] = {
856 &input_dev_attr_group,
857 &input_dev_id_attr_group,
858 &input_dev_caps_attr_group,
859 NULL
860};
861
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -0500862static void input_dev_release(struct class_device *class_dev)
863{
864 struct input_dev *dev = to_input_dev(class_dev);
865
Anssi Hannula509ca1a2006-07-19 01:40:22 -0400866 input_ff_destroy(dev);
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -0500867 kfree(dev);
Anssi Hannula509ca1a2006-07-19 01:40:22 -0400868
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -0500869 module_put(THIS_MODULE);
870}
871
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500872/*
Kay Sievers312c0042005-11-16 09:00:00 +0100873 * Input uevent interface - loading event handlers based on
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500874 * device bitfields.
875 */
Kay Sievers312c0042005-11-16 09:00:00 +0100876static int input_add_uevent_bm_var(char **envp, int num_envp, int *cur_index,
Dmitry Torokhovac648a62006-04-02 00:09:51 -0500877 char *buffer, int buffer_size, int *cur_len,
878 const char *name, unsigned long *bitmap, int max)
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500879{
880 if (*cur_index >= num_envp - 1)
881 return -ENOMEM;
882
883 envp[*cur_index] = buffer + *cur_len;
884
885 *cur_len += snprintf(buffer + *cur_len, max(buffer_size - *cur_len, 0), name);
Dmitry Torokhovac648a62006-04-02 00:09:51 -0500886 if (*cur_len >= buffer_size)
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500887 return -ENOMEM;
888
889 *cur_len += input_print_bitmap(buffer + *cur_len,
890 max(buffer_size - *cur_len, 0),
Dmitry Torokhov2db66872006-04-02 00:09:26 -0500891 bitmap, max, 0) + 1;
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500892 if (*cur_len > buffer_size)
893 return -ENOMEM;
894
895 (*cur_index)++;
896 return 0;
897}
898
Dmitry Torokhovac648a62006-04-02 00:09:51 -0500899static int input_add_uevent_modalias_var(char **envp, int num_envp, int *cur_index,
900 char *buffer, int buffer_size, int *cur_len,
901 struct input_dev *dev)
902{
903 if (*cur_index >= num_envp - 1)
904 return -ENOMEM;
905
906 envp[*cur_index] = buffer + *cur_len;
907
908 *cur_len += snprintf(buffer + *cur_len, max(buffer_size - *cur_len, 0),
909 "MODALIAS=");
910 if (*cur_len >= buffer_size)
911 return -ENOMEM;
912
913 *cur_len += input_print_modalias(buffer + *cur_len,
914 max(buffer_size - *cur_len, 0),
915 dev, 0) + 1;
916 if (*cur_len > buffer_size)
917 return -ENOMEM;
918
919 (*cur_index)++;
920 return 0;
921}
922
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500923#define INPUT_ADD_HOTPLUG_VAR(fmt, val...) \
924 do { \
Dmitry Torokhovac648a62006-04-02 00:09:51 -0500925 int err = add_uevent_var(envp, num_envp, &i, \
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500926 buffer, buffer_size, &len, \
927 fmt, val); \
928 if (err) \
929 return err; \
930 } while (0)
931
932#define INPUT_ADD_HOTPLUG_BM_VAR(name, bm, max) \
933 do { \
Kay Sievers312c0042005-11-16 09:00:00 +0100934 int err = input_add_uevent_bm_var(envp, num_envp, &i, \
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500935 buffer, buffer_size, &len, \
936 name, bm, max); \
937 if (err) \
938 return err; \
939 } while (0)
940
Dmitry Torokhovac648a62006-04-02 00:09:51 -0500941#define INPUT_ADD_HOTPLUG_MODALIAS_VAR(dev) \
942 do { \
943 int err = input_add_uevent_modalias_var(envp, \
944 num_envp, &i, \
945 buffer, buffer_size, &len, \
946 dev); \
947 if (err) \
948 return err; \
949 } while (0)
950
Kay Sievers312c0042005-11-16 09:00:00 +0100951static int input_dev_uevent(struct class_device *cdev, char **envp,
952 int num_envp, char *buffer, int buffer_size)
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500953{
954 struct input_dev *dev = to_input_dev(cdev);
955 int i = 0;
956 int len = 0;
957
958 INPUT_ADD_HOTPLUG_VAR("PRODUCT=%x/%x/%x/%x",
959 dev->id.bustype, dev->id.vendor,
960 dev->id.product, dev->id.version);
961 if (dev->name)
962 INPUT_ADD_HOTPLUG_VAR("NAME=\"%s\"", dev->name);
963 if (dev->phys)
964 INPUT_ADD_HOTPLUG_VAR("PHYS=\"%s\"", dev->phys);
Dmitry Torokhov08de1f02005-11-08 21:34:29 -0800965 if (dev->uniq)
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500966 INPUT_ADD_HOTPLUG_VAR("UNIQ=\"%s\"", dev->uniq);
967
968 INPUT_ADD_HOTPLUG_BM_VAR("EV=", dev->evbit, EV_MAX);
969 if (test_bit(EV_KEY, dev->evbit))
970 INPUT_ADD_HOTPLUG_BM_VAR("KEY=", dev->keybit, KEY_MAX);
971 if (test_bit(EV_REL, dev->evbit))
972 INPUT_ADD_HOTPLUG_BM_VAR("REL=", dev->relbit, REL_MAX);
973 if (test_bit(EV_ABS, dev->evbit))
974 INPUT_ADD_HOTPLUG_BM_VAR("ABS=", dev->absbit, ABS_MAX);
975 if (test_bit(EV_MSC, dev->evbit))
976 INPUT_ADD_HOTPLUG_BM_VAR("MSC=", dev->mscbit, MSC_MAX);
977 if (test_bit(EV_LED, dev->evbit))
978 INPUT_ADD_HOTPLUG_BM_VAR("LED=", dev->ledbit, LED_MAX);
979 if (test_bit(EV_SND, dev->evbit))
980 INPUT_ADD_HOTPLUG_BM_VAR("SND=", dev->sndbit, SND_MAX);
981 if (test_bit(EV_FF, dev->evbit))
982 INPUT_ADD_HOTPLUG_BM_VAR("FF=", dev->ffbit, FF_MAX);
983 if (test_bit(EV_SW, dev->evbit))
984 INPUT_ADD_HOTPLUG_BM_VAR("SW=", dev->swbit, SW_MAX);
985
Dmitry Torokhovac648a62006-04-02 00:09:51 -0500986 INPUT_ADD_HOTPLUG_MODALIAS_VAR(dev);
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500987
Kay Sieversbd37e5a2006-01-05 13:19:55 +0100988 envp[i] = NULL;
Dmitry Torokhova7fadbe2005-09-15 02:01:57 -0500989 return 0;
990}
991
Greg Kroah-Hartmanea9f2402005-10-27 22:25:43 -0700992struct class input_class = {
993 .name = "input",
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -0500994 .release = input_dev_release,
Kay Sievers312c0042005-11-16 09:00:00 +0100995 .uevent = input_dev_uevent,
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -0500996};
Dmitry Torokhovca56fe02006-06-26 01:49:21 -0400997EXPORT_SYMBOL_GPL(input_class);
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -0500998
Dmitry Torokhov14471902006-11-02 23:26:55 -0500999/**
1000 * input_allocate_device - allocate memory for new input device
1001 *
1002 * Returns prepared struct input_dev or NULL.
1003 *
1004 * NOTE: Use input_free_device() to free devices that have not been
1005 * registered; input_unregister_device() should be used for already
1006 * registered devices.
1007 */
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -05001008struct input_dev *input_allocate_device(void)
1009{
1010 struct input_dev *dev;
1011
1012 dev = kzalloc(sizeof(struct input_dev), GFP_KERNEL);
1013 if (dev) {
Greg Kroah-Hartmanea9f2402005-10-27 22:25:43 -07001014 dev->cdev.class = &input_class;
Dmitry Torokhovcb9def42007-03-07 23:20:26 -05001015 dev->cdev.groups = input_dev_attr_groups;
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -05001016 class_device_initialize(&dev->cdev);
Dmitry Torokhovf60d2b12006-06-26 01:48:36 -04001017 mutex_init(&dev->mutex);
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -05001018 INIT_LIST_HEAD(&dev->h_list);
1019 INIT_LIST_HEAD(&dev->node);
Dmitry Torokhov655816e2006-09-14 01:32:14 -04001020
1021 __module_get(THIS_MODULE);
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -05001022 }
1023
1024 return dev;
1025}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -04001026EXPORT_SYMBOL(input_allocate_device);
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -05001027
Dmitry Torokhov14471902006-11-02 23:26:55 -05001028/**
1029 * input_free_device - free memory occupied by input_dev structure
1030 * @dev: input device to free
1031 *
1032 * This function should only be used if input_register_device()
1033 * was not called yet or if it failed. Once device was registered
1034 * use input_unregister_device() and memory will be freed once last
1035 * refrence to the device is dropped.
1036 *
1037 * Device should be allocated by input_allocate_device().
1038 *
1039 * NOTE: If there are references to the input device then memory
1040 * will not be freed until last reference is dropped.
1041 */
Dmitry Torokhovf60d2b12006-06-26 01:48:36 -04001042void input_free_device(struct input_dev *dev)
1043{
Dmitry Torokhov54f9e362007-03-16 00:57:25 -04001044 if (dev)
Dmitry Torokhovf60d2b12006-06-26 01:48:36 -04001045 input_put_device(dev);
Dmitry Torokhovf60d2b12006-06-26 01:48:36 -04001046}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -04001047EXPORT_SYMBOL(input_free_device);
Dmitry Torokhovf60d2b12006-06-26 01:48:36 -04001048
Dmitry Torokhov534565f2007-04-25 00:53:18 -04001049/**
1050 * input_set_capability - mark device as capable of a certain event
1051 * @dev: device that is capable of emitting or accepting event
1052 * @type: type of the event (EV_KEY, EV_REL, etc...)
1053 * @code: event code
1054 *
1055 * In addition to setting up corresponding bit in appropriate capability
1056 * bitmap the function also adjusts dev->evbit.
1057 */
1058void input_set_capability(struct input_dev *dev, unsigned int type, unsigned int code)
1059{
1060 switch (type) {
1061 case EV_KEY:
1062 __set_bit(code, dev->keybit);
1063 break;
1064
1065 case EV_REL:
1066 __set_bit(code, dev->relbit);
1067 break;
1068
1069 case EV_ABS:
1070 __set_bit(code, dev->absbit);
1071 break;
1072
1073 case EV_MSC:
1074 __set_bit(code, dev->mscbit);
1075 break;
1076
1077 case EV_SW:
1078 __set_bit(code, dev->swbit);
1079 break;
1080
1081 case EV_LED:
1082 __set_bit(code, dev->ledbit);
1083 break;
1084
1085 case EV_SND:
1086 __set_bit(code, dev->sndbit);
1087 break;
1088
1089 case EV_FF:
1090 __set_bit(code, dev->ffbit);
1091 break;
1092
1093 default:
1094 printk(KERN_ERR
1095 "input_set_capability: unknown type %u (code %u)\n",
1096 type, code);
1097 dump_stack();
1098 return;
1099 }
1100
1101 __set_bit(type, dev->evbit);
1102}
1103EXPORT_SYMBOL(input_set_capability);
1104
Dmitry Torokhov5f945482005-11-02 22:51:46 -05001105int input_register_device(struct input_dev *dev)
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001106{
Dmitry Torokhovbd0ef232005-11-20 00:56:31 -05001107 static atomic_t input_no = ATOMIC_INIT(0);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001108 struct input_handler *handler;
Dmitry Torokhovbd0ef232005-11-20 00:56:31 -05001109 const char *path;
1110 int error;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001111
Dmitry Torokhov5f945482005-11-02 22:51:46 -05001112 set_bit(EV_SYN, dev->evbit);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001113
1114 /*
1115 * If delay and period are pre-set by the driver, then autorepeating
1116 * is handled by the driver itself and we don't do it in input.c.
1117 */
1118
1119 init_timer(&dev->timer);
1120 if (!dev->rep[REP_DELAY] && !dev->rep[REP_PERIOD]) {
1121 dev->timer.data = (long) dev;
1122 dev->timer.function = input_repeat_key;
1123 dev->rep[REP_DELAY] = 250;
1124 dev->rep[REP_PERIOD] = 33;
1125 }
1126
Marvin Raaijmakersc8e4c772007-03-14 22:50:42 -04001127 if (!dev->getkeycode)
1128 dev->getkeycode = input_default_getkeycode;
1129
1130 if (!dev->setkeycode)
1131 dev->setkeycode = input_default_setkeycode;
1132
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001133 list_add_tail(&dev->node, &input_dev_list);
1134
Dmitry Torokhovbd0ef232005-11-20 00:56:31 -05001135 snprintf(dev->cdev.class_id, sizeof(dev->cdev.class_id),
1136 "input%ld", (unsigned long) atomic_inc_return(&input_no) - 1);
1137
Dmitry Torokhov88a447a2007-04-12 01:34:47 -04001138 if (!dev->cdev.dev)
1139 dev->cdev.dev = dev->dev.parent;
1140
Dmitry Torokhovbd0ef232005-11-20 00:56:31 -05001141 error = class_device_add(&dev->cdev);
1142 if (error)
1143 return error;
1144
Dmitry Torokhovbd0ef232005-11-20 00:56:31 -05001145 path = kobject_get_path(&dev->cdev.kobj, GFP_KERNEL);
1146 printk(KERN_INFO "input: %s as %s\n",
1147 dev->name ? dev->name : "Unspecified device", path ? path : "N/A");
1148 kfree(path);
Greg Kroah-Hartman10204022005-10-27 22:25:43 -07001149
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001150 list_for_each_entry(handler, &input_handler_list, node)
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001151 input_attach_handler(dev, handler);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001152
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001153 input_wakeup_procfs_readers();
Dmitry Torokhov5f945482005-11-02 22:51:46 -05001154
1155 return 0;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001156}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -04001157EXPORT_SYMBOL(input_register_device);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001158
1159void input_unregister_device(struct input_dev *dev)
1160{
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001161 struct input_handle *handle, *next;
Dmitry Torokhov6d2750c2006-09-10 21:56:06 -04001162 int code;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001163
Dmitry Torokhov6d2750c2006-09-10 21:56:06 -04001164 for (code = 0; code <= KEY_MAX; code++)
1165 if (test_bit(code, dev->key))
1166 input_report_key(dev, code, 0);
1167 input_sync(dev);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001168
1169 del_timer_sync(&dev->timer);
1170
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001171 list_for_each_entry_safe(handle, next, &dev->h_list, d_node)
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001172 handle->handler->disconnect(handle);
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001173 WARN_ON(!list_empty(&dev->h_list));
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001174
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001175 list_del_init(&dev->node);
1176
Dmitry Torokhove7374e42006-06-27 08:30:31 -04001177 class_device_unregister(&dev->cdev);
1178
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001179 input_wakeup_procfs_readers();
1180}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -04001181EXPORT_SYMBOL(input_unregister_device);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001182
Dmitry Torokhov4263cf02006-09-14 01:32:39 -04001183int input_register_handler(struct input_handler *handler)
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001184{
1185 struct input_dev *dev;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001186
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001187 INIT_LIST_HEAD(&handler->h_list);
1188
Dmitry Torokhov4263cf02006-09-14 01:32:39 -04001189 if (handler->fops != NULL) {
1190 if (input_table[handler->minor >> 5])
1191 return -EBUSY;
1192
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001193 input_table[handler->minor >> 5] = handler;
Dmitry Torokhov4263cf02006-09-14 01:32:39 -04001194 }
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001195
1196 list_add_tail(&handler->node, &input_handler_list);
1197
1198 list_for_each_entry(dev, &input_dev_list, node)
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001199 input_attach_handler(dev, handler);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001200
1201 input_wakeup_procfs_readers();
Dmitry Torokhov4263cf02006-09-14 01:32:39 -04001202 return 0;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001203}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -04001204EXPORT_SYMBOL(input_register_handler);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001205
1206void input_unregister_handler(struct input_handler *handler)
1207{
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001208 struct input_handle *handle, *next;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001209
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001210 list_for_each_entry_safe(handle, next, &handler->h_list, h_node)
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001211 handler->disconnect(handle);
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001212 WARN_ON(!list_empty(&handler->h_list));
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001213
1214 list_del_init(&handler->node);
1215
1216 if (handler->fops != NULL)
1217 input_table[handler->minor >> 5] = NULL;
1218
1219 input_wakeup_procfs_readers();
1220}
Dmitry Torokhovca56fe02006-06-26 01:49:21 -04001221EXPORT_SYMBOL(input_unregister_handler);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001222
Dmitry Torokhov5b2a0822007-04-12 01:29:46 -04001223int input_register_handle(struct input_handle *handle)
1224{
1225 struct input_handler *handler = handle->handler;
1226
1227 list_add_tail(&handle->d_node, &handle->dev->h_list);
1228 list_add_tail(&handle->h_node, &handler->h_list);
1229
1230 if (handler->start)
1231 handler->start(handle);
1232
1233 return 0;
1234}
1235EXPORT_SYMBOL(input_register_handle);
1236
1237void input_unregister_handle(struct input_handle *handle)
1238{
1239 list_del_init(&handle->h_node);
1240 list_del_init(&handle->d_node);
1241}
1242EXPORT_SYMBOL(input_unregister_handle);
1243
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001244static int input_open_file(struct inode *inode, struct file *file)
1245{
1246 struct input_handler *handler = input_table[iminor(inode) >> 5];
Arjan van de Ven99ac48f2006-03-28 01:56:41 -08001247 const struct file_operations *old_fops, *new_fops = NULL;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001248 int err;
1249
1250 /* No load-on-demand here? */
1251 if (!handler || !(new_fops = fops_get(handler->fops)))
1252 return -ENODEV;
1253
1254 /*
1255 * That's _really_ odd. Usually NULL ->open means "nothing special",
1256 * not "no device". Oh, well...
1257 */
1258 if (!new_fops->open) {
1259 fops_put(new_fops);
1260 return -ENODEV;
1261 }
1262 old_fops = file->f_op;
1263 file->f_op = new_fops;
1264
1265 err = new_fops->open(inode, file);
1266
1267 if (err) {
1268 fops_put(file->f_op);
1269 file->f_op = fops_get(old_fops);
1270 }
1271 fops_put(old_fops);
1272 return err;
1273}
1274
Arjan van de Ven2b8693c2007-02-12 00:55:32 -08001275static const struct file_operations input_fops = {
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001276 .owner = THIS_MODULE,
1277 .open = input_open_file,
1278};
1279
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280static int __init input_init(void)
1281{
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001282 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283
Greg Kroah-Hartmanea9f2402005-10-27 22:25:43 -07001284 err = class_register(&input_class);
Dmitry Torokhovd19fbe82005-09-15 02:01:39 -05001285 if (err) {
1286 printk(KERN_ERR "input: unable to register input_dev class\n");
1287 return err;
1288 }
1289
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001290 err = input_proc_init();
1291 if (err)
Greg Kroah-Hartmanb0fdfeb2005-10-27 22:25:43 -07001292 goto fail1;
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001293
1294 err = register_chrdev(INPUT_MAJOR, "input", &input_fops);
1295 if (err) {
1296 printk(KERN_ERR "input: unable to register char major %d", INPUT_MAJOR);
Greg Kroah-Hartmanb0fdfeb2005-10-27 22:25:43 -07001297 goto fail2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 }
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001299
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001300 return 0;
1301
Greg Kroah-Hartmanb0fdfeb2005-10-27 22:25:43 -07001302 fail2: input_proc_exit();
Greg Kroah-Hartmanea9f2402005-10-27 22:25:43 -07001303 fail1: class_unregister(&input_class);
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001304 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305}
1306
1307static void __exit input_exit(void)
1308{
Dmitry Torokhovf96b4342005-06-30 00:50:29 -05001309 input_proc_exit();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 unregister_chrdev(INPUT_MAJOR, "input");
Greg Kroah-Hartmanea9f2402005-10-27 22:25:43 -07001311 class_unregister(&input_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312}
1313
1314subsys_initcall(input_init);
1315module_exit(input_exit);