iforce-packets.c 7.7 KB
Newer Older
L
Linus Torvalds 已提交
1 2
/*
 *  Copyright (c) 2000-2002 Vojtech Pavlik <vojtech@ucw.cz>
3
 *  Copyright (c) 2001-2002, 2007 Johann Deneux <johann.deneux@gmail.com>
L
Linus Torvalds 已提交
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 31 32 33 34 35 36 37 38 39
 *
 *  USB/RS232 I-Force joysticks and wheels.
 */

/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 * Should you need to contact me, the author, you can do so either by
 * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail:
 * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic
 */

#include "iforce.h"

static struct {
	__s32 x;
	__s32 y;
} iforce_hat_to_axis[16] = {{ 0,-1}, { 1,-1}, { 1, 0}, { 1, 1}, { 0, 1}, {-1, 1}, {-1, 0}, {-1,-1}};


void iforce_dump_packet(char *msg, u16 cmd, unsigned char *data)
{
	int i;

40
	printk(KERN_DEBUG __FILE__ ": %s cmd = %04x, data = ", msg, cmd);
L
Linus Torvalds 已提交
41 42
	for (i = 0; i < LO(cmd); i++)
		printk("%02x ", data[i]);
43
	printk("\n");
L
Linus Torvalds 已提交
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
}

/*
 * Send a packet of bytes to the device
 */
int iforce_send_packet(struct iforce *iforce, u16 cmd, unsigned char* data)
{
	/* Copy data to buffer */
	int n = LO(cmd);
	int c;
	int empty;
	int head, tail;
	unsigned long flags;

/*
 * Update head and tail of xmit buffer
 */
	spin_lock_irqsave(&iforce->xmit_lock, flags);

	head = iforce->xmit.head;
	tail = iforce->xmit.tail;

66

L
Linus Torvalds 已提交
67
	if (CIRC_SPACE(head, tail, XMIT_SIZE) < n+2) {
68 69
		dev_warn(&iforce->dev->dev,
			 "not enough space in xmit buffer to send new packet\n");
L
Linus Torvalds 已提交
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
		spin_unlock_irqrestore(&iforce->xmit_lock, flags);
		return -1;
	}

	empty = head == tail;
	XMIT_INC(iforce->xmit.head, n+2);

/*
 * Store packet in xmit buffer
 */
	iforce->xmit.buf[head] = HI(cmd);
	XMIT_INC(head, 1);
	iforce->xmit.buf[head] = LO(cmd);
	XMIT_INC(head, 1);

	c = CIRC_SPACE_TO_END(head, tail, XMIT_SIZE);
	if (n < c) c=n;

	memcpy(&iforce->xmit.buf[head],
	       data,
	       c);
	if (n != c) {
		memcpy(&iforce->xmit.buf[0],
		       data + c,
		       n - c);
	}
	XMIT_INC(head, n);

	spin_unlock_irqrestore(&iforce->xmit_lock, flags);
/*
 * If necessary, start the transmission
 */
	switch (iforce->bus) {

#ifdef CONFIG_JOYSTICK_IFORCE_232
		case IFORCE_232:
		if (empty)
			iforce_serial_xmit(iforce);
		break;
#endif
#ifdef CONFIG_JOYSTICK_IFORCE_USB
		case IFORCE_USB:

		if (iforce->usbdev && empty &&
			!test_and_set_bit(IFORCE_XMIT_RUNNING, iforce->xmit_flags)) {

			iforce_usb_xmit(iforce);
		}
		break;
#endif
	}
	return 0;
}

/* Start or stop an effect */
int iforce_control_playback(struct iforce* iforce, u16 id, unsigned int value)
{
	unsigned char data[3];

	data[0] = LO(id);
	data[1] = (value > 0) ? ((value > 1) ? 0x41 : 0x01) : 0;
	data[2] = LO(value);
	return iforce_send_packet(iforce, FF_CMD_PLAY, data);
}

/* Mark an effect that was being updated as ready. That means it can be updated
 * again */
static int mark_core_as_ready(struct iforce *iforce, unsigned short addr)
{
	int i;
140

141 142 143 144
	if (!iforce->dev->ff)
		return 0;

	for (i = 0; i < iforce->dev->ff->max_effects; ++i) {
L
Linus Torvalds 已提交
145 146 147 148 149 150 151
		if (test_bit(FF_CORE_IS_USED, iforce->core_effects[i].flags) &&
		    (iforce->core_effects[i].mod1_chunk.start == addr ||
		     iforce->core_effects[i].mod2_chunk.start == addr)) {
			clear_bit(FF_CORE_UPDATE, iforce->core_effects[i].flags);
			return 0;
		}
	}
152
	dev_warn(&iforce->dev->dev, "unused effect %04x updated !!!\n", addr);
L
Linus Torvalds 已提交
153 154 155
	return -1;
}

156
void iforce_process_packet(struct iforce *iforce, u16 cmd, unsigned char *data)
L
Linus Torvalds 已提交
157
{
158
	struct input_dev *dev = iforce->dev;
L
Linus Torvalds 已提交
159 160 161 162
	int i;
	static int being_used = 0;

	if (being_used)
163 164
		dev_warn(&iforce->dev->dev,
			 "re-entrant call to iforce_process %d\n", being_used);
L
Linus Torvalds 已提交
165 166 167 168 169 170 171 172 173
	being_used++;

#ifdef CONFIG_JOYSTICK_IFORCE_232
	if (HI(iforce->expect_packet) == HI(cmd)) {
		iforce->expect_packet = 0;
		iforce->ecmd = cmd;
		memcpy(iforce->edata, data, IFORCE_MAX_LENGTH);
	}
#endif
174
	wake_up(&iforce->wait);
L
Linus Torvalds 已提交
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229

	if (!iforce->type) {
		being_used--;
		return;
	}

	switch (HI(cmd)) {

		case 0x01:	/* joystick position data */
		case 0x03:	/* wheel position data */
			if (HI(cmd) == 1) {
				input_report_abs(dev, ABS_X, (__s16) (((__s16)data[1] << 8) | data[0]));
				input_report_abs(dev, ABS_Y, (__s16) (((__s16)data[3] << 8) | data[2]));
				input_report_abs(dev, ABS_THROTTLE, 255 - data[4]);
				if (LO(cmd) >= 8 && test_bit(ABS_RUDDER ,dev->absbit))
					input_report_abs(dev, ABS_RUDDER, (__s8)data[7]);
			} else {
				input_report_abs(dev, ABS_WHEEL, (__s16) (((__s16)data[1] << 8) | data[0]));
				input_report_abs(dev, ABS_GAS,   255 - data[2]);
				input_report_abs(dev, ABS_BRAKE, 255 - data[3]);
			}

			input_report_abs(dev, ABS_HAT0X, iforce_hat_to_axis[data[6] >> 4].x);
			input_report_abs(dev, ABS_HAT0Y, iforce_hat_to_axis[data[6] >> 4].y);

			for (i = 0; iforce->type->btn[i] >= 0; i++)
				input_report_key(dev, iforce->type->btn[i], data[(i >> 3) + 5] & (1 << (i & 7)));

			/* If there are untouched bits left, interpret them as the second hat */
			if (i <= 8) {
				int btns = data[6];
				if (test_bit(ABS_HAT1X, dev->absbit)) {
					if (btns & 8) input_report_abs(dev, ABS_HAT1X, -1);
					else if (btns & 2) input_report_abs(dev, ABS_HAT1X, 1);
					else input_report_abs(dev, ABS_HAT1X, 0);
				}
				if (test_bit(ABS_HAT1Y, dev->absbit)) {
					if (btns & 1) input_report_abs(dev, ABS_HAT1Y, -1);
					else if (btns & 4) input_report_abs(dev, ABS_HAT1Y, 1);
					else input_report_abs(dev, ABS_HAT1Y, 0);
				}
			}

			input_sync(dev);

			break;

		case 0x02:	/* status report */
			input_report_key(dev, BTN_DEAD, data[0] & 0x02);
			input_sync(dev);

			/* Check if an effect was just started or stopped */
			i = data[1] & 0x7f;
			if (data[1] & 0x80) {
				if (!test_and_set_bit(FF_CORE_IS_PLAYED, iforce->core_effects[i].flags)) {
230 231
					/* Report play event */
					input_report_ff_status(dev, i, FF_STATUS_PLAYING);
L
Linus Torvalds 已提交
232
				}
233
			} else if (test_and_clear_bit(FF_CORE_IS_PLAYED, iforce->core_effects[i].flags)) {
L
Linus Torvalds 已提交
234 235 236 237 238
				/* Report stop event */
				input_report_ff_status(dev, i, FF_STATUS_STOPPED);
			}
			if (LO(cmd) > 3) {
				int j;
239
				for (j = 3; j < LO(cmd); j += 2)
L
Linus Torvalds 已提交
240 241 242 243 244 245 246 247 248 249 250
					mark_core_as_ready(iforce, data[j] | (data[j+1]<<8));
			}
			break;
	}
	being_used--;
}

int iforce_get_id_packet(struct iforce *iforce, char *packet)
{
	switch (iforce->bus) {

251
	case IFORCE_USB: {
L
Linus Torvalds 已提交
252
#ifdef CONFIG_JOYSTICK_IFORCE_USB
253 254
		int status;

L
Linus Torvalds 已提交
255 256 257
		iforce->cr.bRequest = packet[0];
		iforce->ctrl->dev = iforce->usbdev;

J
Johann Deneux 已提交
258 259 260
		status = usb_submit_urb(iforce->ctrl, GFP_ATOMIC);
		if (status) {
			err("usb_submit_urb failed %d", status);
L
Linus Torvalds 已提交
261
			return -1;
J
Johann Deneux 已提交
262
		}
L
Linus Torvalds 已提交
263

264 265
		wait_event_interruptible_timeout(iforce->wait,
			iforce->ctrl->status != -EINPROGRESS, HZ);
L
Linus Torvalds 已提交
266

267
		if (iforce->ctrl->status) {
J
Johann Deneux 已提交
268
			dbg("iforce->ctrl->status = %d", iforce->ctrl->status);
L
Linus Torvalds 已提交
269 270 271 272
			usb_unlink_urb(iforce->ctrl);
			return -1;
		}
#else
J
Johann Deneux 已提交
273
		dbg("iforce_get_id_packet: iforce->bus = USB!");
L
Linus Torvalds 已提交
274
#endif
275
		}
L
Linus Torvalds 已提交
276 277 278 279 280 281 282 283
		break;

	case IFORCE_232:

#ifdef CONFIG_JOYSTICK_IFORCE_232
		iforce->expect_packet = FF_CMD_QUERY;
		iforce_send_packet(iforce, FF_CMD_QUERY, packet);

284 285
		wait_event_interruptible_timeout(iforce->wait,
			!iforce->expect_packet, HZ);
L
Linus Torvalds 已提交
286

287
		if (iforce->expect_packet) {
L
Linus Torvalds 已提交
288 289 290 291
			iforce->expect_packet = 0;
			return -1;
		}
#else
292
		err("iforce_get_id_packet: iforce->bus = SERIO!");
L
Linus Torvalds 已提交
293 294 295 296
#endif
		break;

	default:
297
		err("iforce_get_id_packet: iforce->bus = %d", iforce->bus);
L
Linus Torvalds 已提交
298 299 300 301 302 303
		break;
	}

	return -(iforce->edata[0] != packet[0]);
}