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