1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Copyright (c) 1999-2001 Vojtech Pavlik 4 */ 5 6 /* 7 * Magellan and Space Mouse 6dof controller driver for Linux 8 */ 9 10 #include <linux/kernel.h> 11 #include <linux/module.h> 12 #include <linux/slab.h> 13 #include <linux/input.h> 14 #include <linux/serio.h> 15 16 #define DRIVER_DESC "Magellan and SpaceMouse 6dof controller driver" 17 18 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>"); 19 MODULE_DESCRIPTION(DRIVER_DESC); 20 MODULE_LICENSE("GPL"); 21 22 /* 23 * Definitions & global arrays. 24 */ 25 26 #define MAGELLAN_MAX_LENGTH 32 27 28 static int magellan_buttons[] = { BTN_0, BTN_1, BTN_2, BTN_3, BTN_4, BTN_5, BTN_6, BTN_7, BTN_8 }; 29 static int magellan_axes[] = { ABS_X, ABS_Y, ABS_Z, ABS_RX, ABS_RY, ABS_RZ }; 30 31 /* 32 * Per-Magellan data. 33 */ 34 35 struct magellan { 36 struct input_dev *dev; 37 int idx; 38 unsigned char data[MAGELLAN_MAX_LENGTH]; 39 char phys[32]; 40 }; 41 42 /* 43 * magellan_crunch_nibbles() verifies that the bytes sent from the Magellan 44 * have correct upper nibbles for the lower ones, if not, the packet will 45 * be thrown away. It also strips these upper halves to simplify further 46 * processing. 47 */ 48 49 static int magellan_crunch_nibbles(unsigned char *data, int count) 50 { 51 static unsigned char nibbles[16] = "0AB3D56GH9:K<MN?"; 52 53 do { 54 if (data[count] == nibbles[data[count] & 0xf]) 55 data[count] = data[count] & 0xf; 56 else 57 return -1; 58 } while (--count); 59 60 return 0; 61 } 62 63 static void magellan_process_packet(struct magellan* magellan) 64 { 65 struct input_dev *dev = magellan->dev; 66 unsigned char *data = magellan->data; 67 int i, t; 68 69 if (!magellan->idx) return; 70 71 switch (magellan->data[0]) { 72 73 case 'd': /* Axis data */ 74 if (magellan->idx != 25) return; 75 if (magellan_crunch_nibbles(data, 24)) return; 76 for (i = 0; i < 6; i++) 77 input_report_abs(dev, magellan_axes[i], 78 (data[(i << 2) + 1] << 12 | data[(i << 2) + 2] << 8 | 79 data[(i << 2) + 3] << 4 | data[(i << 2) + 4]) - 32768); 80 break; 81 82 case 'k': /* Button data */ 83 if (magellan->idx != 4) return; 84 if (magellan_crunch_nibbles(data, 3)) return; 85 t = (data[1] << 1) | (data[2] << 5) | data[3]; 86 for (i = 0; i < 9; i++) input_report_key(dev, magellan_buttons[i], (t >> i) & 1); 87 break; 88 } 89 90 input_sync(dev); 91 } 92 93 static irqreturn_t magellan_interrupt(struct serio *serio, 94 unsigned char data, unsigned int flags) 95 { 96 struct magellan* magellan = serio_get_drvdata(serio); 97 98 if (data == '\r') { 99 magellan_process_packet(magellan); 100 magellan->idx = 0; 101 } else { 102 if (magellan->idx < MAGELLAN_MAX_LENGTH) 103 magellan->data[magellan->idx++] = data; 104 } 105 return IRQ_HANDLED; 106 } 107 108 /* 109 * magellan_disconnect() is the opposite of magellan_connect() 110 */ 111 112 static void magellan_disconnect(struct serio *serio) 113 { 114 struct magellan* magellan = serio_get_drvdata(serio); 115 116 serio_close(serio); 117 serio_set_drvdata(serio, NULL); 118 input_unregister_device(magellan->dev); 119 kfree(magellan); 120 } 121 122 /* 123 * magellan_connect() is the routine that is called when someone adds a 124 * new serio device that supports Magellan protocol and registers it as 125 * an input device. 126 */ 127 128 static int magellan_connect(struct serio *serio, struct serio_driver *drv) 129 { 130 struct magellan *magellan; 131 struct input_dev *input_dev; 132 int err = -ENOMEM; 133 int i; 134 135 magellan = kzalloc(sizeof(struct magellan), GFP_KERNEL); 136 input_dev = input_allocate_device(); 137 if (!magellan || !input_dev) 138 goto fail1; 139 140 magellan->dev = input_dev; 141 snprintf(magellan->phys, sizeof(magellan->phys), "%s/input0", serio->phys); 142 143 input_dev->name = "LogiCad3D Magellan / SpaceMouse"; 144 input_dev->phys = magellan->phys; 145 input_dev->id.bustype = BUS_RS232; 146 input_dev->id.vendor = SERIO_MAGELLAN; 147 input_dev->id.product = 0x0001; 148 input_dev->id.version = 0x0100; 149 input_dev->dev.parent = &serio->dev; 150 151 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS); 152 153 for (i = 0; i < 9; i++) 154 set_bit(magellan_buttons[i], input_dev->keybit); 155 156 for (i = 0; i < 6; i++) 157 input_set_abs_params(input_dev, magellan_axes[i], -360, 360, 0, 0); 158 159 serio_set_drvdata(serio, magellan); 160 161 err = serio_open(serio, drv); 162 if (err) 163 goto fail2; 164 165 err = input_register_device(magellan->dev); 166 if (err) 167 goto fail3; 168 169 return 0; 170 171 fail3: serio_close(serio); 172 fail2: serio_set_drvdata(serio, NULL); 173 fail1: input_free_device(input_dev); 174 kfree(magellan); 175 return err; 176 } 177 178 /* 179 * The serio driver structure. 180 */ 181 182 static const struct serio_device_id magellan_serio_ids[] = { 183 { 184 .type = SERIO_RS232, 185 .proto = SERIO_MAGELLAN, 186 .id = SERIO_ANY, 187 .extra = SERIO_ANY, 188 }, 189 { 0 } 190 }; 191 192 MODULE_DEVICE_TABLE(serio, magellan_serio_ids); 193 194 static struct serio_driver magellan_drv = { 195 .driver = { 196 .name = "magellan", 197 }, 198 .description = DRIVER_DESC, 199 .id_table = magellan_serio_ids, 200 .interrupt = magellan_interrupt, 201 .connect = magellan_connect, 202 .disconnect = magellan_disconnect, 203 }; 204 205 module_serio_driver(magellan_drv); 206