Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * $Id: spaceball.c,v 1.17 2002/01/22 20:29:03 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 |
| 8 | * Joseph Krahn |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | /* |
| 12 | * SpaceTec SpaceBall 2003/3003/4000 FLX driver for Linux |
| 13 | */ |
| 14 | |
| 15 | /* |
| 16 | * This program is free software; you can redistribute it and/or modify |
| 17 | * it under the terms of the GNU General Public License as published by |
| 18 | * the Free Software Foundation; either version 2 of the License, or |
| 19 | * (at your option) any later version. |
| 20 | * |
| 21 | * This program is distributed in the hope that it will be useful, |
| 22 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 23 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 24 | * GNU General Public License for more details. |
| 25 | * |
| 26 | * You should have received a copy of the GNU General Public License |
| 27 | * along with this program; if not, write to the Free Software |
| 28 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 29 | * |
| 30 | * Should you need to contact me, the author, you can do so either by |
| 31 | * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail: |
| 32 | * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic |
| 33 | */ |
| 34 | |
| 35 | #include <linux/kernel.h> |
| 36 | #include <linux/slab.h> |
| 37 | #include <linux/module.h> |
| 38 | #include <linux/init.h> |
| 39 | #include <linux/input.h> |
| 40 | #include <linux/serio.h> |
| 41 | |
| 42 | #define DRIVER_DESC "SpaceTec SpaceBall 2003/3003/4000 FLX driver" |
| 43 | |
| 44 | MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>"); |
| 45 | MODULE_DESCRIPTION(DRIVER_DESC); |
| 46 | MODULE_LICENSE("GPL"); |
| 47 | |
| 48 | /* |
| 49 | * Constants. |
| 50 | */ |
| 51 | |
| 52 | #define SPACEBALL_MAX_LENGTH 128 |
| 53 | #define SPACEBALL_MAX_ID 8 |
| 54 | |
| 55 | #define SPACEBALL_1003 1 |
| 56 | #define SPACEBALL_2003B 3 |
| 57 | #define SPACEBALL_2003C 4 |
| 58 | #define SPACEBALL_3003C 7 |
| 59 | #define SPACEBALL_4000FLX 8 |
| 60 | #define SPACEBALL_4000FLX_L 9 |
| 61 | |
| 62 | static int spaceball_axes[] = { ABS_X, ABS_Z, ABS_Y, ABS_RX, ABS_RZ, ABS_RY }; |
| 63 | static char *spaceball_names[] = { |
| 64 | "?", "SpaceTec SpaceBall 1003", "SpaceTec SpaceBall 2003", "SpaceTec SpaceBall 2003B", |
| 65 | "SpaceTec SpaceBall 2003C", "SpaceTec SpaceBall 3003", "SpaceTec SpaceBall SpaceController", |
| 66 | "SpaceTec SpaceBall 3003C", "SpaceTec SpaceBall 4000FLX", "SpaceTec SpaceBall 4000FLX Lefty" }; |
| 67 | |
| 68 | /* |
| 69 | * Per-Ball data. |
| 70 | */ |
| 71 | |
| 72 | struct spaceball { |
| 73 | struct input_dev dev; |
| 74 | struct serio *serio; |
| 75 | int idx; |
| 76 | int escape; |
| 77 | unsigned char data[SPACEBALL_MAX_LENGTH]; |
| 78 | char phys[32]; |
| 79 | }; |
| 80 | |
| 81 | /* |
| 82 | * spaceball_process_packet() decodes packets the driver receives from the |
| 83 | * SpaceBall. |
| 84 | */ |
| 85 | |
| 86 | static void spaceball_process_packet(struct spaceball* spaceball, struct pt_regs *regs) |
| 87 | { |
| 88 | struct input_dev *dev = &spaceball->dev; |
| 89 | unsigned char *data = spaceball->data; |
| 90 | int i; |
| 91 | |
| 92 | if (spaceball->idx < 2) return; |
| 93 | |
| 94 | input_regs(dev, regs); |
| 95 | |
| 96 | switch (spaceball->data[0]) { |
| 97 | |
| 98 | case 'D': /* Ball data */ |
| 99 | if (spaceball->idx != 15) return; |
| 100 | for (i = 0; i < 6; i++) |
| 101 | input_report_abs(dev, spaceball_axes[i], |
| 102 | (__s16)((data[2 * i + 3] << 8) | data[2 * i + 2])); |
| 103 | break; |
| 104 | |
| 105 | case 'K': /* Button data */ |
| 106 | if (spaceball->idx != 3) return; |
| 107 | input_report_key(dev, BTN_1, (data[2] & 0x01) || (data[2] & 0x20)); |
| 108 | input_report_key(dev, BTN_2, data[2] & 0x02); |
| 109 | input_report_key(dev, BTN_3, data[2] & 0x04); |
| 110 | input_report_key(dev, BTN_4, data[2] & 0x08); |
| 111 | input_report_key(dev, BTN_5, data[1] & 0x01); |
| 112 | input_report_key(dev, BTN_6, data[1] & 0x02); |
| 113 | input_report_key(dev, BTN_7, data[1] & 0x04); |
| 114 | input_report_key(dev, BTN_8, data[1] & 0x10); |
| 115 | break; |
| 116 | |
| 117 | case '.': /* Advanced button data */ |
| 118 | if (spaceball->idx != 3) return; |
| 119 | input_report_key(dev, BTN_1, data[2] & 0x01); |
| 120 | input_report_key(dev, BTN_2, data[2] & 0x02); |
| 121 | input_report_key(dev, BTN_3, data[2] & 0x04); |
| 122 | input_report_key(dev, BTN_4, data[2] & 0x08); |
| 123 | input_report_key(dev, BTN_5, data[2] & 0x10); |
| 124 | input_report_key(dev, BTN_6, data[2] & 0x20); |
| 125 | input_report_key(dev, BTN_7, data[2] & 0x80); |
| 126 | input_report_key(dev, BTN_8, data[1] & 0x01); |
| 127 | input_report_key(dev, BTN_9, data[1] & 0x02); |
| 128 | input_report_key(dev, BTN_A, data[1] & 0x04); |
| 129 | input_report_key(dev, BTN_B, data[1] & 0x08); |
| 130 | input_report_key(dev, BTN_C, data[1] & 0x10); |
| 131 | input_report_key(dev, BTN_MODE, data[1] & 0x20); |
| 132 | break; |
| 133 | |
| 134 | case 'E': /* Device error */ |
| 135 | spaceball->data[spaceball->idx - 1] = 0; |
| 136 | printk(KERN_ERR "spaceball: Device error. [%s]\n", spaceball->data + 1); |
| 137 | break; |
| 138 | |
| 139 | case '?': /* Bad command packet */ |
| 140 | spaceball->data[spaceball->idx - 1] = 0; |
| 141 | printk(KERN_ERR "spaceball: Bad command. [%s]\n", spaceball->data + 1); |
| 142 | break; |
| 143 | } |
| 144 | |
| 145 | input_sync(dev); |
| 146 | } |
| 147 | |
| 148 | /* |
| 149 | * Spaceball 4000 FLX packets all start with a one letter packet-type decriptor, |
| 150 | * and end in 0x0d. It uses '^' as an escape for CR, XOFF and XON characters which |
| 151 | * can occur in the axis values. |
| 152 | */ |
| 153 | |
| 154 | static irqreturn_t spaceball_interrupt(struct serio *serio, |
| 155 | unsigned char data, unsigned int flags, struct pt_regs *regs) |
| 156 | { |
| 157 | struct spaceball *spaceball = serio_get_drvdata(serio); |
| 158 | |
| 159 | switch (data) { |
| 160 | case 0xd: |
| 161 | spaceball_process_packet(spaceball, regs); |
| 162 | spaceball->idx = 0; |
| 163 | spaceball->escape = 0; |
| 164 | break; |
| 165 | case '^': |
| 166 | if (!spaceball->escape) { |
| 167 | spaceball->escape = 1; |
| 168 | break; |
| 169 | } |
| 170 | spaceball->escape = 0; |
| 171 | case 'M': |
| 172 | case 'Q': |
| 173 | case 'S': |
| 174 | if (spaceball->escape) { |
| 175 | spaceball->escape = 0; |
| 176 | data &= 0x1f; |
| 177 | } |
| 178 | default: |
| 179 | if (spaceball->escape) |
| 180 | spaceball->escape = 0; |
| 181 | if (spaceball->idx < SPACEBALL_MAX_LENGTH) |
| 182 | spaceball->data[spaceball->idx++] = data; |
| 183 | break; |
| 184 | } |
| 185 | return IRQ_HANDLED; |
| 186 | } |
| 187 | |
| 188 | /* |
| 189 | * spaceball_disconnect() is the opposite of spaceball_connect() |
| 190 | */ |
| 191 | |
| 192 | static void spaceball_disconnect(struct serio *serio) |
| 193 | { |
| 194 | struct spaceball* spaceball = serio_get_drvdata(serio); |
| 195 | |
| 196 | input_unregister_device(&spaceball->dev); |
| 197 | serio_close(serio); |
| 198 | serio_set_drvdata(serio, NULL); |
| 199 | kfree(spaceball); |
| 200 | } |
| 201 | |
| 202 | /* |
| 203 | * spaceball_connect() is the routine that is called when someone adds a |
| 204 | * new serio device that supports Spaceball protocol and registers it as |
| 205 | * an input device. |
| 206 | */ |
| 207 | |
| 208 | static int spaceball_connect(struct serio *serio, struct serio_driver *drv) |
| 209 | { |
| 210 | struct spaceball *spaceball; |
| 211 | int i, t, id; |
| 212 | int err; |
| 213 | |
| 214 | if ((id = serio->id.id) > SPACEBALL_MAX_ID) |
| 215 | return -ENODEV; |
| 216 | |
| 217 | if (!(spaceball = kmalloc(sizeof(struct spaceball), GFP_KERNEL))) |
| 218 | return - ENOMEM; |
| 219 | |
| 220 | memset(spaceball, 0, sizeof(struct spaceball)); |
| 221 | |
| 222 | spaceball->dev.evbit[0] = BIT(EV_KEY) | BIT(EV_ABS); |
| 223 | |
| 224 | switch (id) { |
| 225 | case SPACEBALL_4000FLX: |
| 226 | case SPACEBALL_4000FLX_L: |
| 227 | spaceball->dev.keybit[LONG(BTN_0)] |= BIT(BTN_9); |
| 228 | spaceball->dev.keybit[LONG(BTN_A)] |= BIT(BTN_A) | BIT(BTN_B) | BIT(BTN_C) | BIT(BTN_MODE); |
| 229 | default: |
| 230 | spaceball->dev.keybit[LONG(BTN_0)] |= BIT(BTN_2) | BIT(BTN_3) | BIT(BTN_4) |
| 231 | | BIT(BTN_5) | BIT(BTN_6) | BIT(BTN_7) | BIT(BTN_8); |
| 232 | case SPACEBALL_3003C: |
| 233 | spaceball->dev.keybit[LONG(BTN_0)] |= BIT(BTN_1) | BIT(BTN_8); |
| 234 | } |
| 235 | |
| 236 | for (i = 0; i < 6; i++) { |
| 237 | t = spaceball_axes[i]; |
| 238 | set_bit(t, spaceball->dev.absbit); |
| 239 | spaceball->dev.absmin[t] = i < 3 ? -8000 : -1600; |
| 240 | spaceball->dev.absmax[t] = i < 3 ? 8000 : 1600; |
| 241 | spaceball->dev.absflat[t] = i < 3 ? 40 : 8; |
| 242 | spaceball->dev.absfuzz[t] = i < 3 ? 8 : 2; |
| 243 | } |
| 244 | |
| 245 | spaceball->serio = serio; |
| 246 | spaceball->dev.private = spaceball; |
| 247 | |
| 248 | sprintf(spaceball->phys, "%s/input0", serio->phys); |
| 249 | |
| 250 | init_input_dev(&spaceball->dev); |
| 251 | spaceball->dev.name = spaceball_names[id]; |
| 252 | spaceball->dev.phys = spaceball->phys; |
| 253 | spaceball->dev.id.bustype = BUS_RS232; |
| 254 | spaceball->dev.id.vendor = SERIO_SPACEBALL; |
| 255 | spaceball->dev.id.product = id; |
| 256 | spaceball->dev.id.version = 0x0100; |
| 257 | spaceball->dev.dev = &serio->dev; |
| 258 | |
| 259 | serio_set_drvdata(serio, spaceball); |
| 260 | |
| 261 | err = serio_open(serio, drv); |
| 262 | if (err) { |
| 263 | serio_set_drvdata(serio, NULL); |
| 264 | kfree(spaceball); |
| 265 | return err; |
| 266 | } |
| 267 | |
| 268 | input_register_device(&spaceball->dev); |
| 269 | |
| 270 | printk(KERN_INFO "input: %s on serio%s\n", |
| 271 | spaceball_names[id], serio->phys); |
| 272 | |
| 273 | return 0; |
| 274 | } |
| 275 | |
| 276 | /* |
| 277 | * The serio driver structure. |
| 278 | */ |
| 279 | |
| 280 | static struct serio_device_id spaceball_serio_ids[] = { |
| 281 | { |
| 282 | .type = SERIO_RS232, |
| 283 | .proto = SERIO_SPACEBALL, |
| 284 | .id = SERIO_ANY, |
| 285 | .extra = SERIO_ANY, |
| 286 | }, |
| 287 | { 0 } |
| 288 | }; |
| 289 | |
| 290 | MODULE_DEVICE_TABLE(serio, spaceball_serio_ids); |
| 291 | |
| 292 | static struct serio_driver spaceball_drv = { |
| 293 | .driver = { |
| 294 | .name = "spaceball", |
| 295 | }, |
| 296 | .description = DRIVER_DESC, |
| 297 | .id_table = spaceball_serio_ids, |
| 298 | .interrupt = spaceball_interrupt, |
| 299 | .connect = spaceball_connect, |
| 300 | .disconnect = spaceball_disconnect, |
| 301 | }; |
| 302 | |
| 303 | /* |
| 304 | * The functions for inserting/removing us as a module. |
| 305 | */ |
| 306 | |
| 307 | static int __init spaceball_init(void) |
| 308 | { |
| 309 | serio_register_driver(&spaceball_drv); |
| 310 | return 0; |
| 311 | } |
| 312 | |
| 313 | static void __exit spaceball_exit(void) |
| 314 | { |
| 315 | serio_unregister_driver(&spaceball_drv); |
| 316 | } |
| 317 | |
| 318 | module_init(spaceball_init); |
| 319 | module_exit(spaceball_exit); |