Linux 2.6.19-rc1
[linux-2.6/next.git] / drivers / input / joystick / spaceorb.c
blob3e2782e79834cc24397eb93e8ad3223b1a4afef0
1 /*
2 * $Id: spaceorb.c,v 1.15 2002/01/22 20:29:19 vojtech Exp $
4 * Copyright (c) 1999-2001 Vojtech Pavlik
6 * Based on the work of:
7 * David Thompson
8 */
11 * SpaceTec SpaceOrb 360 and Avenger 6dof controller driver for Linux
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 * Should you need to contact me, the author, you can do so either by
30 * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail:
31 * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic
34 #include <linux/kernel.h>
35 #include <linux/slab.h>
36 #include <linux/module.h>
37 #include <linux/init.h>
38 #include <linux/input.h>
39 #include <linux/serio.h>
41 #define DRIVER_DESC "SpaceTec SpaceOrb 360 and Avenger 6dof controller driver"
43 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
44 MODULE_DESCRIPTION(DRIVER_DESC);
45 MODULE_LICENSE("GPL");
48 * Constants.
51 #define SPACEORB_MAX_LENGTH 64
53 static int spaceorb_buttons[] = { BTN_TL, BTN_TR, BTN_Y, BTN_X, BTN_B, BTN_A };
54 static int spaceorb_axes[] = { ABS_X, ABS_Y, ABS_Z, ABS_RX, ABS_RY, ABS_RZ };
57 * Per-Orb data.
60 struct spaceorb {
61 struct input_dev *dev;
62 int idx;
63 unsigned char data[SPACEORB_MAX_LENGTH];
64 char phys[32];
67 static unsigned char spaceorb_xor[] = "SpaceWare";
69 static unsigned char *spaceorb_errors[] = { "EEPROM storing 0 failed", "Receive queue overflow", "Transmit queue timeout",
70 "Bad packet", "Power brown-out", "EEPROM checksum error", "Hardware fault" };
73 * spaceorb_process_packet() decodes packets the driver receives from the
74 * SpaceOrb.
77 static void spaceorb_process_packet(struct spaceorb *spaceorb, struct pt_regs *regs)
79 struct input_dev *dev = spaceorb->dev;
80 unsigned char *data = spaceorb->data;
81 unsigned char c = 0;
82 int axes[6];
83 int i;
85 if (spaceorb->idx < 2) return;
86 for (i = 0; i < spaceorb->idx; i++) c ^= data[i];
87 if (c) return;
89 input_regs(dev, regs);
91 switch (data[0]) {
93 case 'R': /* Reset packet */
94 spaceorb->data[spaceorb->idx - 1] = 0;
95 for (i = 1; i < spaceorb->idx && spaceorb->data[i] == ' '; i++);
96 printk(KERN_INFO "input: %s [%s] is %s\n",
97 dev->name, spaceorb->data + i, spaceorb->phys);
98 break;
100 case 'D': /* Ball + button data */
101 if (spaceorb->idx != 12) return;
102 for (i = 0; i < 9; i++) spaceorb->data[i+2] ^= spaceorb_xor[i];
103 axes[0] = ( data[2] << 3) | (data[ 3] >> 4);
104 axes[1] = ((data[3] & 0x0f) << 6) | (data[ 4] >> 1);
105 axes[2] = ((data[4] & 0x01) << 9) | (data[ 5] << 2) | (data[4] >> 5);
106 axes[3] = ((data[6] & 0x1f) << 5) | (data[ 7] >> 2);
107 axes[4] = ((data[7] & 0x03) << 8) | (data[ 8] << 1) | (data[7] >> 6);
108 axes[5] = ((data[9] & 0x3f) << 4) | (data[10] >> 3);
109 for (i = 0; i < 6; i++)
110 input_report_abs(dev, spaceorb_axes[i], axes[i] - ((axes[i] & 0x200) ? 1024 : 0));
111 for (i = 0; i < 6; i++)
112 input_report_key(dev, spaceorb_buttons[i], (data[1] >> i) & 1);
113 break;
115 case 'K': /* Button data */
116 if (spaceorb->idx != 5) return;
117 for (i = 0; i < 6; i++)
118 input_report_key(dev, spaceorb_buttons[i], (data[2] >> i) & 1);
120 break;
122 case 'E': /* Error packet */
123 if (spaceorb->idx != 4) return;
124 printk(KERN_ERR "spaceorb: Device error. [ ");
125 for (i = 0; i < 7; i++) if (data[1] & (1 << i)) printk("%s ", spaceorb_errors[i]);
126 printk("]\n");
127 break;
130 input_sync(dev);
133 static irqreturn_t spaceorb_interrupt(struct serio *serio,
134 unsigned char data, unsigned int flags, struct pt_regs *regs)
136 struct spaceorb* spaceorb = serio_get_drvdata(serio);
138 if (~data & 0x80) {
139 if (spaceorb->idx) spaceorb_process_packet(spaceorb, regs);
140 spaceorb->idx = 0;
142 if (spaceorb->idx < SPACEORB_MAX_LENGTH)
143 spaceorb->data[spaceorb->idx++] = data & 0x7f;
144 return IRQ_HANDLED;
148 * spaceorb_disconnect() is the opposite of spaceorb_connect()
151 static void spaceorb_disconnect(struct serio *serio)
153 struct spaceorb* spaceorb = serio_get_drvdata(serio);
155 serio_close(serio);
156 serio_set_drvdata(serio, NULL);
157 input_unregister_device(spaceorb->dev);
158 kfree(spaceorb);
162 * spaceorb_connect() is the routine that is called when someone adds a
163 * new serio device that supports SpaceOrb/Avenger protocol and registers
164 * it as an input device.
167 static int spaceorb_connect(struct serio *serio, struct serio_driver *drv)
169 struct spaceorb *spaceorb;
170 struct input_dev *input_dev;
171 int err = -ENOMEM;
172 int i;
174 spaceorb = kzalloc(sizeof(struct spaceorb), GFP_KERNEL);
175 input_dev = input_allocate_device();
176 if (!spaceorb || !input_dev)
177 goto fail;
179 spaceorb->dev = input_dev;
180 snprintf(spaceorb->phys, sizeof(spaceorb->phys), "%s/input0", serio->phys);
182 input_dev->name = "SpaceTec SpaceOrb 360 / Avenger";
183 input_dev->phys = spaceorb->phys;
184 input_dev->id.bustype = BUS_RS232;
185 input_dev->id.vendor = SERIO_SPACEORB;
186 input_dev->id.product = 0x0001;
187 input_dev->id.version = 0x0100;
188 input_dev->cdev.dev = &serio->dev;
189 input_dev->private = spaceorb;
191 input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
193 for (i = 0; i < 6; i++)
194 set_bit(spaceorb_buttons[i], input_dev->keybit);
196 for (i = 0; i < 6; i++)
197 input_set_abs_params(input_dev, spaceorb_axes[i], -508, 508, 0, 0);
199 serio_set_drvdata(serio, spaceorb);
201 err = serio_open(serio, drv);
202 if (err)
203 goto fail;
205 input_register_device(spaceorb->dev);
206 return 0;
208 fail: serio_set_drvdata(serio, NULL);
209 input_free_device(input_dev);
210 kfree(spaceorb);
211 return err;
215 * The serio driver structure.
218 static struct serio_device_id spaceorb_serio_ids[] = {
220 .type = SERIO_RS232,
221 .proto = SERIO_SPACEORB,
222 .id = SERIO_ANY,
223 .extra = SERIO_ANY,
225 { 0 }
228 MODULE_DEVICE_TABLE(serio, spaceorb_serio_ids);
230 static struct serio_driver spaceorb_drv = {
231 .driver = {
232 .name = "spaceorb",
234 .description = DRIVER_DESC,
235 .id_table = spaceorb_serio_ids,
236 .interrupt = spaceorb_interrupt,
237 .connect = spaceorb_connect,
238 .disconnect = spaceorb_disconnect,
242 * The functions for inserting/removing us as a module.
245 static int __init spaceorb_init(void)
247 serio_register_driver(&spaceorb_drv);
248 return 0;
251 static void __exit spaceorb_exit(void)
253 serio_unregister_driver(&spaceorb_drv);
256 module_init(spaceorb_init);
257 module_exit(spaceorb_exit);