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