ff-core.c 8.7 KB

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