blob: 75eb5ca59992aa21afc6244fe39c264887cbdff0 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * $Id: spaceball.c,v 1.17 2002/01/22 20:29:03 vojtech Exp $
3 *
4 * Copyright (c) 1999-2001 Vojtech Pavlik
5 *
6 * Based on the work of:
Dmitry Torokhovab0c3442005-05-29 02:28:55 -05007 * David Thompson
8 * Joseph Krahn
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 */
10
11/*
12 * SpaceTec SpaceBall 2003/3003/4000 FLX driver for Linux
13 */
14
15/*
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.
20 *
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.
25 *
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
29 *
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
33 */
34
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>
41
42#define DRIVER_DESC "SpaceTec SpaceBall 2003/3003/4000 FLX driver"
43
44MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
45MODULE_DESCRIPTION(DRIVER_DESC);
46MODULE_LICENSE("GPL");
47
48/*
49 * Constants.
50 */
51
52#define SPACEBALL_MAX_LENGTH 128
53#define SPACEBALL_MAX_ID 8
54
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
61
62static int spaceball_axes[] = { ABS_X, ABS_Z, ABS_Y, ABS_RX, ABS_RZ, ABS_RY };
63static 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" };
67
68/*
69 * Per-Ball data.
70 */
71
72struct spaceball {
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -050073 struct input_dev *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 int idx;
75 int escape;
76 unsigned char data[SPACEBALL_MAX_LENGTH];
77 char phys[32];
78};
79
80/*
81 * spaceball_process_packet() decodes packets the driver receives from the
82 * SpaceBall.
83 */
84
85static void spaceball_process_packet(struct spaceball* spaceball, struct pt_regs *regs)
86{
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -050087 struct input_dev *dev = spaceball->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 unsigned char *data = spaceball->data;
89 int i;
90
91 if (spaceball->idx < 2) return;
92
93 input_regs(dev, regs);
94
95 switch (spaceball->data[0]) {
96
97 case 'D': /* Ball data */
98 if (spaceball->idx != 15) return;
99 for (i = 0; i < 6; i++)
100 input_report_abs(dev, spaceball_axes[i],
101 (__s16)((data[2 * i + 3] << 8) | data[2 * i + 2]));
102 break;
103
104 case 'K': /* Button data */
105 if (spaceball->idx != 3) return;
106 input_report_key(dev, BTN_1, (data[2] & 0x01) || (data[2] & 0x20));
107 input_report_key(dev, BTN_2, data[2] & 0x02);
108 input_report_key(dev, BTN_3, data[2] & 0x04);
109 input_report_key(dev, BTN_4, data[2] & 0x08);
110 input_report_key(dev, BTN_5, data[1] & 0x01);
111 input_report_key(dev, BTN_6, data[1] & 0x02);
112 input_report_key(dev, BTN_7, data[1] & 0x04);
113 input_report_key(dev, BTN_8, data[1] & 0x10);
114 break;
115
116 case '.': /* Advanced button data */
117 if (spaceball->idx != 3) return;
118 input_report_key(dev, BTN_1, data[2] & 0x01);
119 input_report_key(dev, BTN_2, data[2] & 0x02);
120 input_report_key(dev, BTN_3, data[2] & 0x04);
121 input_report_key(dev, BTN_4, data[2] & 0x08);
122 input_report_key(dev, BTN_5, data[2] & 0x10);
123 input_report_key(dev, BTN_6, data[2] & 0x20);
124 input_report_key(dev, BTN_7, data[2] & 0x80);
125 input_report_key(dev, BTN_8, data[1] & 0x01);
126 input_report_key(dev, BTN_9, data[1] & 0x02);
127 input_report_key(dev, BTN_A, data[1] & 0x04);
128 input_report_key(dev, BTN_B, data[1] & 0x08);
129 input_report_key(dev, BTN_C, data[1] & 0x10);
130 input_report_key(dev, BTN_MODE, data[1] & 0x20);
131 break;
132
133 case 'E': /* Device error */
134 spaceball->data[spaceball->idx - 1] = 0;
135 printk(KERN_ERR "spaceball: Device error. [%s]\n", spaceball->data + 1);
136 break;
137
138 case '?': /* Bad command packet */
139 spaceball->data[spaceball->idx - 1] = 0;
140 printk(KERN_ERR "spaceball: Bad command. [%s]\n", spaceball->data + 1);
141 break;
142 }
143
144 input_sync(dev);
145}
146
147/*
148 * Spaceball 4000 FLX packets all start with a one letter packet-type decriptor,
149 * and end in 0x0d. It uses '^' as an escape for CR, XOFF and XON characters which
150 * can occur in the axis values.
151 */
152
153static irqreturn_t spaceball_interrupt(struct serio *serio,
154 unsigned char data, unsigned int flags, struct pt_regs *regs)
155{
156 struct spaceball *spaceball = serio_get_drvdata(serio);
157
158 switch (data) {
159 case 0xd:
160 spaceball_process_packet(spaceball, regs);
161 spaceball->idx = 0;
162 spaceball->escape = 0;
163 break;
164 case '^':
165 if (!spaceball->escape) {
166 spaceball->escape = 1;
167 break;
168 }
169 spaceball->escape = 0;
170 case 'M':
171 case 'Q':
172 case 'S':
173 if (spaceball->escape) {
174 spaceball->escape = 0;
175 data &= 0x1f;
176 }
177 default:
178 if (spaceball->escape)
179 spaceball->escape = 0;
180 if (spaceball->idx < SPACEBALL_MAX_LENGTH)
181 spaceball->data[spaceball->idx++] = data;
182 break;
183 }
184 return IRQ_HANDLED;
185}
186
187/*
188 * spaceball_disconnect() is the opposite of spaceball_connect()
189 */
190
191static void spaceball_disconnect(struct serio *serio)
192{
193 struct spaceball* spaceball = serio_get_drvdata(serio);
194
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 serio_close(serio);
196 serio_set_drvdata(serio, NULL);
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500197 input_unregister_device(spaceball->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 kfree(spaceball);
199}
200
201/*
202 * spaceball_connect() is the routine that is called when someone adds a
203 * new serio device that supports Spaceball protocol and registers it as
204 * an input device.
205 */
206
207static int spaceball_connect(struct serio *serio, struct serio_driver *drv)
208{
209 struct spaceball *spaceball;
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500210 struct input_dev *input_dev;
211 int err = -ENOMEM;
212 int i, id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
214 if ((id = serio->id.id) > SPACEBALL_MAX_ID)
215 return -ENODEV;
216
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500217 spaceball = kmalloc(sizeof(struct spaceball), GFP_KERNEL);
218 input_dev = input_allocate_device();
219 if (!spaceball || !input_dev)
220 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500222 spaceball->dev = input_dev;
Dmitry Torokhov10ca4c02006-06-26 01:45:48 -0400223 snprintf(spaceball->phys, sizeof(spaceball->phys), "%s/input0", serio->phys);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500225 input_dev->name = spaceball_names[id];
226 input_dev->phys = spaceball->phys;
227 input_dev->id.bustype = BUS_RS232;
228 input_dev->id.vendor = SERIO_SPACEBALL;
229 input_dev->id.product = id;
230 input_dev->id.version = 0x0100;
231 input_dev->cdev.dev = &serio->dev;
232 input_dev->private = spaceball;
233
234 input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
236 switch (id) {
237 case SPACEBALL_4000FLX:
238 case SPACEBALL_4000FLX_L:
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500239 input_dev->keybit[LONG(BTN_0)] |= BIT(BTN_9);
240 input_dev->keybit[LONG(BTN_A)] |= BIT(BTN_A) | BIT(BTN_B) | BIT(BTN_C) | BIT(BTN_MODE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 default:
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500242 input_dev->keybit[LONG(BTN_0)] |= BIT(BTN_2) | BIT(BTN_3) | BIT(BTN_4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 | BIT(BTN_5) | BIT(BTN_6) | BIT(BTN_7) | BIT(BTN_8);
244 case SPACEBALL_3003C:
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500245 input_dev->keybit[LONG(BTN_0)] |= BIT(BTN_1) | BIT(BTN_8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 }
247
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500248 for (i = 0; i < 3; i++) {
249 input_set_abs_params(input_dev, ABS_X + i, -8000, 8000, 8, 40);
250 input_set_abs_params(input_dev, ABS_RX + i, -1600, 1600, 2, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 }
252
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 serio_set_drvdata(serio, spaceball);
254
255 err = serio_open(serio, drv);
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500256 if (err)
257 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500259 input_register_device(spaceball->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 return 0;
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500261
262 fail: serio_set_drvdata(serio, NULL);
263 input_free_device(input_dev);
264 kfree(spaceball);
265 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266}
267
268/*
269 * The serio driver structure.
270 */
271
272static struct serio_device_id spaceball_serio_ids[] = {
273 {
274 .type = SERIO_RS232,
275 .proto = SERIO_SPACEBALL,
276 .id = SERIO_ANY,
277 .extra = SERIO_ANY,
278 },
279 { 0 }
280};
281
282MODULE_DEVICE_TABLE(serio, spaceball_serio_ids);
283
284static struct serio_driver spaceball_drv = {
285 .driver = {
286 .name = "spaceball",
287 },
288 .description = DRIVER_DESC,
289 .id_table = spaceball_serio_ids,
290 .interrupt = spaceball_interrupt,
291 .connect = spaceball_connect,
292 .disconnect = spaceball_disconnect,
293};
294
295/*
296 * The functions for inserting/removing us as a module.
297 */
298
299static int __init spaceball_init(void)
300{
301 serio_register_driver(&spaceball_drv);
302 return 0;
303}
304
305static void __exit spaceball_exit(void)
306{
307 serio_unregister_driver(&spaceball_drv);
308}
309
310module_init(spaceball_init);
311module_exit(spaceball_exit);