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