Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
494 | giacomo | 1 | /* |
2 | * Event char devices, giving access to raw input device events. |
||
3 | * |
||
4 | * Copyright (c) 1999-2002 Vojtech Pavlik |
||
5 | * |
||
6 | * This program is free software; you can redistribute it and/or modify it |
||
7 | * under the terms of the GNU General Public License version 2 as published by |
||
8 | * the Free Software Foundation. |
||
9 | */ |
||
10 | |||
11 | #include <linuxcomp.h> |
||
12 | |||
13 | #define EVDEV_MINOR_BASE 64 |
||
14 | #define EVDEV_MINORS 32 |
||
15 | #define EVDEV_BUFFER_SIZE 64 |
||
16 | |||
17 | #include <linux/poll.h> |
||
18 | #include <linux/slab.h> |
||
19 | #include <linux/module.h> |
||
20 | #include <linux/init.h> |
||
21 | #include <linux/input.h> |
||
22 | #include <linux/major.h> |
||
23 | #include <linux/smp_lock.h> |
||
24 | #include <linux/device.h> |
||
25 | #include <linux/devfs_fs_kernel.h> |
||
26 | |||
27 | struct evdev { |
||
28 | int exist; |
||
29 | int open; |
||
30 | int minor; |
||
31 | char name[16]; |
||
32 | struct input_handle handle; |
||
33 | wait_queue_head_t wait; |
||
34 | struct evdev_list *grab; |
||
35 | struct list_head list; |
||
36 | }; |
||
37 | |||
38 | struct evdev_list { |
||
39 | struct input_event buffer[EVDEV_BUFFER_SIZE]; |
||
40 | int head; |
||
41 | int tail; |
||
42 | struct fasync_struct *fasync; |
||
43 | struct evdev *evdev; |
||
44 | struct list_head node; |
||
45 | }; |
||
46 | |||
47 | static struct evdev *evdev_table[EVDEV_MINORS]; |
||
48 | |||
49 | static void evdev_event(struct input_handle *handle, unsigned int type, unsigned int code, int value) |
||
50 | { |
||
51 | struct evdev *evdev = handle->private; |
||
52 | struct evdev_list *list; |
||
53 | |||
54 | if (evdev->grab) { |
||
55 | list = evdev->grab; |
||
56 | |||
57 | do_gettimeofday(&list->buffer[list->head].time); |
||
58 | list->buffer[list->head].type = type; |
||
59 | list->buffer[list->head].code = code; |
||
60 | list->buffer[list->head].value = value; |
||
61 | list->head = (list->head + 1) & (EVDEV_BUFFER_SIZE - 1); |
||
62 | |||
63 | kill_fasync(&list->fasync, SIGIO, POLL_IN); |
||
64 | } else |
||
65 | list_for_each_entry(list, &evdev->list, node) { |
||
66 | |||
67 | do_gettimeofday(&list->buffer[list->head].time); |
||
68 | list->buffer[list->head].type = type; |
||
69 | list->buffer[list->head].code = code; |
||
70 | list->buffer[list->head].value = value; |
||
71 | list->head = (list->head + 1) & (EVDEV_BUFFER_SIZE - 1); |
||
72 | |||
73 | kill_fasync(&list->fasync, SIGIO, POLL_IN); |
||
74 | } |
||
75 | |||
76 | wake_up_interruptible(&evdev->wait); |
||
77 | } |
||
78 | |||
79 | static int evdev_fasync(int fd, struct file *file, int on) |
||
80 | { |
||
81 | int retval; |
||
82 | struct evdev_list *list = file->private_data; |
||
83 | retval = fasync_helper(fd, file, on, &list->fasync); |
||
84 | return retval < 0 ? retval : 0; |
||
85 | } |
||
86 | |||
87 | static int evdev_flush(struct file * file) |
||
88 | { |
||
89 | struct evdev_list *list = file->private_data; |
||
90 | if (!list->evdev->exist) return -ENODEV; |
||
91 | return input_flush_device(&list->evdev->handle, file); |
||
92 | } |
||
93 | |||
94 | static void evdev_free(struct evdev *evdev) |
||
95 | { |
||
96 | devfs_remove("input/event%d", evdev->minor); |
||
97 | evdev_table[evdev->minor] = NULL; |
||
98 | kfree(evdev); |
||
99 | } |
||
100 | |||
101 | static int evdev_release(struct inode * inode, struct file * file) |
||
102 | { |
||
103 | struct evdev_list *list = file->private_data; |
||
104 | |||
105 | if (list->evdev->grab == list) { |
||
106 | input_release_device(&list->evdev->handle); |
||
107 | list->evdev->grab = NULL; |
||
108 | } |
||
109 | |||
110 | evdev_fasync(-1, file, 0); |
||
111 | list_del(&list->node); |
||
112 | |||
113 | if (!--list->evdev->open) { |
||
114 | if (list->evdev->exist) |
||
115 | input_close_device(&list->evdev->handle); |
||
116 | else |
||
117 | evdev_free(list->evdev); |
||
118 | } |
||
119 | |||
120 | kfree(list); |
||
121 | return 0; |
||
122 | } |
||
123 | |||
124 | static int evdev_open(struct inode * inode, struct file * file) |
||
125 | { |
||
126 | struct evdev_list *list; |
||
127 | int i = iminor(inode) - EVDEV_MINOR_BASE; |
||
128 | int accept_err; |
||
129 | |||
130 | if (i >= EVDEV_MINORS || !evdev_table[i]) |
||
131 | return -ENODEV; |
||
132 | |||
133 | if ((accept_err = input_accept_process(&(evdev_table[i]->handle), file))) |
||
134 | return accept_err; |
||
135 | |||
136 | if (!(list = kmalloc(sizeof(struct evdev_list), GFP_KERNEL))) |
||
137 | return -ENOMEM; |
||
138 | memset(list, 0, sizeof(struct evdev_list)); |
||
139 | |||
140 | list->evdev = evdev_table[i]; |
||
141 | list_add_tail(&list->node, &evdev_table[i]->list); |
||
142 | file->private_data = list; |
||
143 | |||
144 | if (!list->evdev->open++) |
||
145 | if (list->evdev->exist) |
||
146 | input_open_device(&list->evdev->handle); |
||
147 | |||
148 | return 0; |
||
149 | } |
||
150 | |||
151 | static ssize_t evdev_write(struct file * file, const char * buffer, size_t count, loff_t *ppos) |
||
152 | { |
||
153 | struct evdev_list *list = file->private_data; |
||
154 | struct input_event event; |
||
155 | int retval = 0; |
||
156 | |||
157 | if (!list->evdev->exist) return -ENODEV; |
||
158 | |||
159 | while (retval < count) { |
||
160 | |||
161 | if (copy_from_user(&event, buffer + retval, sizeof(struct input_event))) |
||
162 | return -EFAULT; |
||
163 | input_event(list->evdev->handle.dev, event.type, event.code, event.value); |
||
164 | retval += sizeof(struct input_event); |
||
165 | } |
||
166 | |||
167 | return retval; |
||
168 | } |
||
169 | |||
170 | static ssize_t evdev_read(struct file * file, char * buffer, size_t count, loff_t *ppos) |
||
171 | { |
||
172 | struct evdev_list *list = file->private_data; |
||
173 | int retval; |
||
174 | |||
175 | if (list->head == list->tail && list->evdev->exist && (file->f_flags & O_NONBLOCK)) |
||
176 | return -EAGAIN; |
||
177 | |||
178 | retval = wait_event_interruptible(list->evdev->wait, |
||
179 | list->head != list->tail && list->evdev->exist); |
||
180 | |||
181 | if (retval) |
||
182 | return retval; |
||
183 | |||
184 | if (!list->evdev->exist) |
||
185 | return -ENODEV; |
||
186 | |||
187 | while (list->head != list->tail && retval + sizeof(struct input_event) <= count) { |
||
188 | if (copy_to_user(buffer + retval, list->buffer + list->tail, |
||
189 | sizeof(struct input_event))) return -EFAULT; |
||
190 | list->tail = (list->tail + 1) & (EVDEV_BUFFER_SIZE - 1); |
||
191 | retval += sizeof(struct input_event); |
||
192 | } |
||
193 | |||
194 | return retval; |
||
195 | } |
||
196 | |||
197 | /* No kernel lock - fine */ |
||
198 | static unsigned int evdev_poll(struct file *file, poll_table *wait) |
||
199 | { |
||
200 | struct evdev_list *list = file->private_data; |
||
201 | poll_wait(file, &list->evdev->wait, wait); |
||
202 | if (list->head != list->tail) |
||
203 | return POLLIN | POLLRDNORM; |
||
204 | return 0; |
||
205 | } |
||
206 | |||
207 | static int evdev_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg) |
||
208 | { |
||
209 | struct evdev_list *list = file->private_data; |
||
210 | struct evdev *evdev = list->evdev; |
||
211 | struct input_dev *dev = evdev->handle.dev; |
||
212 | struct input_absinfo abs; |
||
213 | int i, t, u, v; |
||
214 | |||
215 | if (!evdev->exist) return -ENODEV; |
||
216 | |||
217 | switch (cmd) { |
||
218 | |||
219 | case EVIOCGVERSION: |
||
220 | return put_user(EV_VERSION, (int *) arg); |
||
221 | |||
222 | case EVIOCGID: |
||
223 | return copy_to_user((void *) arg, &dev->id, sizeof(struct input_id)) ? -EFAULT : 0; |
||
224 | |||
225 | case EVIOCGKEYCODE: |
||
226 | if (get_user(t, ((int *) arg) + 0)) return -EFAULT; |
||
227 | if (t < 0 || t > dev->keycodemax || !dev->keycodesize) return -EINVAL; |
||
228 | if (put_user(INPUT_KEYCODE(dev, t), ((int *) arg) + 1)) return -EFAULT; |
||
229 | return 0; |
||
230 | |||
231 | case EVIOCSKEYCODE: |
||
232 | if (get_user(t, ((int *) arg) + 0)) return -EFAULT; |
||
233 | if (t < 0 || t > dev->keycodemax || !dev->keycodesize) return -EINVAL; |
||
234 | if (get_user(v, ((int *) arg) + 1)) return -EFAULT; |
||
235 | u = INPUT_KEYCODE(dev, t); |
||
236 | INPUT_KEYCODE(dev, t) = v; |
||
237 | for (i = 0; i < dev->keycodemax; i++) if (v == u) break; |
||
238 | if (i == dev->keycodemax) clear_bit(u, dev->keybit); |
||
239 | set_bit(v, dev->keybit); |
||
240 | return 0; |
||
241 | |||
242 | case EVIOCSFF: |
||
243 | if (dev->upload_effect) { |
||
244 | struct ff_effect effect; |
||
245 | int err; |
||
246 | |||
247 | if (copy_from_user((void*)(&effect), (void*)arg, sizeof(effect))) |
||
248 | return -EFAULT; |
||
249 | err = dev->upload_effect(dev, &effect); |
||
250 | if (put_user(effect.id, &(((struct ff_effect*)arg)->id))) |
||
251 | return -EFAULT; |
||
252 | return err; |
||
253 | } |
||
254 | else return -ENOSYS; |
||
255 | |||
256 | case EVIOCRMFF: |
||
257 | if (dev->erase_effect) { |
||
258 | return dev->erase_effect(dev, (int)arg); |
||
259 | } |
||
260 | else return -ENOSYS; |
||
261 | |||
262 | case EVIOCGEFFECTS: |
||
263 | if (put_user(dev->ff_effects_max, (int*) arg)) |
||
264 | return -EFAULT; |
||
265 | return 0; |
||
266 | |||
267 | case EVIOCGRAB: |
||
268 | if (arg) { |
||
269 | if (evdev->grab) |
||
270 | return -EBUSY; |
||
271 | if (input_grab_device(&evdev->handle)) |
||
272 | return -EBUSY; |
||
273 | evdev->grab = list; |
||
274 | return 0; |
||
275 | } else { |
||
276 | if (evdev->grab != list) |
||
277 | return -EINVAL; |
||
278 | input_release_device(&evdev->handle); |
||
279 | evdev->grab = NULL; |
||
280 | return 0; |
||
281 | } |
||
282 | |||
283 | default: |
||
284 | |||
285 | if (_IOC_TYPE(cmd) != 'E' || _IOC_DIR(cmd) != _IOC_READ) |
||
286 | return -EINVAL; |
||
287 | |||
288 | if ((_IOC_NR(cmd) & ~EV_MAX) == _IOC_NR(EVIOCGBIT(0,0))) { |
||
289 | |||
290 | long *bits; |
||
291 | int len; |
||
292 | |||
293 | switch (_IOC_NR(cmd) & EV_MAX) { |
||
294 | case 0: bits = dev->evbit; len = EV_MAX; break; |
||
295 | case EV_KEY: bits = dev->keybit; len = KEY_MAX; break; |
||
296 | case EV_REL: bits = dev->relbit; len = REL_MAX; break; |
||
297 | case EV_ABS: bits = dev->absbit; len = ABS_MAX; break; |
||
298 | case EV_MSC: bits = dev->mscbit; len = MSC_MAX; break; |
||
299 | case EV_LED: bits = dev->ledbit; len = LED_MAX; break; |
||
300 | case EV_SND: bits = dev->sndbit; len = SND_MAX; break; |
||
301 | case EV_FF: bits = dev->ffbit; len = FF_MAX; break; |
||
302 | default: return -EINVAL; |
||
303 | } |
||
304 | len = NBITS(len) * sizeof(long); |
||
305 | if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd); |
||
306 | return copy_to_user((char *) arg, bits, len) ? -EFAULT : len; |
||
307 | } |
||
308 | |||
309 | if (_IOC_NR(cmd) == _IOC_NR(EVIOCGKEY(0))) { |
||
310 | int len; |
||
311 | len = NBITS(KEY_MAX) * sizeof(long); |
||
312 | if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd); |
||
313 | return copy_to_user((char *) arg, dev->key, len) ? -EFAULT : len; |
||
314 | } |
||
315 | |||
316 | if (_IOC_NR(cmd) == _IOC_NR(EVIOCGLED(0))) { |
||
317 | int len; |
||
318 | len = NBITS(LED_MAX) * sizeof(long); |
||
319 | if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd); |
||
320 | return copy_to_user((char *) arg, dev->led, len) ? -EFAULT : len; |
||
321 | } |
||
322 | |||
323 | if (_IOC_NR(cmd) == _IOC_NR(EVIOCGSND(0))) { |
||
324 | int len; |
||
325 | len = NBITS(SND_MAX) * sizeof(long); |
||
326 | if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd); |
||
327 | return copy_to_user((char *) arg, dev->snd, len) ? -EFAULT : len; |
||
328 | } |
||
329 | |||
330 | if (_IOC_NR(cmd) == _IOC_NR(EVIOCGNAME(0))) { |
||
331 | int len; |
||
332 | if (!dev->name) return -ENOENT; |
||
333 | len = strlen(dev->name) + 1; |
||
334 | if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd); |
||
335 | return copy_to_user((char *) arg, dev->name, len) ? -EFAULT : len; |
||
336 | } |
||
337 | |||
338 | if (_IOC_NR(cmd) == _IOC_NR(EVIOCGPHYS(0))) { |
||
339 | int len; |
||
340 | if (!dev->phys) return -ENOENT; |
||
341 | len = strlen(dev->phys) + 1; |
||
342 | if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd); |
||
343 | return copy_to_user((char *) arg, dev->phys, len) ? -EFAULT : len; |
||
344 | } |
||
345 | |||
346 | if (_IOC_NR(cmd) == _IOC_NR(EVIOCGUNIQ(0))) { |
||
347 | int len; |
||
348 | if (!dev->uniq) return -ENOENT; |
||
349 | len = strlen(dev->uniq) + 1; |
||
350 | if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd); |
||
351 | return copy_to_user((char *) arg, dev->uniq, len) ? -EFAULT : len; |
||
352 | } |
||
353 | |||
354 | if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCGABS(0))) { |
||
355 | |||
356 | int t = _IOC_NR(cmd) & ABS_MAX; |
||
357 | |||
358 | abs.value = dev->abs[t]; |
||
359 | abs.minimum = dev->absmin[t]; |
||
360 | abs.maximum = dev->absmax[t]; |
||
361 | abs.fuzz = dev->absfuzz[t]; |
||
362 | abs.flat = dev->absflat[t]; |
||
363 | |||
364 | if (copy_to_user((void *) arg, &abs, sizeof(struct input_absinfo))) |
||
365 | return -EFAULT; |
||
366 | |||
367 | return 0; |
||
368 | } |
||
369 | |||
370 | if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCSABS(0))) { |
||
371 | |||
372 | int t = _IOC_NR(cmd) & ABS_MAX; |
||
373 | |||
374 | if (copy_from_user(&abs, (void *) arg, sizeof(struct input_absinfo))) |
||
375 | return -EFAULT; |
||
376 | |||
377 | dev->abs[t] = abs.value; |
||
378 | dev->absmin[t] = abs.minimum; |
||
379 | dev->absmax[t] = abs.maximum; |
||
380 | dev->absfuzz[t] = abs.fuzz; |
||
381 | dev->absflat[t] = abs.flat; |
||
382 | |||
383 | return 0; |
||
384 | } |
||
385 | } |
||
386 | return -EINVAL; |
||
387 | } |
||
388 | |||
389 | static struct file_operations evdev_fops = { |
||
390 | .owner = THIS_MODULE, |
||
391 | .read = evdev_read, |
||
392 | .write = evdev_write, |
||
393 | .poll = evdev_poll, |
||
394 | .open = evdev_open, |
||
395 | .release = evdev_release, |
||
396 | .ioctl = evdev_ioctl, |
||
397 | .fasync = evdev_fasync, |
||
398 | .flush = evdev_flush |
||
399 | }; |
||
400 | |||
401 | static struct input_handle *evdev_connect(struct input_handler *handler, struct input_dev *dev, struct input_device_id *id) |
||
402 | { |
||
403 | struct evdev *evdev; |
||
404 | int minor; |
||
405 | |||
406 | for (minor = 0; minor < EVDEV_MINORS && evdev_table[minor]; minor++); |
||
407 | if (minor == EVDEV_MINORS) { |
||
408 | printk(KERN_ERR "evdev: no more free evdev devices\n"); |
||
409 | return NULL; |
||
410 | } |
||
411 | |||
412 | if (!(evdev = kmalloc(sizeof(struct evdev), GFP_KERNEL))) |
||
413 | return NULL; |
||
414 | memset(evdev, 0, sizeof(struct evdev)); |
||
415 | |||
416 | INIT_LIST_HEAD(&evdev->list); |
||
417 | init_waitqueue_head(&evdev->wait); |
||
418 | |||
419 | evdev->exist = 1; |
||
420 | evdev->minor = minor; |
||
421 | evdev->handle.dev = dev; |
||
422 | evdev->handle.name = evdev->name; |
||
423 | evdev->handle.handler = handler; |
||
424 | evdev->handle.private = evdev; |
||
425 | sprintf26(evdev->name, "event%d", minor); |
||
426 | |||
427 | evdev_table[minor] = evdev; |
||
428 | |||
429 | devfs_mk_cdev(MKDEV(INPUT_MAJOR, EVDEV_MINOR_BASE + minor), |
||
430 | S_IFCHR|S_IRUGO|S_IWUSR, "input/event%d", minor); |
||
431 | |||
432 | return &evdev->handle; |
||
433 | } |
||
434 | |||
435 | static void evdev_disconnect(struct input_handle *handle) |
||
436 | { |
||
437 | struct evdev *evdev = handle->private; |
||
438 | |||
439 | evdev->exist = 0; |
||
440 | |||
441 | if (evdev->open) { |
||
442 | input_close_device(handle); |
||
443 | wake_up_interruptible(&evdev->wait); |
||
444 | } else |
||
445 | evdev_free(evdev); |
||
446 | } |
||
447 | |||
448 | static struct input_device_id evdev_ids[] = { |
||
449 | { .driver_info = 1 }, /* Matches all devices */ |
||
450 | { }, /* Terminating zero entry */ |
||
451 | }; |
||
452 | |||
453 | MODULE_DEVICE_TABLE(input, evdev_ids); |
||
454 | |||
455 | static struct input_handler evdev_handler = { |
||
456 | .event = evdev_event, |
||
457 | .connect = evdev_connect, |
||
458 | .disconnect = evdev_disconnect, |
||
459 | .fops = &evdev_fops, |
||
460 | .minor = EVDEV_MINOR_BASE, |
||
461 | .name = "evdev", |
||
462 | .id_table = evdev_ids, |
||
463 | }; |
||
464 | |||
465 | /*static*/ int __init evdev_init(void) |
||
466 | { |
||
467 | input_register_handler(&evdev_handler); |
||
468 | return 0; |
||
469 | } |
||
470 | |||
471 | /*static*/ void __exit evdev_exit(void) |
||
472 | { |
||
473 | input_unregister_handler(&evdev_handler); |
||
474 | } |
||
475 | |||
476 | module_init(evdev_init); |
||
477 | module_exit(evdev_exit); |
||
478 | |||
479 | MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>"); |
||
480 | MODULE_DESCRIPTION("Input driver event char devices"); |
||
481 | MODULE_LICENSE("GPL"); |