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