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> 31509ca1a9SAnssi Hannula 32509ca1a9SAnssi Hannula /* 33509ca1a9SAnssi Hannula * Check that the effect_id is a valid effect and whether the user 34509ca1a9SAnssi Hannula * is the owner 35509ca1a9SAnssi Hannula */ 36509ca1a9SAnssi Hannula static int check_effect_access(struct ff_device *ff, int effect_id, 37509ca1a9SAnssi Hannula struct file *file) 38509ca1a9SAnssi Hannula { 39509ca1a9SAnssi Hannula if (effect_id < 0 || effect_id >= ff->max_effects || 40509ca1a9SAnssi Hannula !ff->effect_owners[effect_id]) 41509ca1a9SAnssi Hannula return -EINVAL; 42509ca1a9SAnssi Hannula 43509ca1a9SAnssi Hannula if (file && ff->effect_owners[effect_id] != file) 44509ca1a9SAnssi Hannula return -EACCES; 45509ca1a9SAnssi Hannula 46509ca1a9SAnssi Hannula return 0; 47509ca1a9SAnssi Hannula } 48509ca1a9SAnssi Hannula 49509ca1a9SAnssi Hannula /* 50509ca1a9SAnssi Hannula * Checks whether 2 effects can be combined together 51509ca1a9SAnssi Hannula */ 52509ca1a9SAnssi Hannula static inline int check_effects_compatible(struct ff_effect *e1, 53509ca1a9SAnssi Hannula struct ff_effect *e2) 54509ca1a9SAnssi Hannula { 55509ca1a9SAnssi Hannula return e1->type == e2->type && 56509ca1a9SAnssi Hannula (e1->type != FF_PERIODIC || 57509ca1a9SAnssi Hannula e1->u.periodic.waveform == e2->u.periodic.waveform); 58509ca1a9SAnssi Hannula } 59509ca1a9SAnssi Hannula 60509ca1a9SAnssi Hannula /* 61509ca1a9SAnssi Hannula * Convert an effect into compatible one 62509ca1a9SAnssi Hannula */ 63509ca1a9SAnssi Hannula static int compat_effect(struct ff_device *ff, struct ff_effect *effect) 64509ca1a9SAnssi Hannula { 65509ca1a9SAnssi Hannula int magnitude; 66509ca1a9SAnssi Hannula 67509ca1a9SAnssi Hannula switch (effect->type) { 68509ca1a9SAnssi Hannula case FF_RUMBLE: 69509ca1a9SAnssi Hannula if (!test_bit(FF_PERIODIC, ff->ffbit)) 70509ca1a9SAnssi Hannula return -EINVAL; 71509ca1a9SAnssi Hannula 72509ca1a9SAnssi Hannula /* 73509ca1a9SAnssi Hannula * calculate manginude of sine wave as average of rumble's 74509ca1a9SAnssi Hannula * 2/3 of strong magnitude and 1/3 of weak magnitude 75509ca1a9SAnssi Hannula */ 76509ca1a9SAnssi Hannula magnitude = effect->u.rumble.strong_magnitude / 3 + 77509ca1a9SAnssi Hannula effect->u.rumble.weak_magnitude / 6; 78509ca1a9SAnssi Hannula 79509ca1a9SAnssi Hannula effect->type = FF_PERIODIC; 80509ca1a9SAnssi Hannula effect->u.periodic.waveform = FF_SINE; 81509ca1a9SAnssi Hannula effect->u.periodic.period = 50; 82509ca1a9SAnssi Hannula effect->u.periodic.magnitude = max(magnitude, 0x7fff); 83509ca1a9SAnssi Hannula effect->u.periodic.offset = 0; 84509ca1a9SAnssi Hannula effect->u.periodic.phase = 0; 85509ca1a9SAnssi Hannula effect->u.periodic.envelope.attack_length = 0; 86509ca1a9SAnssi Hannula effect->u.periodic.envelope.attack_level = 0; 87509ca1a9SAnssi Hannula effect->u.periodic.envelope.fade_length = 0; 88509ca1a9SAnssi Hannula effect->u.periodic.envelope.fade_level = 0; 89509ca1a9SAnssi Hannula 90509ca1a9SAnssi Hannula return 0; 91509ca1a9SAnssi Hannula 92509ca1a9SAnssi Hannula default: 93509ca1a9SAnssi Hannula /* Let driver handle conversion */ 94509ca1a9SAnssi Hannula return 0; 95509ca1a9SAnssi Hannula } 96509ca1a9SAnssi Hannula } 97509ca1a9SAnssi Hannula 98509ca1a9SAnssi Hannula /** 99509ca1a9SAnssi Hannula * input_ff_upload() - upload effect into force-feedback device 100509ca1a9SAnssi Hannula * @dev: input device 101509ca1a9SAnssi Hannula * @effect: effect to be uploaded 102509ca1a9SAnssi Hannula * @file: owner of the effect 103509ca1a9SAnssi Hannula */ 104509ca1a9SAnssi Hannula int input_ff_upload(struct input_dev *dev, struct ff_effect *effect, 105509ca1a9SAnssi Hannula struct file *file) 106509ca1a9SAnssi Hannula { 107509ca1a9SAnssi Hannula struct ff_device *ff = dev->ff; 108509ca1a9SAnssi Hannula struct ff_effect *old; 109509ca1a9SAnssi Hannula int ret = 0; 110509ca1a9SAnssi Hannula int id; 111509ca1a9SAnssi Hannula 112509ca1a9SAnssi Hannula if (!test_bit(EV_FF, dev->evbit)) 113509ca1a9SAnssi Hannula return -ENOSYS; 114509ca1a9SAnssi Hannula 115509ca1a9SAnssi Hannula if (effect->type < FF_EFFECT_MIN || effect->type > FF_EFFECT_MAX || 116509ca1a9SAnssi Hannula !test_bit(effect->type, dev->ffbit)) { 117509ca1a9SAnssi Hannula debug("invalid or not supported effect type in upload"); 118509ca1a9SAnssi Hannula return -EINVAL; 119509ca1a9SAnssi Hannula } 120509ca1a9SAnssi Hannula 121509ca1a9SAnssi Hannula if (effect->type == FF_PERIODIC && 122509ca1a9SAnssi Hannula (effect->u.periodic.waveform < FF_WAVEFORM_MIN || 123509ca1a9SAnssi Hannula effect->u.periodic.waveform > FF_WAVEFORM_MAX || 124509ca1a9SAnssi Hannula !test_bit(effect->u.periodic.waveform, dev->ffbit))) { 125509ca1a9SAnssi Hannula debug("invalid or not supported wave form in upload"); 126509ca1a9SAnssi Hannula return -EINVAL; 127509ca1a9SAnssi Hannula } 128509ca1a9SAnssi Hannula 129509ca1a9SAnssi Hannula if (!test_bit(effect->type, ff->ffbit)) { 130509ca1a9SAnssi Hannula ret = compat_effect(ff, effect); 131509ca1a9SAnssi Hannula if (ret) 132509ca1a9SAnssi Hannula return ret; 133509ca1a9SAnssi Hannula } 134509ca1a9SAnssi Hannula 135509ca1a9SAnssi Hannula mutex_lock(&ff->mutex); 136509ca1a9SAnssi Hannula 137509ca1a9SAnssi Hannula if (effect->id == -1) { 138509ca1a9SAnssi Hannula for (id = 0; id < ff->max_effects; id++) 139509ca1a9SAnssi Hannula if (!ff->effect_owners[id]) 140509ca1a9SAnssi Hannula break; 141509ca1a9SAnssi Hannula 142509ca1a9SAnssi Hannula if (id >= ff->max_effects) { 143509ca1a9SAnssi Hannula ret = -ENOSPC; 144509ca1a9SAnssi Hannula goto out; 145509ca1a9SAnssi Hannula } 146509ca1a9SAnssi Hannula 147509ca1a9SAnssi Hannula effect->id = id; 148509ca1a9SAnssi Hannula old = NULL; 149509ca1a9SAnssi Hannula 150509ca1a9SAnssi Hannula } else { 151509ca1a9SAnssi Hannula id = effect->id; 152509ca1a9SAnssi Hannula 153509ca1a9SAnssi Hannula ret = check_effect_access(ff, id, file); 154509ca1a9SAnssi Hannula if (ret) 155509ca1a9SAnssi Hannula goto out; 156509ca1a9SAnssi Hannula 157509ca1a9SAnssi Hannula old = &ff->effects[id]; 158509ca1a9SAnssi Hannula 159509ca1a9SAnssi Hannula if (!check_effects_compatible(effect, old)) { 160509ca1a9SAnssi Hannula ret = -EINVAL; 161509ca1a9SAnssi Hannula goto out; 162509ca1a9SAnssi Hannula } 163509ca1a9SAnssi Hannula } 164509ca1a9SAnssi Hannula 165509ca1a9SAnssi Hannula ret = ff->upload(dev, effect, old); 166509ca1a9SAnssi Hannula if (ret) 167509ca1a9SAnssi Hannula goto out; 168509ca1a9SAnssi Hannula 169*656acd2bSDmitry Torokhov spin_lock_irq(&dev->event_lock); 170509ca1a9SAnssi Hannula ff->effects[id] = *effect; 171509ca1a9SAnssi Hannula ff->effect_owners[id] = file; 172*656acd2bSDmitry Torokhov spin_unlock_irq(&dev->event_lock); 173509ca1a9SAnssi Hannula 174509ca1a9SAnssi Hannula out: 175509ca1a9SAnssi Hannula mutex_unlock(&ff->mutex); 176509ca1a9SAnssi Hannula return ret; 177509ca1a9SAnssi Hannula } 178509ca1a9SAnssi Hannula EXPORT_SYMBOL_GPL(input_ff_upload); 179509ca1a9SAnssi Hannula 180509ca1a9SAnssi Hannula /* 181509ca1a9SAnssi Hannula * Erases the effect if the requester is also the effect owner. The mutex 182509ca1a9SAnssi Hannula * should already be locked before calling this function. 183509ca1a9SAnssi Hannula */ 184509ca1a9SAnssi Hannula static int erase_effect(struct input_dev *dev, int effect_id, 185509ca1a9SAnssi Hannula struct file *file) 186509ca1a9SAnssi Hannula { 187509ca1a9SAnssi Hannula struct ff_device *ff = dev->ff; 188509ca1a9SAnssi Hannula int error; 189509ca1a9SAnssi Hannula 190509ca1a9SAnssi Hannula error = check_effect_access(ff, effect_id, file); 191509ca1a9SAnssi Hannula if (error) 192509ca1a9SAnssi Hannula return error; 193509ca1a9SAnssi Hannula 194*656acd2bSDmitry Torokhov spin_lock_irq(&dev->event_lock); 195509ca1a9SAnssi Hannula ff->playback(dev, effect_id, 0); 196*656acd2bSDmitry Torokhov ff->effect_owners[effect_id] = NULL; 197*656acd2bSDmitry Torokhov spin_unlock_irq(&dev->event_lock); 198509ca1a9SAnssi Hannula 199509ca1a9SAnssi Hannula if (ff->erase) { 200509ca1a9SAnssi Hannula error = ff->erase(dev, effect_id); 201*656acd2bSDmitry Torokhov if (error) { 202*656acd2bSDmitry Torokhov spin_lock_irq(&dev->event_lock); 203*656acd2bSDmitry Torokhov ff->effect_owners[effect_id] = file; 204*656acd2bSDmitry Torokhov spin_unlock_irq(&dev->event_lock); 205*656acd2bSDmitry Torokhov 206509ca1a9SAnssi Hannula return error; 207509ca1a9SAnssi Hannula } 208*656acd2bSDmitry Torokhov } 209509ca1a9SAnssi Hannula 210509ca1a9SAnssi Hannula return 0; 211509ca1a9SAnssi Hannula } 212509ca1a9SAnssi Hannula 213509ca1a9SAnssi Hannula /** 214e4477d2dSRandy Dunlap * input_ff_erase - erase a force-feedback effect from device 215509ca1a9SAnssi Hannula * @dev: input device to erase effect from 216509ca1a9SAnssi Hannula * @effect_id: id of the ffect to be erased 217509ca1a9SAnssi Hannula * @file: purported owner of the request 218509ca1a9SAnssi Hannula * 219509ca1a9SAnssi Hannula * This function erases a force-feedback effect from specified device. 220509ca1a9SAnssi Hannula * The effect will only be erased if it was uploaded through the same 221509ca1a9SAnssi Hannula * file handle that is requesting erase. 222509ca1a9SAnssi Hannula */ 223509ca1a9SAnssi Hannula int input_ff_erase(struct input_dev *dev, int effect_id, struct file *file) 224509ca1a9SAnssi Hannula { 225509ca1a9SAnssi Hannula struct ff_device *ff = dev->ff; 226509ca1a9SAnssi Hannula int ret; 227509ca1a9SAnssi Hannula 228509ca1a9SAnssi Hannula if (!test_bit(EV_FF, dev->evbit)) 229509ca1a9SAnssi Hannula return -ENOSYS; 230509ca1a9SAnssi Hannula 231509ca1a9SAnssi Hannula mutex_lock(&ff->mutex); 232509ca1a9SAnssi Hannula ret = erase_effect(dev, effect_id, file); 233509ca1a9SAnssi Hannula mutex_unlock(&ff->mutex); 234509ca1a9SAnssi Hannula 235509ca1a9SAnssi Hannula return ret; 236509ca1a9SAnssi Hannula } 237509ca1a9SAnssi Hannula EXPORT_SYMBOL_GPL(input_ff_erase); 238509ca1a9SAnssi Hannula 239509ca1a9SAnssi Hannula /* 240509ca1a9SAnssi Hannula * flush_effects - erase all effects owned by a file handle 241509ca1a9SAnssi Hannula */ 242509ca1a9SAnssi Hannula static int flush_effects(struct input_dev *dev, struct file *file) 243509ca1a9SAnssi Hannula { 244509ca1a9SAnssi Hannula struct ff_device *ff = dev->ff; 245509ca1a9SAnssi Hannula int i; 246509ca1a9SAnssi Hannula 247509ca1a9SAnssi Hannula debug("flushing now"); 248509ca1a9SAnssi Hannula 249509ca1a9SAnssi Hannula mutex_lock(&ff->mutex); 250509ca1a9SAnssi Hannula 251509ca1a9SAnssi Hannula for (i = 0; i < ff->max_effects; i++) 252509ca1a9SAnssi Hannula erase_effect(dev, i, file); 253509ca1a9SAnssi Hannula 254509ca1a9SAnssi Hannula mutex_unlock(&ff->mutex); 255509ca1a9SAnssi Hannula 256509ca1a9SAnssi Hannula return 0; 257509ca1a9SAnssi Hannula } 258509ca1a9SAnssi Hannula 259509ca1a9SAnssi Hannula /** 260509ca1a9SAnssi Hannula * input_ff_event() - generic handler for force-feedback events 261509ca1a9SAnssi Hannula * @dev: input device to send the effect to 262509ca1a9SAnssi Hannula * @type: event type (anything but EV_FF is ignored) 263509ca1a9SAnssi Hannula * @code: event code 264509ca1a9SAnssi Hannula * @value: event value 265509ca1a9SAnssi Hannula */ 266509ca1a9SAnssi Hannula int input_ff_event(struct input_dev *dev, unsigned int type, 267509ca1a9SAnssi Hannula unsigned int code, int value) 268509ca1a9SAnssi Hannula { 269509ca1a9SAnssi Hannula struct ff_device *ff = dev->ff; 270509ca1a9SAnssi Hannula 271509ca1a9SAnssi Hannula if (type != EV_FF) 272509ca1a9SAnssi Hannula return 0; 273509ca1a9SAnssi Hannula 274509ca1a9SAnssi Hannula switch (code) { 275509ca1a9SAnssi Hannula case FF_GAIN: 276509ca1a9SAnssi Hannula if (!test_bit(FF_GAIN, dev->ffbit) || value > 0xffff) 277509ca1a9SAnssi Hannula break; 278509ca1a9SAnssi Hannula 279509ca1a9SAnssi Hannula ff->set_gain(dev, value); 280509ca1a9SAnssi Hannula break; 281509ca1a9SAnssi Hannula 282509ca1a9SAnssi Hannula case FF_AUTOCENTER: 283509ca1a9SAnssi Hannula if (!test_bit(FF_AUTOCENTER, dev->ffbit) || value > 0xffff) 284509ca1a9SAnssi Hannula break; 285509ca1a9SAnssi Hannula 286509ca1a9SAnssi Hannula ff->set_autocenter(dev, value); 287509ca1a9SAnssi Hannula break; 288509ca1a9SAnssi Hannula 289509ca1a9SAnssi Hannula default: 290dec3eb01SDmitry Torokhov if (check_effect_access(ff, code, NULL) == 0) 291509ca1a9SAnssi Hannula ff->playback(dev, code, value); 292509ca1a9SAnssi Hannula break; 293509ca1a9SAnssi Hannula } 294509ca1a9SAnssi Hannula 295509ca1a9SAnssi Hannula return 0; 296509ca1a9SAnssi Hannula } 297509ca1a9SAnssi Hannula EXPORT_SYMBOL_GPL(input_ff_event); 298509ca1a9SAnssi Hannula 299509ca1a9SAnssi Hannula /** 300509ca1a9SAnssi Hannula * input_ff_create() - create force-feedback device 301509ca1a9SAnssi Hannula * @dev: input device supporting force-feedback 302509ca1a9SAnssi Hannula * @max_effects: maximum number of effects supported by the device 303509ca1a9SAnssi Hannula * 304509ca1a9SAnssi Hannula * This function allocates all necessary memory for a force feedback 305509ca1a9SAnssi Hannula * portion of an input device and installs all default handlers. 306509ca1a9SAnssi Hannula * @dev->ffbit should be already set up before calling this function. 307509ca1a9SAnssi Hannula * Once ff device is created you need to setup its upload, erase, 308509ca1a9SAnssi Hannula * playback and other handlers before registering input device 309509ca1a9SAnssi Hannula */ 310509ca1a9SAnssi Hannula int input_ff_create(struct input_dev *dev, int max_effects) 311509ca1a9SAnssi Hannula { 312509ca1a9SAnssi Hannula struct ff_device *ff; 313509ca1a9SAnssi Hannula int i; 314509ca1a9SAnssi Hannula 315509ca1a9SAnssi Hannula if (!max_effects) { 316509ca1a9SAnssi Hannula printk(KERN_ERR 317509ca1a9SAnssi Hannula "ff-core: cannot allocate device without any effects\n"); 318509ca1a9SAnssi Hannula return -EINVAL; 319509ca1a9SAnssi Hannula } 320509ca1a9SAnssi Hannula 321509ca1a9SAnssi Hannula ff = kzalloc(sizeof(struct ff_device) + 322509ca1a9SAnssi Hannula max_effects * sizeof(struct file *), GFP_KERNEL); 323509ca1a9SAnssi Hannula if (!ff) 324509ca1a9SAnssi Hannula return -ENOMEM; 325509ca1a9SAnssi Hannula 326509ca1a9SAnssi Hannula ff->effects = kcalloc(max_effects, sizeof(struct ff_effect), 327509ca1a9SAnssi Hannula GFP_KERNEL); 328509ca1a9SAnssi Hannula if (!ff->effects) { 329509ca1a9SAnssi Hannula kfree(ff); 330509ca1a9SAnssi Hannula return -ENOMEM; 331509ca1a9SAnssi Hannula } 332509ca1a9SAnssi Hannula 333509ca1a9SAnssi Hannula ff->max_effects = max_effects; 334509ca1a9SAnssi Hannula mutex_init(&ff->mutex); 335509ca1a9SAnssi Hannula 336509ca1a9SAnssi Hannula dev->ff = ff; 337509ca1a9SAnssi Hannula dev->flush = flush_effects; 338509ca1a9SAnssi Hannula dev->event = input_ff_event; 339509ca1a9SAnssi Hannula set_bit(EV_FF, dev->evbit); 340509ca1a9SAnssi Hannula 341509ca1a9SAnssi Hannula /* Copy "true" bits into ff device bitmap */ 342509ca1a9SAnssi Hannula for (i = 0; i <= FF_MAX; i++) 343509ca1a9SAnssi Hannula if (test_bit(i, dev->ffbit)) 344509ca1a9SAnssi Hannula set_bit(i, ff->ffbit); 345509ca1a9SAnssi Hannula 346509ca1a9SAnssi Hannula /* we can emulate RUMBLE with periodic effects */ 347509ca1a9SAnssi Hannula if (test_bit(FF_PERIODIC, ff->ffbit)) 348509ca1a9SAnssi Hannula set_bit(FF_RUMBLE, dev->ffbit); 349509ca1a9SAnssi Hannula 350509ca1a9SAnssi Hannula return 0; 351509ca1a9SAnssi Hannula } 352509ca1a9SAnssi Hannula EXPORT_SYMBOL_GPL(input_ff_create); 353509ca1a9SAnssi Hannula 354509ca1a9SAnssi Hannula /** 355509ca1a9SAnssi Hannula * input_ff_free() - frees force feedback portion of input device 356e4477d2dSRandy Dunlap * @dev: input device supporting force feedback 357509ca1a9SAnssi Hannula * 358509ca1a9SAnssi Hannula * This function is only needed in error path as input core will 359509ca1a9SAnssi Hannula * automatically free force feedback structures when device is 360509ca1a9SAnssi Hannula * destroyed. 361509ca1a9SAnssi Hannula */ 362509ca1a9SAnssi Hannula void input_ff_destroy(struct input_dev *dev) 363509ca1a9SAnssi Hannula { 364509ca1a9SAnssi Hannula clear_bit(EV_FF, dev->evbit); 365509ca1a9SAnssi Hannula if (dev->ff) { 366509ca1a9SAnssi Hannula if (dev->ff->destroy) 367509ca1a9SAnssi Hannula dev->ff->destroy(dev->ff); 368509ca1a9SAnssi Hannula kfree(dev->ff->private); 369509ca1a9SAnssi Hannula kfree(dev->ff); 370509ca1a9SAnssi Hannula dev->ff = NULL; 371509ca1a9SAnssi Hannula } 372509ca1a9SAnssi Hannula } 373509ca1a9SAnssi Hannula EXPORT_SYMBOL_GPL(input_ff_destroy); 374