hid-tmff.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. /*
  2. * Force feedback support for various HID compliant devices by ThrustMaster:
  3. * ThrustMaster FireStorm Dual Power 2
  4. * and possibly others whose device ids haven't been added.
  5. *
  6. * Modified to support ThrustMaster devices by Zinx Verituse
  7. * on 2003-01-25 from the Logitech force feedback driver,
  8. * which is by Johann Deneux.
  9. *
  10. * Copyright (c) 2003 Zinx Verituse <zinx@epicsol.org>
  11. * Copyright (c) 2002 Johann Deneux
  12. */
  13. /*
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License as published by
  16. * the Free Software Foundation; either version 2 of the License, or
  17. * (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program; if not, write to the Free Software
  26. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  27. */
  28. #include <linux/input.h>
  29. #include <linux/sched.h>
  30. #undef DEBUG
  31. #include <linux/usb.h>
  32. #include <linux/circ_buf.h>
  33. #include "hid.h"
  34. #include "fixp-arith.h"
  35. /* Usages for thrustmaster devices I know about */
  36. #define THRUSTMASTER_USAGE_RUMBLE_LR (HID_UP_GENDESK | 0xbb)
  37. #define DELAY_CALC(t,delay) ((t) + (delay)*HZ/1000)
  38. /* Effect status */
  39. #define EFFECT_STARTED 0 /* Effect is going to play after some time */
  40. #define EFFECT_PLAYING 1 /* Effect is playing */
  41. #define EFFECT_USED 2
  42. /* For tmff_device::flags */
  43. #define DEVICE_CLOSING 0 /* The driver is being unitialised */
  44. /* Check that the current process can access an effect */
  45. #define CHECK_OWNERSHIP(effect) (current->pid == 0 \
  46. || effect.owner == current->pid)
  47. #define TMFF_CHECK_ID(id) ((id) >= 0 && (id) < TMFF_EFFECTS)
  48. #define TMFF_CHECK_OWNERSHIP(i, l) \
  49. (test_bit(EFFECT_USED, l->effects[i].flags) \
  50. && CHECK_OWNERSHIP(l->effects[i]))
  51. #define TMFF_EFFECTS 8
  52. struct tmff_effect {
  53. pid_t owner;
  54. struct ff_effect effect;
  55. unsigned long flags[1];
  56. unsigned int count; /* Number of times left to play */
  57. unsigned long play_at; /* When the effect starts to play */
  58. unsigned long stop_at; /* When the effect ends */
  59. };
  60. struct tmff_device {
  61. struct hid_device *hid;
  62. struct hid_report *report;
  63. struct hid_field *rumble;
  64. unsigned int effects_playing;
  65. struct tmff_effect effects[TMFF_EFFECTS];
  66. spinlock_t lock; /* device-level lock. Having locks on
  67. a per-effect basis could be nice, but
  68. isn't really necessary */
  69. unsigned long flags[1]; /* Contains various information about the
  70. state of the driver for this device */
  71. struct timer_list timer;
  72. };
  73. /* Callbacks */
  74. static void hid_tmff_exit(struct hid_device *hid);
  75. static int hid_tmff_event(struct hid_device *hid, struct input_dev *input,
  76. unsigned int type, unsigned int code, int value);
  77. static int hid_tmff_flush(struct input_dev *input, struct file *file);
  78. static int hid_tmff_upload_effect(struct input_dev *input,
  79. struct ff_effect *effect);
  80. static int hid_tmff_erase(struct input_dev *input, int id);
  81. /* Local functions */
  82. static void hid_tmff_recalculate_timer(struct tmff_device *tmff);
  83. static void hid_tmff_timer(unsigned long timer_data);
  84. int hid_tmff_init(struct hid_device *hid)
  85. {
  86. struct tmff_device *private;
  87. struct list_head *pos;
  88. struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
  89. private = kmalloc(sizeof(struct tmff_device), GFP_KERNEL);
  90. if (!private)
  91. return -ENOMEM;
  92. memset(private, 0, sizeof(struct tmff_device));
  93. hid->ff_private = private;
  94. /* Find the report to use */
  95. __list_for_each(pos, &hid->report_enum[HID_OUTPUT_REPORT].report_list) {
  96. struct hid_report *report = (struct hid_report *)pos;
  97. int fieldnum;
  98. for (fieldnum = 0; fieldnum < report->maxfield; ++fieldnum) {
  99. struct hid_field *field = report->field[fieldnum];
  100. if (field->maxusage <= 0)
  101. continue;
  102. switch (field->usage[0].hid) {
  103. case THRUSTMASTER_USAGE_RUMBLE_LR:
  104. if (field->report_count < 2) {
  105. warn("ignoring THRUSTMASTER_USAGE_RUMBLE_LR with report_count < 2");
  106. continue;
  107. }
  108. if (field->logical_maximum == field->logical_minimum) {
  109. warn("ignoring THRUSTMASTER_USAGE_RUMBLE_LR with logical_maximum == logical_minimum");
  110. continue;
  111. }
  112. if (private->report && private->report != report) {
  113. warn("ignoring THRUSTMASTER_USAGE_RUMBLE_LR in other report");
  114. continue;
  115. }
  116. if (private->rumble && private->rumble != field) {
  117. warn("ignoring duplicate THRUSTMASTER_USAGE_RUMBLE_LR");
  118. continue;
  119. }
  120. private->report = report;
  121. private->rumble = field;
  122. set_bit(FF_RUMBLE, hidinput->input.ffbit);
  123. break;
  124. default:
  125. warn("ignoring unknown output usage %08x", field->usage[0].hid);
  126. continue;
  127. }
  128. /* Fallthrough to here only when a valid usage is found */
  129. hidinput->input.upload_effect = hid_tmff_upload_effect;
  130. hidinput->input.flush = hid_tmff_flush;
  131. set_bit(EV_FF, hidinput->input.evbit);
  132. hidinput->input.ff_effects_max = TMFF_EFFECTS;
  133. }
  134. }
  135. private->hid = hid;
  136. spin_lock_init(&private->lock);
  137. init_timer(&private->timer);
  138. private->timer.data = (unsigned long)private;
  139. private->timer.function = hid_tmff_timer;
  140. /* Event and exit callbacks */
  141. hid->ff_exit = hid_tmff_exit;
  142. hid->ff_event = hid_tmff_event;
  143. info("Force feedback for ThrustMaster rumble pad devices by Zinx Verituse <zinx@epicsol.org>");
  144. return 0;
  145. }
  146. static void hid_tmff_exit(struct hid_device *hid)
  147. {
  148. struct tmff_device *tmff = hid->ff_private;
  149. unsigned long flags;
  150. spin_lock_irqsave(&tmff->lock, flags);
  151. set_bit(DEVICE_CLOSING, tmff->flags);
  152. del_timer_sync(&tmff->timer);
  153. spin_unlock_irqrestore(&tmff->lock, flags);
  154. kfree(tmff);
  155. }
  156. static int hid_tmff_event(struct hid_device *hid, struct input_dev *input,
  157. unsigned int type, unsigned int code, int value)
  158. {
  159. struct tmff_device *tmff = hid->ff_private;
  160. struct tmff_effect *effect = &tmff->effects[code];
  161. unsigned long flags;
  162. if (type != EV_FF)
  163. return -EINVAL;
  164. if (!TMFF_CHECK_ID(code))
  165. return -EINVAL;
  166. if (!TMFF_CHECK_OWNERSHIP(code, tmff))
  167. return -EACCES;
  168. if (value < 0)
  169. return -EINVAL;
  170. spin_lock_irqsave(&tmff->lock, flags);
  171. if (value > 0) {
  172. set_bit(EFFECT_STARTED, effect->flags);
  173. clear_bit(EFFECT_PLAYING, effect->flags);
  174. effect->count = value;
  175. effect->play_at = DELAY_CALC(jiffies, effect->effect.replay.delay);
  176. } else {
  177. clear_bit(EFFECT_STARTED, effect->flags);
  178. clear_bit(EFFECT_PLAYING, effect->flags);
  179. }
  180. hid_tmff_recalculate_timer(tmff);
  181. spin_unlock_irqrestore(&tmff->lock, flags);
  182. return 0;
  183. }
  184. /* Erase all effects this process owns */
  185. static int hid_tmff_flush(struct input_dev *dev, struct file *file)
  186. {
  187. struct hid_device *hid = dev->private;
  188. struct tmff_device *tmff = hid->ff_private;
  189. int i;
  190. for (i=0; i<dev->ff_effects_max; ++i)
  191. /* NOTE: no need to lock here. The only times EFFECT_USED is
  192. modified is when effects are uploaded or when an effect is
  193. erased. But a process cannot close its dev/input/eventX fd
  194. and perform ioctls on the same fd all at the same time */
  195. if (current->pid == tmff->effects[i].owner
  196. && test_bit(EFFECT_USED, tmff->effects[i].flags))
  197. if (hid_tmff_erase(dev, i))
  198. warn("erase effect %d failed", i);
  199. return 0;
  200. }
  201. static int hid_tmff_erase(struct input_dev *dev, int id)
  202. {
  203. struct hid_device *hid = dev->private;
  204. struct tmff_device *tmff = hid->ff_private;
  205. unsigned long flags;
  206. if (!TMFF_CHECK_ID(id))
  207. return -EINVAL;
  208. if (!TMFF_CHECK_OWNERSHIP(id, tmff))
  209. return -EACCES;
  210. spin_lock_irqsave(&tmff->lock, flags);
  211. tmff->effects[id].flags[0] = 0;
  212. hid_tmff_recalculate_timer(tmff);
  213. spin_unlock_irqrestore(&tmff->lock, flags);
  214. return 0;
  215. }
  216. static int hid_tmff_upload_effect(struct input_dev *input,
  217. struct ff_effect *effect)
  218. {
  219. struct hid_device *hid = input->private;
  220. struct tmff_device *tmff = hid->ff_private;
  221. int id;
  222. unsigned long flags;
  223. if (!test_bit(effect->type, input->ffbit))
  224. return -EINVAL;
  225. if (effect->id != -1 && !TMFF_CHECK_ID(effect->id))
  226. return -EINVAL;
  227. spin_lock_irqsave(&tmff->lock, flags);
  228. if (effect->id == -1) {
  229. /* Find a free effect */
  230. for (id = 0; id < TMFF_EFFECTS && test_bit(EFFECT_USED, tmff->effects[id].flags); ++id);
  231. if (id >= TMFF_EFFECTS) {
  232. spin_unlock_irqrestore(&tmff->lock, flags);
  233. return -ENOSPC;
  234. }
  235. effect->id = id;
  236. tmff->effects[id].owner = current->pid;
  237. tmff->effects[id].flags[0] = 0;
  238. set_bit(EFFECT_USED, tmff->effects[id].flags);
  239. } else {
  240. /* Re-uploading an owned effect, to change parameters */
  241. id = effect->id;
  242. clear_bit(EFFECT_PLAYING, tmff->effects[id].flags);
  243. }
  244. tmff->effects[id].effect = *effect;
  245. hid_tmff_recalculate_timer(tmff);
  246. spin_unlock_irqrestore(&tmff->lock, flags);
  247. return 0;
  248. }
  249. /* Start the timer for the next start/stop/delay */
  250. /* Always call this while tmff->lock is locked */
  251. static void hid_tmff_recalculate_timer(struct tmff_device *tmff)
  252. {
  253. int i;
  254. int events = 0;
  255. unsigned long next_time;
  256. next_time = 0; /* Shut up compiler's incorrect warning */
  257. /* Find the next change in an effect's status */
  258. for (i = 0; i < TMFF_EFFECTS; ++i) {
  259. struct tmff_effect *effect = &tmff->effects[i];
  260. unsigned long play_time;
  261. if (!test_bit(EFFECT_STARTED, effect->flags))
  262. continue;
  263. effect->stop_at = DELAY_CALC(effect->play_at, effect->effect.replay.length);
  264. if (!test_bit(EFFECT_PLAYING, effect->flags))
  265. play_time = effect->play_at;
  266. else
  267. play_time = effect->stop_at;
  268. events++;
  269. if (time_after(jiffies, play_time))
  270. play_time = jiffies;
  271. if (events == 1)
  272. next_time = play_time;
  273. else {
  274. if (time_after(next_time, play_time))
  275. next_time = play_time;
  276. }
  277. }
  278. if (!events && tmff->effects_playing) {
  279. /* Treat all effects turning off as an event */
  280. events = 1;
  281. next_time = jiffies;
  282. }
  283. if (!events) {
  284. /* No events, no time, no need for a timer. */
  285. del_timer_sync(&tmff->timer);
  286. return;
  287. }
  288. mod_timer(&tmff->timer, next_time);
  289. }
  290. /* Changes values from 0 to 0xffff into values from minimum to maximum */
  291. static inline int hid_tmff_scale(unsigned int in, int minimum, int maximum)
  292. {
  293. int ret;
  294. ret = (in * (maximum - minimum) / 0xffff) + minimum;
  295. if (ret < minimum)
  296. return minimum;
  297. if (ret > maximum)
  298. return maximum;
  299. return ret;
  300. }
  301. static void hid_tmff_timer(unsigned long timer_data)
  302. {
  303. struct tmff_device *tmff = (struct tmff_device *) timer_data;
  304. struct hid_device *hid = tmff->hid;
  305. unsigned long flags;
  306. int left = 0, right = 0; /* Rumbling */
  307. int i;
  308. spin_lock_irqsave(&tmff->lock, flags);
  309. tmff->effects_playing = 0;
  310. for (i = 0; i < TMFF_EFFECTS; ++i) {
  311. struct tmff_effect *effect = &tmff->effects[i];
  312. if (!test_bit(EFFECT_STARTED, effect->flags))
  313. continue;
  314. if (!time_after(jiffies, effect->play_at))
  315. continue;
  316. if (time_after(jiffies, effect->stop_at)) {
  317. dbg("Finished playing once %d", i);
  318. clear_bit(EFFECT_PLAYING, effect->flags);
  319. if (--effect->count <= 0) {
  320. dbg("Stopped %d", i);
  321. clear_bit(EFFECT_STARTED, effect->flags);
  322. continue;
  323. } else {
  324. dbg("Start again %d", i);
  325. effect->play_at = DELAY_CALC(jiffies, effect->effect.replay.delay);
  326. continue;
  327. }
  328. }
  329. ++tmff->effects_playing;
  330. set_bit(EFFECT_PLAYING, effect->flags);
  331. switch (effect->effect.type) {
  332. case FF_RUMBLE:
  333. right += effect->effect.u.rumble.strong_magnitude;
  334. left += effect->effect.u.rumble.weak_magnitude;
  335. break;
  336. default:
  337. BUG();
  338. break;
  339. }
  340. }
  341. left = hid_tmff_scale(left, tmff->rumble->logical_minimum, tmff->rumble->logical_maximum);
  342. right = hid_tmff_scale(right, tmff->rumble->logical_minimum, tmff->rumble->logical_maximum);
  343. if (left != tmff->rumble->value[0] || right != tmff->rumble->value[1]) {
  344. tmff->rumble->value[0] = left;
  345. tmff->rumble->value[1] = right;
  346. dbg("(left,right)=(%08x, %08x)", left, right);
  347. hid_submit_report(hid, tmff->report, USB_DIR_OUT);
  348. }
  349. if (!test_bit(DEVICE_CLOSING, tmff->flags))
  350. hid_tmff_recalculate_timer(tmff);
  351. spin_unlock_irqrestore(&tmff->lock, flags);
  352. }