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