ns558.c 6.3 KB

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