Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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: |
Dmitry Torokhov | ab0c344 | 2005-05-29 02:28:55 -0500 | [diff] [blame] | 7 | * David Thompson |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | |
| 56 | /* |
| 57 | * Per-Orb data. |
| 58 | */ |
| 59 | |
| 60 | struct spaceorb { |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 61 | struct input_dev *dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | int idx; |
| 63 | unsigned char data[SPACEORB_MAX_LENGTH]; |
| 64 | char phys[32]; |
| 65 | }; |
| 66 | |
| 67 | static unsigned char spaceorb_xor[] = "SpaceWare"; |
| 68 | |
| 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" }; |
| 71 | |
| 72 | /* |
| 73 | * spaceorb_process_packet() decodes packets the driver receives from the |
| 74 | * SpaceOrb. |
| 75 | */ |
| 76 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 77 | static void spaceorb_process_packet(struct spaceorb *spaceorb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | { |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 79 | struct input_dev *dev = spaceorb->dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | unsigned char *data = spaceorb->data; |
| 81 | unsigned char c = 0; |
| 82 | int axes[6]; |
| 83 | int i; |
| 84 | |
| 85 | if (spaceorb->idx < 2) return; |
| 86 | for (i = 0; i < spaceorb->idx; i++) c ^= data[i]; |
| 87 | if (c) return; |
| 88 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | switch (data[0]) { |
| 90 | |
| 91 | case 'R': /* Reset packet */ |
| 92 | spaceorb->data[spaceorb->idx - 1] = 0; |
| 93 | for (i = 1; i < spaceorb->idx && spaceorb->data[i] == ' '; i++); |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 94 | printk(KERN_INFO "input: %s [%s] is %s\n", |
| 95 | dev->name, spaceorb->data + i, spaceorb->phys); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | break; |
| 97 | |
| 98 | case 'D': /* Ball + button data */ |
| 99 | if (spaceorb->idx != 12) return; |
| 100 | for (i = 0; i < 9; i++) spaceorb->data[i+2] ^= spaceorb_xor[i]; |
| 101 | axes[0] = ( data[2] << 3) | (data[ 3] >> 4); |
| 102 | axes[1] = ((data[3] & 0x0f) << 6) | (data[ 4] >> 1); |
| 103 | axes[2] = ((data[4] & 0x01) << 9) | (data[ 5] << 2) | (data[4] >> 5); |
| 104 | axes[3] = ((data[6] & 0x1f) << 5) | (data[ 7] >> 2); |
| 105 | axes[4] = ((data[7] & 0x03) << 8) | (data[ 8] << 1) | (data[7] >> 6); |
| 106 | axes[5] = ((data[9] & 0x3f) << 4) | (data[10] >> 3); |
| 107 | for (i = 0; i < 6; i++) |
| 108 | input_report_abs(dev, spaceorb_axes[i], axes[i] - ((axes[i] & 0x200) ? 1024 : 0)); |
| 109 | for (i = 0; i < 6; i++) |
| 110 | input_report_key(dev, spaceorb_buttons[i], (data[1] >> i) & 1); |
| 111 | break; |
| 112 | |
| 113 | case 'K': /* Button data */ |
| 114 | if (spaceorb->idx != 5) return; |
Adrian Bunk | 6c207e7 | 2005-05-01 08:59:30 -0700 | [diff] [blame] | 115 | for (i = 0; i < 6; i++) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | input_report_key(dev, spaceorb_buttons[i], (data[2] >> i) & 1); |
| 117 | |
| 118 | break; |
| 119 | |
| 120 | case 'E': /* Error packet */ |
| 121 | if (spaceorb->idx != 4) return; |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 122 | printk(KERN_ERR "spaceorb: Device error. [ "); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | for (i = 0; i < 7; i++) if (data[1] & (1 << i)) printk("%s ", spaceorb_errors[i]); |
| 124 | printk("]\n"); |
| 125 | break; |
| 126 | } |
| 127 | |
| 128 | input_sync(dev); |
| 129 | } |
| 130 | |
| 131 | static irqreturn_t spaceorb_interrupt(struct serio *serio, |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 132 | unsigned char data, unsigned int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | { |
| 134 | struct spaceorb* spaceorb = serio_get_drvdata(serio); |
| 135 | |
| 136 | if (~data & 0x80) { |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 137 | if (spaceorb->idx) spaceorb_process_packet(spaceorb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | spaceorb->idx = 0; |
| 139 | } |
| 140 | if (spaceorb->idx < SPACEORB_MAX_LENGTH) |
| 141 | spaceorb->data[spaceorb->idx++] = data & 0x7f; |
| 142 | return IRQ_HANDLED; |
| 143 | } |
| 144 | |
| 145 | /* |
| 146 | * spaceorb_disconnect() is the opposite of spaceorb_connect() |
| 147 | */ |
| 148 | |
| 149 | static void spaceorb_disconnect(struct serio *serio) |
| 150 | { |
| 151 | struct spaceorb* spaceorb = serio_get_drvdata(serio); |
| 152 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | serio_close(serio); |
| 154 | serio_set_drvdata(serio, NULL); |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 155 | input_unregister_device(spaceorb->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | kfree(spaceorb); |
| 157 | } |
| 158 | |
| 159 | /* |
| 160 | * spaceorb_connect() is the routine that is called when someone adds a |
| 161 | * new serio device that supports SpaceOrb/Avenger protocol and registers |
| 162 | * it as an input device. |
| 163 | */ |
| 164 | |
| 165 | static int spaceorb_connect(struct serio *serio, struct serio_driver *drv) |
| 166 | { |
| 167 | struct spaceorb *spaceorb; |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 168 | struct input_dev *input_dev; |
| 169 | int err = -ENOMEM; |
| 170 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 172 | spaceorb = kzalloc(sizeof(struct spaceorb), GFP_KERNEL); |
| 173 | input_dev = input_allocate_device(); |
| 174 | if (!spaceorb || !input_dev) |
Dmitry Torokhov | 127278c | 2006-11-05 22:40:09 -0500 | [diff] [blame] | 175 | goto fail1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 177 | spaceorb->dev = input_dev; |
Dmitry Torokhov | 10ca4c0 | 2006-06-26 01:45:48 -0400 | [diff] [blame] | 178 | snprintf(spaceorb->phys, sizeof(spaceorb->phys), "%s/input0", serio->phys); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 180 | input_dev->name = "SpaceTec SpaceOrb 360 / Avenger"; |
| 181 | input_dev->phys = spaceorb->phys; |
| 182 | input_dev->id.bustype = BUS_RS232; |
| 183 | input_dev->id.vendor = SERIO_SPACEORB; |
| 184 | input_dev->id.product = 0x0001; |
| 185 | input_dev->id.version = 0x0100; |
| 186 | input_dev->cdev.dev = &serio->dev; |
| 187 | input_dev->private = spaceorb; |
| 188 | |
| 189 | input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_ABS); |
| 190 | |
| 191 | for (i = 0; i < 6; i++) |
| 192 | set_bit(spaceorb_buttons[i], input_dev->keybit); |
| 193 | |
| 194 | for (i = 0; i < 6; i++) |
| 195 | input_set_abs_params(input_dev, spaceorb_axes[i], -508, 508, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | |
| 197 | serio_set_drvdata(serio, spaceorb); |
| 198 | |
| 199 | err = serio_open(serio, drv); |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 200 | if (err) |
Dmitry Torokhov | 127278c | 2006-11-05 22:40:09 -0500 | [diff] [blame] | 201 | goto fail2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | |
Dmitry Torokhov | 127278c | 2006-11-05 22:40:09 -0500 | [diff] [blame] | 203 | err = input_register_device(spaceorb->dev); |
| 204 | if (err) |
| 205 | goto fail3; |
| 206 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | return 0; |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 208 | |
Dmitry Torokhov | 127278c | 2006-11-05 22:40:09 -0500 | [diff] [blame] | 209 | fail3: serio_close(serio); |
| 210 | fail2: serio_set_drvdata(serio, NULL); |
| 211 | fail1: input_free_device(input_dev); |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 212 | kfree(spaceorb); |
| 213 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | /* |
| 217 | * The serio driver structure. |
| 218 | */ |
| 219 | |
| 220 | static struct serio_device_id spaceorb_serio_ids[] = { |
| 221 | { |
| 222 | .type = SERIO_RS232, |
| 223 | .proto = SERIO_SPACEORB, |
| 224 | .id = SERIO_ANY, |
| 225 | .extra = SERIO_ANY, |
| 226 | }, |
| 227 | { 0 } |
| 228 | }; |
| 229 | |
| 230 | MODULE_DEVICE_TABLE(serio, spaceorb_serio_ids); |
| 231 | |
| 232 | static struct serio_driver spaceorb_drv = { |
| 233 | .driver = { |
| 234 | .name = "spaceorb", |
| 235 | }, |
| 236 | .description = DRIVER_DESC, |
| 237 | .id_table = spaceorb_serio_ids, |
| 238 | .interrupt = spaceorb_interrupt, |
| 239 | .connect = spaceorb_connect, |
| 240 | .disconnect = spaceorb_disconnect, |
| 241 | }; |
| 242 | |
| 243 | /* |
| 244 | * The functions for inserting/removing us as a module. |
| 245 | */ |
| 246 | |
| 247 | static int __init spaceorb_init(void) |
| 248 | { |
Akinobu Mita | 153a9df0 | 2006-11-23 23:35:10 -0500 | [diff] [blame^] | 249 | return serio_register_driver(&spaceorb_drv); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | static void __exit spaceorb_exit(void) |
| 253 | { |
| 254 | serio_unregister_driver(&spaceorb_drv); |
| 255 | } |
| 256 | |
| 257 | module_init(spaceorb_init); |
| 258 | module_exit(spaceorb_exit); |