Import changeset
[linux-flexiantxendom0-3.2.10.git] / drivers / input / evdev.c
1 /*
2  * $Id: evdev.c,v 1.10 2000/06/23 09:23:00 vojtech Exp $
3  *
4  *  Copyright (c) 1999-2000 Vojtech Pavlik
5  *
6  *  Event char devices, giving access to raw input device events.
7  *
8  *  Sponsored by SuSE
9  */
10
11 /*
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or 
15  * (at your option) any later version.
16  * 
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  * 
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25  * 
26  * Should you need to contact me, the author, you can do so either by
27  * e-mail - mail your message to <vojtech@suse.cz>, or by paper mail:
28  * Vojtech Pavlik, Ucitelska 1576, Prague 8, 182 00 Czech Republic
29  */
30
31 #define EVDEV_MINOR_BASE        64
32 #define EVDEV_MINORS            32
33 #define EVDEV_BUFFER_SIZE       64
34
35 #include <linux/poll.h>
36 #include <linux/malloc.h>
37 #include <linux/module.h>
38 #include <linux/init.h>
39 #include <linux/input.h>
40 #include <linux/smp_lock.h>
41
42 struct evdev {
43         int exist;
44         int open;
45         int minor;
46         struct input_handle handle;
47         wait_queue_head_t wait;
48         devfs_handle_t devfs;
49         struct evdev_list *list;
50 };
51
52 struct evdev_list {
53         struct input_event buffer[EVDEV_BUFFER_SIZE];
54         int head;
55         int tail;
56         struct fasync_struct *fasync;
57         struct evdev *evdev;
58         struct evdev_list *next;
59 };
60
61 static struct evdev *evdev_table[EVDEV_MINORS] = { NULL, /* ... */ };
62
63 static void evdev_event(struct input_handle *handle, unsigned int type, unsigned int code, int value)
64 {
65         struct evdev *evdev = handle->private;
66         struct evdev_list *list = evdev->list;
67
68         while (list) {
69
70                 get_fast_time(&list->buffer[list->head].time);
71                 list->buffer[list->head].type = type;
72                 list->buffer[list->head].code = code;
73                 list->buffer[list->head].value = value;
74                 list->head = (list->head + 1) & (EVDEV_BUFFER_SIZE - 1);
75                 
76                 kill_fasync(&list->fasync, SIGIO, POLL_IN);
77
78                 list = list->next;
79         }
80
81         wake_up_interruptible(&evdev->wait);
82 }
83
84 static int evdev_fasync(int fd, struct file *file, int on)
85 {
86         int retval;
87         struct evdev_list *list = file->private_data;
88         retval = fasync_helper(fd, file, on, &list->fasync);
89         return retval < 0 ? retval : 0;
90 }
91
92 static int evdev_release(struct inode * inode, struct file * file)
93 {
94         struct evdev_list *list = file->private_data;
95         struct evdev_list **listptr;
96
97         lock_kernel();
98         listptr = &list->evdev->list;
99         evdev_fasync(-1, file, 0);
100
101         while (*listptr && (*listptr != list))
102                 listptr = &((*listptr)->next);
103         *listptr = (*listptr)->next;
104
105         if (!--list->evdev->open) {
106                 if (list->evdev->exist) {
107                         input_close_device(&list->evdev->handle);       
108                 } else {
109                         input_unregister_minor(list->evdev->devfs);
110                         evdev_table[list->evdev->minor] = NULL;
111                         kfree(list->evdev);
112                 }
113         }
114
115         kfree(list);
116         unlock_kernel();
117
118         return 0;
119 }
120
121 static int evdev_open(struct inode * inode, struct file * file)
122 {
123         struct evdev_list *list;
124         int i = MINOR(inode->i_rdev) - EVDEV_MINOR_BASE;
125
126         if (i >= EVDEV_MINORS || !evdev_table[i])
127                 return -ENODEV;
128
129         if (!(list = kmalloc(sizeof(struct evdev_list), GFP_KERNEL)))
130                 return -ENOMEM;
131         memset(list, 0, sizeof(struct evdev_list));
132
133         list->evdev = evdev_table[i];
134         list->next = evdev_table[i]->list;
135         evdev_table[i]->list = list;
136
137         file->private_data = list;
138
139         if (!list->evdev->open++)
140                 if (list->evdev->exist)
141                         input_open_device(&list->evdev->handle);        
142
143         return 0;
144 }
145
146 static ssize_t evdev_write(struct file * file, const char * buffer, size_t count, loff_t *ppos)
147 {
148         return -EINVAL;
149 }
150
151 static ssize_t evdev_read(struct file * file, char * buffer, size_t count, loff_t *ppos)
152 {
153         DECLARE_WAITQUEUE(wait, current);
154         struct evdev_list *list = file->private_data;
155         int retval = 0;
156
157         if (list->head == list->tail) {
158
159                 add_wait_queue(&list->evdev->wait, &wait);
160                 current->state = TASK_INTERRUPTIBLE;
161
162                 while (list->head == list->tail) {
163
164                         if (file->f_flags & O_NONBLOCK) {
165                                 retval = -EAGAIN;
166                                 break;
167                         }
168                         if (signal_pending(current)) {
169                                 retval = -ERESTARTSYS;
170                                 break;
171                         }
172
173                         schedule();
174                 }
175
176                 current->state = TASK_RUNNING;
177                 remove_wait_queue(&list->evdev->wait, &wait);
178         }
179
180         if (retval)
181                 return retval;
182
183         while (list->head != list->tail && retval + sizeof(struct input_event) <= count) {
184                 if (copy_to_user(buffer + retval, list->buffer + list->tail,
185                          sizeof(struct input_event))) return -EFAULT;
186                 list->tail = (list->tail + 1) & (EVDEV_BUFFER_SIZE - 1);
187                 retval += sizeof(struct input_event);
188         }
189
190         return retval;  
191 }
192
193 /* No kernel lock - fine */
194 static unsigned int evdev_poll(struct file *file, poll_table *wait)
195 {
196         struct evdev_list *list = file->private_data;
197         poll_wait(file, &list->evdev->wait, wait);
198         if (list->head != list->tail)
199                 return POLLIN | POLLRDNORM;
200         return 0;
201 }
202
203 static int evdev_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
204 {
205         struct evdev_list *list = file->private_data;
206         struct evdev *evdev = list->evdev;
207         struct input_dev *dev = evdev->handle.dev;
208         int retval;
209
210         switch (cmd) {
211
212                 case EVIOCGVERSION:
213                         return put_user(EV_VERSION, (int *) arg);
214
215                 case EVIOCGID:
216                         if ((retval = put_user(dev->idbus,     ((short *) arg) + 0))) return retval;
217                         if ((retval = put_user(dev->idvendor,  ((short *) arg) + 1))) return retval;
218                         if ((retval = put_user(dev->idproduct, ((short *) arg) + 2))) return retval;
219                         if ((retval = put_user(dev->idversion, ((short *) arg) + 3))) return retval;
220                         return 0;
221
222                 default:
223
224                         if (_IOC_TYPE(cmd) != 'E' || _IOC_DIR(cmd) != _IOC_READ)
225                                 return -EINVAL;
226
227                         if ((_IOC_NR(cmd) & ~EV_MAX) == _IOC_NR(EVIOCGBIT(0,0))) {
228
229                                 long *bits;
230                                 int len;
231
232                                 switch (_IOC_NR(cmd) & EV_MAX) {
233                                         case      0: bits = dev->evbit;  len = EV_MAX;  break;
234                                         case EV_KEY: bits = dev->keybit; len = KEY_MAX; break;
235                                         case EV_REL: bits = dev->relbit; len = REL_MAX; break;
236                                         case EV_ABS: bits = dev->absbit; len = ABS_MAX; break;
237                                         case EV_LED: bits = dev->ledbit; len = LED_MAX; break;
238                                         case EV_SND: bits = dev->sndbit; len = SND_MAX; break;
239                                         default: return -EINVAL;
240                                 }
241                                 len = NBITS(len) * sizeof(long);
242                                 if (len > _IOC_SIZE(cmd)) {
243                                         printk(KERN_WARNING "evdev.c: Truncating bitfield length from %d to %d\n",
244                                                 len, _IOC_SIZE(cmd));
245                                         len = _IOC_SIZE(cmd);
246                                 }
247                                 return copy_to_user((char *) arg, bits, len) ? -EFAULT : len;
248                         }
249
250                         if (_IOC_NR(cmd) == _IOC_NR(EVIOCGNAME(0))) {
251                                 int len;
252                                 if (!dev->name) return 0;
253                                 len = strlen(dev->name) + 1;
254                                 if (len > _IOC_SIZE(cmd)) len = _IOC_SIZE(cmd);
255                                 return copy_to_user((char *) arg, dev->name, len) ? -EFAULT : len;
256                         }
257
258                         if ((_IOC_NR(cmd) & ~ABS_MAX) == _IOC_NR(EVIOCGABS(0))) {
259
260                                 int t = _IOC_NR(cmd) & ABS_MAX;
261
262                                 if ((retval = put_user(dev->abs[t],     ((int *) arg) + 0))) return retval;
263                                 if ((retval = put_user(dev->absmin[t],  ((int *) arg) + 1))) return retval;
264                                 if ((retval = put_user(dev->absmax[t],  ((int *) arg) + 2))) return retval;
265                                 if ((retval = put_user(dev->absfuzz[t], ((int *) arg) + 3))) return retval;
266                                 if ((retval = put_user(dev->absflat[t], ((int *) arg) + 4))) return retval;
267
268                                 return 0;
269                         }
270         }
271         return -EINVAL;
272 }
273
274 static struct file_operations evdev_fops = {
275         owner:          THIS_MODULE,
276         read:           evdev_read,
277         write:          evdev_write,
278         poll:           evdev_poll,
279         open:           evdev_open,
280         release:        evdev_release,
281         ioctl:          evdev_ioctl,
282         fasync:         evdev_fasync,
283 };
284
285 static struct input_handle *evdev_connect(struct input_handler *handler, struct input_dev *dev)
286 {
287         struct evdev *evdev;
288         int minor;
289
290         for (minor = 0; minor < EVDEV_MINORS && evdev_table[minor]; minor++);
291         if (minor == EVDEV_MINORS) {
292                 printk(KERN_ERR "evdev: no more free evdev devices\n");
293                 return NULL;
294         }
295
296         if (!(evdev = kmalloc(sizeof(struct evdev), GFP_KERNEL)))
297                 return NULL;
298         memset(evdev, 0, sizeof(struct evdev));
299
300         init_waitqueue_head(&evdev->wait);
301
302         evdev->minor = minor;
303         evdev_table[minor] = evdev;
304
305         evdev->handle.dev = dev;
306         evdev->handle.handler = handler;
307         evdev->handle.private = evdev;
308
309         evdev->exist = 1;
310
311         evdev->devfs = input_register_minor("event%d", minor, EVDEV_MINOR_BASE);
312
313         printk(KERN_INFO "event%d: Event device for input%d\n", minor, dev->number);
314
315         return &evdev->handle;
316 }
317
318 static void evdev_disconnect(struct input_handle *handle)
319 {
320         struct evdev *evdev = handle->private;
321
322         evdev->exist = 0;
323
324         if (evdev->open) {
325                 input_close_device(handle);
326         } else {
327                 input_unregister_minor(evdev->devfs);
328                 evdev_table[evdev->minor] = NULL;
329                 kfree(evdev);
330         }
331 }
332         
333 static struct input_handler evdev_handler = {
334         event:          evdev_event,
335         connect:        evdev_connect,
336         disconnect:     evdev_disconnect,
337         fops:           &evdev_fops,
338         minor:          EVDEV_MINOR_BASE,
339 };
340
341 static int __init evdev_init(void)
342 {
343         input_register_handler(&evdev_handler);
344         return 0;
345 }
346
347 static void __exit evdev_exit(void)
348 {
349         input_unregister_handler(&evdev_handler);
350 }
351
352 module_init(evdev_init);
353 module_exit(evdev_exit);
354
355 MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>");
356 MODULE_DESCRIPTION("Event character device driver");