2 * $Id: spaceball.c,v 1.17 2002/01/22 20:29:03 vojtech Exp $
4 * Copyright (c) 1999-2001 Vojtech Pavlik
6 * Based on the work of:
12 * SpaceTec SpaceBall 2003/3003/4000 FLX driver for Linux
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 * Should you need to contact me, the author, you can do so either by
31 * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail:
32 * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic
35 #include <linux/kernel.h>
36 #include <linux/slab.h>
37 #include <linux/module.h>
38 #include <linux/init.h>
39 #include <linux/input.h>
40 #include <linux/serio.h>
42 #define DRIVER_DESC "SpaceTec SpaceBall 2003/3003/4000 FLX driver"
44 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
45 MODULE_DESCRIPTION(DRIVER_DESC);
46 MODULE_LICENSE("GPL");
52 #define SPACEBALL_MAX_LENGTH 128
53 #define SPACEBALL_MAX_ID 8
55 #define SPACEBALL_1003 1
56 #define SPACEBALL_2003B 3
57 #define SPACEBALL_2003C 4
58 #define SPACEBALL_3003C 7
59 #define SPACEBALL_4000FLX 8
60 #define SPACEBALL_4000FLX_L 9
62 static int spaceball_axes[] = { ABS_X, ABS_Z, ABS_Y, ABS_RX, ABS_RZ, ABS_RY };
63 static char *spaceball_names[] = {
64 "?", "SpaceTec SpaceBall 1003", "SpaceTec SpaceBall 2003", "SpaceTec SpaceBall 2003B",
65 "SpaceTec SpaceBall 2003C", "SpaceTec SpaceBall 3003", "SpaceTec SpaceBall SpaceController",
66 "SpaceTec SpaceBall 3003C", "SpaceTec SpaceBall 4000FLX", "SpaceTec SpaceBall 4000FLX Lefty" };
77 unsigned char data[SPACEBALL_MAX_LENGTH];
82 * spaceball_process_packet() decodes packets the driver receives from the
86 static void spaceball_process_packet(struct spaceball* spaceball, struct pt_regs *regs)
88 struct input_dev *dev = &spaceball->dev;
89 unsigned char *data = spaceball->data;
92 if (spaceball->idx < 2) return;
94 input_regs(dev, regs);
96 switch (spaceball->data[0]) {
98 case 'D': /* Ball data */
99 if (spaceball->idx != 15) return;
100 for (i = 0; i < 6; i++)
101 input_report_abs(dev, spaceball_axes[i],
102 (__s16)((data[2 * i + 3] << 8) | data[2 * i + 2]));
105 case 'K': /* Button data */
106 if (spaceball->idx != 3) return;
107 input_report_key(dev, BTN_1, (data[2] & 0x01) || (data[2] & 0x20));
108 input_report_key(dev, BTN_2, data[2] & 0x02);
109 input_report_key(dev, BTN_3, data[2] & 0x04);
110 input_report_key(dev, BTN_4, data[2] & 0x08);
111 input_report_key(dev, BTN_5, data[1] & 0x01);
112 input_report_key(dev, BTN_6, data[1] & 0x02);
113 input_report_key(dev, BTN_7, data[1] & 0x04);
114 input_report_key(dev, BTN_8, data[1] & 0x10);
117 case '.': /* Advanced button data */
118 if (spaceball->idx != 3) return;
119 input_report_key(dev, BTN_1, data[2] & 0x01);
120 input_report_key(dev, BTN_2, data[2] & 0x02);
121 input_report_key(dev, BTN_3, data[2] & 0x04);
122 input_report_key(dev, BTN_4, data[2] & 0x08);
123 input_report_key(dev, BTN_5, data[2] & 0x10);
124 input_report_key(dev, BTN_6, data[2] & 0x20);
125 input_report_key(dev, BTN_7, data[2] & 0x80);
126 input_report_key(dev, BTN_8, data[1] & 0x01);
127 input_report_key(dev, BTN_9, data[1] & 0x02);
128 input_report_key(dev, BTN_A, data[1] & 0x04);
129 input_report_key(dev, BTN_B, data[1] & 0x08);
130 input_report_key(dev, BTN_C, data[1] & 0x10);
131 input_report_key(dev, BTN_MODE, data[1] & 0x20);
134 case 'E': /* Device error */
135 spaceball->data[spaceball->idx - 1] = 0;
136 printk(KERN_ERR "spaceball: Device error. [%s]\n", spaceball->data + 1);
139 case '?': /* Bad command packet */
140 spaceball->data[spaceball->idx - 1] = 0;
141 printk(KERN_ERR "spaceball: Bad command. [%s]\n", spaceball->data + 1);
149 * Spaceball 4000 FLX packets all start with a one letter packet-type decriptor,
150 * and end in 0x0d. It uses '^' as an escape for CR, XOFF and XON characters which
151 * can occur in the axis values.
154 static irqreturn_t spaceball_interrupt(struct serio *serio,
155 unsigned char data, unsigned int flags, struct pt_regs *regs)
157 struct spaceball *spaceball = serio_get_drvdata(serio);
161 spaceball_process_packet(spaceball, regs);
163 spaceball->escape = 0;
166 if (!spaceball->escape) {
167 spaceball->escape = 1;
170 spaceball->escape = 0;
174 if (spaceball->escape) {
175 spaceball->escape = 0;
179 if (spaceball->escape)
180 spaceball->escape = 0;
181 if (spaceball->idx < SPACEBALL_MAX_LENGTH)
182 spaceball->data[spaceball->idx++] = data;
189 * spaceball_disconnect() is the opposite of spaceball_connect()
192 static void spaceball_disconnect(struct serio *serio)
194 struct spaceball* spaceball = serio_get_drvdata(serio);
196 input_unregister_device(&spaceball->dev);
198 serio_set_drvdata(serio, NULL);
203 * spaceball_connect() is the routine that is called when someone adds a
204 * new serio device that supports Spaceball protocol and registers it as
208 static int spaceball_connect(struct serio *serio, struct serio_driver *drv)
210 struct spaceball *spaceball;
214 if ((id = serio->id.id) > SPACEBALL_MAX_ID)
217 if (!(spaceball = kmalloc(sizeof(struct spaceball), GFP_KERNEL)))
220 memset(spaceball, 0, sizeof(struct spaceball));
222 spaceball->dev.evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
225 case SPACEBALL_4000FLX:
226 case SPACEBALL_4000FLX_L:
227 spaceball->dev.keybit[LONG(BTN_0)] |= BIT(BTN_9);
228 spaceball->dev.keybit[LONG(BTN_A)] |= BIT(BTN_A) | BIT(BTN_B) | BIT(BTN_C) | BIT(BTN_MODE);
230 spaceball->dev.keybit[LONG(BTN_0)] |= BIT(BTN_2) | BIT(BTN_3) | BIT(BTN_4)
231 | BIT(BTN_5) | BIT(BTN_6) | BIT(BTN_7) | BIT(BTN_8);
232 case SPACEBALL_3003C:
233 spaceball->dev.keybit[LONG(BTN_0)] |= BIT(BTN_1) | BIT(BTN_8);
236 for (i = 0; i < 6; i++) {
237 t = spaceball_axes[i];
238 set_bit(t, spaceball->dev.absbit);
239 spaceball->dev.absmin[t] = i < 3 ? -8000 : -1600;
240 spaceball->dev.absmax[t] = i < 3 ? 8000 : 1600;
241 spaceball->dev.absflat[t] = i < 3 ? 40 : 8;
242 spaceball->dev.absfuzz[t] = i < 3 ? 8 : 2;
245 spaceball->serio = serio;
246 spaceball->dev.private = spaceball;
248 sprintf(spaceball->phys, "%s/input0", serio->phys);
250 init_input_dev(&spaceball->dev);
251 spaceball->dev.name = spaceball_names[id];
252 spaceball->dev.phys = spaceball->phys;
253 spaceball->dev.id.bustype = BUS_RS232;
254 spaceball->dev.id.vendor = SERIO_SPACEBALL;
255 spaceball->dev.id.product = id;
256 spaceball->dev.id.version = 0x0100;
257 spaceball->dev.dev = &serio->dev;
259 serio_set_drvdata(serio, spaceball);
261 err = serio_open(serio, drv);
263 serio_set_drvdata(serio, NULL);
268 input_register_device(&spaceball->dev);
270 printk(KERN_INFO "input: %s on serio%s\n",
271 spaceball_names[id], serio->phys);
277 * The serio driver structure.
280 static struct serio_device_id spaceball_serio_ids[] = {
283 .proto = SERIO_SPACEBALL,
290 MODULE_DEVICE_TABLE(serio, spaceball_serio_ids);
292 static struct serio_driver spaceball_drv = {
296 .description = DRIVER_DESC,
297 .id_table = spaceball_serio_ids,
298 .interrupt = spaceball_interrupt,
299 .connect = spaceball_connect,
300 .disconnect = spaceball_disconnect,
304 * The functions for inserting/removing us as a module.
307 static int __init spaceball_init(void)
309 serio_register_driver(&spaceball_drv);
313 static void __exit spaceball_exit(void)
315 serio_unregister_driver(&spaceball_drv);
318 module_init(spaceball_init);
319 module_exit(spaceball_exit);