ns558.c 7.2 KB

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