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 | * Logitech WingMan Warrior joystick driver for Linux |
| 7 | */ |
| 8 | |
| 9 | /* |
| 10 | * This program is free warftware; 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 "Logitech WingMan Warrior joystick driver" |
| 36 | |
| 37 | MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>"); |
| 38 | MODULE_DESCRIPTION(DRIVER_DESC); |
| 39 | MODULE_LICENSE("GPL"); |
| 40 | |
| 41 | /* |
| 42 | * Constants. |
| 43 | */ |
| 44 | |
| 45 | #define WARRIOR_MAX_LENGTH 16 |
| 46 | static char warrior_lengths[] = { 0, 4, 12, 3, 4, 4, 0, 0 }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | |
| 48 | /* |
| 49 | * Per-Warrior data. |
| 50 | */ |
| 51 | |
| 52 | struct warrior { |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 53 | struct input_dev *dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | int idx, len; |
| 55 | unsigned char data[WARRIOR_MAX_LENGTH]; |
| 56 | char phys[32]; |
| 57 | }; |
| 58 | |
| 59 | /* |
| 60 | * warrior_process_packet() decodes packets the driver receives from the |
| 61 | * Warrior. It updates the data accordingly. |
| 62 | */ |
| 63 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 64 | static void warrior_process_packet(struct warrior *warrior) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | { |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 66 | struct input_dev *dev = warrior->dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | unsigned char *data = warrior->data; |
| 68 | |
| 69 | if (!warrior->idx) return; |
| 70 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | switch ((data[0] >> 4) & 7) { |
| 72 | case 1: /* Button data */ |
| 73 | input_report_key(dev, BTN_TRIGGER, data[3] & 1); |
| 74 | input_report_key(dev, BTN_THUMB, (data[3] >> 1) & 1); |
| 75 | input_report_key(dev, BTN_TOP, (data[3] >> 2) & 1); |
| 76 | input_report_key(dev, BTN_TOP2, (data[3] >> 3) & 1); |
| 77 | break; |
| 78 | case 3: /* XY-axis info->data */ |
| 79 | input_report_abs(dev, ABS_X, ((data[0] & 8) << 5) - (data[2] | ((data[0] & 4) << 5))); |
| 80 | input_report_abs(dev, ABS_Y, (data[1] | ((data[0] & 1) << 7)) - ((data[0] & 2) << 7)); |
| 81 | break; |
| 82 | case 5: /* Throttle, spinner, hat info->data */ |
| 83 | input_report_abs(dev, ABS_THROTTLE, (data[1] | ((data[0] & 1) << 7)) - ((data[0] & 2) << 7)); |
| 84 | input_report_abs(dev, ABS_HAT0X, (data[3] & 2 ? 1 : 0) - (data[3] & 1 ? 1 : 0)); |
| 85 | input_report_abs(dev, ABS_HAT0Y, (data[3] & 8 ? 1 : 0) - (data[3] & 4 ? 1 : 0)); |
| 86 | input_report_rel(dev, REL_DIAL, (data[2] | ((data[0] & 4) << 5)) - ((data[0] & 8) << 5)); |
| 87 | break; |
| 88 | } |
| 89 | input_sync(dev); |
| 90 | } |
| 91 | |
| 92 | /* |
| 93 | * warrior_interrupt() is called by the low level driver when characters |
| 94 | * are ready for us. We then buffer them for further processing, or call the |
| 95 | * packet processing routine. |
| 96 | */ |
| 97 | |
| 98 | static irqreturn_t warrior_interrupt(struct serio *serio, |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 99 | unsigned char data, unsigned int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | { |
| 101 | struct warrior *warrior = serio_get_drvdata(serio); |
| 102 | |
| 103 | if (data & 0x80) { |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 104 | if (warrior->idx) warrior_process_packet(warrior); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | warrior->idx = 0; |
| 106 | warrior->len = warrior_lengths[(data >> 4) & 7]; |
| 107 | } |
| 108 | |
| 109 | if (warrior->idx < warrior->len) |
| 110 | warrior->data[warrior->idx++] = data; |
| 111 | |
| 112 | if (warrior->idx == warrior->len) { |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 113 | if (warrior->idx) warrior_process_packet(warrior); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | warrior->idx = 0; |
| 115 | warrior->len = 0; |
| 116 | } |
| 117 | return IRQ_HANDLED; |
| 118 | } |
| 119 | |
| 120 | /* |
| 121 | * warrior_disconnect() is the opposite of warrior_connect() |
| 122 | */ |
| 123 | |
| 124 | static void warrior_disconnect(struct serio *serio) |
| 125 | { |
| 126 | struct warrior *warrior = serio_get_drvdata(serio); |
| 127 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | serio_close(serio); |
| 129 | serio_set_drvdata(serio, NULL); |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 130 | input_unregister_device(warrior->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | kfree(warrior); |
| 132 | } |
| 133 | |
| 134 | /* |
| 135 | * warrior_connect() is the routine that is called when someone adds a |
| 136 | * new serio device. It looks for the Warrior, and if found, registers |
| 137 | * it as an input device. |
| 138 | */ |
| 139 | |
| 140 | static int warrior_connect(struct serio *serio, struct serio_driver *drv) |
| 141 | { |
| 142 | struct warrior *warrior; |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 143 | struct input_dev *input_dev; |
| 144 | int err = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 146 | warrior = kzalloc(sizeof(struct warrior), GFP_KERNEL); |
| 147 | input_dev = input_allocate_device(); |
| 148 | if (!warrior || !input_dev) |
Dmitry Torokhov | 127278c | 2006-11-05 22:40:09 -0500 | [diff] [blame] | 149 | goto fail1; |
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 | warrior->dev = input_dev; |
Dmitry Torokhov | 10ca4c0 | 2006-06-26 01:45:48 -0400 | [diff] [blame] | 152 | snprintf(warrior->phys, sizeof(warrior->phys), "%s/input0", serio->phys); |
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 | input_dev->name = "Logitech WingMan Warrior"; |
| 155 | input_dev->phys = warrior->phys; |
| 156 | input_dev->id.bustype = BUS_RS232; |
| 157 | input_dev->id.vendor = SERIO_WARRIOR; |
| 158 | input_dev->id.product = 0x0001; |
| 159 | input_dev->id.version = 0x0100; |
Dmitry Torokhov | 935e658 | 2007-04-12 01:35:26 -0400 | [diff] [blame] | 160 | input_dev->dev.parent = &serio->dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | |
Jiri Slaby | 7b19ada | 2007-10-18 23:40:32 -0700 | [diff] [blame] | 162 | input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL) | |
| 163 | BIT_MASK(EV_ABS); |
| 164 | input_dev->keybit[BIT_WORD(BTN_TRIGGER)] = BIT_MASK(BTN_TRIGGER) | |
| 165 | BIT_MASK(BTN_THUMB) | BIT_MASK(BTN_TOP) | BIT_MASK(BTN_TOP2); |
| 166 | input_dev->relbit[0] = BIT_MASK(REL_DIAL); |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 167 | input_set_abs_params(input_dev, ABS_X, -64, 64, 0, 8); |
| 168 | input_set_abs_params(input_dev, ABS_Y, -64, 64, 0, 8); |
| 169 | input_set_abs_params(input_dev, ABS_THROTTLE, -112, 112, 0, 0); |
| 170 | input_set_abs_params(input_dev, ABS_HAT0X, -1, 1, 0, 0); |
Dmitry Torokhov | ae5536d | 2005-12-29 22:19:08 -0500 | [diff] [blame] | 171 | input_set_abs_params(input_dev, ABS_HAT0Y, -1, 1, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | |
| 173 | serio_set_drvdata(serio, warrior); |
| 174 | |
| 175 | err = serio_open(serio, drv); |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 176 | if (err) |
Dmitry Torokhov | 127278c | 2006-11-05 22:40:09 -0500 | [diff] [blame] | 177 | goto fail2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | |
Dmitry Torokhov | 127278c | 2006-11-05 22:40:09 -0500 | [diff] [blame] | 179 | err = input_register_device(warrior->dev); |
| 180 | if (err) |
| 181 | goto fail3; |
| 182 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | return 0; |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 184 | |
Dmitry Torokhov | 127278c | 2006-11-05 22:40:09 -0500 | [diff] [blame] | 185 | fail3: serio_close(serio); |
| 186 | fail2: serio_set_drvdata(serio, NULL); |
| 187 | fail1: input_free_device(input_dev); |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 188 | kfree(warrior); |
| 189 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | /* |
| 193 | * The serio driver structure. |
| 194 | */ |
| 195 | |
| 196 | static struct serio_device_id warrior_serio_ids[] = { |
| 197 | { |
| 198 | .type = SERIO_RS232, |
| 199 | .proto = SERIO_WARRIOR, |
| 200 | .id = SERIO_ANY, |
| 201 | .extra = SERIO_ANY, |
| 202 | }, |
| 203 | { 0 } |
| 204 | }; |
| 205 | |
| 206 | MODULE_DEVICE_TABLE(serio, warrior_serio_ids); |
| 207 | |
| 208 | static struct serio_driver warrior_drv = { |
| 209 | .driver = { |
| 210 | .name = "warrior", |
| 211 | }, |
| 212 | .description = DRIVER_DESC, |
| 213 | .id_table = warrior_serio_ids, |
| 214 | .interrupt = warrior_interrupt, |
| 215 | .connect = warrior_connect, |
| 216 | .disconnect = warrior_disconnect, |
| 217 | }; |
| 218 | |
Axel Lin | 65ac9f7 | 2012-04-03 23:50:17 -0700 | [diff] [blame] | 219 | module_serio_driver(warrior_drv); |