turbografx.c 7.9 KB

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