ff-core.c 9.0 KB

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