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