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