turbografx.c 8.2 KB

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