lightning.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (c) 1998-2001 Vojtech Pavlik
  4. */
  5. /*
  6. * PDPI Lightning 4 gamecard driver for Linux.
  7. */
  8. #include <asm/io.h>
  9. #include <linux/delay.h>
  10. #include <linux/errno.h>
  11. #include <linux/ioport.h>
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/init.h>
  15. #include <linux/gameport.h>
  16. #define L4_PORT 0x201
  17. #define L4_SELECT_ANALOG 0xa4
  18. #define L4_SELECT_DIGITAL 0xa5
  19. #define L4_SELECT_SECONDARY 0xa6
  20. #define L4_CMD_ID 0x80
  21. #define L4_CMD_GETCAL 0x92
  22. #define L4_CMD_SETCAL 0x93
  23. #define L4_ID 0x04
  24. #define L4_BUSY 0x01
  25. #define L4_TIMEOUT 80 /* 80 us */
  26. MODULE_AUTHOR("Vojtech Pavlik <[email protected]>");
  27. MODULE_DESCRIPTION("PDPI Lightning 4 gamecard driver");
  28. MODULE_LICENSE("GPL");
  29. struct l4 {
  30. struct gameport *gameport;
  31. unsigned char port;
  32. };
  33. static struct l4 l4_ports[8];
  34. /*
  35. * l4_wait_ready() waits for the L4 to become ready.
  36. */
  37. static int l4_wait_ready(void)
  38. {
  39. unsigned int t = L4_TIMEOUT;
  40. while ((inb(L4_PORT) & L4_BUSY) && t > 0) t--;
  41. return -(t <= 0);
  42. }
  43. /*
  44. * l4_cooked_read() reads data from the Lightning 4.
  45. */
  46. static int l4_cooked_read(struct gameport *gameport, int *axes, int *buttons)
  47. {
  48. struct l4 *l4 = gameport->port_data;
  49. unsigned char status;
  50. int i, result = -1;
  51. outb(L4_SELECT_ANALOG, L4_PORT);
  52. outb(L4_SELECT_DIGITAL + (l4->port >> 2), L4_PORT);
  53. if (inb(L4_PORT) & L4_BUSY) goto fail;
  54. outb(l4->port & 3, L4_PORT);
  55. if (l4_wait_ready()) goto fail;
  56. status = inb(L4_PORT);
  57. for (i = 0; i < 4; i++)
  58. if (status & (1 << i)) {
  59. if (l4_wait_ready()) goto fail;
  60. axes[i] = inb(L4_PORT);
  61. if (axes[i] > 252) axes[i] = -1;
  62. }
  63. if (status & 0x10) {
  64. if (l4_wait_ready()) goto fail;
  65. *buttons = inb(L4_PORT) & 0x0f;
  66. }
  67. result = 0;
  68. fail: outb(L4_SELECT_ANALOG, L4_PORT);
  69. return result;
  70. }
  71. static int l4_open(struct gameport *gameport, int mode)
  72. {
  73. struct l4 *l4 = gameport->port_data;
  74. if (l4->port != 0 && mode != GAMEPORT_MODE_COOKED)
  75. return -1;
  76. outb(L4_SELECT_ANALOG, L4_PORT);
  77. return 0;
  78. }
  79. /*
  80. * l4_getcal() reads the L4 with calibration values.
  81. */
  82. static int l4_getcal(int port, int *cal)
  83. {
  84. int i, result = -1;
  85. outb(L4_SELECT_ANALOG, L4_PORT);
  86. outb(L4_SELECT_DIGITAL + (port >> 2), L4_PORT);
  87. if (inb(L4_PORT) & L4_BUSY)
  88. goto out;
  89. outb(L4_CMD_GETCAL, L4_PORT);
  90. if (l4_wait_ready())
  91. goto out;
  92. if (inb(L4_PORT) != L4_SELECT_DIGITAL + (port >> 2))
  93. goto out;
  94. if (l4_wait_ready())
  95. goto out;
  96. outb(port & 3, L4_PORT);
  97. for (i = 0; i < 4; i++) {
  98. if (l4_wait_ready())
  99. goto out;
  100. cal[i] = inb(L4_PORT);
  101. }
  102. result = 0;
  103. out: outb(L4_SELECT_ANALOG, L4_PORT);
  104. return result;
  105. }
  106. /*
  107. * l4_setcal() programs the L4 with calibration values.
  108. */
  109. static int l4_setcal(int port, int *cal)
  110. {
  111. int i, result = -1;
  112. outb(L4_SELECT_ANALOG, L4_PORT);
  113. outb(L4_SELECT_DIGITAL + (port >> 2), L4_PORT);
  114. if (inb(L4_PORT) & L4_BUSY)
  115. goto out;
  116. outb(L4_CMD_SETCAL, L4_PORT);
  117. if (l4_wait_ready())
  118. goto out;
  119. if (inb(L4_PORT) != L4_SELECT_DIGITAL + (port >> 2))
  120. goto out;
  121. if (l4_wait_ready())
  122. goto out;
  123. outb(port & 3, L4_PORT);
  124. for (i = 0; i < 4; i++) {
  125. if (l4_wait_ready())
  126. goto out;
  127. outb(cal[i], L4_PORT);
  128. }
  129. result = 0;
  130. out: outb(L4_SELECT_ANALOG, L4_PORT);
  131. return result;
  132. }
  133. /*
  134. * l4_calibrate() calibrates the L4 for the attached device, so
  135. * that the device's resistance fits into the L4's 8-bit range.
  136. */
  137. static int l4_calibrate(struct gameport *gameport, int *axes, int *max)
  138. {
  139. int i, t;
  140. int cal[4];
  141. struct l4 *l4 = gameport->port_data;
  142. if (l4_getcal(l4->port, cal))
  143. return -1;
  144. for (i = 0; i < 4; i++) {
  145. t = (max[i] * cal[i]) / 200;
  146. t = (t < 1) ? 1 : ((t > 255) ? 255 : t);
  147. axes[i] = (axes[i] < 0) ? -1 : (axes[i] * cal[i]) / t;
  148. axes[i] = (axes[i] > 252) ? 252 : axes[i];
  149. cal[i] = t;
  150. }
  151. if (l4_setcal(l4->port, cal))
  152. return -1;
  153. return 0;
  154. }
  155. static int __init l4_create_ports(int card_no)
  156. {
  157. struct l4 *l4;
  158. struct gameport *port;
  159. int i, idx;
  160. for (i = 0; i < 4; i++) {
  161. idx = card_no * 4 + i;
  162. l4 = &l4_ports[idx];
  163. if (!(l4->gameport = port = gameport_allocate_port())) {
  164. printk(KERN_ERR "lightning: Memory allocation failed\n");
  165. while (--i >= 0) {
  166. gameport_free_port(l4->gameport);
  167. l4->gameport = NULL;
  168. }
  169. return -ENOMEM;
  170. }
  171. l4->port = idx;
  172. port->port_data = l4;
  173. port->open = l4_open;
  174. port->cooked_read = l4_cooked_read;
  175. port->calibrate = l4_calibrate;
  176. gameport_set_name(port, "PDPI Lightning 4");
  177. gameport_set_phys(port, "isa%04x/gameport%d", L4_PORT, idx);
  178. if (idx == 0)
  179. port->io = L4_PORT;
  180. }
  181. return 0;
  182. }
  183. static int __init l4_add_card(int card_no)
  184. {
  185. int cal[4] = { 255, 255, 255, 255 };
  186. int i, rev, result;
  187. struct l4 *l4;
  188. outb(L4_SELECT_ANALOG, L4_PORT);
  189. outb(L4_SELECT_DIGITAL + card_no, L4_PORT);
  190. if (inb(L4_PORT) & L4_BUSY)
  191. return -1;
  192. outb(L4_CMD_ID, L4_PORT);
  193. if (l4_wait_ready())
  194. return -1;
  195. if (inb(L4_PORT) != L4_SELECT_DIGITAL + card_no)
  196. return -1;
  197. if (l4_wait_ready())
  198. return -1;
  199. if (inb(L4_PORT) != L4_ID)
  200. return -1;
  201. if (l4_wait_ready())
  202. return -1;
  203. rev = inb(L4_PORT);
  204. if (!rev)
  205. return -1;
  206. result = l4_create_ports(card_no);
  207. if (result)
  208. return result;
  209. printk(KERN_INFO "gameport: PDPI Lightning 4 %s card v%d.%d at %#x\n",
  210. card_no ? "secondary" : "primary", rev >> 4, rev, L4_PORT);
  211. for (i = 0; i < 4; i++) {
  212. l4 = &l4_ports[card_no * 4 + i];
  213. if (rev > 0x28) /* on 2.9+ the setcal command works correctly */
  214. l4_setcal(l4->port, cal);
  215. gameport_register_port(l4->gameport);
  216. }
  217. return 0;
  218. }
  219. static int __init l4_init(void)
  220. {
  221. int i, cards = 0;
  222. if (!request_region(L4_PORT, 1, "lightning"))
  223. return -EBUSY;
  224. for (i = 0; i < 2; i++)
  225. if (l4_add_card(i) == 0)
  226. cards++;
  227. outb(L4_SELECT_ANALOG, L4_PORT);
  228. if (!cards) {
  229. release_region(L4_PORT, 1);
  230. return -ENODEV;
  231. }
  232. return 0;
  233. }
  234. static void __exit l4_exit(void)
  235. {
  236. int i;
  237. int cal[4] = { 59, 59, 59, 59 };
  238. for (i = 0; i < 8; i++)
  239. if (l4_ports[i].gameport) {
  240. l4_setcal(l4_ports[i].port, cal);
  241. gameport_unregister_port(l4_ports[i].gameport);
  242. }
  243. outb(L4_SELECT_ANALOG, L4_PORT);
  244. release_region(L4_PORT, 1);
  245. }
  246. module_init(l4_init);
  247. module_exit(l4_exit);