]> Pileus Git - ~andy/linux/blob - drivers/input/misc/uinput.c
b247e1c8e8f655bc6fef59e70542f7ea27534b8b
[~andy/linux] / drivers / input / misc / uinput.c
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:
23  *      0.3     09/04/2006 (Anssi Hannula <anssi.hannula@gmail.com>)
24  *              - updated ff support for the changes in kernel interface
25  *              - added MODULE_VERSION
26  *      0.2     16/10/2004 (Micah Dowty <micah@navi.cx>)
27  *              - added force feedback support
28  *              - added UI_SET_PHYS
29  *      0.1     20/06/2002
30  *              - first public version
31  */
32 #include <linux/poll.h>
33 #include <linux/sched.h>
34 #include <linux/slab.h>
35 #include <linux/module.h>
36 #include <linux/init.h>
37 #include <linux/fs.h>
38 #include <linux/miscdevice.h>
39 #include <linux/uinput.h>
40 #include <linux/input/mt.h>
41 #include "../input-compat.h"
42
43 static int uinput_dev_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
44 {
45         struct uinput_device    *udev = input_get_drvdata(dev);
46
47         udev->buff[udev->head].type = type;
48         udev->buff[udev->head].code = code;
49         udev->buff[udev->head].value = value;
50         do_gettimeofday(&udev->buff[udev->head].time);
51         udev->head = (udev->head + 1) % UINPUT_BUFFER_SIZE;
52
53         wake_up_interruptible(&udev->waitq);
54
55         return 0;
56 }
57
58 /* Atomically allocate an ID for the given request. Returns 0 on success. */
59 static bool uinput_request_alloc_id(struct uinput_device *udev,
60                                     struct uinput_request *request)
61 {
62         unsigned int id;
63         bool reserved = false;
64
65         spin_lock(&udev->requests_lock);
66
67         for (id = 0; id < UINPUT_NUM_REQUESTS; id++) {
68                 if (!udev->requests[id]) {
69                         request->id = id;
70                         udev->requests[id] = request;
71                         reserved = true;
72                         break;
73                 }
74         }
75
76         spin_unlock(&udev->requests_lock);
77         return reserved;
78 }
79
80 static struct uinput_request *uinput_request_find(struct uinput_device *udev,
81                                                   unsigned int id)
82 {
83         /* Find an input request, by ID. Returns NULL if the ID isn't valid. */
84         if (id >= UINPUT_NUM_REQUESTS)
85                 return NULL;
86
87         return udev->requests[id];
88 }
89
90 static int uinput_request_reserve_slot(struct uinput_device *udev,
91                                        struct uinput_request *request)
92 {
93         /* Allocate slot. If none are available right away, wait. */
94         return wait_event_interruptible(udev->requests_waitq,
95                                         uinput_request_alloc_id(udev, request));
96 }
97
98 static void uinput_request_done(struct uinput_device *udev, struct uinput_request *request)
99 {
100         /* Mark slot as available */
101         udev->requests[request->id] = NULL;
102         wake_up(&udev->requests_waitq);
103
104         complete(&request->done);
105 }
106
107 static int uinput_request_send(struct uinput_device *udev,
108                                struct uinput_request *request)
109 {
110         int retval;
111
112         retval = mutex_lock_interruptible(&udev->mutex);
113         if (retval)
114                 return retval;
115
116         if (udev->state != UIST_CREATED) {
117                 retval = -ENODEV;
118                 goto out;
119         }
120
121         init_completion(&request->done);
122
123         /*
124          * Tell our userspace application about this new request
125          * by queueing an input event.
126          */
127         uinput_dev_event(udev->dev, EV_UINPUT, request->code, request->id);
128
129  out:
130         mutex_unlock(&udev->mutex);
131         return retval;
132 }
133
134 static int uinput_request_submit(struct uinput_device *udev,
135                                  struct uinput_request *request)
136 {
137         int error;
138
139         error = uinput_request_reserve_slot(udev, request);
140         if (error)
141                 return error;
142
143         error = uinput_request_send(udev, request);
144         if (error) {
145                 uinput_request_done(udev, request);
146                 return error;
147         }
148
149         wait_for_completion(&request->done);
150         return request->retval;
151 }
152
153 /*
154  * Fail all ouitstanding requests so handlers don't wait for the userspace
155  * to finish processing them.
156  */
157 static void uinput_flush_requests(struct uinput_device *udev)
158 {
159         struct uinput_request *request;
160         int i;
161
162         spin_lock(&udev->requests_lock);
163
164         for (i = 0; i < UINPUT_NUM_REQUESTS; i++) {
165                 request = udev->requests[i];
166                 if (request) {
167                         request->retval = -ENODEV;
168                         uinput_request_done(udev, request);
169                 }
170         }
171
172         spin_unlock(&udev->requests_lock);
173 }
174
175 static void uinput_dev_set_gain(struct input_dev *dev, u16 gain)
176 {
177         uinput_dev_event(dev, EV_FF, FF_GAIN, gain);
178 }
179
180 static void uinput_dev_set_autocenter(struct input_dev *dev, u16 magnitude)
181 {
182         uinput_dev_event(dev, EV_FF, FF_AUTOCENTER, magnitude);
183 }
184
185 static int uinput_dev_playback(struct input_dev *dev, int effect_id, int value)
186 {
187         return uinput_dev_event(dev, EV_FF, effect_id, value);
188 }
189
190 static int uinput_dev_upload_effect(struct input_dev *dev, struct ff_effect *effect, struct ff_effect *old)
191 {
192         struct uinput_device *udev = input_get_drvdata(dev);
193         struct uinput_request request;
194
195         /*
196          * uinput driver does not currently support periodic effects with
197          * custom waveform since it does not have a way to pass buffer of
198          * samples (custom_data) to userspace. If ever there is a device
199          * supporting custom waveforms we would need to define an additional
200          * ioctl (UI_UPLOAD_SAMPLES) but for now we just bail out.
201          */
202         if (effect->type == FF_PERIODIC &&
203                         effect->u.periodic.waveform == FF_CUSTOM)
204                 return -EINVAL;
205
206         request.code = UI_FF_UPLOAD;
207         request.u.upload.effect = effect;
208         request.u.upload.old = old;
209
210         return uinput_request_submit(udev, &request);
211 }
212
213 static int uinput_dev_erase_effect(struct input_dev *dev, int effect_id)
214 {
215         struct uinput_device *udev = input_get_drvdata(dev);
216         struct uinput_request request;
217
218         if (!test_bit(EV_FF, dev->evbit))
219                 return -ENOSYS;
220
221         request.code = UI_FF_ERASE;
222         request.u.effect_id = effect_id;
223
224         return uinput_request_submit(udev, &request);
225 }
226
227 static void uinput_destroy_device(struct uinput_device *udev)
228 {
229         const char *name, *phys;
230         struct input_dev *dev = udev->dev;
231         enum uinput_state old_state = udev->state;
232
233         udev->state = UIST_NEW_DEVICE;
234
235         if (dev) {
236                 name = dev->name;
237                 phys = dev->phys;
238                 if (old_state == UIST_CREATED) {
239                         uinput_flush_requests(udev);
240                         input_unregister_device(dev);
241                 } else {
242                         input_free_device(dev);
243                 }
244                 kfree(name);
245                 kfree(phys);
246                 udev->dev = NULL;
247         }
248 }
249
250 static int uinput_create_device(struct uinput_device *udev)
251 {
252         struct input_dev *dev = udev->dev;
253         int error;
254
255         if (udev->state != UIST_SETUP_COMPLETE) {
256                 printk(KERN_DEBUG "%s: write device info first\n", UINPUT_NAME);
257                 return -EINVAL;
258         }
259
260         if (udev->ff_effects_max) {
261                 error = input_ff_create(dev, udev->ff_effects_max);
262                 if (error)
263                         goto fail1;
264
265                 dev->ff->upload = uinput_dev_upload_effect;
266                 dev->ff->erase = uinput_dev_erase_effect;
267                 dev->ff->playback = uinput_dev_playback;
268                 dev->ff->set_gain = uinput_dev_set_gain;
269                 dev->ff->set_autocenter = uinput_dev_set_autocenter;
270         }
271
272         error = input_register_device(udev->dev);
273         if (error)
274                 goto fail2;
275
276         udev->state = UIST_CREATED;
277
278         return 0;
279
280  fail2: input_ff_destroy(dev);
281  fail1: uinput_destroy_device(udev);
282         return error;
283 }
284
285 static int uinput_open(struct inode *inode, struct file *file)
286 {
287         struct uinput_device *newdev;
288
289         newdev = kzalloc(sizeof(struct uinput_device), GFP_KERNEL);
290         if (!newdev)
291                 return -ENOMEM;
292
293         mutex_init(&newdev->mutex);
294         spin_lock_init(&newdev->requests_lock);
295         init_waitqueue_head(&newdev->requests_waitq);
296         init_waitqueue_head(&newdev->waitq);
297         newdev->state = UIST_NEW_DEVICE;
298
299         file->private_data = newdev;
300         nonseekable_open(inode, file);
301
302         return 0;
303 }
304
305 static int uinput_validate_absbits(struct input_dev *dev)
306 {
307         unsigned int cnt;
308         int retval = 0;
309
310         for (cnt = 0; cnt < ABS_CNT; cnt++) {
311                 int min, max;
312                 if (!test_bit(cnt, dev->absbit))
313                         continue;
314
315                 min = input_abs_get_min(dev, cnt);
316                 max = input_abs_get_max(dev, cnt);
317
318                 if ((min != 0 || max != 0) && max <= min) {
319                         printk(KERN_DEBUG
320                                 "%s: invalid abs[%02x] min:%d max:%d\n",
321                                 UINPUT_NAME, cnt,
322                                 input_abs_get_min(dev, cnt),
323                                 input_abs_get_max(dev, cnt));
324                         retval = -EINVAL;
325                         break;
326                 }
327
328                 if (input_abs_get_flat(dev, cnt) >
329                     input_abs_get_max(dev, cnt) - input_abs_get_min(dev, cnt)) {
330                         printk(KERN_DEBUG
331                                 "%s: abs_flat #%02x out of range: %d "
332                                 "(min:%d/max:%d)\n",
333                                 UINPUT_NAME, cnt,
334                                 input_abs_get_flat(dev, cnt),
335                                 input_abs_get_min(dev, cnt),
336                                 input_abs_get_max(dev, cnt));
337                         retval = -EINVAL;
338                         break;
339                 }
340         }
341         return retval;
342 }
343
344 static int uinput_allocate_device(struct uinput_device *udev)
345 {
346         udev->dev = input_allocate_device();
347         if (!udev->dev)
348                 return -ENOMEM;
349
350         udev->dev->event = uinput_dev_event;
351         input_set_drvdata(udev->dev, udev);
352
353         return 0;
354 }
355
356 static int uinput_setup_device(struct uinput_device *udev, const char __user *buffer, size_t count)
357 {
358         struct uinput_user_dev  *user_dev;
359         struct input_dev        *dev;
360         int                     i;
361         int                     retval;
362
363         if (count != sizeof(struct uinput_user_dev))
364                 return -EINVAL;
365
366         if (!udev->dev) {
367                 retval = uinput_allocate_device(udev);
368                 if (retval)
369                         return retval;
370         }
371
372         dev = udev->dev;
373
374         user_dev = memdup_user(buffer, sizeof(struct uinput_user_dev));
375         if (IS_ERR(user_dev))
376                 return PTR_ERR(user_dev);
377
378         udev->ff_effects_max = user_dev->ff_effects_max;
379
380         /* Ensure name is filled in */
381         if (!user_dev->name[0]) {
382                 retval = -EINVAL;
383                 goto exit;
384         }
385
386         kfree(dev->name);
387         dev->name = kstrndup(user_dev->name, UINPUT_MAX_NAME_SIZE,
388                              GFP_KERNEL);
389         if (!dev->name) {
390                 retval = -ENOMEM;
391                 goto exit;
392         }
393
394         dev->id.bustype = user_dev->id.bustype;
395         dev->id.vendor  = user_dev->id.vendor;
396         dev->id.product = user_dev->id.product;
397         dev->id.version = user_dev->id.version;
398
399         for (i = 0; i < ABS_CNT; i++) {
400                 input_abs_set_max(dev, i, user_dev->absmax[i]);
401                 input_abs_set_min(dev, i, user_dev->absmin[i]);
402                 input_abs_set_fuzz(dev, i, user_dev->absfuzz[i]);
403                 input_abs_set_flat(dev, i, user_dev->absflat[i]);
404         }
405
406         /* check if absmin/absmax/absfuzz/absflat are filled as
407          * told in Documentation/input/input-programming.txt */
408         if (test_bit(EV_ABS, dev->evbit)) {
409                 retval = uinput_validate_absbits(dev);
410                 if (retval < 0)
411                         goto exit;
412                 if (test_bit(ABS_MT_SLOT, dev->absbit)) {
413                         int nslot = input_abs_get_max(dev, ABS_MT_SLOT) + 1;
414                         input_mt_init_slots(dev, nslot);
415                 } else if (test_bit(ABS_MT_POSITION_X, dev->absbit)) {
416                         input_set_events_per_packet(dev, 60);
417                 }
418         }
419
420         udev->state = UIST_SETUP_COMPLETE;
421         retval = count;
422
423  exit:
424         kfree(user_dev);
425         return retval;
426 }
427
428 static inline ssize_t uinput_inject_event(struct uinput_device *udev, const char __user *buffer, size_t count)
429 {
430         struct input_event ev;
431
432         if (count < input_event_size())
433                 return -EINVAL;
434
435         if (input_event_from_user(buffer, &ev))
436                 return -EFAULT;
437
438         input_event(udev->dev, ev.type, ev.code, ev.value);
439
440         return input_event_size();
441 }
442
443 static ssize_t uinput_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
444 {
445         struct uinput_device *udev = file->private_data;
446         int retval;
447
448         if (count == 0)
449                 return 0;
450
451         retval = mutex_lock_interruptible(&udev->mutex);
452         if (retval)
453                 return retval;
454
455         retval = udev->state == UIST_CREATED ?
456                         uinput_inject_event(udev, buffer, count) :
457                         uinput_setup_device(udev, buffer, count);
458
459         mutex_unlock(&udev->mutex);
460
461         return retval;
462 }
463
464 static bool uinput_fetch_next_event(struct uinput_device *udev,
465                                     struct input_event *event)
466 {
467         bool have_event;
468
469         spin_lock_irq(&udev->dev->event_lock);
470
471         have_event = udev->head != udev->tail;
472         if (have_event) {
473                 *event = udev->buff[udev->tail];
474                 udev->tail = (udev->tail + 1) % UINPUT_BUFFER_SIZE;
475         }
476
477         spin_unlock_irq(&udev->dev->event_lock);
478
479         return have_event;
480 }
481
482 static ssize_t uinput_events_to_user(struct uinput_device *udev,
483                                      char __user *buffer, size_t count)
484 {
485         struct input_event event;
486         size_t read = 0;
487
488         while (read + input_event_size() <= count &&
489                uinput_fetch_next_event(udev, &event)) {
490
491                 if (input_event_to_user(buffer + read, &event))
492                         return -EFAULT;
493
494                 read += input_event_size();
495         }
496
497         return read;
498 }
499
500 static ssize_t uinput_read(struct file *file, char __user *buffer,
501                            size_t count, loff_t *ppos)
502 {
503         struct uinput_device *udev = file->private_data;
504         ssize_t retval;
505
506         if (count != 0 && count < input_event_size())
507                 return -EINVAL;
508
509         do {
510                 retval = mutex_lock_interruptible(&udev->mutex);
511                 if (retval)
512                         return retval;
513
514                 if (udev->state != UIST_CREATED)
515                         retval = -ENODEV;
516                 else if (udev->head == udev->tail &&
517                          (file->f_flags & O_NONBLOCK))
518                         retval = -EAGAIN;
519                 else
520                         retval = uinput_events_to_user(udev, buffer, count);
521
522                 mutex_unlock(&udev->mutex);
523
524                 if (retval || count == 0)
525                         break;
526
527                 if (!(file->f_flags & O_NONBLOCK))
528                         retval = wait_event_interruptible(udev->waitq,
529                                                   udev->head != udev->tail ||
530                                                   udev->state != UIST_CREATED);
531         } while (retval == 0);
532
533         return retval;
534 }
535
536 static unsigned int uinput_poll(struct file *file, poll_table *wait)
537 {
538         struct uinput_device *udev = file->private_data;
539
540         poll_wait(file, &udev->waitq, wait);
541
542         if (udev->head != udev->tail)
543                 return POLLIN | POLLRDNORM;
544
545         return 0;
546 }
547
548 static int uinput_release(struct inode *inode, struct file *file)
549 {
550         struct uinput_device *udev = file->private_data;
551
552         uinput_destroy_device(udev);
553         kfree(udev);
554
555         return 0;
556 }
557
558 #ifdef CONFIG_COMPAT
559 struct uinput_ff_upload_compat {
560         __u32                   request_id;
561         __s32                   retval;
562         struct ff_effect_compat effect;
563         struct ff_effect_compat old;
564 };
565
566 static int uinput_ff_upload_to_user(char __user *buffer,
567                                     const struct uinput_ff_upload *ff_up)
568 {
569         if (INPUT_COMPAT_TEST) {
570                 struct uinput_ff_upload_compat ff_up_compat;
571
572                 ff_up_compat.request_id = ff_up->request_id;
573                 ff_up_compat.retval = ff_up->retval;
574                 /*
575                  * It so happens that the pointer that gives us the trouble
576                  * is the last field in the structure. Since we don't support
577                  * custom waveforms in uinput anyway we can just copy the whole
578                  * thing (to the compat size) and ignore the pointer.
579                  */
580                 memcpy(&ff_up_compat.effect, &ff_up->effect,
581                         sizeof(struct ff_effect_compat));
582                 memcpy(&ff_up_compat.old, &ff_up->old,
583                         sizeof(struct ff_effect_compat));
584
585                 if (copy_to_user(buffer, &ff_up_compat,
586                                  sizeof(struct uinput_ff_upload_compat)))
587                         return -EFAULT;
588         } else {
589                 if (copy_to_user(buffer, ff_up,
590                                  sizeof(struct uinput_ff_upload)))
591                         return -EFAULT;
592         }
593
594         return 0;
595 }
596
597 static int uinput_ff_upload_from_user(const char __user *buffer,
598                                       struct uinput_ff_upload *ff_up)
599 {
600         if (INPUT_COMPAT_TEST) {
601                 struct uinput_ff_upload_compat ff_up_compat;
602
603                 if (copy_from_user(&ff_up_compat, buffer,
604                                    sizeof(struct uinput_ff_upload_compat)))
605                         return -EFAULT;
606
607                 ff_up->request_id = ff_up_compat.request_id;
608                 ff_up->retval = ff_up_compat.retval;
609                 memcpy(&ff_up->effect, &ff_up_compat.effect,
610                         sizeof(struct ff_effect_compat));
611                 memcpy(&ff_up->old, &ff_up_compat.old,
612                         sizeof(struct ff_effect_compat));
613
614         } else {
615                 if (copy_from_user(ff_up, buffer,
616                                    sizeof(struct uinput_ff_upload)))
617                         return -EFAULT;
618         }
619
620         return 0;
621 }
622
623 #else
624
625 static int uinput_ff_upload_to_user(char __user *buffer,
626                                     const struct uinput_ff_upload *ff_up)
627 {
628         if (copy_to_user(buffer, ff_up, sizeof(struct uinput_ff_upload)))
629                 return -EFAULT;
630
631         return 0;
632 }
633
634 static int uinput_ff_upload_from_user(const char __user *buffer,
635                                       struct uinput_ff_upload *ff_up)
636 {
637         if (copy_from_user(ff_up, buffer, sizeof(struct uinput_ff_upload)))
638                 return -EFAULT;
639
640         return 0;
641 }
642
643 #endif
644
645 #define uinput_set_bit(_arg, _bit, _max)                \
646 ({                                                      \
647         int __ret = 0;                                  \
648         if (udev->state == UIST_CREATED)                \
649                 __ret =  -EINVAL;                       \
650         else if ((_arg) > (_max))                       \
651                 __ret = -EINVAL;                        \
652         else set_bit((_arg), udev->dev->_bit);          \
653         __ret;                                          \
654 })
655
656 static long uinput_ioctl_handler(struct file *file, unsigned int cmd,
657                                  unsigned long arg, void __user *p)
658 {
659         int                     retval;
660         struct uinput_device    *udev = file->private_data;
661         struct uinput_ff_upload ff_up;
662         struct uinput_ff_erase  ff_erase;
663         struct uinput_request   *req;
664         char                    *phys;
665
666         retval = mutex_lock_interruptible(&udev->mutex);
667         if (retval)
668                 return retval;
669
670         if (!udev->dev) {
671                 retval = uinput_allocate_device(udev);
672                 if (retval)
673                         goto out;
674         }
675
676         switch (cmd) {
677                 case UI_DEV_CREATE:
678                         retval = uinput_create_device(udev);
679                         break;
680
681                 case UI_DEV_DESTROY:
682                         uinput_destroy_device(udev);
683                         break;
684
685                 case UI_SET_EVBIT:
686                         retval = uinput_set_bit(arg, evbit, EV_MAX);
687                         break;
688
689                 case UI_SET_KEYBIT:
690                         retval = uinput_set_bit(arg, keybit, KEY_MAX);
691                         break;
692
693                 case UI_SET_RELBIT:
694                         retval = uinput_set_bit(arg, relbit, REL_MAX);
695                         break;
696
697                 case UI_SET_ABSBIT:
698                         retval = uinput_set_bit(arg, absbit, ABS_MAX);
699                         break;
700
701                 case UI_SET_MSCBIT:
702                         retval = uinput_set_bit(arg, mscbit, MSC_MAX);
703                         break;
704
705                 case UI_SET_LEDBIT:
706                         retval = uinput_set_bit(arg, ledbit, LED_MAX);
707                         break;
708
709                 case UI_SET_SNDBIT:
710                         retval = uinput_set_bit(arg, sndbit, SND_MAX);
711                         break;
712
713                 case UI_SET_FFBIT:
714                         retval = uinput_set_bit(arg, ffbit, FF_MAX);
715                         break;
716
717                 case UI_SET_SWBIT:
718                         retval = uinput_set_bit(arg, swbit, SW_MAX);
719                         break;
720
721                 case UI_SET_PROPBIT:
722                         retval = uinput_set_bit(arg, propbit, INPUT_PROP_MAX);
723                         break;
724
725                 case UI_SET_PHYS:
726                         if (udev->state == UIST_CREATED) {
727                                 retval = -EINVAL;
728                                 goto out;
729                         }
730
731                         phys = strndup_user(p, 1024);
732                         if (IS_ERR(phys)) {
733                                 retval = PTR_ERR(phys);
734                                 goto out;
735                         }
736
737                         kfree(udev->dev->phys);
738                         udev->dev->phys = phys;
739                         break;
740
741                 case UI_BEGIN_FF_UPLOAD:
742                         retval = uinput_ff_upload_from_user(p, &ff_up);
743                         if (retval)
744                                 break;
745
746                         req = uinput_request_find(udev, ff_up.request_id);
747                         if (!req || req->code != UI_FF_UPLOAD || !req->u.upload.effect) {
748                                 retval = -EINVAL;
749                                 break;
750                         }
751
752                         ff_up.retval = 0;
753                         ff_up.effect = *req->u.upload.effect;
754                         if (req->u.upload.old)
755                                 ff_up.old = *req->u.upload.old;
756                         else
757                                 memset(&ff_up.old, 0, sizeof(struct ff_effect));
758
759                         retval = uinput_ff_upload_to_user(p, &ff_up);
760                         break;
761
762                 case UI_BEGIN_FF_ERASE:
763                         if (copy_from_user(&ff_erase, p, sizeof(ff_erase))) {
764                                 retval = -EFAULT;
765                                 break;
766                         }
767
768                         req = uinput_request_find(udev, ff_erase.request_id);
769                         if (!req || req->code != UI_FF_ERASE) {
770                                 retval = -EINVAL;
771                                 break;
772                         }
773
774                         ff_erase.retval = 0;
775                         ff_erase.effect_id = req->u.effect_id;
776                         if (copy_to_user(p, &ff_erase, sizeof(ff_erase))) {
777                                 retval = -EFAULT;
778                                 break;
779                         }
780
781                         break;
782
783                 case UI_END_FF_UPLOAD:
784                         retval = uinput_ff_upload_from_user(p, &ff_up);
785                         if (retval)
786                                 break;
787
788                         req = uinput_request_find(udev, ff_up.request_id);
789                         if (!req || req->code != UI_FF_UPLOAD ||
790                             !req->u.upload.effect) {
791                                 retval = -EINVAL;
792                                 break;
793                         }
794
795                         req->retval = ff_up.retval;
796                         uinput_request_done(udev, req);
797                         break;
798
799                 case UI_END_FF_ERASE:
800                         if (copy_from_user(&ff_erase, p, sizeof(ff_erase))) {
801                                 retval = -EFAULT;
802                                 break;
803                         }
804
805                         req = uinput_request_find(udev, ff_erase.request_id);
806                         if (!req || req->code != UI_FF_ERASE) {
807                                 retval = -EINVAL;
808                                 break;
809                         }
810
811                         req->retval = ff_erase.retval;
812                         uinput_request_done(udev, req);
813                         break;
814
815                 default:
816                         retval = -EINVAL;
817         }
818
819  out:
820         mutex_unlock(&udev->mutex);
821         return retval;
822 }
823
824 static long uinput_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
825 {
826         return uinput_ioctl_handler(file, cmd, arg, (void __user *)arg);
827 }
828
829 #ifdef CONFIG_COMPAT
830 static long uinput_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
831 {
832         return uinput_ioctl_handler(file, cmd, arg, compat_ptr(arg));
833 }
834 #endif
835
836 static const struct file_operations uinput_fops = {
837         .owner          = THIS_MODULE,
838         .open           = uinput_open,
839         .release        = uinput_release,
840         .read           = uinput_read,
841         .write          = uinput_write,
842         .poll           = uinput_poll,
843         .unlocked_ioctl = uinput_ioctl,
844 #ifdef CONFIG_COMPAT
845         .compat_ioctl   = uinput_compat_ioctl,
846 #endif
847         .llseek         = no_llseek,
848 };
849
850 static struct miscdevice uinput_misc = {
851         .fops           = &uinput_fops,
852         .minor          = UINPUT_MINOR,
853         .name           = UINPUT_NAME,
854 };
855 MODULE_ALIAS_MISCDEV(UINPUT_MINOR);
856 MODULE_ALIAS("devname:" UINPUT_NAME);
857
858 static int __init uinput_init(void)
859 {
860         return misc_register(&uinput_misc);
861 }
862
863 static void __exit uinput_exit(void)
864 {
865         misc_deregister(&uinput_misc);
866 }
867
868 MODULE_AUTHOR("Aristeu Sergio Rozanski Filho");
869 MODULE_DESCRIPTION("User level driver support for input subsystem");
870 MODULE_LICENSE("GPL");
871 MODULE_VERSION("0.3");
872
873 module_init(uinput_init);
874 module_exit(uinput_exit);
875