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