30 #include <linux/kernel.h>
31 #include <linux/module.h>
32 #include <linux/string.h>
33 #include <linux/slab.h>
34 #include <linux/input.h>
35 #include <linux/gameport.h>
39 #define DRIVER_DESC "Logitech ADI joystick family driver"
49 #define ADI_MAX_START 200
50 #define ADI_MAX_STROBE 40
51 #define ADI_INIT_DELAY 10
52 #define ADI_DATA_DELAY 4
54 #define ADI_MAX_LENGTH 256
55 #define ADI_MIN_LENGTH 8
56 #define ADI_MIN_LEN_LENGTH 10
57 #define ADI_MIN_ID_LENGTH 66
58 #define ADI_MAX_NAME_LENGTH 64
59 #define ADI_MAX_CNAME_LENGTH 16
60 #define ADI_MAX_PHYS_LENGTH 64
62 #define ADI_FLAG_HAT 0x04
63 #define ADI_FLAG_10BIT 0x08
65 #define ADI_ID_TPD 0x01
66 #define ADI_ID_WGP 0x06
67 #define ADI_ID_WGPE 0x08
68 #define ADI_ID_MAX 0x0a
74 static char *adi_names[] = {
"WingMan Extreme Digital",
"ThunderPad Digital",
"SideCar",
"CyberMan 2",
75 "WingMan Interceptor",
"WingMan Formula",
"WingMan GamePad",
76 "WingMan Extreme Digital 3D",
"WingMan GamePad Extreme",
77 "WingMan GamePad USB",
"Unknown Device %#x" };
85 static short adi_wmgpe_key[] = {
BTN_A,
BTN_B,
BTN_C,
BTN_X,
BTN_Y,
BTN_Z,
BTN_TL,
BTN_TR,
BTN_START,
BTN_MODE,
BTN_SELECT };
90 static char* adi_abs[] = { adi_wmi_abs, adi_wmgpe_abs, adi_wmf_abs, adi_cm2_abs, adi_wmi_abs, adi_wmf_abs,
91 adi_wmgpe_abs, adi_wmed3d_abs, adi_wmgpe_abs, adi_wmgpe_abs, adi_wmi_abs };
93 static short* adi_key[] = { adi_wmi_key, adi_wmgpe_key, adi_cm2_key, adi_cm2_key, adi_wmi_key, adi_cm2_key,
94 adi_wmgpe_key, adi_wmed3d_key, adi_wmgpe_key, adi_wmgpe_key, adi_wmi_key };
103 } adi_hat_to_axis[] = {{ 0, 0}, { 0,-1}, { 1,-1}, { 1, 0}, { 1, 1}, { 0, 1}, {-1, 1}, {-1, 0}, {-1,-1}};
143 unsigned char u,
v,
w,
x, z;
147 for (i = 0; i < 2; i++) {
155 gameport_trigger(gameport);
156 v = z = gameport_read(gameport);
160 w = u ^ (v = x = gameport_read(gameport));
161 for (i = 0; i < 2; i++, w >>= 2, x >>= 2) {
163 if ((w & 0x30) && s[
i]) {
168 }
else if (!(x & 0x30)) s[
i] = 1;
170 }
while (t[0] > 0 || t[1] > 0);
185 struct adi *adi = port->
adi;
187 adi[0].
idx = adi[1].
idx = 0;
189 if (adi[0].
ret <= 0 || adi[1].
ret <= 0)
return;
190 if (adi[0].
data[0] & 0x20 || ~adi[1].
data[0] & 0x20)
return;
192 for (i = 1; i <= adi[1].
ret; i++)
193 adi[0].
data[((length - 1) >> 1) + i + 1] = adi[1].
data[
i];
203 static inline int adi_get_bits(
struct adi *adi,
int count)
207 if ((adi->
idx += count) > adi->
ret)
return 0;
208 for (i = 0; i <
count; i++)
209 bits |= ((adi->
data[adi->
idx - i] >> 5) & 1) <<
i;
217 static int adi_decode(
struct adi *adi)
219 struct input_dev *
dev = adi->
dev;
224 if (adi->
ret < adi->
length || adi->
id != (adi_get_bits(adi, 4) | (adi_get_bits(adi, 4) << 4)))
227 for (i = 0; i < adi->
axes10; i++)
228 input_report_abs(dev, *abs++, adi_get_bits(adi, 10));
230 for (i = 0; i < adi->
axes8; i++)
231 input_report_abs(dev, *abs++, adi_get_bits(adi, 8));
233 for (i = 0; i < adi->
buttons && i < 63; i++) {
235 t = adi_get_bits(adi, 4);
236 input_report_abs(dev, *abs++, ((t >> 2) & 1) - ( t & 1));
237 input_report_abs(dev, *abs++, ((t >> 1) & 1) - ((t >> 3) & 1));
239 input_report_key(dev, *key++, adi_get_bits(adi, 1));
242 for (i = 0; i < adi->
hats; i++) {
243 if ((t = adi_get_bits(adi, 4)) > 8) t = 0;
244 input_report_abs(dev, *abs++, adi_hat_to_axis[t].x);
245 input_report_abs(dev, *abs++, adi_hat_to_axis[t].
y);
248 for (i = 63; i < adi->
buttons; i++)
249 input_report_key(dev, *key++, adi_get_bits(adi, 1));
260 static int adi_read(
struct adi_port *port)
265 adi_read_packet(port);
266 adi_move_bits(port, port->
adi[0].length);
268 for (i = 0; i < 2; i++)
269 if (port->
adi[i].length)
270 result |= adi_decode(port->
adi + i);
279 static void adi_poll(
struct gameport *gameport)
281 struct adi_port *port = gameport_get_drvdata(gameport);
283 port->
bad -= adi_read(port);
291 static int adi_open(
struct input_dev *dev)
293 struct adi_port *port = input_get_drvdata(dev);
303 static void adi_close(
struct input_dev *dev)
305 struct adi_port *port = input_get_drvdata(dev);
315 static void adi_init_digital(
struct gameport *gameport)
317 int seq[] = { 4, -2, -3, 10, -6, -11, -7, -9, 11, 0 };
320 for (i = 0; seq[
i]; i++) {
321 gameport_trigger(gameport);
331 static void adi_id_decode(
struct adi *adi,
struct adi_port *port)
338 if (adi->
ret < (t = adi_get_bits(adi, 10))) {
343 adi->
id = adi_get_bits(adi, 4) | (adi_get_bits(adi, 4) << 4);
347 adi->
length = adi_get_bits(adi, 10);
355 adi->
axes8 = adi_get_bits(adi, 4);
356 adi->
buttons = adi_get_bits(adi, 6);
358 if (adi_get_bits(adi, 6) != 8 && adi->
hats) {
364 adi->
buttons += adi_get_bits(adi, 6);
365 adi->
hats += adi_get_bits(adi, 4);
367 i = adi_get_bits(adi, 4);
374 t = adi_get_bits(adi, 4);
376 for (i = 0; i <
t; i++)
377 adi->
cname[i] = adi_get_bits(adi, 8);
402 static int adi_init_input(
struct adi *adi,
struct adi_port *port,
int half)
404 struct input_dev *input_dev;
408 adi->
dev = input_dev = input_allocate_device();
418 adi->
abs = adi_abs[
t];
419 adi->
key = adi_key[
t];
421 input_dev->name = adi->
name;
422 input_dev->phys = adi->
phys;
425 input_dev->id.product = adi->
id;
426 input_dev->id.version = 0x0100;
427 input_dev->dev.parent = &port->
gameport->dev;
429 input_set_drvdata(input_dev, port);
431 input_dev->open = adi_open;
432 input_dev->close = adi_close;
439 for (i = 0; i < adi->
buttons; i++)
445 static void adi_init_center(
struct adi *adi)
455 x = input_abs_get_val(adi->
dev, t);
458 x = i < adi->
axes10 ? 512 : 128;
461 input_set_abs_params(adi->
dev, t, 64, x * 2 - 64, 2, 16);
462 else if (i < adi->axes10 + adi->
axes8)
463 input_set_abs_params(adi->
dev, t, 48, x * 2 - 48, 1, 16);
465 input_set_abs_params(adi->
dev, t, -1, 1, 0, 0);
473 static int adi_connect(
struct gameport *gameport,
struct gameport_driver *drv)
485 gameport_set_drvdata(gameport, port);
491 adi_init_digital(gameport);
492 adi_read_packet(port);
495 adi_move_bits(port, adi_get_bits(port->
adi, 10));
497 for (i = 0; i < 2; i++) {
498 adi_id_decode(port->
adi + i, port);
500 if (!port->
adi[i].length)
503 err = adi_init_input(port->
adi + i, port, i);
508 if (!port->
adi[0].length && !port->
adi[1].length) {
513 gameport_set_poll_handler(gameport, adi_poll);
514 gameport_set_poll_interval(gameport, 20);
517 if (adi_read(port)) {
522 for (i = 0; i < 2; i++)
523 if (port->
adi[i].length > 0) {
524 adi_init_center(port->
adi + i);
525 err = input_register_device(port->
adi[i].dev);
532 fail3:
while (--i >= 0) {
533 if (port->
adi[i].length > 0) {
534 input_unregister_device(port->
adi[i].dev);
538 fail2:
for (i = 0; i < 2; i++)
539 if (port->
adi[i].dev)
540 input_free_device(port->
adi[i].dev);
542 fail1: gameport_set_drvdata(gameport,
NULL);
547 static void adi_disconnect(
struct gameport *gameport)
550 struct adi_port *port = gameport_get_drvdata(gameport);
552 for (i = 0; i < 2; i++)
553 if (port->
adi[i].length > 0)
554 input_unregister_device(port->
adi[i].dev);
556 gameport_set_drvdata(gameport,
NULL);
565 .connect = adi_connect,
566 .disconnect = adi_disconnect,