block: inherit CPU completion on bio->rq and rq->rq merges
[linux-2.6/mini2440.git] / drivers / input / joystick / cobra.c
blob639b975a8ed7a6c64ecb06f315dffa45eb2b5a1a
1 /*
2 * Copyright (c) 1999-2001 Vojtech Pavlik
3 */
5 /*
6 * Creative Labs Blaster GamePad Cobra driver for Linux
7 */
9 /*
10 * This program is free software; 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.
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.
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
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
29 #include <linux/kernel.h>
30 #include <linux/module.h>
31 #include <linux/slab.h>
32 #include <linux/init.h>
33 #include <linux/gameport.h>
34 #include <linux/input.h>
35 #include <linux/jiffies.h>
37 #define DRIVER_DESC "Creative Labs Blaster GamePad Cobra driver"
39 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
40 MODULE_DESCRIPTION(DRIVER_DESC);
41 MODULE_LICENSE("GPL");
43 #define COBRA_MAX_STROBE 45 /* 45 us max wait for first strobe */
44 #define COBRA_LENGTH 36
46 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 struct cobra {
49 struct gameport *gameport;
50 struct input_dev *dev[2];
51 int reads;
52 int bads;
53 unsigned char exists;
54 char phys[2][32];
57 static unsigned char cobra_read_packet(struct gameport *gameport, unsigned int *data)
59 unsigned long flags;
60 unsigned char u, v, w;
61 __u64 buf[2];
62 int r[2], t[2];
63 int i, j, ret;
65 int strobe = gameport_time(gameport, COBRA_MAX_STROBE);
67 for (i = 0; i < 2; i++) {
68 r[i] = buf[i] = 0;
69 t[i] = COBRA_MAX_STROBE;
72 local_irq_save(flags);
74 u = gameport_read(gameport);
76 do {
77 t[0]--; t[1]--;
78 v = gameport_read(gameport);
79 for (i = 0, w = u ^ v; i < 2 && w; i++, w >>= 2)
80 if (w & 0x30) {
81 if ((w & 0x30) < 0x30 && r[i] < COBRA_LENGTH && t[i] > 0) {
82 buf[i] |= (__u64)((w >> 5) & 1) << r[i]++;
83 t[i] = strobe;
84 u = v;
85 } else t[i] = 0;
87 } while (t[0] > 0 || t[1] > 0);
89 local_irq_restore(flags);
91 ret = 0;
93 for (i = 0; i < 2; i++) {
95 if (r[i] != COBRA_LENGTH) continue;
97 for (j = 0; j < COBRA_LENGTH && (buf[i] & 0x04104107f) ^ 0x041041040; j++)
98 buf[i] = (buf[i] >> 1) | ((__u64)(buf[i] & 1) << (COBRA_LENGTH - 1));
100 if (j < COBRA_LENGTH) ret |= (1 << i);
102 data[i] = ((buf[i] >> 7) & 0x000001f) | ((buf[i] >> 8) & 0x00003e0)
103 | ((buf[i] >> 9) & 0x0007c00) | ((buf[i] >> 10) & 0x00f8000)
104 | ((buf[i] >> 11) & 0x1f00000);
108 return ret;
111 static void cobra_poll(struct gameport *gameport)
113 struct cobra *cobra = gameport_get_drvdata(gameport);
114 struct input_dev *dev;
115 unsigned int data[2];
116 int i, j, r;
118 cobra->reads++;
120 if ((r = cobra_read_packet(gameport, data)) != cobra->exists) {
121 cobra->bads++;
122 return;
125 for (i = 0; i < 2; i++)
126 if (cobra->exists & r & (1 << i)) {
128 dev = cobra->dev[i];
130 input_report_abs(dev, ABS_X, ((data[i] >> 4) & 1) - ((data[i] >> 3) & 1));
131 input_report_abs(dev, ABS_Y, ((data[i] >> 2) & 1) - ((data[i] >> 1) & 1));
133 for (j = 0; cobra_btn[j]; j++)
134 input_report_key(dev, cobra_btn[j], data[i] & (0x20 << j));
136 input_sync(dev);
141 static int cobra_open(struct input_dev *dev)
143 struct cobra *cobra = input_get_drvdata(dev);
145 gameport_start_polling(cobra->gameport);
146 return 0;
149 static void cobra_close(struct input_dev *dev)
151 struct cobra *cobra = input_get_drvdata(dev);
153 gameport_stop_polling(cobra->gameport);
156 static int cobra_connect(struct gameport *gameport, struct gameport_driver *drv)
158 struct cobra *cobra;
159 struct input_dev *input_dev;
160 unsigned int data[2];
161 int i, j;
162 int err;
164 cobra = kzalloc(sizeof(struct cobra), GFP_KERNEL);
165 if (!cobra)
166 return -ENOMEM;
168 cobra->gameport = gameport;
170 gameport_set_drvdata(gameport, cobra);
172 err = gameport_open(gameport, drv, GAMEPORT_MODE_RAW);
173 if (err)
174 goto fail1;
176 cobra->exists = cobra_read_packet(gameport, data);
178 for (i = 0; i < 2; i++)
179 if ((cobra->exists >> i) & data[i] & 1) {
180 printk(KERN_WARNING "cobra.c: Device %d on %s has the Ext bit set. ID is: %d"
181 " Contact vojtech@ucw.cz\n", i, gameport->phys, (data[i] >> 2) & 7);
182 cobra->exists &= ~(1 << i);
185 if (!cobra->exists) {
186 err = -ENODEV;
187 goto fail2;
190 gameport_set_poll_handler(gameport, cobra_poll);
191 gameport_set_poll_interval(gameport, 20);
193 for (i = 0; i < 2; i++) {
194 if (~(cobra->exists >> i) & 1)
195 continue;
197 cobra->dev[i] = input_dev = input_allocate_device();
198 if (!input_dev) {
199 err = -ENOMEM;
200 goto fail3;
203 snprintf(cobra->phys[i], sizeof(cobra->phys[i]),
204 "%s/input%d", gameport->phys, i);
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->dev.parent = &gameport->dev;
214 input_set_drvdata(input_dev, cobra);
216 input_dev->open = cobra_open;
217 input_dev->close = cobra_close;
219 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
220 input_set_abs_params(input_dev, ABS_X, -1, 1, 0, 0);
221 input_set_abs_params(input_dev, ABS_Y, -1, 1, 0, 0);
222 for (j = 0; cobra_btn[j]; j++)
223 set_bit(cobra_btn[j], input_dev->keybit);
225 err = input_register_device(cobra->dev[i]);
226 if (err)
227 goto fail4;
230 return 0;
232 fail4: input_free_device(cobra->dev[i]);
233 fail3: while (--i >= 0)
234 if (cobra->dev[i])
235 input_unregister_device(cobra->dev[i]);
236 fail2: gameport_close(gameport);
237 fail1: gameport_set_drvdata(gameport, NULL);
238 kfree(cobra);
239 return err;
242 static void cobra_disconnect(struct gameport *gameport)
244 struct cobra *cobra = gameport_get_drvdata(gameport);
245 int i;
247 for (i = 0; i < 2; i++)
248 if ((cobra->exists >> i) & 1)
249 input_unregister_device(cobra->dev[i]);
250 gameport_close(gameport);
251 gameport_set_drvdata(gameport, NULL);
252 kfree(cobra);
255 static struct gameport_driver cobra_drv = {
256 .driver = {
257 .name = "cobra",
259 .description = DRIVER_DESC,
260 .connect = cobra_connect,
261 .disconnect = cobra_disconnect,
264 static int __init cobra_init(void)
266 gameport_register_driver(&cobra_drv);
267 return 0;
270 static void __exit cobra_exit(void)
272 gameport_unregister_driver(&cobra_drv);
275 module_init(cobra_init);
276 module_exit(cobra_exit);