patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / drivers / input / joystick / spaceorb.c
1 /*
2  * $Id: spaceorb.c,v 1.15 2002/01/22 20:29:19 vojtech Exp $
3  *
4  *  Copyright (c) 1999-2001 Vojtech Pavlik
5  *
6  *  Based on the work of:
7  *      David Thompson
8  */
9
10 /*
11  * SpaceTec SpaceOrb 360 and Avenger 6dof controller driver for Linux
12  */
13
14 /*
15  * This program is free software; you can redistribute it and/or modify
16  * it under the terms of the GNU General Public License as published by
17  * the Free Software Foundation; either version 2 of the License, or
18  * (at your option) any later version.
19  *
20  * This program is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23  * GNU General Public License for more details.
24  *
25  * You should have received a copy of the GNU General Public License
26  * along with this program; if not, write to the Free Software
27  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28  *
29  *  Should you need to contact me, the author, you can do so either by
30  * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail:
31  * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic
32  */
33
34 #include <linux/kernel.h>
35 #include <linux/slab.h>
36 #include <linux/module.h>
37 #include <linux/init.h>
38 #include <linux/input.h>
39 #include <linux/serio.h>
40
41 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
42 MODULE_DESCRIPTION("SpaceTec SpaceOrb 360 and Avenger 6dof controller driver");
43 MODULE_LICENSE("GPL");
44
45 /*
46  * Constants.
47  */
48
49 #define SPACEORB_MAX_LENGTH     64
50
51 static int spaceorb_buttons[] = { BTN_TL, BTN_TR, BTN_Y, BTN_X, BTN_B, BTN_A };
52 static int spaceorb_axes[] = { ABS_X, ABS_Y, ABS_Z, ABS_RX, ABS_RY, ABS_RZ };
53 static char *spaceorb_name = "SpaceTec SpaceOrb 360 / Avenger";
54
55 /*
56  * Per-Orb data.
57  */
58
59 struct spaceorb {
60         struct input_dev dev;
61         struct serio *serio;
62         int idx;
63         unsigned char data[SPACEORB_MAX_LENGTH];
64         char phys[32];
65 };
66
67 static unsigned char spaceorb_xor[] = "SpaceWare";
68
69 static unsigned char *spaceorb_errors[] = { "EEPROM storing 0 failed", "Receive queue overflow", "Transmit queue timeout",
70                 "Bad packet", "Power brown-out", "EEPROM checksum error", "Hardware fault" };
71
72 /*
73  * spaceorb_process_packet() decodes packets the driver receives from the
74  * SpaceOrb.
75  */
76
77 static void spaceorb_process_packet(struct spaceorb *spaceorb, struct pt_regs *regs)
78 {
79         struct input_dev *dev = &spaceorb->dev;
80         unsigned char *data = spaceorb->data;
81         unsigned char c = 0;
82         int axes[6];
83         int i;
84
85         if (spaceorb->idx < 2) return;
86         for (i = 0; i < spaceorb->idx; i++) c ^= data[i];
87         if (c) return;
88
89         input_regs(dev, regs);
90
91         switch (data[0]) {
92
93                 case 'R':                               /* Reset packet */
94                         spaceorb->data[spaceorb->idx - 1] = 0;
95                         for (i = 1; i < spaceorb->idx && spaceorb->data[i] == ' '; i++);
96                         printk(KERN_INFO "input: %s [%s] on %s\n",
97                                  spaceorb_name, spaceorb->data + i, spaceorb->serio->phys);
98                         break;
99
100                 case 'D':                               /* Ball + button data */
101                         if (spaceorb->idx != 12) return;
102                         for (i = 0; i < 9; i++) spaceorb->data[i+2] ^= spaceorb_xor[i];
103                         axes[0] = ( data[2]      << 3) | (data[ 3] >> 4);
104                         axes[1] = ((data[3] & 0x0f) << 6) | (data[ 4] >> 1);
105                         axes[2] = ((data[4] & 0x01) << 9) | (data[ 5] << 2) | (data[4] >> 5);
106                         axes[3] = ((data[6] & 0x1f) << 5) | (data[ 7] >> 2);
107                         axes[4] = ((data[7] & 0x03) << 8) | (data[ 8] << 1) | (data[7] >> 6);
108                         axes[5] = ((data[9] & 0x3f) << 4) | (data[10] >> 3);
109                         for (i = 0; i < 6; i++)
110                                 input_report_abs(dev, spaceorb_axes[i], axes[i] - ((axes[i] & 0x200) ? 1024 : 0));
111                         for (i = 0; i < 6; i++)
112                                 input_report_key(dev, spaceorb_buttons[i], (data[1] >> i) & 1);
113                         break;
114
115                 case 'K':                               /* Button data */
116                         if (spaceorb->idx != 5) return;
117                         for (i = 0; i < 7; i++)
118                                 input_report_key(dev, spaceorb_buttons[i], (data[2] >> i) & 1);
119
120                         break;
121
122                 case 'E':                               /* Error packet */
123                         if (spaceorb->idx != 4) return;
124                         printk(KERN_ERR "joy-spaceorb: Device error. [ ");
125                         for (i = 0; i < 7; i++) if (data[1] & (1 << i)) printk("%s ", spaceorb_errors[i]);
126                         printk("]\n");
127                         break;
128         }
129
130         input_sync(dev);
131 }
132
133 static irqreturn_t spaceorb_interrupt(struct serio *serio,
134                 unsigned char data, unsigned int flags, struct pt_regs *regs)
135 {
136         struct spaceorb* spaceorb = serio->private;
137
138         if (~data & 0x80) {
139                 if (spaceorb->idx) spaceorb_process_packet(spaceorb, regs);
140                 spaceorb->idx = 0;
141         }
142         if (spaceorb->idx < SPACEORB_MAX_LENGTH)
143                 spaceorb->data[spaceorb->idx++] = data & 0x7f;
144         return IRQ_HANDLED;
145 }
146
147 /*
148  * spaceorb_disconnect() is the opposite of spaceorb_connect()
149  */
150
151 static void spaceorb_disconnect(struct serio *serio)
152 {
153         struct spaceorb* spaceorb = serio->private;
154         input_unregister_device(&spaceorb->dev);
155         serio_close(serio);
156         kfree(spaceorb);
157 }
158
159 /*
160  * spaceorb_connect() is the routine that is called when someone adds a
161  * new serio device. It looks for the SpaceOrb/Avenger, and if found, registers
162  * it as an input device.
163  */
164
165 static void spaceorb_connect(struct serio *serio, struct serio_dev *dev)
166 {
167         struct spaceorb *spaceorb;
168         int i, t;
169
170         if (serio->type != (SERIO_RS232 | SERIO_SPACEORB))
171                 return;
172
173         if (!(spaceorb = kmalloc(sizeof(struct spaceorb), GFP_KERNEL)))
174                 return;
175         memset(spaceorb, 0, sizeof(struct spaceorb));
176
177         spaceorb->dev.evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
178
179         for (i = 0; i < 6; i++)
180                 set_bit(spaceorb_buttons[i], spaceorb->dev.keybit);
181
182         for (i = 0; i < 6; i++) {
183                 t = spaceorb_axes[i];
184                 set_bit(t, spaceorb->dev.absbit);
185                 spaceorb->dev.absmin[t] = -508;
186                 spaceorb->dev.absmax[t] =  508;
187         }
188
189         spaceorb->serio = serio;
190         spaceorb->dev.private = spaceorb;
191
192         sprintf(spaceorb->phys, "%s/input0", serio->phys);
193
194         init_input_dev(&spaceorb->dev);
195         spaceorb->dev.name = spaceorb_name;
196         spaceorb->dev.phys = spaceorb->phys;
197         spaceorb->dev.id.bustype = BUS_RS232;
198         spaceorb->dev.id.vendor = SERIO_SPACEORB;
199         spaceorb->dev.id.product = 0x0001;
200         spaceorb->dev.id.version = 0x0100;
201
202         serio->private = spaceorb;
203
204         if (serio_open(serio, dev)) {
205                 kfree(spaceorb);
206                 return;
207         }
208
209         input_register_device(&spaceorb->dev);
210 }
211
212 /*
213  * The serio device structure.
214  */
215
216 static struct serio_dev spaceorb_dev = {
217         .interrupt =    spaceorb_interrupt,
218         .connect =      spaceorb_connect,
219         .disconnect =   spaceorb_disconnect,
220 };
221
222 /*
223  * The functions for inserting/removing us as a module.
224  */
225
226 int __init spaceorb_init(void)
227 {
228         serio_register_device(&spaceorb_dev);
229         return 0;
230 }
231
232 void __exit spaceorb_exit(void)
233 {
234         serio_unregister_device(&spaceorb_dev);
235 }
236
237 module_init(spaceorb_init);
238 module_exit(spaceorb_exit);