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