132c88cbcSSimon Wood /* 264013800SSimon Wood * Force feedback support for Logitech Gaming Wheels 332c88cbcSSimon Wood * 464013800SSimon Wood * Including G27, G25, DFP, DFGT, FFEX, Momo, Momo2 & 564013800SSimon Wood * Speed Force Wireless (WiiWheel) 632c88cbcSSimon Wood * 732c88cbcSSimon Wood * Copyright (c) 2010 Simon Wood <simon@mungewell.org> 832c88cbcSSimon Wood */ 932c88cbcSSimon Wood 1032c88cbcSSimon Wood /* 1132c88cbcSSimon Wood * This program is free software; you can redistribute it and/or modify 1232c88cbcSSimon Wood * it under the terms of the GNU General Public License as published by 1332c88cbcSSimon Wood * the Free Software Foundation; either version 2 of the License, or 1432c88cbcSSimon Wood * (at your option) any later version. 1532c88cbcSSimon Wood * 1632c88cbcSSimon Wood * This program is distributed in the hope that it will be useful, 1732c88cbcSSimon Wood * but WITHOUT ANY WARRANTY; without even the implied warranty of 1832c88cbcSSimon Wood * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1932c88cbcSSimon Wood * GNU General Public License for more details. 2032c88cbcSSimon Wood * 2132c88cbcSSimon Wood * You should have received a copy of the GNU General Public License 2232c88cbcSSimon Wood * along with this program; if not, write to the Free Software 2332c88cbcSSimon Wood * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 2432c88cbcSSimon Wood */ 2532c88cbcSSimon Wood 2632c88cbcSSimon Wood 2732c88cbcSSimon Wood #include <linux/input.h> 2832c88cbcSSimon Wood #include <linux/usb.h> 2932c88cbcSSimon Wood #include <linux/hid.h> 3032c88cbcSSimon Wood 3132c88cbcSSimon Wood #include "usbhid/usbhid.h" 3232c88cbcSSimon Wood #include "hid-lg.h" 33a54dc779SMichal Malý #include "hid-lg4ff.h" 347362cd22SMichal Malý #include "hid-ids.h" 3532c88cbcSSimon Wood 3630bb75d7SMichal Malý #define to_hid_device(pdev) container_of(pdev, struct hid_device, dev) 3730bb75d7SMichal Malý 38b96d23ecSMichal Malý #define LG4FF_MMODE_IS_MULTIMODE 0 39e7c23449SMichal Malý #define LG4FF_MMODE_SWITCHED 1 40e7c23449SMichal Malý #define LG4FF_MMODE_NOT_MULTIMODE 2 41e7c23449SMichal Malý 42b96d23ecSMichal Malý #define LG4FF_MODE_NATIVE_IDX 0 43b96d23ecSMichal Malý #define LG4FF_MODE_DFEX_IDX 1 44b96d23ecSMichal Malý #define LG4FF_MODE_DFP_IDX 2 45b96d23ecSMichal Malý #define LG4FF_MODE_G25_IDX 3 46b96d23ecSMichal Malý #define LG4FF_MODE_DFGT_IDX 4 47b96d23ecSMichal Malý #define LG4FF_MODE_G27_IDX 5 48b96d23ecSMichal Malý #define LG4FF_MODE_MAX_IDX 6 49b96d23ecSMichal Malý 50b96d23ecSMichal Malý #define LG4FF_MODE_NATIVE BIT(LG4FF_MODE_NATIVE_IDX) 51b96d23ecSMichal Malý #define LG4FF_MODE_DFEX BIT(LG4FF_MODE_DFEX_IDX) 52b96d23ecSMichal Malý #define LG4FF_MODE_DFP BIT(LG4FF_MODE_DFP_IDX) 53b96d23ecSMichal Malý #define LG4FF_MODE_G25 BIT(LG4FF_MODE_G25_IDX) 54b96d23ecSMichal Malý #define LG4FF_MODE_DFGT BIT(LG4FF_MODE_DFGT_IDX) 55b96d23ecSMichal Malý #define LG4FF_MODE_G27 BIT(LG4FF_MODE_G27_IDX) 56b96d23ecSMichal Malý 57b96d23ecSMichal Malý #define LG4FF_DFEX_TAG "DF-EX" 58b96d23ecSMichal Malý #define LG4FF_DFEX_NAME "Driving Force / Formula EX" 59b96d23ecSMichal Malý #define LG4FF_DFP_TAG "DFP" 60b96d23ecSMichal Malý #define LG4FF_DFP_NAME "Driving Force Pro" 61b96d23ecSMichal Malý #define LG4FF_G25_TAG "G25" 62b96d23ecSMichal Malý #define LG4FF_G25_NAME "G25 Racing Wheel" 63b96d23ecSMichal Malý #define LG4FF_G27_TAG "G27" 64b96d23ecSMichal Malý #define LG4FF_G27_NAME "G27 Racing Wheel" 65b96d23ecSMichal Malý #define LG4FF_DFGT_TAG "DFGT" 66b96d23ecSMichal Malý #define LG4FF_DFGT_NAME "Driving Force GT" 67b96d23ecSMichal Malý 68e7c23449SMichal Malý #define LG4FF_FFEX_REV_MAJ 0x21 69e7c23449SMichal Malý #define LG4FF_FFEX_REV_MIN 0x00 70e7c23449SMichal Malý 71d0afd848SMichal Malý static void lg4ff_set_range_dfp(struct hid_device *hid, u16 range); 72d0afd848SMichal Malý static void lg4ff_set_range_g25(struct hid_device *hid, u16 range); 7330bb75d7SMichal Malý 7472529c65SMichal Malý struct lg4ff_wheel_data { 75*5d9d60adSMichal Malý const u32 product_id; 762a552c30SMichal Malý u16 range; 77*5d9d60adSMichal Malý const u16 min_range; 78*5d9d60adSMichal Malý const u16 max_range; 7922bcefdcSSimon Wood #ifdef CONFIG_LEDS_CLASS 802a552c30SMichal Malý u8 led_state; 8122bcefdcSSimon Wood struct led_classdev *led[5]; 8222bcefdcSSimon Wood #endif 83*5d9d60adSMichal Malý const u32 alternate_modes; 84*5d9d60adSMichal Malý const char * const real_tag; 85*5d9d60adSMichal Malý const char * const real_name; 86*5d9d60adSMichal Malý const u16 real_product_id; 8772529c65SMichal Malý 8830bb75d7SMichal Malý void (*set_range)(struct hid_device *hid, u16 range); 8930bb75d7SMichal Malý }; 9030bb75d7SMichal Malý 9172529c65SMichal Malý struct lg4ff_device_entry { 92c918fe78SMichal Malý spinlock_t report_lock; /* Protect output HID report */ 93c28abd8cSMichal Malý struct hid_report *report; 9472529c65SMichal Malý struct lg4ff_wheel_data wdata; 9572529c65SMichal Malý }; 9672529c65SMichal Malý 977362cd22SMichal Malý static const signed short lg4ff_wheel_effects[] = { 9832c88cbcSSimon Wood FF_CONSTANT, 9932c88cbcSSimon Wood FF_AUTOCENTER, 10032c88cbcSSimon Wood -1 10132c88cbcSSimon Wood }; 10232c88cbcSSimon Wood 1037362cd22SMichal Malý struct lg4ff_wheel { 1042a552c30SMichal Malý const u32 product_id; 1057362cd22SMichal Malý const signed short *ff_effects; 1062a552c30SMichal Malý const u16 min_range; 1072a552c30SMichal Malý const u16 max_range; 10830bb75d7SMichal Malý void (*set_range)(struct hid_device *hid, u16 range); 1097362cd22SMichal Malý }; 1107362cd22SMichal Malý 111e7c23449SMichal Malý struct lg4ff_compat_mode_switch { 1122a552c30SMichal Malý const u8 cmd_count; /* Number of commands to send */ 1132a552c30SMichal Malý const u8 cmd[]; 114e7c23449SMichal Malý }; 115e7c23449SMichal Malý 116e7c23449SMichal Malý struct lg4ff_wheel_ident_info { 117e7c23449SMichal Malý const u16 mask; 118e7c23449SMichal Malý const u16 result; 119e7c23449SMichal Malý const u16 real_product_id; 120e7c23449SMichal Malý }; 121e7c23449SMichal Malý 122e7c23449SMichal Malý struct lg4ff_wheel_ident_checklist { 123e7c23449SMichal Malý const u32 count; 124e7c23449SMichal Malý const struct lg4ff_wheel_ident_info *models[]; 125e7c23449SMichal Malý }; 126e7c23449SMichal Malý 127b96d23ecSMichal Malý struct lg4ff_multimode_wheel { 128b96d23ecSMichal Malý const u16 product_id; 129b96d23ecSMichal Malý const u32 alternate_modes; 130b96d23ecSMichal Malý const char *real_tag; 131b96d23ecSMichal Malý const char *real_name; 132b96d23ecSMichal Malý }; 133b96d23ecSMichal Malý 134b96d23ecSMichal Malý struct lg4ff_alternate_mode { 135b96d23ecSMichal Malý const u16 product_id; 136b96d23ecSMichal Malý const char *tag; 137b96d23ecSMichal Malý const char *name; 138b96d23ecSMichal Malý }; 139b96d23ecSMichal Malý 1407362cd22SMichal Malý static const struct lg4ff_wheel lg4ff_devices[] = { 14130bb75d7SMichal Malý {USB_DEVICE_ID_LOGITECH_WHEEL, lg4ff_wheel_effects, 40, 270, NULL}, 14230bb75d7SMichal Malý {USB_DEVICE_ID_LOGITECH_MOMO_WHEEL, lg4ff_wheel_effects, 40, 270, NULL}, 143d0afd848SMichal Malý {USB_DEVICE_ID_LOGITECH_DFP_WHEEL, lg4ff_wheel_effects, 40, 900, lg4ff_set_range_dfp}, 144d0afd848SMichal Malý {USB_DEVICE_ID_LOGITECH_G25_WHEEL, lg4ff_wheel_effects, 40, 900, lg4ff_set_range_g25}, 145d0afd848SMichal Malý {USB_DEVICE_ID_LOGITECH_DFGT_WHEEL, lg4ff_wheel_effects, 40, 900, lg4ff_set_range_g25}, 146d0afd848SMichal Malý {USB_DEVICE_ID_LOGITECH_G27_WHEEL, lg4ff_wheel_effects, 40, 900, lg4ff_set_range_g25}, 14730bb75d7SMichal Malý {USB_DEVICE_ID_LOGITECH_MOMO_WHEEL2, lg4ff_wheel_effects, 40, 270, NULL}, 14830bb75d7SMichal Malý {USB_DEVICE_ID_LOGITECH_WII_WHEEL, lg4ff_wheel_effects, 40, 270, NULL} 1497362cd22SMichal Malý }; 1507362cd22SMichal Malý 151b96d23ecSMichal Malý static const struct lg4ff_multimode_wheel lg4ff_multimode_wheels[] = { 152b96d23ecSMichal Malý {USB_DEVICE_ID_LOGITECH_DFP_WHEEL, 153b96d23ecSMichal Malý LG4FF_MODE_NATIVE | LG4FF_MODE_DFP | LG4FF_MODE_DFEX, 154b96d23ecSMichal Malý LG4FF_DFP_TAG, LG4FF_DFP_NAME}, 155b96d23ecSMichal Malý {USB_DEVICE_ID_LOGITECH_G25_WHEEL, 156b96d23ecSMichal Malý LG4FF_MODE_NATIVE | LG4FF_MODE_G25 | LG4FF_MODE_DFP | LG4FF_MODE_DFEX, 157b96d23ecSMichal Malý LG4FF_G25_TAG, LG4FF_G25_NAME}, 158b96d23ecSMichal Malý {USB_DEVICE_ID_LOGITECH_DFGT_WHEEL, 159b96d23ecSMichal Malý LG4FF_MODE_NATIVE | LG4FF_MODE_DFGT | LG4FF_MODE_DFP | LG4FF_MODE_DFEX, 160b96d23ecSMichal Malý LG4FF_DFGT_TAG, LG4FF_DFGT_NAME}, 161b96d23ecSMichal Malý {USB_DEVICE_ID_LOGITECH_G27_WHEEL, 162b96d23ecSMichal Malý LG4FF_MODE_NATIVE | LG4FF_MODE_G27 | LG4FF_MODE_G25 | LG4FF_MODE_DFP | LG4FF_MODE_DFEX, 163b96d23ecSMichal Malý LG4FF_G27_TAG, LG4FF_G27_NAME}, 164b96d23ecSMichal Malý }; 165b96d23ecSMichal Malý 166b96d23ecSMichal Malý static const struct lg4ff_alternate_mode lg4ff_alternate_modes[] = { 167b96d23ecSMichal Malý [LG4FF_MODE_NATIVE_IDX] = {0, "native", ""}, 168b96d23ecSMichal Malý [LG4FF_MODE_DFEX_IDX] = {USB_DEVICE_ID_LOGITECH_WHEEL, LG4FF_DFEX_TAG, LG4FF_DFEX_NAME}, 169b96d23ecSMichal Malý [LG4FF_MODE_DFP_IDX] = {USB_DEVICE_ID_LOGITECH_DFP_WHEEL, LG4FF_DFP_TAG, LG4FF_DFP_NAME}, 170b96d23ecSMichal Malý [LG4FF_MODE_G25_IDX] = {USB_DEVICE_ID_LOGITECH_G25_WHEEL, LG4FF_G25_TAG, LG4FF_G25_NAME}, 171b96d23ecSMichal Malý [LG4FF_MODE_DFGT_IDX] = {USB_DEVICE_ID_LOGITECH_DFGT_WHEEL, LG4FF_DFGT_TAG, LG4FF_DFGT_NAME}, 172b96d23ecSMichal Malý [LG4FF_MODE_G27_IDX] = {USB_DEVICE_ID_LOGITECH_G27_WHEEL, LG4FF_G27_TAG, LG4FF_G27_NAME} 173b96d23ecSMichal Malý }; 174b96d23ecSMichal Malý 175e7c23449SMichal Malý /* Multimode wheel identificators */ 176e7c23449SMichal Malý static const struct lg4ff_wheel_ident_info lg4ff_dfp_ident_info = { 177e7c23449SMichal Malý 0xf000, 178e7c23449SMichal Malý 0x1000, 179e7c23449SMichal Malý USB_DEVICE_ID_LOGITECH_DFP_WHEEL 18096440c8aSMichal Malý }; 18196440c8aSMichal Malý 182e7c23449SMichal Malý static const struct lg4ff_wheel_ident_info lg4ff_g25_ident_info = { 183e7c23449SMichal Malý 0xff00, 184e7c23449SMichal Malý 0x1200, 185e7c23449SMichal Malý USB_DEVICE_ID_LOGITECH_G25_WHEEL 18696440c8aSMichal Malý }; 18796440c8aSMichal Malý 188e7c23449SMichal Malý static const struct lg4ff_wheel_ident_info lg4ff_g27_ident_info = { 189e7c23449SMichal Malý 0xfff0, 190e7c23449SMichal Malý 0x1230, 191e7c23449SMichal Malý USB_DEVICE_ID_LOGITECH_G27_WHEEL 192e7c23449SMichal Malý }; 193e7c23449SMichal Malý 194e7c23449SMichal Malý static const struct lg4ff_wheel_ident_info lg4ff_dfgt_ident_info = { 195e7c23449SMichal Malý 0xff00, 196e7c23449SMichal Malý 0x1300, 197e7c23449SMichal Malý USB_DEVICE_ID_LOGITECH_DFGT_WHEEL 198e7c23449SMichal Malý }; 199e7c23449SMichal Malý 200e7c23449SMichal Malý /* Multimode wheel identification checklists */ 201e7c23449SMichal Malý static const struct lg4ff_wheel_ident_checklist lg4ff_main_checklist = { 202e7c23449SMichal Malý 4, 203e7c23449SMichal Malý {&lg4ff_dfgt_ident_info, 204e7c23449SMichal Malý &lg4ff_g27_ident_info, 205e7c23449SMichal Malý &lg4ff_g25_ident_info, 206e7c23449SMichal Malý &lg4ff_dfp_ident_info} 207e7c23449SMichal Malý }; 208e7c23449SMichal Malý 209e7c23449SMichal Malý /* Compatibility mode switching commands */ 210f31a2de3SMichal Malý /* EXT_CMD9 - Understood by G27 and DFGT */ 211f31a2de3SMichal Malý static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext09_dfex = { 212f31a2de3SMichal Malý 2, 213f31a2de3SMichal Malý {0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, /* Revert mode upon USB reset */ 214f31a2de3SMichal Malý 0xf8, 0x09, 0x00, 0x01, 0x00, 0x00, 0x00} /* Switch mode to DF-EX with detach */ 215f31a2de3SMichal Malý }; 216f31a2de3SMichal Malý 217f31a2de3SMichal Malý static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext09_dfp = { 218f31a2de3SMichal Malý 2, 219f31a2de3SMichal Malý {0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, /* Revert mode upon USB reset */ 220f31a2de3SMichal Malý 0xf8, 0x09, 0x01, 0x01, 0x00, 0x00, 0x00} /* Switch mode to DFP with detach */ 221f31a2de3SMichal Malý }; 222f31a2de3SMichal Malý 223f31a2de3SMichal Malý static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext09_g25 = { 224f31a2de3SMichal Malý 2, 225f31a2de3SMichal Malý {0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, /* Revert mode upon USB reset */ 226f31a2de3SMichal Malý 0xf8, 0x09, 0x02, 0x01, 0x00, 0x00, 0x00} /* Switch mode to G25 with detach */ 227f31a2de3SMichal Malý }; 228f31a2de3SMichal Malý 229f31a2de3SMichal Malý static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext09_dfgt = { 230f31a2de3SMichal Malý 2, 231f31a2de3SMichal Malý {0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, /* Revert mode upon USB reset */ 232f31a2de3SMichal Malý 0xf8, 0x09, 0x03, 0x01, 0x00, 0x00, 0x00} /* Switch mode to DFGT with detach */ 233f31a2de3SMichal Malý }; 234f31a2de3SMichal Malý 235f31a2de3SMichal Malý static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext09_g27 = { 236f31a2de3SMichal Malý 2, 237f31a2de3SMichal Malý {0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, /* Revert mode upon USB reset */ 238f31a2de3SMichal Malý 0xf8, 0x09, 0x04, 0x01, 0x00, 0x00, 0x00} /* Switch mode to G27 with detach */ 239f31a2de3SMichal Malý }; 240f31a2de3SMichal Malý 241f31a2de3SMichal Malý /* EXT_CMD1 - Understood by DFP, G25, G27 and DFGT */ 242f31a2de3SMichal Malý static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext01_dfp = { 24396440c8aSMichal Malý 1, 24496440c8aSMichal Malý {0xf8, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00} 24596440c8aSMichal Malý }; 24696440c8aSMichal Malý 247f31a2de3SMichal Malý /* EXT_CMD16 - Understood by G25 and G27 */ 248f31a2de3SMichal Malý static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext16_g25 = { 24996440c8aSMichal Malý 1, 25096440c8aSMichal Malý {0xf8, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00} 25196440c8aSMichal Malý }; 25296440c8aSMichal Malý 2532b24a960SMichal Malý /* Recalculates X axis value accordingly to currently selected range */ 2542a552c30SMichal Malý static s32 lg4ff_adjust_dfp_x_axis(s32 value, u16 range) 2552b24a960SMichal Malý { 2562a552c30SMichal Malý u16 max_range; 2572a552c30SMichal Malý s32 new_value; 2582b24a960SMichal Malý 2592b24a960SMichal Malý if (range == 900) 2602b24a960SMichal Malý return value; 2612b24a960SMichal Malý else if (range == 200) 2622b24a960SMichal Malý return value; 2632b24a960SMichal Malý else if (range < 200) 2642b24a960SMichal Malý max_range = 200; 2652b24a960SMichal Malý else 2662b24a960SMichal Malý max_range = 900; 2672b24a960SMichal Malý 2682b24a960SMichal Malý new_value = 8192 + mult_frac(value - 8192, max_range, range); 2692b24a960SMichal Malý if (new_value < 0) 2702b24a960SMichal Malý return 0; 2712b24a960SMichal Malý else if (new_value > 16383) 2722b24a960SMichal Malý return 16383; 2732b24a960SMichal Malý else 2742b24a960SMichal Malý return new_value; 2752b24a960SMichal Malý } 2762b24a960SMichal Malý 2772b24a960SMichal Malý int lg4ff_adjust_input_event(struct hid_device *hid, struct hid_field *field, 2782a552c30SMichal Malý struct hid_usage *usage, s32 value, struct lg_drv_data *drv_data) 2792b24a960SMichal Malý { 2802b24a960SMichal Malý struct lg4ff_device_entry *entry = drv_data->device_props; 2812a552c30SMichal Malý s32 new_value = 0; 2822b24a960SMichal Malý 2832b24a960SMichal Malý if (!entry) { 2842b24a960SMichal Malý hid_err(hid, "Device properties not found"); 2852b24a960SMichal Malý return 0; 2862b24a960SMichal Malý } 2872b24a960SMichal Malý 28872529c65SMichal Malý switch (entry->wdata.product_id) { 2892b24a960SMichal Malý case USB_DEVICE_ID_LOGITECH_DFP_WHEEL: 2902b24a960SMichal Malý switch (usage->code) { 2912b24a960SMichal Malý case ABS_X: 29272529c65SMichal Malý new_value = lg4ff_adjust_dfp_x_axis(value, entry->wdata.range); 2932b24a960SMichal Malý input_event(field->hidinput->input, usage->type, usage->code, new_value); 2942b24a960SMichal Malý return 1; 2952b24a960SMichal Malý default: 2962b24a960SMichal Malý return 0; 2972b24a960SMichal Malý } 2982b24a960SMichal Malý default: 2992b24a960SMichal Malý return 0; 3002b24a960SMichal Malý } 3012b24a960SMichal Malý } 3022b24a960SMichal Malý 303*5d9d60adSMichal Malý static void lg4ff_init_wheel_data(struct lg4ff_wheel_data * const wdata, const struct lg4ff_wheel *wheel, 304*5d9d60adSMichal Malý const struct lg4ff_multimode_wheel *mmode_wheel, 305*5d9d60adSMichal Malý const u16 real_product_id) 306*5d9d60adSMichal Malý { 307*5d9d60adSMichal Malý u32 alternate_modes = 0; 308*5d9d60adSMichal Malý const char *real_tag = NULL; 309*5d9d60adSMichal Malý const char *real_name = NULL; 310*5d9d60adSMichal Malý 311*5d9d60adSMichal Malý if (mmode_wheel) { 312*5d9d60adSMichal Malý alternate_modes = mmode_wheel->alternate_modes; 313*5d9d60adSMichal Malý real_tag = mmode_wheel->real_tag; 314*5d9d60adSMichal Malý real_name = mmode_wheel->real_name; 315*5d9d60adSMichal Malý } 316*5d9d60adSMichal Malý 317*5d9d60adSMichal Malý { 318*5d9d60adSMichal Malý struct lg4ff_wheel_data t_wdata = { .product_id = wheel->product_id, 319*5d9d60adSMichal Malý .real_product_id = real_product_id, 320*5d9d60adSMichal Malý .min_range = wheel->min_range, 321*5d9d60adSMichal Malý .max_range = wheel->max_range, 322*5d9d60adSMichal Malý .set_range = wheel->set_range, 323*5d9d60adSMichal Malý .alternate_modes = alternate_modes, 324*5d9d60adSMichal Malý .real_tag = real_tag, 325*5d9d60adSMichal Malý .real_name = real_name }; 326*5d9d60adSMichal Malý 327*5d9d60adSMichal Malý memcpy(wdata, &t_wdata, sizeof(t_wdata)); 328*5d9d60adSMichal Malý } 329*5d9d60adSMichal Malý } 330*5d9d60adSMichal Malý 331d0afd848SMichal Malý static int lg4ff_play(struct input_dev *dev, void *data, struct ff_effect *effect) 33232c88cbcSSimon Wood { 33332c88cbcSSimon Wood struct hid_device *hid = input_get_drvdata(dev); 334c918fe78SMichal Malý struct lg4ff_device_entry *entry; 335c918fe78SMichal Malý struct lg_drv_data *drv_data; 336c918fe78SMichal Malý unsigned long flags; 337c28abd8cSMichal Malý s32 *value; 33832c88cbcSSimon Wood int x; 33932c88cbcSSimon Wood 340c918fe78SMichal Malý drv_data = hid_get_drvdata(hid); 341c918fe78SMichal Malý if (!drv_data) { 342c918fe78SMichal Malý hid_err(hid, "Private driver data not found!\n"); 343c918fe78SMichal Malý return -EINVAL; 344c918fe78SMichal Malý } 345c918fe78SMichal Malý 346c918fe78SMichal Malý entry = drv_data->device_props; 347c918fe78SMichal Malý if (!entry) { 348c918fe78SMichal Malý hid_err(hid, "Device properties not found!\n"); 349c918fe78SMichal Malý return -EINVAL; 350c918fe78SMichal Malý } 351c28abd8cSMichal Malý value = entry->report->field[0]->value; 352c918fe78SMichal Malý 353a80fe5d6SMichal Malý #define CLAMP(x) do { if (x < 0) x = 0; else if (x > 0xff) x = 0xff; } while (0) 35432c88cbcSSimon Wood 35532c88cbcSSimon Wood switch (effect->type) { 35632c88cbcSSimon Wood case FF_CONSTANT: 35732c88cbcSSimon Wood x = effect->u.ramp.start_level + 0x80; /* 0x80 is no force */ 35832c88cbcSSimon Wood CLAMP(x); 35956930e7aSSimon Wood 360c918fe78SMichal Malý spin_lock_irqsave(&entry->report_lock, flags); 36156930e7aSSimon Wood if (x == 0x80) { 36256930e7aSSimon Wood /* De-activate force in slot-1*/ 36356930e7aSSimon Wood value[0] = 0x13; 36456930e7aSSimon Wood value[1] = 0x00; 36556930e7aSSimon Wood value[2] = 0x00; 36656930e7aSSimon Wood value[3] = 0x00; 36756930e7aSSimon Wood value[4] = 0x00; 36856930e7aSSimon Wood value[5] = 0x00; 36956930e7aSSimon Wood value[6] = 0x00; 37056930e7aSSimon Wood 371c28abd8cSMichal Malý hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT); 372c918fe78SMichal Malý spin_unlock_irqrestore(&entry->report_lock, flags); 37356930e7aSSimon Wood return 0; 37456930e7aSSimon Wood } 37556930e7aSSimon Wood 37674479ba8SMichal Malý value[0] = 0x11; /* Slot 1 */ 37774479ba8SMichal Malý value[1] = 0x08; 37874479ba8SMichal Malý value[2] = x; 37974479ba8SMichal Malý value[3] = 0x80; 38074479ba8SMichal Malý value[4] = 0x00; 38174479ba8SMichal Malý value[5] = 0x00; 38274479ba8SMichal Malý value[6] = 0x00; 38332c88cbcSSimon Wood 384c28abd8cSMichal Malý hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT); 385c918fe78SMichal Malý spin_unlock_irqrestore(&entry->report_lock, flags); 38632c88cbcSSimon Wood break; 38732c88cbcSSimon Wood } 38832c88cbcSSimon Wood return 0; 38932c88cbcSSimon Wood } 39032c88cbcSSimon Wood 3916e2de8e0SMichal Malý /* Sends default autocentering command compatible with 3926e2de8e0SMichal Malý * all wheels except Formula Force EX */ 393d0afd848SMichal Malý static void lg4ff_set_autocenter_default(struct input_dev *dev, u16 magnitude) 39432c88cbcSSimon Wood { 39532c88cbcSSimon Wood struct hid_device *hid = input_get_drvdata(dev); 39632c88cbcSSimon Wood struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list; 39732c88cbcSSimon Wood struct hid_report *report = list_entry(report_list->next, struct hid_report, list); 3982a552c30SMichal Malý s32 *value = report->field[0]->value; 3992a552c30SMichal Malý u32 expand_a, expand_b; 4001859762eSSimon Wood struct lg4ff_device_entry *entry; 4011859762eSSimon Wood struct lg_drv_data *drv_data; 402c918fe78SMichal Malý unsigned long flags; 4031859762eSSimon Wood 4041859762eSSimon Wood drv_data = hid_get_drvdata(hid); 4051859762eSSimon Wood if (!drv_data) { 4061859762eSSimon Wood hid_err(hid, "Private driver data not found!\n"); 4071859762eSSimon Wood return; 4081859762eSSimon Wood } 4091859762eSSimon Wood 4101859762eSSimon Wood entry = drv_data->device_props; 4111859762eSSimon Wood if (!entry) { 4121859762eSSimon Wood hid_err(hid, "Device properties not found!\n"); 4131859762eSSimon Wood return; 4141859762eSSimon Wood } 415c28abd8cSMichal Malý value = entry->report->field[0]->value; 416f8c23156SSimon Wood 417d2c02da5SSimon Wood /* De-activate Auto-Center */ 418c918fe78SMichal Malý spin_lock_irqsave(&entry->report_lock, flags); 419d2c02da5SSimon Wood if (magnitude == 0) { 420d2c02da5SSimon Wood value[0] = 0xf5; 421d2c02da5SSimon Wood value[1] = 0x00; 422d2c02da5SSimon Wood value[2] = 0x00; 423d2c02da5SSimon Wood value[3] = 0x00; 424d2c02da5SSimon Wood value[4] = 0x00; 425d2c02da5SSimon Wood value[5] = 0x00; 426d2c02da5SSimon Wood value[6] = 0x00; 427d2c02da5SSimon Wood 428c28abd8cSMichal Malý hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT); 429c918fe78SMichal Malý spin_unlock_irqrestore(&entry->report_lock, flags); 430d2c02da5SSimon Wood return; 431d2c02da5SSimon Wood } 432d2c02da5SSimon Wood 433f8c23156SSimon Wood if (magnitude <= 0xaaaa) { 434f8c23156SSimon Wood expand_a = 0x0c * magnitude; 435f8c23156SSimon Wood expand_b = 0x80 * magnitude; 436f8c23156SSimon Wood } else { 437f8c23156SSimon Wood expand_a = (0x0c * 0xaaaa) + 0x06 * (magnitude - 0xaaaa); 438f8c23156SSimon Wood expand_b = (0x80 * 0xaaaa) + 0xff * (magnitude - 0xaaaa); 439f8c23156SSimon Wood } 44032c88cbcSSimon Wood 4411859762eSSimon Wood /* Adjust for non-MOMO wheels */ 44272529c65SMichal Malý switch (entry->wdata.product_id) { 4431859762eSSimon Wood case USB_DEVICE_ID_LOGITECH_MOMO_WHEEL: 4441859762eSSimon Wood case USB_DEVICE_ID_LOGITECH_MOMO_WHEEL2: 4451859762eSSimon Wood break; 4461859762eSSimon Wood default: 4471859762eSSimon Wood expand_a = expand_a >> 1; 4481859762eSSimon Wood break; 4491859762eSSimon Wood } 4501859762eSSimon Wood 45174479ba8SMichal Malý value[0] = 0xfe; 45274479ba8SMichal Malý value[1] = 0x0d; 453f8c23156SSimon Wood value[2] = expand_a / 0xaaaa; 454f8c23156SSimon Wood value[3] = expand_a / 0xaaaa; 455f8c23156SSimon Wood value[4] = expand_b / 0xaaaa; 45674479ba8SMichal Malý value[5] = 0x00; 45774479ba8SMichal Malý value[6] = 0x00; 45832c88cbcSSimon Wood 459c28abd8cSMichal Malý hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT); 460d2c02da5SSimon Wood 461d2c02da5SSimon Wood /* Activate Auto-Center */ 462d2c02da5SSimon Wood value[0] = 0x14; 463d2c02da5SSimon Wood value[1] = 0x00; 464d2c02da5SSimon Wood value[2] = 0x00; 465d2c02da5SSimon Wood value[3] = 0x00; 466d2c02da5SSimon Wood value[4] = 0x00; 467d2c02da5SSimon Wood value[5] = 0x00; 468d2c02da5SSimon Wood value[6] = 0x00; 469d2c02da5SSimon Wood 470c28abd8cSMichal Malý hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT); 471c918fe78SMichal Malý spin_unlock_irqrestore(&entry->report_lock, flags); 47232c88cbcSSimon Wood } 47332c88cbcSSimon Wood 4746e2de8e0SMichal Malý /* Sends autocentering command compatible with Formula Force EX */ 475d0afd848SMichal Malý static void lg4ff_set_autocenter_ffex(struct input_dev *dev, u16 magnitude) 4766e2de8e0SMichal Malý { 4776e2de8e0SMichal Malý struct hid_device *hid = input_get_drvdata(dev); 478c918fe78SMichal Malý struct lg4ff_device_entry *entry; 479c918fe78SMichal Malý struct lg_drv_data *drv_data; 480c918fe78SMichal Malý unsigned long flags; 481c28abd8cSMichal Malý s32 *value; 4826e2de8e0SMichal Malý magnitude = magnitude * 90 / 65535; 4836e2de8e0SMichal Malý 484c918fe78SMichal Malý drv_data = hid_get_drvdata(hid); 485c918fe78SMichal Malý if (!drv_data) { 486c918fe78SMichal Malý hid_err(hid, "Private driver data not found!\n"); 487c918fe78SMichal Malý return; 488c918fe78SMichal Malý } 489c918fe78SMichal Malý 490c918fe78SMichal Malý entry = drv_data->device_props; 491c918fe78SMichal Malý if (!entry) { 492c918fe78SMichal Malý hid_err(hid, "Device properties not found!\n"); 493c918fe78SMichal Malý return; 494c918fe78SMichal Malý } 495c28abd8cSMichal Malý value = entry->report->field[0]->value; 496c918fe78SMichal Malý 497c918fe78SMichal Malý spin_lock_irqsave(&entry->report_lock, flags); 49874479ba8SMichal Malý value[0] = 0xfe; 49974479ba8SMichal Malý value[1] = 0x03; 50074479ba8SMichal Malý value[2] = magnitude >> 14; 50174479ba8SMichal Malý value[3] = magnitude >> 14; 50274479ba8SMichal Malý value[4] = magnitude; 50374479ba8SMichal Malý value[5] = 0x00; 50474479ba8SMichal Malý value[6] = 0x00; 5056e2de8e0SMichal Malý 506c28abd8cSMichal Malý hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT); 507c918fe78SMichal Malý spin_unlock_irqrestore(&entry->report_lock, flags); 5086e2de8e0SMichal Malý } 5096e2de8e0SMichal Malý 51030bb75d7SMichal Malý /* Sends command to set range compatible with G25/G27/Driving Force GT */ 511d0afd848SMichal Malý static void lg4ff_set_range_g25(struct hid_device *hid, u16 range) 51230bb75d7SMichal Malý { 513c918fe78SMichal Malý struct lg4ff_device_entry *entry; 514c918fe78SMichal Malý struct lg_drv_data *drv_data; 515c918fe78SMichal Malý unsigned long flags; 516c28abd8cSMichal Malý s32 *value; 51774479ba8SMichal Malý 518c918fe78SMichal Malý drv_data = hid_get_drvdata(hid); 519c918fe78SMichal Malý if (!drv_data) { 520c918fe78SMichal Malý hid_err(hid, "Private driver data not found!\n"); 521c918fe78SMichal Malý return; 522c918fe78SMichal Malý } 523c918fe78SMichal Malý 524c918fe78SMichal Malý entry = drv_data->device_props; 525c918fe78SMichal Malý if (!entry) { 526c918fe78SMichal Malý hid_err(hid, "Device properties not found!\n"); 527c918fe78SMichal Malý return; 528c918fe78SMichal Malý } 529c28abd8cSMichal Malý value = entry->report->field[0]->value; 53030bb75d7SMichal Malý dbg_hid("G25/G27/DFGT: setting range to %u\n", range); 53130bb75d7SMichal Malý 532c918fe78SMichal Malý spin_lock_irqsave(&entry->report_lock, flags); 53374479ba8SMichal Malý value[0] = 0xf8; 53474479ba8SMichal Malý value[1] = 0x81; 53574479ba8SMichal Malý value[2] = range & 0x00ff; 53674479ba8SMichal Malý value[3] = (range & 0xff00) >> 8; 53774479ba8SMichal Malý value[4] = 0x00; 53874479ba8SMichal Malý value[5] = 0x00; 53974479ba8SMichal Malý value[6] = 0x00; 54030bb75d7SMichal Malý 541c28abd8cSMichal Malý hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT); 542c918fe78SMichal Malý spin_unlock_irqrestore(&entry->report_lock, flags); 54330bb75d7SMichal Malý } 54430bb75d7SMichal Malý 54530bb75d7SMichal Malý /* Sends commands to set range compatible with Driving Force Pro wheel */ 546d0afd848SMichal Malý static void lg4ff_set_range_dfp(struct hid_device *hid, u16 range) 54730bb75d7SMichal Malý { 548c918fe78SMichal Malý struct lg4ff_device_entry *entry; 549c918fe78SMichal Malý struct lg_drv_data *drv_data; 550c918fe78SMichal Malý unsigned long flags; 551c28abd8cSMichal Malý int start_left, start_right, full_range; 552c28abd8cSMichal Malý s32 *value; 55374479ba8SMichal Malý 554c918fe78SMichal Malý drv_data = hid_get_drvdata(hid); 555c918fe78SMichal Malý if (!drv_data) { 556c918fe78SMichal Malý hid_err(hid, "Private driver data not found!\n"); 557c918fe78SMichal Malý return; 558c918fe78SMichal Malý } 559c918fe78SMichal Malý 560c918fe78SMichal Malý entry = drv_data->device_props; 561c918fe78SMichal Malý if (!entry) { 562c918fe78SMichal Malý hid_err(hid, "Device properties not found!\n"); 563c918fe78SMichal Malý return; 564c918fe78SMichal Malý } 565c28abd8cSMichal Malý value = entry->report->field[0]->value; 56630bb75d7SMichal Malý dbg_hid("Driving Force Pro: setting range to %u\n", range); 56730bb75d7SMichal Malý 56830bb75d7SMichal Malý /* Prepare "coarse" limit command */ 569c918fe78SMichal Malý spin_lock_irqsave(&entry->report_lock, flags); 57074479ba8SMichal Malý value[0] = 0xf8; 57174479ba8SMichal Malý value[1] = 0x00; /* Set later */ 57274479ba8SMichal Malý value[2] = 0x00; 57374479ba8SMichal Malý value[3] = 0x00; 57474479ba8SMichal Malý value[4] = 0x00; 57574479ba8SMichal Malý value[5] = 0x00; 57674479ba8SMichal Malý value[6] = 0x00; 57730bb75d7SMichal Malý 57830bb75d7SMichal Malý if (range > 200) { 579c28abd8cSMichal Malý value[1] = 0x03; 58030bb75d7SMichal Malý full_range = 900; 58130bb75d7SMichal Malý } else { 582c28abd8cSMichal Malý value[1] = 0x02; 58330bb75d7SMichal Malý full_range = 200; 58430bb75d7SMichal Malý } 585c28abd8cSMichal Malý hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT); 58630bb75d7SMichal Malý 58730bb75d7SMichal Malý /* Prepare "fine" limit command */ 58874479ba8SMichal Malý value[0] = 0x81; 58974479ba8SMichal Malý value[1] = 0x0b; 59074479ba8SMichal Malý value[2] = 0x00; 59174479ba8SMichal Malý value[3] = 0x00; 59274479ba8SMichal Malý value[4] = 0x00; 59374479ba8SMichal Malý value[5] = 0x00; 59474479ba8SMichal Malý value[6] = 0x00; 59530bb75d7SMichal Malý 59630bb75d7SMichal Malý if (range == 200 || range == 900) { /* Do not apply any fine limit */ 597c28abd8cSMichal Malý hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT); 598c918fe78SMichal Malý spin_unlock_irqrestore(&entry->report_lock, flags); 59930bb75d7SMichal Malý return; 60030bb75d7SMichal Malý } 60130bb75d7SMichal Malý 60230bb75d7SMichal Malý /* Construct fine limit command */ 60330bb75d7SMichal Malý start_left = (((full_range - range + 1) * 2047) / full_range); 60430bb75d7SMichal Malý start_right = 0xfff - start_left; 60530bb75d7SMichal Malý 60674479ba8SMichal Malý value[2] = start_left >> 4; 60774479ba8SMichal Malý value[3] = start_right >> 4; 60874479ba8SMichal Malý value[4] = 0xff; 60974479ba8SMichal Malý value[5] = (start_right & 0xe) << 4 | (start_left & 0xe); 61074479ba8SMichal Malý value[6] = 0xff; 61130bb75d7SMichal Malý 612c28abd8cSMichal Malý hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT); 613c918fe78SMichal Malý spin_unlock_irqrestore(&entry->report_lock, flags); 61430bb75d7SMichal Malý } 61530bb75d7SMichal Malý 616f31a2de3SMichal Malý static const struct lg4ff_compat_mode_switch *lg4ff_get_mode_switch_command(const u16 real_product_id, const u16 target_product_id) 617f31a2de3SMichal Malý { 618f31a2de3SMichal Malý switch (real_product_id) { 619f31a2de3SMichal Malý case USB_DEVICE_ID_LOGITECH_DFP_WHEEL: 620f31a2de3SMichal Malý switch (target_product_id) { 621f31a2de3SMichal Malý case USB_DEVICE_ID_LOGITECH_DFP_WHEEL: 622f31a2de3SMichal Malý return &lg4ff_mode_switch_ext01_dfp; 623f31a2de3SMichal Malý /* DFP can only be switched to its native mode */ 624f31a2de3SMichal Malý default: 625f31a2de3SMichal Malý return NULL; 626f31a2de3SMichal Malý } 627f31a2de3SMichal Malý break; 628f31a2de3SMichal Malý case USB_DEVICE_ID_LOGITECH_G25_WHEEL: 629f31a2de3SMichal Malý switch (target_product_id) { 630f31a2de3SMichal Malý case USB_DEVICE_ID_LOGITECH_DFP_WHEEL: 631f31a2de3SMichal Malý return &lg4ff_mode_switch_ext01_dfp; 632f31a2de3SMichal Malý case USB_DEVICE_ID_LOGITECH_G25_WHEEL: 633f31a2de3SMichal Malý return &lg4ff_mode_switch_ext16_g25; 634f31a2de3SMichal Malý /* G25 can only be switched to DFP mode or its native mode */ 635f31a2de3SMichal Malý default: 636f31a2de3SMichal Malý return NULL; 637f31a2de3SMichal Malý } 638f31a2de3SMichal Malý break; 639f31a2de3SMichal Malý case USB_DEVICE_ID_LOGITECH_G27_WHEEL: 640f31a2de3SMichal Malý switch (target_product_id) { 641f31a2de3SMichal Malý case USB_DEVICE_ID_LOGITECH_WHEEL: 642f31a2de3SMichal Malý return &lg4ff_mode_switch_ext09_dfex; 643f31a2de3SMichal Malý case USB_DEVICE_ID_LOGITECH_DFP_WHEEL: 644f31a2de3SMichal Malý return &lg4ff_mode_switch_ext09_dfp; 645f31a2de3SMichal Malý case USB_DEVICE_ID_LOGITECH_G25_WHEEL: 646f31a2de3SMichal Malý return &lg4ff_mode_switch_ext09_g25; 647f31a2de3SMichal Malý case USB_DEVICE_ID_LOGITECH_G27_WHEEL: 648f31a2de3SMichal Malý return &lg4ff_mode_switch_ext09_g27; 649f31a2de3SMichal Malý /* G27 can only be switched to DF-EX, DFP, G25 or its native mode */ 650f31a2de3SMichal Malý default: 651f31a2de3SMichal Malý return NULL; 652f31a2de3SMichal Malý } 653f31a2de3SMichal Malý break; 654f31a2de3SMichal Malý case USB_DEVICE_ID_LOGITECH_DFGT_WHEEL: 655f31a2de3SMichal Malý switch (target_product_id) { 656f31a2de3SMichal Malý case USB_DEVICE_ID_LOGITECH_WHEEL: 657f31a2de3SMichal Malý return &lg4ff_mode_switch_ext09_dfex; 658f31a2de3SMichal Malý case USB_DEVICE_ID_LOGITECH_DFP_WHEEL: 659f31a2de3SMichal Malý return &lg4ff_mode_switch_ext09_dfp; 660f31a2de3SMichal Malý case USB_DEVICE_ID_LOGITECH_DFGT_WHEEL: 661f31a2de3SMichal Malý return &lg4ff_mode_switch_ext09_dfgt; 662f31a2de3SMichal Malý /* DFGT can only be switched to DF-EX, DFP or its native mode */ 663f31a2de3SMichal Malý default: 664f31a2de3SMichal Malý return NULL; 665f31a2de3SMichal Malý } 666f31a2de3SMichal Malý break; 667f31a2de3SMichal Malý /* No other wheels have multiple modes */ 668f31a2de3SMichal Malý default: 669f31a2de3SMichal Malý return NULL; 670f31a2de3SMichal Malý } 671f31a2de3SMichal Malý } 672f31a2de3SMichal Malý 673e7c23449SMichal Malý static int lg4ff_switch_compatibility_mode(struct hid_device *hid, const struct lg4ff_compat_mode_switch *s) 67496440c8aSMichal Malý { 675c918fe78SMichal Malý struct lg4ff_device_entry *entry; 676c918fe78SMichal Malý struct lg_drv_data *drv_data; 677c918fe78SMichal Malý unsigned long flags; 678c28abd8cSMichal Malý s32 *value; 679e7c23449SMichal Malý u8 i; 68096440c8aSMichal Malý 681c918fe78SMichal Malý drv_data = hid_get_drvdata(hid); 682c918fe78SMichal Malý if (!drv_data) { 683c918fe78SMichal Malý hid_err(hid, "Private driver data not found!\n"); 684c918fe78SMichal Malý return -EINVAL; 685c918fe78SMichal Malý } 686c918fe78SMichal Malý 687c918fe78SMichal Malý entry = drv_data->device_props; 688c918fe78SMichal Malý if (!entry) { 689c918fe78SMichal Malý hid_err(hid, "Device properties not found!\n"); 690c918fe78SMichal Malý return -EINVAL; 691c918fe78SMichal Malý } 692c28abd8cSMichal Malý value = entry->report->field[0]->value; 693c918fe78SMichal Malý 694c918fe78SMichal Malý spin_lock_irqsave(&entry->report_lock, flags); 695e7c23449SMichal Malý for (i = 0; i < s->cmd_count; i++) { 696c1740d13SMichal Malý u8 j; 69796440c8aSMichal Malý 698c1740d13SMichal Malý for (j = 0; j < 7; j++) 699c1740d13SMichal Malý value[j] = s->cmd[j + (7*i)]; 700c1740d13SMichal Malý 701c28abd8cSMichal Malý hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT); 70296440c8aSMichal Malý } 703c918fe78SMichal Malý spin_unlock_irqrestore(&entry->report_lock, flags); 704c1740d13SMichal Malý hid_hw_wait(hid); 705e7c23449SMichal Malý return 0; 70696440c8aSMichal Malý } 70732c88cbcSSimon Wood 708b96d23ecSMichal Malý static ssize_t lg4ff_alternate_modes_show(struct device *dev, struct device_attribute *attr, char *buf) 709b96d23ecSMichal Malý { 710b96d23ecSMichal Malý struct hid_device *hid = to_hid_device(dev); 711b96d23ecSMichal Malý struct lg4ff_device_entry *entry; 712b96d23ecSMichal Malý struct lg_drv_data *drv_data; 713b96d23ecSMichal Malý ssize_t count = 0; 714b96d23ecSMichal Malý int i; 715b96d23ecSMichal Malý 716b96d23ecSMichal Malý drv_data = hid_get_drvdata(hid); 717b96d23ecSMichal Malý if (!drv_data) { 718b96d23ecSMichal Malý hid_err(hid, "Private driver data not found!\n"); 719b96d23ecSMichal Malý return 0; 720b96d23ecSMichal Malý } 721b96d23ecSMichal Malý 722b96d23ecSMichal Malý entry = drv_data->device_props; 723b96d23ecSMichal Malý if (!entry) { 724b96d23ecSMichal Malý hid_err(hid, "Device properties not found!\n"); 725b96d23ecSMichal Malý return 0; 726b96d23ecSMichal Malý } 727b96d23ecSMichal Malý 72872529c65SMichal Malý if (!entry->wdata.real_name) { 729b96d23ecSMichal Malý hid_err(hid, "NULL pointer to string\n"); 730b96d23ecSMichal Malý return 0; 731b96d23ecSMichal Malý } 732b96d23ecSMichal Malý 733b96d23ecSMichal Malý for (i = 0; i < LG4FF_MODE_MAX_IDX; i++) { 73472529c65SMichal Malý if (entry->wdata.alternate_modes & BIT(i)) { 735b96d23ecSMichal Malý /* Print tag and full name */ 736b96d23ecSMichal Malý count += scnprintf(buf + count, PAGE_SIZE - count, "%s: %s", 737b96d23ecSMichal Malý lg4ff_alternate_modes[i].tag, 73872529c65SMichal Malý !lg4ff_alternate_modes[i].product_id ? entry->wdata.real_name : lg4ff_alternate_modes[i].name); 739b96d23ecSMichal Malý if (count >= PAGE_SIZE - 1) 740b96d23ecSMichal Malý return count; 741b96d23ecSMichal Malý 742b96d23ecSMichal Malý /* Mark the currently active mode with an asterisk */ 74372529c65SMichal Malý if (lg4ff_alternate_modes[i].product_id == entry->wdata.product_id || 74472529c65SMichal Malý (lg4ff_alternate_modes[i].product_id == 0 && entry->wdata.product_id == entry->wdata.real_product_id)) 745b96d23ecSMichal Malý count += scnprintf(buf + count, PAGE_SIZE - count, " *\n"); 746b96d23ecSMichal Malý else 747b96d23ecSMichal Malý count += scnprintf(buf + count, PAGE_SIZE - count, "\n"); 748b96d23ecSMichal Malý 749b96d23ecSMichal Malý if (count >= PAGE_SIZE - 1) 750b96d23ecSMichal Malý return count; 751b96d23ecSMichal Malý } 752b96d23ecSMichal Malý } 753b96d23ecSMichal Malý 754b96d23ecSMichal Malý return count; 755b96d23ecSMichal Malý } 756b96d23ecSMichal Malý 757b96d23ecSMichal Malý static ssize_t lg4ff_alternate_modes_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) 758b96d23ecSMichal Malý { 759f31a2de3SMichal Malý struct hid_device *hid = to_hid_device(dev); 760f31a2de3SMichal Malý struct lg4ff_device_entry *entry; 761f31a2de3SMichal Malý struct lg_drv_data *drv_data; 762f31a2de3SMichal Malý const struct lg4ff_compat_mode_switch *s; 763f31a2de3SMichal Malý u16 target_product_id = 0; 764f31a2de3SMichal Malý int i, ret; 765f31a2de3SMichal Malý char *lbuf; 766f31a2de3SMichal Malý 767f31a2de3SMichal Malý drv_data = hid_get_drvdata(hid); 768f31a2de3SMichal Malý if (!drv_data) { 769f31a2de3SMichal Malý hid_err(hid, "Private driver data not found!\n"); 770f31a2de3SMichal Malý return -EINVAL; 771f31a2de3SMichal Malý } 772f31a2de3SMichal Malý 773f31a2de3SMichal Malý entry = drv_data->device_props; 774f31a2de3SMichal Malý if (!entry) { 775f31a2de3SMichal Malý hid_err(hid, "Device properties not found!\n"); 776f31a2de3SMichal Malý return -EINVAL; 777f31a2de3SMichal Malý } 778f31a2de3SMichal Malý 779f31a2de3SMichal Malý /* Allow \n at the end of the input parameter */ 780f31a2de3SMichal Malý lbuf = kasprintf(GFP_KERNEL, "%s", buf); 781f31a2de3SMichal Malý if (!lbuf) 782f31a2de3SMichal Malý return -ENOMEM; 783f31a2de3SMichal Malý 784f31a2de3SMichal Malý i = strlen(lbuf); 785f31a2de3SMichal Malý if (lbuf[i-1] == '\n') { 786f31a2de3SMichal Malý if (i == 1) { 787f31a2de3SMichal Malý kfree(lbuf); 788f31a2de3SMichal Malý return -EINVAL; 789f31a2de3SMichal Malý } 790f31a2de3SMichal Malý lbuf[i-1] = '\0'; 791f31a2de3SMichal Malý } 792f31a2de3SMichal Malý 793f31a2de3SMichal Malý for (i = 0; i < LG4FF_MODE_MAX_IDX; i++) { 794f31a2de3SMichal Malý const u16 mode_product_id = lg4ff_alternate_modes[i].product_id; 795f31a2de3SMichal Malý const char *tag = lg4ff_alternate_modes[i].tag; 796f31a2de3SMichal Malý 79772529c65SMichal Malý if (entry->wdata.alternate_modes & BIT(i)) { 798f31a2de3SMichal Malý if (!strcmp(tag, lbuf)) { 799f31a2de3SMichal Malý if (!mode_product_id) 80072529c65SMichal Malý target_product_id = entry->wdata.real_product_id; 801f31a2de3SMichal Malý else 802f31a2de3SMichal Malý target_product_id = mode_product_id; 803f31a2de3SMichal Malý break; 804f31a2de3SMichal Malý } 805f31a2de3SMichal Malý } 806f31a2de3SMichal Malý } 807f31a2de3SMichal Malý 808f31a2de3SMichal Malý if (i == LG4FF_MODE_MAX_IDX) { 809f31a2de3SMichal Malý hid_info(hid, "Requested mode \"%s\" is not supported by the device\n", lbuf); 810f31a2de3SMichal Malý kfree(lbuf); 811f31a2de3SMichal Malý return -EINVAL; 812f31a2de3SMichal Malý } 813f31a2de3SMichal Malý kfree(lbuf); /* Not needed anymore */ 814f31a2de3SMichal Malý 81572529c65SMichal Malý if (target_product_id == entry->wdata.product_id) /* Nothing to do */ 816f31a2de3SMichal Malý return count; 817f31a2de3SMichal Malý 818f31a2de3SMichal Malý /* Automatic switching has to be disabled for the switch to DF-EX mode to work correctly */ 819f31a2de3SMichal Malý if (target_product_id == USB_DEVICE_ID_LOGITECH_WHEEL && !lg4ff_no_autoswitch) { 820f31a2de3SMichal Malý 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", 82172529c65SMichal Malý entry->wdata.real_name); 822f31a2de3SMichal Malý return -EINVAL; 823f31a2de3SMichal Malý } 824f31a2de3SMichal Malý 825f31a2de3SMichal Malý /* Take care of hardware limitations */ 82672529c65SMichal Malý if ((entry->wdata.real_product_id == USB_DEVICE_ID_LOGITECH_DFP_WHEEL || entry->wdata.real_product_id == USB_DEVICE_ID_LOGITECH_G25_WHEEL) && 82772529c65SMichal Malý entry->wdata.product_id > target_product_id) { 82872529c65SMichal Malý hid_info(hid, "\"%s\" cannot be switched back into \"%s\" mode\n", entry->wdata.real_name, lg4ff_alternate_modes[i].name); 829f31a2de3SMichal Malý return -EINVAL; 830f31a2de3SMichal Malý } 831f31a2de3SMichal Malý 83272529c65SMichal Malý s = lg4ff_get_mode_switch_command(entry->wdata.real_product_id, target_product_id); 833f31a2de3SMichal Malý if (!s) { 834f31a2de3SMichal Malý hid_err(hid, "Invalid target product ID %X\n", target_product_id); 835f31a2de3SMichal Malý return -EINVAL; 836f31a2de3SMichal Malý } 837f31a2de3SMichal Malý 838f31a2de3SMichal Malý ret = lg4ff_switch_compatibility_mode(hid, s); 839f31a2de3SMichal Malý return (ret == 0 ? count : ret); 840b96d23ecSMichal Malý } 841b96d23ecSMichal Malý static DEVICE_ATTR(alternate_modes, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH, lg4ff_alternate_modes_show, lg4ff_alternate_modes_store); 842b96d23ecSMichal Malý 843fbf85e2aSMichal Malý /* Export the currently set range of the wheel */ 844fbf85e2aSMichal Malý static ssize_t lg4ff_range_show(struct device *dev, struct device_attribute *attr, 8452f1cec32SVivien Didelot char *buf) 84630bb75d7SMichal Malý { 84730bb75d7SMichal Malý struct hid_device *hid = to_hid_device(dev); 8483b6b17b7SMichal Malý struct lg4ff_device_entry *entry; 8493b6b17b7SMichal Malý struct lg_drv_data *drv_data; 85030bb75d7SMichal Malý size_t count; 85130bb75d7SMichal Malý 8523b6b17b7SMichal Malý drv_data = hid_get_drvdata(hid); 8533b6b17b7SMichal Malý if (!drv_data) { 8543b6b17b7SMichal Malý hid_err(hid, "Private driver data not found!\n"); 8553b6b17b7SMichal Malý return 0; 85630bb75d7SMichal Malý } 8573b6b17b7SMichal Malý 8583b6b17b7SMichal Malý entry = drv_data->device_props; 8593b6b17b7SMichal Malý if (!entry) { 8603b6b17b7SMichal Malý hid_err(hid, "Device properties not found!\n"); 86130bb75d7SMichal Malý return 0; 86230bb75d7SMichal Malý } 86330bb75d7SMichal Malý 86472529c65SMichal Malý count = scnprintf(buf, PAGE_SIZE, "%u\n", entry->wdata.range); 86530bb75d7SMichal Malý return count; 86630bb75d7SMichal Malý } 86730bb75d7SMichal Malý 86830bb75d7SMichal Malý /* Set range to user specified value, call appropriate function 86930bb75d7SMichal Malý * according to the type of the wheel */ 870fbf85e2aSMichal Malý static ssize_t lg4ff_range_store(struct device *dev, struct device_attribute *attr, 8712f1cec32SVivien Didelot const char *buf, size_t count) 87230bb75d7SMichal Malý { 87330bb75d7SMichal Malý struct hid_device *hid = to_hid_device(dev); 8743b6b17b7SMichal Malý struct lg4ff_device_entry *entry; 8753b6b17b7SMichal Malý struct lg_drv_data *drv_data; 8762a552c30SMichal Malý u16 range = simple_strtoul(buf, NULL, 10); 87730bb75d7SMichal Malý 8783b6b17b7SMichal Malý drv_data = hid_get_drvdata(hid); 8793b6b17b7SMichal Malý if (!drv_data) { 8803b6b17b7SMichal Malý hid_err(hid, "Private driver data not found!\n"); 88129ff6657SSimon Wood return -EINVAL; 88230bb75d7SMichal Malý } 8833b6b17b7SMichal Malý 8843b6b17b7SMichal Malý entry = drv_data->device_props; 8853b6b17b7SMichal Malý if (!entry) { 8863b6b17b7SMichal Malý hid_err(hid, "Device properties not found!\n"); 88729ff6657SSimon Wood return -EINVAL; 88830bb75d7SMichal Malý } 88930bb75d7SMichal Malý 89030bb75d7SMichal Malý if (range == 0) 89172529c65SMichal Malý range = entry->wdata.max_range; 89230bb75d7SMichal Malý 89330bb75d7SMichal Malý /* Check if the wheel supports range setting 89430bb75d7SMichal Malý * and that the range is within limits for the wheel */ 89572529c65SMichal Malý if (entry->wdata.set_range && range >= entry->wdata.min_range && range <= entry->wdata.max_range) { 89672529c65SMichal Malý entry->wdata.set_range(hid, range); 89772529c65SMichal Malý entry->wdata.range = range; 89830bb75d7SMichal Malý } 89930bb75d7SMichal Malý 90030bb75d7SMichal Malý return count; 90130bb75d7SMichal Malý } 902fbf85e2aSMichal Malý static DEVICE_ATTR(range, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH, lg4ff_range_show, lg4ff_range_store); 90330bb75d7SMichal Malý 904b96d23ecSMichal Malý static ssize_t lg4ff_real_id_show(struct device *dev, struct device_attribute *attr, char *buf) 905b96d23ecSMichal Malý { 906b96d23ecSMichal Malý struct hid_device *hid = to_hid_device(dev); 907b96d23ecSMichal Malý struct lg4ff_device_entry *entry; 908b96d23ecSMichal Malý struct lg_drv_data *drv_data; 909b96d23ecSMichal Malý size_t count; 910b96d23ecSMichal Malý 911b96d23ecSMichal Malý drv_data = hid_get_drvdata(hid); 912b96d23ecSMichal Malý if (!drv_data) { 913b96d23ecSMichal Malý hid_err(hid, "Private driver data not found!\n"); 914b96d23ecSMichal Malý return 0; 915b96d23ecSMichal Malý } 916b96d23ecSMichal Malý 917b96d23ecSMichal Malý entry = drv_data->device_props; 918b96d23ecSMichal Malý if (!entry) { 919b96d23ecSMichal Malý hid_err(hid, "Device properties not found!\n"); 920b96d23ecSMichal Malý return 0; 921b96d23ecSMichal Malý } 922b96d23ecSMichal Malý 92372529c65SMichal Malý if (!entry->wdata.real_tag || !entry->wdata.real_name) { 924b96d23ecSMichal Malý hid_err(hid, "NULL pointer to string\n"); 925b96d23ecSMichal Malý return 0; 926b96d23ecSMichal Malý } 927b96d23ecSMichal Malý 92872529c65SMichal Malý count = scnprintf(buf, PAGE_SIZE, "%s: %s\n", entry->wdata.real_tag, entry->wdata.real_name); 929b96d23ecSMichal Malý return count; 930b96d23ecSMichal Malý } 931b96d23ecSMichal Malý 932b96d23ecSMichal Malý static ssize_t lg4ff_real_id_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) 933b96d23ecSMichal Malý { 934b96d23ecSMichal Malý /* Real ID is a read-only value */ 935b96d23ecSMichal Malý return -EPERM; 936b96d23ecSMichal Malý } 937b96d23ecSMichal Malý static DEVICE_ATTR(real_id, S_IRUGO, lg4ff_real_id_show, lg4ff_real_id_store); 938b96d23ecSMichal Malý 93922bcefdcSSimon Wood #ifdef CONFIG_LEDS_CLASS 9402a552c30SMichal Malý static void lg4ff_set_leds(struct hid_device *hid, u8 leds) 94122bcefdcSSimon Wood { 942c918fe78SMichal Malý struct lg_drv_data *drv_data; 943c918fe78SMichal Malý struct lg4ff_device_entry *entry; 944c918fe78SMichal Malý unsigned long flags; 945c28abd8cSMichal Malý s32 *value; 94622bcefdcSSimon Wood 947c918fe78SMichal Malý drv_data = hid_get_drvdata(hid); 948c918fe78SMichal Malý if (!drv_data) { 949c918fe78SMichal Malý hid_err(hid, "Private driver data not found!\n"); 950c918fe78SMichal Malý return; 951c918fe78SMichal Malý } 952c918fe78SMichal Malý 953c918fe78SMichal Malý entry = drv_data->device_props; 954c918fe78SMichal Malý if (!entry) { 955c918fe78SMichal Malý hid_err(hid, "Device properties not found!\n"); 956c918fe78SMichal Malý return; 957c918fe78SMichal Malý } 958c28abd8cSMichal Malý value = entry->report->field[0]->value; 959c918fe78SMichal Malý 960c918fe78SMichal Malý spin_lock_irqsave(&entry->report_lock, flags); 96174479ba8SMichal Malý value[0] = 0xf8; 96274479ba8SMichal Malý value[1] = 0x12; 96374479ba8SMichal Malý value[2] = leds; 96474479ba8SMichal Malý value[3] = 0x00; 96574479ba8SMichal Malý value[4] = 0x00; 96674479ba8SMichal Malý value[5] = 0x00; 96774479ba8SMichal Malý value[6] = 0x00; 968c28abd8cSMichal Malý hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT); 969c918fe78SMichal Malý spin_unlock_irqrestore(&entry->report_lock, flags); 97022bcefdcSSimon Wood } 97122bcefdcSSimon Wood 97222bcefdcSSimon Wood static void lg4ff_led_set_brightness(struct led_classdev *led_cdev, 97322bcefdcSSimon Wood enum led_brightness value) 97422bcefdcSSimon Wood { 97522bcefdcSSimon Wood struct device *dev = led_cdev->dev->parent; 97622bcefdcSSimon Wood struct hid_device *hid = container_of(dev, struct hid_device, dev); 9774629fd16SAxel Lin struct lg_drv_data *drv_data = hid_get_drvdata(hid); 97822bcefdcSSimon Wood struct lg4ff_device_entry *entry; 97922bcefdcSSimon Wood int i, state = 0; 98022bcefdcSSimon Wood 98122bcefdcSSimon Wood if (!drv_data) { 98222bcefdcSSimon Wood hid_err(hid, "Device data not found."); 98322bcefdcSSimon Wood return; 98422bcefdcSSimon Wood } 98522bcefdcSSimon Wood 986371a1d9eSMichal Malý entry = drv_data->device_props; 98722bcefdcSSimon Wood 98822bcefdcSSimon Wood if (!entry) { 98922bcefdcSSimon Wood hid_err(hid, "Device properties not found."); 99022bcefdcSSimon Wood return; 99122bcefdcSSimon Wood } 99222bcefdcSSimon Wood 99322bcefdcSSimon Wood for (i = 0; i < 5; i++) { 99472529c65SMichal Malý if (led_cdev != entry->wdata.led[i]) 99522bcefdcSSimon Wood continue; 99672529c65SMichal Malý state = (entry->wdata.led_state >> i) & 1; 99722bcefdcSSimon Wood if (value == LED_OFF && state) { 99872529c65SMichal Malý entry->wdata.led_state &= ~(1 << i); 99972529c65SMichal Malý lg4ff_set_leds(hid, entry->wdata.led_state); 100022bcefdcSSimon Wood } else if (value != LED_OFF && !state) { 100172529c65SMichal Malý entry->wdata.led_state |= 1 << i; 100272529c65SMichal Malý lg4ff_set_leds(hid, entry->wdata.led_state); 100322bcefdcSSimon Wood } 100422bcefdcSSimon Wood break; 100522bcefdcSSimon Wood } 100622bcefdcSSimon Wood } 100722bcefdcSSimon Wood 100822bcefdcSSimon Wood static enum led_brightness lg4ff_led_get_brightness(struct led_classdev *led_cdev) 100922bcefdcSSimon Wood { 101022bcefdcSSimon Wood struct device *dev = led_cdev->dev->parent; 101122bcefdcSSimon Wood struct hid_device *hid = container_of(dev, struct hid_device, dev); 10124629fd16SAxel Lin struct lg_drv_data *drv_data = hid_get_drvdata(hid); 101322bcefdcSSimon Wood struct lg4ff_device_entry *entry; 101422bcefdcSSimon Wood int i, value = 0; 101522bcefdcSSimon Wood 101622bcefdcSSimon Wood if (!drv_data) { 101722bcefdcSSimon Wood hid_err(hid, "Device data not found."); 101822bcefdcSSimon Wood return LED_OFF; 101922bcefdcSSimon Wood } 102022bcefdcSSimon Wood 1021371a1d9eSMichal Malý entry = drv_data->device_props; 102222bcefdcSSimon Wood 102322bcefdcSSimon Wood if (!entry) { 102422bcefdcSSimon Wood hid_err(hid, "Device properties not found."); 102522bcefdcSSimon Wood return LED_OFF; 102622bcefdcSSimon Wood } 102722bcefdcSSimon Wood 102822bcefdcSSimon Wood for (i = 0; i < 5; i++) 102972529c65SMichal Malý if (led_cdev == entry->wdata.led[i]) { 103072529c65SMichal Malý value = (entry->wdata.led_state >> i) & 1; 103122bcefdcSSimon Wood break; 103222bcefdcSSimon Wood } 103322bcefdcSSimon Wood 103422bcefdcSSimon Wood return value ? LED_FULL : LED_OFF; 103522bcefdcSSimon Wood } 103622bcefdcSSimon Wood #endif 103722bcefdcSSimon Wood 1038e7c23449SMichal Malý static u16 lg4ff_identify_multimode_wheel(struct hid_device *hid, const u16 reported_product_id, const u16 bcdDevice) 1039e7c23449SMichal Malý { 1040e7c23449SMichal Malý const struct lg4ff_wheel_ident_checklist *checklist; 1041e7c23449SMichal Malý int i, from_idx, to_idx; 1042e7c23449SMichal Malý 1043e7c23449SMichal Malý switch (reported_product_id) { 1044e7c23449SMichal Malý case USB_DEVICE_ID_LOGITECH_WHEEL: 1045e7c23449SMichal Malý case USB_DEVICE_ID_LOGITECH_DFP_WHEEL: 1046e7c23449SMichal Malý checklist = &lg4ff_main_checklist; 1047e7c23449SMichal Malý from_idx = 0; 1048e7c23449SMichal Malý to_idx = checklist->count - 1; 1049e7c23449SMichal Malý break; 1050e7c23449SMichal Malý case USB_DEVICE_ID_LOGITECH_G25_WHEEL: 1051e7c23449SMichal Malý checklist = &lg4ff_main_checklist; 1052e7c23449SMichal Malý from_idx = 0; 1053e7c23449SMichal Malý to_idx = checklist->count - 2; /* End identity check at G25 */ 1054e7c23449SMichal Malý break; 1055e7c23449SMichal Malý case USB_DEVICE_ID_LOGITECH_G27_WHEEL: 1056e7c23449SMichal Malý checklist = &lg4ff_main_checklist; 1057e7c23449SMichal Malý from_idx = 1; /* Start identity check at G27 */ 1058e7c23449SMichal Malý to_idx = checklist->count - 3; /* End identity check at G27 */ 1059e7c23449SMichal Malý break; 1060e7c23449SMichal Malý case USB_DEVICE_ID_LOGITECH_DFGT_WHEEL: 1061e7c23449SMichal Malý checklist = &lg4ff_main_checklist; 1062e7c23449SMichal Malý from_idx = 0; 1063e7c23449SMichal Malý to_idx = checklist->count - 4; /* End identity check at DFGT */ 1064e7c23449SMichal Malý break; 1065e7c23449SMichal Malý default: 1066e7c23449SMichal Malý return 0; 1067e7c23449SMichal Malý } 1068e7c23449SMichal Malý 1069e7c23449SMichal Malý for (i = from_idx; i <= to_idx; i++) { 1070e7c23449SMichal Malý const u16 mask = checklist->models[i]->mask; 1071e7c23449SMichal Malý const u16 result = checklist->models[i]->result; 1072e7c23449SMichal Malý const u16 real_product_id = checklist->models[i]->real_product_id; 1073e7c23449SMichal Malý 1074e7c23449SMichal Malý if ((bcdDevice & mask) == result) { 1075e7c23449SMichal Malý dbg_hid("Found wheel with real PID %X whose reported PID is %X\n", real_product_id, reported_product_id); 1076e7c23449SMichal Malý return real_product_id; 1077e7c23449SMichal Malý } 1078e7c23449SMichal Malý } 1079e7c23449SMichal Malý 1080f31a2de3SMichal Malý /* No match found. This is either Driving Force or an unknown 1081f31a2de3SMichal Malý * wheel model, do not touch it */ 1082e7c23449SMichal Malý dbg_hid("Wheel with bcdDevice %X was not recognized as multimode wheel, leaving in its current mode\n", bcdDevice); 1083e7c23449SMichal Malý return 0; 1084e7c23449SMichal Malý } 1085e7c23449SMichal Malý 1086e7c23449SMichal Malý static int lg4ff_handle_multimode_wheel(struct hid_device *hid, u16 *real_product_id, const u16 bcdDevice) 1087e7c23449SMichal Malý { 1088e7c23449SMichal Malý const u16 reported_product_id = hid->product; 1089e7c23449SMichal Malý int ret; 1090e7c23449SMichal Malý 1091e7c23449SMichal Malý *real_product_id = lg4ff_identify_multimode_wheel(hid, reported_product_id, bcdDevice); 1092e7c23449SMichal Malý /* Probed wheel is not a multimode wheel */ 1093e7c23449SMichal Malý if (!*real_product_id) { 1094e7c23449SMichal Malý *real_product_id = reported_product_id; 1095e7c23449SMichal Malý dbg_hid("Wheel is not a multimode wheel\n"); 1096e7c23449SMichal Malý return LG4FF_MMODE_NOT_MULTIMODE; 1097e7c23449SMichal Malý } 1098e7c23449SMichal Malý 1099e7c23449SMichal Malý /* Switch from "Driving Force" mode to native mode automatically. 1100e7c23449SMichal Malý * Otherwise keep the wheel in its current mode */ 1101e7c23449SMichal Malý if (reported_product_id == USB_DEVICE_ID_LOGITECH_WHEEL && 1102a54dc779SMichal Malý reported_product_id != *real_product_id && 1103a54dc779SMichal Malý !lg4ff_no_autoswitch) { 1104f31a2de3SMichal Malý const struct lg4ff_compat_mode_switch *s = lg4ff_get_mode_switch_command(*real_product_id, *real_product_id); 1105e7c23449SMichal Malý 1106f31a2de3SMichal Malý if (!s) { 1107e7c23449SMichal Malý hid_err(hid, "Invalid product id %X\n", *real_product_id); 1108b96d23ecSMichal Malý return LG4FF_MMODE_NOT_MULTIMODE; 1109e7c23449SMichal Malý } 1110e7c23449SMichal Malý 1111e7c23449SMichal Malý ret = lg4ff_switch_compatibility_mode(hid, s); 1112e7c23449SMichal Malý if (ret) { 1113e7c23449SMichal Malý /* Wheel could not have been switched to native mode, 1114e7c23449SMichal Malý * leave it in "Driving Force" mode and continue */ 1115e7c23449SMichal Malý hid_err(hid, "Unable to switch wheel mode, errno %d\n", ret); 1116b96d23ecSMichal Malý return LG4FF_MMODE_IS_MULTIMODE; 1117e7c23449SMichal Malý } 1118e7c23449SMichal Malý return LG4FF_MMODE_SWITCHED; 1119e7c23449SMichal Malý } 1120e7c23449SMichal Malý 1121b96d23ecSMichal Malý return LG4FF_MMODE_IS_MULTIMODE; 1122e7c23449SMichal Malý } 1123e7c23449SMichal Malý 1124e7c23449SMichal Malý 112532c88cbcSSimon Wood int lg4ff_init(struct hid_device *hid) 112632c88cbcSSimon Wood { 112732c88cbcSSimon Wood struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list); 112832c88cbcSSimon Wood struct input_dev *dev = hidinput->input; 1129c28abd8cSMichal Malý struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list; 1130c28abd8cSMichal Malý struct hid_report *report = list_entry(report_list->next, struct hid_report, list); 1131e7c23449SMichal Malý const struct usb_device_descriptor *udesc = &(hid_to_usb_dev(hid)->descriptor); 1132e7c23449SMichal Malý const u16 bcdDevice = le16_to_cpu(udesc->bcdDevice); 1133*5d9d60adSMichal Malý const struct lg4ff_multimode_wheel *mmode_wheel = NULL; 113430bb75d7SMichal Malý struct lg4ff_device_entry *entry; 11353b6b17b7SMichal Malý struct lg_drv_data *drv_data; 1136b96d23ecSMichal Malý int error, i, j; 1137b96d23ecSMichal Malý int mmode_ret, mmode_idx = -1; 1138e7c23449SMichal Malý u16 real_product_id; 113932c88cbcSSimon Wood 114032c88cbcSSimon Wood /* Check that the report looks ok */ 11410fb6bd06SKees Cook if (!hid_validate_values(hid, HID_OUTPUT_REPORT, 0, 0, 7)) 114232c88cbcSSimon Wood return -1; 114332c88cbcSSimon Wood 114472529c65SMichal Malý drv_data = hid_get_drvdata(hid); 114572529c65SMichal Malý if (!drv_data) { 114672529c65SMichal Malý hid_err(hid, "Cannot add device, private driver data not allocated\n"); 114772529c65SMichal Malý return -1; 114872529c65SMichal Malý } 114972529c65SMichal Malý entry = kzalloc(sizeof(*entry), GFP_KERNEL); 115072529c65SMichal Malý if (!entry) 115172529c65SMichal Malý return -ENOMEM; 1152c918fe78SMichal Malý spin_lock_init(&entry->report_lock); 1153c28abd8cSMichal Malý entry->report = report; 115472529c65SMichal Malý drv_data->device_props = entry; 115572529c65SMichal Malý 1156e7c23449SMichal Malý /* Check if a multimode wheel has been connected and 1157e7c23449SMichal Malý * handle it appropriately */ 1158b96d23ecSMichal Malý mmode_ret = lg4ff_handle_multimode_wheel(hid, &real_product_id, bcdDevice); 1159e7c23449SMichal Malý 1160e7c23449SMichal Malý /* Wheel has been told to switch to native mode. There is no point in going on 1161e7c23449SMichal Malý * with the initialization as the wheel will do a USB reset when it switches mode 1162e7c23449SMichal Malý */ 1163b96d23ecSMichal Malý if (mmode_ret == LG4FF_MMODE_SWITCHED) 1164e7c23449SMichal Malý return 0; 116572529c65SMichal Malý else if (mmode_ret < 0) { 116672529c65SMichal Malý hid_err(hid, "Unable to switch device mode during initialization, errno %d\n", mmode_ret); 116772529c65SMichal Malý error = mmode_ret; 116872529c65SMichal Malý goto err_init; 116972529c65SMichal Malý } 1170e7c23449SMichal Malý 11717362cd22SMichal Malý /* Check what wheel has been connected */ 11727362cd22SMichal Malý for (i = 0; i < ARRAY_SIZE(lg4ff_devices); i++) { 11737362cd22SMichal Malý if (hid->product == lg4ff_devices[i].product_id) { 11747362cd22SMichal Malý dbg_hid("Found compatible device, product ID %04X\n", lg4ff_devices[i].product_id); 11757362cd22SMichal Malý break; 11767362cd22SMichal Malý } 11777362cd22SMichal Malý } 11787362cd22SMichal Malý 11797362cd22SMichal Malý if (i == ARRAY_SIZE(lg4ff_devices)) { 11809c2a6bd1SMichal Malý hid_err(hid, "This device is flagged to be handled by the lg4ff module but this module does not know how to handle it. " 11819c2a6bd1SMichal Malý "Please report this as a bug to LKML, Simon Wood <simon@mungewell.org> or " 11829c2a6bd1SMichal Malý "Michal Maly <madcatxster@devoid-pointer.net>\n"); 118372529c65SMichal Malý error = -1; 118472529c65SMichal Malý goto err_init; 11857362cd22SMichal Malý } 11867362cd22SMichal Malý 1187b96d23ecSMichal Malý if (mmode_ret == LG4FF_MMODE_IS_MULTIMODE) { 1188b96d23ecSMichal Malý for (mmode_idx = 0; mmode_idx < ARRAY_SIZE(lg4ff_multimode_wheels); mmode_idx++) { 1189b96d23ecSMichal Malý if (real_product_id == lg4ff_multimode_wheels[mmode_idx].product_id) 1190b96d23ecSMichal Malý break; 1191b96d23ecSMichal Malý } 1192b96d23ecSMichal Malý 1193b96d23ecSMichal Malý if (mmode_idx == ARRAY_SIZE(lg4ff_multimode_wheels)) { 1194b96d23ecSMichal Malý hid_err(hid, "Device product ID %X is not listed as a multimode wheel", real_product_id); 119572529c65SMichal Malý error = -1; 119672529c65SMichal Malý goto err_init; 1197b96d23ecSMichal Malý } 1198b96d23ecSMichal Malý } 1199b96d23ecSMichal Malý 12007362cd22SMichal Malý /* Set supported force feedback capabilities */ 12017362cd22SMichal Malý for (j = 0; lg4ff_devices[i].ff_effects[j] >= 0; j++) 12027362cd22SMichal Malý set_bit(lg4ff_devices[i].ff_effects[j], dev->ffbit); 120332c88cbcSSimon Wood 1204d0afd848SMichal Malý error = input_ff_create_memless(dev, NULL, lg4ff_play); 120532c88cbcSSimon Wood 120632c88cbcSSimon Wood if (error) 120772529c65SMichal Malý goto err_init; 120832c88cbcSSimon Wood 1209*5d9d60adSMichal Malý /* Initialize device properties */ 1210b96d23ecSMichal Malý if (mmode_ret == LG4FF_MMODE_IS_MULTIMODE) { 1211b96d23ecSMichal Malý BUG_ON(mmode_idx == -1); 1212*5d9d60adSMichal Malý mmode_wheel = &lg4ff_multimode_wheels[mmode_idx]; 1213b96d23ecSMichal Malý } 1214*5d9d60adSMichal Malý lg4ff_init_wheel_data(&entry->wdata, &lg4ff_devices[i], mmode_wheel, real_product_id); 121530bb75d7SMichal Malý 1216114a55cfSSimon Wood /* Check if autocentering is available and 1217114a55cfSSimon Wood * set the centering force to zero by default */ 1218114a55cfSSimon Wood if (test_bit(FF_AUTOCENTER, dev->ffbit)) { 1219e7c23449SMichal Malý /* Formula Force EX expects different autocentering command */ 1220e7c23449SMichal Malý if ((bcdDevice >> 8) == LG4FF_FFEX_REV_MAJ && 1221e7c23449SMichal Malý (bcdDevice & 0xff) == LG4FF_FFEX_REV_MIN) 1222d0afd848SMichal Malý dev->ff->set_autocenter = lg4ff_set_autocenter_ffex; 1223114a55cfSSimon Wood else 1224d0afd848SMichal Malý dev->ff->set_autocenter = lg4ff_set_autocenter_default; 1225114a55cfSSimon Wood 1226114a55cfSSimon Wood dev->ff->set_autocenter(dev, 0); 1227114a55cfSSimon Wood } 1228114a55cfSSimon Wood 122930bb75d7SMichal Malý /* Create sysfs interface */ 123030bb75d7SMichal Malý error = device_create_file(&hid->dev, &dev_attr_range); 123130bb75d7SMichal Malý if (error) 123272529c65SMichal Malý goto err_init; 1233b96d23ecSMichal Malý if (mmode_ret == LG4FF_MMODE_IS_MULTIMODE) { 1234b96d23ecSMichal Malý error = device_create_file(&hid->dev, &dev_attr_real_id); 1235b96d23ecSMichal Malý if (error) 123672529c65SMichal Malý goto err_init; 123772529c65SMichal Malý 1238b96d23ecSMichal Malý error = device_create_file(&hid->dev, &dev_attr_alternate_modes); 1239b96d23ecSMichal Malý if (error) 124072529c65SMichal Malý goto err_init; 1241b96d23ecSMichal Malý } 124230bb75d7SMichal Malý dbg_hid("sysfs interface created\n"); 124330bb75d7SMichal Malý 124430bb75d7SMichal Malý /* Set the maximum range to start with */ 124572529c65SMichal Malý entry->wdata.range = entry->wdata.max_range; 124672529c65SMichal Malý if (entry->wdata.set_range) 124772529c65SMichal Malý entry->wdata.set_range(hid, entry->wdata.range); 124830bb75d7SMichal Malý 124922bcefdcSSimon Wood #ifdef CONFIG_LEDS_CLASS 125022bcefdcSSimon Wood /* register led subsystem - G27 only */ 125172529c65SMichal Malý entry->wdata.led_state = 0; 125222bcefdcSSimon Wood for (j = 0; j < 5; j++) 125372529c65SMichal Malý entry->wdata.led[j] = NULL; 125422bcefdcSSimon Wood 125522bcefdcSSimon Wood if (lg4ff_devices[i].product_id == USB_DEVICE_ID_LOGITECH_G27_WHEEL) { 125622bcefdcSSimon Wood struct led_classdev *led; 125722bcefdcSSimon Wood size_t name_sz; 125822bcefdcSSimon Wood char *name; 125922bcefdcSSimon Wood 126022bcefdcSSimon Wood lg4ff_set_leds(hid, 0); 126122bcefdcSSimon Wood 126222bcefdcSSimon Wood name_sz = strlen(dev_name(&hid->dev)) + 8; 126322bcefdcSSimon Wood 126422bcefdcSSimon Wood for (j = 0; j < 5; j++) { 126522bcefdcSSimon Wood led = kzalloc(sizeof(struct led_classdev)+name_sz, GFP_KERNEL); 126622bcefdcSSimon Wood if (!led) { 126722bcefdcSSimon Wood hid_err(hid, "can't allocate memory for LED %d\n", j); 126872529c65SMichal Malý goto err_leds; 126922bcefdcSSimon Wood } 127022bcefdcSSimon Wood 127122bcefdcSSimon Wood name = (void *)(&led[1]); 127222bcefdcSSimon Wood snprintf(name, name_sz, "%s::RPM%d", dev_name(&hid->dev), j+1); 127322bcefdcSSimon Wood led->name = name; 127422bcefdcSSimon Wood led->brightness = 0; 127522bcefdcSSimon Wood led->max_brightness = 1; 127622bcefdcSSimon Wood led->brightness_get = lg4ff_led_get_brightness; 127722bcefdcSSimon Wood led->brightness_set = lg4ff_led_set_brightness; 127822bcefdcSSimon Wood 127972529c65SMichal Malý entry->wdata.led[j] = led; 128022bcefdcSSimon Wood error = led_classdev_register(&hid->dev, led); 128122bcefdcSSimon Wood 128222bcefdcSSimon Wood if (error) { 128322bcefdcSSimon Wood hid_err(hid, "failed to register LED %d. Aborting.\n", j); 128472529c65SMichal Malý err_leds: 128522bcefdcSSimon Wood /* Deregister LEDs (if any) */ 128622bcefdcSSimon Wood for (j = 0; j < 5; j++) { 128772529c65SMichal Malý led = entry->wdata.led[j]; 128872529c65SMichal Malý entry->wdata.led[j] = NULL; 128922bcefdcSSimon Wood if (!led) 129022bcefdcSSimon Wood continue; 129122bcefdcSSimon Wood led_classdev_unregister(led); 129222bcefdcSSimon Wood kfree(led); 129322bcefdcSSimon Wood } 129422bcefdcSSimon Wood goto out; /* Let the driver continue without LEDs */ 129522bcefdcSSimon Wood } 129622bcefdcSSimon Wood } 129722bcefdcSSimon Wood } 129822bcefdcSSimon Wood out: 1299c6e6dc87SJiri Kosina #endif 130064013800SSimon Wood hid_info(hid, "Force feedback support for Logitech Gaming Wheels\n"); 130132c88cbcSSimon Wood return 0; 130272529c65SMichal Malý 130372529c65SMichal Malý err_init: 130472529c65SMichal Malý drv_data->device_props = NULL; 130572529c65SMichal Malý kfree(entry); 130672529c65SMichal Malý return error; 130732c88cbcSSimon Wood } 130832c88cbcSSimon Wood 130930bb75d7SMichal Malý int lg4ff_deinit(struct hid_device *hid) 131030bb75d7SMichal Malý { 131130bb75d7SMichal Malý struct lg4ff_device_entry *entry; 13123b6b17b7SMichal Malý struct lg_drv_data *drv_data; 13136a2e176bSMichal Malý 13143b6b17b7SMichal Malý drv_data = hid_get_drvdata(hid); 13153b6b17b7SMichal Malý if (!drv_data) { 13163b6b17b7SMichal Malý hid_err(hid, "Error while deinitializing device, no private driver data.\n"); 131730bb75d7SMichal Malý return -1; 131830bb75d7SMichal Malý } 13193b6b17b7SMichal Malý entry = drv_data->device_props; 1320e7c23449SMichal Malý if (!entry) 1321e7c23449SMichal Malý goto out; /* Nothing more to do */ 1322e7c23449SMichal Malý 1323b96d23ecSMichal Malý /* Multimode devices will have at least the "MODE_NATIVE" bit set */ 132472529c65SMichal Malý if (entry->wdata.alternate_modes) { 1325b96d23ecSMichal Malý device_remove_file(&hid->dev, &dev_attr_real_id); 1326b96d23ecSMichal Malý device_remove_file(&hid->dev, &dev_attr_alternate_modes); 1327b96d23ecSMichal Malý } 1328b96d23ecSMichal Malý 132972529c65SMichal Malý device_remove_file(&hid->dev, &dev_attr_range); 133022bcefdcSSimon Wood #ifdef CONFIG_LEDS_CLASS 133122bcefdcSSimon Wood { 133222bcefdcSSimon Wood int j; 133322bcefdcSSimon Wood struct led_classdev *led; 133422bcefdcSSimon Wood 133522bcefdcSSimon Wood /* Deregister LEDs (if any) */ 133622bcefdcSSimon Wood for (j = 0; j < 5; j++) { 133722bcefdcSSimon Wood 133872529c65SMichal Malý led = entry->wdata.led[j]; 133972529c65SMichal Malý entry->wdata.led[j] = NULL; 134022bcefdcSSimon Wood if (!led) 134122bcefdcSSimon Wood continue; 134222bcefdcSSimon Wood led_classdev_unregister(led); 134322bcefdcSSimon Wood kfree(led); 134422bcefdcSSimon Wood } 134522bcefdcSSimon Wood } 134622bcefdcSSimon Wood #endif 1347b211a638SMichal Malý hid_hw_stop(hid); 1348b211a638SMichal Malý drv_data->device_props = NULL; 134922bcefdcSSimon Wood 13503b6b17b7SMichal Malý kfree(entry); 1351e7c23449SMichal Malý out: 135230bb75d7SMichal Malý dbg_hid("Device successfully unregistered\n"); 135330bb75d7SMichal Malý return 0; 135430bb75d7SMichal Malý } 1355