ns558.c 6.9 KB

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