turbografx.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. static int tgfx[] __initdata = { -1, 0, 0, 0, 0, 0, 0, 0 };
  41. static int tgfx_nargs __initdata = 0;
  42. module_param_array_named(map, tgfx, int, &tgfx_nargs, 0);
  43. MODULE_PARM_DESC(map, "Describes first set of devices (<parport#>,<js1>,<js2>,..<js7>");
  44. static int tgfx_2[] __initdata = { -1, 0, 0, 0, 0, 0, 0, 0 };
  45. static int tgfx_nargs_2 __initdata = 0;
  46. module_param_array_named(map2, tgfx_2, int, &tgfx_nargs_2, 0);
  47. MODULE_PARM_DESC(map2, "Describes second set of devices");
  48. static int tgfx_3[] __initdata = { -1, 0, 0, 0, 0, 0, 0, 0 };
  49. static int tgfx_nargs_3 __initdata = 0;
  50. module_param_array_named(map3, tgfx_3, int, &tgfx_nargs_3, 0);
  51. MODULE_PARM_DESC(map3, "Describes third set of devices");
  52. __obsolete_setup("tgfx=");
  53. __obsolete_setup("tgfx_2=");
  54. __obsolete_setup("tgfx_3=");
  55. #define TGFX_REFRESH_TIME HZ/100 /* 10 ms */
  56. #define TGFX_TRIGGER 0x08
  57. #define TGFX_UP 0x10
  58. #define TGFX_DOWN 0x20
  59. #define TGFX_LEFT 0x40
  60. #define TGFX_RIGHT 0x80
  61. #define TGFX_THUMB 0x02
  62. #define TGFX_THUMB2 0x04
  63. #define TGFX_TOP 0x01
  64. #define TGFX_TOP2 0x08
  65. static int tgfx_buttons[] = { BTN_TRIGGER, BTN_THUMB, BTN_THUMB2, BTN_TOP, BTN_TOP2 };
  66. static char *tgfx_name = "TurboGraFX Multisystem joystick";
  67. static struct tgfx {
  68. struct pardevice *pd;
  69. struct timer_list timer;
  70. struct input_dev dev[7];
  71. char phys[7][32];
  72. int sticks;
  73. int used;
  74. struct semaphore sem;
  75. } *tgfx_base[3];
  76. /*
  77. * tgfx_timer() reads and analyzes TurboGraFX joystick data.
  78. */
  79. static void tgfx_timer(unsigned long private)
  80. {
  81. struct tgfx *tgfx = (void *) private;
  82. struct input_dev *dev;
  83. int data1, data2, i;
  84. for (i = 0; i < 7; i++)
  85. if (tgfx->sticks & (1 << i)) {
  86. dev = tgfx->dev + i;
  87. parport_write_data(tgfx->pd->port, ~(1 << i));
  88. data1 = parport_read_status(tgfx->pd->port) ^ 0x7f;
  89. data2 = parport_read_control(tgfx->pd->port) ^ 0x04; /* CAVEAT parport */
  90. input_report_abs(dev, ABS_X, !!(data1 & TGFX_RIGHT) - !!(data1 & TGFX_LEFT));
  91. input_report_abs(dev, ABS_Y, !!(data1 & TGFX_DOWN ) - !!(data1 & TGFX_UP ));
  92. input_report_key(dev, BTN_TRIGGER, (data1 & TGFX_TRIGGER));
  93. input_report_key(dev, BTN_THUMB, (data2 & TGFX_THUMB ));
  94. input_report_key(dev, BTN_THUMB2, (data2 & TGFX_THUMB2 ));
  95. input_report_key(dev, BTN_TOP, (data2 & TGFX_TOP ));
  96. input_report_key(dev, BTN_TOP2, (data2 & TGFX_TOP2 ));
  97. input_sync(dev);
  98. }
  99. mod_timer(&tgfx->timer, jiffies + TGFX_REFRESH_TIME);
  100. }
  101. static int tgfx_open(struct input_dev *dev)
  102. {
  103. struct tgfx *tgfx = dev->private;
  104. int err;
  105. err = down_interruptible(&tgfx->sem);
  106. if (err)
  107. return err;
  108. if (!tgfx->used++) {
  109. parport_claim(tgfx->pd);
  110. parport_write_control(tgfx->pd->port, 0x04);
  111. mod_timer(&tgfx->timer, jiffies + TGFX_REFRESH_TIME);
  112. }
  113. up(&tgfx->sem);
  114. return 0;
  115. }
  116. static void tgfx_close(struct input_dev *dev)
  117. {
  118. struct tgfx *tgfx = dev->private;
  119. down(&tgfx->sem);
  120. if (!--tgfx->used) {
  121. del_timer_sync(&tgfx->timer);
  122. parport_write_control(tgfx->pd->port, 0x00);
  123. parport_release(tgfx->pd);
  124. }
  125. up(&tgfx->sem);
  126. }
  127. /*
  128. * tgfx_probe() probes for tg gamepads.
  129. */
  130. static struct tgfx __init *tgfx_probe(int *config, int nargs)
  131. {
  132. struct tgfx *tgfx;
  133. struct parport *pp;
  134. int i, j;
  135. if (config[0] < 0)
  136. return NULL;
  137. if (nargs < 2) {
  138. printk(KERN_ERR "turbografx.c: at least one joystick must be specified\n");
  139. return NULL;
  140. }
  141. pp = parport_find_number(config[0]);
  142. if (!pp) {
  143. printk(KERN_ERR "turbografx.c: no such parport\n");
  144. return NULL;
  145. }
  146. if (!(tgfx = kzalloc(sizeof(struct tgfx), GFP_KERNEL))) {
  147. parport_put_port(pp);
  148. return NULL;
  149. }
  150. init_MUTEX(&tgfx->sem);
  151. tgfx->pd = parport_register_device(pp, "turbografx", NULL, NULL, NULL, PARPORT_DEV_EXCL, NULL);
  152. parport_put_port(pp);
  153. if (!tgfx->pd) {
  154. printk(KERN_ERR "turbografx.c: parport busy already - lp.o loaded?\n");
  155. kfree(tgfx);
  156. return NULL;
  157. }
  158. init_timer(&tgfx->timer);
  159. tgfx->timer.data = (long) tgfx;
  160. tgfx->timer.function = tgfx_timer;
  161. tgfx->sticks = 0;
  162. for (i = 0; i < nargs - 1; i++)
  163. if (config[i+1] > 0 && config[i+1] < 6) {
  164. tgfx->sticks |= (1 << i);
  165. tgfx->dev[i].private = tgfx;
  166. tgfx->dev[i].open = tgfx_open;
  167. tgfx->dev[i].close = tgfx_close;
  168. sprintf(tgfx->phys[i], "%s/input0", tgfx->pd->port->name);
  169. tgfx->dev[i].name = tgfx_name;
  170. tgfx->dev[i].phys = tgfx->phys[i];
  171. tgfx->dev[i].id.bustype = BUS_PARPORT;
  172. tgfx->dev[i].id.vendor = 0x0003;
  173. tgfx->dev[i].id.product = config[i+1];
  174. tgfx->dev[i].id.version = 0x0100;
  175. tgfx->dev[i].evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
  176. tgfx->dev[i].absbit[0] = BIT(ABS_X) | BIT(ABS_Y);
  177. for (j = 0; j < config[i+1]; j++)
  178. set_bit(tgfx_buttons[j], tgfx->dev[i].keybit);
  179. tgfx->dev[i].absmin[ABS_X] = -1; tgfx->dev[i].absmax[ABS_X] = 1;
  180. tgfx->dev[i].absmin[ABS_Y] = -1; tgfx->dev[i].absmax[ABS_Y] = 1;
  181. input_register_device(tgfx->dev + i);
  182. printk(KERN_INFO "input: %d-button Multisystem joystick on %s\n",
  183. config[i+1], tgfx->pd->port->name);
  184. }
  185. if (!tgfx->sticks) {
  186. parport_unregister_device(tgfx->pd);
  187. kfree(tgfx);
  188. return NULL;
  189. }
  190. return tgfx;
  191. }
  192. static int __init tgfx_init(void)
  193. {
  194. tgfx_base[0] = tgfx_probe(tgfx, tgfx_nargs);
  195. tgfx_base[1] = tgfx_probe(tgfx_2, tgfx_nargs_2);
  196. tgfx_base[2] = tgfx_probe(tgfx_3, tgfx_nargs_3);
  197. if (tgfx_base[0] || tgfx_base[1] || tgfx_base[2])
  198. return 0;
  199. return -ENODEV;
  200. }
  201. static void __exit tgfx_exit(void)
  202. {
  203. int i, j;
  204. for (i = 0; i < 3; i++)
  205. if (tgfx_base[i]) {
  206. for (j = 0; j < 7; j++)
  207. if (tgfx_base[i]->sticks & (1 << j))
  208. input_unregister_device(tgfx_base[i]->dev + j);
  209. parport_unregister_device(tgfx_base[i]->pd);
  210. }
  211. }
  212. module_init(tgfx_init);
  213. module_exit(tgfx_exit);