29 #include <linux/kernel.h>
30 #include <linux/module.h>
32 #include <linux/slab.h>
33 #include <linux/gameport.h>
34 #include <linux/input.h>
37 #define DRIVER_DESC "Gravis GrIP protocol joystick driver"
43 #define GRIP_MODE_GPP 1
44 #define GRIP_MODE_BD 2
45 #define GRIP_MODE_XT 3
46 #define GRIP_MODE_DC 4
48 #define GRIP_LENGTH_GPP 24
49 #define GRIP_STROBE_GPP 200
50 #define GRIP_LENGTH_XT 4
51 #define GRIP_STROBE_XT 64
52 #define GRIP_MAX_CHUNKS_XT 10
53 #define GRIP_MAX_BITS_XT 30
57 struct input_dev *
dev[2];
64 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 };
66 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 };
69 static int grip_abs_gpp[] = {
ABS_X,
ABS_Y, -1 };
74 static char *grip_name[] = {
NULL,
"Gravis GamePad Pro",
"Gravis Blackhawk Digital",
75 "Gravis Xterminator Digital",
"Gravis Xterminator DualControl" };
76 static int *grip_abs[] = {
NULL, grip_abs_gpp, grip_abs_bd, grip_abs_xt, grip_abs_dc };
77 static int *grip_btn[] = {
NULL, grip_btn_gpp, grip_btn_bd, grip_btn_xt, grip_btn_dc };
78 static char grip_anx[] = { 0, 0, 3, 5, 5 };
79 static char grip_cen[] = { 0, 0, 2, 2, 4 };
100 v = gameport_read(gameport) >> shift;
104 u =
v; v = (gameport_read(gameport) >> shift) & 3;
106 data[0] |= (v >> 1) << i++;
109 }
while (i < GRIP_LENGTH_GPP && t > 0);
115 for (i = 0; i <
GRIP_LENGTH_GPP && (data[0] & 0xfe4210) ^ 0x7c0000; i++)
125 static int grip_xt_read_packet(
struct gameport *gameport,
int shift,
unsigned int *data)
128 unsigned char u,
v,
w;
135 data[0] = data[1] = data[2] = data[3] = 0;
136 status = buf = i = j = 0;
141 v = w = (gameport_read(gameport) >> shift) & 3;
145 u = (gameport_read(gameport) >> shift) & 3;
155 if ((((u ^ v) & (v ^ w)) >> 1) & ~(u | v | w) & 1) {
157 crc = buf ^ (buf >> 7) ^ (buf >> 14);
158 if (!((crc ^ (0x25cb9e70 >> ((crc >> 2) & 0x1c))) & 0xf)) {
159 data[buf >> 18] = buf >> 4;
160 status |= 1 << (buf >> 18);
172 }
while (status != 0xf && i <
GRIP_MAX_BITS_XT && j < GRIP_MAX_CHUNKS_XT && t > 0);
176 return -(status != 0xf);
183 static void grip_poll(
struct gameport *gameport)
185 struct grip *
grip = gameport_get_drvdata(gameport);
187 struct input_dev *
dev;
190 for (i = 0; i < 2; i++) {
198 switch (grip->
mode[i]) {
202 if (grip_gpp_read_packet(grip->
gameport, (i << 1) + 4, data)) {
207 input_report_abs(dev,
ABS_X, ((*data >> 15) & 1) - ((*data >> 16) & 1));
208 input_report_abs(dev,
ABS_Y, ((*data >> 13) & 1) - ((*data >> 12) & 1));
210 for (j = 0; j < 12; j++)
212 input_report_key(dev, grip_btn_gpp[j], (*data >> j) & 1);
218 if (grip_xt_read_packet(grip->
gameport, (i << 1) + 4, data)) {
223 input_report_abs(dev,
ABS_X, (data[0] >> 2) & 0x3f);
224 input_report_abs(dev,
ABS_Y, 63 - ((data[0] >> 8) & 0x3f));
225 input_report_abs(dev,
ABS_THROTTLE, (data[2] >> 8) & 0x3f);
227 input_report_abs(dev,
ABS_HAT0X, ((data[2] >> 1) & 1) - ( data[2] & 1));
228 input_report_abs(dev,
ABS_HAT0Y, ((data[2] >> 2) & 1) - ((data[2] >> 3) & 1));
230 for (j = 0; j < 5; j++)
231 input_report_key(dev, grip_btn_bd[j], (data[3] >> (j + 4)) & 1);
237 if (grip_xt_read_packet(grip->
gameport, (i << 1) + 4, data)) {
242 input_report_abs(dev,
ABS_X, (data[0] >> 2) & 0x3f);
243 input_report_abs(dev,
ABS_Y, 63 - ((data[0] >> 8) & 0x3f));
244 input_report_abs(dev,
ABS_BRAKE, (data[1] >> 2) & 0x3f);
245 input_report_abs(dev,
ABS_GAS, (data[1] >> 8) & 0x3f);
246 input_report_abs(dev,
ABS_THROTTLE, (data[2] >> 8) & 0x3f);
248 input_report_abs(dev,
ABS_HAT0X, ((data[2] >> 1) & 1) - ( data[2] & 1));
249 input_report_abs(dev,
ABS_HAT0Y, ((data[2] >> 2) & 1) - ((data[2] >> 3) & 1));
250 input_report_abs(dev,
ABS_HAT1X, ((data[2] >> 5) & 1) - ((data[2] >> 4) & 1));
251 input_report_abs(dev,
ABS_HAT1Y, ((data[2] >> 6) & 1) - ((data[2] >> 7) & 1));
253 for (j = 0; j < 11; j++)
254 input_report_key(dev, grip_btn_xt[j], (data[3] >> (j + 3)) & 1);
259 if (grip_xt_read_packet(grip->
gameport, (i << 1) + 4, data)) {
264 input_report_abs(dev,
ABS_X, (data[0] >> 2) & 0x3f);
265 input_report_abs(dev,
ABS_Y, (data[0] >> 8) & 0x3f);
266 input_report_abs(dev,
ABS_RX, (data[1] >> 2) & 0x3f);
267 input_report_abs(dev,
ABS_RY, (data[1] >> 8) & 0x3f);
268 input_report_abs(dev,
ABS_THROTTLE, (data[2] >> 8) & 0x3f);
270 input_report_abs(dev,
ABS_HAT0X, ((data[2] >> 1) & 1) - ( data[2] & 1));
271 input_report_abs(dev,
ABS_HAT0Y, ((data[2] >> 2) & 1) - ((data[2] >> 3) & 1));
273 for (j = 0; j < 9; j++)
274 input_report_key(dev, grip_btn_dc[j], (data[3] >> (j + 3)) & 1);
284 static int grip_open(
struct input_dev *dev)
286 struct grip *grip = input_get_drvdata(dev);
292 static void grip_close(
struct input_dev *dev)
294 struct grip *grip = input_get_drvdata(dev);
299 static int grip_connect(
struct gameport *gameport,
struct gameport_driver *drv)
302 struct input_dev *input_dev;
307 if (!(grip = kzalloc(
sizeof(
struct grip),
GFP_KERNEL)))
312 gameport_set_drvdata(gameport, grip);
318 for (i = 0; i < 2; i++) {
319 if (!grip_gpp_read_packet(gameport, (i << 1) + 4, data)) {
323 if (!grip_xt_read_packet(gameport, (i << 1) + 4, data)) {
324 if (!(data[3] & 7)) {
328 if (!(data[2] & 0xf0)) {
337 if (!grip->
mode[0] && !grip->
mode[1]) {
342 gameport_set_poll_handler(gameport, grip_poll);
343 gameport_set_poll_interval(gameport, 20);
345 for (i = 0; i < 2; i++) {
349 grip->
dev[
i] = input_dev = input_allocate_device();
356 "%s/input%d", gameport->
phys, i);
358 input_dev->name = grip_name[grip->
mode[
i]];
359 input_dev->phys = grip->
phys[
i];
362 input_dev->id.product = grip->
mode[
i];
363 input_dev->id.version = 0x0100;
364 input_dev->dev.parent = &gameport->
dev;
366 input_set_drvdata(input_dev, grip);
368 input_dev->open = grip_open;
369 input_dev->close = grip_close;
373 for (j = 0; (t = grip_abs[grip->
mode[
i]][
j]) >= 0; j++) {
375 if (j < grip_cen[grip->
mode[i]])
376 input_set_abs_params(input_dev, t, 14, 52, 1, 2);
377 else if (j < grip_anx[grip->
mode[i]])
378 input_set_abs_params(input_dev, t, 3, 57, 1, 0);
380 input_set_abs_params(input_dev, t, -1, 1, 0, 0);
383 for (j = 0; (t = grip_btn[grip->
mode[
i]][
j]) >= 0; j++)
387 err = input_register_device(grip->
dev[i]);
394 fail4: input_free_device(grip->
dev[i]);
395 fail3:
while (--i >= 0)
397 input_unregister_device(grip->
dev[i]);
399 fail1: gameport_set_drvdata(gameport,
NULL);
404 static void grip_disconnect(
struct gameport *gameport)
406 struct grip *grip = gameport_get_drvdata(gameport);
409 for (i = 0; i < 2; i++)
411 input_unregister_device(grip->
dev[i]);
413 gameport_set_drvdata(gameport,
NULL);
423 .connect = grip_connect,
424 .disconnect = grip_disconnect,