1/*
2 *  Copyright (c) 2000-2001 Vojtech Pavlik <vojtech@ucw.cz>
3 *  Copyright (c) 2001, 2007 Johann Deneux <johann.deneux@gmail.com>
4 *
5 *  USB/RS232 I-Force joysticks and wheels.
6 */
7
8/*
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.
13 *
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.
18 *
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
22 *
23 * Should you need to contact me, the author, you can do so either by
24 * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail:
25 * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic
26 */
27
28#include "iforce.h"
29
30void iforce_serial_xmit(struct iforce *iforce)
31{
32	unsigned char cs;
33	int i;
34	unsigned long flags;
35
36	if (test_and_set_bit(IFORCE_XMIT_RUNNING, iforce->xmit_flags)) {
37		set_bit(IFORCE_XMIT_AGAIN, iforce->xmit_flags);
38		return;
39	}
40
41	spin_lock_irqsave(&iforce->xmit_lock, flags);
42
43again:
44	if (iforce->xmit.head == iforce->xmit.tail) {
45		clear_bit(IFORCE_XMIT_RUNNING, iforce->xmit_flags);
46		spin_unlock_irqrestore(&iforce->xmit_lock, flags);
47		return;
48	}
49
50	cs = 0x2b;
51
52	serio_write(iforce->serio, 0x2b);
53
54	serio_write(iforce->serio, iforce->xmit.buf[iforce->xmit.tail]);
55	cs ^= iforce->xmit.buf[iforce->xmit.tail];
56	XMIT_INC(iforce->xmit.tail, 1);
57
58	for (i=iforce->xmit.buf[iforce->xmit.tail]; i >= 0; --i) {
59		serio_write(iforce->serio, iforce->xmit.buf[iforce->xmit.tail]);
60		cs ^= iforce->xmit.buf[iforce->xmit.tail];
61		XMIT_INC(iforce->xmit.tail, 1);
62	}
63
64	serio_write(iforce->serio, cs);
65
66	if (test_and_clear_bit(IFORCE_XMIT_AGAIN, iforce->xmit_flags))
67		goto again;
68
69	clear_bit(IFORCE_XMIT_RUNNING, iforce->xmit_flags);
70
71	spin_unlock_irqrestore(&iforce->xmit_lock, flags);
72}
73
74static void iforce_serio_write_wakeup(struct serio *serio)
75{
76	struct iforce *iforce = serio_get_drvdata(serio);
77
78	iforce_serial_xmit(iforce);
79}
80
81static irqreturn_t iforce_serio_irq(struct serio *serio,
82		unsigned char data, unsigned int flags)
83{
84	struct iforce *iforce = serio_get_drvdata(serio);
85
86	if (!iforce->pkt) {
87		if (data == 0x2b)
88			iforce->pkt = 1;
89		goto out;
90	}
91
92	if (!iforce->id) {
93		if (data > 3 && data != 0xff)
94			iforce->pkt = 0;
95		else
96			iforce->id = data;
97		goto out;
98	}
99
100	if (!iforce->len) {
101		if (data > IFORCE_MAX_LENGTH) {
102			iforce->pkt = 0;
103			iforce->id = 0;
104		} else {
105			iforce->len = data;
106		}
107		goto out;
108	}
109
110	if (iforce->idx < iforce->len) {
111		iforce->csum += iforce->data[iforce->idx++] = data;
112		goto out;
113	}
114
115	if (iforce->idx == iforce->len) {
116		iforce_process_packet(iforce, (iforce->id << 8) | iforce->idx, iforce->data);
117		iforce->pkt = 0;
118		iforce->id  = 0;
119		iforce->len = 0;
120		iforce->idx = 0;
121		iforce->csum = 0;
122	}
123out:
124	return IRQ_HANDLED;
125}
126
127static int iforce_serio_connect(struct serio *serio, struct serio_driver *drv)
128{
129	struct iforce *iforce;
130	int err;
131
132	iforce = kzalloc(sizeof(struct iforce), GFP_KERNEL);
133	if (!iforce)
134		return -ENOMEM;
135
136	iforce->bus = IFORCE_232;
137	iforce->serio = serio;
138
139	serio_set_drvdata(serio, iforce);
140
141	err = serio_open(serio, drv);
142	if (err)
143		goto fail1;
144
145	err = iforce_init_device(iforce);
146	if (err)
147		goto fail2;
148
149	return 0;
150
151 fail2:	serio_close(serio);
152 fail1:	serio_set_drvdata(serio, NULL);
153	kfree(iforce);
154	return err;
155}
156
157static void iforce_serio_disconnect(struct serio *serio)
158{
159	struct iforce *iforce = serio_get_drvdata(serio);
160
161	input_unregister_device(iforce->dev);
162	serio_close(serio);
163	serio_set_drvdata(serio, NULL);
164	kfree(iforce);
165}
166
167static struct serio_device_id iforce_serio_ids[] = {
168	{
169		.type	= SERIO_RS232,
170		.proto	= SERIO_IFORCE,
171		.id	= SERIO_ANY,
172		.extra	= SERIO_ANY,
173	},
174	{ 0 }
175};
176
177MODULE_DEVICE_TABLE(serio, iforce_serio_ids);
178
179struct serio_driver iforce_serio_drv = {
180	.driver		= {
181		.name	= "iforce",
182	},
183	.description	= "RS232 I-Force joysticks and wheels driver",
184	.id_table	= iforce_serio_ids,
185	.write_wakeup	= iforce_serio_write_wakeup,
186	.interrupt	= iforce_serio_irq,
187	.connect	= iforce_serio_connect,
188	.disconnect	= iforce_serio_disconnect,
189};
190