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