1 /*
2  *  Copyright (c) 1999-2001 Vojtech Pavlik
3  *
4  *  Based on the work of:
5  *	David Thompson
6  *	Joseph Krahn
7  */
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/input.h>
37 #include <linux/serio.h>
38 
39 #define DRIVER_DESC	"SpaceTec SpaceBall 2003/3003/4000 FLX driver"
40 
41 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
42 MODULE_DESCRIPTION(DRIVER_DESC);
43 MODULE_LICENSE("GPL");
44 
45 /*
46  * Constants.
47  */
48 
49 #define SPACEBALL_MAX_LENGTH	128
50 #define SPACEBALL_MAX_ID	9
51 
52 #define SPACEBALL_1003      1
53 #define SPACEBALL_2003B     3
54 #define SPACEBALL_2003C     4
55 #define SPACEBALL_3003C     7
56 #define SPACEBALL_4000FLX   8
57 #define SPACEBALL_4000FLX_L 9
58 
59 static int spaceball_axes[] = { ABS_X, ABS_Z, ABS_Y, ABS_RX, ABS_RZ, ABS_RY };
60 static char *spaceball_names[] = {
61 	"?", "SpaceTec SpaceBall 1003", "SpaceTec SpaceBall 2003", "SpaceTec SpaceBall 2003B",
62 	"SpaceTec SpaceBall 2003C", "SpaceTec SpaceBall 3003", "SpaceTec SpaceBall SpaceController",
63 	"SpaceTec SpaceBall 3003C", "SpaceTec SpaceBall 4000FLX", "SpaceTec SpaceBall 4000FLX Lefty" };
64 
65 /*
66  * Per-Ball data.
67  */
68 
69 struct spaceball {
70 	struct input_dev *dev;
71 	int idx;
72 	int escape;
73 	unsigned char data[SPACEBALL_MAX_LENGTH];
74 	char phys[32];
75 };
76 
77 /*
78  * spaceball_process_packet() decodes packets the driver receives from the
79  * SpaceBall.
80  */
81 
82 static void spaceball_process_packet(struct spaceball* spaceball)
83 {
84 	struct input_dev *dev = spaceball->dev;
85 	unsigned char *data = spaceball->data;
86 	int i;
87 
88 	if (spaceball->idx < 2) return;
89 
90 	switch (spaceball->data[0]) {
91 
92 		case 'D':					/* Ball data */
93 			if (spaceball->idx != 15) return;
94 			for (i = 0; i < 6; i++)
95 				input_report_abs(dev, spaceball_axes[i],
96 					(__s16)((data[2 * i + 3] << 8) | data[2 * i + 2]));
97 			break;
98 
99 		case 'K':					/* Button data */
100 			if (spaceball->idx != 3) return;
101 			input_report_key(dev, BTN_1, (data[2] & 0x01) || (data[2] & 0x20));
102 			input_report_key(dev, BTN_2, data[2] & 0x02);
103 			input_report_key(dev, BTN_3, data[2] & 0x04);
104 			input_report_key(dev, BTN_4, data[2] & 0x08);
105 			input_report_key(dev, BTN_5, data[1] & 0x01);
106 			input_report_key(dev, BTN_6, data[1] & 0x02);
107 			input_report_key(dev, BTN_7, data[1] & 0x04);
108 			input_report_key(dev, BTN_8, data[1] & 0x10);
109 			break;
110 
111 		case '.':					/* Advanced button data */
112 			if (spaceball->idx != 3) return;
113 			input_report_key(dev, BTN_1, data[2] & 0x01);
114 			input_report_key(dev, BTN_2, data[2] & 0x02);
115 			input_report_key(dev, BTN_3, data[2] & 0x04);
116 			input_report_key(dev, BTN_4, data[2] & 0x08);
117 			input_report_key(dev, BTN_5, data[2] & 0x10);
118 			input_report_key(dev, BTN_6, data[2] & 0x20);
119 			input_report_key(dev, BTN_7, data[2] & 0x80);
120 			input_report_key(dev, BTN_8, data[1] & 0x01);
121 			input_report_key(dev, BTN_9, data[1] & 0x02);
122 			input_report_key(dev, BTN_A, data[1] & 0x04);
123 			input_report_key(dev, BTN_B, data[1] & 0x08);
124 			input_report_key(dev, BTN_C, data[1] & 0x10);
125 			input_report_key(dev, BTN_MODE, data[1] & 0x20);
126 			break;
127 
128 		case 'E':					/* Device error */
129 			spaceball->data[spaceball->idx - 1] = 0;
130 			printk(KERN_ERR "spaceball: Device error. [%s]\n", spaceball->data + 1);
131 			break;
132 
133 		case '?':					/* Bad command packet */
134 			spaceball->data[spaceball->idx - 1] = 0;
135 			printk(KERN_ERR "spaceball: Bad command. [%s]\n", spaceball->data + 1);
136 			break;
137 	}
138 
139 	input_sync(dev);
140 }
141 
142 /*
143  * Spaceball 4000 FLX packets all start with a one letter packet-type decriptor,
144  * and end in 0x0d. It uses '^' as an escape for CR, XOFF and XON characters which
145  * can occur in the axis values.
146  */
147 
148 static irqreturn_t spaceball_interrupt(struct serio *serio,
149 		unsigned char data, unsigned int flags)
150 {
151 	struct spaceball *spaceball = serio_get_drvdata(serio);
152 
153 	switch (data) {
154 		case 0xd:
155 			spaceball_process_packet(spaceball);
156 			spaceball->idx = 0;
157 			spaceball->escape = 0;
158 			break;
159 		case '^':
160 			if (!spaceball->escape) {
161 				spaceball->escape = 1;
162 				break;
163 			}
164 			spaceball->escape = 0;
165 			/* fall through */
166 		case 'M':
167 		case 'Q':
168 		case 'S':
169 			if (spaceball->escape) {
170 				spaceball->escape = 0;
171 				data &= 0x1f;
172 			}
173 			/* fall through */
174 		default:
175 			if (spaceball->escape)
176 				spaceball->escape = 0;
177 			if (spaceball->idx < SPACEBALL_MAX_LENGTH)
178 				spaceball->data[spaceball->idx++] = data;
179 			break;
180 	}
181 	return IRQ_HANDLED;
182 }
183 
184 /*
185  * spaceball_disconnect() is the opposite of spaceball_connect()
186  */
187 
188 static void spaceball_disconnect(struct serio *serio)
189 {
190 	struct spaceball* spaceball = serio_get_drvdata(serio);
191 
192 	serio_close(serio);
193 	serio_set_drvdata(serio, NULL);
194 	input_unregister_device(spaceball->dev);
195 	kfree(spaceball);
196 }
197 
198 /*
199  * spaceball_connect() is the routine that is called when someone adds a
200  * new serio device that supports Spaceball protocol and registers it as
201  * an input device.
202  */
203 
204 static int spaceball_connect(struct serio *serio, struct serio_driver *drv)
205 {
206 	struct spaceball *spaceball;
207 	struct input_dev *input_dev;
208 	int err = -ENOMEM;
209 	int i, id;
210 
211 	if ((id = serio->id.id) > SPACEBALL_MAX_ID)
212 		return -ENODEV;
213 
214 	spaceball = kmalloc(sizeof(struct spaceball), GFP_KERNEL);
215 	input_dev = input_allocate_device();
216 	if (!spaceball || !input_dev)
217 		goto fail1;
218 
219 	spaceball->dev = input_dev;
220 	snprintf(spaceball->phys, sizeof(spaceball->phys), "%s/input0", serio->phys);
221 
222 	input_dev->name = spaceball_names[id];
223 	input_dev->phys = spaceball->phys;
224 	input_dev->id.bustype = BUS_RS232;
225 	input_dev->id.vendor = SERIO_SPACEBALL;
226 	input_dev->id.product = id;
227 	input_dev->id.version = 0x0100;
228 	input_dev->dev.parent = &serio->dev;
229 
230 	input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
231 
232 	switch (id) {
233 		case SPACEBALL_4000FLX:
234 		case SPACEBALL_4000FLX_L:
235 			input_dev->keybit[BIT_WORD(BTN_0)] |= BIT_MASK(BTN_9);
236 			input_dev->keybit[BIT_WORD(BTN_A)] |= BIT_MASK(BTN_A) |
237 				BIT_MASK(BTN_B) | BIT_MASK(BTN_C) |
238 				BIT_MASK(BTN_MODE);
239 			/* fall through */
240 		default:
241 			input_dev->keybit[BIT_WORD(BTN_0)] |= BIT_MASK(BTN_2) |
242 				BIT_MASK(BTN_3) | BIT_MASK(BTN_4) |
243 				BIT_MASK(BTN_5) | BIT_MASK(BTN_6) |
244 				BIT_MASK(BTN_7) | BIT_MASK(BTN_8);
245 			/* fall through */
246 		case SPACEBALL_3003C:
247 			input_dev->keybit[BIT_WORD(BTN_0)] |= BIT_MASK(BTN_1) |
248 				BIT_MASK(BTN_8);
249 	}
250 
251 	for (i = 0; i < 3; i++) {
252 		input_set_abs_params(input_dev, ABS_X + i, -8000, 8000, 8, 40);
253 		input_set_abs_params(input_dev, ABS_RX + i, -1600, 1600, 2, 8);
254 	}
255 
256 	serio_set_drvdata(serio, spaceball);
257 
258 	err = serio_open(serio, drv);
259 	if (err)
260 		goto fail2;
261 
262 	err = input_register_device(spaceball->dev);
263 	if (err)
264 		goto fail3;
265 
266 	return 0;
267 
268  fail3:	serio_close(serio);
269  fail2:	serio_set_drvdata(serio, NULL);
270  fail1:	input_free_device(input_dev);
271 	kfree(spaceball);
272 	return err;
273 }
274 
275 /*
276  * The serio driver structure.
277  */
278 
279 static const struct serio_device_id spaceball_serio_ids[] = {
280 	{
281 		.type	= SERIO_RS232,
282 		.proto	= SERIO_SPACEBALL,
283 		.id	= SERIO_ANY,
284 		.extra	= SERIO_ANY,
285 	},
286 	{ 0 }
287 };
288 
289 MODULE_DEVICE_TABLE(serio, spaceball_serio_ids);
290 
291 static struct serio_driver spaceball_drv = {
292 	.driver		= {
293 		.name	= "spaceball",
294 	},
295 	.description	= DRIVER_DESC,
296 	.id_table	= spaceball_serio_ids,
297 	.interrupt	= spaceball_interrupt,
298 	.connect	= spaceball_connect,
299 	.disconnect	= spaceball_disconnect,
300 };
301 
302 module_serio_driver(spaceball_drv);
303