MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / drivers / input / misc / uinput.c
blob477579b9ee51f104cb208c90289f092fec5dc1f9
1 /*
2 * User level driver support for input subsystem
4 * Heavily based on evdev.c by Vojtech Pavlik
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.
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.
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
20 * Author: Aristeu Sergio Rozanski Filho <aris@cathedrallabs.org>
22 * Changes/Revisions:
23 * 0.1 20/06/2002
24 * - first public version
26 #include <linux/poll.h>
27 #include <linux/slab.h>
28 #include <linux/module.h>
29 #include <linux/init.h>
30 #include <linux/input.h>
31 #include <linux/smp_lock.h>
32 #include <linux/fs.h>
33 #include <linux/miscdevice.h>
34 #include <linux/uinput.h>
36 static int uinput_dev_open(struct input_dev *dev)
38 return 0;
41 static void uinput_dev_close(struct input_dev *dev)
46 static int uinput_dev_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
48 struct uinput_device *udev;
50 udev = (struct uinput_device *)dev->private;
52 udev->buff[udev->head].type = type;
53 udev->buff[udev->head].code = code;
54 udev->buff[udev->head].value = value;
55 do_gettimeofday(&udev->buff[udev->head].time);
56 udev->head = (udev->head + 1) % UINPUT_BUFFER_SIZE;
58 wake_up_interruptible(&udev->waitq);
60 return 0;
63 static int uinput_dev_upload_effect(struct input_dev *dev, struct ff_effect *effect)
65 return 0;
68 static int uinput_dev_erase_effect(struct input_dev *dev, int effect_id)
70 return 0;
73 static int uinput_create_device(struct uinput_device *udev)
75 if (!udev->dev->name) {
76 printk(KERN_DEBUG "%s: write device info first\n", UINPUT_NAME);
77 return -EINVAL;
80 udev->dev->open = uinput_dev_open;
81 udev->dev->close = uinput_dev_close;
82 udev->dev->event = uinput_dev_event;
83 udev->dev->upload_effect = uinput_dev_upload_effect;
84 udev->dev->erase_effect = uinput_dev_erase_effect;
85 udev->dev->private = udev;
87 init_waitqueue_head(&(udev->waitq));
89 input_register_device(udev->dev);
91 set_bit(UIST_CREATED, &(udev->state));
93 return 0;
96 static int uinput_destroy_device(struct uinput_device *udev)
98 if (!test_bit(UIST_CREATED, &(udev->state))) {
99 printk(KERN_WARNING "%s: create the device first\n", UINPUT_NAME);
100 return -EINVAL;
103 input_unregister_device(udev->dev);
105 clear_bit(UIST_CREATED, &(udev->state));
107 return 0;
110 static int uinput_open(struct inode *inode, struct file *file)
112 struct uinput_device *newdev;
113 struct input_dev *newinput;
115 newdev = kmalloc(sizeof(struct uinput_device), GFP_KERNEL);
116 if (!newdev)
117 goto error;
118 memset(newdev, 0, sizeof(struct uinput_device));
120 newinput = kmalloc(sizeof(struct input_dev), GFP_KERNEL);
121 if (!newinput)
122 goto cleanup;
123 memset(newinput, 0, sizeof(struct input_dev));
125 newdev->dev = newinput;
127 file->private_data = newdev;
129 return 0;
130 cleanup:
131 kfree(newdev);
132 error:
133 return -ENOMEM;
136 static int uinput_validate_absbits(struct input_dev *dev)
138 unsigned int cnt;
139 int retval = 0;
141 for (cnt = 0; cnt < ABS_MAX; cnt++) {
142 if (!test_bit(cnt, dev->absbit))
143 continue;
145 if (/*!dev->absmin[cnt] || !dev->absmax[cnt] || */
146 (dev->absmax[cnt] <= dev->absmin[cnt])) {
147 printk(KERN_DEBUG
148 "%s: invalid abs[%02x] min:%d max:%d\n",
149 UINPUT_NAME, cnt,
150 dev->absmin[cnt], dev->absmax[cnt]);
151 retval = -EINVAL;
152 break;
155 if ((dev->absflat[cnt] < dev->absmin[cnt]) ||
156 (dev->absflat[cnt] > dev->absmax[cnt])) {
157 printk(KERN_DEBUG
158 "%s: absflat[%02x] out of range: %d "
159 "(min:%d/max:%d)\n",
160 UINPUT_NAME, cnt, dev->absflat[cnt],
161 dev->absmin[cnt], dev->absmax[cnt]);
162 retval = -EINVAL;
163 break;
166 return retval;
169 static int uinput_alloc_device(struct file *file, const char __user *buffer, size_t count)
171 struct uinput_user_dev *user_dev;
172 struct input_dev *dev;
173 struct uinput_device *udev;
174 int size,
175 retval;
177 retval = count;
179 udev = (struct uinput_device *)file->private_data;
180 dev = udev->dev;
182 user_dev = kmalloc(sizeof(*user_dev), GFP_KERNEL);
183 if (!user_dev) {
184 retval = -ENOMEM;
185 goto exit;
188 if (copy_from_user(user_dev, buffer, sizeof(struct uinput_user_dev))) {
189 retval = -EFAULT;
190 goto exit;
193 if (NULL != dev->name)
194 kfree(dev->name);
196 size = strnlen(user_dev->name, UINPUT_MAX_NAME_SIZE) + 1;
197 dev->name = kmalloc(size, GFP_KERNEL);
198 if (!dev->name) {
199 retval = -ENOMEM;
200 goto exit;
203 strlcpy(dev->name, user_dev->name, size);
204 dev->id.bustype = user_dev->id.bustype;
205 dev->id.vendor = user_dev->id.vendor;
206 dev->id.product = user_dev->id.product;
207 dev->id.version = user_dev->id.version;
208 dev->ff_effects_max = user_dev->ff_effects_max;
210 size = sizeof(int) * (ABS_MAX + 1);
211 memcpy(dev->absmax, user_dev->absmax, size);
212 memcpy(dev->absmin, user_dev->absmin, size);
213 memcpy(dev->absfuzz, user_dev->absfuzz, size);
214 memcpy(dev->absflat, user_dev->absflat, size);
216 /* check if absmin/absmax/absfuzz/absflat are filled as
217 * told in Documentation/input/input-programming.txt */
218 if (test_bit(EV_ABS, dev->evbit)) {
219 retval = uinput_validate_absbits(dev);
220 if (retval < 0)
221 kfree(dev->name);
224 exit:
225 kfree(user_dev);
226 return retval;
229 static ssize_t uinput_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
231 struct uinput_device *udev = file->private_data;
233 if (test_bit(UIST_CREATED, &(udev->state))) {
234 struct input_event ev;
236 if (copy_from_user(&ev, buffer, sizeof(struct input_event)))
237 return -EFAULT;
238 input_event(udev->dev, ev.type, ev.code, ev.value);
240 else
241 count = uinput_alloc_device(file, buffer, count);
243 return count;
246 static ssize_t uinput_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
248 struct uinput_device *udev = file->private_data;
249 int retval = 0;
251 if (!test_bit(UIST_CREATED, &(udev->state)))
252 return -ENODEV;
254 if ((udev->head == udev->tail) && (file->f_flags & O_NONBLOCK))
255 return -EAGAIN;
257 retval = wait_event_interruptible(udev->waitq,
258 (udev->head != udev->tail) ||
259 !test_bit(UIST_CREATED, &(udev->state)));
261 if (retval)
262 return retval;
264 if (!test_bit(UIST_CREATED, &(udev->state)))
265 return -ENODEV;
267 while ((udev->head != udev->tail) &&
268 (retval + sizeof(struct input_event) <= count)) {
269 if (copy_to_user(buffer + retval, &(udev->buff[udev->tail]),
270 sizeof(struct input_event))) return -EFAULT;
271 udev->tail = (udev->tail + 1) % UINPUT_BUFFER_SIZE;
272 retval += sizeof(struct input_event);
275 return retval;
278 static unsigned int uinput_poll(struct file *file, poll_table *wait)
280 struct uinput_device *udev = file->private_data;
282 if (!test_bit(UIST_CREATED, &(udev->state)))
283 return 0;
285 poll_wait(file, &udev->waitq, wait);
287 if (udev->head != udev->tail)
288 return POLLIN | POLLRDNORM;
290 return 0;
293 static int uinput_burn_device(struct uinput_device *udev)
295 if (test_bit(UIST_CREATED, &(udev->state)))
296 uinput_destroy_device(udev);
298 kfree(udev->dev);
299 kfree(udev);
301 return 0;
304 static int uinput_close(struct inode *inode, struct file *file)
306 return uinput_burn_device((struct uinput_device *)file->private_data);
309 static int uinput_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
311 int retval = 0;
312 struct uinput_device *udev;
314 udev = (struct uinput_device *)file->private_data;
316 /* device attributes can not be changed after the device is created */
317 if (cmd >= UI_SET_EVBIT && test_bit(UIST_CREATED, &(udev->state)))
318 return -EINVAL;
320 switch (cmd) {
321 case UI_DEV_CREATE:
322 retval = uinput_create_device(udev);
323 break;
325 case UI_DEV_DESTROY:
326 retval = uinput_destroy_device(udev);
327 break;
329 case UI_SET_EVBIT:
330 if (arg > EV_MAX) {
331 retval = -EINVAL;
332 break;
334 set_bit(arg, udev->dev->evbit);
335 break;
337 case UI_SET_KEYBIT:
338 if (arg > KEY_MAX) {
339 retval = -EINVAL;
340 break;
342 set_bit(arg, udev->dev->keybit);
343 break;
345 case UI_SET_RELBIT:
346 if (arg > REL_MAX) {
347 retval = -EINVAL;
348 break;
350 set_bit(arg, udev->dev->relbit);
351 break;
353 case UI_SET_ABSBIT:
354 if (arg > ABS_MAX) {
355 retval = -EINVAL;
356 break;
358 set_bit(arg, udev->dev->absbit);
359 break;
361 case UI_SET_MSCBIT:
362 if (arg > MSC_MAX) {
363 retval = -EINVAL;
364 break;
366 set_bit(arg, udev->dev->mscbit);
367 break;
369 case UI_SET_LEDBIT:
370 if (arg > LED_MAX) {
371 retval = -EINVAL;
372 break;
374 set_bit(arg, udev->dev->ledbit);
375 break;
377 case UI_SET_SNDBIT:
378 if (arg > SND_MAX) {
379 retval = -EINVAL;
380 break;
382 set_bit(arg, udev->dev->sndbit);
383 break;
385 case UI_SET_FFBIT:
386 if (arg > FF_MAX) {
387 retval = -EINVAL;
388 break;
390 set_bit(arg, udev->dev->ffbit);
391 break;
393 default:
394 retval = -EFAULT;
396 return retval;
399 struct file_operations uinput_fops = {
400 .owner = THIS_MODULE,
401 .open = uinput_open,
402 .release = uinput_close,
403 .read = uinput_read,
404 .write = uinput_write,
405 .poll = uinput_poll,
406 .ioctl = uinput_ioctl,
409 static struct miscdevice uinput_misc = {
410 .fops = &uinput_fops,
411 .minor = UINPUT_MINOR,
412 .name = UINPUT_NAME,
415 static int __init uinput_init(void)
417 return misc_register(&uinput_misc);
420 static void __exit uinput_exit(void)
422 misc_deregister(&uinput_misc);
425 MODULE_AUTHOR("Aristeu Sergio Rozanski Filho");
426 MODULE_DESCRIPTION("User level driver support for input subsystem");
427 MODULE_LICENSE("GPL");
429 module_init(uinput_init);
430 module_exit(uinput_exit);