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