iforce-packets.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /*
  2. * $Id: iforce-packets.c,v 1.16 2002/07/07 10:22:50 jdeneux Exp $
  3. *
  4. * Copyright (c) 2000-2002 Vojtech Pavlik <vojtech@ucw.cz>
  5. * Copyright (c) 2001-2002, 2007 Johann Deneux <johann.deneux@gmail.com>
  6. *
  7. * USB/RS232 I-Force joysticks and wheels.
  8. */
  9. /*
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. *
  24. * Should you need to contact me, the author, you can do so either by
  25. * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail:
  26. * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic
  27. */
  28. #include "iforce.h"
  29. static struct {
  30. __s32 x;
  31. __s32 y;
  32. } iforce_hat_to_axis[16] = {{ 0,-1}, { 1,-1}, { 1, 0}, { 1, 1}, { 0, 1}, {-1, 1}, {-1, 0}, {-1,-1}};
  33. void iforce_dump_packet(char *msg, u16 cmd, unsigned char *data)
  34. {
  35. int i;
  36. printk(KERN_DEBUG __FILE__ ": %s cmd = %04x, data = ", msg, cmd);
  37. for (i = 0; i < LO(cmd); i++)
  38. printk("%02x ", data[i]);
  39. printk("\n");
  40. }
  41. /*
  42. * Send a packet of bytes to the device
  43. */
  44. int iforce_send_packet(struct iforce *iforce, u16 cmd, unsigned char* data)
  45. {
  46. /* Copy data to buffer */
  47. int n = LO(cmd);
  48. int c;
  49. int empty;
  50. int head, tail;
  51. unsigned long flags;
  52. /*
  53. * Update head and tail of xmit buffer
  54. */
  55. spin_lock_irqsave(&iforce->xmit_lock, flags);
  56. head = iforce->xmit.head;
  57. tail = iforce->xmit.tail;
  58. if (CIRC_SPACE(head, tail, XMIT_SIZE) < n+2) {
  59. warn("not enough space in xmit buffer to send new packet");
  60. spin_unlock_irqrestore(&iforce->xmit_lock, flags);
  61. return -1;
  62. }
  63. empty = head == tail;
  64. XMIT_INC(iforce->xmit.head, n+2);
  65. /*
  66. * Store packet in xmit buffer
  67. */
  68. iforce->xmit.buf[head] = HI(cmd);
  69. XMIT_INC(head, 1);
  70. iforce->xmit.buf[head] = LO(cmd);
  71. XMIT_INC(head, 1);
  72. c = CIRC_SPACE_TO_END(head, tail, XMIT_SIZE);
  73. if (n < c) c=n;
  74. memcpy(&iforce->xmit.buf[head],
  75. data,
  76. c);
  77. if (n != c) {
  78. memcpy(&iforce->xmit.buf[0],
  79. data + c,
  80. n - c);
  81. }
  82. XMIT_INC(head, n);
  83. spin_unlock_irqrestore(&iforce->xmit_lock, flags);
  84. /*
  85. * If necessary, start the transmission
  86. */
  87. switch (iforce->bus) {
  88. #ifdef CONFIG_JOYSTICK_IFORCE_232
  89. case IFORCE_232:
  90. if (empty)
  91. iforce_serial_xmit(iforce);
  92. break;
  93. #endif
  94. #ifdef CONFIG_JOYSTICK_IFORCE_USB
  95. case IFORCE_USB:
  96. if (iforce->usbdev && empty &&
  97. !test_and_set_bit(IFORCE_XMIT_RUNNING, iforce->xmit_flags)) {
  98. iforce_usb_xmit(iforce);
  99. }
  100. break;
  101. #endif
  102. }
  103. return 0;
  104. }
  105. /* Start or stop an effect */
  106. int iforce_control_playback(struct iforce* iforce, u16 id, unsigned int value)
  107. {
  108. unsigned char data[3];
  109. data[0] = LO(id);
  110. data[1] = (value > 0) ? ((value > 1) ? 0x41 : 0x01) : 0;
  111. data[2] = LO(value);
  112. return iforce_send_packet(iforce, FF_CMD_PLAY, data);
  113. }
  114. /* Mark an effect that was being updated as ready. That means it can be updated
  115. * again */
  116. static int mark_core_as_ready(struct iforce *iforce, unsigned short addr)
  117. {
  118. int i;
  119. if (!iforce->dev->ff)
  120. return 0;
  121. for (i = 0; i < iforce->dev->ff->max_effects; ++i) {
  122. if (test_bit(FF_CORE_IS_USED, iforce->core_effects[i].flags) &&
  123. (iforce->core_effects[i].mod1_chunk.start == addr ||
  124. iforce->core_effects[i].mod2_chunk.start == addr)) {
  125. clear_bit(FF_CORE_UPDATE, iforce->core_effects[i].flags);
  126. return 0;
  127. }
  128. }
  129. warn("unused effect %04x updated !!!", addr);
  130. return -1;
  131. }
  132. void iforce_process_packet(struct iforce *iforce, u16 cmd, unsigned char *data)
  133. {
  134. struct input_dev *dev = iforce->dev;
  135. int i;
  136. static int being_used = 0;
  137. if (being_used)
  138. warn("re-entrant call to iforce_process %d", 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. iforce->cr.bRequest = packet[0];
  215. iforce->ctrl->dev = iforce->usbdev;
  216. if (usb_submit_urb(iforce->ctrl, GFP_ATOMIC))
  217. return -1;
  218. wait_event_interruptible_timeout(iforce->wait,
  219. iforce->ctrl->status != -EINPROGRESS, HZ);
  220. if (iforce->ctrl->status) {
  221. usb_unlink_urb(iforce->ctrl);
  222. return -1;
  223. }
  224. #else
  225. err("iforce_get_id_packet: iforce->bus = USB!");
  226. #endif
  227. break;
  228. case IFORCE_232:
  229. #ifdef CONFIG_JOYSTICK_IFORCE_232
  230. iforce->expect_packet = FF_CMD_QUERY;
  231. iforce_send_packet(iforce, FF_CMD_QUERY, packet);
  232. wait_event_interruptible_timeout(iforce->wait,
  233. !iforce->expect_packet, HZ);
  234. if (iforce->expect_packet) {
  235. iforce->expect_packet = 0;
  236. return -1;
  237. }
  238. #else
  239. err("iforce_get_id_packet: iforce->bus = SERIO!");
  240. #endif
  241. break;
  242. default:
  243. err("iforce_get_id_packet: iforce->bus = %d", iforce->bus);
  244. break;
  245. }
  246. return -(iforce->edata[0] != packet[0]);
  247. }