1 /*
2  *  Copyright (c) 1999-2001 Vojtech Pavlik
3  */
4 
5 /*
6  * Magellan and Space Mouse 6dof controller driver for Linux
7  */
8 
9 /*
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23  */
24 
25 #include <linux/kernel.h>
26 #include <linux/module.h>
27 #include <linux/slab.h>
28 #include <linux/input.h>
29 #include <linux/serio.h>
30 
31 #define DRIVER_DESC	"Magellan and SpaceMouse 6dof controller driver"
32 
33 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
34 MODULE_DESCRIPTION(DRIVER_DESC);
35 MODULE_LICENSE("GPL");
36 
37 /*
38  * Definitions & global arrays.
39  */
40 
41 #define	MAGELLAN_MAX_LENGTH	32
42 
43 static int magellan_buttons[] = { BTN_0, BTN_1, BTN_2, BTN_3, BTN_4, BTN_5, BTN_6, BTN_7, BTN_8 };
44 static int magellan_axes[] = { ABS_X, ABS_Y, ABS_Z, ABS_RX, ABS_RY, ABS_RZ };
45 
46 /*
47  * Per-Magellan data.
48  */
49 
50 struct magellan {
51 	struct input_dev *dev;
52 	int idx;
53 	unsigned char data[MAGELLAN_MAX_LENGTH];
54 	char phys[32];
55 };
56 
57 /*
58  * magellan_crunch_nibbles() verifies that the bytes sent from the Magellan
59  * have correct upper nibbles for the lower ones, if not, the packet will
60  * be thrown away. It also strips these upper halves to simplify further
61  * processing.
62  */
63 
64 static int magellan_crunch_nibbles(unsigned char *data, int count)
65 {
66 	static unsigned char nibbles[16] = "0AB3D56GH9:K<MN?";
67 
68 	do {
69 		if (data[count] == nibbles[data[count] & 0xf])
70 			data[count] = data[count] & 0xf;
71 		else
72 			return -1;
73 	} while (--count);
74 
75 	return 0;
76 }
77 
78 static void magellan_process_packet(struct magellan* magellan)
79 {
80 	struct input_dev *dev = magellan->dev;
81 	unsigned char *data = magellan->data;
82 	int i, t;
83 
84 	if (!magellan->idx) return;
85 
86 	switch (magellan->data[0]) {
87 
88 		case 'd':				/* Axis data */
89 			if (magellan->idx != 25) return;
90 			if (magellan_crunch_nibbles(data, 24)) return;
91 			for (i = 0; i < 6; i++)
92 				input_report_abs(dev, magellan_axes[i],
93 					(data[(i << 2) + 1] << 12 | data[(i << 2) + 2] << 8 |
94 					 data[(i << 2) + 3] <<  4 | data[(i << 2) + 4]) - 32768);
95 			break;
96 
97 		case 'k':				/* Button data */
98 			if (magellan->idx != 4) return;
99 			if (magellan_crunch_nibbles(data, 3)) return;
100 			t = (data[1] << 1) | (data[2] << 5) | data[3];
101 			for (i = 0; i < 9; i++) input_report_key(dev, magellan_buttons[i], (t >> i) & 1);
102 			break;
103 	}
104 
105 	input_sync(dev);
106 }
107 
108 static irqreturn_t magellan_interrupt(struct serio *serio,
109 		unsigned char data, unsigned int flags)
110 {
111 	struct magellan* magellan = serio_get_drvdata(serio);
112 
113 	if (data == '\r') {
114 		magellan_process_packet(magellan);
115 		magellan->idx = 0;
116 	} else {
117 		if (magellan->idx < MAGELLAN_MAX_LENGTH)
118 			magellan->data[magellan->idx++] = data;
119 	}
120 	return IRQ_HANDLED;
121 }
122 
123 /*
124  * magellan_disconnect() is the opposite of magellan_connect()
125  */
126 
127 static void magellan_disconnect(struct serio *serio)
128 {
129 	struct magellan* magellan = serio_get_drvdata(serio);
130 
131 	serio_close(serio);
132 	serio_set_drvdata(serio, NULL);
133 	input_unregister_device(magellan->dev);
134 	kfree(magellan);
135 }
136 
137 /*
138  * magellan_connect() is the routine that is called when someone adds a
139  * new serio device that supports Magellan protocol and registers it as
140  * an input device.
141  */
142 
143 static int magellan_connect(struct serio *serio, struct serio_driver *drv)
144 {
145 	struct magellan *magellan;
146 	struct input_dev *input_dev;
147 	int err = -ENOMEM;
148 	int i;
149 
150 	magellan = kzalloc(sizeof(struct magellan), GFP_KERNEL);
151 	input_dev = input_allocate_device();
152 	if (!magellan || !input_dev)
153 		goto fail1;
154 
155 	magellan->dev = input_dev;
156 	snprintf(magellan->phys, sizeof(magellan->phys), "%s/input0", serio->phys);
157 
158 	input_dev->name = "LogiCad3D Magellan / SpaceMouse";
159 	input_dev->phys = magellan->phys;
160 	input_dev->id.bustype = BUS_RS232;
161 	input_dev->id.vendor = SERIO_MAGELLAN;
162 	input_dev->id.product = 0x0001;
163 	input_dev->id.version = 0x0100;
164 	input_dev->dev.parent = &serio->dev;
165 
166 	input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
167 
168 	for (i = 0; i < 9; i++)
169 		set_bit(magellan_buttons[i], input_dev->keybit);
170 
171 	for (i = 0; i < 6; i++)
172 		input_set_abs_params(input_dev, magellan_axes[i], -360, 360, 0, 0);
173 
174 	serio_set_drvdata(serio, magellan);
175 
176 	err = serio_open(serio, drv);
177 	if (err)
178 		goto fail2;
179 
180 	err = input_register_device(magellan->dev);
181 	if (err)
182 		goto fail3;
183 
184 	return 0;
185 
186  fail3:	serio_close(serio);
187  fail2:	serio_set_drvdata(serio, NULL);
188  fail1:	input_free_device(input_dev);
189 	kfree(magellan);
190 	return err;
191 }
192 
193 /*
194  * The serio driver structure.
195  */
196 
197 static const struct serio_device_id magellan_serio_ids[] = {
198 	{
199 		.type	= SERIO_RS232,
200 		.proto	= SERIO_MAGELLAN,
201 		.id	= SERIO_ANY,
202 		.extra	= SERIO_ANY,
203 	},
204 	{ 0 }
205 };
206 
207 MODULE_DEVICE_TABLE(serio, magellan_serio_ids);
208 
209 static struct serio_driver magellan_drv = {
210 	.driver		= {
211 		.name	= "magellan",
212 	},
213 	.description	= DRIVER_DESC,
214 	.id_table	= magellan_serio_ids,
215 	.interrupt	= magellan_interrupt,
216 	.connect	= magellan_connect,
217 	.disconnect	= magellan_disconnect,
218 };
219 
220 module_serio_driver(magellan_drv);
221