ns558.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /*
  2. * $Id: ns558.c,v 1.43 2002/01/24 19:23:21 vojtech Exp $
  3. *
  4. * Copyright (c) 1999-2001 Vojtech Pavlik
  5. * Copyright (c) 1999 Brian Gerst
  6. */
  7. /*
  8. * NS558 based standard IBM game port driver for Linux
  9. */
  10. /*
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  24. *
  25. * Should you need to contact me, the author, you can do so either by
  26. * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail:
  27. * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic
  28. */
  29. #include <asm/io.h>
  30. #include <linux/module.h>
  31. #include <linux/ioport.h>
  32. #include <linux/init.h>
  33. #include <linux/delay.h>
  34. #include <linux/gameport.h>
  35. #include <linux/slab.h>
  36. #include <linux/pnp.h>
  37. MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
  38. MODULE_DESCRIPTION("Classic gameport (ISA/PnP) driver");
  39. MODULE_LICENSE("GPL");
  40. static int ns558_isa_portlist[] = { 0x201, 0x200, 0x202, 0x203, 0x204, 0x205, 0x207, 0x209,
  41. 0x20b, 0x20c, 0x20e, 0x20f, 0x211, 0x219, 0x101, 0 };
  42. struct ns558 {
  43. int type;
  44. int io;
  45. int size;
  46. struct pnp_dev *dev;
  47. struct gameport *gameport;
  48. struct list_head node;
  49. };
  50. static LIST_HEAD(ns558_list);
  51. /*
  52. * ns558_isa_probe() tries to find an isa gameport at the
  53. * specified address, and also checks for mirrors.
  54. * A joystick must be attached for this to work.
  55. */
  56. static int ns558_isa_probe(int io)
  57. {
  58. int i, j, b;
  59. unsigned char c, u, v;
  60. struct ns558 *ns558;
  61. struct gameport *port;
  62. /*
  63. * No one should be using this address.
  64. */
  65. if (!request_region(io, 1, "ns558-isa"))
  66. return -EBUSY;
  67. /*
  68. * We must not be able to write arbitrary values to the port.
  69. * The lower two axis bits must be 1 after a write.
  70. */
  71. c = inb(io);
  72. outb(~c & ~3, io);
  73. if (~(u = v = inb(io)) & 3) {
  74. outb(c, io);
  75. release_region(io, 1);
  76. return -ENODEV;
  77. }
  78. /*
  79. * After a trigger, there must be at least some bits changing.
  80. */
  81. for (i = 0; i < 1000; i++) v &= inb(io);
  82. if (u == v) {
  83. outb(c, io);
  84. release_region(io, 1);
  85. return -ENODEV;
  86. }
  87. msleep(3);
  88. /*
  89. * After some time (4ms) the axes shouldn't change anymore.
  90. */
  91. u = inb(io);
  92. for (i = 0; i < 1000; i++)
  93. if ((u ^ inb(io)) & 0xf) {
  94. outb(c, io);
  95. release_region(io, 1);
  96. return -ENODEV;
  97. }
  98. /*
  99. * And now find the number of mirrors of the port.
  100. */
  101. for (i = 1; i < 5; i++) {
  102. release_region(io & (-1 << (i - 1)), (1 << (i - 1)));
  103. if (!request_region(io & (-1 << i), (1 << i), "ns558-isa"))
  104. break; /* Don't disturb anyone */
  105. outb(0xff, io & (-1 << i));
  106. for (j = b = 0; j < 1000; j++)
  107. if (inb(io & (-1 << i)) != inb((io & (-1 << i)) + (1 << i) - 1)) b++;
  108. msleep(3);
  109. if (b > 300) { /* We allow 30% difference */
  110. release_region(io & (-1 << i), (1 << i));
  111. break;
  112. }
  113. }
  114. i--;
  115. if (i != 4) {
  116. if (!request_region(io & (-1 << i), (1 << i), "ns558-isa"))
  117. return -EBUSY;
  118. }
  119. ns558 = kzalloc(sizeof(struct ns558), GFP_KERNEL);
  120. port = gameport_allocate_port();
  121. if (!ns558 || !port) {
  122. printk(KERN_ERR "ns558: Memory allocation failed.\n");
  123. release_region(io & (-1 << i), (1 << i));
  124. kfree(ns558);
  125. gameport_free_port(port);
  126. return -ENOMEM;
  127. }
  128. memset(ns558, 0, sizeof(struct ns558));
  129. ns558->io = io;
  130. ns558->size = 1 << i;
  131. ns558->gameport = port;
  132. port->io = io;
  133. gameport_set_name(port, "NS558 ISA Gameport");
  134. gameport_set_phys(port, "isa%04x/gameport0", io & (-1 << i));
  135. gameport_register_port(port);
  136. list_add(&ns558->node, &ns558_list);
  137. return 0;
  138. }
  139. #ifdef CONFIG_PNP
  140. static struct pnp_device_id pnp_devids[] = {
  141. { .id = "@P@0001", .driver_data = 0 }, /* ALS 100 */
  142. { .id = "@P@0020", .driver_data = 0 }, /* ALS 200 */
  143. { .id = "@P@1001", .driver_data = 0 }, /* ALS 100+ */
  144. { .id = "@P@2001", .driver_data = 0 }, /* ALS 120 */
  145. { .id = "ASB16fd", .driver_data = 0 }, /* AdLib NSC16 */
  146. { .id = "AZT3001", .driver_data = 0 }, /* AZT1008 */
  147. { .id = "CDC0001", .driver_data = 0 }, /* Opl3-SAx */
  148. { .id = "CSC0001", .driver_data = 0 }, /* CS4232 */
  149. { .id = "CSC000f", .driver_data = 0 }, /* CS4236 */
  150. { .id = "CSC0101", .driver_data = 0 }, /* CS4327 */
  151. { .id = "CTL7001", .driver_data = 0 }, /* SB16 */
  152. { .id = "CTL7002", .driver_data = 0 }, /* AWE64 */
  153. { .id = "CTL7005", .driver_data = 0 }, /* Vibra16 */
  154. { .id = "ENS2020", .driver_data = 0 }, /* SoundscapeVIVO */
  155. { .id = "ESS0001", .driver_data = 0 }, /* ES1869 */
  156. { .id = "ESS0005", .driver_data = 0 }, /* ES1878 */
  157. { .id = "ESS6880", .driver_data = 0 }, /* ES688 */
  158. { .id = "IBM0012", .driver_data = 0 }, /* CS4232 */
  159. { .id = "OPT0001", .driver_data = 0 }, /* OPTi Audio16 */
  160. { .id = "YMH0006", .driver_data = 0 }, /* Opl3-SA */
  161. { .id = "YMH0022", .driver_data = 0 }, /* Opl3-SAx */
  162. { .id = "PNPb02f", .driver_data = 0 }, /* Generic */
  163. { .id = "", },
  164. };
  165. MODULE_DEVICE_TABLE(pnp, pnp_devids);
  166. static int ns558_pnp_probe(struct pnp_dev *dev, const struct pnp_device_id *did)
  167. {
  168. int ioport, iolen;
  169. struct ns558 *ns558;
  170. struct gameport *port;
  171. if (!pnp_port_valid(dev, 0)) {
  172. printk(KERN_WARNING "ns558: No i/o ports on a gameport? Weird\n");
  173. return -ENODEV;
  174. }
  175. ioport = pnp_port_start(dev, 0);
  176. iolen = pnp_port_len(dev, 0);
  177. if (!request_region(ioport, iolen, "ns558-pnp"))
  178. return -EBUSY;
  179. ns558 = kzalloc(sizeof(struct ns558), GFP_KERNEL);
  180. port = gameport_allocate_port();
  181. if (!ns558 || !port) {
  182. printk(KERN_ERR "ns558: Memory allocation failed\n");
  183. kfree(ns558);
  184. gameport_free_port(port);
  185. return -ENOMEM;
  186. }
  187. ns558->io = ioport;
  188. ns558->size = iolen;
  189. ns558->dev = dev;
  190. ns558->gameport = port;
  191. gameport_set_name(port, "NS558 PnP Gameport");
  192. gameport_set_phys(port, "pnp%s/gameport0", dev->dev.bus_id);
  193. port->dev.parent = &dev->dev;
  194. port->io = ioport;
  195. gameport_register_port(port);
  196. list_add_tail(&ns558->node, &ns558_list);
  197. return 0;
  198. }
  199. static struct pnp_driver ns558_pnp_driver = {
  200. .name = "ns558",
  201. .id_table = pnp_devids,
  202. .probe = ns558_pnp_probe,
  203. };
  204. #else
  205. static struct pnp_driver ns558_pnp_driver;
  206. #endif
  207. static int __init ns558_init(void)
  208. {
  209. int i = 0;
  210. int error;
  211. error = pnp_register_driver(&ns558_pnp_driver);
  212. if (error && error != -ENODEV) /* should be ENOSYS really */
  213. return error;
  214. /*
  215. * Probe ISA ports after PnP, so that PnP ports that are already
  216. * enabled get detected as PnP. This may be suboptimal in multi-device
  217. * configurations, but saves hassle with simple setups.
  218. */
  219. while (ns558_isa_portlist[i])
  220. ns558_isa_probe(ns558_isa_portlist[i++]);
  221. return list_empty(&ns558_list) && error ? -ENODEV : 0;
  222. }
  223. static void __exit ns558_exit(void)
  224. {
  225. struct ns558 *ns558, *safe;
  226. list_for_each_entry_safe(ns558, safe, &ns558_list, node) {
  227. gameport_unregister_port(ns558->gameport);
  228. release_region(ns558->io & ~(ns558->size - 1), ns558->size);
  229. kfree(ns558);
  230. }
  231. pnp_unregister_driver(&ns558_pnp_driver);
  232. }
  233. module_init(ns558_init);
  234. module_exit(ns558_exit);