1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
|
/*
* joy-thrustmaster.c Version 1.2
*
* Copyright (c) 1998 Vojtech Pavlik
*/
/*
* This is a module for the Linux joystick driver, supporting
* ThrustMaster DirectConnect (BSP) joystick family.
*/
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Should you need to contact me, the author, you can do so either by
* e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail:
* Vojtech Pavlik, Ucitelska 1576, Prague 8, 182 00 Czech Republic
*/
#include <asm/io.h>
#include <asm/system.h>
#include <linux/delay.h>
#include <linux/errno.h>
#include <linux/ioport.h>
#include <linux/joystick.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/string.h>
#define JS_TM_MAX_START 400
#define JS_TM_MAX_STROBE 25
#define JS_TM_MAX_LENGTH 13
#define JS_TM_MODE_M3DI 1
#define JS_TM_MODE_3DRP 3
#define JS_TM_MODE_WCS3 4
#define JS_TM_MODE_MAX 5 /* Last mode + 1 */
#define JS_TM_BYTE_A0 0
#define JS_TM_BYTE_A1 1
#define JS_TM_BYTE_A2 3
#define JS_TM_BYTE_A3 4
#define JS_TM_BYTE_A4 6
#define JS_TM_BYTE_A5 7
#define JS_TM_BYTE_D0 2
#define JS_TM_BYTE_D1 5
#define JS_TM_BYTE_D2 8
#define JS_TM_BYTE_D3 9
#define JS_TM_BYTE_ID 10
#define JS_TM_BYTE_REV 11
#define JS_TM_BYTE_DEF 12
static int js_tm_port_list[] __initdata = {0x201, 0};
static struct js_port* js_tm_port __initdata = NULL;
struct js_tm_info {
int io;
unsigned char mode;
};
static int js_tm_id_to_def[JS_TM_MODE_MAX] = {0x00, 0x42, 0x00, 0x22, 0x00};
/*
* js_tm_read_packet() reads a ThrustMaster packet.
*/
static int js_tm_read_packet(int io, unsigned char *data)
{
unsigned int t, t1;
unsigned char u, v, error;
int i, j;
unsigned long flags;
int start = (js_time_speed * JS_TM_MAX_START) >> 10;
int strobe = (js_time_speed * JS_TM_MAX_STROBE) >> 10;
error = 0;
i = j = 0;
__save_flags(flags);
__cli();
outb(0xff,io);
t = js_get_time();
do {
u = inb(io);
t1 = js_get_time();
} while ((u & 1) && js_delta(t1, t) < start);
t = t1;
u >>= 4;
do {
v = inb(io) >> 4;
t1 = js_get_time();
if ((u ^ v) & u & 2) {
if (j) {
if (j < 9) { /* Data bit */
data[i] |= (~v & 1) << (j - 1);
j++;
} else { /* Stop bit */
error |= v & 1;
j = 0;
i++;
}
} else { /* Start bit */
data[i] = 0;
error |= ~v & 1;
j++;
}
t = t1;
}
u = v;
} while (!error && i < JS_TM_MAX_LENGTH && js_delta(t1,t) < strobe);
__restore_flags(flags);
return -(i != JS_TM_MAX_LENGTH);
}
/*
* js_tm_read() reads and analyzes ThrustMaster joystick data.
*/
static int js_tm_read(void *xinfo, int **axes, int **buttons)
{
struct js_tm_info *info = xinfo;
unsigned char data[JS_TM_MAX_LENGTH];
if (js_tm_read_packet(info->io, data)) {
printk(KERN_WARNING "joy-thrustmaster: failed to read data packet\n");
return -1;
}
if (data[JS_TM_BYTE_ID] != info->mode) {
printk(KERN_WARNING "joy-thrustmaster: ID (%d) != mode (%d)\n",
data[JS_TM_BYTE_ID], info->mode);
return -1;
}
if (data[JS_TM_BYTE_DEF] != js_tm_id_to_def[info->mode]) {
printk(KERN_WARNING "joy-thrustmaster: DEF (%d) != def(mode) (%d)\n",
data[JS_TM_BYTE_DEF], js_tm_id_to_def[info->mode]);
return -1;
}
switch (info->mode) {
case JS_TM_MODE_M3DI:
axes[0][0] = data[JS_TM_BYTE_A0];
axes[0][1] = data[JS_TM_BYTE_A1];
axes[0][2] = data[JS_TM_BYTE_A2];
axes[0][3] = data[JS_TM_BYTE_A3];
axes[0][4] = ((data[JS_TM_BYTE_D0] >> 3) & 1) - ((data[JS_TM_BYTE_D0] >> 1) & 1);
axes[0][5] = ((data[JS_TM_BYTE_D0] >> 2) & 1) - ( data[JS_TM_BYTE_D0] & 1);
buttons[0][0] = ((data[JS_TM_BYTE_D0] >> 6) & 0x01) | ((data[JS_TM_BYTE_D0] >> 3) & 0x06)
| ((data[JS_TM_BYTE_D0] >> 4) & 0x08) | ((data[JS_TM_BYTE_D1] >> 2) & 0x30);
return 0;
case JS_TM_MODE_3DRP:
axes[0][0] = data[JS_TM_BYTE_A0];
axes[0][1] = data[JS_TM_BYTE_A1];
buttons[0][0] = ( data[JS_TM_BYTE_D0] & 0x3f) | ((data[JS_TM_BYTE_D1] << 6) & 0xc0)
| (( ((int) data[JS_TM_BYTE_D0]) << 2) & 0x300);
return 0;
}
return -1;
}
/*
* js_tm_open() is a callback from the file open routine.
*/
static int js_tm_open(struct js_dev *jd)
{
MOD_INC_USE_COUNT;
return 0;
}
/*
* js_tm_close() is a callback from the file release routine.
*/
static int js_tm_close(struct js_dev *jd)
{
MOD_DEC_USE_COUNT;
return 0;
}
/*
* js_tm_init_corr() initializes the correction values for
* ThrustMaster joysticks.
*/
static void __init js_tm_init_corr(int num_axes, int mode, int **axes, struct js_corr **corr)
{
int j;
for (j = 0; j < num_axes; j++) {
corr[0][j].type = JS_CORR_BROKEN;
corr[0][j].prec = 0;
corr[0][j].coef[0] = 127 - 2;
corr[0][j].coef[1] = 128 + 2;
corr[0][j].coef[2] = (1 << 29) / (127 - 4);
corr[0][j].coef[3] = (1 << 29) / (127 - 4);
}
switch (mode) {
case JS_TM_MODE_M3DI: j = 4; break;
case JS_TM_MODE_3DRP: j = 2; break;
default: j = 0; break;
}
for (; j < num_axes; j++) {
corr[0][j].type = JS_CORR_BROKEN;
corr[0][j].prec = 0;
corr[0][j].coef[0] = 0;
corr[0][j].coef[1] = 0;
corr[0][j].coef[2] = (1 << 29);
corr[0][j].coef[3] = (1 << 29);
}
}
/*
* js_tm_probe() probes for ThrustMaster type joysticks.
*/
static struct js_port __init *js_tm_probe(int io, struct js_port *port)
{
struct js_tm_info info;
char *names[JS_TM_MODE_MAX] = { NULL, "ThrustMaster Millenium 3D Inceptor", NULL,
"ThrustMaster Rage 3D Gamepad", "ThrustMaster WCS III" };
char axes[JS_TM_MODE_MAX] = { 0, 6, 0, 2, 0 };
char buttons[JS_TM_MODE_MAX] = { 0, 5, 0, 10, 0 };
unsigned char data[JS_TM_MAX_LENGTH];
unsigned char u;
if (check_region(io, 1)) return port;
if (((u = inb(io)) & 3) == 3) return port;
outb(0xff,io);
if (!((inb(io) ^ u) & ~u & 0xf)) return port;
if(js_tm_read_packet(io, data)) {
printk(KERN_WARNING "joy-thrustmaster: probe - can't read packet\n");
return port;
}
info.io = io;
info.mode = data[JS_TM_BYTE_ID];
if (!info.mode) return port;
if (info.mode >= JS_TM_MODE_MAX || !names[info.mode]) {
printk(KERN_WARNING "joy-thrustmaster: unknown device detected "
"(io=%#x, id=%d), contact <vojtech@ucw.cz>\n",
io, info.mode);
return port;
}
if (data[JS_TM_BYTE_DEF] != js_tm_id_to_def[info.mode]) {
printk(KERN_WARNING "joy-thrustmaster: wrong DEF (%d) for ID %d - should be %d\n",
data[JS_TM_BYTE_DEF], info.mode, js_tm_id_to_def[info.mode]);
}
request_region(io, 1, "joystick (thrustmaster)");
port = js_register_port(port, &info, 1, sizeof(struct js_tm_info), js_tm_read);
printk(KERN_INFO "js%d: %s revision %d at %#x\n",
js_register_device(port, 0, axes[info.mode], buttons[info.mode],
names[info.mode], js_tm_open, js_tm_close), names[info.mode], data[JS_TM_BYTE_REV], io);
js_tm_init_corr(axes[info.mode], info.mode, port->axes, port->corr);
return port;
}
#ifdef MODULE
int init_module(void)
#else
int __init js_tm_init(void)
#endif
{
int *p;
for (p = js_tm_port_list; *p; p++) js_tm_port = js_tm_probe(*p, js_tm_port);
if (js_tm_port) return 0;
#ifdef MODULE
printk(KERN_WARNING "joy-thrustmaster: no joysticks found\n");
#endif
return -ENODEV;
}
#ifdef MODULE
void cleanup_module(void)
{
struct js_tm_info *info;
while (js_tm_port != NULL) {
js_unregister_device(js_tm_port->devs[0]);
info = js_tm_port->info;
release_region(info->io, 1);
js_tm_port = js_unregister_port(js_tm_port);
}
}
#endif
|