1509ca1a9SAnssi Hannula /* 2509ca1a9SAnssi Hannula * Force feedback support for Linux input subsystem 3509ca1a9SAnssi Hannula * 4509ca1a9SAnssi Hannula * Copyright (c) 2006 Anssi Hannula <anssi.hannula@gmail.com> 5509ca1a9SAnssi Hannula * Copyright (c) 2006 Dmitry Torokhov <dtor@mail.ru> 6509ca1a9SAnssi Hannula */ 7509ca1a9SAnssi Hannula 8509ca1a9SAnssi Hannula /* 9509ca1a9SAnssi Hannula * This program is free software; you can redistribute it and/or modify 10509ca1a9SAnssi Hannula * it under the terms of the GNU General Public License as published by 11509ca1a9SAnssi Hannula * the Free Software Foundation; either version 2 of the License, or 12509ca1a9SAnssi Hannula * (at your option) any later version. 13509ca1a9SAnssi Hannula * 14509ca1a9SAnssi Hannula * This program is distributed in the hope that it will be useful, 15509ca1a9SAnssi Hannula * but WITHOUT ANY WARRANTY; without even the implied warranty of 16509ca1a9SAnssi Hannula * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17509ca1a9SAnssi Hannula * GNU General Public License for more details. 18509ca1a9SAnssi Hannula * 19509ca1a9SAnssi Hannula * You should have received a copy of the GNU General Public License 20509ca1a9SAnssi Hannula * along with this program; if not, write to the Free Software 21509ca1a9SAnssi Hannula * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 22509ca1a9SAnssi Hannula */ 23509ca1a9SAnssi Hannula 24509ca1a9SAnssi Hannula /* #define DEBUG */ 25509ca1a9SAnssi Hannula 26509ca1a9SAnssi Hannula #define debug(format, arg...) pr_debug("ff-core: " format "\n", ## arg) 27509ca1a9SAnssi Hannula 28509ca1a9SAnssi Hannula #include <linux/input.h> 29509ca1a9SAnssi Hannula #include <linux/module.h> 30509ca1a9SAnssi Hannula #include <linux/mutex.h> 3183680cdbSGeert Uytterhoeven #include <linux/sched.h> 32509ca1a9SAnssi Hannula 33509ca1a9SAnssi Hannula /* 34509ca1a9SAnssi Hannula * Check that the effect_id is a valid effect and whether the user 35509ca1a9SAnssi Hannula * is the owner 36509ca1a9SAnssi Hannula */ 37509ca1a9SAnssi Hannula static int check_effect_access(struct ff_device *ff, int effect_id, 38509ca1a9SAnssi Hannula struct file *file) 39509ca1a9SAnssi Hannula { 40509ca1a9SAnssi Hannula if (effect_id < 0 || effect_id >= ff->max_effects || 41509ca1a9SAnssi Hannula !ff->effect_owners[effect_id]) 42509ca1a9SAnssi Hannula return -EINVAL; 43509ca1a9SAnssi Hannula 44509ca1a9SAnssi Hannula if (file && ff->effect_owners[effect_id] != file) 45509ca1a9SAnssi Hannula return -EACCES; 46509ca1a9SAnssi Hannula 47509ca1a9SAnssi Hannula return 0; 48509ca1a9SAnssi Hannula } 49509ca1a9SAnssi Hannula 50509ca1a9SAnssi Hannula /* 51509ca1a9SAnssi Hannula * Checks whether 2 effects can be combined together 52509ca1a9SAnssi Hannula */ 53509ca1a9SAnssi Hannula static inline int check_effects_compatible(struct ff_effect *e1, 54509ca1a9SAnssi Hannula struct ff_effect *e2) 55509ca1a9SAnssi Hannula { 56509ca1a9SAnssi Hannula return e1->type == e2->type && 57509ca1a9SAnssi Hannula (e1->type != FF_PERIODIC || 58509ca1a9SAnssi Hannula e1->u.periodic.waveform == e2->u.periodic.waveform); 59509ca1a9SAnssi Hannula } 60509ca1a9SAnssi Hannula 61509ca1a9SAnssi Hannula /* 62509ca1a9SAnssi Hannula * Convert an effect into compatible one 63509ca1a9SAnssi Hannula */ 64509ca1a9SAnssi Hannula static int compat_effect(struct ff_device *ff, struct ff_effect *effect) 65509ca1a9SAnssi Hannula { 66509ca1a9SAnssi Hannula int magnitude; 67509ca1a9SAnssi Hannula 68509ca1a9SAnssi Hannula switch (effect->type) { 69509ca1a9SAnssi Hannula case FF_RUMBLE: 70509ca1a9SAnssi Hannula if (!test_bit(FF_PERIODIC, ff->ffbit)) 71509ca1a9SAnssi Hannula return -EINVAL; 72509ca1a9SAnssi Hannula 73509ca1a9SAnssi Hannula /* 74509ca1a9SAnssi Hannula * calculate manginude of sine wave as average of rumble's 75509ca1a9SAnssi Hannula * 2/3 of strong magnitude and 1/3 of weak magnitude 76509ca1a9SAnssi Hannula */ 77509ca1a9SAnssi Hannula magnitude = effect->u.rumble.strong_magnitude / 3 + 78509ca1a9SAnssi Hannula effect->u.rumble.weak_magnitude / 6; 79509ca1a9SAnssi Hannula 80509ca1a9SAnssi Hannula effect->type = FF_PERIODIC; 81509ca1a9SAnssi Hannula effect->u.periodic.waveform = FF_SINE; 82509ca1a9SAnssi Hannula effect->u.periodic.period = 50; 83509ca1a9SAnssi Hannula effect->u.periodic.magnitude = max(magnitude, 0x7fff); 84509ca1a9SAnssi Hannula effect->u.periodic.offset = 0; 85509ca1a9SAnssi Hannula effect->u.periodic.phase = 0; 86509ca1a9SAnssi Hannula effect->u.periodic.envelope.attack_length = 0; 87509ca1a9SAnssi Hannula effect->u.periodic.envelope.attack_level = 0; 88509ca1a9SAnssi Hannula effect->u.periodic.envelope.fade_length = 0; 89509ca1a9SAnssi Hannula effect->u.periodic.envelope.fade_level = 0; 90509ca1a9SAnssi Hannula 91509ca1a9SAnssi Hannula return 0; 92509ca1a9SAnssi Hannula 93509ca1a9SAnssi Hannula default: 94509ca1a9SAnssi Hannula /* Let driver handle conversion */ 95509ca1a9SAnssi Hannula return 0; 96509ca1a9SAnssi Hannula } 97509ca1a9SAnssi Hannula } 98509ca1a9SAnssi Hannula 99509ca1a9SAnssi Hannula /** 100509ca1a9SAnssi Hannula * input_ff_upload() - upload effect into force-feedback device 101509ca1a9SAnssi Hannula * @dev: input device 102509ca1a9SAnssi Hannula * @effect: effect to be uploaded 103509ca1a9SAnssi Hannula * @file: owner of the effect 104509ca1a9SAnssi Hannula */ 105509ca1a9SAnssi Hannula int input_ff_upload(struct input_dev *dev, struct ff_effect *effect, 106509ca1a9SAnssi Hannula struct file *file) 107509ca1a9SAnssi Hannula { 108509ca1a9SAnssi Hannula struct ff_device *ff = dev->ff; 109509ca1a9SAnssi Hannula struct ff_effect *old; 110509ca1a9SAnssi Hannula int ret = 0; 111509ca1a9SAnssi Hannula int id; 112509ca1a9SAnssi Hannula 113509ca1a9SAnssi Hannula if (!test_bit(EV_FF, dev->evbit)) 114509ca1a9SAnssi Hannula return -ENOSYS; 115509ca1a9SAnssi Hannula 116509ca1a9SAnssi Hannula if (effect->type < FF_EFFECT_MIN || effect->type > FF_EFFECT_MAX || 117509ca1a9SAnssi Hannula !test_bit(effect->type, dev->ffbit)) { 118509ca1a9SAnssi Hannula debug("invalid or not supported effect type in upload"); 119509ca1a9SAnssi Hannula return -EINVAL; 120509ca1a9SAnssi Hannula } 121509ca1a9SAnssi Hannula 122509ca1a9SAnssi Hannula if (effect->type == FF_PERIODIC && 123509ca1a9SAnssi Hannula (effect->u.periodic.waveform < FF_WAVEFORM_MIN || 124509ca1a9SAnssi Hannula effect->u.periodic.waveform > FF_WAVEFORM_MAX || 125509ca1a9SAnssi Hannula !test_bit(effect->u.periodic.waveform, dev->ffbit))) { 126509ca1a9SAnssi Hannula debug("invalid or not supported wave form in upload"); 127509ca1a9SAnssi Hannula return -EINVAL; 128509ca1a9SAnssi Hannula } 129509ca1a9SAnssi Hannula 130509ca1a9SAnssi Hannula if (!test_bit(effect->type, ff->ffbit)) { 131509ca1a9SAnssi Hannula ret = compat_effect(ff, effect); 132509ca1a9SAnssi Hannula if (ret) 133509ca1a9SAnssi Hannula return ret; 134509ca1a9SAnssi Hannula } 135509ca1a9SAnssi Hannula 136509ca1a9SAnssi Hannula mutex_lock(&ff->mutex); 137509ca1a9SAnssi Hannula 138509ca1a9SAnssi Hannula if (effect->id == -1) { 139509ca1a9SAnssi Hannula for (id = 0; id < ff->max_effects; id++) 140509ca1a9SAnssi Hannula if (!ff->effect_owners[id]) 141509ca1a9SAnssi Hannula break; 142509ca1a9SAnssi Hannula 143509ca1a9SAnssi Hannula if (id >= ff->max_effects) { 144509ca1a9SAnssi Hannula ret = -ENOSPC; 145509ca1a9SAnssi Hannula goto out; 146509ca1a9SAnssi Hannula } 147509ca1a9SAnssi Hannula 148509ca1a9SAnssi Hannula effect->id = id; 149509ca1a9SAnssi Hannula old = NULL; 150509ca1a9SAnssi Hannula 151509ca1a9SAnssi Hannula } else { 152509ca1a9SAnssi Hannula id = effect->id; 153509ca1a9SAnssi Hannula 154509ca1a9SAnssi Hannula ret = check_effect_access(ff, id, file); 155509ca1a9SAnssi Hannula if (ret) 156509ca1a9SAnssi Hannula goto out; 157509ca1a9SAnssi Hannula 158509ca1a9SAnssi Hannula old = &ff->effects[id]; 159509ca1a9SAnssi Hannula 160509ca1a9SAnssi Hannula if (!check_effects_compatible(effect, old)) { 161509ca1a9SAnssi Hannula ret = -EINVAL; 162509ca1a9SAnssi Hannula goto out; 163509ca1a9SAnssi Hannula } 164509ca1a9SAnssi Hannula } 165509ca1a9SAnssi Hannula 166509ca1a9SAnssi Hannula ret = ff->upload(dev, effect, old); 167509ca1a9SAnssi Hannula if (ret) 168509ca1a9SAnssi Hannula goto out; 169509ca1a9SAnssi Hannula 170656acd2bSDmitry Torokhov spin_lock_irq(&dev->event_lock); 171509ca1a9SAnssi Hannula ff->effects[id] = *effect; 172509ca1a9SAnssi Hannula ff->effect_owners[id] = file; 173656acd2bSDmitry Torokhov spin_unlock_irq(&dev->event_lock); 174509ca1a9SAnssi Hannula 175509ca1a9SAnssi Hannula out: 176509ca1a9SAnssi Hannula mutex_unlock(&ff->mutex); 177509ca1a9SAnssi Hannula return ret; 178509ca1a9SAnssi Hannula } 179509ca1a9SAnssi Hannula EXPORT_SYMBOL_GPL(input_ff_upload); 180509ca1a9SAnssi Hannula 181509ca1a9SAnssi Hannula /* 182509ca1a9SAnssi Hannula * Erases the effect if the requester is also the effect owner. The mutex 183509ca1a9SAnssi Hannula * should already be locked before calling this function. 184509ca1a9SAnssi Hannula */ 185509ca1a9SAnssi Hannula static int erase_effect(struct input_dev *dev, int effect_id, 186509ca1a9SAnssi Hannula struct file *file) 187509ca1a9SAnssi Hannula { 188509ca1a9SAnssi Hannula struct ff_device *ff = dev->ff; 189509ca1a9SAnssi Hannula int error; 190509ca1a9SAnssi Hannula 191509ca1a9SAnssi Hannula error = check_effect_access(ff, effect_id, file); 192509ca1a9SAnssi Hannula if (error) 193509ca1a9SAnssi Hannula return error; 194509ca1a9SAnssi Hannula 195656acd2bSDmitry Torokhov spin_lock_irq(&dev->event_lock); 196509ca1a9SAnssi Hannula ff->playback(dev, effect_id, 0); 197656acd2bSDmitry Torokhov ff->effect_owners[effect_id] = NULL; 198656acd2bSDmitry Torokhov spin_unlock_irq(&dev->event_lock); 199509ca1a9SAnssi Hannula 200509ca1a9SAnssi Hannula if (ff->erase) { 201509ca1a9SAnssi Hannula error = ff->erase(dev, effect_id); 202656acd2bSDmitry Torokhov if (error) { 203656acd2bSDmitry Torokhov spin_lock_irq(&dev->event_lock); 204656acd2bSDmitry Torokhov ff->effect_owners[effect_id] = file; 205656acd2bSDmitry Torokhov spin_unlock_irq(&dev->event_lock); 206656acd2bSDmitry Torokhov 207509ca1a9SAnssi Hannula return error; 208509ca1a9SAnssi Hannula } 209656acd2bSDmitry Torokhov } 210509ca1a9SAnssi Hannula 211509ca1a9SAnssi Hannula return 0; 212509ca1a9SAnssi Hannula } 213509ca1a9SAnssi Hannula 214509ca1a9SAnssi Hannula /** 215e4477d2dSRandy Dunlap * input_ff_erase - erase a force-feedback effect from device 216509ca1a9SAnssi Hannula * @dev: input device to erase effect from 217509ca1a9SAnssi Hannula * @effect_id: id of the ffect to be erased 218509ca1a9SAnssi Hannula * @file: purported owner of the request 219509ca1a9SAnssi Hannula * 220509ca1a9SAnssi Hannula * This function erases a force-feedback effect from specified device. 221509ca1a9SAnssi Hannula * The effect will only be erased if it was uploaded through the same 222509ca1a9SAnssi Hannula * file handle that is requesting erase. 223509ca1a9SAnssi Hannula */ 224509ca1a9SAnssi Hannula int input_ff_erase(struct input_dev *dev, int effect_id, struct file *file) 225509ca1a9SAnssi Hannula { 226509ca1a9SAnssi Hannula struct ff_device *ff = dev->ff; 227509ca1a9SAnssi Hannula int ret; 228509ca1a9SAnssi Hannula 229509ca1a9SAnssi Hannula if (!test_bit(EV_FF, dev->evbit)) 230509ca1a9SAnssi Hannula return -ENOSYS; 231509ca1a9SAnssi Hannula 232509ca1a9SAnssi Hannula mutex_lock(&ff->mutex); 233509ca1a9SAnssi Hannula ret = erase_effect(dev, effect_id, file); 234509ca1a9SAnssi Hannula mutex_unlock(&ff->mutex); 235509ca1a9SAnssi Hannula 236509ca1a9SAnssi Hannula return ret; 237509ca1a9SAnssi Hannula } 238509ca1a9SAnssi Hannula EXPORT_SYMBOL_GPL(input_ff_erase); 239509ca1a9SAnssi Hannula 240509ca1a9SAnssi Hannula /* 241509ca1a9SAnssi Hannula * flush_effects - erase all effects owned by a file handle 242509ca1a9SAnssi Hannula */ 243509ca1a9SAnssi Hannula static int flush_effects(struct input_dev *dev, struct file *file) 244509ca1a9SAnssi Hannula { 245509ca1a9SAnssi Hannula struct ff_device *ff = dev->ff; 246509ca1a9SAnssi Hannula int i; 247509ca1a9SAnssi Hannula 248509ca1a9SAnssi Hannula debug("flushing now"); 249509ca1a9SAnssi Hannula 250509ca1a9SAnssi Hannula mutex_lock(&ff->mutex); 251509ca1a9SAnssi Hannula 252509ca1a9SAnssi Hannula for (i = 0; i < ff->max_effects; i++) 253509ca1a9SAnssi Hannula erase_effect(dev, i, file); 254509ca1a9SAnssi Hannula 255509ca1a9SAnssi Hannula mutex_unlock(&ff->mutex); 256509ca1a9SAnssi Hannula 257509ca1a9SAnssi Hannula return 0; 258509ca1a9SAnssi Hannula } 259509ca1a9SAnssi Hannula 260509ca1a9SAnssi Hannula /** 261509ca1a9SAnssi Hannula * input_ff_event() - generic handler for force-feedback events 262509ca1a9SAnssi Hannula * @dev: input device to send the effect to 263509ca1a9SAnssi Hannula * @type: event type (anything but EV_FF is ignored) 264509ca1a9SAnssi Hannula * @code: event code 265509ca1a9SAnssi Hannula * @value: event value 266509ca1a9SAnssi Hannula */ 267509ca1a9SAnssi Hannula int input_ff_event(struct input_dev *dev, unsigned int type, 268509ca1a9SAnssi Hannula unsigned int code, int value) 269509ca1a9SAnssi Hannula { 270509ca1a9SAnssi Hannula struct ff_device *ff = dev->ff; 271509ca1a9SAnssi Hannula 272509ca1a9SAnssi Hannula if (type != EV_FF) 273509ca1a9SAnssi Hannula return 0; 274509ca1a9SAnssi Hannula 275509ca1a9SAnssi Hannula switch (code) { 276509ca1a9SAnssi Hannula case FF_GAIN: 277509ca1a9SAnssi Hannula if (!test_bit(FF_GAIN, dev->ffbit) || value > 0xffff) 278509ca1a9SAnssi Hannula break; 279509ca1a9SAnssi Hannula 280509ca1a9SAnssi Hannula ff->set_gain(dev, value); 281509ca1a9SAnssi Hannula break; 282509ca1a9SAnssi Hannula 283509ca1a9SAnssi Hannula case FF_AUTOCENTER: 284509ca1a9SAnssi Hannula if (!test_bit(FF_AUTOCENTER, dev->ffbit) || value > 0xffff) 285509ca1a9SAnssi Hannula break; 286509ca1a9SAnssi Hannula 287509ca1a9SAnssi Hannula ff->set_autocenter(dev, value); 288509ca1a9SAnssi Hannula break; 289509ca1a9SAnssi Hannula 290509ca1a9SAnssi Hannula default: 291dec3eb01SDmitry Torokhov if (check_effect_access(ff, code, NULL) == 0) 292509ca1a9SAnssi Hannula ff->playback(dev, code, value); 293509ca1a9SAnssi Hannula break; 294509ca1a9SAnssi Hannula } 295509ca1a9SAnssi Hannula 296509ca1a9SAnssi Hannula return 0; 297509ca1a9SAnssi Hannula } 298509ca1a9SAnssi Hannula EXPORT_SYMBOL_GPL(input_ff_event); 299509ca1a9SAnssi Hannula 300509ca1a9SAnssi Hannula /** 301509ca1a9SAnssi Hannula * input_ff_create() - create force-feedback device 302509ca1a9SAnssi Hannula * @dev: input device supporting force-feedback 303509ca1a9SAnssi Hannula * @max_effects: maximum number of effects supported by the device 304509ca1a9SAnssi Hannula * 305509ca1a9SAnssi Hannula * This function allocates all necessary memory for a force feedback 306509ca1a9SAnssi Hannula * portion of an input device and installs all default handlers. 307509ca1a9SAnssi Hannula * @dev->ffbit should be already set up before calling this function. 308509ca1a9SAnssi Hannula * Once ff device is created you need to setup its upload, erase, 309509ca1a9SAnssi Hannula * playback and other handlers before registering input device 310509ca1a9SAnssi Hannula */ 311509ca1a9SAnssi Hannula int input_ff_create(struct input_dev *dev, int max_effects) 312509ca1a9SAnssi Hannula { 313509ca1a9SAnssi Hannula struct ff_device *ff; 314509ca1a9SAnssi Hannula int i; 315509ca1a9SAnssi Hannula 316509ca1a9SAnssi Hannula if (!max_effects) { 317509ca1a9SAnssi Hannula printk(KERN_ERR 318509ca1a9SAnssi Hannula "ff-core: cannot allocate device without any effects\n"); 319509ca1a9SAnssi Hannula return -EINVAL; 320509ca1a9SAnssi Hannula } 321509ca1a9SAnssi Hannula 322509ca1a9SAnssi Hannula ff = kzalloc(sizeof(struct ff_device) + 323509ca1a9SAnssi Hannula max_effects * sizeof(struct file *), GFP_KERNEL); 324509ca1a9SAnssi Hannula if (!ff) 325509ca1a9SAnssi Hannula return -ENOMEM; 326509ca1a9SAnssi Hannula 327509ca1a9SAnssi Hannula ff->effects = kcalloc(max_effects, sizeof(struct ff_effect), 328509ca1a9SAnssi Hannula GFP_KERNEL); 329509ca1a9SAnssi Hannula if (!ff->effects) { 330509ca1a9SAnssi Hannula kfree(ff); 331509ca1a9SAnssi Hannula return -ENOMEM; 332509ca1a9SAnssi Hannula } 333509ca1a9SAnssi Hannula 334509ca1a9SAnssi Hannula ff->max_effects = max_effects; 335509ca1a9SAnssi Hannula mutex_init(&ff->mutex); 336509ca1a9SAnssi Hannula 337509ca1a9SAnssi Hannula dev->ff = ff; 338509ca1a9SAnssi Hannula dev->flush = flush_effects; 339509ca1a9SAnssi Hannula dev->event = input_ff_event; 340509ca1a9SAnssi Hannula set_bit(EV_FF, dev->evbit); 341509ca1a9SAnssi Hannula 342509ca1a9SAnssi Hannula /* Copy "true" bits into ff device bitmap */ 343509ca1a9SAnssi Hannula for (i = 0; i <= FF_MAX; i++) 344509ca1a9SAnssi Hannula if (test_bit(i, dev->ffbit)) 345509ca1a9SAnssi Hannula set_bit(i, ff->ffbit); 346509ca1a9SAnssi Hannula 347509ca1a9SAnssi Hannula /* we can emulate RUMBLE with periodic effects */ 348509ca1a9SAnssi Hannula if (test_bit(FF_PERIODIC, ff->ffbit)) 349509ca1a9SAnssi Hannula set_bit(FF_RUMBLE, dev->ffbit); 350509ca1a9SAnssi Hannula 351509ca1a9SAnssi Hannula return 0; 352509ca1a9SAnssi Hannula } 353509ca1a9SAnssi Hannula EXPORT_SYMBOL_GPL(input_ff_create); 354509ca1a9SAnssi Hannula 355509ca1a9SAnssi Hannula /** 356*721a730eSRoger Quadros * input_ff_destroy() - frees force feedback portion of input device 357e4477d2dSRandy Dunlap * @dev: input device supporting force feedback 358509ca1a9SAnssi Hannula * 359509ca1a9SAnssi Hannula * This function is only needed in error path as input core will 360509ca1a9SAnssi Hannula * automatically free force feedback structures when device is 361509ca1a9SAnssi Hannula * destroyed. 362509ca1a9SAnssi Hannula */ 363509ca1a9SAnssi Hannula void input_ff_destroy(struct input_dev *dev) 364509ca1a9SAnssi Hannula { 365509ca1a9SAnssi Hannula clear_bit(EV_FF, dev->evbit); 366509ca1a9SAnssi Hannula if (dev->ff) { 367509ca1a9SAnssi Hannula if (dev->ff->destroy) 368509ca1a9SAnssi Hannula dev->ff->destroy(dev->ff); 369509ca1a9SAnssi Hannula kfree(dev->ff->private); 370509ca1a9SAnssi Hannula kfree(dev->ff); 371509ca1a9SAnssi Hannula dev->ff = NULL; 372509ca1a9SAnssi Hannula } 373509ca1a9SAnssi Hannula } 374509ca1a9SAnssi Hannula EXPORT_SYMBOL_GPL(input_ff_destroy); 375