Rev 494 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
494 | giacomo | 1 | /* |
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 <linuxcomp.h> |
||
14 | |||
15 | #include <linux/init.h> |
||
16 | #include <linux/sched.h> |
||
17 | #include <linux/smp_lock.h> |
||
18 | #include <linux/input.h> |
||
19 | #include <linux/module.h> |
||
20 | #include <linux/random.h> |
||
21 | #include <linux/major.h> |
||
22 | #include <linux/pm.h> |
||
23 | #include <linux/proc_fs.h> |
||
24 | #include <linux/kmod.h> |
||
25 | #include <linux/interrupt.h> |
||
26 | #include <linux/poll.h> |
||
27 | #include <linux/device.h> |
||
28 | #include <linux/devfs_fs_kernel.h> |
||
29 | |||
522 | mauro | 30 | //#define INPUT_DEBUG |
494 | giacomo | 31 | |
32 | MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>"); |
||
33 | MODULE_DESCRIPTION("Input core"); |
||
34 | MODULE_LICENSE("GPL"); |
||
35 | |||
36 | EXPORT_SYMBOL(input_register_device); |
||
37 | EXPORT_SYMBOL(input_unregister_device); |
||
38 | EXPORT_SYMBOL(input_register_handler); |
||
39 | EXPORT_SYMBOL(input_unregister_handler); |
||
40 | EXPORT_SYMBOL(input_grab_device); |
||
41 | EXPORT_SYMBOL(input_release_device); |
||
42 | EXPORT_SYMBOL(input_open_device); |
||
43 | EXPORT_SYMBOL(input_close_device); |
||
44 | EXPORT_SYMBOL(input_accept_process); |
||
45 | EXPORT_SYMBOL(input_flush_device); |
||
46 | EXPORT_SYMBOL(input_event); |
||
47 | EXPORT_SYMBOL(input_class); |
||
48 | |||
49 | #define INPUT_DEVICES 256 |
||
50 | |||
51 | static LIST_HEAD(input_dev_list); |
||
52 | static LIST_HEAD(input_handler_list); |
||
53 | |||
54 | static struct input_handler *input_table[8]; |
||
55 | |||
56 | #ifdef CONFIG_PROC_FS |
||
57 | static struct proc_dir_entry *proc_bus_input_dir; |
||
58 | DECLARE_WAIT_QUEUE_HEAD(input_devices_poll_wait); |
||
59 | static int input_devices_state; |
||
60 | #endif |
||
61 | |||
62 | static inline unsigned int ms_to_jiffies(unsigned int ms) |
||
63 | { |
||
64 | unsigned int j; |
||
65 | j = (ms * HZ + 500) / 1000; |
||
66 | return (j > 0) ? j : 1; |
||
67 | } |
||
68 | |||
69 | |||
70 | void input_event(struct input_dev *dev, unsigned int type, unsigned int code, int value) |
||
71 | { |
||
72 | struct input_handle *handle; |
||
73 | |||
74 | if (dev->pm_dev) |
||
75 | pm_access(dev->pm_dev); |
||
76 | |||
77 | if (type > EV_MAX || !test_bit(type, dev->evbit)) |
||
78 | return; |
||
79 | |||
80 | //!!!add_mouse_randomness((type << 4) ^ code ^ (code >> 4) ^ value); |
||
81 | |||
82 | switch (type) { |
||
83 | |||
84 | case EV_SYN: |
||
85 | switch (code) { |
||
86 | case SYN_CONFIG: |
||
87 | if (dev->event) dev->event(dev, type, code, value); |
||
88 | break; |
||
89 | |||
90 | case SYN_REPORT: |
||
91 | if (dev->sync) return; |
||
92 | dev->sync = 1; |
||
93 | break; |
||
94 | } |
||
95 | break; |
||
96 | |||
97 | case EV_KEY: |
||
98 | |||
99 | if (code > KEY_MAX || !test_bit(code, dev->keybit) || !!test_bit(code, dev->key) == value) |
||
100 | return; |
||
101 | |||
102 | if (value == 2) |
||
103 | break; |
||
104 | |||
105 | change_bit(code, dev->key); |
||
106 | |||
107 | if (test_bit(EV_REP, dev->evbit) && dev->rep[REP_PERIOD] && dev->timer.data && value) { |
||
108 | dev->repeat_key = code; |
||
109 | mod_timer(&dev->timer, jiffies26 + ms_to_jiffies(dev->rep[REP_DELAY])); |
||
110 | } |
||
111 | |||
112 | break; |
||
113 | |||
114 | case EV_ABS: |
||
115 | |||
116 | if (code > ABS_MAX || !test_bit(code, dev->absbit)) |
||
117 | return; |
||
118 | |||
119 | if (dev->absfuzz[code]) { |
||
120 | if ((value > dev->abs[code] - (dev->absfuzz[code] >> 1)) && |
||
121 | (value < dev->abs[code] + (dev->absfuzz[code] >> 1))) |
||
122 | return; |
||
123 | |||
124 | if ((value > dev->abs[code] - dev->absfuzz[code]) && |
||
125 | (value < dev->abs[code] + dev->absfuzz[code])) |
||
126 | value = (dev->abs[code] * 3 + value) >> 2; |
||
127 | |||
128 | if ((value > dev->abs[code] - (dev->absfuzz[code] << 1)) && |
||
129 | (value < dev->abs[code] + (dev->absfuzz[code] << 1))) |
||
130 | value = (dev->abs[code] + value) >> 1; |
||
131 | } |
||
132 | |||
133 | if (dev->abs[code] == value) |
||
134 | return; |
||
135 | |||
136 | dev->abs[code] = value; |
||
137 | break; |
||
138 | |||
139 | case EV_REL: |
||
140 | |||
141 | if (code > REL_MAX || !test_bit(code, dev->relbit) || (value == 0)) |
||
142 | return; |
||
143 | |||
144 | break; |
||
145 | |||
146 | case EV_MSC: |
||
147 | |||
148 | if (code > MSC_MAX || !test_bit(code, dev->mscbit)) |
||
149 | return; |
||
150 | |||
151 | if (dev->event) dev->event(dev, type, code, value); |
||
152 | |||
153 | break; |
||
154 | |||
155 | case EV_LED: |
||
156 | |||
157 | if (code > LED_MAX || !test_bit(code, dev->ledbit) || !!test_bit(code, dev->led) == value) |
||
158 | return; |
||
159 | |||
160 | change_bit(code, dev->led); |
||
161 | if (dev->event) dev->event(dev, type, code, value); |
||
162 | |||
163 | break; |
||
164 | |||
165 | case EV_SND: |
||
166 | |||
167 | if (code > SND_MAX || !test_bit(code, dev->sndbit)) |
||
168 | return; |
||
169 | |||
170 | if (dev->event) dev->event(dev, type, code, value); |
||
171 | |||
172 | break; |
||
173 | |||
174 | case EV_REP: |
||
175 | |||
176 | if (code > REP_MAX || value < 0 || dev->rep[code] == value) return; |
||
177 | |||
178 | dev->rep[code] = value; |
||
179 | if (dev->event) dev->event(dev, type, code, value); |
||
180 | |||
181 | break; |
||
182 | |||
183 | case EV_FF: |
||
184 | if (dev->event) dev->event(dev, type, code, value); |
||
185 | break; |
||
186 | } |
||
187 | |||
188 | if (type != EV_SYN) |
||
189 | dev->sync = 0; |
||
190 | |||
191 | if (dev->grab) |
||
192 | dev->grab->handler->event(dev->grab, type, code, value); |
||
193 | else |
||
194 | list_for_each_entry(handle, &dev->h_list, d_node) |
||
195 | if (handle->open) |
||
196 | handle->handler->event(handle, type, code, value); |
||
197 | } |
||
198 | |||
199 | static void input_repeat_key(unsigned long data) |
||
200 | { |
||
201 | struct input_dev *dev = (void *) data; |
||
202 | |||
203 | if (!test_bit(dev->repeat_key, dev->key)) |
||
204 | return; |
||
205 | |||
206 | input_event(dev, EV_KEY, dev->repeat_key, 2); |
||
207 | input_sync(dev); |
||
208 | |||
209 | mod_timer(&dev->timer, jiffies26 + ms_to_jiffies(dev->rep[REP_PERIOD])); |
||
210 | } |
||
211 | |||
212 | int input_accept_process(struct input_handle *handle, struct file *file) |
||
213 | { |
||
214 | if (handle->dev->accept) |
||
215 | return handle->dev->accept(handle->dev, file); |
||
216 | |||
217 | return 0; |
||
218 | } |
||
219 | |||
220 | int input_grab_device(struct input_handle *handle) |
||
221 | { |
||
222 | if (handle->dev->grab) |
||
223 | return -EBUSY; |
||
224 | |||
225 | handle->dev->grab = handle; |
||
226 | return 0; |
||
227 | } |
||
228 | |||
229 | void input_release_device(struct input_handle *handle) |
||
230 | { |
||
231 | if (handle->dev->grab == handle) |
||
232 | handle->dev->grab = NULL; |
||
233 | } |
||
234 | |||
235 | int input_open_device(struct input_handle *handle) |
||
236 | { |
||
237 | if (handle->dev->pm_dev) |
||
238 | pm_access(handle->dev->pm_dev); |
||
239 | handle->open++; |
||
240 | if (handle->dev->open) |
||
241 | return handle->dev->open(handle->dev); |
||
242 | return 0; |
||
243 | } |
||
244 | |||
245 | int input_flush_device(struct input_handle* handle, struct file* file) |
||
246 | { |
||
247 | if (handle->dev->flush) |
||
248 | return handle->dev->flush(handle->dev, file); |
||
249 | |||
250 | return 0; |
||
251 | } |
||
252 | |||
253 | void input_close_device(struct input_handle *handle) |
||
254 | { |
||
255 | input_release_device(handle); |
||
256 | if (handle->dev->pm_dev) |
||
257 | pm_dev_idle(handle->dev->pm_dev); |
||
258 | if (handle->dev->close) |
||
259 | handle->dev->close(handle->dev); |
||
260 | handle->open--; |
||
261 | } |
||
262 | |||
263 | static void input_link_handle(struct input_handle *handle) |
||
264 | { |
||
265 | list_add_tail(&handle->d_node, &handle->dev->h_list); |
||
266 | list_add_tail(&handle->h_node, &handle->handler->h_list); |
||
267 | } |
||
268 | |||
269 | #define MATCH_BIT(bit, max) \ |
||
270 | for (i = 0; i < NBITS(max); i++) \ |
||
271 | if ((id->bit[i] & dev->bit[i]) != id->bit[i]) \ |
||
272 | break; \ |
||
273 | if (i != NBITS(max)) \ |
||
274 | continue; |
||
275 | |||
276 | static struct input_device_id *input_match_device(struct input_device_id *id, struct input_dev *dev) |
||
277 | { |
||
278 | int i; |
||
279 | |||
280 | for (; id->flags || id->driver_info; id++) { |
||
281 | |||
282 | if (id->flags & INPUT_DEVICE_ID_MATCH_BUS) |
||
283 | if (id->id.bustype != dev->id.bustype) |
||
284 | continue; |
||
285 | |||
286 | if (id->flags & INPUT_DEVICE_ID_MATCH_VENDOR) |
||
287 | if (id->id.vendor != dev->id.vendor) |
||
288 | continue; |
||
289 | |||
290 | if (id->flags & INPUT_DEVICE_ID_MATCH_PRODUCT) |
||
291 | if (id->id.product != dev->id.product) |
||
292 | continue; |
||
293 | |||
294 | if (id->flags & INPUT_DEVICE_ID_MATCH_VERSION) |
||
295 | if (id->id.version != dev->id.version) |
||
296 | continue; |
||
297 | |||
298 | MATCH_BIT(evbit, EV_MAX); |
||
299 | MATCH_BIT(keybit, KEY_MAX); |
||
300 | MATCH_BIT(relbit, REL_MAX); |
||
301 | MATCH_BIT(absbit, ABS_MAX); |
||
302 | MATCH_BIT(mscbit, MSC_MAX); |
||
303 | MATCH_BIT(ledbit, LED_MAX); |
||
304 | MATCH_BIT(sndbit, SND_MAX); |
||
305 | MATCH_BIT(ffbit, FF_MAX); |
||
306 | |||
307 | return id; |
||
308 | } |
||
309 | |||
310 | return NULL; |
||
311 | } |
||
312 | |||
313 | /* |
||
314 | * Input hotplugging interface - loading event handlers based on |
||
315 | * device bitfields. |
||
316 | */ |
||
317 | |||
318 | #ifdef CONFIG_HOTPLUG |
||
319 | |||
320 | /* |
||
321 | * Input hotplugging invokes what /proc/sys/kernel/hotplug says |
||
322 | * (normally /sbin/hotplug) when input devices get added or removed. |
||
323 | * |
||
324 | * This invokes a user mode policy agent, typically helping to load driver |
||
325 | * or other modules, configure the device, and more. Drivers can provide |
||
326 | * a MODULE_DEVICE_TABLE to help with module loading subtasks. |
||
327 | * |
||
328 | */ |
||
329 | |||
330 | #define SPRINTF_BIT_A(bit, name, max) \ |
||
331 | do { \ |
||
332 | envp[i++] = scratch; \ |
||
333 | scratch += sprintf26(scratch, name); \ |
||
334 | for (j = NBITS(max) - 1; j >= 0; j--) \ |
||
335 | if (dev->bit[j]) break; \ |
||
336 | for (; j >= 0; j--) \ |
||
337 | scratch += sprintf26(scratch, "%lx ", dev->bit[j]); \ |
||
338 | scratch++; \ |
||
339 | } while (0) |
||
340 | |||
341 | #define SPRINTF_BIT_A2(bit, name, max, ev) \ |
||
342 | do { \ |
||
343 | if (test_bit(ev, dev->evbit)) \ |
||
344 | SPRINTF_BIT_A(bit, name, max); \ |
||
345 | } while (0) |
||
346 | |||
347 | static void input_call_hotplug(char *verb, struct input_dev *dev) |
||
348 | { |
||
349 | char *argv[3], **envp, *buf, *scratch; |
||
350 | int i = 0, j, value; |
||
351 | |||
352 | if (!hotplug_path[0]) { |
||
353 | printk(KERN_ERR "input.c: calling hotplug without a hotplug agent defined\n"); |
||
354 | return; |
||
355 | } |
||
356 | if (in_interrupt()) { |
||
357 | printk(KERN_ERR "input.c: calling hotplug from interrupt\n"); |
||
358 | return; |
||
359 | } |
||
360 | if (!current->fs->root) { |
||
361 | printk(KERN_WARNING "input.c: calling hotplug without valid filesystem\n"); |
||
362 | return; |
||
363 | } |
||
364 | if (!(envp = (char **) kmalloc(20 * sizeof(char *), GFP_KERNEL))) { |
||
365 | printk(KERN_ERR "input.c: not enough memory allocating hotplug environment\n"); |
||
366 | return; |
||
367 | } |
||
368 | if (!(buf = kmalloc(1024, GFP_KERNEL))) { |
||
369 | kfree (envp); |
||
370 | printk(KERN_ERR "input.c: not enough memory allocating hotplug environment\n"); |
||
371 | return; |
||
372 | } |
||
373 | |||
374 | argv[0] = hotplug_path; |
||
375 | argv[1] = "input"; |
||
376 | argv[2] = 0; |
||
377 | |||
378 | envp[i++] = "HOME=/"; |
||
379 | envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin"; |
||
380 | |||
381 | scratch = buf; |
||
382 | |||
383 | envp[i++] = scratch; |
||
384 | scratch += sprintf26(scratch, "ACTION=%s", verb) + 1; |
||
385 | |||
386 | envp[i++] = scratch; |
||
387 | scratch += sprintf26(scratch, "PRODUCT=%x/%x/%x/%x", |
||
388 | dev->id.bustype, dev->id.vendor, dev->id.product, dev->id.version) + 1; |
||
389 | |||
390 | if (dev->name) { |
||
391 | envp[i++] = scratch; |
||
392 | scratch += sprintf26(scratch, "NAME=%s", dev->name) + 1; |
||
393 | } |
||
394 | |||
395 | if (dev->phys) { |
||
396 | envp[i++] = scratch; |
||
397 | scratch += sprintf26(scratch, "PHYS=%s", dev->phys) + 1; |
||
398 | } |
||
399 | |||
400 | SPRINTF_BIT_A(evbit, "EV=", EV_MAX); |
||
401 | SPRINTF_BIT_A2(keybit, "KEY=", KEY_MAX, EV_KEY); |
||
402 | SPRINTF_BIT_A2(relbit, "REL=", REL_MAX, EV_REL); |
||
403 | SPRINTF_BIT_A2(absbit, "ABS=", ABS_MAX, EV_ABS); |
||
404 | SPRINTF_BIT_A2(mscbit, "MSC=", MSC_MAX, EV_MSC); |
||
405 | SPRINTF_BIT_A2(ledbit, "LED=", LED_MAX, EV_LED); |
||
406 | SPRINTF_BIT_A2(sndbit, "SND=", SND_MAX, EV_SND); |
||
407 | SPRINTF_BIT_A2(ffbit, "FF=", FF_MAX, EV_FF); |
||
408 | |||
409 | envp[i++] = 0; |
||
410 | |||
411 | #ifdef INPUT_DEBUG |
||
412 | printk(KERN_DEBUG "input.c: calling %s %s [%s %s %s %s %s]\n", |
||
413 | argv[0], argv[1], envp[0], envp[1], envp[2], envp[3], envp[4]); |
||
414 | #endif |
||
415 | |||
416 | value = call_usermodehelper(argv [0], argv, envp, 0); |
||
417 | |||
418 | kfree(buf); |
||
419 | kfree(envp); |
||
420 | |||
421 | #ifdef INPUT_DEBUG |
||
422 | if (value != 0) |
||
423 | printk(KERN_DEBUG "input.c: hotplug returned %d\n", value); |
||
424 | #endif |
||
425 | } |
||
426 | |||
427 | #endif |
||
428 | |||
429 | void input_register_device(struct input_dev *dev) |
||
430 | { |
||
431 | struct input_handle *handle; |
||
432 | struct input_handler *handler; |
||
433 | struct input_device_id *id; |
||
434 | |||
435 | set_bit(EV_SYN, dev->evbit); |
||
436 | |||
437 | /* |
||
438 | * If delay and period are pre-set by the driver, then autorepeating |
||
439 | * is handled by the driver itself and we don't do it in input.c. |
||
440 | */ |
||
441 | |||
442 | init_timer(&dev->timer); |
||
443 | if (!dev->rep[REP_DELAY] && !dev->rep[REP_PERIOD]) { |
||
444 | dev->timer.data = (long) dev; |
||
445 | dev->timer.function = input_repeat_key; |
||
446 | dev->rep[REP_DELAY] = 250; |
||
447 | dev->rep[REP_PERIOD] = 33; |
||
448 | } |
||
449 | |||
450 | INIT_LIST_HEAD(&dev->h_list); |
||
451 | list_add_tail(&dev->node, &input_dev_list); |
||
452 | |||
453 | list_for_each_entry(handler, &input_handler_list, node) |
||
454 | if ((id = input_match_device(handler->id_table, dev))) |
||
455 | if ((handle = handler->connect(handler, dev, id))) |
||
456 | input_link_handle(handle); |
||
457 | |||
458 | #ifdef CONFIG_HOTPLUG |
||
459 | input_call_hotplug("add", dev); |
||
460 | #endif |
||
461 | |||
462 | #ifdef CONFIG_PROC_FS |
||
463 | input_devices_state++; |
||
464 | wake_up(&input_devices_poll_wait); |
||
465 | #endif |
||
466 | } |
||
467 | |||
468 | void input_unregister_device(struct input_dev *dev) |
||
469 | { |
||
470 | struct list_head * node, * next; |
||
471 | |||
472 | if (!dev) return; |
||
473 | |||
474 | if (dev->pm_dev) |
||
475 | pm_unregister(dev->pm_dev); |
||
476 | |||
477 | del_timer_sync(&dev->timer); |
||
478 | |||
479 | list_for_each_safe(node, next, &dev->h_list) { |
||
480 | struct input_handle * handle = to_handle(node); |
||
481 | list_del_init(&handle->d_node); |
||
482 | list_del_init(&handle->h_node); |
||
483 | handle->handler->disconnect(handle); |
||
484 | } |
||
485 | |||
486 | #ifdef CONFIG_HOTPLUG |
||
487 | input_call_hotplug("remove", dev); |
||
488 | #endif |
||
489 | |||
490 | list_del_init(&dev->node); |
||
491 | |||
492 | #ifdef CONFIG_PROC_FS |
||
493 | input_devices_state++; |
||
494 | wake_up(&input_devices_poll_wait); |
||
495 | #endif |
||
496 | } |
||
497 | |||
498 | void input_register_handler(struct input_handler *handler) |
||
499 | { |
||
500 | struct input_dev *dev; |
||
501 | struct input_handle *handle; |
||
502 | struct input_device_id *id; |
||
503 | |||
504 | if (!handler) return; |
||
505 | |||
506 | INIT_LIST_HEAD(&handler->h_list); |
||
507 | |||
508 | if (handler->fops != NULL) |
||
509 | input_table[handler->minor >> 5] = handler; |
||
510 | |||
511 | list_add_tail(&handler->node, &input_handler_list); |
||
512 | |||
513 | list_for_each_entry(dev, &input_dev_list, node) |
||
514 | if ((id = input_match_device(handler->id_table, dev))) |
||
515 | if ((handle = handler->connect(handler, dev, id))) |
||
516 | input_link_handle(handle); |
||
517 | |||
518 | #ifdef CONFIG_PROC_FS |
||
519 | input_devices_state++; |
||
520 | wake_up(&input_devices_poll_wait); |
||
521 | #endif |
||
522 | } |
||
523 | |||
524 | void input_unregister_handler(struct input_handler *handler) |
||
525 | { |
||
526 | struct list_head * node, * next; |
||
527 | |||
528 | list_for_each_safe(node, next, &handler->h_list) { |
||
529 | struct input_handle * handle = to_handle_h(node); |
||
530 | list_del_init(&handle->h_node); |
||
531 | list_del_init(&handle->d_node); |
||
532 | handler->disconnect(handle); |
||
533 | } |
||
534 | |||
535 | list_del_init(&handler->node); |
||
536 | |||
537 | if (handler->fops != NULL) |
||
538 | input_table[handler->minor >> 5] = NULL; |
||
539 | |||
540 | #ifdef CONFIG_PROC_FS |
||
541 | input_devices_state++; |
||
542 | wake_up(&input_devices_poll_wait); |
||
543 | #endif |
||
544 | } |
||
545 | |||
546 | static int input_open_file(struct inode *inode, struct file *file) |
||
547 | { |
||
548 | struct input_handler *handler = input_table[iminor(inode) >> 5]; |
||
549 | struct file_operations *old_fops, *new_fops = NULL; |
||
550 | int err; |
||
551 | |||
552 | /* No load-on-demand here? */ |
||
553 | if (!handler || !(new_fops = fops_get(handler->fops))) |
||
554 | return -ENODEV; |
||
555 | |||
556 | /* |
||
557 | * That's _really_ odd. Usually NULL ->open means "nothing special", |
||
558 | * not "no device". Oh, well... |
||
559 | */ |
||
560 | if (!new_fops->open) { |
||
561 | fops_put(new_fops); |
||
562 | return -ENODEV; |
||
563 | } |
||
564 | old_fops = file->f_op; |
||
565 | file->f_op = new_fops; |
||
566 | |||
567 | err = new_fops->open(inode, file); |
||
568 | |||
569 | if (err) { |
||
570 | fops_put(file->f_op); |
||
571 | file->f_op = fops_get(old_fops); |
||
572 | } |
||
573 | fops_put(old_fops); |
||
574 | return err; |
||
575 | } |
||
576 | |||
577 | static struct file_operations input_fops = { |
||
578 | .owner = THIS_MODULE, |
||
579 | .open = input_open_file, |
||
580 | }; |
||
581 | |||
582 | #ifdef CONFIG_PROC_FS |
||
583 | |||
584 | #define SPRINTF_BIT_B(bit, name, max) \ |
||
585 | do { \ |
||
586 | len += sprintf26(buf + len, "B: %s", name); \ |
||
587 | for (i = NBITS(max) - 1; i >= 0; i--) \ |
||
588 | if (dev->bit[i]) break; \ |
||
589 | for (; i >= 0; i--) \ |
||
590 | len += sprintf26(buf + len, "%lx ", dev->bit[i]); \ |
||
591 | len += sprintf26(buf + len, "\n"); \ |
||
592 | } while (0) |
||
593 | |||
594 | #define SPRINTF_BIT_B2(bit, name, max, ev) \ |
||
595 | do { \ |
||
596 | if (test_bit(ev, dev->evbit)) \ |
||
597 | SPRINTF_BIT_B(bit, name, max); \ |
||
598 | } while (0) |
||
599 | |||
600 | |||
601 | static unsigned int input_devices_poll(struct file *file, poll_table *wait) |
||
602 | { |
||
603 | int state = input_devices_state; |
||
604 | poll_wait(file, &input_devices_poll_wait, wait); |
||
605 | if (state != input_devices_state) |
||
606 | return POLLIN | POLLRDNORM; |
||
607 | return 0; |
||
608 | } |
||
609 | |||
610 | static int input_devices_read(char *buf, char **start, off_t pos, int count, int *eof, void *data) |
||
611 | { |
||
612 | struct input_dev *dev; |
||
613 | struct input_handle *handle; |
||
614 | |||
615 | off_t at = 0; |
||
616 | int i, len, cnt = 0; |
||
617 | |||
618 | list_for_each_entry(dev, &input_dev_list, node) { |
||
619 | |||
620 | len = sprintf26(buf, "I: Bus=%04x Vendor=%04x Product=%04x Version=%04x\n", |
||
621 | dev->id.bustype, dev->id.vendor, dev->id.product, dev->id.version); |
||
622 | |||
623 | len += sprintf26(buf + len, "N: Name=\"%s\"\n", dev->name ? dev->name : ""); |
||
624 | len += sprintf26(buf + len, "P: Phys=%s\n", dev->phys ? dev->phys : ""); |
||
625 | len += sprintf26(buf + len, "H: Handlers="); |
||
626 | |||
627 | list_for_each_entry(handle, &dev->h_list, d_node) |
||
628 | len += sprintf26(buf + len, "%s ", handle->name); |
||
629 | |||
630 | len += sprintf26(buf + len, "\n"); |
||
631 | |||
632 | SPRINTF_BIT_B(evbit, "EV=", EV_MAX); |
||
633 | SPRINTF_BIT_B2(keybit, "KEY=", KEY_MAX, EV_KEY); |
||
634 | SPRINTF_BIT_B2(relbit, "REL=", REL_MAX, EV_REL); |
||
635 | SPRINTF_BIT_B2(absbit, "ABS=", ABS_MAX, EV_ABS); |
||
636 | SPRINTF_BIT_B2(mscbit, "MSC=", MSC_MAX, EV_MSC); |
||
637 | SPRINTF_BIT_B2(ledbit, "LED=", LED_MAX, EV_LED); |
||
638 | SPRINTF_BIT_B2(sndbit, "SND=", SND_MAX, EV_SND); |
||
639 | SPRINTF_BIT_B2(ffbit, "FF=", FF_MAX, EV_FF); |
||
640 | |||
641 | len += sprintf26(buf + len, "\n"); |
||
642 | |||
643 | at += len; |
||
644 | |||
645 | if (at >= pos) { |
||
646 | if (!*start) { |
||
647 | *start = buf + (pos - (at - len)); |
||
648 | cnt = at - pos; |
||
649 | } else cnt += len; |
||
650 | buf += len; |
||
651 | if (cnt >= count) |
||
652 | break; |
||
653 | } |
||
654 | } |
||
655 | |||
656 | if (&dev->node == &input_dev_list) |
||
657 | *eof = 1; |
||
658 | |||
659 | return (count > cnt) ? cnt : count; |
||
660 | } |
||
661 | |||
662 | static int input_handlers_read(char *buf, char **start, off_t pos, int count, int *eof, void *data) |
||
663 | { |
||
664 | struct input_handler *handler; |
||
665 | |||
666 | off_t at = 0; |
||
667 | int len = 0, cnt = 0; |
||
668 | int i = 0; |
||
669 | |||
670 | list_for_each_entry(handler, &input_handler_list, node) { |
||
671 | |||
672 | if (handler->fops) |
||
673 | len = sprintf26(buf, "N: Number=%d Name=%s Minor=%d\n", |
||
674 | i++, handler->name, handler->minor); |
||
675 | else |
||
676 | len = sprintf26(buf, "N: Number=%d Name=%s\n", |
||
677 | i++, handler->name); |
||
678 | |||
679 | at += len; |
||
680 | |||
681 | if (at >= pos) { |
||
682 | if (!*start) { |
||
683 | *start = buf + (pos - (at - len)); |
||
684 | cnt = at - pos; |
||
685 | } else cnt += len; |
||
686 | buf += len; |
||
687 | if (cnt >= count) |
||
688 | break; |
||
689 | } |
||
690 | } |
||
691 | if (&handler->node == &input_handler_list) |
||
692 | *eof = 1; |
||
693 | |||
694 | return (count > cnt) ? cnt : count; |
||
695 | } |
||
696 | |||
697 | static int __init input_proc_init(void) |
||
698 | { |
||
699 | struct proc_dir_entry *entry; |
||
700 | |||
701 | proc_bus_input_dir = proc_mkdir("input", proc_bus); |
||
702 | if (proc_bus_input_dir == NULL) |
||
703 | return -ENOMEM; |
||
704 | proc_bus_input_dir->owner = THIS_MODULE; |
||
705 | entry = create_proc_read_entry("devices", 0, proc_bus_input_dir, input_devices_read, NULL); |
||
706 | if (entry == NULL) { |
||
707 | remove_proc_entry("input", proc_bus); |
||
708 | return -ENOMEM; |
||
709 | } |
||
710 | entry->owner = THIS_MODULE; |
||
711 | entry->proc_fops->poll = input_devices_poll; |
||
712 | entry = create_proc_read_entry("handlers", 0, proc_bus_input_dir, input_handlers_read, NULL); |
||
713 | if (entry == NULL) { |
||
714 | remove_proc_entry("devices", proc_bus_input_dir); |
||
715 | remove_proc_entry("input", proc_bus); |
||
716 | return -ENOMEM; |
||
717 | } |
||
718 | entry->owner = THIS_MODULE; |
||
719 | return 0; |
||
720 | } |
||
721 | #else /* !CONFIG_PROC_FS */ |
||
722 | static inline int input_proc_init(void) { return 0; } |
||
723 | #endif |
||
724 | |||
725 | struct class input_class = { |
||
726 | .name = "input", |
||
727 | }; |
||
728 | |||
729 | /*static*/ int __init input_init(void) |
||
730 | { |
||
731 | int retval = -ENOMEM; |
||
732 | |||
733 | class_register(&input_class); |
||
734 | input_proc_init(); |
||
735 | retval = register_chrdev(INPUT_MAJOR, "input", &input_fops); |
||
736 | if (retval) { |
||
737 | printk(KERN_ERR "input: unable to register char major %d", INPUT_MAJOR); |
||
738 | remove_proc_entry("devices", proc_bus_input_dir); |
||
739 | remove_proc_entry("handlers", proc_bus_input_dir); |
||
740 | remove_proc_entry("input", proc_bus); |
||
741 | return retval; |
||
742 | } |
||
743 | |||
744 | retval = devfs_mk_dir("input"); |
||
745 | if (retval) { |
||
746 | remove_proc_entry("devices", proc_bus_input_dir); |
||
747 | remove_proc_entry("handlers", proc_bus_input_dir); |
||
748 | remove_proc_entry("input", proc_bus); |
||
749 | unregister_chrdev(INPUT_MAJOR, "input"); |
||
750 | } |
||
751 | return retval; |
||
752 | } |
||
753 | |||
754 | /*static*/ void __exit input_exit(void) |
||
755 | { |
||
756 | remove_proc_entry("devices", proc_bus_input_dir); |
||
757 | remove_proc_entry("handlers", proc_bus_input_dir); |
||
758 | remove_proc_entry("input", proc_bus); |
||
759 | |||
760 | devfs_remove("input"); |
||
761 | unregister_chrdev(INPUT_MAJOR, "input"); |
||
762 | class_unregister(&input_class); |
||
763 | } |
||
764 | |||
765 | subsys_initcall(input_init); |
||
766 | module_exit(input_exit); |