turbografx.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /*
  2. * $Id: turbografx.c,v 1.14 2002/01/22 20:30:39 vojtech Exp $
  3. *
  4. * Copyright (c) 1998-2001 Vojtech Pavlik
  5. *
  6. * Based on the work of:
  7. * Steffen Schwenke
  8. */
  9. /*
  10. * TurboGraFX parallel port interface driver for Linux.
  11. */
  12. /*
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation; either version 2 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  26. *
  27. * Should you need to contact me, the author, you can do so either by
  28. * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail:
  29. * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic
  30. */
  31. #include <linux/kernel.h>
  32. #include <linux/parport.h>
  33. #include <linux/input.h>
  34. #include <linux/module.h>
  35. #include <linux/moduleparam.h>
  36. #include <linux/init.h>
  37. #include <linux/mutex.h>
  38. MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
  39. MODULE_DESCRIPTION("TurboGraFX parallel port interface driver");
  40. MODULE_LICENSE("GPL");
  41. #define TGFX_MAX_PORTS 3
  42. #define TGFX_MAX_DEVICES 7
  43. struct tgfx_config {
  44. int args[TGFX_MAX_DEVICES + 1];
  45. int nargs;
  46. };
  47. static struct tgfx_config tgfx[TGFX_MAX_PORTS] __initdata;
  48. module_param_array_named(map, tgfx[0].args, int, &tgfx[0].nargs, 0);
  49. MODULE_PARM_DESC(map, "Describes first set of devices (<parport#>,<js1>,<js2>,..<js7>");
  50. module_param_array_named(map2, tgfx[1].args, int, &tgfx[1].nargs, 0);
  51. MODULE_PARM_DESC(map2, "Describes second set of devices");
  52. module_param_array_named(map3, tgfx[2].args, int, &tgfx[2].nargs, 0);
  53. MODULE_PARM_DESC(map3, "Describes third set of devices");
  54. __obsolete_setup("tgfx=");
  55. __obsolete_setup("tgfx_2=");
  56. __obsolete_setup("tgfx_3=");
  57. #define TGFX_REFRESH_TIME HZ/100 /* 10 ms */
  58. #define TGFX_TRIGGER 0x08
  59. #define TGFX_UP 0x10
  60. #define TGFX_DOWN 0x20
  61. #define TGFX_LEFT 0x40
  62. #define TGFX_RIGHT 0x80
  63. #define TGFX_THUMB 0x02
  64. #define TGFX_THUMB2 0x04
  65. #define TGFX_TOP 0x01
  66. #define TGFX_TOP2 0x08
  67. static int tgfx_buttons[] = { BTN_TRIGGER, BTN_THUMB, BTN_THUMB2, BTN_TOP, BTN_TOP2 };
  68. static struct tgfx {
  69. struct pardevice *pd;
  70. struct timer_list timer;
  71. struct input_dev *dev[TGFX_MAX_DEVICES];
  72. char name[TGFX_MAX_DEVICES][64];
  73. char phys[TGFX_MAX_DEVICES][32];
  74. int sticks;
  75. int used;
  76. struct mutex sem;
  77. } *tgfx_base[TGFX_MAX_PORTS];
  78. /*
  79. * tgfx_timer() reads and analyzes TurboGraFX joystick data.
  80. */
  81. static void tgfx_timer(unsigned long private)
  82. {
  83. struct tgfx *tgfx = (void *) private;
  84. struct input_dev *dev;
  85. int data1, data2, i;
  86. for (i = 0; i < 7; i++)
  87. if (tgfx->sticks & (1 << i)) {
  88. dev = tgfx->dev[i];
  89. parport_write_data(tgfx->pd->port, ~(1 << i));
  90. data1 = parport_read_status(tgfx->pd->port) ^ 0x7f;
  91. data2 = parport_read_control(tgfx->pd->port) ^ 0x04; /* CAVEAT parport */
  92. input_report_abs(dev, ABS_X, !!(data1 & TGFX_RIGHT) - !!(data1 & TGFX_LEFT));
  93. input_report_abs(dev, ABS_Y, !!(data1 & TGFX_DOWN ) - !!(data1 & TGFX_UP ));
  94. input_report_key(dev, BTN_TRIGGER, (data1 & TGFX_TRIGGER));
  95. input_report_key(dev, BTN_THUMB, (data2 & TGFX_THUMB ));
  96. input_report_key(dev, BTN_THUMB2, (data2 & TGFX_THUMB2 ));
  97. input_report_key(dev, BTN_TOP, (data2 & TGFX_TOP ));
  98. input_report_key(dev, BTN_TOP2, (data2 & TGFX_TOP2 ));
  99. input_sync(dev);
  100. }
  101. mod_timer(&tgfx->timer, jiffies + TGFX_REFRESH_TIME);
  102. }
  103. static int tgfx_open(struct input_dev *dev)
  104. {
  105. struct tgfx *tgfx = dev->private;
  106. int err;
  107. err = mutex_lock_interruptible(&tgfx->sem);
  108. if (err)
  109. return err;
  110. if (!tgfx->used++) {
  111. parport_claim(tgfx->pd);
  112. parport_write_control(tgfx->pd->port, 0x04);
  113. mod_timer(&tgfx->timer, jiffies + TGFX_REFRESH_TIME);
  114. }
  115. mutex_unlock(&tgfx->sem);
  116. return 0;
  117. }
  118. static void tgfx_close(struct input_dev *dev)
  119. {
  120. struct tgfx *tgfx = dev->private;
  121. mutex_lock(&tgfx->sem);
  122. if (!--tgfx->used) {
  123. del_timer_sync(&tgfx->timer);
  124. parport_write_control(tgfx->pd->port, 0x00);
  125. parport_release(tgfx->pd);
  126. }
  127. mutex_unlock(&tgfx->sem);
  128. }
  129. /*
  130. * tgfx_probe() probes for tg gamepads.
  131. */
  132. static struct tgfx __init *tgfx_probe(int parport, int *n_buttons, int n_devs)
  133. {
  134. struct tgfx *tgfx;
  135. struct input_dev *input_dev;
  136. struct parport *pp;
  137. struct pardevice *pd;
  138. int i, j;
  139. int err;
  140. pp = parport_find_number(parport);
  141. if (!pp) {
  142. printk(KERN_ERR "turbografx.c: no such parport\n");
  143. err = -EINVAL;
  144. goto err_out;
  145. }
  146. pd = parport_register_device(pp, "turbografx", NULL, NULL, NULL, PARPORT_DEV_EXCL, NULL);
  147. if (!pd) {
  148. printk(KERN_ERR "turbografx.c: parport busy already - lp.o loaded?\n");
  149. err = -EBUSY;
  150. goto err_put_pp;
  151. }
  152. tgfx = kzalloc(sizeof(struct tgfx), GFP_KERNEL);
  153. if (!tgfx) {
  154. printk(KERN_ERR "turbografx.c: Not enough memory\n");
  155. err = -ENOMEM;
  156. goto err_unreg_pardev;
  157. }
  158. mutex_init(&tgfx->sem);
  159. tgfx->pd = pd;
  160. init_timer(&tgfx->timer);
  161. tgfx->timer.data = (long) tgfx;
  162. tgfx->timer.function = tgfx_timer;
  163. for (i = 0; i < n_devs; i++) {
  164. if (n_buttons[i] < 1)
  165. continue;
  166. if (n_buttons[i] > 6) {
  167. printk(KERN_ERR "turbografx.c: Invalid number of buttons %d\n", n_buttons[i]);
  168. err = -EINVAL;
  169. goto err_unreg_devs;
  170. }
  171. tgfx->dev[i] = input_dev = input_allocate_device();
  172. if (!input_dev) {
  173. printk(KERN_ERR "turbografx.c: Not enough memory for input device\n");
  174. err = -ENOMEM;
  175. goto err_unreg_devs;
  176. }
  177. tgfx->sticks |= (1 << i);
  178. snprintf(tgfx->name[i], sizeof(tgfx->name[i]),
  179. "TurboGraFX %d-button Multisystem joystick", n_buttons[i]);
  180. snprintf(tgfx->phys[i], sizeof(tgfx->phys[i]),
  181. "%s/input%d", tgfx->pd->port->name, i);
  182. input_dev->name = tgfx->name[i];
  183. input_dev->phys = tgfx->phys[i];
  184. input_dev->id.bustype = BUS_PARPORT;
  185. input_dev->id.vendor = 0x0003;
  186. input_dev->id.product = n_buttons[i];
  187. input_dev->id.version = 0x0100;
  188. input_dev->private = tgfx;
  189. input_dev->open = tgfx_open;
  190. input_dev->close = tgfx_close;
  191. input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
  192. input_set_abs_params(input_dev, ABS_X, -1, 1, 0, 0);
  193. input_set_abs_params(input_dev, ABS_Y, -1, 1, 0, 0);
  194. for (j = 0; j < n_buttons[i]; j++)
  195. set_bit(tgfx_buttons[j], input_dev->keybit);
  196. err = input_register_device(tgfx->dev[i]);
  197. if (err)
  198. goto err_free_dev;
  199. }
  200. if (!tgfx->sticks) {
  201. printk(KERN_ERR "turbografx.c: No valid devices specified\n");
  202. err = -EINVAL;
  203. goto err_free_tgfx;
  204. }
  205. return tgfx;
  206. err_free_dev:
  207. input_free_device(tgfx->dev[i]);
  208. err_unreg_devs:
  209. while (--i >= 0)
  210. if (tgfx->dev[i])
  211. input_unregister_device(tgfx->dev[i]);
  212. err_free_tgfx:
  213. kfree(tgfx);
  214. err_unreg_pardev:
  215. parport_unregister_device(pd);
  216. err_put_pp:
  217. parport_put_port(pp);
  218. err_out:
  219. return ERR_PTR(err);
  220. }
  221. static void tgfx_remove(struct tgfx *tgfx)
  222. {
  223. int i;
  224. for (i = 0; i < TGFX_MAX_DEVICES; i++)
  225. if (tgfx->dev[i])
  226. input_unregister_device(tgfx->dev[i]);
  227. parport_unregister_device(tgfx->pd);
  228. kfree(tgfx);
  229. }
  230. static int __init tgfx_init(void)
  231. {
  232. int i;
  233. int have_dev = 0;
  234. int err = 0;
  235. for (i = 0; i < TGFX_MAX_PORTS; i++) {
  236. if (tgfx[i].nargs == 0 || tgfx[i].args[0] < 0)
  237. continue;
  238. if (tgfx[i].nargs < 2) {
  239. printk(KERN_ERR "turbografx.c: at least one joystick must be specified\n");
  240. err = -EINVAL;
  241. break;
  242. }
  243. tgfx_base[i] = tgfx_probe(tgfx[i].args[0], tgfx[i].args + 1, tgfx[i].nargs - 1);
  244. if (IS_ERR(tgfx_base[i])) {
  245. err = PTR_ERR(tgfx_base[i]);
  246. break;
  247. }
  248. have_dev = 1;
  249. }
  250. if (err) {
  251. while (--i >= 0)
  252. if (tgfx_base[i])
  253. tgfx_remove(tgfx_base[i]);
  254. return err;
  255. }
  256. return have_dev ? 0 : -ENODEV;
  257. }
  258. static void __exit tgfx_exit(void)
  259. {
  260. int i;
  261. for (i = 0; i < TGFX_MAX_PORTS; i++)
  262. if (tgfx_base[i])
  263. tgfx_remove(tgfx_base[i]);
  264. }
  265. module_init(tgfx_init);
  266. module_exit(tgfx_exit);