ff-core.c 8.9 KB

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