ff-core.c 8.5 KB

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