Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * $Id: guillemot.c,v 1.10 2002/01/22 20:28:12 vojtech Exp $ |
| 3 | * |
| 4 | * Copyright (c) 2001 Vojtech Pavlik |
| 5 | */ |
| 6 | |
| 7 | /* |
| 8 | * Guillemot Digital Interface Protocol 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/slab.h> |
| 33 | #include <linux/module.h> |
| 34 | #include <linux/delay.h> |
| 35 | #include <linux/init.h> |
| 36 | #include <linux/gameport.h> |
| 37 | #include <linux/input.h> |
Tim Schmielau | 4e57b68 | 2005-10-30 15:03:48 -0800 | [diff] [blame] | 38 | #include <linux/jiffies.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | |
| 40 | #define DRIVER_DESC "Guillemot Digital joystick driver" |
| 41 | |
| 42 | MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>"); |
| 43 | MODULE_DESCRIPTION(DRIVER_DESC); |
| 44 | MODULE_LICENSE("GPL"); |
| 45 | |
| 46 | #define GUILLEMOT_MAX_START 600 /* 600 us */ |
| 47 | #define GUILLEMOT_MAX_STROBE 60 /* 60 us */ |
| 48 | #define GUILLEMOT_MAX_LENGTH 17 /* 17 bytes */ |
| 49 | |
| 50 | static short guillemot_abs_pad[] = |
| 51 | { ABS_X, ABS_Y, ABS_THROTTLE, ABS_RUDDER, -1 }; |
| 52 | |
| 53 | static short guillemot_btn_pad[] = |
| 54 | { BTN_A, BTN_B, BTN_C, BTN_X, BTN_Y, BTN_Z, BTN_TL, BTN_TR, BTN_MODE, BTN_SELECT, -1 }; |
| 55 | |
| 56 | static struct { |
| 57 | int x; |
| 58 | int y; |
| 59 | } guillemot_hat_to_axis[16] = {{ 0,-1}, { 1,-1}, { 1, 0}, { 1, 1}, { 0, 1}, {-1, 1}, {-1, 0}, {-1,-1}}; |
| 60 | |
| 61 | struct guillemot_type { |
| 62 | unsigned char id; |
| 63 | short *abs; |
| 64 | short *btn; |
| 65 | int hat; |
| 66 | char *name; |
| 67 | }; |
| 68 | |
| 69 | struct guillemot { |
| 70 | struct gameport *gameport; |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 71 | struct input_dev *dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | int bads; |
| 73 | int reads; |
| 74 | struct guillemot_type *type; |
| 75 | unsigned char length; |
| 76 | char phys[32]; |
| 77 | }; |
| 78 | |
| 79 | static struct guillemot_type guillemot_type[] = { |
| 80 | { 0x00, guillemot_abs_pad, guillemot_btn_pad, 1, "Guillemot Pad" }, |
| 81 | { 0 }}; |
| 82 | |
| 83 | /* |
| 84 | * guillemot_read_packet() reads Guillemot joystick data. |
| 85 | */ |
| 86 | |
| 87 | static int guillemot_read_packet(struct gameport *gameport, u8 *data) |
| 88 | { |
| 89 | unsigned long flags; |
| 90 | unsigned char u, v; |
| 91 | unsigned int t, s; |
| 92 | int i; |
| 93 | |
| 94 | for (i = 0; i < GUILLEMOT_MAX_LENGTH; i++) |
| 95 | data[i] = 0; |
| 96 | |
| 97 | i = 0; |
| 98 | t = gameport_time(gameport, GUILLEMOT_MAX_START); |
| 99 | s = gameport_time(gameport, GUILLEMOT_MAX_STROBE); |
| 100 | |
| 101 | local_irq_save(flags); |
| 102 | gameport_trigger(gameport); |
| 103 | v = gameport_read(gameport); |
| 104 | |
| 105 | while (t > 0 && i < GUILLEMOT_MAX_LENGTH * 8) { |
| 106 | t--; |
| 107 | u = v; v = gameport_read(gameport); |
| 108 | if (v & ~u & 0x10) { |
| 109 | data[i >> 3] |= ((v >> 5) & 1) << (i & 7); |
| 110 | i++; |
| 111 | t = s; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | local_irq_restore(flags); |
| 116 | |
| 117 | return i; |
| 118 | } |
| 119 | |
| 120 | /* |
| 121 | * guillemot_poll() reads and analyzes Guillemot joystick data. |
| 122 | */ |
| 123 | |
| 124 | static void guillemot_poll(struct gameport *gameport) |
| 125 | { |
| 126 | struct guillemot *guillemot = gameport_get_drvdata(gameport); |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 127 | struct input_dev *dev = guillemot->dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | u8 data[GUILLEMOT_MAX_LENGTH]; |
| 129 | int i; |
| 130 | |
| 131 | guillemot->reads++; |
| 132 | |
| 133 | if (guillemot_read_packet(guillemot->gameport, data) != GUILLEMOT_MAX_LENGTH * 8 || |
| 134 | data[0] != 0x55 || data[16] != 0xaa) { |
| 135 | guillemot->bads++; |
| 136 | } else { |
| 137 | |
| 138 | for (i = 0; i < 6 && guillemot->type->abs[i] >= 0; i++) |
| 139 | input_report_abs(dev, guillemot->type->abs[i], data[i + 5]); |
| 140 | |
| 141 | if (guillemot->type->hat) { |
| 142 | input_report_abs(dev, ABS_HAT0X, guillemot_hat_to_axis[data[4] >> 4].x); |
| 143 | input_report_abs(dev, ABS_HAT0Y, guillemot_hat_to_axis[data[4] >> 4].y); |
| 144 | } |
| 145 | |
| 146 | for (i = 0; i < 16 && guillemot->type->btn[i] >= 0; i++) |
| 147 | input_report_key(dev, guillemot->type->btn[i], (data[2 + (i >> 3)] >> (i & 7)) & 1); |
| 148 | } |
| 149 | |
| 150 | input_sync(dev); |
| 151 | } |
| 152 | |
| 153 | /* |
| 154 | * guillemot_open() is a callback from the input open routine. |
| 155 | */ |
| 156 | |
| 157 | static int guillemot_open(struct input_dev *dev) |
| 158 | { |
Dmitry Torokhov | 8715c1c | 2007-04-12 01:34:14 -0400 | [diff] [blame] | 159 | struct guillemot *guillemot = input_get_drvdata(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | |
| 161 | gameport_start_polling(guillemot->gameport); |
| 162 | return 0; |
| 163 | } |
| 164 | |
| 165 | /* |
| 166 | * guillemot_close() is a callback from the input close routine. |
| 167 | */ |
| 168 | |
| 169 | static void guillemot_close(struct input_dev *dev) |
| 170 | { |
Dmitry Torokhov | 8715c1c | 2007-04-12 01:34:14 -0400 | [diff] [blame] | 171 | struct guillemot *guillemot = input_get_drvdata(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | |
| 173 | gameport_stop_polling(guillemot->gameport); |
| 174 | } |
| 175 | |
| 176 | /* |
| 177 | * guillemot_connect() probes for Guillemot joysticks. |
| 178 | */ |
| 179 | |
| 180 | static int guillemot_connect(struct gameport *gameport, struct gameport_driver *drv) |
| 181 | { |
| 182 | struct guillemot *guillemot; |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 183 | struct input_dev *input_dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | u8 data[GUILLEMOT_MAX_LENGTH]; |
| 185 | int i, t; |
| 186 | int err; |
| 187 | |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 188 | guillemot = kzalloc(sizeof(struct guillemot), GFP_KERNEL); |
| 189 | input_dev = input_allocate_device(); |
| 190 | if (!guillemot || !input_dev) { |
| 191 | err = -ENOMEM; |
| 192 | goto fail1; |
| 193 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | |
| 195 | guillemot->gameport = gameport; |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 196 | guillemot->dev = input_dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | |
| 198 | gameport_set_drvdata(gameport, guillemot); |
| 199 | |
| 200 | err = gameport_open(gameport, drv, GAMEPORT_MODE_RAW); |
| 201 | if (err) |
| 202 | goto fail1; |
| 203 | |
| 204 | i = guillemot_read_packet(gameport, data); |
| 205 | |
| 206 | if (i != GUILLEMOT_MAX_LENGTH * 8 || data[0] != 0x55 || data[16] != 0xaa) { |
| 207 | err = -ENODEV; |
| 208 | goto fail2; |
| 209 | } |
| 210 | |
| 211 | for (i = 0; guillemot_type[i].name; i++) |
| 212 | if (guillemot_type[i].id == data[11]) |
| 213 | break; |
| 214 | |
| 215 | if (!guillemot_type[i].name) { |
| 216 | printk(KERN_WARNING "guillemot.c: Unknown joystick on %s. [ %02x%02x:%04x, ver %d.%02d ]\n", |
| 217 | gameport->phys, data[12], data[13], data[11], data[14], data[15]); |
| 218 | err = -ENODEV; |
| 219 | goto fail2; |
| 220 | } |
| 221 | |
| 222 | gameport_set_poll_handler(gameport, guillemot_poll); |
| 223 | gameport_set_poll_interval(gameport, 20); |
| 224 | |
Dmitry Torokhov | 10ca4c0 | 2006-06-26 01:45:48 -0400 | [diff] [blame] | 225 | snprintf(guillemot->phys, sizeof(guillemot->phys), "%s/input0", gameport->phys); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | guillemot->type = guillemot_type + i; |
| 227 | |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 228 | input_dev->name = guillemot_type[i].name; |
| 229 | input_dev->phys = guillemot->phys; |
| 230 | input_dev->id.bustype = BUS_GAMEPORT; |
| 231 | input_dev->id.vendor = GAMEPORT_ID_VENDOR_GUILLEMOT; |
| 232 | input_dev->id.product = guillemot_type[i].id; |
| 233 | input_dev->id.version = (int)data[14] << 8 | data[15]; |
Dmitry Torokhov | 935e658 | 2007-04-12 01:35:26 -0400 | [diff] [blame] | 234 | input_dev->dev.parent = &gameport->dev; |
Dmitry Torokhov | 8715c1c | 2007-04-12 01:34:14 -0400 | [diff] [blame] | 235 | |
| 236 | input_set_drvdata(input_dev, guillemot); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 238 | input_dev->open = guillemot_open; |
| 239 | input_dev->close = guillemot_close; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | |
Jiri Slaby | 7b19ada | 2007-10-18 23:40:32 -0700 | [diff] [blame^] | 241 | input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | |
| 243 | for (i = 0; (t = guillemot->type->abs[i]) >= 0; i++) |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 244 | input_set_abs_params(input_dev, t, 0, 255, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | |
| 246 | if (guillemot->type->hat) { |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 247 | input_set_abs_params(input_dev, ABS_HAT0X, -1, 1, 0, 0); |
| 248 | input_set_abs_params(input_dev, ABS_HAT0Y, -1, 1, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | for (i = 0; (t = guillemot->type->btn[i]) >= 0; i++) |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 252 | set_bit(t, input_dev->keybit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | |
Dmitry Torokhov | 127278c | 2006-11-05 22:40:09 -0500 | [diff] [blame] | 254 | err = input_register_device(guillemot->dev); |
| 255 | if (err) |
| 256 | goto fail2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | |
| 258 | return 0; |
| 259 | |
| 260 | fail2: gameport_close(gameport); |
| 261 | fail1: gameport_set_drvdata(gameport, NULL); |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 262 | input_free_device(input_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | kfree(guillemot); |
| 264 | return err; |
| 265 | } |
| 266 | |
| 267 | static void guillemot_disconnect(struct gameport *gameport) |
| 268 | { |
| 269 | struct guillemot *guillemot = gameport_get_drvdata(gameport); |
| 270 | |
| 271 | printk(KERN_INFO "guillemot.c: Failed %d reads out of %d on %s\n", guillemot->reads, guillemot->bads, guillemot->phys); |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 272 | input_unregister_device(guillemot->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | gameport_close(gameport); |
| 274 | kfree(guillemot); |
| 275 | } |
| 276 | |
| 277 | static struct gameport_driver guillemot_drv = { |
| 278 | .driver = { |
| 279 | .name = "guillemot", |
| 280 | }, |
| 281 | .description = DRIVER_DESC, |
| 282 | .connect = guillemot_connect, |
| 283 | .disconnect = guillemot_disconnect, |
| 284 | }; |
| 285 | |
| 286 | static int __init guillemot_init(void) |
| 287 | { |
| 288 | gameport_register_driver(&guillemot_drv); |
| 289 | return 0; |
| 290 | } |
| 291 | |
| 292 | static void __exit guillemot_exit(void) |
| 293 | { |
| 294 | gameport_unregister_driver(&guillemot_drv); |
| 295 | } |
| 296 | |
| 297 | module_init(guillemot_init); |
| 298 | module_exit(guillemot_exit); |