2 * $Id: magellan.c,v 1.1.1.1 2005/04/11 02:50:21 jack Exp $
4 * Copyright (c) 1999-2000 Vojtech Pavlik
10 * Magellan and Space Mouse 6dof controller driver for Linux
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.
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.
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
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
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>
41 * Definitions & global arrays.
44 #define MAGELLAN_MAX_LENGTH 32
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";
57 unsigned char data[MAGELLAN_MAX_LENGTH];
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
67 static int magellan_crunch_nibbles(unsigned char *data, int count)
69 static unsigned char nibbles[16] = "0AB3D56GH9:K<MN?";
72 if (data[count] == nibbles[data[count] & 0xf])
73 data[count] = data[count] & 0xf;
81 static void magellan_process_packet(struct magellan* magellan)
83 struct input_dev *dev = &magellan->dev;
84 unsigned char *data = magellan->data;
87 if (!magellan->idx) return;
89 switch (magellan->data[0]) {
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);
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);
109 static void magellan_interrupt(struct serio *serio, unsigned char data, unsigned int flags)
111 struct magellan* magellan = serio->private;
114 magellan_process_packet(magellan);
117 if (magellan->idx < MAGELLAN_MAX_LENGTH)
118 magellan->data[magellan->idx++] = data;
123 * magellan_disconnect() is the opposite of magellan_connect()
126 static void magellan_disconnect(struct serio *serio)
128 struct magellan* magellan = serio->private;
129 input_unregister_device(&magellan->dev);
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.
140 static void magellan_connect(struct serio *serio, struct serio_dev *dev)
142 struct magellan *magellan;
145 if (serio->type != (SERIO_RS232 | SERIO_MAGELLAN))
148 if (!(magellan = kmalloc(sizeof(struct magellan), GFP_KERNEL)))
151 memset(magellan, 0, sizeof(struct magellan));
153 magellan->dev.evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
155 for (i = 0; i < 9; i++)
156 set_bit(magellan_buttons[i], &magellan->dev.keybit);
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;
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;
172 serio->private = magellan;
174 if (serio_open(serio, dev)) {
179 input_register_device(&magellan->dev);
181 printk(KERN_INFO "input%d: %s on serio%d\n", magellan->dev.number, magellan_name, serio->number);
185 * The serio device structure.
188 static struct serio_dev magellan_dev = {
189 interrupt: magellan_interrupt,
190 connect: magellan_connect,
191 disconnect: magellan_disconnect,
195 * The functions for inserting/removing us as a module.
198 int __init magellan_init(void)
200 serio_register_device(&magellan_dev);
204 void __exit magellan_exit(void)
206 serio_unregister_device(&magellan_dev);
209 module_init(magellan_init);
210 module_exit(magellan_exit);
212 MODULE_LICENSE("GPL");