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

Subversion Repositories or1k

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

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1275 phoenix
/*
2
 * $Id: iforce.c,v 1.1.1.1 2004-04-15 02:03:29 phoenix Exp $
3
 *
4
 *  Copyright (c) 2000-2001 Vojtech Pavlik <vojtech@suse.cz>
5
 *  Copyright (c) 2001 Johann Deneux <deneux@ifrance.com>
6
 *
7
 *  USB/RS232 I-Force joysticks and wheels.
8
 *
9
 *  Sponsored by SuSE
10
 */
11
 
12
/*
13
 * This program is free software; you can redistribute it and/or modify
14
 * it under the terms of the GNU General Public License as published by
15
 * the Free Software Foundation; either version 2 of the License, or
16
 * (at your option) any later version.
17
 *
18
 * This program is distributed in the hope that it will be useful,
19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21
 * GNU General Public License for more details.
22
 *
23
 * You should have received a copy of the GNU General Public License
24
 * along with this program; if not, write to the Free Software
25
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26
 *
27
 * Should you need to contact me, the author, you can do so either by
28
 * e-mail - mail your message to <vojtech@suse.cz>, or by paper mail:
29
 * Vojtech Pavlik, Ucitelska 1576, Prague 8, 182 00 Czech Republic
30
 */
31
 
32
#include <linux/kernel.h>
33
#include <linux/slab.h>
34
#include <linux/input.h>
35
#include <linux/module.h>
36
#include <linux/init.h>
37
#include <linux/spinlock.h>
38
#include <linux/usb.h>
39
#include <linux/serio.h>
40
#include <linux/config.h>
41
 
42
/* FF: This module provides arbitrary resource management routines.
43
 * I use it to manage the device's memory.
44
 * Despite the name of this module, I am *not* going to access the ioports.
45
 */
46
#include <linux/ioport.h>
47
 
48
MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>, Johann Deneux <deneux@ifrance.com>");
49
MODULE_DESCRIPTION("USB/RS232 I-Force joysticks and wheels driver");
50
MODULE_LICENSE("GPL");
51
 
52
#define IFORCE_MAX_LENGTH       16
53
 
54
#if defined(CONFIG_INPUT_IFORCE_232) || defined(CONFIG_INPUT_IFORCE_232_MODULE)
55
#define IFORCE_232      1
56
#endif
57
#if defined(CONFIG_INPUT_IFORCE_USB) || defined(CONFIG_INPUT_IFORCE_USB_MODULE)
58
#define IFORCE_USB      2
59
#endif
60
 
61
#define FF_EFFECTS_MAX  32
62
 
63
/* Each force feedback effect is made of one core effect, which can be
64
 * associated to at most to effect modifiers
65
 */
66
#define FF_MOD1_IS_USED         0
67
#define FF_MOD2_IS_USED         1
68
#define FF_CORE_IS_USED         2
69
#define FF_CORE_IS_PLAYED       3
70
#define FF_MODCORE_MAX          3
71
 
72
struct iforce_core_effect {
73
        /* Information about where modifiers are stored in the device's memory */
74
        struct resource mod1_chunk;
75
        struct resource mod2_chunk;
76
        unsigned long flags[NBITS(FF_MODCORE_MAX)];
77
};
78
 
79
#define FF_CMD_EFFECT           0x010e
80
#define FF_CMD_SHAPE            0x0208
81
#define FF_CMD_MAGNITUDE        0x0303
82
#define FF_CMD_PERIOD           0x0407
83
#define FF_CMD_INTERACT         0x050a
84
 
85
#define FF_CMD_AUTOCENTER       0x4002
86
#define FF_CMD_PLAY             0x4103
87
#define FF_CMD_ENABLE           0x4201
88
#define FF_CMD_GAIN             0x4301
89
 
90
#define FF_CMD_QUERY            0xff01
91
 
92
static signed short btn_joystick[] = { BTN_TRIGGER, BTN_TOP, BTN_THUMB, BTN_TOP2, BTN_BASE,
93
        BTN_BASE2, BTN_BASE3, BTN_BASE4, BTN_BASE5, BTN_A, BTN_B, BTN_C, BTN_DEAD, -1 };
94
static signed short btn_wheel[] =    { BTN_TRIGGER, BTN_TOP, BTN_THUMB, BTN_TOP2, BTN_BASE,
95
        BTN_BASE2, BTN_BASE3, BTN_BASE4, BTN_BASE5, BTN_A, BTN_B, BTN_C, -1 };
96
static signed short btn_avb_tw[] =   { BTN_TRIGGER, BTN_THUMB, BTN_TOP, BTN_TOP2, BTN_BASE, BTN_BASE2, BTN_BASE3, BTN_BASE4, -1 };
97
static signed short abs_joystick[] = { ABS_X, ABS_Y, ABS_THROTTLE, ABS_HAT0X, ABS_HAT0Y, -1 };
98
static signed short abs_wheel[] =    { ABS_WHEEL, ABS_GAS, ABS_BRAKE, ABS_HAT0X, ABS_HAT0Y, -1 };
99
static signed short ff_iforce[] =    { FF_PERIODIC, FF_CONSTANT, FF_SPRING, FF_FRICTION,
100
        FF_SQUARE, FF_TRIANGLE, FF_SINE, FF_SAW_UP, FF_SAW_DOWN, FF_GAIN, FF_AUTOCENTER, -1 };
101
 
102
static struct iforce_device {
103
        u16 idvendor;
104
        u16 idproduct;
105
        char *name;
106
        signed short *btn;
107
        signed short *abs;
108
        signed short *ff;
109
} iforce_device[] = {
110
        { 0x044f, 0xa01c, "Thrustmaster Motor Sport GT",                btn_wheel, abs_wheel, ff_iforce },
111
        { 0x046d, 0xc281, "Logitech WingMan Force",                     btn_joystick, abs_joystick, ff_iforce },
112
        { 0x046d, 0xc291, "Logitech WingMan Formula Force",             btn_wheel, abs_wheel, ff_iforce },
113
        { 0x05ef, 0x020a, "AVB Top Shot Pegasus",                       btn_joystick, abs_joystick, ff_iforce },
114
        { 0x05ef, 0x8884, "AVB Mag Turbo Force",                        btn_wheel, abs_wheel, ff_iforce },
115
        { 0x05ef, 0x8888, "AVB Top Shot Force Feedback Racing Wheel",   btn_avb_tw, abs_wheel, ff_iforce },
116
        { 0x061c, 0xc084, "ACT LABS Force RS",                          btn_wheel, abs_wheel, ff_iforce },
117
        { 0x06f8, 0x0001, "Guillemot Race Leader Force Feedback",       btn_wheel, abs_wheel, ff_iforce },
118
        { 0x06f8, 0x0004, "Guillemot Force Feedback Racing Wheel",      btn_wheel, abs_wheel, ff_iforce },
119
        { 0x0000, 0x0000, "Unknown I-Force Device [%04x:%04x]",         btn_joystick, abs_joystick, ff_iforce }
120
};
121
 
122
struct iforce {
123
        struct input_dev dev;           /* Input device interface */
124
        struct iforce_device *type;
125
        char name[64];
126
        int open;
127
        int bus;
128
 
129
        unsigned char data[IFORCE_MAX_LENGTH];
130
        unsigned char edata[IFORCE_MAX_LENGTH];
131
        u16 ecmd;
132
        u16 expect_packet;
133
 
134
#ifdef IFORCE_232
135
        struct serio *serio;            /* RS232 transfer */
136
        int idx, pkt, len, id;
137
        unsigned char csum;
138
#endif
139
#ifdef IFORCE_USB
140
        struct usb_device *usbdev;      /* USB transfer */
141
        struct urb irq, out, ctrl;
142
        struct usb_ctrlrequest dr;
143
#endif
144
                                        /* Force Feedback */
145
        wait_queue_head_t wait;
146
        struct resource device_memory;
147
        struct iforce_core_effect core_effects[FF_EFFECTS_MAX];
148
};
149
 
150
static struct {
151
        __s32 x;
152
        __s32 y;
153
} iforce_hat_to_axis[16] = {{ 0,-1}, { 1,-1}, { 1, 0}, { 1, 1}, { 0, 1}, {-1, 1}, {-1, 0}, {-1,-1}};
154
 
155
/* Get hi and low bytes of a 16-bits int */
156
#define HI(a)   ((unsigned char)((a) >> 8))
157
#define LO(a)   ((unsigned char)((a) & 0xff))
158
 
159
/* Encode a time value */
160
#define TIME_SCALE(a)   ((a) == 0xffff ? 0xffff : (a) * 1000 / 256)
161
 
162
static void dump_packet(char *msg, u16 cmd, unsigned char *data)
163
{
164
        int i;
165
 
166
        printk(KERN_DEBUG "iforce.c: %s ( cmd = %04x, data = ", msg, cmd);
167
        for (i = 0; i < LO(cmd); i++)
168
                printk("%02x ", data[i]);
169
        printk(")\n");
170
}
171
 
172
/*
173
 * Send a packet of bytes to the device
174
 */
175
static void send_packet(struct iforce *iforce, u16 cmd, unsigned char* data)
176
{
177
        switch (iforce->bus) {
178
 
179
#ifdef IFORCE_232
180
                case IFORCE_232: {
181
 
182
                        int i;
183
                        unsigned char csum = 0x2b ^ HI(cmd) ^ LO(cmd);
184
 
185
                        serio_write(iforce->serio, 0x2b);
186
                        serio_write(iforce->serio, HI(cmd));
187
                        serio_write(iforce->serio, LO(cmd));
188
 
189
                        for (i = 0; i < LO(cmd); i++) {
190
                                serio_write(iforce->serio, data[i]);
191
                                csum = csum ^ data[i];
192
                        }
193
 
194
                        serio_write(iforce->serio, csum);
195
                        return;
196
                }
197
#endif
198
#ifdef IFORCE_USB
199
                case IFORCE_USB: {
200
 
201
                        DECLARE_WAITQUEUE(wait, current);
202
                        int timeout = HZ; /* 1 second */
203
 
204
                        memcpy(iforce->out.transfer_buffer + 1, data, LO(cmd));
205
                        ((char*)iforce->out.transfer_buffer)[0] = HI(cmd);
206
                        iforce->out.transfer_buffer_length = LO(cmd) + 2;
207
                        iforce->out.dev = iforce->usbdev;
208
 
209
                        set_current_state(TASK_INTERRUPTIBLE);
210
                        add_wait_queue(&iforce->wait, &wait);
211
 
212
                        if (usb_submit_urb(&iforce->out)) {
213
                                set_current_state(TASK_RUNNING);
214
                                remove_wait_queue(&iforce->wait, &wait);
215
                                return;
216
                        }
217
 
218
                        while (timeout && iforce->out.status == -EINPROGRESS)
219
                                timeout = schedule_timeout(timeout);
220
 
221
                        set_current_state(TASK_RUNNING);
222
                        remove_wait_queue(&iforce->wait, &wait);
223
 
224
                        if (!timeout)
225
                                usb_unlink_urb(&iforce->out);
226
 
227
                        return;
228
                }
229
#endif
230
        }
231
}
232
 
233
static void iforce_process_packet(struct iforce *iforce, u16 cmd, unsigned char *data)
234
{
235
        struct input_dev *dev = &iforce->dev;
236
        int i;
237
 
238
#ifdef IFORCE_232
239
        if (HI(iforce->expect_packet) == HI(cmd)) {
240
                iforce->expect_packet = 0;
241
                iforce->ecmd = cmd;
242
                memcpy(iforce->edata, data, IFORCE_MAX_LENGTH);
243
                if (waitqueue_active(&iforce->wait))
244
                        wake_up(&iforce->wait);
245
        }
246
#endif
247
 
248
        if (!iforce->type)
249
                return;
250
 
251
        switch (HI(cmd)) {
252
 
253
                case 0x01:      /* joystick position data */
254
                case 0x03:      /* wheel position data */
255
 
256
                        if (HI(cmd) == 1) {
257
                                input_report_abs(dev, ABS_X, (__s16) (((__s16)data[1] << 8) | data[0]));
258
                                input_report_abs(dev, ABS_Y, (__s16) (((__s16)data[3] << 8) | data[2]));
259
                                input_report_abs(dev, ABS_THROTTLE, 255 - data[4]);
260
                        } else {
261
                                input_report_abs(dev, ABS_WHEEL, (__s16) (((__s16)data[1] << 8) | data[0]));
262
                                input_report_abs(dev, ABS_GAS,   255 - data[2]);
263
                                input_report_abs(dev, ABS_BRAKE, 255 - data[3]);
264
                        }
265
 
266
                        input_report_abs(dev, ABS_HAT0X, iforce_hat_to_axis[data[6] >> 4].x);
267
                        input_report_abs(dev, ABS_HAT0Y, iforce_hat_to_axis[data[6] >> 4].y);
268
 
269
                        for (i = 0; iforce->type->btn[i] >= 0; i++)
270
                                input_report_key(dev, iforce->type->btn[i], data[(i >> 3) + 5] & (1 << (i & 7)));
271
 
272
                        break;
273
 
274
                case 0x02:      /* status report */
275
 
276
                        input_report_key(dev, BTN_DEAD, data[0] & 0x02);
277
                        break;
278
        }
279
}
280
 
281
static int get_id_packet(struct iforce *iforce, char *packet)
282
{
283
        DECLARE_WAITQUEUE(wait, current);
284
        int timeout = HZ; /* 1 second */
285
 
286
        switch (iforce->bus) {
287
 
288
#ifdef IFORCE_USB
289
                case IFORCE_USB:
290
 
291
                        iforce->dr.bRequest = packet[0];
292
                        iforce->ctrl.dev = iforce->usbdev;
293
 
294
                        set_current_state(TASK_INTERRUPTIBLE);
295
                        add_wait_queue(&iforce->wait, &wait);
296
 
297
                        if (usb_submit_urb(&iforce->ctrl)) {
298
                                set_current_state(TASK_RUNNING);
299
                                remove_wait_queue(&iforce->wait, &wait);
300
                                return -1;
301
                        }
302
 
303
                        while (timeout && iforce->ctrl.status == -EINPROGRESS)
304
                                timeout = schedule_timeout(timeout);
305
 
306
                        set_current_state(TASK_RUNNING);
307
                        remove_wait_queue(&iforce->wait, &wait);
308
 
309
                        if (!timeout) {
310
                                usb_unlink_urb(&iforce->ctrl);
311
                                return -1;
312
                        }
313
 
314
                        break;
315
#endif
316
#ifdef IFORCE_232
317
                case IFORCE_232:
318
 
319
                        iforce->expect_packet = FF_CMD_QUERY;
320
                        send_packet(iforce, FF_CMD_QUERY, packet);
321
 
322
                        set_current_state(TASK_INTERRUPTIBLE);
323
                        add_wait_queue(&iforce->wait, &wait);
324
 
325
                        while (timeout && iforce->expect_packet)
326
                                timeout = schedule_timeout(timeout);
327
 
328
                        set_current_state(TASK_RUNNING);
329
                        remove_wait_queue(&iforce->wait, &wait);
330
 
331
                        if (!timeout) {
332
                                iforce->expect_packet = 0;
333
                                return -1;
334
                        }
335
 
336
                        break;
337
#endif
338
        }
339
 
340
        return -(iforce->edata[0] != packet[0]);
341
}
342
 
343
static int iforce_open(struct input_dev *dev)
344
{
345
        struct iforce *iforce = dev->private;
346
 
347
        switch (iforce->bus) {
348
#ifdef IFORCE_USB
349
                case IFORCE_USB:
350
                        if (iforce->open++)
351
                                break;
352
                        iforce->irq.dev = iforce->usbdev;
353
                        if (usb_submit_urb(&iforce->irq))
354
                                        return -EIO;
355
                        break;
356
#endif
357
        }
358
        return 0;
359
}
360
 
361
static void iforce_close(struct input_dev *dev)
362
{
363
        struct iforce *iforce = dev->private;
364
 
365
        switch (iforce->bus) {
366
#ifdef IFORCE_USB
367
                case IFORCE_USB:
368
                        if (!--iforce->open)
369
                                usb_unlink_urb(&iforce->irq);
370
                        break;
371
#endif
372
        }
373
}
374
 
375
/*
376
 * Start or stop playing an effect
377
 */
378
 
379
static int iforce_input_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
380
{
381
        struct iforce* iforce = (struct iforce*)(dev->private);
382
        unsigned char data[3];
383
 
384
        printk(KERN_DEBUG "iforce.c: input_event(type = %d, code = %d, value = %d)\n", type, code, value);
385
 
386
        if (type != EV_FF)
387
                return -1;
388
 
389
        switch (code) {
390
 
391
                case FF_GAIN:
392
 
393
                        data[0] = value >> 9;
394
                        send_packet(iforce, FF_CMD_GAIN, data);
395
 
396
                        return 0;
397
 
398
                case FF_AUTOCENTER:
399
 
400
                        data[0] = 0x03;
401
                        data[1] = value >> 9;
402
                        send_packet(iforce, FF_CMD_AUTOCENTER, data);
403
 
404
                        data[0] = 0x04;
405
                        data[1] = 0x01;
406
                        send_packet(iforce, FF_CMD_AUTOCENTER, data);
407
 
408
                        return 0;
409
 
410
                default: /* Play an effect */
411
 
412
                        if (code >= iforce->dev.ff_effects_max)
413
                                return -1;
414
 
415
                        data[0] = LO(code);
416
                        data[1] = (value > 0) ? ((value > 1) ? 0x41 : 0x01) : 0;
417
                        data[2] = LO(value);
418
                        send_packet(iforce, FF_CMD_PLAY, data);
419
 
420
                        return 0;
421
        }
422
 
423
        return -1;
424
}
425
 
426
/*
427
 * Set the magnitude of a constant force effect
428
 * Return error code
429
 *
430
 * Note: caller must ensure exclusive access to device
431
 */
432
 
433
static int make_magnitude_modifier(struct iforce* iforce,
434
        struct resource* mod_chunk, __s16 level)
435
{
436
        unsigned char data[3];
437
 
438
        if (allocate_resource(&(iforce->device_memory), mod_chunk, 2,
439
                iforce->device_memory.start, iforce->device_memory.end, 2L,
440
                NULL, NULL)) {
441
                return -ENOMEM;
442
        }
443
 
444
        data[0] = LO(mod_chunk->start);
445
        data[1] = HI(mod_chunk->start);
446
        data[2] = HI(level);
447
 
448
        send_packet(iforce, FF_CMD_MAGNITUDE, data);
449
 
450
        return 0;
451
}
452
 
453
/*
454
 * Upload the component of an effect dealing with the period, phase and magnitude
455
 */
456
 
457
static int make_period_modifier(struct iforce* iforce, struct resource* mod_chunk,
458
        __s16 magnitude, __s16 offset, u16 period, u16 phase)
459
{
460
        unsigned char data[7];
461
 
462
        period = TIME_SCALE(period);
463
 
464
        if (allocate_resource(&(iforce->device_memory), mod_chunk, 0x0c,
465
                iforce->device_memory.start, iforce->device_memory.end, 2L,
466
                NULL, NULL)) {
467
                return -ENOMEM;
468
        }
469
 
470
        data[0] = LO(mod_chunk->start);
471
        data[1] = HI(mod_chunk->start);
472
 
473
        data[2] = HI(magnitude);
474
        data[3] = HI(offset);
475
        data[4] = HI(phase);
476
 
477
        data[5] = LO(period);
478
        data[6] = HI(period);
479
 
480
        send_packet(iforce, FF_CMD_PERIOD, data);
481
 
482
        return 0;
483
}
484
 
485
/*
486
 * Uploads the part of an effect setting the shape of the force
487
 */
488
 
489
static int make_shape_modifier(struct iforce* iforce, struct resource* mod_chunk,
490
        u16 attack_duration, __s16 initial_level,
491
        u16 fade_duration, __s16 final_level)
492
{
493
        unsigned char data[8];
494
 
495
        attack_duration = TIME_SCALE(attack_duration);
496
        fade_duration = TIME_SCALE(fade_duration);
497
 
498
        if (allocate_resource(&(iforce->device_memory), mod_chunk, 0x0e,
499
                iforce->device_memory.start, iforce->device_memory.end, 2L,
500
                NULL, NULL)) {
501
                return -ENOMEM;
502
        }
503
 
504
        data[0] = LO(mod_chunk->start);
505
        data[1] = HI(mod_chunk->start);
506
 
507
        data[2] = LO(attack_duration);
508
        data[3] = HI(attack_duration);
509
        data[4] = HI(initial_level);
510
 
511
        data[5] = LO(fade_duration);
512
        data[6] = HI(fade_duration);
513
        data[7] = HI(final_level);
514
 
515
        send_packet(iforce, FF_CMD_SHAPE, data);
516
 
517
        return 0;
518
}
519
 
520
/*
521
 * Component of spring, friction, inertia... effects
522
 */
523
 
524
static int make_interactive_modifier(struct iforce* iforce,
525
        struct resource* mod_chunk,
526
        __s16 rsat, __s16 lsat, __s16 rk, __s16 lk, u16 db, __s16 center)
527
{
528
        unsigned char data[10];
529
 
530
        if (allocate_resource(&(iforce->device_memory), mod_chunk, 8,
531
                iforce->device_memory.start, iforce->device_memory.end, 2L,
532
                NULL, NULL)) {
533
                return -ENOMEM;
534
        }
535
 
536
        data[0] = LO(mod_chunk->start);
537
        data[1] = HI(mod_chunk->start);
538
 
539
        data[2] = HI(rk);
540
        data[3] = HI(lk);
541
 
542
        data[4] = LO(center);
543
        data[5] = HI(center);
544
 
545
        data[6] = LO(db);
546
        data[7] = HI(db);
547
 
548
        data[8] = HI(rsat);
549
        data[9] = HI(lsat);
550
 
551
        send_packet(iforce, FF_CMD_INTERACT, data);
552
 
553
        return 0;
554
}
555
 
556
static unsigned char find_button(struct iforce *iforce, signed short button)
557
{
558
        int i;
559
        for (i = 1; iforce->type->btn[i] >= 0; i++)
560
                if (iforce->type->btn[i] == button)
561
                        return i + 1;
562
        return 0;
563
}
564
 
565
/*
566
 * Send the part common to all effects to the device
567
 */
568
 
569
static int make_core(struct iforce* iforce, u16 id, u16 mod_id1, u16 mod_id2,
570
        u8 effect_type, u8 axes, u16 duration, u16 delay, u16 button,
571
        u16 interval, u16 direction)
572
{
573
        unsigned char data[14];
574
 
575
        duration = TIME_SCALE(duration);
576
        delay    = TIME_SCALE(delay);
577
        interval = TIME_SCALE(interval);
578
 
579
        data[0]  = LO(id);
580
        data[1]  = effect_type;
581
        data[2]  = LO(axes) | find_button(iforce, button);
582
 
583
        data[3]  = LO(duration);
584
        data[4]  = HI(duration);
585
 
586
        data[5]  = HI(direction);
587
 
588
        data[6]  = LO(interval);
589
        data[7]  = HI(interval);
590
 
591
        data[8]  = LO(mod_id1);
592
        data[9]  = HI(mod_id1);
593
        data[10] = LO(mod_id2);
594
        data[11] = HI(mod_id2);
595
 
596
        data[12] = LO(delay);
597
        data[13] = HI(delay);
598
 
599
        send_packet(iforce, FF_CMD_EFFECT, data);
600
 
601
        return 0;
602
}
603
 
604
/*
605
 * Upload a periodic effect to the device
606
 */
607
 
608
static int iforce_upload_periodic(struct iforce* iforce, struct ff_effect* effect)
609
{
610
        u8 wave_code;
611
        int core_id = effect->id;
612
        struct iforce_core_effect* core_effect = iforce->core_effects + core_id;
613
        struct resource* mod1_chunk = &(iforce->core_effects[core_id].mod1_chunk);
614
        struct resource* mod2_chunk = &(iforce->core_effects[core_id].mod2_chunk);
615
        int err = 0;
616
 
617
        err = make_period_modifier(iforce, mod1_chunk,
618
                effect->u.periodic.magnitude, effect->u.periodic.offset,
619
                effect->u.periodic.period, effect->u.periodic.phase);
620
        if (err) return err;
621
        set_bit(FF_MOD1_IS_USED, core_effect->flags);
622
 
623
        err = make_shape_modifier(iforce, mod2_chunk,
624
                effect->u.periodic.shape.attack_length,
625
                effect->u.periodic.shape.attack_level,
626
                effect->u.periodic.shape.fade_length,
627
                effect->u.periodic.shape.fade_level);
628
        if (err) return err;
629
        set_bit(FF_MOD2_IS_USED, core_effect->flags);
630
 
631
        switch (effect->u.periodic.waveform) {
632
                case FF_SQUARE:         wave_code = 0x20; break;
633
                case FF_TRIANGLE:       wave_code = 0x21; break;
634
                case FF_SINE:           wave_code = 0x22; break;
635
                case FF_SAW_UP:         wave_code = 0x23; break;
636
                case FF_SAW_DOWN:       wave_code = 0x24; break;
637
                default:                wave_code = 0x20; break;
638
        }
639
 
640
        err = make_core(iforce, effect->id,
641
                mod1_chunk->start,
642
                mod2_chunk->start,
643
                wave_code,
644
                0x20,
645
                effect->replay.length,
646
                effect->replay.delay,
647
                effect->trigger.button,
648
                effect->trigger.interval,
649
                effect->u.periodic.direction);
650
 
651
        return err;
652
}
653
 
654
/*
655
 * Upload a constant force effect
656
 */
657
static int iforce_upload_constant(struct iforce* iforce, struct ff_effect* effect)
658
{
659
        int core_id = effect->id;
660
        struct iforce_core_effect* core_effect = iforce->core_effects + core_id;
661
        struct resource* mod1_chunk = &(iforce->core_effects[core_id].mod1_chunk);
662
        struct resource* mod2_chunk = &(iforce->core_effects[core_id].mod2_chunk);
663
        int err = 0;
664
 
665
        printk(KERN_DEBUG "iforce.c: make constant effect\n");
666
 
667
        err = make_magnitude_modifier(iforce, mod1_chunk, effect->u.constant.level);
668
        if (err) return err;
669
        set_bit(FF_MOD1_IS_USED, core_effect->flags);
670
 
671
        err = make_shape_modifier(iforce, mod2_chunk,
672
                effect->u.constant.shape.attack_length,
673
                effect->u.constant.shape.attack_level,
674
                effect->u.constant.shape.fade_length,
675
                effect->u.constant.shape.fade_level);
676
        if (err) return err;
677
        set_bit(FF_MOD2_IS_USED, core_effect->flags);
678
 
679
        err = make_core(iforce, effect->id,
680
                mod1_chunk->start,
681
                mod2_chunk->start,
682
                0x00,
683
                0x20,
684
                effect->replay.length,
685
                effect->replay.delay,
686
                effect->trigger.button,
687
                effect->trigger.interval,
688
                effect->u.constant.direction);
689
 
690
        return err;
691
}
692
 
693
/*
694
 * Upload an interactive effect. Those are for example friction, inertia, springs...
695
 */
696
static int iforce_upload_interactive(struct iforce* iforce, struct ff_effect* effect)
697
{
698
        int core_id = effect->id;
699
        struct iforce_core_effect* core_effect = iforce->core_effects + core_id;
700
        struct resource* mod_chunk = &(core_effect->mod1_chunk);
701
        u8 type, axes;
702
        u16 mod1, mod2, direction;
703
        int err = 0;
704
 
705
        printk(KERN_DEBUG "iforce.c: make interactive effect\n");
706
 
707
        switch (effect->type) {
708
                case FF_SPRING:         type = 0x40; break;
709
                case FF_FRICTION:       type = 0x41; break;
710
                default: return -1;
711
        }
712
 
713
        err = make_interactive_modifier(iforce, mod_chunk,
714
                effect->u.interactive.right_saturation,
715
                effect->u.interactive.left_saturation,
716
                effect->u.interactive.right_coeff,
717
                effect->u.interactive.left_coeff,
718
                effect->u.interactive.deadband,
719
                effect->u.interactive.center);
720
        if (err) return err;
721
        set_bit(FF_MOD1_IS_USED, core_effect->flags);
722
 
723
        switch ((test_bit(ABS_X, &effect->u.interactive.axis) ||
724
                test_bit(ABS_WHEEL, &effect->u.interactive.axis)) |
725
                (!!test_bit(ABS_Y, &effect->u.interactive.axis) << 1)) {
726
 
727
                case 0: /* Only one axis, choose orientation */
728
                        mod1 = mod_chunk->start;
729
                        mod2 = 0xffff;
730
                        direction = effect->u.interactive.direction;
731
                        axes = 0x20;
732
                        break;
733
 
734
                case 1: /* Only X axis */
735
                        mod1 = mod_chunk->start;
736
                        mod2 = 0xffff;
737
                        direction = 0x5a00;
738
                        axes = 0x40;
739
                        break;
740
 
741
                case 2: /* Only Y axis */
742
                        mod1 = 0xffff;
743
                        mod2 = mod_chunk->start;
744
                        direction = 0xb400;
745
                        axes = 0x80;
746
                        break;
747
 
748
                case 3: /* Both X and Y axes */
749
                        /* TODO: same setting for both axes is not mandatory */
750
                        mod1 = mod_chunk->start;
751
                        mod2 = mod_chunk->start;
752
                        direction = 0x6000;
753
                        axes = 0xc0;
754
                        break;
755
 
756
                default:
757
                        return -1;
758
        }
759
 
760
        err = make_core(iforce, effect->id,
761
                mod1, mod2,
762
                type, axes,
763
                effect->replay.length, effect->replay.delay,
764
                effect->trigger.button, effect->trigger.interval,
765
                direction);
766
 
767
        return err;
768
}
769
 
770
/*
771
 * Function called when an ioctl is performed on the event dev entry.
772
 * It uploads an effect to the device
773
 */
774
static int iforce_upload_effect(struct input_dev *dev, struct ff_effect *effect)
775
{
776
        struct iforce* iforce = (struct iforce*)(dev->private);
777
        int id;
778
 
779
        printk(KERN_DEBUG "iforce.c: upload effect\n");
780
 
781
/*
782
 * Get a free id
783
 */
784
 
785
        for (id=0; id < FF_EFFECTS_MAX; ++id)
786
                if (!test_bit(FF_CORE_IS_USED, iforce->core_effects[id].flags)) break;
787
 
788
        if ( id == FF_EFFECTS_MAX || id >= iforce->dev.ff_effects_max)
789
                return -ENOMEM;
790
 
791
        effect->id = id;
792
        set_bit(FF_CORE_IS_USED, iforce->core_effects[id].flags);
793
 
794
/*
795
 * Upload the effect
796
 */
797
 
798
        switch (effect->type) {
799
 
800
                case FF_PERIODIC:
801
                        return iforce_upload_periodic(iforce, effect);
802
 
803
                case FF_CONSTANT:
804
                        return iforce_upload_constant(iforce, effect);
805
 
806
                case FF_SPRING:
807
                case FF_FRICTION:
808
                        return iforce_upload_interactive(iforce, effect);
809
 
810
                default:
811
                        return -1;
812
        }
813
}
814
 
815
/*
816
 * Erases an effect: it frees the effect id and mark as unused the memory
817
 * allocated for the parameters
818
 */
819
static int iforce_erase_effect(struct input_dev *dev, int effect_id)
820
{
821
        struct iforce* iforce = (struct iforce*)(dev->private);
822
        int err = 0;
823
        struct iforce_core_effect* core_effect;
824
 
825
        printk(KERN_DEBUG "iforce.c: erase effect %d\n", effect_id);
826
 
827
        if (effect_id < 0 || effect_id >= FF_EFFECTS_MAX)
828
                return -EINVAL;
829
 
830
        core_effect = iforce->core_effects + effect_id;
831
 
832
        if (test_bit(FF_MOD1_IS_USED, core_effect->flags))
833
                err = release_resource(&(iforce->core_effects[effect_id].mod1_chunk));
834
 
835
        if (!err && test_bit(FF_MOD2_IS_USED, core_effect->flags))
836
                err = release_resource(&(iforce->core_effects[effect_id].mod2_chunk));
837
 
838
        /*TODO: remember to change that if more FF_MOD* bits are added */
839
        core_effect->flags[0] = 0;
840
 
841
        return err;
842
}
843
static int iforce_init_device(struct iforce *iforce)
844
{
845
        unsigned char c[] = "CEOV";
846
        int i;
847
 
848
        init_waitqueue_head(&iforce->wait);
849
        iforce->dev.ff_effects_max = 10;
850
 
851
/*
852
 * Input device fields.
853
 */
854
 
855
        iforce->dev.idbus = BUS_USB;
856
        iforce->dev.private = iforce;
857
        iforce->dev.name = iforce->name;
858
        iforce->dev.open = iforce_open;
859
        iforce->dev.close = iforce_close;
860
        iforce->dev.event = iforce_input_event;
861
        iforce->dev.upload_effect = iforce_upload_effect;
862
        iforce->dev.erase_effect = iforce_erase_effect;
863
 
864
/*
865
 * On-device memory allocation.
866
 */
867
 
868
        iforce->device_memory.name = "I-Force device effect memory";
869
        iforce->device_memory.start = 0;
870
        iforce->device_memory.end = 200;
871
        iforce->device_memory.flags = IORESOURCE_MEM;
872
        iforce->device_memory.parent = NULL;
873
        iforce->device_memory.child = NULL;
874
        iforce->device_memory.sibling = NULL;
875
 
876
/*
877
 * Wait until device ready - until it sends its first response.
878
 */
879
 
880
        for (i = 0; i < 20; i++)
881
                if (!get_id_packet(iforce, "O"))
882
                        break;
883
 
884
        if (i == 20) { /* 5 seconds */
885
                printk(KERN_ERR "iforce.c: Timeout waiting for response from device.\n");
886
                iforce_close(&iforce->dev);
887
                return -1;
888
        }
889
 
890
/*
891
 * Get device info.
892
 */
893
 
894
        if (!get_id_packet(iforce, "M"))
895
                iforce->dev.idvendor = (iforce->edata[2] << 8) | iforce->edata[1];
896
        if (!get_id_packet(iforce, "P"))
897
                iforce->dev.idproduct = (iforce->edata[2] << 8) | iforce->edata[1];
898
        if (!get_id_packet(iforce, "B"))
899
                iforce->device_memory.end = (iforce->edata[2] << 8) | iforce->edata[1];
900
        if (!get_id_packet(iforce, "N"))
901
                iforce->dev.ff_effects_max = iforce->edata[1];
902
 
903
/*
904
 * Display additional info.
905
 */
906
 
907
        for (i = 0; c[i]; i++)
908
                if (!get_id_packet(iforce, c + i))
909
                        dump_packet("info", iforce->ecmd, iforce->edata);
910
 
911
/*
912
 * Disable spring, enable force feedback.
913
 * FIXME: We should use iforce_set_autocenter() et al here.
914
 */
915
 
916
        send_packet(iforce, FF_CMD_AUTOCENTER, "\004\000");
917
        send_packet(iforce, FF_CMD_ENABLE, "\004");
918
 
919
/*
920
 * Find appropriate device entry
921
 */
922
 
923
        for (i = 0; iforce_device[i].idvendor; i++)
924
                if (iforce_device[i].idvendor == iforce->dev.idvendor &&
925
                    iforce_device[i].idproduct == iforce->dev.idproduct)
926
                        break;
927
 
928
        iforce->type = iforce_device + i;
929
 
930
        sprintf(iforce->name, iforce->type->name,
931
                iforce->dev.idproduct, iforce->dev.idvendor);
932
 
933
/*
934
 * Set input device bitfields and ranges.
935
 */
936
 
937
        iforce->dev.evbit[0] = BIT(EV_KEY) | BIT(EV_ABS) | BIT(EV_FF);
938
 
939
        for (i = 0; iforce->type->btn[i] >= 0; i++) {
940
                signed short t = iforce->type->btn[i];
941
                set_bit(t, iforce->dev.keybit);
942
                if (t != BTN_DEAD)
943
                        set_bit(FF_BTN(t), iforce->dev.ffbit);
944
        }
945
 
946
        for (i = 0; iforce->type->abs[i] >= 0; i++) {
947
 
948
                signed short t = iforce->type->abs[i];
949
                set_bit(t, iforce->dev.absbit);
950
 
951
                switch (t) {
952
 
953
                        case ABS_X:
954
                        case ABS_Y:
955
                        case ABS_WHEEL:
956
 
957
                                iforce->dev.absmax[t] =  1920;
958
                                iforce->dev.absmin[t] = -1920;
959
                                iforce->dev.absflat[t] = 128;
960
                                iforce->dev.absfuzz[t] = 16;
961
 
962
                                set_bit(FF_ABS(t), iforce->dev.ffbit);
963
                                break;
964
 
965
                        case ABS_THROTTLE:
966
                        case ABS_GAS:
967
                        case ABS_BRAKE:
968
 
969
                                iforce->dev.absmax[t] = 255;
970
                                iforce->dev.absmin[t] = 0;
971
                                break;
972
 
973
                        case ABS_HAT0X:
974
                        case ABS_HAT0Y:
975
                                iforce->dev.absmax[t] =  1;
976
                                iforce->dev.absmin[t] = -1;
977
                                break;
978
                }
979
        }
980
 
981
        for (i = 0; iforce->type->ff[i] >= 0; i++)
982
                set_bit(iforce->type->ff[i], iforce->dev.ffbit);
983
 
984
/*
985
 * Register input device.
986
 */
987
 
988
        input_register_device(&iforce->dev);
989
 
990
        return 0;
991
}
992
 
993
#ifdef IFORCE_USB
994
 
995
static void iforce_usb_irq(struct urb *urb)
996
{
997
        struct iforce *iforce = urb->context;
998
        if (urb->status) return;
999
        iforce_process_packet(iforce,
1000
                (iforce->data[0] << 8) | (urb->actual_length - 1), iforce->data + 1);
1001
}
1002
 
1003
static void iforce_usb_out(struct urb *urb)
1004
{
1005
        struct iforce *iforce = urb->context;
1006
        if (urb->status) return;
1007
        if (waitqueue_active(&iforce->wait))
1008
                wake_up(&iforce->wait);
1009
}
1010
 
1011
static void iforce_usb_ctrl(struct urb *urb)
1012
{
1013
        struct iforce *iforce = urb->context;
1014
        if (urb->status) return;
1015
        iforce->ecmd = 0xff00 | urb->actual_length;
1016
        if (waitqueue_active(&iforce->wait))
1017
                wake_up(&iforce->wait);
1018
}
1019
 
1020
static void *iforce_usb_probe(struct usb_device *dev, unsigned int ifnum,
1021
                                const struct usb_device_id *id)
1022
{
1023
        struct usb_endpoint_descriptor *epirq, *epout;
1024
        struct iforce *iforce;
1025
 
1026
        epirq = dev->config[0].interface[ifnum].altsetting[0].endpoint + 0;
1027
        epout = dev->config[0].interface[ifnum].altsetting[0].endpoint + 1;
1028
 
1029
        if (!(iforce = kmalloc(sizeof(struct iforce) + 32, GFP_KERNEL))) return NULL;
1030
        memset(iforce, 0, sizeof(struct iforce));
1031
 
1032
        iforce->bus = IFORCE_USB;
1033
        iforce->usbdev = dev;
1034
 
1035
        iforce->dr.bRequestType = USB_TYPE_VENDOR | USB_DIR_IN | USB_RECIP_INTERFACE;
1036
        iforce->dr.wIndex = 0;
1037
        iforce->dr.wLength = 16;
1038
 
1039
        FILL_INT_URB(&iforce->irq, dev, usb_rcvintpipe(dev, epirq->bEndpointAddress),
1040
                        iforce->data, 16, iforce_usb_irq, iforce, epirq->bInterval);
1041
 
1042
        FILL_BULK_URB(&iforce->out, dev, usb_sndbulkpipe(dev, epout->bEndpointAddress),
1043
                        iforce + 1, 32, iforce_usb_out, iforce);
1044
 
1045
        FILL_CONTROL_URB(&iforce->ctrl, dev, usb_rcvctrlpipe(dev, 0),
1046
                        (void*) &iforce->dr, iforce->edata, 16, iforce_usb_ctrl, iforce);
1047
 
1048
        if (iforce_init_device(iforce)) {
1049
                kfree(iforce);
1050
                return NULL;
1051
        }
1052
 
1053
        printk(KERN_INFO "input%d: %s [%d effects, %ld bytes memory] on usb%d:%d.%d\n",
1054
                iforce->dev.number, iforce->dev.name, iforce->dev.ff_effects_max,
1055
                iforce->device_memory.end, dev->bus->busnum, dev->devnum, ifnum);
1056
 
1057
        return iforce;
1058
}
1059
 
1060
static void iforce_usb_disconnect(struct usb_device *dev, void *ptr)
1061
{
1062
        struct iforce *iforce = ptr;
1063
        usb_unlink_urb(&iforce->irq);
1064
        input_unregister_device(&iforce->dev);
1065
        kfree(iforce);
1066
}
1067
 
1068
static struct usb_device_id iforce_usb_ids [] = {
1069
        { USB_DEVICE(0x044f, 0xa01c) },         /* Thrustmaster Motor Sport GT */
1070
        { USB_DEVICE(0x046d, 0xc281) },         /* Logitech WingMan Force */
1071
        { USB_DEVICE(0x046d, 0xc291) },         /* Logitech WingMan Formula Force */
1072
        { USB_DEVICE(0x05ef, 0x020a) },         /* AVB Top Shot Pegasus */
1073
        { USB_DEVICE(0x05ef, 0x8884) },         /* AVB Mag Turbo Force */
1074
        { USB_DEVICE(0x05ef, 0x8888) },         /* AVB Top Shot FFB Racing Wheel */
1075
        { USB_DEVICE(0x061c, 0xc084) },         /* ACT LABS Force RS */
1076
        { USB_DEVICE(0x06f8, 0x0001) },         /* Guillemot Race Leader Force Feedback */
1077
        { USB_DEVICE(0x06f8, 0x0004) },         /* Guillemot Force Feedback Racing Wheel */
1078
        { }                                     /* Terminating entry */
1079
};
1080
 
1081
MODULE_DEVICE_TABLE (usb, iforce_usb_ids);
1082
 
1083
static struct usb_driver iforce_usb_driver = {
1084
        name:           "iforce",
1085
        probe:          iforce_usb_probe,
1086
        disconnect:     iforce_usb_disconnect,
1087
        id_table:       iforce_usb_ids,
1088
};
1089
 
1090
#endif
1091
 
1092
#ifdef IFORCE_232
1093
 
1094
static void iforce_serio_irq(struct serio *serio, unsigned char data, unsigned int flags)
1095
{
1096
        struct iforce* iforce = serio->private;
1097
 
1098
        if (!iforce->pkt) {
1099
                if (data != 0x2b) {
1100
                        return;
1101
                }
1102
                iforce->pkt = 1;
1103
                return;
1104
        }
1105
 
1106
        if (!iforce->id) {
1107
                if (data > 3 && data != 0xff) {
1108
                        iforce->pkt = 0;
1109
                        return;
1110
                }
1111
                iforce->id = data;
1112
                return;
1113
        }
1114
 
1115
        if (!iforce->len) {
1116
                if (data > IFORCE_MAX_LENGTH) {
1117
                        iforce->pkt = 0;
1118
                        iforce->id = 0;
1119
                        return;
1120
                }
1121
                iforce->len = data;
1122
                return;
1123
        }
1124
 
1125
        if (iforce->idx < iforce->len) {
1126
                iforce->csum += iforce->data[iforce->idx++] = data;
1127
                return;
1128
        }
1129
 
1130
        if (iforce->idx == iforce->len) {
1131
                iforce_process_packet(iforce, (iforce->id << 8) | iforce->idx, iforce->data);
1132
                iforce->pkt = 0;
1133
                iforce->id  = 0;
1134
                iforce->len = 0;
1135
                iforce->idx = 0;
1136
                iforce->csum = 0;
1137
        }
1138
}
1139
 
1140
static void iforce_serio_connect(struct serio *serio, struct serio_dev *dev)
1141
{
1142
        struct iforce *iforce;
1143
        if (serio->type != (SERIO_RS232 | SERIO_IFORCE))
1144
                return;
1145
 
1146
        if (!(iforce = kmalloc(sizeof(struct iforce), GFP_KERNEL))) return;
1147
        memset(iforce, 0, sizeof(struct iforce));
1148
 
1149
        iforce->bus = IFORCE_232;
1150
        iforce->serio = serio;
1151
        serio->private = iforce;
1152
 
1153
        if (serio_open(serio, dev)) {
1154
                kfree(iforce);
1155
                return;
1156
        }
1157
 
1158
        if (iforce_init_device(iforce)) {
1159
                serio_close(serio);
1160
                kfree(iforce);
1161
                return;
1162
        }
1163
 
1164
        printk(KERN_INFO "input%d: %s [%d effects, %ld bytes memory] on serio%d\n",
1165
                iforce->dev.number, iforce->dev.name, iforce->dev.ff_effects_max,
1166
                iforce->device_memory.end, serio->number);
1167
}
1168
 
1169
static void iforce_serio_disconnect(struct serio *serio)
1170
{
1171
        struct iforce* iforce = serio->private;
1172
 
1173
        input_unregister_device(&iforce->dev);
1174
        serio_close(serio);
1175
        kfree(iforce);
1176
}
1177
 
1178
static struct serio_dev iforce_serio_dev = {
1179
        interrupt:      iforce_serio_irq,
1180
        connect:        iforce_serio_connect,
1181
        disconnect:     iforce_serio_disconnect,
1182
};
1183
 
1184
#endif
1185
 
1186
static int __init iforce_init(void)
1187
{
1188
#ifdef IFORCE_USB
1189
        usb_register(&iforce_usb_driver);
1190
#endif
1191
#ifdef IFORCE_232
1192
        serio_register_device(&iforce_serio_dev);
1193
#endif
1194
        return 0;
1195
}
1196
 
1197
static void __exit iforce_exit(void)
1198
{
1199
#ifdef IFORCE_USB
1200
        usb_deregister(&iforce_usb_driver);
1201
#endif
1202
#ifdef IFORCE_232
1203
        serio_unregister_device(&iforce_serio_dev);
1204
#endif
1205
}
1206
 
1207
module_init(iforce_init);
1208
module_exit(iforce_exit);

powered by: WebSVN 2.1.0

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