Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  *  Force feedback support for Logitech Gaming Wheels
0004  *
0005  *  Including G27, G25, DFP, DFGT, FFEX, Momo, Momo2 &
0006  *  Speed Force Wireless (WiiWheel)
0007  *
0008  *  Copyright (c) 2010 Simon Wood <simon@mungewell.org>
0009  */
0010 
0011 /*
0012  */
0013 
0014 
0015 #include <linux/input.h>
0016 #include <linux/usb.h>
0017 #include <linux/hid.h>
0018 
0019 #include "usbhid/usbhid.h"
0020 #include "hid-lg.h"
0021 #include "hid-lg4ff.h"
0022 #include "hid-ids.h"
0023 
0024 #define LG4FF_MMODE_IS_MULTIMODE 0
0025 #define LG4FF_MMODE_SWITCHED 1
0026 #define LG4FF_MMODE_NOT_MULTIMODE 2
0027 
0028 #define LG4FF_MODE_NATIVE_IDX 0
0029 #define LG4FF_MODE_DFEX_IDX 1
0030 #define LG4FF_MODE_DFP_IDX 2
0031 #define LG4FF_MODE_G25_IDX 3
0032 #define LG4FF_MODE_DFGT_IDX 4
0033 #define LG4FF_MODE_G27_IDX 5
0034 #define LG4FF_MODE_G29_IDX 6
0035 #define LG4FF_MODE_MAX_IDX 7
0036 
0037 #define LG4FF_MODE_NATIVE BIT(LG4FF_MODE_NATIVE_IDX)
0038 #define LG4FF_MODE_DFEX BIT(LG4FF_MODE_DFEX_IDX)
0039 #define LG4FF_MODE_DFP BIT(LG4FF_MODE_DFP_IDX)
0040 #define LG4FF_MODE_G25 BIT(LG4FF_MODE_G25_IDX)
0041 #define LG4FF_MODE_DFGT BIT(LG4FF_MODE_DFGT_IDX)
0042 #define LG4FF_MODE_G27 BIT(LG4FF_MODE_G27_IDX)
0043 #define LG4FF_MODE_G29 BIT(LG4FF_MODE_G29_IDX)
0044 
0045 #define LG4FF_DFEX_TAG "DF-EX"
0046 #define LG4FF_DFEX_NAME "Driving Force / Formula EX"
0047 #define LG4FF_DFP_TAG "DFP"
0048 #define LG4FF_DFP_NAME "Driving Force Pro"
0049 #define LG4FF_G25_TAG "G25"
0050 #define LG4FF_G25_NAME "G25 Racing Wheel"
0051 #define LG4FF_G27_TAG "G27"
0052 #define LG4FF_G27_NAME "G27 Racing Wheel"
0053 #define LG4FF_G29_TAG "G29"
0054 #define LG4FF_G29_NAME "G29 Racing Wheel"
0055 #define LG4FF_DFGT_TAG "DFGT"
0056 #define LG4FF_DFGT_NAME "Driving Force GT"
0057 
0058 #define LG4FF_FFEX_REV_MAJ 0x21
0059 #define LG4FF_FFEX_REV_MIN 0x00
0060 
0061 static void lg4ff_set_range_dfp(struct hid_device *hid, u16 range);
0062 static void lg4ff_set_range_g25(struct hid_device *hid, u16 range);
0063 
0064 struct lg4ff_wheel_data {
0065     const u32 product_id;
0066     u16 combine;
0067     u16 range;
0068     const u16 min_range;
0069     const u16 max_range;
0070 #ifdef CONFIG_LEDS_CLASS
0071     u8  led_state;
0072     struct led_classdev *led[5];
0073 #endif
0074     const u32 alternate_modes;
0075     const char * const real_tag;
0076     const char * const real_name;
0077     const u16 real_product_id;
0078 
0079     void (*set_range)(struct hid_device *hid, u16 range);
0080 };
0081 
0082 struct lg4ff_device_entry {
0083     spinlock_t report_lock; /* Protect output HID report */
0084     struct hid_report *report;
0085     struct lg4ff_wheel_data wdata;
0086 };
0087 
0088 static const signed short lg4ff_wheel_effects[] = {
0089     FF_CONSTANT,
0090     FF_AUTOCENTER,
0091     -1
0092 };
0093 
0094 static const signed short no_wheel_effects[] = {
0095     -1
0096 };
0097 
0098 struct lg4ff_wheel {
0099     const u32 product_id;
0100     const signed short *ff_effects;
0101     const u16 min_range;
0102     const u16 max_range;
0103     void (*set_range)(struct hid_device *hid, u16 range);
0104 };
0105 
0106 struct lg4ff_compat_mode_switch {
0107     const u8 cmd_count; /* Number of commands to send */
0108     const u8 cmd[];
0109 };
0110 
0111 struct lg4ff_wheel_ident_info {
0112     const u32 modes;
0113     const u16 mask;
0114     const u16 result;
0115     const u16 real_product_id;
0116 };
0117 
0118 struct lg4ff_multimode_wheel {
0119     const u16 product_id;
0120     const u32 alternate_modes;
0121     const char *real_tag;
0122     const char *real_name;
0123 };
0124 
0125 struct lg4ff_alternate_mode {
0126     const u16 product_id;
0127     const char *tag;
0128     const char *name;
0129 };
0130 
0131 static const struct lg4ff_wheel lg4ff_devices[] = {
0132     {USB_DEVICE_ID_LOGITECH_WINGMAN_FG,  no_wheel_effects,    40, 180, NULL},
0133     {USB_DEVICE_ID_LOGITECH_WINGMAN_FFG, lg4ff_wheel_effects, 40, 180, NULL},
0134     {USB_DEVICE_ID_LOGITECH_WHEEL,       lg4ff_wheel_effects, 40, 270, NULL},
0135     {USB_DEVICE_ID_LOGITECH_MOMO_WHEEL,  lg4ff_wheel_effects, 40, 270, NULL},
0136     {USB_DEVICE_ID_LOGITECH_DFP_WHEEL,   lg4ff_wheel_effects, 40, 900, lg4ff_set_range_dfp},
0137     {USB_DEVICE_ID_LOGITECH_G25_WHEEL,   lg4ff_wheel_effects, 40, 900, lg4ff_set_range_g25},
0138     {USB_DEVICE_ID_LOGITECH_DFGT_WHEEL,  lg4ff_wheel_effects, 40, 900, lg4ff_set_range_g25},
0139     {USB_DEVICE_ID_LOGITECH_G27_WHEEL,   lg4ff_wheel_effects, 40, 900, lg4ff_set_range_g25},
0140     {USB_DEVICE_ID_LOGITECH_G29_WHEEL,   lg4ff_wheel_effects, 40, 900, lg4ff_set_range_g25},
0141     {USB_DEVICE_ID_LOGITECH_MOMO_WHEEL2, lg4ff_wheel_effects, 40, 270, NULL},
0142     {USB_DEVICE_ID_LOGITECH_WII_WHEEL,   lg4ff_wheel_effects, 40, 270, NULL}
0143 };
0144 
0145 static const struct lg4ff_multimode_wheel lg4ff_multimode_wheels[] = {
0146     {USB_DEVICE_ID_LOGITECH_DFP_WHEEL,
0147      LG4FF_MODE_NATIVE | LG4FF_MODE_DFP | LG4FF_MODE_DFEX,
0148      LG4FF_DFP_TAG, LG4FF_DFP_NAME},
0149     {USB_DEVICE_ID_LOGITECH_G25_WHEEL,
0150      LG4FF_MODE_NATIVE | LG4FF_MODE_G25 | LG4FF_MODE_DFP | LG4FF_MODE_DFEX,
0151      LG4FF_G25_TAG, LG4FF_G25_NAME},
0152     {USB_DEVICE_ID_LOGITECH_DFGT_WHEEL,
0153      LG4FF_MODE_NATIVE | LG4FF_MODE_DFGT | LG4FF_MODE_DFP | LG4FF_MODE_DFEX,
0154      LG4FF_DFGT_TAG, LG4FF_DFGT_NAME},
0155     {USB_DEVICE_ID_LOGITECH_G27_WHEEL,
0156      LG4FF_MODE_NATIVE | LG4FF_MODE_G27 | LG4FF_MODE_G25 | LG4FF_MODE_DFP | LG4FF_MODE_DFEX,
0157      LG4FF_G27_TAG, LG4FF_G27_NAME},
0158     {USB_DEVICE_ID_LOGITECH_G29_WHEEL,
0159      LG4FF_MODE_NATIVE | LG4FF_MODE_G29 | LG4FF_MODE_G27 | LG4FF_MODE_G25 | LG4FF_MODE_DFGT | LG4FF_MODE_DFP | LG4FF_MODE_DFEX,
0160      LG4FF_G29_TAG, LG4FF_G29_NAME},
0161 };
0162 
0163 static const struct lg4ff_alternate_mode lg4ff_alternate_modes[] = {
0164     [LG4FF_MODE_NATIVE_IDX] = {0, "native", ""},
0165     [LG4FF_MODE_DFEX_IDX] = {USB_DEVICE_ID_LOGITECH_WHEEL, LG4FF_DFEX_TAG, LG4FF_DFEX_NAME},
0166     [LG4FF_MODE_DFP_IDX] = {USB_DEVICE_ID_LOGITECH_DFP_WHEEL, LG4FF_DFP_TAG, LG4FF_DFP_NAME},
0167     [LG4FF_MODE_G25_IDX] = {USB_DEVICE_ID_LOGITECH_G25_WHEEL, LG4FF_G25_TAG, LG4FF_G25_NAME},
0168     [LG4FF_MODE_DFGT_IDX] = {USB_DEVICE_ID_LOGITECH_DFGT_WHEEL, LG4FF_DFGT_TAG, LG4FF_DFGT_NAME},
0169     [LG4FF_MODE_G27_IDX] = {USB_DEVICE_ID_LOGITECH_G27_WHEEL, LG4FF_G27_TAG, LG4FF_G27_NAME},
0170     [LG4FF_MODE_G29_IDX] = {USB_DEVICE_ID_LOGITECH_G29_WHEEL, LG4FF_G29_TAG, LG4FF_G29_NAME},
0171 };
0172 
0173 /* Multimode wheel identificators */
0174 static const struct lg4ff_wheel_ident_info lg4ff_dfp_ident_info = {
0175     LG4FF_MODE_DFP | LG4FF_MODE_DFEX,
0176     0xf000,
0177     0x1000,
0178     USB_DEVICE_ID_LOGITECH_DFP_WHEEL
0179 };
0180 
0181 static const struct lg4ff_wheel_ident_info lg4ff_g25_ident_info = {
0182     LG4FF_MODE_G25 | LG4FF_MODE_DFP | LG4FF_MODE_DFEX,
0183     0xff00,
0184     0x1200,
0185     USB_DEVICE_ID_LOGITECH_G25_WHEEL
0186 };
0187 
0188 static const struct lg4ff_wheel_ident_info lg4ff_g27_ident_info = {
0189     LG4FF_MODE_G27 | LG4FF_MODE_G25 | LG4FF_MODE_DFP | LG4FF_MODE_DFEX,
0190     0xfff0,
0191     0x1230,
0192     USB_DEVICE_ID_LOGITECH_G27_WHEEL
0193 };
0194 
0195 static const struct lg4ff_wheel_ident_info lg4ff_dfgt_ident_info = {
0196     LG4FF_MODE_DFGT | LG4FF_MODE_DFP | LG4FF_MODE_DFEX,
0197     0xff00,
0198     0x1300,
0199     USB_DEVICE_ID_LOGITECH_DFGT_WHEEL
0200 };
0201 
0202 static const struct lg4ff_wheel_ident_info lg4ff_g29_ident_info = {
0203     LG4FF_MODE_G29 | LG4FF_MODE_G27 | LG4FF_MODE_G25 | LG4FF_MODE_DFGT | LG4FF_MODE_DFP | LG4FF_MODE_DFEX,
0204     0xfff8,
0205     0x1350,
0206     USB_DEVICE_ID_LOGITECH_G29_WHEEL
0207 };
0208 
0209 static const struct lg4ff_wheel_ident_info lg4ff_g29_ident_info2 = {
0210     LG4FF_MODE_G29 | LG4FF_MODE_G27 | LG4FF_MODE_G25 | LG4FF_MODE_DFGT | LG4FF_MODE_DFP | LG4FF_MODE_DFEX,
0211     0xff00,
0212     0x8900,
0213     USB_DEVICE_ID_LOGITECH_G29_WHEEL
0214 };
0215 
0216 /* Multimode wheel identification checklists */
0217 static const struct lg4ff_wheel_ident_info *lg4ff_main_checklist[] = {
0218     &lg4ff_g29_ident_info,
0219     &lg4ff_g29_ident_info2,
0220     &lg4ff_dfgt_ident_info,
0221     &lg4ff_g27_ident_info,
0222     &lg4ff_g25_ident_info,
0223     &lg4ff_dfp_ident_info
0224 };
0225 
0226 /* Compatibility mode switching commands */
0227 /* EXT_CMD9 - Understood by G27 and DFGT */
0228 static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext09_dfex = {
0229     2,
0230     {0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00,  /* Revert mode upon USB reset */
0231      0xf8, 0x09, 0x00, 0x01, 0x00, 0x00, 0x00}  /* Switch mode to DF-EX with detach */
0232 };
0233 
0234 static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext09_dfp = {
0235     2,
0236     {0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00,  /* Revert mode upon USB reset */
0237      0xf8, 0x09, 0x01, 0x01, 0x00, 0x00, 0x00}  /* Switch mode to DFP with detach */
0238 };
0239 
0240 static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext09_g25 = {
0241     2,
0242     {0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00,  /* Revert mode upon USB reset */
0243      0xf8, 0x09, 0x02, 0x01, 0x00, 0x00, 0x00}  /* Switch mode to G25 with detach */
0244 };
0245 
0246 static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext09_dfgt = {
0247     2,
0248     {0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00,  /* Revert mode upon USB reset */
0249      0xf8, 0x09, 0x03, 0x01, 0x00, 0x00, 0x00}  /* Switch mode to DFGT with detach */
0250 };
0251 
0252 static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext09_g27 = {
0253     2,
0254     {0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00,  /* Revert mode upon USB reset */
0255      0xf8, 0x09, 0x04, 0x01, 0x00, 0x00, 0x00}  /* Switch mode to G27 with detach */
0256 };
0257 
0258 static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext09_g29 = {
0259     2,
0260     {0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00,  /* Revert mode upon USB reset */
0261      0xf8, 0x09, 0x05, 0x01, 0x01, 0x00, 0x00}  /* Switch mode to G29 with detach */
0262 };
0263 
0264 /* EXT_CMD1 - Understood by DFP, G25, G27 and DFGT */
0265 static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext01_dfp = {
0266     1,
0267     {0xf8, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00}
0268 };
0269 
0270 /* EXT_CMD16 - Understood by G25 and G27 */
0271 static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext16_g25 = {
0272     1,
0273     {0xf8, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00}
0274 };
0275 
0276 /* Recalculates X axis value accordingly to currently selected range */
0277 static s32 lg4ff_adjust_dfp_x_axis(s32 value, u16 range)
0278 {
0279     u16 max_range;
0280     s32 new_value;
0281 
0282     if (range == 900)
0283         return value;
0284     else if (range == 200)
0285         return value;
0286     else if (range < 200)
0287         max_range = 200;
0288     else
0289         max_range = 900;
0290 
0291     new_value = 8192 + mult_frac(value - 8192, max_range, range);
0292     if (new_value < 0)
0293         return 0;
0294     else if (new_value > 16383)
0295         return 16383;
0296     else
0297         return new_value;
0298 }
0299 
0300 int lg4ff_adjust_input_event(struct hid_device *hid, struct hid_field *field,
0301                  struct hid_usage *usage, s32 value, struct lg_drv_data *drv_data)
0302 {
0303     struct lg4ff_device_entry *entry = drv_data->device_props;
0304     s32 new_value = 0;
0305 
0306     if (!entry) {
0307         hid_err(hid, "Device properties not found");
0308         return 0;
0309     }
0310 
0311     switch (entry->wdata.product_id) {
0312     case USB_DEVICE_ID_LOGITECH_DFP_WHEEL:
0313         switch (usage->code) {
0314         case ABS_X:
0315             new_value = lg4ff_adjust_dfp_x_axis(value, entry->wdata.range);
0316             input_event(field->hidinput->input, usage->type, usage->code, new_value);
0317             return 1;
0318         default:
0319             return 0;
0320         }
0321     default:
0322         return 0;
0323     }
0324 }
0325 
0326 int lg4ff_raw_event(struct hid_device *hdev, struct hid_report *report,
0327         u8 *rd, int size, struct lg_drv_data *drv_data)
0328 {
0329     int offset;
0330     struct lg4ff_device_entry *entry = drv_data->device_props;
0331 
0332     if (!entry)
0333         return 0;
0334 
0335     /* adjust HID report present combined pedals data */
0336     if (entry->wdata.combine) {
0337         switch (entry->wdata.product_id) {
0338         case USB_DEVICE_ID_LOGITECH_WHEEL:
0339             rd[5] = rd[3];
0340             rd[6] = 0x7F;
0341             return 1;
0342         case USB_DEVICE_ID_LOGITECH_WINGMAN_FG:
0343         case USB_DEVICE_ID_LOGITECH_WINGMAN_FFG:
0344         case USB_DEVICE_ID_LOGITECH_MOMO_WHEEL:
0345         case USB_DEVICE_ID_LOGITECH_MOMO_WHEEL2:
0346             rd[4] = rd[3];
0347             rd[5] = 0x7F;
0348             return 1;
0349         case USB_DEVICE_ID_LOGITECH_DFP_WHEEL:
0350             rd[5] = rd[4];
0351             rd[6] = 0x7F;
0352             return 1;
0353         case USB_DEVICE_ID_LOGITECH_G25_WHEEL:
0354         case USB_DEVICE_ID_LOGITECH_G27_WHEEL:
0355             offset = 5;
0356             break;
0357         case USB_DEVICE_ID_LOGITECH_DFGT_WHEEL:
0358         case USB_DEVICE_ID_LOGITECH_G29_WHEEL:
0359             offset = 6;
0360             break;
0361         case USB_DEVICE_ID_LOGITECH_WII_WHEEL:
0362             offset = 3;
0363             break;
0364         default:
0365             return 0;
0366         }
0367 
0368         /* Compute a combined axis when wheel does not supply it */
0369         rd[offset] = (0xFF + rd[offset] - rd[offset+1]) >> 1;
0370         rd[offset+1] = 0x7F;
0371         return 1;
0372     }
0373 
0374     return 0;
0375 }
0376 
0377 static void lg4ff_init_wheel_data(struct lg4ff_wheel_data * const wdata, const struct lg4ff_wheel *wheel,
0378                   const struct lg4ff_multimode_wheel *mmode_wheel,
0379                   const u16 real_product_id)
0380 {
0381     u32 alternate_modes = 0;
0382     const char *real_tag = NULL;
0383     const char *real_name = NULL;
0384 
0385     if (mmode_wheel) {
0386         alternate_modes = mmode_wheel->alternate_modes;
0387         real_tag = mmode_wheel->real_tag;
0388         real_name = mmode_wheel->real_name;
0389     }
0390 
0391     {
0392         struct lg4ff_wheel_data t_wdata =  { .product_id = wheel->product_id,
0393                              .real_product_id = real_product_id,
0394                              .combine = 0,
0395                              .min_range = wheel->min_range,
0396                              .max_range = wheel->max_range,
0397                              .set_range = wheel->set_range,
0398                              .alternate_modes = alternate_modes,
0399                              .real_tag = real_tag,
0400                              .real_name = real_name };
0401 
0402         memcpy(wdata, &t_wdata, sizeof(t_wdata));
0403     }
0404 }
0405 
0406 static int lg4ff_play(struct input_dev *dev, void *data, struct ff_effect *effect)
0407 {
0408     struct hid_device *hid = input_get_drvdata(dev);
0409     struct lg4ff_device_entry *entry;
0410     struct lg_drv_data *drv_data;
0411     unsigned long flags;
0412     s32 *value;
0413     int x;
0414 
0415     drv_data = hid_get_drvdata(hid);
0416     if (!drv_data) {
0417         hid_err(hid, "Private driver data not found!\n");
0418         return -EINVAL;
0419     }
0420 
0421     entry = drv_data->device_props;
0422     if (!entry) {
0423         hid_err(hid, "Device properties not found!\n");
0424         return -EINVAL;
0425     }
0426     value = entry->report->field[0]->value;
0427 
0428 #define CLAMP(x) do { if (x < 0) x = 0; else if (x > 0xff) x = 0xff; } while (0)
0429 
0430     switch (effect->type) {
0431     case FF_CONSTANT:
0432         x = effect->u.ramp.start_level + 0x80;  /* 0x80 is no force */
0433         CLAMP(x);
0434 
0435         spin_lock_irqsave(&entry->report_lock, flags);
0436         if (x == 0x80) {
0437             /* De-activate force in slot-1*/
0438             value[0] = 0x13;
0439             value[1] = 0x00;
0440             value[2] = 0x00;
0441             value[3] = 0x00;
0442             value[4] = 0x00;
0443             value[5] = 0x00;
0444             value[6] = 0x00;
0445 
0446             hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT);
0447             spin_unlock_irqrestore(&entry->report_lock, flags);
0448             return 0;
0449         }
0450 
0451         value[0] = 0x11;    /* Slot 1 */
0452         value[1] = 0x08;
0453         value[2] = x;
0454         value[3] = 0x80;
0455         value[4] = 0x00;
0456         value[5] = 0x00;
0457         value[6] = 0x00;
0458 
0459         hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT);
0460         spin_unlock_irqrestore(&entry->report_lock, flags);
0461         break;
0462     }
0463     return 0;
0464 }
0465 
0466 /* Sends default autocentering command compatible with
0467  * all wheels except Formula Force EX */
0468 static void lg4ff_set_autocenter_default(struct input_dev *dev, u16 magnitude)
0469 {
0470     struct hid_device *hid = input_get_drvdata(dev);
0471     s32 *value;
0472     u32 expand_a, expand_b;
0473     struct lg4ff_device_entry *entry;
0474     struct lg_drv_data *drv_data;
0475     unsigned long flags;
0476 
0477     drv_data = hid_get_drvdata(hid);
0478     if (!drv_data) {
0479         hid_err(hid, "Private driver data not found!\n");
0480         return;
0481     }
0482 
0483     entry = drv_data->device_props;
0484     if (!entry) {
0485         hid_err(hid, "Device properties not found!\n");
0486         return;
0487     }
0488     value = entry->report->field[0]->value;
0489 
0490     /* De-activate Auto-Center */
0491     spin_lock_irqsave(&entry->report_lock, flags);
0492     if (magnitude == 0) {
0493         value[0] = 0xf5;
0494         value[1] = 0x00;
0495         value[2] = 0x00;
0496         value[3] = 0x00;
0497         value[4] = 0x00;
0498         value[5] = 0x00;
0499         value[6] = 0x00;
0500 
0501         hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT);
0502         spin_unlock_irqrestore(&entry->report_lock, flags);
0503         return;
0504     }
0505 
0506     if (magnitude <= 0xaaaa) {
0507         expand_a = 0x0c * magnitude;
0508         expand_b = 0x80 * magnitude;
0509     } else {
0510         expand_a = (0x0c * 0xaaaa) + 0x06 * (magnitude - 0xaaaa);
0511         expand_b = (0x80 * 0xaaaa) + 0xff * (magnitude - 0xaaaa);
0512     }
0513 
0514     /* Adjust for non-MOMO wheels */
0515     switch (entry->wdata.product_id) {
0516     case USB_DEVICE_ID_LOGITECH_MOMO_WHEEL:
0517     case USB_DEVICE_ID_LOGITECH_MOMO_WHEEL2:
0518         break;
0519     default:
0520         expand_a = expand_a >> 1;
0521         break;
0522     }
0523 
0524     value[0] = 0xfe;
0525     value[1] = 0x0d;
0526     value[2] = expand_a / 0xaaaa;
0527     value[3] = expand_a / 0xaaaa;
0528     value[4] = expand_b / 0xaaaa;
0529     value[5] = 0x00;
0530     value[6] = 0x00;
0531 
0532     hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT);
0533 
0534     /* Activate Auto-Center */
0535     value[0] = 0x14;
0536     value[1] = 0x00;
0537     value[2] = 0x00;
0538     value[3] = 0x00;
0539     value[4] = 0x00;
0540     value[5] = 0x00;
0541     value[6] = 0x00;
0542 
0543     hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT);
0544     spin_unlock_irqrestore(&entry->report_lock, flags);
0545 }
0546 
0547 /* Sends autocentering command compatible with Formula Force EX */
0548 static void lg4ff_set_autocenter_ffex(struct input_dev *dev, u16 magnitude)
0549 {
0550     struct hid_device *hid = input_get_drvdata(dev);
0551     struct lg4ff_device_entry *entry;
0552     struct lg_drv_data *drv_data;
0553     unsigned long flags;
0554     s32 *value;
0555     magnitude = magnitude * 90 / 65535;
0556 
0557     drv_data = hid_get_drvdata(hid);
0558     if (!drv_data) {
0559         hid_err(hid, "Private driver data not found!\n");
0560         return;
0561     }
0562 
0563     entry = drv_data->device_props;
0564     if (!entry) {
0565         hid_err(hid, "Device properties not found!\n");
0566         return;
0567     }
0568     value = entry->report->field[0]->value;
0569 
0570     spin_lock_irqsave(&entry->report_lock, flags);
0571     value[0] = 0xfe;
0572     value[1] = 0x03;
0573     value[2] = magnitude >> 14;
0574     value[3] = magnitude >> 14;
0575     value[4] = magnitude;
0576     value[5] = 0x00;
0577     value[6] = 0x00;
0578 
0579     hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT);
0580     spin_unlock_irqrestore(&entry->report_lock, flags);
0581 }
0582 
0583 /* Sends command to set range compatible with G25/G27/Driving Force GT */
0584 static void lg4ff_set_range_g25(struct hid_device *hid, u16 range)
0585 {
0586     struct lg4ff_device_entry *entry;
0587     struct lg_drv_data *drv_data;
0588     unsigned long flags;
0589     s32 *value;
0590 
0591     drv_data = hid_get_drvdata(hid);
0592     if (!drv_data) {
0593         hid_err(hid, "Private driver data not found!\n");
0594         return;
0595     }
0596 
0597     entry = drv_data->device_props;
0598     if (!entry) {
0599         hid_err(hid, "Device properties not found!\n");
0600         return;
0601     }
0602     value = entry->report->field[0]->value;
0603     dbg_hid("G25/G27/DFGT: setting range to %u\n", range);
0604 
0605     spin_lock_irqsave(&entry->report_lock, flags);
0606     value[0] = 0xf8;
0607     value[1] = 0x81;
0608     value[2] = range & 0x00ff;
0609     value[3] = (range & 0xff00) >> 8;
0610     value[4] = 0x00;
0611     value[5] = 0x00;
0612     value[6] = 0x00;
0613 
0614     hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT);
0615     spin_unlock_irqrestore(&entry->report_lock, flags);
0616 }
0617 
0618 /* Sends commands to set range compatible with Driving Force Pro wheel */
0619 static void lg4ff_set_range_dfp(struct hid_device *hid, u16 range)
0620 {
0621     struct lg4ff_device_entry *entry;
0622     struct lg_drv_data *drv_data;
0623     unsigned long flags;
0624     int start_left, start_right, full_range;
0625     s32 *value;
0626 
0627     drv_data = hid_get_drvdata(hid);
0628     if (!drv_data) {
0629         hid_err(hid, "Private driver data not found!\n");
0630         return;
0631     }
0632 
0633     entry = drv_data->device_props;
0634     if (!entry) {
0635         hid_err(hid, "Device properties not found!\n");
0636         return;
0637     }
0638     value = entry->report->field[0]->value;
0639     dbg_hid("Driving Force Pro: setting range to %u\n", range);
0640 
0641     /* Prepare "coarse" limit command */
0642     spin_lock_irqsave(&entry->report_lock, flags);
0643     value[0] = 0xf8;
0644     value[1] = 0x00;    /* Set later */
0645     value[2] = 0x00;
0646     value[3] = 0x00;
0647     value[4] = 0x00;
0648     value[5] = 0x00;
0649     value[6] = 0x00;
0650 
0651     if (range > 200) {
0652         value[1] = 0x03;
0653         full_range = 900;
0654     } else {
0655         value[1] = 0x02;
0656         full_range = 200;
0657     }
0658     hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT);
0659 
0660     /* Prepare "fine" limit command */
0661     value[0] = 0x81;
0662     value[1] = 0x0b;
0663     value[2] = 0x00;
0664     value[3] = 0x00;
0665     value[4] = 0x00;
0666     value[5] = 0x00;
0667     value[6] = 0x00;
0668 
0669     if (range == 200 || range == 900) { /* Do not apply any fine limit */
0670         hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT);
0671         spin_unlock_irqrestore(&entry->report_lock, flags);
0672         return;
0673     }
0674 
0675     /* Construct fine limit command */
0676     start_left = (((full_range - range + 1) * 2047) / full_range);
0677     start_right = 0xfff - start_left;
0678 
0679     value[2] = start_left >> 4;
0680     value[3] = start_right >> 4;
0681     value[4] = 0xff;
0682     value[5] = (start_right & 0xe) << 4 | (start_left & 0xe);
0683     value[6] = 0xff;
0684 
0685     hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT);
0686     spin_unlock_irqrestore(&entry->report_lock, flags);
0687 }
0688 
0689 static const struct lg4ff_compat_mode_switch *lg4ff_get_mode_switch_command(const u16 real_product_id, const u16 target_product_id)
0690 {
0691     switch (real_product_id) {
0692     case USB_DEVICE_ID_LOGITECH_DFP_WHEEL:
0693         switch (target_product_id) {
0694         case USB_DEVICE_ID_LOGITECH_DFP_WHEEL:
0695             return &lg4ff_mode_switch_ext01_dfp;
0696         /* DFP can only be switched to its native mode */
0697         default:
0698             return NULL;
0699         }
0700         break;
0701     case USB_DEVICE_ID_LOGITECH_G25_WHEEL:
0702         switch (target_product_id) {
0703         case USB_DEVICE_ID_LOGITECH_DFP_WHEEL:
0704             return &lg4ff_mode_switch_ext01_dfp;
0705         case USB_DEVICE_ID_LOGITECH_G25_WHEEL:
0706             return &lg4ff_mode_switch_ext16_g25;
0707         /* G25 can only be switched to DFP mode or its native mode */
0708         default:
0709             return NULL;
0710         }
0711         break;
0712     case USB_DEVICE_ID_LOGITECH_G27_WHEEL:
0713         switch (target_product_id) {
0714         case USB_DEVICE_ID_LOGITECH_WHEEL:
0715             return &lg4ff_mode_switch_ext09_dfex;
0716         case USB_DEVICE_ID_LOGITECH_DFP_WHEEL:
0717             return &lg4ff_mode_switch_ext09_dfp;
0718         case USB_DEVICE_ID_LOGITECH_G25_WHEEL:
0719             return &lg4ff_mode_switch_ext09_g25;
0720         case USB_DEVICE_ID_LOGITECH_G27_WHEEL:
0721             return &lg4ff_mode_switch_ext09_g27;
0722         /* G27 can only be switched to DF-EX, DFP, G25 or its native mode */
0723         default:
0724             return NULL;
0725         }
0726         break;
0727     case USB_DEVICE_ID_LOGITECH_G29_WHEEL:
0728         switch (target_product_id) {
0729         case USB_DEVICE_ID_LOGITECH_DFP_WHEEL:
0730             return &lg4ff_mode_switch_ext09_dfp;
0731         case USB_DEVICE_ID_LOGITECH_DFGT_WHEEL:
0732             return &lg4ff_mode_switch_ext09_dfgt;
0733         case USB_DEVICE_ID_LOGITECH_G25_WHEEL:
0734             return &lg4ff_mode_switch_ext09_g25;
0735         case USB_DEVICE_ID_LOGITECH_G27_WHEEL:
0736             return &lg4ff_mode_switch_ext09_g27;
0737         case USB_DEVICE_ID_LOGITECH_G29_WHEEL:
0738             return &lg4ff_mode_switch_ext09_g29;
0739         /* G29 can only be switched to DF-EX, DFP, DFGT, G25, G27 or its native mode */
0740         default:
0741             return NULL;
0742         }
0743         break;
0744     case USB_DEVICE_ID_LOGITECH_DFGT_WHEEL:
0745         switch (target_product_id) {
0746         case USB_DEVICE_ID_LOGITECH_WHEEL:
0747             return &lg4ff_mode_switch_ext09_dfex;
0748         case USB_DEVICE_ID_LOGITECH_DFP_WHEEL:
0749             return &lg4ff_mode_switch_ext09_dfp;
0750         case USB_DEVICE_ID_LOGITECH_DFGT_WHEEL:
0751             return &lg4ff_mode_switch_ext09_dfgt;
0752         /* DFGT can only be switched to DF-EX, DFP or its native mode */
0753         default:
0754             return NULL;
0755         }
0756         break;
0757     /* No other wheels have multiple modes */
0758     default:
0759         return NULL;
0760     }
0761 }
0762 
0763 static int lg4ff_switch_compatibility_mode(struct hid_device *hid, const struct lg4ff_compat_mode_switch *s)
0764 {
0765     struct lg4ff_device_entry *entry;
0766     struct lg_drv_data *drv_data;
0767     unsigned long flags;
0768     s32 *value;
0769     u8 i;
0770 
0771     drv_data = hid_get_drvdata(hid);
0772     if (!drv_data) {
0773         hid_err(hid, "Private driver data not found!\n");
0774         return -EINVAL;
0775     }
0776 
0777     entry = drv_data->device_props;
0778     if (!entry) {
0779         hid_err(hid, "Device properties not found!\n");
0780         return -EINVAL;
0781     }
0782     value = entry->report->field[0]->value;
0783 
0784     spin_lock_irqsave(&entry->report_lock, flags);
0785     for (i = 0; i < s->cmd_count; i++) {
0786         u8 j;
0787 
0788         for (j = 0; j < 7; j++)
0789             value[j] = s->cmd[j + (7*i)];
0790 
0791         hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT);
0792     }
0793     spin_unlock_irqrestore(&entry->report_lock, flags);
0794     hid_hw_wait(hid);
0795     return 0;
0796 }
0797 
0798 static ssize_t lg4ff_alternate_modes_show(struct device *dev, struct device_attribute *attr, char *buf)
0799 {
0800     struct hid_device *hid = to_hid_device(dev);
0801     struct lg4ff_device_entry *entry;
0802     struct lg_drv_data *drv_data;
0803     ssize_t count = 0;
0804     int i;
0805 
0806     drv_data = hid_get_drvdata(hid);
0807     if (!drv_data) {
0808         hid_err(hid, "Private driver data not found!\n");
0809         return 0;
0810     }
0811 
0812     entry = drv_data->device_props;
0813     if (!entry) {
0814         hid_err(hid, "Device properties not found!\n");
0815         return 0;
0816     }
0817 
0818     if (!entry->wdata.real_name) {
0819         hid_err(hid, "NULL pointer to string\n");
0820         return 0;
0821     }
0822 
0823     for (i = 0; i < LG4FF_MODE_MAX_IDX; i++) {
0824         if (entry->wdata.alternate_modes & BIT(i)) {
0825             /* Print tag and full name */
0826             count += scnprintf(buf + count, PAGE_SIZE - count, "%s: %s",
0827                        lg4ff_alternate_modes[i].tag,
0828                        !lg4ff_alternate_modes[i].product_id ? entry->wdata.real_name : lg4ff_alternate_modes[i].name);
0829             if (count >= PAGE_SIZE - 1)
0830                 return count;
0831 
0832             /* Mark the currently active mode with an asterisk */
0833             if (lg4ff_alternate_modes[i].product_id == entry->wdata.product_id ||
0834                 (lg4ff_alternate_modes[i].product_id == 0 && entry->wdata.product_id == entry->wdata.real_product_id))
0835                 count += scnprintf(buf + count, PAGE_SIZE - count, " *\n");
0836             else
0837                 count += scnprintf(buf + count, PAGE_SIZE - count, "\n");
0838 
0839             if (count >= PAGE_SIZE - 1)
0840                 return count;
0841         }
0842     }
0843 
0844     return count;
0845 }
0846 
0847 static ssize_t lg4ff_alternate_modes_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
0848 {
0849     struct hid_device *hid = to_hid_device(dev);
0850     struct lg4ff_device_entry *entry;
0851     struct lg_drv_data *drv_data;
0852     const struct lg4ff_compat_mode_switch *s;
0853     u16 target_product_id = 0;
0854     int i, ret;
0855     char *lbuf;
0856 
0857     drv_data = hid_get_drvdata(hid);
0858     if (!drv_data) {
0859         hid_err(hid, "Private driver data not found!\n");
0860         return -EINVAL;
0861     }
0862 
0863     entry = drv_data->device_props;
0864     if (!entry) {
0865         hid_err(hid, "Device properties not found!\n");
0866         return -EINVAL;
0867     }
0868 
0869     /* Allow \n at the end of the input parameter */
0870     lbuf = kasprintf(GFP_KERNEL, "%s", buf);
0871     if (!lbuf)
0872         return -ENOMEM;
0873 
0874     i = strlen(lbuf);
0875     if (lbuf[i-1] == '\n') {
0876         if (i == 1) {
0877             kfree(lbuf);
0878             return -EINVAL;
0879         }
0880         lbuf[i-1] = '\0';
0881     }
0882 
0883     for (i = 0; i < LG4FF_MODE_MAX_IDX; i++) {
0884         const u16 mode_product_id = lg4ff_alternate_modes[i].product_id;
0885         const char *tag = lg4ff_alternate_modes[i].tag;
0886 
0887         if (entry->wdata.alternate_modes & BIT(i)) {
0888             if (!strcmp(tag, lbuf)) {
0889                 if (!mode_product_id)
0890                     target_product_id = entry->wdata.real_product_id;
0891                 else
0892                     target_product_id = mode_product_id;
0893                 break;
0894             }
0895         }
0896     }
0897 
0898     if (i == LG4FF_MODE_MAX_IDX) {
0899         hid_info(hid, "Requested mode \"%s\" is not supported by the device\n", lbuf);
0900         kfree(lbuf);
0901         return -EINVAL;
0902     }
0903     kfree(lbuf); /* Not needed anymore */
0904 
0905     if (target_product_id == entry->wdata.product_id) /* Nothing to do */
0906         return count;
0907 
0908     /* Automatic switching has to be disabled for the switch to DF-EX mode to work correctly */
0909     if (target_product_id == USB_DEVICE_ID_LOGITECH_WHEEL && !lg4ff_no_autoswitch) {
0910         hid_info(hid, "\"%s\" cannot be switched to \"DF-EX\" mode. Load the \"hid_logitech\" module with \"lg4ff_no_autoswitch=1\" parameter set and try again\n",
0911              entry->wdata.real_name);
0912         return -EINVAL;
0913     }
0914 
0915     /* Take care of hardware limitations */
0916     if ((entry->wdata.real_product_id == USB_DEVICE_ID_LOGITECH_DFP_WHEEL || entry->wdata.real_product_id == USB_DEVICE_ID_LOGITECH_G25_WHEEL) &&
0917         entry->wdata.product_id > target_product_id) {
0918         hid_info(hid, "\"%s\" cannot be switched back into \"%s\" mode\n", entry->wdata.real_name, lg4ff_alternate_modes[i].name);
0919         return -EINVAL;
0920     }
0921 
0922     s = lg4ff_get_mode_switch_command(entry->wdata.real_product_id, target_product_id);
0923     if (!s) {
0924         hid_err(hid, "Invalid target product ID %X\n", target_product_id);
0925         return -EINVAL;
0926     }
0927 
0928     ret = lg4ff_switch_compatibility_mode(hid, s);
0929     return (ret == 0 ? count : ret);
0930 }
0931 static DEVICE_ATTR(alternate_modes, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH, lg4ff_alternate_modes_show, lg4ff_alternate_modes_store);
0932 
0933 static ssize_t lg4ff_combine_show(struct device *dev, struct device_attribute *attr,
0934                 char *buf)
0935 {
0936     struct hid_device *hid = to_hid_device(dev);
0937     struct lg4ff_device_entry *entry;
0938     struct lg_drv_data *drv_data;
0939     size_t count;
0940 
0941     drv_data = hid_get_drvdata(hid);
0942     if (!drv_data) {
0943         hid_err(hid, "Private driver data not found!\n");
0944         return 0;
0945     }
0946 
0947     entry = drv_data->device_props;
0948     if (!entry) {
0949         hid_err(hid, "Device properties not found!\n");
0950         return 0;
0951     }
0952 
0953     count = scnprintf(buf, PAGE_SIZE, "%u\n", entry->wdata.combine);
0954     return count;
0955 }
0956 
0957 static ssize_t lg4ff_combine_store(struct device *dev, struct device_attribute *attr,
0958                  const char *buf, size_t count)
0959 {
0960     struct hid_device *hid = to_hid_device(dev);
0961     struct lg4ff_device_entry *entry;
0962     struct lg_drv_data *drv_data;
0963     u16 combine = simple_strtoul(buf, NULL, 10);
0964 
0965     drv_data = hid_get_drvdata(hid);
0966     if (!drv_data) {
0967         hid_err(hid, "Private driver data not found!\n");
0968         return -EINVAL;
0969     }
0970 
0971     entry = drv_data->device_props;
0972     if (!entry) {
0973         hid_err(hid, "Device properties not found!\n");
0974         return -EINVAL;
0975     }
0976 
0977     if (combine > 1)
0978         combine = 1;
0979 
0980     entry->wdata.combine = combine;
0981     return count;
0982 }
0983 static DEVICE_ATTR(combine_pedals, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH, lg4ff_combine_show, lg4ff_combine_store);
0984 
0985 /* Export the currently set range of the wheel */
0986 static ssize_t lg4ff_range_show(struct device *dev, struct device_attribute *attr,
0987                 char *buf)
0988 {
0989     struct hid_device *hid = to_hid_device(dev);
0990     struct lg4ff_device_entry *entry;
0991     struct lg_drv_data *drv_data;
0992     size_t count;
0993 
0994     drv_data = hid_get_drvdata(hid);
0995     if (!drv_data) {
0996         hid_err(hid, "Private driver data not found!\n");
0997         return 0;
0998     }
0999 
1000     entry = drv_data->device_props;
1001     if (!entry) {
1002         hid_err(hid, "Device properties not found!\n");
1003         return 0;
1004     }
1005 
1006     count = scnprintf(buf, PAGE_SIZE, "%u\n", entry->wdata.range);
1007     return count;
1008 }
1009 
1010 /* Set range to user specified value, call appropriate function
1011  * according to the type of the wheel */
1012 static ssize_t lg4ff_range_store(struct device *dev, struct device_attribute *attr,
1013                  const char *buf, size_t count)
1014 {
1015     struct hid_device *hid = to_hid_device(dev);
1016     struct lg4ff_device_entry *entry;
1017     struct lg_drv_data *drv_data;
1018     u16 range = simple_strtoul(buf, NULL, 10);
1019 
1020     drv_data = hid_get_drvdata(hid);
1021     if (!drv_data) {
1022         hid_err(hid, "Private driver data not found!\n");
1023         return -EINVAL;
1024     }
1025 
1026     entry = drv_data->device_props;
1027     if (!entry) {
1028         hid_err(hid, "Device properties not found!\n");
1029         return -EINVAL;
1030     }
1031 
1032     if (range == 0)
1033         range = entry->wdata.max_range;
1034 
1035     /* Check if the wheel supports range setting
1036      * and that the range is within limits for the wheel */
1037     if (entry->wdata.set_range && range >= entry->wdata.min_range && range <= entry->wdata.max_range) {
1038         entry->wdata.set_range(hid, range);
1039         entry->wdata.range = range;
1040     }
1041 
1042     return count;
1043 }
1044 static DEVICE_ATTR(range, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH, lg4ff_range_show, lg4ff_range_store);
1045 
1046 static ssize_t lg4ff_real_id_show(struct device *dev, struct device_attribute *attr, char *buf)
1047 {
1048     struct hid_device *hid = to_hid_device(dev);
1049     struct lg4ff_device_entry *entry;
1050     struct lg_drv_data *drv_data;
1051     size_t count;
1052 
1053     drv_data = hid_get_drvdata(hid);
1054     if (!drv_data) {
1055         hid_err(hid, "Private driver data not found!\n");
1056         return 0;
1057     }
1058 
1059     entry = drv_data->device_props;
1060     if (!entry) {
1061         hid_err(hid, "Device properties not found!\n");
1062         return 0;
1063     }
1064 
1065     if (!entry->wdata.real_tag || !entry->wdata.real_name) {
1066         hid_err(hid, "NULL pointer to string\n");
1067         return 0;
1068     }
1069 
1070     count = scnprintf(buf, PAGE_SIZE, "%s: %s\n", entry->wdata.real_tag, entry->wdata.real_name);
1071     return count;
1072 }
1073 
1074 static ssize_t lg4ff_real_id_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
1075 {
1076     /* Real ID is a read-only value */
1077     return -EPERM;
1078 }
1079 static DEVICE_ATTR(real_id, S_IRUGO, lg4ff_real_id_show, lg4ff_real_id_store);
1080 
1081 #ifdef CONFIG_LEDS_CLASS
1082 static void lg4ff_set_leds(struct hid_device *hid, u8 leds)
1083 {
1084     struct lg_drv_data *drv_data;
1085     struct lg4ff_device_entry *entry;
1086     unsigned long flags;
1087     s32 *value;
1088 
1089     drv_data = hid_get_drvdata(hid);
1090     if (!drv_data) {
1091         hid_err(hid, "Private driver data not found!\n");
1092         return;
1093     }
1094 
1095     entry = drv_data->device_props;
1096     if (!entry) {
1097         hid_err(hid, "Device properties not found!\n");
1098         return;
1099     }
1100     value = entry->report->field[0]->value;
1101 
1102     spin_lock_irqsave(&entry->report_lock, flags);
1103     value[0] = 0xf8;
1104     value[1] = 0x12;
1105     value[2] = leds;
1106     value[3] = 0x00;
1107     value[4] = 0x00;
1108     value[5] = 0x00;
1109     value[6] = 0x00;
1110     hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT);
1111     spin_unlock_irqrestore(&entry->report_lock, flags);
1112 }
1113 
1114 static void lg4ff_led_set_brightness(struct led_classdev *led_cdev,
1115             enum led_brightness value)
1116 {
1117     struct device *dev = led_cdev->dev->parent;
1118     struct hid_device *hid = to_hid_device(dev);
1119     struct lg_drv_data *drv_data = hid_get_drvdata(hid);
1120     struct lg4ff_device_entry *entry;
1121     int i, state = 0;
1122 
1123     if (!drv_data) {
1124         hid_err(hid, "Device data not found.");
1125         return;
1126     }
1127 
1128     entry = drv_data->device_props;
1129 
1130     if (!entry) {
1131         hid_err(hid, "Device properties not found.");
1132         return;
1133     }
1134 
1135     for (i = 0; i < 5; i++) {
1136         if (led_cdev != entry->wdata.led[i])
1137             continue;
1138         state = (entry->wdata.led_state >> i) & 1;
1139         if (value == LED_OFF && state) {
1140             entry->wdata.led_state &= ~(1 << i);
1141             lg4ff_set_leds(hid, entry->wdata.led_state);
1142         } else if (value != LED_OFF && !state) {
1143             entry->wdata.led_state |= 1 << i;
1144             lg4ff_set_leds(hid, entry->wdata.led_state);
1145         }
1146         break;
1147     }
1148 }
1149 
1150 static enum led_brightness lg4ff_led_get_brightness(struct led_classdev *led_cdev)
1151 {
1152     struct device *dev = led_cdev->dev->parent;
1153     struct hid_device *hid = to_hid_device(dev);
1154     struct lg_drv_data *drv_data = hid_get_drvdata(hid);
1155     struct lg4ff_device_entry *entry;
1156     int i, value = 0;
1157 
1158     if (!drv_data) {
1159         hid_err(hid, "Device data not found.");
1160         return LED_OFF;
1161     }
1162 
1163     entry = drv_data->device_props;
1164 
1165     if (!entry) {
1166         hid_err(hid, "Device properties not found.");
1167         return LED_OFF;
1168     }
1169 
1170     for (i = 0; i < 5; i++)
1171         if (led_cdev == entry->wdata.led[i]) {
1172             value = (entry->wdata.led_state >> i) & 1;
1173             break;
1174         }
1175 
1176     return value ? LED_FULL : LED_OFF;
1177 }
1178 #endif
1179 
1180 static u16 lg4ff_identify_multimode_wheel(struct hid_device *hid, const u16 reported_product_id, const u16 bcdDevice)
1181 {
1182     u32 current_mode;
1183     int i;
1184 
1185     /* identify current mode from USB PID */
1186     for (i = 1; i < ARRAY_SIZE(lg4ff_alternate_modes); i++) {
1187         dbg_hid("Testing whether PID is %X\n", lg4ff_alternate_modes[i].product_id);
1188         if (reported_product_id == lg4ff_alternate_modes[i].product_id)
1189             break;
1190     }
1191 
1192     if (i == ARRAY_SIZE(lg4ff_alternate_modes))
1193         return 0;
1194 
1195     current_mode = BIT(i);
1196 
1197     for (i = 0; i < ARRAY_SIZE(lg4ff_main_checklist); i++) {
1198         const u16 mask = lg4ff_main_checklist[i]->mask;
1199         const u16 result = lg4ff_main_checklist[i]->result;
1200         const u16 real_product_id = lg4ff_main_checklist[i]->real_product_id;
1201 
1202         if ((current_mode & lg4ff_main_checklist[i]->modes) && \
1203                 (bcdDevice & mask) == result) {
1204             dbg_hid("Found wheel with real PID %X whose reported PID is %X\n", real_product_id, reported_product_id);
1205             return real_product_id;
1206         }
1207     }
1208 
1209     /* No match found. This is either Driving Force or an unknown
1210      * wheel model, do not touch it */
1211     dbg_hid("Wheel with bcdDevice %X was not recognized as multimode wheel, leaving in its current mode\n", bcdDevice);
1212     return 0;
1213 }
1214 
1215 static int lg4ff_handle_multimode_wheel(struct hid_device *hid, u16 *real_product_id, const u16 bcdDevice)
1216 {
1217     const u16 reported_product_id = hid->product;
1218     int ret;
1219 
1220     *real_product_id = lg4ff_identify_multimode_wheel(hid, reported_product_id, bcdDevice);
1221     /* Probed wheel is not a multimode wheel */
1222     if (!*real_product_id) {
1223         *real_product_id = reported_product_id;
1224         dbg_hid("Wheel is not a multimode wheel\n");
1225         return LG4FF_MMODE_NOT_MULTIMODE;
1226     }
1227 
1228     /* Switch from "Driving Force" mode to native mode automatically.
1229      * Otherwise keep the wheel in its current mode */
1230     if (reported_product_id == USB_DEVICE_ID_LOGITECH_WHEEL &&
1231         reported_product_id != *real_product_id &&
1232         !lg4ff_no_autoswitch) {
1233         const struct lg4ff_compat_mode_switch *s = lg4ff_get_mode_switch_command(*real_product_id, *real_product_id);
1234 
1235         if (!s) {
1236             hid_err(hid, "Invalid product id %X\n", *real_product_id);
1237             return LG4FF_MMODE_NOT_MULTIMODE;
1238         }
1239 
1240         ret = lg4ff_switch_compatibility_mode(hid, s);
1241         if (ret) {
1242             /* Wheel could not have been switched to native mode,
1243              * leave it in "Driving Force" mode and continue */
1244             hid_err(hid, "Unable to switch wheel mode, errno %d\n", ret);
1245             return LG4FF_MMODE_IS_MULTIMODE;
1246         }
1247         return LG4FF_MMODE_SWITCHED;
1248     }
1249 
1250     return LG4FF_MMODE_IS_MULTIMODE;
1251 }
1252 
1253 
1254 int lg4ff_init(struct hid_device *hid)
1255 {
1256     struct hid_input *hidinput;
1257     struct input_dev *dev;
1258     struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
1259     struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
1260     const struct usb_device_descriptor *udesc = &(hid_to_usb_dev(hid)->descriptor);
1261     const u16 bcdDevice = le16_to_cpu(udesc->bcdDevice);
1262     const struct lg4ff_multimode_wheel *mmode_wheel = NULL;
1263     struct lg4ff_device_entry *entry;
1264     struct lg_drv_data *drv_data;
1265     int error, i, j;
1266     int mmode_ret, mmode_idx = -1;
1267     u16 real_product_id;
1268 
1269     if (list_empty(&hid->inputs)) {
1270         hid_err(hid, "no inputs found\n");
1271         return -ENODEV;
1272     }
1273     hidinput = list_entry(hid->inputs.next, struct hid_input, list);
1274     dev = hidinput->input;
1275 
1276     /* Check that the report looks ok */
1277     if (!hid_validate_values(hid, HID_OUTPUT_REPORT, 0, 0, 7))
1278         return -1;
1279 
1280     drv_data = hid_get_drvdata(hid);
1281     if (!drv_data) {
1282         hid_err(hid, "Cannot add device, private driver data not allocated\n");
1283         return -1;
1284     }
1285     entry = kzalloc(sizeof(*entry), GFP_KERNEL);
1286     if (!entry)
1287         return -ENOMEM;
1288     spin_lock_init(&entry->report_lock);
1289     entry->report = report;
1290     drv_data->device_props = entry;
1291 
1292     /* Check if a multimode wheel has been connected and
1293      * handle it appropriately */
1294     mmode_ret = lg4ff_handle_multimode_wheel(hid, &real_product_id, bcdDevice);
1295 
1296     /* Wheel has been told to switch to native mode. There is no point in going on
1297      * with the initialization as the wheel will do a USB reset when it switches mode
1298      */
1299     if (mmode_ret == LG4FF_MMODE_SWITCHED)
1300         return 0;
1301     else if (mmode_ret < 0) {
1302         hid_err(hid, "Unable to switch device mode during initialization, errno %d\n", mmode_ret);
1303         error = mmode_ret;
1304         goto err_init;
1305     }
1306 
1307     /* Check what wheel has been connected */
1308     for (i = 0; i < ARRAY_SIZE(lg4ff_devices); i++) {
1309         if (hid->product == lg4ff_devices[i].product_id) {
1310             dbg_hid("Found compatible device, product ID %04X\n", lg4ff_devices[i].product_id);
1311             break;
1312         }
1313     }
1314 
1315     if (i == ARRAY_SIZE(lg4ff_devices)) {
1316         hid_err(hid, "This device is flagged to be handled by the lg4ff module but this module does not know how to handle it. "
1317                  "Please report this as a bug to LKML, Simon Wood <simon@mungewell.org> or "
1318                  "Michal Maly <madcatxster@devoid-pointer.net>\n");
1319         error = -1;
1320         goto err_init;
1321     }
1322 
1323     if (mmode_ret == LG4FF_MMODE_IS_MULTIMODE) {
1324         for (mmode_idx = 0; mmode_idx < ARRAY_SIZE(lg4ff_multimode_wheels); mmode_idx++) {
1325             if (real_product_id == lg4ff_multimode_wheels[mmode_idx].product_id)
1326                 break;
1327         }
1328 
1329         if (mmode_idx == ARRAY_SIZE(lg4ff_multimode_wheels)) {
1330             hid_err(hid, "Device product ID %X is not listed as a multimode wheel", real_product_id);
1331             error = -1;
1332             goto err_init;
1333         }
1334     }
1335 
1336     /* Set supported force feedback capabilities */
1337     for (j = 0; lg4ff_devices[i].ff_effects[j] >= 0; j++)
1338         set_bit(lg4ff_devices[i].ff_effects[j], dev->ffbit);
1339 
1340     error = input_ff_create_memless(dev, NULL, lg4ff_play);
1341 
1342     if (error)
1343         goto err_init;
1344 
1345     /* Initialize device properties */
1346     if (mmode_ret == LG4FF_MMODE_IS_MULTIMODE) {
1347         BUG_ON(mmode_idx == -1);
1348         mmode_wheel = &lg4ff_multimode_wheels[mmode_idx];
1349     }
1350     lg4ff_init_wheel_data(&entry->wdata, &lg4ff_devices[i], mmode_wheel, real_product_id);
1351 
1352     /* Check if autocentering is available and
1353      * set the centering force to zero by default */
1354     if (test_bit(FF_AUTOCENTER, dev->ffbit)) {
1355         /* Formula Force EX expects different autocentering command */
1356         if ((bcdDevice >> 8) == LG4FF_FFEX_REV_MAJ &&
1357             (bcdDevice & 0xff) == LG4FF_FFEX_REV_MIN)
1358             dev->ff->set_autocenter = lg4ff_set_autocenter_ffex;
1359         else
1360             dev->ff->set_autocenter = lg4ff_set_autocenter_default;
1361 
1362         dev->ff->set_autocenter(dev, 0);
1363     }
1364 
1365     /* Create sysfs interface */
1366     error = device_create_file(&hid->dev, &dev_attr_combine_pedals);
1367     if (error)
1368         hid_warn(hid, "Unable to create sysfs interface for \"combine\", errno %d\n", error);
1369     error = device_create_file(&hid->dev, &dev_attr_range);
1370     if (error)
1371         hid_warn(hid, "Unable to create sysfs interface for \"range\", errno %d\n", error);
1372     if (mmode_ret == LG4FF_MMODE_IS_MULTIMODE) {
1373         error = device_create_file(&hid->dev, &dev_attr_real_id);
1374         if (error)
1375             hid_warn(hid, "Unable to create sysfs interface for \"real_id\", errno %d\n", error);
1376         error = device_create_file(&hid->dev, &dev_attr_alternate_modes);
1377         if (error)
1378             hid_warn(hid, "Unable to create sysfs interface for \"alternate_modes\", errno %d\n", error);
1379     }
1380     dbg_hid("sysfs interface created\n");
1381 
1382     /* Set the maximum range to start with */
1383     entry->wdata.range = entry->wdata.max_range;
1384     if (entry->wdata.set_range)
1385         entry->wdata.set_range(hid, entry->wdata.range);
1386 
1387 #ifdef CONFIG_LEDS_CLASS
1388     /* register led subsystem - G27/G29 only */
1389     entry->wdata.led_state = 0;
1390     for (j = 0; j < 5; j++)
1391         entry->wdata.led[j] = NULL;
1392 
1393     if (lg4ff_devices[i].product_id == USB_DEVICE_ID_LOGITECH_G27_WHEEL ||
1394             lg4ff_devices[i].product_id == USB_DEVICE_ID_LOGITECH_G29_WHEEL) {
1395         struct led_classdev *led;
1396         size_t name_sz;
1397         char *name;
1398 
1399         lg4ff_set_leds(hid, 0);
1400 
1401         name_sz = strlen(dev_name(&hid->dev)) + 8;
1402 
1403         for (j = 0; j < 5; j++) {
1404             led = kzalloc(sizeof(struct led_classdev)+name_sz, GFP_KERNEL);
1405             if (!led) {
1406                 hid_err(hid, "can't allocate memory for LED %d\n", j);
1407                 goto err_leds;
1408             }
1409 
1410             name = (void *)(&led[1]);
1411             snprintf(name, name_sz, "%s::RPM%d", dev_name(&hid->dev), j+1);
1412             led->name = name;
1413             led->brightness = 0;
1414             led->max_brightness = 1;
1415             led->brightness_get = lg4ff_led_get_brightness;
1416             led->brightness_set = lg4ff_led_set_brightness;
1417 
1418             entry->wdata.led[j] = led;
1419             error = led_classdev_register(&hid->dev, led);
1420 
1421             if (error) {
1422                 hid_err(hid, "failed to register LED %d. Aborting.\n", j);
1423 err_leds:
1424                 /* Deregister LEDs (if any) */
1425                 for (j = 0; j < 5; j++) {
1426                     led = entry->wdata.led[j];
1427                     entry->wdata.led[j] = NULL;
1428                     if (!led)
1429                         continue;
1430                     led_classdev_unregister(led);
1431                     kfree(led);
1432                 }
1433                 goto out;   /* Let the driver continue without LEDs */
1434             }
1435         }
1436     }
1437 out:
1438 #endif
1439     hid_info(hid, "Force feedback support for Logitech Gaming Wheels\n");
1440     return 0;
1441 
1442 err_init:
1443     drv_data->device_props = NULL;
1444     kfree(entry);
1445     return error;
1446 }
1447 
1448 int lg4ff_deinit(struct hid_device *hid)
1449 {
1450     struct lg4ff_device_entry *entry;
1451     struct lg_drv_data *drv_data;
1452 
1453     drv_data = hid_get_drvdata(hid);
1454     if (!drv_data) {
1455         hid_err(hid, "Error while deinitializing device, no private driver data.\n");
1456         return -1;
1457     }
1458     entry = drv_data->device_props;
1459     if (!entry)
1460         goto out; /* Nothing more to do */
1461 
1462     /* Multimode devices will have at least the "MODE_NATIVE" bit set */
1463     if (entry->wdata.alternate_modes) {
1464         device_remove_file(&hid->dev, &dev_attr_real_id);
1465         device_remove_file(&hid->dev, &dev_attr_alternate_modes);
1466     }
1467 
1468     device_remove_file(&hid->dev, &dev_attr_combine_pedals);
1469     device_remove_file(&hid->dev, &dev_attr_range);
1470 #ifdef CONFIG_LEDS_CLASS
1471     {
1472         int j;
1473         struct led_classdev *led;
1474 
1475         /* Deregister LEDs (if any) */
1476         for (j = 0; j < 5; j++) {
1477 
1478             led = entry->wdata.led[j];
1479             entry->wdata.led[j] = NULL;
1480             if (!led)
1481                 continue;
1482             led_classdev_unregister(led);
1483             kfree(led);
1484         }
1485     }
1486 #endif
1487     drv_data->device_props = NULL;
1488 
1489     kfree(entry);
1490 out:
1491     dbg_hid("Device successfully unregistered\n");
1492     return 0;
1493 }