fm801-gp.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. * FM801 gameport driver for Linux
  3. *
  4. * Copyright (c) by Takashi Iwai <tiwai@suse.de>
  5. *
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. *
  21. */
  22. #include <asm/io.h>
  23. #include <linux/delay.h>
  24. #include <linux/errno.h>
  25. #include <linux/ioport.h>
  26. #include <linux/kernel.h>
  27. #include <linux/module.h>
  28. #include <linux/pci.h>
  29. #include <linux/init.h>
  30. #include <linux/slab.h>
  31. #include <linux/gameport.h>
  32. #define PCI_VENDOR_ID_FORTEMEDIA 0x1319
  33. #define PCI_DEVICE_ID_FM801_GP 0x0802
  34. #define HAVE_COOKED
  35. struct fm801_gp {
  36. struct gameport *gameport;
  37. struct resource *res_port;
  38. };
  39. #ifdef HAVE_COOKED
  40. static int fm801_gp_cooked_read(struct gameport *gameport, int *axes, int *buttons)
  41. {
  42. unsigned short w;
  43. w = inw(gameport->io + 2);
  44. *buttons = (~w >> 14) & 0x03;
  45. axes[0] = (w == 0xffff) ? -1 : ((w & 0x1fff) << 5);
  46. w = inw(gameport->io + 4);
  47. axes[1] = (w == 0xffff) ? -1 : ((w & 0x1fff) << 5);
  48. w = inw(gameport->io + 6);
  49. *buttons |= ((~w >> 14) & 0x03) << 2;
  50. axes[2] = (w == 0xffff) ? -1 : ((w & 0x1fff) << 5);
  51. w = inw(gameport->io + 8);
  52. axes[3] = (w == 0xffff) ? -1 : ((w & 0x1fff) << 5);
  53. outw(0xff, gameport->io); /* reset */
  54. return 0;
  55. }
  56. #endif
  57. static int fm801_gp_open(struct gameport *gameport, int mode)
  58. {
  59. switch (mode) {
  60. #ifdef HAVE_COOKED
  61. case GAMEPORT_MODE_COOKED:
  62. return 0;
  63. #endif
  64. case GAMEPORT_MODE_RAW:
  65. return 0;
  66. default:
  67. return -1;
  68. }
  69. return 0;
  70. }
  71. static int __devinit fm801_gp_probe(struct pci_dev *pci, const struct pci_device_id *id)
  72. {
  73. struct fm801_gp *gp;
  74. struct gameport *port;
  75. gp = kzalloc(sizeof(struct fm801_gp), GFP_KERNEL);
  76. port = gameport_allocate_port();
  77. if (!gp || !port) {
  78. printk(KERN_ERR "fm801-gp: Memory allocation failed\n");
  79. kfree(gp);
  80. gameport_free_port(port);
  81. return -ENOMEM;
  82. }
  83. pci_enable_device(pci);
  84. port->open = fm801_gp_open;
  85. #ifdef HAVE_COOKED
  86. port->cooked_read = fm801_gp_cooked_read;
  87. #endif
  88. gameport_set_name(port, "FM801");
  89. gameport_set_phys(port, "pci%s/gameport0", pci_name(pci));
  90. port->dev.parent = &pci->dev;
  91. port->io = pci_resource_start(pci, 0);
  92. gp->gameport = port;
  93. gp->res_port = request_region(port->io, 0x10, "FM801 GP");
  94. if (!gp->res_port) {
  95. kfree(gp);
  96. gameport_free_port(port);
  97. printk(KERN_DEBUG "fm801-gp: unable to grab region 0x%x-0x%x\n",
  98. port->io, port->io + 0x0f);
  99. return -EBUSY;
  100. }
  101. pci_set_drvdata(pci, gp);
  102. outb(0x60, port->io + 0x0d); /* enable joystick 1 and 2 */
  103. gameport_register_port(port);
  104. return 0;
  105. }
  106. static void __devexit fm801_gp_remove(struct pci_dev *pci)
  107. {
  108. struct fm801_gp *gp = pci_get_drvdata(pci);
  109. if (gp) {
  110. gameport_unregister_port(gp->gameport);
  111. release_resource(gp->res_port);
  112. kfree(gp);
  113. }
  114. }
  115. static struct pci_device_id fm801_gp_id_table[] = {
  116. { PCI_VENDOR_ID_FORTEMEDIA, PCI_DEVICE_ID_FM801_GP, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
  117. { 0 }
  118. };
  119. static struct pci_driver fm801_gp_driver = {
  120. .name = "FM801_gameport",
  121. .id_table = fm801_gp_id_table,
  122. .probe = fm801_gp_probe,
  123. .remove = __devexit_p(fm801_gp_remove),
  124. };
  125. static int __init fm801_gp_init(void)
  126. {
  127. return pci_register_driver(&fm801_gp_driver);
  128. }
  129. static void __exit fm801_gp_exit(void)
  130. {
  131. pci_unregister_driver(&fm801_gp_driver);
  132. }
  133. module_init(fm801_gp_init);
  134. module_exit(fm801_gp_exit);
  135. MODULE_DEVICE_TABLE(pci, fm801_gp_id_table);
  136. MODULE_AUTHOR("Takashi Iwai <tiwai@suse.de>");
  137. MODULE_LICENSE("GPL");