1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Copyright (c) 1999-2001 Vojtech Pavlik 4 * 5 * Based on the work of: 6 * David Thompson 7 * Joseph Krahn 8 */ 9 10 /* 11 * SpaceTec SpaceBall 2003/3003/4000 FLX driver for Linux 12 */ 13 14 /* 15 */ 16 17 #include <linux/kernel.h> 18 #include <linux/slab.h> 19 #include <linux/module.h> 20 #include <linux/input.h> 21 #include <linux/serio.h> 22 23 #define DRIVER_DESC "SpaceTec SpaceBall 2003/3003/4000 FLX driver" 24 25 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>"); 26 MODULE_DESCRIPTION(DRIVER_DESC); 27 MODULE_LICENSE("GPL"); 28 29 /* 30 * Constants. 31 */ 32 33 #define SPACEBALL_MAX_LENGTH 128 34 #define SPACEBALL_MAX_ID 9 35 36 #define SPACEBALL_1003 1 37 #define SPACEBALL_2003B 3 38 #define SPACEBALL_2003C 4 39 #define SPACEBALL_3003C 7 40 #define SPACEBALL_4000FLX 8 41 #define SPACEBALL_4000FLX_L 9 42 43 static int spaceball_axes[] = { ABS_X, ABS_Z, ABS_Y, ABS_RX, ABS_RZ, ABS_RY }; 44 static char *spaceball_names[] = { 45 "?", "SpaceTec SpaceBall 1003", "SpaceTec SpaceBall 2003", "SpaceTec SpaceBall 2003B", 46 "SpaceTec SpaceBall 2003C", "SpaceTec SpaceBall 3003", "SpaceTec SpaceBall SpaceController", 47 "SpaceTec SpaceBall 3003C", "SpaceTec SpaceBall 4000FLX", "SpaceTec SpaceBall 4000FLX Lefty" }; 48 49 /* 50 * Per-Ball data. 51 */ 52 53 struct spaceball { 54 struct input_dev *dev; 55 int idx; 56 int escape; 57 unsigned char data[SPACEBALL_MAX_LENGTH]; 58 char phys[32]; 59 }; 60 61 /* 62 * spaceball_process_packet() decodes packets the driver receives from the 63 * SpaceBall. 64 */ 65 66 static void spaceball_process_packet(struct spaceball* spaceball) 67 { 68 struct input_dev *dev = spaceball->dev; 69 unsigned char *data = spaceball->data; 70 int i; 71 72 if (spaceball->idx < 2) return; 73 74 switch (spaceball->data[0]) { 75 76 case 'D': /* Ball data */ 77 if (spaceball->idx != 15) return; 78 for (i = 0; i < 6; i++) 79 input_report_abs(dev, spaceball_axes[i], 80 (__s16)((data[2 * i + 3] << 8) | data[2 * i + 2])); 81 break; 82 83 case 'K': /* Button data */ 84 if (spaceball->idx != 3) return; 85 input_report_key(dev, BTN_1, (data[2] & 0x01) || (data[2] & 0x20)); 86 input_report_key(dev, BTN_2, data[2] & 0x02); 87 input_report_key(dev, BTN_3, data[2] & 0x04); 88 input_report_key(dev, BTN_4, data[2] & 0x08); 89 input_report_key(dev, BTN_5, data[1] & 0x01); 90 input_report_key(dev, BTN_6, data[1] & 0x02); 91 input_report_key(dev, BTN_7, data[1] & 0x04); 92 input_report_key(dev, BTN_8, data[1] & 0x10); 93 break; 94 95 case '.': /* Advanced button data */ 96 if (spaceball->idx != 3) return; 97 input_report_key(dev, BTN_1, data[2] & 0x01); 98 input_report_key(dev, BTN_2, data[2] & 0x02); 99 input_report_key(dev, BTN_3, data[2] & 0x04); 100 input_report_key(dev, BTN_4, data[2] & 0x08); 101 input_report_key(dev, BTN_5, data[2] & 0x10); 102 input_report_key(dev, BTN_6, data[2] & 0x20); 103 input_report_key(dev, BTN_7, data[2] & 0x80); 104 input_report_key(dev, BTN_8, data[1] & 0x01); 105 input_report_key(dev, BTN_9, data[1] & 0x02); 106 input_report_key(dev, BTN_A, data[1] & 0x04); 107 input_report_key(dev, BTN_B, data[1] & 0x08); 108 input_report_key(dev, BTN_C, data[1] & 0x10); 109 input_report_key(dev, BTN_MODE, data[1] & 0x20); 110 break; 111 112 case 'E': /* Device error */ 113 spaceball->data[spaceball->idx - 1] = 0; 114 printk(KERN_ERR "spaceball: Device error. [%s]\n", spaceball->data + 1); 115 break; 116 117 case '?': /* Bad command packet */ 118 spaceball->data[spaceball->idx - 1] = 0; 119 printk(KERN_ERR "spaceball: Bad command. [%s]\n", spaceball->data + 1); 120 break; 121 } 122 123 input_sync(dev); 124 } 125 126 /* 127 * Spaceball 4000 FLX packets all start with a one letter packet-type decriptor, 128 * and end in 0x0d. It uses '^' as an escape for CR, XOFF and XON characters which 129 * can occur in the axis values. 130 */ 131 132 static irqreturn_t spaceball_interrupt(struct serio *serio, 133 unsigned char data, unsigned int flags) 134 { 135 struct spaceball *spaceball = serio_get_drvdata(serio); 136 137 switch (data) { 138 case 0xd: 139 spaceball_process_packet(spaceball); 140 spaceball->idx = 0; 141 spaceball->escape = 0; 142 break; 143 case '^': 144 if (!spaceball->escape) { 145 spaceball->escape = 1; 146 break; 147 } 148 spaceball->escape = 0; 149 fallthrough; 150 case 'M': 151 case 'Q': 152 case 'S': 153 if (spaceball->escape) { 154 spaceball->escape = 0; 155 data &= 0x1f; 156 } 157 fallthrough; 158 default: 159 if (spaceball->escape) 160 spaceball->escape = 0; 161 if (spaceball->idx < SPACEBALL_MAX_LENGTH) 162 spaceball->data[spaceball->idx++] = data; 163 break; 164 } 165 return IRQ_HANDLED; 166 } 167 168 /* 169 * spaceball_disconnect() is the opposite of spaceball_connect() 170 */ 171 172 static void spaceball_disconnect(struct serio *serio) 173 { 174 struct spaceball* spaceball = serio_get_drvdata(serio); 175 176 serio_close(serio); 177 serio_set_drvdata(serio, NULL); 178 input_unregister_device(spaceball->dev); 179 kfree(spaceball); 180 } 181 182 /* 183 * spaceball_connect() is the routine that is called when someone adds a 184 * new serio device that supports Spaceball protocol and registers it as 185 * an input device. 186 */ 187 188 static int spaceball_connect(struct serio *serio, struct serio_driver *drv) 189 { 190 struct spaceball *spaceball; 191 struct input_dev *input_dev; 192 int err = -ENOMEM; 193 int i, id; 194 195 if ((id = serio->id.id) > SPACEBALL_MAX_ID) 196 return -ENODEV; 197 198 spaceball = kmalloc(sizeof(struct spaceball), GFP_KERNEL); 199 input_dev = input_allocate_device(); 200 if (!spaceball || !input_dev) 201 goto fail1; 202 203 spaceball->dev = input_dev; 204 snprintf(spaceball->phys, sizeof(spaceball->phys), "%s/input0", serio->phys); 205 206 input_dev->name = spaceball_names[id]; 207 input_dev->phys = spaceball->phys; 208 input_dev->id.bustype = BUS_RS232; 209 input_dev->id.vendor = SERIO_SPACEBALL; 210 input_dev->id.product = id; 211 input_dev->id.version = 0x0100; 212 input_dev->dev.parent = &serio->dev; 213 214 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS); 215 216 switch (id) { 217 case SPACEBALL_4000FLX: 218 case SPACEBALL_4000FLX_L: 219 input_dev->keybit[BIT_WORD(BTN_0)] |= BIT_MASK(BTN_9); 220 input_dev->keybit[BIT_WORD(BTN_A)] |= BIT_MASK(BTN_A) | 221 BIT_MASK(BTN_B) | BIT_MASK(BTN_C) | 222 BIT_MASK(BTN_MODE); 223 fallthrough; 224 default: 225 input_dev->keybit[BIT_WORD(BTN_0)] |= BIT_MASK(BTN_2) | 226 BIT_MASK(BTN_3) | BIT_MASK(BTN_4) | 227 BIT_MASK(BTN_5) | BIT_MASK(BTN_6) | 228 BIT_MASK(BTN_7) | BIT_MASK(BTN_8); 229 fallthrough; 230 case SPACEBALL_3003C: 231 input_dev->keybit[BIT_WORD(BTN_0)] |= BIT_MASK(BTN_1) | 232 BIT_MASK(BTN_8); 233 } 234 235 for (i = 0; i < 3; i++) { 236 input_set_abs_params(input_dev, ABS_X + i, -8000, 8000, 8, 40); 237 input_set_abs_params(input_dev, ABS_RX + i, -1600, 1600, 2, 8); 238 } 239 240 serio_set_drvdata(serio, spaceball); 241 242 err = serio_open(serio, drv); 243 if (err) 244 goto fail2; 245 246 err = input_register_device(spaceball->dev); 247 if (err) 248 goto fail3; 249 250 return 0; 251 252 fail3: serio_close(serio); 253 fail2: serio_set_drvdata(serio, NULL); 254 fail1: input_free_device(input_dev); 255 kfree(spaceball); 256 return err; 257 } 258 259 /* 260 * The serio driver structure. 261 */ 262 263 static const struct serio_device_id spaceball_serio_ids[] = { 264 { 265 .type = SERIO_RS232, 266 .proto = SERIO_SPACEBALL, 267 .id = SERIO_ANY, 268 .extra = SERIO_ANY, 269 }, 270 { 0 } 271 }; 272 273 MODULE_DEVICE_TABLE(serio, spaceball_serio_ids); 274 275 static struct serio_driver spaceball_drv = { 276 .driver = { 277 .name = "spaceball", 278 }, 279 .description = DRIVER_DESC, 280 .id_table = spaceball_serio_ids, 281 .interrupt = spaceball_interrupt, 282 .connect = spaceball_connect, 283 .disconnect = spaceball_disconnect, 284 }; 285 286 module_serio_driver(spaceball_drv); 287