2 * $Id: cobra.c,v 1.19 2002/01/22 20:26:52 vojtech Exp $
4 * Copyright (c) 1999-2001 Vojtech Pavlik
8 * Creative Labs Blaster GamePad Cobra driver for Linux
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.
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.
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
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
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>
38 #define DRIVER_DESC "Creative Labs Blaster GamePad Cobra driver"
40 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
41 MODULE_DESCRIPTION(DRIVER_DESC
);
42 MODULE_LICENSE("GPL");
44 #define COBRA_MAX_STROBE 45 /* 45 us max wait for first strobe */
45 #define COBRA_LENGTH 36
47 static char* cobra_name
= "Creative Labs Blaster GamePad Cobra";
49 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 };
52 struct gameport
*gameport
;
53 struct input_dev dev
[2];
60 static unsigned char cobra_read_packet(struct gameport
*gameport
, unsigned int *data
)
63 unsigned char u
, v
, w
;
68 int strobe
= gameport_time(gameport
, COBRA_MAX_STROBE
);
70 for (i
= 0; i
< 2; i
++) {
72 t
[i
] = COBRA_MAX_STROBE
;
75 local_irq_save(flags
);
77 u
= gameport_read(gameport
);
81 v
= gameport_read(gameport
);
82 for (i
= 0, w
= u
^ v
; i
< 2 && w
; i
++, w
>>= 2)
84 if ((w
& 0x30) < 0x30 && r
[i
] < COBRA_LENGTH
&& t
[i
] > 0) {
85 buf
[i
] |= (__u64
)((w
>> 5) & 1) << r
[i
]++;
90 } while (t
[0] > 0 || t
[1] > 0);
92 local_irq_restore(flags
);
96 for (i
= 0; i
< 2; i
++) {
98 if (r
[i
] != COBRA_LENGTH
) continue;
100 for (j
= 0; j
< COBRA_LENGTH
&& (buf
[i
] & 0x04104107f) ^ 0x041041040; j
++)
101 buf
[i
] = (buf
[i
] >> 1) | ((__u64
)(buf
[i
] & 1) << (COBRA_LENGTH
- 1));
103 if (j
< COBRA_LENGTH
) ret
|= (1 << i
);
105 data
[i
] = ((buf
[i
] >> 7) & 0x000001f) | ((buf
[i
] >> 8) & 0x00003e0)
106 | ((buf
[i
] >> 9) & 0x0007c00) | ((buf
[i
] >> 10) & 0x00f8000)
107 | ((buf
[i
] >> 11) & 0x1f00000);
114 static void cobra_poll(struct gameport
*gameport
)
116 struct cobra
*cobra
= gameport_get_drvdata(gameport
);
117 struct input_dev
*dev
;
118 unsigned int data
[2];
123 if ((r
= cobra_read_packet(gameport
, data
)) != cobra
->exists
) {
128 for (i
= 0; i
< 2; i
++)
129 if (cobra
->exists
& r
& (1 << i
)) {
131 dev
= cobra
->dev
+ i
;
133 input_report_abs(dev
, ABS_X
, ((data
[i
] >> 4) & 1) - ((data
[i
] >> 3) & 1));
134 input_report_abs(dev
, ABS_Y
, ((data
[i
] >> 2) & 1) - ((data
[i
] >> 1) & 1));
136 for (j
= 0; cobra_btn
[j
]; j
++)
137 input_report_key(dev
, cobra_btn
[j
], data
[i
] & (0x20 << j
));
144 static int cobra_open(struct input_dev
*dev
)
146 struct cobra
*cobra
= dev
->private;
148 gameport_start_polling(cobra
->gameport
);
152 static void cobra_close(struct input_dev
*dev
)
154 struct cobra
*cobra
= dev
->private;
156 gameport_stop_polling(cobra
->gameport
);
159 static int cobra_connect(struct gameport
*gameport
, struct gameport_driver
*drv
)
162 unsigned int data
[2];
166 if (!(cobra
= kcalloc(1, sizeof(struct cobra
), GFP_KERNEL
)))
169 cobra
->gameport
= gameport
;
171 gameport_set_drvdata(gameport
, cobra
);
173 err
= gameport_open(gameport
, drv
, GAMEPORT_MODE_RAW
);
177 cobra
->exists
= cobra_read_packet(gameport
, data
);
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
);
186 if (!cobra
->exists
) {
191 gameport_set_poll_handler(gameport
, cobra_poll
);
192 gameport_set_poll_interval(gameport
, 20);
194 for (i
= 0; i
< 2; i
++)
195 if ((cobra
->exists
>> i
) & 1) {
197 sprintf(cobra
->phys
[i
], "%s/input%d", gameport
->phys
, i
);
199 cobra
->dev
[i
].private = cobra
;
200 cobra
->dev
[i
].open
= cobra_open
;
201 cobra
->dev
[i
].close
= cobra_close
;
203 cobra
->dev
[i
].name
= cobra_name
;
204 cobra
->dev
[i
].phys
= cobra
->phys
[i
];
205 cobra
->dev
[i
].id
.bustype
= BUS_GAMEPORT
;
206 cobra
->dev
[i
].id
.vendor
= GAMEPORT_ID_VENDOR_CREATIVE
;
207 cobra
->dev
[i
].id
.product
= 0x0008;
208 cobra
->dev
[i
].id
.version
= 0x0100;
210 cobra
->dev
[i
].evbit
[0] = BIT(EV_KEY
) | BIT(EV_ABS
);
212 input_set_abs_params(&cobra
->dev
[i
], ABS_X
, -1, 1, 0, 0);
213 input_set_abs_params(&cobra
->dev
[i
], ABS_Y
, -1, 1, 0, 0);
215 for (j
= 0; cobra_btn
[j
]; j
++)
216 set_bit(cobra_btn
[j
], cobra
->dev
[i
].keybit
);
218 input_register_device(&cobra
->dev
[i
]);
219 printk(KERN_INFO
"input: %s on %s\n", cobra_name
, gameport
->phys
);
224 fail2
: gameport_close(gameport
);
225 fail1
: gameport_set_drvdata(gameport
, NULL
);
230 static void cobra_disconnect(struct gameport
*gameport
)
232 struct cobra
*cobra
= gameport_get_drvdata(gameport
);
235 for (i
= 0; i
< 2; i
++)
236 if ((cobra
->exists
>> i
) & 1)
237 input_unregister_device(cobra
->dev
+ i
);
238 gameport_close(gameport
);
239 gameport_set_drvdata(gameport
, NULL
);
243 static struct gameport_driver cobra_drv
= {
247 .description
= DRIVER_DESC
,
248 .connect
= cobra_connect
,
249 .disconnect
= cobra_disconnect
,
252 static int __init
cobra_init(void)
254 gameport_register_driver(&cobra_drv
);
258 static void __exit
cobra_exit(void)
260 gameport_unregister_driver(&cobra_drv
);
263 module_init(cobra_init
);
264 module_exit(cobra_exit
);