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