au88x0_game.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * Manuel Jander.
  3. *
  4. * Based on the work of:
  5. * Vojtech Pavlik
  6. * Raymond Ingles
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. *
  22. * Should you need to contact me, the author, you can do so either by
  23. * e-mail - mail your message to <vojtech@suse.cz>, or by paper mail:
  24. * Vojtech Pavlik, Ucitelska 1576, Prague 8, 182 00 Czech Republic
  25. *
  26. * Based 90% on Vojtech Pavlik pcigame driver.
  27. * Merged and modified by Manuel Jander, for the OpenVortex
  28. * driver. (email: mjander@embedded.cl).
  29. */
  30. #include <linux/time.h>
  31. #include <linux/delay.h>
  32. #include <linux/init.h>
  33. #include <sound/core.h>
  34. #include "au88x0.h"
  35. #include <linux/gameport.h>
  36. #include <linux/export.h>
  37. #if IS_REACHABLE(CONFIG_GAMEPORT)
  38. #define VORTEX_GAME_DWAIT 20 /* 20 ms */
  39. static unsigned char vortex_game_read(struct gameport *gameport)
  40. {
  41. vortex_t *vortex = gameport_get_port_data(gameport);
  42. return hwread(vortex->mmio, VORTEX_GAME_LEGACY);
  43. }
  44. static void vortex_game_trigger(struct gameport *gameport)
  45. {
  46. vortex_t *vortex = gameport_get_port_data(gameport);
  47. hwwrite(vortex->mmio, VORTEX_GAME_LEGACY, 0xff);
  48. }
  49. static int
  50. vortex_game_cooked_read(struct gameport *gameport, int *axes, int *buttons)
  51. {
  52. vortex_t *vortex = gameport_get_port_data(gameport);
  53. int i;
  54. *buttons = (~hwread(vortex->mmio, VORTEX_GAME_LEGACY) >> 4) & 0xf;
  55. for (i = 0; i < 4; i++) {
  56. axes[i] =
  57. hwread(vortex->mmio, VORTEX_GAME_AXIS + (i * AXIS_SIZE));
  58. if (axes[i] == AXIS_RANGE)
  59. axes[i] = -1;
  60. }
  61. return 0;
  62. }
  63. static int vortex_game_open(struct gameport *gameport, int mode)
  64. {
  65. vortex_t *vortex = gameport_get_port_data(gameport);
  66. switch (mode) {
  67. case GAMEPORT_MODE_COOKED:
  68. hwwrite(vortex->mmio, VORTEX_CTRL2,
  69. hwread(vortex->mmio,
  70. VORTEX_CTRL2) | CTRL2_GAME_ADCMODE);
  71. msleep(VORTEX_GAME_DWAIT);
  72. return 0;
  73. case GAMEPORT_MODE_RAW:
  74. hwwrite(vortex->mmio, VORTEX_CTRL2,
  75. hwread(vortex->mmio,
  76. VORTEX_CTRL2) & ~CTRL2_GAME_ADCMODE);
  77. return 0;
  78. default:
  79. return -1;
  80. }
  81. return 0;
  82. }
  83. static int vortex_gameport_register(vortex_t *vortex)
  84. {
  85. struct gameport *gp;
  86. vortex->gameport = gp = gameport_allocate_port();
  87. if (!gp) {
  88. dev_err(vortex->card->dev,
  89. "cannot allocate memory for gameport\n");
  90. return -ENOMEM;
  91. }
  92. gameport_set_name(gp, "AU88x0 Gameport");
  93. gameport_set_phys(gp, "pci%s/gameport0", pci_name(vortex->pci_dev));
  94. gameport_set_dev_parent(gp, &vortex->pci_dev->dev);
  95. gp->read = vortex_game_read;
  96. gp->trigger = vortex_game_trigger;
  97. gp->cooked_read = vortex_game_cooked_read;
  98. gp->open = vortex_game_open;
  99. gameport_set_port_data(gp, vortex);
  100. gp->fuzz = 64;
  101. gameport_register_port(gp);
  102. return 0;
  103. }
  104. static void vortex_gameport_unregister(vortex_t * vortex)
  105. {
  106. if (vortex->gameport) {
  107. gameport_unregister_port(vortex->gameport);
  108. vortex->gameport = NULL;
  109. }
  110. }
  111. #else
  112. static inline int vortex_gameport_register(vortex_t * vortex) { return -ENOSYS; }
  113. static inline void vortex_gameport_unregister(vortex_t * vortex) { }
  114. #endif