lightning.c 6.0 KB

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