lightning.c 7.0 KB

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