turbografx.c 7.9 KB

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