/drivers/input/joystick/iforce/iforce-packets.c

http://github.com/mirrors/linux · C · 211 lines · 145 code · 39 blank · 27 comment · 29 complexity · efb85575081a580bebc68c85de431a7a MD5 · raw file

  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (c) 2000-2002 Vojtech Pavlik <vojtech@ucw.cz>
  4. * Copyright (c) 2001-2002, 2007 Johann Deneux <johann.deneux@gmail.com>
  5. *
  6. * USB/RS232 I-Force joysticks and wheels.
  7. */
  8. #include <asm/unaligned.h>
  9. #include "iforce.h"
  10. static struct {
  11. __s32 x;
  12. __s32 y;
  13. } iforce_hat_to_axis[16] = {{ 0,-1}, { 1,-1}, { 1, 0}, { 1, 1}, { 0, 1}, {-1, 1}, {-1, 0}, {-1,-1}};
  14. void iforce_dump_packet(struct iforce *iforce, char *msg, u16 cmd, unsigned char *data)
  15. {
  16. dev_dbg(iforce->dev->dev.parent, "%s %s cmd = %04x, data = %*ph\n",
  17. __func__, msg, cmd, LO(cmd), data);
  18. }
  19. /*
  20. * Send a packet of bytes to the device
  21. */
  22. int iforce_send_packet(struct iforce *iforce, u16 cmd, unsigned char* data)
  23. {
  24. /* Copy data to buffer */
  25. int n = LO(cmd);
  26. int c;
  27. int empty;
  28. int head, tail;
  29. unsigned long flags;
  30. /*
  31. * Update head and tail of xmit buffer
  32. */
  33. spin_lock_irqsave(&iforce->xmit_lock, flags);
  34. head = iforce->xmit.head;
  35. tail = iforce->xmit.tail;
  36. if (CIRC_SPACE(head, tail, XMIT_SIZE) < n+2) {
  37. dev_warn(&iforce->dev->dev,
  38. "not enough space in xmit buffer to send new packet\n");
  39. spin_unlock_irqrestore(&iforce->xmit_lock, flags);
  40. return -1;
  41. }
  42. empty = head == tail;
  43. XMIT_INC(iforce->xmit.head, n+2);
  44. /*
  45. * Store packet in xmit buffer
  46. */
  47. iforce->xmit.buf[head] = HI(cmd);
  48. XMIT_INC(head, 1);
  49. iforce->xmit.buf[head] = LO(cmd);
  50. XMIT_INC(head, 1);
  51. c = CIRC_SPACE_TO_END(head, tail, XMIT_SIZE);
  52. if (n < c) c=n;
  53. memcpy(&iforce->xmit.buf[head],
  54. data,
  55. c);
  56. if (n != c) {
  57. memcpy(&iforce->xmit.buf[0],
  58. data + c,
  59. n - c);
  60. }
  61. XMIT_INC(head, n);
  62. spin_unlock_irqrestore(&iforce->xmit_lock, flags);
  63. /*
  64. * If necessary, start the transmission
  65. */
  66. if (empty)
  67. iforce->xport_ops->xmit(iforce);
  68. return 0;
  69. }
  70. EXPORT_SYMBOL(iforce_send_packet);
  71. /* Start or stop an effect */
  72. int iforce_control_playback(struct iforce* iforce, u16 id, unsigned int value)
  73. {
  74. unsigned char data[3];
  75. data[0] = LO(id);
  76. data[1] = (value > 0) ? ((value > 1) ? 0x41 : 0x01) : 0;
  77. data[2] = LO(value);
  78. return iforce_send_packet(iforce, FF_CMD_PLAY, data);
  79. }
  80. /* Mark an effect that was being updated as ready. That means it can be updated
  81. * again */
  82. static int mark_core_as_ready(struct iforce *iforce, unsigned short addr)
  83. {
  84. int i;
  85. if (!iforce->dev->ff)
  86. return 0;
  87. for (i = 0; i < iforce->dev->ff->max_effects; ++i) {
  88. if (test_bit(FF_CORE_IS_USED, iforce->core_effects[i].flags) &&
  89. (iforce->core_effects[i].mod1_chunk.start == addr ||
  90. iforce->core_effects[i].mod2_chunk.start == addr)) {
  91. clear_bit(FF_CORE_UPDATE, iforce->core_effects[i].flags);
  92. return 0;
  93. }
  94. }
  95. dev_warn(&iforce->dev->dev, "unused effect %04x updated !!!\n", addr);
  96. return -1;
  97. }
  98. static void iforce_report_hats_buttons(struct iforce *iforce, u8 *data)
  99. {
  100. struct input_dev *dev = iforce->dev;
  101. int i;
  102. input_report_abs(dev, ABS_HAT0X, iforce_hat_to_axis[data[6] >> 4].x);
  103. input_report_abs(dev, ABS_HAT0Y, iforce_hat_to_axis[data[6] >> 4].y);
  104. for (i = 0; iforce->type->btn[i] >= 0; i++)
  105. input_report_key(dev, iforce->type->btn[i],
  106. data[(i >> 3) + 5] & (1 << (i & 7)));
  107. /* If there are untouched bits left, interpret them as the second hat */
  108. if (i <= 8) {
  109. u8 btns = data[6];
  110. if (test_bit(ABS_HAT1X, dev->absbit)) {
  111. if (btns & BIT(3))
  112. input_report_abs(dev, ABS_HAT1X, -1);
  113. else if (btns & BIT(1))
  114. input_report_abs(dev, ABS_HAT1X, 1);
  115. else
  116. input_report_abs(dev, ABS_HAT1X, 0);
  117. }
  118. if (test_bit(ABS_HAT1Y, dev->absbit)) {
  119. if (btns & BIT(0))
  120. input_report_abs(dev, ABS_HAT1Y, -1);
  121. else if (btns & BIT(2))
  122. input_report_abs(dev, ABS_HAT1Y, 1);
  123. else
  124. input_report_abs(dev, ABS_HAT1Y, 0);
  125. }
  126. }
  127. }
  128. void iforce_process_packet(struct iforce *iforce,
  129. u8 packet_id, u8 *data, size_t len)
  130. {
  131. struct input_dev *dev = iforce->dev;
  132. int i, j;
  133. switch (packet_id) {
  134. case 0x01: /* joystick position data */
  135. input_report_abs(dev, ABS_X,
  136. (__s16) get_unaligned_le16(data));
  137. input_report_abs(dev, ABS_Y,
  138. (__s16) get_unaligned_le16(data + 2));
  139. input_report_abs(dev, ABS_THROTTLE, 255 - data[4]);
  140. if (len >= 8 && test_bit(ABS_RUDDER ,dev->absbit))
  141. input_report_abs(dev, ABS_RUDDER, (__s8)data[7]);
  142. iforce_report_hats_buttons(iforce, data);
  143. input_sync(dev);
  144. break;
  145. case 0x03: /* wheel position data */
  146. input_report_abs(dev, ABS_WHEEL,
  147. (__s16) get_unaligned_le16(data));
  148. input_report_abs(dev, ABS_GAS, 255 - data[2]);
  149. input_report_abs(dev, ABS_BRAKE, 255 - data[3]);
  150. iforce_report_hats_buttons(iforce, data);
  151. input_sync(dev);
  152. break;
  153. case 0x02: /* status report */
  154. input_report_key(dev, BTN_DEAD, data[0] & 0x02);
  155. input_sync(dev);
  156. /* Check if an effect was just started or stopped */
  157. i = data[1] & 0x7f;
  158. if (data[1] & 0x80) {
  159. if (!test_and_set_bit(FF_CORE_IS_PLAYED, iforce->core_effects[i].flags)) {
  160. /* Report play event */
  161. input_report_ff_status(dev, i, FF_STATUS_PLAYING);
  162. }
  163. } else if (test_and_clear_bit(FF_CORE_IS_PLAYED, iforce->core_effects[i].flags)) {
  164. /* Report stop event */
  165. input_report_ff_status(dev, i, FF_STATUS_STOPPED);
  166. }
  167. for (j = 3; j < len; j += 2)
  168. mark_core_as_ready(iforce, get_unaligned_le16(data + j));
  169. break;
  170. }
  171. }
  172. EXPORT_SYMBOL(iforce_process_packet);