blob: f2667820e8c5f0177a635eb62afa9736f49d5f4f [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
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 */
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 * Should you need to contact me, the author, you can do so either by
28 * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail:
29 * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic
30 */
31
32#include <linux/kernel.h>
33#include <linux/slab.h>
34#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/input.h>
36#include <linux/serio.h>
37
38#define DRIVER_DESC "SpaceTec SpaceOrb 360 and Avenger 6dof controller driver"
39
40MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
41MODULE_DESCRIPTION(DRIVER_DESC);
42MODULE_LICENSE("GPL");
43
44/*
45 * Constants.
46 */
47
48#define SPACEORB_MAX_LENGTH 64
49
50static int spaceorb_buttons[] = { BTN_TL, BTN_TR, BTN_Y, BTN_X, BTN_B, BTN_A };
51static int spaceorb_axes[] = { ABS_X, ABS_Y, ABS_Z, ABS_RX, ABS_RY, ABS_RZ };
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53/*
54 * Per-Orb data.
55 */
56
57struct spaceorb {
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -050058 struct input_dev *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 int idx;
60 unsigned char data[SPACEORB_MAX_LENGTH];
61 char phys[32];
62};
63
64static unsigned char spaceorb_xor[] = "SpaceWare";
65
66static unsigned char *spaceorb_errors[] = { "EEPROM storing 0 failed", "Receive queue overflow", "Transmit queue timeout",
67 "Bad packet", "Power brown-out", "EEPROM checksum error", "Hardware fault" };
68
69/*
70 * spaceorb_process_packet() decodes packets the driver receives from the
71 * SpaceOrb.
72 */
73
David Howells7d12e782006-10-05 14:55:46 +010074static void spaceorb_process_packet(struct spaceorb *spaceorb)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075{
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -050076 struct input_dev *dev = spaceorb->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 unsigned char *data = spaceorb->data;
78 unsigned char c = 0;
79 int axes[6];
80 int i;
81
82 if (spaceorb->idx < 2) return;
83 for (i = 0; i < spaceorb->idx; i++) c ^= data[i];
84 if (c) return;
85
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 switch (data[0]) {
87
88 case 'R': /* Reset packet */
89 spaceorb->data[spaceorb->idx - 1] = 0;
90 for (i = 1; i < spaceorb->idx && spaceorb->data[i] == ' '; i++);
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -050091 printk(KERN_INFO "input: %s [%s] is %s\n",
92 dev->name, spaceorb->data + i, spaceorb->phys);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 break;
94
95 case 'D': /* Ball + button data */
96 if (spaceorb->idx != 12) return;
97 for (i = 0; i < 9; i++) spaceorb->data[i+2] ^= spaceorb_xor[i];
98 axes[0] = ( data[2] << 3) | (data[ 3] >> 4);
99 axes[1] = ((data[3] & 0x0f) << 6) | (data[ 4] >> 1);
100 axes[2] = ((data[4] & 0x01) << 9) | (data[ 5] << 2) | (data[4] >> 5);
101 axes[3] = ((data[6] & 0x1f) << 5) | (data[ 7] >> 2);
102 axes[4] = ((data[7] & 0x03) << 8) | (data[ 8] << 1) | (data[7] >> 6);
103 axes[5] = ((data[9] & 0x3f) << 4) | (data[10] >> 3);
104 for (i = 0; i < 6; i++)
105 input_report_abs(dev, spaceorb_axes[i], axes[i] - ((axes[i] & 0x200) ? 1024 : 0));
106 for (i = 0; i < 6; i++)
107 input_report_key(dev, spaceorb_buttons[i], (data[1] >> i) & 1);
108 break;
109
110 case 'K': /* Button data */
111 if (spaceorb->idx != 5) return;
Adrian Bunk6c207e72005-05-01 08:59:30 -0700112 for (i = 0; i < 6; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 input_report_key(dev, spaceorb_buttons[i], (data[2] >> i) & 1);
114
115 break;
116
117 case 'E': /* Error packet */
118 if (spaceorb->idx != 4) return;
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500119 printk(KERN_ERR "spaceorb: Device error. [ ");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 for (i = 0; i < 7; i++) if (data[1] & (1 << i)) printk("%s ", spaceorb_errors[i]);
121 printk("]\n");
122 break;
123 }
124
125 input_sync(dev);
126}
127
128static irqreturn_t spaceorb_interrupt(struct serio *serio,
David Howells7d12e782006-10-05 14:55:46 +0100129 unsigned char data, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130{
131 struct spaceorb* spaceorb = serio_get_drvdata(serio);
132
133 if (~data & 0x80) {
David Howells7d12e782006-10-05 14:55:46 +0100134 if (spaceorb->idx) spaceorb_process_packet(spaceorb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 spaceorb->idx = 0;
136 }
137 if (spaceorb->idx < SPACEORB_MAX_LENGTH)
138 spaceorb->data[spaceorb->idx++] = data & 0x7f;
139 return IRQ_HANDLED;
140}
141
142/*
143 * spaceorb_disconnect() is the opposite of spaceorb_connect()
144 */
145
146static void spaceorb_disconnect(struct serio *serio)
147{
148 struct spaceorb* spaceorb = serio_get_drvdata(serio);
149
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 serio_close(serio);
151 serio_set_drvdata(serio, NULL);
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500152 input_unregister_device(spaceorb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 kfree(spaceorb);
154}
155
156/*
157 * spaceorb_connect() is the routine that is called when someone adds a
158 * new serio device that supports SpaceOrb/Avenger protocol and registers
159 * it as an input device.
160 */
161
162static int spaceorb_connect(struct serio *serio, struct serio_driver *drv)
163{
164 struct spaceorb *spaceorb;
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500165 struct input_dev *input_dev;
166 int err = -ENOMEM;
167 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500169 spaceorb = kzalloc(sizeof(struct spaceorb), GFP_KERNEL);
170 input_dev = input_allocate_device();
171 if (!spaceorb || !input_dev)
Dmitry Torokhov127278c2006-11-05 22:40:09 -0500172 goto fail1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500174 spaceorb->dev = input_dev;
Dmitry Torokhov10ca4c02006-06-26 01:45:48 -0400175 snprintf(spaceorb->phys, sizeof(spaceorb->phys), "%s/input0", serio->phys);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500177 input_dev->name = "SpaceTec SpaceOrb 360 / Avenger";
178 input_dev->phys = spaceorb->phys;
179 input_dev->id.bustype = BUS_RS232;
180 input_dev->id.vendor = SERIO_SPACEORB;
181 input_dev->id.product = 0x0001;
182 input_dev->id.version = 0x0100;
Dmitry Torokhov935e6582007-04-12 01:35:26 -0400183 input_dev->dev.parent = &serio->dev;
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500184
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700185 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500186
187 for (i = 0; i < 6; i++)
188 set_bit(spaceorb_buttons[i], input_dev->keybit);
189
190 for (i = 0; i < 6; i++)
191 input_set_abs_params(input_dev, spaceorb_axes[i], -508, 508, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
193 serio_set_drvdata(serio, spaceorb);
194
195 err = serio_open(serio, drv);
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500196 if (err)
Dmitry Torokhov127278c2006-11-05 22:40:09 -0500197 goto fail2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
Dmitry Torokhov127278c2006-11-05 22:40:09 -0500199 err = input_register_device(spaceorb->dev);
200 if (err)
201 goto fail3;
202
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 return 0;
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500204
Dmitry Torokhov127278c2006-11-05 22:40:09 -0500205 fail3: serio_close(serio);
206 fail2: serio_set_drvdata(serio, NULL);
207 fail1: input_free_device(input_dev);
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500208 kfree(spaceorb);
209 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210}
211
212/*
213 * The serio driver structure.
214 */
215
216static struct serio_device_id spaceorb_serio_ids[] = {
217 {
218 .type = SERIO_RS232,
219 .proto = SERIO_SPACEORB,
220 .id = SERIO_ANY,
221 .extra = SERIO_ANY,
222 },
223 { 0 }
224};
225
226MODULE_DEVICE_TABLE(serio, spaceorb_serio_ids);
227
228static struct serio_driver spaceorb_drv = {
229 .driver = {
230 .name = "spaceorb",
231 },
232 .description = DRIVER_DESC,
233 .id_table = spaceorb_serio_ids,
234 .interrupt = spaceorb_interrupt,
235 .connect = spaceorb_connect,
236 .disconnect = spaceorb_disconnect,
237};
238
Axel Lin65ac9f72012-04-03 23:50:17 -0700239module_serio_driver(spaceorb_drv);