Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  *  Copyright (c) 2000-2002 Vojtech Pavlik <vojtech@ucw.cz>
0004  *  Copyright (c) 2001-2002, 2007 Johann Deneux <johann.deneux@gmail.com>
0005  *
0006  *  USB/RS232 I-Force joysticks and wheels.
0007  */
0008 
0009 #include <asm/unaligned.h>
0010 #include "iforce.h"
0011 
0012 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>, Johann Deneux <johann.deneux@gmail.com>");
0013 MODULE_DESCRIPTION("Core I-Force joysticks and wheels driver");
0014 MODULE_LICENSE("GPL");
0015 
0016 static signed short btn_joystick[] =
0017 { BTN_TRIGGER, BTN_TOP, BTN_THUMB, BTN_TOP2, BTN_BASE,
0018   BTN_BASE2, BTN_BASE3, BTN_BASE4, BTN_BASE5, BTN_A,
0019   BTN_B, BTN_C, BTN_DEAD, -1 };
0020 
0021 static signed short btn_joystick_avb[] =
0022 { BTN_TRIGGER, BTN_THUMB, BTN_TOP, BTN_TOP2, BTN_BASE,
0023   BTN_BASE2, BTN_BASE3, BTN_BASE4, BTN_DEAD, -1 };
0024 
0025 static signed short btn_wheel[] =
0026 { BTN_GEAR_DOWN, BTN_GEAR_UP, BTN_BASE, BTN_BASE2, BTN_BASE3,
0027   BTN_BASE4, BTN_BASE5, BTN_BASE6, -1 };
0028 
0029 static signed short abs_joystick[] =
0030 { ABS_X, ABS_Y, ABS_THROTTLE, ABS_HAT0X, ABS_HAT0Y, -1 };
0031 
0032 static signed short abs_joystick_rudder[] =
0033 { ABS_X, ABS_Y, ABS_THROTTLE, ABS_RUDDER, ABS_HAT0X, ABS_HAT0Y, -1 };
0034 
0035 static signed short abs_avb_pegasus[] =
0036 { ABS_X, ABS_Y, ABS_THROTTLE, ABS_RUDDER, ABS_HAT0X, ABS_HAT0Y,
0037   ABS_HAT1X, ABS_HAT1Y, -1 };
0038 
0039 static signed short abs_wheel[] =
0040 { ABS_WHEEL, ABS_GAS, ABS_BRAKE, ABS_HAT0X, ABS_HAT0Y, -1 };
0041 
0042 static signed short ff_iforce[] =
0043 { FF_PERIODIC, FF_CONSTANT, FF_SPRING, FF_DAMPER,
0044   FF_SQUARE, FF_TRIANGLE, FF_SINE, FF_SAW_UP, FF_SAW_DOWN, FF_GAIN,
0045   FF_AUTOCENTER, -1 };
0046 
0047 static struct iforce_device iforce_device[] = {
0048     { 0x044f, 0xa01c, "Thrustmaster Motor Sport GT",        btn_wheel, abs_wheel, ff_iforce },
0049     { 0x046d, 0xc281, "Logitech WingMan Force",         btn_joystick, abs_joystick, ff_iforce },
0050     { 0x046d, 0xc291, "Logitech WingMan Formula Force",     btn_wheel, abs_wheel, ff_iforce },
0051     { 0x05ef, 0x020a, "AVB Top Shot Pegasus",           btn_joystick_avb, abs_avb_pegasus, ff_iforce },
0052     { 0x05ef, 0x8884, "AVB Mag Turbo Force",            btn_wheel, abs_wheel, ff_iforce },
0053     { 0x05ef, 0x8886, "Boeder Force Feedback Wheel",        btn_wheel, abs_wheel, ff_iforce },
0054     { 0x05ef, 0x8888, "AVB Top Shot Force Feedback Racing Wheel",   btn_wheel, abs_wheel, ff_iforce }, //?
0055     { 0x061c, 0xc0a4, "ACT LABS Force RS",                          btn_wheel, abs_wheel, ff_iforce }, //?
0056     { 0x061c, 0xc084, "ACT LABS Force RS",              btn_wheel, abs_wheel, ff_iforce },
0057     { 0x06a3, 0xff04, "Saitek R440 Force Wheel",            btn_wheel, abs_wheel, ff_iforce }, //?
0058     { 0x06f8, 0x0001, "Guillemot Race Leader Force Feedback",   btn_wheel, abs_wheel, ff_iforce }, //?
0059     { 0x06f8, 0x0001, "Guillemot Jet Leader Force Feedback",    btn_joystick, abs_joystick_rudder, ff_iforce },
0060     { 0x06f8, 0x0004, "Guillemot Force Feedback Racing Wheel",  btn_wheel, abs_wheel, ff_iforce }, //?
0061     { 0x06f8, 0xa302, "Guillemot Jet Leader 3D",            btn_joystick, abs_joystick, ff_iforce }, //?
0062     { 0x06d6, 0x29bc, "Trust Force Feedback Race Master",       btn_wheel, abs_wheel, ff_iforce },
0063     { 0x0000, 0x0000, "Unknown I-Force Device [%04x:%04x]",     btn_joystick, abs_joystick, ff_iforce }
0064 };
0065 
0066 static int iforce_playback(struct input_dev *dev, int effect_id, int value)
0067 {
0068     struct iforce *iforce = input_get_drvdata(dev);
0069     struct iforce_core_effect *core_effect = &iforce->core_effects[effect_id];
0070 
0071     if (value > 0)
0072         set_bit(FF_CORE_SHOULD_PLAY, core_effect->flags);
0073     else
0074         clear_bit(FF_CORE_SHOULD_PLAY, core_effect->flags);
0075 
0076     iforce_control_playback(iforce, effect_id, value);
0077     return 0;
0078 }
0079 
0080 static void iforce_set_gain(struct input_dev *dev, u16 gain)
0081 {
0082     struct iforce *iforce = input_get_drvdata(dev);
0083     unsigned char data[3];
0084 
0085     data[0] = gain >> 9;
0086     iforce_send_packet(iforce, FF_CMD_GAIN, data);
0087 }
0088 
0089 static void iforce_set_autocenter(struct input_dev *dev, u16 magnitude)
0090 {
0091     struct iforce *iforce = input_get_drvdata(dev);
0092     unsigned char data[3];
0093 
0094     data[0] = 0x03;
0095     data[1] = magnitude >> 9;
0096     iforce_send_packet(iforce, FF_CMD_AUTOCENTER, data);
0097 
0098     data[0] = 0x04;
0099     data[1] = 0x01;
0100     iforce_send_packet(iforce, FF_CMD_AUTOCENTER, data);
0101 }
0102 
0103 /*
0104  * Function called when an ioctl is performed on the event dev entry.
0105  * It uploads an effect to the device
0106  */
0107 static int iforce_upload_effect(struct input_dev *dev, struct ff_effect *effect, struct ff_effect *old)
0108 {
0109     struct iforce *iforce = input_get_drvdata(dev);
0110     struct iforce_core_effect *core_effect = &iforce->core_effects[effect->id];
0111     int ret;
0112 
0113     if (__test_and_set_bit(FF_CORE_IS_USED, core_effect->flags)) {
0114         /* Check the effect is not already being updated */
0115         if (test_bit(FF_CORE_UPDATE, core_effect->flags))
0116             return -EAGAIN;
0117     }
0118 
0119 /*
0120  * Upload the effect
0121  */
0122     switch (effect->type) {
0123     case FF_PERIODIC:
0124         ret = iforce_upload_periodic(iforce, effect, old);
0125         break;
0126 
0127     case FF_CONSTANT:
0128         ret = iforce_upload_constant(iforce, effect, old);
0129         break;
0130 
0131     case FF_SPRING:
0132     case FF_DAMPER:
0133         ret = iforce_upload_condition(iforce, effect, old);
0134         break;
0135 
0136     default:
0137         return -EINVAL;
0138     }
0139 
0140     if (ret == 0) {
0141         /* A packet was sent, forbid new updates until we are notified
0142          * that the packet was updated
0143          */
0144         set_bit(FF_CORE_UPDATE, core_effect->flags);
0145     }
0146     return ret;
0147 }
0148 
0149 /*
0150  * Erases an effect: it frees the effect id and mark as unused the memory
0151  * allocated for the parameters
0152  */
0153 static int iforce_erase_effect(struct input_dev *dev, int effect_id)
0154 {
0155     struct iforce *iforce = input_get_drvdata(dev);
0156     struct iforce_core_effect *core_effect = &iforce->core_effects[effect_id];
0157     int err = 0;
0158 
0159     if (test_bit(FF_MOD1_IS_USED, core_effect->flags))
0160         err = release_resource(&core_effect->mod1_chunk);
0161 
0162     if (!err && test_bit(FF_MOD2_IS_USED, core_effect->flags))
0163         err = release_resource(&core_effect->mod2_chunk);
0164 
0165     /* TODO: remember to change that if more FF_MOD* bits are added */
0166     core_effect->flags[0] = 0;
0167 
0168     return err;
0169 }
0170 
0171 static int iforce_open(struct input_dev *dev)
0172 {
0173     struct iforce *iforce = input_get_drvdata(dev);
0174 
0175     iforce->xport_ops->start_io(iforce);
0176 
0177     if (test_bit(EV_FF, dev->evbit)) {
0178         /* Enable force feedback */
0179         iforce_send_packet(iforce, FF_CMD_ENABLE, "\004");
0180     }
0181 
0182     return 0;
0183 }
0184 
0185 static void iforce_close(struct input_dev *dev)
0186 {
0187     struct iforce *iforce = input_get_drvdata(dev);
0188     int i;
0189 
0190     if (test_bit(EV_FF, dev->evbit)) {
0191         /* Check: no effects should be present in memory */
0192         for (i = 0; i < dev->ff->max_effects; i++) {
0193             if (test_bit(FF_CORE_IS_USED, iforce->core_effects[i].flags)) {
0194                 dev_warn(&dev->dev,
0195                     "%s: Device still owns effects\n",
0196                     __func__);
0197                 break;
0198             }
0199         }
0200 
0201         /* Disable force feedback playback */
0202         iforce_send_packet(iforce, FF_CMD_ENABLE, "\001");
0203         /* Wait for the command to complete */
0204         wait_event_interruptible(iforce->wait,
0205             !test_bit(IFORCE_XMIT_RUNNING, iforce->xmit_flags));
0206     }
0207 
0208     iforce->xport_ops->stop_io(iforce);
0209 }
0210 
0211 int iforce_init_device(struct device *parent, u16 bustype,
0212                struct iforce *iforce)
0213 {
0214     struct input_dev *input_dev;
0215     struct ff_device *ff;
0216     u8 c[] = "CEOV";
0217     u8 buf[IFORCE_MAX_LENGTH];
0218     size_t len;
0219     int i, error;
0220     int ff_effects = 0;
0221 
0222     input_dev = input_allocate_device();
0223     if (!input_dev)
0224         return -ENOMEM;
0225 
0226     init_waitqueue_head(&iforce->wait);
0227     spin_lock_init(&iforce->xmit_lock);
0228     mutex_init(&iforce->mem_mutex);
0229     iforce->xmit.buf = iforce->xmit_data;
0230     iforce->dev = input_dev;
0231 
0232 /*
0233  * Input device fields.
0234  */
0235 
0236     input_dev->id.bustype = bustype;
0237     input_dev->dev.parent = parent;
0238 
0239     input_set_drvdata(input_dev, iforce);
0240 
0241     input_dev->name = "Unknown I-Force device";
0242     input_dev->open = iforce_open;
0243     input_dev->close = iforce_close;
0244 
0245 /*
0246  * On-device memory allocation.
0247  */
0248 
0249     iforce->device_memory.name = "I-Force device effect memory";
0250     iforce->device_memory.start = 0;
0251     iforce->device_memory.end = 200;
0252     iforce->device_memory.flags = IORESOURCE_MEM;
0253     iforce->device_memory.parent = NULL;
0254     iforce->device_memory.child = NULL;
0255     iforce->device_memory.sibling = NULL;
0256 
0257 /*
0258  * Wait until device ready - until it sends its first response.
0259  */
0260 
0261     for (i = 0; i < 20; i++)
0262         if (!iforce_get_id_packet(iforce, 'O', buf, &len))
0263             break;
0264 
0265     if (i == 20) { /* 5 seconds */
0266         dev_err(&input_dev->dev,
0267             "Timeout waiting for response from device.\n");
0268         error = -ENODEV;
0269         goto fail;
0270     }
0271 
0272 /*
0273  * Get device info.
0274  */
0275 
0276     if (!iforce_get_id_packet(iforce, 'M', buf, &len) || len < 3)
0277         input_dev->id.vendor = get_unaligned_le16(buf + 1);
0278     else
0279         dev_warn(&iforce->dev->dev, "Device does not respond to id packet M\n");
0280 
0281     if (!iforce_get_id_packet(iforce, 'P', buf, &len) || len < 3)
0282         input_dev->id.product = get_unaligned_le16(buf + 1);
0283     else
0284         dev_warn(&iforce->dev->dev, "Device does not respond to id packet P\n");
0285 
0286     if (!iforce_get_id_packet(iforce, 'B', buf, &len) || len < 3)
0287         iforce->device_memory.end = get_unaligned_le16(buf + 1);
0288     else
0289         dev_warn(&iforce->dev->dev, "Device does not respond to id packet B\n");
0290 
0291     if (!iforce_get_id_packet(iforce, 'N', buf, &len) || len < 2)
0292         ff_effects = buf[1];
0293     else
0294         dev_warn(&iforce->dev->dev, "Device does not respond to id packet N\n");
0295 
0296     /* Check if the device can store more effects than the driver can really handle */
0297     if (ff_effects > IFORCE_EFFECTS_MAX) {
0298         dev_warn(&iforce->dev->dev, "Limiting number of effects to %d (device reports %d)\n",
0299                IFORCE_EFFECTS_MAX, ff_effects);
0300         ff_effects = IFORCE_EFFECTS_MAX;
0301     }
0302 
0303 /*
0304  * Display additional info.
0305  */
0306 
0307     for (i = 0; c[i]; i++)
0308         if (!iforce_get_id_packet(iforce, c[i], buf, &len))
0309             iforce_dump_packet(iforce, "info",
0310                        (FF_CMD_QUERY & 0xff00) | len, buf);
0311 
0312 /*
0313  * Disable spring, enable force feedback.
0314  */
0315     iforce_set_autocenter(input_dev, 0);
0316 
0317 /*
0318  * Find appropriate device entry
0319  */
0320 
0321     for (i = 0; iforce_device[i].idvendor; i++)
0322         if (iforce_device[i].idvendor == input_dev->id.vendor &&
0323             iforce_device[i].idproduct == input_dev->id.product)
0324             break;
0325 
0326     iforce->type = iforce_device + i;
0327     input_dev->name = iforce->type->name;
0328 
0329 /*
0330  * Set input device bitfields and ranges.
0331  */
0332 
0333     input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS) |
0334         BIT_MASK(EV_FF_STATUS);
0335 
0336     for (i = 0; iforce->type->btn[i] >= 0; i++)
0337         set_bit(iforce->type->btn[i], input_dev->keybit);
0338 
0339     for (i = 0; iforce->type->abs[i] >= 0; i++) {
0340 
0341         signed short t = iforce->type->abs[i];
0342 
0343         switch (t) {
0344         case ABS_X:
0345         case ABS_Y:
0346         case ABS_WHEEL:
0347             input_set_abs_params(input_dev, t, -1920, 1920, 16, 128);
0348             set_bit(t, input_dev->ffbit);
0349             break;
0350 
0351         case ABS_THROTTLE:
0352         case ABS_GAS:
0353         case ABS_BRAKE:
0354             input_set_abs_params(input_dev, t, 0, 255, 0, 0);
0355             break;
0356 
0357         case ABS_RUDDER:
0358             input_set_abs_params(input_dev, t, -128, 127, 0, 0);
0359             break;
0360 
0361         case ABS_HAT0X:
0362         case ABS_HAT0Y:
0363         case ABS_HAT1X:
0364         case ABS_HAT1Y:
0365             input_set_abs_params(input_dev, t, -1, 1, 0, 0);
0366             break;
0367         }
0368     }
0369 
0370     if (ff_effects) {
0371 
0372         for (i = 0; iforce->type->ff[i] >= 0; i++)
0373             set_bit(iforce->type->ff[i], input_dev->ffbit);
0374 
0375         error = input_ff_create(input_dev, ff_effects);
0376         if (error)
0377             goto fail;
0378 
0379         ff = input_dev->ff;
0380         ff->upload = iforce_upload_effect;
0381         ff->erase = iforce_erase_effect;
0382         ff->set_gain = iforce_set_gain;
0383         ff->set_autocenter = iforce_set_autocenter;
0384         ff->playback = iforce_playback;
0385     }
0386 /*
0387  * Register input device.
0388  */
0389 
0390     error = input_register_device(iforce->dev);
0391     if (error)
0392         goto fail;
0393 
0394     return 0;
0395 
0396  fail:  input_free_device(input_dev);
0397     return error;
0398 }
0399 EXPORT_SYMBOL(iforce_init_device);