1/*
2 * Copyright (c) 1999-2001 Vojtech Pavlik
3 *
4 * Based on the work of:
5 * David Thompson
6 */
7
8/*
9 * SpaceTec SpaceOrb 360 and Avenger 6dof controller driver for Linux
10 */
11
12/*
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 */
27
28#include <linux/kernel.h>
29#include <linux/slab.h>
30#include <linux/module.h>
31#include <linux/input.h>
32#include <linux/serio.h>
33
34#define DRIVER_DESC "SpaceTec SpaceOrb 360 and Avenger 6dof controller driver"
35
36MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
37MODULE_DESCRIPTION(DRIVER_DESC);
38MODULE_LICENSE("GPL");
39
40/*
41 * Constants.
42 */
43
44#define SPACEORB_MAX_LENGTH 64
45
46static int spaceorb_buttons[] = { BTN_TL, BTN_TR, BTN_Y, BTN_X, BTN_B, BTN_A };
47static int spaceorb_axes[] = { ABS_X, ABS_Y, ABS_Z, ABS_RX, ABS_RY, ABS_RZ };
48
49/*
50 * Per-Orb data.
51 */
52
53struct spaceorb {
54 struct input_dev *dev;
55 int idx;
56 unsigned char data[SPACEORB_MAX_LENGTH];
57 char phys[32];
58};
59
60static unsigned char spaceorb_xor[] = "SpaceWare";
61
62static unsigned char *spaceorb_errors[] = { "EEPROM storing 0 failed", "Receive queue overflow", "Transmit queue timeout",
63 "Bad packet", "Power brown-out", "EEPROM checksum error", "Hardware fault" };
64
65/*
66 * spaceorb_process_packet() decodes packets the driver receives from the
67 * SpaceOrb.
68 */
69
70static void spaceorb_process_packet(struct spaceorb *spaceorb)
71{
72 struct input_dev *dev = spaceorb->dev;
73 unsigned char *data = spaceorb->data;
74 unsigned char c = 0;
75 int axes[6];
76 int i;
77
78 if (spaceorb->idx < 2) return;
79 for (i = 0; i < spaceorb->idx; i++) c ^= data[i];
80 if (c) return;
81
82 switch (data[0]) {
83
84 case 'R': /* Reset packet */
85 spaceorb->data[spaceorb->idx - 1] = 0;
86 for (i = 1; i < spaceorb->idx && spaceorb->data[i] == ' '; i++);
87 printk(KERN_INFO "input: %s [%s] is %s\n",
88 dev->name, spaceorb->data + i, spaceorb->phys);
89 break;
90
91 case 'D': /* Ball + button data */
92 if (spaceorb->idx != 12) return;
93 for (i = 0; i < 9; i++) spaceorb->data[i+2] ^= spaceorb_xor[i];
94 axes[0] = ( data[2] << 3) | (data[ 3] >> 4);
95 axes[1] = ((data[3] & 0x0f) << 6) | (data[ 4] >> 1);
96 axes[2] = ((data[4] & 0x01) << 9) | (data[ 5] << 2) | (data[4] >> 5);
97 axes[3] = ((data[6] & 0x1f) << 5) | (data[ 7] >> 2);
98 axes[4] = ((data[7] & 0x03) << 8) | (data[ 8] << 1) | (data[7] >> 6);
99 axes[5] = ((data[9] & 0x3f) << 4) | (data[10] >> 3);
100 for (i = 0; i < 6; i++)
101 input_report_abs(dev, spaceorb_axes[i], axes[i] - ((axes[i] & 0x200) ? 1024 : 0));
102 for (i = 0; i < 6; i++)
103 input_report_key(dev, spaceorb_buttons[i], (data[1] >> i) & 1);
104 break;
105
106 case 'K': /* Button data */
107 if (spaceorb->idx != 5) return;
108 for (i = 0; i < 6; i++)
109 input_report_key(dev, spaceorb_buttons[i], (data[2] >> i) & 1);
110
111 break;
112
113 case 'E': /* Error packet */
114 if (spaceorb->idx != 4) return;
115 printk(KERN_ERR "spaceorb: Device error. [ ");
116 for (i = 0; i < 7; i++) if (data[1] & (1 << i)) printk("%s ", spaceorb_errors[i]);
117 printk("]\n");
118 break;
119 }
120
121 input_sync(dev);
122}
123
124static irqreturn_t spaceorb_interrupt(struct serio *serio,
125 unsigned char data, unsigned int flags)
126{
127 struct spaceorb* spaceorb = serio_get_drvdata(serio);
128
129 if (~data & 0x80) {
130 if (spaceorb->idx) spaceorb_process_packet(spaceorb);
131 spaceorb->idx = 0;
132 }
133 if (spaceorb->idx < SPACEORB_MAX_LENGTH)
134 spaceorb->data[spaceorb->idx++] = data & 0x7f;
135 return IRQ_HANDLED;
136}
137
138/*
139 * spaceorb_disconnect() is the opposite of spaceorb_connect()
140 */
141
142static void spaceorb_disconnect(struct serio *serio)
143{
144 struct spaceorb* spaceorb = serio_get_drvdata(serio);
145
146 serio_close(serio);
147 serio_set_drvdata(serio, NULL);
148 input_unregister_device(spaceorb->dev);
149 kfree(spaceorb);
150}
151
152/*
153 * spaceorb_connect() is the routine that is called when someone adds a
154 * new serio device that supports SpaceOrb/Avenger protocol and registers
155 * it as an input device.
156 */
157
158static int spaceorb_connect(struct serio *serio, struct serio_driver *drv)
159{
160 struct spaceorb *spaceorb;
161 struct input_dev *input_dev;
162 int err = -ENOMEM;
163 int i;
164
165 spaceorb = kzalloc(sizeof(struct spaceorb), GFP_KERNEL);
166 input_dev = input_allocate_device();
167 if (!spaceorb || !input_dev)
168 goto fail1;
169
170 spaceorb->dev = input_dev;
171 snprintf(spaceorb->phys, sizeof(spaceorb->phys), "%s/input0", serio->phys);
172
173 input_dev->name = "SpaceTec SpaceOrb 360 / Avenger";
174 input_dev->phys = spaceorb->phys;
175 input_dev->id.bustype = BUS_RS232;
176 input_dev->id.vendor = SERIO_SPACEORB;
177 input_dev->id.product = 0x0001;
178 input_dev->id.version = 0x0100;
179 input_dev->dev.parent = &serio->dev;
180
181 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
182
183 for (i = 0; i < 6; i++)
184 set_bit(spaceorb_buttons[i], input_dev->keybit);
185
186 for (i = 0; i < 6; i++)
187 input_set_abs_params(input_dev, spaceorb_axes[i], -508, 508, 0, 0);
188
189 serio_set_drvdata(serio, spaceorb);
190
191 err = serio_open(serio, drv);
192 if (err)
193 goto fail2;
194
195 err = input_register_device(spaceorb->dev);
196 if (err)
197 goto fail3;
198
199 return 0;
200
201 fail3: serio_close(serio);
202 fail2: serio_set_drvdata(serio, NULL);
203 fail1: input_free_device(input_dev);
204 kfree(spaceorb);
205 return err;
206}
207
208/*
209 * The serio driver structure.
210 */
211
212static const struct serio_device_id spaceorb_serio_ids[] = {
213 {
214 .type = SERIO_RS232,
215 .proto = SERIO_SPACEORB,
216 .id = SERIO_ANY,
217 .extra = SERIO_ANY,
218 },
219 { 0 }
220};
221
222MODULE_DEVICE_TABLE(serio, spaceorb_serio_ids);
223
224static struct serio_driver spaceorb_drv = {
225 .driver = {
226 .name = "spaceorb",
227 },
228 .description = DRIVER_DESC,
229 .id_table = spaceorb_serio_ids,
230 .interrupt = spaceorb_interrupt,
231 .connect = spaceorb_connect,
232 .disconnect = spaceorb_disconnect,
233};
234
235module_serio_driver(spaceorb_drv);
236