Branch data Line data Source code
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 pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12 : :
13 : : #define EVDEV_MINOR_BASE 64
14 : : #define EVDEV_MINORS 32
15 : : #define EVDEV_MIN_BUFFER_SIZE 64U
16 : : #define EVDEV_BUF_PACKETS 8
17 : :
18 : : #include <linux/poll.h>
19 : : #include <linux/sched.h>
20 : : #include <linux/slab.h>
21 : : #include <linux/vmalloc.h>
22 : : #include <linux/mm.h>
23 : : #include <linux/module.h>
24 : : #include <linux/init.h>
25 : : #include <linux/input/mt.h>
26 : : #include <linux/major.h>
27 : : #include <linux/device.h>
28 : : #include <linux/cdev.h>
29 : : #include <linux/wakelock.h>
30 : : #include "input-compat.h"
31 : :
32 : : struct evdev {
33 : : int open;
34 : : struct input_handle handle;
35 : : wait_queue_head_t wait;
36 : : struct evdev_client __rcu *grab;
37 : : struct list_head client_list;
38 : : spinlock_t client_lock; /* protects client_list */
39 : : struct mutex mutex;
40 : : struct device dev;
41 : : struct cdev cdev;
42 : : bool exist;
43 : : };
44 : :
45 : : struct evdev_client {
46 : : unsigned int head;
47 : : unsigned int tail;
48 : : unsigned int packet_head; /* [future] position of the first element of next packet */
49 : : spinlock_t buffer_lock; /* protects access to buffer, head and tail */
50 : : struct wake_lock wake_lock;
51 : : bool use_wake_lock;
52 : : char name[28];
53 : : struct fasync_struct *fasync;
54 : : struct evdev *evdev;
55 : : struct list_head node;
56 : : int clkid;
57 : : bool revoked;
58 : : unsigned int bufsize;
59 : : struct input_event buffer[];
60 : : };
61 : :
62 : : /* flush queued events of type @type, caller must hold client->buffer_lock */
63 : 0 : static void __evdev_flush_queue(struct evdev_client *client, unsigned int type)
64 : : {
65 : : unsigned int i, head, num;
66 : 0 : unsigned int mask = client->bufsize - 1;
67 : : bool is_report;
68 : : struct input_event *ev;
69 : :
70 [ # # ]: 0 : BUG_ON(type == EV_SYN);
71 : :
72 : 0 : head = client->tail;
73 : 0 : client->packet_head = client->tail;
74 : :
75 : : /* init to 1 so a leading SYN_REPORT will not be dropped */
76 : : num = 1;
77 : :
78 [ # # ]: 0 : for (i = client->tail; i != client->head; i = (i + 1) & mask) {
79 : 0 : ev = &client->buffer[i];
80 : 0 : is_report = ev->type == EV_SYN && ev->code == SYN_REPORT;
81 : :
82 [ # # ]: 0 : if (ev->type == type) {
83 : : /* drop matched entry */
84 : 0 : continue;
85 [ # # ]: 0 : } else if (is_report && !num) {
86 : : /* drop empty SYN_REPORT groups */
87 : 0 : continue;
88 [ # # ]: 0 : } else if (head != i) {
89 : : /* move entry to fill the gap */
90 : 0 : client->buffer[head].time = ev->time;
91 : 0 : client->buffer[head].type = ev->type;
92 : 0 : client->buffer[head].code = ev->code;
93 : 0 : client->buffer[head].value = ev->value;
94 : : }
95 : :
96 : 0 : num++;
97 : 0 : head = (head + 1) & mask;
98 : :
99 [ # # ]: 0 : if (is_report) {
100 : : num = 0;
101 : 0 : client->packet_head = head;
102 : : }
103 : : }
104 : :
105 : 0 : client->head = head;
106 : 0 : }
107 : :
108 : : /* queue SYN_DROPPED event */
109 : 0 : static void evdev_queue_syn_dropped(struct evdev_client *client)
110 : : {
111 : : unsigned long flags;
112 : : struct input_event ev;
113 : : ktime_t time;
114 : :
115 : 0 : time = ktime_get();
116 [ # # ]: 0 : if (client->clkid != CLOCK_MONOTONIC)
117 : 0 : time = ktime_sub(time, ktime_get_monotonic_offset());
118 : :
119 : 0 : ev.time = ktime_to_timeval(time);
120 : : ev.type = EV_SYN;
121 : : ev.code = SYN_DROPPED;
122 : : ev.value = 0;
123 : :
124 : 0 : spin_lock_irqsave(&client->buffer_lock, flags);
125 : :
126 : 0 : client->buffer[client->head++] = ev;
127 : 0 : client->head &= client->bufsize - 1;
128 : :
129 [ # # ]: 0 : if (unlikely(client->head == client->tail)) {
130 : : /* drop queue but keep our SYN_DROPPED event */
131 : 0 : client->tail = (client->head - 1) & (client->bufsize - 1);
132 : 0 : client->packet_head = client->tail;
133 : : }
134 : :
135 : : spin_unlock_irqrestore(&client->buffer_lock, flags);
136 : 0 : }
137 : :
138 : 0 : static void __pass_event(struct evdev_client *client,
139 : : const struct input_event *event)
140 : : {
141 : 0 : client->buffer[client->head++] = *event;
142 : 0 : client->head &= client->bufsize - 1;
143 : :
144 [ # # ]: 0 : if (unlikely(client->head == client->tail)) {
145 : : /*
146 : : * This effectively "drops" all unconsumed events, leaving
147 : : * EV_SYN/SYN_DROPPED plus the newest event in the queue.
148 : : */
149 : 0 : client->tail = (client->head - 2) & (client->bufsize - 1);
150 : :
151 : 0 : client->buffer[client->tail].time = event->time;
152 : 0 : client->buffer[client->tail].type = EV_SYN;
153 : 0 : client->buffer[client->tail].code = SYN_DROPPED;
154 : 0 : client->buffer[client->tail].value = 0;
155 : :
156 : 0 : client->packet_head = client->tail;
157 [ # # ]: 0 : if (client->use_wake_lock)
158 : : wake_unlock(&client->wake_lock);
159 : : }
160 : :
161 [ # # ]: 0 : if (event->type == EV_SYN && event->code == SYN_REPORT) {
162 : 0 : client->packet_head = client->head;
163 [ # # ]: 0 : if (client->use_wake_lock)
164 : : wake_lock(&client->wake_lock);
165 : 0 : kill_fasync(&client->fasync, SIGIO, POLL_IN);
166 : : }
167 : 0 : }
168 : :
169 : 0 : static void evdev_pass_values(struct evdev_client *client,
170 : : const struct input_value *vals, unsigned int count,
171 : : ktime_t mono, ktime_t real)
172 : : {
173 : 0 : struct evdev *evdev = client->evdev;
174 : : const struct input_value *v;
175 : : struct input_event event;
176 : : bool wakeup = false;
177 : :
178 [ # # ]: 0 : if (client->revoked)
179 : 0 : return;
180 : :
181 [ # # ]: 0 : event.time = ktime_to_timeval(client->clkid == CLOCK_MONOTONIC ?
182 : : mono : real);
183 : :
184 : : /* Interrupts are disabled, just acquire the lock. */
185 : : spin_lock(&client->buffer_lock);
186 : :
187 [ # # ]: 0 : for (v = vals; v != vals + count; v++) {
188 : 0 : event.type = v->type;
189 : 0 : event.code = v->code;
190 : 0 : event.value = v->value;
191 : 0 : __pass_event(client, &event);
192 [ # # ]: 0 : if (v->type == EV_SYN && v->code == SYN_REPORT)
193 : : wakeup = true;
194 : : }
195 : :
196 : : spin_unlock(&client->buffer_lock);
197 : :
198 [ # # ]: 0 : if (wakeup)
199 : 0 : wake_up_interruptible(&evdev->wait);
200 : : }
201 : :
202 : : /*
203 : : * Pass incoming events to all connected clients.
204 : : */
205 : 0 : static void evdev_events(struct input_handle *handle,
206 : : const struct input_value *vals, unsigned int count)
207 : : {
208 : 0 : struct evdev *evdev = handle->private;
209 : : struct evdev_client *client;
210 : : ktime_t time_mono, time_real;
211 : :
212 : 0 : time_mono = ktime_get();
213 : 0 : time_real = ktime_sub(time_mono, ktime_get_monotonic_offset());
214 : :
215 : : rcu_read_lock();
216 : :
217 : 0 : client = rcu_dereference(evdev->grab);
218 : :
219 [ # # ]: 0 : if (client)
220 : 0 : evdev_pass_values(client, vals, count, time_mono, time_real);
221 : : else
222 [ # # ]: 0 : list_for_each_entry_rcu(client, &evdev->client_list, node)
223 : 0 : evdev_pass_values(client, vals, count,
224 : : time_mono, time_real);
225 : :
226 : : rcu_read_unlock();
227 : 0 : }
228 : :
229 : : /*
230 : : * Pass incoming event to all connected clients.
231 : : */
232 : 0 : static void evdev_event(struct input_handle *handle,
233 : : unsigned int type, unsigned int code, int value)
234 : : {
235 : 0 : struct input_value vals[] = { { type, code, value } };
236 : :
237 : 0 : evdev_events(handle, vals, 1);
238 : 0 : }
239 : :
240 : 0 : static int evdev_fasync(int fd, struct file *file, int on)
241 : : {
242 : 0 : struct evdev_client *client = file->private_data;
243 : :
244 : 0 : return fasync_helper(fd, file, on, &client->fasync);
245 : : }
246 : :
247 : 0 : static int evdev_flush(struct file *file, fl_owner_t id)
248 : : {
249 : 0 : struct evdev_client *client = file->private_data;
250 : 0 : struct evdev *evdev = client->evdev;
251 : : int retval;
252 : :
253 : 0 : retval = mutex_lock_interruptible(&evdev->mutex);
254 [ # # ]: 0 : if (retval)
255 : : return retval;
256 : :
257 [ # # ][ # # ]: 0 : if (!evdev->exist || client->revoked)
258 : : retval = -ENODEV;
259 : : else
260 : 0 : retval = input_flush_device(&evdev->handle, file);
261 : :
262 : 0 : mutex_unlock(&evdev->mutex);
263 : 0 : return retval;
264 : : }
265 : :
266 : 0 : static void evdev_free(struct device *dev)
267 : : {
268 : 0 : struct evdev *evdev = container_of(dev, struct evdev, dev);
269 : :
270 : 0 : input_put_device(evdev->handle.dev);
271 : 0 : kfree(evdev);
272 : 0 : }
273 : :
274 : : /*
275 : : * Grabs an event device (along with underlying input device).
276 : : * This function is called with evdev->mutex taken.
277 : : */
278 : : static int evdev_grab(struct evdev *evdev, struct evdev_client *client)
279 : : {
280 : : int error;
281 : :
282 [ # # ]: 0 : if (evdev->grab)
283 : : return -EBUSY;
284 : :
285 : 0 : error = input_grab_device(&evdev->handle);
286 [ # # ]: 0 : if (error)
287 : : return error;
288 : :
289 : 0 : rcu_assign_pointer(evdev->grab, client);
290 : :
291 : : return 0;
292 : : }
293 : :
294 : 0 : static int evdev_ungrab(struct evdev *evdev, struct evdev_client *client)
295 : : {
296 : 0 : struct evdev_client *grab = rcu_dereference_protected(evdev->grab,
297 : : lockdep_is_held(&evdev->mutex));
298 : :
299 [ # # ][ # # ]: 0 : if (grab != client)
300 : : return -EINVAL;
301 : :
302 : 0 : rcu_assign_pointer(evdev->grab, NULL);
303 : : synchronize_rcu();
304 : 0 : input_release_device(&evdev->handle);
305 : :
306 : 0 : return 0;
307 : : }
308 : :
309 : 0 : static void evdev_attach_client(struct evdev *evdev,
310 : : struct evdev_client *client)
311 : : {
312 : : spin_lock(&evdev->client_lock);
313 : 0 : list_add_tail_rcu(&client->node, &evdev->client_list);
314 : : spin_unlock(&evdev->client_lock);
315 : 0 : }
316 : :
317 : 0 : static void evdev_detach_client(struct evdev *evdev,
318 : : struct evdev_client *client)
319 : : {
320 : : spin_lock(&evdev->client_lock);
321 : : list_del_rcu(&client->node);
322 : : spin_unlock(&evdev->client_lock);
323 : : synchronize_rcu();
324 : 0 : }
325 : :
326 : 0 : static int evdev_open_device(struct evdev *evdev)
327 : : {
328 : : int retval;
329 : :
330 : 0 : retval = mutex_lock_interruptible(&evdev->mutex);
331 [ # # ]: 0 : if (retval)
332 : : return retval;
333 : :
334 [ # # ]: 0 : if (!evdev->exist)
335 : : retval = -ENODEV;
336 [ # # ]: 0 : else if (!evdev->open++) {
337 : 0 : retval = input_open_device(&evdev->handle);
338 [ # # ]: 0 : if (retval)
339 : 0 : evdev->open--;
340 : : }
341 : :
342 : 0 : mutex_unlock(&evdev->mutex);
343 : 0 : return retval;
344 : : }
345 : :
346 : 0 : static void evdev_close_device(struct evdev *evdev)
347 : : {
348 : 0 : mutex_lock(&evdev->mutex);
349 : :
350 [ # # ][ # # ]: 0 : if (evdev->exist && !--evdev->open)
351 : 0 : input_close_device(&evdev->handle);
352 : :
353 : 0 : mutex_unlock(&evdev->mutex);
354 : 0 : }
355 : :
356 : : /*
357 : : * Wake up users waiting for IO so they can disconnect from
358 : : * dead device.
359 : : */
360 : 0 : static void evdev_hangup(struct evdev *evdev)
361 : : {
362 : : struct evdev_client *client;
363 : :
364 : : spin_lock(&evdev->client_lock);
365 [ # # ]: 0 : list_for_each_entry(client, &evdev->client_list, node)
366 : 0 : kill_fasync(&client->fasync, SIGIO, POLL_HUP);
367 : : spin_unlock(&evdev->client_lock);
368 : :
369 : 0 : wake_up_interruptible(&evdev->wait);
370 : 0 : }
371 : :
372 : 0 : static int evdev_release(struct inode *inode, struct file *file)
373 : : {
374 : 0 : struct evdev_client *client = file->private_data;
375 : 0 : struct evdev *evdev = client->evdev;
376 : :
377 : 0 : mutex_lock(&evdev->mutex);
378 : 0 : evdev_ungrab(evdev, client);
379 : 0 : mutex_unlock(&evdev->mutex);
380 : :
381 : 0 : evdev_detach_client(evdev, client);
382 : :
383 [ # # ]: 0 : if (client->use_wake_lock)
384 : : wake_lock_destroy(&client->wake_lock);
385 : :
386 [ # # ]: 0 : if (is_vmalloc_addr(client))
387 : 0 : vfree(client);
388 : : else
389 : 0 : kfree(client);
390 : :
391 : 0 : evdev_close_device(evdev);
392 : :
393 : 0 : return 0;
394 : : }
395 : :
396 : 0 : static unsigned int evdev_compute_buffer_size(struct input_dev *dev)
397 : : {
398 : : unsigned int n_events =
399 : 0 : max(dev->hint_events_per_packet * EVDEV_BUF_PACKETS,
400 : : EVDEV_MIN_BUFFER_SIZE);
401 : :
402 [ # # ][ # # ]: 0 : return roundup_pow_of_two(n_events);
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ][ # # ]
403 : : }
404 : :
405 : 0 : static int evdev_open(struct inode *inode, struct file *file)
406 : : {
407 : 0 : struct evdev *evdev = container_of(inode->i_cdev, struct evdev, cdev);
408 : 0 : unsigned int bufsize = evdev_compute_buffer_size(evdev->handle.dev);
409 : 0 : unsigned int size = sizeof(struct evdev_client) +
410 : 0 : bufsize * sizeof(struct input_event);
411 : : struct evdev_client *client;
412 : : int error;
413 : :
414 : : client = kzalloc(size, GFP_KERNEL | __GFP_NOWARN);
415 [ # # ]: 0 : if (!client)
416 : 0 : client = vzalloc(size);
417 [ # # ]: 0 : if (!client)
418 : : return -ENOMEM;
419 : :
420 : 0 : client->bufsize = bufsize;
421 : 0 : spin_lock_init(&client->buffer_lock);
422 : 0 : snprintf(client->name, sizeof(client->name), "%s-%d",
423 : : dev_name(&evdev->dev), task_tgid_vnr(current));
424 : 0 : client->evdev = evdev;
425 : 0 : evdev_attach_client(evdev, client);
426 : :
427 : 0 : error = evdev_open_device(evdev);
428 [ # # ]: 0 : if (error)
429 : : goto err_free_client;
430 : :
431 : 0 : file->private_data = client;
432 : 0 : nonseekable_open(inode, file);
433 : :
434 : 0 : return 0;
435 : :
436 : : err_free_client:
437 : 0 : evdev_detach_client(evdev, client);
438 : 0 : kfree(client);
439 : 0 : return error;
440 : : }
441 : :
442 : 0 : static ssize_t evdev_write(struct file *file, const char __user *buffer,
443 : : size_t count, loff_t *ppos)
444 : : {
445 : 0 : struct evdev_client *client = file->private_data;
446 : 0 : struct evdev *evdev = client->evdev;
447 : : struct input_event event;
448 : : int retval = 0;
449 : :
450 [ # # ][ # # ]: 0 : if (count != 0 && count < input_event_size())
451 : : return -EINVAL;
452 : :
453 : 0 : retval = mutex_lock_interruptible(&evdev->mutex);
454 [ # # ]: 0 : if (retval)
455 : : return retval;
456 : :
457 [ # # ][ # # ]: 0 : if (!evdev->exist || client->revoked) {
458 : : retval = -ENODEV;
459 : : goto out;
460 : : }
461 : :
462 [ # # ]: 0 : while (retval + input_event_size() <= count) {
463 : :
464 [ # # ]: 0 : if (input_event_from_user(buffer + retval, &event)) {
465 : : retval = -EFAULT;
466 : : goto out;
467 : : }
468 : 0 : retval += input_event_size();
469 : :
470 : 0 : input_inject_event(&evdev->handle,
471 : 0 : event.type, event.code, event.value);
472 : : }
473 : :
474 : : out:
475 : 0 : mutex_unlock(&evdev->mutex);
476 : 0 : return retval;
477 : : }
478 : :
479 : 0 : static int evdev_fetch_next_event(struct evdev_client *client,
480 : : struct input_event *event)
481 : : {
482 : : int have_event;
483 : :
484 : : spin_lock_irq(&client->buffer_lock);
485 : :
486 : 0 : have_event = client->packet_head != client->tail;
487 [ # # ]: 0 : if (have_event) {
488 : 0 : *event = client->buffer[client->tail++];
489 : 0 : client->tail &= client->bufsize - 1;
490 [ # # ][ # # ]: 0 : if (client->use_wake_lock &&
491 : 0 : client->packet_head == client->tail)
492 : : wake_unlock(&client->wake_lock);
493 : : }
494 : :
495 : : spin_unlock_irq(&client->buffer_lock);
496 : :
497 : 0 : return have_event;
498 : : }
499 : :
500 : 0 : static ssize_t evdev_read(struct file *file, char __user *buffer,
501 : : size_t count, loff_t *ppos)
502 : : {
503 : 0 : struct evdev_client *client = file->private_data;
504 : 0 : struct evdev *evdev = client->evdev;
505 : : struct input_event event;
506 : : size_t read = 0;
507 : : int error;
508 : :
509 [ # # ][ # # ]: 0 : if (count != 0 && count < input_event_size())
510 : : return -EINVAL;
511 : :
512 : : for (;;) {
513 [ # # ][ # # ]: 0 : if (!evdev->exist || client->revoked)
514 : : return -ENODEV;
515 : :
516 [ # # ][ # # ]: 0 : if (client->packet_head == client->tail &&
517 : 0 : (file->f_flags & O_NONBLOCK))
518 : : return -EAGAIN;
519 : :
520 : : /*
521 : : * count == 0 is special - no IO is done but we check
522 : : * for error conditions (see above).
523 : : */
524 [ # # ]: 0 : if (count == 0)
525 : : break;
526 : :
527 [ # # # # ]: 0 : while (read + input_event_size() <= count &&
528 : 0 : evdev_fetch_next_event(client, &event)) {
529 : :
530 [ # # ]: 0 : if (input_event_to_user(buffer + read, &event))
531 : : return -EFAULT;
532 : :
533 : : read += input_event_size();
534 : : }
535 : :
536 [ # # ]: 0 : if (read)
537 : : break;
538 : :
539 [ # # ]: 0 : if (!(file->f_flags & O_NONBLOCK)) {
540 [ # # ][ # # ]: 0 : error = wait_event_interruptible(evdev->wait,
[ # # ][ # # ]
[ # # ][ # # ]
[ # # ]
541 : : client->packet_head != client->tail ||
542 : : !evdev->exist || client->revoked);
543 [ # # ]: 0 : if (error)
544 : : return error;
545 : : }
546 : : }
547 : :
548 : 0 : return read;
549 : : }
550 : :
551 : : /* No kernel lock - fine */
552 : 0 : static unsigned int evdev_poll(struct file *file, poll_table *wait)
553 : : {
554 : 0 : struct evdev_client *client = file->private_data;
555 : 0 : struct evdev *evdev = client->evdev;
556 : : unsigned int mask;
557 : :
558 : 0 : poll_wait(file, &evdev->wait, wait);
559 : :
560 [ # # ][ # # ]: 0 : if (evdev->exist && !client->revoked)
561 : : mask = POLLOUT | POLLWRNORM;
562 : : else
563 : : mask = POLLHUP | POLLERR;
564 : :
565 [ # # ]: 0 : if (client->packet_head != client->tail)
566 : 0 : mask |= POLLIN | POLLRDNORM;
567 : :
568 : 0 : return mask;
569 : : }
570 : :
571 : : #ifdef CONFIG_COMPAT
572 : :
573 : : #define BITS_PER_LONG_COMPAT (sizeof(compat_long_t) * 8)
574 : : #define BITS_TO_LONGS_COMPAT(x) ((((x) - 1) / BITS_PER_LONG_COMPAT) + 1)
575 : :
576 : : #ifdef __BIG_ENDIAN
577 : : static int bits_to_user(unsigned long *bits, unsigned int maxbit,
578 : : unsigned int maxlen, void __user *p, int compat)
579 : : {
580 : : int len, i;
581 : :
582 : : if (compat) {
583 : : len = BITS_TO_LONGS_COMPAT(maxbit) * sizeof(compat_long_t);
584 : : if (len > maxlen)
585 : : len = maxlen;
586 : :
587 : : for (i = 0; i < len / sizeof(compat_long_t); i++)
588 : : if (copy_to_user((compat_long_t __user *) p + i,
589 : : (compat_long_t *) bits +
590 : : i + 1 - ((i % 2) << 1),
591 : : sizeof(compat_long_t)))
592 : : return -EFAULT;
593 : : } else {
594 : : len = BITS_TO_LONGS(maxbit) * sizeof(long);
595 : : if (len > maxlen)
596 : : len = maxlen;
597 : :
598 : : if (copy_to_user(p, bits, len))
599 : : return -EFAULT;
600 : : }
601 : :
602 : : return len;
603 : : }
604 : : #else
605 : : static int bits_to_user(unsigned long *bits, unsigned int maxbit,
606 : : unsigned int maxlen, void __user *p, int compat)
607 : : {
608 : : int len = compat ?
609 : : BITS_TO_LONGS_COMPAT(maxbit) * sizeof(compat_long_t) :
610 : : BITS_TO_LONGS(maxbit) * sizeof(long);
611 : :
612 : : if (len > maxlen)
613 : : len = maxlen;
614 : :
615 : : return copy_to_user(p, bits, len) ? -EFAULT : len;
616 : : }
617 : : #endif /* __BIG_ENDIAN */
618 : :
619 : : #else
620 : :
621 : 0 : static int bits_to_user(unsigned long *bits, unsigned int maxbit,
622 : : unsigned int maxlen, void __user *p, int compat)
623 : : {
624 : 0 : int len = BITS_TO_LONGS(maxbit) * sizeof(long);
625 : :
626 [ # # ]: 0 : if (len > maxlen)
627 : 0 : len = maxlen;
628 : :
629 [ # # ]: 0 : return copy_to_user(p, bits, len) ? -EFAULT : len;
630 : : }
631 : :
632 : : #endif /* CONFIG_COMPAT */
633 : :
634 : 0 : static int str_to_user(const char *str, unsigned int maxlen, void __user *p)
635 : : {
636 : : int len;
637 : :
638 [ # # ]: 0 : if (!str)
639 : : return -ENOENT;
640 : :
641 : 0 : len = strlen(str) + 1;
642 [ # # ]: 0 : if (len > maxlen)
643 : 0 : len = maxlen;
644 : :
645 [ # # ]: 0 : return copy_to_user(p, str, len) ? -EFAULT : len;
646 : : }
647 : :
648 : : #define OLD_KEY_MAX 0x1ff
649 : 0 : static int handle_eviocgbit(struct input_dev *dev,
650 : : unsigned int type, unsigned int size,
651 : : void __user *p, int compat_mode)
652 : : {
653 : : static unsigned long keymax_warn_time;
654 : : unsigned long *bits;
655 : : int len;
656 : :
657 [ # # # # : 0 : switch (type) {
# # # # #
# ]
658 : :
659 : 0 : case 0: bits = dev->evbit; len = EV_MAX; break;
660 : 0 : case EV_KEY: bits = dev->keybit; len = KEY_MAX; break;
661 : 0 : case EV_REL: bits = dev->relbit; len = REL_MAX; break;
662 : 0 : case EV_ABS: bits = dev->absbit; len = ABS_MAX; break;
663 : 0 : case EV_MSC: bits = dev->mscbit; len = MSC_MAX; break;
664 : 0 : case EV_LED: bits = dev->ledbit; len = LED_MAX; break;
665 : 0 : case EV_SND: bits = dev->sndbit; len = SND_MAX; break;
666 : 0 : case EV_FF: bits = dev->ffbit; len = FF_MAX; break;
667 : 0 : case EV_SW: bits = dev->swbit; len = SW_MAX; break;
668 : : default: return -EINVAL;
669 : : }
670 : :
671 : : /*
672 : : * Work around bugs in userspace programs that like to do
673 : : * EVIOCGBIT(EV_KEY, KEY_MAX) and not realize that 'len'
674 : : * should be in bytes, not in bits.
675 : : */
676 [ # # ]: 0 : if (type == EV_KEY && size == OLD_KEY_MAX) {
677 : : len = OLD_KEY_MAX;
678 [ # # ]: 0 : if (printk_timed_ratelimit(&keymax_warn_time, 10 * 1000))
679 : 0 : pr_warning("(EVIOCGBIT): Suspicious buffer size %u, "
680 : : "limiting output to %zu bytes. See "
681 : : "http://userweb.kernel.org/~dtor/eviocgbit-bug.html\n",
682 : : OLD_KEY_MAX,
683 : : BITS_TO_LONGS(OLD_KEY_MAX) * sizeof(long));
684 : : }
685 : :
686 : 0 : return bits_to_user(bits, len, size, p, compat_mode);
687 : : }
688 : : #undef OLD_KEY_MAX
689 : :
690 : 0 : static int evdev_handle_get_keycode(struct input_dev *dev, void __user *p)
691 : : {
692 : 0 : struct input_keymap_entry ke = {
693 : : .len = sizeof(unsigned int),
694 : : .flags = 0,
695 : : };
696 : : int __user *ip = (int __user *)p;
697 : : int error;
698 : :
699 : : /* legacy case */
700 [ # # ]: 0 : if (copy_from_user(ke.scancode, p, sizeof(unsigned int)))
701 : : return -EFAULT;
702 : :
703 : 0 : error = input_get_keycode(dev, &ke);
704 [ # # ]: 0 : if (error)
705 : : return error;
706 : :
707 [ # # ]: 0 : if (put_user(ke.keycode, ip + 1))
708 : : return -EFAULT;
709 : :
710 : 0 : return 0;
711 : : }
712 : :
713 : 0 : static int evdev_handle_get_keycode_v2(struct input_dev *dev, void __user *p)
714 : : {
715 : : struct input_keymap_entry ke;
716 : : int error;
717 : :
718 [ # # ]: 0 : if (copy_from_user(&ke, p, sizeof(ke)))
719 : : return -EFAULT;
720 : :
721 : 0 : error = input_get_keycode(dev, &ke);
722 [ # # ]: 0 : if (error)
723 : : return error;
724 : :
725 [ # # ]: 0 : if (copy_to_user(p, &ke, sizeof(ke)))
726 : : return -EFAULT;
727 : :
728 : 0 : return 0;
729 : : }
730 : :
731 : 0 : static int evdev_handle_set_keycode(struct input_dev *dev, void __user *p)
732 : : {
733 : 0 : struct input_keymap_entry ke = {
734 : : .len = sizeof(unsigned int),
735 : : .flags = 0,
736 : : };
737 : : int __user *ip = (int __user *)p;
738 : :
739 [ # # ]: 0 : if (copy_from_user(ke.scancode, p, sizeof(unsigned int)))
740 : : return -EFAULT;
741 : :
742 [ # # ]: 0 : if (get_user(ke.keycode, ip + 1))
743 : : return -EFAULT;
744 : :
745 : 0 : return input_set_keycode(dev, &ke);
746 : : }
747 : :
748 : 0 : static int evdev_handle_set_keycode_v2(struct input_dev *dev, void __user *p)
749 : : {
750 : : struct input_keymap_entry ke;
751 : :
752 [ # # ]: 0 : if (copy_from_user(&ke, p, sizeof(ke)))
753 : : return -EFAULT;
754 : :
755 [ # # ]: 0 : if (ke.len > sizeof(ke.scancode))
756 : : return -EINVAL;
757 : :
758 : 0 : return input_set_keycode(dev, &ke);
759 : : }
760 : :
761 : : /*
762 : : * If we transfer state to the user, we should flush all pending events
763 : : * of the same type from the client's queue. Otherwise, they might end up
764 : : * with duplicate events, which can screw up client's state tracking.
765 : : * If bits_to_user fails after flushing the queue, we queue a SYN_DROPPED
766 : : * event so user-space will notice missing events.
767 : : *
768 : : * LOCKING:
769 : : * We need to take event_lock before buffer_lock to avoid dead-locks. But we
770 : : * need the even_lock only to guarantee consistent state. We can safely release
771 : : * it while flushing the queue. This allows input-core to handle filters while
772 : : * we flush the queue.
773 : : */
774 : 0 : static int evdev_handle_get_val(struct evdev_client *client,
775 : : struct input_dev *dev, unsigned int type,
776 : : unsigned long *bits, unsigned int max,
777 : : unsigned int size, void __user *p, int compat)
778 : : {
779 : : int ret;
780 : : unsigned long *mem;
781 : :
782 : 0 : mem = kmalloc(sizeof(unsigned long) * max, GFP_KERNEL);
783 [ # # ]: 0 : if (!mem)
784 : : return -ENOMEM;
785 : :
786 : : spin_lock_irq(&dev->event_lock);
787 : : spin_lock(&client->buffer_lock);
788 : :
789 : 0 : memcpy(mem, bits, sizeof(unsigned long) * max);
790 : :
791 : : spin_unlock(&dev->event_lock);
792 : :
793 : 0 : __evdev_flush_queue(client, type);
794 : :
795 : : spin_unlock_irq(&client->buffer_lock);
796 : :
797 : 0 : ret = bits_to_user(mem, max, size, p, compat);
798 [ # # ]: 0 : if (ret < 0)
799 : 0 : evdev_queue_syn_dropped(client);
800 : :
801 : 0 : kfree(mem);
802 : :
803 : : return ret;
804 : : }
805 : :
806 : 0 : static int evdev_handle_mt_request(struct input_dev *dev,
807 : : unsigned int size,
808 : : int __user *ip)
809 : : {
810 : 0 : const struct input_mt *mt = dev->mt;
811 : : unsigned int code;
812 : : int max_slots;
813 : : int i;
814 : :
815 [ # # ]: 0 : if (get_user(code, &ip[0]))
816 : : return -EFAULT;
817 [ # # ][ # # ]: 0 : if (!mt || !input_is_mt_value(code))
818 : : return -EINVAL;
819 : :
820 : 0 : max_slots = (size - sizeof(__u32)) / sizeof(__s32);
821 [ # # ][ # # ]: 0 : for (i = 0; i < mt->num_slots && i < max_slots; i++) {
822 : 0 : int value = input_mt_get_value(&mt->slots[i], code);
823 [ # # ]: 0 : if (put_user(value, &ip[1 + i]))
824 : : return -EFAULT;
825 : : }
826 : :
827 : : return 0;
828 : : }
829 : :
830 : 0 : static int evdev_enable_suspend_block(struct evdev *evdev,
831 : : struct evdev_client *client)
832 : : {
833 [ # # ]: 0 : if (client->use_wake_lock)
834 : : return 0;
835 : :
836 : : spin_lock_irq(&client->buffer_lock);
837 : 0 : wake_lock_init(&client->wake_lock, WAKE_LOCK_SUSPEND, client->name);
838 : 0 : client->use_wake_lock = true;
839 [ # # ]: 0 : if (client->packet_head != client->tail)
840 : : wake_lock(&client->wake_lock);
841 : : spin_unlock_irq(&client->buffer_lock);
842 : : return 0;
843 : : }
844 : :
845 : 0 : static int evdev_disable_suspend_block(struct evdev *evdev,
846 : : struct evdev_client *client)
847 : : {
848 [ # # ]: 0 : if (!client->use_wake_lock)
849 : : return 0;
850 : :
851 : : spin_lock_irq(&client->buffer_lock);
852 : 0 : client->use_wake_lock = false;
853 : : wake_lock_destroy(&client->wake_lock);
854 : : spin_unlock_irq(&client->buffer_lock);
855 : :
856 : : return 0;
857 : : }
858 : :
859 : 0 : static int evdev_revoke(struct evdev *evdev, struct evdev_client *client,
860 : : struct file *file)
861 : : {
862 : 0 : client->revoked = true;
863 : 0 : evdev_ungrab(evdev, client);
864 : 0 : input_flush_device(&evdev->handle, file);
865 : 0 : wake_up_interruptible(&evdev->wait);
866 : :
867 : 0 : return 0;
868 : : }
869 : :
870 : 0 : static long evdev_do_ioctl(struct file *file, unsigned int cmd,
871 : : void __user *p, int compat_mode)
872 : : {
873 : 0 : struct evdev_client *client = file->private_data;
874 : 0 : struct evdev *evdev = client->evdev;
875 : 0 : struct input_dev *dev = evdev->handle.dev;
876 : : struct input_absinfo abs;
877 : : struct ff_effect effect;
878 : : int __user *ip = (int __user *)p;
879 : : unsigned int i, t, u, v;
880 : : unsigned int size;
881 : : int error;
882 : :
883 : : /* First we check for fixed-length commands */
884 [ # # # # : 0 : switch (cmd) {
# # # # #
# # # # #
# # ]
885 : :
886 : : case EVIOCGVERSION:
887 : 0 : return put_user(EV_VERSION, ip);
888 : :
889 : : case EVIOCGID:
890 [ # # ]: 0 : if (copy_to_user(p, &dev->id, sizeof(struct input_id)))
891 : : return -EFAULT;
892 : : return 0;
893 : :
894 : : case EVIOCGREP:
895 [ # # ]: 0 : if (!test_bit(EV_REP, dev->evbit))
896 : : return -ENOSYS;
897 [ # # ]: 0 : if (put_user(dev->rep[REP_DELAY], ip))
898 : : return -EFAULT;
899 [ # # ]: 0 : if (put_user(dev->rep[REP_PERIOD], ip + 1))
900 : : return -EFAULT;
901 : : return 0;
902 : :
903 : : case EVIOCSREP:
904 [ # # ]: 0 : if (!test_bit(EV_REP, dev->evbit))
905 : : return -ENOSYS;
906 [ # # ]: 0 : if (get_user(u, ip))
907 : : return -EFAULT;
908 [ # # ]: 0 : if (get_user(v, ip + 1))
909 : : return -EFAULT;
910 : :
911 : 0 : input_inject_event(&evdev->handle, EV_REP, REP_DELAY, u);
912 : 0 : input_inject_event(&evdev->handle, EV_REP, REP_PERIOD, v);
913 : :
914 : : return 0;
915 : :
916 : : case EVIOCRMFF:
917 : 0 : return input_ff_erase(dev, (int)(unsigned long) p, file);
918 : :
919 : : case EVIOCGEFFECTS:
920 [ # # ]: 0 : i = test_bit(EV_FF, dev->evbit) ?
921 : 0 : dev->ff->max_effects : 0;
922 [ # # ]: 0 : if (put_user(i, ip))
923 : : return -EFAULT;
924 : : return 0;
925 : :
926 : : case EVIOCGRAB:
927 [ # # ]: 0 : if (p)
928 : : return evdev_grab(evdev, client);
929 : : else
930 : : return evdev_ungrab(evdev, client);
931 : :
932 : : case EVIOCREVOKE:
933 [ # # ]: 0 : if (p)
934 : : return -EINVAL;
935 : : else
936 : 0 : return evdev_revoke(evdev, client, file);
937 : :
938 : : case EVIOCSCLOCKID:
939 [ # # ]: 0 : if (copy_from_user(&i, p, sizeof(unsigned int)))
940 : : return -EFAULT;
941 [ # # ]: 0 : if (i != CLOCK_MONOTONIC && i != CLOCK_REALTIME)
942 : : return -EINVAL;
943 : 0 : client->clkid = i;
944 : : return 0;
945 : :
946 : : case EVIOCGKEYCODE:
947 : 0 : return evdev_handle_get_keycode(dev, p);
948 : :
949 : : case EVIOCSKEYCODE:
950 : 0 : return evdev_handle_set_keycode(dev, p);
951 : :
952 : : case EVIOCGKEYCODE_V2:
953 : 0 : return evdev_handle_get_keycode_v2(dev, p);
954 : :
955 : : case EVIOCSKEYCODE_V2:
956 : 0 : return evdev_handle_set_keycode_v2(dev, p);
957 : :
958 : : case EVIOCGSUSPENDBLOCK:
959 : 0 : return put_user(client->use_wake_lock, ip);
960 : :
961 : : case EVIOCSSUSPENDBLOCK:
962 [ # # ]: 0 : if (p)
963 : 0 : return evdev_enable_suspend_block(evdev, client);
964 : : else
965 : 0 : return evdev_disable_suspend_block(evdev, client);
966 : : }
967 : :
968 : 0 : size = _IOC_SIZE(cmd);
969 : :
970 : : /* Now check variable-length commands */
971 : : #define EVIOC_MASK_SIZE(nr) ((nr) & ~(_IOC_SIZEMASK << _IOC_SIZESHIFT))
972 [ # # # # : 0 : switch (EVIOC_MASK_SIZE(cmd)) {
# # # # #
# # ]
973 : :
974 : : case EVIOCGPROP(0):
975 : 0 : return bits_to_user(dev->propbit, INPUT_PROP_MAX,
976 : : size, p, compat_mode);
977 : :
978 : : case EVIOCGMTSLOTS(0):
979 : 0 : return evdev_handle_mt_request(dev, size, ip);
980 : :
981 : : case EVIOCGKEY(0):
982 : 0 : return evdev_handle_get_val(client, dev, EV_KEY, dev->key,
983 : : KEY_MAX, size, p, compat_mode);
984 : :
985 : : case EVIOCGLED(0):
986 : 0 : return evdev_handle_get_val(client, dev, EV_LED, dev->led,
987 : : LED_MAX, size, p, compat_mode);
988 : :
989 : : case EVIOCGSND(0):
990 : 0 : return evdev_handle_get_val(client, dev, EV_SND, dev->snd,
991 : : SND_MAX, size, p, compat_mode);
992 : :
993 : : case EVIOCGSW(0):
994 : 0 : return evdev_handle_get_val(client, dev, EV_SW, dev->sw,
995 : : SW_MAX, size, p, compat_mode);
996 : :
997 : : case EVIOCGNAME(0):
998 : 0 : return str_to_user(dev->name, size, p);
999 : :
1000 : : case EVIOCGPHYS(0):
1001 : 0 : return str_to_user(dev->phys, size, p);
1002 : :
1003 : : case EVIOCGUNIQ(0):
1004 : 0 : return str_to_user(dev->uniq, size, p);
1005 : :
1006 : : case EVIOC_MASK_SIZE(EVIOCSFF):
1007 [ # # ]: 0 : if (input_ff_effect_from_user(p, size, &effect))
1008 : : return -EFAULT;
1009 : :
1010 : 0 : error = input_ff_upload(dev, &effect, file);
1011 : :
1012 [ # # ]: 0 : if (put_user(effect.id, &(((struct ff_effect __user *)p)->id)))
1013 : : return -EFAULT;
1014 : :
1015 : : return error;
1016 : : }
1017 : :
1018 : : /* Multi-number variable-length handlers */
1019 [ # # ]: 0 : if (_IOC_TYPE(cmd) != 'E')
1020 : : return -EINVAL;
1021 : :
1022 [ # # ]: 0 : if (_IOC_DIR(cmd) == _IOC_READ) {
1023 : :
1024 [ # # ]: 0 : if ((_IOC_NR(cmd) & ~EV_MAX) == _IOC_NR(EVIOCGBIT(0, 0)))
1025 : 0 : return handle_eviocgbit(dev,
1026 : : _IOC_NR(cmd) & EV_MAX, size,
1027 : : p, compat_mode);
1028 : :
1029 [ # # ]: 0 : if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCGABS(0))) {
1030 : :
1031 [ # # ]: 0 : if (!dev->absinfo)
1032 : : return -EINVAL;
1033 : :
1034 : 0 : t = _IOC_NR(cmd) & ABS_MAX;
1035 : 0 : abs = dev->absinfo[t];
1036 : :
1037 [ # # ]: 0 : if (copy_to_user(p, &abs, min_t(size_t,
1038 : : size, sizeof(struct input_absinfo))))
1039 : : return -EFAULT;
1040 : :
1041 : : return 0;
1042 : : }
1043 : : }
1044 : :
1045 [ # # ]: 0 : if (_IOC_DIR(cmd) == _IOC_WRITE) {
1046 : :
1047 [ # # ]: 0 : if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCSABS(0))) {
1048 : :
1049 [ # # ]: 0 : if (!dev->absinfo)
1050 : : return -EINVAL;
1051 : :
1052 : 0 : t = _IOC_NR(cmd) & ABS_MAX;
1053 : :
1054 [ # # ]: 0 : if (copy_from_user(&abs, p, min_t(size_t,
1055 : : size, sizeof(struct input_absinfo))))
1056 : : return -EFAULT;
1057 : :
1058 [ # # ]: 0 : if (size < sizeof(struct input_absinfo))
1059 : 0 : abs.resolution = 0;
1060 : :
1061 : : /* We can't change number of reserved MT slots */
1062 [ # # ]: 0 : if (t == ABS_MT_SLOT)
1063 : : return -EINVAL;
1064 : :
1065 : : /*
1066 : : * Take event lock to ensure that we are not
1067 : : * changing device parameters in the middle
1068 : : * of event.
1069 : : */
1070 : : spin_lock_irq(&dev->event_lock);
1071 : 0 : dev->absinfo[t] = abs;
1072 : : spin_unlock_irq(&dev->event_lock);
1073 : :
1074 : : return 0;
1075 : : }
1076 : : }
1077 : :
1078 : : return -EINVAL;
1079 : : }
1080 : :
1081 : 0 : static long evdev_ioctl_handler(struct file *file, unsigned int cmd,
1082 : : void __user *p, int compat_mode)
1083 : : {
1084 : 0 : struct evdev_client *client = file->private_data;
1085 : 0 : struct evdev *evdev = client->evdev;
1086 : : int retval;
1087 : :
1088 : 0 : retval = mutex_lock_interruptible(&evdev->mutex);
1089 [ # # ]: 0 : if (retval)
1090 : : return retval;
1091 : :
1092 [ # # ][ # # ]: 0 : if (!evdev->exist || client->revoked) {
1093 : : retval = -ENODEV;
1094 : : goto out;
1095 : : }
1096 : :
1097 : 0 : retval = evdev_do_ioctl(file, cmd, p, compat_mode);
1098 : :
1099 : : out:
1100 : 0 : mutex_unlock(&evdev->mutex);
1101 : : return retval;
1102 : : }
1103 : :
1104 : 0 : static long evdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1105 : : {
1106 : 0 : return evdev_ioctl_handler(file, cmd, (void __user *)arg, 0);
1107 : : }
1108 : :
1109 : : #ifdef CONFIG_COMPAT
1110 : : static long evdev_ioctl_compat(struct file *file,
1111 : : unsigned int cmd, unsigned long arg)
1112 : : {
1113 : : return evdev_ioctl_handler(file, cmd, compat_ptr(arg), 1);
1114 : : }
1115 : : #endif
1116 : :
1117 : : static const struct file_operations evdev_fops = {
1118 : : .owner = THIS_MODULE,
1119 : : .read = evdev_read,
1120 : : .write = evdev_write,
1121 : : .poll = evdev_poll,
1122 : : .open = evdev_open,
1123 : : .release = evdev_release,
1124 : : .unlocked_ioctl = evdev_ioctl,
1125 : : #ifdef CONFIG_COMPAT
1126 : : .compat_ioctl = evdev_ioctl_compat,
1127 : : #endif
1128 : : .fasync = evdev_fasync,
1129 : : .flush = evdev_flush,
1130 : : .llseek = no_llseek,
1131 : : };
1132 : :
1133 : : /*
1134 : : * Mark device non-existent. This disables writes, ioctls and
1135 : : * prevents new users from opening the device. Already posted
1136 : : * blocking reads will stay, however new ones will fail.
1137 : : */
1138 : : static void evdev_mark_dead(struct evdev *evdev)
1139 : : {
1140 : 0 : mutex_lock(&evdev->mutex);
1141 : 0 : evdev->exist = false;
1142 : 0 : mutex_unlock(&evdev->mutex);
1143 : : }
1144 : :
1145 : 0 : static void evdev_cleanup(struct evdev *evdev)
1146 : : {
1147 : 0 : struct input_handle *handle = &evdev->handle;
1148 : :
1149 : : evdev_mark_dead(evdev);
1150 : 0 : evdev_hangup(evdev);
1151 : :
1152 : 0 : cdev_del(&evdev->cdev);
1153 : :
1154 : : /* evdev is marked dead so no one else accesses evdev->open */
1155 [ # # ]: 0 : if (evdev->open) {
1156 : 0 : input_flush_device(handle, NULL);
1157 : 0 : input_close_device(handle);
1158 : : }
1159 : 0 : }
1160 : :
1161 : : /*
1162 : : * Create new evdev device. Note that input core serializes calls
1163 : : * to connect and disconnect.
1164 : : */
1165 : 0 : static int evdev_connect(struct input_handler *handler, struct input_dev *dev,
1166 : : const struct input_device_id *id)
1167 : : {
1168 : : struct evdev *evdev;
1169 : : int minor;
1170 : : int dev_no;
1171 : : int error;
1172 : :
1173 : 0 : minor = input_get_new_minor(EVDEV_MINOR_BASE, EVDEV_MINORS, true);
1174 [ # # ]: 0 : if (minor < 0) {
1175 : : error = minor;
1176 : 0 : pr_err("failed to reserve new minor: %d\n", error);
1177 : 0 : return error;
1178 : : }
1179 : :
1180 : : evdev = kzalloc(sizeof(struct evdev), GFP_KERNEL);
1181 [ # # ]: 0 : if (!evdev) {
1182 : : error = -ENOMEM;
1183 : : goto err_free_minor;
1184 : : }
1185 : :
1186 : 0 : INIT_LIST_HEAD(&evdev->client_list);
1187 : 0 : spin_lock_init(&evdev->client_lock);
1188 : 0 : mutex_init(&evdev->mutex);
1189 : 0 : init_waitqueue_head(&evdev->wait);
1190 : 0 : evdev->exist = true;
1191 : :
1192 : : dev_no = minor;
1193 : : /* Normalize device number if it falls into legacy range */
1194 [ # # ]: 0 : if (dev_no < EVDEV_MINOR_BASE + EVDEV_MINORS)
1195 : 0 : dev_no -= EVDEV_MINOR_BASE;
1196 : 0 : dev_set_name(&evdev->dev, "event%d", dev_no);
1197 : :
1198 : 0 : evdev->handle.dev = input_get_device(dev);
1199 : 0 : evdev->handle.name = dev_name(&evdev->dev);
1200 : 0 : evdev->handle.handler = handler;
1201 : 0 : evdev->handle.private = evdev;
1202 : :
1203 : 0 : evdev->dev.devt = MKDEV(INPUT_MAJOR, minor);
1204 : 0 : evdev->dev.class = &input_class;
1205 : 0 : evdev->dev.parent = &dev->dev;
1206 : 0 : evdev->dev.release = evdev_free;
1207 : 0 : device_initialize(&evdev->dev);
1208 : :
1209 : 0 : error = input_register_handle(&evdev->handle);
1210 [ # # ]: 0 : if (error)
1211 : : goto err_free_evdev;
1212 : :
1213 : 0 : cdev_init(&evdev->cdev, &evdev_fops);
1214 : 0 : evdev->cdev.kobj.parent = &evdev->dev.kobj;
1215 : 0 : error = cdev_add(&evdev->cdev, evdev->dev.devt, 1);
1216 [ # # ]: 0 : if (error)
1217 : : goto err_unregister_handle;
1218 : :
1219 : 0 : error = device_add(&evdev->dev);
1220 [ # # ]: 0 : if (error)
1221 : : goto err_cleanup_evdev;
1222 : :
1223 : : return 0;
1224 : :
1225 : : err_cleanup_evdev:
1226 : 0 : evdev_cleanup(evdev);
1227 : : err_unregister_handle:
1228 : 0 : input_unregister_handle(&evdev->handle);
1229 : : err_free_evdev:
1230 : 0 : put_device(&evdev->dev);
1231 : : err_free_minor:
1232 : 0 : input_free_minor(minor);
1233 : 0 : return error;
1234 : : }
1235 : :
1236 : 0 : static void evdev_disconnect(struct input_handle *handle)
1237 : : {
1238 : 0 : struct evdev *evdev = handle->private;
1239 : :
1240 : 0 : device_del(&evdev->dev);
1241 : 0 : evdev_cleanup(evdev);
1242 : 0 : input_free_minor(MINOR(evdev->dev.devt));
1243 : 0 : input_unregister_handle(handle);
1244 : 0 : put_device(&evdev->dev);
1245 : 0 : }
1246 : :
1247 : : static const struct input_device_id evdev_ids[] = {
1248 : : { .driver_info = 1 }, /* Matches all devices */
1249 : : { }, /* Terminating zero entry */
1250 : : };
1251 : :
1252 : : MODULE_DEVICE_TABLE(input, evdev_ids);
1253 : :
1254 : : static struct input_handler evdev_handler = {
1255 : : .event = evdev_event,
1256 : : .events = evdev_events,
1257 : : .connect = evdev_connect,
1258 : : .disconnect = evdev_disconnect,
1259 : : .legacy_minors = true,
1260 : : .minor = EVDEV_MINOR_BASE,
1261 : : .name = "evdev",
1262 : : .id_table = evdev_ids,
1263 : : };
1264 : :
1265 : 0 : static int __init evdev_init(void)
1266 : : {
1267 : 0 : return input_register_handler(&evdev_handler);
1268 : : }
1269 : :
1270 : 0 : static void __exit evdev_exit(void)
1271 : : {
1272 : 0 : input_unregister_handler(&evdev_handler);
1273 : 0 : }
1274 : :
1275 : : module_init(evdev_init);
1276 : : module_exit(evdev_exit);
1277 : :
1278 : : MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
1279 : : MODULE_DESCRIPTION("Input driver event char devices");
1280 : : MODULE_LICENSE("GPL");
|