Linux-2.6.12-rc2
[linux-flexiantxendom0-natty.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_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                 retval = uinput_validate_absbits(dev);
302                 if (retval < 0)
303                         kfree(dev->name);
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         }
322         else
323                 count = uinput_alloc_device(file, buffer, count);
324
325         return count;
326 }
327
328 static ssize_t uinput_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
329 {
330         struct uinput_device *udev = file->private_data;
331         int retval = 0;
332
333         if (!test_bit(UIST_CREATED, &(udev->state)))
334                 return -ENODEV;
335
336         if ((udev->head == udev->tail) && (file->f_flags & O_NONBLOCK))
337                 return -EAGAIN;
338
339         retval = wait_event_interruptible(udev->waitq,
340                         (udev->head != udev->tail) ||
341                         !test_bit(UIST_CREATED, &(udev->state)));
342
343         if (retval)
344                 return retval;
345
346         if (!test_bit(UIST_CREATED, &(udev->state)))
347                 return -ENODEV;
348
349         while ((udev->head != udev->tail) &&
350             (retval + sizeof(struct input_event) <= count)) {
351                 if (copy_to_user(buffer + retval, &(udev->buff[udev->tail]),
352                     sizeof(struct input_event))) return -EFAULT;
353                 udev->tail = (udev->tail + 1) % UINPUT_BUFFER_SIZE;
354                 retval += sizeof(struct input_event);
355         }
356
357         return retval;
358 }
359
360 static unsigned int uinput_poll(struct file *file, poll_table *wait)
361 {
362         struct uinput_device *udev = file->private_data;
363
364         poll_wait(file, &udev->waitq, wait);
365
366         if (udev->head != udev->tail)
367                 return POLLIN | POLLRDNORM;
368
369         return 0;
370 }
371
372 static int uinput_burn_device(struct uinput_device *udev)
373 {
374         if (test_bit(UIST_CREATED, &(udev->state)))
375                 uinput_destroy_device(udev);
376
377         if (NULL != udev->dev->name)
378                 kfree(udev->dev->name);
379         if (NULL != udev->dev->phys)
380                 kfree(udev->dev->phys);
381
382         kfree(udev->dev);
383         kfree(udev);
384
385         return 0;
386 }
387
388 static int uinput_close(struct inode *inode, struct file *file)
389 {
390         return uinput_burn_device(file->private_data);
391 }
392
393 static int uinput_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
394 {
395         int                     retval = 0;
396         struct uinput_device    *udev;
397         void __user             *p = (void __user *)arg;
398         struct uinput_ff_upload ff_up;
399         struct uinput_ff_erase  ff_erase;
400         struct uinput_request   *req;
401         int                     length;
402
403         udev = file->private_data;
404
405         /* device attributes can not be changed after the device is created */
406         switch (cmd) {
407                 case UI_SET_EVBIT:
408                 case UI_SET_KEYBIT:
409                 case UI_SET_RELBIT:
410                 case UI_SET_ABSBIT:
411                 case UI_SET_MSCBIT:
412                 case UI_SET_LEDBIT:
413                 case UI_SET_SNDBIT:
414                 case UI_SET_FFBIT:
415                 case UI_SET_PHYS:
416                         if (test_bit(UIST_CREATED, &(udev->state)))
417                                 return -EINVAL;
418         }
419
420         switch (cmd) {
421                 case UI_DEV_CREATE:
422                         retval = uinput_create_device(udev);
423                         break;
424
425                 case UI_DEV_DESTROY:
426                         retval = uinput_destroy_device(udev);
427                         break;
428
429                 case UI_SET_EVBIT:
430                         if (arg > EV_MAX) {
431                                 retval = -EINVAL;
432                                 break;
433                         }
434                         set_bit(arg, udev->dev->evbit);
435                         break;
436
437                 case UI_SET_KEYBIT:
438                         if (arg > KEY_MAX) {
439                                 retval = -EINVAL;
440                                 break;
441                         }
442                         set_bit(arg, udev->dev->keybit);
443                         break;
444
445                 case UI_SET_RELBIT:
446                         if (arg > REL_MAX) {
447                                 retval = -EINVAL;
448                                 break;
449                         }
450                         set_bit(arg, udev->dev->relbit);
451                         break;
452
453                 case UI_SET_ABSBIT:
454                         if (arg > ABS_MAX) {
455                                 retval = -EINVAL;
456                                 break;
457                         }
458                         set_bit(arg, udev->dev->absbit);
459                         break;
460
461                 case UI_SET_MSCBIT:
462                         if (arg > MSC_MAX) {
463                                 retval = -EINVAL;
464                                 break;
465                         }
466                         set_bit(arg, udev->dev->mscbit);
467                         break;
468
469                 case UI_SET_LEDBIT:
470                         if (arg > LED_MAX) {
471                                 retval = -EINVAL;
472                                 break;
473                         }
474                         set_bit(arg, udev->dev->ledbit);
475                         break;
476
477                 case UI_SET_SNDBIT:
478                         if (arg > SND_MAX) {
479                                 retval = -EINVAL;
480                                 break;
481                         }
482                         set_bit(arg, udev->dev->sndbit);
483                         break;
484
485                 case UI_SET_FFBIT:
486                         if (arg > FF_MAX) {
487                                 retval = -EINVAL;
488                                 break;
489                         }
490                         set_bit(arg, udev->dev->ffbit);
491                         break;
492
493                 case UI_SET_PHYS:
494                         length = strnlen_user(p, 1024);
495                         if (length <= 0) {
496                                 retval = -EFAULT;
497                                 break;
498                         }
499                         if (NULL != udev->dev->phys)
500                                 kfree(udev->dev->phys);
501                         udev->dev->phys = kmalloc(length, GFP_KERNEL);
502                         if (!udev->dev->phys) {
503                                 retval = -ENOMEM;
504                                 break;
505                         }
506                         if (copy_from_user(udev->dev->phys, p, length)) {
507                                 retval = -EFAULT;
508                                 kfree(udev->dev->phys);
509                                 udev->dev->phys = NULL;
510                                 break;
511                         }
512                         udev->dev->phys[length-1] = '\0';
513                         break;
514
515                 case UI_BEGIN_FF_UPLOAD:
516                         if (copy_from_user(&ff_up, p, sizeof(ff_up))) {
517                                 retval = -EFAULT;
518                                 break;
519                         }
520                         req = uinput_request_find(udev, ff_up.request_id);
521                         if (!(req && req->code==UI_FF_UPLOAD && req->u.effect)) {
522                                 retval = -EINVAL;
523                                 break;
524                         }
525                         ff_up.retval = 0;
526                         memcpy(&ff_up.effect, req->u.effect, sizeof(struct ff_effect));
527                         if (copy_to_user(p, &ff_up, sizeof(ff_up))) {
528                                 retval = -EFAULT;
529                                 break;
530                         }
531                         break;
532
533                 case UI_BEGIN_FF_ERASE:
534                         if (copy_from_user(&ff_erase, p, sizeof(ff_erase))) {
535                                 retval = -EFAULT;
536                                 break;
537                         }
538                         req = uinput_request_find(udev, ff_erase.request_id);
539                         if (!(req && req->code==UI_FF_ERASE)) {
540                                 retval = -EINVAL;
541                                 break;
542                         }
543                         ff_erase.retval = 0;
544                         ff_erase.effect_id = req->u.effect_id;
545                         if (copy_to_user(p, &ff_erase, sizeof(ff_erase))) {
546                                 retval = -EFAULT;
547                                 break;
548                         }
549                         break;
550
551                 case UI_END_FF_UPLOAD:
552                         if (copy_from_user(&ff_up, p, sizeof(ff_up))) {
553                                 retval = -EFAULT;
554                                 break;
555                         }
556                         req = uinput_request_find(udev, ff_up.request_id);
557                         if (!(req && req->code==UI_FF_UPLOAD && req->u.effect)) {
558                                 retval = -EINVAL;
559                                 break;
560                         }
561                         req->retval = ff_up.retval;
562                         memcpy(req->u.effect, &ff_up.effect, sizeof(struct ff_effect));
563                         req->completed = 1;
564                         wake_up_interruptible(&req->waitq);
565                         break;
566
567                 case UI_END_FF_ERASE:
568                         if (copy_from_user(&ff_erase, p, sizeof(ff_erase))) {
569                                 retval = -EFAULT;
570                                 break;
571                         }
572                         req = uinput_request_find(udev, ff_erase.request_id);
573                         if (!(req && req->code==UI_FF_ERASE)) {
574                                 retval = -EINVAL;
575                                 break;
576                         }
577                         req->retval = ff_erase.retval;
578                         req->completed = 1;
579                         wake_up_interruptible(&req->waitq);
580                         break;
581
582                 default:
583                         retval = -EINVAL;
584         }
585         return retval;
586 }
587
588 static struct file_operations uinput_fops = {
589         .owner =        THIS_MODULE,
590         .open =         uinput_open,
591         .release =      uinput_close,
592         .read =         uinput_read,
593         .write =        uinput_write,
594         .poll =         uinput_poll,
595         .ioctl =        uinput_ioctl,
596 };
597
598 static struct miscdevice uinput_misc = {
599         .fops =         &uinput_fops,
600         .minor =        UINPUT_MINOR,
601         .name =         UINPUT_NAME,
602 };
603
604 static int __init uinput_init(void)
605 {
606         return misc_register(&uinput_misc);
607 }
608
609 static void __exit uinput_exit(void)
610 {
611         misc_deregister(&uinput_misc);
612 }
613
614 MODULE_AUTHOR("Aristeu Sergio Rozanski Filho");
615 MODULE_DESCRIPTION("User level driver support for input subsystem");
616 MODULE_LICENSE("GPL");
617
618 module_init(uinput_init);
619 module_exit(uinput_exit);
620