2 * Copyright (c) 2000-2002 Vojtech Pavlik <vojtech@ucw.cz>
3 * Copyright (c) 2001-2002, 2007 Johann Deneux <johann.deneux@gmail.com>
5 * USB/RS232 I-Force joysticks and wheels.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include <asm/unaligned.h>
30 } 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(struct iforce *iforce, char *msg, u16 cmd, unsigned char *data)
35 dev_dbg(iforce->dev->dev.parent, "%s %s cmd = %04x, data = %*ph\n",
36 __func__, msg, cmd, LO(cmd), data);
40 * Send a packet of bytes to the device
42 int iforce_send_packet(struct iforce *iforce, u16 cmd, unsigned char* data)
44 /* Copy data to buffer */
52 * Update head and tail of xmit buffer
54 spin_lock_irqsave(&iforce->xmit_lock, flags);
56 head = iforce->xmit.head;
57 tail = iforce->xmit.tail;
60 if (CIRC_SPACE(head, tail, XMIT_SIZE) < n+2) {
61 dev_warn(&iforce->dev->dev,
62 "not enough space in xmit buffer to send new packet\n");
63 spin_unlock_irqrestore(&iforce->xmit_lock, flags);
68 XMIT_INC(iforce->xmit.head, n+2);
71 * Store packet in xmit buffer
73 iforce->xmit.buf[head] = HI(cmd);
75 iforce->xmit.buf[head] = LO(cmd);
78 c = CIRC_SPACE_TO_END(head, tail, XMIT_SIZE);
81 memcpy(&iforce->xmit.buf[head],
85 memcpy(&iforce->xmit.buf[0],
91 spin_unlock_irqrestore(&iforce->xmit_lock, flags);
93 * If necessary, start the transmission
96 iforce->xport_ops->xmit(iforce);
100 EXPORT_SYMBOL(iforce_send_packet);
102 /* Start or stop an effect */
103 int iforce_control_playback(struct iforce* iforce, u16 id, unsigned int value)
105 unsigned char data[3];
108 data[1] = (value > 0) ? ((value > 1) ? 0x41 : 0x01) : 0;
110 return iforce_send_packet(iforce, FF_CMD_PLAY, data);
113 /* Mark an effect that was being updated as ready. That means it can be updated
115 static int mark_core_as_ready(struct iforce *iforce, unsigned short addr)
119 if (!iforce->dev->ff)
122 for (i = 0; i < iforce->dev->ff->max_effects; ++i) {
123 if (test_bit(FF_CORE_IS_USED, iforce->core_effects[i].flags) &&
124 (iforce->core_effects[i].mod1_chunk.start == addr ||
125 iforce->core_effects[i].mod2_chunk.start == addr)) {
126 clear_bit(FF_CORE_UPDATE, iforce->core_effects[i].flags);
130 dev_warn(&iforce->dev->dev, "unused effect %04x updated !!!\n", addr);
134 static void iforce_report_hats_buttons(struct iforce *iforce, u8 *data)
136 struct input_dev *dev = iforce->dev;
139 input_report_abs(dev, ABS_HAT0X, iforce_hat_to_axis[data[6] >> 4].x);
140 input_report_abs(dev, ABS_HAT0Y, iforce_hat_to_axis[data[6] >> 4].y);
142 for (i = 0; iforce->type->btn[i] >= 0; i++)
143 input_report_key(dev, iforce->type->btn[i],
144 data[(i >> 3) + 5] & (1 << (i & 7)));
146 /* If there are untouched bits left, interpret them as the second hat */
150 if (test_bit(ABS_HAT1X, dev->absbit)) {
152 input_report_abs(dev, ABS_HAT1X, -1);
153 else if (btns & BIT(1))
154 input_report_abs(dev, ABS_HAT1X, 1);
156 input_report_abs(dev, ABS_HAT1X, 0);
159 if (test_bit(ABS_HAT1Y, dev->absbit)) {
161 input_report_abs(dev, ABS_HAT1Y, -1);
162 else if (btns & BIT(2))
163 input_report_abs(dev, ABS_HAT1Y, 1);
165 input_report_abs(dev, ABS_HAT1Y, 0);
170 void iforce_process_packet(struct iforce *iforce,
171 u8 packet_id, u8 *data, size_t len)
173 struct input_dev *dev = iforce->dev;
178 case 0x01: /* joystick position data */
179 input_report_abs(dev, ABS_X,
180 (__s16) get_unaligned_le16(data));
181 input_report_abs(dev, ABS_Y,
182 (__s16) get_unaligned_le16(data + 2));
183 input_report_abs(dev, ABS_THROTTLE, 255 - data[4]);
185 if (len >= 8 && test_bit(ABS_RUDDER ,dev->absbit))
186 input_report_abs(dev, ABS_RUDDER, (__s8)data[7]);
188 iforce_report_hats_buttons(iforce, data);
193 case 0x03: /* wheel position data */
194 input_report_abs(dev, ABS_WHEEL,
195 (__s16) get_unaligned_le16(data));
196 input_report_abs(dev, ABS_GAS, 255 - data[2]);
197 input_report_abs(dev, ABS_BRAKE, 255 - data[3]);
199 iforce_report_hats_buttons(iforce, data);
204 case 0x02: /* status report */
205 input_report_key(dev, BTN_DEAD, data[0] & 0x02);
208 /* Check if an effect was just started or stopped */
210 if (data[1] & 0x80) {
211 if (!test_and_set_bit(FF_CORE_IS_PLAYED, iforce->core_effects[i].flags)) {
212 /* Report play event */
213 input_report_ff_status(dev, i, FF_STATUS_PLAYING);
215 } else if (test_and_clear_bit(FF_CORE_IS_PLAYED, iforce->core_effects[i].flags)) {
216 /* Report stop event */
217 input_report_ff_status(dev, i, FF_STATUS_STOPPED);
220 for (j = 3; j < len; j += 2)
221 mark_core_as_ready(iforce, get_unaligned_le16(data + j));
226 EXPORT_SYMBOL(iforce_process_packet);