Commit | Line | Data |
---|---|---|
1da177e4 LT |
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 | #define EVDEV_MINOR_BASE 64 | |
12 | #define EVDEV_MINORS 32 | |
13 | #define EVDEV_BUFFER_SIZE 64 | |
14 | ||
15 | #include <linux/poll.h> | |
16 | #include <linux/slab.h> | |
17 | #include <linux/module.h> | |
18 | #include <linux/init.h> | |
19 | #include <linux/input.h> | |
20 | #include <linux/major.h> | |
21 | #include <linux/smp_lock.h> | |
22 | #include <linux/device.h> | |
52658bb6 | 23 | #include <linux/compat.h> |
1da177e4 LT |
24 | |
25 | struct evdev { | |
26 | int exist; | |
27 | int open; | |
28 | int minor; | |
29 | char name[16]; | |
30 | struct input_handle handle; | |
31 | wait_queue_head_t wait; | |
d0ffb9be DT |
32 | struct evdev_client *grab; |
33 | struct list_head client_list; | |
1da177e4 LT |
34 | }; |
35 | ||
d0ffb9be | 36 | struct evdev_client { |
1da177e4 LT |
37 | struct input_event buffer[EVDEV_BUFFER_SIZE]; |
38 | int head; | |
39 | int tail; | |
40 | struct fasync_struct *fasync; | |
41 | struct evdev *evdev; | |
42 | struct list_head node; | |
43 | }; | |
44 | ||
45 | static struct evdev *evdev_table[EVDEV_MINORS]; | |
46 | ||
47 | static void evdev_event(struct input_handle *handle, unsigned int type, unsigned int code, int value) | |
48 | { | |
49 | struct evdev *evdev = handle->private; | |
d0ffb9be | 50 | struct evdev_client *client; |
1da177e4 LT |
51 | |
52 | if (evdev->grab) { | |
d0ffb9be | 53 | client = evdev->grab; |
1da177e4 | 54 | |
d0ffb9be DT |
55 | do_gettimeofday(&client->buffer[client->head].time); |
56 | client->buffer[client->head].type = type; | |
57 | client->buffer[client->head].code = code; | |
58 | client->buffer[client->head].value = value; | |
59 | client->head = (client->head + 1) & (EVDEV_BUFFER_SIZE - 1); | |
1da177e4 | 60 | |
d0ffb9be | 61 | kill_fasync(&client->fasync, SIGIO, POLL_IN); |
1da177e4 | 62 | } else |
d0ffb9be | 63 | list_for_each_entry(client, &evdev->client_list, node) { |
1da177e4 | 64 | |
d0ffb9be DT |
65 | do_gettimeofday(&client->buffer[client->head].time); |
66 | client->buffer[client->head].type = type; | |
67 | client->buffer[client->head].code = code; | |
68 | client->buffer[client->head].value = value; | |
69 | client->head = (client->head + 1) & (EVDEV_BUFFER_SIZE - 1); | |
1da177e4 | 70 | |
d0ffb9be | 71 | kill_fasync(&client->fasync, SIGIO, POLL_IN); |
1da177e4 LT |
72 | } |
73 | ||
74 | wake_up_interruptible(&evdev->wait); | |
75 | } | |
76 | ||
77 | static int evdev_fasync(int fd, struct file *file, int on) | |
78 | { | |
d0ffb9be | 79 | struct evdev_client *client = file->private_data; |
1da177e4 | 80 | int retval; |
1e0afb28 | 81 | |
d0ffb9be | 82 | retval = fasync_helper(fd, file, on, &client->fasync); |
1e0afb28 | 83 | |
1da177e4 LT |
84 | return retval < 0 ? retval : 0; |
85 | } | |
86 | ||
1e0afb28 | 87 | static int evdev_flush(struct file *file, fl_owner_t id) |
1da177e4 | 88 | { |
d0ffb9be DT |
89 | struct evdev_client *client = file->private_data; |
90 | struct evdev *evdev = client->evdev; | |
1e0afb28 | 91 | |
d0ffb9be | 92 | if (!evdev->exist) |
1e0afb28 DT |
93 | return -ENODEV; |
94 | ||
d0ffb9be | 95 | return input_flush_device(&evdev->handle, file); |
1da177e4 LT |
96 | } |
97 | ||
98 | static void evdev_free(struct evdev *evdev) | |
99 | { | |
100 | evdev_table[evdev->minor] = NULL; | |
101 | kfree(evdev); | |
102 | } | |
103 | ||
d0ffb9be | 104 | static int evdev_release(struct inode *inode, struct file *file) |
1da177e4 | 105 | { |
d0ffb9be DT |
106 | struct evdev_client *client = file->private_data; |
107 | struct evdev *evdev = client->evdev; | |
1da177e4 | 108 | |
d0ffb9be DT |
109 | if (evdev->grab == client) { |
110 | input_release_device(&evdev->handle); | |
111 | evdev->grab = NULL; | |
1da177e4 LT |
112 | } |
113 | ||
114 | evdev_fasync(-1, file, 0); | |
d0ffb9be DT |
115 | list_del(&client->node); |
116 | kfree(client); | |
1da177e4 | 117 | |
d0ffb9be DT |
118 | if (!--evdev->open) { |
119 | if (evdev->exist) | |
120 | input_close_device(&evdev->handle); | |
1da177e4 | 121 | else |
d0ffb9be | 122 | evdev_free(evdev); |
1da177e4 LT |
123 | } |
124 | ||
1da177e4 LT |
125 | return 0; |
126 | } | |
127 | ||
d0ffb9be | 128 | static int evdev_open(struct inode *inode, struct file *file) |
1da177e4 | 129 | { |
d0ffb9be DT |
130 | struct evdev_client *client; |
131 | struct evdev *evdev; | |
1da177e4 | 132 | int i = iminor(inode) - EVDEV_MINOR_BASE; |
1da177e4 | 133 | |
d0ffb9be DT |
134 | if (i >= EVDEV_MINORS) |
135 | return -ENODEV; | |
136 | ||
137 | evdev = evdev_table[i]; | |
138 | ||
139 | if (!evdev || !evdev->exist) | |
1da177e4 LT |
140 | return -ENODEV; |
141 | ||
d0ffb9be DT |
142 | client = kzalloc(sizeof(struct evdev_client), GFP_KERNEL); |
143 | if (!client) | |
1da177e4 | 144 | return -ENOMEM; |
1da177e4 | 145 | |
d0ffb9be DT |
146 | client->evdev = evdev; |
147 | list_add_tail(&client->node, &evdev->client_list); | |
1da177e4 | 148 | |
d0ffb9be DT |
149 | if (!evdev->open++ && evdev->exist) |
150 | input_open_device(&evdev->handle); | |
1da177e4 | 151 | |
d0ffb9be | 152 | file->private_data = client; |
1da177e4 LT |
153 | return 0; |
154 | } | |
155 | ||
52658bb6 | 156 | #ifdef CONFIG_COMPAT |
3a51f7c4 | 157 | |
52658bb6 JK |
158 | struct input_event_compat { |
159 | struct compat_timeval time; | |
160 | __u16 type; | |
161 | __u16 code; | |
162 | __s32 value; | |
163 | }; | |
164 | ||
bf2fcc6f AK |
165 | /* Note to the author of this code: did it ever occur to |
166 | you why the ifdefs are needed? Think about it again. -AK */ | |
52658bb6 | 167 | #ifdef CONFIG_X86_64 |
bf2fcc6f | 168 | # define COMPAT_TEST is_compat_task() |
52658bb6 | 169 | #elif defined(CONFIG_IA64) |
6450578f | 170 | # define COMPAT_TEST IS_IA32_PROCESS(task_pt_regs(current)) |
347a8dc3 | 171 | #elif defined(CONFIG_S390) |
52658bb6 | 172 | # define COMPAT_TEST test_thread_flag(TIF_31BIT) |
59df6bbf RB |
173 | #elif defined(CONFIG_MIPS) |
174 | # define COMPAT_TEST (current->thread.mflags & MF_32BIT_ADDR) | |
52658bb6 JK |
175 | #else |
176 | # define COMPAT_TEST test_thread_flag(TIF_32BIT) | |
177 | #endif | |
178 | ||
3a51f7c4 | 179 | static inline size_t evdev_event_size(void) |
52658bb6 | 180 | { |
3a51f7c4 DT |
181 | return COMPAT_TEST ? |
182 | sizeof(struct input_event_compat) : sizeof(struct input_event); | |
183 | } | |
52658bb6 | 184 | |
3a51f7c4 DT |
185 | static int evdev_event_from_user(const char __user *buffer, struct input_event *event) |
186 | { | |
187 | if (COMPAT_TEST) { | |
188 | struct input_event_compat compat_event; | |
189 | ||
190 | if (copy_from_user(&compat_event, buffer, sizeof(struct input_event_compat))) | |
191 | return -EFAULT; | |
192 | ||
193 | event->time.tv_sec = compat_event.time.tv_sec; | |
194 | event->time.tv_usec = compat_event.time.tv_usec; | |
195 | event->type = compat_event.type; | |
196 | event->code = compat_event.code; | |
197 | event->value = compat_event.value; | |
198 | ||
199 | } else { | |
200 | if (copy_from_user(event, buffer, sizeof(struct input_event))) | |
52658bb6 | 201 | return -EFAULT; |
52658bb6 JK |
202 | } |
203 | ||
3a51f7c4 | 204 | return 0; |
52658bb6 | 205 | } |
52658bb6 | 206 | |
3a51f7c4 | 207 | static int evdev_event_to_user(char __user *buffer, const struct input_event *event) |
1da177e4 | 208 | { |
3a51f7c4 DT |
209 | if (COMPAT_TEST) { |
210 | struct input_event_compat compat_event; | |
1da177e4 | 211 | |
3a51f7c4 DT |
212 | compat_event.time.tv_sec = event->time.tv_sec; |
213 | compat_event.time.tv_usec = event->time.tv_usec; | |
214 | compat_event.type = event->type; | |
215 | compat_event.code = event->code; | |
216 | compat_event.value = event->value; | |
52658bb6 | 217 | |
3a51f7c4 DT |
218 | if (copy_to_user(buffer, &compat_event, sizeof(struct input_event_compat))) |
219 | return -EFAULT; | |
1da177e4 | 220 | |
3a51f7c4 DT |
221 | } else { |
222 | if (copy_to_user(buffer, event, sizeof(struct input_event))) | |
1da177e4 | 223 | return -EFAULT; |
1da177e4 LT |
224 | } |
225 | ||
3a51f7c4 | 226 | return 0; |
1da177e4 LT |
227 | } |
228 | ||
3a51f7c4 DT |
229 | #else |
230 | ||
231 | static inline size_t evdev_event_size(void) | |
52658bb6 | 232 | { |
3a51f7c4 DT |
233 | return sizeof(struct input_event); |
234 | } | |
52658bb6 | 235 | |
3a51f7c4 DT |
236 | static int evdev_event_from_user(const char __user *buffer, struct input_event *event) |
237 | { | |
238 | if (copy_from_user(event, buffer, sizeof(struct input_event))) | |
239 | return -EFAULT; | |
52658bb6 | 240 | |
3a51f7c4 DT |
241 | return 0; |
242 | } | |
52658bb6 | 243 | |
3a51f7c4 DT |
244 | static int evdev_event_to_user(char __user *buffer, const struct input_event *event) |
245 | { | |
246 | if (copy_to_user(buffer, event, sizeof(struct input_event))) | |
247 | return -EFAULT; | |
52658bb6 | 248 | |
3a51f7c4 DT |
249 | return 0; |
250 | } | |
251 | ||
252 | #endif /* CONFIG_COMPAT */ | |
253 | ||
d0ffb9be | 254 | static ssize_t evdev_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos) |
3a51f7c4 | 255 | { |
d0ffb9be DT |
256 | struct evdev_client *client = file->private_data; |
257 | struct evdev *evdev = client->evdev; | |
3a51f7c4 DT |
258 | struct input_event event; |
259 | int retval = 0; | |
52658bb6 | 260 | |
d0ffb9be | 261 | if (!evdev->exist) |
52658bb6 JK |
262 | return -ENODEV; |
263 | ||
3a51f7c4 DT |
264 | while (retval < count) { |
265 | ||
266 | if (evdev_event_from_user(buffer + retval, &event)) | |
267 | return -EFAULT; | |
d0ffb9be | 268 | input_inject_event(&evdev->handle, event.type, event.code, event.value); |
3a51f7c4 | 269 | retval += evdev_event_size(); |
52658bb6 JK |
270 | } |
271 | ||
272 | return retval; | |
273 | } | |
52658bb6 | 274 | |
d0ffb9be | 275 | static ssize_t evdev_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos) |
1da177e4 | 276 | { |
d0ffb9be DT |
277 | struct evdev_client *client = file->private_data; |
278 | struct evdev *evdev = client->evdev; | |
1da177e4 LT |
279 | int retval; |
280 | ||
3a51f7c4 | 281 | if (count < evdev_event_size()) |
1da177e4 LT |
282 | return -EINVAL; |
283 | ||
d0ffb9be | 284 | if (client->head == client->tail && evdev->exist && (file->f_flags & O_NONBLOCK)) |
1da177e4 LT |
285 | return -EAGAIN; |
286 | ||
d0ffb9be DT |
287 | retval = wait_event_interruptible(evdev->wait, |
288 | client->head != client->tail || !evdev->exist); | |
1da177e4 LT |
289 | if (retval) |
290 | return retval; | |
291 | ||
d0ffb9be | 292 | if (!evdev->exist) |
1da177e4 LT |
293 | return -ENODEV; |
294 | ||
d0ffb9be | 295 | while (client->head != client->tail && retval + evdev_event_size() <= count) { |
3a51f7c4 | 296 | |
d0ffb9be | 297 | struct input_event *event = (struct input_event *) client->buffer + client->tail; |
3a51f7c4 DT |
298 | |
299 | if (evdev_event_to_user(buffer + retval, event)) | |
300 | return -EFAULT; | |
301 | ||
d0ffb9be | 302 | client->tail = (client->tail + 1) & (EVDEV_BUFFER_SIZE - 1); |
3a51f7c4 | 303 | retval += evdev_event_size(); |
1da177e4 LT |
304 | } |
305 | ||
306 | return retval; | |
307 | } | |
308 | ||
309 | /* No kernel lock - fine */ | |
310 | static unsigned int evdev_poll(struct file *file, poll_table *wait) | |
311 | { | |
d0ffb9be DT |
312 | struct evdev_client *client = file->private_data; |
313 | struct evdev *evdev = client->evdev; | |
1e0afb28 | 314 | |
d0ffb9be DT |
315 | poll_wait(file, &evdev->wait, wait); |
316 | return ((client->head == client->tail) ? 0 : (POLLIN | POLLRDNORM)) | | |
317 | (evdev->exist ? 0 : (POLLHUP | POLLERR)); | |
1da177e4 LT |
318 | } |
319 | ||
3a51f7c4 DT |
320 | #ifdef CONFIG_COMPAT |
321 | ||
322 | #define BITS_PER_LONG_COMPAT (sizeof(compat_long_t) * 8) | |
323 | #define NBITS_COMPAT(x) ((((x) - 1) / BITS_PER_LONG_COMPAT) + 1) | |
324 | ||
325 | #ifdef __BIG_ENDIAN | |
326 | static int bits_to_user(unsigned long *bits, unsigned int maxbit, | |
327 | unsigned int maxlen, void __user *p, int compat) | |
328 | { | |
329 | int len, i; | |
330 | ||
331 | if (compat) { | |
332 | len = NBITS_COMPAT(maxbit) * sizeof(compat_long_t); | |
333 | if (len < maxlen) | |
334 | len = maxlen; | |
335 | ||
336 | for (i = 0; i < len / sizeof(compat_long_t); i++) | |
337 | if (copy_to_user((compat_long_t __user *) p + i, | |
338 | (compat_long_t *) bits + | |
339 | i + 1 - ((i % 2) << 1), | |
340 | sizeof(compat_long_t))) | |
341 | return -EFAULT; | |
342 | } else { | |
343 | len = NBITS(maxbit) * sizeof(long); | |
344 | if (len > maxlen) | |
345 | len = maxlen; | |
346 | ||
347 | if (copy_to_user(p, bits, len)) | |
348 | return -EFAULT; | |
349 | } | |
350 | ||
351 | return len; | |
352 | } | |
353 | #else | |
354 | static int bits_to_user(unsigned long *bits, unsigned int maxbit, | |
355 | unsigned int maxlen, void __user *p, int compat) | |
356 | { | |
357 | int len = compat ? | |
358 | NBITS_COMPAT(maxbit) * sizeof(compat_long_t) : | |
359 | NBITS(maxbit) * sizeof(long); | |
360 | ||
361 | if (len > maxlen) | |
362 | len = maxlen; | |
363 | ||
364 | return copy_to_user(p, bits, len) ? -EFAULT : len; | |
365 | } | |
366 | #endif /* __BIG_ENDIAN */ | |
367 | ||
368 | #else | |
369 | ||
370 | static int bits_to_user(unsigned long *bits, unsigned int maxbit, | |
371 | unsigned int maxlen, void __user *p, int compat) | |
372 | { | |
373 | int len = NBITS(maxbit) * sizeof(long); | |
374 | ||
375 | if (len > maxlen) | |
376 | len = maxlen; | |
377 | ||
378 | return copy_to_user(p, bits, len) ? -EFAULT : len; | |
379 | } | |
380 | ||
381 | #endif /* CONFIG_COMPAT */ | |
382 | ||
383 | static int str_to_user(const char *str, unsigned int maxlen, void __user *p) | |
384 | { | |
385 | int len; | |
386 | ||
387 | if (!str) | |
388 | return -ENOENT; | |
389 | ||
390 | len = strlen(str) + 1; | |
391 | if (len > maxlen) | |
392 | len = maxlen; | |
393 | ||
394 | return copy_to_user(p, str, len) ? -EFAULT : len; | |
395 | } | |
396 | ||
397 | static long evdev_ioctl_handler(struct file *file, unsigned int cmd, | |
398 | void __user *p, int compat_mode) | |
1da177e4 | 399 | { |
d0ffb9be DT |
400 | struct evdev_client *client = file->private_data; |
401 | struct evdev *evdev = client->evdev; | |
1da177e4 LT |
402 | struct input_dev *dev = evdev->handle.dev; |
403 | struct input_absinfo abs; | |
509ca1a9 | 404 | struct ff_effect effect; |
3a51f7c4 | 405 | int __user *ip = (int __user *)p; |
1da177e4 | 406 | int i, t, u, v; |
509ca1a9 | 407 | int error; |
1da177e4 | 408 | |
3a51f7c4 DT |
409 | if (!evdev->exist) |
410 | return -ENODEV; | |
1da177e4 LT |
411 | |
412 | switch (cmd) { | |
413 | ||
414 | case EVIOCGVERSION: | |
415 | return put_user(EV_VERSION, ip); | |
416 | ||
417 | case EVIOCGID: | |
3a51f7c4 DT |
418 | if (copy_to_user(p, &dev->id, sizeof(struct input_id))) |
419 | return -EFAULT; | |
08791e5c DT |
420 | return 0; |
421 | ||
422 | case EVIOCGREP: | |
423 | if (!test_bit(EV_REP, dev->evbit)) | |
424 | return -ENOSYS; | |
425 | if (put_user(dev->rep[REP_DELAY], ip)) | |
426 | return -EFAULT; | |
427 | if (put_user(dev->rep[REP_PERIOD], ip + 1)) | |
428 | return -EFAULT; | |
429 | return 0; | |
430 | ||
431 | case EVIOCSREP: | |
432 | if (!test_bit(EV_REP, dev->evbit)) | |
433 | return -ENOSYS; | |
434 | if (get_user(u, ip)) | |
435 | return -EFAULT; | |
436 | if (get_user(v, ip + 1)) | |
437 | return -EFAULT; | |
438 | ||
0e739d28 DT |
439 | input_inject_event(&evdev->handle, EV_REP, REP_DELAY, u); |
440 | input_inject_event(&evdev->handle, EV_REP, REP_PERIOD, v); | |
3a51f7c4 DT |
441 | |
442 | return 0; | |
1da177e4 LT |
443 | |
444 | case EVIOCGKEYCODE: | |
3a51f7c4 DT |
445 | if (get_user(t, ip)) |
446 | return -EFAULT; | |
c8e4c772 MR |
447 | |
448 | error = dev->getkeycode(dev, t, &v); | |
449 | if (error) | |
450 | return error; | |
451 | ||
452 | if (put_user(v, ip + 1)) | |
3a51f7c4 | 453 | return -EFAULT; |
c8e4c772 | 454 | |
1da177e4 LT |
455 | return 0; |
456 | ||
457 | case EVIOCSKEYCODE: | |
c8e4c772 | 458 | if (get_user(t, ip) || get_user(v, ip + 1)) |
3a51f7c4 | 459 | return -EFAULT; |
3a51f7c4 | 460 | |
c8e4c772 | 461 | return dev->setkeycode(dev, t, v); |
1da177e4 LT |
462 | |
463 | case EVIOCSFF: | |
509ca1a9 AH |
464 | if (copy_from_user(&effect, p, sizeof(effect))) |
465 | return -EFAULT; | |
1da177e4 | 466 | |
509ca1a9 | 467 | error = input_ff_upload(dev, &effect, file); |
3a51f7c4 | 468 | |
509ca1a9 AH |
469 | if (put_user(effect.id, &(((struct ff_effect __user *)p)->id))) |
470 | return -EFAULT; | |
471 | ||
472 | return error; | |
473 | ||
474 | case EVIOCRMFF: | |
475 | return input_ff_erase(dev, (int)(unsigned long) p, file); | |
1da177e4 LT |
476 | |
477 | case EVIOCGEFFECTS: | |
509ca1a9 AH |
478 | i = test_bit(EV_FF, dev->evbit) ? dev->ff->max_effects : 0; |
479 | if (put_user(i, ip)) | |
1da177e4 LT |
480 | return -EFAULT; |
481 | return 0; | |
482 | ||
483 | case EVIOCGRAB: | |
3a51f7c4 | 484 | if (p) { |
1da177e4 LT |
485 | if (evdev->grab) |
486 | return -EBUSY; | |
487 | if (input_grab_device(&evdev->handle)) | |
488 | return -EBUSY; | |
d0ffb9be | 489 | evdev->grab = client; |
1da177e4 LT |
490 | return 0; |
491 | } else { | |
d0ffb9be | 492 | if (evdev->grab != client) |
1da177e4 LT |
493 | return -EINVAL; |
494 | input_release_device(&evdev->handle); | |
495 | evdev->grab = NULL; | |
496 | return 0; | |
497 | } | |
498 | ||
499 | default: | |
500 | ||
41e979f8 | 501 | if (_IOC_TYPE(cmd) != 'E') |
1da177e4 LT |
502 | return -EINVAL; |
503 | ||
41e979f8 VP |
504 | if (_IOC_DIR(cmd) == _IOC_READ) { |
505 | ||
506 | if ((_IOC_NR(cmd) & ~EV_MAX) == _IOC_NR(EVIOCGBIT(0,0))) { | |
507 | ||
508 | long *bits; | |
509 | int len; | |
510 | ||
511 | switch (_IOC_NR(cmd) & EV_MAX) { | |
512 | case 0: bits = dev->evbit; len = EV_MAX; break; | |
513 | case EV_KEY: bits = dev->keybit; len = KEY_MAX; break; | |
514 | case EV_REL: bits = dev->relbit; len = REL_MAX; break; | |
515 | case EV_ABS: bits = dev->absbit; len = ABS_MAX; break; | |
516 | case EV_MSC: bits = dev->mscbit; len = MSC_MAX; break; | |
517 | case EV_LED: bits = dev->ledbit; len = LED_MAX; break; | |
518 | case EV_SND: bits = dev->sndbit; len = SND_MAX; break; | |
519 | case EV_FF: bits = dev->ffbit; len = FF_MAX; break; | |
31581066 | 520 | case EV_SW: bits = dev->swbit; len = SW_MAX; break; |
41e979f8 VP |
521 | default: return -EINVAL; |
522 | } | |
3a51f7c4 | 523 | return bits_to_user(bits, len, _IOC_SIZE(cmd), p, compat_mode); |
1da177e4 | 524 | } |
1da177e4 | 525 | |
3a51f7c4 DT |
526 | if (_IOC_NR(cmd) == _IOC_NR(EVIOCGKEY(0))) |
527 | return bits_to_user(dev->key, KEY_MAX, _IOC_SIZE(cmd), | |
528 | p, compat_mode); | |
1da177e4 | 529 | |
3a51f7c4 DT |
530 | if (_IOC_NR(cmd) == _IOC_NR(EVIOCGLED(0))) |
531 | return bits_to_user(dev->led, LED_MAX, _IOC_SIZE(cmd), | |
532 | p, compat_mode); | |
1da177e4 | 533 | |
3a51f7c4 DT |
534 | if (_IOC_NR(cmd) == _IOC_NR(EVIOCGSND(0))) |
535 | return bits_to_user(dev->snd, SND_MAX, _IOC_SIZE(cmd), | |
536 | p, compat_mode); | |
1da177e4 | 537 | |
3a51f7c4 DT |
538 | if (_IOC_NR(cmd) == _IOC_NR(EVIOCGSW(0))) |
539 | return bits_to_user(dev->sw, SW_MAX, _IOC_SIZE(cmd), | |
540 | p, compat_mode); | |
31581066 | 541 | |
3a51f7c4 DT |
542 | if (_IOC_NR(cmd) == _IOC_NR(EVIOCGNAME(0))) |
543 | return str_to_user(dev->name, _IOC_SIZE(cmd), p); | |
1da177e4 | 544 | |
3a51f7c4 DT |
545 | if (_IOC_NR(cmd) == _IOC_NR(EVIOCGPHYS(0))) |
546 | return str_to_user(dev->phys, _IOC_SIZE(cmd), p); | |
1da177e4 | 547 | |
3a51f7c4 DT |
548 | if (_IOC_NR(cmd) == _IOC_NR(EVIOCGUNIQ(0))) |
549 | return str_to_user(dev->uniq, _IOC_SIZE(cmd), p); | |
1da177e4 | 550 | |
41e979f8 | 551 | if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCGABS(0))) { |
1da177e4 | 552 | |
41e979f8 | 553 | int t = _IOC_NR(cmd) & ABS_MAX; |
1da177e4 | 554 | |
41e979f8 VP |
555 | abs.value = dev->abs[t]; |
556 | abs.minimum = dev->absmin[t]; | |
557 | abs.maximum = dev->absmax[t]; | |
558 | abs.fuzz = dev->absfuzz[t]; | |
559 | abs.flat = dev->absflat[t]; | |
1da177e4 | 560 | |
41e979f8 VP |
561 | if (copy_to_user(p, &abs, sizeof(struct input_absinfo))) |
562 | return -EFAULT; | |
563 | ||
564 | return 0; | |
565 | } | |
1da177e4 | 566 | |
1da177e4 LT |
567 | } |
568 | ||
41e979f8 | 569 | if (_IOC_DIR(cmd) == _IOC_WRITE) { |
1da177e4 | 570 | |
41e979f8 | 571 | if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCSABS(0))) { |
1da177e4 | 572 | |
41e979f8 VP |
573 | int t = _IOC_NR(cmd) & ABS_MAX; |
574 | ||
575 | if (copy_from_user(&abs, p, sizeof(struct input_absinfo))) | |
576 | return -EFAULT; | |
1da177e4 | 577 | |
41e979f8 VP |
578 | dev->abs[t] = abs.value; |
579 | dev->absmin[t] = abs.minimum; | |
580 | dev->absmax[t] = abs.maximum; | |
581 | dev->absfuzz[t] = abs.fuzz; | |
582 | dev->absflat[t] = abs.flat; | |
1da177e4 | 583 | |
41e979f8 VP |
584 | return 0; |
585 | } | |
1da177e4 | 586 | } |
1da177e4 LT |
587 | } |
588 | return -EINVAL; | |
589 | } | |
1da177e4 | 590 | |
3a51f7c4 DT |
591 | static long evdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
592 | { | |
593 | return evdev_ioctl_handler(file, cmd, (void __user *)arg, 0); | |
594 | } | |
41e979f8 | 595 | |
3a51f7c4 | 596 | #ifdef CONFIG_COMPAT |
52658bb6 JK |
597 | static long evdev_ioctl_compat(struct file *file, unsigned int cmd, unsigned long arg) |
598 | { | |
3a51f7c4 | 599 | return evdev_ioctl_handler(file, cmd, compat_ptr(arg), 1); |
1da177e4 | 600 | } |
52658bb6 | 601 | #endif |
1da177e4 | 602 | |
66e66118 | 603 | static const struct file_operations evdev_fops = { |
1da177e4 LT |
604 | .owner = THIS_MODULE, |
605 | .read = evdev_read, | |
606 | .write = evdev_write, | |
607 | .poll = evdev_poll, | |
608 | .open = evdev_open, | |
609 | .release = evdev_release, | |
52658bb6 JK |
610 | .unlocked_ioctl = evdev_ioctl, |
611 | #ifdef CONFIG_COMPAT | |
612 | .compat_ioctl = evdev_ioctl_compat, | |
613 | #endif | |
1da177e4 LT |
614 | .fasync = evdev_fasync, |
615 | .flush = evdev_flush | |
616 | }; | |
617 | ||
5b2a0826 DT |
618 | static int evdev_connect(struct input_handler *handler, struct input_dev *dev, |
619 | const struct input_device_id *id) | |
1da177e4 LT |
620 | { |
621 | struct evdev *evdev; | |
c9bcd582 | 622 | struct class_device *cdev; |
5b2a0826 | 623 | dev_t devt; |
1da177e4 | 624 | int minor; |
5b2a0826 | 625 | int error; |
1da177e4 LT |
626 | |
627 | for (minor = 0; minor < EVDEV_MINORS && evdev_table[minor]; minor++); | |
628 | if (minor == EVDEV_MINORS) { | |
629 | printk(KERN_ERR "evdev: no more free evdev devices\n"); | |
5b2a0826 | 630 | return -ENFILE; |
1da177e4 LT |
631 | } |
632 | ||
5b2a0826 DT |
633 | evdev = kzalloc(sizeof(struct evdev), GFP_KERNEL); |
634 | if (!evdev) | |
635 | return -ENOMEM; | |
1da177e4 | 636 | |
d0ffb9be | 637 | INIT_LIST_HEAD(&evdev->client_list); |
1da177e4 LT |
638 | init_waitqueue_head(&evdev->wait); |
639 | ||
640 | evdev->exist = 1; | |
641 | evdev->minor = minor; | |
642 | evdev->handle.dev = dev; | |
643 | evdev->handle.name = evdev->name; | |
644 | evdev->handle.handler = handler; | |
645 | evdev->handle.private = evdev; | |
646 | sprintf(evdev->name, "event%d", minor); | |
647 | ||
648 | evdev_table[minor] = evdev; | |
649 | ||
5b2a0826 DT |
650 | devt = MKDEV(INPUT_MAJOR, EVDEV_MINOR_BASE + minor), |
651 | ||
652 | cdev = class_device_create(&input_class, &dev->cdev, devt, | |
653 | dev->cdev.dev, evdev->name); | |
654 | if (IS_ERR(cdev)) { | |
655 | error = PTR_ERR(cdev); | |
656 | goto err_free_evdev; | |
657 | } | |
c9bcd582 GKH |
658 | |
659 | /* temporary symlink to keep userspace happy */ | |
5b2a0826 DT |
660 | error = sysfs_create_link(&input_class.subsys.kset.kobj, |
661 | &cdev->kobj, evdev->name); | |
662 | if (error) | |
663 | goto err_cdev_destroy; | |
664 | ||
665 | error = input_register_handle(&evdev->handle); | |
666 | if (error) | |
667 | goto err_remove_link; | |
1da177e4 | 668 | |
5b2a0826 DT |
669 | return 0; |
670 | ||
671 | err_remove_link: | |
672 | sysfs_remove_link(&input_class.subsys.kset.kobj, evdev->name); | |
673 | err_cdev_destroy: | |
674 | class_device_destroy(&input_class, devt); | |
675 | err_free_evdev: | |
676 | kfree(evdev); | |
677 | evdev_table[minor] = NULL; | |
678 | return error; | |
1da177e4 LT |
679 | } |
680 | ||
681 | static void evdev_disconnect(struct input_handle *handle) | |
682 | { | |
683 | struct evdev *evdev = handle->private; | |
d0ffb9be | 684 | struct evdev_client *client; |
1da177e4 | 685 | |
5b2a0826 DT |
686 | input_unregister_handle(handle); |
687 | ||
c9bcd582 | 688 | sysfs_remove_link(&input_class.subsys.kset.kobj, evdev->name); |
ea9f240b | 689 | class_device_destroy(&input_class, |
1235686f | 690 | MKDEV(INPUT_MAJOR, EVDEV_MINOR_BASE + evdev->minor)); |
1da177e4 LT |
691 | evdev->exist = 0; |
692 | ||
693 | if (evdev->open) { | |
509ca1a9 | 694 | input_flush_device(handle, NULL); |
1da177e4 LT |
695 | input_close_device(handle); |
696 | wake_up_interruptible(&evdev->wait); | |
d0ffb9be DT |
697 | list_for_each_entry(client, &evdev->client_list, node) |
698 | kill_fasync(&client->fasync, SIGIO, POLL_HUP); | |
1da177e4 LT |
699 | } else |
700 | evdev_free(evdev); | |
701 | } | |
702 | ||
66e66118 | 703 | static const struct input_device_id evdev_ids[] = { |
1da177e4 LT |
704 | { .driver_info = 1 }, /* Matches all devices */ |
705 | { }, /* Terminating zero entry */ | |
706 | }; | |
707 | ||
708 | MODULE_DEVICE_TABLE(input, evdev_ids); | |
709 | ||
710 | static struct input_handler evdev_handler = { | |
711 | .event = evdev_event, | |
712 | .connect = evdev_connect, | |
713 | .disconnect = evdev_disconnect, | |
714 | .fops = &evdev_fops, | |
715 | .minor = EVDEV_MINOR_BASE, | |
716 | .name = "evdev", | |
717 | .id_table = evdev_ids, | |
718 | }; | |
719 | ||
720 | static int __init evdev_init(void) | |
721 | { | |
4263cf0f | 722 | return input_register_handler(&evdev_handler); |
1da177e4 LT |
723 | } |
724 | ||
725 | static void __exit evdev_exit(void) | |
726 | { | |
727 | input_unregister_handler(&evdev_handler); | |
728 | } | |
729 | ||
730 | module_init(evdev_init); | |
731 | module_exit(evdev_exit); | |
732 | ||
733 | MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>"); | |
734 | MODULE_DESCRIPTION("Input driver event char devices"); | |
735 | MODULE_LICENSE("GPL"); |