hid-lgff.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  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 = kmalloc(sizeof(struct lgff_device), GFP_KERNEL);
  128. if (!private)
  129. return -1;
  130. memset(private, 0, sizeof(struct lgff_device));
  131. hid->ff_private = private;
  132. /* Input init */
  133. hid_lgff_input_init(hid);
  134. private->constant = hid_lgff_duplicate_report(report);
  135. if (!private->constant) {
  136. kfree(private);
  137. return -1;
  138. }
  139. private->constant->field[0]->value[0] = 0x51;
  140. private->constant->field[0]->value[1] = 0x08;
  141. private->constant->field[0]->value[2] = 0x7f;
  142. private->constant->field[0]->value[3] = 0x7f;
  143. private->rumble = hid_lgff_duplicate_report(report);
  144. if (!private->rumble) {
  145. hid_lgff_delete_report(private->constant);
  146. kfree(private);
  147. return -1;
  148. }
  149. private->rumble->field[0]->value[0] = 0x42;
  150. private->condition = hid_lgff_duplicate_report(report);
  151. if (!private->condition) {
  152. hid_lgff_delete_report(private->rumble);
  153. hid_lgff_delete_report(private->constant);
  154. kfree(private);
  155. return -1;
  156. }
  157. private->hid = hid;
  158. spin_lock_init(&private->lock);
  159. init_timer(&private->timer);
  160. private->timer.data = (unsigned long)private;
  161. private->timer.function = hid_lgff_timer;
  162. /* Event and exit callbacks */
  163. hid->ff_exit = hid_lgff_exit;
  164. hid->ff_event = hid_lgff_event;
  165. /* Start the update task */
  166. private->timer.expires = RUN_AT(PERIOD);
  167. add_timer(&private->timer); /*TODO: only run the timer when at least
  168. one effect is playing */
  169. printk(KERN_INFO "Force feedback for Logitech force feedback devices by Johann Deneux <johann.deneux@it.uu.se>\n");
  170. return 0;
  171. }
  172. static struct hid_report* hid_lgff_duplicate_report(struct hid_report* report)
  173. {
  174. struct hid_report* ret;
  175. ret = kmalloc(sizeof(struct lgff_device), GFP_KERNEL);
  176. if (!ret)
  177. return NULL;
  178. *ret = *report;
  179. ret->field[0] = kmalloc(sizeof(struct hid_field), GFP_KERNEL);
  180. if (!ret->field[0]) {
  181. kfree(ret);
  182. return NULL;
  183. }
  184. *ret->field[0] = *report->field[0];
  185. ret->field[0]->value = kmalloc(sizeof(s32[8]), GFP_KERNEL);
  186. if (!ret->field[0]->value) {
  187. kfree(ret->field[0]);
  188. kfree(ret);
  189. return NULL;
  190. }
  191. memset(ret->field[0]->value, 0, sizeof(s32[8]));
  192. return ret;
  193. }
  194. static void hid_lgff_delete_report(struct hid_report* report)
  195. {
  196. if (report) {
  197. kfree(report->field[0]->value);
  198. kfree(report->field[0]);
  199. kfree(report);
  200. }
  201. }
  202. static void hid_lgff_input_init(struct hid_device* hid)
  203. {
  204. struct device_type* dev = devices;
  205. signed short* ff;
  206. u16 idVendor = le16_to_cpu(hid->dev->descriptor.idVendor);
  207. u16 idProduct = le16_to_cpu(hid->dev->descriptor.idProduct);
  208. struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
  209. struct input_dev *input_dev = hidinput->input;
  210. while (dev->idVendor && (idVendor != dev->idVendor || idProduct != dev->idProduct))
  211. dev++;
  212. for (ff = dev->ff; *ff >= 0; ff++)
  213. set_bit(*ff, input_dev->ffbit);
  214. input_dev->upload_effect = hid_lgff_upload_effect;
  215. input_dev->flush = hid_lgff_flush;
  216. set_bit(EV_FF, input_dev->evbit);
  217. input_dev->ff_effects_max = LGFF_EFFECTS;
  218. }
  219. static void hid_lgff_exit(struct hid_device* hid)
  220. {
  221. struct lgff_device *lgff = hid->ff_private;
  222. set_bit(DEVICE_CLOSING, lgff->flags);
  223. del_timer_sync(&lgff->timer);
  224. hid_lgff_delete_report(lgff->condition);
  225. hid_lgff_delete_report(lgff->rumble);
  226. hid_lgff_delete_report(lgff->constant);
  227. kfree(lgff);
  228. }
  229. static int hid_lgff_event(struct hid_device *hid, struct input_dev* input,
  230. unsigned int type, unsigned int code, int value)
  231. {
  232. struct lgff_device *lgff = hid->ff_private;
  233. struct lgff_effect *effect = lgff->effects + code;
  234. unsigned long flags;
  235. if (type != EV_FF) return -EINVAL;
  236. if (!LGFF_CHECK_OWNERSHIP(code, lgff)) return -EACCES;
  237. if (value < 0) return -EINVAL;
  238. spin_lock_irqsave(&lgff->lock, flags);
  239. if (value > 0) {
  240. if (test_bit(EFFECT_STARTED, effect->flags)) {
  241. spin_unlock_irqrestore(&lgff->lock, flags);
  242. return -EBUSY;
  243. }
  244. if (test_bit(EFFECT_PLAYING, effect->flags)) {
  245. spin_unlock_irqrestore(&lgff->lock, flags);
  246. return -EBUSY;
  247. }
  248. effect->count = value;
  249. if (effect->effect.replay.delay) {
  250. set_bit(EFFECT_STARTED, effect->flags);
  251. } else {
  252. set_bit(EFFECT_PLAYING, effect->flags);
  253. }
  254. effect->started_at = jiffies;
  255. }
  256. else { /* value == 0 */
  257. clear_bit(EFFECT_STARTED, effect->flags);
  258. clear_bit(EFFECT_PLAYING, effect->flags);
  259. }
  260. spin_unlock_irqrestore(&lgff->lock, flags);
  261. return 0;
  262. }
  263. /* Erase all effects this process owns */
  264. static int hid_lgff_flush(struct input_dev *dev, struct file *file)
  265. {
  266. struct hid_device *hid = dev->private;
  267. struct lgff_device *lgff = hid->ff_private;
  268. int i;
  269. for (i=0; i<dev->ff_effects_max; ++i) {
  270. /*NOTE: no need to lock here. The only times EFFECT_USED is
  271. modified is when effects are uploaded or when an effect is
  272. erased. But a process cannot close its dev/input/eventX fd
  273. and perform ioctls on the same fd all at the same time */
  274. if ( current->pid == lgff->effects[i].owner
  275. && test_bit(EFFECT_USED, lgff->effects[i].flags)) {
  276. if (hid_lgff_erase(dev, i))
  277. warn("erase effect %d failed", i);
  278. }
  279. }
  280. return 0;
  281. }
  282. static int hid_lgff_erase(struct input_dev *dev, int id)
  283. {
  284. struct hid_device *hid = dev->private;
  285. struct lgff_device *lgff = hid->ff_private;
  286. unsigned long flags;
  287. if (!LGFF_CHECK_OWNERSHIP(id, lgff)) return -EACCES;
  288. spin_lock_irqsave(&lgff->lock, flags);
  289. lgff->effects[id].flags[0] = 0;
  290. spin_unlock_irqrestore(&lgff->lock, flags);
  291. return 0;
  292. }
  293. static int hid_lgff_upload_effect(struct input_dev* input,
  294. struct ff_effect* effect)
  295. {
  296. struct hid_device *hid = input->private;
  297. struct lgff_device *lgff = hid->ff_private;
  298. struct lgff_effect new;
  299. int id;
  300. unsigned long flags;
  301. dbg("ioctl rumble");
  302. if (!test_bit(effect->type, input->ffbit)) return -EINVAL;
  303. spin_lock_irqsave(&lgff->lock, flags);
  304. if (effect->id == -1) {
  305. int i;
  306. for (i=0; i<LGFF_EFFECTS && test_bit(EFFECT_USED, lgff->effects[i].flags); ++i);
  307. if (i >= LGFF_EFFECTS) {
  308. spin_unlock_irqrestore(&lgff->lock, flags);
  309. return -ENOSPC;
  310. }
  311. effect->id = i;
  312. lgff->effects[i].owner = current->pid;
  313. lgff->effects[i].flags[0] = 0;
  314. set_bit(EFFECT_USED, lgff->effects[i].flags);
  315. }
  316. else if (!LGFF_CHECK_OWNERSHIP(effect->id, lgff)) {
  317. spin_unlock_irqrestore(&lgff->lock, flags);
  318. return -EACCES;
  319. }
  320. id = effect->id;
  321. new = lgff->effects[id];
  322. new.effect = *effect;
  323. if (test_bit(EFFECT_STARTED, lgff->effects[id].flags)
  324. || test_bit(EFFECT_STARTED, lgff->effects[id].flags)) {
  325. /* Changing replay parameters is not allowed (for the time
  326. being) */
  327. if (new.effect.replay.delay != lgff->effects[id].effect.replay.delay
  328. || new.effect.replay.length != lgff->effects[id].effect.replay.length) {
  329. spin_unlock_irqrestore(&lgff->lock, flags);
  330. return -ENOSYS;
  331. }
  332. lgff->effects[id] = new;
  333. } else {
  334. lgff->effects[id] = new;
  335. }
  336. spin_unlock_irqrestore(&lgff->lock, flags);
  337. return 0;
  338. }
  339. static void hid_lgff_timer(unsigned long timer_data)
  340. {
  341. struct lgff_device *lgff = (struct lgff_device*)timer_data;
  342. struct hid_device *hid = lgff->hid;
  343. unsigned long flags;
  344. int x = 0x7f, y = 0x7f; // Coordinates of constant effects
  345. unsigned int left = 0, right = 0; // Rumbling
  346. int i;
  347. spin_lock_irqsave(&lgff->lock, flags);
  348. for (i=0; i<LGFF_EFFECTS; ++i) {
  349. struct lgff_effect* effect = lgff->effects +i;
  350. if (test_bit(EFFECT_PLAYING, effect->flags)) {
  351. switch (effect->effect.type) {
  352. case FF_CONSTANT: {
  353. //TODO: handle envelopes
  354. int degrees = effect->effect.direction * 360 >> 16;
  355. x += fixp_mult(fixp_sin(degrees),
  356. fixp_new16(effect->effect.u.constant.level));
  357. y += fixp_mult(-fixp_cos(degrees),
  358. fixp_new16(effect->effect.u.constant.level));
  359. } break;
  360. case FF_RUMBLE:
  361. right += effect->effect.u.rumble.strong_magnitude;
  362. left += effect->effect.u.rumble.weak_magnitude;
  363. break;
  364. };
  365. /* One run of the effect is finished playing */
  366. if (time_after(jiffies,
  367. effect->started_at
  368. + effect->effect.replay.delay*HZ/1000
  369. + effect->effect.replay.length*HZ/1000)) {
  370. dbg("Finished playing once %d", i);
  371. if (--effect->count <= 0) {
  372. dbg("Stopped %d", i);
  373. clear_bit(EFFECT_PLAYING, effect->flags);
  374. }
  375. else {
  376. dbg("Start again %d", i);
  377. if (effect->effect.replay.length != 0) {
  378. clear_bit(EFFECT_PLAYING, effect->flags);
  379. set_bit(EFFECT_STARTED, effect->flags);
  380. }
  381. effect->started_at = jiffies;
  382. }
  383. }
  384. } else if (test_bit(EFFECT_STARTED, lgff->effects[i].flags)) {
  385. /* Check if we should start playing the effect */
  386. if (time_after(jiffies,
  387. lgff->effects[i].started_at
  388. + lgff->effects[i].effect.replay.delay*HZ/1000)) {
  389. dbg("Now playing %d", i);
  390. clear_bit(EFFECT_STARTED, lgff->effects[i].flags);
  391. set_bit(EFFECT_PLAYING, lgff->effects[i].flags);
  392. }
  393. }
  394. }
  395. #define CLAMP(x) if (x < 0) x = 0; if (x > 0xff) x = 0xff
  396. // Clamp values
  397. CLAMP(x);
  398. CLAMP(y);
  399. CLAMP(left);
  400. CLAMP(right);
  401. #undef CLAMP
  402. if (x != lgff->constant->field[0]->value[2]
  403. || y != lgff->constant->field[0]->value[3]) {
  404. lgff->constant->field[0]->value[2] = x;
  405. lgff->constant->field[0]->value[3] = y;
  406. dbg("(x,y)=(%04x, %04x)", x, y);
  407. hid_submit_report(hid, lgff->constant, USB_DIR_OUT);
  408. }
  409. if (left != lgff->rumble->field[0]->value[2]
  410. || right != lgff->rumble->field[0]->value[3]) {
  411. lgff->rumble->field[0]->value[2] = left;
  412. lgff->rumble->field[0]->value[3] = right;
  413. dbg("(left,right)=(%04x, %04x)", left, right);
  414. hid_submit_report(hid, lgff->rumble, USB_DIR_OUT);
  415. }
  416. if (!test_bit(DEVICE_CLOSING, lgff->flags)) {
  417. lgff->timer.expires = RUN_AT(PERIOD);
  418. add_timer(&lgff->timer);
  419. }
  420. spin_unlock_irqrestore(&lgff->lock, flags);
  421. }