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 |
Nick Martin | 2c1dd69 | 2006-07-19 01:14:44 -0400 | [diff] [blame] | 53 | #define SPACEBALL_MAX_ID 9 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 { |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 73 | struct input_dev *dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | int idx; |
| 75 | int escape; |
| 76 | unsigned char data[SPACEBALL_MAX_LENGTH]; |
| 77 | char phys[32]; |
| 78 | }; |
| 79 | |
| 80 | /* |
| 81 | * spaceball_process_packet() decodes packets the driver receives from the |
| 82 | * SpaceBall. |
| 83 | */ |
| 84 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 85 | static void spaceball_process_packet(struct spaceball* spaceball) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | { |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 87 | struct input_dev *dev = spaceball->dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | unsigned char *data = spaceball->data; |
| 89 | int i; |
| 90 | |
| 91 | if (spaceball->idx < 2) return; |
| 92 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | switch (spaceball->data[0]) { |
| 94 | |
| 95 | case 'D': /* Ball data */ |
| 96 | if (spaceball->idx != 15) return; |
| 97 | for (i = 0; i < 6; i++) |
| 98 | input_report_abs(dev, spaceball_axes[i], |
| 99 | (__s16)((data[2 * i + 3] << 8) | data[2 * i + 2])); |
| 100 | break; |
| 101 | |
| 102 | case 'K': /* Button data */ |
| 103 | if (spaceball->idx != 3) return; |
| 104 | input_report_key(dev, BTN_1, (data[2] & 0x01) || (data[2] & 0x20)); |
| 105 | input_report_key(dev, BTN_2, data[2] & 0x02); |
| 106 | input_report_key(dev, BTN_3, data[2] & 0x04); |
| 107 | input_report_key(dev, BTN_4, data[2] & 0x08); |
| 108 | input_report_key(dev, BTN_5, data[1] & 0x01); |
| 109 | input_report_key(dev, BTN_6, data[1] & 0x02); |
| 110 | input_report_key(dev, BTN_7, data[1] & 0x04); |
| 111 | input_report_key(dev, BTN_8, data[1] & 0x10); |
| 112 | break; |
| 113 | |
| 114 | case '.': /* Advanced button data */ |
| 115 | if (spaceball->idx != 3) return; |
| 116 | input_report_key(dev, BTN_1, data[2] & 0x01); |
| 117 | input_report_key(dev, BTN_2, data[2] & 0x02); |
| 118 | input_report_key(dev, BTN_3, data[2] & 0x04); |
| 119 | input_report_key(dev, BTN_4, data[2] & 0x08); |
| 120 | input_report_key(dev, BTN_5, data[2] & 0x10); |
| 121 | input_report_key(dev, BTN_6, data[2] & 0x20); |
| 122 | input_report_key(dev, BTN_7, data[2] & 0x80); |
| 123 | input_report_key(dev, BTN_8, data[1] & 0x01); |
| 124 | input_report_key(dev, BTN_9, data[1] & 0x02); |
| 125 | input_report_key(dev, BTN_A, data[1] & 0x04); |
| 126 | input_report_key(dev, BTN_B, data[1] & 0x08); |
| 127 | input_report_key(dev, BTN_C, data[1] & 0x10); |
| 128 | input_report_key(dev, BTN_MODE, data[1] & 0x20); |
| 129 | break; |
| 130 | |
| 131 | case 'E': /* Device error */ |
| 132 | spaceball->data[spaceball->idx - 1] = 0; |
| 133 | printk(KERN_ERR "spaceball: Device error. [%s]\n", spaceball->data + 1); |
| 134 | break; |
| 135 | |
| 136 | case '?': /* Bad command packet */ |
| 137 | spaceball->data[spaceball->idx - 1] = 0; |
| 138 | printk(KERN_ERR "spaceball: Bad command. [%s]\n", spaceball->data + 1); |
| 139 | break; |
| 140 | } |
| 141 | |
| 142 | input_sync(dev); |
| 143 | } |
| 144 | |
| 145 | /* |
| 146 | * Spaceball 4000 FLX packets all start with a one letter packet-type decriptor, |
| 147 | * and end in 0x0d. It uses '^' as an escape for CR, XOFF and XON characters which |
| 148 | * can occur in the axis values. |
| 149 | */ |
| 150 | |
| 151 | static irqreturn_t spaceball_interrupt(struct serio *serio, |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 152 | unsigned char data, unsigned int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | { |
| 154 | struct spaceball *spaceball = serio_get_drvdata(serio); |
| 155 | |
| 156 | switch (data) { |
| 157 | case 0xd: |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 158 | spaceball_process_packet(spaceball); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | spaceball->idx = 0; |
| 160 | spaceball->escape = 0; |
| 161 | break; |
| 162 | case '^': |
| 163 | if (!spaceball->escape) { |
| 164 | spaceball->escape = 1; |
| 165 | break; |
| 166 | } |
| 167 | spaceball->escape = 0; |
| 168 | case 'M': |
| 169 | case 'Q': |
| 170 | case 'S': |
| 171 | if (spaceball->escape) { |
| 172 | spaceball->escape = 0; |
| 173 | data &= 0x1f; |
| 174 | } |
| 175 | default: |
| 176 | if (spaceball->escape) |
| 177 | spaceball->escape = 0; |
| 178 | if (spaceball->idx < SPACEBALL_MAX_LENGTH) |
| 179 | spaceball->data[spaceball->idx++] = data; |
| 180 | break; |
| 181 | } |
| 182 | return IRQ_HANDLED; |
| 183 | } |
| 184 | |
| 185 | /* |
| 186 | * spaceball_disconnect() is the opposite of spaceball_connect() |
| 187 | */ |
| 188 | |
| 189 | static void spaceball_disconnect(struct serio *serio) |
| 190 | { |
| 191 | struct spaceball* spaceball = serio_get_drvdata(serio); |
| 192 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | serio_close(serio); |
| 194 | serio_set_drvdata(serio, NULL); |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 195 | input_unregister_device(spaceball->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | kfree(spaceball); |
| 197 | } |
| 198 | |
| 199 | /* |
| 200 | * spaceball_connect() is the routine that is called when someone adds a |
| 201 | * new serio device that supports Spaceball protocol and registers it as |
| 202 | * an input device. |
| 203 | */ |
| 204 | |
| 205 | static int spaceball_connect(struct serio *serio, struct serio_driver *drv) |
| 206 | { |
| 207 | struct spaceball *spaceball; |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 208 | struct input_dev *input_dev; |
| 209 | int err = -ENOMEM; |
| 210 | int i, id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | |
| 212 | if ((id = serio->id.id) > SPACEBALL_MAX_ID) |
| 213 | return -ENODEV; |
| 214 | |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 215 | spaceball = kmalloc(sizeof(struct spaceball), GFP_KERNEL); |
| 216 | input_dev = input_allocate_device(); |
| 217 | if (!spaceball || !input_dev) |
Dmitry Torokhov | 127278c | 2006-11-05 22:40:09 -0500 | [diff] [blame] | 218 | goto fail1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 220 | spaceball->dev = input_dev; |
Dmitry Torokhov | 10ca4c0 | 2006-06-26 01:45:48 -0400 | [diff] [blame] | 221 | snprintf(spaceball->phys, sizeof(spaceball->phys), "%s/input0", serio->phys); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 223 | input_dev->name = spaceball_names[id]; |
| 224 | input_dev->phys = spaceball->phys; |
| 225 | input_dev->id.bustype = BUS_RS232; |
| 226 | input_dev->id.vendor = SERIO_SPACEBALL; |
| 227 | input_dev->id.product = id; |
| 228 | input_dev->id.version = 0x0100; |
Dmitry Torokhov | 935e658 | 2007-04-12 01:35:26 -0400 | [diff] [blame] | 229 | input_dev->dev.parent = &serio->dev; |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 230 | |
| 231 | input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_ABS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | |
| 233 | switch (id) { |
| 234 | case SPACEBALL_4000FLX: |
| 235 | case SPACEBALL_4000FLX_L: |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 236 | input_dev->keybit[LONG(BTN_0)] |= BIT(BTN_9); |
| 237 | input_dev->keybit[LONG(BTN_A)] |= BIT(BTN_A) | BIT(BTN_B) | BIT(BTN_C) | BIT(BTN_MODE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | default: |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 239 | input_dev->keybit[LONG(BTN_0)] |= BIT(BTN_2) | BIT(BTN_3) | BIT(BTN_4) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | | BIT(BTN_5) | BIT(BTN_6) | BIT(BTN_7) | BIT(BTN_8); |
| 241 | case SPACEBALL_3003C: |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 242 | input_dev->keybit[LONG(BTN_0)] |= BIT(BTN_1) | BIT(BTN_8); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | } |
| 244 | |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 245 | for (i = 0; i < 3; i++) { |
| 246 | input_set_abs_params(input_dev, ABS_X + i, -8000, 8000, 8, 40); |
| 247 | input_set_abs_params(input_dev, ABS_RX + i, -1600, 1600, 2, 8); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | } |
| 249 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | serio_set_drvdata(serio, spaceball); |
| 251 | |
| 252 | err = serio_open(serio, drv); |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 253 | if (err) |
Dmitry Torokhov | 127278c | 2006-11-05 22:40:09 -0500 | [diff] [blame] | 254 | goto fail2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | |
Dmitry Torokhov | 127278c | 2006-11-05 22:40:09 -0500 | [diff] [blame] | 256 | err = input_register_device(spaceball->dev); |
| 257 | if (err) |
| 258 | goto fail3; |
| 259 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | return 0; |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 261 | |
Dmitry Torokhov | 127278c | 2006-11-05 22:40:09 -0500 | [diff] [blame] | 262 | fail3: serio_close(serio); |
| 263 | fail2: serio_set_drvdata(serio, NULL); |
| 264 | fail1: input_free_device(input_dev); |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 265 | kfree(spaceball); |
| 266 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | /* |
| 270 | * The serio driver structure. |
| 271 | */ |
| 272 | |
| 273 | static struct serio_device_id spaceball_serio_ids[] = { |
| 274 | { |
| 275 | .type = SERIO_RS232, |
| 276 | .proto = SERIO_SPACEBALL, |
| 277 | .id = SERIO_ANY, |
| 278 | .extra = SERIO_ANY, |
| 279 | }, |
| 280 | { 0 } |
| 281 | }; |
| 282 | |
| 283 | MODULE_DEVICE_TABLE(serio, spaceball_serio_ids); |
| 284 | |
| 285 | static struct serio_driver spaceball_drv = { |
| 286 | .driver = { |
| 287 | .name = "spaceball", |
| 288 | }, |
| 289 | .description = DRIVER_DESC, |
| 290 | .id_table = spaceball_serio_ids, |
| 291 | .interrupt = spaceball_interrupt, |
| 292 | .connect = spaceball_connect, |
| 293 | .disconnect = spaceball_disconnect, |
| 294 | }; |
| 295 | |
| 296 | /* |
| 297 | * The functions for inserting/removing us as a module. |
| 298 | */ |
| 299 | |
| 300 | static int __init spaceball_init(void) |
| 301 | { |
Akinobu Mita | 153a9df0 | 2006-11-23 23:35:10 -0500 | [diff] [blame] | 302 | return serio_register_driver(&spaceball_drv); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | static void __exit spaceball_exit(void) |
| 306 | { |
| 307 | serio_unregister_driver(&spaceball_drv); |
| 308 | } |
| 309 | |
| 310 | module_init(spaceball_init); |
| 311 | module_exit(spaceball_exit); |