ns558.c 7.1 KB

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