hid-lgff.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. /*
  2. * $$
  3. *
  4. * Force feedback support for hid-compliant for some of the devices from
  5. * Logitech, namely:
  6. * - WingMan Cordless RumblePad
  7. * - WingMan Force 3D
  8. *
  9. * Copyright (c) 2002-2004 Johann Deneux
  10. */
  11. /*
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  25. *
  26. * Should you need to contact me, the author, you can do so by
  27. * e-mail - mail your message to <johann.deneux@it.uu.se>
  28. */
  29. #include <linux/input.h>
  30. #include <linux/sched.h>
  31. //#define DEBUG
  32. #include <linux/usb.h>
  33. #include <linux/circ_buf.h>
  34. #include "hid.h"
  35. #include "fixp-arith.h"
  36. /* Periodicity of the update */
  37. #define PERIOD (HZ/10)
  38. #define RUN_AT(t) (jiffies + (t))
  39. /* Effect status */
  40. #define EFFECT_STARTED 0 /* Effect is going to play after some time
  41. (ff_replay.delay) */
  42. #define EFFECT_PLAYING 1 /* Effect is being played */
  43. #define EFFECT_USED 2
  44. // For lgff_device::flags
  45. #define DEVICE_CLOSING 0 /* The driver is being unitialised */
  46. /* Check that the current process can access an effect */
  47. #define CHECK_OWNERSHIP(effect) (current->pid == 0 \
  48. || effect.owner == current->pid)
  49. #define LGFF_CHECK_OWNERSHIP(i, l) \
  50. (i>=0 && i<LGFF_EFFECTS \
  51. && test_bit(EFFECT_USED, l->effects[i].flags) \
  52. && CHECK_OWNERSHIP(l->effects[i]))
  53. #define LGFF_EFFECTS 8
  54. struct device_type {
  55. u16 idVendor;
  56. u16 idProduct;
  57. signed short *ff;
  58. };
  59. struct lgff_effect {
  60. pid_t owner;
  61. struct ff_effect effect;
  62. unsigned long flags[1];
  63. unsigned int count; /* Number of times left to play */
  64. unsigned long started_at; /* When the effect started to play */
  65. };
  66. struct lgff_device {
  67. struct hid_device* hid;
  68. struct hid_report* constant;
  69. struct hid_report* rumble;
  70. struct hid_report* condition;
  71. struct lgff_effect effects[LGFF_EFFECTS];
  72. spinlock_t lock; /* device-level lock. Having locks on
  73. a per-effect basis could be nice, but
  74. isn't really necessary */
  75. unsigned long flags[1]; /* Contains various information about the
  76. state of the driver for this device */
  77. struct timer_list timer;
  78. };
  79. /* Callbacks */
  80. static void hid_lgff_exit(struct hid_device* hid);
  81. static int hid_lgff_event(struct hid_device *hid, struct input_dev *input,
  82. unsigned int type, unsigned int code, int value);
  83. static int hid_lgff_flush(struct input_dev *input, struct file *file);
  84. static int hid_lgff_upload_effect(struct input_dev *input,
  85. struct ff_effect *effect);
  86. static int hid_lgff_erase(struct input_dev *input, int id);
  87. /* Local functions */
  88. static void hid_lgff_input_init(struct hid_device* hid);
  89. static void hid_lgff_timer(unsigned long timer_data);
  90. static struct hid_report* hid_lgff_duplicate_report(struct hid_report*);
  91. static void hid_lgff_delete_report(struct hid_report*);
  92. static signed short ff_rumble[] = {
  93. FF_RUMBLE,
  94. -1
  95. };
  96. static signed short ff_joystick[] = {
  97. FF_CONSTANT,
  98. -1
  99. };
  100. static struct device_type devices[] = {
  101. {0x046d, 0xc211, ff_rumble},
  102. {0x046d, 0xc219, ff_rumble},
  103. {0x046d, 0xc283, ff_joystick},
  104. {0x0000, 0x0000, ff_joystick}
  105. };
  106. int hid_lgff_init(struct hid_device* hid)
  107. {
  108. struct lgff_device *private;
  109. struct hid_report* report;
  110. struct hid_field* field;
  111. /* Find the report to use */
  112. if (list_empty(&hid->report_enum[HID_OUTPUT_REPORT].report_list)) {
  113. err("No output report found");
  114. return -1;
  115. }
  116. /* Check that the report looks ok */
  117. report = (struct hid_report*)hid->report_enum[HID_OUTPUT_REPORT].report_list.next;
  118. if (!report) {
  119. err("NULL output report");
  120. return -1;
  121. }
  122. field = report->field[0];
  123. if (!field) {
  124. err("NULL field");
  125. return -1;
  126. }
  127. private = kzalloc(sizeof(struct lgff_device), GFP_KERNEL);
  128. if (!private)
  129. return -1;
  130. hid->ff_private = private;
  131. /* Input init */
  132. hid_lgff_input_init(hid);
  133. private->constant = hid_lgff_duplicate_report(report);
  134. if (!private->constant) {
  135. kfree(private);
  136. return -1;
  137. }
  138. private->constant->field[0]->value[0] = 0x51;
  139. private->constant->field[0]->value[1] = 0x08;
  140. private->constant->field[0]->value[2] = 0x7f;
  141. private->constant->field[0]->value[3] = 0x7f;
  142. private->rumble = hid_lgff_duplicate_report(report);
  143. if (!private->rumble) {
  144. hid_lgff_delete_report(private->constant);
  145. kfree(private);
  146. return -1;
  147. }
  148. private->rumble->field[0]->value[0] = 0x42;
  149. private->condition = hid_lgff_duplicate_report(report);
  150. if (!private->condition) {
  151. hid_lgff_delete_report(private->rumble);
  152. hid_lgff_delete_report(private->constant);
  153. kfree(private);
  154. return -1;
  155. }
  156. private->hid = hid;
  157. spin_lock_init(&private->lock);
  158. init_timer(&private->timer);
  159. private->timer.data = (unsigned long)private;
  160. private->timer.function = hid_lgff_timer;
  161. /* Event and exit callbacks */
  162. hid->ff_exit = hid_lgff_exit;
  163. hid->ff_event = hid_lgff_event;
  164. /* Start the update task */
  165. private->timer.expires = RUN_AT(PERIOD);
  166. add_timer(&private->timer); /*TODO: only run the timer when at least
  167. one effect is playing */
  168. printk(KERN_INFO "Force feedback for Logitech force feedback devices by Johann Deneux <johann.deneux@it.uu.se>\n");
  169. return 0;
  170. }
  171. static struct hid_report* hid_lgff_duplicate_report(struct hid_report* report)
  172. {
  173. struct hid_report* ret;
  174. ret = kmalloc(sizeof(struct lgff_device), GFP_KERNEL);
  175. if (!ret)
  176. return NULL;
  177. *ret = *report;
  178. ret->field[0] = kmalloc(sizeof(struct hid_field), GFP_KERNEL);
  179. if (!ret->field[0]) {
  180. kfree(ret);
  181. return NULL;
  182. }
  183. *ret->field[0] = *report->field[0];
  184. ret->field[0]->value = kzalloc(sizeof(s32[8]), GFP_KERNEL);
  185. if (!ret->field[0]->value) {
  186. kfree(ret->field[0]);
  187. kfree(ret);
  188. return NULL;
  189. }
  190. return ret;
  191. }
  192. static void hid_lgff_delete_report(struct hid_report* report)
  193. {
  194. if (report) {
  195. kfree(report->field[0]->value);
  196. kfree(report->field[0]);
  197. kfree(report);
  198. }
  199. }
  200. static void hid_lgff_input_init(struct hid_device* hid)
  201. {
  202. struct device_type* dev = devices;
  203. signed short* ff;
  204. u16 idVendor = le16_to_cpu(hid->dev->descriptor.idVendor);
  205. u16 idProduct = le16_to_cpu(hid->dev->descriptor.idProduct);
  206. struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
  207. struct input_dev *input_dev = hidinput->input;
  208. while (dev->idVendor && (idVendor != dev->idVendor || idProduct != dev->idProduct))
  209. dev++;
  210. for (ff = dev->ff; *ff >= 0; ff++)
  211. set_bit(*ff, input_dev->ffbit);
  212. input_dev->upload_effect = hid_lgff_upload_effect;
  213. input_dev->flush = hid_lgff_flush;
  214. set_bit(EV_FF, input_dev->evbit);
  215. input_dev->ff_effects_max = LGFF_EFFECTS;
  216. }
  217. static void hid_lgff_exit(struct hid_device* hid)
  218. {
  219. struct lgff_device *lgff = hid->ff_private;
  220. set_bit(DEVICE_CLOSING, lgff->flags);
  221. del_timer_sync(&lgff->timer);
  222. hid_lgff_delete_report(lgff->condition);
  223. hid_lgff_delete_report(lgff->rumble);
  224. hid_lgff_delete_report(lgff->constant);
  225. kfree(lgff);
  226. }
  227. static int hid_lgff_event(struct hid_device *hid, struct input_dev* input,
  228. unsigned int type, unsigned int code, int value)
  229. {
  230. struct lgff_device *lgff = hid->ff_private;
  231. struct lgff_effect *effect = lgff->effects + code;
  232. unsigned long flags;
  233. if (type != EV_FF) return -EINVAL;
  234. if (!LGFF_CHECK_OWNERSHIP(code, lgff)) return -EACCES;
  235. if (value < 0) return -EINVAL;
  236. spin_lock_irqsave(&lgff->lock, flags);
  237. if (value > 0) {
  238. if (test_bit(EFFECT_STARTED, effect->flags)) {
  239. spin_unlock_irqrestore(&lgff->lock, flags);
  240. return -EBUSY;
  241. }
  242. if (test_bit(EFFECT_PLAYING, effect->flags)) {
  243. spin_unlock_irqrestore(&lgff->lock, flags);
  244. return -EBUSY;
  245. }
  246. effect->count = value;
  247. if (effect->effect.replay.delay) {
  248. set_bit(EFFECT_STARTED, effect->flags);
  249. } else {
  250. set_bit(EFFECT_PLAYING, effect->flags);
  251. }
  252. effect->started_at = jiffies;
  253. }
  254. else { /* value == 0 */
  255. clear_bit(EFFECT_STARTED, effect->flags);
  256. clear_bit(EFFECT_PLAYING, effect->flags);
  257. }
  258. spin_unlock_irqrestore(&lgff->lock, flags);
  259. return 0;
  260. }
  261. /* Erase all effects this process owns */
  262. static int hid_lgff_flush(struct input_dev *dev, struct file *file)
  263. {
  264. struct hid_device *hid = dev->private;
  265. struct lgff_device *lgff = hid->ff_private;
  266. int i;
  267. for (i=0; i<dev->ff_effects_max; ++i) {
  268. /*NOTE: no need to lock here. The only times EFFECT_USED is
  269. modified is when effects are uploaded or when an effect is
  270. erased. But a process cannot close its dev/input/eventX fd
  271. and perform ioctls on the same fd all at the same time */
  272. if ( current->pid == lgff->effects[i].owner
  273. && test_bit(EFFECT_USED, lgff->effects[i].flags)) {
  274. if (hid_lgff_erase(dev, i))
  275. warn("erase effect %d failed", i);
  276. }
  277. }
  278. return 0;
  279. }
  280. static int hid_lgff_erase(struct input_dev *dev, int id)
  281. {
  282. struct hid_device *hid = dev->private;
  283. struct lgff_device *lgff = hid->ff_private;
  284. unsigned long flags;
  285. if (!LGFF_CHECK_OWNERSHIP(id, lgff)) return -EACCES;
  286. spin_lock_irqsave(&lgff->lock, flags);
  287. lgff->effects[id].flags[0] = 0;
  288. spin_unlock_irqrestore(&lgff->lock, flags);
  289. return 0;
  290. }
  291. static int hid_lgff_upload_effect(struct input_dev* input,
  292. struct ff_effect* effect)
  293. {
  294. struct hid_device *hid = input->private;
  295. struct lgff_device *lgff = hid->ff_private;
  296. struct lgff_effect new;
  297. int id;
  298. unsigned long flags;
  299. dbg("ioctl rumble");
  300. if (!test_bit(effect->type, input->ffbit)) return -EINVAL;
  301. spin_lock_irqsave(&lgff->lock, flags);
  302. if (effect->id == -1) {
  303. int i;
  304. for (i=0; i<LGFF_EFFECTS && test_bit(EFFECT_USED, lgff->effects[i].flags); ++i);
  305. if (i >= LGFF_EFFECTS) {
  306. spin_unlock_irqrestore(&lgff->lock, flags);
  307. return -ENOSPC;
  308. }
  309. effect->id = i;
  310. lgff->effects[i].owner = current->pid;
  311. lgff->effects[i].flags[0] = 0;
  312. set_bit(EFFECT_USED, lgff->effects[i].flags);
  313. }
  314. else if (!LGFF_CHECK_OWNERSHIP(effect->id, lgff)) {
  315. spin_unlock_irqrestore(&lgff->lock, flags);
  316. return -EACCES;
  317. }
  318. id = effect->id;
  319. new = lgff->effects[id];
  320. new.effect = *effect;
  321. if (test_bit(EFFECT_STARTED, lgff->effects[id].flags)
  322. || test_bit(EFFECT_STARTED, lgff->effects[id].flags)) {
  323. /* Changing replay parameters is not allowed (for the time
  324. being) */
  325. if (new.effect.replay.delay != lgff->effects[id].effect.replay.delay
  326. || new.effect.replay.length != lgff->effects[id].effect.replay.length) {
  327. spin_unlock_irqrestore(&lgff->lock, flags);
  328. return -ENOSYS;
  329. }
  330. lgff->effects[id] = new;
  331. } else {
  332. lgff->effects[id] = new;
  333. }
  334. spin_unlock_irqrestore(&lgff->lock, flags);
  335. return 0;
  336. }
  337. static void hid_lgff_timer(unsigned long timer_data)
  338. {
  339. struct lgff_device *lgff = (struct lgff_device*)timer_data;
  340. struct hid_device *hid = lgff->hid;
  341. unsigned long flags;
  342. int x = 0x7f, y = 0x7f; // Coordinates of constant effects
  343. unsigned int left = 0, right = 0; // Rumbling
  344. int i;
  345. spin_lock_irqsave(&lgff->lock, flags);
  346. for (i=0; i<LGFF_EFFECTS; ++i) {
  347. struct lgff_effect* effect = lgff->effects +i;
  348. if (test_bit(EFFECT_PLAYING, effect->flags)) {
  349. switch (effect->effect.type) {
  350. case FF_CONSTANT: {
  351. //TODO: handle envelopes
  352. int degrees = effect->effect.direction * 360 >> 16;
  353. x += fixp_mult(fixp_sin(degrees),
  354. fixp_new16(effect->effect.u.constant.level));
  355. y += fixp_mult(-fixp_cos(degrees),
  356. fixp_new16(effect->effect.u.constant.level));
  357. } break;
  358. case FF_RUMBLE:
  359. right += effect->effect.u.rumble.strong_magnitude;
  360. left += effect->effect.u.rumble.weak_magnitude;
  361. break;
  362. };
  363. /* One run of the effect is finished playing */
  364. if (time_after(jiffies,
  365. effect->started_at
  366. + effect->effect.replay.delay*HZ/1000
  367. + effect->effect.replay.length*HZ/1000)) {
  368. dbg("Finished playing once %d", i);
  369. if (--effect->count <= 0) {
  370. dbg("Stopped %d", i);
  371. clear_bit(EFFECT_PLAYING, effect->flags);
  372. }
  373. else {
  374. dbg("Start again %d", i);
  375. if (effect->effect.replay.length != 0) {
  376. clear_bit(EFFECT_PLAYING, effect->flags);
  377. set_bit(EFFECT_STARTED, effect->flags);
  378. }
  379. effect->started_at = jiffies;
  380. }
  381. }
  382. } else if (test_bit(EFFECT_STARTED, lgff->effects[i].flags)) {
  383. /* Check if we should start playing the effect */
  384. if (time_after(jiffies,
  385. lgff->effects[i].started_at
  386. + lgff->effects[i].effect.replay.delay*HZ/1000)) {
  387. dbg("Now playing %d", i);
  388. clear_bit(EFFECT_STARTED, lgff->effects[i].flags);
  389. set_bit(EFFECT_PLAYING, lgff->effects[i].flags);
  390. }
  391. }
  392. }
  393. #define CLAMP(x) if (x < 0) x = 0; if (x > 0xff) x = 0xff
  394. // Clamp values
  395. CLAMP(x);
  396. CLAMP(y);
  397. CLAMP(left);
  398. CLAMP(right);
  399. #undef CLAMP
  400. if (x != lgff->constant->field[0]->value[2]
  401. || y != lgff->constant->field[0]->value[3]) {
  402. lgff->constant->field[0]->value[2] = x;
  403. lgff->constant->field[0]->value[3] = y;
  404. dbg("(x,y)=(%04x, %04x)", x, y);
  405. hid_submit_report(hid, lgff->constant, USB_DIR_OUT);
  406. }
  407. if (left != lgff->rumble->field[0]->value[2]
  408. || right != lgff->rumble->field[0]->value[3]) {
  409. lgff->rumble->field[0]->value[2] = left;
  410. lgff->rumble->field[0]->value[3] = right;
  411. dbg("(left,right)=(%04x, %04x)", left, right);
  412. hid_submit_report(hid, lgff->rumble, USB_DIR_OUT);
  413. }
  414. if (!test_bit(DEVICE_CLOSING, lgff->flags)) {
  415. lgff->timer.expires = RUN_AT(PERIOD);
  416. add_timer(&lgff->timer);
  417. }
  418. spin_unlock_irqrestore(&lgff->lock, flags);
  419. }