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