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> |
Alexey Dobriyan | a99bbaf | 2009-10-04 16:11:37 +0400 | [diff] [blame] | 33 | #include <linux/sched.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | #include <linux/slab.h> |
| 35 | #include <linux/module.h> |
| 36 | #include <linux/init.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | #include <linux/fs.h> |
| 38 | #include <linux/miscdevice.h> |
| 39 | #include <linux/uinput.h> |
Henrik Rydberg | 47c78e8 | 2010-11-27 09:16:48 +0100 | [diff] [blame] | 40 | #include <linux/input/mt.h> |
Philip Langdale | 2d56f3a | 2008-10-16 22:31:42 -0400 | [diff] [blame] | 41 | #include "../input-compat.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | static int uinput_dev_event(struct input_dev *dev, unsigned int type, unsigned int code, int value) |
| 44 | { |
Dmitry Torokhov | 373f971 | 2007-04-12 01:34:33 -0400 | [diff] [blame] | 45 | struct uinput_device *udev = input_get_drvdata(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | |
| 47 | udev->buff[udev->head].type = type; |
| 48 | udev->buff[udev->head].code = code; |
| 49 | udev->buff[udev->head].value = value; |
| 50 | do_gettimeofday(&udev->buff[udev->head].time); |
| 51 | udev->head = (udev->head + 1) % UINPUT_BUFFER_SIZE; |
| 52 | |
| 53 | wake_up_interruptible(&udev->waitq); |
| 54 | |
| 55 | return 0; |
| 56 | } |
| 57 | |
Aristeu Sergio Rozanski Filho | 05cebd3 | 2009-05-14 22:01:57 -0700 | [diff] [blame] | 58 | /* Atomically allocate an ID for the given request. Returns 0 on success. */ |
Dmitry Torokhov | 00ce756 | 2012-07-29 22:48:32 -0700 | [diff] [blame] | 59 | static bool uinput_request_alloc_id(struct uinput_device *udev, |
| 60 | struct uinput_request *request) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | { |
Dmitry Torokhov | c5b3533 | 2012-07-29 22:48:32 -0700 | [diff] [blame^] | 62 | unsigned int id; |
Dmitry Torokhov | 00ce756 | 2012-07-29 22:48:32 -0700 | [diff] [blame] | 63 | bool reserved = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | |
Dmitry Torokhov | 0048e60 | 2005-06-30 00:48:14 -0500 | [diff] [blame] | 65 | spin_lock(&udev->requests_lock); |
Dmitry Torokhov | 152c12f | 2005-06-30 00:47:50 -0500 | [diff] [blame] | 66 | |
Aristeu Sergio Rozanski Filho | 05cebd3 | 2009-05-14 22:01:57 -0700 | [diff] [blame] | 67 | for (id = 0; id < UINPUT_NUM_REQUESTS; id++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | if (!udev->requests[id]) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | request->id = id; |
Dmitry Torokhov | 0048e60 | 2005-06-30 00:48:14 -0500 | [diff] [blame] | 70 | udev->requests[id] = request; |
Dmitry Torokhov | 00ce756 | 2012-07-29 22:48:32 -0700 | [diff] [blame] | 71 | reserved = true; |
Dmitry Torokhov | 152c12f | 2005-06-30 00:47:50 -0500 | [diff] [blame] | 72 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | } |
Aristeu Sergio Rozanski Filho | 05cebd3 | 2009-05-14 22:01:57 -0700 | [diff] [blame] | 74 | } |
Dmitry Torokhov | 152c12f | 2005-06-30 00:47:50 -0500 | [diff] [blame] | 75 | |
Dmitry Torokhov | 0048e60 | 2005-06-30 00:48:14 -0500 | [diff] [blame] | 76 | spin_unlock(&udev->requests_lock); |
Dmitry Torokhov | 00ce756 | 2012-07-29 22:48:32 -0700 | [diff] [blame] | 77 | return reserved; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | } |
| 79 | |
Dmitry Torokhov | c5b3533 | 2012-07-29 22:48:32 -0700 | [diff] [blame^] | 80 | static struct uinput_request *uinput_request_find(struct uinput_device *udev, |
| 81 | unsigned int id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | { |
| 83 | /* Find an input request, by ID. Returns NULL if the ID isn't valid. */ |
Dmitry Torokhov | c5b3533 | 2012-07-29 22:48:32 -0700 | [diff] [blame^] | 84 | if (id >= UINPUT_NUM_REQUESTS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | return NULL; |
Philip Langdale | 2d56f3a | 2008-10-16 22:31:42 -0400 | [diff] [blame] | 86 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | return udev->requests[id]; |
| 88 | } |
| 89 | |
Dmitry Torokhov | 00ce756 | 2012-07-29 22:48:32 -0700 | [diff] [blame] | 90 | static int uinput_request_reserve_slot(struct uinput_device *udev, |
| 91 | 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 | /* Allocate slot. If none are available right away, wait. */ |
| 94 | return wait_event_interruptible(udev->requests_waitq, |
Dmitry Torokhov | 00ce756 | 2012-07-29 22:48:32 -0700 | [diff] [blame] | 95 | uinput_request_alloc_id(udev, request)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | } |
| 97 | |
Dmitry Torokhov | 0048e60 | 2005-06-30 00:48:14 -0500 | [diff] [blame] | 98 | 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] | 99 | { |
Dmitry Torokhov | 0048e60 | 2005-06-30 00:48:14 -0500 | [diff] [blame] | 100 | /* Mark slot as available */ |
| 101 | udev->requests[request->id] = NULL; |
Dmitry Torokhov | e597f0c8 | 2005-11-20 00:51:43 -0500 | [diff] [blame] | 102 | wake_up(&udev->requests_waitq); |
Dmitry Torokhov | e7507ed | 2005-10-17 16:43:32 -0700 | [diff] [blame] | 103 | |
| 104 | complete(&request->done); |
Dmitry Torokhov | 0048e60 | 2005-06-30 00:48:14 -0500 | [diff] [blame] | 105 | } |
| 106 | |
Dmitry Torokhov | 00ce756 | 2012-07-29 22:48:32 -0700 | [diff] [blame] | 107 | static int uinput_request_send(struct uinput_device *udev, |
| 108 | struct uinput_request *request) |
Dmitry Torokhov | 0048e60 | 2005-06-30 00:48:14 -0500 | [diff] [blame] | 109 | { |
Aristeu Sergio Rozanski Filho | 05cebd3 | 2009-05-14 22:01:57 -0700 | [diff] [blame] | 110 | int retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | |
Aristeu Sergio Rozanski Filho | 05cebd3 | 2009-05-14 22:01:57 -0700 | [diff] [blame] | 112 | retval = mutex_lock_interruptible(&udev->mutex); |
| 113 | if (retval) |
| 114 | return retval; |
| 115 | |
| 116 | if (udev->state != UIST_CREATED) { |
| 117 | retval = -ENODEV; |
| 118 | goto out; |
| 119 | } |
| 120 | |
Dmitry Torokhov | 00ce756 | 2012-07-29 22:48:32 -0700 | [diff] [blame] | 121 | init_completion(&request->done); |
| 122 | |
| 123 | /* |
| 124 | * Tell our userspace application about this new request |
| 125 | * by queueing an input event. |
| 126 | */ |
Aristeu Sergio Rozanski Filho | 05cebd3 | 2009-05-14 22:01:57 -0700 | [diff] [blame] | 127 | uinput_dev_event(udev->dev, EV_UINPUT, request->code, request->id); |
| 128 | |
| 129 | out: |
| 130 | mutex_unlock(&udev->mutex); |
| 131 | return retval; |
| 132 | } |
| 133 | |
Dmitry Torokhov | 00ce756 | 2012-07-29 22:48:32 -0700 | [diff] [blame] | 134 | static int uinput_request_submit(struct uinput_device *udev, |
| 135 | struct uinput_request *request) |
| 136 | { |
| 137 | int error; |
| 138 | |
| 139 | error = uinput_request_reserve_slot(udev, request); |
| 140 | if (error) |
| 141 | return error; |
| 142 | |
| 143 | error = uinput_request_send(udev, request); |
| 144 | if (error) { |
| 145 | uinput_request_done(udev, request); |
| 146 | return error; |
| 147 | } |
| 148 | |
| 149 | wait_for_completion(&request->done); |
| 150 | return request->retval; |
| 151 | } |
| 152 | |
Aristeu Sergio Rozanski Filho | 05cebd3 | 2009-05-14 22:01:57 -0700 | [diff] [blame] | 153 | /* |
| 154 | * Fail all ouitstanding requests so handlers don't wait for the userspace |
| 155 | * to finish processing them. |
| 156 | */ |
| 157 | static void uinput_flush_requests(struct uinput_device *udev) |
| 158 | { |
| 159 | struct uinput_request *request; |
| 160 | int i; |
| 161 | |
| 162 | spin_lock(&udev->requests_lock); |
| 163 | |
| 164 | for (i = 0; i < UINPUT_NUM_REQUESTS; i++) { |
| 165 | request = udev->requests[i]; |
| 166 | if (request) { |
| 167 | request->retval = -ENODEV; |
| 168 | uinput_request_done(udev, request); |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | spin_unlock(&udev->requests_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | } |
| 174 | |
Anssi Hannula | ff46255 | 2006-07-19 01:41:09 -0400 | [diff] [blame] | 175 | static void uinput_dev_set_gain(struct input_dev *dev, u16 gain) |
| 176 | { |
| 177 | uinput_dev_event(dev, EV_FF, FF_GAIN, gain); |
| 178 | } |
| 179 | |
| 180 | static void uinput_dev_set_autocenter(struct input_dev *dev, u16 magnitude) |
| 181 | { |
| 182 | uinput_dev_event(dev, EV_FF, FF_AUTOCENTER, magnitude); |
| 183 | } |
| 184 | |
| 185 | static int uinput_dev_playback(struct input_dev *dev, int effect_id, int value) |
| 186 | { |
| 187 | return uinput_dev_event(dev, EV_FF, effect_id, value); |
| 188 | } |
| 189 | |
| 190 | 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] | 191 | { |
Aristeu Sergio Rozanski Filho | 05cebd3 | 2009-05-14 22:01:57 -0700 | [diff] [blame] | 192 | struct uinput_device *udev = input_get_drvdata(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | struct uinput_request request; |
| 194 | |
Philip Langdale | 2d56f3a | 2008-10-16 22:31:42 -0400 | [diff] [blame] | 195 | /* |
| 196 | * uinput driver does not currently support periodic effects with |
| 197 | * custom waveform since it does not have a way to pass buffer of |
| 198 | * samples (custom_data) to userspace. If ever there is a device |
| 199 | * supporting custom waveforms we would need to define an additional |
| 200 | * ioctl (UI_UPLOAD_SAMPLES) but for now we just bail out. |
| 201 | */ |
| 202 | if (effect->type == FF_PERIODIC && |
| 203 | effect->u.periodic.waveform == FF_CUSTOM) |
| 204 | return -EINVAL; |
| 205 | |
Dmitry Torokhov | 0048e60 | 2005-06-30 00:48:14 -0500 | [diff] [blame] | 206 | request.code = UI_FF_UPLOAD; |
Anssi Hannula | ff46255 | 2006-07-19 01:41:09 -0400 | [diff] [blame] | 207 | request.u.upload.effect = effect; |
| 208 | request.u.upload.old = old; |
Dmitry Torokhov | 0048e60 | 2005-06-30 00:48:14 -0500 | [diff] [blame] | 209 | |
Dmitry Torokhov | 00ce756 | 2012-07-29 22:48:32 -0700 | [diff] [blame] | 210 | return uinput_request_submit(udev, &request); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | static int uinput_dev_erase_effect(struct input_dev *dev, int effect_id) |
| 214 | { |
Aristeu Sergio Rozanski Filho | 05cebd3 | 2009-05-14 22:01:57 -0700 | [diff] [blame] | 215 | struct uinput_device *udev = input_get_drvdata(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | struct uinput_request request; |
| 217 | |
| 218 | if (!test_bit(EV_FF, dev->evbit)) |
| 219 | return -ENOSYS; |
| 220 | |
Dmitry Torokhov | 0048e60 | 2005-06-30 00:48:14 -0500 | [diff] [blame] | 221 | request.code = UI_FF_ERASE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | request.u.effect_id = effect_id; |
Dmitry Torokhov | 0048e60 | 2005-06-30 00:48:14 -0500 | [diff] [blame] | 223 | |
Dmitry Torokhov | 00ce756 | 2012-07-29 22:48:32 -0700 | [diff] [blame] | 224 | return uinput_request_submit(udev, &request); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | } |
| 226 | |
Dmitry Torokhov | 2950641 | 2005-11-20 00:51:22 -0500 | [diff] [blame] | 227 | static void uinput_destroy_device(struct uinput_device *udev) |
| 228 | { |
| 229 | const char *name, *phys; |
Aristeu Sergio Rozanski Filho | 05cebd3 | 2009-05-14 22:01:57 -0700 | [diff] [blame] | 230 | struct input_dev *dev = udev->dev; |
| 231 | enum uinput_state old_state = udev->state; |
Dmitry Torokhov | 2950641 | 2005-11-20 00:51:22 -0500 | [diff] [blame] | 232 | |
Aristeu Sergio Rozanski Filho | 05cebd3 | 2009-05-14 22:01:57 -0700 | [diff] [blame] | 233 | udev->state = UIST_NEW_DEVICE; |
| 234 | |
| 235 | if (dev) { |
| 236 | name = dev->name; |
| 237 | phys = dev->phys; |
| 238 | if (old_state == UIST_CREATED) { |
| 239 | uinput_flush_requests(udev); |
| 240 | input_unregister_device(dev); |
| 241 | } else { |
| 242 | input_free_device(dev); |
| 243 | } |
Dmitry Torokhov | 2950641 | 2005-11-20 00:51:22 -0500 | [diff] [blame] | 244 | kfree(name); |
| 245 | kfree(phys); |
| 246 | udev->dev = NULL; |
| 247 | } |
Dmitry Torokhov | 2950641 | 2005-11-20 00:51:22 -0500 | [diff] [blame] | 248 | } |
| 249 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | static int uinput_create_device(struct uinput_device *udev) |
| 251 | { |
Anssi Hannula | ff46255 | 2006-07-19 01:41:09 -0400 | [diff] [blame] | 252 | struct input_dev *dev = udev->dev; |
Dmitry Torokhov | 2950641 | 2005-11-20 00:51:22 -0500 | [diff] [blame] | 253 | int error; |
| 254 | |
| 255 | if (udev->state != UIST_SETUP_COMPLETE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | printk(KERN_DEBUG "%s: write device info first\n", UINPUT_NAME); |
| 257 | return -EINVAL; |
| 258 | } |
| 259 | |
Anssi Hannula | ff46255 | 2006-07-19 01:41:09 -0400 | [diff] [blame] | 260 | if (udev->ff_effects_max) { |
| 261 | error = input_ff_create(dev, udev->ff_effects_max); |
| 262 | if (error) |
| 263 | goto fail1; |
| 264 | |
| 265 | dev->ff->upload = uinput_dev_upload_effect; |
| 266 | dev->ff->erase = uinput_dev_erase_effect; |
| 267 | dev->ff->playback = uinput_dev_playback; |
| 268 | dev->ff->set_gain = uinput_dev_set_gain; |
| 269 | dev->ff->set_autocenter = uinput_dev_set_autocenter; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | } |
| 271 | |
Anssi Hannula | ff46255 | 2006-07-19 01:41:09 -0400 | [diff] [blame] | 272 | error = input_register_device(udev->dev); |
| 273 | if (error) |
| 274 | goto fail2; |
| 275 | |
Dmitry Torokhov | 2950641 | 2005-11-20 00:51:22 -0500 | [diff] [blame] | 276 | udev->state = UIST_CREATED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | |
| 278 | return 0; |
Anssi Hannula | ff46255 | 2006-07-19 01:41:09 -0400 | [diff] [blame] | 279 | |
| 280 | fail2: input_ff_destroy(dev); |
| 281 | fail1: uinput_destroy_device(udev); |
| 282 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | static int uinput_open(struct inode *inode, struct file *file) |
| 286 | { |
Dmitry Torokhov | 2950641 | 2005-11-20 00:51:22 -0500 | [diff] [blame] | 287 | struct uinput_device *newdev; |
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 | newdev = kzalloc(sizeof(struct uinput_device), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | if (!newdev) |
Dmitry Torokhov | 2950641 | 2005-11-20 00:51:22 -0500 | [diff] [blame] | 291 | return -ENOMEM; |
| 292 | |
Dmitry Torokhov | 221979a | 2006-02-19 00:22:36 -0500 | [diff] [blame] | 293 | mutex_init(&newdev->mutex); |
Dmitry Torokhov | 0048e60 | 2005-06-30 00:48:14 -0500 | [diff] [blame] | 294 | spin_lock_init(&newdev->requests_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | init_waitqueue_head(&newdev->requests_waitq); |
Dmitry Torokhov | 2950641 | 2005-11-20 00:51:22 -0500 | [diff] [blame] | 296 | init_waitqueue_head(&newdev->waitq); |
| 297 | newdev->state = UIST_NEW_DEVICE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | |
| 299 | file->private_data = newdev; |
Dmitry Torokhov | daf8a96 | 2010-02-04 00:30:39 -0800 | [diff] [blame] | 300 | nonseekable_open(inode, file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | |
| 302 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | static int uinput_validate_absbits(struct input_dev *dev) |
| 306 | { |
| 307 | unsigned int cnt; |
| 308 | int retval = 0; |
| 309 | |
Daniel Mack | 81c2a3b | 2010-05-20 22:52:58 -0700 | [diff] [blame] | 310 | for (cnt = 0; cnt < ABS_CNT; cnt++) { |
Peter Hutterer | a718d79 | 2011-03-30 22:25:34 -0700 | [diff] [blame] | 311 | int min, max; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | if (!test_bit(cnt, dev->absbit)) |
| 313 | continue; |
| 314 | |
Peter Hutterer | a718d79 | 2011-03-30 22:25:34 -0700 | [diff] [blame] | 315 | min = input_abs_get_min(dev, cnt); |
| 316 | max = input_abs_get_max(dev, cnt); |
| 317 | |
| 318 | if ((min != 0 || max != 0) && max <= min) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | printk(KERN_DEBUG |
| 320 | "%s: invalid abs[%02x] min:%d max:%d\n", |
| 321 | UINPUT_NAME, cnt, |
Daniel Mack | 987a6c0 | 2010-08-02 20:15:17 -0700 | [diff] [blame] | 322 | input_abs_get_min(dev, cnt), |
| 323 | input_abs_get_max(dev, cnt)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | retval = -EINVAL; |
| 325 | break; |
| 326 | } |
| 327 | |
Daniel Mack | 987a6c0 | 2010-08-02 20:15:17 -0700 | [diff] [blame] | 328 | if (input_abs_get_flat(dev, cnt) > |
| 329 | input_abs_get_max(dev, cnt) - input_abs_get_min(dev, cnt)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | printk(KERN_DEBUG |
Daniel Mack | 987a6c0 | 2010-08-02 20:15:17 -0700 | [diff] [blame] | 331 | "%s: abs_flat #%02x out of range: %d " |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | "(min:%d/max:%d)\n", |
Daniel Mack | 987a6c0 | 2010-08-02 20:15:17 -0700 | [diff] [blame] | 333 | UINPUT_NAME, cnt, |
| 334 | input_abs_get_flat(dev, cnt), |
| 335 | input_abs_get_min(dev, cnt), |
| 336 | input_abs_get_max(dev, cnt)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | retval = -EINVAL; |
| 338 | break; |
| 339 | } |
| 340 | } |
| 341 | return retval; |
| 342 | } |
| 343 | |
Dmitry Torokhov | 2950641 | 2005-11-20 00:51:22 -0500 | [diff] [blame] | 344 | static int uinput_allocate_device(struct uinput_device *udev) |
| 345 | { |
| 346 | udev->dev = input_allocate_device(); |
| 347 | if (!udev->dev) |
| 348 | return -ENOMEM; |
| 349 | |
| 350 | udev->dev->event = uinput_dev_event; |
Dmitry Torokhov | 373f971 | 2007-04-12 01:34:33 -0400 | [diff] [blame] | 351 | input_set_drvdata(udev->dev, udev); |
Dmitry Torokhov | 2950641 | 2005-11-20 00:51:22 -0500 | [diff] [blame] | 352 | |
| 353 | return 0; |
| 354 | } |
| 355 | |
| 356 | 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] | 357 | { |
| 358 | struct uinput_user_dev *user_dev; |
| 359 | struct input_dev *dev; |
David Herrmann | 5d9d6e9 | 2011-02-11 01:10:44 -0800 | [diff] [blame] | 360 | int i; |
Dmitry Torokhov | 152c12f | 2005-06-30 00:47:50 -0500 | [diff] [blame] | 361 | int retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | |
Dmitry Torokhov | 2950641 | 2005-11-20 00:51:22 -0500 | [diff] [blame] | 363 | if (count != sizeof(struct uinput_user_dev)) |
| 364 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | |
Dmitry Torokhov | 2950641 | 2005-11-20 00:51:22 -0500 | [diff] [blame] | 366 | if (!udev->dev) { |
| 367 | retval = uinput_allocate_device(udev); |
| 368 | if (retval) |
| 369 | return retval; |
| 370 | } |
| 371 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | dev = udev->dev; |
| 373 | |
Dmitry Torokhov | 4dfcc27 | 2011-02-11 01:10:45 -0800 | [diff] [blame] | 374 | user_dev = memdup_user(buffer, sizeof(struct uinput_user_dev)); |
Dan Carpenter | 163d277 | 2011-02-18 08:30:52 -0800 | [diff] [blame] | 375 | if (IS_ERR(user_dev)) |
Dmitry Torokhov | 4dfcc27 | 2011-02-11 01:10:45 -0800 | [diff] [blame] | 376 | return PTR_ERR(user_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | |
Anssi Hannula | ff46255 | 2006-07-19 01:41:09 -0400 | [diff] [blame] | 378 | udev->ff_effects_max = user_dev->ff_effects_max; |
| 379 | |
David Herrmann | 5d9d6e9 | 2011-02-11 01:10:44 -0800 | [diff] [blame] | 380 | /* Ensure name is filled in */ |
| 381 | if (!user_dev->name[0]) { |
Dmitry Torokhov | 2950641 | 2005-11-20 00:51:22 -0500 | [diff] [blame] | 382 | retval = -EINVAL; |
| 383 | goto exit; |
| 384 | } |
| 385 | |
| 386 | kfree(dev->name); |
David Herrmann | 5d9d6e9 | 2011-02-11 01:10:44 -0800 | [diff] [blame] | 387 | dev->name = kstrndup(user_dev->name, UINPUT_MAX_NAME_SIZE, |
| 388 | GFP_KERNEL); |
| 389 | if (!dev->name) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | retval = -ENOMEM; |
| 391 | goto exit; |
| 392 | } |
| 393 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | dev->id.bustype = user_dev->id.bustype; |
| 395 | dev->id.vendor = user_dev->id.vendor; |
| 396 | dev->id.product = user_dev->id.product; |
| 397 | dev->id.version = user_dev->id.version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | |
Daniel Mack | 987a6c0 | 2010-08-02 20:15:17 -0700 | [diff] [blame] | 399 | for (i = 0; i < ABS_CNT; i++) { |
| 400 | input_abs_set_max(dev, i, user_dev->absmax[i]); |
| 401 | input_abs_set_min(dev, i, user_dev->absmin[i]); |
| 402 | input_abs_set_fuzz(dev, i, user_dev->absfuzz[i]); |
| 403 | input_abs_set_flat(dev, i, user_dev->absflat[i]); |
| 404 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | |
| 406 | /* check if absmin/absmax/absfuzz/absflat are filled as |
| 407 | * told in Documentation/input/input-programming.txt */ |
| 408 | if (test_bit(EV_ABS, dev->evbit)) { |
Dmitry Torokhov | 2950641 | 2005-11-20 00:51:22 -0500 | [diff] [blame] | 409 | retval = uinput_validate_absbits(dev); |
| 410 | if (retval < 0) |
| 411 | goto exit; |
Henrik Rydberg | 38e7afe | 2010-09-19 16:25:36 -0700 | [diff] [blame] | 412 | if (test_bit(ABS_MT_SLOT, dev->absbit)) { |
| 413 | int nslot = input_abs_get_max(dev, ABS_MT_SLOT) + 1; |
Henrik Rydberg | 8cde810 | 2010-11-27 10:50:54 +0100 | [diff] [blame] | 414 | input_mt_init_slots(dev, nslot); |
Henrik Rydberg | 38e7afe | 2010-09-19 16:25:36 -0700 | [diff] [blame] | 415 | } else if (test_bit(ABS_MT_POSITION_X, dev->absbit)) { |
| 416 | input_set_events_per_packet(dev, 60); |
| 417 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | } |
| 419 | |
Dmitry Torokhov | 2950641 | 2005-11-20 00:51:22 -0500 | [diff] [blame] | 420 | udev->state = UIST_SETUP_COMPLETE; |
| 421 | retval = count; |
| 422 | |
| 423 | exit: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | kfree(user_dev); |
| 425 | return retval; |
| 426 | } |
| 427 | |
Dmitry Torokhov | 2950641 | 2005-11-20 00:51:22 -0500 | [diff] [blame] | 428 | static inline ssize_t uinput_inject_event(struct uinput_device *udev, const char __user *buffer, size_t count) |
| 429 | { |
| 430 | struct input_event ev; |
| 431 | |
Philip Langdale | 2d56f3a | 2008-10-16 22:31:42 -0400 | [diff] [blame] | 432 | if (count < input_event_size()) |
Dmitry Torokhov | 2950641 | 2005-11-20 00:51:22 -0500 | [diff] [blame] | 433 | return -EINVAL; |
| 434 | |
Philip Langdale | 2d56f3a | 2008-10-16 22:31:42 -0400 | [diff] [blame] | 435 | if (input_event_from_user(buffer, &ev)) |
Dmitry Torokhov | 2950641 | 2005-11-20 00:51:22 -0500 | [diff] [blame] | 436 | return -EFAULT; |
| 437 | |
| 438 | input_event(udev->dev, ev.type, ev.code, ev.value); |
| 439 | |
Philip Langdale | 2d56f3a | 2008-10-16 22:31:42 -0400 | [diff] [blame] | 440 | return input_event_size(); |
Dmitry Torokhov | 2950641 | 2005-11-20 00:51:22 -0500 | [diff] [blame] | 441 | } |
| 442 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | static ssize_t uinput_write(struct file *file, const char __user *buffer, size_t count, loff_t *ppos) |
| 444 | { |
| 445 | struct uinput_device *udev = file->private_data; |
Dmitry Torokhov | 2950641 | 2005-11-20 00:51:22 -0500 | [diff] [blame] | 446 | int retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | |
Dmitry Torokhov | 22ae19c | 2012-07-29 22:48:31 -0700 | [diff] [blame] | 448 | if (count == 0) |
| 449 | return 0; |
| 450 | |
Dmitry Torokhov | 221979a | 2006-02-19 00:22:36 -0500 | [diff] [blame] | 451 | retval = mutex_lock_interruptible(&udev->mutex); |
Dmitry Torokhov | 2950641 | 2005-11-20 00:51:22 -0500 | [diff] [blame] | 452 | if (retval) |
| 453 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | |
Dmitry Torokhov | 2950641 | 2005-11-20 00:51:22 -0500 | [diff] [blame] | 455 | retval = udev->state == UIST_CREATED ? |
| 456 | uinput_inject_event(udev, buffer, count) : |
| 457 | uinput_setup_device(udev, buffer, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | |
Dmitry Torokhov | 221979a | 2006-02-19 00:22:36 -0500 | [diff] [blame] | 459 | mutex_unlock(&udev->mutex); |
Dmitry Torokhov | 2950641 | 2005-11-20 00:51:22 -0500 | [diff] [blame] | 460 | |
| 461 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | } |
| 463 | |
Dmitry Torokhov | 929d1af | 2012-07-29 22:48:31 -0700 | [diff] [blame] | 464 | static bool uinput_fetch_next_event(struct uinput_device *udev, |
| 465 | struct input_event *event) |
| 466 | { |
| 467 | bool have_event; |
| 468 | |
| 469 | spin_lock_irq(&udev->dev->event_lock); |
| 470 | |
| 471 | have_event = udev->head != udev->tail; |
| 472 | if (have_event) { |
| 473 | *event = udev->buff[udev->tail]; |
| 474 | udev->tail = (udev->tail + 1) % UINPUT_BUFFER_SIZE; |
| 475 | } |
| 476 | |
| 477 | spin_unlock_irq(&udev->dev->event_lock); |
| 478 | |
| 479 | return have_event; |
| 480 | } |
| 481 | |
Dmitry Torokhov | 22ae19c | 2012-07-29 22:48:31 -0700 | [diff] [blame] | 482 | static ssize_t uinput_events_to_user(struct uinput_device *udev, |
| 483 | char __user *buffer, size_t count) |
| 484 | { |
| 485 | struct input_event event; |
| 486 | size_t read = 0; |
Dmitry Torokhov | 22ae19c | 2012-07-29 22:48:31 -0700 | [diff] [blame] | 487 | |
| 488 | while (read + input_event_size() <= count && |
| 489 | uinput_fetch_next_event(udev, &event)) { |
| 490 | |
Dmitry Torokhov | 00ce756 | 2012-07-29 22:48:32 -0700 | [diff] [blame] | 491 | if (input_event_to_user(buffer + read, &event)) |
| 492 | return -EFAULT; |
Dmitry Torokhov | 22ae19c | 2012-07-29 22:48:31 -0700 | [diff] [blame] | 493 | |
| 494 | read += input_event_size(); |
| 495 | } |
| 496 | |
Dmitry Torokhov | 00ce756 | 2012-07-29 22:48:32 -0700 | [diff] [blame] | 497 | return read; |
Dmitry Torokhov | 22ae19c | 2012-07-29 22:48:31 -0700 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | static ssize_t uinput_read(struct file *file, char __user *buffer, |
| 501 | size_t count, loff_t *ppos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 502 | { |
| 503 | struct uinput_device *udev = file->private_data; |
Dmitry Torokhov | 22ae19c | 2012-07-29 22:48:31 -0700 | [diff] [blame] | 504 | ssize_t retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | |
David Herrmann | f40033a | 2012-07-29 22:48:31 -0700 | [diff] [blame] | 506 | if (count != 0 && count < input_event_size()) |
| 507 | return -EINVAL; |
| 508 | |
Dmitry Torokhov | 22ae19c | 2012-07-29 22:48:31 -0700 | [diff] [blame] | 509 | do { |
| 510 | retval = mutex_lock_interruptible(&udev->mutex); |
| 511 | if (retval) |
| 512 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | |
Dmitry Torokhov | 22ae19c | 2012-07-29 22:48:31 -0700 | [diff] [blame] | 514 | if (udev->state != UIST_CREATED) |
| 515 | retval = -ENODEV; |
| 516 | else if (udev->head == udev->tail && |
| 517 | (file->f_flags & O_NONBLOCK)) |
| 518 | retval = -EAGAIN; |
| 519 | else |
| 520 | retval = uinput_events_to_user(udev, buffer, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 521 | |
Dmitry Torokhov | 22ae19c | 2012-07-29 22:48:31 -0700 | [diff] [blame] | 522 | mutex_unlock(&udev->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 523 | |
Dmitry Torokhov | 22ae19c | 2012-07-29 22:48:31 -0700 | [diff] [blame] | 524 | if (retval || count == 0) |
| 525 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 526 | |
Dmitry Torokhov | 22ae19c | 2012-07-29 22:48:31 -0700 | [diff] [blame] | 527 | if (!(file->f_flags & O_NONBLOCK)) |
| 528 | retval = wait_event_interruptible(udev->waitq, |
| 529 | udev->head != udev->tail || |
| 530 | udev->state != UIST_CREATED); |
| 531 | } while (retval == 0); |
Dmitry Torokhov | 2950641 | 2005-11-20 00:51:22 -0500 | [diff] [blame] | 532 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 533 | return retval; |
| 534 | } |
| 535 | |
| 536 | static unsigned int uinput_poll(struct file *file, poll_table *wait) |
| 537 | { |
| 538 | struct uinput_device *udev = file->private_data; |
| 539 | |
| 540 | poll_wait(file, &udev->waitq, wait); |
| 541 | |
| 542 | if (udev->head != udev->tail) |
| 543 | return POLLIN | POLLRDNORM; |
| 544 | |
| 545 | return 0; |
| 546 | } |
| 547 | |
Dmitry Torokhov | 2950641 | 2005-11-20 00:51:22 -0500 | [diff] [blame] | 548 | static int uinput_release(struct inode *inode, struct file *file) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 549 | { |
Dmitry Torokhov | 2950641 | 2005-11-20 00:51:22 -0500 | [diff] [blame] | 550 | struct uinput_device *udev = file->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | |
Dmitry Torokhov | 2950641 | 2005-11-20 00:51:22 -0500 | [diff] [blame] | 552 | uinput_destroy_device(udev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | kfree(udev); |
| 554 | |
| 555 | return 0; |
| 556 | } |
| 557 | |
Philip Langdale | 2d56f3a | 2008-10-16 22:31:42 -0400 | [diff] [blame] | 558 | #ifdef CONFIG_COMPAT |
| 559 | struct uinput_ff_upload_compat { |
Dmitry Torokhov | c5b3533 | 2012-07-29 22:48:32 -0700 | [diff] [blame^] | 560 | __u32 request_id; |
| 561 | __s32 retval; |
Philip Langdale | 2d56f3a | 2008-10-16 22:31:42 -0400 | [diff] [blame] | 562 | struct ff_effect_compat effect; |
| 563 | struct ff_effect_compat old; |
| 564 | }; |
| 565 | |
| 566 | static int uinput_ff_upload_to_user(char __user *buffer, |
| 567 | const struct uinput_ff_upload *ff_up) |
| 568 | { |
| 569 | if (INPUT_COMPAT_TEST) { |
| 570 | struct uinput_ff_upload_compat ff_up_compat; |
| 571 | |
| 572 | ff_up_compat.request_id = ff_up->request_id; |
| 573 | ff_up_compat.retval = ff_up->retval; |
| 574 | /* |
| 575 | * It so happens that the pointer that gives us the trouble |
| 576 | * is the last field in the structure. Since we don't support |
| 577 | * custom waveforms in uinput anyway we can just copy the whole |
| 578 | * thing (to the compat size) and ignore the pointer. |
| 579 | */ |
| 580 | memcpy(&ff_up_compat.effect, &ff_up->effect, |
| 581 | sizeof(struct ff_effect_compat)); |
| 582 | memcpy(&ff_up_compat.old, &ff_up->old, |
| 583 | sizeof(struct ff_effect_compat)); |
| 584 | |
| 585 | if (copy_to_user(buffer, &ff_up_compat, |
| 586 | sizeof(struct uinput_ff_upload_compat))) |
| 587 | return -EFAULT; |
| 588 | } else { |
| 589 | if (copy_to_user(buffer, ff_up, |
| 590 | sizeof(struct uinput_ff_upload))) |
| 591 | return -EFAULT; |
| 592 | } |
| 593 | |
| 594 | return 0; |
| 595 | } |
| 596 | |
| 597 | static int uinput_ff_upload_from_user(const char __user *buffer, |
| 598 | struct uinput_ff_upload *ff_up) |
| 599 | { |
| 600 | if (INPUT_COMPAT_TEST) { |
| 601 | struct uinput_ff_upload_compat ff_up_compat; |
| 602 | |
| 603 | if (copy_from_user(&ff_up_compat, buffer, |
| 604 | sizeof(struct uinput_ff_upload_compat))) |
| 605 | return -EFAULT; |
| 606 | |
| 607 | ff_up->request_id = ff_up_compat.request_id; |
| 608 | ff_up->retval = ff_up_compat.retval; |
| 609 | memcpy(&ff_up->effect, &ff_up_compat.effect, |
| 610 | sizeof(struct ff_effect_compat)); |
| 611 | memcpy(&ff_up->old, &ff_up_compat.old, |
| 612 | sizeof(struct ff_effect_compat)); |
| 613 | |
| 614 | } else { |
| 615 | if (copy_from_user(ff_up, buffer, |
| 616 | sizeof(struct uinput_ff_upload))) |
| 617 | return -EFAULT; |
| 618 | } |
| 619 | |
| 620 | return 0; |
| 621 | } |
| 622 | |
| 623 | #else |
| 624 | |
| 625 | static int uinput_ff_upload_to_user(char __user *buffer, |
| 626 | const struct uinput_ff_upload *ff_up) |
| 627 | { |
| 628 | if (copy_to_user(buffer, ff_up, sizeof(struct uinput_ff_upload))) |
| 629 | return -EFAULT; |
| 630 | |
| 631 | return 0; |
| 632 | } |
| 633 | |
| 634 | static int uinput_ff_upload_from_user(const char __user *buffer, |
| 635 | struct uinput_ff_upload *ff_up) |
| 636 | { |
| 637 | if (copy_from_user(ff_up, buffer, sizeof(struct uinput_ff_upload))) |
| 638 | return -EFAULT; |
| 639 | |
| 640 | return 0; |
| 641 | } |
| 642 | |
| 643 | #endif |
| 644 | |
Dmitry Torokhov | 2950641 | 2005-11-20 00:51:22 -0500 | [diff] [blame] | 645 | #define uinput_set_bit(_arg, _bit, _max) \ |
| 646 | ({ \ |
| 647 | int __ret = 0; \ |
| 648 | if (udev->state == UIST_CREATED) \ |
| 649 | __ret = -EINVAL; \ |
| 650 | else if ((_arg) > (_max)) \ |
| 651 | __ret = -EINVAL; \ |
| 652 | else set_bit((_arg), udev->dev->_bit); \ |
| 653 | __ret; \ |
| 654 | }) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 655 | |
Philip Langdale | 2d56f3a | 2008-10-16 22:31:42 -0400 | [diff] [blame] | 656 | static long uinput_ioctl_handler(struct file *file, unsigned int cmd, |
| 657 | unsigned long arg, void __user *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 658 | { |
Dmitry Torokhov | 2950641 | 2005-11-20 00:51:22 -0500 | [diff] [blame] | 659 | int retval; |
Philip Langdale | 2d56f3a | 2008-10-16 22:31:42 -0400 | [diff] [blame] | 660 | struct uinput_device *udev = file->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 661 | struct uinput_ff_upload ff_up; |
| 662 | struct uinput_ff_erase ff_erase; |
| 663 | struct uinput_request *req; |
Dmitry Torokhov | 5b6271b | 2005-06-30 00:50:38 -0500 | [diff] [blame] | 664 | char *phys; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 665 | |
Dmitry Torokhov | 221979a | 2006-02-19 00:22:36 -0500 | [diff] [blame] | 666 | retval = mutex_lock_interruptible(&udev->mutex); |
Dmitry Torokhov | 2950641 | 2005-11-20 00:51:22 -0500 | [diff] [blame] | 667 | if (retval) |
| 668 | return retval; |
| 669 | |
| 670 | if (!udev->dev) { |
| 671 | retval = uinput_allocate_device(udev); |
| 672 | if (retval) |
| 673 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | } |
| 675 | |
| 676 | switch (cmd) { |
| 677 | case UI_DEV_CREATE: |
| 678 | retval = uinput_create_device(udev); |
| 679 | break; |
| 680 | |
| 681 | case UI_DEV_DESTROY: |
Dmitry Torokhov | 2950641 | 2005-11-20 00:51:22 -0500 | [diff] [blame] | 682 | uinput_destroy_device(udev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 683 | break; |
| 684 | |
| 685 | case UI_SET_EVBIT: |
Dmitry Torokhov | 2950641 | 2005-11-20 00:51:22 -0500 | [diff] [blame] | 686 | retval = uinput_set_bit(arg, evbit, EV_MAX); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 687 | break; |
| 688 | |
| 689 | case UI_SET_KEYBIT: |
Dmitry Torokhov | 2950641 | 2005-11-20 00:51:22 -0500 | [diff] [blame] | 690 | retval = uinput_set_bit(arg, keybit, KEY_MAX); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 691 | break; |
| 692 | |
| 693 | case UI_SET_RELBIT: |
Dmitry Torokhov | 2950641 | 2005-11-20 00:51:22 -0500 | [diff] [blame] | 694 | retval = uinput_set_bit(arg, relbit, REL_MAX); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 695 | break; |
| 696 | |
| 697 | case UI_SET_ABSBIT: |
Dmitry Torokhov | 2950641 | 2005-11-20 00:51:22 -0500 | [diff] [blame] | 698 | retval = uinput_set_bit(arg, absbit, ABS_MAX); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 699 | break; |
| 700 | |
| 701 | case UI_SET_MSCBIT: |
Dmitry Torokhov | 2950641 | 2005-11-20 00:51:22 -0500 | [diff] [blame] | 702 | retval = uinput_set_bit(arg, mscbit, MSC_MAX); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 703 | break; |
| 704 | |
| 705 | case UI_SET_LEDBIT: |
Dmitry Torokhov | 2950641 | 2005-11-20 00:51:22 -0500 | [diff] [blame] | 706 | retval = uinput_set_bit(arg, ledbit, LED_MAX); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 707 | break; |
| 708 | |
| 709 | case UI_SET_SNDBIT: |
Dmitry Torokhov | 2950641 | 2005-11-20 00:51:22 -0500 | [diff] [blame] | 710 | retval = uinput_set_bit(arg, sndbit, SND_MAX); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 711 | break; |
| 712 | |
| 713 | case UI_SET_FFBIT: |
Dmitry Torokhov | 2950641 | 2005-11-20 00:51:22 -0500 | [diff] [blame] | 714 | retval = uinput_set_bit(arg, ffbit, FF_MAX); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 715 | break; |
| 716 | |
Dmitry Torokhov | 59c7c03 | 2005-11-20 00:51:33 -0500 | [diff] [blame] | 717 | case UI_SET_SWBIT: |
| 718 | retval = uinput_set_bit(arg, swbit, SW_MAX); |
| 719 | break; |
| 720 | |
Henrik Rydberg | 85b7720 | 2010-12-18 20:51:13 +0100 | [diff] [blame] | 721 | case UI_SET_PROPBIT: |
| 722 | retval = uinput_set_bit(arg, propbit, INPUT_PROP_MAX); |
| 723 | break; |
| 724 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 725 | case UI_SET_PHYS: |
Dmitry Torokhov | 2950641 | 2005-11-20 00:51:22 -0500 | [diff] [blame] | 726 | if (udev->state == UIST_CREATED) { |
| 727 | retval = -EINVAL; |
| 728 | goto out; |
| 729 | } |
Dmitry Torokhov | 4dfcc27 | 2011-02-11 01:10:45 -0800 | [diff] [blame] | 730 | |
| 731 | phys = strndup_user(p, 1024); |
| 732 | if (IS_ERR(phys)) { |
| 733 | retval = PTR_ERR(phys); |
| 734 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 735 | } |
Dmitry Torokhov | 4dfcc27 | 2011-02-11 01:10:45 -0800 | [diff] [blame] | 736 | |
Dmitry Torokhov | 5b6271b | 2005-06-30 00:50:38 -0500 | [diff] [blame] | 737 | kfree(udev->dev->phys); |
Dmitry Torokhov | 4dfcc27 | 2011-02-11 01:10:45 -0800 | [diff] [blame] | 738 | udev->dev->phys = phys; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 739 | break; |
| 740 | |
| 741 | case UI_BEGIN_FF_UPLOAD: |
Philip Langdale | 2d56f3a | 2008-10-16 22:31:42 -0400 | [diff] [blame] | 742 | retval = uinput_ff_upload_from_user(p, &ff_up); |
| 743 | if (retval) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 744 | break; |
Philip Langdale | 2d56f3a | 2008-10-16 22:31:42 -0400 | [diff] [blame] | 745 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 746 | req = uinput_request_find(udev, ff_up.request_id); |
Philip Langdale | 2d56f3a | 2008-10-16 22:31:42 -0400 | [diff] [blame] | 747 | if (!req || req->code != UI_FF_UPLOAD || !req->u.upload.effect) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 748 | retval = -EINVAL; |
| 749 | break; |
| 750 | } |
Philip Langdale | 2d56f3a | 2008-10-16 22:31:42 -0400 | [diff] [blame] | 751 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 752 | ff_up.retval = 0; |
Philip Langdale | 2d56f3a | 2008-10-16 22:31:42 -0400 | [diff] [blame] | 753 | ff_up.effect = *req->u.upload.effect; |
Anssi Hannula | ff46255 | 2006-07-19 01:41:09 -0400 | [diff] [blame] | 754 | if (req->u.upload.old) |
Philip Langdale | 2d56f3a | 2008-10-16 22:31:42 -0400 | [diff] [blame] | 755 | ff_up.old = *req->u.upload.old; |
Anssi Hannula | ff46255 | 2006-07-19 01:41:09 -0400 | [diff] [blame] | 756 | else |
| 757 | memset(&ff_up.old, 0, sizeof(struct ff_effect)); |
| 758 | |
Philip Langdale | 2d56f3a | 2008-10-16 22:31:42 -0400 | [diff] [blame] | 759 | retval = uinput_ff_upload_to_user(p, &ff_up); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 760 | break; |
| 761 | |
| 762 | case UI_BEGIN_FF_ERASE: |
| 763 | if (copy_from_user(&ff_erase, p, sizeof(ff_erase))) { |
| 764 | retval = -EFAULT; |
| 765 | break; |
| 766 | } |
Philip Langdale | 2d56f3a | 2008-10-16 22:31:42 -0400 | [diff] [blame] | 767 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 768 | req = uinput_request_find(udev, ff_erase.request_id); |
Philip Langdale | 2d56f3a | 2008-10-16 22:31:42 -0400 | [diff] [blame] | 769 | if (!req || req->code != UI_FF_ERASE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 770 | retval = -EINVAL; |
| 771 | break; |
| 772 | } |
Philip Langdale | 2d56f3a | 2008-10-16 22:31:42 -0400 | [diff] [blame] | 773 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 774 | ff_erase.retval = 0; |
| 775 | ff_erase.effect_id = req->u.effect_id; |
| 776 | if (copy_to_user(p, &ff_erase, sizeof(ff_erase))) { |
| 777 | retval = -EFAULT; |
| 778 | break; |
| 779 | } |
Philip Langdale | 2d56f3a | 2008-10-16 22:31:42 -0400 | [diff] [blame] | 780 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 781 | break; |
| 782 | |
| 783 | case UI_END_FF_UPLOAD: |
Philip Langdale | 2d56f3a | 2008-10-16 22:31:42 -0400 | [diff] [blame] | 784 | retval = uinput_ff_upload_from_user(p, &ff_up); |
| 785 | if (retval) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 786 | break; |
Philip Langdale | 2d56f3a | 2008-10-16 22:31:42 -0400 | [diff] [blame] | 787 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 788 | req = uinput_request_find(udev, ff_up.request_id); |
Philip Langdale | 2d56f3a | 2008-10-16 22:31:42 -0400 | [diff] [blame] | 789 | if (!req || req->code != UI_FF_UPLOAD || |
| 790 | !req->u.upload.effect) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 791 | retval = -EINVAL; |
| 792 | break; |
| 793 | } |
Philip Langdale | 2d56f3a | 2008-10-16 22:31:42 -0400 | [diff] [blame] | 794 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 795 | req->retval = ff_up.retval; |
Dmitry Torokhov | 0048e60 | 2005-06-30 00:48:14 -0500 | [diff] [blame] | 796 | uinput_request_done(udev, req); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 797 | break; |
| 798 | |
| 799 | case UI_END_FF_ERASE: |
| 800 | if (copy_from_user(&ff_erase, p, sizeof(ff_erase))) { |
| 801 | retval = -EFAULT; |
| 802 | break; |
| 803 | } |
Philip Langdale | 2d56f3a | 2008-10-16 22:31:42 -0400 | [diff] [blame] | 804 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 805 | req = uinput_request_find(udev, ff_erase.request_id); |
Philip Langdale | 2d56f3a | 2008-10-16 22:31:42 -0400 | [diff] [blame] | 806 | if (!req || req->code != UI_FF_ERASE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 807 | retval = -EINVAL; |
| 808 | break; |
| 809 | } |
Philip Langdale | 2d56f3a | 2008-10-16 22:31:42 -0400 | [diff] [blame] | 810 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 811 | req->retval = ff_erase.retval; |
Dmitry Torokhov | 0048e60 | 2005-06-30 00:48:14 -0500 | [diff] [blame] | 812 | uinput_request_done(udev, req); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 813 | break; |
| 814 | |
| 815 | default: |
| 816 | retval = -EINVAL; |
| 817 | } |
Dmitry Torokhov | 2950641 | 2005-11-20 00:51:22 -0500 | [diff] [blame] | 818 | |
| 819 | out: |
Dmitry Torokhov | 221979a | 2006-02-19 00:22:36 -0500 | [diff] [blame] | 820 | mutex_unlock(&udev->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 821 | return retval; |
| 822 | } |
| 823 | |
Philip Langdale | 2d56f3a | 2008-10-16 22:31:42 -0400 | [diff] [blame] | 824 | static long uinput_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 825 | { |
| 826 | return uinput_ioctl_handler(file, cmd, arg, (void __user *)arg); |
| 827 | } |
| 828 | |
| 829 | #ifdef CONFIG_COMPAT |
| 830 | static long uinput_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 831 | { |
| 832 | return uinput_ioctl_handler(file, cmd, arg, compat_ptr(arg)); |
| 833 | } |
| 834 | #endif |
| 835 | |
Arjan van de Ven | 2b8693c | 2007-02-12 00:55:32 -0800 | [diff] [blame] | 836 | static const struct file_operations uinput_fops = { |
Dmitry Torokhov | 2950641 | 2005-11-20 00:51:22 -0500 | [diff] [blame] | 837 | .owner = THIS_MODULE, |
| 838 | .open = uinput_open, |
| 839 | .release = uinput_release, |
| 840 | .read = uinput_read, |
| 841 | .write = uinput_write, |
| 842 | .poll = uinput_poll, |
| 843 | .unlocked_ioctl = uinput_ioctl, |
Philip Langdale | 2d56f3a | 2008-10-16 22:31:42 -0400 | [diff] [blame] | 844 | #ifdef CONFIG_COMPAT |
| 845 | .compat_ioctl = uinput_compat_ioctl, |
| 846 | #endif |
Arnd Bergmann | 6038f37 | 2010-08-15 18:52:59 +0200 | [diff] [blame] | 847 | .llseek = no_llseek, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 848 | }; |
| 849 | |
| 850 | static struct miscdevice uinput_misc = { |
Dmitry Torokhov | 2950641 | 2005-11-20 00:51:22 -0500 | [diff] [blame] | 851 | .fops = &uinput_fops, |
| 852 | .minor = UINPUT_MINOR, |
| 853 | .name = UINPUT_NAME, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 854 | }; |
Kay Sievers | 8905aaa | 2010-08-19 09:52:28 -0700 | [diff] [blame] | 855 | MODULE_ALIAS_MISCDEV(UINPUT_MINOR); |
| 856 | MODULE_ALIAS("devname:" UINPUT_NAME); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 857 | |
| 858 | static int __init uinput_init(void) |
| 859 | { |
| 860 | return misc_register(&uinput_misc); |
| 861 | } |
| 862 | |
| 863 | static void __exit uinput_exit(void) |
| 864 | { |
| 865 | misc_deregister(&uinput_misc); |
| 866 | } |
| 867 | |
| 868 | MODULE_AUTHOR("Aristeu Sergio Rozanski Filho"); |
| 869 | MODULE_DESCRIPTION("User level driver support for input subsystem"); |
| 870 | MODULE_LICENSE("GPL"); |
Anssi Hannula | ff46255 | 2006-07-19 01:41:09 -0400 | [diff] [blame] | 871 | MODULE_VERSION("0.3"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 872 | |
| 873 | module_init(uinput_init); |
| 874 | module_exit(uinput_exit); |
| 875 | |