aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/input/joystick/grip_mp.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/input/joystick/grip_mp.c')
-rw-r--r--drivers/input/joystick/grip_mp.c149
1 files changed, 87 insertions, 62 deletions
diff --git a/drivers/input/joystick/grip_mp.c b/drivers/input/joystick/grip_mp.c
index da17eee6f574..a0ba93ccac72 100644
--- a/drivers/input/joystick/grip_mp.c
+++ b/drivers/input/joystick/grip_mp.c
@@ -32,23 +32,37 @@ MODULE_LICENSE("GPL");
32#define dbg(format, arg...) do {} while (0) 32#define dbg(format, arg...) do {} while (0)
33#endif 33#endif
34 34
35#define GRIP_MAX_PORTS 4
35/* 36/*
36 * Grip multiport state 37 * Grip multiport state
37 */ 38 */
38 39
40struct grip_port {
41 struct input_dev *dev;
42 int mode;
43 int registered;
44
45 /* individual gamepad states */
46 int buttons;
47 int xaxes;
48 int yaxes;
49 int dirty; /* has the state been updated? */
50};
51
39struct grip_mp { 52struct grip_mp {
40 struct gameport *gameport; 53 struct gameport *gameport;
41 struct input_dev dev[4]; 54 struct grip_port *port[GRIP_MAX_PORTS];
42 int mode[4]; 55// struct input_dev *dev[4];
43 int registered[4]; 56// int mode[4];
57// int registered[4];
44 int reads; 58 int reads;
45 int bads; 59 int bads;
46 60
47 /* individual gamepad states */ 61 /* individual gamepad states */
48 int buttons[4]; 62// int buttons[4];
49 int xaxes[4]; 63// int xaxes[4];
50 int yaxes[4]; 64// int yaxes[4];
51 int dirty[4]; /* has the state been updated? */ 65// int dirty[4]; /* has the state been updated? */
52}; 66};
53 67
54/* 68/*
@@ -85,16 +99,16 @@ struct grip_mp {
85#define GRIP_MODE_GP 2 99#define GRIP_MODE_GP 2
86#define GRIP_MODE_C64 3 100#define GRIP_MODE_C64 3
87 101
88static int grip_btn_gp[] = { BTN_TR, BTN_TL, BTN_A, BTN_B, BTN_C, BTN_X, BTN_Y, BTN_Z, -1 }; 102static const int grip_btn_gp[] = { BTN_TR, BTN_TL, BTN_A, BTN_B, BTN_C, BTN_X, BTN_Y, BTN_Z, -1 };
89static int grip_btn_c64[] = { BTN_JOYSTICK, -1 }; 103static const int grip_btn_c64[] = { BTN_JOYSTICK, -1 };
90 104
91static int grip_abs_gp[] = { ABS_X, ABS_Y, -1 }; 105static const int grip_abs_gp[] = { ABS_X, ABS_Y, -1 };
92static int grip_abs_c64[] = { ABS_X, ABS_Y, -1 }; 106static const int grip_abs_c64[] = { ABS_X, ABS_Y, -1 };
93 107
94static int *grip_abs[] = { NULL, NULL, grip_abs_gp, grip_abs_c64 }; 108static const int *grip_abs[] = { NULL, NULL, grip_abs_gp, grip_abs_c64 };
95static int *grip_btn[] = { NULL, NULL, grip_btn_gp, grip_btn_c64 }; 109static const int *grip_btn[] = { NULL, NULL, grip_btn_gp, grip_btn_c64 };
96 110
97static char *grip_name[] = { NULL, NULL, "Gravis Grip Pad", "Commodore 64 Joystick" }; 111static const char *grip_name[] = { NULL, NULL, "Gravis Grip Pad", "Commodore 64 Joystick" };
98 112
99static const int init_seq[] = { 113static const int init_seq[] = {
100 1, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 114 1, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1,
@@ -104,9 +118,9 @@ static const int init_seq[] = {
104 118
105/* Maps multiport directional values to X,Y axis values (each axis encoded in 3 bits) */ 119/* Maps multiport directional values to X,Y axis values (each axis encoded in 3 bits) */
106 120
107static int axis_map[] = { 5, 9, 1, 5, 6, 10, 2, 6, 4, 8, 0, 4, 5, 9, 1, 5 }; 121static const int axis_map[] = { 5, 9, 1, 5, 6, 10, 2, 6, 4, 8, 0, 4, 5, 9, 1, 5 };
108 122
109static void register_slot(int i, struct grip_mp *grip); 123static int register_slot(int i, struct grip_mp *grip);
110 124
111/* 125/*
112 * Returns whether an odd or even number of bits are on in pkt. 126 * Returns whether an odd or even number of bits are on in pkt.
@@ -353,9 +367,10 @@ static int dig_mode_start(struct gameport *gameport, u32 *packet)
353 367
354static int get_and_decode_packet(struct grip_mp *grip, int flags) 368static int get_and_decode_packet(struct grip_mp *grip, int flags)
355{ 369{
370 struct grip_port *port;
356 u32 packet; 371 u32 packet;
357 int joytype = 0; 372 int joytype = 0;
358 int slot = 0; 373 int slot;
359 374
360 /* Get a packet and check for validity */ 375 /* Get a packet and check for validity */
361 376
@@ -377,6 +392,8 @@ static int get_and_decode_packet(struct grip_mp *grip, int flags)
377 if ((slot < 0) || (slot > 3)) 392 if ((slot < 0) || (slot > 3))
378 return flags; 393 return flags;
379 394
395 port = grip->port[slot];
396
380 /* 397 /*
381 * Handle "reset" packets, which occur at startup, and when gamepads 398 * Handle "reset" packets, which occur at startup, and when gamepads
382 * are removed or plugged in. May contain configuration of a new gamepad. 399 * are removed or plugged in. May contain configuration of a new gamepad.
@@ -385,14 +402,14 @@ static int get_and_decode_packet(struct grip_mp *grip, int flags)
385 joytype = (packet >> 16) & 0x1f; 402 joytype = (packet >> 16) & 0x1f;
386 if (!joytype) { 403 if (!joytype) {
387 404
388 if (grip->registered[slot]) { 405 if (port->registered) {
389 printk(KERN_INFO "grip_mp: removing %s, slot %d\n", 406 printk(KERN_INFO "grip_mp: removing %s, slot %d\n",
390 grip_name[grip->mode[slot]], slot); 407 grip_name[port->mode], slot);
391 input_unregister_device(grip->dev + slot); 408 input_unregister_device(port->dev);
392 grip->registered[slot] = 0; 409 port->registered = 0;
393 } 410 }
394 dbg("Reset: grip multiport slot %d\n", slot); 411 dbg("Reset: grip multiport slot %d\n", slot);
395 grip->mode[slot] = GRIP_MODE_RESET; 412 port->mode = GRIP_MODE_RESET;
396 flags |= IO_SLOT_CHANGE; 413 flags |= IO_SLOT_CHANGE;
397 return flags; 414 return flags;
398 } 415 }
@@ -402,17 +419,17 @@ static int get_and_decode_packet(struct grip_mp *grip, int flags)
402 if (joytype == 0x1f) { 419 if (joytype == 0x1f) {
403 420
404 int dir = (packet >> 8) & 0xf; /* eight way directional value */ 421 int dir = (packet >> 8) & 0xf; /* eight way directional value */
405 grip->buttons[slot] = (~packet) & 0xff; 422 port->buttons = (~packet) & 0xff;
406 grip->yaxes[slot] = ((axis_map[dir] >> 2) & 3) - 1; 423 port->yaxes = ((axis_map[dir] >> 2) & 3) - 1;
407 grip->xaxes[slot] = (axis_map[dir] & 3) - 1; 424 port->xaxes = (axis_map[dir] & 3) - 1;
408 grip->dirty[slot] = 1; 425 port->dirty = 1;
409 426
410 if (grip->mode[slot] == GRIP_MODE_RESET) 427 if (port->mode == GRIP_MODE_RESET)
411 flags |= IO_SLOT_CHANGE; 428 flags |= IO_SLOT_CHANGE;
412 429
413 grip->mode[slot] = GRIP_MODE_GP; 430 port->mode = GRIP_MODE_GP;
414 431
415 if (!grip->registered[slot]) { 432 if (!port->registered) {
416 dbg("New Grip pad in multiport slot %d.\n", slot); 433 dbg("New Grip pad in multiport slot %d.\n", slot);
417 register_slot(slot, grip); 434 register_slot(slot, grip);
418 } 435 }
@@ -445,9 +462,9 @@ static int slots_valid(struct grip_mp *grip)
445 return 0; 462 return 0;
446 463
447 for (slot = 0; slot < 4; slot++) { 464 for (slot = 0; slot < 4; slot++) {
448 if (grip->mode[slot] == GRIP_MODE_RESET) 465 if (grip->port[slot]->mode == GRIP_MODE_RESET)
449 invalid = 1; 466 invalid = 1;
450 if (grip->mode[slot] != GRIP_MODE_NONE) 467 if (grip->port[slot]->mode != GRIP_MODE_NONE)
451 active = 1; 468 active = 1;
452 } 469 }
453 470
@@ -484,7 +501,7 @@ static int multiport_init(struct grip_mp *grip)
484 501
485 /* Get packets, store multiport state, and check state's validity */ 502 /* Get packets, store multiport state, and check state's validity */
486 for (tries = 0; tries < 4096; tries++) { 503 for (tries = 0; tries < 4096; tries++) {
487 if ( slots_valid(grip) ) { 504 if (slots_valid(grip)) {
488 initialized = 1; 505 initialized = 1;
489 break; 506 break;
490 } 507 }
@@ -499,24 +516,24 @@ static int multiport_init(struct grip_mp *grip)
499 516
500static void report_slot(struct grip_mp *grip, int slot) 517static void report_slot(struct grip_mp *grip, int slot)
501{ 518{
502 struct input_dev *dev = &(grip->dev[slot]); 519 struct grip_port *port = grip->port[slot];
503 int i, buttons = grip->buttons[slot]; 520 int i;
504 521
505 /* Store button states with linux input driver */ 522 /* Store button states with linux input driver */
506 523
507 for (i = 0; i < 8; i++) 524 for (i = 0; i < 8; i++)
508 input_report_key(dev, grip_btn_gp[i], (buttons >> i) & 1); 525 input_report_key(port->dev, grip_btn_gp[i], (port->buttons >> i) & 1);
509 526
510 /* Store axis states with linux driver */ 527 /* Store axis states with linux driver */
511 528
512 input_report_abs(dev, ABS_X, grip->xaxes[slot]); 529 input_report_abs(port->dev, ABS_X, port->xaxes);
513 input_report_abs(dev, ABS_Y, grip->yaxes[slot]); 530 input_report_abs(port->dev, ABS_Y, port->yaxes);
514 531
515 /* Tell the receiver of the events to process them */ 532 /* Tell the receiver of the events to process them */
516 533
517 input_sync(dev); 534 input_sync(port->dev);
518 535
519 grip->dirty[slot] = 0; 536 port->dirty = 0;
520} 537}
521 538
522/* 539/*
@@ -540,7 +557,7 @@ static void grip_poll(struct gameport *gameport)
540 } 557 }
541 558
542 for (i = 0; i < 4; i++) 559 for (i = 0; i < 4; i++)
543 if (grip->dirty[i]) 560 if (grip->port[i]->dirty)
544 report_slot(grip, i); 561 report_slot(grip, i);
545} 562}
546 563
@@ -571,35 +588,43 @@ static void grip_close(struct input_dev *dev)
571 * Tell the linux input layer about a newly plugged-in gamepad. 588 * Tell the linux input layer about a newly plugged-in gamepad.
572 */ 589 */
573 590
574static void register_slot(int slot, struct grip_mp *grip) 591static int register_slot(int slot, struct grip_mp *grip)
575{ 592{
593 struct grip_port *port = grip->port[slot];
594 struct input_dev *input_dev;
576 int j, t; 595 int j, t;
577 596
578 grip->dev[slot].private = grip; 597 port->dev = input_dev = input_allocate_device();
579 grip->dev[slot].open = grip_open; 598 if (!input_dev)
580 grip->dev[slot].close = grip_close; 599 return -ENOMEM;
581 grip->dev[slot].name = grip_name[grip->mode[slot]]; 600
582 grip->dev[slot].id.bustype = BUS_GAMEPORT; 601 input_dev->name = grip_name[port->mode];
583 grip->dev[slot].id.vendor = GAMEPORT_ID_VENDOR_GRAVIS; 602 input_dev->id.bustype = BUS_GAMEPORT;
584 grip->dev[slot].id.product = 0x0100 + grip->mode[slot]; 603 input_dev->id.vendor = GAMEPORT_ID_VENDOR_GRAVIS;
585 grip->dev[slot].id.version = 0x0100; 604 input_dev->id.product = 0x0100 + port->mode;
586 grip->dev[slot].evbit[0] = BIT(EV_KEY) | BIT(EV_ABS); 605 input_dev->id.version = 0x0100;
606 input_dev->cdev.dev = &grip->gameport->dev;
607 input_dev->private = grip;
608
609 input_dev->open = grip_open;
610 input_dev->close = grip_close;
587 611
588 for (j = 0; (t = grip_abs[grip->mode[slot]][j]) >= 0; j++) 612 input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
589 input_set_abs_params(&grip->dev[slot], t, -1, 1, 0, 0);
590 613
591 for (j = 0; (t = grip_btn[grip->mode[slot]][j]) >= 0; j++) 614 for (j = 0; (t = grip_abs[port->mode][j]) >= 0; j++)
615 input_set_abs_params(input_dev, t, -1, 1, 0, 0);
616
617 for (j = 0; (t = grip_btn[port->mode][j]) >= 0; j++)
592 if (t > 0) 618 if (t > 0)
593 set_bit(t, grip->dev[slot].keybit); 619 set_bit(t, input_dev->keybit);
594 620
595 input_register_device(grip->dev + slot); 621 input_register_device(port->dev);
596 grip->registered[slot] = 1; 622 port->registered = 1;
597 623
598 if (grip->dirty[slot]) /* report initial state, if any */ 624 if (port->dirty) /* report initial state, if any */
599 report_slot(grip, slot); 625 report_slot(grip, slot);
600 626
601 printk(KERN_INFO "grip_mp: added %s, slot %d\n", 627 return 0;
602 grip_name[grip->mode[slot]], slot);
603} 628}
604 629
605static int grip_connect(struct gameport *gameport, struct gameport_driver *drv) 630static int grip_connect(struct gameport *gameport, struct gameport_driver *drv)
@@ -626,7 +651,7 @@ static int grip_connect(struct gameport *gameport, struct gameport_driver *drv)
626 goto fail2; 651 goto fail2;
627 } 652 }
628 653
629 if (!grip->mode[0] && !grip->mode[1] && !grip->mode[2] && !grip->mode[3]) { 654 if (!grip->port[0]->mode && !grip->port[1]->mode && !grip->port[2]->mode && !grip->port[3]->mode) {
630 /* nothing plugged in */ 655 /* nothing plugged in */
631 err = -ENODEV; 656 err = -ENODEV;
632 goto fail2; 657 goto fail2;
@@ -646,8 +671,8 @@ static void grip_disconnect(struct gameport *gameport)
646 int i; 671 int i;
647 672
648 for (i = 0; i < 4; i++) 673 for (i = 0; i < 4; i++)
649 if (grip->registered[i]) 674 if (grip->port[i]->registered)
650 input_unregister_device(grip->dev + i); 675 input_unregister_device(grip->port[i]->dev);
651 gameport_close(gameport); 676 gameport_close(gameport);
652 gameport_set_drvdata(gameport, NULL); 677 gameport_set_drvdata(gameport, NULL);
653 kfree(grip); 678 kfree(grip);