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ý 7430bb75d7SMichal Malý struct lg4ff_device_entry { 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; 8730bb75d7SMichal Malý void (*set_range)(struct hid_device *hid, u16 range); 8830bb75d7SMichal Malý }; 8930bb75d7SMichal Malý 907362cd22SMichal Malý static const signed short lg4ff_wheel_effects[] = { 9132c88cbcSSimon Wood FF_CONSTANT, 9232c88cbcSSimon Wood FF_AUTOCENTER, 9332c88cbcSSimon Wood -1 9432c88cbcSSimon Wood }; 9532c88cbcSSimon Wood 967362cd22SMichal Malý struct lg4ff_wheel { 972a552c30SMichal Malý const u32 product_id; 987362cd22SMichal Malý const signed short *ff_effects; 992a552c30SMichal Malý const u16 min_range; 1002a552c30SMichal Malý const u16 max_range; 10130bb75d7SMichal Malý void (*set_range)(struct hid_device *hid, u16 range); 1027362cd22SMichal Malý }; 1037362cd22SMichal Malý 104e7c23449SMichal Malý struct lg4ff_compat_mode_switch { 1052a552c30SMichal Malý const u8 cmd_count; /* Number of commands to send */ 1062a552c30SMichal Malý const u8 cmd[]; 107e7c23449SMichal Malý }; 108e7c23449SMichal Malý 109e7c23449SMichal Malý struct lg4ff_wheel_ident_info { 110e7c23449SMichal Malý const u16 mask; 111e7c23449SMichal Malý const u16 result; 112e7c23449SMichal Malý const u16 real_product_id; 113e7c23449SMichal Malý }; 114e7c23449SMichal Malý 115e7c23449SMichal Malý struct lg4ff_wheel_ident_checklist { 116e7c23449SMichal Malý const u32 count; 117e7c23449SMichal Malý const struct lg4ff_wheel_ident_info *models[]; 118e7c23449SMichal Malý }; 119e7c23449SMichal Malý 120b96d23ecSMichal Malý struct lg4ff_multimode_wheel { 121b96d23ecSMichal Malý const u16 product_id; 122b96d23ecSMichal Malý const u32 alternate_modes; 123b96d23ecSMichal Malý const char *real_tag; 124b96d23ecSMichal Malý const char *real_name; 125b96d23ecSMichal Malý }; 126b96d23ecSMichal Malý 127b96d23ecSMichal Malý struct lg4ff_alternate_mode { 128b96d23ecSMichal Malý const u16 product_id; 129b96d23ecSMichal Malý const char *tag; 130b96d23ecSMichal Malý const char *name; 131b96d23ecSMichal Malý }; 132b96d23ecSMichal Malý 1337362cd22SMichal Malý static const struct lg4ff_wheel lg4ff_devices[] = { 13430bb75d7SMichal Malý {USB_DEVICE_ID_LOGITECH_WHEEL, lg4ff_wheel_effects, 40, 270, NULL}, 13530bb75d7SMichal Malý {USB_DEVICE_ID_LOGITECH_MOMO_WHEEL, lg4ff_wheel_effects, 40, 270, NULL}, 136d0afd848SMichal Malý {USB_DEVICE_ID_LOGITECH_DFP_WHEEL, lg4ff_wheel_effects, 40, 900, lg4ff_set_range_dfp}, 137d0afd848SMichal Malý {USB_DEVICE_ID_LOGITECH_G25_WHEEL, lg4ff_wheel_effects, 40, 900, lg4ff_set_range_g25}, 138d0afd848SMichal Malý {USB_DEVICE_ID_LOGITECH_DFGT_WHEEL, lg4ff_wheel_effects, 40, 900, lg4ff_set_range_g25}, 139d0afd848SMichal Malý {USB_DEVICE_ID_LOGITECH_G27_WHEEL, lg4ff_wheel_effects, 40, 900, lg4ff_set_range_g25}, 14030bb75d7SMichal Malý {USB_DEVICE_ID_LOGITECH_MOMO_WHEEL2, lg4ff_wheel_effects, 40, 270, NULL}, 14130bb75d7SMichal Malý {USB_DEVICE_ID_LOGITECH_WII_WHEEL, lg4ff_wheel_effects, 40, 270, NULL} 1427362cd22SMichal Malý }; 1437362cd22SMichal Malý 144b96d23ecSMichal Malý static const struct lg4ff_multimode_wheel lg4ff_multimode_wheels[] = { 145b96d23ecSMichal Malý {USB_DEVICE_ID_LOGITECH_DFP_WHEEL, 146b96d23ecSMichal Malý LG4FF_MODE_NATIVE | LG4FF_MODE_DFP | LG4FF_MODE_DFEX, 147b96d23ecSMichal Malý LG4FF_DFP_TAG, LG4FF_DFP_NAME}, 148b96d23ecSMichal Malý {USB_DEVICE_ID_LOGITECH_G25_WHEEL, 149b96d23ecSMichal Malý LG4FF_MODE_NATIVE | LG4FF_MODE_G25 | LG4FF_MODE_DFP | LG4FF_MODE_DFEX, 150b96d23ecSMichal Malý LG4FF_G25_TAG, LG4FF_G25_NAME}, 151b96d23ecSMichal Malý {USB_DEVICE_ID_LOGITECH_DFGT_WHEEL, 152b96d23ecSMichal Malý LG4FF_MODE_NATIVE | LG4FF_MODE_DFGT | LG4FF_MODE_DFP | LG4FF_MODE_DFEX, 153b96d23ecSMichal Malý LG4FF_DFGT_TAG, LG4FF_DFGT_NAME}, 154b96d23ecSMichal Malý {USB_DEVICE_ID_LOGITECH_G27_WHEEL, 155b96d23ecSMichal Malý LG4FF_MODE_NATIVE | LG4FF_MODE_G27 | LG4FF_MODE_G25 | LG4FF_MODE_DFP | LG4FF_MODE_DFEX, 156b96d23ecSMichal Malý LG4FF_G27_TAG, LG4FF_G27_NAME}, 157b96d23ecSMichal Malý }; 158b96d23ecSMichal Malý 159b96d23ecSMichal Malý static const struct lg4ff_alternate_mode lg4ff_alternate_modes[] = { 160b96d23ecSMichal Malý [LG4FF_MODE_NATIVE_IDX] = {0, "native", ""}, 161b96d23ecSMichal Malý [LG4FF_MODE_DFEX_IDX] = {USB_DEVICE_ID_LOGITECH_WHEEL, LG4FF_DFEX_TAG, LG4FF_DFEX_NAME}, 162b96d23ecSMichal Malý [LG4FF_MODE_DFP_IDX] = {USB_DEVICE_ID_LOGITECH_DFP_WHEEL, LG4FF_DFP_TAG, LG4FF_DFP_NAME}, 163b96d23ecSMichal Malý [LG4FF_MODE_G25_IDX] = {USB_DEVICE_ID_LOGITECH_G25_WHEEL, LG4FF_G25_TAG, LG4FF_G25_NAME}, 164b96d23ecSMichal Malý [LG4FF_MODE_DFGT_IDX] = {USB_DEVICE_ID_LOGITECH_DFGT_WHEEL, LG4FF_DFGT_TAG, LG4FF_DFGT_NAME}, 165b96d23ecSMichal Malý [LG4FF_MODE_G27_IDX] = {USB_DEVICE_ID_LOGITECH_G27_WHEEL, LG4FF_G27_TAG, LG4FF_G27_NAME} 166b96d23ecSMichal Malý }; 167b96d23ecSMichal Malý 168e7c23449SMichal Malý /* Multimode wheel identificators */ 169e7c23449SMichal Malý static const struct lg4ff_wheel_ident_info lg4ff_dfp_ident_info = { 170e7c23449SMichal Malý 0xf000, 171e7c23449SMichal Malý 0x1000, 172e7c23449SMichal Malý USB_DEVICE_ID_LOGITECH_DFP_WHEEL 17396440c8aSMichal Malý }; 17496440c8aSMichal Malý 175e7c23449SMichal Malý static const struct lg4ff_wheel_ident_info lg4ff_g25_ident_info = { 176e7c23449SMichal Malý 0xff00, 177e7c23449SMichal Malý 0x1200, 178e7c23449SMichal Malý USB_DEVICE_ID_LOGITECH_G25_WHEEL 17996440c8aSMichal Malý }; 18096440c8aSMichal Malý 181e7c23449SMichal Malý static const struct lg4ff_wheel_ident_info lg4ff_g27_ident_info = { 182e7c23449SMichal Malý 0xfff0, 183e7c23449SMichal Malý 0x1230, 184e7c23449SMichal Malý USB_DEVICE_ID_LOGITECH_G27_WHEEL 185e7c23449SMichal Malý }; 186e7c23449SMichal Malý 187e7c23449SMichal Malý static const struct lg4ff_wheel_ident_info lg4ff_dfgt_ident_info = { 188e7c23449SMichal Malý 0xff00, 189e7c23449SMichal Malý 0x1300, 190e7c23449SMichal Malý USB_DEVICE_ID_LOGITECH_DFGT_WHEEL 191e7c23449SMichal Malý }; 192e7c23449SMichal Malý 193e7c23449SMichal Malý /* Multimode wheel identification checklists */ 194e7c23449SMichal Malý static const struct lg4ff_wheel_ident_checklist lg4ff_main_checklist = { 195e7c23449SMichal Malý 4, 196e7c23449SMichal Malý {&lg4ff_dfgt_ident_info, 197e7c23449SMichal Malý &lg4ff_g27_ident_info, 198e7c23449SMichal Malý &lg4ff_g25_ident_info, 199e7c23449SMichal Malý &lg4ff_dfp_ident_info} 200e7c23449SMichal Malý }; 201e7c23449SMichal Malý 202e7c23449SMichal Malý /* Compatibility mode switching commands */ 203f31a2de3SMichal Malý /* EXT_CMD9 - Understood by G27 and DFGT */ 204f31a2de3SMichal Malý static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext09_dfex = { 205f31a2de3SMichal Malý 2, 206f31a2de3SMichal Malý {0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, /* Revert mode upon USB reset */ 207f31a2de3SMichal Malý 0xf8, 0x09, 0x00, 0x01, 0x00, 0x00, 0x00} /* Switch mode to DF-EX with detach */ 208f31a2de3SMichal Malý }; 209f31a2de3SMichal Malý 210f31a2de3SMichal Malý static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext09_dfp = { 211f31a2de3SMichal Malý 2, 212f31a2de3SMichal Malý {0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, /* Revert mode upon USB reset */ 213f31a2de3SMichal Malý 0xf8, 0x09, 0x01, 0x01, 0x00, 0x00, 0x00} /* Switch mode to DFP with detach */ 214f31a2de3SMichal Malý }; 215f31a2de3SMichal Malý 216f31a2de3SMichal Malý static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext09_g25 = { 217f31a2de3SMichal Malý 2, 218f31a2de3SMichal Malý {0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, /* Revert mode upon USB reset */ 219f31a2de3SMichal Malý 0xf8, 0x09, 0x02, 0x01, 0x00, 0x00, 0x00} /* Switch mode to G25 with detach */ 220f31a2de3SMichal Malý }; 221f31a2de3SMichal Malý 222f31a2de3SMichal Malý static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext09_dfgt = { 223f31a2de3SMichal Malý 2, 224f31a2de3SMichal Malý {0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, /* Revert mode upon USB reset */ 225f31a2de3SMichal Malý 0xf8, 0x09, 0x03, 0x01, 0x00, 0x00, 0x00} /* Switch mode to DFGT with detach */ 226f31a2de3SMichal Malý }; 227f31a2de3SMichal Malý 228f31a2de3SMichal Malý static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext09_g27 = { 229f31a2de3SMichal Malý 2, 230f31a2de3SMichal Malý {0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, /* Revert mode upon USB reset */ 231f31a2de3SMichal Malý 0xf8, 0x09, 0x04, 0x01, 0x00, 0x00, 0x00} /* Switch mode to G27 with detach */ 232f31a2de3SMichal Malý }; 233f31a2de3SMichal Malý 234f31a2de3SMichal Malý /* EXT_CMD1 - Understood by DFP, G25, G27 and DFGT */ 235f31a2de3SMichal Malý static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext01_dfp = { 23696440c8aSMichal Malý 1, 23796440c8aSMichal Malý {0xf8, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00} 23896440c8aSMichal Malý }; 23996440c8aSMichal Malý 240f31a2de3SMichal Malý /* EXT_CMD16 - Understood by G25 and G27 */ 241f31a2de3SMichal Malý static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext16_g25 = { 24296440c8aSMichal Malý 1, 24396440c8aSMichal Malý {0xf8, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00} 24496440c8aSMichal Malý }; 24596440c8aSMichal Malý 2462b24a960SMichal Malý /* Recalculates X axis value accordingly to currently selected range */ 2472a552c30SMichal Malý static s32 lg4ff_adjust_dfp_x_axis(s32 value, u16 range) 2482b24a960SMichal Malý { 2492a552c30SMichal Malý u16 max_range; 2502a552c30SMichal Malý s32 new_value; 2512b24a960SMichal Malý 2522b24a960SMichal Malý if (range == 900) 2532b24a960SMichal Malý return value; 2542b24a960SMichal Malý else if (range == 200) 2552b24a960SMichal Malý return value; 2562b24a960SMichal Malý else if (range < 200) 2572b24a960SMichal Malý max_range = 200; 2582b24a960SMichal Malý else 2592b24a960SMichal Malý max_range = 900; 2602b24a960SMichal Malý 2612b24a960SMichal Malý new_value = 8192 + mult_frac(value - 8192, max_range, range); 2622b24a960SMichal Malý if (new_value < 0) 2632b24a960SMichal Malý return 0; 2642b24a960SMichal Malý else if (new_value > 16383) 2652b24a960SMichal Malý return 16383; 2662b24a960SMichal Malý else 2672b24a960SMichal Malý return new_value; 2682b24a960SMichal Malý } 2692b24a960SMichal Malý 2702b24a960SMichal Malý int lg4ff_adjust_input_event(struct hid_device *hid, struct hid_field *field, 2712a552c30SMichal Malý struct hid_usage *usage, s32 value, struct lg_drv_data *drv_data) 2722b24a960SMichal Malý { 2732b24a960SMichal Malý struct lg4ff_device_entry *entry = drv_data->device_props; 2742a552c30SMichal Malý s32 new_value = 0; 2752b24a960SMichal Malý 2762b24a960SMichal Malý if (!entry) { 2772b24a960SMichal Malý hid_err(hid, "Device properties not found"); 2782b24a960SMichal Malý return 0; 2792b24a960SMichal Malý } 2802b24a960SMichal Malý 2812b24a960SMichal Malý switch (entry->product_id) { 2822b24a960SMichal Malý case USB_DEVICE_ID_LOGITECH_DFP_WHEEL: 2832b24a960SMichal Malý switch (usage->code) { 2842b24a960SMichal Malý case ABS_X: 2852b24a960SMichal Malý new_value = lg4ff_adjust_dfp_x_axis(value, entry->range); 2862b24a960SMichal Malý input_event(field->hidinput->input, usage->type, usage->code, new_value); 2872b24a960SMichal Malý return 1; 2882b24a960SMichal Malý default: 2892b24a960SMichal Malý return 0; 2902b24a960SMichal Malý } 2912b24a960SMichal Malý default: 2922b24a960SMichal Malý return 0; 2932b24a960SMichal Malý } 2942b24a960SMichal Malý } 2952b24a960SMichal Malý 296d0afd848SMichal Malý static int lg4ff_play(struct input_dev *dev, void *data, struct ff_effect *effect) 29732c88cbcSSimon Wood { 29832c88cbcSSimon Wood struct hid_device *hid = input_get_drvdata(dev); 29932c88cbcSSimon Wood struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list; 30032c88cbcSSimon Wood struct hid_report *report = list_entry(report_list->next, struct hid_report, list); 3012a552c30SMichal Malý s32 *value = report->field[0]->value; 30232c88cbcSSimon Wood int x; 30332c88cbcSSimon Wood 304a80fe5d6SMichal Malý #define CLAMP(x) do { if (x < 0) x = 0; else if (x > 0xff) x = 0xff; } while (0) 30532c88cbcSSimon Wood 30632c88cbcSSimon Wood switch (effect->type) { 30732c88cbcSSimon Wood case FF_CONSTANT: 30832c88cbcSSimon Wood x = effect->u.ramp.start_level + 0x80; /* 0x80 is no force */ 30932c88cbcSSimon Wood CLAMP(x); 31056930e7aSSimon Wood 31156930e7aSSimon Wood if (x == 0x80) { 31256930e7aSSimon Wood /* De-activate force in slot-1*/ 31356930e7aSSimon Wood value[0] = 0x13; 31456930e7aSSimon Wood value[1] = 0x00; 31556930e7aSSimon Wood value[2] = 0x00; 31656930e7aSSimon Wood value[3] = 0x00; 31756930e7aSSimon Wood value[4] = 0x00; 31856930e7aSSimon Wood value[5] = 0x00; 31956930e7aSSimon Wood value[6] = 0x00; 32056930e7aSSimon Wood 32156930e7aSSimon Wood hid_hw_request(hid, report, HID_REQ_SET_REPORT); 32256930e7aSSimon Wood return 0; 32356930e7aSSimon Wood } 32456930e7aSSimon Wood 32574479ba8SMichal Malý value[0] = 0x11; /* Slot 1 */ 32674479ba8SMichal Malý value[1] = 0x08; 32774479ba8SMichal Malý value[2] = x; 32874479ba8SMichal Malý value[3] = 0x80; 32974479ba8SMichal Malý value[4] = 0x00; 33074479ba8SMichal Malý value[5] = 0x00; 33174479ba8SMichal Malý value[6] = 0x00; 33232c88cbcSSimon Wood 333d8814272SBenjamin Tissoires hid_hw_request(hid, report, HID_REQ_SET_REPORT); 33432c88cbcSSimon Wood break; 33532c88cbcSSimon Wood } 33632c88cbcSSimon Wood return 0; 33732c88cbcSSimon Wood } 33832c88cbcSSimon Wood 3396e2de8e0SMichal Malý /* Sends default autocentering command compatible with 3406e2de8e0SMichal Malý * all wheels except Formula Force EX */ 341d0afd848SMichal Malý static void lg4ff_set_autocenter_default(struct input_dev *dev, u16 magnitude) 34232c88cbcSSimon Wood { 34332c88cbcSSimon Wood struct hid_device *hid = input_get_drvdata(dev); 34432c88cbcSSimon Wood struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list; 34532c88cbcSSimon Wood struct hid_report *report = list_entry(report_list->next, struct hid_report, list); 3462a552c30SMichal Malý s32 *value = report->field[0]->value; 3472a552c30SMichal Malý u32 expand_a, expand_b; 3481859762eSSimon Wood struct lg4ff_device_entry *entry; 3491859762eSSimon Wood struct lg_drv_data *drv_data; 3501859762eSSimon Wood 3511859762eSSimon Wood drv_data = hid_get_drvdata(hid); 3521859762eSSimon Wood if (!drv_data) { 3531859762eSSimon Wood hid_err(hid, "Private driver data not found!\n"); 3541859762eSSimon Wood return; 3551859762eSSimon Wood } 3561859762eSSimon Wood 3571859762eSSimon Wood entry = drv_data->device_props; 3581859762eSSimon Wood if (!entry) { 3591859762eSSimon Wood hid_err(hid, "Device properties not found!\n"); 3601859762eSSimon Wood return; 3611859762eSSimon Wood } 362f8c23156SSimon Wood 363d2c02da5SSimon Wood /* De-activate Auto-Center */ 364d2c02da5SSimon Wood if (magnitude == 0) { 365d2c02da5SSimon Wood value[0] = 0xf5; 366d2c02da5SSimon Wood value[1] = 0x00; 367d2c02da5SSimon Wood value[2] = 0x00; 368d2c02da5SSimon Wood value[3] = 0x00; 369d2c02da5SSimon Wood value[4] = 0x00; 370d2c02da5SSimon Wood value[5] = 0x00; 371d2c02da5SSimon Wood value[6] = 0x00; 372d2c02da5SSimon Wood 373d2c02da5SSimon Wood hid_hw_request(hid, report, HID_REQ_SET_REPORT); 374d2c02da5SSimon Wood return; 375d2c02da5SSimon Wood } 376d2c02da5SSimon Wood 377f8c23156SSimon Wood if (magnitude <= 0xaaaa) { 378f8c23156SSimon Wood expand_a = 0x0c * magnitude; 379f8c23156SSimon Wood expand_b = 0x80 * magnitude; 380f8c23156SSimon Wood } else { 381f8c23156SSimon Wood expand_a = (0x0c * 0xaaaa) + 0x06 * (magnitude - 0xaaaa); 382f8c23156SSimon Wood expand_b = (0x80 * 0xaaaa) + 0xff * (magnitude - 0xaaaa); 383f8c23156SSimon Wood } 38432c88cbcSSimon Wood 3851859762eSSimon Wood /* Adjust for non-MOMO wheels */ 3861859762eSSimon Wood switch (entry->product_id) { 3871859762eSSimon Wood case USB_DEVICE_ID_LOGITECH_MOMO_WHEEL: 3881859762eSSimon Wood case USB_DEVICE_ID_LOGITECH_MOMO_WHEEL2: 3891859762eSSimon Wood break; 3901859762eSSimon Wood default: 3911859762eSSimon Wood expand_a = expand_a >> 1; 3921859762eSSimon Wood break; 3931859762eSSimon Wood } 3941859762eSSimon Wood 39574479ba8SMichal Malý value[0] = 0xfe; 39674479ba8SMichal Malý value[1] = 0x0d; 397f8c23156SSimon Wood value[2] = expand_a / 0xaaaa; 398f8c23156SSimon Wood value[3] = expand_a / 0xaaaa; 399f8c23156SSimon Wood value[4] = expand_b / 0xaaaa; 40074479ba8SMichal Malý value[5] = 0x00; 40174479ba8SMichal Malý value[6] = 0x00; 40232c88cbcSSimon Wood 403d8814272SBenjamin Tissoires hid_hw_request(hid, report, HID_REQ_SET_REPORT); 404d2c02da5SSimon Wood 405d2c02da5SSimon Wood /* Activate Auto-Center */ 406d2c02da5SSimon Wood value[0] = 0x14; 407d2c02da5SSimon Wood value[1] = 0x00; 408d2c02da5SSimon Wood value[2] = 0x00; 409d2c02da5SSimon Wood value[3] = 0x00; 410d2c02da5SSimon Wood value[4] = 0x00; 411d2c02da5SSimon Wood value[5] = 0x00; 412d2c02da5SSimon Wood value[6] = 0x00; 413d2c02da5SSimon Wood 414d2c02da5SSimon Wood hid_hw_request(hid, report, HID_REQ_SET_REPORT); 41532c88cbcSSimon Wood } 41632c88cbcSSimon Wood 4176e2de8e0SMichal Malý /* Sends autocentering command compatible with Formula Force EX */ 418d0afd848SMichal Malý static void lg4ff_set_autocenter_ffex(struct input_dev *dev, u16 magnitude) 4196e2de8e0SMichal Malý { 4206e2de8e0SMichal Malý struct hid_device *hid = input_get_drvdata(dev); 4216e2de8e0SMichal Malý struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list; 4226e2de8e0SMichal Malý struct hid_report *report = list_entry(report_list->next, struct hid_report, list); 4232a552c30SMichal Malý s32 *value = report->field[0]->value; 4246e2de8e0SMichal Malý magnitude = magnitude * 90 / 65535; 4256e2de8e0SMichal Malý 42674479ba8SMichal Malý value[0] = 0xfe; 42774479ba8SMichal Malý value[1] = 0x03; 42874479ba8SMichal Malý value[2] = magnitude >> 14; 42974479ba8SMichal Malý value[3] = magnitude >> 14; 43074479ba8SMichal Malý value[4] = magnitude; 43174479ba8SMichal Malý value[5] = 0x00; 43274479ba8SMichal Malý value[6] = 0x00; 4336e2de8e0SMichal Malý 434d8814272SBenjamin Tissoires hid_hw_request(hid, report, HID_REQ_SET_REPORT); 4356e2de8e0SMichal Malý } 4366e2de8e0SMichal Malý 43730bb75d7SMichal Malý /* Sends command to set range compatible with G25/G27/Driving Force GT */ 438d0afd848SMichal Malý static void lg4ff_set_range_g25(struct hid_device *hid, u16 range) 43930bb75d7SMichal Malý { 44030bb75d7SMichal Malý struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list; 44130bb75d7SMichal Malý struct hid_report *report = list_entry(report_list->next, struct hid_report, list); 4422a552c30SMichal Malý s32 *value = report->field[0]->value; 44374479ba8SMichal Malý 44430bb75d7SMichal Malý dbg_hid("G25/G27/DFGT: setting range to %u\n", range); 44530bb75d7SMichal Malý 44674479ba8SMichal Malý value[0] = 0xf8; 44774479ba8SMichal Malý value[1] = 0x81; 44874479ba8SMichal Malý value[2] = range & 0x00ff; 44974479ba8SMichal Malý value[3] = (range & 0xff00) >> 8; 45074479ba8SMichal Malý value[4] = 0x00; 45174479ba8SMichal Malý value[5] = 0x00; 45274479ba8SMichal Malý value[6] = 0x00; 45330bb75d7SMichal Malý 454d8814272SBenjamin Tissoires hid_hw_request(hid, report, HID_REQ_SET_REPORT); 45530bb75d7SMichal Malý } 45630bb75d7SMichal Malý 45730bb75d7SMichal Malý /* Sends commands to set range compatible with Driving Force Pro wheel */ 458d0afd848SMichal Malý static void lg4ff_set_range_dfp(struct hid_device *hid, u16 range) 45930bb75d7SMichal Malý { 46030bb75d7SMichal Malý struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list; 46130bb75d7SMichal Malý struct hid_report *report = list_entry(report_list->next, struct hid_report, list); 46230bb75d7SMichal Malý int start_left, start_right, full_range; 4632a552c30SMichal Malý s32 *value = report->field[0]->value; 46474479ba8SMichal Malý 46530bb75d7SMichal Malý dbg_hid("Driving Force Pro: setting range to %u\n", range); 46630bb75d7SMichal Malý 46730bb75d7SMichal Malý /* Prepare "coarse" limit command */ 46874479ba8SMichal Malý value[0] = 0xf8; 46974479ba8SMichal Malý value[1] = 0x00; /* Set later */ 47074479ba8SMichal Malý value[2] = 0x00; 47174479ba8SMichal Malý value[3] = 0x00; 47274479ba8SMichal Malý value[4] = 0x00; 47374479ba8SMichal Malý value[5] = 0x00; 47474479ba8SMichal Malý value[6] = 0x00; 47530bb75d7SMichal Malý 47630bb75d7SMichal Malý if (range > 200) { 47730bb75d7SMichal Malý report->field[0]->value[1] = 0x03; 47830bb75d7SMichal Malý full_range = 900; 47930bb75d7SMichal Malý } else { 48030bb75d7SMichal Malý report->field[0]->value[1] = 0x02; 48130bb75d7SMichal Malý full_range = 200; 48230bb75d7SMichal Malý } 483d8814272SBenjamin Tissoires hid_hw_request(hid, report, HID_REQ_SET_REPORT); 48430bb75d7SMichal Malý 48530bb75d7SMichal Malý /* Prepare "fine" limit command */ 48674479ba8SMichal Malý value[0] = 0x81; 48774479ba8SMichal Malý value[1] = 0x0b; 48874479ba8SMichal Malý value[2] = 0x00; 48974479ba8SMichal Malý value[3] = 0x00; 49074479ba8SMichal Malý value[4] = 0x00; 49174479ba8SMichal Malý value[5] = 0x00; 49274479ba8SMichal Malý value[6] = 0x00; 49330bb75d7SMichal Malý 49430bb75d7SMichal Malý if (range == 200 || range == 900) { /* Do not apply any fine limit */ 495d8814272SBenjamin Tissoires hid_hw_request(hid, report, HID_REQ_SET_REPORT); 49630bb75d7SMichal Malý return; 49730bb75d7SMichal Malý } 49830bb75d7SMichal Malý 49930bb75d7SMichal Malý /* Construct fine limit command */ 50030bb75d7SMichal Malý start_left = (((full_range - range + 1) * 2047) / full_range); 50130bb75d7SMichal Malý start_right = 0xfff - start_left; 50230bb75d7SMichal Malý 50374479ba8SMichal Malý value[2] = start_left >> 4; 50474479ba8SMichal Malý value[3] = start_right >> 4; 50574479ba8SMichal Malý value[4] = 0xff; 50674479ba8SMichal Malý value[5] = (start_right & 0xe) << 4 | (start_left & 0xe); 50774479ba8SMichal Malý value[6] = 0xff; 50830bb75d7SMichal Malý 509d8814272SBenjamin Tissoires hid_hw_request(hid, report, HID_REQ_SET_REPORT); 51030bb75d7SMichal Malý } 51130bb75d7SMichal Malý 512f31a2de3SMichal Malý static const struct lg4ff_compat_mode_switch *lg4ff_get_mode_switch_command(const u16 real_product_id, const u16 target_product_id) 513f31a2de3SMichal Malý { 514f31a2de3SMichal Malý switch (real_product_id) { 515f31a2de3SMichal Malý case USB_DEVICE_ID_LOGITECH_DFP_WHEEL: 516f31a2de3SMichal Malý switch (target_product_id) { 517f31a2de3SMichal Malý case USB_DEVICE_ID_LOGITECH_DFP_WHEEL: 518f31a2de3SMichal Malý return &lg4ff_mode_switch_ext01_dfp; 519f31a2de3SMichal Malý /* DFP can only be switched to its native mode */ 520f31a2de3SMichal Malý default: 521f31a2de3SMichal Malý return NULL; 522f31a2de3SMichal Malý } 523f31a2de3SMichal Malý break; 524f31a2de3SMichal Malý case USB_DEVICE_ID_LOGITECH_G25_WHEEL: 525f31a2de3SMichal Malý switch (target_product_id) { 526f31a2de3SMichal Malý case USB_DEVICE_ID_LOGITECH_DFP_WHEEL: 527f31a2de3SMichal Malý return &lg4ff_mode_switch_ext01_dfp; 528f31a2de3SMichal Malý case USB_DEVICE_ID_LOGITECH_G25_WHEEL: 529f31a2de3SMichal Malý return &lg4ff_mode_switch_ext16_g25; 530f31a2de3SMichal Malý /* G25 can only be switched to DFP mode or its native mode */ 531f31a2de3SMichal Malý default: 532f31a2de3SMichal Malý return NULL; 533f31a2de3SMichal Malý } 534f31a2de3SMichal Malý break; 535f31a2de3SMichal Malý case USB_DEVICE_ID_LOGITECH_G27_WHEEL: 536f31a2de3SMichal Malý switch (target_product_id) { 537f31a2de3SMichal Malý case USB_DEVICE_ID_LOGITECH_WHEEL: 538f31a2de3SMichal Malý return &lg4ff_mode_switch_ext09_dfex; 539f31a2de3SMichal Malý case USB_DEVICE_ID_LOGITECH_DFP_WHEEL: 540f31a2de3SMichal Malý return &lg4ff_mode_switch_ext09_dfp; 541f31a2de3SMichal Malý case USB_DEVICE_ID_LOGITECH_G25_WHEEL: 542f31a2de3SMichal Malý return &lg4ff_mode_switch_ext09_g25; 543f31a2de3SMichal Malý case USB_DEVICE_ID_LOGITECH_G27_WHEEL: 544f31a2de3SMichal Malý return &lg4ff_mode_switch_ext09_g27; 545f31a2de3SMichal Malý /* G27 can only be switched to DF-EX, DFP, G25 or its native mode */ 546f31a2de3SMichal Malý default: 547f31a2de3SMichal Malý return NULL; 548f31a2de3SMichal Malý } 549f31a2de3SMichal Malý break; 550f31a2de3SMichal Malý case USB_DEVICE_ID_LOGITECH_DFGT_WHEEL: 551f31a2de3SMichal Malý switch (target_product_id) { 552f31a2de3SMichal Malý case USB_DEVICE_ID_LOGITECH_WHEEL: 553f31a2de3SMichal Malý return &lg4ff_mode_switch_ext09_dfex; 554f31a2de3SMichal Malý case USB_DEVICE_ID_LOGITECH_DFP_WHEEL: 555f31a2de3SMichal Malý return &lg4ff_mode_switch_ext09_dfp; 556f31a2de3SMichal Malý case USB_DEVICE_ID_LOGITECH_DFGT_WHEEL: 557f31a2de3SMichal Malý return &lg4ff_mode_switch_ext09_dfgt; 558f31a2de3SMichal Malý /* DFGT can only be switched to DF-EX, DFP or its native mode */ 559f31a2de3SMichal Malý default: 560f31a2de3SMichal Malý return NULL; 561f31a2de3SMichal Malý } 562f31a2de3SMichal Malý break; 563f31a2de3SMichal Malý /* No other wheels have multiple modes */ 564f31a2de3SMichal Malý default: 565f31a2de3SMichal Malý return NULL; 566f31a2de3SMichal Malý } 567f31a2de3SMichal Malý } 568f31a2de3SMichal Malý 569e7c23449SMichal Malý static int lg4ff_switch_compatibility_mode(struct hid_device *hid, const struct lg4ff_compat_mode_switch *s) 57096440c8aSMichal Malý { 571c1740d13SMichal Malý struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list; 572c1740d13SMichal Malý struct hid_report *report = list_entry(report_list->next, struct hid_report, list); 5732a552c30SMichal Malý s32 *value = report->field[0]->value; 574e7c23449SMichal Malý u8 i; 57596440c8aSMichal Malý 576e7c23449SMichal Malý for (i = 0; i < s->cmd_count; i++) { 577c1740d13SMichal Malý u8 j; 57896440c8aSMichal Malý 579c1740d13SMichal Malý for (j = 0; j < 7; j++) 580c1740d13SMichal Malý value[j] = s->cmd[j + (7*i)]; 581c1740d13SMichal Malý 582c1740d13SMichal Malý hid_hw_request(hid, report, HID_REQ_SET_REPORT); 58396440c8aSMichal Malý } 584c1740d13SMichal Malý hid_hw_wait(hid); 585e7c23449SMichal Malý return 0; 58696440c8aSMichal Malý } 58732c88cbcSSimon Wood 588b96d23ecSMichal Malý static ssize_t lg4ff_alternate_modes_show(struct device *dev, struct device_attribute *attr, char *buf) 589b96d23ecSMichal Malý { 590b96d23ecSMichal Malý struct hid_device *hid = to_hid_device(dev); 591b96d23ecSMichal Malý struct lg4ff_device_entry *entry; 592b96d23ecSMichal Malý struct lg_drv_data *drv_data; 593b96d23ecSMichal Malý ssize_t count = 0; 594b96d23ecSMichal Malý int i; 595b96d23ecSMichal Malý 596b96d23ecSMichal Malý drv_data = hid_get_drvdata(hid); 597b96d23ecSMichal Malý if (!drv_data) { 598b96d23ecSMichal Malý hid_err(hid, "Private driver data not found!\n"); 599b96d23ecSMichal Malý return 0; 600b96d23ecSMichal Malý } 601b96d23ecSMichal Malý 602b96d23ecSMichal Malý entry = drv_data->device_props; 603b96d23ecSMichal Malý if (!entry) { 604b96d23ecSMichal Malý hid_err(hid, "Device properties not found!\n"); 605b96d23ecSMichal Malý return 0; 606b96d23ecSMichal Malý } 607b96d23ecSMichal Malý 608b96d23ecSMichal Malý if (!entry->real_name) { 609b96d23ecSMichal Malý hid_err(hid, "NULL pointer to string\n"); 610b96d23ecSMichal Malý return 0; 611b96d23ecSMichal Malý } 612b96d23ecSMichal Malý 613b96d23ecSMichal Malý for (i = 0; i < LG4FF_MODE_MAX_IDX; i++) { 614b96d23ecSMichal Malý if (entry->alternate_modes & BIT(i)) { 615b96d23ecSMichal Malý /* Print tag and full name */ 616b96d23ecSMichal Malý count += scnprintf(buf + count, PAGE_SIZE - count, "%s: %s", 617b96d23ecSMichal Malý lg4ff_alternate_modes[i].tag, 618b96d23ecSMichal Malý !lg4ff_alternate_modes[i].product_id ? entry->real_name : lg4ff_alternate_modes[i].name); 619b96d23ecSMichal Malý if (count >= PAGE_SIZE - 1) 620b96d23ecSMichal Malý return count; 621b96d23ecSMichal Malý 622b96d23ecSMichal Malý /* Mark the currently active mode with an asterisk */ 623b96d23ecSMichal Malý if (lg4ff_alternate_modes[i].product_id == entry->product_id || 624b96d23ecSMichal Malý (lg4ff_alternate_modes[i].product_id == 0 && entry->product_id == entry->real_product_id)) 625b96d23ecSMichal Malý count += scnprintf(buf + count, PAGE_SIZE - count, " *\n"); 626b96d23ecSMichal Malý else 627b96d23ecSMichal Malý count += scnprintf(buf + count, PAGE_SIZE - count, "\n"); 628b96d23ecSMichal Malý 629b96d23ecSMichal Malý if (count >= PAGE_SIZE - 1) 630b96d23ecSMichal Malý return count; 631b96d23ecSMichal Malý } 632b96d23ecSMichal Malý } 633b96d23ecSMichal Malý 634b96d23ecSMichal Malý return count; 635b96d23ecSMichal Malý } 636b96d23ecSMichal Malý 637b96d23ecSMichal Malý static ssize_t lg4ff_alternate_modes_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) 638b96d23ecSMichal Malý { 639f31a2de3SMichal Malý struct hid_device *hid = to_hid_device(dev); 640f31a2de3SMichal Malý struct lg4ff_device_entry *entry; 641f31a2de3SMichal Malý struct lg_drv_data *drv_data; 642f31a2de3SMichal Malý const struct lg4ff_compat_mode_switch *s; 643f31a2de3SMichal Malý u16 target_product_id = 0; 644f31a2de3SMichal Malý int i, ret; 645f31a2de3SMichal Malý char *lbuf; 646f31a2de3SMichal Malý 647f31a2de3SMichal Malý drv_data = hid_get_drvdata(hid); 648f31a2de3SMichal Malý if (!drv_data) { 649f31a2de3SMichal Malý hid_err(hid, "Private driver data not found!\n"); 650f31a2de3SMichal Malý return -EINVAL; 651f31a2de3SMichal Malý } 652f31a2de3SMichal Malý 653f31a2de3SMichal Malý entry = drv_data->device_props; 654f31a2de3SMichal Malý if (!entry) { 655f31a2de3SMichal Malý hid_err(hid, "Device properties not found!\n"); 656f31a2de3SMichal Malý return -EINVAL; 657f31a2de3SMichal Malý } 658f31a2de3SMichal Malý 659f31a2de3SMichal Malý /* Allow \n at the end of the input parameter */ 660f31a2de3SMichal Malý lbuf = kasprintf(GFP_KERNEL, "%s", buf); 661f31a2de3SMichal Malý if (!lbuf) 662f31a2de3SMichal Malý return -ENOMEM; 663f31a2de3SMichal Malý 664f31a2de3SMichal Malý i = strlen(lbuf); 665f31a2de3SMichal Malý if (lbuf[i-1] == '\n') { 666f31a2de3SMichal Malý if (i == 1) { 667f31a2de3SMichal Malý kfree(lbuf); 668f31a2de3SMichal Malý return -EINVAL; 669f31a2de3SMichal Malý } 670f31a2de3SMichal Malý lbuf[i-1] = '\0'; 671f31a2de3SMichal Malý } 672f31a2de3SMichal Malý 673f31a2de3SMichal Malý for (i = 0; i < LG4FF_MODE_MAX_IDX; i++) { 674f31a2de3SMichal Malý const u16 mode_product_id = lg4ff_alternate_modes[i].product_id; 675f31a2de3SMichal Malý const char *tag = lg4ff_alternate_modes[i].tag; 676f31a2de3SMichal Malý 677f31a2de3SMichal Malý if (entry->alternate_modes & BIT(i)) { 678f31a2de3SMichal Malý if (!strcmp(tag, lbuf)) { 679f31a2de3SMichal Malý if (!mode_product_id) 680f31a2de3SMichal Malý target_product_id = entry->real_product_id; 681f31a2de3SMichal Malý else 682f31a2de3SMichal Malý target_product_id = mode_product_id; 683f31a2de3SMichal Malý break; 684f31a2de3SMichal Malý } 685f31a2de3SMichal Malý } 686f31a2de3SMichal Malý } 687f31a2de3SMichal Malý 688f31a2de3SMichal Malý if (i == LG4FF_MODE_MAX_IDX) { 689f31a2de3SMichal Malý hid_info(hid, "Requested mode \"%s\" is not supported by the device\n", lbuf); 690f31a2de3SMichal Malý kfree(lbuf); 691f31a2de3SMichal Malý return -EINVAL; 692f31a2de3SMichal Malý } 693f31a2de3SMichal Malý kfree(lbuf); /* Not needed anymore */ 694f31a2de3SMichal Malý 695f31a2de3SMichal Malý if (target_product_id == entry->product_id) /* Nothing to do */ 696f31a2de3SMichal Malý return count; 697f31a2de3SMichal Malý 698f31a2de3SMichal Malý /* Automatic switching has to be disabled for the switch to DF-EX mode to work correctly */ 699f31a2de3SMichal Malý if (target_product_id == USB_DEVICE_ID_LOGITECH_WHEEL && !lg4ff_no_autoswitch) { 700f31a2de3SMichal 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", 701f31a2de3SMichal Malý entry->real_name); 702f31a2de3SMichal Malý return -EINVAL; 703f31a2de3SMichal Malý } 704f31a2de3SMichal Malý 705f31a2de3SMichal Malý /* Take care of hardware limitations */ 706f31a2de3SMichal Malý if ((entry->real_product_id == USB_DEVICE_ID_LOGITECH_DFP_WHEEL || entry->real_product_id == USB_DEVICE_ID_LOGITECH_G25_WHEEL) && 707f31a2de3SMichal Malý entry->product_id > target_product_id) { 708f31a2de3SMichal Malý hid_info(hid, "\"%s\" cannot be switched back into \"%s\" mode\n", entry->real_name, lg4ff_alternate_modes[i].name); 709f31a2de3SMichal Malý return -EINVAL; 710f31a2de3SMichal Malý } 711f31a2de3SMichal Malý 712f31a2de3SMichal Malý s = lg4ff_get_mode_switch_command(entry->real_product_id, target_product_id); 713f31a2de3SMichal Malý if (!s) { 714f31a2de3SMichal Malý hid_err(hid, "Invalid target product ID %X\n", target_product_id); 715f31a2de3SMichal Malý return -EINVAL; 716f31a2de3SMichal Malý } 717f31a2de3SMichal Malý 718f31a2de3SMichal Malý ret = lg4ff_switch_compatibility_mode(hid, s); 719f31a2de3SMichal Malý return (ret == 0 ? count : ret); 720b96d23ecSMichal Malý } 721b96d23ecSMichal Malý static DEVICE_ATTR(alternate_modes, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH, lg4ff_alternate_modes_show, lg4ff_alternate_modes_store); 722b96d23ecSMichal Malý 723fbf85e2aSMichal Malý /* Export the currently set range of the wheel */ 724fbf85e2aSMichal Malý static ssize_t lg4ff_range_show(struct device *dev, struct device_attribute *attr, 7252f1cec32SVivien Didelot char *buf) 72630bb75d7SMichal Malý { 72730bb75d7SMichal Malý struct hid_device *hid = to_hid_device(dev); 7283b6b17b7SMichal Malý struct lg4ff_device_entry *entry; 7293b6b17b7SMichal Malý struct lg_drv_data *drv_data; 73030bb75d7SMichal Malý size_t count; 73130bb75d7SMichal Malý 7323b6b17b7SMichal Malý drv_data = hid_get_drvdata(hid); 7333b6b17b7SMichal Malý if (!drv_data) { 7343b6b17b7SMichal Malý hid_err(hid, "Private driver data not found!\n"); 7353b6b17b7SMichal Malý return 0; 73630bb75d7SMichal Malý } 7373b6b17b7SMichal Malý 7383b6b17b7SMichal Malý entry = drv_data->device_props; 7393b6b17b7SMichal Malý if (!entry) { 7403b6b17b7SMichal Malý hid_err(hid, "Device properties not found!\n"); 74130bb75d7SMichal Malý return 0; 74230bb75d7SMichal Malý } 74330bb75d7SMichal Malý 74430bb75d7SMichal Malý count = scnprintf(buf, PAGE_SIZE, "%u\n", entry->range); 74530bb75d7SMichal Malý return count; 74630bb75d7SMichal Malý } 74730bb75d7SMichal Malý 74830bb75d7SMichal Malý /* Set range to user specified value, call appropriate function 74930bb75d7SMichal Malý * according to the type of the wheel */ 750fbf85e2aSMichal Malý static ssize_t lg4ff_range_store(struct device *dev, struct device_attribute *attr, 7512f1cec32SVivien Didelot const char *buf, size_t count) 75230bb75d7SMichal Malý { 75330bb75d7SMichal Malý struct hid_device *hid = to_hid_device(dev); 7543b6b17b7SMichal Malý struct lg4ff_device_entry *entry; 7553b6b17b7SMichal Malý struct lg_drv_data *drv_data; 7562a552c30SMichal Malý u16 range = simple_strtoul(buf, NULL, 10); 75730bb75d7SMichal Malý 7583b6b17b7SMichal Malý drv_data = hid_get_drvdata(hid); 7593b6b17b7SMichal Malý if (!drv_data) { 7603b6b17b7SMichal Malý hid_err(hid, "Private driver data not found!\n"); 76129ff6657SSimon Wood return -EINVAL; 76230bb75d7SMichal Malý } 7633b6b17b7SMichal Malý 7643b6b17b7SMichal Malý entry = drv_data->device_props; 7653b6b17b7SMichal Malý if (!entry) { 7663b6b17b7SMichal Malý hid_err(hid, "Device properties not found!\n"); 76729ff6657SSimon Wood return -EINVAL; 76830bb75d7SMichal Malý } 76930bb75d7SMichal Malý 77030bb75d7SMichal Malý if (range == 0) 77130bb75d7SMichal Malý range = entry->max_range; 77230bb75d7SMichal Malý 77330bb75d7SMichal Malý /* Check if the wheel supports range setting 77430bb75d7SMichal Malý * and that the range is within limits for the wheel */ 77530bb75d7SMichal Malý if (entry->set_range != NULL && range >= entry->min_range && range <= entry->max_range) { 77630bb75d7SMichal Malý entry->set_range(hid, range); 77730bb75d7SMichal Malý entry->range = range; 77830bb75d7SMichal Malý } 77930bb75d7SMichal Malý 78030bb75d7SMichal Malý return count; 78130bb75d7SMichal Malý } 782fbf85e2aSMichal Malý static DEVICE_ATTR(range, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH, lg4ff_range_show, lg4ff_range_store); 78330bb75d7SMichal Malý 784b96d23ecSMichal Malý static ssize_t lg4ff_real_id_show(struct device *dev, struct device_attribute *attr, char *buf) 785b96d23ecSMichal Malý { 786b96d23ecSMichal Malý struct hid_device *hid = to_hid_device(dev); 787b96d23ecSMichal Malý struct lg4ff_device_entry *entry; 788b96d23ecSMichal Malý struct lg_drv_data *drv_data; 789b96d23ecSMichal Malý size_t count; 790b96d23ecSMichal Malý 791b96d23ecSMichal Malý drv_data = hid_get_drvdata(hid); 792b96d23ecSMichal Malý if (!drv_data) { 793b96d23ecSMichal Malý hid_err(hid, "Private driver data not found!\n"); 794b96d23ecSMichal Malý return 0; 795b96d23ecSMichal Malý } 796b96d23ecSMichal Malý 797b96d23ecSMichal Malý entry = drv_data->device_props; 798b96d23ecSMichal Malý if (!entry) { 799b96d23ecSMichal Malý hid_err(hid, "Device properties not found!\n"); 800b96d23ecSMichal Malý return 0; 801b96d23ecSMichal Malý } 802b96d23ecSMichal Malý 803b96d23ecSMichal Malý if (!entry->real_tag || !entry->real_name) { 804b96d23ecSMichal Malý hid_err(hid, "NULL pointer to string\n"); 805b96d23ecSMichal Malý return 0; 806b96d23ecSMichal Malý } 807b96d23ecSMichal Malý 808b96d23ecSMichal Malý count = scnprintf(buf, PAGE_SIZE, "%s: %s\n", entry->real_tag, entry->real_name); 809b96d23ecSMichal Malý return count; 810b96d23ecSMichal Malý } 811b96d23ecSMichal Malý 812b96d23ecSMichal Malý static ssize_t lg4ff_real_id_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) 813b96d23ecSMichal Malý { 814b96d23ecSMichal Malý /* Real ID is a read-only value */ 815b96d23ecSMichal Malý return -EPERM; 816b96d23ecSMichal Malý } 817b96d23ecSMichal Malý static DEVICE_ATTR(real_id, S_IRUGO, lg4ff_real_id_show, lg4ff_real_id_store); 818b96d23ecSMichal Malý 81922bcefdcSSimon Wood #ifdef CONFIG_LEDS_CLASS 8202a552c30SMichal Malý static void lg4ff_set_leds(struct hid_device *hid, u8 leds) 82122bcefdcSSimon Wood { 82222bcefdcSSimon Wood struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list; 82322bcefdcSSimon Wood struct hid_report *report = list_entry(report_list->next, struct hid_report, list); 8242a552c30SMichal Malý s32 *value = report->field[0]->value; 82522bcefdcSSimon Wood 82674479ba8SMichal Malý value[0] = 0xf8; 82774479ba8SMichal Malý value[1] = 0x12; 82874479ba8SMichal Malý value[2] = leds; 82974479ba8SMichal Malý value[3] = 0x00; 83074479ba8SMichal Malý value[4] = 0x00; 83174479ba8SMichal Malý value[5] = 0x00; 83274479ba8SMichal Malý value[6] = 0x00; 833d8814272SBenjamin Tissoires hid_hw_request(hid, report, HID_REQ_SET_REPORT); 83422bcefdcSSimon Wood } 83522bcefdcSSimon Wood 83622bcefdcSSimon Wood static void lg4ff_led_set_brightness(struct led_classdev *led_cdev, 83722bcefdcSSimon Wood enum led_brightness value) 83822bcefdcSSimon Wood { 83922bcefdcSSimon Wood struct device *dev = led_cdev->dev->parent; 84022bcefdcSSimon Wood struct hid_device *hid = container_of(dev, struct hid_device, dev); 8414629fd16SAxel Lin struct lg_drv_data *drv_data = hid_get_drvdata(hid); 84222bcefdcSSimon Wood struct lg4ff_device_entry *entry; 84322bcefdcSSimon Wood int i, state = 0; 84422bcefdcSSimon Wood 84522bcefdcSSimon Wood if (!drv_data) { 84622bcefdcSSimon Wood hid_err(hid, "Device data not found."); 84722bcefdcSSimon Wood return; 84822bcefdcSSimon Wood } 84922bcefdcSSimon Wood 850*371a1d9eSMichal Malý entry = drv_data->device_props; 85122bcefdcSSimon Wood 85222bcefdcSSimon Wood if (!entry) { 85322bcefdcSSimon Wood hid_err(hid, "Device properties not found."); 85422bcefdcSSimon Wood return; 85522bcefdcSSimon Wood } 85622bcefdcSSimon Wood 85722bcefdcSSimon Wood for (i = 0; i < 5; i++) { 85822bcefdcSSimon Wood if (led_cdev != entry->led[i]) 85922bcefdcSSimon Wood continue; 86022bcefdcSSimon Wood state = (entry->led_state >> i) & 1; 86122bcefdcSSimon Wood if (value == LED_OFF && state) { 86222bcefdcSSimon Wood entry->led_state &= ~(1 << i); 86322bcefdcSSimon Wood lg4ff_set_leds(hid, entry->led_state); 86422bcefdcSSimon Wood } else if (value != LED_OFF && !state) { 86522bcefdcSSimon Wood entry->led_state |= 1 << i; 86622bcefdcSSimon Wood lg4ff_set_leds(hid, entry->led_state); 86722bcefdcSSimon Wood } 86822bcefdcSSimon Wood break; 86922bcefdcSSimon Wood } 87022bcefdcSSimon Wood } 87122bcefdcSSimon Wood 87222bcefdcSSimon Wood static enum led_brightness lg4ff_led_get_brightness(struct led_classdev *led_cdev) 87322bcefdcSSimon Wood { 87422bcefdcSSimon Wood struct device *dev = led_cdev->dev->parent; 87522bcefdcSSimon Wood struct hid_device *hid = container_of(dev, struct hid_device, dev); 8764629fd16SAxel Lin struct lg_drv_data *drv_data = hid_get_drvdata(hid); 87722bcefdcSSimon Wood struct lg4ff_device_entry *entry; 87822bcefdcSSimon Wood int i, value = 0; 87922bcefdcSSimon Wood 88022bcefdcSSimon Wood if (!drv_data) { 88122bcefdcSSimon Wood hid_err(hid, "Device data not found."); 88222bcefdcSSimon Wood return LED_OFF; 88322bcefdcSSimon Wood } 88422bcefdcSSimon Wood 885*371a1d9eSMichal Malý entry = drv_data->device_props; 88622bcefdcSSimon Wood 88722bcefdcSSimon Wood if (!entry) { 88822bcefdcSSimon Wood hid_err(hid, "Device properties not found."); 88922bcefdcSSimon Wood return LED_OFF; 89022bcefdcSSimon Wood } 89122bcefdcSSimon Wood 89222bcefdcSSimon Wood for (i = 0; i < 5; i++) 89322bcefdcSSimon Wood if (led_cdev == entry->led[i]) { 89422bcefdcSSimon Wood value = (entry->led_state >> i) & 1; 89522bcefdcSSimon Wood break; 89622bcefdcSSimon Wood } 89722bcefdcSSimon Wood 89822bcefdcSSimon Wood return value ? LED_FULL : LED_OFF; 89922bcefdcSSimon Wood } 90022bcefdcSSimon Wood #endif 90122bcefdcSSimon Wood 902e7c23449SMichal Malý static u16 lg4ff_identify_multimode_wheel(struct hid_device *hid, const u16 reported_product_id, const u16 bcdDevice) 903e7c23449SMichal Malý { 904e7c23449SMichal Malý const struct lg4ff_wheel_ident_checklist *checklist; 905e7c23449SMichal Malý int i, from_idx, to_idx; 906e7c23449SMichal Malý 907e7c23449SMichal Malý switch (reported_product_id) { 908e7c23449SMichal Malý case USB_DEVICE_ID_LOGITECH_WHEEL: 909e7c23449SMichal Malý case USB_DEVICE_ID_LOGITECH_DFP_WHEEL: 910e7c23449SMichal Malý checklist = &lg4ff_main_checklist; 911e7c23449SMichal Malý from_idx = 0; 912e7c23449SMichal Malý to_idx = checklist->count - 1; 913e7c23449SMichal Malý break; 914e7c23449SMichal Malý case USB_DEVICE_ID_LOGITECH_G25_WHEEL: 915e7c23449SMichal Malý checklist = &lg4ff_main_checklist; 916e7c23449SMichal Malý from_idx = 0; 917e7c23449SMichal Malý to_idx = checklist->count - 2; /* End identity check at G25 */ 918e7c23449SMichal Malý break; 919e7c23449SMichal Malý case USB_DEVICE_ID_LOGITECH_G27_WHEEL: 920e7c23449SMichal Malý checklist = &lg4ff_main_checklist; 921e7c23449SMichal Malý from_idx = 1; /* Start identity check at G27 */ 922e7c23449SMichal Malý to_idx = checklist->count - 3; /* End identity check at G27 */ 923e7c23449SMichal Malý break; 924e7c23449SMichal Malý case USB_DEVICE_ID_LOGITECH_DFGT_WHEEL: 925e7c23449SMichal Malý checklist = &lg4ff_main_checklist; 926e7c23449SMichal Malý from_idx = 0; 927e7c23449SMichal Malý to_idx = checklist->count - 4; /* End identity check at DFGT */ 928e7c23449SMichal Malý break; 929e7c23449SMichal Malý default: 930e7c23449SMichal Malý return 0; 931e7c23449SMichal Malý } 932e7c23449SMichal Malý 933e7c23449SMichal Malý for (i = from_idx; i <= to_idx; i++) { 934e7c23449SMichal Malý const u16 mask = checklist->models[i]->mask; 935e7c23449SMichal Malý const u16 result = checklist->models[i]->result; 936e7c23449SMichal Malý const u16 real_product_id = checklist->models[i]->real_product_id; 937e7c23449SMichal Malý 938e7c23449SMichal Malý if ((bcdDevice & mask) == result) { 939e7c23449SMichal Malý dbg_hid("Found wheel with real PID %X whose reported PID is %X\n", real_product_id, reported_product_id); 940e7c23449SMichal Malý return real_product_id; 941e7c23449SMichal Malý } 942e7c23449SMichal Malý } 943e7c23449SMichal Malý 944f31a2de3SMichal Malý /* No match found. This is either Driving Force or an unknown 945f31a2de3SMichal Malý * wheel model, do not touch it */ 946e7c23449SMichal Malý dbg_hid("Wheel with bcdDevice %X was not recognized as multimode wheel, leaving in its current mode\n", bcdDevice); 947e7c23449SMichal Malý return 0; 948e7c23449SMichal Malý } 949e7c23449SMichal Malý 950e7c23449SMichal Malý static int lg4ff_handle_multimode_wheel(struct hid_device *hid, u16 *real_product_id, const u16 bcdDevice) 951e7c23449SMichal Malý { 952e7c23449SMichal Malý const u16 reported_product_id = hid->product; 953e7c23449SMichal Malý int ret; 954e7c23449SMichal Malý 955e7c23449SMichal Malý *real_product_id = lg4ff_identify_multimode_wheel(hid, reported_product_id, bcdDevice); 956e7c23449SMichal Malý /* Probed wheel is not a multimode wheel */ 957e7c23449SMichal Malý if (!*real_product_id) { 958e7c23449SMichal Malý *real_product_id = reported_product_id; 959e7c23449SMichal Malý dbg_hid("Wheel is not a multimode wheel\n"); 960e7c23449SMichal Malý return LG4FF_MMODE_NOT_MULTIMODE; 961e7c23449SMichal Malý } 962e7c23449SMichal Malý 963e7c23449SMichal Malý /* Switch from "Driving Force" mode to native mode automatically. 964e7c23449SMichal Malý * Otherwise keep the wheel in its current mode */ 965e7c23449SMichal Malý if (reported_product_id == USB_DEVICE_ID_LOGITECH_WHEEL && 966a54dc779SMichal Malý reported_product_id != *real_product_id && 967a54dc779SMichal Malý !lg4ff_no_autoswitch) { 968f31a2de3SMichal Malý const struct lg4ff_compat_mode_switch *s = lg4ff_get_mode_switch_command(*real_product_id, *real_product_id); 969e7c23449SMichal Malý 970f31a2de3SMichal Malý if (!s) { 971e7c23449SMichal Malý hid_err(hid, "Invalid product id %X\n", *real_product_id); 972b96d23ecSMichal Malý return LG4FF_MMODE_NOT_MULTIMODE; 973e7c23449SMichal Malý } 974e7c23449SMichal Malý 975e7c23449SMichal Malý ret = lg4ff_switch_compatibility_mode(hid, s); 976e7c23449SMichal Malý if (ret) { 977e7c23449SMichal Malý /* Wheel could not have been switched to native mode, 978e7c23449SMichal Malý * leave it in "Driving Force" mode and continue */ 979e7c23449SMichal Malý hid_err(hid, "Unable to switch wheel mode, errno %d\n", ret); 980b96d23ecSMichal Malý return LG4FF_MMODE_IS_MULTIMODE; 981e7c23449SMichal Malý } 982e7c23449SMichal Malý return LG4FF_MMODE_SWITCHED; 983e7c23449SMichal Malý } 984e7c23449SMichal Malý 985b96d23ecSMichal Malý return LG4FF_MMODE_IS_MULTIMODE; 986e7c23449SMichal Malý } 987e7c23449SMichal Malý 988e7c23449SMichal Malý 98932c88cbcSSimon Wood int lg4ff_init(struct hid_device *hid) 99032c88cbcSSimon Wood { 99132c88cbcSSimon Wood struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list); 99232c88cbcSSimon Wood struct input_dev *dev = hidinput->input; 993e7c23449SMichal Malý const struct usb_device_descriptor *udesc = &(hid_to_usb_dev(hid)->descriptor); 994e7c23449SMichal Malý const u16 bcdDevice = le16_to_cpu(udesc->bcdDevice); 99530bb75d7SMichal Malý struct lg4ff_device_entry *entry; 9963b6b17b7SMichal Malý struct lg_drv_data *drv_data; 997b96d23ecSMichal Malý int error, i, j; 998b96d23ecSMichal Malý int mmode_ret, mmode_idx = -1; 999e7c23449SMichal Malý u16 real_product_id; 100032c88cbcSSimon Wood 100132c88cbcSSimon Wood /* Check that the report looks ok */ 10020fb6bd06SKees Cook if (!hid_validate_values(hid, HID_OUTPUT_REPORT, 0, 0, 7)) 100332c88cbcSSimon Wood return -1; 100432c88cbcSSimon Wood 1005e7c23449SMichal Malý /* Check if a multimode wheel has been connected and 1006e7c23449SMichal Malý * handle it appropriately */ 1007b96d23ecSMichal Malý mmode_ret = lg4ff_handle_multimode_wheel(hid, &real_product_id, bcdDevice); 1008e7c23449SMichal Malý 1009e7c23449SMichal Malý /* Wheel has been told to switch to native mode. There is no point in going on 1010e7c23449SMichal Malý * with the initialization as the wheel will do a USB reset when it switches mode 1011e7c23449SMichal Malý */ 1012b96d23ecSMichal Malý if (mmode_ret == LG4FF_MMODE_SWITCHED) 1013e7c23449SMichal Malý return 0; 1014e7c23449SMichal Malý 10157362cd22SMichal Malý /* Check what wheel has been connected */ 10167362cd22SMichal Malý for (i = 0; i < ARRAY_SIZE(lg4ff_devices); i++) { 10177362cd22SMichal Malý if (hid->product == lg4ff_devices[i].product_id) { 10187362cd22SMichal Malý dbg_hid("Found compatible device, product ID %04X\n", lg4ff_devices[i].product_id); 10197362cd22SMichal Malý break; 10207362cd22SMichal Malý } 10217362cd22SMichal Malý } 10227362cd22SMichal Malý 10237362cd22SMichal Malý if (i == ARRAY_SIZE(lg4ff_devices)) { 10247362cd22SMichal Malý hid_err(hid, "Device is not supported by lg4ff driver. If you think it should be, consider reporting a bug to" 10257362cd22SMichal Malý "LKML, Simon Wood <simon@mungewell.org> or Michal Maly <madcatxster@gmail.com>\n"); 10267362cd22SMichal Malý return -1; 10277362cd22SMichal Malý } 10287362cd22SMichal Malý 1029b96d23ecSMichal Malý if (mmode_ret == LG4FF_MMODE_IS_MULTIMODE) { 1030b96d23ecSMichal Malý for (mmode_idx = 0; mmode_idx < ARRAY_SIZE(lg4ff_multimode_wheels); mmode_idx++) { 1031b96d23ecSMichal Malý if (real_product_id == lg4ff_multimode_wheels[mmode_idx].product_id) 1032b96d23ecSMichal Malý break; 1033b96d23ecSMichal Malý } 1034b96d23ecSMichal Malý 1035b96d23ecSMichal Malý if (mmode_idx == ARRAY_SIZE(lg4ff_multimode_wheels)) { 1036b96d23ecSMichal Malý hid_err(hid, "Device product ID %X is not listed as a multimode wheel", real_product_id); 1037b96d23ecSMichal Malý return -1; 1038b96d23ecSMichal Malý } 1039b96d23ecSMichal Malý } 1040b96d23ecSMichal Malý 10417362cd22SMichal Malý /* Set supported force feedback capabilities */ 10427362cd22SMichal Malý for (j = 0; lg4ff_devices[i].ff_effects[j] >= 0; j++) 10437362cd22SMichal Malý set_bit(lg4ff_devices[i].ff_effects[j], dev->ffbit); 104432c88cbcSSimon Wood 1045d0afd848SMichal Malý error = input_ff_create_memless(dev, NULL, lg4ff_play); 104632c88cbcSSimon Wood 104732c88cbcSSimon Wood if (error) 104832c88cbcSSimon Wood return error; 104932c88cbcSSimon Wood 10503b6b17b7SMichal Malý /* Get private driver data */ 10513b6b17b7SMichal Malý drv_data = hid_get_drvdata(hid); 10523b6b17b7SMichal Malý if (!drv_data) { 10533b6b17b7SMichal Malý hid_err(hid, "Cannot add device, private driver data not allocated\n"); 10543b6b17b7SMichal Malý return -1; 105530bb75d7SMichal Malý } 105630bb75d7SMichal Malý 10573b6b17b7SMichal Malý /* Initialize device properties */ 10588383c6bfSThomas Meyer entry = kzalloc(sizeof(struct lg4ff_device_entry), GFP_KERNEL); 105930bb75d7SMichal Malý if (!entry) { 10603b6b17b7SMichal Malý hid_err(hid, "Cannot add device, insufficient memory to allocate device properties.\n"); 106130bb75d7SMichal Malý return -ENOMEM; 106230bb75d7SMichal Malý } 10633b6b17b7SMichal Malý drv_data->device_props = entry; 10643b6b17b7SMichal Malý 10652b24a960SMichal Malý entry->product_id = lg4ff_devices[i].product_id; 1066b96d23ecSMichal Malý entry->real_product_id = real_product_id; 106730bb75d7SMichal Malý entry->min_range = lg4ff_devices[i].min_range; 106830bb75d7SMichal Malý entry->max_range = lg4ff_devices[i].max_range; 106930bb75d7SMichal Malý entry->set_range = lg4ff_devices[i].set_range; 1070b96d23ecSMichal Malý if (mmode_ret == LG4FF_MMODE_IS_MULTIMODE) { 1071b96d23ecSMichal Malý BUG_ON(mmode_idx == -1); 1072b96d23ecSMichal Malý entry->alternate_modes = lg4ff_multimode_wheels[mmode_idx].alternate_modes; 1073b96d23ecSMichal Malý entry->real_tag = lg4ff_multimode_wheels[mmode_idx].real_tag; 1074b96d23ecSMichal Malý entry->real_name = lg4ff_multimode_wheels[mmode_idx].real_name; 1075b96d23ecSMichal Malý } 107630bb75d7SMichal Malý 1077114a55cfSSimon Wood /* Check if autocentering is available and 1078114a55cfSSimon Wood * set the centering force to zero by default */ 1079114a55cfSSimon Wood if (test_bit(FF_AUTOCENTER, dev->ffbit)) { 1080e7c23449SMichal Malý /* Formula Force EX expects different autocentering command */ 1081e7c23449SMichal Malý if ((bcdDevice >> 8) == LG4FF_FFEX_REV_MAJ && 1082e7c23449SMichal Malý (bcdDevice & 0xff) == LG4FF_FFEX_REV_MIN) 1083d0afd848SMichal Malý dev->ff->set_autocenter = lg4ff_set_autocenter_ffex; 1084114a55cfSSimon Wood else 1085d0afd848SMichal Malý dev->ff->set_autocenter = lg4ff_set_autocenter_default; 1086114a55cfSSimon Wood 1087114a55cfSSimon Wood dev->ff->set_autocenter(dev, 0); 1088114a55cfSSimon Wood } 1089114a55cfSSimon Wood 109030bb75d7SMichal Malý /* Create sysfs interface */ 109130bb75d7SMichal Malý error = device_create_file(&hid->dev, &dev_attr_range); 109230bb75d7SMichal Malý if (error) 109330bb75d7SMichal Malý return error; 1094b96d23ecSMichal Malý if (mmode_ret == LG4FF_MMODE_IS_MULTIMODE) { 1095b96d23ecSMichal Malý error = device_create_file(&hid->dev, &dev_attr_real_id); 1096b96d23ecSMichal Malý if (error) 1097b96d23ecSMichal Malý return error; 1098b96d23ecSMichal Malý error = device_create_file(&hid->dev, &dev_attr_alternate_modes); 1099b96d23ecSMichal Malý if (error) 1100b96d23ecSMichal Malý return error; 1101b96d23ecSMichal Malý } 110230bb75d7SMichal Malý dbg_hid("sysfs interface created\n"); 110330bb75d7SMichal Malý 110430bb75d7SMichal Malý /* Set the maximum range to start with */ 110530bb75d7SMichal Malý entry->range = entry->max_range; 110630bb75d7SMichal Malý if (entry->set_range != NULL) 110730bb75d7SMichal Malý entry->set_range(hid, entry->range); 110830bb75d7SMichal Malý 110922bcefdcSSimon Wood #ifdef CONFIG_LEDS_CLASS 111022bcefdcSSimon Wood /* register led subsystem - G27 only */ 111122bcefdcSSimon Wood entry->led_state = 0; 111222bcefdcSSimon Wood for (j = 0; j < 5; j++) 111322bcefdcSSimon Wood entry->led[j] = NULL; 111422bcefdcSSimon Wood 111522bcefdcSSimon Wood if (lg4ff_devices[i].product_id == USB_DEVICE_ID_LOGITECH_G27_WHEEL) { 111622bcefdcSSimon Wood struct led_classdev *led; 111722bcefdcSSimon Wood size_t name_sz; 111822bcefdcSSimon Wood char *name; 111922bcefdcSSimon Wood 112022bcefdcSSimon Wood lg4ff_set_leds(hid, 0); 112122bcefdcSSimon Wood 112222bcefdcSSimon Wood name_sz = strlen(dev_name(&hid->dev)) + 8; 112322bcefdcSSimon Wood 112422bcefdcSSimon Wood for (j = 0; j < 5; j++) { 112522bcefdcSSimon Wood led = kzalloc(sizeof(struct led_classdev)+name_sz, GFP_KERNEL); 112622bcefdcSSimon Wood if (!led) { 112722bcefdcSSimon Wood hid_err(hid, "can't allocate memory for LED %d\n", j); 112822bcefdcSSimon Wood goto err; 112922bcefdcSSimon Wood } 113022bcefdcSSimon Wood 113122bcefdcSSimon Wood name = (void *)(&led[1]); 113222bcefdcSSimon Wood snprintf(name, name_sz, "%s::RPM%d", dev_name(&hid->dev), j+1); 113322bcefdcSSimon Wood led->name = name; 113422bcefdcSSimon Wood led->brightness = 0; 113522bcefdcSSimon Wood led->max_brightness = 1; 113622bcefdcSSimon Wood led->brightness_get = lg4ff_led_get_brightness; 113722bcefdcSSimon Wood led->brightness_set = lg4ff_led_set_brightness; 113822bcefdcSSimon Wood 113922bcefdcSSimon Wood entry->led[j] = led; 114022bcefdcSSimon Wood error = led_classdev_register(&hid->dev, led); 114122bcefdcSSimon Wood 114222bcefdcSSimon Wood if (error) { 114322bcefdcSSimon Wood hid_err(hid, "failed to register LED %d. Aborting.\n", j); 114422bcefdcSSimon Wood err: 114522bcefdcSSimon Wood /* Deregister LEDs (if any) */ 114622bcefdcSSimon Wood for (j = 0; j < 5; j++) { 114722bcefdcSSimon Wood led = entry->led[j]; 114822bcefdcSSimon Wood entry->led[j] = NULL; 114922bcefdcSSimon Wood if (!led) 115022bcefdcSSimon Wood continue; 115122bcefdcSSimon Wood led_classdev_unregister(led); 115222bcefdcSSimon Wood kfree(led); 115322bcefdcSSimon Wood } 115422bcefdcSSimon Wood goto out; /* Let the driver continue without LEDs */ 115522bcefdcSSimon Wood } 115622bcefdcSSimon Wood } 115722bcefdcSSimon Wood } 115822bcefdcSSimon Wood out: 1159c6e6dc87SJiri Kosina #endif 116064013800SSimon Wood hid_info(hid, "Force feedback support for Logitech Gaming Wheels\n"); 116132c88cbcSSimon Wood return 0; 116232c88cbcSSimon Wood } 116332c88cbcSSimon Wood 116430bb75d7SMichal Malý int lg4ff_deinit(struct hid_device *hid) 116530bb75d7SMichal Malý { 116630bb75d7SMichal Malý struct lg4ff_device_entry *entry; 11673b6b17b7SMichal Malý struct lg_drv_data *drv_data; 11686a2e176bSMichal Malý 11693b6b17b7SMichal Malý drv_data = hid_get_drvdata(hid); 11703b6b17b7SMichal Malý if (!drv_data) { 11713b6b17b7SMichal Malý hid_err(hid, "Error while deinitializing device, no private driver data.\n"); 117230bb75d7SMichal Malý return -1; 117330bb75d7SMichal Malý } 11743b6b17b7SMichal Malý entry = drv_data->device_props; 1175e7c23449SMichal Malý if (!entry) 1176e7c23449SMichal Malý goto out; /* Nothing more to do */ 1177e7c23449SMichal Malý 1178e7c23449SMichal Malý device_remove_file(&hid->dev, &dev_attr_range); 117922bcefdcSSimon Wood 1180b96d23ecSMichal Malý /* Multimode devices will have at least the "MODE_NATIVE" bit set */ 1181b96d23ecSMichal Malý if (entry->alternate_modes) { 1182b96d23ecSMichal Malý device_remove_file(&hid->dev, &dev_attr_real_id); 1183b96d23ecSMichal Malý device_remove_file(&hid->dev, &dev_attr_alternate_modes); 1184b96d23ecSMichal Malý } 1185b96d23ecSMichal Malý 118622bcefdcSSimon Wood #ifdef CONFIG_LEDS_CLASS 118722bcefdcSSimon Wood { 118822bcefdcSSimon Wood int j; 118922bcefdcSSimon Wood struct led_classdev *led; 119022bcefdcSSimon Wood 119122bcefdcSSimon Wood /* Deregister LEDs (if any) */ 119222bcefdcSSimon Wood for (j = 0; j < 5; j++) { 119322bcefdcSSimon Wood 119422bcefdcSSimon Wood led = entry->led[j]; 119522bcefdcSSimon Wood entry->led[j] = NULL; 119622bcefdcSSimon Wood if (!led) 119722bcefdcSSimon Wood continue; 119822bcefdcSSimon Wood led_classdev_unregister(led); 119922bcefdcSSimon Wood kfree(led); 120022bcefdcSSimon Wood } 120122bcefdcSSimon Wood } 120222bcefdcSSimon Wood #endif 120322bcefdcSSimon Wood 12043b6b17b7SMichal Malý /* Deallocate memory */ 12053b6b17b7SMichal Malý kfree(entry); 120630bb75d7SMichal Malý 1207e7c23449SMichal Malý out: 120830bb75d7SMichal Malý dbg_hid("Device successfully unregistered\n"); 120930bb75d7SMichal Malý return 0; 121030bb75d7SMichal Malý } 1211