iforce-packets.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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 Johann Deneux <deneux@ifrance.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 "iforce.c: %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. printk(KERN_WARNING "iforce.c: not enough space in xmit buffer to send new packet\n");
  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. printk(KERN_DEBUG "iforce-packets.c: control_playback %d %d\n", id, value);
  110. data[0] = LO(id);
  111. data[1] = (value > 0) ? ((value > 1) ? 0x41 : 0x01) : 0;
  112. data[2] = LO(value);
  113. return iforce_send_packet(iforce, FF_CMD_PLAY, data);
  114. }
  115. /* Mark an effect that was being updated as ready. That means it can be updated
  116. * again */
  117. static int mark_core_as_ready(struct iforce *iforce, unsigned short addr)
  118. {
  119. int i;
  120. for (i=0; i<iforce->dev.ff_effects_max; ++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. printk(KERN_WARNING "iforce-packets.c: unused effect %04x updated !!!\n", addr);
  129. return -1;
  130. }
  131. void iforce_process_packet(struct iforce *iforce, u16 cmd, unsigned char *data, struct pt_regs *regs)
  132. {
  133. struct input_dev *dev = &iforce->dev;
  134. int i;
  135. static int being_used = 0;
  136. if (being_used)
  137. printk(KERN_WARNING "iforce-packets.c: re-entrant call to iforce_process %d\n", being_used);
  138. being_used++;
  139. #ifdef CONFIG_JOYSTICK_IFORCE_232
  140. if (HI(iforce->expect_packet) == HI(cmd)) {
  141. iforce->expect_packet = 0;
  142. iforce->ecmd = cmd;
  143. memcpy(iforce->edata, data, IFORCE_MAX_LENGTH);
  144. wake_up(&iforce->wait);
  145. }
  146. #endif
  147. if (!iforce->type) {
  148. being_used--;
  149. return;
  150. }
  151. switch (HI(cmd)) {
  152. case 0x01: /* joystick position data */
  153. case 0x03: /* wheel position data */
  154. input_regs(dev, regs);
  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_regs(dev, regs);
  188. input_report_key(dev, BTN_DEAD, data[0] & 0x02);
  189. input_sync(dev);
  190. /* Check if an effect was just started or stopped */
  191. i = data[1] & 0x7f;
  192. if (data[1] & 0x80) {
  193. if (!test_and_set_bit(FF_CORE_IS_PLAYED, iforce->core_effects[i].flags)) {
  194. /* Report play event */
  195. input_report_ff_status(dev, i, FF_STATUS_PLAYING);
  196. }
  197. }
  198. else if (test_and_clear_bit(FF_CORE_IS_PLAYED, iforce->core_effects[i].flags)) {
  199. /* Report stop event */
  200. input_report_ff_status(dev, i, FF_STATUS_STOPPED);
  201. }
  202. if (LO(cmd) > 3) {
  203. int j;
  204. for (j=3; j<LO(cmd); j+=2) {
  205. mark_core_as_ready(iforce, data[j] | (data[j+1]<<8));
  206. }
  207. }
  208. break;
  209. }
  210. being_used--;
  211. }
  212. int iforce_get_id_packet(struct iforce *iforce, char *packet)
  213. {
  214. DECLARE_WAITQUEUE(wait, current);
  215. int timeout = HZ; /* 1 second */
  216. switch (iforce->bus) {
  217. case IFORCE_USB:
  218. #ifdef CONFIG_JOYSTICK_IFORCE_USB
  219. iforce->cr.bRequest = packet[0];
  220. iforce->ctrl->dev = iforce->usbdev;
  221. set_current_state(TASK_INTERRUPTIBLE);
  222. add_wait_queue(&iforce->wait, &wait);
  223. if (usb_submit_urb(iforce->ctrl, GFP_ATOMIC)) {
  224. set_current_state(TASK_RUNNING);
  225. remove_wait_queue(&iforce->wait, &wait);
  226. return -1;
  227. }
  228. while (timeout && iforce->ctrl->status == -EINPROGRESS)
  229. timeout = schedule_timeout(timeout);
  230. set_current_state(TASK_RUNNING);
  231. remove_wait_queue(&iforce->wait, &wait);
  232. if (!timeout) {
  233. usb_unlink_urb(iforce->ctrl);
  234. return -1;
  235. }
  236. #else
  237. printk(KERN_ERR "iforce_get_id_packet: iforce->bus = USB!\n");
  238. #endif
  239. break;
  240. case IFORCE_232:
  241. #ifdef CONFIG_JOYSTICK_IFORCE_232
  242. iforce->expect_packet = FF_CMD_QUERY;
  243. iforce_send_packet(iforce, FF_CMD_QUERY, packet);
  244. set_current_state(TASK_INTERRUPTIBLE);
  245. add_wait_queue(&iforce->wait, &wait);
  246. while (timeout && iforce->expect_packet)
  247. timeout = schedule_timeout(timeout);
  248. set_current_state(TASK_RUNNING);
  249. remove_wait_queue(&iforce->wait, &wait);
  250. if (!timeout) {
  251. iforce->expect_packet = 0;
  252. return -1;
  253. }
  254. #else
  255. printk(KERN_ERR "iforce_get_id_packet: iforce->bus = SERIO!\n");
  256. #endif
  257. break;
  258. default:
  259. printk(KERN_ERR "iforce_get_id_packet: iforce->bus = %d\n",
  260. iforce->bus);
  261. break;
  262. }
  263. return -(iforce->edata[0] != packet[0]);
  264. }