1 /* 2 * Force feedback support for Linux input subsystem 3 * 4 * Copyright (c) 2006 Anssi Hannula <anssi.hannula@gmail.com> 5 * Copyright (c) 2006 Dmitry Torokhov <dtor@mail.ru> 6 */ 7 8 /* 9 * This program is free software; you can redistribute it and/or modify 10 * it under the terms of the GNU General Public License as published by 11 * the Free Software Foundation; either version 2 of the License, or 12 * (at your option) any later version. 13 * 14 * This program is distributed in the hope that it will be useful, 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 * GNU General Public License for more details. 18 * 19 * You should have received a copy of the GNU General Public License 20 * along with this program; if not, write to the Free Software 21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 22 */ 23 24 /* #define DEBUG */ 25 26 #define debug(format, arg...) pr_debug("ff-core: " format "\n", ## arg) 27 28 #include <linux/input.h> 29 #include <linux/module.h> 30 #include <linux/mutex.h> 31 #include <linux/sched.h> 32 33 /* 34 * Check that the effect_id is a valid effect and whether the user 35 * is the owner 36 */ 37 static int check_effect_access(struct ff_device *ff, int effect_id, 38 struct file *file) 39 { 40 if (effect_id < 0 || effect_id >= ff->max_effects || 41 !ff->effect_owners[effect_id]) 42 return -EINVAL; 43 44 if (file && ff->effect_owners[effect_id] != file) 45 return -EACCES; 46 47 return 0; 48 } 49 50 /* 51 * Checks whether 2 effects can be combined together 52 */ 53 static inline int check_effects_compatible(struct ff_effect *e1, 54 struct ff_effect *e2) 55 { 56 return e1->type == e2->type && 57 (e1->type != FF_PERIODIC || 58 e1->u.periodic.waveform == e2->u.periodic.waveform); 59 } 60 61 /* 62 * Convert an effect into compatible one 63 */ 64 static int compat_effect(struct ff_device *ff, struct ff_effect *effect) 65 { 66 int magnitude; 67 68 switch (effect->type) { 69 case FF_RUMBLE: 70 if (!test_bit(FF_PERIODIC, ff->ffbit)) 71 return -EINVAL; 72 73 /* 74 * calculate manginude of sine wave as average of rumble's 75 * 2/3 of strong magnitude and 1/3 of weak magnitude 76 */ 77 magnitude = effect->u.rumble.strong_magnitude / 3 + 78 effect->u.rumble.weak_magnitude / 6; 79 80 effect->type = FF_PERIODIC; 81 effect->u.periodic.waveform = FF_SINE; 82 effect->u.periodic.period = 50; 83 effect->u.periodic.magnitude = max(magnitude, 0x7fff); 84 effect->u.periodic.offset = 0; 85 effect->u.periodic.phase = 0; 86 effect->u.periodic.envelope.attack_length = 0; 87 effect->u.periodic.envelope.attack_level = 0; 88 effect->u.periodic.envelope.fade_length = 0; 89 effect->u.periodic.envelope.fade_level = 0; 90 91 return 0; 92 93 default: 94 /* Let driver handle conversion */ 95 return 0; 96 } 97 } 98 99 /** 100 * input_ff_upload() - upload effect into force-feedback device 101 * @dev: input device 102 * @effect: effect to be uploaded 103 * @file: owner of the effect 104 */ 105 int input_ff_upload(struct input_dev *dev, struct ff_effect *effect, 106 struct file *file) 107 { 108 struct ff_device *ff = dev->ff; 109 struct ff_effect *old; 110 int ret = 0; 111 int id; 112 113 if (!test_bit(EV_FF, dev->evbit)) 114 return -ENOSYS; 115 116 if (effect->type < FF_EFFECT_MIN || effect->type > FF_EFFECT_MAX || 117 !test_bit(effect->type, dev->ffbit)) { 118 debug("invalid or not supported effect type in upload"); 119 return -EINVAL; 120 } 121 122 if (effect->type == FF_PERIODIC && 123 (effect->u.periodic.waveform < FF_WAVEFORM_MIN || 124 effect->u.periodic.waveform > FF_WAVEFORM_MAX || 125 !test_bit(effect->u.periodic.waveform, dev->ffbit))) { 126 debug("invalid or not supported wave form in upload"); 127 return -EINVAL; 128 } 129 130 if (!test_bit(effect->type, ff->ffbit)) { 131 ret = compat_effect(ff, effect); 132 if (ret) 133 return ret; 134 } 135 136 mutex_lock(&ff->mutex); 137 138 if (effect->id == -1) { 139 for (id = 0; id < ff->max_effects; id++) 140 if (!ff->effect_owners[id]) 141 break; 142 143 if (id >= ff->max_effects) { 144 ret = -ENOSPC; 145 goto out; 146 } 147 148 effect->id = id; 149 old = NULL; 150 151 } else { 152 id = effect->id; 153 154 ret = check_effect_access(ff, id, file); 155 if (ret) 156 goto out; 157 158 old = &ff->effects[id]; 159 160 if (!check_effects_compatible(effect, old)) { 161 ret = -EINVAL; 162 goto out; 163 } 164 } 165 166 ret = ff->upload(dev, effect, old); 167 if (ret) 168 goto out; 169 170 spin_lock_irq(&dev->event_lock); 171 ff->effects[id] = *effect; 172 ff->effect_owners[id] = file; 173 spin_unlock_irq(&dev->event_lock); 174 175 out: 176 mutex_unlock(&ff->mutex); 177 return ret; 178 } 179 EXPORT_SYMBOL_GPL(input_ff_upload); 180 181 /* 182 * Erases the effect if the requester is also the effect owner. The mutex 183 * should already be locked before calling this function. 184 */ 185 static int erase_effect(struct input_dev *dev, int effect_id, 186 struct file *file) 187 { 188 struct ff_device *ff = dev->ff; 189 int error; 190 191 error = check_effect_access(ff, effect_id, file); 192 if (error) 193 return error; 194 195 spin_lock_irq(&dev->event_lock); 196 ff->playback(dev, effect_id, 0); 197 ff->effect_owners[effect_id] = NULL; 198 spin_unlock_irq(&dev->event_lock); 199 200 if (ff->erase) { 201 error = ff->erase(dev, effect_id); 202 if (error) { 203 spin_lock_irq(&dev->event_lock); 204 ff->effect_owners[effect_id] = file; 205 spin_unlock_irq(&dev->event_lock); 206 207 return error; 208 } 209 } 210 211 return 0; 212 } 213 214 /** 215 * input_ff_erase - erase a force-feedback effect from device 216 * @dev: input device to erase effect from 217 * @effect_id: id of the ffect to be erased 218 * @file: purported owner of the request 219 * 220 * This function erases a force-feedback effect from specified device. 221 * The effect will only be erased if it was uploaded through the same 222 * file handle that is requesting erase. 223 */ 224 int input_ff_erase(struct input_dev *dev, int effect_id, struct file *file) 225 { 226 struct ff_device *ff = dev->ff; 227 int ret; 228 229 if (!test_bit(EV_FF, dev->evbit)) 230 return -ENOSYS; 231 232 mutex_lock(&ff->mutex); 233 ret = erase_effect(dev, effect_id, file); 234 mutex_unlock(&ff->mutex); 235 236 return ret; 237 } 238 EXPORT_SYMBOL_GPL(input_ff_erase); 239 240 /* 241 * flush_effects - erase all effects owned by a file handle 242 */ 243 static int flush_effects(struct input_dev *dev, struct file *file) 244 { 245 struct ff_device *ff = dev->ff; 246 int i; 247 248 debug("flushing now"); 249 250 mutex_lock(&ff->mutex); 251 252 for (i = 0; i < ff->max_effects; i++) 253 erase_effect(dev, i, file); 254 255 mutex_unlock(&ff->mutex); 256 257 return 0; 258 } 259 260 /** 261 * input_ff_event() - generic handler for force-feedback events 262 * @dev: input device to send the effect to 263 * @type: event type (anything but EV_FF is ignored) 264 * @code: event code 265 * @value: event value 266 */ 267 int input_ff_event(struct input_dev *dev, unsigned int type, 268 unsigned int code, int value) 269 { 270 struct ff_device *ff = dev->ff; 271 272 if (type != EV_FF) 273 return 0; 274 275 switch (code) { 276 case FF_GAIN: 277 if (!test_bit(FF_GAIN, dev->ffbit) || value > 0xffff) 278 break; 279 280 ff->set_gain(dev, value); 281 break; 282 283 case FF_AUTOCENTER: 284 if (!test_bit(FF_AUTOCENTER, dev->ffbit) || value > 0xffff) 285 break; 286 287 ff->set_autocenter(dev, value); 288 break; 289 290 default: 291 if (check_effect_access(ff, code, NULL) == 0) 292 ff->playback(dev, code, value); 293 break; 294 } 295 296 return 0; 297 } 298 EXPORT_SYMBOL_GPL(input_ff_event); 299 300 /** 301 * input_ff_create() - create force-feedback device 302 * @dev: input device supporting force-feedback 303 * @max_effects: maximum number of effects supported by the device 304 * 305 * This function allocates all necessary memory for a force feedback 306 * portion of an input device and installs all default handlers. 307 * @dev->ffbit should be already set up before calling this function. 308 * Once ff device is created you need to setup its upload, erase, 309 * playback and other handlers before registering input device 310 */ 311 int input_ff_create(struct input_dev *dev, int max_effects) 312 { 313 struct ff_device *ff; 314 int i; 315 316 if (!max_effects) { 317 printk(KERN_ERR 318 "ff-core: cannot allocate device without any effects\n"); 319 return -EINVAL; 320 } 321 322 ff = kzalloc(sizeof(struct ff_device) + 323 max_effects * sizeof(struct file *), GFP_KERNEL); 324 if (!ff) 325 return -ENOMEM; 326 327 ff->effects = kcalloc(max_effects, sizeof(struct ff_effect), 328 GFP_KERNEL); 329 if (!ff->effects) { 330 kfree(ff); 331 return -ENOMEM; 332 } 333 334 ff->max_effects = max_effects; 335 mutex_init(&ff->mutex); 336 337 dev->ff = ff; 338 dev->flush = flush_effects; 339 dev->event = input_ff_event; 340 set_bit(EV_FF, dev->evbit); 341 342 /* Copy "true" bits into ff device bitmap */ 343 for (i = 0; i <= FF_MAX; i++) 344 if (test_bit(i, dev->ffbit)) 345 set_bit(i, ff->ffbit); 346 347 /* we can emulate RUMBLE with periodic effects */ 348 if (test_bit(FF_PERIODIC, ff->ffbit)) 349 set_bit(FF_RUMBLE, dev->ffbit); 350 351 return 0; 352 } 353 EXPORT_SYMBOL_GPL(input_ff_create); 354 355 /** 356 * input_ff_free() - frees force feedback portion of input device 357 * @dev: input device supporting force feedback 358 * 359 * This function is only needed in error path as input core will 360 * automatically free force feedback structures when device is 361 * destroyed. 362 */ 363 void input_ff_destroy(struct input_dev *dev) 364 { 365 clear_bit(EV_FF, dev->evbit); 366 if (dev->ff) { 367 if (dev->ff->destroy) 368 dev->ff->destroy(dev->ff); 369 kfree(dev->ff->private); 370 kfree(dev->ff); 371 dev->ff = NULL; 372 } 373 } 374 EXPORT_SYMBOL_GPL(input_ff_destroy); 375