Thomas Gleixner | 1a59d1b8 | 2019-05-27 08:55:05 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * Copyright (c) 1999-2001 Vojtech Pavlik |
| 4 | * |
| 5 | * Based on the work of: |
Dmitry Torokhov | ab0c344 | 2005-05-29 02:28:55 -0500 | [diff] [blame] | 6 | * David Thompson |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | /* |
| 10 | * SpaceTec SpaceOrb 360 and Avenger 6dof controller driver for Linux |
| 11 | */ |
| 12 | |
| 13 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | */ |
| 15 | |
| 16 | #include <linux/kernel.h> |
| 17 | #include <linux/slab.h> |
| 18 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include <linux/input.h> |
| 20 | #include <linux/serio.h> |
| 21 | |
| 22 | #define DRIVER_DESC "SpaceTec SpaceOrb 360 and Avenger 6dof controller driver" |
| 23 | |
| 24 | MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>"); |
| 25 | MODULE_DESCRIPTION(DRIVER_DESC); |
| 26 | MODULE_LICENSE("GPL"); |
| 27 | |
| 28 | /* |
| 29 | * Constants. |
| 30 | */ |
| 31 | |
| 32 | #define SPACEORB_MAX_LENGTH 64 |
| 33 | |
| 34 | static int spaceorb_buttons[] = { BTN_TL, BTN_TR, BTN_Y, BTN_X, BTN_B, BTN_A }; |
| 35 | 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] | 36 | |
| 37 | /* |
| 38 | * Per-Orb data. |
| 39 | */ |
| 40 | |
| 41 | struct spaceorb { |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 42 | struct input_dev *dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | int idx; |
| 44 | unsigned char data[SPACEORB_MAX_LENGTH]; |
| 45 | char phys[32]; |
| 46 | }; |
| 47 | |
| 48 | static unsigned char spaceorb_xor[] = "SpaceWare"; |
| 49 | |
| 50 | static unsigned char *spaceorb_errors[] = { "EEPROM storing 0 failed", "Receive queue overflow", "Transmit queue timeout", |
| 51 | "Bad packet", "Power brown-out", "EEPROM checksum error", "Hardware fault" }; |
| 52 | |
| 53 | /* |
| 54 | * spaceorb_process_packet() decodes packets the driver receives from the |
| 55 | * SpaceOrb. |
| 56 | */ |
| 57 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 58 | static void spaceorb_process_packet(struct spaceorb *spaceorb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | { |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 60 | struct input_dev *dev = spaceorb->dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | unsigned char *data = spaceorb->data; |
| 62 | unsigned char c = 0; |
| 63 | int axes[6]; |
| 64 | int i; |
| 65 | |
| 66 | if (spaceorb->idx < 2) return; |
| 67 | for (i = 0; i < spaceorb->idx; i++) c ^= data[i]; |
| 68 | if (c) return; |
| 69 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | switch (data[0]) { |
| 71 | |
| 72 | case 'R': /* Reset packet */ |
| 73 | spaceorb->data[spaceorb->idx - 1] = 0; |
| 74 | for (i = 1; i < spaceorb->idx && spaceorb->data[i] == ' '; i++); |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 75 | printk(KERN_INFO "input: %s [%s] is %s\n", |
| 76 | dev->name, spaceorb->data + i, spaceorb->phys); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | break; |
| 78 | |
| 79 | case 'D': /* Ball + button data */ |
| 80 | if (spaceorb->idx != 12) return; |
| 81 | for (i = 0; i < 9; i++) spaceorb->data[i+2] ^= spaceorb_xor[i]; |
| 82 | axes[0] = ( data[2] << 3) | (data[ 3] >> 4); |
| 83 | axes[1] = ((data[3] & 0x0f) << 6) | (data[ 4] >> 1); |
| 84 | axes[2] = ((data[4] & 0x01) << 9) | (data[ 5] << 2) | (data[4] >> 5); |
| 85 | axes[3] = ((data[6] & 0x1f) << 5) | (data[ 7] >> 2); |
| 86 | axes[4] = ((data[7] & 0x03) << 8) | (data[ 8] << 1) | (data[7] >> 6); |
| 87 | axes[5] = ((data[9] & 0x3f) << 4) | (data[10] >> 3); |
| 88 | for (i = 0; i < 6; i++) |
| 89 | input_report_abs(dev, spaceorb_axes[i], axes[i] - ((axes[i] & 0x200) ? 1024 : 0)); |
| 90 | for (i = 0; i < 6; i++) |
| 91 | input_report_key(dev, spaceorb_buttons[i], (data[1] >> i) & 1); |
| 92 | break; |
| 93 | |
| 94 | case 'K': /* Button data */ |
| 95 | if (spaceorb->idx != 5) return; |
Adrian Bunk | 6c207e7 | 2005-05-01 08:59:30 -0700 | [diff] [blame] | 96 | for (i = 0; i < 6; i++) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | input_report_key(dev, spaceorb_buttons[i], (data[2] >> i) & 1); |
| 98 | |
| 99 | break; |
| 100 | |
| 101 | case 'E': /* Error packet */ |
| 102 | if (spaceorb->idx != 4) return; |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 103 | printk(KERN_ERR "spaceorb: Device error. [ "); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | for (i = 0; i < 7; i++) if (data[1] & (1 << i)) printk("%s ", spaceorb_errors[i]); |
| 105 | printk("]\n"); |
| 106 | break; |
| 107 | } |
| 108 | |
| 109 | input_sync(dev); |
| 110 | } |
| 111 | |
| 112 | static irqreturn_t spaceorb_interrupt(struct serio *serio, |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 113 | unsigned char data, unsigned int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | { |
| 115 | struct spaceorb* spaceorb = serio_get_drvdata(serio); |
| 116 | |
| 117 | if (~data & 0x80) { |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 118 | if (spaceorb->idx) spaceorb_process_packet(spaceorb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | spaceorb->idx = 0; |
| 120 | } |
| 121 | if (spaceorb->idx < SPACEORB_MAX_LENGTH) |
| 122 | spaceorb->data[spaceorb->idx++] = data & 0x7f; |
| 123 | return IRQ_HANDLED; |
| 124 | } |
| 125 | |
| 126 | /* |
| 127 | * spaceorb_disconnect() is the opposite of spaceorb_connect() |
| 128 | */ |
| 129 | |
| 130 | static void spaceorb_disconnect(struct serio *serio) |
| 131 | { |
| 132 | struct spaceorb* spaceorb = serio_get_drvdata(serio); |
| 133 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | serio_close(serio); |
| 135 | serio_set_drvdata(serio, NULL); |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 136 | input_unregister_device(spaceorb->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | kfree(spaceorb); |
| 138 | } |
| 139 | |
| 140 | /* |
| 141 | * spaceorb_connect() is the routine that is called when someone adds a |
| 142 | * new serio device that supports SpaceOrb/Avenger protocol and registers |
| 143 | * it as an input device. |
| 144 | */ |
| 145 | |
| 146 | static int spaceorb_connect(struct serio *serio, struct serio_driver *drv) |
| 147 | { |
| 148 | struct spaceorb *spaceorb; |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 149 | struct input_dev *input_dev; |
| 150 | int err = -ENOMEM; |
| 151 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 153 | spaceorb = kzalloc(sizeof(struct spaceorb), GFP_KERNEL); |
| 154 | input_dev = input_allocate_device(); |
| 155 | if (!spaceorb || !input_dev) |
Dmitry Torokhov | 127278c | 2006-11-05 22:40:09 -0500 | [diff] [blame] | 156 | goto fail1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 158 | spaceorb->dev = input_dev; |
Dmitry Torokhov | 10ca4c0 | 2006-06-26 01:45:48 -0400 | [diff] [blame] | 159 | snprintf(spaceorb->phys, sizeof(spaceorb->phys), "%s/input0", serio->phys); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 161 | input_dev->name = "SpaceTec SpaceOrb 360 / Avenger"; |
| 162 | input_dev->phys = spaceorb->phys; |
| 163 | input_dev->id.bustype = BUS_RS232; |
| 164 | input_dev->id.vendor = SERIO_SPACEORB; |
| 165 | input_dev->id.product = 0x0001; |
| 166 | input_dev->id.version = 0x0100; |
Dmitry Torokhov | 935e658 | 2007-04-12 01:35:26 -0400 | [diff] [blame] | 167 | input_dev->dev.parent = &serio->dev; |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 168 | |
Jiri Slaby | 7b19ada | 2007-10-18 23:40:32 -0700 | [diff] [blame] | 169 | input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS); |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 170 | |
| 171 | for (i = 0; i < 6; i++) |
| 172 | set_bit(spaceorb_buttons[i], input_dev->keybit); |
| 173 | |
| 174 | for (i = 0; i < 6; i++) |
| 175 | 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] | 176 | |
| 177 | serio_set_drvdata(serio, spaceorb); |
| 178 | |
| 179 | err = serio_open(serio, drv); |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 180 | if (err) |
Dmitry Torokhov | 127278c | 2006-11-05 22:40:09 -0500 | [diff] [blame] | 181 | goto fail2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | |
Dmitry Torokhov | 127278c | 2006-11-05 22:40:09 -0500 | [diff] [blame] | 183 | err = input_register_device(spaceorb->dev); |
| 184 | if (err) |
| 185 | goto fail3; |
| 186 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | return 0; |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 188 | |
Dmitry Torokhov | 127278c | 2006-11-05 22:40:09 -0500 | [diff] [blame] | 189 | fail3: serio_close(serio); |
| 190 | fail2: serio_set_drvdata(serio, NULL); |
| 191 | fail1: input_free_device(input_dev); |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 192 | kfree(spaceorb); |
| 193 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | /* |
| 197 | * The serio driver structure. |
| 198 | */ |
| 199 | |
Arvind Yadav | ecfd71af | 2017-08-18 17:08:17 -0700 | [diff] [blame] | 200 | static const struct serio_device_id spaceorb_serio_ids[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | { |
| 202 | .type = SERIO_RS232, |
| 203 | .proto = SERIO_SPACEORB, |
| 204 | .id = SERIO_ANY, |
| 205 | .extra = SERIO_ANY, |
| 206 | }, |
| 207 | { 0 } |
| 208 | }; |
| 209 | |
| 210 | MODULE_DEVICE_TABLE(serio, spaceorb_serio_ids); |
| 211 | |
| 212 | static struct serio_driver spaceorb_drv = { |
| 213 | .driver = { |
| 214 | .name = "spaceorb", |
| 215 | }, |
| 216 | .description = DRIVER_DESC, |
| 217 | .id_table = spaceorb_serio_ids, |
| 218 | .interrupt = spaceorb_interrupt, |
| 219 | .connect = spaceorb_connect, |
| 220 | .disconnect = spaceorb_disconnect, |
| 221 | }; |
| 222 | |
Axel Lin | 65ac9f7a | 2012-04-03 23:50:17 -0700 | [diff] [blame] | 223 | module_serio_driver(spaceorb_drv); |