2 * $Id: iforce-packets.c,v 1.16 2002/07/07 10:22:50 jdeneux Exp $
4 * Copyright (c) 2000-2002 Vojtech Pavlik <vojtech@ucw.cz>
5 * Copyright (c) 2001-2002 Johann Deneux <deneux@ifrance.com>
7 * USB/RS232 I-Force joysticks and wheels.
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 * Should you need to contact me, the author, you can do so either by
26 * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail:
27 * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic
35 } iforce_hat_to_axis[16] = {{ 0,-1}, { 1,-1}, { 1, 0}, { 1, 1}, { 0, 1}, {-1, 1}, {-1, 0}, {-1,-1}};
38 void iforce_dump_packet(char *msg, u16 cmd, unsigned char *data)
42 printk(KERN_DEBUG "iforce.c: %s ( cmd = %04x, data = ", msg, cmd);
43 for (i = 0; i < LO(cmd); i++)
44 printk("%02x ", data[i]);
49 * Send a packet of bytes to the device
51 int iforce_send_packet(struct iforce *iforce, u16 cmd, unsigned char* data)
53 /* Copy data to buffer */
61 * Update head and tail of xmit buffer
63 spin_lock_irqsave(&iforce->xmit_lock, flags);
65 head = iforce->xmit.head;
66 tail = iforce->xmit.tail;
68 if (CIRC_SPACE(head, tail, XMIT_SIZE) < n+2) {
69 printk(KERN_WARNING "iforce.c: not enough space in xmit buffer to send new packet\n");
70 spin_unlock_irqrestore(&iforce->xmit_lock, flags);
75 XMIT_INC(iforce->xmit.head, n+2);
78 * Store packet in xmit buffer
80 iforce->xmit.buf[head] = HI(cmd);
82 iforce->xmit.buf[head] = LO(cmd);
85 c = CIRC_SPACE_TO_END(head, tail, XMIT_SIZE);
88 memcpy(&iforce->xmit.buf[head],
92 memcpy(&iforce->xmit.buf[0],
98 spin_unlock_irqrestore(&iforce->xmit_lock, flags);
100 * If necessary, start the transmission
102 switch (iforce->bus) {
104 #ifdef CONFIG_JOYSTICK_IFORCE_232
107 iforce_serial_xmit(iforce);
110 #ifdef CONFIG_JOYSTICK_IFORCE_USB
113 if (iforce->usbdev && empty &&
114 !test_and_set_bit(IFORCE_XMIT_RUNNING, iforce->xmit_flags)) {
116 iforce_usb_xmit(iforce);
124 /* Start or stop an effect */
125 int iforce_control_playback(struct iforce* iforce, u16 id, unsigned int value)
127 unsigned char data[3];
129 printk(KERN_DEBUG "iforce-packets.c: control_playback %d %d\n", id, value);
132 data[1] = (value > 0) ? ((value > 1) ? 0x41 : 0x01) : 0;
134 return iforce_send_packet(iforce, FF_CMD_PLAY, data);
137 /* Mark an effect that was being updated as ready. That means it can be updated
139 static int mark_core_as_ready(struct iforce *iforce, unsigned short addr)
143 if (!iforce->dev->ff)
146 for (i = 0; i < iforce->dev->ff->max_effects; ++i) {
147 if (test_bit(FF_CORE_IS_USED, iforce->core_effects[i].flags) &&
148 (iforce->core_effects[i].mod1_chunk.start == addr ||
149 iforce->core_effects[i].mod2_chunk.start == addr)) {
150 clear_bit(FF_CORE_UPDATE, iforce->core_effects[i].flags);
154 printk(KERN_WARNING "iforce-packets.c: unused effect %04x updated !!!\n", addr);
158 void iforce_process_packet(struct iforce *iforce, u16 cmd, unsigned char *data, struct pt_regs *regs)
160 struct input_dev *dev = iforce->dev;
162 static int being_used = 0;
165 printk(KERN_WARNING "iforce-packets.c: re-entrant call to iforce_process %d\n", being_used);
168 #ifdef CONFIG_JOYSTICK_IFORCE_232
169 if (HI(iforce->expect_packet) == HI(cmd)) {
170 iforce->expect_packet = 0;
172 memcpy(iforce->edata, data, IFORCE_MAX_LENGTH);
175 wake_up(&iforce->wait);
184 case 0x01: /* joystick position data */
185 case 0x03: /* wheel position data */
187 input_regs(dev, regs);
190 input_report_abs(dev, ABS_X, (__s16) (((__s16)data[1] << 8) | data[0]));
191 input_report_abs(dev, ABS_Y, (__s16) (((__s16)data[3] << 8) | data[2]));
192 input_report_abs(dev, ABS_THROTTLE, 255 - data[4]);
193 if (LO(cmd) >= 8 && test_bit(ABS_RUDDER ,dev->absbit))
194 input_report_abs(dev, ABS_RUDDER, (__s8)data[7]);
196 input_report_abs(dev, ABS_WHEEL, (__s16) (((__s16)data[1] << 8) | data[0]));
197 input_report_abs(dev, ABS_GAS, 255 - data[2]);
198 input_report_abs(dev, ABS_BRAKE, 255 - data[3]);
201 input_report_abs(dev, ABS_HAT0X, iforce_hat_to_axis[data[6] >> 4].x);
202 input_report_abs(dev, ABS_HAT0Y, iforce_hat_to_axis[data[6] >> 4].y);
204 for (i = 0; iforce->type->btn[i] >= 0; i++)
205 input_report_key(dev, iforce->type->btn[i], data[(i >> 3) + 5] & (1 << (i & 7)));
207 /* If there are untouched bits left, interpret them as the second hat */
210 if (test_bit(ABS_HAT1X, dev->absbit)) {
211 if (btns & 8) input_report_abs(dev, ABS_HAT1X, -1);
212 else if (btns & 2) input_report_abs(dev, ABS_HAT1X, 1);
213 else input_report_abs(dev, ABS_HAT1X, 0);
215 if (test_bit(ABS_HAT1Y, dev->absbit)) {
216 if (btns & 1) input_report_abs(dev, ABS_HAT1Y, -1);
217 else if (btns & 4) input_report_abs(dev, ABS_HAT1Y, 1);
218 else input_report_abs(dev, ABS_HAT1Y, 0);
226 case 0x02: /* status report */
227 input_regs(dev, regs);
228 input_report_key(dev, BTN_DEAD, data[0] & 0x02);
231 /* Check if an effect was just started or stopped */
233 if (data[1] & 0x80) {
234 if (!test_and_set_bit(FF_CORE_IS_PLAYED, iforce->core_effects[i].flags)) {
235 /* Report play event */
236 input_report_ff_status(dev, i, FF_STATUS_PLAYING);
238 } else if (test_and_clear_bit(FF_CORE_IS_PLAYED, iforce->core_effects[i].flags)) {
239 /* Report stop event */
240 input_report_ff_status(dev, i, FF_STATUS_STOPPED);
244 for (j = 3; j < LO(cmd); j += 2)
245 mark_core_as_ready(iforce, data[j] | (data[j+1]<<8));
252 int iforce_get_id_packet(struct iforce *iforce, char *packet)
254 switch (iforce->bus) {
258 #ifdef CONFIG_JOYSTICK_IFORCE_USB
259 iforce->cr.bRequest = packet[0];
260 iforce->ctrl->dev = iforce->usbdev;
262 if (usb_submit_urb(iforce->ctrl, GFP_ATOMIC))
265 wait_event_interruptible_timeout(iforce->wait,
266 iforce->ctrl->status != -EINPROGRESS, HZ);
268 if (iforce->ctrl->status) {
269 usb_unlink_urb(iforce->ctrl);
273 printk(KERN_ERR "iforce_get_id_packet: iforce->bus = USB!\n");
279 #ifdef CONFIG_JOYSTICK_IFORCE_232
280 iforce->expect_packet = FF_CMD_QUERY;
281 iforce_send_packet(iforce, FF_CMD_QUERY, packet);
283 wait_event_interruptible_timeout(iforce->wait,
284 !iforce->expect_packet, HZ);
286 if (iforce->expect_packet) {
287 iforce->expect_packet = 0;
291 printk(KERN_ERR "iforce_get_id_packet: iforce->bus = SERIO!\n");
296 printk(KERN_ERR "iforce_get_id_packet: iforce->bus = %d\n",
301 return -(iforce->edata[0] != packet[0]);