b5dea273f98e39a9398b8c554c2fbdfad3f512a6
[sfrench/cifs-2.6.git] / drivers / input / joystick / iforce / iforce-serio.c
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 static void iforce_serio_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 int iforce_serio_get_id(struct iforce *iforce, u8 *packet)
71 {
72         iforce->expect_packet = FF_CMD_QUERY;
73         iforce_send_packet(iforce, FF_CMD_QUERY, packet);
74
75         wait_event_interruptible_timeout(iforce->wait,
76                                          !iforce->expect_packet, HZ);
77
78         if (iforce->expect_packet) {
79                 iforce->expect_packet = 0;
80                 return -EIO;
81         }
82
83         return -(iforce->edata[0] != packet[0]);
84 }
85
86 static int iforce_serio_start_io(struct iforce *iforce)
87 {
88         /* No special handling required */
89         return 0;
90 }
91
92 static void iforce_serio_stop_io(struct iforce *iforce)
93 {
94         //TODO: Wait for the last packets to be sent
95 }
96
97 static const struct iforce_xport_ops iforce_serio_xport_ops = {
98         .xmit           = iforce_serio_xmit,
99         .get_id         = iforce_serio_get_id,
100         .start_io       = iforce_serio_start_io,
101         .stop_io        = iforce_serio_stop_io,
102 };
103
104 static void iforce_serio_write_wakeup(struct serio *serio)
105 {
106         struct iforce *iforce = serio_get_drvdata(serio);
107
108         iforce_serio_xmit(iforce);
109 }
110
111 static irqreturn_t iforce_serio_irq(struct serio *serio,
112                 unsigned char data, unsigned int flags)
113 {
114         struct iforce *iforce = serio_get_drvdata(serio);
115
116         if (!iforce->pkt) {
117                 if (data == 0x2b)
118                         iforce->pkt = 1;
119                 goto out;
120         }
121
122         if (!iforce->id) {
123                 if (data > 3 && data != 0xff)
124                         iforce->pkt = 0;
125                 else
126                         iforce->id = data;
127                 goto out;
128         }
129
130         if (!iforce->len) {
131                 if (data > IFORCE_MAX_LENGTH) {
132                         iforce->pkt = 0;
133                         iforce->id = 0;
134                 } else {
135                         iforce->len = data;
136                 }
137                 goto out;
138         }
139
140         if (iforce->idx < iforce->len) {
141                 iforce->csum += iforce->data[iforce->idx++] = data;
142                 goto out;
143         }
144
145         if (iforce->idx == iforce->len) {
146                 u16 cmd = (iforce->id << 8) | iforce->idx;
147
148                 /* Handle command completion */
149                 if (HI(iforce->expect_packet) == HI(cmd)) {
150                         iforce->expect_packet = 0;
151                         iforce->ecmd = cmd;
152                         memcpy(iforce->edata, iforce->data, IFORCE_MAX_LENGTH);
153                 }
154
155                 iforce_process_packet(iforce, cmd, iforce->data);
156
157                 iforce->pkt = 0;
158                 iforce->id  = 0;
159                 iforce->len = 0;
160                 iforce->idx = 0;
161                 iforce->csum = 0;
162         }
163 out:
164         return IRQ_HANDLED;
165 }
166
167 static int iforce_serio_connect(struct serio *serio, struct serio_driver *drv)
168 {
169         struct iforce *iforce;
170         int err;
171
172         iforce = kzalloc(sizeof(struct iforce), GFP_KERNEL);
173         if (!iforce)
174                 return -ENOMEM;
175
176         iforce->xport_ops = &iforce_serio_xport_ops;
177         iforce->bus = IFORCE_232;
178         iforce->serio = serio;
179
180         serio_set_drvdata(serio, iforce);
181
182         err = serio_open(serio, drv);
183         if (err)
184                 goto fail1;
185
186         err = iforce_init_device(&serio->dev, BUS_RS232, iforce);
187         if (err)
188                 goto fail2;
189
190         return 0;
191
192  fail2: serio_close(serio);
193  fail1: serio_set_drvdata(serio, NULL);
194         kfree(iforce);
195         return err;
196 }
197
198 static void iforce_serio_disconnect(struct serio *serio)
199 {
200         struct iforce *iforce = serio_get_drvdata(serio);
201
202         input_unregister_device(iforce->dev);
203         serio_close(serio);
204         serio_set_drvdata(serio, NULL);
205         kfree(iforce);
206 }
207
208 static const struct serio_device_id iforce_serio_ids[] = {
209         {
210                 .type   = SERIO_RS232,
211                 .proto  = SERIO_IFORCE,
212                 .id     = SERIO_ANY,
213                 .extra  = SERIO_ANY,
214         },
215         { 0 }
216 };
217
218 MODULE_DEVICE_TABLE(serio, iforce_serio_ids);
219
220 struct serio_driver iforce_serio_drv = {
221         .driver         = {
222                 .name   = "iforce",
223         },
224         .description    = "RS232 I-Force joysticks and wheels driver",
225         .id_table       = iforce_serio_ids,
226         .write_wakeup   = iforce_serio_write_wakeup,
227         .interrupt      = iforce_serio_irq,
228         .connect        = iforce_serio_connect,
229         .disconnect     = iforce_serio_disconnect,
230 };