turbografx.c 8.0 KB

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