Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * Copyright (c) 1999-2001 Vojtech Pavlik |
| 3 | */ |
| 4 | |
| 5 | /* |
| 6 | * Magellan and Space Mouse 6dof controller driver for Linux |
| 7 | */ |
| 8 | |
| 9 | /* |
| 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License as published by |
| 12 | * the Free Software Foundation; either version 2 of the License, or |
| 13 | * (at your option) any later version. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU General Public License |
| 21 | * along with this program; if not, write to the Free Software |
| 22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 23 | * |
| 24 | * Should you need to contact me, the author, you can do so either by |
| 25 | * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail: |
| 26 | * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic |
| 27 | */ |
| 28 | |
| 29 | #include <linux/kernel.h> |
| 30 | #include <linux/module.h> |
| 31 | #include <linux/slab.h> |
| 32 | #include <linux/input.h> |
| 33 | #include <linux/serio.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | |
| 35 | #define DRIVER_DESC "Magellan and SpaceMouse 6dof controller driver" |
| 36 | |
| 37 | MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>"); |
| 38 | MODULE_DESCRIPTION(DRIVER_DESC); |
| 39 | MODULE_LICENSE("GPL"); |
| 40 | |
| 41 | /* |
| 42 | * Definitions & global arrays. |
| 43 | */ |
| 44 | |
| 45 | #define MAGELLAN_MAX_LENGTH 32 |
| 46 | |
| 47 | static int magellan_buttons[] = { BTN_0, BTN_1, BTN_2, BTN_3, BTN_4, BTN_5, BTN_6, BTN_7, BTN_8 }; |
| 48 | static int magellan_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] | 49 | |
| 50 | /* |
| 51 | * Per-Magellan data. |
| 52 | */ |
| 53 | |
| 54 | struct magellan { |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 55 | struct input_dev *dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | int idx; |
| 57 | unsigned char data[MAGELLAN_MAX_LENGTH]; |
| 58 | char phys[32]; |
| 59 | }; |
| 60 | |
| 61 | /* |
| 62 | * magellan_crunch_nibbles() verifies that the bytes sent from the Magellan |
| 63 | * have correct upper nibbles for the lower ones, if not, the packet will |
| 64 | * be thrown away. It also strips these upper halves to simplify further |
| 65 | * processing. |
| 66 | */ |
| 67 | |
| 68 | static int magellan_crunch_nibbles(unsigned char *data, int count) |
| 69 | { |
| 70 | static unsigned char nibbles[16] = "0AB3D56GH9:K<MN?"; |
| 71 | |
| 72 | do { |
| 73 | if (data[count] == nibbles[data[count] & 0xf]) |
| 74 | data[count] = data[count] & 0xf; |
| 75 | else |
| 76 | return -1; |
| 77 | } while (--count); |
| 78 | |
| 79 | return 0; |
| 80 | } |
| 81 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 82 | static void magellan_process_packet(struct magellan* magellan) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | { |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 84 | struct input_dev *dev = magellan->dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | unsigned char *data = magellan->data; |
| 86 | int i, t; |
| 87 | |
| 88 | if (!magellan->idx) return; |
| 89 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | switch (magellan->data[0]) { |
| 91 | |
| 92 | case 'd': /* Axis data */ |
| 93 | if (magellan->idx != 25) return; |
| 94 | if (magellan_crunch_nibbles(data, 24)) return; |
| 95 | for (i = 0; i < 6; i++) |
| 96 | input_report_abs(dev, magellan_axes[i], |
| 97 | (data[(i << 2) + 1] << 12 | data[(i << 2) + 2] << 8 | |
| 98 | data[(i << 2) + 3] << 4 | data[(i << 2) + 4]) - 32768); |
| 99 | break; |
| 100 | |
| 101 | case 'k': /* Button data */ |
| 102 | if (magellan->idx != 4) return; |
| 103 | if (magellan_crunch_nibbles(data, 3)) return; |
| 104 | t = (data[1] << 1) | (data[2] << 5) | data[3]; |
| 105 | for (i = 0; i < 9; i++) input_report_key(dev, magellan_buttons[i], (t >> i) & 1); |
| 106 | break; |
| 107 | } |
| 108 | |
| 109 | input_sync(dev); |
| 110 | } |
| 111 | |
| 112 | static irqreturn_t magellan_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 magellan* magellan = serio_get_drvdata(serio); |
| 116 | |
| 117 | if (data == '\r') { |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 118 | magellan_process_packet(magellan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | magellan->idx = 0; |
| 120 | } else { |
| 121 | if (magellan->idx < MAGELLAN_MAX_LENGTH) |
| 122 | magellan->data[magellan->idx++] = data; |
| 123 | } |
| 124 | return IRQ_HANDLED; |
| 125 | } |
| 126 | |
| 127 | /* |
| 128 | * magellan_disconnect() is the opposite of magellan_connect() |
| 129 | */ |
| 130 | |
| 131 | static void magellan_disconnect(struct serio *serio) |
| 132 | { |
| 133 | struct magellan* magellan = serio_get_drvdata(serio); |
| 134 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | serio_close(serio); |
| 136 | serio_set_drvdata(serio, NULL); |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 137 | input_unregister_device(magellan->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | kfree(magellan); |
| 139 | } |
| 140 | |
| 141 | /* |
| 142 | * magellan_connect() is the routine that is called when someone adds a |
| 143 | * new serio device that supports Magellan protocol and registers it as |
| 144 | * an input device. |
| 145 | */ |
| 146 | |
| 147 | static int magellan_connect(struct serio *serio, struct serio_driver *drv) |
| 148 | { |
| 149 | struct magellan *magellan; |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 150 | struct input_dev *input_dev; |
| 151 | int err = -ENOMEM; |
| 152 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 154 | magellan = kzalloc(sizeof(struct magellan), GFP_KERNEL); |
| 155 | input_dev = input_allocate_device(); |
| 156 | if (!magellan || !input_dev) |
Dmitry Torokhov | 127278c | 2006-11-05 22:40:09 -0500 | [diff] [blame] | 157 | goto fail1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 159 | magellan->dev = input_dev; |
Dmitry Torokhov | 10ca4c0 | 2006-06-26 01:45:48 -0400 | [diff] [blame] | 160 | snprintf(magellan->phys, sizeof(magellan->phys), "%s/input0", serio->phys); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 162 | input_dev->name = "LogiCad3D Magellan / SpaceMouse"; |
| 163 | input_dev->phys = magellan->phys; |
| 164 | input_dev->id.bustype = BUS_RS232; |
| 165 | input_dev->id.vendor = SERIO_MAGELLAN; |
| 166 | input_dev->id.product = 0x0001; |
| 167 | input_dev->id.version = 0x0100; |
Dmitry Torokhov | 935e658 | 2007-04-12 01:35:26 -0400 | [diff] [blame] | 168 | input_dev->dev.parent = &serio->dev; |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 169 | |
Jiri Slaby | 7b19ada | 2007-10-18 23:40:32 -0700 | [diff] [blame] | 170 | input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS); |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 171 | |
| 172 | for (i = 0; i < 9; i++) |
| 173 | set_bit(magellan_buttons[i], input_dev->keybit); |
| 174 | |
| 175 | for (i = 0; i < 6; i++) |
| 176 | input_set_abs_params(input_dev, magellan_axes[i], -360, 360, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | |
| 178 | serio_set_drvdata(serio, magellan); |
| 179 | |
| 180 | err = serio_open(serio, drv); |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 181 | if (err) |
Dmitry Torokhov | 127278c | 2006-11-05 22:40:09 -0500 | [diff] [blame] | 182 | goto fail2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | |
Dmitry Torokhov | 127278c | 2006-11-05 22:40:09 -0500 | [diff] [blame] | 184 | err = input_register_device(magellan->dev); |
| 185 | if (err) |
| 186 | goto fail3; |
| 187 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | return 0; |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 189 | |
Dmitry Torokhov | 127278c | 2006-11-05 22:40:09 -0500 | [diff] [blame] | 190 | fail3: serio_close(serio); |
| 191 | fail2: serio_set_drvdata(serio, NULL); |
| 192 | fail1: input_free_device(input_dev); |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 193 | kfree(magellan); |
| 194 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | /* |
| 198 | * The serio driver structure. |
| 199 | */ |
| 200 | |
| 201 | static struct serio_device_id magellan_serio_ids[] = { |
| 202 | { |
| 203 | .type = SERIO_RS232, |
| 204 | .proto = SERIO_MAGELLAN, |
| 205 | .id = SERIO_ANY, |
| 206 | .extra = SERIO_ANY, |
| 207 | }, |
| 208 | { 0 } |
| 209 | }; |
| 210 | |
| 211 | MODULE_DEVICE_TABLE(serio, magellan_serio_ids); |
| 212 | |
| 213 | static struct serio_driver magellan_drv = { |
| 214 | .driver = { |
| 215 | .name = "magellan", |
| 216 | }, |
| 217 | .description = DRIVER_DESC, |
| 218 | .id_table = magellan_serio_ids, |
| 219 | .interrupt = magellan_interrupt, |
| 220 | .connect = magellan_connect, |
| 221 | .disconnect = magellan_disconnect, |
| 222 | }; |
| 223 | |
Axel Lin | 65ac9f7 | 2012-04-03 23:50:17 -0700 | [diff] [blame] | 224 | module_serio_driver(magellan_drv); |