vserver 1.9.3
[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 #define DRIVER_DESC     "SpaceTec SpaceOrb 360 and Avenger 6dof controller driver"
42
43 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
44 MODULE_DESCRIPTION(DRIVER_DESC);
45 MODULE_LICENSE("GPL");
46
47 /*
48  * Constants.
49  */
50
51 #define SPACEORB_MAX_LENGTH     64
52
53 static int spaceorb_buttons[] = { BTN_TL, BTN_TR, BTN_Y, BTN_X, BTN_B, BTN_A };
54 static int spaceorb_axes[] = { ABS_X, ABS_Y, ABS_Z, ABS_RX, ABS_RY, ABS_RZ };
55 static char *spaceorb_name = "SpaceTec SpaceOrb 360 / Avenger";
56
57 /*
58  * Per-Orb data.
59  */
60
61 struct spaceorb {
62         struct input_dev dev;
63         struct serio *serio;
64         int idx;
65         unsigned char data[SPACEORB_MAX_LENGTH];
66         char phys[32];
67 };
68
69 static unsigned char spaceorb_xor[] = "SpaceWare";
70
71 static unsigned char *spaceorb_errors[] = { "EEPROM storing 0 failed", "Receive queue overflow", "Transmit queue timeout",
72                 "Bad packet", "Power brown-out", "EEPROM checksum error", "Hardware fault" };
73
74 /*
75  * spaceorb_process_packet() decodes packets the driver receives from the
76  * SpaceOrb.
77  */
78
79 static void spaceorb_process_packet(struct spaceorb *spaceorb, struct pt_regs *regs)
80 {
81         struct input_dev *dev = &spaceorb->dev;
82         unsigned char *data = spaceorb->data;
83         unsigned char c = 0;
84         int axes[6];
85         int i;
86
87         if (spaceorb->idx < 2) return;
88         for (i = 0; i < spaceorb->idx; i++) c ^= data[i];
89         if (c) return;
90
91         input_regs(dev, regs);
92
93         switch (data[0]) {
94
95                 case 'R':                               /* Reset packet */
96                         spaceorb->data[spaceorb->idx - 1] = 0;
97                         for (i = 1; i < spaceorb->idx && spaceorb->data[i] == ' '; i++);
98                         printk(KERN_INFO "input: %s [%s] on %s\n",
99                                  spaceorb_name, spaceorb->data + i, spaceorb->serio->phys);
100                         break;
101
102                 case 'D':                               /* Ball + button data */
103                         if (spaceorb->idx != 12) return;
104                         for (i = 0; i < 9; i++) spaceorb->data[i+2] ^= spaceorb_xor[i];
105                         axes[0] = ( data[2]      << 3) | (data[ 3] >> 4);
106                         axes[1] = ((data[3] & 0x0f) << 6) | (data[ 4] >> 1);
107                         axes[2] = ((data[4] & 0x01) << 9) | (data[ 5] << 2) | (data[4] >> 5);
108                         axes[3] = ((data[6] & 0x1f) << 5) | (data[ 7] >> 2);
109                         axes[4] = ((data[7] & 0x03) << 8) | (data[ 8] << 1) | (data[7] >> 6);
110                         axes[5] = ((data[9] & 0x3f) << 4) | (data[10] >> 3);
111                         for (i = 0; i < 6; i++)
112                                 input_report_abs(dev, spaceorb_axes[i], axes[i] - ((axes[i] & 0x200) ? 1024 : 0));
113                         for (i = 0; i < 6; i++)
114                                 input_report_key(dev, spaceorb_buttons[i], (data[1] >> i) & 1);
115                         break;
116
117                 case 'K':                               /* Button data */
118                         if (spaceorb->idx != 5) return;
119                         for (i = 0; i < 7; i++)
120                                 input_report_key(dev, spaceorb_buttons[i], (data[2] >> i) & 1);
121
122                         break;
123
124                 case 'E':                               /* Error packet */
125                         if (spaceorb->idx != 4) return;
126                         printk(KERN_ERR "joy-spaceorb: Device error. [ ");
127                         for (i = 0; i < 7; i++) if (data[1] & (1 << i)) printk("%s ", spaceorb_errors[i]);
128                         printk("]\n");
129                         break;
130         }
131
132         input_sync(dev);
133 }
134
135 static irqreturn_t spaceorb_interrupt(struct serio *serio,
136                 unsigned char data, unsigned int flags, struct pt_regs *regs)
137 {
138         struct spaceorb* spaceorb = serio->private;
139
140         if (~data & 0x80) {
141                 if (spaceorb->idx) spaceorb_process_packet(spaceorb, regs);
142                 spaceorb->idx = 0;
143         }
144         if (spaceorb->idx < SPACEORB_MAX_LENGTH)
145                 spaceorb->data[spaceorb->idx++] = data & 0x7f;
146         return IRQ_HANDLED;
147 }
148
149 /*
150  * spaceorb_disconnect() is the opposite of spaceorb_connect()
151  */
152
153 static void spaceorb_disconnect(struct serio *serio)
154 {
155         struct spaceorb* spaceorb = serio->private;
156         input_unregister_device(&spaceorb->dev);
157         serio_close(serio);
158         kfree(spaceorb);
159 }
160
161 /*
162  * spaceorb_connect() is the routine that is called when someone adds a
163  * new serio device. It looks for the SpaceOrb/Avenger, and if found, registers
164  * it as an input device.
165  */
166
167 static void spaceorb_connect(struct serio *serio, struct serio_driver *drv)
168 {
169         struct spaceorb *spaceorb;
170         int i, t;
171
172         if (serio->type != (SERIO_RS232 | SERIO_SPACEORB))
173                 return;
174
175         if (!(spaceorb = kmalloc(sizeof(struct spaceorb), GFP_KERNEL)))
176                 return;
177         memset(spaceorb, 0, sizeof(struct spaceorb));
178
179         spaceorb->dev.evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
180
181         for (i = 0; i < 6; i++)
182                 set_bit(spaceorb_buttons[i], spaceorb->dev.keybit);
183
184         for (i = 0; i < 6; i++) {
185                 t = spaceorb_axes[i];
186                 set_bit(t, spaceorb->dev.absbit);
187                 spaceorb->dev.absmin[t] = -508;
188                 spaceorb->dev.absmax[t] =  508;
189         }
190
191         spaceorb->serio = serio;
192         spaceorb->dev.private = spaceorb;
193
194         sprintf(spaceorb->phys, "%s/input0", serio->phys);
195
196         init_input_dev(&spaceorb->dev);
197         spaceorb->dev.name = spaceorb_name;
198         spaceorb->dev.phys = spaceorb->phys;
199         spaceorb->dev.id.bustype = BUS_RS232;
200         spaceorb->dev.id.vendor = SERIO_SPACEORB;
201         spaceorb->dev.id.product = 0x0001;
202         spaceorb->dev.id.version = 0x0100;
203
204         serio->private = spaceorb;
205
206         if (serio_open(serio, drv)) {
207                 kfree(spaceorb);
208                 return;
209         }
210
211         input_register_device(&spaceorb->dev);
212 }
213
214 /*
215  * The serio device structure.
216  */
217
218 static struct serio_driver spaceorb_drv = {
219         .driver         = {
220                 .name   = "spaceorb",
221         },
222         .description    = DRIVER_DESC,
223         .interrupt      = spaceorb_interrupt,
224         .connect        = spaceorb_connect,
225         .disconnect     = spaceorb_disconnect,
226 };
227
228 /*
229  * The functions for inserting/removing us as a module.
230  */
231
232 int __init spaceorb_init(void)
233 {
234         serio_register_driver(&spaceorb_drv);
235         return 0;
236 }
237
238 void __exit spaceorb_exit(void)
239 {
240         serio_unregister_driver(&spaceorb_drv);
241 }
242
243 module_init(spaceorb_init);
244 module_exit(spaceorb_exit);