hid-tmff.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  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. struct input_dev *input_dev = hidinput->input;
  90. private = kmalloc(sizeof(struct tmff_device), GFP_KERNEL);
  91. if (!private)
  92. return -ENOMEM;
  93. memset(private, 0, sizeof(struct tmff_device));
  94. hid->ff_private = private;
  95. /* Find the report to use */
  96. __list_for_each(pos, &hid->report_enum[HID_OUTPUT_REPORT].report_list) {
  97. struct hid_report *report = (struct hid_report *)pos;
  98. int fieldnum;
  99. for (fieldnum = 0; fieldnum < report->maxfield; ++fieldnum) {
  100. struct hid_field *field = report->field[fieldnum];
  101. if (field->maxusage <= 0)
  102. continue;
  103. switch (field->usage[0].hid) {
  104. case THRUSTMASTER_USAGE_RUMBLE_LR:
  105. if (field->report_count < 2) {
  106. warn("ignoring THRUSTMASTER_USAGE_RUMBLE_LR with report_count < 2");
  107. continue;
  108. }
  109. if (field->logical_maximum == field->logical_minimum) {
  110. warn("ignoring THRUSTMASTER_USAGE_RUMBLE_LR with logical_maximum == logical_minimum");
  111. continue;
  112. }
  113. if (private->report && private->report != report) {
  114. warn("ignoring THRUSTMASTER_USAGE_RUMBLE_LR in other report");
  115. continue;
  116. }
  117. if (private->rumble && private->rumble != field) {
  118. warn("ignoring duplicate THRUSTMASTER_USAGE_RUMBLE_LR");
  119. continue;
  120. }
  121. private->report = report;
  122. private->rumble = field;
  123. set_bit(FF_RUMBLE, input_dev->ffbit);
  124. break;
  125. default:
  126. warn("ignoring unknown output usage %08x", field->usage[0].hid);
  127. continue;
  128. }
  129. /* Fallthrough to here only when a valid usage is found */
  130. input_dev->upload_effect = hid_tmff_upload_effect;
  131. input_dev->flush = hid_tmff_flush;
  132. set_bit(EV_FF, input_dev->evbit);
  133. input_dev->ff_effects_max = TMFF_EFFECTS;
  134. }
  135. }
  136. private->hid = hid;
  137. spin_lock_init(&private->lock);
  138. init_timer(&private->timer);
  139. private->timer.data = (unsigned long)private;
  140. private->timer.function = hid_tmff_timer;
  141. /* Event and exit callbacks */
  142. hid->ff_exit = hid_tmff_exit;
  143. hid->ff_event = hid_tmff_event;
  144. info("Force feedback for ThrustMaster rumble pad devices by Zinx Verituse <zinx@epicsol.org>");
  145. return 0;
  146. }
  147. static void hid_tmff_exit(struct hid_device *hid)
  148. {
  149. struct tmff_device *tmff = hid->ff_private;
  150. unsigned long flags;
  151. spin_lock_irqsave(&tmff->lock, flags);
  152. set_bit(DEVICE_CLOSING, tmff->flags);
  153. del_timer_sync(&tmff->timer);
  154. spin_unlock_irqrestore(&tmff->lock, flags);
  155. kfree(tmff);
  156. }
  157. static int hid_tmff_event(struct hid_device *hid, struct input_dev *input,
  158. unsigned int type, unsigned int code, int value)
  159. {
  160. struct tmff_device *tmff = hid->ff_private;
  161. struct tmff_effect *effect = &tmff->effects[code];
  162. unsigned long flags;
  163. if (type != EV_FF)
  164. return -EINVAL;
  165. if (!TMFF_CHECK_ID(code))
  166. return -EINVAL;
  167. if (!TMFF_CHECK_OWNERSHIP(code, tmff))
  168. return -EACCES;
  169. if (value < 0)
  170. return -EINVAL;
  171. spin_lock_irqsave(&tmff->lock, flags);
  172. if (value > 0) {
  173. set_bit(EFFECT_STARTED, effect->flags);
  174. clear_bit(EFFECT_PLAYING, effect->flags);
  175. effect->count = value;
  176. effect->play_at = DELAY_CALC(jiffies, effect->effect.replay.delay);
  177. } else {
  178. clear_bit(EFFECT_STARTED, effect->flags);
  179. clear_bit(EFFECT_PLAYING, effect->flags);
  180. }
  181. hid_tmff_recalculate_timer(tmff);
  182. spin_unlock_irqrestore(&tmff->lock, flags);
  183. return 0;
  184. }
  185. /* Erase all effects this process owns */
  186. static int hid_tmff_flush(struct input_dev *dev, struct file *file)
  187. {
  188. struct hid_device *hid = dev->private;
  189. struct tmff_device *tmff = hid->ff_private;
  190. int i;
  191. for (i=0; i<dev->ff_effects_max; ++i)
  192. /* NOTE: no need to lock here. The only times EFFECT_USED is
  193. modified is when effects are uploaded or when an effect is
  194. erased. But a process cannot close its dev/input/eventX fd
  195. and perform ioctls on the same fd all at the same time */
  196. if (current->pid == tmff->effects[i].owner
  197. && test_bit(EFFECT_USED, tmff->effects[i].flags))
  198. if (hid_tmff_erase(dev, i))
  199. warn("erase effect %d failed", i);
  200. return 0;
  201. }
  202. static int hid_tmff_erase(struct input_dev *dev, int id)
  203. {
  204. struct hid_device *hid = dev->private;
  205. struct tmff_device *tmff = hid->ff_private;
  206. unsigned long flags;
  207. if (!TMFF_CHECK_ID(id))
  208. return -EINVAL;
  209. if (!TMFF_CHECK_OWNERSHIP(id, tmff))
  210. return -EACCES;
  211. spin_lock_irqsave(&tmff->lock, flags);
  212. tmff->effects[id].flags[0] = 0;
  213. hid_tmff_recalculate_timer(tmff);
  214. spin_unlock_irqrestore(&tmff->lock, flags);
  215. return 0;
  216. }
  217. static int hid_tmff_upload_effect(struct input_dev *input,
  218. struct ff_effect *effect)
  219. {
  220. struct hid_device *hid = input->private;
  221. struct tmff_device *tmff = hid->ff_private;
  222. int id;
  223. unsigned long flags;
  224. if (!test_bit(effect->type, input->ffbit))
  225. return -EINVAL;
  226. if (effect->id != -1 && !TMFF_CHECK_ID(effect->id))
  227. return -EINVAL;
  228. spin_lock_irqsave(&tmff->lock, flags);
  229. if (effect->id == -1) {
  230. /* Find a free effect */
  231. for (id = 0; id < TMFF_EFFECTS && test_bit(EFFECT_USED, tmff->effects[id].flags); ++id);
  232. if (id >= TMFF_EFFECTS) {
  233. spin_unlock_irqrestore(&tmff->lock, flags);
  234. return -ENOSPC;
  235. }
  236. effect->id = id;
  237. tmff->effects[id].owner = current->pid;
  238. tmff->effects[id].flags[0] = 0;
  239. set_bit(EFFECT_USED, tmff->effects[id].flags);
  240. } else {
  241. /* Re-uploading an owned effect, to change parameters */
  242. id = effect->id;
  243. clear_bit(EFFECT_PLAYING, tmff->effects[id].flags);
  244. }
  245. tmff->effects[id].effect = *effect;
  246. hid_tmff_recalculate_timer(tmff);
  247. spin_unlock_irqrestore(&tmff->lock, flags);
  248. return 0;
  249. }
  250. /* Start the timer for the next start/stop/delay */
  251. /* Always call this while tmff->lock is locked */
  252. static void hid_tmff_recalculate_timer(struct tmff_device *tmff)
  253. {
  254. int i;
  255. int events = 0;
  256. unsigned long next_time;
  257. next_time = 0; /* Shut up compiler's incorrect warning */
  258. /* Find the next change in an effect's status */
  259. for (i = 0; i < TMFF_EFFECTS; ++i) {
  260. struct tmff_effect *effect = &tmff->effects[i];
  261. unsigned long play_time;
  262. if (!test_bit(EFFECT_STARTED, effect->flags))
  263. continue;
  264. effect->stop_at = DELAY_CALC(effect->play_at, effect->effect.replay.length);
  265. if (!test_bit(EFFECT_PLAYING, effect->flags))
  266. play_time = effect->play_at;
  267. else
  268. play_time = effect->stop_at;
  269. events++;
  270. if (time_after(jiffies, play_time))
  271. play_time = jiffies;
  272. if (events == 1)
  273. next_time = play_time;
  274. else {
  275. if (time_after(next_time, play_time))
  276. next_time = play_time;
  277. }
  278. }
  279. if (!events && tmff->effects_playing) {
  280. /* Treat all effects turning off as an event */
  281. events = 1;
  282. next_time = jiffies;
  283. }
  284. if (!events) {
  285. /* No events, no time, no need for a timer. */
  286. del_timer_sync(&tmff->timer);
  287. return;
  288. }
  289. mod_timer(&tmff->timer, next_time);
  290. }
  291. /* Changes values from 0 to 0xffff into values from minimum to maximum */
  292. static inline int hid_tmff_scale(unsigned int in, int minimum, int maximum)
  293. {
  294. int ret;
  295. ret = (in * (maximum - minimum) / 0xffff) + minimum;
  296. if (ret < minimum)
  297. return minimum;
  298. if (ret > maximum)
  299. return maximum;
  300. return ret;
  301. }
  302. static void hid_tmff_timer(unsigned long timer_data)
  303. {
  304. struct tmff_device *tmff = (struct tmff_device *) timer_data;
  305. struct hid_device *hid = tmff->hid;
  306. unsigned long flags;
  307. int left = 0, right = 0; /* Rumbling */
  308. int i;
  309. spin_lock_irqsave(&tmff->lock, flags);
  310. tmff->effects_playing = 0;
  311. for (i = 0; i < TMFF_EFFECTS; ++i) {
  312. struct tmff_effect *effect = &tmff->effects[i];
  313. if (!test_bit(EFFECT_STARTED, effect->flags))
  314. continue;
  315. if (!time_after(jiffies, effect->play_at))
  316. continue;
  317. if (time_after(jiffies, effect->stop_at)) {
  318. dbg("Finished playing once %d", i);
  319. clear_bit(EFFECT_PLAYING, effect->flags);
  320. if (--effect->count <= 0) {
  321. dbg("Stopped %d", i);
  322. clear_bit(EFFECT_STARTED, effect->flags);
  323. continue;
  324. } else {
  325. dbg("Start again %d", i);
  326. effect->play_at = DELAY_CALC(jiffies, effect->effect.replay.delay);
  327. continue;
  328. }
  329. }
  330. ++tmff->effects_playing;
  331. set_bit(EFFECT_PLAYING, effect->flags);
  332. switch (effect->effect.type) {
  333. case FF_RUMBLE:
  334. right += effect->effect.u.rumble.strong_magnitude;
  335. left += effect->effect.u.rumble.weak_magnitude;
  336. break;
  337. default:
  338. BUG();
  339. break;
  340. }
  341. }
  342. left = hid_tmff_scale(left, tmff->rumble->logical_minimum, tmff->rumble->logical_maximum);
  343. right = hid_tmff_scale(right, tmff->rumble->logical_minimum, tmff->rumble->logical_maximum);
  344. if (left != tmff->rumble->value[0] || right != tmff->rumble->value[1]) {
  345. tmff->rumble->value[0] = left;
  346. tmff->rumble->value[1] = right;
  347. dbg("(left,right)=(%08x, %08x)", left, right);
  348. hid_submit_report(hid, tmff->report, USB_DIR_OUT);
  349. }
  350. if (!test_bit(DEVICE_CLOSING, tmff->flags))
  351. hid_tmff_recalculate_timer(tmff);
  352. spin_unlock_irqrestore(&tmff->lock, flags);
  353. }