turbografx.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:7k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /*
  2.  * $Id: turbografx.c,v 1.8 2000/05/29 20:39:38 vojtech Exp $
  3.  *
  4.  *  Copyright (c) 1998-2000 Vojtech Pavlik
  5.  *
  6.  *  Based on the work of:
  7.  * Steffen Schwenke
  8.  *
  9.  *  Sponsored by SuSE
  10.  */
  11. /*
  12.  * TurboGraFX parallel port interface driver for Linux.
  13.  */
  14. /*
  15.  * This program is free software; you can redistribute it and/or modify
  16.  * it under the terms of the GNU General Public License as published by
  17.  * the Free Software Foundation; either version 2 of the License, or 
  18.  * (at your option) any later version.
  19.  * 
  20.  * This program is distributed in the hope that it will be useful,
  21.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  23.  * GNU General Public License for more details.
  24.  * 
  25.  * You should have received a copy of the GNU General Public License
  26.  * along with this program; if not, write to the Free Software
  27.  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  28.  * 
  29.  * Should you need to contact me, the author, you can do so either by
  30.  * e-mail - mail your message to <vojtech@suse.cz>, or by paper mail:
  31.  * Vojtech Pavlik, Ucitelska 1576, Prague 8, 182 00 Czech Republic
  32.  */
  33. #include <linux/kernel.h>
  34. #include <linux/parport.h>
  35. #include <linux/input.h>
  36. #include <linux/module.h>
  37. #include <linux/init.h>
  38. MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>");
  39. MODULE_LICENSE("GPL");
  40. MODULE_PARM(tgfx, "2-8i");
  41. MODULE_PARM(tgfx_2, "2-8i");
  42. MODULE_PARM(tgfx_3, "2-8i");
  43. #define TGFX_REFRESH_TIME HZ/100 /* 10 ms */
  44. #define TGFX_TRIGGER 0x08
  45. #define TGFX_UP 0x10
  46. #define TGFX_DOWN 0x20
  47. #define TGFX_LEFT 0x40
  48. #define TGFX_RIGHT 0x80
  49. #define TGFX_THUMB 0x02
  50. #define TGFX_THUMB2 0x04
  51. #define TGFX_TOP 0x01
  52. #define TGFX_TOP2 0x08
  53. static int tgfx[] __initdata = { -1, 0, 0, 0, 0, 0, 0, 0 };
  54. static int tgfx_2[] __initdata = { -1, 0, 0, 0, 0, 0, 0, 0 };
  55. static int tgfx_3[] __initdata = { -1, 0, 0, 0, 0, 0, 0, 0 };
  56. static int tgfx_buttons[] = { BTN_TRIGGER, BTN_THUMB, BTN_THUMB2, BTN_TOP, BTN_TOP2 };
  57. static char *tgfx_name = "TurboGraFX Multisystem joystick";
  58. struct tgfx {
  59. struct pardevice *pd;
  60. struct timer_list timer;
  61. struct input_dev dev[7];
  62. int sticks;
  63. int used;
  64. } *tgfx_base[3];
  65. /*
  66.  * tgfx_timer() reads and analyzes TurboGraFX joystick data.
  67.  */
  68. static void tgfx_timer(unsigned long private)
  69. {
  70. struct tgfx *tgfx = (void *) private;
  71. struct input_dev *dev;
  72. int data1, data2, i;
  73. for (i = 0; i < 7; i++)
  74. if (tgfx->sticks & (1 << i)) {
  75.   dev = tgfx->dev + i;
  76. parport_write_data(tgfx->pd->port, ~(1 << i));
  77. data1 = parport_read_status(tgfx->pd->port) ^ 0x7f;
  78. data2 = parport_read_control(tgfx->pd->port) ^ 0x04; /* CAVEAT parport */
  79. input_report_abs(dev, ABS_X, !!(data1 & TGFX_RIGHT) - !!(data1 & TGFX_LEFT));
  80. input_report_abs(dev, ABS_Y, !!(data1 & TGFX_DOWN ) - !!(data1 & TGFX_UP  ));
  81. input_report_key(dev, BTN_TRIGGER, (data1 & TGFX_TRIGGER));
  82. input_report_key(dev, BTN_THUMB,   (data2 & TGFX_THUMB  ));
  83. input_report_key(dev, BTN_THUMB2,  (data2 & TGFX_THUMB2 ));
  84. input_report_key(dev, BTN_TOP,     (data2 & TGFX_TOP    ));
  85. input_report_key(dev, BTN_TOP2,    (data2 & TGFX_TOP2   ));
  86. }
  87. mod_timer(&tgfx->timer, jiffies + TGFX_REFRESH_TIME);
  88. }
  89. static int tgfx_open(struct input_dev *dev)
  90. {
  91.         struct tgfx *tgfx = dev->private;
  92.         if (!tgfx->used++) {
  93. parport_claim(tgfx->pd);
  94. parport_write_control(tgfx->pd->port, 0x04);
  95.                 mod_timer(&tgfx->timer, jiffies + TGFX_REFRESH_TIME); 
  96. }
  97.         return 0;
  98. }
  99. static void tgfx_close(struct input_dev *dev)
  100. {
  101.         struct tgfx *tgfx = dev->private;
  102.         if (!--tgfx->used) {
  103.                 del_timer(&tgfx->timer);
  104. parport_write_control(tgfx->pd->port, 0x00);
  105.          parport_release(tgfx->pd);
  106. }
  107. }
  108. /*
  109.  * tgfx_probe() probes for tg gamepads.
  110.  */
  111. static struct tgfx __init *tgfx_probe(int *config)
  112. {
  113. struct tgfx *tgfx;
  114. struct parport *pp;
  115. int i, j;
  116. if (config[0] < 0)
  117. return NULL;
  118. for (pp = parport_enumerate(); pp && (config[0] > 0); pp = pp->next)
  119. config[0]--;
  120. if (!pp) {
  121. printk(KERN_ERR "turbografx.c: no such parportn");
  122. return NULL;
  123. }
  124. if (!(tgfx = kmalloc(sizeof(struct tgfx), GFP_KERNEL)))
  125. return NULL;
  126. memset(tgfx, 0, sizeof(struct tgfx));
  127. tgfx->pd = parport_register_device(pp, "turbografx", NULL, NULL, NULL, PARPORT_DEV_EXCL, NULL);
  128. if (!tgfx->pd) {
  129. printk(KERN_ERR "turbografx.c: parport busy already - lp.o loaded?n");
  130. kfree(tgfx);
  131. return NULL;
  132. }
  133. init_timer(&tgfx->timer);
  134. tgfx->timer.data = (long) tgfx;
  135. tgfx->timer.function = tgfx_timer;
  136. tgfx->sticks = 0;
  137. for (i = 0; i < 7; i++)
  138. if (config[i+1] > 0 && config[i+1] < 6) {
  139. tgfx->sticks |= (1 << i);
  140. tgfx->dev[i].private = tgfx;
  141. tgfx->dev[i].open = tgfx_open;
  142. tgfx->dev[i].close = tgfx_close;
  143. tgfx->dev[i].name = tgfx_name;
  144. tgfx->dev[i].idbus = BUS_PARPORT;
  145. tgfx->dev[i].idvendor = 0x0003;
  146. tgfx->dev[i].idproduct = config[i+1];
  147. tgfx->dev[i].idversion = 0x0100;
  148. tgfx->dev[i].evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
  149. tgfx->dev[i].absbit[0] = BIT(ABS_X) | BIT(ABS_Y);
  150. for (j = 0; j < config[i+1]; j++)
  151. set_bit(tgfx_buttons[j], tgfx->dev[i].keybit); 
  152. tgfx->dev[i].absmin[ABS_X] = -1; tgfx->dev[i].absmax[ABS_X] = 1;
  153. tgfx->dev[i].absmin[ABS_Y] = -1; tgfx->dev[i].absmax[ABS_Y] = 1;
  154. input_register_device(tgfx->dev + i);
  155. printk(KERN_INFO "input%d: %d-button Multisystem joystick on %sn",
  156. tgfx->dev[i].number, config[i+1], tgfx->pd->port->name);
  157. }
  158.         if (!tgfx->sticks) {
  159. parport_unregister_device(tgfx->pd);
  160. kfree(tgfx);
  161. return NULL;
  162.         }
  163. return tgfx;
  164. }
  165. #ifndef MODULE
  166. int __init tgfx_setup(char *str)
  167. {
  168. int i, ints[9];
  169. get_options(str, ARRAY_SIZE(ints), ints);
  170. for (i = 0; i <= ints[0] && i < 8; i++) tgfx[i] = ints[i + 1];
  171. return 1;
  172. }
  173. int __init tgfx_setup_2(char *str)
  174. {
  175. int i, ints[9];
  176. get_options(str, ARRAY_SIZE(ints), ints);
  177. for (i = 0; i <= ints[0] && i < 8; i++) tgfx_2[i] = ints[i + 1];
  178. return 1;
  179. }
  180. int __init tgfx_setup_3(char *str)
  181. {
  182. int i, ints[9];
  183. get_options(str, ARRAY_SIZE(ints), ints);
  184. for (i = 0; i <= ints[0] && i < 8; i++) tgfx_3[i] = ints[i + 1];
  185. return 1;
  186. }
  187. __setup("tgfx=", tgfx_setup);
  188. __setup("tgfx_2=", tgfx_setup_2);
  189. __setup("tgfx_3=", tgfx_setup_3);
  190. #endif
  191. int __init tgfx_init(void)
  192. {
  193. tgfx_base[0] = tgfx_probe(tgfx);
  194. tgfx_base[1] = tgfx_probe(tgfx_2);
  195. tgfx_base[2] = tgfx_probe(tgfx_3);
  196. if (tgfx_base[0] || tgfx_base[1] || tgfx_base[2])
  197. return 0;
  198. return -ENODEV;
  199. }
  200. void __exit tgfx_exit(void)
  201. {
  202. int i, j;
  203. for (i = 0; i < 3; i++) 
  204. if (tgfx_base[i]) {
  205. for (j = 0; j < 7; j++)
  206. if (tgfx_base[i]->sticks & (1 << j))
  207. input_unregister_device(tgfx_base[i]->dev + j);
  208. parport_unregister_device(tgfx_base[i]->pd);
  209. }
  210. }
  211. module_init(tgfx_init);
  212. module_exit(tgfx_exit);