2 * Copyright (c) 1999-2001 Vojtech Pavlik
4 * Based on the work of:
10 * SpaceTec SpaceBall 2003/3003/4000 FLX 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, Simunkova 1594, Prague 8, 182 00 Czech Republic
33 #include <linux/kernel.h>
34 #include <linux/slab.h>
35 #include <linux/module.h>
36 #include <linux/input.h>
37 #include <linux/serio.h>
38 #include <asm/unaligned.h>
40 #define DRIVER_DESC "SpaceTec SpaceBall 2003/3003/4000 FLX driver"
42 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
43 MODULE_DESCRIPTION(DRIVER_DESC);
44 MODULE_LICENSE("GPL");
50 #define SPACEBALL_MAX_LENGTH 128
51 #define SPACEBALL_MAX_ID 9
53 #define SPACEBALL_1003 1
54 #define SPACEBALL_2003B 3
55 #define SPACEBALL_2003C 4
56 #define SPACEBALL_3003C 7
57 #define SPACEBALL_4000FLX 8
58 #define SPACEBALL_4000FLX_L 9
60 static int spaceball_axes[] = { ABS_X, ABS_Z, ABS_Y, ABS_RX, ABS_RZ, ABS_RY };
61 static char *spaceball_names[] = {
62 "?", "SpaceTec SpaceBall 1003", "SpaceTec SpaceBall 2003", "SpaceTec SpaceBall 2003B",
63 "SpaceTec SpaceBall 2003C", "SpaceTec SpaceBall 3003", "SpaceTec SpaceBall SpaceController",
64 "SpaceTec SpaceBall 3003C", "SpaceTec SpaceBall 4000FLX", "SpaceTec SpaceBall 4000FLX Lefty" };
71 struct input_dev *dev;
74 unsigned char data[SPACEBALL_MAX_LENGTH];
79 * spaceball_process_packet() decodes packets the driver receives from the
83 static void spaceball_process_packet(struct spaceball* spaceball)
85 struct input_dev *dev = spaceball->dev;
86 unsigned char *data = spaceball->data;
89 if (spaceball->idx < 2) return;
91 switch (spaceball->data[0]) {
93 case 'D': /* Ball data */
94 if (spaceball->idx != 15) return;
96 * Skip first three bytes; read six axes worth of data.
97 * Axis values are signed 16-bit big-endian.
100 for (i = 0; i < ARRAY_SIZE(spaceball_axes); i++) {
101 input_report_abs(dev, spaceball_axes[i],
102 (__s16)get_unaligned_be16(&data[i * 2]));
106 case 'K': /* Button data */
107 if (spaceball->idx != 3) return;
108 input_report_key(dev, BTN_1, (data[2] & 0x01) || (data[2] & 0x20));
109 input_report_key(dev, BTN_2, data[2] & 0x02);
110 input_report_key(dev, BTN_3, data[2] & 0x04);
111 input_report_key(dev, BTN_4, data[2] & 0x08);
112 input_report_key(dev, BTN_5, data[1] & 0x01);
113 input_report_key(dev, BTN_6, data[1] & 0x02);
114 input_report_key(dev, BTN_7, data[1] & 0x04);
115 input_report_key(dev, BTN_8, data[1] & 0x10);
118 case '.': /* Advanced button data */
119 if (spaceball->idx != 3) return;
120 input_report_key(dev, BTN_1, data[2] & 0x01);
121 input_report_key(dev, BTN_2, data[2] & 0x02);
122 input_report_key(dev, BTN_3, data[2] & 0x04);
123 input_report_key(dev, BTN_4, data[2] & 0x08);
124 input_report_key(dev, BTN_5, data[2] & 0x10);
125 input_report_key(dev, BTN_6, data[2] & 0x20);
126 input_report_key(dev, BTN_7, data[2] & 0x80);
127 input_report_key(dev, BTN_8, data[1] & 0x01);
128 input_report_key(dev, BTN_9, data[1] & 0x02);
129 input_report_key(dev, BTN_A, data[1] & 0x04);
130 input_report_key(dev, BTN_B, data[1] & 0x08);
131 input_report_key(dev, BTN_C, data[1] & 0x10);
132 input_report_key(dev, BTN_MODE, data[1] & 0x20);
135 case 'E': /* Device error */
136 spaceball->data[spaceball->idx - 1] = 0;
137 printk(KERN_ERR "spaceball: Device error. [%s]\n", spaceball->data + 1);
140 case '?': /* Bad command packet */
141 spaceball->data[spaceball->idx - 1] = 0;
142 printk(KERN_ERR "spaceball: Bad command. [%s]\n", spaceball->data + 1);
150 * Spaceball 4000 FLX packets all start with a one letter packet-type decriptor,
151 * and end in 0x0d. It uses '^' as an escape for CR, XOFF and XON characters which
152 * can occur in the axis values.
155 static irqreturn_t spaceball_interrupt(struct serio *serio,
156 unsigned char data, unsigned int flags)
158 struct spaceball *spaceball = serio_get_drvdata(serio);
162 spaceball_process_packet(spaceball);
164 spaceball->escape = 0;
167 if (!spaceball->escape) {
168 spaceball->escape = 1;
171 spaceball->escape = 0;
175 if (spaceball->escape) {
176 spaceball->escape = 0;
180 if (spaceball->escape)
181 spaceball->escape = 0;
182 if (spaceball->idx < SPACEBALL_MAX_LENGTH)
183 spaceball->data[spaceball->idx++] = data;
190 * spaceball_disconnect() is the opposite of spaceball_connect()
193 static void spaceball_disconnect(struct serio *serio)
195 struct spaceball* spaceball = serio_get_drvdata(serio);
198 serio_set_drvdata(serio, NULL);
199 input_unregister_device(spaceball->dev);
204 * spaceball_connect() is the routine that is called when someone adds a
205 * new serio device that supports Spaceball protocol and registers it as
209 static int spaceball_connect(struct serio *serio, struct serio_driver *drv)
211 struct spaceball *spaceball;
212 struct input_dev *input_dev;
216 if ((id = serio->id.id) > SPACEBALL_MAX_ID)
219 spaceball = kmalloc(sizeof(struct spaceball), GFP_KERNEL);
220 input_dev = input_allocate_device();
221 if (!spaceball || !input_dev)
224 spaceball->dev = input_dev;
225 snprintf(spaceball->phys, sizeof(spaceball->phys), "%s/input0", serio->phys);
227 input_dev->name = spaceball_names[id];
228 input_dev->phys = spaceball->phys;
229 input_dev->id.bustype = BUS_RS232;
230 input_dev->id.vendor = SERIO_SPACEBALL;
231 input_dev->id.product = id;
232 input_dev->id.version = 0x0100;
233 input_dev->dev.parent = &serio->dev;
235 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
238 case SPACEBALL_4000FLX:
239 case SPACEBALL_4000FLX_L:
240 input_dev->keybit[BIT_WORD(BTN_0)] |= BIT_MASK(BTN_9);
241 input_dev->keybit[BIT_WORD(BTN_A)] |= BIT_MASK(BTN_A) |
242 BIT_MASK(BTN_B) | BIT_MASK(BTN_C) |
245 input_dev->keybit[BIT_WORD(BTN_0)] |= BIT_MASK(BTN_2) |
246 BIT_MASK(BTN_3) | BIT_MASK(BTN_4) |
247 BIT_MASK(BTN_5) | BIT_MASK(BTN_6) |
248 BIT_MASK(BTN_7) | BIT_MASK(BTN_8);
249 case SPACEBALL_3003C:
250 input_dev->keybit[BIT_WORD(BTN_0)] |= BIT_MASK(BTN_1) |
254 for (i = 0; i < 3; i++) {
255 input_set_abs_params(input_dev, ABS_X + i, -8000, 8000, 8, 40);
256 input_set_abs_params(input_dev, ABS_RX + i, -1600, 1600, 2, 8);
259 serio_set_drvdata(serio, spaceball);
261 err = serio_open(serio, drv);
265 err = input_register_device(spaceball->dev);
271 fail3: serio_close(serio);
272 fail2: serio_set_drvdata(serio, NULL);
273 fail1: input_free_device(input_dev);
279 * The serio driver structure.
282 static struct serio_device_id spaceball_serio_ids[] = {
285 .proto = SERIO_SPACEBALL,
292 MODULE_DEVICE_TABLE(serio, spaceball_serio_ids);
294 static struct serio_driver spaceball_drv = {
298 .description = DRIVER_DESC,
299 .id_table = spaceball_serio_ids,
300 .interrupt = spaceball_interrupt,
301 .connect = spaceball_connect,
302 .disconnect = spaceball_disconnect,
305 module_serio_driver(spaceball_drv);