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