Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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: |
Anssi Hannula | ff46255 | 2006-07-19 01:41:09 -0400 | [diff] [blame] | 23 | * 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | * 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> |
| 36 | #include <linux/input.h> |
| 37 | #include <linux/smp_lock.h> |
| 38 | #include <linux/fs.h> |
| 39 | #include <linux/miscdevice.h> |
| 40 | #include <linux/uinput.h> |
| 41 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | static int uinput_dev_event(struct input_dev *dev, unsigned int type, unsigned int code, int value) |
| 43 | { |
Dmitry Torokhov | 373f971 | 2007-04-12 01:34:33 -0400 | [diff] [blame^] | 44 | struct uinput_device *udev = input_get_drvdata(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | |
| 46 | udev->buff[udev->head].type = type; |
| 47 | udev->buff[udev->head].code = code; |
| 48 | udev->buff[udev->head].value = value; |
| 49 | do_gettimeofday(&udev->buff[udev->head].time); |
| 50 | udev->head = (udev->head + 1) % UINPUT_BUFFER_SIZE; |
| 51 | |
| 52 | wake_up_interruptible(&udev->waitq); |
| 53 | |
| 54 | return 0; |
| 55 | } |
| 56 | |
Dmitry Torokhov | 0048e60 | 2005-06-30 00:48:14 -0500 | [diff] [blame] | 57 | static int uinput_request_alloc_id(struct uinput_device *udev, struct uinput_request *request) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | { |
| 59 | /* Atomically allocate an ID for the given request. Returns 0 on success. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | int id; |
Dmitry Torokhov | 152c12f | 2005-06-30 00:47:50 -0500 | [diff] [blame] | 61 | int err = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | |
Dmitry Torokhov | 0048e60 | 2005-06-30 00:48:14 -0500 | [diff] [blame] | 63 | spin_lock(&udev->requests_lock); |
Dmitry Torokhov | 152c12f | 2005-06-30 00:47:50 -0500 | [diff] [blame] | 64 | |
| 65 | for (id = 0; id < UINPUT_NUM_REQUESTS; id++) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | if (!udev->requests[id]) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | request->id = id; |
Dmitry Torokhov | 0048e60 | 2005-06-30 00:48:14 -0500 | [diff] [blame] | 68 | udev->requests[id] = request; |
Dmitry Torokhov | 152c12f | 2005-06-30 00:47:50 -0500 | [diff] [blame] | 69 | err = 0; |
| 70 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | } |
Dmitry Torokhov | 152c12f | 2005-06-30 00:47:50 -0500 | [diff] [blame] | 72 | |
Dmitry Torokhov | 0048e60 | 2005-06-30 00:48:14 -0500 | [diff] [blame] | 73 | spin_unlock(&udev->requests_lock); |
Dmitry Torokhov | 152c12f | 2005-06-30 00:47:50 -0500 | [diff] [blame] | 74 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | static struct uinput_request* uinput_request_find(struct uinput_device *udev, int id) |
| 78 | { |
| 79 | /* Find an input request, by ID. Returns NULL if the ID isn't valid. */ |
| 80 | if (id >= UINPUT_NUM_REQUESTS || id < 0) |
| 81 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | return udev->requests[id]; |
| 83 | } |
| 84 | |
Dmitry Torokhov | 0048e60 | 2005-06-30 00:48:14 -0500 | [diff] [blame] | 85 | static inline int uinput_request_reserve_slot(struct uinput_device *udev, struct uinput_request *request) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | { |
Dmitry Torokhov | 0048e60 | 2005-06-30 00:48:14 -0500 | [diff] [blame] | 87 | /* Allocate slot. If none are available right away, wait. */ |
| 88 | return wait_event_interruptible(udev->requests_waitq, |
| 89 | !uinput_request_alloc_id(udev, request)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Dmitry Torokhov | 0048e60 | 2005-06-30 00:48:14 -0500 | [diff] [blame] | 92 | static void uinput_request_done(struct uinput_device *udev, struct uinput_request *request) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | { |
Dmitry Torokhov | 0048e60 | 2005-06-30 00:48:14 -0500 | [diff] [blame] | 94 | /* Mark slot as available */ |
| 95 | udev->requests[request->id] = NULL; |
Dmitry Torokhov | e597f0c8 | 2005-11-20 00:51:43 -0500 | [diff] [blame] | 96 | wake_up(&udev->requests_waitq); |
Dmitry Torokhov | e7507ed | 2005-10-17 16:43:32 -0700 | [diff] [blame] | 97 | |
| 98 | complete(&request->done); |
Dmitry Torokhov | 0048e60 | 2005-06-30 00:48:14 -0500 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | static int uinput_request_submit(struct input_dev *dev, struct uinput_request *request) |
| 102 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | /* Tell our userspace app about this new request by queueing an input event */ |
| 104 | uinput_dev_event(dev, EV_UINPUT, request->code, request->id); |
| 105 | |
| 106 | /* Wait for the request to complete */ |
Dmitry Torokhov | e597f0c8 | 2005-11-20 00:51:43 -0500 | [diff] [blame] | 107 | wait_for_completion(&request->done); |
| 108 | return request->retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | } |
| 110 | |
Anssi Hannula | ff46255 | 2006-07-19 01:41:09 -0400 | [diff] [blame] | 111 | static void uinput_dev_set_gain(struct input_dev *dev, u16 gain) |
| 112 | { |
| 113 | uinput_dev_event(dev, EV_FF, FF_GAIN, gain); |
| 114 | } |
| 115 | |
| 116 | static void uinput_dev_set_autocenter(struct input_dev *dev, u16 magnitude) |
| 117 | { |
| 118 | uinput_dev_event(dev, EV_FF, FF_AUTOCENTER, magnitude); |
| 119 | } |
| 120 | |
| 121 | static int uinput_dev_playback(struct input_dev *dev, int effect_id, int value) |
| 122 | { |
| 123 | return uinput_dev_event(dev, EV_FF, effect_id, value); |
| 124 | } |
| 125 | |
| 126 | static int uinput_dev_upload_effect(struct input_dev *dev, struct ff_effect *effect, struct ff_effect *old) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | { |
| 128 | struct uinput_request request; |
Dmitry Torokhov | 0048e60 | 2005-06-30 00:48:14 -0500 | [diff] [blame] | 129 | int retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | |
Dmitry Torokhov | 0048e60 | 2005-06-30 00:48:14 -0500 | [diff] [blame] | 131 | request.id = -1; |
| 132 | init_completion(&request.done); |
| 133 | request.code = UI_FF_UPLOAD; |
Anssi Hannula | ff46255 | 2006-07-19 01:41:09 -0400 | [diff] [blame] | 134 | request.u.upload.effect = effect; |
| 135 | request.u.upload.old = old; |
Dmitry Torokhov | 0048e60 | 2005-06-30 00:48:14 -0500 | [diff] [blame] | 136 | |
Dmitry Torokhov | 373f971 | 2007-04-12 01:34:33 -0400 | [diff] [blame^] | 137 | retval = uinput_request_reserve_slot(input_get_drvdata(dev), &request); |
Dmitry Torokhov | 0048e60 | 2005-06-30 00:48:14 -0500 | [diff] [blame] | 138 | if (!retval) |
| 139 | retval = uinput_request_submit(dev, &request); |
| 140 | |
| 141 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | static int uinput_dev_erase_effect(struct input_dev *dev, int effect_id) |
| 145 | { |
| 146 | struct uinput_request request; |
Dmitry Torokhov | 0048e60 | 2005-06-30 00:48:14 -0500 | [diff] [blame] | 147 | int retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | |
| 149 | if (!test_bit(EV_FF, dev->evbit)) |
| 150 | return -ENOSYS; |
| 151 | |
Dmitry Torokhov | 0048e60 | 2005-06-30 00:48:14 -0500 | [diff] [blame] | 152 | request.id = -1; |
| 153 | init_completion(&request.done); |
| 154 | request.code = UI_FF_ERASE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | request.u.effect_id = effect_id; |
Dmitry Torokhov | 0048e60 | 2005-06-30 00:48:14 -0500 | [diff] [blame] | 156 | |
Dmitry Torokhov | 373f971 | 2007-04-12 01:34:33 -0400 | [diff] [blame^] | 157 | retval = uinput_request_reserve_slot(input_get_drvdata(dev), &request); |
Dmitry Torokhov | 0048e60 | 2005-06-30 00:48:14 -0500 | [diff] [blame] | 158 | if (!retval) |
| 159 | retval = uinput_request_submit(dev, &request); |
| 160 | |
| 161 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | } |
| 163 | |
Dmitry Torokhov | 2950641 | 2005-11-20 00:51:22 -0500 | [diff] [blame] | 164 | static void uinput_destroy_device(struct uinput_device *udev) |
| 165 | { |
| 166 | const char *name, *phys; |
| 167 | |
| 168 | if (udev->dev) { |
| 169 | name = udev->dev->name; |
| 170 | phys = udev->dev->phys; |
| 171 | if (udev->state == UIST_CREATED) |
| 172 | input_unregister_device(udev->dev); |
| 173 | else |
| 174 | input_free_device(udev->dev); |
| 175 | kfree(name); |
| 176 | kfree(phys); |
| 177 | udev->dev = NULL; |
| 178 | } |
| 179 | |
| 180 | udev->state = UIST_NEW_DEVICE; |
| 181 | } |
| 182 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | static int uinput_create_device(struct uinput_device *udev) |
| 184 | { |
Anssi Hannula | ff46255 | 2006-07-19 01:41:09 -0400 | [diff] [blame] | 185 | struct input_dev *dev = udev->dev; |
Dmitry Torokhov | 2950641 | 2005-11-20 00:51:22 -0500 | [diff] [blame] | 186 | int error; |
| 187 | |
| 188 | if (udev->state != UIST_SETUP_COMPLETE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | printk(KERN_DEBUG "%s: write device info first\n", UINPUT_NAME); |
| 190 | return -EINVAL; |
| 191 | } |
| 192 | |
Anssi Hannula | ff46255 | 2006-07-19 01:41:09 -0400 | [diff] [blame] | 193 | if (udev->ff_effects_max) { |
| 194 | error = input_ff_create(dev, udev->ff_effects_max); |
| 195 | if (error) |
| 196 | goto fail1; |
| 197 | |
| 198 | dev->ff->upload = uinput_dev_upload_effect; |
| 199 | dev->ff->erase = uinput_dev_erase_effect; |
| 200 | dev->ff->playback = uinput_dev_playback; |
| 201 | dev->ff->set_gain = uinput_dev_set_gain; |
| 202 | dev->ff->set_autocenter = uinput_dev_set_autocenter; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | } |
| 204 | |
Anssi Hannula | ff46255 | 2006-07-19 01:41:09 -0400 | [diff] [blame] | 205 | error = input_register_device(udev->dev); |
| 206 | if (error) |
| 207 | goto fail2; |
| 208 | |
Dmitry Torokhov | 2950641 | 2005-11-20 00:51:22 -0500 | [diff] [blame] | 209 | udev->state = UIST_CREATED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | |
| 211 | return 0; |
Anssi Hannula | ff46255 | 2006-07-19 01:41:09 -0400 | [diff] [blame] | 212 | |
| 213 | fail2: input_ff_destroy(dev); |
| 214 | fail1: uinput_destroy_device(udev); |
| 215 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | static int uinput_open(struct inode *inode, struct file *file) |
| 219 | { |
Dmitry Torokhov | 2950641 | 2005-11-20 00:51:22 -0500 | [diff] [blame] | 220 | struct uinput_device *newdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | |
Dmitry Torokhov | 2950641 | 2005-11-20 00:51:22 -0500 | [diff] [blame] | 222 | newdev = kzalloc(sizeof(struct uinput_device), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | if (!newdev) |
Dmitry Torokhov | 2950641 | 2005-11-20 00:51:22 -0500 | [diff] [blame] | 224 | return -ENOMEM; |
| 225 | |
Dmitry Torokhov | 221979a | 2006-02-19 00:22:36 -0500 | [diff] [blame] | 226 | mutex_init(&newdev->mutex); |
Dmitry Torokhov | 0048e60 | 2005-06-30 00:48:14 -0500 | [diff] [blame] | 227 | spin_lock_init(&newdev->requests_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | init_waitqueue_head(&newdev->requests_waitq); |
Dmitry Torokhov | 2950641 | 2005-11-20 00:51:22 -0500 | [diff] [blame] | 229 | init_waitqueue_head(&newdev->waitq); |
| 230 | newdev->state = UIST_NEW_DEVICE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | |
| 232 | file->private_data = newdev; |
| 233 | |
| 234 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | static int uinput_validate_absbits(struct input_dev *dev) |
| 238 | { |
| 239 | unsigned int cnt; |
| 240 | int retval = 0; |
| 241 | |
| 242 | for (cnt = 0; cnt < ABS_MAX + 1; cnt++) { |
| 243 | if (!test_bit(cnt, dev->absbit)) |
| 244 | continue; |
| 245 | |
| 246 | if ((dev->absmax[cnt] <= dev->absmin[cnt])) { |
| 247 | printk(KERN_DEBUG |
| 248 | "%s: invalid abs[%02x] min:%d max:%d\n", |
| 249 | UINPUT_NAME, cnt, |
| 250 | dev->absmin[cnt], dev->absmax[cnt]); |
| 251 | retval = -EINVAL; |
| 252 | break; |
| 253 | } |
| 254 | |
| 255 | if (dev->absflat[cnt] > (dev->absmax[cnt] - dev->absmin[cnt])) { |
| 256 | printk(KERN_DEBUG |
| 257 | "%s: absflat[%02x] out of range: %d " |
| 258 | "(min:%d/max:%d)\n", |
| 259 | UINPUT_NAME, cnt, dev->absflat[cnt], |
| 260 | dev->absmin[cnt], dev->absmax[cnt]); |
| 261 | retval = -EINVAL; |
| 262 | break; |
| 263 | } |
| 264 | } |
| 265 | return retval; |
| 266 | } |
| 267 | |
Dmitry Torokhov | 2950641 | 2005-11-20 00:51:22 -0500 | [diff] [blame] | 268 | static int uinput_allocate_device(struct uinput_device *udev) |
| 269 | { |
| 270 | udev->dev = input_allocate_device(); |
| 271 | if (!udev->dev) |
| 272 | return -ENOMEM; |
| 273 | |
| 274 | udev->dev->event = uinput_dev_event; |
Dmitry Torokhov | 373f971 | 2007-04-12 01:34:33 -0400 | [diff] [blame^] | 275 | input_set_drvdata(udev->dev, udev); |
Dmitry Torokhov | 2950641 | 2005-11-20 00:51:22 -0500 | [diff] [blame] | 276 | |
| 277 | return 0; |
| 278 | } |
| 279 | |
| 280 | static int uinput_setup_device(struct uinput_device *udev, const char __user *buffer, size_t count) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | { |
| 282 | struct uinput_user_dev *user_dev; |
| 283 | struct input_dev *dev; |
Dmitry Torokhov | 5b6271b | 2005-06-30 00:50:38 -0500 | [diff] [blame] | 284 | char *name; |
Dmitry Torokhov | 152c12f | 2005-06-30 00:47:50 -0500 | [diff] [blame] | 285 | int size; |
| 286 | int retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | |
Dmitry Torokhov | 2950641 | 2005-11-20 00:51:22 -0500 | [diff] [blame] | 288 | if (count != sizeof(struct uinput_user_dev)) |
| 289 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | |
Dmitry Torokhov | 2950641 | 2005-11-20 00:51:22 -0500 | [diff] [blame] | 291 | if (!udev->dev) { |
| 292 | retval = uinput_allocate_device(udev); |
| 293 | if (retval) |
| 294 | return retval; |
| 295 | } |
| 296 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | dev = udev->dev; |
| 298 | |
Dmitry Torokhov | 152c12f | 2005-06-30 00:47:50 -0500 | [diff] [blame] | 299 | user_dev = kmalloc(sizeof(struct uinput_user_dev), GFP_KERNEL); |
Dmitry Torokhov | 2950641 | 2005-11-20 00:51:22 -0500 | [diff] [blame] | 300 | if (!user_dev) |
| 301 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | |
| 303 | if (copy_from_user(user_dev, buffer, sizeof(struct uinput_user_dev))) { |
| 304 | retval = -EFAULT; |
| 305 | goto exit; |
| 306 | } |
| 307 | |
Anssi Hannula | ff46255 | 2006-07-19 01:41:09 -0400 | [diff] [blame] | 308 | udev->ff_effects_max = user_dev->ff_effects_max; |
| 309 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | size = strnlen(user_dev->name, UINPUT_MAX_NAME_SIZE) + 1; |
Dmitry Torokhov | 2950641 | 2005-11-20 00:51:22 -0500 | [diff] [blame] | 311 | if (!size) { |
| 312 | retval = -EINVAL; |
| 313 | goto exit; |
| 314 | } |
| 315 | |
| 316 | kfree(dev->name); |
Dmitry Torokhov | 5b6271b | 2005-06-30 00:50:38 -0500 | [diff] [blame] | 317 | dev->name = name = kmalloc(size, GFP_KERNEL); |
| 318 | if (!name) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | retval = -ENOMEM; |
| 320 | goto exit; |
| 321 | } |
Dmitry Torokhov | 5b6271b | 2005-06-30 00:50:38 -0500 | [diff] [blame] | 322 | strlcpy(name, user_dev->name, size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | dev->id.bustype = user_dev->id.bustype; |
| 325 | dev->id.vendor = user_dev->id.vendor; |
| 326 | dev->id.product = user_dev->id.product; |
| 327 | dev->id.version = user_dev->id.version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | |
| 329 | size = sizeof(int) * (ABS_MAX + 1); |
| 330 | memcpy(dev->absmax, user_dev->absmax, size); |
| 331 | memcpy(dev->absmin, user_dev->absmin, size); |
| 332 | memcpy(dev->absfuzz, user_dev->absfuzz, size); |
| 333 | memcpy(dev->absflat, user_dev->absflat, size); |
| 334 | |
| 335 | /* check if absmin/absmax/absfuzz/absflat are filled as |
| 336 | * told in Documentation/input/input-programming.txt */ |
| 337 | if (test_bit(EV_ABS, dev->evbit)) { |
Dmitry Torokhov | 2950641 | 2005-11-20 00:51:22 -0500 | [diff] [blame] | 338 | retval = uinput_validate_absbits(dev); |
| 339 | if (retval < 0) |
| 340 | goto exit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | } |
| 342 | |
Dmitry Torokhov | 2950641 | 2005-11-20 00:51:22 -0500 | [diff] [blame] | 343 | udev->state = UIST_SETUP_COMPLETE; |
| 344 | retval = count; |
| 345 | |
| 346 | exit: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | kfree(user_dev); |
| 348 | return retval; |
| 349 | } |
| 350 | |
Dmitry Torokhov | 2950641 | 2005-11-20 00:51:22 -0500 | [diff] [blame] | 351 | static inline ssize_t uinput_inject_event(struct uinput_device *udev, const char __user *buffer, size_t count) |
| 352 | { |
| 353 | struct input_event ev; |
| 354 | |
| 355 | if (count != sizeof(struct input_event)) |
| 356 | return -EINVAL; |
| 357 | |
| 358 | if (copy_from_user(&ev, buffer, sizeof(struct input_event))) |
| 359 | return -EFAULT; |
| 360 | |
| 361 | input_event(udev->dev, ev.type, ev.code, ev.value); |
| 362 | |
| 363 | return sizeof(struct input_event); |
| 364 | } |
| 365 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | static ssize_t uinput_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos) |
| 367 | { |
| 368 | struct uinput_device *udev = file->private_data; |
Dmitry Torokhov | 2950641 | 2005-11-20 00:51:22 -0500 | [diff] [blame] | 369 | int retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | |
Dmitry Torokhov | 221979a | 2006-02-19 00:22:36 -0500 | [diff] [blame] | 371 | retval = mutex_lock_interruptible(&udev->mutex); |
Dmitry Torokhov | 2950641 | 2005-11-20 00:51:22 -0500 | [diff] [blame] | 372 | if (retval) |
| 373 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | |
Dmitry Torokhov | 2950641 | 2005-11-20 00:51:22 -0500 | [diff] [blame] | 375 | retval = udev->state == UIST_CREATED ? |
| 376 | uinput_inject_event(udev, buffer, count) : |
| 377 | uinput_setup_device(udev, buffer, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | |
Dmitry Torokhov | 221979a | 2006-02-19 00:22:36 -0500 | [diff] [blame] | 379 | mutex_unlock(&udev->mutex); |
Dmitry Torokhov | 2950641 | 2005-11-20 00:51:22 -0500 | [diff] [blame] | 380 | |
| 381 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | static ssize_t uinput_read(struct file *file, char __user *buffer, size_t count, loff_t *ppos) |
| 385 | { |
| 386 | struct uinput_device *udev = file->private_data; |
| 387 | int retval = 0; |
| 388 | |
Dmitry Torokhov | 2950641 | 2005-11-20 00:51:22 -0500 | [diff] [blame] | 389 | if (udev->state != UIST_CREATED) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | return -ENODEV; |
| 391 | |
Dmitry Torokhov | 152c12f | 2005-06-30 00:47:50 -0500 | [diff] [blame] | 392 | if (udev->head == udev->tail && (file->f_flags & O_NONBLOCK)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | return -EAGAIN; |
| 394 | |
| 395 | retval = wait_event_interruptible(udev->waitq, |
Dmitry Torokhov | 2950641 | 2005-11-20 00:51:22 -0500 | [diff] [blame] | 396 | udev->head != udev->tail || udev->state != UIST_CREATED); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | if (retval) |
| 398 | return retval; |
| 399 | |
Dmitry Torokhov | 221979a | 2006-02-19 00:22:36 -0500 | [diff] [blame] | 400 | retval = mutex_lock_interruptible(&udev->mutex); |
Dmitry Torokhov | 2950641 | 2005-11-20 00:51:22 -0500 | [diff] [blame] | 401 | if (retval) |
| 402 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | |
Dmitry Torokhov | 2950641 | 2005-11-20 00:51:22 -0500 | [diff] [blame] | 404 | if (udev->state != UIST_CREATED) { |
| 405 | retval = -ENODEV; |
| 406 | goto out; |
| 407 | } |
| 408 | |
| 409 | while (udev->head != udev->tail && retval + sizeof(struct input_event) <= count) { |
| 410 | if (copy_to_user(buffer + retval, &udev->buff[udev->tail], sizeof(struct input_event))) { |
| 411 | retval = -EFAULT; |
| 412 | goto out; |
| 413 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | udev->tail = (udev->tail + 1) % UINPUT_BUFFER_SIZE; |
| 415 | retval += sizeof(struct input_event); |
| 416 | } |
| 417 | |
Dmitry Torokhov | 2950641 | 2005-11-20 00:51:22 -0500 | [diff] [blame] | 418 | out: |
Dmitry Torokhov | 221979a | 2006-02-19 00:22:36 -0500 | [diff] [blame] | 419 | mutex_unlock(&udev->mutex); |
Dmitry Torokhov | 2950641 | 2005-11-20 00:51:22 -0500 | [diff] [blame] | 420 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | return retval; |
| 422 | } |
| 423 | |
| 424 | static unsigned int uinput_poll(struct file *file, poll_table *wait) |
| 425 | { |
| 426 | struct uinput_device *udev = file->private_data; |
| 427 | |
| 428 | poll_wait(file, &udev->waitq, wait); |
| 429 | |
| 430 | if (udev->head != udev->tail) |
| 431 | return POLLIN | POLLRDNORM; |
| 432 | |
| 433 | return 0; |
| 434 | } |
| 435 | |
Dmitry Torokhov | 2950641 | 2005-11-20 00:51:22 -0500 | [diff] [blame] | 436 | static int uinput_release(struct inode *inode, struct file *file) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | { |
Dmitry Torokhov | 2950641 | 2005-11-20 00:51:22 -0500 | [diff] [blame] | 438 | struct uinput_device *udev = file->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | |
Dmitry Torokhov | 2950641 | 2005-11-20 00:51:22 -0500 | [diff] [blame] | 440 | uinput_destroy_device(udev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | kfree(udev); |
| 442 | |
| 443 | return 0; |
| 444 | } |
| 445 | |
Dmitry Torokhov | 2950641 | 2005-11-20 00:51:22 -0500 | [diff] [blame] | 446 | #define uinput_set_bit(_arg, _bit, _max) \ |
| 447 | ({ \ |
| 448 | int __ret = 0; \ |
| 449 | if (udev->state == UIST_CREATED) \ |
| 450 | __ret = -EINVAL; \ |
| 451 | else if ((_arg) > (_max)) \ |
| 452 | __ret = -EINVAL; \ |
| 453 | else set_bit((_arg), udev->dev->_bit); \ |
| 454 | __ret; \ |
| 455 | }) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | |
Dmitry Torokhov | 2950641 | 2005-11-20 00:51:22 -0500 | [diff] [blame] | 457 | static long uinput_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | { |
Dmitry Torokhov | 2950641 | 2005-11-20 00:51:22 -0500 | [diff] [blame] | 459 | int retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | struct uinput_device *udev; |
| 461 | void __user *p = (void __user *)arg; |
| 462 | struct uinput_ff_upload ff_up; |
| 463 | struct uinput_ff_erase ff_erase; |
| 464 | struct uinput_request *req; |
| 465 | int length; |
Dmitry Torokhov | 5b6271b | 2005-06-30 00:50:38 -0500 | [diff] [blame] | 466 | char *phys; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | |
| 468 | udev = file->private_data; |
| 469 | |
Dmitry Torokhov | 221979a | 2006-02-19 00:22:36 -0500 | [diff] [blame] | 470 | retval = mutex_lock_interruptible(&udev->mutex); |
Dmitry Torokhov | 2950641 | 2005-11-20 00:51:22 -0500 | [diff] [blame] | 471 | if (retval) |
| 472 | return retval; |
| 473 | |
| 474 | if (!udev->dev) { |
| 475 | retval = uinput_allocate_device(udev); |
| 476 | if (retval) |
| 477 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | } |
| 479 | |
| 480 | switch (cmd) { |
| 481 | case UI_DEV_CREATE: |
| 482 | retval = uinput_create_device(udev); |
| 483 | break; |
| 484 | |
| 485 | case UI_DEV_DESTROY: |
Dmitry Torokhov | 2950641 | 2005-11-20 00:51:22 -0500 | [diff] [blame] | 486 | uinput_destroy_device(udev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | break; |
| 488 | |
| 489 | case UI_SET_EVBIT: |
Dmitry Torokhov | 2950641 | 2005-11-20 00:51:22 -0500 | [diff] [blame] | 490 | retval = uinput_set_bit(arg, evbit, EV_MAX); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | break; |
| 492 | |
| 493 | case UI_SET_KEYBIT: |
Dmitry Torokhov | 2950641 | 2005-11-20 00:51:22 -0500 | [diff] [blame] | 494 | retval = uinput_set_bit(arg, keybit, KEY_MAX); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | break; |
| 496 | |
| 497 | case UI_SET_RELBIT: |
Dmitry Torokhov | 2950641 | 2005-11-20 00:51:22 -0500 | [diff] [blame] | 498 | retval = uinput_set_bit(arg, relbit, REL_MAX); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 499 | break; |
| 500 | |
| 501 | case UI_SET_ABSBIT: |
Dmitry Torokhov | 2950641 | 2005-11-20 00:51:22 -0500 | [diff] [blame] | 502 | retval = uinput_set_bit(arg, absbit, ABS_MAX); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | break; |
| 504 | |
| 505 | case UI_SET_MSCBIT: |
Dmitry Torokhov | 2950641 | 2005-11-20 00:51:22 -0500 | [diff] [blame] | 506 | retval = uinput_set_bit(arg, mscbit, MSC_MAX); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 507 | break; |
| 508 | |
| 509 | case UI_SET_LEDBIT: |
Dmitry Torokhov | 2950641 | 2005-11-20 00:51:22 -0500 | [diff] [blame] | 510 | retval = uinput_set_bit(arg, ledbit, LED_MAX); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 | break; |
| 512 | |
| 513 | case UI_SET_SNDBIT: |
Dmitry Torokhov | 2950641 | 2005-11-20 00:51:22 -0500 | [diff] [blame] | 514 | retval = uinput_set_bit(arg, sndbit, SND_MAX); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 515 | break; |
| 516 | |
| 517 | case UI_SET_FFBIT: |
Dmitry Torokhov | 2950641 | 2005-11-20 00:51:22 -0500 | [diff] [blame] | 518 | retval = uinput_set_bit(arg, ffbit, FF_MAX); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 519 | break; |
| 520 | |
Dmitry Torokhov | 59c7c03 | 2005-11-20 00:51:33 -0500 | [diff] [blame] | 521 | case UI_SET_SWBIT: |
| 522 | retval = uinput_set_bit(arg, swbit, SW_MAX); |
| 523 | break; |
| 524 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | case UI_SET_PHYS: |
Dmitry Torokhov | 2950641 | 2005-11-20 00:51:22 -0500 | [diff] [blame] | 526 | if (udev->state == UIST_CREATED) { |
| 527 | retval = -EINVAL; |
| 528 | goto out; |
| 529 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 530 | length = strnlen_user(p, 1024); |
| 531 | if (length <= 0) { |
| 532 | retval = -EFAULT; |
| 533 | break; |
| 534 | } |
Dmitry Torokhov | 5b6271b | 2005-06-30 00:50:38 -0500 | [diff] [blame] | 535 | kfree(udev->dev->phys); |
| 536 | udev->dev->phys = phys = kmalloc(length, GFP_KERNEL); |
| 537 | if (!phys) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 538 | retval = -ENOMEM; |
| 539 | break; |
| 540 | } |
Dmitry Torokhov | 5b6271b | 2005-06-30 00:50:38 -0500 | [diff] [blame] | 541 | if (copy_from_user(phys, p, length)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 542 | udev->dev->phys = NULL; |
Dmitry Torokhov | 5b6271b | 2005-06-30 00:50:38 -0500 | [diff] [blame] | 543 | kfree(phys); |
| 544 | retval = -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 545 | break; |
| 546 | } |
Dmitry Torokhov | 5b6271b | 2005-06-30 00:50:38 -0500 | [diff] [blame] | 547 | phys[length - 1] = '\0'; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 548 | break; |
| 549 | |
| 550 | case UI_BEGIN_FF_UPLOAD: |
| 551 | if (copy_from_user(&ff_up, p, sizeof(ff_up))) { |
| 552 | retval = -EFAULT; |
| 553 | break; |
| 554 | } |
| 555 | req = uinput_request_find(udev, ff_up.request_id); |
Anssi Hannula | ff46255 | 2006-07-19 01:41:09 -0400 | [diff] [blame] | 556 | if (!(req && req->code == UI_FF_UPLOAD && req->u.upload.effect)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | retval = -EINVAL; |
| 558 | break; |
| 559 | } |
| 560 | ff_up.retval = 0; |
Anssi Hannula | ff46255 | 2006-07-19 01:41:09 -0400 | [diff] [blame] | 561 | memcpy(&ff_up.effect, req->u.upload.effect, sizeof(struct ff_effect)); |
| 562 | if (req->u.upload.old) |
| 563 | memcpy(&ff_up.old, req->u.upload.old, sizeof(struct ff_effect)); |
| 564 | else |
| 565 | memset(&ff_up.old, 0, sizeof(struct ff_effect)); |
| 566 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 567 | if (copy_to_user(p, &ff_up, sizeof(ff_up))) { |
| 568 | retval = -EFAULT; |
| 569 | break; |
| 570 | } |
| 571 | break; |
| 572 | |
| 573 | case UI_BEGIN_FF_ERASE: |
| 574 | if (copy_from_user(&ff_erase, p, sizeof(ff_erase))) { |
| 575 | retval = -EFAULT; |
| 576 | break; |
| 577 | } |
| 578 | req = uinput_request_find(udev, ff_erase.request_id); |
Dmitry Torokhov | 152c12f | 2005-06-30 00:47:50 -0500 | [diff] [blame] | 579 | if (!(req && req->code == UI_FF_ERASE)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 580 | retval = -EINVAL; |
| 581 | break; |
| 582 | } |
| 583 | ff_erase.retval = 0; |
| 584 | ff_erase.effect_id = req->u.effect_id; |
| 585 | if (copy_to_user(p, &ff_erase, sizeof(ff_erase))) { |
| 586 | retval = -EFAULT; |
| 587 | break; |
| 588 | } |
| 589 | break; |
| 590 | |
| 591 | case UI_END_FF_UPLOAD: |
| 592 | if (copy_from_user(&ff_up, p, sizeof(ff_up))) { |
| 593 | retval = -EFAULT; |
| 594 | break; |
| 595 | } |
| 596 | req = uinput_request_find(udev, ff_up.request_id); |
Anssi Hannula | ff46255 | 2006-07-19 01:41:09 -0400 | [diff] [blame] | 597 | if (!(req && req->code == UI_FF_UPLOAD && req->u.upload.effect)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 598 | retval = -EINVAL; |
| 599 | break; |
| 600 | } |
| 601 | req->retval = ff_up.retval; |
Dmitry Torokhov | 0048e60 | 2005-06-30 00:48:14 -0500 | [diff] [blame] | 602 | uinput_request_done(udev, req); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 603 | break; |
| 604 | |
| 605 | case UI_END_FF_ERASE: |
| 606 | if (copy_from_user(&ff_erase, p, sizeof(ff_erase))) { |
| 607 | retval = -EFAULT; |
| 608 | break; |
| 609 | } |
| 610 | req = uinput_request_find(udev, ff_erase.request_id); |
Dmitry Torokhov | 152c12f | 2005-06-30 00:47:50 -0500 | [diff] [blame] | 611 | if (!(req && req->code == UI_FF_ERASE)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 612 | retval = -EINVAL; |
| 613 | break; |
| 614 | } |
| 615 | req->retval = ff_erase.retval; |
Dmitry Torokhov | 0048e60 | 2005-06-30 00:48:14 -0500 | [diff] [blame] | 616 | uinput_request_done(udev, req); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 617 | break; |
| 618 | |
| 619 | default: |
| 620 | retval = -EINVAL; |
| 621 | } |
Dmitry Torokhov | 2950641 | 2005-11-20 00:51:22 -0500 | [diff] [blame] | 622 | |
| 623 | out: |
Dmitry Torokhov | 221979a | 2006-02-19 00:22:36 -0500 | [diff] [blame] | 624 | mutex_unlock(&udev->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | return retval; |
| 626 | } |
| 627 | |
Arjan van de Ven | 2b8693c | 2007-02-12 00:55:32 -0800 | [diff] [blame] | 628 | static const struct file_operations uinput_fops = { |
Dmitry Torokhov | 2950641 | 2005-11-20 00:51:22 -0500 | [diff] [blame] | 629 | .owner = THIS_MODULE, |
| 630 | .open = uinput_open, |
| 631 | .release = uinput_release, |
| 632 | .read = uinput_read, |
| 633 | .write = uinput_write, |
| 634 | .poll = uinput_poll, |
| 635 | .unlocked_ioctl = uinput_ioctl, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 636 | }; |
| 637 | |
| 638 | static struct miscdevice uinput_misc = { |
Dmitry Torokhov | 2950641 | 2005-11-20 00:51:22 -0500 | [diff] [blame] | 639 | .fops = &uinput_fops, |
| 640 | .minor = UINPUT_MINOR, |
| 641 | .name = UINPUT_NAME, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 642 | }; |
| 643 | |
| 644 | static int __init uinput_init(void) |
| 645 | { |
| 646 | return misc_register(&uinput_misc); |
| 647 | } |
| 648 | |
| 649 | static void __exit uinput_exit(void) |
| 650 | { |
| 651 | misc_deregister(&uinput_misc); |
| 652 | } |
| 653 | |
| 654 | MODULE_AUTHOR("Aristeu Sergio Rozanski Filho"); |
| 655 | MODULE_DESCRIPTION("User level driver support for input subsystem"); |
| 656 | MODULE_LICENSE("GPL"); |
Anssi Hannula | ff46255 | 2006-07-19 01:41:09 -0400 | [diff] [blame] | 657 | MODULE_VERSION("0.3"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 658 | |
| 659 | module_init(uinput_init); |
| 660 | module_exit(uinput_exit); |
| 661 | |