iforce-packets.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /*
  2. * Copyright (c) 2000-2002 Vojtech Pavlik <vojtech@ucw.cz>
  3. * Copyright (c) 2001-2002, 2007 Johann Deneux <johann.deneux@gmail.com>
  4. *
  5. * USB/RS232 I-Force joysticks and wheels.
  6. */
  7. /*
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. *
  22. * Should you need to contact me, the author, you can do so either by
  23. * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail:
  24. * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic
  25. */
  26. #include "iforce.h"
  27. static struct {
  28. __s32 x;
  29. __s32 y;
  30. } iforce_hat_to_axis[16] = {{ 0,-1}, { 1,-1}, { 1, 0}, { 1, 1}, { 0, 1}, {-1, 1}, {-1, 0}, {-1,-1}};
  31. void iforce_dump_packet(char *msg, u16 cmd, unsigned char *data)
  32. {
  33. int i;
  34. printk(KERN_DEBUG __FILE__ ": %s cmd = %04x, data = ", msg, cmd);
  35. for (i = 0; i < LO(cmd); i++)
  36. printk("%02x ", data[i]);
  37. printk("\n");
  38. }
  39. /*
  40. * Send a packet of bytes to the device
  41. */
  42. int iforce_send_packet(struct iforce *iforce, u16 cmd, unsigned char* data)
  43. {
  44. /* Copy data to buffer */
  45. int n = LO(cmd);
  46. int c;
  47. int empty;
  48. int head, tail;
  49. unsigned long flags;
  50. /*
  51. * Update head and tail of xmit buffer
  52. */
  53. spin_lock_irqsave(&iforce->xmit_lock, flags);
  54. head = iforce->xmit.head;
  55. tail = iforce->xmit.tail;
  56. if (CIRC_SPACE(head, tail, XMIT_SIZE) < n+2) {
  57. dev_warn(&iforce->dev->dev,
  58. "not enough space in xmit buffer to send new packet\n");
  59. spin_unlock_irqrestore(&iforce->xmit_lock, flags);
  60. return -1;
  61. }
  62. empty = head == tail;
  63. XMIT_INC(iforce->xmit.head, n+2);
  64. /*
  65. * Store packet in xmit buffer
  66. */
  67. iforce->xmit.buf[head] = HI(cmd);
  68. XMIT_INC(head, 1);
  69. iforce->xmit.buf[head] = LO(cmd);
  70. XMIT_INC(head, 1);
  71. c = CIRC_SPACE_TO_END(head, tail, XMIT_SIZE);
  72. if (n < c) c=n;
  73. memcpy(&iforce->xmit.buf[head],
  74. data,
  75. c);
  76. if (n != c) {
  77. memcpy(&iforce->xmit.buf[0],
  78. data + c,
  79. n - c);
  80. }
  81. XMIT_INC(head, n);
  82. spin_unlock_irqrestore(&iforce->xmit_lock, flags);
  83. /*
  84. * If necessary, start the transmission
  85. */
  86. switch (iforce->bus) {
  87. #ifdef CONFIG_JOYSTICK_IFORCE_232
  88. case IFORCE_232:
  89. if (empty)
  90. iforce_serial_xmit(iforce);
  91. break;
  92. #endif
  93. #ifdef CONFIG_JOYSTICK_IFORCE_USB
  94. case IFORCE_USB:
  95. if (iforce->usbdev && empty &&
  96. !test_and_set_bit(IFORCE_XMIT_RUNNING, iforce->xmit_flags)) {
  97. iforce_usb_xmit(iforce);
  98. }
  99. break;
  100. #endif
  101. }
  102. return 0;
  103. }
  104. /* Start or stop an effect */
  105. int iforce_control_playback(struct iforce* iforce, u16 id, unsigned int value)
  106. {
  107. unsigned char data[3];
  108. data[0] = LO(id);
  109. data[1] = (value > 0) ? ((value > 1) ? 0x41 : 0x01) : 0;
  110. data[2] = LO(value);
  111. return iforce_send_packet(iforce, FF_CMD_PLAY, data);
  112. }
  113. /* Mark an effect that was being updated as ready. That means it can be updated
  114. * again */
  115. static int mark_core_as_ready(struct iforce *iforce, unsigned short addr)
  116. {
  117. int i;
  118. if (!iforce->dev->ff)
  119. return 0;
  120. for (i = 0; i < iforce->dev->ff->max_effects; ++i) {
  121. if (test_bit(FF_CORE_IS_USED, iforce->core_effects[i].flags) &&
  122. (iforce->core_effects[i].mod1_chunk.start == addr ||
  123. iforce->core_effects[i].mod2_chunk.start == addr)) {
  124. clear_bit(FF_CORE_UPDATE, iforce->core_effects[i].flags);
  125. return 0;
  126. }
  127. }
  128. dev_warn(&iforce->dev->dev, "unused effect %04x updated !!!\n", addr);
  129. return -1;
  130. }
  131. void iforce_process_packet(struct iforce *iforce, u16 cmd, unsigned char *data)
  132. {
  133. struct input_dev *dev = iforce->dev;
  134. int i;
  135. static int being_used = 0;
  136. if (being_used)
  137. dev_warn(&iforce->dev->dev,
  138. "re-entrant call to iforce_process %d\n", being_used);
  139. being_used++;
  140. #ifdef CONFIG_JOYSTICK_IFORCE_232
  141. if (HI(iforce->expect_packet) == HI(cmd)) {
  142. iforce->expect_packet = 0;
  143. iforce->ecmd = cmd;
  144. memcpy(iforce->edata, data, IFORCE_MAX_LENGTH);
  145. }
  146. #endif
  147. wake_up(&iforce->wait);
  148. if (!iforce->type) {
  149. being_used--;
  150. return;
  151. }
  152. switch (HI(cmd)) {
  153. case 0x01: /* joystick position data */
  154. case 0x03: /* wheel position data */
  155. if (HI(cmd) == 1) {
  156. input_report_abs(dev, ABS_X, (__s16) (((__s16)data[1] << 8) | data[0]));
  157. input_report_abs(dev, ABS_Y, (__s16) (((__s16)data[3] << 8) | data[2]));
  158. input_report_abs(dev, ABS_THROTTLE, 255 - data[4]);
  159. if (LO(cmd) >= 8 && test_bit(ABS_RUDDER ,dev->absbit))
  160. input_report_abs(dev, ABS_RUDDER, (__s8)data[7]);
  161. } else {
  162. input_report_abs(dev, ABS_WHEEL, (__s16) (((__s16)data[1] << 8) | data[0]));
  163. input_report_abs(dev, ABS_GAS, 255 - data[2]);
  164. input_report_abs(dev, ABS_BRAKE, 255 - data[3]);
  165. }
  166. input_report_abs(dev, ABS_HAT0X, iforce_hat_to_axis[data[6] >> 4].x);
  167. input_report_abs(dev, ABS_HAT0Y, iforce_hat_to_axis[data[6] >> 4].y);
  168. for (i = 0; iforce->type->btn[i] >= 0; i++)
  169. input_report_key(dev, iforce->type->btn[i], data[(i >> 3) + 5] & (1 << (i & 7)));
  170. /* If there are untouched bits left, interpret them as the second hat */
  171. if (i <= 8) {
  172. int btns = data[6];
  173. if (test_bit(ABS_HAT1X, dev->absbit)) {
  174. if (btns & 8) input_report_abs(dev, ABS_HAT1X, -1);
  175. else if (btns & 2) input_report_abs(dev, ABS_HAT1X, 1);
  176. else input_report_abs(dev, ABS_HAT1X, 0);
  177. }
  178. if (test_bit(ABS_HAT1Y, dev->absbit)) {
  179. if (btns & 1) input_report_abs(dev, ABS_HAT1Y, -1);
  180. else if (btns & 4) input_report_abs(dev, ABS_HAT1Y, 1);
  181. else input_report_abs(dev, ABS_HAT1Y, 0);
  182. }
  183. }
  184. input_sync(dev);
  185. break;
  186. case 0x02: /* status report */
  187. input_report_key(dev, BTN_DEAD, data[0] & 0x02);
  188. input_sync(dev);
  189. /* Check if an effect was just started or stopped */
  190. i = data[1] & 0x7f;
  191. if (data[1] & 0x80) {
  192. if (!test_and_set_bit(FF_CORE_IS_PLAYED, iforce->core_effects[i].flags)) {
  193. /* Report play event */
  194. input_report_ff_status(dev, i, FF_STATUS_PLAYING);
  195. }
  196. } else if (test_and_clear_bit(FF_CORE_IS_PLAYED, iforce->core_effects[i].flags)) {
  197. /* Report stop event */
  198. input_report_ff_status(dev, i, FF_STATUS_STOPPED);
  199. }
  200. if (LO(cmd) > 3) {
  201. int j;
  202. for (j = 3; j < LO(cmd); j += 2)
  203. mark_core_as_ready(iforce, data[j] | (data[j+1]<<8));
  204. }
  205. break;
  206. }
  207. being_used--;
  208. }
  209. int iforce_get_id_packet(struct iforce *iforce, char *packet)
  210. {
  211. switch (iforce->bus) {
  212. case IFORCE_USB: {
  213. #ifdef CONFIG_JOYSTICK_IFORCE_USB
  214. int status;
  215. iforce->cr.bRequest = packet[0];
  216. iforce->ctrl->dev = iforce->usbdev;
  217. status = usb_submit_urb(iforce->ctrl, GFP_ATOMIC);
  218. if (status) {
  219. dev_err(&iforce->intf->dev,
  220. "usb_submit_urb failed %d\n", status);
  221. return -1;
  222. }
  223. wait_event_interruptible_timeout(iforce->wait,
  224. iforce->ctrl->status != -EINPROGRESS, HZ);
  225. if (iforce->ctrl->status) {
  226. dev_dbg(&iforce->intf->dev,
  227. "iforce->ctrl->status = %d\n",
  228. iforce->ctrl->status);
  229. usb_unlink_urb(iforce->ctrl);
  230. return -1;
  231. }
  232. #else
  233. printk(KERN_DEBUG "iforce_get_id_packet: iforce->bus = USB!\n");
  234. #endif
  235. }
  236. break;
  237. case IFORCE_232:
  238. #ifdef CONFIG_JOYSTICK_IFORCE_232
  239. iforce->expect_packet = FF_CMD_QUERY;
  240. iforce_send_packet(iforce, FF_CMD_QUERY, packet);
  241. wait_event_interruptible_timeout(iforce->wait,
  242. !iforce->expect_packet, HZ);
  243. if (iforce->expect_packet) {
  244. iforce->expect_packet = 0;
  245. return -1;
  246. }
  247. #else
  248. dev_err(&iforce->dev->dev,
  249. "iforce_get_id_packet: iforce->bus = SERIO!\n");
  250. #endif
  251. break;
  252. default:
  253. dev_err(&iforce->dev->dev,
  254. "iforce_get_id_packet: iforce->bus = %d\n",
  255. iforce->bus);
  256. break;
  257. }
  258. return -(iforce->edata[0] != packet[0]);
  259. }