Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * User level driver support for input subsystem | |
3 | * | |
4 | * Heavily based on evdev.c by Vojtech Pavlik | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation; either version 2 of the License, or | |
9 | * (at your option) any later version. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with this program; if not, write to the Free Software | |
18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
19 | * | |
20 | * Author: Aristeu Sergio Rozanski Filho <aris@cathedrallabs.org> | |
21 | * | |
22 | * Changes/Revisions: | |
e3480a61 BT |
23 | * 0.4 01/09/2014 (Benjamin Tissoires <benjamin.tissoires@redhat.com>) |
24 | * - add UI_GET_SYSNAME ioctl | |
ff462551 AH |
25 | * 0.3 09/04/2006 (Anssi Hannula <anssi.hannula@gmail.com>) |
26 | * - updated ff support for the changes in kernel interface | |
27 | * - added MODULE_VERSION | |
1da177e4 LT |
28 | * 0.2 16/10/2004 (Micah Dowty <micah@navi.cx>) |
29 | * - added force feedback support | |
30 | * - added UI_SET_PHYS | |
31 | * 0.1 20/06/2002 | |
32 | * - first public version | |
33 | */ | |
a11bc476 | 34 | #include <uapi/linux/uinput.h> |
1da177e4 | 35 | #include <linux/poll.h> |
a99bbaf5 | 36 | #include <linux/sched.h> |
1da177e4 LT |
37 | #include <linux/slab.h> |
38 | #include <linux/module.h> | |
39 | #include <linux/init.h> | |
1da177e4 LT |
40 | #include <linux/fs.h> |
41 | #include <linux/miscdevice.h> | |
47c78e89 | 42 | #include <linux/input/mt.h> |
2d56f3a3 | 43 | #include "../input-compat.h" |
1da177e4 | 44 | |
a11bc476 DT |
45 | #define UINPUT_NAME "uinput" |
46 | #define UINPUT_BUFFER_SIZE 16 | |
47 | #define UINPUT_NUM_REQUESTS 16 | |
48 | ||
49 | enum uinput_state { UIST_NEW_DEVICE, UIST_SETUP_COMPLETE, UIST_CREATED }; | |
50 | ||
51 | struct uinput_request { | |
52 | unsigned int id; | |
53 | unsigned int code; /* UI_FF_UPLOAD, UI_FF_ERASE */ | |
54 | ||
55 | int retval; | |
56 | struct completion done; | |
57 | ||
58 | union { | |
59 | unsigned int effect_id; | |
60 | struct { | |
61 | struct ff_effect *effect; | |
62 | struct ff_effect *old; | |
63 | } upload; | |
64 | } u; | |
65 | }; | |
66 | ||
67 | struct uinput_device { | |
68 | struct input_dev *dev; | |
69 | struct mutex mutex; | |
70 | enum uinput_state state; | |
71 | wait_queue_head_t waitq; | |
72 | unsigned char ready; | |
73 | unsigned char head; | |
74 | unsigned char tail; | |
75 | struct input_event buff[UINPUT_BUFFER_SIZE]; | |
76 | unsigned int ff_effects_max; | |
77 | ||
78 | struct uinput_request *requests[UINPUT_NUM_REQUESTS]; | |
79 | wait_queue_head_t requests_waitq; | |
80 | spinlock_t requests_lock; | |
81 | }; | |
82 | ||
54ce165e DT |
83 | static int uinput_dev_event(struct input_dev *dev, |
84 | unsigned int type, unsigned int code, int value) | |
1da177e4 | 85 | { |
373f9713 | 86 | struct uinput_device *udev = input_get_drvdata(dev); |
1da177e4 LT |
87 | |
88 | udev->buff[udev->head].type = type; | |
89 | udev->buff[udev->head].code = code; | |
90 | udev->buff[udev->head].value = value; | |
91 | do_gettimeofday(&udev->buff[udev->head].time); | |
92 | udev->head = (udev->head + 1) % UINPUT_BUFFER_SIZE; | |
93 | ||
94 | wake_up_interruptible(&udev->waitq); | |
95 | ||
96 | return 0; | |
97 | } | |
98 | ||
05cebd38 | 99 | /* Atomically allocate an ID for the given request. Returns 0 on success. */ |
00ce756c DT |
100 | static bool uinput_request_alloc_id(struct uinput_device *udev, |
101 | struct uinput_request *request) | |
1da177e4 | 102 | { |
c5b3533a | 103 | unsigned int id; |
00ce756c | 104 | bool reserved = false; |
1da177e4 | 105 | |
0048e603 | 106 | spin_lock(&udev->requests_lock); |
152c12f5 | 107 | |
05cebd38 | 108 | for (id = 0; id < UINPUT_NUM_REQUESTS; id++) { |
1da177e4 | 109 | if (!udev->requests[id]) { |
1da177e4 | 110 | request->id = id; |
0048e603 | 111 | udev->requests[id] = request; |
00ce756c | 112 | reserved = true; |
152c12f5 | 113 | break; |
1da177e4 | 114 | } |
05cebd38 | 115 | } |
152c12f5 | 116 | |
0048e603 | 117 | spin_unlock(&udev->requests_lock); |
00ce756c | 118 | return reserved; |
1da177e4 LT |
119 | } |
120 | ||
c5b3533a DT |
121 | static struct uinput_request *uinput_request_find(struct uinput_device *udev, |
122 | unsigned int id) | |
1da177e4 LT |
123 | { |
124 | /* Find an input request, by ID. Returns NULL if the ID isn't valid. */ | |
c5b3533a | 125 | if (id >= UINPUT_NUM_REQUESTS) |
1da177e4 | 126 | return NULL; |
2d56f3a3 | 127 | |
1da177e4 LT |
128 | return udev->requests[id]; |
129 | } | |
130 | ||
00ce756c DT |
131 | static int uinput_request_reserve_slot(struct uinput_device *udev, |
132 | struct uinput_request *request) | |
1da177e4 | 133 | { |
0048e603 DT |
134 | /* Allocate slot. If none are available right away, wait. */ |
135 | return wait_event_interruptible(udev->requests_waitq, | |
00ce756c | 136 | uinput_request_alloc_id(udev, request)); |
0048e603 | 137 | } |
1da177e4 | 138 | |
6b4877c7 DT |
139 | static void uinput_request_release_slot(struct uinput_device *udev, |
140 | unsigned int id) | |
0048e603 | 141 | { |
0048e603 | 142 | /* Mark slot as available */ |
6b4877c7 DT |
143 | spin_lock(&udev->requests_lock); |
144 | udev->requests[id] = NULL; | |
145 | spin_unlock(&udev->requests_lock); | |
e7507ed9 | 146 | |
6b4877c7 | 147 | wake_up(&udev->requests_waitq); |
1da177e4 LT |
148 | } |
149 | ||
00ce756c DT |
150 | static int uinput_request_send(struct uinput_device *udev, |
151 | struct uinput_request *request) | |
1da177e4 | 152 | { |
05cebd38 ASRF |
153 | int retval; |
154 | ||
05cebd38 ASRF |
155 | retval = mutex_lock_interruptible(&udev->mutex); |
156 | if (retval) | |
157 | return retval; | |
158 | ||
159 | if (udev->state != UIST_CREATED) { | |
160 | retval = -ENODEV; | |
161 | goto out; | |
162 | } | |
163 | ||
00ce756c DT |
164 | init_completion(&request->done); |
165 | ||
166 | /* | |
167 | * Tell our userspace application about this new request | |
168 | * by queueing an input event. | |
169 | */ | |
05cebd38 ASRF |
170 | uinput_dev_event(udev->dev, EV_UINPUT, request->code, request->id); |
171 | ||
172 | out: | |
173 | mutex_unlock(&udev->mutex); | |
174 | return retval; | |
175 | } | |
176 | ||
00ce756c DT |
177 | static int uinput_request_submit(struct uinput_device *udev, |
178 | struct uinput_request *request) | |
179 | { | |
6b4877c7 | 180 | int retval; |
00ce756c | 181 | |
6b4877c7 DT |
182 | retval = uinput_request_reserve_slot(udev, request); |
183 | if (retval) | |
184 | return retval; | |
00ce756c | 185 | |
6b4877c7 DT |
186 | retval = uinput_request_send(udev, request); |
187 | if (retval) | |
188 | goto out; | |
00ce756c | 189 | |
8e009118 DT |
190 | if (!wait_for_completion_timeout(&request->done, 30 * HZ)) { |
191 | retval = -ETIMEDOUT; | |
192 | goto out; | |
193 | } | |
194 | ||
6b4877c7 DT |
195 | retval = request->retval; |
196 | ||
197 | out: | |
198 | uinput_request_release_slot(udev, request->id); | |
199 | return retval; | |
00ce756c DT |
200 | } |
201 | ||
05cebd38 | 202 | /* |
54ce165e | 203 | * Fail all outstanding requests so handlers don't wait for the userspace |
05cebd38 ASRF |
204 | * to finish processing them. |
205 | */ | |
206 | static void uinput_flush_requests(struct uinput_device *udev) | |
207 | { | |
208 | struct uinput_request *request; | |
209 | int i; | |
1da177e4 | 210 | |
05cebd38 ASRF |
211 | spin_lock(&udev->requests_lock); |
212 | ||
213 | for (i = 0; i < UINPUT_NUM_REQUESTS; i++) { | |
214 | request = udev->requests[i]; | |
215 | if (request) { | |
216 | request->retval = -ENODEV; | |
6b4877c7 | 217 | complete(&request->done); |
05cebd38 ASRF |
218 | } |
219 | } | |
220 | ||
221 | spin_unlock(&udev->requests_lock); | |
1da177e4 LT |
222 | } |
223 | ||
ff462551 AH |
224 | static void uinput_dev_set_gain(struct input_dev *dev, u16 gain) |
225 | { | |
226 | uinput_dev_event(dev, EV_FF, FF_GAIN, gain); | |
227 | } | |
228 | ||
229 | static void uinput_dev_set_autocenter(struct input_dev *dev, u16 magnitude) | |
230 | { | |
231 | uinput_dev_event(dev, EV_FF, FF_AUTOCENTER, magnitude); | |
232 | } | |
233 | ||
234 | static int uinput_dev_playback(struct input_dev *dev, int effect_id, int value) | |
235 | { | |
236 | return uinput_dev_event(dev, EV_FF, effect_id, value); | |
237 | } | |
238 | ||
54ce165e DT |
239 | static int uinput_dev_upload_effect(struct input_dev *dev, |
240 | struct ff_effect *effect, | |
241 | struct ff_effect *old) | |
1da177e4 | 242 | { |
05cebd38 | 243 | struct uinput_device *udev = input_get_drvdata(dev); |
1da177e4 LT |
244 | struct uinput_request request; |
245 | ||
2d56f3a3 PL |
246 | /* |
247 | * uinput driver does not currently support periodic effects with | |
248 | * custom waveform since it does not have a way to pass buffer of | |
249 | * samples (custom_data) to userspace. If ever there is a device | |
250 | * supporting custom waveforms we would need to define an additional | |
251 | * ioctl (UI_UPLOAD_SAMPLES) but for now we just bail out. | |
252 | */ | |
253 | if (effect->type == FF_PERIODIC && | |
254 | effect->u.periodic.waveform == FF_CUSTOM) | |
255 | return -EINVAL; | |
256 | ||
0048e603 | 257 | request.code = UI_FF_UPLOAD; |
ff462551 AH |
258 | request.u.upload.effect = effect; |
259 | request.u.upload.old = old; | |
0048e603 | 260 | |
00ce756c | 261 | return uinput_request_submit(udev, &request); |
1da177e4 LT |
262 | } |
263 | ||
264 | static int uinput_dev_erase_effect(struct input_dev *dev, int effect_id) | |
265 | { | |
05cebd38 | 266 | struct uinput_device *udev = input_get_drvdata(dev); |
1da177e4 LT |
267 | struct uinput_request request; |
268 | ||
269 | if (!test_bit(EV_FF, dev->evbit)) | |
270 | return -ENOSYS; | |
271 | ||
0048e603 | 272 | request.code = UI_FF_ERASE; |
1da177e4 | 273 | request.u.effect_id = effect_id; |
0048e603 | 274 | |
00ce756c | 275 | return uinput_request_submit(udev, &request); |
1da177e4 LT |
276 | } |
277 | ||
e8b95728 DT |
278 | static int uinput_dev_flush(struct input_dev *dev, struct file *file) |
279 | { | |
280 | /* | |
281 | * If we are called with file == NULL that means we are tearing | |
282 | * down the device, and therefore we can not handle FF erase | |
283 | * requests: either we are handling UI_DEV_DESTROY (and holding | |
284 | * the udev->mutex), or the file descriptor is closed and there is | |
285 | * nobody on the other side anymore. | |
286 | */ | |
287 | return file ? input_ff_flush(dev, file) : 0; | |
288 | } | |
289 | ||
29506415 | 290 | static void uinput_destroy_device(struct uinput_device *udev) |
1da177e4 | 291 | { |
29506415 | 292 | const char *name, *phys; |
05cebd38 ASRF |
293 | struct input_dev *dev = udev->dev; |
294 | enum uinput_state old_state = udev->state; | |
295 | ||
296 | udev->state = UIST_NEW_DEVICE; | |
29506415 | 297 | |
05cebd38 ASRF |
298 | if (dev) { |
299 | name = dev->name; | |
300 | phys = dev->phys; | |
301 | if (old_state == UIST_CREATED) { | |
302 | uinput_flush_requests(udev); | |
303 | input_unregister_device(dev); | |
304 | } else { | |
305 | input_free_device(dev); | |
306 | } | |
29506415 DT |
307 | kfree(name); |
308 | kfree(phys); | |
309 | udev->dev = NULL; | |
1da177e4 | 310 | } |
1da177e4 LT |
311 | } |
312 | ||
29506415 | 313 | static int uinput_create_device(struct uinput_device *udev) |
1da177e4 | 314 | { |
ff462551 | 315 | struct input_dev *dev = udev->dev; |
fbae10db | 316 | int error, nslot; |
29506415 DT |
317 | |
318 | if (udev->state != UIST_SETUP_COMPLETE) { | |
319 | printk(KERN_DEBUG "%s: write device info first\n", UINPUT_NAME); | |
1da177e4 LT |
320 | return -EINVAL; |
321 | } | |
322 | ||
601bbbe0 DT |
323 | if (test_bit(EV_ABS, dev->evbit)) { |
324 | input_alloc_absinfo(dev); | |
325 | if (!dev->absinfo) { | |
326 | error = -EINVAL; | |
fbae10db | 327 | goto fail1; |
601bbbe0 DT |
328 | } |
329 | ||
330 | if (test_bit(ABS_MT_SLOT, dev->absbit)) { | |
331 | nslot = input_abs_get_max(dev, ABS_MT_SLOT) + 1; | |
332 | error = input_mt_init_slots(dev, nslot, 0); | |
333 | if (error) | |
334 | goto fail1; | |
335 | } else if (test_bit(ABS_MT_POSITION_X, dev->absbit)) { | |
336 | input_set_events_per_packet(dev, 60); | |
337 | } | |
fbae10db DH |
338 | } |
339 | ||
daf6cd0c EV |
340 | if (test_bit(EV_FF, dev->evbit) && !udev->ff_effects_max) { |
341 | printk(KERN_DEBUG "%s: ff_effects_max should be non-zero when FF_BIT is set\n", | |
342 | UINPUT_NAME); | |
343 | error = -EINVAL; | |
344 | goto fail1; | |
345 | } | |
346 | ||
ff462551 AH |
347 | if (udev->ff_effects_max) { |
348 | error = input_ff_create(dev, udev->ff_effects_max); | |
349 | if (error) | |
350 | goto fail1; | |
351 | ||
352 | dev->ff->upload = uinput_dev_upload_effect; | |
353 | dev->ff->erase = uinput_dev_erase_effect; | |
354 | dev->ff->playback = uinput_dev_playback; | |
355 | dev->ff->set_gain = uinput_dev_set_gain; | |
356 | dev->ff->set_autocenter = uinput_dev_set_autocenter; | |
e8b95728 DT |
357 | /* |
358 | * The standard input_ff_flush() implementation does | |
359 | * not quite work for uinput as we can't reasonably | |
360 | * handle FF requests during device teardown. | |
361 | */ | |
362 | dev->flush = uinput_dev_flush; | |
29506415 | 363 | } |
1da177e4 | 364 | |
04ce40a6 DT |
365 | dev->event = uinput_dev_event; |
366 | ||
367 | input_set_drvdata(udev->dev, udev); | |
368 | ||
ff462551 AH |
369 | error = input_register_device(udev->dev); |
370 | if (error) | |
371 | goto fail2; | |
372 | ||
29506415 | 373 | udev->state = UIST_CREATED; |
1da177e4 LT |
374 | |
375 | return 0; | |
ff462551 AH |
376 | |
377 | fail2: input_ff_destroy(dev); | |
378 | fail1: uinput_destroy_device(udev); | |
379 | return error; | |
1da177e4 LT |
380 | } |
381 | ||
382 | static int uinput_open(struct inode *inode, struct file *file) | |
383 | { | |
29506415 | 384 | struct uinput_device *newdev; |
1da177e4 | 385 | |
29506415 | 386 | newdev = kzalloc(sizeof(struct uinput_device), GFP_KERNEL); |
1da177e4 | 387 | if (!newdev) |
29506415 DT |
388 | return -ENOMEM; |
389 | ||
221979aa | 390 | mutex_init(&newdev->mutex); |
0048e603 | 391 | spin_lock_init(&newdev->requests_lock); |
1da177e4 | 392 | init_waitqueue_head(&newdev->requests_waitq); |
29506415 DT |
393 | init_waitqueue_head(&newdev->waitq); |
394 | newdev->state = UIST_NEW_DEVICE; | |
1da177e4 LT |
395 | |
396 | file->private_data = newdev; | |
daf8a96b | 397 | nonseekable_open(inode, file); |
1da177e4 LT |
398 | |
399 | return 0; | |
1da177e4 LT |
400 | } |
401 | ||
fbae10db DH |
402 | static int uinput_validate_absinfo(struct input_dev *dev, unsigned int code, |
403 | const struct input_absinfo *abs) | |
404 | { | |
405 | int min, max; | |
406 | ||
407 | min = abs->minimum; | |
408 | max = abs->maximum; | |
409 | ||
410 | if ((min != 0 || max != 0) && max <= min) { | |
411 | printk(KERN_DEBUG | |
412 | "%s: invalid abs[%02x] min:%d max:%d\n", | |
413 | UINPUT_NAME, code, min, max); | |
414 | return -EINVAL; | |
415 | } | |
416 | ||
417 | if (abs->flat > max - min) { | |
418 | printk(KERN_DEBUG | |
419 | "%s: abs_flat #%02x out of range: %d (min:%d/max:%d)\n", | |
420 | UINPUT_NAME, code, abs->flat, min, max); | |
421 | return -EINVAL; | |
422 | } | |
423 | ||
424 | return 0; | |
425 | } | |
426 | ||
1da177e4 LT |
427 | static int uinput_validate_absbits(struct input_dev *dev) |
428 | { | |
429 | unsigned int cnt; | |
fbae10db | 430 | int error; |
bcb898e5 DH |
431 | |
432 | if (!test_bit(EV_ABS, dev->evbit)) | |
433 | return 0; | |
434 | ||
435 | /* | |
436 | * Check if absmin/absmax/absfuzz/absflat are sane. | |
437 | */ | |
1da177e4 | 438 | |
b6d30968 | 439 | for_each_set_bit(cnt, dev->absbit, ABS_CNT) { |
fbae10db | 440 | if (!dev->absinfo) |
bcb898e5 | 441 | return -EINVAL; |
bcb898e5 | 442 | |
fbae10db DH |
443 | error = uinput_validate_absinfo(dev, cnt, &dev->absinfo[cnt]); |
444 | if (error) | |
445 | return error; | |
bcb898e5 DH |
446 | } |
447 | ||
448 | return 0; | |
1da177e4 LT |
449 | } |
450 | ||
052876f8 BT |
451 | static int uinput_dev_setup(struct uinput_device *udev, |
452 | struct uinput_setup __user *arg) | |
453 | { | |
454 | struct uinput_setup setup; | |
455 | struct input_dev *dev; | |
052876f8 BT |
456 | |
457 | if (udev->state == UIST_CREATED) | |
458 | return -EINVAL; | |
459 | ||
460 | if (copy_from_user(&setup, arg, sizeof(setup))) | |
461 | return -EFAULT; | |
462 | ||
463 | if (!setup.name[0]) | |
464 | return -EINVAL; | |
465 | ||
466 | dev = udev->dev; | |
467 | dev->id = setup.id; | |
468 | udev->ff_effects_max = setup.ff_effects_max; | |
469 | ||
470 | kfree(dev->name); | |
471 | dev->name = kstrndup(setup.name, UINPUT_MAX_NAME_SIZE, GFP_KERNEL); | |
472 | if (!dev->name) | |
473 | return -ENOMEM; | |
474 | ||
052876f8 BT |
475 | udev->state = UIST_SETUP_COMPLETE; |
476 | return 0; | |
477 | } | |
478 | ||
479 | static int uinput_abs_setup(struct uinput_device *udev, | |
480 | struct uinput_setup __user *arg, size_t size) | |
481 | { | |
482 | struct uinput_abs_setup setup = {}; | |
483 | struct input_dev *dev; | |
fbae10db | 484 | int error; |
052876f8 BT |
485 | |
486 | if (size > sizeof(setup)) | |
487 | return -E2BIG; | |
488 | ||
489 | if (udev->state == UIST_CREATED) | |
490 | return -EINVAL; | |
491 | ||
492 | if (copy_from_user(&setup, arg, size)) | |
493 | return -EFAULT; | |
494 | ||
495 | if (setup.code > ABS_MAX) | |
496 | return -ERANGE; | |
497 | ||
498 | dev = udev->dev; | |
499 | ||
fbae10db DH |
500 | error = uinput_validate_absinfo(dev, setup.code, &setup.absinfo); |
501 | if (error) | |
502 | return error; | |
503 | ||
052876f8 BT |
504 | input_alloc_absinfo(dev); |
505 | if (!dev->absinfo) | |
506 | return -ENOMEM; | |
507 | ||
508 | set_bit(setup.code, dev->absbit); | |
509 | dev->absinfo[setup.code] = setup.absinfo; | |
052876f8 BT |
510 | return 0; |
511 | } | |
512 | ||
513 | /* legacy setup via write() */ | |
514 | static int uinput_setup_device_legacy(struct uinput_device *udev, | |
515 | const char __user *buffer, size_t count) | |
1da177e4 LT |
516 | { |
517 | struct uinput_user_dev *user_dev; | |
518 | struct input_dev *dev; | |
5d9d6e91 | 519 | int i; |
152c12f5 | 520 | int retval; |
1da177e4 | 521 | |
29506415 DT |
522 | if (count != sizeof(struct uinput_user_dev)) |
523 | return -EINVAL; | |
524 | ||
525 | if (!udev->dev) { | |
04ce40a6 DT |
526 | udev->dev = input_allocate_device(); |
527 | if (!udev->dev) | |
528 | return -ENOMEM; | |
29506415 | 529 | } |
1da177e4 | 530 | |
1da177e4 LT |
531 | dev = udev->dev; |
532 | ||
4dfcc271 | 533 | user_dev = memdup_user(buffer, sizeof(struct uinput_user_dev)); |
163d2770 | 534 | if (IS_ERR(user_dev)) |
4dfcc271 | 535 | return PTR_ERR(user_dev); |
1da177e4 | 536 | |
ff462551 AH |
537 | udev->ff_effects_max = user_dev->ff_effects_max; |
538 | ||
5d9d6e91 DH |
539 | /* Ensure name is filled in */ |
540 | if (!user_dev->name[0]) { | |
29506415 DT |
541 | retval = -EINVAL; |
542 | goto exit; | |
543 | } | |
544 | ||
545 | kfree(dev->name); | |
5d9d6e91 DH |
546 | dev->name = kstrndup(user_dev->name, UINPUT_MAX_NAME_SIZE, |
547 | GFP_KERNEL); | |
548 | if (!dev->name) { | |
1da177e4 LT |
549 | retval = -ENOMEM; |
550 | goto exit; | |
551 | } | |
552 | ||
1da177e4 LT |
553 | dev->id.bustype = user_dev->id.bustype; |
554 | dev->id.vendor = user_dev->id.vendor; | |
555 | dev->id.product = user_dev->id.product; | |
556 | dev->id.version = user_dev->id.version; | |
1da177e4 | 557 | |
72d47362 | 558 | for (i = 0; i < ABS_CNT; i++) { |
987a6c02 DM |
559 | input_abs_set_max(dev, i, user_dev->absmax[i]); |
560 | input_abs_set_min(dev, i, user_dev->absmin[i]); | |
561 | input_abs_set_fuzz(dev, i, user_dev->absfuzz[i]); | |
562 | input_abs_set_flat(dev, i, user_dev->absflat[i]); | |
563 | } | |
1da177e4 | 564 | |
bcb898e5 DH |
565 | retval = uinput_validate_absbits(dev); |
566 | if (retval < 0) | |
567 | goto exit; | |
1da177e4 | 568 | |
29506415 DT |
569 | udev->state = UIST_SETUP_COMPLETE; |
570 | retval = count; | |
571 | ||
572 | exit: | |
1da177e4 LT |
573 | kfree(user_dev); |
574 | return retval; | |
575 | } | |
576 | ||
cbf05413 RM |
577 | static ssize_t uinput_inject_events(struct uinput_device *udev, |
578 | const char __user *buffer, size_t count) | |
29506415 DT |
579 | { |
580 | struct input_event ev; | |
cbf05413 | 581 | size_t bytes = 0; |
29506415 | 582 | |
cbf05413 | 583 | if (count != 0 && count < input_event_size()) |
29506415 DT |
584 | return -EINVAL; |
585 | ||
cbf05413 RM |
586 | while (bytes + input_event_size() <= count) { |
587 | /* | |
588 | * Note that even if some events were fetched successfully | |
589 | * we are still going to return EFAULT instead of partial | |
590 | * count to let userspace know that it got it's buffers | |
591 | * all wrong. | |
592 | */ | |
593 | if (input_event_from_user(buffer + bytes, &ev)) | |
594 | return -EFAULT; | |
29506415 | 595 | |
cbf05413 RM |
596 | input_event(udev->dev, ev.type, ev.code, ev.value); |
597 | bytes += input_event_size(); | |
598 | } | |
29506415 | 599 | |
cbf05413 | 600 | return bytes; |
29506415 DT |
601 | } |
602 | ||
54ce165e DT |
603 | static ssize_t uinput_write(struct file *file, const char __user *buffer, |
604 | size_t count, loff_t *ppos) | |
1da177e4 LT |
605 | { |
606 | struct uinput_device *udev = file->private_data; | |
29506415 | 607 | int retval; |
1da177e4 | 608 | |
22ae19c6 DT |
609 | if (count == 0) |
610 | return 0; | |
611 | ||
221979aa | 612 | retval = mutex_lock_interruptible(&udev->mutex); |
29506415 DT |
613 | if (retval) |
614 | return retval; | |
615 | ||
616 | retval = udev->state == UIST_CREATED ? | |
cbf05413 | 617 | uinput_inject_events(udev, buffer, count) : |
052876f8 | 618 | uinput_setup_device_legacy(udev, buffer, count); |
1da177e4 | 619 | |
221979aa | 620 | mutex_unlock(&udev->mutex); |
1da177e4 | 621 | |
29506415 | 622 | return retval; |
1da177e4 LT |
623 | } |
624 | ||
929d1af5 DT |
625 | static bool uinput_fetch_next_event(struct uinput_device *udev, |
626 | struct input_event *event) | |
627 | { | |
628 | bool have_event; | |
629 | ||
630 | spin_lock_irq(&udev->dev->event_lock); | |
631 | ||
632 | have_event = udev->head != udev->tail; | |
633 | if (have_event) { | |
634 | *event = udev->buff[udev->tail]; | |
635 | udev->tail = (udev->tail + 1) % UINPUT_BUFFER_SIZE; | |
636 | } | |
637 | ||
638 | spin_unlock_irq(&udev->dev->event_lock); | |
639 | ||
640 | return have_event; | |
641 | } | |
642 | ||
22ae19c6 DT |
643 | static ssize_t uinput_events_to_user(struct uinput_device *udev, |
644 | char __user *buffer, size_t count) | |
1da177e4 | 645 | { |
929d1af5 | 646 | struct input_event event; |
22ae19c6 | 647 | size_t read = 0; |
1da177e4 | 648 | |
22ae19c6 DT |
649 | while (read + input_event_size() <= count && |
650 | uinput_fetch_next_event(udev, &event)) { | |
f40033ac | 651 | |
00ce756c DT |
652 | if (input_event_to_user(buffer + read, &event)) |
653 | return -EFAULT; | |
1da177e4 | 654 | |
22ae19c6 DT |
655 | read += input_event_size(); |
656 | } | |
1da177e4 | 657 | |
00ce756c | 658 | return read; |
22ae19c6 | 659 | } |
1da177e4 | 660 | |
22ae19c6 DT |
661 | static ssize_t uinput_read(struct file *file, char __user *buffer, |
662 | size_t count, loff_t *ppos) | |
663 | { | |
664 | struct uinput_device *udev = file->private_data; | |
665 | ssize_t retval; | |
29506415 | 666 | |
22ae19c6 DT |
667 | if (count != 0 && count < input_event_size()) |
668 | return -EINVAL; | |
1da177e4 | 669 | |
22ae19c6 DT |
670 | do { |
671 | retval = mutex_lock_interruptible(&udev->mutex); | |
672 | if (retval) | |
673 | return retval; | |
929d1af5 | 674 | |
22ae19c6 DT |
675 | if (udev->state != UIST_CREATED) |
676 | retval = -ENODEV; | |
677 | else if (udev->head == udev->tail && | |
678 | (file->f_flags & O_NONBLOCK)) | |
679 | retval = -EAGAIN; | |
680 | else | |
681 | retval = uinput_events_to_user(udev, buffer, count); | |
929d1af5 | 682 | |
22ae19c6 | 683 | mutex_unlock(&udev->mutex); |
1da177e4 | 684 | |
22ae19c6 DT |
685 | if (retval || count == 0) |
686 | break; | |
687 | ||
688 | if (!(file->f_flags & O_NONBLOCK)) | |
689 | retval = wait_event_interruptible(udev->waitq, | |
690 | udev->head != udev->tail || | |
691 | udev->state != UIST_CREATED); | |
692 | } while (retval == 0); | |
29506415 | 693 | |
1da177e4 LT |
694 | return retval; |
695 | } | |
696 | ||
afc9a42b | 697 | static __poll_t uinput_poll(struct file *file, poll_table *wait) |
1da177e4 LT |
698 | { |
699 | struct uinput_device *udev = file->private_data; | |
700 | ||
701 | poll_wait(file, &udev->waitq, wait); | |
702 | ||
703 | if (udev->head != udev->tail) | |
704 | return POLLIN | POLLRDNORM; | |
705 | ||
706 | return 0; | |
707 | } | |
708 | ||
29506415 | 709 | static int uinput_release(struct inode *inode, struct file *file) |
1da177e4 | 710 | { |
29506415 | 711 | struct uinput_device *udev = file->private_data; |
1da177e4 | 712 | |
29506415 | 713 | uinput_destroy_device(udev); |
1da177e4 LT |
714 | kfree(udev); |
715 | ||
716 | return 0; | |
717 | } | |
718 | ||
2d56f3a3 PL |
719 | #ifdef CONFIG_COMPAT |
720 | struct uinput_ff_upload_compat { | |
c5b3533a DT |
721 | __u32 request_id; |
722 | __s32 retval; | |
2d56f3a3 PL |
723 | struct ff_effect_compat effect; |
724 | struct ff_effect_compat old; | |
725 | }; | |
726 | ||
727 | static int uinput_ff_upload_to_user(char __user *buffer, | |
728 | const struct uinput_ff_upload *ff_up) | |
729 | { | |
b8b4ead1 | 730 | if (in_compat_syscall()) { |
2d56f3a3 PL |
731 | struct uinput_ff_upload_compat ff_up_compat; |
732 | ||
733 | ff_up_compat.request_id = ff_up->request_id; | |
734 | ff_up_compat.retval = ff_up->retval; | |
735 | /* | |
736 | * It so happens that the pointer that gives us the trouble | |
737 | * is the last field in the structure. Since we don't support | |
738 | * custom waveforms in uinput anyway we can just copy the whole | |
739 | * thing (to the compat size) and ignore the pointer. | |
740 | */ | |
741 | memcpy(&ff_up_compat.effect, &ff_up->effect, | |
742 | sizeof(struct ff_effect_compat)); | |
743 | memcpy(&ff_up_compat.old, &ff_up->old, | |
744 | sizeof(struct ff_effect_compat)); | |
745 | ||
746 | if (copy_to_user(buffer, &ff_up_compat, | |
747 | sizeof(struct uinput_ff_upload_compat))) | |
748 | return -EFAULT; | |
749 | } else { | |
750 | if (copy_to_user(buffer, ff_up, | |
751 | sizeof(struct uinput_ff_upload))) | |
752 | return -EFAULT; | |
753 | } | |
754 | ||
755 | return 0; | |
756 | } | |
757 | ||
758 | static int uinput_ff_upload_from_user(const char __user *buffer, | |
759 | struct uinput_ff_upload *ff_up) | |
760 | { | |
b8b4ead1 | 761 | if (in_compat_syscall()) { |
2d56f3a3 PL |
762 | struct uinput_ff_upload_compat ff_up_compat; |
763 | ||
764 | if (copy_from_user(&ff_up_compat, buffer, | |
765 | sizeof(struct uinput_ff_upload_compat))) | |
766 | return -EFAULT; | |
767 | ||
768 | ff_up->request_id = ff_up_compat.request_id; | |
769 | ff_up->retval = ff_up_compat.retval; | |
770 | memcpy(&ff_up->effect, &ff_up_compat.effect, | |
771 | sizeof(struct ff_effect_compat)); | |
772 | memcpy(&ff_up->old, &ff_up_compat.old, | |
773 | sizeof(struct ff_effect_compat)); | |
774 | ||
775 | } else { | |
776 | if (copy_from_user(ff_up, buffer, | |
777 | sizeof(struct uinput_ff_upload))) | |
778 | return -EFAULT; | |
779 | } | |
780 | ||
781 | return 0; | |
782 | } | |
783 | ||
784 | #else | |
785 | ||
786 | static int uinput_ff_upload_to_user(char __user *buffer, | |
787 | const struct uinput_ff_upload *ff_up) | |
788 | { | |
789 | if (copy_to_user(buffer, ff_up, sizeof(struct uinput_ff_upload))) | |
790 | return -EFAULT; | |
791 | ||
792 | return 0; | |
793 | } | |
794 | ||
795 | static int uinput_ff_upload_from_user(const char __user *buffer, | |
796 | struct uinput_ff_upload *ff_up) | |
797 | { | |
798 | if (copy_from_user(ff_up, buffer, sizeof(struct uinput_ff_upload))) | |
799 | return -EFAULT; | |
800 | ||
801 | return 0; | |
802 | } | |
803 | ||
804 | #endif | |
805 | ||
29506415 DT |
806 | #define uinput_set_bit(_arg, _bit, _max) \ |
807 | ({ \ | |
808 | int __ret = 0; \ | |
809 | if (udev->state == UIST_CREATED) \ | |
810 | __ret = -EINVAL; \ | |
811 | else if ((_arg) > (_max)) \ | |
812 | __ret = -EINVAL; \ | |
813 | else set_bit((_arg), udev->dev->_bit); \ | |
814 | __ret; \ | |
815 | }) | |
816 | ||
e3480a61 BT |
817 | static int uinput_str_to_user(void __user *dest, const char *str, |
818 | unsigned int maxlen) | |
819 | { | |
820 | char __user *p = dest; | |
821 | int len, ret; | |
822 | ||
823 | if (!str) | |
824 | return -ENOENT; | |
825 | ||
826 | if (maxlen == 0) | |
827 | return -EINVAL; | |
828 | ||
829 | len = strlen(str) + 1; | |
830 | if (len > maxlen) | |
831 | len = maxlen; | |
832 | ||
833 | ret = copy_to_user(p, str, len); | |
834 | if (ret) | |
835 | return -EFAULT; | |
836 | ||
837 | /* force terminating '\0' */ | |
838 | ret = put_user(0, p + len - 1); | |
839 | return ret ? -EFAULT : len; | |
840 | } | |
841 | ||
2d56f3a3 PL |
842 | static long uinput_ioctl_handler(struct file *file, unsigned int cmd, |
843 | unsigned long arg, void __user *p) | |
1da177e4 | 844 | { |
29506415 | 845 | int retval; |
2d56f3a3 | 846 | struct uinput_device *udev = file->private_data; |
1da177e4 LT |
847 | struct uinput_ff_upload ff_up; |
848 | struct uinput_ff_erase ff_erase; | |
849 | struct uinput_request *req; | |
5b6271bd | 850 | char *phys; |
e3480a61 BT |
851 | const char *name; |
852 | unsigned int size; | |
1da177e4 | 853 | |
221979aa | 854 | retval = mutex_lock_interruptible(&udev->mutex); |
29506415 DT |
855 | if (retval) |
856 | return retval; | |
857 | ||
858 | if (!udev->dev) { | |
04ce40a6 | 859 | udev->dev = input_allocate_device(); |
781f2dd0 DC |
860 | if (!udev->dev) { |
861 | retval = -ENOMEM; | |
862 | goto out; | |
863 | } | |
1da177e4 LT |
864 | } |
865 | ||
866 | switch (cmd) { | |
c0661652 DT |
867 | case UI_GET_VERSION: |
868 | if (put_user(UINPUT_VERSION, (unsigned int __user *)p)) | |
869 | retval = -EFAULT; | |
870 | goto out; | |
ba4e9a61 | 871 | |
c0661652 DT |
872 | case UI_DEV_CREATE: |
873 | retval = uinput_create_device(udev); | |
874 | goto out; | |
1da177e4 | 875 | |
c0661652 DT |
876 | case UI_DEV_DESTROY: |
877 | uinput_destroy_device(udev); | |
878 | goto out; | |
1da177e4 | 879 | |
c0661652 DT |
880 | case UI_DEV_SETUP: |
881 | retval = uinput_dev_setup(udev, p); | |
882 | goto out; | |
052876f8 | 883 | |
c0661652 | 884 | /* UI_ABS_SETUP is handled in the variable size ioctls */ |
052876f8 | 885 | |
c0661652 DT |
886 | case UI_SET_EVBIT: |
887 | retval = uinput_set_bit(arg, evbit, EV_MAX); | |
888 | goto out; | |
1da177e4 | 889 | |
c0661652 DT |
890 | case UI_SET_KEYBIT: |
891 | retval = uinput_set_bit(arg, keybit, KEY_MAX); | |
892 | goto out; | |
1da177e4 | 893 | |
c0661652 DT |
894 | case UI_SET_RELBIT: |
895 | retval = uinput_set_bit(arg, relbit, REL_MAX); | |
896 | goto out; | |
1da177e4 | 897 | |
c0661652 DT |
898 | case UI_SET_ABSBIT: |
899 | retval = uinput_set_bit(arg, absbit, ABS_MAX); | |
900 | goto out; | |
1da177e4 | 901 | |
c0661652 DT |
902 | case UI_SET_MSCBIT: |
903 | retval = uinput_set_bit(arg, mscbit, MSC_MAX); | |
904 | goto out; | |
1da177e4 | 905 | |
c0661652 DT |
906 | case UI_SET_LEDBIT: |
907 | retval = uinput_set_bit(arg, ledbit, LED_MAX); | |
908 | goto out; | |
909 | ||
910 | case UI_SET_SNDBIT: | |
911 | retval = uinput_set_bit(arg, sndbit, SND_MAX); | |
912 | goto out; | |
913 | ||
914 | case UI_SET_FFBIT: | |
915 | retval = uinput_set_bit(arg, ffbit, FF_MAX); | |
916 | goto out; | |
917 | ||
918 | case UI_SET_SWBIT: | |
919 | retval = uinput_set_bit(arg, swbit, SW_MAX); | |
920 | goto out; | |
921 | ||
922 | case UI_SET_PROPBIT: | |
923 | retval = uinput_set_bit(arg, propbit, INPUT_PROP_MAX); | |
924 | goto out; | |
1da177e4 | 925 | |
c0661652 DT |
926 | case UI_SET_PHYS: |
927 | if (udev->state == UIST_CREATED) { | |
928 | retval = -EINVAL; | |
9d51e801 | 929 | goto out; |
c0661652 | 930 | } |
1da177e4 | 931 | |
c0661652 DT |
932 | phys = strndup_user(p, 1024); |
933 | if (IS_ERR(phys)) { | |
934 | retval = PTR_ERR(phys); | |
9d51e801 | 935 | goto out; |
c0661652 DT |
936 | } |
937 | ||
938 | kfree(udev->dev->phys); | |
939 | udev->dev->phys = phys; | |
940 | goto out; | |
1da177e4 | 941 | |
c0661652 DT |
942 | case UI_BEGIN_FF_UPLOAD: |
943 | retval = uinput_ff_upload_from_user(p, &ff_up); | |
944 | if (retval) | |
9d51e801 | 945 | goto out; |
59c7c037 | 946 | |
c0661652 DT |
947 | req = uinput_request_find(udev, ff_up.request_id); |
948 | if (!req || req->code != UI_FF_UPLOAD || | |
949 | !req->u.upload.effect) { | |
950 | retval = -EINVAL; | |
9d51e801 | 951 | goto out; |
c0661652 | 952 | } |
85b77200 | 953 | |
c0661652 DT |
954 | ff_up.retval = 0; |
955 | ff_up.effect = *req->u.upload.effect; | |
956 | if (req->u.upload.old) | |
957 | ff_up.old = *req->u.upload.old; | |
958 | else | |
959 | memset(&ff_up.old, 0, sizeof(struct ff_effect)); | |
4dfcc271 | 960 | |
c0661652 DT |
961 | retval = uinput_ff_upload_to_user(p, &ff_up); |
962 | goto out; | |
4dfcc271 | 963 | |
c0661652 DT |
964 | case UI_BEGIN_FF_ERASE: |
965 | if (copy_from_user(&ff_erase, p, sizeof(ff_erase))) { | |
966 | retval = -EFAULT; | |
9d51e801 | 967 | goto out; |
c0661652 | 968 | } |
1da177e4 | 969 | |
c0661652 DT |
970 | req = uinput_request_find(udev, ff_erase.request_id); |
971 | if (!req || req->code != UI_FF_ERASE) { | |
972 | retval = -EINVAL; | |
9d51e801 | 973 | goto out; |
c0661652 | 974 | } |
1da177e4 | 975 | |
c0661652 DT |
976 | ff_erase.retval = 0; |
977 | ff_erase.effect_id = req->u.effect_id; | |
978 | if (copy_to_user(p, &ff_erase, sizeof(ff_erase))) { | |
979 | retval = -EFAULT; | |
9d51e801 | 980 | goto out; |
c0661652 | 981 | } |
1da177e4 | 982 | |
c0661652 | 983 | goto out; |
2d56f3a3 | 984 | |
c0661652 DT |
985 | case UI_END_FF_UPLOAD: |
986 | retval = uinput_ff_upload_from_user(p, &ff_up); | |
987 | if (retval) | |
988 | goto out; | |
2d56f3a3 | 989 | |
c0661652 DT |
990 | req = uinput_request_find(udev, ff_up.request_id); |
991 | if (!req || req->code != UI_FF_UPLOAD || | |
992 | !req->u.upload.effect) { | |
993 | retval = -EINVAL; | |
9d51e801 | 994 | goto out; |
c0661652 | 995 | } |
1da177e4 | 996 | |
c0661652 DT |
997 | req->retval = ff_up.retval; |
998 | complete(&req->done); | |
999 | goto out; | |
2d56f3a3 | 1000 | |
c0661652 DT |
1001 | case UI_END_FF_ERASE: |
1002 | if (copy_from_user(&ff_erase, p, sizeof(ff_erase))) { | |
1003 | retval = -EFAULT; | |
1004 | goto out; | |
1005 | } | |
2d56f3a3 | 1006 | |
c0661652 DT |
1007 | req = uinput_request_find(udev, ff_erase.request_id); |
1008 | if (!req || req->code != UI_FF_ERASE) { | |
1009 | retval = -EINVAL; | |
9d51e801 | 1010 | goto out; |
c0661652 DT |
1011 | } |
1012 | ||
1013 | req->retval = ff_erase.retval; | |
1014 | complete(&req->done); | |
1015 | goto out; | |
1da177e4 | 1016 | } |
29506415 | 1017 | |
e3480a61 BT |
1018 | size = _IOC_SIZE(cmd); |
1019 | ||
1020 | /* Now check variable-length commands */ | |
1021 | switch (cmd & ~IOCSIZE_MASK) { | |
1022 | case UI_GET_SYSNAME(0): | |
1023 | if (udev->state != UIST_CREATED) { | |
1024 | retval = -ENOENT; | |
1025 | goto out; | |
1026 | } | |
1027 | name = dev_name(&udev->dev->dev); | |
1028 | retval = uinput_str_to_user(p, name, size); | |
1029 | goto out; | |
052876f8 BT |
1030 | |
1031 | case UI_ABS_SETUP & ~IOCSIZE_MASK: | |
1032 | retval = uinput_abs_setup(udev, p, size); | |
1033 | goto out; | |
e3480a61 BT |
1034 | } |
1035 | ||
9d51e801 | 1036 | retval = -EINVAL; |
29506415 | 1037 | out: |
221979aa | 1038 | mutex_unlock(&udev->mutex); |
1da177e4 LT |
1039 | return retval; |
1040 | } | |
1041 | ||
2d56f3a3 PL |
1042 | static long uinput_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
1043 | { | |
1044 | return uinput_ioctl_handler(file, cmd, arg, (void __user *)arg); | |
1045 | } | |
1046 | ||
1047 | #ifdef CONFIG_COMPAT | |
affa80bd RL |
1048 | |
1049 | #define UI_SET_PHYS_COMPAT _IOW(UINPUT_IOCTL_BASE, 108, compat_uptr_t) | |
1050 | ||
54ce165e DT |
1051 | static long uinput_compat_ioctl(struct file *file, |
1052 | unsigned int cmd, unsigned long arg) | |
2d56f3a3 | 1053 | { |
affa80bd RL |
1054 | if (cmd == UI_SET_PHYS_COMPAT) |
1055 | cmd = UI_SET_PHYS; | |
1056 | ||
2d56f3a3 PL |
1057 | return uinput_ioctl_handler(file, cmd, arg, compat_ptr(arg)); |
1058 | } | |
1059 | #endif | |
1060 | ||
2b8693c0 | 1061 | static const struct file_operations uinput_fops = { |
29506415 DT |
1062 | .owner = THIS_MODULE, |
1063 | .open = uinput_open, | |
1064 | .release = uinput_release, | |
1065 | .read = uinput_read, | |
1066 | .write = uinput_write, | |
1067 | .poll = uinput_poll, | |
1068 | .unlocked_ioctl = uinput_ioctl, | |
2d56f3a3 PL |
1069 | #ifdef CONFIG_COMPAT |
1070 | .compat_ioctl = uinput_compat_ioctl, | |
1071 | #endif | |
6038f373 | 1072 | .llseek = no_llseek, |
1da177e4 LT |
1073 | }; |
1074 | ||
1075 | static struct miscdevice uinput_misc = { | |
29506415 DT |
1076 | .fops = &uinput_fops, |
1077 | .minor = UINPUT_MINOR, | |
1078 | .name = UINPUT_NAME, | |
1da177e4 | 1079 | }; |
ca75d601 PM |
1080 | module_misc_device(uinput_misc); |
1081 | ||
8905aaaf KS |
1082 | MODULE_ALIAS_MISCDEV(UINPUT_MINOR); |
1083 | MODULE_ALIAS("devname:" UINPUT_NAME); | |
1da177e4 | 1084 | |
1da177e4 LT |
1085 | MODULE_AUTHOR("Aristeu Sergio Rozanski Filho"); |
1086 | MODULE_DESCRIPTION("User level driver support for input subsystem"); | |
1087 | MODULE_LICENSE("GPL"); | |
ff462551 | 1088 | MODULE_VERSION("0.3"); |