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