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