1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30#include "iforce.h"
31
32static struct {
33 __s32 x;
34 __s32 y;
35} iforce_hat_to_axis[16] = {{ 0,-1}, { 1,-1}, { 1, 0}, { 1, 1}, { 0, 1}, {-1, 1}, {-1, 0}, {-1,-1}};
36
37
38void iforce_dump_packet(char *msg, u16 cmd, unsigned char *data)
39{
40 int i;
41
42 printk(KERN_DEBUG __FILE__ ": %s cmd = %04x, data = ", msg, cmd);
43 for (i = 0; i < LO(cmd); i++)
44 printk("%02x ", data[i]);
45 printk("\n");
46}
47
48
49
50
51int iforce_send_packet(struct iforce *iforce, u16 cmd, unsigned char* data)
52{
53
54 int n = LO(cmd);
55 int c;
56 int empty;
57 int head, tail;
58 unsigned long flags;
59
60
61
62
63 spin_lock_irqsave(&iforce->xmit_lock, flags);
64
65 head = iforce->xmit.head;
66 tail = iforce->xmit.tail;
67
68
69 if (CIRC_SPACE(head, tail, XMIT_SIZE) < n+2) {
70 warn("not enough space in xmit buffer to send new packet");
71 spin_unlock_irqrestore(&iforce->xmit_lock, flags);
72 return -1;
73 }
74
75 empty = head == tail;
76 XMIT_INC(iforce->xmit.head, n+2);
77
78
79
80
81 iforce->xmit.buf[head] = HI(cmd);
82 XMIT_INC(head, 1);
83 iforce->xmit.buf[head] = LO(cmd);
84 XMIT_INC(head, 1);
85
86 c = CIRC_SPACE_TO_END(head, tail, XMIT_SIZE);
87 if (n < c) c=n;
88
89 memcpy(&iforce->xmit.buf[head],
90 data,
91 c);
92 if (n != c) {
93 memcpy(&iforce->xmit.buf[0],
94 data + c,
95 n - c);
96 }
97 XMIT_INC(head, n);
98
99 spin_unlock_irqrestore(&iforce->xmit_lock, flags);
100
101
102
103 switch (iforce->bus) {
104
105#ifdef CONFIG_JOYSTICK_IFORCE_232
106 case IFORCE_232:
107 if (empty)
108 iforce_serial_xmit(iforce);
109 break;
110#endif
111#ifdef CONFIG_JOYSTICK_IFORCE_USB
112 case IFORCE_USB:
113
114 if (iforce->usbdev && empty &&
115 !test_and_set_bit(IFORCE_XMIT_RUNNING, iforce->xmit_flags)) {
116
117 iforce_usb_xmit(iforce);
118 }
119 break;
120#endif
121 }
122 return 0;
123}
124
125
126int iforce_control_playback(struct iforce* iforce, u16 id, unsigned int value)
127{
128 unsigned char data[3];
129
130 data[0] = LO(id);
131 data[1] = (value > 0) ? ((value > 1) ? 0x41 : 0x01) : 0;
132 data[2] = LO(value);
133 return iforce_send_packet(iforce, FF_CMD_PLAY, data);
134}
135
136
137
138static int mark_core_as_ready(struct iforce *iforce, unsigned short addr)
139{
140 int i;
141
142 if (!iforce->dev->ff)
143 return 0;
144
145 for (i = 0; i < iforce->dev->ff->max_effects; ++i) {
146 if (test_bit(FF_CORE_IS_USED, iforce->core_effects[i].flags) &&
147 (iforce->core_effects[i].mod1_chunk.start == addr ||
148 iforce->core_effects[i].mod2_chunk.start == addr)) {
149 clear_bit(FF_CORE_UPDATE, iforce->core_effects[i].flags);
150 return 0;
151 }
152 }
153 warn("unused effect %04x updated !!!", addr);
154 return -1;
155}
156
157void iforce_process_packet(struct iforce *iforce, u16 cmd, unsigned char *data)
158{
159 struct input_dev *dev = iforce->dev;
160 int i;
161 static int being_used = 0;
162
163 if (being_used)
164 warn("re-entrant call to iforce_process %d", being_used);
165 being_used++;
166
167#ifdef CONFIG_JOYSTICK_IFORCE_232
168 if (HI(iforce->expect_packet) == HI(cmd)) {
169 iforce->expect_packet = 0;
170 iforce->ecmd = cmd;
171 memcpy(iforce->edata, data, IFORCE_MAX_LENGTH);
172 }
173#endif
174 wake_up(&iforce->wait);
175
176 if (!iforce->type) {
177 being_used--;
178 return;
179 }
180
181 switch (HI(cmd)) {
182
183 case 0x01:
184 case 0x03:
185 if (HI(cmd) == 1) {
186 input_report_abs(dev, ABS_X, (__s16) (((__s16)data[1] << 8) | data[0]));
187 input_report_abs(dev, ABS_Y, (__s16) (((__s16)data[3] << 8) | data[2]));
188 input_report_abs(dev, ABS_THROTTLE, 255 - data[4]);
189 if (LO(cmd) >= 8 && test_bit(ABS_RUDDER ,dev->absbit))
190 input_report_abs(dev, ABS_RUDDER, (__s8)data[7]);
191 } else {
192 input_report_abs(dev, ABS_WHEEL, (__s16) (((__s16)data[1] << 8) | data[0]));
193 input_report_abs(dev, ABS_GAS, 255 - data[2]);
194 input_report_abs(dev, ABS_BRAKE, 255 - data[3]);
195 }
196
197 input_report_abs(dev, ABS_HAT0X, iforce_hat_to_axis[data[6] >> 4].x);
198 input_report_abs(dev, ABS_HAT0Y, iforce_hat_to_axis[data[6] >> 4].y);
199
200 for (i = 0; iforce->type->btn[i] >= 0; i++)
201 input_report_key(dev, iforce->type->btn[i], data[(i >> 3) + 5] & (1 << (i & 7)));
202
203
204 if (i <= 8) {
205 int btns = data[6];
206 if (test_bit(ABS_HAT1X, dev->absbit)) {
207 if (btns & 8) input_report_abs(dev, ABS_HAT1X, -1);
208 else if (btns & 2) input_report_abs(dev, ABS_HAT1X, 1);
209 else input_report_abs(dev, ABS_HAT1X, 0);
210 }
211 if (test_bit(ABS_HAT1Y, dev->absbit)) {
212 if (btns & 1) input_report_abs(dev, ABS_HAT1Y, -1);
213 else if (btns & 4) input_report_abs(dev, ABS_HAT1Y, 1);
214 else input_report_abs(dev, ABS_HAT1Y, 0);
215 }
216 }
217
218 input_sync(dev);
219
220 break;
221
222 case 0x02:
223 input_report_key(dev, BTN_DEAD, data[0] & 0x02);
224 input_sync(dev);
225
226
227 i = data[1] & 0x7f;
228 if (data[1] & 0x80) {
229 if (!test_and_set_bit(FF_CORE_IS_PLAYED, iforce->core_effects[i].flags)) {
230
231 input_report_ff_status(dev, i, FF_STATUS_PLAYING);
232 }
233 } else if (test_and_clear_bit(FF_CORE_IS_PLAYED, iforce->core_effects[i].flags)) {
234
235 input_report_ff_status(dev, i, FF_STATUS_STOPPED);
236 }
237 if (LO(cmd) > 3) {
238 int j;
239 for (j = 3; j < LO(cmd); j += 2)
240 mark_core_as_ready(iforce, data[j] | (data[j+1]<<8));
241 }
242 break;
243 }
244 being_used--;
245}
246
247int iforce_get_id_packet(struct iforce *iforce, char *packet)
248{
249 switch (iforce->bus) {
250
251 case IFORCE_USB: {
252#ifdef CONFIG_JOYSTICK_IFORCE_USB
253 int status;
254
255 iforce->cr.bRequest = packet[0];
256 iforce->ctrl->dev = iforce->usbdev;
257
258 status = usb_submit_urb(iforce->ctrl, GFP_ATOMIC);
259 if (status) {
260 err("usb_submit_urb failed %d", status);
261 return -1;
262 }
263
264 wait_event_interruptible_timeout(iforce->wait,
265 iforce->ctrl->status != -EINPROGRESS, HZ);
266
267 if (iforce->ctrl->status) {
268 dbg("iforce->ctrl->status = %d", iforce->ctrl->status);
269 usb_unlink_urb(iforce->ctrl);
270 return -1;
271 }
272#else
273 dbg("iforce_get_id_packet: iforce->bus = USB!");
274#endif
275 }
276 break;
277
278 case IFORCE_232:
279
280#ifdef CONFIG_JOYSTICK_IFORCE_232
281 iforce->expect_packet = FF_CMD_QUERY;
282 iforce_send_packet(iforce, FF_CMD_QUERY, packet);
283
284 wait_event_interruptible_timeout(iforce->wait,
285 !iforce->expect_packet, HZ);
286
287 if (iforce->expect_packet) {
288 iforce->expect_packet = 0;
289 return -1;
290 }
291#else
292 err("iforce_get_id_packet: iforce->bus = SERIO!");
293#endif
294 break;
295
296 default:
297 err("iforce_get_id_packet: iforce->bus = %d", iforce->bus);
298 break;
299 }
300
301 return -(iforce->edata[0] != packet[0]);
302}
303
304