vserver 1.9.3
[linux-2.6.git] / drivers / input / joystick / magellan.c
1 /*
2  * $Id: magellan.c,v 1.16 2002/01/22 20:28:39 vojtech Exp $
3  *
4  *  Copyright (c) 1999-2001 Vojtech Pavlik
5  */
6
7 /*
8  * Magellan and Space Mouse 6dof controller driver for Linux
9  */
10
11 /*
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25  *
26  *  Should you need to contact me, the author, you can do so either by
27  * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail:
28  * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic
29  */
30
31 #include <linux/kernel.h>
32 #include <linux/module.h>
33 #include <linux/slab.h>
34 #include <linux/input.h>
35 #include <linux/serio.h>
36 #include <linux/init.h>
37
38 #define DRIVER_DESC     "Magellan and SpaceMouse 6dof controller driver"
39
40 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
41 MODULE_DESCRIPTION(DRIVER_DESC);
42 MODULE_LICENSE("GPL");
43
44 /*
45  * Definitions & global arrays.
46  */
47
48 #define MAGELLAN_MAX_LENGTH     32
49
50 static int magellan_buttons[] = { BTN_0, BTN_1, BTN_2, BTN_3, BTN_4, BTN_5, BTN_6, BTN_7, BTN_8 };
51 static int magellan_axes[] = { ABS_X, ABS_Y, ABS_Z, ABS_RX, ABS_RY, ABS_RZ };
52 static char *magellan_name = "LogiCad3D Magellan / SpaceMouse";
53
54 /*
55  * Per-Magellan data.
56  */
57
58 struct magellan {
59         struct input_dev dev;
60         int idx;
61         unsigned char data[MAGELLAN_MAX_LENGTH];
62         char phys[32];
63 };
64
65 /*
66  * magellan_crunch_nibbles() verifies that the bytes sent from the Magellan
67  * have correct upper nibbles for the lower ones, if not, the packet will
68  * be thrown away. It also strips these upper halves to simplify further
69  * processing.
70  */
71
72 static int magellan_crunch_nibbles(unsigned char *data, int count)
73 {
74         static unsigned char nibbles[16] = "0AB3D56GH9:K<MN?";
75
76         do {
77                 if (data[count] == nibbles[data[count] & 0xf])
78                         data[count] = data[count] & 0xf;
79                 else
80                         return -1;
81         } while (--count);
82
83         return 0;
84 }
85
86 static void magellan_process_packet(struct magellan* magellan, struct pt_regs *regs)
87 {
88         struct input_dev *dev = &magellan->dev;
89         unsigned char *data = magellan->data;
90         int i, t;
91
92         if (!magellan->idx) return;
93
94         input_regs(dev, regs);
95
96         switch (magellan->data[0]) {
97
98                 case 'd':                               /* Axis data */
99                         if (magellan->idx != 25) return;
100                         if (magellan_crunch_nibbles(data, 24)) return;
101                         for (i = 0; i < 6; i++)
102                                 input_report_abs(dev, magellan_axes[i],
103                                         (data[(i << 2) + 1] << 12 | data[(i << 2) + 2] << 8 |
104                                          data[(i << 2) + 3] <<  4 | data[(i << 2) + 4]) - 32768);
105                         break;
106
107                 case 'k':                               /* Button data */
108                         if (magellan->idx != 4) return;
109                         if (magellan_crunch_nibbles(data, 3)) return;
110                         t = (data[1] << 1) | (data[2] << 5) | data[3];
111                         for (i = 0; i < 9; i++) input_report_key(dev, magellan_buttons[i], (t >> i) & 1);
112                         break;
113         }
114
115         input_sync(dev);
116 }
117
118 static irqreturn_t magellan_interrupt(struct serio *serio,
119                 unsigned char data, unsigned int flags, struct pt_regs *regs)
120 {
121         struct magellan* magellan = serio->private;
122
123         if (data == '\r') {
124                 magellan_process_packet(magellan, regs);
125                 magellan->idx = 0;
126         } else {
127                 if (magellan->idx < MAGELLAN_MAX_LENGTH)
128                         magellan->data[magellan->idx++] = data;
129         }
130         return IRQ_HANDLED;
131 }
132
133 /*
134  * magellan_disconnect() is the opposite of magellan_connect()
135  */
136
137 static void magellan_disconnect(struct serio *serio)
138 {
139         struct magellan* magellan = serio->private;
140         input_unregister_device(&magellan->dev);
141         serio_close(serio);
142         kfree(magellan);
143 }
144
145 /*
146  * magellan_connect() is the routine that is called when someone adds a
147  * new serio device. It looks for the Magellan, and if found, registers
148  * it as an input device.
149  */
150
151 static void magellan_connect(struct serio *serio, struct serio_driver *drv)
152 {
153         struct magellan *magellan;
154         int i, t;
155
156         if (serio->type != (SERIO_RS232 | SERIO_MAGELLAN))
157                 return;
158
159         if (!(magellan = kmalloc(sizeof(struct magellan), GFP_KERNEL)))
160                 return;
161
162         memset(magellan, 0, sizeof(struct magellan));
163
164         magellan->dev.evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
165
166         for (i = 0; i < 9; i++)
167                 set_bit(magellan_buttons[i], magellan->dev.keybit);
168
169         for (i = 0; i < 6; i++) {
170                 t = magellan_axes[i];
171                 set_bit(t, magellan->dev.absbit);
172                 magellan->dev.absmin[t] = -360;
173                 magellan->dev.absmax[t] =  360;
174         }
175
176         sprintf(magellan->phys, "%s/input0", serio->phys);
177
178         init_input_dev(&magellan->dev);
179         magellan->dev.private = magellan;
180         magellan->dev.name = magellan_name;
181         magellan->dev.phys = magellan->phys;
182         magellan->dev.id.bustype = BUS_RS232;
183         magellan->dev.id.vendor = SERIO_MAGELLAN;
184         magellan->dev.id.product = 0x0001;
185         magellan->dev.id.version = 0x0100;
186
187         serio->private = magellan;
188
189         if (serio_open(serio, drv)) {
190                 kfree(magellan);
191                 return;
192         }
193
194         input_register_device(&magellan->dev);
195
196         printk(KERN_INFO "input: %s on %s\n", magellan_name, serio->phys);
197
198 }
199
200 /*
201  * The serio device structure.
202  */
203
204 static struct serio_driver magellan_drv = {
205         .driver         = {
206                 .name   = "magellan",
207         },
208         .description    = DRIVER_DESC,
209         .interrupt      = magellan_interrupt,
210         .connect        = magellan_connect,
211         .disconnect     = magellan_disconnect,
212 };
213
214 /*
215  * The functions for inserting/removing us as a module.
216  */
217
218 int __init magellan_init(void)
219 {
220         serio_register_driver(&magellan_drv);
221         return 0;
222 }
223
224 void __exit magellan_exit(void)
225 {
226         serio_unregister_driver(&magellan_drv);
227 }
228
229 module_init(magellan_init);
230 module_exit(magellan_exit);