Merge master.kernel.org:/pub/scm/linux/kernel/git/perex/alsa
[pandora-kernel.git] / 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.2     16/10/2004 (Micah Dowty <micah@navi.cx>)
24  *              - added force feedback support
25  *              - added UI_SET_PHYS
26  *      0.1     20/06/2002
27  *              - first public version
28  */
29 #include <linux/poll.h>
30 #include <linux/slab.h>
31 #include <linux/module.h>
32 #include <linux/init.h>
33 #include <linux/input.h>
34 #include <linux/smp_lock.h>
35 #include <linux/fs.h>
36 #include <linux/miscdevice.h>
37 #include <linux/uinput.h>
38
39 static int uinput_dev_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
40 {
41         struct uinput_device    *udev;
42
43         udev = dev->private;
44
45         udev->buff[udev->head].type = type;
46         udev->buff[udev->head].code = code;
47         udev->buff[udev->head].value = value;
48         do_gettimeofday(&udev->buff[udev->head].time);
49         udev->head = (udev->head + 1) % UINPUT_BUFFER_SIZE;
50
51         wake_up_interruptible(&udev->waitq);
52
53         return 0;
54 }
55
56 static int uinput_request_alloc_id(struct uinput_device *udev, struct uinput_request *request)
57 {
58         /* Atomically allocate an ID for the given request. Returns 0 on success. */
59         int id;
60         int err = -1;
61
62         spin_lock(&udev->requests_lock);
63
64         for (id = 0; id < UINPUT_NUM_REQUESTS; id++)
65                 if (!udev->requests[id]) {
66                         request->id = id;
67                         udev->requests[id] = request;
68                         err = 0;
69                         break;
70                 }
71
72         spin_unlock(&udev->requests_lock);
73         return err;
74 }
75
76 static struct uinput_request* uinput_request_find(struct uinput_device *udev, int id)
77 {
78         /* Find an input request, by ID. Returns NULL if the ID isn't valid. */
79         if (id >= UINPUT_NUM_REQUESTS || id < 0)
80                 return NULL;
81         return udev->requests[id];
82 }
83
84 static inline int uinput_request_reserve_slot(struct uinput_device *udev, struct uinput_request *request)
85 {
86         /* Allocate slot. If none are available right away, wait. */
87         return wait_event_interruptible(udev->requests_waitq,
88                                         !uinput_request_alloc_id(udev, request));
89 }
90
91 static void uinput_request_done(struct uinput_device *udev, struct uinput_request *request)
92 {
93         /* Mark slot as available */
94         udev->requests[request->id] = NULL;
95         wake_up_interruptible(&udev->requests_waitq);
96
97         complete(&request->done);
98 }
99
100 static int uinput_request_submit(struct input_dev *dev, struct uinput_request *request)
101 {
102         int retval;
103
104         /* Tell our userspace app about this new request by queueing an input event */
105         uinput_dev_event(dev, EV_UINPUT, request->code, request->id);
106
107         /* Wait for the request to complete */
108         retval = wait_for_completion_interruptible(&request->done);
109         if (!retval)
110                 retval = request->retval;
111
112         return retval;
113 }
114
115 static int uinput_dev_upload_effect(struct input_dev *dev, struct ff_effect *effect)
116 {
117         struct uinput_request request;
118         int retval;
119
120         if (!test_bit(EV_FF, dev->evbit))
121                 return -ENOSYS;
122
123         request.id = -1;
124         init_completion(&request.done);
125         request.code = UI_FF_UPLOAD;
126         request.u.effect = effect;
127
128         retval = uinput_request_reserve_slot(dev->private, &request);
129         if (!retval)
130                 retval = uinput_request_submit(dev, &request);
131
132         return retval;
133 }
134
135 static int uinput_dev_erase_effect(struct input_dev *dev, int effect_id)
136 {
137         struct uinput_request request;
138         int retval;
139
140         if (!test_bit(EV_FF, dev->evbit))
141                 return -ENOSYS;
142
143         request.id = -1;
144         init_completion(&request.done);
145         request.code = UI_FF_ERASE;
146         request.u.effect_id = effect_id;
147
148         retval = uinput_request_reserve_slot(dev->private, &request);
149         if (!retval)
150                 retval = uinput_request_submit(dev, &request);
151
152         return retval;
153 }
154
155 static int uinput_create_device(struct uinput_device *udev)
156 {
157         if (!udev->dev->name) {
158                 printk(KERN_DEBUG "%s: write device info first\n", UINPUT_NAME);
159                 return -EINVAL;
160         }
161
162         udev->dev->event = uinput_dev_event;
163         udev->dev->upload_effect = uinput_dev_upload_effect;
164         udev->dev->erase_effect = uinput_dev_erase_effect;
165         udev->dev->private = udev;
166
167         init_waitqueue_head(&udev->waitq);
168
169         input_register_device(udev->dev);
170
171         set_bit(UIST_CREATED, &udev->state);
172
173         return 0;
174 }
175
176 static int uinput_destroy_device(struct uinput_device *udev)
177 {
178         if (!test_bit(UIST_CREATED, &udev->state)) {
179                 printk(KERN_WARNING "%s: create the device first\n", UINPUT_NAME);
180                 return -EINVAL;
181         }
182
183         input_unregister_device(udev->dev);
184
185         clear_bit(UIST_CREATED, &udev->state);
186
187         return 0;
188 }
189
190 static int uinput_open(struct inode *inode, struct file *file)
191 {
192         struct uinput_device    *newdev;
193         struct input_dev        *newinput;
194
195         newdev = kmalloc(sizeof(struct uinput_device), GFP_KERNEL);
196         if (!newdev)
197                 goto error;
198         memset(newdev, 0, sizeof(struct uinput_device));
199         spin_lock_init(&newdev->requests_lock);
200         init_waitqueue_head(&newdev->requests_waitq);
201
202         newinput = kmalloc(sizeof(struct input_dev), GFP_KERNEL);
203         if (!newinput)
204                 goto cleanup;
205         memset(newinput, 0, sizeof(struct input_dev));
206
207         newdev->dev = newinput;
208
209         file->private_data = newdev;
210
211         return 0;
212 cleanup:
213         kfree(newdev);
214 error:
215         return -ENOMEM;
216 }
217
218 static int uinput_validate_absbits(struct input_dev *dev)
219 {
220         unsigned int cnt;
221         int retval = 0;
222
223         for (cnt = 0; cnt < ABS_MAX + 1; cnt++) {
224                 if (!test_bit(cnt, dev->absbit))
225                         continue;
226
227                 if ((dev->absmax[cnt] <= dev->absmin[cnt])) {
228                         printk(KERN_DEBUG
229                                 "%s: invalid abs[%02x] min:%d max:%d\n",
230                                 UINPUT_NAME, cnt,
231                                 dev->absmin[cnt], dev->absmax[cnt]);
232                         retval = -EINVAL;
233                         break;
234                 }
235
236                 if (dev->absflat[cnt] > (dev->absmax[cnt] - dev->absmin[cnt])) {
237                         printk(KERN_DEBUG
238                                 "%s: absflat[%02x] out of range: %d "
239                                 "(min:%d/max:%d)\n",
240                                 UINPUT_NAME, cnt, dev->absflat[cnt],
241                                 dev->absmin[cnt], dev->absmax[cnt]);
242                         retval = -EINVAL;
243                         break;
244                 }
245         }
246         return retval;
247 }
248
249 static int uinput_alloc_device(struct file *file, const char __user *buffer, size_t count)
250 {
251         struct uinput_user_dev  *user_dev;
252         struct input_dev        *dev;
253         struct uinput_device    *udev;
254         char                    *name;
255         int                     size;
256         int                     retval;
257
258         retval = count;
259
260         udev = file->private_data;
261         dev = udev->dev;
262
263         user_dev = kmalloc(sizeof(struct uinput_user_dev), GFP_KERNEL);
264         if (!user_dev) {
265                 retval = -ENOMEM;
266                 goto exit;
267         }
268
269         if (copy_from_user(user_dev, buffer, sizeof(struct uinput_user_dev))) {
270                 retval = -EFAULT;
271                 goto exit;
272         }
273
274         kfree(dev->name);
275
276         size = strnlen(user_dev->name, UINPUT_MAX_NAME_SIZE) + 1;
277         dev->name = name = kmalloc(size, GFP_KERNEL);
278         if (!name) {
279                 retval = -ENOMEM;
280                 goto exit;
281         }
282         strlcpy(name, user_dev->name, size);
283
284         dev->id.bustype = user_dev->id.bustype;
285         dev->id.vendor  = user_dev->id.vendor;
286         dev->id.product = user_dev->id.product;
287         dev->id.version = user_dev->id.version;
288         dev->ff_effects_max = user_dev->ff_effects_max;
289
290         size = sizeof(int) * (ABS_MAX + 1);
291         memcpy(dev->absmax, user_dev->absmax, size);
292         memcpy(dev->absmin, user_dev->absmin, size);
293         memcpy(dev->absfuzz, user_dev->absfuzz, size);
294         memcpy(dev->absflat, user_dev->absflat, size);
295
296         /* check if absmin/absmax/absfuzz/absflat are filled as
297          * told in Documentation/input/input-programming.txt */
298         if (test_bit(EV_ABS, dev->evbit)) {
299                 int err = uinput_validate_absbits(dev);
300                 if (err < 0) {
301                         retval = err;
302                         kfree(dev->name);
303                 }
304         }
305
306 exit:
307         kfree(user_dev);
308         return retval;
309 }
310
311 static ssize_t uinput_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
312 {
313         struct uinput_device *udev = file->private_data;
314
315         if (test_bit(UIST_CREATED, &udev->state)) {
316                 struct input_event      ev;
317
318                 if (copy_from_user(&ev, buffer, sizeof(struct input_event)))
319                         return -EFAULT;
320                 input_event(udev->dev, ev.type, ev.code, ev.value);
321         } else
322                 count = uinput_alloc_device(file, buffer, count);
323
324         return count;
325 }
326
327 static ssize_t uinput_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
328 {
329         struct uinput_device *udev = file->private_data;
330         int retval = 0;
331
332         if (!test_bit(UIST_CREATED, &udev->state))
333                 return -ENODEV;
334
335         if (udev->head == udev->tail && (file->f_flags & O_NONBLOCK))
336                 return -EAGAIN;
337
338         retval = wait_event_interruptible(udev->waitq,
339                         udev->head != udev->tail || !test_bit(UIST_CREATED, &udev->state));
340         if (retval)
341                 return retval;
342
343         if (!test_bit(UIST_CREATED, &udev->state))
344                 return -ENODEV;
345
346         while ((udev->head != udev->tail) &&
347             (retval + sizeof(struct input_event) <= count)) {
348                 if (copy_to_user(buffer + retval, &udev->buff[udev->tail], sizeof(struct input_event)))
349                         return -EFAULT;
350                 udev->tail = (udev->tail + 1) % UINPUT_BUFFER_SIZE;
351                 retval += sizeof(struct input_event);
352         }
353
354         return retval;
355 }
356
357 static unsigned int uinput_poll(struct file *file, poll_table *wait)
358 {
359         struct uinput_device *udev = file->private_data;
360
361         poll_wait(file, &udev->waitq, wait);
362
363         if (udev->head != udev->tail)
364                 return POLLIN | POLLRDNORM;
365
366         return 0;
367 }
368
369 static int uinput_burn_device(struct uinput_device *udev)
370 {
371         if (test_bit(UIST_CREATED, &udev->state))
372                 uinput_destroy_device(udev);
373
374         kfree(udev->dev->name);
375         kfree(udev->dev->phys);
376         kfree(udev->dev);
377         kfree(udev);
378
379         return 0;
380 }
381
382 static int uinput_close(struct inode *inode, struct file *file)
383 {
384         uinput_burn_device(file->private_data);
385         return 0;
386 }
387
388 static int uinput_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
389 {
390         int                     retval = 0;
391         struct uinput_device    *udev;
392         void __user             *p = (void __user *)arg;
393         struct uinput_ff_upload ff_up;
394         struct uinput_ff_erase  ff_erase;
395         struct uinput_request   *req;
396         int                     length;
397         char                    *phys;
398
399         udev = file->private_data;
400
401         /* device attributes can not be changed after the device is created */
402         switch (cmd) {
403                 case UI_SET_EVBIT:
404                 case UI_SET_KEYBIT:
405                 case UI_SET_RELBIT:
406                 case UI_SET_ABSBIT:
407                 case UI_SET_MSCBIT:
408                 case UI_SET_LEDBIT:
409                 case UI_SET_SNDBIT:
410                 case UI_SET_FFBIT:
411                 case UI_SET_PHYS:
412                         if (test_bit(UIST_CREATED, &udev->state))
413                                 return -EINVAL;
414         }
415
416         switch (cmd) {
417                 case UI_DEV_CREATE:
418                         retval = uinput_create_device(udev);
419                         break;
420
421                 case UI_DEV_DESTROY:
422                         retval = uinput_destroy_device(udev);
423                         break;
424
425                 case UI_SET_EVBIT:
426                         if (arg > EV_MAX) {
427                                 retval = -EINVAL;
428                                 break;
429                         }
430                         set_bit(arg, udev->dev->evbit);
431                         break;
432
433                 case UI_SET_KEYBIT:
434                         if (arg > KEY_MAX) {
435                                 retval = -EINVAL;
436                                 break;
437                         }
438                         set_bit(arg, udev->dev->keybit);
439                         break;
440
441                 case UI_SET_RELBIT:
442                         if (arg > REL_MAX) {
443                                 retval = -EINVAL;
444                                 break;
445                         }
446                         set_bit(arg, udev->dev->relbit);
447                         break;
448
449                 case UI_SET_ABSBIT:
450                         if (arg > ABS_MAX) {
451                                 retval = -EINVAL;
452                                 break;
453                         }
454                         set_bit(arg, udev->dev->absbit);
455                         break;
456
457                 case UI_SET_MSCBIT:
458                         if (arg > MSC_MAX) {
459                                 retval = -EINVAL;
460                                 break;
461                         }
462                         set_bit(arg, udev->dev->mscbit);
463                         break;
464
465                 case UI_SET_LEDBIT:
466                         if (arg > LED_MAX) {
467                                 retval = -EINVAL;
468                                 break;
469                         }
470                         set_bit(arg, udev->dev->ledbit);
471                         break;
472
473                 case UI_SET_SNDBIT:
474                         if (arg > SND_MAX) {
475                                 retval = -EINVAL;
476                                 break;
477                         }
478                         set_bit(arg, udev->dev->sndbit);
479                         break;
480
481                 case UI_SET_FFBIT:
482                         if (arg > FF_MAX) {
483                                 retval = -EINVAL;
484                                 break;
485                         }
486                         set_bit(arg, udev->dev->ffbit);
487                         break;
488
489                 case UI_SET_PHYS:
490                         length = strnlen_user(p, 1024);
491                         if (length <= 0) {
492                                 retval = -EFAULT;
493                                 break;
494                         }
495                         kfree(udev->dev->phys);
496                         udev->dev->phys = phys = kmalloc(length, GFP_KERNEL);
497                         if (!phys) {
498                                 retval = -ENOMEM;
499                                 break;
500                         }
501                         if (copy_from_user(phys, p, length)) {
502                                 udev->dev->phys = NULL;
503                                 kfree(phys);
504                                 retval = -EFAULT;
505                                 break;
506                         }
507                         phys[length - 1] = '\0';
508                         break;
509
510                 case UI_BEGIN_FF_UPLOAD:
511                         if (copy_from_user(&ff_up, p, sizeof(ff_up))) {
512                                 retval = -EFAULT;
513                                 break;
514                         }
515                         req = uinput_request_find(udev, ff_up.request_id);
516                         if (!(req && req->code == UI_FF_UPLOAD && req->u.effect)) {
517                                 retval = -EINVAL;
518                                 break;
519                         }
520                         ff_up.retval = 0;
521                         memcpy(&ff_up.effect, req->u.effect, sizeof(struct ff_effect));
522                         if (copy_to_user(p, &ff_up, sizeof(ff_up))) {
523                                 retval = -EFAULT;
524                                 break;
525                         }
526                         break;
527
528                 case UI_BEGIN_FF_ERASE:
529                         if (copy_from_user(&ff_erase, p, sizeof(ff_erase))) {
530                                 retval = -EFAULT;
531                                 break;
532                         }
533                         req = uinput_request_find(udev, ff_erase.request_id);
534                         if (!(req && req->code == UI_FF_ERASE)) {
535                                 retval = -EINVAL;
536                                 break;
537                         }
538                         ff_erase.retval = 0;
539                         ff_erase.effect_id = req->u.effect_id;
540                         if (copy_to_user(p, &ff_erase, sizeof(ff_erase))) {
541                                 retval = -EFAULT;
542                                 break;
543                         }
544                         break;
545
546                 case UI_END_FF_UPLOAD:
547                         if (copy_from_user(&ff_up, p, sizeof(ff_up))) {
548                                 retval = -EFAULT;
549                                 break;
550                         }
551                         req = uinput_request_find(udev, ff_up.request_id);
552                         if (!(req && req->code == UI_FF_UPLOAD && req->u.effect)) {
553                                 retval = -EINVAL;
554                                 break;
555                         }
556                         req->retval = ff_up.retval;
557                         memcpy(req->u.effect, &ff_up.effect, sizeof(struct ff_effect));
558                         uinput_request_done(udev, req);
559                         break;
560
561                 case UI_END_FF_ERASE:
562                         if (copy_from_user(&ff_erase, p, sizeof(ff_erase))) {
563                                 retval = -EFAULT;
564                                 break;
565                         }
566                         req = uinput_request_find(udev, ff_erase.request_id);
567                         if (!(req && req->code == UI_FF_ERASE)) {
568                                 retval = -EINVAL;
569                                 break;
570                         }
571                         req->retval = ff_erase.retval;
572                         uinput_request_done(udev, req);
573                         break;
574
575                 default:
576                         retval = -EINVAL;
577         }
578         return retval;
579 }
580
581 static struct file_operations uinput_fops = {
582         .owner =        THIS_MODULE,
583         .open =         uinput_open,
584         .release =      uinput_close,
585         .read =         uinput_read,
586         .write =        uinput_write,
587         .poll =         uinput_poll,
588         .ioctl =        uinput_ioctl,
589 };
590
591 static struct miscdevice uinput_misc = {
592         .fops =         &uinput_fops,
593         .minor =        UINPUT_MINOR,
594         .name =         UINPUT_NAME,
595 };
596
597 static int __init uinput_init(void)
598 {
599         return misc_register(&uinput_misc);
600 }
601
602 static void __exit uinput_exit(void)
603 {
604         misc_deregister(&uinput_misc);
605 }
606
607 MODULE_AUTHOR("Aristeu Sergio Rozanski Filho");
608 MODULE_DESCRIPTION("User level driver support for input subsystem");
609 MODULE_LICENSE("GPL");
610
611 module_init(uinput_init);
612 module_exit(uinput_exit);
613