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