blob: a56ad4ba8fe2eb482ee4a26f1f36baa56753e1c5 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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:
Anssi Hannulaff462552006-07-19 01:41:09 -040023 * 0.3 09/04/2006 (Anssi Hannula <anssi.hannula@gmail.com>)
24 * - updated ff support for the changes in kernel interface
25 * - added MODULE_VERSION
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 * 0.2 16/10/2004 (Micah Dowty <micah@navi.cx>)
27 * - added force feedback support
28 * - added UI_SET_PHYS
29 * 0.1 20/06/2002
30 * - first public version
31 */
32#include <linux/poll.h>
33#include <linux/slab.h>
34#include <linux/module.h>
35#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/smp_lock.h>
37#include <linux/fs.h>
38#include <linux/miscdevice.h>
39#include <linux/uinput.h>
40
Linus Torvalds1da177e2005-04-16 15:20:36 -070041static int uinput_dev_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
42{
Dmitry Torokhov373f9712007-04-12 01:34:33 -040043 struct uinput_device *udev = input_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
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
Dmitry Torokhov0048e602005-06-30 00:48:14 -050056static int uinput_request_alloc_id(struct uinput_device *udev, struct uinput_request *request)
Linus Torvalds1da177e2005-04-16 15:20:36 -070057{
58 /* Atomically allocate an ID for the given request. Returns 0 on success. */
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 int id;
Dmitry Torokhov152c12f2005-06-30 00:47:50 -050060 int err = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Dmitry Torokhov0048e602005-06-30 00:48:14 -050062 spin_lock(&udev->requests_lock);
Dmitry Torokhov152c12f2005-06-30 00:47:50 -050063
64 for (id = 0; id < UINPUT_NUM_REQUESTS; id++)
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 if (!udev->requests[id]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 request->id = id;
Dmitry Torokhov0048e602005-06-30 00:48:14 -050067 udev->requests[id] = request;
Dmitry Torokhov152c12f2005-06-30 00:47:50 -050068 err = 0;
69 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 }
Dmitry Torokhov152c12f2005-06-30 00:47:50 -050071
Dmitry Torokhov0048e602005-06-30 00:48:14 -050072 spin_unlock(&udev->requests_lock);
Dmitry Torokhov152c12f2005-06-30 00:47:50 -050073 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074}
75
76static 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 return udev->requests[id];
82}
83
Dmitry Torokhov0048e602005-06-30 00:48:14 -050084static inline int uinput_request_reserve_slot(struct uinput_device *udev, struct uinput_request *request)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085{
Dmitry Torokhov0048e602005-06-30 00:48:14 -050086 /* Allocate slot. If none are available right away, wait. */
87 return wait_event_interruptible(udev->requests_waitq,
88 !uinput_request_alloc_id(udev, request));
Linus Torvalds1da177e2005-04-16 15:20:36 -070089}
90
Dmitry Torokhov0048e602005-06-30 00:48:14 -050091static void uinput_request_done(struct uinput_device *udev, struct uinput_request *request)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092{
Dmitry Torokhov0048e602005-06-30 00:48:14 -050093 /* Mark slot as available */
94 udev->requests[request->id] = NULL;
Dmitry Torokhove597f0c82005-11-20 00:51:43 -050095 wake_up(&udev->requests_waitq);
Dmitry Torokhove7507ed2005-10-17 16:43:32 -070096
97 complete(&request->done);
Dmitry Torokhov0048e602005-06-30 00:48:14 -050098}
99
100static int uinput_request_submit(struct input_dev *dev, struct uinput_request *request)
101{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 /* Tell our userspace app about this new request by queueing an input event */
103 uinput_dev_event(dev, EV_UINPUT, request->code, request->id);
104
105 /* Wait for the request to complete */
Dmitry Torokhove597f0c82005-11-20 00:51:43 -0500106 wait_for_completion(&request->done);
107 return request->retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108}
109
Anssi Hannulaff462552006-07-19 01:41:09 -0400110static void uinput_dev_set_gain(struct input_dev *dev, u16 gain)
111{
112 uinput_dev_event(dev, EV_FF, FF_GAIN, gain);
113}
114
115static void uinput_dev_set_autocenter(struct input_dev *dev, u16 magnitude)
116{
117 uinput_dev_event(dev, EV_FF, FF_AUTOCENTER, magnitude);
118}
119
120static int uinput_dev_playback(struct input_dev *dev, int effect_id, int value)
121{
122 return uinput_dev_event(dev, EV_FF, effect_id, value);
123}
124
125static int uinput_dev_upload_effect(struct input_dev *dev, struct ff_effect *effect, struct ff_effect *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126{
127 struct uinput_request request;
Dmitry Torokhov0048e602005-06-30 00:48:14 -0500128 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Dmitry Torokhov0048e602005-06-30 00:48:14 -0500130 request.id = -1;
131 init_completion(&request.done);
132 request.code = UI_FF_UPLOAD;
Anssi Hannulaff462552006-07-19 01:41:09 -0400133 request.u.upload.effect = effect;
134 request.u.upload.old = old;
Dmitry Torokhov0048e602005-06-30 00:48:14 -0500135
Dmitry Torokhov373f9712007-04-12 01:34:33 -0400136 retval = uinput_request_reserve_slot(input_get_drvdata(dev), &request);
Dmitry Torokhov0048e602005-06-30 00:48:14 -0500137 if (!retval)
138 retval = uinput_request_submit(dev, &request);
139
140 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141}
142
143static int uinput_dev_erase_effect(struct input_dev *dev, int effect_id)
144{
145 struct uinput_request request;
Dmitry Torokhov0048e602005-06-30 00:48:14 -0500146 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
148 if (!test_bit(EV_FF, dev->evbit))
149 return -ENOSYS;
150
Dmitry Torokhov0048e602005-06-30 00:48:14 -0500151 request.id = -1;
152 init_completion(&request.done);
153 request.code = UI_FF_ERASE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 request.u.effect_id = effect_id;
Dmitry Torokhov0048e602005-06-30 00:48:14 -0500155
Dmitry Torokhov373f9712007-04-12 01:34:33 -0400156 retval = uinput_request_reserve_slot(input_get_drvdata(dev), &request);
Dmitry Torokhov0048e602005-06-30 00:48:14 -0500157 if (!retval)
158 retval = uinput_request_submit(dev, &request);
159
160 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161}
162
Dmitry Torokhov29506412005-11-20 00:51:22 -0500163static void uinput_destroy_device(struct uinput_device *udev)
164{
165 const char *name, *phys;
166
167 if (udev->dev) {
168 name = udev->dev->name;
169 phys = udev->dev->phys;
170 if (udev->state == UIST_CREATED)
171 input_unregister_device(udev->dev);
172 else
173 input_free_device(udev->dev);
174 kfree(name);
175 kfree(phys);
176 udev->dev = NULL;
177 }
178
179 udev->state = UIST_NEW_DEVICE;
180}
181
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182static int uinput_create_device(struct uinput_device *udev)
183{
Anssi Hannulaff462552006-07-19 01:41:09 -0400184 struct input_dev *dev = udev->dev;
Dmitry Torokhov29506412005-11-20 00:51:22 -0500185 int error;
186
187 if (udev->state != UIST_SETUP_COMPLETE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 printk(KERN_DEBUG "%s: write device info first\n", UINPUT_NAME);
189 return -EINVAL;
190 }
191
Anssi Hannulaff462552006-07-19 01:41:09 -0400192 if (udev->ff_effects_max) {
193 error = input_ff_create(dev, udev->ff_effects_max);
194 if (error)
195 goto fail1;
196
197 dev->ff->upload = uinput_dev_upload_effect;
198 dev->ff->erase = uinput_dev_erase_effect;
199 dev->ff->playback = uinput_dev_playback;
200 dev->ff->set_gain = uinput_dev_set_gain;
201 dev->ff->set_autocenter = uinput_dev_set_autocenter;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 }
203
Anssi Hannulaff462552006-07-19 01:41:09 -0400204 error = input_register_device(udev->dev);
205 if (error)
206 goto fail2;
207
Dmitry Torokhov29506412005-11-20 00:51:22 -0500208 udev->state = UIST_CREATED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
210 return 0;
Anssi Hannulaff462552006-07-19 01:41:09 -0400211
212 fail2: input_ff_destroy(dev);
213 fail1: uinput_destroy_device(udev);
214 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215}
216
217static int uinput_open(struct inode *inode, struct file *file)
218{
Dmitry Torokhov29506412005-11-20 00:51:22 -0500219 struct uinput_device *newdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
Dmitry Torokhov29506412005-11-20 00:51:22 -0500221 newdev = kzalloc(sizeof(struct uinput_device), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 if (!newdev)
Dmitry Torokhov29506412005-11-20 00:51:22 -0500223 return -ENOMEM;
224
Dmitry Torokhov221979a2006-02-19 00:22:36 -0500225 mutex_init(&newdev->mutex);
Dmitry Torokhov0048e602005-06-30 00:48:14 -0500226 spin_lock_init(&newdev->requests_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 init_waitqueue_head(&newdev->requests_waitq);
Dmitry Torokhov29506412005-11-20 00:51:22 -0500228 init_waitqueue_head(&newdev->waitq);
229 newdev->state = UIST_NEW_DEVICE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
231 file->private_data = newdev;
232
233 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234}
235
236static int uinput_validate_absbits(struct input_dev *dev)
237{
238 unsigned int cnt;
239 int retval = 0;
240
241 for (cnt = 0; cnt < ABS_MAX + 1; cnt++) {
242 if (!test_bit(cnt, dev->absbit))
243 continue;
244
245 if ((dev->absmax[cnt] <= dev->absmin[cnt])) {
246 printk(KERN_DEBUG
247 "%s: invalid abs[%02x] min:%d max:%d\n",
248 UINPUT_NAME, cnt,
249 dev->absmin[cnt], dev->absmax[cnt]);
250 retval = -EINVAL;
251 break;
252 }
253
254 if (dev->absflat[cnt] > (dev->absmax[cnt] - dev->absmin[cnt])) {
255 printk(KERN_DEBUG
256 "%s: absflat[%02x] out of range: %d "
257 "(min:%d/max:%d)\n",
258 UINPUT_NAME, cnt, dev->absflat[cnt],
259 dev->absmin[cnt], dev->absmax[cnt]);
260 retval = -EINVAL;
261 break;
262 }
263 }
264 return retval;
265}
266
Dmitry Torokhov29506412005-11-20 00:51:22 -0500267static int uinput_allocate_device(struct uinput_device *udev)
268{
269 udev->dev = input_allocate_device();
270 if (!udev->dev)
271 return -ENOMEM;
272
273 udev->dev->event = uinput_dev_event;
Dmitry Torokhov373f9712007-04-12 01:34:33 -0400274 input_set_drvdata(udev->dev, udev);
Dmitry Torokhov29506412005-11-20 00:51:22 -0500275
276 return 0;
277}
278
279static int uinput_setup_device(struct uinput_device *udev, const char __user *buffer, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280{
281 struct uinput_user_dev *user_dev;
282 struct input_dev *dev;
Dmitry Torokhov5b6271b2005-06-30 00:50:38 -0500283 char *name;
Dmitry Torokhov152c12f2005-06-30 00:47:50 -0500284 int size;
285 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
Dmitry Torokhov29506412005-11-20 00:51:22 -0500287 if (count != sizeof(struct uinput_user_dev))
288 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
Dmitry Torokhov29506412005-11-20 00:51:22 -0500290 if (!udev->dev) {
291 retval = uinput_allocate_device(udev);
292 if (retval)
293 return retval;
294 }
295
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 dev = udev->dev;
297
Dmitry Torokhov152c12f2005-06-30 00:47:50 -0500298 user_dev = kmalloc(sizeof(struct uinput_user_dev), GFP_KERNEL);
Dmitry Torokhov29506412005-11-20 00:51:22 -0500299 if (!user_dev)
300 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
302 if (copy_from_user(user_dev, buffer, sizeof(struct uinput_user_dev))) {
303 retval = -EFAULT;
304 goto exit;
305 }
306
Anssi Hannulaff462552006-07-19 01:41:09 -0400307 udev->ff_effects_max = user_dev->ff_effects_max;
308
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 size = strnlen(user_dev->name, UINPUT_MAX_NAME_SIZE) + 1;
Dmitry Torokhov29506412005-11-20 00:51:22 -0500310 if (!size) {
311 retval = -EINVAL;
312 goto exit;
313 }
314
315 kfree(dev->name);
Dmitry Torokhov5b6271b2005-06-30 00:50:38 -0500316 dev->name = name = kmalloc(size, GFP_KERNEL);
317 if (!name) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 retval = -ENOMEM;
319 goto exit;
320 }
Dmitry Torokhov5b6271b2005-06-30 00:50:38 -0500321 strlcpy(name, user_dev->name, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 dev->id.bustype = user_dev->id.bustype;
324 dev->id.vendor = user_dev->id.vendor;
325 dev->id.product = user_dev->id.product;
326 dev->id.version = user_dev->id.version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
328 size = sizeof(int) * (ABS_MAX + 1);
329 memcpy(dev->absmax, user_dev->absmax, size);
330 memcpy(dev->absmin, user_dev->absmin, size);
331 memcpy(dev->absfuzz, user_dev->absfuzz, size);
332 memcpy(dev->absflat, user_dev->absflat, size);
333
334 /* check if absmin/absmax/absfuzz/absflat are filled as
335 * told in Documentation/input/input-programming.txt */
336 if (test_bit(EV_ABS, dev->evbit)) {
Dmitry Torokhov29506412005-11-20 00:51:22 -0500337 retval = uinput_validate_absbits(dev);
338 if (retval < 0)
339 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 }
341
Dmitry Torokhov29506412005-11-20 00:51:22 -0500342 udev->state = UIST_SETUP_COMPLETE;
343 retval = count;
344
345 exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 kfree(user_dev);
347 return retval;
348}
349
Dmitry Torokhov29506412005-11-20 00:51:22 -0500350static inline ssize_t uinput_inject_event(struct uinput_device *udev, const char __user *buffer, size_t count)
351{
352 struct input_event ev;
353
354 if (count != sizeof(struct input_event))
355 return -EINVAL;
356
357 if (copy_from_user(&ev, buffer, sizeof(struct input_event)))
358 return -EFAULT;
359
360 input_event(udev->dev, ev.type, ev.code, ev.value);
361
362 return sizeof(struct input_event);
363}
364
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365static ssize_t uinput_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos)
366{
367 struct uinput_device *udev = file->private_data;
Dmitry Torokhov29506412005-11-20 00:51:22 -0500368 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
Dmitry Torokhov221979a2006-02-19 00:22:36 -0500370 retval = mutex_lock_interruptible(&udev->mutex);
Dmitry Torokhov29506412005-11-20 00:51:22 -0500371 if (retval)
372 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
Dmitry Torokhov29506412005-11-20 00:51:22 -0500374 retval = udev->state == UIST_CREATED ?
375 uinput_inject_event(udev, buffer, count) :
376 uinput_setup_device(udev, buffer, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
Dmitry Torokhov221979a2006-02-19 00:22:36 -0500378 mutex_unlock(&udev->mutex);
Dmitry Torokhov29506412005-11-20 00:51:22 -0500379
380 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381}
382
383static ssize_t uinput_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos)
384{
385 struct uinput_device *udev = file->private_data;
386 int retval = 0;
387
Dmitry Torokhov29506412005-11-20 00:51:22 -0500388 if (udev->state != UIST_CREATED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 return -ENODEV;
390
Dmitry Torokhov152c12f2005-06-30 00:47:50 -0500391 if (udev->head == udev->tail && (file->f_flags & O_NONBLOCK))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 return -EAGAIN;
393
394 retval = wait_event_interruptible(udev->waitq,
Dmitry Torokhov29506412005-11-20 00:51:22 -0500395 udev->head != udev->tail || udev->state != UIST_CREATED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 if (retval)
397 return retval;
398
Dmitry Torokhov221979a2006-02-19 00:22:36 -0500399 retval = mutex_lock_interruptible(&udev->mutex);
Dmitry Torokhov29506412005-11-20 00:51:22 -0500400 if (retval)
401 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
Dmitry Torokhov29506412005-11-20 00:51:22 -0500403 if (udev->state != UIST_CREATED) {
404 retval = -ENODEV;
405 goto out;
406 }
407
408 while (udev->head != udev->tail && retval + sizeof(struct input_event) <= count) {
409 if (copy_to_user(buffer + retval, &udev->buff[udev->tail], sizeof(struct input_event))) {
410 retval = -EFAULT;
411 goto out;
412 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 udev->tail = (udev->tail + 1) % UINPUT_BUFFER_SIZE;
414 retval += sizeof(struct input_event);
415 }
416
Dmitry Torokhov29506412005-11-20 00:51:22 -0500417 out:
Dmitry Torokhov221979a2006-02-19 00:22:36 -0500418 mutex_unlock(&udev->mutex);
Dmitry Torokhov29506412005-11-20 00:51:22 -0500419
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 return retval;
421}
422
423static unsigned int uinput_poll(struct file *file, poll_table *wait)
424{
425 struct uinput_device *udev = file->private_data;
426
427 poll_wait(file, &udev->waitq, wait);
428
429 if (udev->head != udev->tail)
430 return POLLIN | POLLRDNORM;
431
432 return 0;
433}
434
Dmitry Torokhov29506412005-11-20 00:51:22 -0500435static int uinput_release(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436{
Dmitry Torokhov29506412005-11-20 00:51:22 -0500437 struct uinput_device *udev = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
Dmitry Torokhov29506412005-11-20 00:51:22 -0500439 uinput_destroy_device(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 kfree(udev);
441
442 return 0;
443}
444
Dmitry Torokhov29506412005-11-20 00:51:22 -0500445#define uinput_set_bit(_arg, _bit, _max) \
446({ \
447 int __ret = 0; \
448 if (udev->state == UIST_CREATED) \
449 __ret = -EINVAL; \
450 else if ((_arg) > (_max)) \
451 __ret = -EINVAL; \
452 else set_bit((_arg), udev->dev->_bit); \
453 __ret; \
454})
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
Dmitry Torokhov29506412005-11-20 00:51:22 -0500456static long uinput_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457{
Dmitry Torokhov29506412005-11-20 00:51:22 -0500458 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 struct uinput_device *udev;
460 void __user *p = (void __user *)arg;
461 struct uinput_ff_upload ff_up;
462 struct uinput_ff_erase ff_erase;
463 struct uinput_request *req;
464 int length;
Dmitry Torokhov5b6271b2005-06-30 00:50:38 -0500465 char *phys;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
467 udev = file->private_data;
468
Dmitry Torokhov221979a2006-02-19 00:22:36 -0500469 retval = mutex_lock_interruptible(&udev->mutex);
Dmitry Torokhov29506412005-11-20 00:51:22 -0500470 if (retval)
471 return retval;
472
473 if (!udev->dev) {
474 retval = uinput_allocate_device(udev);
475 if (retval)
476 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 }
478
479 switch (cmd) {
480 case UI_DEV_CREATE:
481 retval = uinput_create_device(udev);
482 break;
483
484 case UI_DEV_DESTROY:
Dmitry Torokhov29506412005-11-20 00:51:22 -0500485 uinput_destroy_device(udev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 break;
487
488 case UI_SET_EVBIT:
Dmitry Torokhov29506412005-11-20 00:51:22 -0500489 retval = uinput_set_bit(arg, evbit, EV_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 break;
491
492 case UI_SET_KEYBIT:
Dmitry Torokhov29506412005-11-20 00:51:22 -0500493 retval = uinput_set_bit(arg, keybit, KEY_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 break;
495
496 case UI_SET_RELBIT:
Dmitry Torokhov29506412005-11-20 00:51:22 -0500497 retval = uinput_set_bit(arg, relbit, REL_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 break;
499
500 case UI_SET_ABSBIT:
Dmitry Torokhov29506412005-11-20 00:51:22 -0500501 retval = uinput_set_bit(arg, absbit, ABS_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 break;
503
504 case UI_SET_MSCBIT:
Dmitry Torokhov29506412005-11-20 00:51:22 -0500505 retval = uinput_set_bit(arg, mscbit, MSC_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 break;
507
508 case UI_SET_LEDBIT:
Dmitry Torokhov29506412005-11-20 00:51:22 -0500509 retval = uinput_set_bit(arg, ledbit, LED_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 break;
511
512 case UI_SET_SNDBIT:
Dmitry Torokhov29506412005-11-20 00:51:22 -0500513 retval = uinput_set_bit(arg, sndbit, SND_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 break;
515
516 case UI_SET_FFBIT:
Dmitry Torokhov29506412005-11-20 00:51:22 -0500517 retval = uinput_set_bit(arg, ffbit, FF_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 break;
519
Dmitry Torokhov59c7c032005-11-20 00:51:33 -0500520 case UI_SET_SWBIT:
521 retval = uinput_set_bit(arg, swbit, SW_MAX);
522 break;
523
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 case UI_SET_PHYS:
Dmitry Torokhov29506412005-11-20 00:51:22 -0500525 if (udev->state == UIST_CREATED) {
526 retval = -EINVAL;
527 goto out;
528 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 length = strnlen_user(p, 1024);
530 if (length <= 0) {
531 retval = -EFAULT;
532 break;
533 }
Dmitry Torokhov5b6271b2005-06-30 00:50:38 -0500534 kfree(udev->dev->phys);
535 udev->dev->phys = phys = kmalloc(length, GFP_KERNEL);
536 if (!phys) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 retval = -ENOMEM;
538 break;
539 }
Dmitry Torokhov5b6271b2005-06-30 00:50:38 -0500540 if (copy_from_user(phys, p, length)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 udev->dev->phys = NULL;
Dmitry Torokhov5b6271b2005-06-30 00:50:38 -0500542 kfree(phys);
543 retval = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 break;
545 }
Dmitry Torokhov5b6271b2005-06-30 00:50:38 -0500546 phys[length - 1] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 break;
548
549 case UI_BEGIN_FF_UPLOAD:
550 if (copy_from_user(&ff_up, p, sizeof(ff_up))) {
551 retval = -EFAULT;
552 break;
553 }
554 req = uinput_request_find(udev, ff_up.request_id);
Anssi Hannulaff462552006-07-19 01:41:09 -0400555 if (!(req && req->code == UI_FF_UPLOAD && req->u.upload.effect)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 retval = -EINVAL;
557 break;
558 }
559 ff_up.retval = 0;
Anssi Hannulaff462552006-07-19 01:41:09 -0400560 memcpy(&ff_up.effect, req->u.upload.effect, sizeof(struct ff_effect));
561 if (req->u.upload.old)
562 memcpy(&ff_up.old, req->u.upload.old, sizeof(struct ff_effect));
563 else
564 memset(&ff_up.old, 0, sizeof(struct ff_effect));
565
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 if (copy_to_user(p, &ff_up, sizeof(ff_up))) {
567 retval = -EFAULT;
568 break;
569 }
570 break;
571
572 case UI_BEGIN_FF_ERASE:
573 if (copy_from_user(&ff_erase, p, sizeof(ff_erase))) {
574 retval = -EFAULT;
575 break;
576 }
577 req = uinput_request_find(udev, ff_erase.request_id);
Dmitry Torokhov152c12f2005-06-30 00:47:50 -0500578 if (!(req && req->code == UI_FF_ERASE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 retval = -EINVAL;
580 break;
581 }
582 ff_erase.retval = 0;
583 ff_erase.effect_id = req->u.effect_id;
584 if (copy_to_user(p, &ff_erase, sizeof(ff_erase))) {
585 retval = -EFAULT;
586 break;
587 }
588 break;
589
590 case UI_END_FF_UPLOAD:
591 if (copy_from_user(&ff_up, p, sizeof(ff_up))) {
592 retval = -EFAULT;
593 break;
594 }
595 req = uinput_request_find(udev, ff_up.request_id);
Anssi Hannulaff462552006-07-19 01:41:09 -0400596 if (!(req && req->code == UI_FF_UPLOAD && req->u.upload.effect)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 retval = -EINVAL;
598 break;
599 }
600 req->retval = ff_up.retval;
Dmitry Torokhov0048e602005-06-30 00:48:14 -0500601 uinput_request_done(udev, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 break;
603
604 case UI_END_FF_ERASE:
605 if (copy_from_user(&ff_erase, p, sizeof(ff_erase))) {
606 retval = -EFAULT;
607 break;
608 }
609 req = uinput_request_find(udev, ff_erase.request_id);
Dmitry Torokhov152c12f2005-06-30 00:47:50 -0500610 if (!(req && req->code == UI_FF_ERASE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 retval = -EINVAL;
612 break;
613 }
614 req->retval = ff_erase.retval;
Dmitry Torokhov0048e602005-06-30 00:48:14 -0500615 uinput_request_done(udev, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 break;
617
618 default:
619 retval = -EINVAL;
620 }
Dmitry Torokhov29506412005-11-20 00:51:22 -0500621
622 out:
Dmitry Torokhov221979a2006-02-19 00:22:36 -0500623 mutex_unlock(&udev->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 return retval;
625}
626
Arjan van de Ven2b8693c2007-02-12 00:55:32 -0800627static const struct file_operations uinput_fops = {
Dmitry Torokhov29506412005-11-20 00:51:22 -0500628 .owner = THIS_MODULE,
629 .open = uinput_open,
630 .release = uinput_release,
631 .read = uinput_read,
632 .write = uinput_write,
633 .poll = uinput_poll,
634 .unlocked_ioctl = uinput_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635};
636
637static struct miscdevice uinput_misc = {
Dmitry Torokhov29506412005-11-20 00:51:22 -0500638 .fops = &uinput_fops,
639 .minor = UINPUT_MINOR,
640 .name = UINPUT_NAME,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641};
642
643static int __init uinput_init(void)
644{
645 return misc_register(&uinput_misc);
646}
647
648static void __exit uinput_exit(void)
649{
650 misc_deregister(&uinput_misc);
651}
652
653MODULE_AUTHOR("Aristeu Sergio Rozanski Filho");
654MODULE_DESCRIPTION("User level driver support for input subsystem");
655MODULE_LICENSE("GPL");
Anssi Hannulaff462552006-07-19 01:41:09 -0400656MODULE_VERSION("0.3");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
658module_init(uinput_init);
659module_exit(uinput_exit);
660