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

Subversion Repositories or1k

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

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1275 phoenix
/*
2
 * $Id: adi.c,v 1.1.1.1 2004-04-15 02:03:24 phoenix Exp $
3
 *
4
 *  Copyright (c) 1998-2000 Vojtech Pavlik
5
 *
6
 *  Sponsored by SuSE
7
 */
8
 
9
/*
10
 * Logitech ADI joystick family 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/module.h>
36
#include <linux/string.h>
37
#include <linux/slab.h>
38
#include <linux/input.h>
39
#include <linux/gameport.h>
40
#include <linux/init.h>
41
 
42
/*
43
 * Times, array sizes, flags, ids.
44
 */
45
 
46
#define ADI_MAX_START           200     /* Trigger to packet timeout [200us] */
47
#define ADI_MAX_STROBE          40      /* Single bit timeout [40us] */
48
#define ADI_REFRESH_TIME        HZ/50   /* How often to poll the joystick [20 ms] */
49
#define ADI_INIT_DELAY          10      /* Delay after init packet [10ms] */
50
#define ADI_DATA_DELAY          4       /* Delay after data packet [4ms] */
51
 
52
#define ADI_MAX_LENGTH          256
53
#define ADI_MIN_LENGTH          8
54
#define ADI_MIN_LEN_LENGTH      10
55
#define ADI_MIN_ID_LENGTH       66
56
#define ADI_MAX_NAME_LENGTH     48
57
#define ADI_MAX_CNAME_LENGTH    16
58
 
59
#define ADI_FLAG_HAT            0x04
60
#define ADI_FLAG_10BIT          0x08
61
 
62
#define ADI_ID_TPD              0x01
63
#define ADI_ID_WGP              0x06
64
#define ADI_ID_WGPE             0x08
65
#define ADI_ID_MAX              0x0a
66
 
67
/*
68
 * Names, buttons, axes ...
69
 */
70
 
71
static char *adi_names[] = {    "WingMan Extreme Digital", "ThunderPad Digital", "SideCar", "CyberMan 2",
72
                                "WingMan Interceptor", "WingMan Formula", "WingMan GamePad",
73
                                "WingMan Extreme Digital 3D", "WingMan GamePad Extreme",
74
                                "WingMan GamePad USB", "Unknown Device %#x" };
75
 
76
static char adi_wmgpe_abs[] =   { ABS_X, ABS_Y, ABS_HAT0X, ABS_HAT0Y };
77
static char adi_wmi_abs[] =     { ABS_X, ABS_Y, ABS_THROTTLE, ABS_HAT0X, ABS_HAT0Y, ABS_HAT1X, ABS_HAT1Y, ABS_HAT2X, ABS_HAT2Y };
78
static char adi_wmed3d_abs[] =  { ABS_X, ABS_Y, ABS_THROTTLE, ABS_RZ, ABS_HAT0X, ABS_HAT0Y };
79
static char adi_cm2_abs[] =     { ABS_X, ABS_Y, ABS_Z, ABS_RX, ABS_RY, ABS_RZ };
80
static char adi_wmf_abs[] =     { ABS_WHEEL, ABS_GAS, ABS_BRAKE, ABS_HAT0X, ABS_HAT0Y, ABS_HAT1X, ABS_HAT1Y, ABS_HAT2X, ABS_HAT2Y };
81
 
82
static short adi_wmgpe_key[] =  { BTN_A, BTN_B, BTN_C, BTN_X, BTN_Y, BTN_Z,  BTN_TL, BTN_TR, BTN_START, BTN_MODE, BTN_SELECT };
83
static short adi_wmi_key[] =    { BTN_TRIGGER,  BTN_TOP, BTN_THUMB, BTN_TOP2, BTN_BASE, BTN_BASE2, BTN_BASE3, BTN_BASE4, BTN_EXTRA };
84
static short adi_wmed3d_key[] = { BTN_TRIGGER, BTN_THUMB, BTN_THUMB2, BTN_TOP, BTN_TOP2, BTN_BASE, BTN_BASE2 };
85
static short adi_cm2_key[] =    { BTN_1, BTN_2, BTN_3, BTN_4, BTN_5, BTN_6, BTN_7, BTN_8 };
86
 
87
static char* adi_abs[] = { adi_wmi_abs, adi_wmgpe_abs, adi_wmf_abs, adi_cm2_abs, adi_wmi_abs, adi_wmf_abs,
88
                           adi_wmgpe_abs, adi_wmed3d_abs, adi_wmgpe_abs, adi_wmgpe_abs, adi_wmi_abs };
89
 
90
static short* adi_key[] = { adi_wmi_key, adi_wmgpe_key, adi_cm2_key, adi_cm2_key, adi_wmi_key, adi_cm2_key,
91
                            adi_wmgpe_key, adi_wmed3d_key, adi_wmgpe_key, adi_wmgpe_key, adi_wmi_key };
92
 
93
/*
94
 * Hat to axis conversion arrays.
95
 */
96
 
97
static struct {
98
        int x;
99
        int y;
100
} adi_hat_to_axis[] = {{ 0, 0}, { 0,-1}, { 1,-1}, { 1, 0}, { 1, 1}, { 0, 1}, {-1, 1}, {-1, 0}, {-1,-1}};
101
 
102
/*
103
 * Per-port information.
104
 */
105
 
106
struct adi {
107
        struct input_dev dev;
108
        int length;
109
        int ret;
110
        int idx;
111
        unsigned char id;
112
        char buttons;
113
        char axes10;
114
        char axes8;
115
        signed char pad;
116
        char hats;
117
        char *abs;
118
        short *key;
119
        char name[ADI_MAX_NAME_LENGTH];
120
        char cname[ADI_MAX_CNAME_LENGTH];
121
        unsigned char data[ADI_MAX_LENGTH];
122
};
123
 
124
struct adi_port {
125
        struct gameport *gameport;
126
        struct timer_list timer;
127
        struct adi adi[2];
128
        int bad;
129
        int reads;
130
        int used;
131
};
132
 
133
/*
134
 * adi_read_packet() reads a Logitech ADI packet.
135
 */
136
 
137
static void adi_read_packet(struct adi_port *port)
138
{
139
        struct adi *adi = port->adi;
140
        struct gameport *gameport = port->gameport;
141
        unsigned char u, v, w, x, z;
142
        int t[2], s[2], i;
143
        unsigned long flags;
144
 
145
        for (i = 0; i < 2; i++) {
146
                adi[i].ret = -1;
147
                t[i] = gameport_time(gameport, ADI_MAX_START);
148
                s[i] = 0;
149
        }
150
 
151
        __save_flags(flags);
152
        __cli();
153
 
154
        gameport_trigger(gameport);
155
        v = z = gameport_read(gameport);
156
 
157
        do {
158
                u = v;
159
                w = u ^ (v = x = gameport_read(gameport));
160
                for (i = 0; i < 2; i++, w >>= 2, x >>= 2) {
161
                        t[i]--;
162
                        if ((w & 0x30) && s[i]) {
163
                                if ((w & 0x30) < 0x30 && adi[i].ret < ADI_MAX_LENGTH && t[i] > 0) {
164
                                        adi[i].data[++adi[i].ret] = w;
165
                                        t[i] = gameport_time(gameport, ADI_MAX_STROBE);
166
                                } else t[i] = 0;
167
                        } else if (!(x & 0x30)) s[i] = 1;
168
                }
169
        } while (t[0] > 0 || t[1] > 0);
170
 
171
        __restore_flags(flags);
172
 
173
        return;
174
}
175
 
176
/*
177
 * adi_move_bits() detects a possible 2-stream mode, and moves
178
 * the bits accordingly.
179
 */
180
 
181
static void adi_move_bits(struct adi_port *port, int length)
182
{
183
        int i;
184
        struct adi *adi = port->adi;
185
 
186
        adi[0].idx = adi[1].idx = 0;
187
 
188
        if (adi[0].ret <= 0 || adi[1].ret <= 0) return;
189
        if (adi[0].data[0] & 0x20 || ~adi[1].data[0] & 0x20) return;
190
 
191
        for (i = 1; i <= adi[1].ret; i++)
192
                adi[0].data[((length - 1) >> 1) + i + 1] = adi[1].data[i];
193
 
194
        adi[0].ret += adi[1].ret;
195
        adi[1].ret = -1;
196
}
197
 
198
/*
199
 * adi_get_bits() gathers bits from the data packet.
200
 */
201
 
202
static inline int adi_get_bits(struct adi *adi, int count)
203
{
204
        int bits = 0;
205
        int i;
206
        if ((adi->idx += count) > adi->ret) return 0;
207
        for (i = 0; i < count; i++)
208
                bits |= ((adi->data[adi->idx - i] >> 5) & 1) << i;
209
        return bits;
210
}
211
 
212
/*
213
 * adi_decode() decodes Logitech joystick data into input events.
214
 */
215
 
216
static int adi_decode(struct adi *adi)
217
{
218
        struct input_dev *dev = &adi->dev;
219
        char *abs = adi->abs;
220
        short *key = adi->key;
221
        int i, t;
222
 
223
        if (adi->ret < adi->length || adi->id != (adi_get_bits(adi, 4) | (adi_get_bits(adi, 4) << 4)))
224
                return -1;
225
 
226
        for (i = 0; i < adi->axes10; i++)
227
                input_report_abs(dev, *abs++, adi_get_bits(adi, 10));
228
 
229
        for (i = 0; i < adi->axes8; i++)
230
                input_report_abs(dev, *abs++, adi_get_bits(adi, 8));
231
 
232
        for (i = 0; i < adi->buttons && i < 63; i++) {
233
                if (i == adi->pad) {
234
                        t = adi_get_bits(adi, 4);
235
                        input_report_abs(dev, *abs++, ((t >> 2) & 1) - ( t       & 1));
236
                        input_report_abs(dev, *abs++, ((t >> 1) & 1) - ((t >> 3) & 1));
237
                }
238
                input_report_key(dev, *key++, adi_get_bits(adi, 1));
239
        }
240
 
241
        for (i = 0; i < adi->hats; i++) {
242
                if ((t = adi_get_bits(adi, 4)) > 8) t = 0;
243
                input_report_abs(dev, *abs++, adi_hat_to_axis[t].x);
244
                input_report_abs(dev, *abs++, adi_hat_to_axis[t].y);
245
        }
246
 
247
        for (i = 63; i < adi->buttons; i++)
248
                input_report_key(dev, *key++, adi_get_bits(adi, 1));
249
 
250
        return 0;
251
}
252
 
253
/*
254
 * adi_read() reads the data packet and decodes it.
255
 */
256
 
257
static int adi_read(struct adi_port *port)
258
{
259
        int i;
260
        int result = 0;
261
 
262
        adi_read_packet(port);
263
        adi_move_bits(port, port->adi[0].length);
264
 
265
        for (i = 0; i < 2; i++)
266
                if (port->adi[i].length)
267
                         result |= adi_decode(port->adi + i);
268
 
269
        return result;
270
}
271
 
272
/*
273
 * adi_timer() repeatedly polls the Logitech joysticks.
274
 */
275
 
276
static void adi_timer(unsigned long data)
277
{
278
        struct adi_port *port = (void *) data;
279
        port->bad -= adi_read(port);
280
        port->reads++;
281
        mod_timer(&port->timer, jiffies + ADI_REFRESH_TIME);
282
}
283
 
284
/*
285
 * adi_open() is a callback from the input open routine.
286
 */
287
 
288
static int adi_open(struct input_dev *dev)
289
{
290
        struct adi_port *port = dev->private;
291
        if (!port->used++)
292
                mod_timer(&port->timer, jiffies + ADI_REFRESH_TIME);
293
        return 0;
294
}
295
 
296
/*
297
 * adi_close() is a callback from the input close routine.
298
 */
299
 
300
static void adi_close(struct input_dev *dev)
301
{
302
        struct adi_port *port = dev->private;
303
        if (!--port->used)
304
                del_timer(&port->timer);
305
}
306
 
307
/*
308
 * adi_init_digital() sends a trigger & delay sequence
309
 * to reset and initialize a Logitech joystick into digital mode.
310
 */
311
 
312
static void adi_init_digital(struct gameport *gameport)
313
{
314
        int seq[] = { 3, -2, -3, 10, -6, -11, -7, -9, 11, 0 };
315
        int i;
316
 
317
        for (i = 0; seq[i]; i++) {
318
                gameport_trigger(gameport);
319
                if (seq[i] > 0) wait_ms(seq[i]);
320
                if (seq[i] < 0) mdelay(-seq[i]);
321
        }
322
}
323
 
324
static void adi_id_decode(struct adi *adi, struct adi_port *port)
325
{
326
        int i, t;
327
 
328
        if (adi->ret < ADI_MIN_ID_LENGTH) /* Minimum ID packet length */
329
                return;
330
 
331
        if (adi->ret < (t = adi_get_bits(adi, 10))) {
332
                printk(KERN_WARNING "adi: Short ID packet: reported: %d != read: %d\n", t, adi->ret);
333
                return;
334
        }
335
 
336
        adi->id = adi_get_bits(adi, 4) | (adi_get_bits(adi, 4) << 4);
337
 
338
        if ((t = adi_get_bits(adi, 4)) & ADI_FLAG_HAT) adi->hats++;
339
 
340
        adi->length = adi_get_bits(adi, 10);
341
 
342
        if (adi->length >= ADI_MAX_LENGTH || adi->length < ADI_MIN_LENGTH) {
343
                printk(KERN_WARNING "adi: Bad data packet length (%d).\n", adi->length);
344
                adi->length = 0;
345
                return;
346
        }
347
 
348
        adi->axes8 = adi_get_bits(adi, 4);
349
        adi->buttons = adi_get_bits(adi, 6);
350
 
351
        if (adi_get_bits(adi, 6) != 8 && adi->hats) {
352
                printk(KERN_WARNING "adi: Other than 8-dir POVs not supported yet.\n");
353
                adi->length = 0;
354
                return;
355
        }
356
 
357
        adi->buttons += adi_get_bits(adi, 6);
358
        adi->hats += adi_get_bits(adi, 4);
359
 
360
        i = adi_get_bits(adi, 4);
361
 
362
        if (t & ADI_FLAG_10BIT) {
363
                adi->axes10 = adi->axes8 - i;
364
                adi->axes8 = i;
365
        }
366
 
367
        t = adi_get_bits(adi, 4);
368
 
369
        for (i = 0; i < t; i++)
370
                adi->cname[i] = adi_get_bits(adi, 8);
371
        adi->cname[i] = 0;
372
 
373
        t = 8 + adi->buttons + adi->axes10 * 10 + adi->axes8 * 8 + adi->hats * 4;
374
        if (adi->length != t && adi->length != t + (t & 1)) {
375
                printk(KERN_WARNING "adi: Expected length %d != data length %d\n", t, adi->length);
376
                adi->length = 0;
377
                return;
378
        }
379
 
380
        switch (adi->id) {
381
                case ADI_ID_TPD:
382
                        adi->pad = 4;
383
                        adi->buttons -= 4;
384
                        break;
385
                case ADI_ID_WGP:
386
                        adi->pad = 0;
387
                        adi->buttons -= 4;
388
                        break;
389
                default:
390
                        adi->pad = -1;
391
                        break;
392
        }
393
}
394
 
395
static void adi_init_input(struct adi *adi, struct adi_port *port)
396
{
397
        int i, t;
398
        char buf[ADI_MAX_NAME_LENGTH];
399
 
400
        if (!adi->length) return;
401
 
402
        t = adi->id < ADI_ID_MAX ? adi->id : ADI_ID_MAX;
403
 
404
        sprintf(buf, adi_names[t], adi->id);
405
        sprintf(adi->name, "Logitech %s", buf);
406
 
407
        adi->abs = adi_abs[t];
408
        adi->key = adi_key[t];
409
 
410
        adi->dev.open = adi_open;
411
        adi->dev.close = adi_close;
412
 
413
        adi->dev.name = adi->name;
414
        adi->dev.idbus = BUS_GAMEPORT;
415
        adi->dev.idvendor = GAMEPORT_ID_VENDOR_LOGITECH;
416
        adi->dev.idproduct = adi->id;
417
        adi->dev.idversion = 0x0100;
418
 
419
        adi->dev.private = port;
420
        adi->dev.evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
421
 
422
        for (i = 0; i < adi->axes10 + adi->axes8 + (adi->hats + (adi->pad > 0)) * 2; i++)
423
                set_bit(adi->abs[i], &adi->dev.absbit);
424
 
425
        for (i = 0; i < adi->buttons; i++)
426
                set_bit(adi->key[i], &adi->dev.keybit);
427
}
428
 
429
static void adi_init_center(struct adi *adi)
430
{
431
        int i, t, x;
432
 
433
        if (!adi->length) return;
434
 
435
        for (i = 0; i < adi->axes10 + adi->axes8 + (adi->hats + (adi->pad > 0)) * 2; i++) {
436
 
437
                t = adi->abs[i];
438
                x = adi->dev.abs[t];
439
 
440
                if (t == ABS_THROTTLE || t == ABS_RUDDER || adi->id == ADI_ID_WGPE) {
441
                        if (i < adi->axes10) x = 512; else x = 128;
442
                }
443
 
444
                if (i < adi->axes10) {
445
                        adi->dev.absmax[t] = x * 2 - 64;
446
                        adi->dev.absmin[t] = 64;
447
                        adi->dev.absfuzz[t] = 2;
448
                        adi->dev.absflat[t] = 16;
449
                        continue;
450
                }
451
 
452
                if (i < adi->axes10 + adi->axes8) {
453
                        adi->dev.absmax[t] = x * 2 - 48;
454
                        adi->dev.absmin[t] = 48;
455
                        adi->dev.absfuzz[t] = 1;
456
                        adi->dev.absflat[t] = 16;
457
                        continue;
458
                }
459
 
460
                adi->dev.absmax[t] = 1;
461
                adi->dev.absmin[t] = -1;
462
        }
463
}
464
 
465
/*
466
 * adi_connect() probes for Logitech ADI joysticks.
467
 */
468
 
469
static void adi_connect(struct gameport *gameport, struct gameport_dev *dev)
470
{
471
        struct adi_port *port;
472
        int i;
473
 
474
        if (!(port = kmalloc(sizeof(struct adi_port), GFP_KERNEL)))
475
                return;
476
        memset(port, 0, sizeof(struct adi_port));
477
 
478
        gameport->private = port;
479
 
480
        port->gameport = gameport;
481
        init_timer(&port->timer);
482
        port->timer.data = (long) port;
483
        port->timer.function = adi_timer;
484
 
485
        if (gameport_open(gameport, dev, GAMEPORT_MODE_RAW)) {
486
                kfree(port);
487
                return;
488
        }
489
 
490
        adi_init_digital(gameport);
491
        adi_read_packet(port);
492
 
493
        if (port->adi[0].ret >= ADI_MIN_LEN_LENGTH)
494
                adi_move_bits(port, adi_get_bits(port->adi, 10));
495
 
496
        for (i = 0; i < 2; i++) {
497
                adi_id_decode(port->adi + i, port);
498
                adi_init_input(port->adi + i, port);
499
        }
500
 
501
        if (!port->adi[0].length && !port->adi[1].length) {
502
                gameport_close(gameport);
503
                kfree(port);
504
                return;
505
        }
506
 
507
        wait_ms(ADI_INIT_DELAY);
508
        if (adi_read(port)) {
509
                wait_ms(ADI_DATA_DELAY);
510
                adi_read(port);
511
        }
512
 
513
        for (i = 0; i < 2; i++)
514
                if (port->adi[i].length > 0) {
515
                        adi_init_center(port->adi + i);
516
                        input_register_device(&port->adi[i].dev);
517
                        printk(KERN_INFO "input%d: %s [%s] on gameport%d.%d\n",
518
                                port->adi[i].dev.number, port->adi[i].name, port->adi[i].cname, gameport->number, i);
519
                }
520
}
521
 
522
static void adi_disconnect(struct gameport *gameport)
523
{
524
        int i;
525
 
526
        struct adi_port *port = gameport->private;
527
        for (i = 0; i < 2; i++)
528
                if (port->adi[i].length > 0)
529
                        input_unregister_device(&port->adi[i].dev);
530
        gameport_close(gameport);
531
        kfree(port);
532
}
533
 
534
/*
535
 * The gameport device structure.
536
 */
537
 
538
static struct gameport_dev adi_dev = {
539
        connect:        adi_connect,
540
        disconnect:     adi_disconnect,
541
};
542
 
543
int __init adi_init(void)
544
{
545
        gameport_register_device(&adi_dev);
546
        return 0;
547
}
548
 
549
void __exit adi_exit(void)
550
{
551
        gameport_unregister_device(&adi_dev);
552
}
553
 
554
module_init(adi_init);
555
module_exit(adi_exit);
556
 
557
MODULE_LICENSE("GPL");

powered by: WebSVN 2.1.0

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