lightning.c 6.7 KB

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