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 { 752a552c30SMichal Malý u32 product_id; 762a552c30SMichal Malý u16 range; 772a552c30SMichal Malý u16 min_range; 782a552c30SMichal Malý u16 max_range; 7922bcefdcSSimon Wood #ifdef CONFIG_LEDS_CLASS 802a552c30SMichal Malý u8 led_state; 8122bcefdcSSimon Wood struct led_classdev *led[5]; 8222bcefdcSSimon Wood #endif 83b96d23ecSMichal Malý u32 alternate_modes; 84b96d23ecSMichal Malý const char *real_tag; 85b96d23ecSMichal Malý const char *real_name; 86b96d23ecSMichal Malý 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 { 92*c918fe78SMichal Malý spinlock_t report_lock; /* Protect output HID report */ 9372529c65SMichal Malý struct lg4ff_wheel_data wdata; 9472529c65SMichal Malý }; 9572529c65SMichal Malý 967362cd22SMichal Malý static const signed short lg4ff_wheel_effects[] = { 9732c88cbcSSimon Wood FF_CONSTANT, 9832c88cbcSSimon Wood FF_AUTOCENTER, 9932c88cbcSSimon Wood -1 10032c88cbcSSimon Wood }; 10132c88cbcSSimon Wood 1027362cd22SMichal Malý struct lg4ff_wheel { 1032a552c30SMichal Malý const u32 product_id; 1047362cd22SMichal Malý const signed short *ff_effects; 1052a552c30SMichal Malý const u16 min_range; 1062a552c30SMichal Malý const u16 max_range; 10730bb75d7SMichal Malý void (*set_range)(struct hid_device *hid, u16 range); 1087362cd22SMichal Malý }; 1097362cd22SMichal Malý 110e7c23449SMichal Malý struct lg4ff_compat_mode_switch { 1112a552c30SMichal Malý const u8 cmd_count; /* Number of commands to send */ 1122a552c30SMichal Malý const u8 cmd[]; 113e7c23449SMichal Malý }; 114e7c23449SMichal Malý 115e7c23449SMichal Malý struct lg4ff_wheel_ident_info { 116e7c23449SMichal Malý const u16 mask; 117e7c23449SMichal Malý const u16 result; 118e7c23449SMichal Malý const u16 real_product_id; 119e7c23449SMichal Malý }; 120e7c23449SMichal Malý 121e7c23449SMichal Malý struct lg4ff_wheel_ident_checklist { 122e7c23449SMichal Malý const u32 count; 123e7c23449SMichal Malý const struct lg4ff_wheel_ident_info *models[]; 124e7c23449SMichal Malý }; 125e7c23449SMichal Malý 126b96d23ecSMichal Malý struct lg4ff_multimode_wheel { 127b96d23ecSMichal Malý const u16 product_id; 128b96d23ecSMichal Malý const u32 alternate_modes; 129b96d23ecSMichal Malý const char *real_tag; 130b96d23ecSMichal Malý const char *real_name; 131b96d23ecSMichal Malý }; 132b96d23ecSMichal Malý 133b96d23ecSMichal Malý struct lg4ff_alternate_mode { 134b96d23ecSMichal Malý const u16 product_id; 135b96d23ecSMichal Malý const char *tag; 136b96d23ecSMichal Malý const char *name; 137b96d23ecSMichal Malý }; 138b96d23ecSMichal Malý 1397362cd22SMichal Malý static const struct lg4ff_wheel lg4ff_devices[] = { 14030bb75d7SMichal Malý {USB_DEVICE_ID_LOGITECH_WHEEL, lg4ff_wheel_effects, 40, 270, NULL}, 14130bb75d7SMichal Malý {USB_DEVICE_ID_LOGITECH_MOMO_WHEEL, lg4ff_wheel_effects, 40, 270, NULL}, 142d0afd848SMichal Malý {USB_DEVICE_ID_LOGITECH_DFP_WHEEL, lg4ff_wheel_effects, 40, 900, lg4ff_set_range_dfp}, 143d0afd848SMichal Malý {USB_DEVICE_ID_LOGITECH_G25_WHEEL, lg4ff_wheel_effects, 40, 900, lg4ff_set_range_g25}, 144d0afd848SMichal Malý {USB_DEVICE_ID_LOGITECH_DFGT_WHEEL, lg4ff_wheel_effects, 40, 900, lg4ff_set_range_g25}, 145d0afd848SMichal Malý {USB_DEVICE_ID_LOGITECH_G27_WHEEL, lg4ff_wheel_effects, 40, 900, lg4ff_set_range_g25}, 14630bb75d7SMichal Malý {USB_DEVICE_ID_LOGITECH_MOMO_WHEEL2, lg4ff_wheel_effects, 40, 270, NULL}, 14730bb75d7SMichal Malý {USB_DEVICE_ID_LOGITECH_WII_WHEEL, lg4ff_wheel_effects, 40, 270, NULL} 1487362cd22SMichal Malý }; 1497362cd22SMichal Malý 150b96d23ecSMichal Malý static const struct lg4ff_multimode_wheel lg4ff_multimode_wheels[] = { 151b96d23ecSMichal Malý {USB_DEVICE_ID_LOGITECH_DFP_WHEEL, 152b96d23ecSMichal Malý LG4FF_MODE_NATIVE | LG4FF_MODE_DFP | LG4FF_MODE_DFEX, 153b96d23ecSMichal Malý LG4FF_DFP_TAG, LG4FF_DFP_NAME}, 154b96d23ecSMichal Malý {USB_DEVICE_ID_LOGITECH_G25_WHEEL, 155b96d23ecSMichal Malý LG4FF_MODE_NATIVE | LG4FF_MODE_G25 | LG4FF_MODE_DFP | LG4FF_MODE_DFEX, 156b96d23ecSMichal Malý LG4FF_G25_TAG, LG4FF_G25_NAME}, 157b96d23ecSMichal Malý {USB_DEVICE_ID_LOGITECH_DFGT_WHEEL, 158b96d23ecSMichal Malý LG4FF_MODE_NATIVE | LG4FF_MODE_DFGT | LG4FF_MODE_DFP | LG4FF_MODE_DFEX, 159b96d23ecSMichal Malý LG4FF_DFGT_TAG, LG4FF_DFGT_NAME}, 160b96d23ecSMichal Malý {USB_DEVICE_ID_LOGITECH_G27_WHEEL, 161b96d23ecSMichal Malý LG4FF_MODE_NATIVE | LG4FF_MODE_G27 | LG4FF_MODE_G25 | LG4FF_MODE_DFP | LG4FF_MODE_DFEX, 162b96d23ecSMichal Malý LG4FF_G27_TAG, LG4FF_G27_NAME}, 163b96d23ecSMichal Malý }; 164b96d23ecSMichal Malý 165b96d23ecSMichal Malý static const struct lg4ff_alternate_mode lg4ff_alternate_modes[] = { 166b96d23ecSMichal Malý [LG4FF_MODE_NATIVE_IDX] = {0, "native", ""}, 167b96d23ecSMichal Malý [LG4FF_MODE_DFEX_IDX] = {USB_DEVICE_ID_LOGITECH_WHEEL, LG4FF_DFEX_TAG, LG4FF_DFEX_NAME}, 168b96d23ecSMichal Malý [LG4FF_MODE_DFP_IDX] = {USB_DEVICE_ID_LOGITECH_DFP_WHEEL, LG4FF_DFP_TAG, LG4FF_DFP_NAME}, 169b96d23ecSMichal Malý [LG4FF_MODE_G25_IDX] = {USB_DEVICE_ID_LOGITECH_G25_WHEEL, LG4FF_G25_TAG, LG4FF_G25_NAME}, 170b96d23ecSMichal Malý [LG4FF_MODE_DFGT_IDX] = {USB_DEVICE_ID_LOGITECH_DFGT_WHEEL, LG4FF_DFGT_TAG, LG4FF_DFGT_NAME}, 171b96d23ecSMichal Malý [LG4FF_MODE_G27_IDX] = {USB_DEVICE_ID_LOGITECH_G27_WHEEL, LG4FF_G27_TAG, LG4FF_G27_NAME} 172b96d23ecSMichal Malý }; 173b96d23ecSMichal Malý 174e7c23449SMichal Malý /* Multimode wheel identificators */ 175e7c23449SMichal Malý static const struct lg4ff_wheel_ident_info lg4ff_dfp_ident_info = { 176e7c23449SMichal Malý 0xf000, 177e7c23449SMichal Malý 0x1000, 178e7c23449SMichal Malý USB_DEVICE_ID_LOGITECH_DFP_WHEEL 17996440c8aSMichal Malý }; 18096440c8aSMichal Malý 181e7c23449SMichal Malý static const struct lg4ff_wheel_ident_info lg4ff_g25_ident_info = { 182e7c23449SMichal Malý 0xff00, 183e7c23449SMichal Malý 0x1200, 184e7c23449SMichal Malý USB_DEVICE_ID_LOGITECH_G25_WHEEL 18596440c8aSMichal Malý }; 18696440c8aSMichal Malý 187e7c23449SMichal Malý static const struct lg4ff_wheel_ident_info lg4ff_g27_ident_info = { 188e7c23449SMichal Malý 0xfff0, 189e7c23449SMichal Malý 0x1230, 190e7c23449SMichal Malý USB_DEVICE_ID_LOGITECH_G27_WHEEL 191e7c23449SMichal Malý }; 192e7c23449SMichal Malý 193e7c23449SMichal Malý static const struct lg4ff_wheel_ident_info lg4ff_dfgt_ident_info = { 194e7c23449SMichal Malý 0xff00, 195e7c23449SMichal Malý 0x1300, 196e7c23449SMichal Malý USB_DEVICE_ID_LOGITECH_DFGT_WHEEL 197e7c23449SMichal Malý }; 198e7c23449SMichal Malý 199e7c23449SMichal Malý /* Multimode wheel identification checklists */ 200e7c23449SMichal Malý static const struct lg4ff_wheel_ident_checklist lg4ff_main_checklist = { 201e7c23449SMichal Malý 4, 202e7c23449SMichal Malý {&lg4ff_dfgt_ident_info, 203e7c23449SMichal Malý &lg4ff_g27_ident_info, 204e7c23449SMichal Malý &lg4ff_g25_ident_info, 205e7c23449SMichal Malý &lg4ff_dfp_ident_info} 206e7c23449SMichal Malý }; 207e7c23449SMichal Malý 208e7c23449SMichal Malý /* Compatibility mode switching commands */ 209f31a2de3SMichal Malý /* EXT_CMD9 - Understood by G27 and DFGT */ 210f31a2de3SMichal Malý static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext09_dfex = { 211f31a2de3SMichal Malý 2, 212f31a2de3SMichal Malý {0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, /* Revert mode upon USB reset */ 213f31a2de3SMichal Malý 0xf8, 0x09, 0x00, 0x01, 0x00, 0x00, 0x00} /* Switch mode to DF-EX with detach */ 214f31a2de3SMichal Malý }; 215f31a2de3SMichal Malý 216f31a2de3SMichal Malý static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext09_dfp = { 217f31a2de3SMichal Malý 2, 218f31a2de3SMichal Malý {0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, /* Revert mode upon USB reset */ 219f31a2de3SMichal Malý 0xf8, 0x09, 0x01, 0x01, 0x00, 0x00, 0x00} /* Switch mode to DFP with detach */ 220f31a2de3SMichal Malý }; 221f31a2de3SMichal Malý 222f31a2de3SMichal Malý static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext09_g25 = { 223f31a2de3SMichal Malý 2, 224f31a2de3SMichal Malý {0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, /* Revert mode upon USB reset */ 225f31a2de3SMichal Malý 0xf8, 0x09, 0x02, 0x01, 0x00, 0x00, 0x00} /* Switch mode to G25 with detach */ 226f31a2de3SMichal Malý }; 227f31a2de3SMichal Malý 228f31a2de3SMichal Malý static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext09_dfgt = { 229f31a2de3SMichal Malý 2, 230f31a2de3SMichal Malý {0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, /* Revert mode upon USB reset */ 231f31a2de3SMichal Malý 0xf8, 0x09, 0x03, 0x01, 0x00, 0x00, 0x00} /* Switch mode to DFGT with detach */ 232f31a2de3SMichal Malý }; 233f31a2de3SMichal Malý 234f31a2de3SMichal Malý static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext09_g27 = { 235f31a2de3SMichal Malý 2, 236f31a2de3SMichal Malý {0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, /* Revert mode upon USB reset */ 237f31a2de3SMichal Malý 0xf8, 0x09, 0x04, 0x01, 0x00, 0x00, 0x00} /* Switch mode to G27 with detach */ 238f31a2de3SMichal Malý }; 239f31a2de3SMichal Malý 240f31a2de3SMichal Malý /* EXT_CMD1 - Understood by DFP, G25, G27 and DFGT */ 241f31a2de3SMichal Malý static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext01_dfp = { 24296440c8aSMichal Malý 1, 24396440c8aSMichal Malý {0xf8, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00} 24496440c8aSMichal Malý }; 24596440c8aSMichal Malý 246f31a2de3SMichal Malý /* EXT_CMD16 - Understood by G25 and G27 */ 247f31a2de3SMichal Malý static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext16_g25 = { 24896440c8aSMichal Malý 1, 24996440c8aSMichal Malý {0xf8, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00} 25096440c8aSMichal Malý }; 25196440c8aSMichal Malý 2522b24a960SMichal Malý /* Recalculates X axis value accordingly to currently selected range */ 2532a552c30SMichal Malý static s32 lg4ff_adjust_dfp_x_axis(s32 value, u16 range) 2542b24a960SMichal Malý { 2552a552c30SMichal Malý u16 max_range; 2562a552c30SMichal Malý s32 new_value; 2572b24a960SMichal Malý 2582b24a960SMichal Malý if (range == 900) 2592b24a960SMichal Malý return value; 2602b24a960SMichal Malý else if (range == 200) 2612b24a960SMichal Malý return value; 2622b24a960SMichal Malý else if (range < 200) 2632b24a960SMichal Malý max_range = 200; 2642b24a960SMichal Malý else 2652b24a960SMichal Malý max_range = 900; 2662b24a960SMichal Malý 2672b24a960SMichal Malý new_value = 8192 + mult_frac(value - 8192, max_range, range); 2682b24a960SMichal Malý if (new_value < 0) 2692b24a960SMichal Malý return 0; 2702b24a960SMichal Malý else if (new_value > 16383) 2712b24a960SMichal Malý return 16383; 2722b24a960SMichal Malý else 2732b24a960SMichal Malý return new_value; 2742b24a960SMichal Malý } 2752b24a960SMichal Malý 2762b24a960SMichal Malý int lg4ff_adjust_input_event(struct hid_device *hid, struct hid_field *field, 2772a552c30SMichal Malý struct hid_usage *usage, s32 value, struct lg_drv_data *drv_data) 2782b24a960SMichal Malý { 2792b24a960SMichal Malý struct lg4ff_device_entry *entry = drv_data->device_props; 2802a552c30SMichal Malý s32 new_value = 0; 2812b24a960SMichal Malý 2822b24a960SMichal Malý if (!entry) { 2832b24a960SMichal Malý hid_err(hid, "Device properties not found"); 2842b24a960SMichal Malý return 0; 2852b24a960SMichal Malý } 2862b24a960SMichal Malý 28772529c65SMichal Malý switch (entry->wdata.product_id) { 2882b24a960SMichal Malý case USB_DEVICE_ID_LOGITECH_DFP_WHEEL: 2892b24a960SMichal Malý switch (usage->code) { 2902b24a960SMichal Malý case ABS_X: 29172529c65SMichal Malý new_value = lg4ff_adjust_dfp_x_axis(value, entry->wdata.range); 2922b24a960SMichal Malý input_event(field->hidinput->input, usage->type, usage->code, new_value); 2932b24a960SMichal Malý return 1; 2942b24a960SMichal Malý default: 2952b24a960SMichal Malý return 0; 2962b24a960SMichal Malý } 2972b24a960SMichal Malý default: 2982b24a960SMichal Malý return 0; 2992b24a960SMichal Malý } 3002b24a960SMichal Malý } 3012b24a960SMichal Malý 302d0afd848SMichal Malý static int lg4ff_play(struct input_dev *dev, void *data, struct ff_effect *effect) 30332c88cbcSSimon Wood { 30432c88cbcSSimon Wood struct hid_device *hid = input_get_drvdata(dev); 30532c88cbcSSimon Wood struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list; 30632c88cbcSSimon Wood struct hid_report *report = list_entry(report_list->next, struct hid_report, list); 307*c918fe78SMichal Malý struct lg4ff_device_entry *entry; 308*c918fe78SMichal Malý struct lg_drv_data *drv_data; 309*c918fe78SMichal Malý unsigned long flags; 3102a552c30SMichal Malý s32 *value = report->field[0]->value; 31132c88cbcSSimon Wood int x; 31232c88cbcSSimon Wood 313*c918fe78SMichal Malý drv_data = hid_get_drvdata(hid); 314*c918fe78SMichal Malý if (!drv_data) { 315*c918fe78SMichal Malý hid_err(hid, "Private driver data not found!\n"); 316*c918fe78SMichal Malý return -EINVAL; 317*c918fe78SMichal Malý } 318*c918fe78SMichal Malý 319*c918fe78SMichal Malý entry = drv_data->device_props; 320*c918fe78SMichal Malý if (!entry) { 321*c918fe78SMichal Malý hid_err(hid, "Device properties not found!\n"); 322*c918fe78SMichal Malý return -EINVAL; 323*c918fe78SMichal Malý } 324*c918fe78SMichal Malý 325a80fe5d6SMichal Malý #define CLAMP(x) do { if (x < 0) x = 0; else if (x > 0xff) x = 0xff; } while (0) 32632c88cbcSSimon Wood 32732c88cbcSSimon Wood switch (effect->type) { 32832c88cbcSSimon Wood case FF_CONSTANT: 32932c88cbcSSimon Wood x = effect->u.ramp.start_level + 0x80; /* 0x80 is no force */ 33032c88cbcSSimon Wood CLAMP(x); 33156930e7aSSimon Wood 332*c918fe78SMichal Malý spin_lock_irqsave(&entry->report_lock, flags); 33356930e7aSSimon Wood if (x == 0x80) { 33456930e7aSSimon Wood /* De-activate force in slot-1*/ 33556930e7aSSimon Wood value[0] = 0x13; 33656930e7aSSimon Wood value[1] = 0x00; 33756930e7aSSimon Wood value[2] = 0x00; 33856930e7aSSimon Wood value[3] = 0x00; 33956930e7aSSimon Wood value[4] = 0x00; 34056930e7aSSimon Wood value[5] = 0x00; 34156930e7aSSimon Wood value[6] = 0x00; 34256930e7aSSimon Wood 34356930e7aSSimon Wood hid_hw_request(hid, report, HID_REQ_SET_REPORT); 344*c918fe78SMichal Malý spin_unlock_irqrestore(&entry->report_lock, flags); 34556930e7aSSimon Wood return 0; 34656930e7aSSimon Wood } 34756930e7aSSimon Wood 34874479ba8SMichal Malý value[0] = 0x11; /* Slot 1 */ 34974479ba8SMichal Malý value[1] = 0x08; 35074479ba8SMichal Malý value[2] = x; 35174479ba8SMichal Malý value[3] = 0x80; 35274479ba8SMichal Malý value[4] = 0x00; 35374479ba8SMichal Malý value[5] = 0x00; 35474479ba8SMichal Malý value[6] = 0x00; 35532c88cbcSSimon Wood 356d8814272SBenjamin Tissoires hid_hw_request(hid, report, HID_REQ_SET_REPORT); 357*c918fe78SMichal Malý spin_unlock_irqrestore(&entry->report_lock, flags); 35832c88cbcSSimon Wood break; 35932c88cbcSSimon Wood } 36032c88cbcSSimon Wood return 0; 36132c88cbcSSimon Wood } 36232c88cbcSSimon Wood 3636e2de8e0SMichal Malý /* Sends default autocentering command compatible with 3646e2de8e0SMichal Malý * all wheels except Formula Force EX */ 365d0afd848SMichal Malý static void lg4ff_set_autocenter_default(struct input_dev *dev, u16 magnitude) 36632c88cbcSSimon Wood { 36732c88cbcSSimon Wood struct hid_device *hid = input_get_drvdata(dev); 36832c88cbcSSimon Wood struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list; 36932c88cbcSSimon Wood struct hid_report *report = list_entry(report_list->next, struct hid_report, list); 3702a552c30SMichal Malý s32 *value = report->field[0]->value; 3712a552c30SMichal Malý u32 expand_a, expand_b; 3721859762eSSimon Wood struct lg4ff_device_entry *entry; 3731859762eSSimon Wood struct lg_drv_data *drv_data; 374*c918fe78SMichal Malý unsigned long flags; 3751859762eSSimon Wood 3761859762eSSimon Wood drv_data = hid_get_drvdata(hid); 3771859762eSSimon Wood if (!drv_data) { 3781859762eSSimon Wood hid_err(hid, "Private driver data not found!\n"); 3791859762eSSimon Wood return; 3801859762eSSimon Wood } 3811859762eSSimon Wood 3821859762eSSimon Wood entry = drv_data->device_props; 3831859762eSSimon Wood if (!entry) { 3841859762eSSimon Wood hid_err(hid, "Device properties not found!\n"); 3851859762eSSimon Wood return; 3861859762eSSimon Wood } 387f8c23156SSimon Wood 388d2c02da5SSimon Wood /* De-activate Auto-Center */ 389*c918fe78SMichal Malý spin_lock_irqsave(&entry->report_lock, flags); 390d2c02da5SSimon Wood if (magnitude == 0) { 391d2c02da5SSimon Wood value[0] = 0xf5; 392d2c02da5SSimon Wood value[1] = 0x00; 393d2c02da5SSimon Wood value[2] = 0x00; 394d2c02da5SSimon Wood value[3] = 0x00; 395d2c02da5SSimon Wood value[4] = 0x00; 396d2c02da5SSimon Wood value[5] = 0x00; 397d2c02da5SSimon Wood value[6] = 0x00; 398d2c02da5SSimon Wood 399d2c02da5SSimon Wood hid_hw_request(hid, report, HID_REQ_SET_REPORT); 400*c918fe78SMichal Malý spin_unlock_irqrestore(&entry->report_lock, flags); 401d2c02da5SSimon Wood return; 402d2c02da5SSimon Wood } 403d2c02da5SSimon Wood 404f8c23156SSimon Wood if (magnitude <= 0xaaaa) { 405f8c23156SSimon Wood expand_a = 0x0c * magnitude; 406f8c23156SSimon Wood expand_b = 0x80 * magnitude; 407f8c23156SSimon Wood } else { 408f8c23156SSimon Wood expand_a = (0x0c * 0xaaaa) + 0x06 * (magnitude - 0xaaaa); 409f8c23156SSimon Wood expand_b = (0x80 * 0xaaaa) + 0xff * (magnitude - 0xaaaa); 410f8c23156SSimon Wood } 41132c88cbcSSimon Wood 4121859762eSSimon Wood /* Adjust for non-MOMO wheels */ 41372529c65SMichal Malý switch (entry->wdata.product_id) { 4141859762eSSimon Wood case USB_DEVICE_ID_LOGITECH_MOMO_WHEEL: 4151859762eSSimon Wood case USB_DEVICE_ID_LOGITECH_MOMO_WHEEL2: 4161859762eSSimon Wood break; 4171859762eSSimon Wood default: 4181859762eSSimon Wood expand_a = expand_a >> 1; 4191859762eSSimon Wood break; 4201859762eSSimon Wood } 4211859762eSSimon Wood 42274479ba8SMichal Malý value[0] = 0xfe; 42374479ba8SMichal Malý value[1] = 0x0d; 424f8c23156SSimon Wood value[2] = expand_a / 0xaaaa; 425f8c23156SSimon Wood value[3] = expand_a / 0xaaaa; 426f8c23156SSimon Wood value[4] = expand_b / 0xaaaa; 42774479ba8SMichal Malý value[5] = 0x00; 42874479ba8SMichal Malý value[6] = 0x00; 42932c88cbcSSimon Wood 430d8814272SBenjamin Tissoires hid_hw_request(hid, report, HID_REQ_SET_REPORT); 431d2c02da5SSimon Wood 432d2c02da5SSimon Wood /* Activate Auto-Center */ 433d2c02da5SSimon Wood value[0] = 0x14; 434d2c02da5SSimon Wood value[1] = 0x00; 435d2c02da5SSimon Wood value[2] = 0x00; 436d2c02da5SSimon Wood value[3] = 0x00; 437d2c02da5SSimon Wood value[4] = 0x00; 438d2c02da5SSimon Wood value[5] = 0x00; 439d2c02da5SSimon Wood value[6] = 0x00; 440d2c02da5SSimon Wood 441d2c02da5SSimon Wood hid_hw_request(hid, report, HID_REQ_SET_REPORT); 442*c918fe78SMichal Malý spin_unlock_irqrestore(&entry->report_lock, flags); 44332c88cbcSSimon Wood } 44432c88cbcSSimon Wood 4456e2de8e0SMichal Malý /* Sends autocentering command compatible with Formula Force EX */ 446d0afd848SMichal Malý static void lg4ff_set_autocenter_ffex(struct input_dev *dev, u16 magnitude) 4476e2de8e0SMichal Malý { 4486e2de8e0SMichal Malý struct hid_device *hid = input_get_drvdata(dev); 4496e2de8e0SMichal Malý struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list; 4506e2de8e0SMichal Malý struct hid_report *report = list_entry(report_list->next, struct hid_report, list); 451*c918fe78SMichal Malý struct lg4ff_device_entry *entry; 452*c918fe78SMichal Malý struct lg_drv_data *drv_data; 453*c918fe78SMichal Malý unsigned long flags; 4542a552c30SMichal Malý s32 *value = report->field[0]->value; 4556e2de8e0SMichal Malý magnitude = magnitude * 90 / 65535; 4566e2de8e0SMichal Malý 457*c918fe78SMichal Malý drv_data = hid_get_drvdata(hid); 458*c918fe78SMichal Malý if (!drv_data) { 459*c918fe78SMichal Malý hid_err(hid, "Private driver data not found!\n"); 460*c918fe78SMichal Malý return; 461*c918fe78SMichal Malý } 462*c918fe78SMichal Malý 463*c918fe78SMichal Malý entry = drv_data->device_props; 464*c918fe78SMichal Malý if (!entry) { 465*c918fe78SMichal Malý hid_err(hid, "Device properties not found!\n"); 466*c918fe78SMichal Malý return; 467*c918fe78SMichal Malý } 468*c918fe78SMichal Malý 469*c918fe78SMichal Malý spin_lock_irqsave(&entry->report_lock, flags); 47074479ba8SMichal Malý value[0] = 0xfe; 47174479ba8SMichal Malý value[1] = 0x03; 47274479ba8SMichal Malý value[2] = magnitude >> 14; 47374479ba8SMichal Malý value[3] = magnitude >> 14; 47474479ba8SMichal Malý value[4] = magnitude; 47574479ba8SMichal Malý value[5] = 0x00; 47674479ba8SMichal Malý value[6] = 0x00; 4776e2de8e0SMichal Malý 478d8814272SBenjamin Tissoires hid_hw_request(hid, report, HID_REQ_SET_REPORT); 479*c918fe78SMichal Malý spin_unlock_irqrestore(&entry->report_lock, flags); 4806e2de8e0SMichal Malý } 4816e2de8e0SMichal Malý 48230bb75d7SMichal Malý /* Sends command to set range compatible with G25/G27/Driving Force GT */ 483d0afd848SMichal Malý static void lg4ff_set_range_g25(struct hid_device *hid, u16 range) 48430bb75d7SMichal Malý { 48530bb75d7SMichal Malý struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list; 48630bb75d7SMichal Malý struct hid_report *report = list_entry(report_list->next, struct hid_report, list); 487*c918fe78SMichal Malý struct lg4ff_device_entry *entry; 488*c918fe78SMichal Malý struct lg_drv_data *drv_data; 489*c918fe78SMichal Malý unsigned long flags; 4902a552c30SMichal Malý s32 *value = report->field[0]->value; 49174479ba8SMichal Malý 492*c918fe78SMichal Malý drv_data = hid_get_drvdata(hid); 493*c918fe78SMichal Malý if (!drv_data) { 494*c918fe78SMichal Malý hid_err(hid, "Private driver data not found!\n"); 495*c918fe78SMichal Malý return; 496*c918fe78SMichal Malý } 497*c918fe78SMichal Malý 498*c918fe78SMichal Malý entry = drv_data->device_props; 499*c918fe78SMichal Malý if (!entry) { 500*c918fe78SMichal Malý hid_err(hid, "Device properties not found!\n"); 501*c918fe78SMichal Malý return; 502*c918fe78SMichal Malý } 503*c918fe78SMichal Malý 50430bb75d7SMichal Malý dbg_hid("G25/G27/DFGT: setting range to %u\n", range); 50530bb75d7SMichal Malý 506*c918fe78SMichal Malý spin_lock_irqsave(&entry->report_lock, flags); 50774479ba8SMichal Malý value[0] = 0xf8; 50874479ba8SMichal Malý value[1] = 0x81; 50974479ba8SMichal Malý value[2] = range & 0x00ff; 51074479ba8SMichal Malý value[3] = (range & 0xff00) >> 8; 51174479ba8SMichal Malý value[4] = 0x00; 51274479ba8SMichal Malý value[5] = 0x00; 51374479ba8SMichal Malý value[6] = 0x00; 51430bb75d7SMichal Malý 515d8814272SBenjamin Tissoires hid_hw_request(hid, report, HID_REQ_SET_REPORT); 516*c918fe78SMichal Malý spin_unlock_irqrestore(&entry->report_lock, flags); 51730bb75d7SMichal Malý } 51830bb75d7SMichal Malý 51930bb75d7SMichal Malý /* Sends commands to set range compatible with Driving Force Pro wheel */ 520d0afd848SMichal Malý static void lg4ff_set_range_dfp(struct hid_device *hid, u16 range) 52130bb75d7SMichal Malý { 52230bb75d7SMichal Malý struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list; 52330bb75d7SMichal Malý struct hid_report *report = list_entry(report_list->next, struct hid_report, list); 52430bb75d7SMichal Malý int start_left, start_right, full_range; 525*c918fe78SMichal Malý struct lg4ff_device_entry *entry; 526*c918fe78SMichal Malý struct lg_drv_data *drv_data; 527*c918fe78SMichal Malý unsigned long flags; 5282a552c30SMichal Malý s32 *value = report->field[0]->value; 52974479ba8SMichal Malý 530*c918fe78SMichal Malý drv_data = hid_get_drvdata(hid); 531*c918fe78SMichal Malý if (!drv_data) { 532*c918fe78SMichal Malý hid_err(hid, "Private driver data not found!\n"); 533*c918fe78SMichal Malý return; 534*c918fe78SMichal Malý } 535*c918fe78SMichal Malý 536*c918fe78SMichal Malý entry = drv_data->device_props; 537*c918fe78SMichal Malý if (!entry) { 538*c918fe78SMichal Malý hid_err(hid, "Device properties not found!\n"); 539*c918fe78SMichal Malý return; 540*c918fe78SMichal Malý } 541*c918fe78SMichal Malý 54230bb75d7SMichal Malý dbg_hid("Driving Force Pro: setting range to %u\n", range); 54330bb75d7SMichal Malý 54430bb75d7SMichal Malý /* Prepare "coarse" limit command */ 545*c918fe78SMichal Malý spin_lock_irqsave(&entry->report_lock, flags); 54674479ba8SMichal Malý value[0] = 0xf8; 54774479ba8SMichal Malý value[1] = 0x00; /* Set later */ 54874479ba8SMichal Malý value[2] = 0x00; 54974479ba8SMichal Malý value[3] = 0x00; 55074479ba8SMichal Malý value[4] = 0x00; 55174479ba8SMichal Malý value[5] = 0x00; 55274479ba8SMichal Malý value[6] = 0x00; 55330bb75d7SMichal Malý 55430bb75d7SMichal Malý if (range > 200) { 55530bb75d7SMichal Malý report->field[0]->value[1] = 0x03; 55630bb75d7SMichal Malý full_range = 900; 55730bb75d7SMichal Malý } else { 55830bb75d7SMichal Malý report->field[0]->value[1] = 0x02; 55930bb75d7SMichal Malý full_range = 200; 56030bb75d7SMichal Malý } 561d8814272SBenjamin Tissoires hid_hw_request(hid, report, HID_REQ_SET_REPORT); 56230bb75d7SMichal Malý 56330bb75d7SMichal Malý /* Prepare "fine" limit command */ 56474479ba8SMichal Malý value[0] = 0x81; 56574479ba8SMichal Malý value[1] = 0x0b; 56674479ba8SMichal Malý value[2] = 0x00; 56774479ba8SMichal Malý value[3] = 0x00; 56874479ba8SMichal Malý value[4] = 0x00; 56974479ba8SMichal Malý value[5] = 0x00; 57074479ba8SMichal Malý value[6] = 0x00; 57130bb75d7SMichal Malý 57230bb75d7SMichal Malý if (range == 200 || range == 900) { /* Do not apply any fine limit */ 573d8814272SBenjamin Tissoires hid_hw_request(hid, report, HID_REQ_SET_REPORT); 574*c918fe78SMichal Malý spin_unlock_irqrestore(&entry->report_lock, flags); 57530bb75d7SMichal Malý return; 57630bb75d7SMichal Malý } 57730bb75d7SMichal Malý 57830bb75d7SMichal Malý /* Construct fine limit command */ 57930bb75d7SMichal Malý start_left = (((full_range - range + 1) * 2047) / full_range); 58030bb75d7SMichal Malý start_right = 0xfff - start_left; 58130bb75d7SMichal Malý 58274479ba8SMichal Malý value[2] = start_left >> 4; 58374479ba8SMichal Malý value[3] = start_right >> 4; 58474479ba8SMichal Malý value[4] = 0xff; 58574479ba8SMichal Malý value[5] = (start_right & 0xe) << 4 | (start_left & 0xe); 58674479ba8SMichal Malý value[6] = 0xff; 58730bb75d7SMichal Malý 588d8814272SBenjamin Tissoires hid_hw_request(hid, report, HID_REQ_SET_REPORT); 589*c918fe78SMichal Malý spin_unlock_irqrestore(&entry->report_lock, flags); 59030bb75d7SMichal Malý } 59130bb75d7SMichal Malý 592f31a2de3SMichal Malý static const struct lg4ff_compat_mode_switch *lg4ff_get_mode_switch_command(const u16 real_product_id, const u16 target_product_id) 593f31a2de3SMichal Malý { 594f31a2de3SMichal Malý switch (real_product_id) { 595f31a2de3SMichal Malý case USB_DEVICE_ID_LOGITECH_DFP_WHEEL: 596f31a2de3SMichal Malý switch (target_product_id) { 597f31a2de3SMichal Malý case USB_DEVICE_ID_LOGITECH_DFP_WHEEL: 598f31a2de3SMichal Malý return &lg4ff_mode_switch_ext01_dfp; 599f31a2de3SMichal Malý /* DFP can only be switched to its native mode */ 600f31a2de3SMichal Malý default: 601f31a2de3SMichal Malý return NULL; 602f31a2de3SMichal Malý } 603f31a2de3SMichal Malý break; 604f31a2de3SMichal Malý case USB_DEVICE_ID_LOGITECH_G25_WHEEL: 605f31a2de3SMichal Malý switch (target_product_id) { 606f31a2de3SMichal Malý case USB_DEVICE_ID_LOGITECH_DFP_WHEEL: 607f31a2de3SMichal Malý return &lg4ff_mode_switch_ext01_dfp; 608f31a2de3SMichal Malý case USB_DEVICE_ID_LOGITECH_G25_WHEEL: 609f31a2de3SMichal Malý return &lg4ff_mode_switch_ext16_g25; 610f31a2de3SMichal Malý /* G25 can only be switched to DFP mode or its native mode */ 611f31a2de3SMichal Malý default: 612f31a2de3SMichal Malý return NULL; 613f31a2de3SMichal Malý } 614f31a2de3SMichal Malý break; 615f31a2de3SMichal Malý case USB_DEVICE_ID_LOGITECH_G27_WHEEL: 616f31a2de3SMichal Malý switch (target_product_id) { 617f31a2de3SMichal Malý case USB_DEVICE_ID_LOGITECH_WHEEL: 618f31a2de3SMichal Malý return &lg4ff_mode_switch_ext09_dfex; 619f31a2de3SMichal Malý case USB_DEVICE_ID_LOGITECH_DFP_WHEEL: 620f31a2de3SMichal Malý return &lg4ff_mode_switch_ext09_dfp; 621f31a2de3SMichal Malý case USB_DEVICE_ID_LOGITECH_G25_WHEEL: 622f31a2de3SMichal Malý return &lg4ff_mode_switch_ext09_g25; 623f31a2de3SMichal Malý case USB_DEVICE_ID_LOGITECH_G27_WHEEL: 624f31a2de3SMichal Malý return &lg4ff_mode_switch_ext09_g27; 625f31a2de3SMichal Malý /* G27 can only be switched to DF-EX, DFP, G25 or its native mode */ 626f31a2de3SMichal Malý default: 627f31a2de3SMichal Malý return NULL; 628f31a2de3SMichal Malý } 629f31a2de3SMichal Malý break; 630f31a2de3SMichal Malý case USB_DEVICE_ID_LOGITECH_DFGT_WHEEL: 631f31a2de3SMichal Malý switch (target_product_id) { 632f31a2de3SMichal Malý case USB_DEVICE_ID_LOGITECH_WHEEL: 633f31a2de3SMichal Malý return &lg4ff_mode_switch_ext09_dfex; 634f31a2de3SMichal Malý case USB_DEVICE_ID_LOGITECH_DFP_WHEEL: 635f31a2de3SMichal Malý return &lg4ff_mode_switch_ext09_dfp; 636f31a2de3SMichal Malý case USB_DEVICE_ID_LOGITECH_DFGT_WHEEL: 637f31a2de3SMichal Malý return &lg4ff_mode_switch_ext09_dfgt; 638f31a2de3SMichal Malý /* DFGT can only be switched to DF-EX, DFP or its native mode */ 639f31a2de3SMichal Malý default: 640f31a2de3SMichal Malý return NULL; 641f31a2de3SMichal Malý } 642f31a2de3SMichal Malý break; 643f31a2de3SMichal Malý /* No other wheels have multiple modes */ 644f31a2de3SMichal Malý default: 645f31a2de3SMichal Malý return NULL; 646f31a2de3SMichal Malý } 647f31a2de3SMichal Malý } 648f31a2de3SMichal Malý 649e7c23449SMichal Malý static int lg4ff_switch_compatibility_mode(struct hid_device *hid, const struct lg4ff_compat_mode_switch *s) 65096440c8aSMichal Malý { 651c1740d13SMichal Malý struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list; 652c1740d13SMichal Malý struct hid_report *report = list_entry(report_list->next, struct hid_report, list); 653*c918fe78SMichal Malý struct lg4ff_device_entry *entry; 654*c918fe78SMichal Malý struct lg_drv_data *drv_data; 655*c918fe78SMichal Malý unsigned long flags; 6562a552c30SMichal Malý s32 *value = report->field[0]->value; 657e7c23449SMichal Malý u8 i; 65896440c8aSMichal Malý 659*c918fe78SMichal Malý drv_data = hid_get_drvdata(hid); 660*c918fe78SMichal Malý if (!drv_data) { 661*c918fe78SMichal Malý hid_err(hid, "Private driver data not found!\n"); 662*c918fe78SMichal Malý return -EINVAL; 663*c918fe78SMichal Malý } 664*c918fe78SMichal Malý 665*c918fe78SMichal Malý entry = drv_data->device_props; 666*c918fe78SMichal Malý if (!entry) { 667*c918fe78SMichal Malý hid_err(hid, "Device properties not found!\n"); 668*c918fe78SMichal Malý return -EINVAL; 669*c918fe78SMichal Malý } 670*c918fe78SMichal Malý 671*c918fe78SMichal Malý spin_lock_irqsave(&entry->report_lock, flags); 672e7c23449SMichal Malý for (i = 0; i < s->cmd_count; i++) { 673c1740d13SMichal Malý u8 j; 67496440c8aSMichal Malý 675c1740d13SMichal Malý for (j = 0; j < 7; j++) 676c1740d13SMichal Malý value[j] = s->cmd[j + (7*i)]; 677c1740d13SMichal Malý 678c1740d13SMichal Malý hid_hw_request(hid, report, HID_REQ_SET_REPORT); 67996440c8aSMichal Malý } 680*c918fe78SMichal Malý spin_unlock_irqrestore(&entry->report_lock, flags); 681c1740d13SMichal Malý hid_hw_wait(hid); 682e7c23449SMichal Malý return 0; 68396440c8aSMichal Malý } 68432c88cbcSSimon Wood 685b96d23ecSMichal Malý static ssize_t lg4ff_alternate_modes_show(struct device *dev, struct device_attribute *attr, char *buf) 686b96d23ecSMichal Malý { 687b96d23ecSMichal Malý struct hid_device *hid = to_hid_device(dev); 688b96d23ecSMichal Malý struct lg4ff_device_entry *entry; 689b96d23ecSMichal Malý struct lg_drv_data *drv_data; 690b96d23ecSMichal Malý ssize_t count = 0; 691b96d23ecSMichal Malý int i; 692b96d23ecSMichal Malý 693b96d23ecSMichal Malý drv_data = hid_get_drvdata(hid); 694b96d23ecSMichal Malý if (!drv_data) { 695b96d23ecSMichal Malý hid_err(hid, "Private driver data not found!\n"); 696b96d23ecSMichal Malý return 0; 697b96d23ecSMichal Malý } 698b96d23ecSMichal Malý 699b96d23ecSMichal Malý entry = drv_data->device_props; 700b96d23ecSMichal Malý if (!entry) { 701b96d23ecSMichal Malý hid_err(hid, "Device properties not found!\n"); 702b96d23ecSMichal Malý return 0; 703b96d23ecSMichal Malý } 704b96d23ecSMichal Malý 70572529c65SMichal Malý if (!entry->wdata.real_name) { 706b96d23ecSMichal Malý hid_err(hid, "NULL pointer to string\n"); 707b96d23ecSMichal Malý return 0; 708b96d23ecSMichal Malý } 709b96d23ecSMichal Malý 710b96d23ecSMichal Malý for (i = 0; i < LG4FF_MODE_MAX_IDX; i++) { 71172529c65SMichal Malý if (entry->wdata.alternate_modes & BIT(i)) { 712b96d23ecSMichal Malý /* Print tag and full name */ 713b96d23ecSMichal Malý count += scnprintf(buf + count, PAGE_SIZE - count, "%s: %s", 714b96d23ecSMichal Malý lg4ff_alternate_modes[i].tag, 71572529c65SMichal Malý !lg4ff_alternate_modes[i].product_id ? entry->wdata.real_name : lg4ff_alternate_modes[i].name); 716b96d23ecSMichal Malý if (count >= PAGE_SIZE - 1) 717b96d23ecSMichal Malý return count; 718b96d23ecSMichal Malý 719b96d23ecSMichal Malý /* Mark the currently active mode with an asterisk */ 72072529c65SMichal Malý if (lg4ff_alternate_modes[i].product_id == entry->wdata.product_id || 72172529c65SMichal Malý (lg4ff_alternate_modes[i].product_id == 0 && entry->wdata.product_id == entry->wdata.real_product_id)) 722b96d23ecSMichal Malý count += scnprintf(buf + count, PAGE_SIZE - count, " *\n"); 723b96d23ecSMichal Malý else 724b96d23ecSMichal Malý count += scnprintf(buf + count, PAGE_SIZE - count, "\n"); 725b96d23ecSMichal Malý 726b96d23ecSMichal Malý if (count >= PAGE_SIZE - 1) 727b96d23ecSMichal Malý return count; 728b96d23ecSMichal Malý } 729b96d23ecSMichal Malý } 730b96d23ecSMichal Malý 731b96d23ecSMichal Malý return count; 732b96d23ecSMichal Malý } 733b96d23ecSMichal Malý 734b96d23ecSMichal Malý static ssize_t lg4ff_alternate_modes_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) 735b96d23ecSMichal Malý { 736f31a2de3SMichal Malý struct hid_device *hid = to_hid_device(dev); 737f31a2de3SMichal Malý struct lg4ff_device_entry *entry; 738f31a2de3SMichal Malý struct lg_drv_data *drv_data; 739f31a2de3SMichal Malý const struct lg4ff_compat_mode_switch *s; 740f31a2de3SMichal Malý u16 target_product_id = 0; 741f31a2de3SMichal Malý int i, ret; 742f31a2de3SMichal Malý char *lbuf; 743f31a2de3SMichal Malý 744f31a2de3SMichal Malý drv_data = hid_get_drvdata(hid); 745f31a2de3SMichal Malý if (!drv_data) { 746f31a2de3SMichal Malý hid_err(hid, "Private driver data not found!\n"); 747f31a2de3SMichal Malý return -EINVAL; 748f31a2de3SMichal Malý } 749f31a2de3SMichal Malý 750f31a2de3SMichal Malý entry = drv_data->device_props; 751f31a2de3SMichal Malý if (!entry) { 752f31a2de3SMichal Malý hid_err(hid, "Device properties not found!\n"); 753f31a2de3SMichal Malý return -EINVAL; 754f31a2de3SMichal Malý } 755f31a2de3SMichal Malý 756f31a2de3SMichal Malý /* Allow \n at the end of the input parameter */ 757f31a2de3SMichal Malý lbuf = kasprintf(GFP_KERNEL, "%s", buf); 758f31a2de3SMichal Malý if (!lbuf) 759f31a2de3SMichal Malý return -ENOMEM; 760f31a2de3SMichal Malý 761f31a2de3SMichal Malý i = strlen(lbuf); 762f31a2de3SMichal Malý if (lbuf[i-1] == '\n') { 763f31a2de3SMichal Malý if (i == 1) { 764f31a2de3SMichal Malý kfree(lbuf); 765f31a2de3SMichal Malý return -EINVAL; 766f31a2de3SMichal Malý } 767f31a2de3SMichal Malý lbuf[i-1] = '\0'; 768f31a2de3SMichal Malý } 769f31a2de3SMichal Malý 770f31a2de3SMichal Malý for (i = 0; i < LG4FF_MODE_MAX_IDX; i++) { 771f31a2de3SMichal Malý const u16 mode_product_id = lg4ff_alternate_modes[i].product_id; 772f31a2de3SMichal Malý const char *tag = lg4ff_alternate_modes[i].tag; 773f31a2de3SMichal Malý 77472529c65SMichal Malý if (entry->wdata.alternate_modes & BIT(i)) { 775f31a2de3SMichal Malý if (!strcmp(tag, lbuf)) { 776f31a2de3SMichal Malý if (!mode_product_id) 77772529c65SMichal Malý target_product_id = entry->wdata.real_product_id; 778f31a2de3SMichal Malý else 779f31a2de3SMichal Malý target_product_id = mode_product_id; 780f31a2de3SMichal Malý break; 781f31a2de3SMichal Malý } 782f31a2de3SMichal Malý } 783f31a2de3SMichal Malý } 784f31a2de3SMichal Malý 785f31a2de3SMichal Malý if (i == LG4FF_MODE_MAX_IDX) { 786f31a2de3SMichal Malý hid_info(hid, "Requested mode \"%s\" is not supported by the device\n", lbuf); 787f31a2de3SMichal Malý kfree(lbuf); 788f31a2de3SMichal Malý return -EINVAL; 789f31a2de3SMichal Malý } 790f31a2de3SMichal Malý kfree(lbuf); /* Not needed anymore */ 791f31a2de3SMichal Malý 79272529c65SMichal Malý if (target_product_id == entry->wdata.product_id) /* Nothing to do */ 793f31a2de3SMichal Malý return count; 794f31a2de3SMichal Malý 795f31a2de3SMichal Malý /* Automatic switching has to be disabled for the switch to DF-EX mode to work correctly */ 796f31a2de3SMichal Malý if (target_product_id == USB_DEVICE_ID_LOGITECH_WHEEL && !lg4ff_no_autoswitch) { 797f31a2de3SMichal 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", 79872529c65SMichal Malý entry->wdata.real_name); 799f31a2de3SMichal Malý return -EINVAL; 800f31a2de3SMichal Malý } 801f31a2de3SMichal Malý 802f31a2de3SMichal Malý /* Take care of hardware limitations */ 80372529c65SMichal Malý if ((entry->wdata.real_product_id == USB_DEVICE_ID_LOGITECH_DFP_WHEEL || entry->wdata.real_product_id == USB_DEVICE_ID_LOGITECH_G25_WHEEL) && 80472529c65SMichal Malý entry->wdata.product_id > target_product_id) { 80572529c65SMichal Malý hid_info(hid, "\"%s\" cannot be switched back into \"%s\" mode\n", entry->wdata.real_name, lg4ff_alternate_modes[i].name); 806f31a2de3SMichal Malý return -EINVAL; 807f31a2de3SMichal Malý } 808f31a2de3SMichal Malý 80972529c65SMichal Malý s = lg4ff_get_mode_switch_command(entry->wdata.real_product_id, target_product_id); 810f31a2de3SMichal Malý if (!s) { 811f31a2de3SMichal Malý hid_err(hid, "Invalid target product ID %X\n", target_product_id); 812f31a2de3SMichal Malý return -EINVAL; 813f31a2de3SMichal Malý } 814f31a2de3SMichal Malý 815f31a2de3SMichal Malý ret = lg4ff_switch_compatibility_mode(hid, s); 816f31a2de3SMichal Malý return (ret == 0 ? count : ret); 817b96d23ecSMichal Malý } 818b96d23ecSMichal Malý static DEVICE_ATTR(alternate_modes, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH, lg4ff_alternate_modes_show, lg4ff_alternate_modes_store); 819b96d23ecSMichal Malý 820fbf85e2aSMichal Malý /* Export the currently set range of the wheel */ 821fbf85e2aSMichal Malý static ssize_t lg4ff_range_show(struct device *dev, struct device_attribute *attr, 8222f1cec32SVivien Didelot char *buf) 82330bb75d7SMichal Malý { 82430bb75d7SMichal Malý struct hid_device *hid = to_hid_device(dev); 8253b6b17b7SMichal Malý struct lg4ff_device_entry *entry; 8263b6b17b7SMichal Malý struct lg_drv_data *drv_data; 82730bb75d7SMichal Malý size_t count; 82830bb75d7SMichal Malý 8293b6b17b7SMichal Malý drv_data = hid_get_drvdata(hid); 8303b6b17b7SMichal Malý if (!drv_data) { 8313b6b17b7SMichal Malý hid_err(hid, "Private driver data not found!\n"); 8323b6b17b7SMichal Malý return 0; 83330bb75d7SMichal Malý } 8343b6b17b7SMichal Malý 8353b6b17b7SMichal Malý entry = drv_data->device_props; 8363b6b17b7SMichal Malý if (!entry) { 8373b6b17b7SMichal Malý hid_err(hid, "Device properties not found!\n"); 83830bb75d7SMichal Malý return 0; 83930bb75d7SMichal Malý } 84030bb75d7SMichal Malý 84172529c65SMichal Malý count = scnprintf(buf, PAGE_SIZE, "%u\n", entry->wdata.range); 84230bb75d7SMichal Malý return count; 84330bb75d7SMichal Malý } 84430bb75d7SMichal Malý 84530bb75d7SMichal Malý /* Set range to user specified value, call appropriate function 84630bb75d7SMichal Malý * according to the type of the wheel */ 847fbf85e2aSMichal Malý static ssize_t lg4ff_range_store(struct device *dev, struct device_attribute *attr, 8482f1cec32SVivien Didelot const char *buf, size_t count) 84930bb75d7SMichal Malý { 85030bb75d7SMichal Malý struct hid_device *hid = to_hid_device(dev); 8513b6b17b7SMichal Malý struct lg4ff_device_entry *entry; 8523b6b17b7SMichal Malý struct lg_drv_data *drv_data; 8532a552c30SMichal Malý u16 range = simple_strtoul(buf, NULL, 10); 85430bb75d7SMichal Malý 8553b6b17b7SMichal Malý drv_data = hid_get_drvdata(hid); 8563b6b17b7SMichal Malý if (!drv_data) { 8573b6b17b7SMichal Malý hid_err(hid, "Private driver data not found!\n"); 85829ff6657SSimon Wood return -EINVAL; 85930bb75d7SMichal Malý } 8603b6b17b7SMichal Malý 8613b6b17b7SMichal Malý entry = drv_data->device_props; 8623b6b17b7SMichal Malý if (!entry) { 8633b6b17b7SMichal Malý hid_err(hid, "Device properties not found!\n"); 86429ff6657SSimon Wood return -EINVAL; 86530bb75d7SMichal Malý } 86630bb75d7SMichal Malý 86730bb75d7SMichal Malý if (range == 0) 86872529c65SMichal Malý range = entry->wdata.max_range; 86930bb75d7SMichal Malý 87030bb75d7SMichal Malý /* Check if the wheel supports range setting 87130bb75d7SMichal Malý * and that the range is within limits for the wheel */ 87272529c65SMichal Malý if (entry->wdata.set_range && range >= entry->wdata.min_range && range <= entry->wdata.max_range) { 87372529c65SMichal Malý entry->wdata.set_range(hid, range); 87472529c65SMichal Malý entry->wdata.range = range; 87530bb75d7SMichal Malý } 87630bb75d7SMichal Malý 87730bb75d7SMichal Malý return count; 87830bb75d7SMichal Malý } 879fbf85e2aSMichal Malý static DEVICE_ATTR(range, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH, lg4ff_range_show, lg4ff_range_store); 88030bb75d7SMichal Malý 881b96d23ecSMichal Malý static ssize_t lg4ff_real_id_show(struct device *dev, struct device_attribute *attr, char *buf) 882b96d23ecSMichal Malý { 883b96d23ecSMichal Malý struct hid_device *hid = to_hid_device(dev); 884b96d23ecSMichal Malý struct lg4ff_device_entry *entry; 885b96d23ecSMichal Malý struct lg_drv_data *drv_data; 886b96d23ecSMichal Malý size_t count; 887b96d23ecSMichal Malý 888b96d23ecSMichal Malý drv_data = hid_get_drvdata(hid); 889b96d23ecSMichal Malý if (!drv_data) { 890b96d23ecSMichal Malý hid_err(hid, "Private driver data not found!\n"); 891b96d23ecSMichal Malý return 0; 892b96d23ecSMichal Malý } 893b96d23ecSMichal Malý 894b96d23ecSMichal Malý entry = drv_data->device_props; 895b96d23ecSMichal Malý if (!entry) { 896b96d23ecSMichal Malý hid_err(hid, "Device properties not found!\n"); 897b96d23ecSMichal Malý return 0; 898b96d23ecSMichal Malý } 899b96d23ecSMichal Malý 90072529c65SMichal Malý if (!entry->wdata.real_tag || !entry->wdata.real_name) { 901b96d23ecSMichal Malý hid_err(hid, "NULL pointer to string\n"); 902b96d23ecSMichal Malý return 0; 903b96d23ecSMichal Malý } 904b96d23ecSMichal Malý 90572529c65SMichal Malý count = scnprintf(buf, PAGE_SIZE, "%s: %s\n", entry->wdata.real_tag, entry->wdata.real_name); 906b96d23ecSMichal Malý return count; 907b96d23ecSMichal Malý } 908b96d23ecSMichal Malý 909b96d23ecSMichal Malý static ssize_t lg4ff_real_id_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) 910b96d23ecSMichal Malý { 911b96d23ecSMichal Malý /* Real ID is a read-only value */ 912b96d23ecSMichal Malý return -EPERM; 913b96d23ecSMichal Malý } 914b96d23ecSMichal Malý static DEVICE_ATTR(real_id, S_IRUGO, lg4ff_real_id_show, lg4ff_real_id_store); 915b96d23ecSMichal Malý 91622bcefdcSSimon Wood #ifdef CONFIG_LEDS_CLASS 9172a552c30SMichal Malý static void lg4ff_set_leds(struct hid_device *hid, u8 leds) 91822bcefdcSSimon Wood { 91922bcefdcSSimon Wood struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list; 92022bcefdcSSimon Wood struct hid_report *report = list_entry(report_list->next, struct hid_report, list); 921*c918fe78SMichal Malý struct lg_drv_data *drv_data; 922*c918fe78SMichal Malý struct lg4ff_device_entry *entry; 923*c918fe78SMichal Malý unsigned long flags; 9242a552c30SMichal Malý s32 *value = report->field[0]->value; 92522bcefdcSSimon Wood 926*c918fe78SMichal Malý drv_data = hid_get_drvdata(hid); 927*c918fe78SMichal Malý if (!drv_data) { 928*c918fe78SMichal Malý hid_err(hid, "Private driver data not found!\n"); 929*c918fe78SMichal Malý return; 930*c918fe78SMichal Malý } 931*c918fe78SMichal Malý 932*c918fe78SMichal Malý entry = drv_data->device_props; 933*c918fe78SMichal Malý if (!entry) { 934*c918fe78SMichal Malý hid_err(hid, "Device properties not found!\n"); 935*c918fe78SMichal Malý return; 936*c918fe78SMichal Malý } 937*c918fe78SMichal Malý 938*c918fe78SMichal Malý spin_lock_irqsave(&entry->report_lock, flags); 93974479ba8SMichal Malý value[0] = 0xf8; 94074479ba8SMichal Malý value[1] = 0x12; 94174479ba8SMichal Malý value[2] = leds; 94274479ba8SMichal Malý value[3] = 0x00; 94374479ba8SMichal Malý value[4] = 0x00; 94474479ba8SMichal Malý value[5] = 0x00; 94574479ba8SMichal Malý value[6] = 0x00; 946d8814272SBenjamin Tissoires hid_hw_request(hid, report, HID_REQ_SET_REPORT); 947*c918fe78SMichal Malý spin_unlock_irqrestore(&entry->report_lock, flags); 94822bcefdcSSimon Wood } 94922bcefdcSSimon Wood 95022bcefdcSSimon Wood static void lg4ff_led_set_brightness(struct led_classdev *led_cdev, 95122bcefdcSSimon Wood enum led_brightness value) 95222bcefdcSSimon Wood { 95322bcefdcSSimon Wood struct device *dev = led_cdev->dev->parent; 95422bcefdcSSimon Wood struct hid_device *hid = container_of(dev, struct hid_device, dev); 9554629fd16SAxel Lin struct lg_drv_data *drv_data = hid_get_drvdata(hid); 95622bcefdcSSimon Wood struct lg4ff_device_entry *entry; 95722bcefdcSSimon Wood int i, state = 0; 95822bcefdcSSimon Wood 95922bcefdcSSimon Wood if (!drv_data) { 96022bcefdcSSimon Wood hid_err(hid, "Device data not found."); 96122bcefdcSSimon Wood return; 96222bcefdcSSimon Wood } 96322bcefdcSSimon Wood 964371a1d9eSMichal Malý entry = drv_data->device_props; 96522bcefdcSSimon Wood 96622bcefdcSSimon Wood if (!entry) { 96722bcefdcSSimon Wood hid_err(hid, "Device properties not found."); 96822bcefdcSSimon Wood return; 96922bcefdcSSimon Wood } 97022bcefdcSSimon Wood 97122bcefdcSSimon Wood for (i = 0; i < 5; i++) { 97272529c65SMichal Malý if (led_cdev != entry->wdata.led[i]) 97322bcefdcSSimon Wood continue; 97472529c65SMichal Malý state = (entry->wdata.led_state >> i) & 1; 97522bcefdcSSimon Wood if (value == LED_OFF && state) { 97672529c65SMichal Malý entry->wdata.led_state &= ~(1 << i); 97772529c65SMichal Malý lg4ff_set_leds(hid, entry->wdata.led_state); 97822bcefdcSSimon Wood } else if (value != LED_OFF && !state) { 97972529c65SMichal Malý entry->wdata.led_state |= 1 << i; 98072529c65SMichal Malý lg4ff_set_leds(hid, entry->wdata.led_state); 98122bcefdcSSimon Wood } 98222bcefdcSSimon Wood break; 98322bcefdcSSimon Wood } 98422bcefdcSSimon Wood } 98522bcefdcSSimon Wood 98622bcefdcSSimon Wood static enum led_brightness lg4ff_led_get_brightness(struct led_classdev *led_cdev) 98722bcefdcSSimon Wood { 98822bcefdcSSimon Wood struct device *dev = led_cdev->dev->parent; 98922bcefdcSSimon Wood struct hid_device *hid = container_of(dev, struct hid_device, dev); 9904629fd16SAxel Lin struct lg_drv_data *drv_data = hid_get_drvdata(hid); 99122bcefdcSSimon Wood struct lg4ff_device_entry *entry; 99222bcefdcSSimon Wood int i, value = 0; 99322bcefdcSSimon Wood 99422bcefdcSSimon Wood if (!drv_data) { 99522bcefdcSSimon Wood hid_err(hid, "Device data not found."); 99622bcefdcSSimon Wood return LED_OFF; 99722bcefdcSSimon Wood } 99822bcefdcSSimon Wood 999371a1d9eSMichal Malý entry = drv_data->device_props; 100022bcefdcSSimon Wood 100122bcefdcSSimon Wood if (!entry) { 100222bcefdcSSimon Wood hid_err(hid, "Device properties not found."); 100322bcefdcSSimon Wood return LED_OFF; 100422bcefdcSSimon Wood } 100522bcefdcSSimon Wood 100622bcefdcSSimon Wood for (i = 0; i < 5; i++) 100772529c65SMichal Malý if (led_cdev == entry->wdata.led[i]) { 100872529c65SMichal Malý value = (entry->wdata.led_state >> i) & 1; 100922bcefdcSSimon Wood break; 101022bcefdcSSimon Wood } 101122bcefdcSSimon Wood 101222bcefdcSSimon Wood return value ? LED_FULL : LED_OFF; 101322bcefdcSSimon Wood } 101422bcefdcSSimon Wood #endif 101522bcefdcSSimon Wood 1016e7c23449SMichal Malý static u16 lg4ff_identify_multimode_wheel(struct hid_device *hid, const u16 reported_product_id, const u16 bcdDevice) 1017e7c23449SMichal Malý { 1018e7c23449SMichal Malý const struct lg4ff_wheel_ident_checklist *checklist; 1019e7c23449SMichal Malý int i, from_idx, to_idx; 1020e7c23449SMichal Malý 1021e7c23449SMichal Malý switch (reported_product_id) { 1022e7c23449SMichal Malý case USB_DEVICE_ID_LOGITECH_WHEEL: 1023e7c23449SMichal Malý case USB_DEVICE_ID_LOGITECH_DFP_WHEEL: 1024e7c23449SMichal Malý checklist = &lg4ff_main_checklist; 1025e7c23449SMichal Malý from_idx = 0; 1026e7c23449SMichal Malý to_idx = checklist->count - 1; 1027e7c23449SMichal Malý break; 1028e7c23449SMichal Malý case USB_DEVICE_ID_LOGITECH_G25_WHEEL: 1029e7c23449SMichal Malý checklist = &lg4ff_main_checklist; 1030e7c23449SMichal Malý from_idx = 0; 1031e7c23449SMichal Malý to_idx = checklist->count - 2; /* End identity check at G25 */ 1032e7c23449SMichal Malý break; 1033e7c23449SMichal Malý case USB_DEVICE_ID_LOGITECH_G27_WHEEL: 1034e7c23449SMichal Malý checklist = &lg4ff_main_checklist; 1035e7c23449SMichal Malý from_idx = 1; /* Start identity check at G27 */ 1036e7c23449SMichal Malý to_idx = checklist->count - 3; /* End identity check at G27 */ 1037e7c23449SMichal Malý break; 1038e7c23449SMichal Malý case USB_DEVICE_ID_LOGITECH_DFGT_WHEEL: 1039e7c23449SMichal Malý checklist = &lg4ff_main_checklist; 1040e7c23449SMichal Malý from_idx = 0; 1041e7c23449SMichal Malý to_idx = checklist->count - 4; /* End identity check at DFGT */ 1042e7c23449SMichal Malý break; 1043e7c23449SMichal Malý default: 1044e7c23449SMichal Malý return 0; 1045e7c23449SMichal Malý } 1046e7c23449SMichal Malý 1047e7c23449SMichal Malý for (i = from_idx; i <= to_idx; i++) { 1048e7c23449SMichal Malý const u16 mask = checklist->models[i]->mask; 1049e7c23449SMichal Malý const u16 result = checklist->models[i]->result; 1050e7c23449SMichal Malý const u16 real_product_id = checklist->models[i]->real_product_id; 1051e7c23449SMichal Malý 1052e7c23449SMichal Malý if ((bcdDevice & mask) == result) { 1053e7c23449SMichal Malý dbg_hid("Found wheel with real PID %X whose reported PID is %X\n", real_product_id, reported_product_id); 1054e7c23449SMichal Malý return real_product_id; 1055e7c23449SMichal Malý } 1056e7c23449SMichal Malý } 1057e7c23449SMichal Malý 1058f31a2de3SMichal Malý /* No match found. This is either Driving Force or an unknown 1059f31a2de3SMichal Malý * wheel model, do not touch it */ 1060e7c23449SMichal Malý dbg_hid("Wheel with bcdDevice %X was not recognized as multimode wheel, leaving in its current mode\n", bcdDevice); 1061e7c23449SMichal Malý return 0; 1062e7c23449SMichal Malý } 1063e7c23449SMichal Malý 1064e7c23449SMichal Malý static int lg4ff_handle_multimode_wheel(struct hid_device *hid, u16 *real_product_id, const u16 bcdDevice) 1065e7c23449SMichal Malý { 1066e7c23449SMichal Malý const u16 reported_product_id = hid->product; 1067e7c23449SMichal Malý int ret; 1068e7c23449SMichal Malý 1069e7c23449SMichal Malý *real_product_id = lg4ff_identify_multimode_wheel(hid, reported_product_id, bcdDevice); 1070e7c23449SMichal Malý /* Probed wheel is not a multimode wheel */ 1071e7c23449SMichal Malý if (!*real_product_id) { 1072e7c23449SMichal Malý *real_product_id = reported_product_id; 1073e7c23449SMichal Malý dbg_hid("Wheel is not a multimode wheel\n"); 1074e7c23449SMichal Malý return LG4FF_MMODE_NOT_MULTIMODE; 1075e7c23449SMichal Malý } 1076e7c23449SMichal Malý 1077e7c23449SMichal Malý /* Switch from "Driving Force" mode to native mode automatically. 1078e7c23449SMichal Malý * Otherwise keep the wheel in its current mode */ 1079e7c23449SMichal Malý if (reported_product_id == USB_DEVICE_ID_LOGITECH_WHEEL && 1080a54dc779SMichal Malý reported_product_id != *real_product_id && 1081a54dc779SMichal Malý !lg4ff_no_autoswitch) { 1082f31a2de3SMichal Malý const struct lg4ff_compat_mode_switch *s = lg4ff_get_mode_switch_command(*real_product_id, *real_product_id); 1083e7c23449SMichal Malý 1084f31a2de3SMichal Malý if (!s) { 1085e7c23449SMichal Malý hid_err(hid, "Invalid product id %X\n", *real_product_id); 1086b96d23ecSMichal Malý return LG4FF_MMODE_NOT_MULTIMODE; 1087e7c23449SMichal Malý } 1088e7c23449SMichal Malý 1089e7c23449SMichal Malý ret = lg4ff_switch_compatibility_mode(hid, s); 1090e7c23449SMichal Malý if (ret) { 1091e7c23449SMichal Malý /* Wheel could not have been switched to native mode, 1092e7c23449SMichal Malý * leave it in "Driving Force" mode and continue */ 1093e7c23449SMichal Malý hid_err(hid, "Unable to switch wheel mode, errno %d\n", ret); 1094b96d23ecSMichal Malý return LG4FF_MMODE_IS_MULTIMODE; 1095e7c23449SMichal Malý } 1096e7c23449SMichal Malý return LG4FF_MMODE_SWITCHED; 1097e7c23449SMichal Malý } 1098e7c23449SMichal Malý 1099b96d23ecSMichal Malý return LG4FF_MMODE_IS_MULTIMODE; 1100e7c23449SMichal Malý } 1101e7c23449SMichal Malý 1102e7c23449SMichal Malý 110332c88cbcSSimon Wood int lg4ff_init(struct hid_device *hid) 110432c88cbcSSimon Wood { 110532c88cbcSSimon Wood struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list); 110632c88cbcSSimon Wood struct input_dev *dev = hidinput->input; 1107e7c23449SMichal Malý const struct usb_device_descriptor *udesc = &(hid_to_usb_dev(hid)->descriptor); 1108e7c23449SMichal Malý const u16 bcdDevice = le16_to_cpu(udesc->bcdDevice); 110930bb75d7SMichal Malý struct lg4ff_device_entry *entry; 11103b6b17b7SMichal Malý struct lg_drv_data *drv_data; 1111b96d23ecSMichal Malý int error, i, j; 1112b96d23ecSMichal Malý int mmode_ret, mmode_idx = -1; 1113e7c23449SMichal Malý u16 real_product_id; 111432c88cbcSSimon Wood 111532c88cbcSSimon Wood /* Check that the report looks ok */ 11160fb6bd06SKees Cook if (!hid_validate_values(hid, HID_OUTPUT_REPORT, 0, 0, 7)) 111732c88cbcSSimon Wood return -1; 111832c88cbcSSimon Wood 111972529c65SMichal Malý drv_data = hid_get_drvdata(hid); 112072529c65SMichal Malý if (!drv_data) { 112172529c65SMichal Malý hid_err(hid, "Cannot add device, private driver data not allocated\n"); 112272529c65SMichal Malý return -1; 112372529c65SMichal Malý } 112472529c65SMichal Malý entry = kzalloc(sizeof(*entry), GFP_KERNEL); 112572529c65SMichal Malý if (!entry) 112672529c65SMichal Malý return -ENOMEM; 1127*c918fe78SMichal Malý spin_lock_init(&entry->report_lock); 112872529c65SMichal Malý drv_data->device_props = entry; 112972529c65SMichal Malý 1130e7c23449SMichal Malý /* Check if a multimode wheel has been connected and 1131e7c23449SMichal Malý * handle it appropriately */ 1132b96d23ecSMichal Malý mmode_ret = lg4ff_handle_multimode_wheel(hid, &real_product_id, bcdDevice); 1133e7c23449SMichal Malý 1134e7c23449SMichal Malý /* Wheel has been told to switch to native mode. There is no point in going on 1135e7c23449SMichal Malý * with the initialization as the wheel will do a USB reset when it switches mode 1136e7c23449SMichal Malý */ 1137b96d23ecSMichal Malý if (mmode_ret == LG4FF_MMODE_SWITCHED) 1138e7c23449SMichal Malý return 0; 113972529c65SMichal Malý else if (mmode_ret < 0) { 114072529c65SMichal Malý hid_err(hid, "Unable to switch device mode during initialization, errno %d\n", mmode_ret); 114172529c65SMichal Malý error = mmode_ret; 114272529c65SMichal Malý goto err_init; 114372529c65SMichal Malý } 1144e7c23449SMichal Malý 11457362cd22SMichal Malý /* Check what wheel has been connected */ 11467362cd22SMichal Malý for (i = 0; i < ARRAY_SIZE(lg4ff_devices); i++) { 11477362cd22SMichal Malý if (hid->product == lg4ff_devices[i].product_id) { 11487362cd22SMichal Malý dbg_hid("Found compatible device, product ID %04X\n", lg4ff_devices[i].product_id); 11497362cd22SMichal Malý break; 11507362cd22SMichal Malý } 11517362cd22SMichal Malý } 11527362cd22SMichal Malý 11537362cd22SMichal Malý if (i == ARRAY_SIZE(lg4ff_devices)) { 11549c2a6bd1SMichal 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. " 11559c2a6bd1SMichal Malý "Please report this as a bug to LKML, Simon Wood <simon@mungewell.org> or " 11569c2a6bd1SMichal Malý "Michal Maly <madcatxster@devoid-pointer.net>\n"); 115772529c65SMichal Malý error = -1; 115872529c65SMichal Malý goto err_init; 11597362cd22SMichal Malý } 11607362cd22SMichal Malý 1161b96d23ecSMichal Malý if (mmode_ret == LG4FF_MMODE_IS_MULTIMODE) { 1162b96d23ecSMichal Malý for (mmode_idx = 0; mmode_idx < ARRAY_SIZE(lg4ff_multimode_wheels); mmode_idx++) { 1163b96d23ecSMichal Malý if (real_product_id == lg4ff_multimode_wheels[mmode_idx].product_id) 1164b96d23ecSMichal Malý break; 1165b96d23ecSMichal Malý } 1166b96d23ecSMichal Malý 1167b96d23ecSMichal Malý if (mmode_idx == ARRAY_SIZE(lg4ff_multimode_wheels)) { 1168b96d23ecSMichal Malý hid_err(hid, "Device product ID %X is not listed as a multimode wheel", real_product_id); 116972529c65SMichal Malý error = -1; 117072529c65SMichal Malý goto err_init; 1171b96d23ecSMichal Malý } 1172b96d23ecSMichal Malý } 1173b96d23ecSMichal Malý 11747362cd22SMichal Malý /* Set supported force feedback capabilities */ 11757362cd22SMichal Malý for (j = 0; lg4ff_devices[i].ff_effects[j] >= 0; j++) 11767362cd22SMichal Malý set_bit(lg4ff_devices[i].ff_effects[j], dev->ffbit); 117732c88cbcSSimon Wood 1178d0afd848SMichal Malý error = input_ff_create_memless(dev, NULL, lg4ff_play); 117932c88cbcSSimon Wood 118032c88cbcSSimon Wood if (error) 118172529c65SMichal Malý goto err_init; 118232c88cbcSSimon Wood 118372529c65SMichal Malý entry->wdata.product_id = lg4ff_devices[i].product_id; 118472529c65SMichal Malý entry->wdata.real_product_id = real_product_id; 118572529c65SMichal Malý entry->wdata.min_range = lg4ff_devices[i].min_range; 118672529c65SMichal Malý entry->wdata.max_range = lg4ff_devices[i].max_range; 118772529c65SMichal Malý entry->wdata.set_range = lg4ff_devices[i].set_range; 1188b96d23ecSMichal Malý if (mmode_ret == LG4FF_MMODE_IS_MULTIMODE) { 1189b96d23ecSMichal Malý BUG_ON(mmode_idx == -1); 119072529c65SMichal Malý entry->wdata.alternate_modes = lg4ff_multimode_wheels[mmode_idx].alternate_modes; 119172529c65SMichal Malý entry->wdata.real_tag = lg4ff_multimode_wheels[mmode_idx].real_tag; 119272529c65SMichal Malý entry->wdata.real_name = lg4ff_multimode_wheels[mmode_idx].real_name; 1193b96d23ecSMichal Malý } 119430bb75d7SMichal Malý 1195114a55cfSSimon Wood /* Check if autocentering is available and 1196114a55cfSSimon Wood * set the centering force to zero by default */ 1197114a55cfSSimon Wood if (test_bit(FF_AUTOCENTER, dev->ffbit)) { 1198e7c23449SMichal Malý /* Formula Force EX expects different autocentering command */ 1199e7c23449SMichal Malý if ((bcdDevice >> 8) == LG4FF_FFEX_REV_MAJ && 1200e7c23449SMichal Malý (bcdDevice & 0xff) == LG4FF_FFEX_REV_MIN) 1201d0afd848SMichal Malý dev->ff->set_autocenter = lg4ff_set_autocenter_ffex; 1202114a55cfSSimon Wood else 1203d0afd848SMichal Malý dev->ff->set_autocenter = lg4ff_set_autocenter_default; 1204114a55cfSSimon Wood 1205114a55cfSSimon Wood dev->ff->set_autocenter(dev, 0); 1206114a55cfSSimon Wood } 1207114a55cfSSimon Wood 120830bb75d7SMichal Malý /* Create sysfs interface */ 120930bb75d7SMichal Malý error = device_create_file(&hid->dev, &dev_attr_range); 121030bb75d7SMichal Malý if (error) 121172529c65SMichal Malý goto err_init; 1212b96d23ecSMichal Malý if (mmode_ret == LG4FF_MMODE_IS_MULTIMODE) { 1213b96d23ecSMichal Malý error = device_create_file(&hid->dev, &dev_attr_real_id); 1214b96d23ecSMichal Malý if (error) 121572529c65SMichal Malý goto err_init; 121672529c65SMichal Malý 1217b96d23ecSMichal Malý error = device_create_file(&hid->dev, &dev_attr_alternate_modes); 1218b96d23ecSMichal Malý if (error) 121972529c65SMichal Malý goto err_init; 1220b96d23ecSMichal Malý } 122130bb75d7SMichal Malý dbg_hid("sysfs interface created\n"); 122230bb75d7SMichal Malý 122330bb75d7SMichal Malý /* Set the maximum range to start with */ 122472529c65SMichal Malý entry->wdata.range = entry->wdata.max_range; 122572529c65SMichal Malý if (entry->wdata.set_range) 122672529c65SMichal Malý entry->wdata.set_range(hid, entry->wdata.range); 122730bb75d7SMichal Malý 122822bcefdcSSimon Wood #ifdef CONFIG_LEDS_CLASS 122922bcefdcSSimon Wood /* register led subsystem - G27 only */ 123072529c65SMichal Malý entry->wdata.led_state = 0; 123122bcefdcSSimon Wood for (j = 0; j < 5; j++) 123272529c65SMichal Malý entry->wdata.led[j] = NULL; 123322bcefdcSSimon Wood 123422bcefdcSSimon Wood if (lg4ff_devices[i].product_id == USB_DEVICE_ID_LOGITECH_G27_WHEEL) { 123522bcefdcSSimon Wood struct led_classdev *led; 123622bcefdcSSimon Wood size_t name_sz; 123722bcefdcSSimon Wood char *name; 123822bcefdcSSimon Wood 123922bcefdcSSimon Wood lg4ff_set_leds(hid, 0); 124022bcefdcSSimon Wood 124122bcefdcSSimon Wood name_sz = strlen(dev_name(&hid->dev)) + 8; 124222bcefdcSSimon Wood 124322bcefdcSSimon Wood for (j = 0; j < 5; j++) { 124422bcefdcSSimon Wood led = kzalloc(sizeof(struct led_classdev)+name_sz, GFP_KERNEL); 124522bcefdcSSimon Wood if (!led) { 124622bcefdcSSimon Wood hid_err(hid, "can't allocate memory for LED %d\n", j); 124772529c65SMichal Malý goto err_leds; 124822bcefdcSSimon Wood } 124922bcefdcSSimon Wood 125022bcefdcSSimon Wood name = (void *)(&led[1]); 125122bcefdcSSimon Wood snprintf(name, name_sz, "%s::RPM%d", dev_name(&hid->dev), j+1); 125222bcefdcSSimon Wood led->name = name; 125322bcefdcSSimon Wood led->brightness = 0; 125422bcefdcSSimon Wood led->max_brightness = 1; 125522bcefdcSSimon Wood led->brightness_get = lg4ff_led_get_brightness; 125622bcefdcSSimon Wood led->brightness_set = lg4ff_led_set_brightness; 125722bcefdcSSimon Wood 125872529c65SMichal Malý entry->wdata.led[j] = led; 125922bcefdcSSimon Wood error = led_classdev_register(&hid->dev, led); 126022bcefdcSSimon Wood 126122bcefdcSSimon Wood if (error) { 126222bcefdcSSimon Wood hid_err(hid, "failed to register LED %d. Aborting.\n", j); 126372529c65SMichal Malý err_leds: 126422bcefdcSSimon Wood /* Deregister LEDs (if any) */ 126522bcefdcSSimon Wood for (j = 0; j < 5; j++) { 126672529c65SMichal Malý led = entry->wdata.led[j]; 126772529c65SMichal Malý entry->wdata.led[j] = NULL; 126822bcefdcSSimon Wood if (!led) 126922bcefdcSSimon Wood continue; 127022bcefdcSSimon Wood led_classdev_unregister(led); 127122bcefdcSSimon Wood kfree(led); 127222bcefdcSSimon Wood } 127322bcefdcSSimon Wood goto out; /* Let the driver continue without LEDs */ 127422bcefdcSSimon Wood } 127522bcefdcSSimon Wood } 127622bcefdcSSimon Wood } 127722bcefdcSSimon Wood out: 1278c6e6dc87SJiri Kosina #endif 127964013800SSimon Wood hid_info(hid, "Force feedback support for Logitech Gaming Wheels\n"); 128032c88cbcSSimon Wood return 0; 128172529c65SMichal Malý 128272529c65SMichal Malý err_init: 128372529c65SMichal Malý drv_data->device_props = NULL; 128472529c65SMichal Malý kfree(entry); 128572529c65SMichal Malý return error; 128632c88cbcSSimon Wood } 128732c88cbcSSimon Wood 128830bb75d7SMichal Malý int lg4ff_deinit(struct hid_device *hid) 128930bb75d7SMichal Malý { 129030bb75d7SMichal Malý struct lg4ff_device_entry *entry; 12913b6b17b7SMichal Malý struct lg_drv_data *drv_data; 12926a2e176bSMichal Malý 12933b6b17b7SMichal Malý drv_data = hid_get_drvdata(hid); 12943b6b17b7SMichal Malý if (!drv_data) { 12953b6b17b7SMichal Malý hid_err(hid, "Error while deinitializing device, no private driver data.\n"); 129630bb75d7SMichal Malý return -1; 129730bb75d7SMichal Malý } 12983b6b17b7SMichal Malý entry = drv_data->device_props; 1299e7c23449SMichal Malý if (!entry) 1300e7c23449SMichal Malý goto out; /* Nothing more to do */ 1301e7c23449SMichal Malý 1302b96d23ecSMichal Malý /* Multimode devices will have at least the "MODE_NATIVE" bit set */ 130372529c65SMichal Malý if (entry->wdata.alternate_modes) { 1304b96d23ecSMichal Malý device_remove_file(&hid->dev, &dev_attr_real_id); 1305b96d23ecSMichal Malý device_remove_file(&hid->dev, &dev_attr_alternate_modes); 1306b96d23ecSMichal Malý } 1307b96d23ecSMichal Malý 130872529c65SMichal Malý device_remove_file(&hid->dev, &dev_attr_range); 130922bcefdcSSimon Wood #ifdef CONFIG_LEDS_CLASS 131022bcefdcSSimon Wood { 131122bcefdcSSimon Wood int j; 131222bcefdcSSimon Wood struct led_classdev *led; 131322bcefdcSSimon Wood 131422bcefdcSSimon Wood /* Deregister LEDs (if any) */ 131522bcefdcSSimon Wood for (j = 0; j < 5; j++) { 131622bcefdcSSimon Wood 131772529c65SMichal Malý led = entry->wdata.led[j]; 131872529c65SMichal Malý entry->wdata.led[j] = NULL; 131922bcefdcSSimon Wood if (!led) 132022bcefdcSSimon Wood continue; 132122bcefdcSSimon Wood led_classdev_unregister(led); 132222bcefdcSSimon Wood kfree(led); 132322bcefdcSSimon Wood } 132422bcefdcSSimon Wood } 132522bcefdcSSimon Wood #endif 1326b211a638SMichal Malý hid_hw_stop(hid); 1327b211a638SMichal Malý drv_data->device_props = NULL; 132822bcefdcSSimon Wood 13293b6b17b7SMichal Malý kfree(entry); 1330e7c23449SMichal Malý out: 133130bb75d7SMichal Malý dbg_hid("Device successfully unregistered\n"); 133230bb75d7SMichal Malý return 0; 133330bb75d7SMichal Malý } 1334