OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [linux/] [linux-2.4/] [drivers/] [char/] [joystick/] [gf2k.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1275 phoenix
/*
2
 * $Id: gf2k.c,v 1.1.1.1 2004-04-15 02:03:26 phoenix Exp $
3
 *
4
 *  Copyright (c) 1998-2000 Vojtech Pavlik
5
 *
6
 *  Sponsored by SuSE
7
 */
8
 
9
/*
10
 * Genius Flight 2000 joystick driver for Linux
11
 */
12
 
13
/*
14
 * This program is free software; you can redistribute it and/or modify
15
 * it under the terms of the GNU General Public License as published by
16
 * the Free Software Foundation; either version 2 of the License, or
17
 * (at your option) any later version.
18
 *
19
 * This program is distributed in the hope that it will be useful,
20
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
 * GNU General Public License for more details.
23
 *
24
 * You should have received a copy of the GNU General Public License
25
 * along with this program; if not, write to the Free Software
26
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27
 *
28
 * Should you need to contact me, the author, you can do so either by
29
 * e-mail - mail your message to <vojtech@suse.cz>, or by paper mail:
30
 * Vojtech Pavlik, Ucitelska 1576, Prague 8, 182 00 Czech Republic
31
 */
32
 
33
#include <linux/delay.h>
34
#include <linux/kernel.h>
35
#include <linux/slab.h>
36
#include <linux/module.h>
37
#include <linux/init.h>
38
#include <linux/input.h>
39
#include <linux/gameport.h>
40
 
41
#define GF2K_START              400     /* The time we wait for the first bit [400 us] */
42
#define GF2K_STROBE             40      /* The time we wait for the first bit [40 us] */
43
#define GF2K_TIMEOUT            4       /* Wait for everything to settle [4 ms] */
44
#define GF2K_LENGTH             80      /* Max number of triplets in a packet */
45
#define GF2K_REFRESH            HZ/50   /* Time between joystick polls [20 ms] */
46
 
47
/*
48
 * Genius joystick ids ...
49
 */
50
 
51
#define GF2K_ID_G09             1
52
#define GF2K_ID_F30D            2
53
#define GF2K_ID_F30             3
54
#define GF2K_ID_F31D            4
55
#define GF2K_ID_F305            5
56
#define GF2K_ID_F23P            6
57
#define GF2K_ID_F31             7
58
#define GF2K_ID_MAX             7
59
 
60
static char gf2k_length[] = { 40, 40, 40, 40, 40, 40, 40, 40 };
61
static char gf2k_hat_to_axis[][2] = {{ 0, 0}, { 0,-1}, { 1,-1}, { 1, 0}, { 1, 1}, { 0, 1}, {-1, 1}, {-1, 0}, {-1,-1}};
62
 
63
static char *gf2k_names[] = {"", "Genius G-09D", "Genius F-30D", "Genius F-30", "Genius MaxFighter F-31D",
64
                                "Genius F-30-5", "Genius Flight2000 F-23", "Genius F-31"};
65
static unsigned char gf2k_hats[] = { 0, 2, 0, 0, 2, 0, 2, 0 };
66
static unsigned char gf2k_axes[] = { 0, 2, 0, 0, 4, 0, 4, 0 };
67
static unsigned char gf2k_joys[] = { 0, 0, 0, 0,10, 0, 8, 0 };
68
static unsigned char gf2k_pads[] = { 0, 6, 0, 0, 0, 0, 0, 0 };
69
static unsigned char gf2k_lens[] = { 0,18, 0, 0,18, 0,18, 0 };
70
 
71
static unsigned char gf2k_abs[] = { ABS_X, ABS_Y, ABS_THROTTLE, ABS_RUDDER, ABS_GAS, ABS_BRAKE };
72
static short gf2k_btn_joy[] = { BTN_TRIGGER, BTN_THUMB, BTN_TOP, BTN_TOP2, BTN_BASE, BTN_BASE2, BTN_BASE3, BTN_BASE4 };
73
static short gf2k_btn_pad[] = { BTN_A, BTN_B, BTN_C, BTN_X, BTN_Y, BTN_Z, BTN_TL, BTN_TR, BTN_TL2, BTN_TR2, BTN_START, BTN_SELECT };
74
 
75
 
76
static short gf2k_seq_reset[] = { 240, 340, 0 };
77
static short gf2k_seq_digital[] = { 590, 320, 860, 0 };
78
 
79
struct gf2k {
80
        struct gameport *gameport;
81
        struct timer_list timer;
82
        struct input_dev dev;
83
        int reads;
84
        int bads;
85
        int used;
86
        unsigned char id;
87
        unsigned char length;
88
};
89
 
90
/*
91
 * gf2k_read_packet() reads a Genius Flight2000 packet.
92
 */
93
 
94
static int gf2k_read_packet(struct gameport *gameport, int length, char *data)
95
{
96
        unsigned char u, v;
97
        int i;
98
        unsigned int t, p;
99
        unsigned long flags;
100
 
101
        t = gameport_time(gameport, GF2K_START);
102
        p = gameport_time(gameport, GF2K_STROBE);
103
 
104
        i = 0;
105
 
106
        __save_flags(flags);
107
        __cli();
108
 
109
        gameport_trigger(gameport);
110
        v = gameport_read(gameport);;
111
 
112
        while (t > 0 && i < length) {
113
                t--; u = v;
114
                v = gameport_read(gameport);
115
                if (v & ~u & 0x10) {
116
                        data[i++] = v >> 5;
117
                        t = p;
118
                }
119
        }
120
 
121
        __restore_flags(flags);
122
 
123
        return i;
124
}
125
 
126
/*
127
 * gf2k_trigger_seq() initializes a Genius Flight2000 joystick
128
 * into digital mode.
129
 */
130
 
131
static void gf2k_trigger_seq(struct gameport *gameport, short *seq)
132
{
133
 
134
        unsigned long flags;
135
        int i, t;
136
 
137
        __save_flags(flags);
138
        __cli();
139
 
140
        i = 0;
141
        do {
142
                gameport_trigger(gameport);
143
                t = gameport_time(gameport, GF2K_TIMEOUT * 1000);
144
                while ((gameport_read(gameport) & 1) && t) t--;
145
                udelay(seq[i]);
146
        } while (seq[++i]);
147
 
148
        gameport_trigger(gameport);
149
 
150
        __restore_flags(flags);
151
}
152
 
153
/*
154
 * js_sw_get_bits() composes bits from the triplet buffer into a __u64.
155
 * Parameter 'pos' is bit number inside packet where to start at, 'num' is number
156
 * of bits to be read, 'shift' is offset in the resulting __u64 to start at, bits
157
 * is number of bits per triplet.
158
 */
159
 
160
#define GB(p,n,s)       gf2k_get_bits(data, p, n, s)
161
 
162
static int gf2k_get_bits(unsigned char *buf, int pos, int num, int shift)
163
{
164
        __u64 data = 0;
165
        int i;
166
 
167
        for (i = 0; i < num / 3 + 2; i++)
168
                data |= buf[pos / 3 + i] << (i * 3);
169
        data >>= pos % 3;
170
        data &= (1 << num) - 1;
171
        data <<= shift;
172
 
173
        return data;
174
}
175
 
176
static void gf2k_read(struct gf2k *gf2k, unsigned char *data)
177
{
178
        struct input_dev *dev = &gf2k->dev;
179
        int i, t;
180
 
181
        for (i = 0; i < 4 && i < gf2k_axes[gf2k->id]; i++)
182
                input_report_abs(dev, gf2k_abs[i], GB(i<<3,8,0) | GB(i+46,1,8) | GB(i+50,1,9));
183
 
184
        for (i = 0; i < 2 && i < gf2k_axes[gf2k->id] - 4; i++)
185
                input_report_abs(dev, gf2k_abs[i], GB(i*9+60,8,0) | GB(i+54,1,9));
186
 
187
        t = GB(40,4,0);
188
 
189
        for (i = 0; i < gf2k_hats[gf2k->id]; i++)
190
                input_report_abs(dev, ABS_HAT0X + i, gf2k_hat_to_axis[t][i]);
191
 
192
        t = GB(44,2,0) | GB(32,8,2) | GB(78,2,10);
193
 
194
        for (i = 0; i < gf2k_joys[gf2k->id]; i++)
195
                input_report_key(dev, gf2k_btn_joy[i], (t >> i) & 1);
196
 
197
        for (i = 0; i < gf2k_pads[gf2k->id]; i++)
198
                input_report_key(dev, gf2k_btn_pad[i], (t >> i) & 1);
199
}
200
 
201
/*
202
 * gf2k_timer() reads and analyzes Genius joystick data.
203
 */
204
 
205
static void gf2k_timer(unsigned long private)
206
{
207
        struct gf2k *gf2k = (void *) private;
208
        unsigned char data[GF2K_LENGTH];
209
 
210
        gf2k->reads++;
211
 
212
        if (gf2k_read_packet(gf2k->gameport, gf2k_length[gf2k->id], data) < gf2k_length[gf2k->id]) {
213
                gf2k->bads++;
214
        } else gf2k_read(gf2k, data);
215
 
216
        mod_timer(&gf2k->timer, jiffies + GF2K_REFRESH);
217
}
218
 
219
static int gf2k_open(struct input_dev *dev)
220
{
221
        struct gf2k *gf2k = dev->private;
222
        if (!gf2k->used++)
223
                mod_timer(&gf2k->timer, jiffies + GF2K_REFRESH);
224
        return 0;
225
}
226
 
227
static void gf2k_close(struct input_dev *dev)
228
{
229
        struct gf2k *gf2k = dev->private;
230
        if (!--gf2k->used)
231
                del_timer(&gf2k->timer);
232
}
233
 
234
/*
235
 * gf2k_connect() probes for Genius id joysticks.
236
 */
237
 
238
static void gf2k_connect(struct gameport *gameport, struct gameport_dev *dev)
239
{
240
        struct gf2k *gf2k;
241
        unsigned char data[GF2K_LENGTH];
242
        int i;
243
 
244
        if (!(gf2k = kmalloc(sizeof(struct gf2k), GFP_KERNEL)))
245
                return;
246
        memset(gf2k, 0, sizeof(struct gf2k));
247
 
248
        gameport->private = gf2k;
249
 
250
        gf2k->gameport = gameport;
251
        init_timer(&gf2k->timer);
252
        gf2k->timer.data = (long) gf2k;
253
        gf2k->timer.function = gf2k_timer;
254
 
255
        if (gameport_open(gameport, dev, GAMEPORT_MODE_RAW))
256
                goto fail1;
257
 
258
        gf2k_trigger_seq(gameport, gf2k_seq_reset);
259
 
260
        wait_ms(GF2K_TIMEOUT);
261
 
262
        gf2k_trigger_seq(gameport, gf2k_seq_digital);
263
 
264
        wait_ms(GF2K_TIMEOUT);
265
 
266
        if (gf2k_read_packet(gameport, GF2K_LENGTH, data) < 12)
267
                goto fail2;
268
 
269
        if (!(gf2k->id = GB(7,2,0) | GB(3,3,2) | GB(0,3,5)))
270
                goto fail2;
271
 
272
#ifdef RESET_WORKS
273
        if ((gf2k->id != (GB(19,2,0) | GB(15,3,2) | GB(12,3,5))) ||
274
            (gf2k->id != (GB(31,2,0) | GB(27,3,2) | GB(24,3,5))))
275
                goto fail2;
276
#else
277
        gf2k->id = 6;
278
#endif
279
 
280
        if (gf2k->id > GF2K_ID_MAX || !gf2k_axes[gf2k->id]) {
281
                printk(KERN_WARNING "gf2k.c: Not yet supported joystick on gameport%d. [id: %d type:%s]\n",
282
                        gameport->number, gf2k->id, gf2k->id > GF2K_ID_MAX ? "Unknown" : gf2k_names[gf2k->id]);
283
                goto fail2;
284
        }
285
 
286
        gf2k->length = gf2k_lens[gf2k->id];
287
 
288
        gf2k->dev.private = gf2k;
289
        gf2k->dev.open = gf2k_open;
290
        gf2k->dev.close = gf2k_close;
291
        gf2k->dev.evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
292
 
293
        gf2k->dev.name = gf2k_names[gf2k->id];
294
        gf2k->dev.idbus = BUS_GAMEPORT;
295
        gf2k->dev.idvendor = GAMEPORT_ID_VENDOR_GENIUS;
296
        gf2k->dev.idproduct = gf2k->id;
297
        gf2k->dev.idversion = 0x0100;
298
 
299
        for (i = 0; i < gf2k_axes[gf2k->id]; i++)
300
                set_bit(gf2k_abs[i], gf2k->dev.absbit);
301
 
302
        for (i = 0; i < gf2k_hats[gf2k->id]; i++) {
303
                set_bit(ABS_HAT0X + i, gf2k->dev.absbit);
304
                gf2k->dev.absmin[ABS_HAT0X + i] = -1;
305
                gf2k->dev.absmax[ABS_HAT0X + i] = 1;
306
        }
307
 
308
        for (i = 0; i < gf2k_joys[gf2k->id]; i++)
309
                set_bit(gf2k_btn_joy[i], gf2k->dev.keybit);
310
 
311
        for (i = 0; i < gf2k_pads[gf2k->id]; i++)
312
                set_bit(gf2k_btn_pad[i], gf2k->dev.keybit);
313
 
314
        gf2k_read_packet(gameport, gf2k->length, data);
315
        gf2k_read(gf2k, data);
316
 
317
        for (i = 0; i < gf2k_axes[gf2k->id]; i++) {
318
                gf2k->dev.absmax[gf2k_abs[i]] = (i < 2) ? gf2k->dev.abs[gf2k_abs[i]] * 2 - 32 :
319
                          gf2k->dev.abs[gf2k_abs[0]] + gf2k->dev.abs[gf2k_abs[1]] - 32;
320
                gf2k->dev.absmin[gf2k_abs[i]] = 32;
321
                gf2k->dev.absfuzz[gf2k_abs[i]] = 8;
322
                gf2k->dev.absflat[gf2k_abs[i]] = (i < 2) ? 24 : 0;
323
        }
324
 
325
        input_register_device(&gf2k->dev);
326
        printk(KERN_INFO "input%d: %s on gameport%d.0\n",
327
                gf2k->dev.number, gf2k_names[gf2k->id], gameport->number);
328
 
329
        return;
330
fail2:  gameport_close(gameport);
331
fail1:  kfree(gf2k);
332
}
333
 
334
static void gf2k_disconnect(struct gameport *gameport)
335
{
336
        struct gf2k *gf2k = gameport->private;
337
        input_unregister_device(&gf2k->dev);
338
        gameport_close(gameport);
339
        kfree(gf2k);
340
}
341
 
342
static struct gameport_dev gf2k_dev = {
343
        connect:        gf2k_connect,
344
        disconnect:     gf2k_disconnect,
345
};
346
 
347
int __init gf2k_init(void)
348
{
349
        gameport_register_device(&gf2k_dev);
350
        return 0;
351
}
352
 
353
void __exit gf2k_exit(void)
354
{
355
        gameport_unregister_device(&gf2k_dev);
356
}
357
 
358
module_init(gf2k_init);
359
module_exit(gf2k_exit);
360
 
361
MODULE_LICENSE("GPL");

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.