patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / drivers / input / joystick / iforce / iforce-serio.c
1 /*
2  * $Id: iforce-serio.c,v 1.4 2002/01/28 22:45:00 jdeneux Exp $
3  *
4  *  Copyright (c) 2000-2001 Vojtech Pavlik <vojtech@ucw.cz>
5  *  Copyright (c) 2001 Johann Deneux <deneux@ifrance.com>
6  *
7  *  USB/RS232 I-Force joysticks and wheels.
8  */
9
10 /*
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.
15  *
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.
20  *
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
24  *
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
28  */
29
30 #include "iforce.h"
31
32 void iforce_serial_xmit(struct iforce *iforce)
33 {
34         unsigned char cs;
35         int i;
36         unsigned long flags;
37
38         if (test_and_set_bit(IFORCE_XMIT_RUNNING, iforce->xmit_flags)) {
39                 set_bit(IFORCE_XMIT_AGAIN, iforce->xmit_flags);
40                 return;
41         }
42
43         spin_lock_irqsave(&iforce->xmit_lock, flags);
44
45 again:
46         if (iforce->xmit.head == iforce->xmit.tail) {
47                 clear_bit(IFORCE_XMIT_RUNNING, iforce->xmit_flags);
48                 spin_unlock_irqrestore(&iforce->xmit_lock, flags);
49                 return;
50         }
51
52         cs = 0x2b;
53
54         serio_write(iforce->serio, 0x2b);
55
56         serio_write(iforce->serio, iforce->xmit.buf[iforce->xmit.tail]);
57         cs ^= iforce->xmit.buf[iforce->xmit.tail];
58         XMIT_INC(iforce->xmit.tail, 1);
59
60         for (i=iforce->xmit.buf[iforce->xmit.tail]; i >= 0; --i) {
61                 serio_write(iforce->serio, iforce->xmit.buf[iforce->xmit.tail]);
62                 cs ^= iforce->xmit.buf[iforce->xmit.tail];
63                 XMIT_INC(iforce->xmit.tail, 1);
64         }
65
66         serio_write(iforce->serio, cs);
67
68         if (test_and_clear_bit(IFORCE_XMIT_AGAIN, iforce->xmit_flags))
69                 goto again;
70
71         clear_bit(IFORCE_XMIT_RUNNING, iforce->xmit_flags);
72
73         spin_unlock_irqrestore(&iforce->xmit_lock, flags);
74 }
75
76 static void iforce_serio_write_wakeup(struct serio *serio)
77 {
78         iforce_serial_xmit((struct iforce *)serio->private);
79 }
80
81 static irqreturn_t iforce_serio_irq(struct serio *serio,
82                 unsigned char data, unsigned int flags, struct pt_regs *regs)
83 {
84         struct iforce* iforce = serio->private;
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, regs);
117                 iforce->pkt = 0;
118                 iforce->id  = 0;
119                 iforce->len = 0;
120                 iforce->idx = 0;
121                 iforce->csum = 0;
122         }
123 out:
124         return IRQ_HANDLED;
125 }
126
127 static void iforce_serio_connect(struct serio *serio, struct serio_dev *dev)
128 {
129         struct iforce *iforce;
130         if (serio->type != (SERIO_RS232 | SERIO_IFORCE))
131                 return;
132
133         if (!(iforce = kmalloc(sizeof(struct iforce), GFP_KERNEL))) return;
134         memset(iforce, 0, sizeof(struct iforce));
135
136         iforce->bus = IFORCE_232;
137         iforce->serio = serio;
138         serio->private = iforce;
139
140         if (serio_open(serio, dev)) {
141                 kfree(iforce);
142                 return;
143         }
144
145         if (iforce_init_device(iforce)) {
146                 serio_close(serio);
147                 kfree(iforce);
148                 return;
149         }
150 }
151
152 static void iforce_serio_disconnect(struct serio *serio)
153 {
154         struct iforce* iforce = serio->private;
155
156         input_unregister_device(&iforce->dev);
157         serio_close(serio);
158         kfree(iforce);
159 }
160
161 struct serio_dev iforce_serio_dev = {
162         .write_wakeup = iforce_serio_write_wakeup,
163         .interrupt =    iforce_serio_irq,
164         .connect =      iforce_serio_connect,
165         .disconnect =   iforce_serio_disconnect,
166 };