1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (c) 1998-2001 Vojtech Pavlik
7 * Gravis/Kensington GrIP protocol joystick and gamepad driver for Linux
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/slab.h>
16 #include <linux/gameport.h>
17 #include <linux/input.h>
18 #include <linux/jiffies.h>
20 #define DRIVER_DESC "Gravis GrIP protocol joystick driver"
22 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
23 MODULE_DESCRIPTION(DRIVER_DESC
);
24 MODULE_LICENSE("GPL");
26 #define GRIP_MODE_GPP 1
27 #define GRIP_MODE_BD 2
28 #define GRIP_MODE_XT 3
29 #define GRIP_MODE_DC 4
31 #define GRIP_LENGTH_GPP 24
32 #define GRIP_STROBE_GPP 200 /* 200 us */
33 #define GRIP_LENGTH_XT 4
34 #define GRIP_STROBE_XT 64 /* 64 us */
35 #define GRIP_MAX_CHUNKS_XT 10
36 #define GRIP_MAX_BITS_XT 30
39 struct gameport
*gameport
;
40 struct input_dev
*dev
[2];
41 unsigned char mode
[2];
47 static int grip_btn_gpp
[] = { BTN_START
, BTN_SELECT
, BTN_TR2
, BTN_Y
, 0, BTN_TL2
, BTN_A
, BTN_B
, BTN_X
, 0, BTN_TL
, BTN_TR
, -1 };
48 static int grip_btn_bd
[] = { BTN_THUMB
, BTN_THUMB2
, BTN_TRIGGER
, BTN_TOP
, BTN_BASE
, -1 };
49 static int grip_btn_xt
[] = { BTN_TRIGGER
, BTN_THUMB
, BTN_A
, BTN_B
, BTN_C
, BTN_X
, BTN_Y
, BTN_Z
, BTN_SELECT
, BTN_START
, BTN_MODE
, -1 };
50 static int grip_btn_dc
[] = { BTN_TRIGGER
, BTN_THUMB
, BTN_TOP
, BTN_TOP2
, BTN_BASE
, BTN_BASE2
, BTN_BASE3
, BTN_BASE4
, BTN_BASE5
, -1 };
52 static int grip_abs_gpp
[] = { ABS_X
, ABS_Y
, -1 };
53 static int grip_abs_bd
[] = { ABS_X
, ABS_Y
, ABS_THROTTLE
, ABS_HAT0X
, ABS_HAT0Y
, -1 };
54 static int grip_abs_xt
[] = { ABS_X
, ABS_Y
, ABS_BRAKE
, ABS_GAS
, ABS_THROTTLE
, ABS_HAT0X
, ABS_HAT0Y
, ABS_HAT1X
, ABS_HAT1Y
, -1 };
55 static int grip_abs_dc
[] = { ABS_X
, ABS_Y
, ABS_RX
, ABS_RY
, ABS_THROTTLE
, ABS_HAT0X
, ABS_HAT0Y
, -1 };
57 static char *grip_name
[] = { NULL
, "Gravis GamePad Pro", "Gravis Blackhawk Digital",
58 "Gravis Xterminator Digital", "Gravis Xterminator DualControl" };
59 static int *grip_abs
[] = { NULL
, grip_abs_gpp
, grip_abs_bd
, grip_abs_xt
, grip_abs_dc
};
60 static int *grip_btn
[] = { NULL
, grip_btn_gpp
, grip_btn_bd
, grip_btn_xt
, grip_btn_dc
};
61 static char grip_anx
[] = { 0, 0, 3, 5, 5 };
62 static char grip_cen
[] = { 0, 0, 2, 2, 4 };
65 * grip_gpp_read_packet() reads a Gravis GamePad Pro packet.
68 static int grip_gpp_read_packet(struct gameport
*gameport
, int shift
, unsigned int *data
)
75 int strobe
= gameport_time(gameport
, GRIP_STROBE_GPP
);
81 local_irq_save(flags
);
83 v
= gameport_read(gameport
) >> shift
;
87 u
= v
; v
= (gameport_read(gameport
) >> shift
) & 3;
89 data
[0] |= (v
>> 1) << i
++;
92 } while (i
< GRIP_LENGTH_GPP
&& t
> 0);
94 local_irq_restore(flags
);
96 if (i
< GRIP_LENGTH_GPP
) return -1;
98 for (i
= 0; i
< GRIP_LENGTH_GPP
&& (data
[0] & 0xfe4210) ^ 0x7c0000; i
++)
99 data
[0] = data
[0] >> 1 | (data
[0] & 1) << (GRIP_LENGTH_GPP
- 1);
101 return -(i
== GRIP_LENGTH_GPP
);
105 * grip_xt_read_packet() reads a Gravis Xterminator packet.
108 static int grip_xt_read_packet(struct gameport
*gameport
, int shift
, unsigned int *data
)
110 unsigned int i
, j
, buf
, crc
;
111 unsigned char u
, v
, w
;
116 int strobe
= gameport_time(gameport
, GRIP_STROBE_XT
);
118 data
[0] = data
[1] = data
[2] = data
[3] = 0;
119 status
= buf
= i
= j
= 0;
122 local_irq_save(flags
);
124 v
= w
= (gameport_read(gameport
) >> shift
) & 3;
128 u
= (gameport_read(gameport
) >> shift
) & 3;
133 buf
= (buf
<< 1) | (u
>> 1);
138 if ((((u
^ v
) & (v
^ w
)) >> 1) & ~(u
| v
| w
) & 1) {
140 crc
= buf
^ (buf
>> 7) ^ (buf
>> 14);
141 if (!((crc
^ (0x25cb9e70 >> ((crc
>> 2) & 0x1c))) & 0xf)) {
142 data
[buf
>> 18] = buf
>> 4;
143 status
|= 1 << (buf
>> 18);
155 } while (status
!= 0xf && i
< GRIP_MAX_BITS_XT
&& j
< GRIP_MAX_CHUNKS_XT
&& t
> 0);
157 local_irq_restore(flags
);
159 return -(status
!= 0xf);
163 * grip_timer() repeatedly polls the joysticks and generates events.
166 static void grip_poll(struct gameport
*gameport
)
168 struct grip
*grip
= gameport_get_drvdata(gameport
);
169 unsigned int data
[GRIP_LENGTH_XT
];
170 struct input_dev
*dev
;
173 for (i
= 0; i
< 2; i
++) {
181 switch (grip
->mode
[i
]) {
185 if (grip_gpp_read_packet(grip
->gameport
, (i
<< 1) + 4, data
)) {
190 input_report_abs(dev
, ABS_X
, ((*data
>> 15) & 1) - ((*data
>> 16) & 1));
191 input_report_abs(dev
, ABS_Y
, ((*data
>> 13) & 1) - ((*data
>> 12) & 1));
193 for (j
= 0; j
< 12; j
++)
195 input_report_key(dev
, grip_btn_gpp
[j
], (*data
>> j
) & 1);
201 if (grip_xt_read_packet(grip
->gameport
, (i
<< 1) + 4, data
)) {
206 input_report_abs(dev
, ABS_X
, (data
[0] >> 2) & 0x3f);
207 input_report_abs(dev
, ABS_Y
, 63 - ((data
[0] >> 8) & 0x3f));
208 input_report_abs(dev
, ABS_THROTTLE
, (data
[2] >> 8) & 0x3f);
210 input_report_abs(dev
, ABS_HAT0X
, ((data
[2] >> 1) & 1) - ( data
[2] & 1));
211 input_report_abs(dev
, ABS_HAT0Y
, ((data
[2] >> 2) & 1) - ((data
[2] >> 3) & 1));
213 for (j
= 0; j
< 5; j
++)
214 input_report_key(dev
, grip_btn_bd
[j
], (data
[3] >> (j
+ 4)) & 1);
220 if (grip_xt_read_packet(grip
->gameport
, (i
<< 1) + 4, data
)) {
225 input_report_abs(dev
, ABS_X
, (data
[0] >> 2) & 0x3f);
226 input_report_abs(dev
, ABS_Y
, 63 - ((data
[0] >> 8) & 0x3f));
227 input_report_abs(dev
, ABS_BRAKE
, (data
[1] >> 2) & 0x3f);
228 input_report_abs(dev
, ABS_GAS
, (data
[1] >> 8) & 0x3f);
229 input_report_abs(dev
, ABS_THROTTLE
, (data
[2] >> 8) & 0x3f);
231 input_report_abs(dev
, ABS_HAT0X
, ((data
[2] >> 1) & 1) - ( data
[2] & 1));
232 input_report_abs(dev
, ABS_HAT0Y
, ((data
[2] >> 2) & 1) - ((data
[2] >> 3) & 1));
233 input_report_abs(dev
, ABS_HAT1X
, ((data
[2] >> 5) & 1) - ((data
[2] >> 4) & 1));
234 input_report_abs(dev
, ABS_HAT1Y
, ((data
[2] >> 6) & 1) - ((data
[2] >> 7) & 1));
236 for (j
= 0; j
< 11; j
++)
237 input_report_key(dev
, grip_btn_xt
[j
], (data
[3] >> (j
+ 3)) & 1);
242 if (grip_xt_read_packet(grip
->gameport
, (i
<< 1) + 4, data
)) {
247 input_report_abs(dev
, ABS_X
, (data
[0] >> 2) & 0x3f);
248 input_report_abs(dev
, ABS_Y
, (data
[0] >> 8) & 0x3f);
249 input_report_abs(dev
, ABS_RX
, (data
[1] >> 2) & 0x3f);
250 input_report_abs(dev
, ABS_RY
, (data
[1] >> 8) & 0x3f);
251 input_report_abs(dev
, ABS_THROTTLE
, (data
[2] >> 8) & 0x3f);
253 input_report_abs(dev
, ABS_HAT0X
, ((data
[2] >> 1) & 1) - ( data
[2] & 1));
254 input_report_abs(dev
, ABS_HAT0Y
, ((data
[2] >> 2) & 1) - ((data
[2] >> 3) & 1));
256 for (j
= 0; j
< 9; j
++)
257 input_report_key(dev
, grip_btn_dc
[j
], (data
[3] >> (j
+ 3)) & 1);
267 static int grip_open(struct input_dev
*dev
)
269 struct grip
*grip
= input_get_drvdata(dev
);
271 gameport_start_polling(grip
->gameport
);
275 static void grip_close(struct input_dev
*dev
)
277 struct grip
*grip
= input_get_drvdata(dev
);
279 gameport_stop_polling(grip
->gameport
);
282 static int grip_connect(struct gameport
*gameport
, struct gameport_driver
*drv
)
285 struct input_dev
*input_dev
;
286 unsigned int data
[GRIP_LENGTH_XT
];
290 if (!(grip
= kzalloc(sizeof(struct grip
), GFP_KERNEL
)))
293 grip
->gameport
= gameport
;
295 gameport_set_drvdata(gameport
, grip
);
297 err
= gameport_open(gameport
, drv
, GAMEPORT_MODE_RAW
);
301 for (i
= 0; i
< 2; i
++) {
302 if (!grip_gpp_read_packet(gameport
, (i
<< 1) + 4, data
)) {
303 grip
->mode
[i
] = GRIP_MODE_GPP
;
306 if (!grip_xt_read_packet(gameport
, (i
<< 1) + 4, data
)) {
307 if (!(data
[3] & 7)) {
308 grip
->mode
[i
] = GRIP_MODE_BD
;
311 if (!(data
[2] & 0xf0)) {
312 grip
->mode
[i
] = GRIP_MODE_XT
;
315 grip
->mode
[i
] = GRIP_MODE_DC
;
320 if (!grip
->mode
[0] && !grip
->mode
[1]) {
325 gameport_set_poll_handler(gameport
, grip_poll
);
326 gameport_set_poll_interval(gameport
, 20);
328 for (i
= 0; i
< 2; i
++) {
332 grip
->dev
[i
] = input_dev
= input_allocate_device();
338 snprintf(grip
->phys
[i
], sizeof(grip
->phys
[i
]),
339 "%s/input%d", gameport
->phys
, i
);
341 input_dev
->name
= grip_name
[grip
->mode
[i
]];
342 input_dev
->phys
= grip
->phys
[i
];
343 input_dev
->id
.bustype
= BUS_GAMEPORT
;
344 input_dev
->id
.vendor
= GAMEPORT_ID_VENDOR_GRAVIS
;
345 input_dev
->id
.product
= grip
->mode
[i
];
346 input_dev
->id
.version
= 0x0100;
347 input_dev
->dev
.parent
= &gameport
->dev
;
349 input_set_drvdata(input_dev
, grip
);
351 input_dev
->open
= grip_open
;
352 input_dev
->close
= grip_close
;
354 input_dev
->evbit
[0] = BIT_MASK(EV_KEY
) | BIT_MASK(EV_ABS
);
356 for (j
= 0; (t
= grip_abs
[grip
->mode
[i
]][j
]) >= 0; j
++) {
358 if (j
< grip_cen
[grip
->mode
[i
]])
359 input_set_abs_params(input_dev
, t
, 14, 52, 1, 2);
360 else if (j
< grip_anx
[grip
->mode
[i
]])
361 input_set_abs_params(input_dev
, t
, 3, 57, 1, 0);
363 input_set_abs_params(input_dev
, t
, -1, 1, 0, 0);
366 for (j
= 0; (t
= grip_btn
[grip
->mode
[i
]][j
]) >= 0; j
++)
368 set_bit(t
, input_dev
->keybit
);
370 err
= input_register_device(grip
->dev
[i
]);
377 fail4
: input_free_device(grip
->dev
[i
]);
378 fail3
: while (--i
>= 0)
380 input_unregister_device(grip
->dev
[i
]);
381 fail2
: gameport_close(gameport
);
382 fail1
: gameport_set_drvdata(gameport
, NULL
);
387 static void grip_disconnect(struct gameport
*gameport
)
389 struct grip
*grip
= gameport_get_drvdata(gameport
);
392 for (i
= 0; i
< 2; i
++)
394 input_unregister_device(grip
->dev
[i
]);
395 gameport_close(gameport
);
396 gameport_set_drvdata(gameport
, NULL
);
400 static struct gameport_driver grip_drv
= {
403 .owner
= THIS_MODULE
,
405 .description
= DRIVER_DESC
,
406 .connect
= grip_connect
,
407 .disconnect
= grip_disconnect
,
410 module_gameport_driver(grip_drv
);