Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Force feedback support for various HID compliant devices by ThrustMaster:
0004  *    ThrustMaster FireStorm Dual Power 2
0005  * and possibly others whose device ids haven't been added.
0006  *
0007  *  Modified to support ThrustMaster devices by Zinx Verituse
0008  *  on 2003-01-25 from the Logitech force feedback driver,
0009  *  which is by Johann Deneux.
0010  *
0011  *  Copyright (c) 2003 Zinx Verituse <zinx@epicsol.org>
0012  *  Copyright (c) 2002 Johann Deneux
0013  */
0014 
0015 /*
0016  */
0017 
0018 #include <linux/hid.h>
0019 #include <linux/input.h>
0020 #include <linux/slab.h>
0021 #include <linux/module.h>
0022 
0023 #include "hid-ids.h"
0024 
0025 #define THRUSTMASTER_DEVICE_ID_2_IN_1_DT    0xb320
0026 
0027 static const signed short ff_rumble[] = {
0028     FF_RUMBLE,
0029     -1
0030 };
0031 
0032 static const signed short ff_joystick[] = {
0033     FF_CONSTANT,
0034     -1
0035 };
0036 
0037 #ifdef CONFIG_THRUSTMASTER_FF
0038 
0039 /* Usages for thrustmaster devices I know about */
0040 #define THRUSTMASTER_USAGE_FF   (HID_UP_GENDESK | 0xbb)
0041 
0042 struct tmff_device {
0043     struct hid_report *report;
0044     struct hid_field *ff_field;
0045 };
0046 
0047 /* Changes values from 0 to 0xffff into values from minimum to maximum */
0048 static inline int tmff_scale_u16(unsigned int in, int minimum, int maximum)
0049 {
0050     int ret;
0051 
0052     ret = (in * (maximum - minimum) / 0xffff) + minimum;
0053     if (ret < minimum)
0054         return minimum;
0055     if (ret > maximum)
0056         return maximum;
0057     return ret;
0058 }
0059 
0060 /* Changes values from -0x80 to 0x7f into values from minimum to maximum */
0061 static inline int tmff_scale_s8(int in, int minimum, int maximum)
0062 {
0063     int ret;
0064 
0065     ret = (((in + 0x80) * (maximum - minimum)) / 0xff) + minimum;
0066     if (ret < minimum)
0067         return minimum;
0068     if (ret > maximum)
0069         return maximum;
0070     return ret;
0071 }
0072 
0073 static int tmff_play(struct input_dev *dev, void *data,
0074         struct ff_effect *effect)
0075 {
0076     struct hid_device *hid = input_get_drvdata(dev);
0077     struct tmff_device *tmff = data;
0078     struct hid_field *ff_field = tmff->ff_field;
0079     int x, y;
0080     int left, right;    /* Rumbling */
0081 
0082     switch (effect->type) {
0083     case FF_CONSTANT:
0084         x = tmff_scale_s8(effect->u.ramp.start_level,
0085                     ff_field->logical_minimum,
0086                     ff_field->logical_maximum);
0087         y = tmff_scale_s8(effect->u.ramp.end_level,
0088                     ff_field->logical_minimum,
0089                     ff_field->logical_maximum);
0090 
0091         dbg_hid("(x, y)=(%04x, %04x)\n", x, y);
0092         ff_field->value[0] = x;
0093         ff_field->value[1] = y;
0094         hid_hw_request(hid, tmff->report, HID_REQ_SET_REPORT);
0095         break;
0096 
0097     case FF_RUMBLE:
0098         left = tmff_scale_u16(effect->u.rumble.weak_magnitude,
0099                     ff_field->logical_minimum,
0100                     ff_field->logical_maximum);
0101         right = tmff_scale_u16(effect->u.rumble.strong_magnitude,
0102                     ff_field->logical_minimum,
0103                     ff_field->logical_maximum);
0104 
0105         /* 2-in-1 strong motor is left */
0106         if (hid->product == THRUSTMASTER_DEVICE_ID_2_IN_1_DT)
0107             swap(left, right);
0108 
0109         dbg_hid("(left,right)=(%08x, %08x)\n", left, right);
0110         ff_field->value[0] = left;
0111         ff_field->value[1] = right;
0112         hid_hw_request(hid, tmff->report, HID_REQ_SET_REPORT);
0113         break;
0114     }
0115     return 0;
0116 }
0117 
0118 static int tmff_init(struct hid_device *hid, const signed short *ff_bits)
0119 {
0120     struct tmff_device *tmff;
0121     struct hid_report *report;
0122     struct list_head *report_list;
0123     struct hid_input *hidinput;
0124     struct input_dev *input_dev;
0125     int error;
0126     int i;
0127 
0128     if (list_empty(&hid->inputs)) {
0129         hid_err(hid, "no inputs found\n");
0130         return -ENODEV;
0131     }
0132     hidinput = list_entry(hid->inputs.next, struct hid_input, list);
0133     input_dev = hidinput->input;
0134 
0135     tmff = kzalloc(sizeof(struct tmff_device), GFP_KERNEL);
0136     if (!tmff)
0137         return -ENOMEM;
0138 
0139     /* Find the report to use */
0140     report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
0141     list_for_each_entry(report, report_list, list) {
0142         int fieldnum;
0143 
0144         for (fieldnum = 0; fieldnum < report->maxfield; ++fieldnum) {
0145             struct hid_field *field = report->field[fieldnum];
0146 
0147             if (field->maxusage <= 0)
0148                 continue;
0149 
0150             switch (field->usage[0].hid) {
0151             case THRUSTMASTER_USAGE_FF:
0152                 if (field->report_count < 2) {
0153                     hid_warn(hid, "ignoring FF field with report_count < 2\n");
0154                     continue;
0155                 }
0156 
0157                 if (field->logical_maximum ==
0158                         field->logical_minimum) {
0159                     hid_warn(hid, "ignoring FF field with logical_maximum == logical_minimum\n");
0160                     continue;
0161                 }
0162 
0163                 if (tmff->report && tmff->report != report) {
0164                     hid_warn(hid, "ignoring FF field in other report\n");
0165                     continue;
0166                 }
0167 
0168                 if (tmff->ff_field && tmff->ff_field != field) {
0169                     hid_warn(hid, "ignoring duplicate FF field\n");
0170                     continue;
0171                 }
0172 
0173                 tmff->report = report;
0174                 tmff->ff_field = field;
0175 
0176                 for (i = 0; ff_bits[i] >= 0; i++)
0177                     set_bit(ff_bits[i], input_dev->ffbit);
0178 
0179                 break;
0180 
0181             default:
0182                 hid_warn(hid, "ignoring unknown output usage %08x\n",
0183                      field->usage[0].hid);
0184                 continue;
0185             }
0186         }
0187     }
0188 
0189     if (!tmff->report) {
0190         hid_err(hid, "can't find FF field in output reports\n");
0191         error = -ENODEV;
0192         goto fail;
0193     }
0194 
0195     error = input_ff_create_memless(input_dev, tmff, tmff_play);
0196     if (error)
0197         goto fail;
0198 
0199     hid_info(hid, "force feedback for ThrustMaster devices by Zinx Verituse <zinx@epicsol.org>\n");
0200     return 0;
0201 
0202 fail:
0203     kfree(tmff);
0204     return error;
0205 }
0206 #else
0207 static inline int tmff_init(struct hid_device *hid, const signed short *ff_bits)
0208 {
0209     return 0;
0210 }
0211 #endif
0212 
0213 static int tm_probe(struct hid_device *hdev, const struct hid_device_id *id)
0214 {
0215     int ret;
0216 
0217     ret = hid_parse(hdev);
0218     if (ret) {
0219         hid_err(hdev, "parse failed\n");
0220         goto err;
0221     }
0222 
0223     ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT & ~HID_CONNECT_FF);
0224     if (ret) {
0225         hid_err(hdev, "hw start failed\n");
0226         goto err;
0227     }
0228 
0229     tmff_init(hdev, (void *)id->driver_data);
0230 
0231     return 0;
0232 err:
0233     return ret;
0234 }
0235 
0236 static const struct hid_device_id tm_devices[] = {
0237     { HID_USB_DEVICE(USB_VENDOR_ID_THRUSTMASTER, 0xb300),
0238         .driver_data = (unsigned long)ff_rumble },
0239     { HID_USB_DEVICE(USB_VENDOR_ID_THRUSTMASTER, 0xb304),   /* FireStorm Dual Power 2 (and 3) */
0240         .driver_data = (unsigned long)ff_rumble },
0241     { HID_USB_DEVICE(USB_VENDOR_ID_THRUSTMASTER, THRUSTMASTER_DEVICE_ID_2_IN_1_DT),   /* Dual Trigger 2-in-1 */
0242         .driver_data = (unsigned long)ff_rumble },
0243     { HID_USB_DEVICE(USB_VENDOR_ID_THRUSTMASTER, 0xb323),   /* Dual Trigger 3-in-1 (PC Mode) */
0244         .driver_data = (unsigned long)ff_rumble },
0245     { HID_USB_DEVICE(USB_VENDOR_ID_THRUSTMASTER, 0xb324),   /* Dual Trigger 3-in-1 (PS3 Mode) */
0246         .driver_data = (unsigned long)ff_rumble },
0247     { HID_USB_DEVICE(USB_VENDOR_ID_THRUSTMASTER, 0xb605),   /* NASCAR PRO FF2 Wheel */
0248         .driver_data = (unsigned long)ff_joystick },
0249     { HID_USB_DEVICE(USB_VENDOR_ID_THRUSTMASTER, 0xb651),   /* FGT Rumble Force Wheel */
0250         .driver_data = (unsigned long)ff_rumble },
0251     { HID_USB_DEVICE(USB_VENDOR_ID_THRUSTMASTER, 0xb653),   /* RGT Force Feedback CLUTCH Raging Wheel */
0252         .driver_data = (unsigned long)ff_joystick },
0253     { HID_USB_DEVICE(USB_VENDOR_ID_THRUSTMASTER, 0xb654),   /* FGT Force Feedback Wheel */
0254         .driver_data = (unsigned long)ff_joystick },
0255     { HID_USB_DEVICE(USB_VENDOR_ID_THRUSTMASTER, 0xb65a),   /* F430 Force Feedback Wheel */
0256         .driver_data = (unsigned long)ff_joystick },
0257     { }
0258 };
0259 MODULE_DEVICE_TABLE(hid, tm_devices);
0260 
0261 static struct hid_driver tm_driver = {
0262     .name = "thrustmaster",
0263     .id_table = tm_devices,
0264     .probe = tm_probe,
0265 };
0266 module_hid_driver(tm_driver);
0267 
0268 MODULE_LICENSE("GPL");