| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* | 
 | 2 |  * $Id: cobra.c,v 1.19 2002/01/22 20:26:52 vojtech Exp $ | 
 | 3 |  * | 
 | 4 |  *  Copyright (c) 1999-2001 Vojtech Pavlik | 
 | 5 |  */ | 
 | 6 |  | 
 | 7 | /* | 
 | 8 |  * Creative Labs Blaster GamePad Cobra 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/module.h> | 
 | 33 | #include <linux/slab.h> | 
 | 34 | #include <linux/init.h> | 
 | 35 | #include <linux/gameport.h> | 
 | 36 | #include <linux/input.h> | 
| Tim Schmielau | 4e57b68 | 2005-10-30 15:03:48 -0800 | [diff] [blame] | 37 | #include <linux/jiffies.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 |  | 
 | 39 | #define DRIVER_DESC	"Creative Labs Blaster GamePad Cobra driver" | 
 | 40 |  | 
 | 41 | MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>"); | 
 | 42 | MODULE_DESCRIPTION(DRIVER_DESC); | 
 | 43 | MODULE_LICENSE("GPL"); | 
 | 44 |  | 
 | 45 | #define COBRA_MAX_STROBE	45	/* 45 us max wait for first strobe */ | 
 | 46 | #define COBRA_LENGTH		36 | 
 | 47 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | static int cobra_btn[] = { BTN_START, BTN_SELECT, BTN_TL, BTN_TR, BTN_X, BTN_Y, BTN_Z, BTN_A, BTN_B, BTN_C, BTN_TL2, BTN_TR2, 0 }; | 
 | 49 |  | 
 | 50 | struct cobra { | 
 | 51 | 	struct gameport *gameport; | 
| Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 52 | 	struct input_dev *dev[2]; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | 	int reads; | 
 | 54 | 	int bads; | 
 | 55 | 	unsigned char exists; | 
 | 56 | 	char phys[2][32]; | 
 | 57 | }; | 
 | 58 |  | 
 | 59 | static unsigned char cobra_read_packet(struct gameport *gameport, unsigned int *data) | 
 | 60 | { | 
 | 61 | 	unsigned long flags; | 
 | 62 | 	unsigned char u, v, w; | 
 | 63 | 	__u64 buf[2]; | 
 | 64 | 	int r[2], t[2]; | 
 | 65 | 	int i, j, ret; | 
 | 66 |  | 
 | 67 | 	int strobe = gameport_time(gameport, COBRA_MAX_STROBE); | 
 | 68 |  | 
 | 69 | 	for (i = 0; i < 2; i++) { | 
 | 70 | 		r[i] = buf[i] = 0; | 
 | 71 | 		t[i] = COBRA_MAX_STROBE; | 
 | 72 | 	} | 
 | 73 |  | 
 | 74 | 	local_irq_save(flags); | 
 | 75 |  | 
 | 76 | 	u = gameport_read(gameport); | 
 | 77 |  | 
 | 78 | 	do { | 
 | 79 | 		t[0]--; t[1]--; | 
 | 80 | 		v = gameport_read(gameport); | 
 | 81 | 		for (i = 0, w = u ^ v; i < 2 && w; i++, w >>= 2) | 
 | 82 | 			if (w & 0x30) { | 
 | 83 | 				if ((w & 0x30) < 0x30 && r[i] < COBRA_LENGTH && t[i] > 0) { | 
 | 84 | 					buf[i] |= (__u64)((w >> 5) & 1) << r[i]++; | 
 | 85 | 					t[i] = strobe; | 
 | 86 | 					u = v; | 
 | 87 | 				} else t[i] = 0; | 
 | 88 | 			} | 
 | 89 | 	} while (t[0] > 0 || t[1] > 0); | 
 | 90 |  | 
 | 91 | 	local_irq_restore(flags); | 
 | 92 |  | 
 | 93 | 	ret = 0; | 
 | 94 |  | 
 | 95 | 	for (i = 0; i < 2; i++) { | 
 | 96 |  | 
 | 97 | 		if (r[i] != COBRA_LENGTH) continue; | 
 | 98 |  | 
 | 99 | 		for (j = 0; j < COBRA_LENGTH && (buf[i] & 0x04104107f) ^ 0x041041040; j++) | 
 | 100 | 			buf[i] = (buf[i] >> 1) | ((__u64)(buf[i] & 1) << (COBRA_LENGTH - 1)); | 
 | 101 |  | 
 | 102 | 		if (j < COBRA_LENGTH) ret |= (1 << i); | 
 | 103 |  | 
 | 104 | 		data[i] = ((buf[i] >>  7) & 0x000001f) | ((buf[i] >>  8) & 0x00003e0) | 
 | 105 | 			| ((buf[i] >>  9) & 0x0007c00) | ((buf[i] >> 10) & 0x00f8000) | 
 | 106 | 			| ((buf[i] >> 11) & 0x1f00000); | 
 | 107 |  | 
 | 108 | 	} | 
 | 109 |  | 
 | 110 | 	return ret; | 
 | 111 | } | 
 | 112 |  | 
 | 113 | static void cobra_poll(struct gameport *gameport) | 
 | 114 | { | 
 | 115 | 	struct cobra *cobra = gameport_get_drvdata(gameport); | 
 | 116 | 	struct input_dev *dev; | 
 | 117 | 	unsigned int data[2]; | 
 | 118 | 	int i, j, r; | 
 | 119 |  | 
 | 120 | 	cobra->reads++; | 
 | 121 |  | 
 | 122 | 	if ((r = cobra_read_packet(gameport, data)) != cobra->exists) { | 
 | 123 | 		cobra->bads++; | 
 | 124 | 		return; | 
 | 125 | 	} | 
 | 126 |  | 
 | 127 | 	for (i = 0; i < 2; i++) | 
 | 128 | 		if (cobra->exists & r & (1 << i)) { | 
 | 129 |  | 
| Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 130 | 			dev = cobra->dev[i]; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 |  | 
 | 132 | 			input_report_abs(dev, ABS_X, ((data[i] >> 4) & 1) - ((data[i] >> 3) & 1)); | 
 | 133 | 			input_report_abs(dev, ABS_Y, ((data[i] >> 2) & 1) - ((data[i] >> 1) & 1)); | 
 | 134 |  | 
 | 135 | 			for (j = 0; cobra_btn[j]; j++) | 
 | 136 | 				input_report_key(dev, cobra_btn[j], data[i] & (0x20 << j)); | 
 | 137 |  | 
 | 138 | 			input_sync(dev); | 
 | 139 |  | 
 | 140 | 		} | 
 | 141 | } | 
 | 142 |  | 
 | 143 | static int cobra_open(struct input_dev *dev) | 
 | 144 | { | 
 | 145 | 	struct cobra *cobra = dev->private; | 
 | 146 |  | 
 | 147 | 	gameport_start_polling(cobra->gameport); | 
 | 148 | 	return 0; | 
 | 149 | } | 
 | 150 |  | 
 | 151 | static void cobra_close(struct input_dev *dev) | 
 | 152 | { | 
 | 153 | 	struct cobra *cobra = dev->private; | 
 | 154 |  | 
 | 155 | 	gameport_stop_polling(cobra->gameport); | 
 | 156 | } | 
 | 157 |  | 
 | 158 | static int cobra_connect(struct gameport *gameport, struct gameport_driver *drv) | 
 | 159 | { | 
 | 160 | 	struct cobra *cobra; | 
| Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 161 | 	struct input_dev *input_dev; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | 	unsigned int data[2]; | 
 | 163 | 	int i, j; | 
 | 164 | 	int err; | 
 | 165 |  | 
| Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 166 | 	cobra = kzalloc(sizeof(struct cobra), GFP_KERNEL); | 
 | 167 | 	if (!cobra) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | 		return -ENOMEM; | 
 | 169 |  | 
 | 170 | 	cobra->gameport = gameport; | 
 | 171 |  | 
 | 172 | 	gameport_set_drvdata(gameport, cobra); | 
 | 173 |  | 
 | 174 | 	err = gameport_open(gameport, drv, GAMEPORT_MODE_RAW); | 
 | 175 | 	if (err) | 
 | 176 | 		goto fail1; | 
 | 177 |  | 
 | 178 | 	cobra->exists = cobra_read_packet(gameport, data); | 
 | 179 |  | 
 | 180 | 	for (i = 0; i < 2; i++) | 
 | 181 | 		if ((cobra->exists >> i) & data[i] & 1) { | 
 | 182 | 			printk(KERN_WARNING "cobra.c: Device %d on %s has the Ext bit set. ID is: %d" | 
 | 183 | 				" Contact vojtech@ucw.cz\n", i, gameport->phys, (data[i] >> 2) & 7); | 
 | 184 | 			cobra->exists &= ~(1 << i); | 
 | 185 | 		} | 
 | 186 |  | 
 | 187 | 	if (!cobra->exists) { | 
 | 188 | 		err = -ENODEV; | 
 | 189 | 		goto fail2; | 
 | 190 | 	} | 
 | 191 |  | 
 | 192 | 	gameport_set_poll_handler(gameport, cobra_poll); | 
 | 193 | 	gameport_set_poll_interval(gameport, 20); | 
 | 194 |  | 
| Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 195 | 	for (i = 0; i < 2; i++) { | 
 | 196 | 		if (~(cobra->exists >> i) & 1) | 
 | 197 | 			continue; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 |  | 
| Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 199 | 		cobra->dev[i] = input_dev = input_allocate_device(); | 
 | 200 | 		if (!input_dev) { | 
 | 201 | 			err = -ENOMEM; | 
 | 202 | 			goto fail3; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | 		} | 
 | 204 |  | 
| Dmitry Torokhov | 10ca4c0 | 2006-06-26 01:45:48 -0400 | [diff] [blame] | 205 | 		snprintf(cobra->phys[i], sizeof(cobra->phys[i]), | 
 | 206 | 			 "%s/input%d", gameport->phys, i); | 
| Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 207 |  | 
 | 208 | 		input_dev->name = "Creative Labs Blaster GamePad Cobra"; | 
 | 209 | 		input_dev->phys = cobra->phys[i]; | 
 | 210 | 		input_dev->id.bustype = BUS_GAMEPORT; | 
 | 211 | 		input_dev->id.vendor = GAMEPORT_ID_VENDOR_CREATIVE; | 
 | 212 | 		input_dev->id.product = 0x0008; | 
 | 213 | 		input_dev->id.version = 0x0100; | 
 | 214 | 		input_dev->cdev.dev = &gameport->dev; | 
 | 215 | 		input_dev->private = cobra; | 
 | 216 |  | 
 | 217 | 		input_dev->open = cobra_open; | 
 | 218 | 		input_dev->close = cobra_close; | 
 | 219 |  | 
 | 220 | 		input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_ABS); | 
 | 221 | 		input_set_abs_params(input_dev, ABS_X, -1, 1, 0, 0); | 
 | 222 | 		input_set_abs_params(input_dev, ABS_Y, -1, 1, 0, 0); | 
 | 223 | 		for (j = 0; cobra_btn[j]; j++) | 
 | 224 | 			set_bit(cobra_btn[j], input_dev->keybit); | 
 | 225 |  | 
 | 226 | 		input_register_device(cobra->dev[i]); | 
 | 227 | 	} | 
 | 228 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | 	return 0; | 
 | 230 |  | 
| Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 231 |  fail3:	for (i = 0; i < 2; i++) | 
 | 232 | 		if (cobra->dev[i]) | 
 | 233 | 			input_unregister_device(cobra->dev[i]); | 
 | 234 |  fail2:	gameport_close(gameport); | 
 | 235 |  fail1:	gameport_set_drvdata(gameport, NULL); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | 	kfree(cobra); | 
 | 237 | 	return err; | 
 | 238 | } | 
 | 239 |  | 
 | 240 | static void cobra_disconnect(struct gameport *gameport) | 
 | 241 | { | 
 | 242 | 	struct cobra *cobra = gameport_get_drvdata(gameport); | 
 | 243 | 	int i; | 
 | 244 |  | 
 | 245 | 	for (i = 0; i < 2; i++) | 
 | 246 | 		if ((cobra->exists >> i) & 1) | 
| Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 247 | 			input_unregister_device(cobra->dev[i]); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | 	gameport_close(gameport); | 
 | 249 | 	gameport_set_drvdata(gameport, NULL); | 
 | 250 | 	kfree(cobra); | 
 | 251 | } | 
 | 252 |  | 
 | 253 | static struct gameport_driver cobra_drv = { | 
 | 254 | 	.driver		= { | 
 | 255 | 		.name	= "cobra", | 
 | 256 | 	}, | 
 | 257 | 	.description	= DRIVER_DESC, | 
 | 258 | 	.connect	= cobra_connect, | 
 | 259 | 	.disconnect	= cobra_disconnect, | 
 | 260 | }; | 
 | 261 |  | 
 | 262 | static int __init cobra_init(void) | 
 | 263 | { | 
 | 264 | 	gameport_register_driver(&cobra_drv); | 
 | 265 | 	return 0; | 
 | 266 | } | 
 | 267 |  | 
 | 268 | static void __exit cobra_exit(void) | 
 | 269 | { | 
 | 270 | 	gameport_unregister_driver(&cobra_drv); | 
 | 271 | } | 
 | 272 |  | 
 | 273 | module_init(cobra_init); | 
 | 274 | module_exit(cobra_exit); |