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