turbografx.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. } *tgfx_base[3];
  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 = dev->private;
  103. if (!tgfx->used++) {
  104. parport_claim(tgfx->pd);
  105. parport_write_control(tgfx->pd->port, 0x04);
  106. mod_timer(&tgfx->timer, jiffies + TGFX_REFRESH_TIME);
  107. }
  108. return 0;
  109. }
  110. static void tgfx_close(struct input_dev *dev)
  111. {
  112. struct tgfx *tgfx = dev->private;
  113. if (!--tgfx->used) {
  114. del_timer(&tgfx->timer);
  115. parport_write_control(tgfx->pd->port, 0x00);
  116. parport_release(tgfx->pd);
  117. }
  118. }
  119. /*
  120. * tgfx_probe() probes for tg gamepads.
  121. */
  122. static struct tgfx __init *tgfx_probe(int *config, int nargs)
  123. {
  124. struct tgfx *tgfx;
  125. struct parport *pp;
  126. int i, j;
  127. if (config[0] < 0)
  128. return NULL;
  129. if (nargs < 2) {
  130. printk(KERN_ERR "turbografx.c: at least one joystick must be specified\n");
  131. return NULL;
  132. }
  133. pp = parport_find_number(config[0]);
  134. if (!pp) {
  135. printk(KERN_ERR "turbografx.c: no such parport\n");
  136. return NULL;
  137. }
  138. if (!(tgfx = kmalloc(sizeof(struct tgfx), GFP_KERNEL))) {
  139. parport_put_port(pp);
  140. return NULL;
  141. }
  142. memset(tgfx, 0, sizeof(struct tgfx));
  143. tgfx->pd = parport_register_device(pp, "turbografx", NULL, NULL, NULL, PARPORT_DEV_EXCL, NULL);
  144. parport_put_port(pp);
  145. if (!tgfx->pd) {
  146. printk(KERN_ERR "turbografx.c: parport busy already - lp.o loaded?\n");
  147. kfree(tgfx);
  148. return NULL;
  149. }
  150. init_timer(&tgfx->timer);
  151. tgfx->timer.data = (long) tgfx;
  152. tgfx->timer.function = tgfx_timer;
  153. tgfx->sticks = 0;
  154. for (i = 0; i < nargs - 1; i++)
  155. if (config[i+1] > 0 && config[i+1] < 6) {
  156. tgfx->sticks |= (1 << i);
  157. tgfx->dev[i].private = tgfx;
  158. tgfx->dev[i].open = tgfx_open;
  159. tgfx->dev[i].close = tgfx_close;
  160. sprintf(tgfx->phys[i], "%s/input0", tgfx->pd->port->name);
  161. tgfx->dev[i].name = tgfx_name;
  162. tgfx->dev[i].phys = tgfx->phys[i];
  163. tgfx->dev[i].id.bustype = BUS_PARPORT;
  164. tgfx->dev[i].id.vendor = 0x0003;
  165. tgfx->dev[i].id.product = config[i+1];
  166. tgfx->dev[i].id.version = 0x0100;
  167. tgfx->dev[i].evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
  168. tgfx->dev[i].absbit[0] = BIT(ABS_X) | BIT(ABS_Y);
  169. for (j = 0; j < config[i+1]; j++)
  170. set_bit(tgfx_buttons[j], tgfx->dev[i].keybit);
  171. tgfx->dev[i].absmin[ABS_X] = -1; tgfx->dev[i].absmax[ABS_X] = 1;
  172. tgfx->dev[i].absmin[ABS_Y] = -1; tgfx->dev[i].absmax[ABS_Y] = 1;
  173. input_register_device(tgfx->dev + i);
  174. printk(KERN_INFO "input: %d-button Multisystem joystick on %s\n",
  175. config[i+1], tgfx->pd->port->name);
  176. }
  177. if (!tgfx->sticks) {
  178. parport_unregister_device(tgfx->pd);
  179. kfree(tgfx);
  180. return NULL;
  181. }
  182. return tgfx;
  183. }
  184. static int __init tgfx_init(void)
  185. {
  186. tgfx_base[0] = tgfx_probe(tgfx, tgfx_nargs);
  187. tgfx_base[1] = tgfx_probe(tgfx_2, tgfx_nargs_2);
  188. tgfx_base[2] = tgfx_probe(tgfx_3, tgfx_nargs_3);
  189. if (tgfx_base[0] || tgfx_base[1] || tgfx_base[2])
  190. return 0;
  191. return -ENODEV;
  192. }
  193. static void __exit tgfx_exit(void)
  194. {
  195. int i, j;
  196. for (i = 0; i < 3; i++)
  197. if (tgfx_base[i]) {
  198. for (j = 0; j < 7; j++)
  199. if (tgfx_base[i]->sticks & (1 << j))
  200. input_unregister_device(tgfx_base[i]->dev + j);
  201. parport_unregister_device(tgfx_base[i]->pd);
  202. }
  203. }
  204. module_init(tgfx_init);
  205. module_exit(tgfx_exit);