turbografx.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (c) 1998-2001 Vojtech Pavlik
  4. *
  5. * Based on the work of:
  6. * Steffen Schwenke
  7. */
  8. /*
  9. * TurboGraFX parallel port interface driver for Linux.
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/parport.h>
  13. #include <linux/input.h>
  14. #include <linux/module.h>
  15. #include <linux/init.h>
  16. #include <linux/mutex.h>
  17. #include <linux/slab.h>
  18. MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
  19. MODULE_DESCRIPTION("TurboGraFX parallel port interface driver");
  20. MODULE_LICENSE("GPL");
  21. #define TGFX_MAX_PORTS 3
  22. #define TGFX_MAX_DEVICES 7
  23. struct tgfx_config {
  24. int args[TGFX_MAX_DEVICES + 1];
  25. unsigned int nargs;
  26. };
  27. static struct tgfx_config tgfx_cfg[TGFX_MAX_PORTS];
  28. module_param_array_named(map, tgfx_cfg[0].args, int, &tgfx_cfg[0].nargs, 0);
  29. MODULE_PARM_DESC(map, "Describes first set of devices (<parport#>,<js1>,<js2>,..<js7>");
  30. module_param_array_named(map2, tgfx_cfg[1].args, int, &tgfx_cfg[1].nargs, 0);
  31. MODULE_PARM_DESC(map2, "Describes second set of devices");
  32. module_param_array_named(map3, tgfx_cfg[2].args, int, &tgfx_cfg[2].nargs, 0);
  33. MODULE_PARM_DESC(map3, "Describes third set of devices");
  34. #define TGFX_REFRESH_TIME HZ/100 /* 10 ms */
  35. #define TGFX_TRIGGER 0x08
  36. #define TGFX_UP 0x10
  37. #define TGFX_DOWN 0x20
  38. #define TGFX_LEFT 0x40
  39. #define TGFX_RIGHT 0x80
  40. #define TGFX_THUMB 0x02
  41. #define TGFX_THUMB2 0x04
  42. #define TGFX_TOP 0x01
  43. #define TGFX_TOP2 0x08
  44. static int tgfx_buttons[] = { BTN_TRIGGER, BTN_THUMB, BTN_THUMB2, BTN_TOP, BTN_TOP2 };
  45. static struct tgfx {
  46. struct pardevice *pd;
  47. struct timer_list timer;
  48. struct input_dev *dev[TGFX_MAX_DEVICES];
  49. char name[TGFX_MAX_DEVICES][64];
  50. char phys[TGFX_MAX_DEVICES][32];
  51. int sticks;
  52. int used;
  53. int parportno;
  54. struct mutex sem;
  55. } *tgfx_base[TGFX_MAX_PORTS];
  56. /*
  57. * tgfx_timer() reads and analyzes TurboGraFX joystick data.
  58. */
  59. static void tgfx_timer(struct timer_list *t)
  60. {
  61. struct tgfx *tgfx = timer_container_of(tgfx, t, timer);
  62. struct input_dev *dev;
  63. int data1, data2, i;
  64. for (i = 0; i < 7; i++)
  65. if (tgfx->sticks & (1 << i)) {
  66. dev = tgfx->dev[i];
  67. parport_write_data(tgfx->pd->port, ~(1 << i));
  68. data1 = parport_read_status(tgfx->pd->port) ^ 0x7f;
  69. data2 = parport_read_control(tgfx->pd->port) ^ 0x04; /* CAVEAT parport */
  70. input_report_abs(dev, ABS_X, !!(data1 & TGFX_RIGHT) - !!(data1 & TGFX_LEFT));
  71. input_report_abs(dev, ABS_Y, !!(data1 & TGFX_DOWN ) - !!(data1 & TGFX_UP ));
  72. input_report_key(dev, BTN_TRIGGER, (data1 & TGFX_TRIGGER));
  73. input_report_key(dev, BTN_THUMB, (data2 & TGFX_THUMB ));
  74. input_report_key(dev, BTN_THUMB2, (data2 & TGFX_THUMB2 ));
  75. input_report_key(dev, BTN_TOP, (data2 & TGFX_TOP ));
  76. input_report_key(dev, BTN_TOP2, (data2 & TGFX_TOP2 ));
  77. input_sync(dev);
  78. }
  79. mod_timer(&tgfx->timer, jiffies + TGFX_REFRESH_TIME);
  80. }
  81. static int tgfx_open(struct input_dev *dev)
  82. {
  83. struct tgfx *tgfx = input_get_drvdata(dev);
  84. scoped_guard(mutex_intr, &tgfx->sem) {
  85. if (!tgfx->used++) {
  86. parport_claim(tgfx->pd);
  87. parport_write_control(tgfx->pd->port, 0x04);
  88. mod_timer(&tgfx->timer, jiffies + TGFX_REFRESH_TIME);
  89. }
  90. return 0;
  91. }
  92. return -EINTR;
  93. }
  94. static void tgfx_close(struct input_dev *dev)
  95. {
  96. struct tgfx *tgfx = input_get_drvdata(dev);
  97. guard(mutex)(&tgfx->sem);
  98. if (!--tgfx->used) {
  99. timer_delete_sync(&tgfx->timer);
  100. parport_write_control(tgfx->pd->port, 0x00);
  101. parport_release(tgfx->pd);
  102. }
  103. }
  104. /*
  105. * tgfx_probe() probes for tg gamepads.
  106. */
  107. static void tgfx_attach(struct parport *pp)
  108. {
  109. struct tgfx *tgfx;
  110. struct input_dev *input_dev;
  111. struct pardevice *pd;
  112. int i, j, port_idx;
  113. int *n_buttons, n_devs;
  114. struct pardev_cb tgfx_parport_cb;
  115. for (port_idx = 0; port_idx < TGFX_MAX_PORTS; port_idx++) {
  116. if (tgfx_cfg[port_idx].nargs == 0 ||
  117. tgfx_cfg[port_idx].args[0] < 0)
  118. continue;
  119. if (tgfx_cfg[port_idx].args[0] == pp->number)
  120. break;
  121. }
  122. if (port_idx == TGFX_MAX_PORTS) {
  123. pr_debug("Not using parport%d.\n", pp->number);
  124. return;
  125. }
  126. n_buttons = tgfx_cfg[port_idx].args + 1;
  127. n_devs = tgfx_cfg[port_idx].nargs - 1;
  128. memset(&tgfx_parport_cb, 0, sizeof(tgfx_parport_cb));
  129. tgfx_parport_cb.flags = PARPORT_FLAG_EXCL;
  130. pd = parport_register_dev_model(pp, "turbografx", &tgfx_parport_cb,
  131. port_idx);
  132. if (!pd) {
  133. pr_err("parport busy already - lp.o loaded?\n");
  134. return;
  135. }
  136. tgfx = kzalloc_obj(*tgfx);
  137. if (!tgfx) {
  138. printk(KERN_ERR "turbografx.c: Not enough memory\n");
  139. goto err_unreg_pardev;
  140. }
  141. mutex_init(&tgfx->sem);
  142. tgfx->pd = pd;
  143. tgfx->parportno = pp->number;
  144. timer_setup(&tgfx->timer, tgfx_timer, 0);
  145. for (i = 0; i < n_devs; i++) {
  146. if (n_buttons[i] < 1)
  147. continue;
  148. if (n_buttons[i] > ARRAY_SIZE(tgfx_buttons)) {
  149. printk(KERN_ERR "turbografx.c: Invalid number of buttons %d\n", n_buttons[i]);
  150. goto err_unreg_devs;
  151. }
  152. tgfx->dev[i] = input_dev = input_allocate_device();
  153. if (!input_dev) {
  154. printk(KERN_ERR "turbografx.c: Not enough memory for input device\n");
  155. goto err_unreg_devs;
  156. }
  157. tgfx->sticks |= (1 << i);
  158. snprintf(tgfx->name[i], sizeof(tgfx->name[i]),
  159. "TurboGraFX %d-button Multisystem joystick", n_buttons[i]);
  160. snprintf(tgfx->phys[i], sizeof(tgfx->phys[i]),
  161. "%s/input%d", tgfx->pd->port->name, i);
  162. input_dev->name = tgfx->name[i];
  163. input_dev->phys = tgfx->phys[i];
  164. input_dev->id.bustype = BUS_PARPORT;
  165. input_dev->id.vendor = 0x0003;
  166. input_dev->id.product = n_buttons[i];
  167. input_dev->id.version = 0x0100;
  168. input_set_drvdata(input_dev, tgfx);
  169. input_dev->open = tgfx_open;
  170. input_dev->close = tgfx_close;
  171. input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
  172. input_set_abs_params(input_dev, ABS_X, -1, 1, 0, 0);
  173. input_set_abs_params(input_dev, ABS_Y, -1, 1, 0, 0);
  174. for (j = 0; j < n_buttons[i]; j++)
  175. set_bit(tgfx_buttons[j], input_dev->keybit);
  176. if (input_register_device(tgfx->dev[i]))
  177. goto err_free_dev;
  178. }
  179. if (!tgfx->sticks) {
  180. printk(KERN_ERR "turbografx.c: No valid devices specified\n");
  181. goto err_free_tgfx;
  182. }
  183. tgfx_base[port_idx] = tgfx;
  184. return;
  185. err_free_dev:
  186. input_free_device(tgfx->dev[i]);
  187. err_unreg_devs:
  188. while (--i >= 0)
  189. if (tgfx->dev[i])
  190. input_unregister_device(tgfx->dev[i]);
  191. err_free_tgfx:
  192. kfree(tgfx);
  193. err_unreg_pardev:
  194. parport_unregister_device(pd);
  195. }
  196. static void tgfx_detach(struct parport *port)
  197. {
  198. int i;
  199. struct tgfx *tgfx;
  200. for (i = 0; i < TGFX_MAX_PORTS; i++) {
  201. if (tgfx_base[i] && tgfx_base[i]->parportno == port->number)
  202. break;
  203. }
  204. if (i == TGFX_MAX_PORTS)
  205. return;
  206. tgfx = tgfx_base[i];
  207. tgfx_base[i] = NULL;
  208. for (i = 0; i < TGFX_MAX_DEVICES; i++)
  209. if (tgfx->dev[i])
  210. input_unregister_device(tgfx->dev[i]);
  211. parport_unregister_device(tgfx->pd);
  212. kfree(tgfx);
  213. }
  214. static struct parport_driver tgfx_parport_driver = {
  215. .name = "turbografx",
  216. .match_port = tgfx_attach,
  217. .detach = tgfx_detach,
  218. };
  219. static int __init tgfx_init(void)
  220. {
  221. int i;
  222. int have_dev = 0;
  223. for (i = 0; i < TGFX_MAX_PORTS; i++) {
  224. if (tgfx_cfg[i].nargs == 0 || tgfx_cfg[i].args[0] < 0)
  225. continue;
  226. if (tgfx_cfg[i].nargs < 2) {
  227. printk(KERN_ERR "turbografx.c: at least one joystick must be specified\n");
  228. return -EINVAL;
  229. }
  230. have_dev = 1;
  231. }
  232. if (!have_dev)
  233. return -ENODEV;
  234. return parport_register_driver(&tgfx_parport_driver);
  235. }
  236. static void __exit tgfx_exit(void)
  237. {
  238. parport_unregister_driver(&tgfx_parport_driver);
  239. }
  240. module_init(tgfx_init);
  241. module_exit(tgfx_exit);