iforce-packets.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. warn("not enough space in xmit buffer to send new packet");
  58. spin_unlock_irqrestore(&iforce->xmit_lock, flags);
  59. return -1;
  60. }
  61. empty = head == tail;
  62. XMIT_INC(iforce->xmit.head, n+2);
  63. /*
  64. * Store packet in xmit buffer
  65. */
  66. iforce->xmit.buf[head] = HI(cmd);
  67. XMIT_INC(head, 1);
  68. iforce->xmit.buf[head] = LO(cmd);
  69. XMIT_INC(head, 1);
  70. c = CIRC_SPACE_TO_END(head, tail, XMIT_SIZE);
  71. if (n < c) c=n;
  72. memcpy(&iforce->xmit.buf[head],
  73. data,
  74. c);
  75. if (n != c) {
  76. memcpy(&iforce->xmit.buf[0],
  77. data + c,
  78. n - c);
  79. }
  80. XMIT_INC(head, n);
  81. spin_unlock_irqrestore(&iforce->xmit_lock, flags);
  82. /*
  83. * If necessary, start the transmission
  84. */
  85. switch (iforce->bus) {
  86. #ifdef CONFIG_JOYSTICK_IFORCE_232
  87. case IFORCE_232:
  88. if (empty)
  89. iforce_serial_xmit(iforce);
  90. break;
  91. #endif
  92. #ifdef CONFIG_JOYSTICK_IFORCE_USB
  93. case IFORCE_USB:
  94. if (iforce->usbdev && empty &&
  95. !test_and_set_bit(IFORCE_XMIT_RUNNING, iforce->xmit_flags)) {
  96. iforce_usb_xmit(iforce);
  97. }
  98. break;
  99. #endif
  100. }
  101. return 0;
  102. }
  103. /* Start or stop an effect */
  104. int iforce_control_playback(struct iforce* iforce, u16 id, unsigned int value)
  105. {
  106. unsigned char data[3];
  107. data[0] = LO(id);
  108. data[1] = (value > 0) ? ((value > 1) ? 0x41 : 0x01) : 0;
  109. data[2] = LO(value);
  110. return iforce_send_packet(iforce, FF_CMD_PLAY, data);
  111. }
  112. /* Mark an effect that was being updated as ready. That means it can be updated
  113. * again */
  114. static int mark_core_as_ready(struct iforce *iforce, unsigned short addr)
  115. {
  116. int i;
  117. if (!iforce->dev->ff)
  118. return 0;
  119. for (i = 0; i < iforce->dev->ff->max_effects; ++i) {
  120. if (test_bit(FF_CORE_IS_USED, iforce->core_effects[i].flags) &&
  121. (iforce->core_effects[i].mod1_chunk.start == addr ||
  122. iforce->core_effects[i].mod2_chunk.start == addr)) {
  123. clear_bit(FF_CORE_UPDATE, iforce->core_effects[i].flags);
  124. return 0;
  125. }
  126. }
  127. warn("unused effect %04x updated !!!", addr);
  128. return -1;
  129. }
  130. void iforce_process_packet(struct iforce *iforce, u16 cmd, unsigned char *data)
  131. {
  132. struct input_dev *dev = iforce->dev;
  133. int i;
  134. static int being_used = 0;
  135. if (being_used)
  136. warn("re-entrant call to iforce_process %d", being_used);
  137. being_used++;
  138. #ifdef CONFIG_JOYSTICK_IFORCE_232
  139. if (HI(iforce->expect_packet) == HI(cmd)) {
  140. iforce->expect_packet = 0;
  141. iforce->ecmd = cmd;
  142. memcpy(iforce->edata, data, IFORCE_MAX_LENGTH);
  143. }
  144. #endif
  145. wake_up(&iforce->wait);
  146. if (!iforce->type) {
  147. being_used--;
  148. return;
  149. }
  150. switch (HI(cmd)) {
  151. case 0x01: /* joystick position data */
  152. case 0x03: /* wheel position data */
  153. if (HI(cmd) == 1) {
  154. input_report_abs(dev, ABS_X, (__s16) (((__s16)data[1] << 8) | data[0]));
  155. input_report_abs(dev, ABS_Y, (__s16) (((__s16)data[3] << 8) | data[2]));
  156. input_report_abs(dev, ABS_THROTTLE, 255 - data[4]);
  157. if (LO(cmd) >= 8 && test_bit(ABS_RUDDER ,dev->absbit))
  158. input_report_abs(dev, ABS_RUDDER, (__s8)data[7]);
  159. } else {
  160. input_report_abs(dev, ABS_WHEEL, (__s16) (((__s16)data[1] << 8) | data[0]));
  161. input_report_abs(dev, ABS_GAS, 255 - data[2]);
  162. input_report_abs(dev, ABS_BRAKE, 255 - data[3]);
  163. }
  164. input_report_abs(dev, ABS_HAT0X, iforce_hat_to_axis[data[6] >> 4].x);
  165. input_report_abs(dev, ABS_HAT0Y, iforce_hat_to_axis[data[6] >> 4].y);
  166. for (i = 0; iforce->type->btn[i] >= 0; i++)
  167. input_report_key(dev, iforce->type->btn[i], data[(i >> 3) + 5] & (1 << (i & 7)));
  168. /* If there are untouched bits left, interpret them as the second hat */
  169. if (i <= 8) {
  170. int btns = data[6];
  171. if (test_bit(ABS_HAT1X, dev->absbit)) {
  172. if (btns & 8) input_report_abs(dev, ABS_HAT1X, -1);
  173. else if (btns & 2) input_report_abs(dev, ABS_HAT1X, 1);
  174. else input_report_abs(dev, ABS_HAT1X, 0);
  175. }
  176. if (test_bit(ABS_HAT1Y, dev->absbit)) {
  177. if (btns & 1) input_report_abs(dev, ABS_HAT1Y, -1);
  178. else if (btns & 4) input_report_abs(dev, ABS_HAT1Y, 1);
  179. else input_report_abs(dev, ABS_HAT1Y, 0);
  180. }
  181. }
  182. input_sync(dev);
  183. break;
  184. case 0x02: /* status report */
  185. input_report_key(dev, BTN_DEAD, data[0] & 0x02);
  186. input_sync(dev);
  187. /* Check if an effect was just started or stopped */
  188. i = data[1] & 0x7f;
  189. if (data[1] & 0x80) {
  190. if (!test_and_set_bit(FF_CORE_IS_PLAYED, iforce->core_effects[i].flags)) {
  191. /* Report play event */
  192. input_report_ff_status(dev, i, FF_STATUS_PLAYING);
  193. }
  194. } else if (test_and_clear_bit(FF_CORE_IS_PLAYED, iforce->core_effects[i].flags)) {
  195. /* Report stop event */
  196. input_report_ff_status(dev, i, FF_STATUS_STOPPED);
  197. }
  198. if (LO(cmd) > 3) {
  199. int j;
  200. for (j = 3; j < LO(cmd); j += 2)
  201. mark_core_as_ready(iforce, data[j] | (data[j+1]<<8));
  202. }
  203. break;
  204. }
  205. being_used--;
  206. }
  207. int iforce_get_id_packet(struct iforce *iforce, char *packet)
  208. {
  209. switch (iforce->bus) {
  210. case IFORCE_USB: {
  211. #ifdef CONFIG_JOYSTICK_IFORCE_USB
  212. int status;
  213. iforce->cr.bRequest = packet[0];
  214. iforce->ctrl->dev = iforce->usbdev;
  215. status = usb_submit_urb(iforce->ctrl, GFP_ATOMIC);
  216. if (status) {
  217. err("usb_submit_urb failed %d", status);
  218. return -1;
  219. }
  220. wait_event_interruptible_timeout(iforce->wait,
  221. iforce->ctrl->status != -EINPROGRESS, HZ);
  222. if (iforce->ctrl->status) {
  223. dbg("iforce->ctrl->status = %d", iforce->ctrl->status);
  224. usb_unlink_urb(iforce->ctrl);
  225. return -1;
  226. }
  227. #else
  228. dbg("iforce_get_id_packet: iforce->bus = USB!");
  229. #endif
  230. }
  231. break;
  232. case IFORCE_232:
  233. #ifdef CONFIG_JOYSTICK_IFORCE_232
  234. iforce->expect_packet = FF_CMD_QUERY;
  235. iforce_send_packet(iforce, FF_CMD_QUERY, packet);
  236. wait_event_interruptible_timeout(iforce->wait,
  237. !iforce->expect_packet, HZ);
  238. if (iforce->expect_packet) {
  239. iforce->expect_packet = 0;
  240. return -1;
  241. }
  242. #else
  243. err("iforce_get_id_packet: iforce->bus = SERIO!");
  244. #endif
  245. break;
  246. default:
  247. err("iforce_get_id_packet: iforce->bus = %d", iforce->bus);
  248. break;
  249. }
  250. return -(iforce->edata[0] != packet[0]);
  251. }