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

Subversion Repositories or1k

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

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1275 phoenix
/*
2
 * $Id: tmdc.c,v 1.1.1.1 2004-04-15 02:03:27 phoenix Exp $
3
 *
4
 *  Copyright (c) 1998-2000 Vojtech Pavlik
5
 *
6
 *  Sponsored by SuSE
7
 *
8
 *   Based on the work of:
9
 *      Trystan Larey-Williams
10
 *
11
 */
12
 
13
/*
14
 * ThrustMaster DirectConnect (BSP) joystick family driver for Linux
15
 */
16
 
17
/*
18
 * This program is free software; you can redistribute it and/or modify
19
 * it under the terms of the GNU General Public License as published by
20
 * the Free Software Foundation; either version 2 of the License, or
21
 * (at your option) any later version.
22
 *
23
 * This program is distributed in the hope that it will be useful,
24
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26
 * GNU General Public License for more details.
27
 *
28
 * You should have received a copy of the GNU General Public License
29
 * along with this program; if not, write to the Free Software
30
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31
 *
32
 * Should you need to contact me, the author, you can do so either by
33
 * e-mail - mail your message to <vojtech@suse.cz>, or by paper mail:
34
 * Vojtech Pavlik, Ucitelska 1576, Prague 8, 182 00 Czech Republic
35
 */
36
 
37
#include <linux/delay.h>
38
#include <linux/kernel.h>
39
#include <linux/slab.h>
40
#include <linux/module.h>
41
#include <linux/init.h>
42
#include <linux/gameport.h>
43
#include <linux/input.h>
44
 
45
#define TMDC_MAX_START          400     /* 400 us */
46
#define TMDC_MAX_STROBE         45      /* 45 us */
47
#define TMDC_MAX_LENGTH         13
48
#define TMDC_REFRESH_TIME       HZ/50   /* 20 ms */
49
 
50
#define TMDC_MODE_M3DI          1
51
#define TMDC_MODE_3DRP          3
52
#define TMDC_MODE_AT            4
53
#define TMDC_MODE_FM            8
54
#define TMDC_MODE_FGP           163
55
 
56
#define TMDC_BYTE_ID            10
57
#define TMDC_BYTE_REV           11
58
#define TMDC_BYTE_DEF           12
59
 
60
#define TMDC_ABS                7       
61
#define TMDC_ABS_HAT            4
62
#define TMDC_BTN                16
63
 
64
static unsigned char tmdc_byte_a[16] = { 0, 1, 3, 4, 6, 7 };
65
static unsigned char tmdc_byte_d[16] = { 2, 5, 8, 9 };
66
 
67
static signed char tmdc_abs[TMDC_ABS] =
68
        { ABS_X, ABS_Y, ABS_RUDDER, ABS_THROTTLE, ABS_RX, ABS_RY, ABS_RZ };
69
static signed char tmdc_abs_hat[TMDC_ABS_HAT] =
70
        { ABS_HAT0X, ABS_HAT0Y, ABS_HAT1X, ABS_HAT1Y };
71
static signed char tmdc_abs_at[TMDC_ABS] =
72
        { ABS_X, ABS_Y, ABS_RUDDER, -1, ABS_THROTTLE };
73
static signed char tmdc_abs_fm[TMDC_ABS] =
74
        { ABS_RX, ABS_RY, ABS_X, ABS_Y };
75
 
76
static short tmdc_btn_pad[TMDC_BTN] =
77
        { BTN_A, BTN_B, BTN_C, BTN_X, BTN_Y, BTN_Z, BTN_START, BTN_SELECT, BTN_TL, BTN_TR };
78
static short tmdc_btn_joy[TMDC_BTN] =
79
        { BTN_TRIGGER, BTN_THUMB, BTN_TOP, BTN_TOP2, BTN_BASE, BTN_BASE2, BTN_THUMB2, BTN_PINKIE,
80
          BTN_BASE3, BTN_BASE4, BTN_A, BTN_B, BTN_C, BTN_X, BTN_Y, BTN_Z };
81
static short tmdc_btn_fm[TMDC_BTN] =
82
        { BTN_TRIGGER, BTN_C, BTN_B, BTN_A, BTN_THUMB, BTN_X, BTN_Y, BTN_Z, BTN_TOP, BTN_TOP2 };
83
static short tmdc_btn_at[TMDC_BTN] =
84
        { BTN_TRIGGER, BTN_THUMB2, BTN_PINKIE, BTN_THUMB, BTN_BASE6, BTN_BASE5, BTN_BASE4,
85
          BTN_BASE3, BTN_BASE2, BTN_BASE };
86
 
87
static struct {
88
        int x;
89
        int y;
90
} tmdc_hat_to_axis[] = {{ 0, 0}, { 1, 0}, { 0,-1}, {-1, 0}, { 0, 1}};
91
 
92
struct tmdc {
93
        struct gameport *gameport;
94
        struct timer_list timer;
95
        struct input_dev dev[2];
96
        char name[2][64];
97
        int mode[2];
98
        signed char *abs[2];
99
        short *btn[2];
100
        unsigned char absc[2];
101
        unsigned char btnc[2][4];
102
        unsigned char btno[2][4];
103
        int used;
104
        int reads;
105
        int bads;
106
        unsigned char exists;
107
};
108
 
109
/*
110
 * tmdc_read_packet() reads a ThrustMaster packet.
111
 */
112
 
113
static int tmdc_read_packet(struct gameport *gameport, unsigned char data[2][TMDC_MAX_LENGTH])
114
{
115
        unsigned char u, v, w, x;
116
        unsigned long flags;
117
        int i[2], j[2], t[2], p, k;
118
 
119
        p = gameport_time(gameport, TMDC_MAX_STROBE);
120
 
121
        for (k = 0; k < 2; k++) {
122
                t[k] = gameport_time(gameport, TMDC_MAX_START);
123
                i[k] = j[k] = 0;
124
        }
125
 
126
        __save_flags(flags);
127
        __cli();
128
        gameport_trigger(gameport);
129
 
130
        w = gameport_read(gameport) >> 4;
131
 
132
        do {
133
                x = w;
134
                w = gameport_read(gameport) >> 4;
135
 
136
                for (k = 0, v = w, u = x; k < 2; k++, v >>= 2, u >>= 2) {
137
                        if (~v & u & 2) {
138
                                if (t[k] <= 0 || i[k] >= TMDC_MAX_LENGTH) continue;
139
                                t[k] = p;
140
                                if (j[k] == 0) {                          /* Start bit */
141
                                        if (~v & 1) t[k] = 0;
142
                                        data[k][i[k]] = 0; j[k]++; continue;
143
                                }
144
                                if (j[k] == 9) {                                /* Stop bit */
145
                                        if (v & 1) t[k] = 0;
146
                                        j[k] = 0; i[k]++; continue;
147
                                }
148
                                data[k][i[k]] |= (~v & 1) << (j[k]++ - 1);      /* Data bit */
149
                        }
150
                        t[k]--;
151
                }
152
        } while (t[0] > 0 || t[1] > 0);
153
 
154
        __restore_flags(flags);
155
 
156
        return (i[0] == TMDC_MAX_LENGTH) | ((i[1] == TMDC_MAX_LENGTH) << 1);
157
}
158
 
159
/*
160
 * tmdc_read() reads and analyzes ThrustMaster joystick data.
161
 */
162
 
163
static void tmdc_timer(unsigned long private)
164
{
165
        unsigned char data[2][TMDC_MAX_LENGTH];
166
        struct tmdc *tmdc = (void *) private;
167
        struct input_dev *dev;
168
        unsigned char r, bad = 0;
169
        int i, j, k, l;
170
 
171
        tmdc->reads++;
172
 
173
        if ((r = tmdc_read_packet(tmdc->gameport, data)) != tmdc->exists)
174
                bad = 1;
175
 
176
        for (j = 0; j < 2; j++)
177
                if (r & (1 << j) & tmdc->exists) {
178
 
179
                        if (data[j][TMDC_BYTE_ID] != tmdc->mode[j]) {
180
                                bad = 1;
181
                                continue;
182
                        }
183
 
184
                        dev = tmdc->dev + j;
185
 
186
                        for (i = 0; i < tmdc->absc[j]; i++) {
187
                                if (tmdc->abs[j][i] < 0) continue;
188
                                input_report_abs(dev, tmdc->abs[j][i], data[j][tmdc_byte_a[i]]);
189
                        }
190
 
191
                        switch (tmdc->mode[j]) {
192
 
193
                                case TMDC_MODE_M3DI:
194
 
195
                                        i = tmdc_byte_d[0];
196
                                        input_report_abs(dev, ABS_HAT0X, ((data[j][i] >> 3) & 1) - ((data[j][i] >> 1) & 1));
197
                                        input_report_abs(dev, ABS_HAT0Y, ((data[j][i] >> 2) & 1) - ( data[j][i]       & 1));
198
                                        break;
199
 
200
                                case TMDC_MODE_AT:
201
 
202
                                        i = tmdc_byte_a[3];
203
                                        input_report_abs(dev, ABS_HAT0X, tmdc_hat_to_axis[(data[j][i] - 141) / 25].x);
204
                                        input_report_abs(dev, ABS_HAT0Y, tmdc_hat_to_axis[(data[j][i] - 141) / 25].y);
205
                                        break;
206
 
207
                        }
208
 
209
                        for (k = l = 0; k < 4; k++) {
210
                                for (i = 0; i < tmdc->btnc[j][k]; i++)
211
                                        input_report_key(dev, tmdc->btn[j][i + l],
212
                                                ((data[j][tmdc_byte_d[k]] >> (i + tmdc->btno[j][k])) & 1));
213
                                l += tmdc->btnc[j][k];
214
                        }
215
        }
216
 
217
        tmdc->bads += bad;
218
 
219
        mod_timer(&tmdc->timer, jiffies + TMDC_REFRESH_TIME);
220
}
221
 
222
static int tmdc_open(struct input_dev *dev)
223
{
224
        struct tmdc *tmdc = dev->private;
225
        if (!tmdc->used++)
226
                mod_timer(&tmdc->timer, jiffies + TMDC_REFRESH_TIME);
227
        return 0;
228
}
229
 
230
static void tmdc_close(struct input_dev *dev)
231
{
232
        struct tmdc *tmdc = dev->private;
233
        if (!--tmdc->used)
234
                del_timer(&tmdc->timer);
235
}
236
 
237
/*
238
 * tmdc_probe() probes for ThrustMaster type joysticks.
239
 */
240
 
241
static void tmdc_connect(struct gameport *gameport, struct gameport_dev *dev)
242
{
243
        struct models {
244
                unsigned char id;
245
                char *name;
246
                char abs;
247
                char hats;
248
                char btnc[4];
249
                char btno[4];
250
                signed char *axes;
251
                short *buttons;
252
        } models[] = {  {   1, "ThrustMaster Millenium 3D Inceptor",      6, 2, { 4, 2 }, { 4, 6 }, tmdc_abs, tmdc_btn_joy },
253
                        {   3, "ThrustMaster Rage 3D Gamepad",            2, 0, { 8, 2 }, { 0, 0 }, tmdc_abs, tmdc_btn_pad },
254
                        {   4, "ThrustMaster Attack Throttle",            5, 2, { 4, 6 }, { 4, 2 }, tmdc_abs_at, tmdc_btn_at },
255
                        {   8, "ThrustMaster FragMaster",                 4, 0, { 8, 2 }, { 0, 0 }, tmdc_abs_fm, tmdc_btn_fm },
256
                        { 163, "Thrustmaster Fusion GamePad",             2, 0, { 8, 2 }, { 0, 0 }, tmdc_abs, tmdc_btn_pad },
257
                        {   0, "Unknown %d-axis, %d-button TM device %d", 0, 0, { 0, 0 }, { 0, 0 }, tmdc_abs, tmdc_btn_joy }};
258
 
259
        unsigned char data[2][TMDC_MAX_LENGTH];
260
        struct tmdc *tmdc;
261
        int i, j, k, l, m;
262
 
263
        if (!(tmdc = kmalloc(sizeof(struct tmdc), GFP_KERNEL)))
264
                return;
265
        memset(tmdc, 0, sizeof(struct tmdc));
266
 
267
        gameport->private = tmdc;
268
 
269
        tmdc->gameport = gameport;
270
        init_timer(&tmdc->timer);
271
        tmdc->timer.data = (long) tmdc;
272
        tmdc->timer.function = tmdc_timer;
273
 
274
        if (gameport_open(gameport, dev, GAMEPORT_MODE_RAW))
275
                goto fail1;
276
 
277
        if (!(tmdc->exists = tmdc_read_packet(gameport, data)))
278
                goto fail2;
279
 
280
        for (j = 0; j < 2; j++)
281
                if (tmdc->exists & (1 << j)) {
282
 
283
                        tmdc->mode[j] = data[j][TMDC_BYTE_ID];
284
 
285
                        for (m = 0; models[m].id && models[m].id != tmdc->mode[j]; m++);
286
 
287
                        tmdc->abs[j] = models[m].axes;
288
                        tmdc->btn[j] = models[m].buttons;
289
 
290
                        if (!models[m].id) {
291
                                models[m].abs = data[j][TMDC_BYTE_DEF] >> 4;
292
                                for (k = 0; k < 4; k++)
293
                                        models[m].btnc[k] = k < (data[j][TMDC_BYTE_DEF] & 0xf) ? 8 : 0;
294
                        }
295
 
296
                        tmdc->absc[j] = models[m].abs;
297
                        for (k = 0; k < 4; k++) {
298
                                tmdc->btnc[j][k] = models[m].btnc[k];
299
                                tmdc->btno[j][k] = models[m].btno[k];
300
                        }
301
 
302
                        sprintf(tmdc->name[j], models[m].name, models[m].abs,
303
                                (data[j][TMDC_BYTE_DEF] & 0xf) << 3, tmdc->mode[j]);
304
 
305
                        tmdc->dev[j].private = tmdc;
306
                        tmdc->dev[j].open = tmdc_open;
307
                        tmdc->dev[j].close = tmdc_close;
308
 
309
                        tmdc->dev[j].name = tmdc->name[j];
310
                        tmdc->dev[j].idbus = BUS_GAMEPORT;
311
                        tmdc->dev[j].idvendor = GAMEPORT_ID_VENDOR_THRUSTMASTER;
312
                        tmdc->dev[j].idproduct = models[m].id;
313
                        tmdc->dev[j].idversion = 0x0100;
314
 
315
                        tmdc->dev[j].evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
316
 
317
                        for (i = 0; i < models[m].abs && i < TMDC_ABS; i++) {
318
                                if (tmdc->abs[i] < 0) continue;
319
                                set_bit(tmdc->abs[j][i], tmdc->dev[j].absbit);
320
                                tmdc->dev[j].absmin[tmdc->abs[j][i]] = 8;
321
                                tmdc->dev[j].absmax[tmdc->abs[j][i]] = 248;
322
                                tmdc->dev[j].absfuzz[tmdc->abs[j][i]] = 2;
323
                                tmdc->dev[j].absflat[tmdc->abs[j][i]] = 4;
324
                        }
325
 
326
                        for (i = 0; i < models[m].hats && i < TMDC_ABS_HAT; i++) {
327
                                set_bit(tmdc_abs_hat[i], tmdc->dev[j].absbit);
328
                                tmdc->dev[j].absmin[tmdc_abs_hat[i]] = -1;
329
                                tmdc->dev[j].absmax[tmdc_abs_hat[i]] = 1;
330
                        }
331
 
332
                        for (k = l = 0; k < 4; k++) {
333
                                for (i = 0; i < models[m].btnc[k] && i < TMDC_BTN; i++)
334
                                        set_bit(tmdc->btn[j][i + l], tmdc->dev[j].keybit);
335
                                l += models[m].btnc[k];
336
                        }
337
 
338
                        input_register_device(tmdc->dev + j);
339
                        printk(KERN_INFO "input%d: %s on gameport%d.%d\n",
340
                                tmdc->dev[j].number, tmdc->name[j], gameport->number, j);
341
                }
342
 
343
        return;
344
fail2:  gameport_close(gameport);
345
fail1:  kfree(tmdc);
346
}
347
 
348
static void tmdc_disconnect(struct gameport *gameport)
349
{
350
        struct tmdc *tmdc = gameport->private;
351
        int i;
352
        for (i = 0; i < 2; i++)
353
                if (tmdc->exists & (1 << i))
354
                        input_unregister_device(tmdc->dev + i);
355
        gameport_close(gameport);
356
        kfree(tmdc);
357
}
358
 
359
static struct gameport_dev tmdc_dev = {
360
        connect:        tmdc_connect,
361
        disconnect:     tmdc_disconnect,
362
};
363
 
364
int __init tmdc_init(void)
365
{
366
        gameport_register_device(&tmdc_dev);
367
        return 0;
368
}
369
 
370
void __exit tmdc_exit(void)
371
{
372
        gameport_unregister_device(&tmdc_dev);
373
}
374
 
375
module_init(tmdc_init);
376
module_exit(tmdc_exit);
377
 
378
MODULE_LICENSE("GPL");

powered by: WebSVN 2.1.0

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