patch-2_6_7-vs1_9_1_12
[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 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
39 MODULE_DESCRIPTION("Magellan and SpaceMouse 6dof controller driver");
40 MODULE_LICENSE("GPL");
41
42 /*
43  * Definitions & global arrays.
44  */
45
46 #define MAGELLAN_MAX_LENGTH     32
47
48 static int magellan_buttons[] = { BTN_0, BTN_1, BTN_2, BTN_3, BTN_4, BTN_5, BTN_6, BTN_7, BTN_8 };
49 static int magellan_axes[] = { ABS_X, ABS_Y, ABS_Z, ABS_RX, ABS_RY, ABS_RZ };
50 static char *magellan_name = "LogiCad3D Magellan / SpaceMouse";
51
52 /*
53  * Per-Magellan data.
54  */
55
56 struct magellan {
57         struct input_dev dev;
58         int idx;
59         unsigned char data[MAGELLAN_MAX_LENGTH];
60         char phys[32];
61 };
62
63 /*
64  * magellan_crunch_nibbles() verifies that the bytes sent from the Magellan
65  * have correct upper nibbles for the lower ones, if not, the packet will
66  * be thrown away. It also strips these upper halves to simplify further
67  * processing.
68  */
69
70 static int magellan_crunch_nibbles(unsigned char *data, int count)
71 {
72         static unsigned char nibbles[16] = "0AB3D56GH9:K<MN?";
73
74         do {
75                 if (data[count] == nibbles[data[count] & 0xf])
76                         data[count] = data[count] & 0xf;
77                 else
78                         return -1;
79         } while (--count);
80
81         return 0;
82 }
83
84 static void magellan_process_packet(struct magellan* magellan, struct pt_regs *regs)
85 {
86         struct input_dev *dev = &magellan->dev;
87         unsigned char *data = magellan->data;
88         int i, t;
89
90         if (!magellan->idx) return;
91
92         input_regs(dev, regs);
93
94         switch (magellan->data[0]) {
95
96                 case 'd':                               /* Axis data */
97                         if (magellan->idx != 25) return;
98                         if (magellan_crunch_nibbles(data, 24)) return;
99                         for (i = 0; i < 6; i++)
100                                 input_report_abs(dev, magellan_axes[i],
101                                         (data[(i << 2) + 1] << 12 | data[(i << 2) + 2] << 8 |
102                                          data[(i << 2) + 3] <<  4 | data[(i << 2) + 4]) - 32768);
103                         break;
104
105                 case 'k':                               /* Button data */
106                         if (magellan->idx != 4) return;
107                         if (magellan_crunch_nibbles(data, 3)) return;
108                         t = (data[1] << 1) | (data[2] << 5) | data[3];
109                         for (i = 0; i < 9; i++) input_report_key(dev, magellan_buttons[i], (t >> i) & 1);
110                         break;
111         }
112
113         input_sync(dev);
114 }
115
116 static irqreturn_t magellan_interrupt(struct serio *serio,
117                 unsigned char data, unsigned int flags, struct pt_regs *regs)
118 {
119         struct magellan* magellan = serio->private;
120
121         if (data == '\r') {
122                 magellan_process_packet(magellan, regs);
123                 magellan->idx = 0;
124         } else {
125                 if (magellan->idx < MAGELLAN_MAX_LENGTH)
126                         magellan->data[magellan->idx++] = data;
127         }
128         return IRQ_HANDLED;
129 }
130
131 /*
132  * magellan_disconnect() is the opposite of magellan_connect()
133  */
134
135 static void magellan_disconnect(struct serio *serio)
136 {
137         struct magellan* magellan = serio->private;
138         input_unregister_device(&magellan->dev);
139         serio_close(serio);
140         kfree(magellan);
141 }
142
143 /*
144  * magellan_connect() is the routine that is called when someone adds a
145  * new serio device. It looks for the Magellan, and if found, registers
146  * it as an input device.
147  */
148
149 static void magellan_connect(struct serio *serio, struct serio_dev *dev)
150 {
151         struct magellan *magellan;
152         int i, t;
153
154         if (serio->type != (SERIO_RS232 | SERIO_MAGELLAN))
155                 return;
156
157         if (!(magellan = kmalloc(sizeof(struct magellan), GFP_KERNEL)))
158                 return;
159
160         memset(magellan, 0, sizeof(struct magellan));
161
162         magellan->dev.evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
163
164         for (i = 0; i < 9; i++)
165                 set_bit(magellan_buttons[i], magellan->dev.keybit);
166
167         for (i = 0; i < 6; i++) {
168                 t = magellan_axes[i];
169                 set_bit(t, magellan->dev.absbit);
170                 magellan->dev.absmin[t] = -360;
171                 magellan->dev.absmax[t] =  360;
172         }
173
174         sprintf(magellan->phys, "%s/input0", serio->phys);
175
176         init_input_dev(&magellan->dev);
177         magellan->dev.private = magellan;
178         magellan->dev.name = magellan_name;
179         magellan->dev.phys = magellan->phys;
180         magellan->dev.id.bustype = BUS_RS232;
181         magellan->dev.id.vendor = SERIO_MAGELLAN;
182         magellan->dev.id.product = 0x0001;
183         magellan->dev.id.version = 0x0100;
184
185         serio->private = magellan;
186
187         if (serio_open(serio, dev)) {
188                 kfree(magellan);
189                 return;
190         }
191
192         input_register_device(&magellan->dev);
193
194         printk(KERN_INFO "input: %s on %s\n", magellan_name, serio->phys);
195
196 }
197
198 /*
199  * The serio device structure.
200  */
201
202 static struct serio_dev magellan_dev = {
203         .interrupt =    magellan_interrupt,
204         .connect =      magellan_connect,
205         .disconnect =   magellan_disconnect,
206 };
207
208 /*
209  * The functions for inserting/removing us as a module.
210  */
211
212 int __init magellan_init(void)
213 {
214         serio_register_device(&magellan_dev);
215         return 0;
216 }
217
218 void __exit magellan_exit(void)
219 {
220         serio_unregister_device(&magellan_dev);
221 }
222
223 module_init(magellan_init);
224 module_exit(magellan_exit);