Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * $Id: stinger.c,v 1.10 2002/01/22 20:29:31 vojtech Exp $ |
| 3 | * |
| 4 | * Copyright (c) 2000-2001 Vojtech Pavlik |
| 5 | * Copyright (c) 2000 Mark Fletcher |
| 6 | */ |
| 7 | |
| 8 | /* |
| 9 | * Gravis Stinger gamepad driver for Linux |
| 10 | */ |
| 11 | |
| 12 | /* |
| 13 | * This program is free warftware; you can redistribute it and/or modify |
| 14 | * it under the terms of the GNU General Public License as published by |
| 15 | * the Free Software Foundation; either version 2 of the License, or |
| 16 | * (at your option) any later version. |
| 17 | * |
| 18 | * This program is distributed in the hope that it will be useful, |
| 19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 | * GNU General Public License for more details. |
| 22 | * |
| 23 | * You should have received a copy of the GNU General Public License |
| 24 | * along with this program; if not, write to the Free Software |
| 25 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 26 | * |
| 27 | * Should you need to contact me, the author, you can do so either by |
| 28 | * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail: |
| 29 | * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic |
| 30 | */ |
| 31 | |
| 32 | #include <linux/kernel.h> |
| 33 | #include <linux/module.h> |
| 34 | #include <linux/slab.h> |
| 35 | #include <linux/input.h> |
| 36 | #include <linux/serio.h> |
| 37 | #include <linux/init.h> |
| 38 | |
| 39 | #define DRIVER_DESC "Gravis Stinger gamepad driver" |
| 40 | |
| 41 | MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>"); |
| 42 | MODULE_DESCRIPTION(DRIVER_DESC); |
| 43 | MODULE_LICENSE("GPL"); |
| 44 | |
| 45 | /* |
| 46 | * Constants. |
| 47 | */ |
| 48 | |
| 49 | #define STINGER_MAX_LENGTH 8 |
| 50 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | /* |
| 52 | * Per-Stinger data. |
| 53 | */ |
| 54 | |
| 55 | struct stinger { |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 56 | struct input_dev *dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | int idx; |
| 58 | unsigned char data[STINGER_MAX_LENGTH]; |
| 59 | char phys[32]; |
| 60 | }; |
| 61 | |
| 62 | /* |
| 63 | * stinger_process_packet() decodes packets the driver receives from the |
| 64 | * Stinger. It updates the data accordingly. |
| 65 | */ |
| 66 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 67 | static void stinger_process_packet(struct stinger *stinger) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | { |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 69 | struct input_dev *dev = stinger->dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | unsigned char *data = stinger->data; |
| 71 | |
| 72 | if (!stinger->idx) return; |
| 73 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | input_report_key(dev, BTN_A, ((data[0] & 0x20) >> 5)); |
| 75 | input_report_key(dev, BTN_B, ((data[0] & 0x10) >> 4)); |
| 76 | input_report_key(dev, BTN_C, ((data[0] & 0x08) >> 3)); |
| 77 | input_report_key(dev, BTN_X, ((data[0] & 0x04) >> 2)); |
| 78 | input_report_key(dev, BTN_Y, ((data[3] & 0x20) >> 5)); |
| 79 | input_report_key(dev, BTN_Z, ((data[3] & 0x10) >> 4)); |
| 80 | input_report_key(dev, BTN_TL, ((data[3] & 0x08) >> 3)); |
| 81 | input_report_key(dev, BTN_TR, ((data[3] & 0x04) >> 2)); |
| 82 | input_report_key(dev, BTN_SELECT, ((data[3] & 0x02) >> 1)); |
| 83 | input_report_key(dev, BTN_START, (data[3] & 0x01)); |
| 84 | |
| 85 | input_report_abs(dev, ABS_X, (data[1] & 0x3F) - ((data[0] & 0x01) << 6)); |
| 86 | input_report_abs(dev, ABS_Y, ((data[0] & 0x02) << 5) - (data[2] & 0x3F)); |
| 87 | |
| 88 | input_sync(dev); |
| 89 | |
| 90 | return; |
| 91 | } |
| 92 | |
| 93 | /* |
| 94 | * stinger_interrupt() is called by the low level driver when characters |
| 95 | * are ready for us. We then buffer them for further processing, or call the |
| 96 | * packet processing routine. |
| 97 | */ |
| 98 | |
| 99 | static irqreturn_t stinger_interrupt(struct serio *serio, |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 100 | unsigned char data, unsigned int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | { |
| 102 | struct stinger *stinger = serio_get_drvdata(serio); |
| 103 | |
| 104 | /* All Stinger packets are 4 bytes */ |
| 105 | |
| 106 | if (stinger->idx < STINGER_MAX_LENGTH) |
| 107 | stinger->data[stinger->idx++] = data; |
| 108 | |
| 109 | if (stinger->idx == 4) { |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 110 | stinger_process_packet(stinger); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | stinger->idx = 0; |
| 112 | } |
| 113 | |
| 114 | return IRQ_HANDLED; |
| 115 | } |
| 116 | |
| 117 | /* |
| 118 | * stinger_disconnect() is the opposite of stinger_connect() |
| 119 | */ |
| 120 | |
| 121 | static void stinger_disconnect(struct serio *serio) |
| 122 | { |
| 123 | struct stinger *stinger = serio_get_drvdata(serio); |
| 124 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | serio_close(serio); |
| 126 | serio_set_drvdata(serio, NULL); |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 127 | input_unregister_device(stinger->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | kfree(stinger); |
| 129 | } |
| 130 | |
| 131 | /* |
| 132 | * stinger_connect() is the routine that is called when someone adds a |
| 133 | * new serio device that supports Stinger protocol and registers it as |
| 134 | * an input device. |
| 135 | */ |
| 136 | |
| 137 | static int stinger_connect(struct serio *serio, struct serio_driver *drv) |
| 138 | { |
| 139 | struct stinger *stinger; |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 140 | struct input_dev *input_dev; |
| 141 | int err = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 143 | stinger = kmalloc(sizeof(struct stinger), GFP_KERNEL); |
| 144 | input_dev = input_allocate_device(); |
| 145 | if (!stinger || !input_dev) |
Dmitry Torokhov | 127278c | 2006-11-05 22:40:09 -0500 | [diff] [blame] | 146 | goto fail1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 148 | stinger->dev = input_dev; |
Dmitry Torokhov | 10ca4c0 | 2006-06-26 01:45:48 -0400 | [diff] [blame] | 149 | snprintf(stinger->phys, sizeof(stinger->phys), "%s/serio0", serio->phys); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 151 | input_dev->name = "Gravis Stinger"; |
| 152 | input_dev->phys = stinger->phys; |
| 153 | input_dev->id.bustype = BUS_RS232; |
| 154 | input_dev->id.vendor = SERIO_STINGER; |
| 155 | input_dev->id.product = 0x0001; |
| 156 | input_dev->id.version = 0x0100; |
Dmitry Torokhov | 935e658 | 2007-04-12 01:35:26 -0400 | [diff] [blame] | 157 | input_dev->dev.parent = &serio->dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | |
Jiri Slaby | 7b19ada | 2007-10-18 23:40:32 -0700 | [diff] [blame] | 159 | input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS); |
| 160 | input_dev->keybit[BIT_WORD(BTN_A)] = BIT_MASK(BTN_A) | BIT_MASK(BTN_B) | |
| 161 | BIT_MASK(BTN_C) | BIT_MASK(BTN_X) | BIT_MASK(BTN_Y) | |
| 162 | BIT_MASK(BTN_Z) | BIT_MASK(BTN_TL) | BIT_MASK(BTN_TR) | |
| 163 | BIT_MASK(BTN_START) | BIT_MASK(BTN_SELECT); |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 164 | input_set_abs_params(input_dev, ABS_X, -64, 64, 0, 4); |
| 165 | input_set_abs_params(input_dev, ABS_Y, -64, 64, 0, 4); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | |
| 167 | serio_set_drvdata(serio, stinger); |
| 168 | |
| 169 | err = serio_open(serio, drv); |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 170 | if (err) |
Dmitry Torokhov | 127278c | 2006-11-05 22:40:09 -0500 | [diff] [blame] | 171 | goto fail2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | |
Dmitry Torokhov | 127278c | 2006-11-05 22:40:09 -0500 | [diff] [blame] | 173 | err = input_register_device(stinger->dev); |
| 174 | if (err) |
| 175 | goto fail3; |
| 176 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | return 0; |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 178 | |
Dmitry Torokhov | 127278c | 2006-11-05 22:40:09 -0500 | [diff] [blame] | 179 | fail3: serio_close(serio); |
| 180 | fail2: serio_set_drvdata(serio, NULL); |
| 181 | fail1: input_free_device(input_dev); |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 182 | kfree(stinger); |
| 183 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | /* |
| 187 | * The serio driver structure. |
| 188 | */ |
| 189 | |
| 190 | static struct serio_device_id stinger_serio_ids[] = { |
| 191 | { |
| 192 | .type = SERIO_RS232, |
| 193 | .proto = SERIO_STINGER, |
| 194 | .id = SERIO_ANY, |
| 195 | .extra = SERIO_ANY, |
| 196 | }, |
| 197 | { 0 } |
| 198 | }; |
| 199 | |
| 200 | MODULE_DEVICE_TABLE(serio, stinger_serio_ids); |
| 201 | |
| 202 | static struct serio_driver stinger_drv = { |
| 203 | .driver = { |
| 204 | .name = "stinger", |
| 205 | }, |
| 206 | .description = DRIVER_DESC, |
| 207 | .id_table = stinger_serio_ids, |
| 208 | .interrupt = stinger_interrupt, |
| 209 | .connect = stinger_connect, |
| 210 | .disconnect = stinger_disconnect, |
| 211 | }; |
| 212 | |
| 213 | /* |
| 214 | * The functions for inserting/removing us as a module. |
| 215 | */ |
| 216 | |
| 217 | static int __init stinger_init(void) |
| 218 | { |
Akinobu Mita | 153a9df0 | 2006-11-23 23:35:10 -0500 | [diff] [blame] | 219 | return serio_register_driver(&stinger_drv); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | static void __exit stinger_exit(void) |
| 223 | { |
| 224 | serio_unregister_driver(&stinger_drv); |
| 225 | } |
| 226 | |
| 227 | module_init(stinger_init); |
| 228 | module_exit(stinger_exit); |