Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * $Id: twidjoy.c,v 1.5 2002/01/22 20:31:53 vojtech Exp $ |
| 3 | * |
| 4 | * derived from CVS-ID "stinger.c,v 1.5 2001/05/29 12:57:18 vojtech Exp" |
| 5 | * |
| 6 | * Copyright (c) 2001 Arndt Schoenewald |
| 7 | * Copyright (c) 2000-2001 Vojtech Pavlik |
| 8 | * Copyright (c) 2000 Mark Fletcher |
| 9 | * |
| 10 | * Sponsored by Quelltext AG (http://www.quelltext-ag.de), Dortmund, Germany |
| 11 | */ |
| 12 | |
| 13 | /* |
| 14 | * Driver to use Handykey's Twiddler (the first edition, i.e. the one with |
| 15 | * the RS232 interface) as a joystick under Linux |
| 16 | * |
| 17 | * The Twiddler is a one-handed chording keyboard featuring twelve buttons on |
| 18 | * the front, six buttons on the top, and a built-in tilt sensor. The buttons |
| 19 | * on the front, which are grouped as four rows of three buttons, are pressed |
| 20 | * by the four fingers (this implies only one button per row can be held down |
| 21 | * at the same time) and the buttons on the top are for the thumb. The tilt |
| 22 | * sensor delivers X and Y axis data depending on how the Twiddler is held. |
| 23 | * Additional information can be found at http://www.handykey.com. |
| 24 | * |
| 25 | * This driver does not use the Twiddler for its intended purpose, i.e. as |
| 26 | * a chording keyboard, but as a joystick: pressing and releasing a button |
| 27 | * immediately sends a corresponding button event, and tilting it generates |
| 28 | * corresponding ABS_X and ABS_Y events. This turns the Twiddler into a game |
| 29 | * controller with amazing 18 buttons :-) |
| 30 | * |
| 31 | * Note: The Twiddler2 (the successor of the Twiddler that connects directly |
| 32 | * to the PS/2 keyboard and mouse ports) is NOT supported by this driver! |
| 33 | * |
| 34 | * For questions or feedback regarding this driver module please contact: |
| 35 | * Arndt Schoenewald <arndt@quelltext.com> |
| 36 | */ |
| 37 | |
| 38 | /* |
| 39 | * This program is free software; you can redistribute it and/or modify |
| 40 | * it under the terms of the GNU General Public License as published by |
| 41 | * the Free Software Foundation; either version 2 of the License, or |
| 42 | * (at your option) any later version. |
| 43 | * |
| 44 | * This program is distributed in the hope that it will be useful, |
| 45 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 46 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 47 | * GNU General Public License for more details. |
| 48 | * |
| 49 | * You should have received a copy of the GNU General Public License |
| 50 | * along with this program; if not, write to the Free Software |
| 51 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 52 | */ |
| 53 | |
| 54 | #include <linux/kernel.h> |
| 55 | #include <linux/module.h> |
| 56 | #include <linux/slab.h> |
| 57 | #include <linux/input.h> |
| 58 | #include <linux/serio.h> |
| 59 | #include <linux/init.h> |
| 60 | |
| 61 | #define DRIVER_DESC "Handykey Twiddler keyboard as a joystick driver" |
| 62 | |
| 63 | MODULE_DESCRIPTION(DRIVER_DESC); |
| 64 | MODULE_LICENSE("GPL"); |
| 65 | |
| 66 | /* |
| 67 | * Constants. |
| 68 | */ |
| 69 | |
| 70 | #define TWIDJOY_MAX_LENGTH 5 |
| 71 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | static struct twidjoy_button_spec { |
| 73 | int bitshift; |
| 74 | int bitmask; |
| 75 | int buttons[3]; |
| 76 | } |
| 77 | twidjoy_buttons[] = { |
| 78 | { 0, 3, { BTN_A, BTN_B, BTN_C } }, |
| 79 | { 2, 3, { BTN_X, BTN_Y, BTN_Z } }, |
| 80 | { 4, 3, { BTN_TL, BTN_TR, BTN_TR2 } }, |
| 81 | { 6, 3, { BTN_SELECT, BTN_START, BTN_MODE } }, |
| 82 | { 8, 1, { BTN_BASE5 } }, |
| 83 | { 9, 1, { BTN_BASE } }, |
| 84 | { 10, 1, { BTN_BASE3 } }, |
| 85 | { 11, 1, { BTN_BASE4 } }, |
| 86 | { 12, 1, { BTN_BASE2 } }, |
| 87 | { 13, 1, { BTN_BASE6 } }, |
| 88 | { 0, 0, { 0 } } |
| 89 | }; |
| 90 | |
| 91 | /* |
| 92 | * Per-Twiddler data. |
| 93 | */ |
| 94 | |
| 95 | struct twidjoy { |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 96 | struct input_dev *dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | int idx; |
| 98 | unsigned char data[TWIDJOY_MAX_LENGTH]; |
| 99 | char phys[32]; |
| 100 | }; |
| 101 | |
| 102 | /* |
| 103 | * twidjoy_process_packet() decodes packets the driver receives from the |
| 104 | * Twiddler. It updates the data accordingly. |
| 105 | */ |
| 106 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 107 | static void twidjoy_process_packet(struct twidjoy *twidjoy) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | { |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 109 | struct input_dev *dev = twidjoy->dev; |
| 110 | unsigned char *data = twidjoy->data; |
| 111 | struct twidjoy_button_spec *bp; |
| 112 | int button_bits, abs_x, abs_y; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 114 | button_bits = ((data[1] & 0x7f) << 7) | (data[0] & 0x7f); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 116 | for (bp = twidjoy_buttons; bp->bitmask; bp++) { |
| 117 | int value = (button_bits & (bp->bitmask << bp->bitshift)) >> bp->bitshift; |
| 118 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 120 | for (i = 0; i < bp->bitmask; i++) |
| 121 | input_report_key(dev, bp->buttons[i], i+1 == value); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | } |
| 123 | |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 124 | abs_x = ((data[4] & 0x07) << 5) | ((data[3] & 0x7C) >> 2); |
| 125 | if (data[4] & 0x08) abs_x -= 256; |
| 126 | |
| 127 | abs_y = ((data[3] & 0x01) << 7) | ((data[2] & 0x7F) >> 0); |
| 128 | if (data[3] & 0x02) abs_y -= 256; |
| 129 | |
| 130 | input_report_abs(dev, ABS_X, -abs_x); |
| 131 | input_report_abs(dev, ABS_Y, +abs_y); |
| 132 | |
| 133 | input_sync(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | /* |
| 137 | * twidjoy_interrupt() is called by the low level driver when characters |
| 138 | * are ready for us. We then buffer them for further processing, or call the |
| 139 | * packet processing routine. |
| 140 | */ |
| 141 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 142 | static irqreturn_t twidjoy_interrupt(struct serio *serio, unsigned char data, unsigned int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | { |
| 144 | struct twidjoy *twidjoy = serio_get_drvdata(serio); |
| 145 | |
| 146 | /* All Twiddler packets are 5 bytes. The fact that the first byte |
| 147 | * has a MSB of 0 and all other bytes have a MSB of 1 can be used |
| 148 | * to check and regain sync. */ |
| 149 | |
| 150 | if ((data & 0x80) == 0) |
| 151 | twidjoy->idx = 0; /* this byte starts a new packet */ |
| 152 | else if (twidjoy->idx == 0) |
| 153 | return IRQ_HANDLED; /* wrong MSB -- ignore this byte */ |
| 154 | |
| 155 | if (twidjoy->idx < TWIDJOY_MAX_LENGTH) |
| 156 | twidjoy->data[twidjoy->idx++] = data; |
| 157 | |
| 158 | if (twidjoy->idx == TWIDJOY_MAX_LENGTH) { |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 159 | twidjoy_process_packet(twidjoy); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | twidjoy->idx = 0; |
| 161 | } |
| 162 | |
| 163 | return IRQ_HANDLED; |
| 164 | } |
| 165 | |
| 166 | /* |
| 167 | * twidjoy_disconnect() is the opposite of twidjoy_connect() |
| 168 | */ |
| 169 | |
| 170 | static void twidjoy_disconnect(struct serio *serio) |
| 171 | { |
| 172 | struct twidjoy *twidjoy = serio_get_drvdata(serio); |
| 173 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | serio_close(serio); |
| 175 | serio_set_drvdata(serio, NULL); |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 176 | input_unregister_device(twidjoy->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | kfree(twidjoy); |
| 178 | } |
| 179 | |
| 180 | /* |
| 181 | * twidjoy_connect() is the routine that is called when someone adds a |
| 182 | * new serio device. It looks for the Twiddler, and if found, registers |
| 183 | * it as an input device. |
| 184 | */ |
| 185 | |
| 186 | static int twidjoy_connect(struct serio *serio, struct serio_driver *drv) |
| 187 | { |
| 188 | struct twidjoy_button_spec *bp; |
| 189 | struct twidjoy *twidjoy; |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 190 | struct input_dev *input_dev; |
| 191 | int err = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 194 | twidjoy = kzalloc(sizeof(struct twidjoy), GFP_KERNEL); |
| 195 | input_dev = input_allocate_device(); |
| 196 | if (!twidjoy || !input_dev) |
Dmitry Torokhov | 127278c | 2006-11-05 22:40:09 -0500 | [diff] [blame] | 197 | goto fail1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 199 | twidjoy->dev = input_dev; |
Dmitry Torokhov | 10ca4c0 | 2006-06-26 01:45:48 -0400 | [diff] [blame] | 200 | snprintf(twidjoy->phys, sizeof(twidjoy->phys), "%s/input0", serio->phys); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 202 | input_dev->name = "Handykey Twiddler"; |
| 203 | input_dev->phys = twidjoy->phys; |
| 204 | input_dev->id.bustype = BUS_RS232; |
| 205 | input_dev->id.vendor = SERIO_TWIDJOY; |
| 206 | input_dev->id.product = 0x0001; |
| 207 | input_dev->id.version = 0x0100; |
| 208 | input_dev->cdev.dev = &serio->dev; |
| 209 | input_dev->private = twidjoy; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 211 | input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_ABS); |
| 212 | input_dev->absbit[0] = BIT(ABS_X) | BIT(ABS_Y); |
| 213 | input_set_abs_params(input_dev, ABS_X, -50, 50, 4, 4); |
| 214 | input_set_abs_params(input_dev, ABS_Y, -50, 50, 4, 4); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 216 | for (bp = twidjoy_buttons; bp->bitmask; bp++) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | for (i = 0; i < bp->bitmask; i++) |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 218 | set_bit(bp->buttons[i], input_dev->keybit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | |
| 220 | serio_set_drvdata(serio, twidjoy); |
| 221 | |
| 222 | err = serio_open(serio, drv); |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 223 | if (err) |
Dmitry Torokhov | 127278c | 2006-11-05 22:40:09 -0500 | [diff] [blame] | 224 | goto fail2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | |
Dmitry Torokhov | 127278c | 2006-11-05 22:40:09 -0500 | [diff] [blame] | 226 | err = input_register_device(twidjoy->dev); |
| 227 | if (err) |
| 228 | goto fail3; |
| 229 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | return 0; |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 231 | |
Dmitry Torokhov | 127278c | 2006-11-05 22:40:09 -0500 | [diff] [blame] | 232 | fail3: serio_close(serio); |
| 233 | fail2: serio_set_drvdata(serio, NULL); |
| 234 | fail1: input_free_device(input_dev); |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 235 | kfree(twidjoy); |
| 236 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | /* |
| 240 | * The serio driver structure. |
| 241 | */ |
| 242 | |
| 243 | static struct serio_device_id twidjoy_serio_ids[] = { |
| 244 | { |
| 245 | .type = SERIO_RS232, |
| 246 | .proto = SERIO_TWIDJOY, |
| 247 | .id = SERIO_ANY, |
| 248 | .extra = SERIO_ANY, |
| 249 | }, |
| 250 | { 0 } |
| 251 | }; |
| 252 | |
| 253 | MODULE_DEVICE_TABLE(serio, twidjoy_serio_ids); |
| 254 | |
| 255 | static struct serio_driver twidjoy_drv = { |
| 256 | .driver = { |
| 257 | .name = "twidjoy", |
| 258 | }, |
| 259 | .description = DRIVER_DESC, |
| 260 | .id_table = twidjoy_serio_ids, |
| 261 | .interrupt = twidjoy_interrupt, |
| 262 | .connect = twidjoy_connect, |
| 263 | .disconnect = twidjoy_disconnect, |
| 264 | }; |
| 265 | |
| 266 | /* |
| 267 | * The functions for inserting/removing us as a module. |
| 268 | */ |
| 269 | |
Adrian Bunk | ffc6b529 | 2006-01-29 21:51:07 -0500 | [diff] [blame] | 270 | static int __init twidjoy_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | { |
Akinobu Mita | 153a9df0 | 2006-11-23 23:35:10 -0500 | [diff] [blame^] | 272 | return serio_register_driver(&twidjoy_drv); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | } |
| 274 | |
Adrian Bunk | ffc6b529 | 2006-01-29 21:51:07 -0500 | [diff] [blame] | 275 | static void __exit twidjoy_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | { |
| 277 | serio_unregister_driver(&twidjoy_drv); |
| 278 | } |
| 279 | |
| 280 | module_init(twidjoy_init); |
| 281 | module_exit(twidjoy_exit); |