lightning.c 6.9 KB

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