blob: 80a7b27a457a046cb94efd10dbc8ff84bb0eab8b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Copyright (c) 1999-2001 Vojtech Pavlik
3 *
4 * Based on the work of:
Dmitry Torokhovab0c3442005-05-29 02:28:55 -05005 * David Thompson
6 * Joseph Krahn
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 */
8
9/*
10 * SpaceTec SpaceBall 2003/3003/4000 FLX driver for Linux
11 */
12
13/*
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.
18 *
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.
23 *
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
27 *
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
31 */
32
33#include <linux/kernel.h>
34#include <linux/slab.h>
35#include <linux/module.h>
36#include <linux/init.h>
37#include <linux/input.h>
38#include <linux/serio.h>
39
40#define DRIVER_DESC "SpaceTec SpaceBall 2003/3003/4000 FLX driver"
41
42MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
43MODULE_DESCRIPTION(DRIVER_DESC);
44MODULE_LICENSE("GPL");
45
46/*
47 * Constants.
48 */
49
50#define SPACEBALL_MAX_LENGTH 128
Nick Martin2c1dd692006-07-19 01:14:44 -040051#define SPACEBALL_MAX_ID 9
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
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
59
60static int spaceball_axes[] = { ABS_X, ABS_Z, ABS_Y, ABS_RX, ABS_RZ, ABS_RY };
61static 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" };
65
66/*
67 * Per-Ball data.
68 */
69
70struct spaceball {
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -050071 struct input_dev *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 int idx;
73 int escape;
74 unsigned char data[SPACEBALL_MAX_LENGTH];
75 char phys[32];
76};
77
78/*
79 * spaceball_process_packet() decodes packets the driver receives from the
80 * SpaceBall.
81 */
82
David Howells7d12e782006-10-05 14:55:46 +010083static void spaceball_process_packet(struct spaceball* spaceball)
Linus Torvalds1da177e2005-04-16 15:20:36 -070084{
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -050085 struct input_dev *dev = spaceball->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 unsigned char *data = spaceball->data;
87 int i;
88
89 if (spaceball->idx < 2) return;
90
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 switch (spaceball->data[0]) {
92
93 case 'D': /* Ball data */
94 if (spaceball->idx != 15) return;
95 for (i = 0; i < 6; i++)
96 input_report_abs(dev, spaceball_axes[i],
97 (__s16)((data[2 * i + 3] << 8) | data[2 * i + 2]));
98 break;
99
100 case 'K': /* Button data */
101 if (spaceball->idx != 3) return;
102 input_report_key(dev, BTN_1, (data[2] & 0x01) || (data[2] & 0x20));
103 input_report_key(dev, BTN_2, data[2] & 0x02);
104 input_report_key(dev, BTN_3, data[2] & 0x04);
105 input_report_key(dev, BTN_4, data[2] & 0x08);
106 input_report_key(dev, BTN_5, data[1] & 0x01);
107 input_report_key(dev, BTN_6, data[1] & 0x02);
108 input_report_key(dev, BTN_7, data[1] & 0x04);
109 input_report_key(dev, BTN_8, data[1] & 0x10);
110 break;
111
112 case '.': /* Advanced button data */
113 if (spaceball->idx != 3) return;
114 input_report_key(dev, BTN_1, data[2] & 0x01);
115 input_report_key(dev, BTN_2, data[2] & 0x02);
116 input_report_key(dev, BTN_3, data[2] & 0x04);
117 input_report_key(dev, BTN_4, data[2] & 0x08);
118 input_report_key(dev, BTN_5, data[2] & 0x10);
119 input_report_key(dev, BTN_6, data[2] & 0x20);
120 input_report_key(dev, BTN_7, data[2] & 0x80);
121 input_report_key(dev, BTN_8, data[1] & 0x01);
122 input_report_key(dev, BTN_9, data[1] & 0x02);
123 input_report_key(dev, BTN_A, data[1] & 0x04);
124 input_report_key(dev, BTN_B, data[1] & 0x08);
125 input_report_key(dev, BTN_C, data[1] & 0x10);
126 input_report_key(dev, BTN_MODE, data[1] & 0x20);
127 break;
128
129 case 'E': /* Device error */
130 spaceball->data[spaceball->idx - 1] = 0;
131 printk(KERN_ERR "spaceball: Device error. [%s]\n", spaceball->data + 1);
132 break;
133
134 case '?': /* Bad command packet */
135 spaceball->data[spaceball->idx - 1] = 0;
136 printk(KERN_ERR "spaceball: Bad command. [%s]\n", spaceball->data + 1);
137 break;
138 }
139
140 input_sync(dev);
141}
142
143/*
144 * Spaceball 4000 FLX packets all start with a one letter packet-type decriptor,
145 * and end in 0x0d. It uses '^' as an escape for CR, XOFF and XON characters which
146 * can occur in the axis values.
147 */
148
149static irqreturn_t spaceball_interrupt(struct serio *serio,
David Howells7d12e782006-10-05 14:55:46 +0100150 unsigned char data, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151{
152 struct spaceball *spaceball = serio_get_drvdata(serio);
153
154 switch (data) {
155 case 0xd:
David Howells7d12e782006-10-05 14:55:46 +0100156 spaceball_process_packet(spaceball);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 spaceball->idx = 0;
158 spaceball->escape = 0;
159 break;
160 case '^':
161 if (!spaceball->escape) {
162 spaceball->escape = 1;
163 break;
164 }
165 spaceball->escape = 0;
166 case 'M':
167 case 'Q':
168 case 'S':
169 if (spaceball->escape) {
170 spaceball->escape = 0;
171 data &= 0x1f;
172 }
173 default:
174 if (spaceball->escape)
175 spaceball->escape = 0;
176 if (spaceball->idx < SPACEBALL_MAX_LENGTH)
177 spaceball->data[spaceball->idx++] = data;
178 break;
179 }
180 return IRQ_HANDLED;
181}
182
183/*
184 * spaceball_disconnect() is the opposite of spaceball_connect()
185 */
186
187static void spaceball_disconnect(struct serio *serio)
188{
189 struct spaceball* spaceball = serio_get_drvdata(serio);
190
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 serio_close(serio);
192 serio_set_drvdata(serio, NULL);
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500193 input_unregister_device(spaceball->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 kfree(spaceball);
195}
196
197/*
198 * spaceball_connect() is the routine that is called when someone adds a
199 * new serio device that supports Spaceball protocol and registers it as
200 * an input device.
201 */
202
203static int spaceball_connect(struct serio *serio, struct serio_driver *drv)
204{
205 struct spaceball *spaceball;
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500206 struct input_dev *input_dev;
207 int err = -ENOMEM;
208 int i, id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
210 if ((id = serio->id.id) > SPACEBALL_MAX_ID)
211 return -ENODEV;
212
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500213 spaceball = kmalloc(sizeof(struct spaceball), GFP_KERNEL);
214 input_dev = input_allocate_device();
215 if (!spaceball || !input_dev)
Dmitry Torokhov127278c2006-11-05 22:40:09 -0500216 goto fail1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500218 spaceball->dev = input_dev;
Dmitry Torokhov10ca4c02006-06-26 01:45:48 -0400219 snprintf(spaceball->phys, sizeof(spaceball->phys), "%s/input0", serio->phys);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500221 input_dev->name = spaceball_names[id];
222 input_dev->phys = spaceball->phys;
223 input_dev->id.bustype = BUS_RS232;
224 input_dev->id.vendor = SERIO_SPACEBALL;
225 input_dev->id.product = id;
226 input_dev->id.version = 0x0100;
Dmitry Torokhov935e6582007-04-12 01:35:26 -0400227 input_dev->dev.parent = &serio->dev;
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500228
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700229 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
231 switch (id) {
232 case SPACEBALL_4000FLX:
233 case SPACEBALL_4000FLX_L:
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700234 input_dev->keybit[BIT_WORD(BTN_0)] |= BIT_MASK(BTN_9);
235 input_dev->keybit[BIT_WORD(BTN_A)] |= BIT_MASK(BTN_A) |
236 BIT_MASK(BTN_B) | BIT_MASK(BTN_C) |
237 BIT_MASK(BTN_MODE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 default:
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700239 input_dev->keybit[BIT_WORD(BTN_0)] |= BIT_MASK(BTN_2) |
240 BIT_MASK(BTN_3) | BIT_MASK(BTN_4) |
241 BIT_MASK(BTN_5) | BIT_MASK(BTN_6) |
242 BIT_MASK(BTN_7) | BIT_MASK(BTN_8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 case SPACEBALL_3003C:
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700244 input_dev->keybit[BIT_WORD(BTN_0)] |= BIT_MASK(BTN_1) |
245 BIT_MASK(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)
Dmitry Torokhov127278c2006-11-05 22:40:09 -0500257 goto fail2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
Dmitry Torokhov127278c2006-11-05 22:40:09 -0500259 err = input_register_device(spaceball->dev);
260 if (err)
261 goto fail3;
262
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 return 0;
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500264
Dmitry Torokhov127278c2006-11-05 22:40:09 -0500265 fail3: serio_close(serio);
266 fail2: serio_set_drvdata(serio, NULL);
267 fail1: input_free_device(input_dev);
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500268 kfree(spaceball);
269 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270}
271
272/*
273 * The serio driver structure.
274 */
275
276static struct serio_device_id spaceball_serio_ids[] = {
277 {
278 .type = SERIO_RS232,
279 .proto = SERIO_SPACEBALL,
280 .id = SERIO_ANY,
281 .extra = SERIO_ANY,
282 },
283 { 0 }
284};
285
286MODULE_DEVICE_TABLE(serio, spaceball_serio_ids);
287
288static struct serio_driver spaceball_drv = {
289 .driver = {
290 .name = "spaceball",
291 },
292 .description = DRIVER_DESC,
293 .id_table = spaceball_serio_ids,
294 .interrupt = spaceball_interrupt,
295 .connect = spaceball_connect,
296 .disconnect = spaceball_disconnect,
297};
298
Axel Lin65ac9f72012-04-03 23:50:17 -0700299module_serio_driver(spaceball_drv);