2 * Copyright (c) 1999-2001 Vojtech Pavlik
6 * Magellan and Space Mouse 6dof controller driver for Linux
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 * Should you need to contact me, the author, you can do so either by
25 * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail:
26 * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic
29 #include <linux/kernel.h>
30 #include <linux/module.h>
31 #include <linux/slab.h>
32 #include <linux/input.h>
33 #include <linux/serio.h>
34 #include <linux/init.h>
36 #define DRIVER_DESC "Magellan and SpaceMouse 6dof controller driver"
38 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
39 MODULE_DESCRIPTION(DRIVER_DESC);
40 MODULE_LICENSE("GPL");
43 * Definitions & global arrays.
46 #define MAGELLAN_MAX_LENGTH 32
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 };
56 struct input_dev *dev;
58 unsigned char data[MAGELLAN_MAX_LENGTH];
63 * magellan_crunch_nibbles() verifies that the bytes sent from the Magellan
64 * have correct upper nibbles for the lower ones, if not, the packet will
65 * be thrown away. It also strips these upper halves to simplify further
69 static int magellan_crunch_nibbles(unsigned char *data, int count)
71 static unsigned char nibbles[16] = "0AB3D56GH9:K<MN?";
74 if (data[count] == nibbles[data[count] & 0xf])
75 data[count] = data[count] & 0xf;
83 static void magellan_process_packet(struct magellan* magellan)
85 struct input_dev *dev = magellan->dev;
86 unsigned char *data = magellan->data;
89 if (!magellan->idx) return;
91 switch (magellan->data[0]) {
93 case 'd': /* Axis data */
94 if (magellan->idx != 25) return;
95 if (magellan_crunch_nibbles(data, 24)) return;
96 for (i = 0; i < 6; i++)
97 input_report_abs(dev, magellan_axes[i],
98 (data[(i << 2) + 1] << 12 | data[(i << 2) + 2] << 8 |
99 data[(i << 2) + 3] << 4 | data[(i << 2) + 4]) - 32768);
102 case 'k': /* Button data */
103 if (magellan->idx != 4) return;
104 if (magellan_crunch_nibbles(data, 3)) return;
105 t = (data[1] << 1) | (data[2] << 5) | data[3];
106 for (i = 0; i < 9; i++) input_report_key(dev, magellan_buttons[i], (t >> i) & 1);
113 static irqreturn_t magellan_interrupt(struct serio *serio,
114 unsigned char data, unsigned int flags)
116 struct magellan* magellan = serio_get_drvdata(serio);
119 magellan_process_packet(magellan);
122 if (magellan->idx < MAGELLAN_MAX_LENGTH)
123 magellan->data[magellan->idx++] = data;
129 * magellan_disconnect() is the opposite of magellan_connect()
132 static void magellan_disconnect(struct serio *serio)
134 struct magellan* magellan = serio_get_drvdata(serio);
137 serio_set_drvdata(serio, NULL);
138 input_unregister_device(magellan->dev);
143 * magellan_connect() is the routine that is called when someone adds a
144 * new serio device that supports Magellan protocol and registers it as
148 static int magellan_connect(struct serio *serio, struct serio_driver *drv)
150 struct magellan *magellan;
151 struct input_dev *input_dev;
155 magellan = kzalloc(sizeof(struct magellan), GFP_KERNEL);
156 input_dev = input_allocate_device();
157 if (!magellan || !input_dev)
160 magellan->dev = input_dev;
161 snprintf(magellan->phys, sizeof(magellan->phys), "%s/input0", serio->phys);
163 input_dev->name = "LogiCad3D Magellan / SpaceMouse";
164 input_dev->phys = magellan->phys;
165 input_dev->id.bustype = BUS_RS232;
166 input_dev->id.vendor = SERIO_MAGELLAN;
167 input_dev->id.product = 0x0001;
168 input_dev->id.version = 0x0100;
169 input_dev->dev.parent = &serio->dev;
171 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
173 for (i = 0; i < 9; i++)
174 set_bit(magellan_buttons[i], input_dev->keybit);
176 for (i = 0; i < 6; i++)
177 input_set_abs_params(input_dev, magellan_axes[i], -360, 360, 0, 0);
179 serio_set_drvdata(serio, magellan);
181 err = serio_open(serio, drv);
185 err = input_register_device(magellan->dev);
191 fail3: serio_close(serio);
192 fail2: serio_set_drvdata(serio, NULL);
193 fail1: input_free_device(input_dev);
199 * The serio driver structure.
202 static struct serio_device_id magellan_serio_ids[] = {
205 .proto = SERIO_MAGELLAN,
212 MODULE_DEVICE_TABLE(serio, magellan_serio_ids);
214 static struct serio_driver magellan_drv = {
218 .description = DRIVER_DESC,
219 .id_table = magellan_serio_ids,
220 .interrupt = magellan_interrupt,
221 .connect = magellan_connect,
222 .disconnect = magellan_disconnect,
226 * The functions for inserting/removing us as a module.
229 static int __init magellan_init(void)
231 return serio_register_driver(&magellan_drv);
234 static void __exit magellan_exit(void)
236 serio_unregister_driver(&magellan_drv);
239 module_init(magellan_init);
240 module_exit(magellan_exit);