1 /* 2 * $Id: spaceorb.c,v 1.15 2002/01/22 20:29:19 vojtech Exp $ 3 * 4 * Copyright (c) 1999-2001 Vojtech Pavlik 5 * 6 * Based on the work of: 7 * David Thompson 8 */ 9 10 /* 11 * SpaceTec SpaceOrb 360 and Avenger 6dof controller driver for Linux 12 */ 13 14 /* 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. 19 * 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. 24 * 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 28 * 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 32 */ 33 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> 40 41 #define DRIVER_DESC "SpaceTec SpaceOrb 360 and Avenger 6dof controller driver" 42 43 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>"); 44 MODULE_DESCRIPTION(DRIVER_DESC); 45 MODULE_LICENSE("GPL"); 46 47 /* 48 * Constants. 49 */ 50 51 #define SPACEORB_MAX_LENGTH 64 52 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 }; 55 static char *spaceorb_name = "SpaceTec SpaceOrb 360 / Avenger"; 56 57 /* 58 * Per-Orb data. 59 */ 60 61 struct spaceorb { 62 struct input_dev dev; 63 struct serio *serio; 64 int idx; 65 unsigned char data[SPACEORB_MAX_LENGTH]; 66 char phys[32]; 67 }; 68 69 static unsigned char spaceorb_xor[] = "SpaceWare"; 70 71 static unsigned char *spaceorb_errors[] = { "EEPROM storing 0 failed", "Receive queue overflow", "Transmit queue timeout", 72 "Bad packet", "Power brown-out", "EEPROM checksum error", "Hardware fault" }; 73 74 /* 75 * spaceorb_process_packet() decodes packets the driver receives from the 76 * SpaceOrb. 77 */ 78 79 static void spaceorb_process_packet(struct spaceorb *spaceorb, struct pt_regs *regs) 80 { 81 struct input_dev *dev = &spaceorb->dev; 82 unsigned char *data = spaceorb->data; 83 unsigned char c = 0; 84 int axes[6]; 85 int i; 86 87 if (spaceorb->idx < 2) return; 88 for (i = 0; i < spaceorb->idx; i++) c ^= data[i]; 89 if (c) return; 90 91 input_regs(dev, regs); 92 93 switch (data[0]) { 94 95 case 'R': /* Reset packet */ 96 spaceorb->data[spaceorb->idx - 1] = 0; 97 for (i = 1; i < spaceorb->idx && spaceorb->data[i] == ' '; i++); 98 printk(KERN_INFO "input: %s [%s] on %s\n", 99 spaceorb_name, spaceorb->data + i, spaceorb->serio->phys); 100 break; 101 102 case 'D': /* Ball + button data */ 103 if (spaceorb->idx != 12) return; 104 for (i = 0; i < 9; i++) spaceorb->data[i+2] ^= spaceorb_xor[i]; 105 axes[0] = ( data[2] << 3) | (data[ 3] >> 4); 106 axes[1] = ((data[3] & 0x0f) << 6) | (data[ 4] >> 1); 107 axes[2] = ((data[4] & 0x01) << 9) | (data[ 5] << 2) | (data[4] >> 5); 108 axes[3] = ((data[6] & 0x1f) << 5) | (data[ 7] >> 2); 109 axes[4] = ((data[7] & 0x03) << 8) | (data[ 8] << 1) | (data[7] >> 6); 110 axes[5] = ((data[9] & 0x3f) << 4) | (data[10] >> 3); 111 for (i = 0; i < 6; i++) 112 input_report_abs(dev, spaceorb_axes[i], axes[i] - ((axes[i] & 0x200) ? 1024 : 0)); 113 for (i = 0; i < 6; i++) 114 input_report_key(dev, spaceorb_buttons[i], (data[1] >> i) & 1); 115 break; 116 117 case 'K': /* Button data */ 118 if (spaceorb->idx != 5) return; 119 for (i = 0; i < 7; i++) 120 input_report_key(dev, spaceorb_buttons[i], (data[2] >> i) & 1); 121 122 break; 123 124 case 'E': /* Error packet */ 125 if (spaceorb->idx != 4) return; 126 printk(KERN_ERR "joy-spaceorb: Device error. [ "); 127 for (i = 0; i < 7; i++) if (data[1] & (1 << i)) printk("%s ", spaceorb_errors[i]); 128 printk("]\n"); 129 break; 130 } 131 132 input_sync(dev); 133 } 134 135 static irqreturn_t spaceorb_interrupt(struct serio *serio, 136 unsigned char data, unsigned int flags, struct pt_regs *regs) 137 { 138 struct spaceorb* spaceorb = serio_get_drvdata(serio); 139 140 if (~data & 0x80) { 141 if (spaceorb->idx) spaceorb_process_packet(spaceorb, regs); 142 spaceorb->idx = 0; 143 } 144 if (spaceorb->idx < SPACEORB_MAX_LENGTH) 145 spaceorb->data[spaceorb->idx++] = data & 0x7f; 146 return IRQ_HANDLED; 147 } 148 149 /* 150 * spaceorb_disconnect() is the opposite of spaceorb_connect() 151 */ 152 153 static void spaceorb_disconnect(struct serio *serio) 154 { 155 struct spaceorb* spaceorb = serio_get_drvdata(serio); 156 157 input_unregister_device(&spaceorb->dev); 158 serio_close(serio); 159 serio_set_drvdata(serio, NULL); 160 kfree(spaceorb); 161 } 162 163 /* 164 * spaceorb_connect() is the routine that is called when someone adds a 165 * new serio device that supports SpaceOrb/Avenger protocol and registers 166 * it as an input device. 167 */ 168 169 static int spaceorb_connect(struct serio *serio, struct serio_driver *drv) 170 { 171 struct spaceorb *spaceorb; 172 int i, t; 173 int err; 174 175 if (!(spaceorb = kmalloc(sizeof(struct spaceorb), GFP_KERNEL))) 176 return -ENOMEM; 177 178 memset(spaceorb, 0, sizeof(struct spaceorb)); 179 180 spaceorb->dev.evbit[0] = BIT(EV_KEY) | BIT(EV_ABS); 181 182 for (i = 0; i < 6; i++) 183 set_bit(spaceorb_buttons[i], spaceorb->dev.keybit); 184 185 for (i = 0; i < 6; i++) { 186 t = spaceorb_axes[i]; 187 set_bit(t, spaceorb->dev.absbit); 188 spaceorb->dev.absmin[t] = -508; 189 spaceorb->dev.absmax[t] = 508; 190 } 191 192 spaceorb->serio = serio; 193 spaceorb->dev.private = spaceorb; 194 195 sprintf(spaceorb->phys, "%s/input0", serio->phys); 196 197 init_input_dev(&spaceorb->dev); 198 spaceorb->dev.name = spaceorb_name; 199 spaceorb->dev.phys = spaceorb->phys; 200 spaceorb->dev.id.bustype = BUS_RS232; 201 spaceorb->dev.id.vendor = SERIO_SPACEORB; 202 spaceorb->dev.id.product = 0x0001; 203 spaceorb->dev.id.version = 0x0100; 204 spaceorb->dev.dev = &serio->dev; 205 206 serio_set_drvdata(serio, spaceorb); 207 208 err = serio_open(serio, drv); 209 if (err) { 210 serio_set_drvdata(serio, NULL); 211 kfree(spaceorb); 212 return err; 213 } 214 215 input_register_device(&spaceorb->dev); 216 217 return 0; 218 } 219 220 /* 221 * The serio driver structure. 222 */ 223 224 static struct serio_device_id spaceorb_serio_ids[] = { 225 { 226 .type = SERIO_RS232, 227 .proto = SERIO_SPACEORB, 228 .id = SERIO_ANY, 229 .extra = SERIO_ANY, 230 }, 231 { 0 } 232 }; 233 234 MODULE_DEVICE_TABLE(serio, spaceorb_serio_ids); 235 236 static struct serio_driver spaceorb_drv = { 237 .driver = { 238 .name = "spaceorb", 239 }, 240 .description = DRIVER_DESC, 241 .id_table = spaceorb_serio_ids, 242 .interrupt = spaceorb_interrupt, 243 .connect = spaceorb_connect, 244 .disconnect = spaceorb_disconnect, 245 }; 246 247 /* 248 * The functions for inserting/removing us as a module. 249 */ 250 251 static int __init spaceorb_init(void) 252 { 253 serio_register_driver(&spaceorb_drv); 254 return 0; 255 } 256 257 static void __exit spaceorb_exit(void) 258 { 259 serio_unregister_driver(&spaceorb_drv); 260 } 261 262 module_init(spaceorb_init); 263 module_exit(spaceorb_exit); 264