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" 337362cd22SMichal Malý #include "hid-ids.h" 3432c88cbcSSimon Wood 3596440c8aSMichal Malý #define DFGT_REV_MAJ 0x13 3696440c8aSMichal Malý #define DFGT_REV_MIN 0x22 37d991938aSSimon Wood #define DFGT2_REV_MIN 0x26 3896440c8aSMichal Malý #define DFP_REV_MAJ 0x11 3996440c8aSMichal Malý #define DFP_REV_MIN 0x06 4096440c8aSMichal Malý #define FFEX_REV_MAJ 0x21 4196440c8aSMichal Malý #define FFEX_REV_MIN 0x00 4296440c8aSMichal Malý #define G25_REV_MAJ 0x12 4396440c8aSMichal Malý #define G25_REV_MIN 0x22 4496440c8aSMichal Malý #define G27_REV_MAJ 0x12 4596440c8aSMichal Malý #define G27_REV_MIN 0x38 4696440c8aSMichal Malý 4730bb75d7SMichal Malý #define to_hid_device(pdev) container_of(pdev, struct hid_device, dev) 4830bb75d7SMichal Malý 4930bb75d7SMichal Malý static void hid_lg4ff_set_range_dfp(struct hid_device *hid, u16 range); 5030bb75d7SMichal Malý static void hid_lg4ff_set_range_g25(struct hid_device *hid, u16 range); 5130bb75d7SMichal Malý static ssize_t lg4ff_range_show(struct device *dev, struct device_attribute *attr, char *buf); 5230bb75d7SMichal Malý static ssize_t lg4ff_range_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count); 5330bb75d7SMichal Malý 5430bb75d7SMichal Malý static DEVICE_ATTR(range, S_IRWXU | S_IRWXG | S_IRWXO, lg4ff_range_show, lg4ff_range_store); 5530bb75d7SMichal Malý 5630bb75d7SMichal Malý struct lg4ff_device_entry { 572b24a960SMichal Malý __u32 product_id; 5830bb75d7SMichal Malý __u16 range; 5930bb75d7SMichal Malý __u16 min_range; 6030bb75d7SMichal Malý __u16 max_range; 6122bcefdcSSimon Wood #ifdef CONFIG_LEDS_CLASS 6222bcefdcSSimon Wood __u8 led_state; 6322bcefdcSSimon Wood struct led_classdev *led[5]; 6422bcefdcSSimon Wood #endif 6530bb75d7SMichal Malý struct list_head list; 6630bb75d7SMichal Malý void (*set_range)(struct hid_device *hid, u16 range); 6730bb75d7SMichal Malý }; 6830bb75d7SMichal Malý 697362cd22SMichal Malý static const signed short lg4ff_wheel_effects[] = { 7032c88cbcSSimon Wood FF_CONSTANT, 7132c88cbcSSimon Wood FF_AUTOCENTER, 7232c88cbcSSimon Wood -1 7332c88cbcSSimon Wood }; 7432c88cbcSSimon Wood 757362cd22SMichal Malý struct lg4ff_wheel { 767362cd22SMichal Malý const __u32 product_id; 777362cd22SMichal Malý const signed short *ff_effects; 787362cd22SMichal Malý const __u16 min_range; 797362cd22SMichal Malý const __u16 max_range; 8030bb75d7SMichal Malý void (*set_range)(struct hid_device *hid, u16 range); 817362cd22SMichal Malý }; 827362cd22SMichal Malý 837362cd22SMichal Malý static const struct lg4ff_wheel lg4ff_devices[] = { 8430bb75d7SMichal Malý {USB_DEVICE_ID_LOGITECH_WHEEL, lg4ff_wheel_effects, 40, 270, NULL}, 8530bb75d7SMichal Malý {USB_DEVICE_ID_LOGITECH_MOMO_WHEEL, lg4ff_wheel_effects, 40, 270, NULL}, 8630bb75d7SMichal Malý {USB_DEVICE_ID_LOGITECH_DFP_WHEEL, lg4ff_wheel_effects, 40, 900, hid_lg4ff_set_range_dfp}, 8730bb75d7SMichal Malý {USB_DEVICE_ID_LOGITECH_G25_WHEEL, lg4ff_wheel_effects, 40, 900, hid_lg4ff_set_range_g25}, 8830bb75d7SMichal Malý {USB_DEVICE_ID_LOGITECH_DFGT_WHEEL, lg4ff_wheel_effects, 40, 900, hid_lg4ff_set_range_g25}, 8930bb75d7SMichal Malý {USB_DEVICE_ID_LOGITECH_G27_WHEEL, lg4ff_wheel_effects, 40, 900, hid_lg4ff_set_range_g25}, 9030bb75d7SMichal Malý {USB_DEVICE_ID_LOGITECH_MOMO_WHEEL2, lg4ff_wheel_effects, 40, 270, NULL}, 9130bb75d7SMichal Malý {USB_DEVICE_ID_LOGITECH_WII_WHEEL, lg4ff_wheel_effects, 40, 270, NULL} 927362cd22SMichal Malý }; 937362cd22SMichal Malý 9496440c8aSMichal Malý struct lg4ff_native_cmd { 9596440c8aSMichal Malý const __u8 cmd_num; /* Number of commands to send */ 9696440c8aSMichal Malý const __u8 cmd[]; 9796440c8aSMichal Malý }; 9896440c8aSMichal Malý 9996440c8aSMichal Malý struct lg4ff_usb_revision { 10096440c8aSMichal Malý const __u16 rev_maj; 10196440c8aSMichal Malý const __u16 rev_min; 10296440c8aSMichal Malý const struct lg4ff_native_cmd *command; 10396440c8aSMichal Malý }; 10496440c8aSMichal Malý 10596440c8aSMichal Malý static const struct lg4ff_native_cmd native_dfp = { 10696440c8aSMichal Malý 1, 10796440c8aSMichal Malý {0xf8, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00} 10896440c8aSMichal Malý }; 10996440c8aSMichal Malý 11096440c8aSMichal Malý static const struct lg4ff_native_cmd native_dfgt = { 11196440c8aSMichal Malý 2, 11296440c8aSMichal Malý {0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, /* 1st command */ 11396440c8aSMichal Malý 0xf8, 0x09, 0x03, 0x01, 0x00, 0x00, 0x00} /* 2nd command */ 11496440c8aSMichal Malý }; 11596440c8aSMichal Malý 11696440c8aSMichal Malý static const struct lg4ff_native_cmd native_g25 = { 11796440c8aSMichal Malý 1, 11896440c8aSMichal Malý {0xf8, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00} 11996440c8aSMichal Malý }; 12096440c8aSMichal Malý 12196440c8aSMichal Malý static const struct lg4ff_native_cmd native_g27 = { 12296440c8aSMichal Malý 2, 12396440c8aSMichal Malý {0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, /* 1st command */ 12496440c8aSMichal Malý 0xf8, 0x09, 0x04, 0x01, 0x00, 0x00, 0x00} /* 2nd command */ 12596440c8aSMichal Malý }; 12696440c8aSMichal Malý 12796440c8aSMichal Malý static const struct lg4ff_usb_revision lg4ff_revs[] = { 12896440c8aSMichal Malý {DFGT_REV_MAJ, DFGT_REV_MIN, &native_dfgt}, /* Driving Force GT */ 129d991938aSSimon Wood {DFGT_REV_MAJ, DFGT2_REV_MIN, &native_dfgt}, /* Driving Force GT v2 */ 13096440c8aSMichal Malý {DFP_REV_MAJ, DFP_REV_MIN, &native_dfp}, /* Driving Force Pro */ 13196440c8aSMichal Malý {G25_REV_MAJ, G25_REV_MIN, &native_g25}, /* G25 */ 13296440c8aSMichal Malý {G27_REV_MAJ, G27_REV_MIN, &native_g27}, /* G27 */ 13396440c8aSMichal Malý }; 13496440c8aSMichal Malý 1352b24a960SMichal Malý /* Recalculates X axis value accordingly to currently selected range */ 1362b24a960SMichal Malý static __s32 lg4ff_adjust_dfp_x_axis(__s32 value, __u16 range) 1372b24a960SMichal Malý { 1382b24a960SMichal Malý __u16 max_range; 1392b24a960SMichal Malý __s32 new_value; 1402b24a960SMichal Malý 1412b24a960SMichal Malý if (range == 900) 1422b24a960SMichal Malý return value; 1432b24a960SMichal Malý else if (range == 200) 1442b24a960SMichal Malý return value; 1452b24a960SMichal Malý else if (range < 200) 1462b24a960SMichal Malý max_range = 200; 1472b24a960SMichal Malý else 1482b24a960SMichal Malý max_range = 900; 1492b24a960SMichal Malý 1502b24a960SMichal Malý new_value = 8192 + mult_frac(value - 8192, max_range, range); 1512b24a960SMichal Malý if (new_value < 0) 1522b24a960SMichal Malý return 0; 1532b24a960SMichal Malý else if (new_value > 16383) 1542b24a960SMichal Malý return 16383; 1552b24a960SMichal Malý else 1562b24a960SMichal Malý return new_value; 1572b24a960SMichal Malý } 1582b24a960SMichal Malý 1592b24a960SMichal Malý int lg4ff_adjust_input_event(struct hid_device *hid, struct hid_field *field, 1602b24a960SMichal Malý struct hid_usage *usage, __s32 value, struct lg_drv_data *drv_data) 1612b24a960SMichal Malý { 1622b24a960SMichal Malý struct lg4ff_device_entry *entry = drv_data->device_props; 1632b24a960SMichal Malý __s32 new_value = 0; 1642b24a960SMichal Malý 1652b24a960SMichal Malý if (!entry) { 1662b24a960SMichal Malý hid_err(hid, "Device properties not found"); 1672b24a960SMichal Malý return 0; 1682b24a960SMichal Malý } 1692b24a960SMichal Malý 1702b24a960SMichal Malý switch (entry->product_id) { 1712b24a960SMichal Malý case USB_DEVICE_ID_LOGITECH_DFP_WHEEL: 1722b24a960SMichal Malý switch (usage->code) { 1732b24a960SMichal Malý case ABS_X: 1742b24a960SMichal Malý new_value = lg4ff_adjust_dfp_x_axis(value, entry->range); 1752b24a960SMichal Malý input_event(field->hidinput->input, usage->type, usage->code, new_value); 1762b24a960SMichal Malý return 1; 1772b24a960SMichal Malý default: 1782b24a960SMichal Malý return 0; 1792b24a960SMichal Malý } 1802b24a960SMichal Malý default: 1812b24a960SMichal Malý return 0; 1822b24a960SMichal Malý } 1832b24a960SMichal Malý } 1842b24a960SMichal Malý 18530bb75d7SMichal Malý static int hid_lg4ff_play(struct input_dev *dev, void *data, struct ff_effect *effect) 18632c88cbcSSimon Wood { 18732c88cbcSSimon Wood struct hid_device *hid = input_get_drvdata(dev); 18832c88cbcSSimon Wood struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list; 18932c88cbcSSimon Wood struct hid_report *report = list_entry(report_list->next, struct hid_report, list); 19074479ba8SMichal Malý __s32 *value = report->field[0]->value; 19132c88cbcSSimon Wood int x; 19232c88cbcSSimon Wood 193a80fe5d6SMichal Malý #define CLAMP(x) do { if (x < 0) x = 0; else if (x > 0xff) x = 0xff; } while (0) 19432c88cbcSSimon Wood 19532c88cbcSSimon Wood switch (effect->type) { 19632c88cbcSSimon Wood case FF_CONSTANT: 19732c88cbcSSimon Wood x = effect->u.ramp.start_level + 0x80; /* 0x80 is no force */ 19832c88cbcSSimon Wood CLAMP(x); 199*56930e7aSSimon Wood 200*56930e7aSSimon Wood if (x == 0x80) { 201*56930e7aSSimon Wood /* De-activate force in slot-1*/ 202*56930e7aSSimon Wood value[0] = 0x13; 203*56930e7aSSimon Wood value[1] = 0x00; 204*56930e7aSSimon Wood value[2] = 0x00; 205*56930e7aSSimon Wood value[3] = 0x00; 206*56930e7aSSimon Wood value[4] = 0x00; 207*56930e7aSSimon Wood value[5] = 0x00; 208*56930e7aSSimon Wood value[6] = 0x00; 209*56930e7aSSimon Wood 210*56930e7aSSimon Wood hid_hw_request(hid, report, HID_REQ_SET_REPORT); 211*56930e7aSSimon Wood return 0; 212*56930e7aSSimon Wood } 213*56930e7aSSimon Wood 21474479ba8SMichal Malý value[0] = 0x11; /* Slot 1 */ 21574479ba8SMichal Malý value[1] = 0x08; 21674479ba8SMichal Malý value[2] = x; 21774479ba8SMichal Malý value[3] = 0x80; 21874479ba8SMichal Malý value[4] = 0x00; 21974479ba8SMichal Malý value[5] = 0x00; 22074479ba8SMichal Malý value[6] = 0x00; 22132c88cbcSSimon Wood 222d8814272SBenjamin Tissoires hid_hw_request(hid, report, HID_REQ_SET_REPORT); 22332c88cbcSSimon Wood break; 22432c88cbcSSimon Wood } 22532c88cbcSSimon Wood return 0; 22632c88cbcSSimon Wood } 22732c88cbcSSimon Wood 2286e2de8e0SMichal Malý /* Sends default autocentering command compatible with 2296e2de8e0SMichal Malý * all wheels except Formula Force EX */ 2306e2de8e0SMichal Malý static void hid_lg4ff_set_autocenter_default(struct input_dev *dev, u16 magnitude) 23132c88cbcSSimon Wood { 23232c88cbcSSimon Wood struct hid_device *hid = input_get_drvdata(dev); 23332c88cbcSSimon Wood struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list; 23432c88cbcSSimon Wood struct hid_report *report = list_entry(report_list->next, struct hid_report, list); 23574479ba8SMichal Malý __s32 *value = report->field[0]->value; 236f8c23156SSimon Wood __u32 expand_a, expand_b; 237f8c23156SSimon Wood 238d2c02da5SSimon Wood /* De-activate Auto-Center */ 239d2c02da5SSimon Wood if (magnitude == 0) { 240d2c02da5SSimon Wood value[0] = 0xf5; 241d2c02da5SSimon Wood value[1] = 0x00; 242d2c02da5SSimon Wood value[2] = 0x00; 243d2c02da5SSimon Wood value[3] = 0x00; 244d2c02da5SSimon Wood value[4] = 0x00; 245d2c02da5SSimon Wood value[5] = 0x00; 246d2c02da5SSimon Wood value[6] = 0x00; 247d2c02da5SSimon Wood 248d2c02da5SSimon Wood hid_hw_request(hid, report, HID_REQ_SET_REPORT); 249d2c02da5SSimon Wood return; 250d2c02da5SSimon Wood } 251d2c02da5SSimon Wood 252f8c23156SSimon Wood if (magnitude <= 0xaaaa) { 253f8c23156SSimon Wood expand_a = 0x0c * magnitude; 254f8c23156SSimon Wood expand_b = 0x80 * magnitude; 255f8c23156SSimon Wood } else { 256f8c23156SSimon Wood expand_a = (0x0c * 0xaaaa) + 0x06 * (magnitude - 0xaaaa); 257f8c23156SSimon Wood expand_b = (0x80 * 0xaaaa) + 0xff * (magnitude - 0xaaaa); 258f8c23156SSimon Wood } 25932c88cbcSSimon Wood 26074479ba8SMichal Malý value[0] = 0xfe; 26174479ba8SMichal Malý value[1] = 0x0d; 262f8c23156SSimon Wood value[2] = expand_a / 0xaaaa; 263f8c23156SSimon Wood value[3] = expand_a / 0xaaaa; 264f8c23156SSimon Wood value[4] = expand_b / 0xaaaa; 26574479ba8SMichal Malý value[5] = 0x00; 26674479ba8SMichal Malý value[6] = 0x00; 26732c88cbcSSimon Wood 268d8814272SBenjamin Tissoires hid_hw_request(hid, report, HID_REQ_SET_REPORT); 269d2c02da5SSimon Wood 270d2c02da5SSimon Wood /* Activate Auto-Center */ 271d2c02da5SSimon Wood value[0] = 0x14; 272d2c02da5SSimon Wood value[1] = 0x00; 273d2c02da5SSimon Wood value[2] = 0x00; 274d2c02da5SSimon Wood value[3] = 0x00; 275d2c02da5SSimon Wood value[4] = 0x00; 276d2c02da5SSimon Wood value[5] = 0x00; 277d2c02da5SSimon Wood value[6] = 0x00; 278d2c02da5SSimon Wood 279d2c02da5SSimon Wood hid_hw_request(hid, report, HID_REQ_SET_REPORT); 28032c88cbcSSimon Wood } 28132c88cbcSSimon Wood 2826e2de8e0SMichal Malý /* Sends autocentering command compatible with Formula Force EX */ 2836e2de8e0SMichal Malý static void hid_lg4ff_set_autocenter_ffex(struct input_dev *dev, u16 magnitude) 2846e2de8e0SMichal Malý { 2856e2de8e0SMichal Malý struct hid_device *hid = input_get_drvdata(dev); 2866e2de8e0SMichal Malý struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list; 2876e2de8e0SMichal Malý struct hid_report *report = list_entry(report_list->next, struct hid_report, list); 28874479ba8SMichal Malý __s32 *value = report->field[0]->value; 2896e2de8e0SMichal Malý magnitude = magnitude * 90 / 65535; 2906e2de8e0SMichal Malý 29174479ba8SMichal Malý value[0] = 0xfe; 29274479ba8SMichal Malý value[1] = 0x03; 29374479ba8SMichal Malý value[2] = magnitude >> 14; 29474479ba8SMichal Malý value[3] = magnitude >> 14; 29574479ba8SMichal Malý value[4] = magnitude; 29674479ba8SMichal Malý value[5] = 0x00; 29774479ba8SMichal Malý value[6] = 0x00; 2986e2de8e0SMichal Malý 299d8814272SBenjamin Tissoires hid_hw_request(hid, report, HID_REQ_SET_REPORT); 3006e2de8e0SMichal Malý } 3016e2de8e0SMichal Malý 30230bb75d7SMichal Malý /* Sends command to set range compatible with G25/G27/Driving Force GT */ 30330bb75d7SMichal Malý static void hid_lg4ff_set_range_g25(struct hid_device *hid, u16 range) 30430bb75d7SMichal Malý { 30530bb75d7SMichal Malý struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list; 30630bb75d7SMichal Malý struct hid_report *report = list_entry(report_list->next, struct hid_report, list); 30774479ba8SMichal Malý __s32 *value = report->field[0]->value; 30874479ba8SMichal Malý 30930bb75d7SMichal Malý dbg_hid("G25/G27/DFGT: setting range to %u\n", range); 31030bb75d7SMichal Malý 31174479ba8SMichal Malý value[0] = 0xf8; 31274479ba8SMichal Malý value[1] = 0x81; 31374479ba8SMichal Malý value[2] = range & 0x00ff; 31474479ba8SMichal Malý value[3] = (range & 0xff00) >> 8; 31574479ba8SMichal Malý value[4] = 0x00; 31674479ba8SMichal Malý value[5] = 0x00; 31774479ba8SMichal Malý value[6] = 0x00; 31830bb75d7SMichal Malý 319d8814272SBenjamin Tissoires hid_hw_request(hid, report, HID_REQ_SET_REPORT); 32030bb75d7SMichal Malý } 32130bb75d7SMichal Malý 32230bb75d7SMichal Malý /* Sends commands to set range compatible with Driving Force Pro wheel */ 32330bb75d7SMichal Malý static void hid_lg4ff_set_range_dfp(struct hid_device *hid, __u16 range) 32430bb75d7SMichal Malý { 32530bb75d7SMichal Malý struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list; 32630bb75d7SMichal Malý struct hid_report *report = list_entry(report_list->next, struct hid_report, list); 32730bb75d7SMichal Malý int start_left, start_right, full_range; 32874479ba8SMichal Malý __s32 *value = report->field[0]->value; 32974479ba8SMichal Malý 33030bb75d7SMichal Malý dbg_hid("Driving Force Pro: setting range to %u\n", range); 33130bb75d7SMichal Malý 33230bb75d7SMichal Malý /* Prepare "coarse" limit command */ 33374479ba8SMichal Malý value[0] = 0xf8; 33474479ba8SMichal Malý value[1] = 0x00; /* Set later */ 33574479ba8SMichal Malý value[2] = 0x00; 33674479ba8SMichal Malý value[3] = 0x00; 33774479ba8SMichal Malý value[4] = 0x00; 33874479ba8SMichal Malý value[5] = 0x00; 33974479ba8SMichal Malý value[6] = 0x00; 34030bb75d7SMichal Malý 34130bb75d7SMichal Malý if (range > 200) { 34230bb75d7SMichal Malý report->field[0]->value[1] = 0x03; 34330bb75d7SMichal Malý full_range = 900; 34430bb75d7SMichal Malý } else { 34530bb75d7SMichal Malý report->field[0]->value[1] = 0x02; 34630bb75d7SMichal Malý full_range = 200; 34730bb75d7SMichal Malý } 348d8814272SBenjamin Tissoires hid_hw_request(hid, report, HID_REQ_SET_REPORT); 34930bb75d7SMichal Malý 35030bb75d7SMichal Malý /* Prepare "fine" limit command */ 35174479ba8SMichal Malý value[0] = 0x81; 35274479ba8SMichal Malý value[1] = 0x0b; 35374479ba8SMichal Malý value[2] = 0x00; 35474479ba8SMichal Malý value[3] = 0x00; 35574479ba8SMichal Malý value[4] = 0x00; 35674479ba8SMichal Malý value[5] = 0x00; 35774479ba8SMichal Malý value[6] = 0x00; 35830bb75d7SMichal Malý 35930bb75d7SMichal Malý if (range == 200 || range == 900) { /* Do not apply any fine limit */ 360d8814272SBenjamin Tissoires hid_hw_request(hid, report, HID_REQ_SET_REPORT); 36130bb75d7SMichal Malý return; 36230bb75d7SMichal Malý } 36330bb75d7SMichal Malý 36430bb75d7SMichal Malý /* Construct fine limit command */ 36530bb75d7SMichal Malý start_left = (((full_range - range + 1) * 2047) / full_range); 36630bb75d7SMichal Malý start_right = 0xfff - start_left; 36730bb75d7SMichal Malý 36874479ba8SMichal Malý value[2] = start_left >> 4; 36974479ba8SMichal Malý value[3] = start_right >> 4; 37074479ba8SMichal Malý value[4] = 0xff; 37174479ba8SMichal Malý value[5] = (start_right & 0xe) << 4 | (start_left & 0xe); 37274479ba8SMichal Malý value[6] = 0xff; 37330bb75d7SMichal Malý 374d8814272SBenjamin Tissoires hid_hw_request(hid, report, HID_REQ_SET_REPORT); 37530bb75d7SMichal Malý } 37630bb75d7SMichal Malý 37796440c8aSMichal Malý static void hid_lg4ff_switch_native(struct hid_device *hid, const struct lg4ff_native_cmd *cmd) 37896440c8aSMichal Malý { 37996440c8aSMichal Malý struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list; 38096440c8aSMichal Malý struct hid_report *report = list_entry(report_list->next, struct hid_report, list); 38196440c8aSMichal Malý __u8 i, j; 38296440c8aSMichal Malý 38396440c8aSMichal Malý j = 0; 38496440c8aSMichal Malý while (j < 7*cmd->cmd_num) { 38596440c8aSMichal Malý for (i = 0; i < 7; i++) 38696440c8aSMichal Malý report->field[0]->value[i] = cmd->cmd[j++]; 38796440c8aSMichal Malý 388d8814272SBenjamin Tissoires hid_hw_request(hid, report, HID_REQ_SET_REPORT); 38996440c8aSMichal Malý } 39096440c8aSMichal Malý } 39132c88cbcSSimon Wood 39230bb75d7SMichal Malý /* Read current range and display it in terminal */ 39330bb75d7SMichal Malý static ssize_t lg4ff_range_show(struct device *dev, struct device_attribute *attr, char *buf) 39430bb75d7SMichal Malý { 39530bb75d7SMichal Malý struct hid_device *hid = to_hid_device(dev); 3963b6b17b7SMichal Malý struct lg4ff_device_entry *entry; 3973b6b17b7SMichal Malý struct lg_drv_data *drv_data; 39830bb75d7SMichal Malý size_t count; 39930bb75d7SMichal Malý 4003b6b17b7SMichal Malý drv_data = hid_get_drvdata(hid); 4013b6b17b7SMichal Malý if (!drv_data) { 4023b6b17b7SMichal Malý hid_err(hid, "Private driver data not found!\n"); 4033b6b17b7SMichal Malý return 0; 40430bb75d7SMichal Malý } 4053b6b17b7SMichal Malý 4063b6b17b7SMichal Malý entry = drv_data->device_props; 4073b6b17b7SMichal Malý if (!entry) { 4083b6b17b7SMichal Malý hid_err(hid, "Device properties not found!\n"); 40930bb75d7SMichal Malý return 0; 41030bb75d7SMichal Malý } 41130bb75d7SMichal Malý 41230bb75d7SMichal Malý count = scnprintf(buf, PAGE_SIZE, "%u\n", entry->range); 41330bb75d7SMichal Malý return count; 41430bb75d7SMichal Malý } 41530bb75d7SMichal Malý 41630bb75d7SMichal Malý /* Set range to user specified value, call appropriate function 41730bb75d7SMichal Malý * according to the type of the wheel */ 41830bb75d7SMichal Malý static ssize_t lg4ff_range_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) 41930bb75d7SMichal Malý { 42030bb75d7SMichal Malý struct hid_device *hid = to_hid_device(dev); 4213b6b17b7SMichal Malý struct lg4ff_device_entry *entry; 4223b6b17b7SMichal Malý struct lg_drv_data *drv_data; 42330bb75d7SMichal Malý __u16 range = simple_strtoul(buf, NULL, 10); 42430bb75d7SMichal Malý 4253b6b17b7SMichal Malý drv_data = hid_get_drvdata(hid); 4263b6b17b7SMichal Malý if (!drv_data) { 4273b6b17b7SMichal Malý hid_err(hid, "Private driver data not found!\n"); 4283b6b17b7SMichal Malý return 0; 42930bb75d7SMichal Malý } 4303b6b17b7SMichal Malý 4313b6b17b7SMichal Malý entry = drv_data->device_props; 4323b6b17b7SMichal Malý if (!entry) { 4333b6b17b7SMichal Malý hid_err(hid, "Device properties not found!\n"); 4343b6b17b7SMichal Malý return 0; 43530bb75d7SMichal Malý } 43630bb75d7SMichal Malý 43730bb75d7SMichal Malý if (range == 0) 43830bb75d7SMichal Malý range = entry->max_range; 43930bb75d7SMichal Malý 44030bb75d7SMichal Malý /* Check if the wheel supports range setting 44130bb75d7SMichal Malý * and that the range is within limits for the wheel */ 44230bb75d7SMichal Malý if (entry->set_range != NULL && range >= entry->min_range && range <= entry->max_range) { 44330bb75d7SMichal Malý entry->set_range(hid, range); 44430bb75d7SMichal Malý entry->range = range; 44530bb75d7SMichal Malý } 44630bb75d7SMichal Malý 44730bb75d7SMichal Malý return count; 44830bb75d7SMichal Malý } 44930bb75d7SMichal Malý 45022bcefdcSSimon Wood #ifdef CONFIG_LEDS_CLASS 45122bcefdcSSimon Wood static void lg4ff_set_leds(struct hid_device *hid, __u8 leds) 45222bcefdcSSimon Wood { 45322bcefdcSSimon Wood struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list; 45422bcefdcSSimon Wood struct hid_report *report = list_entry(report_list->next, struct hid_report, list); 45574479ba8SMichal Malý __s32 *value = report->field[0]->value; 45622bcefdcSSimon Wood 45774479ba8SMichal Malý value[0] = 0xf8; 45874479ba8SMichal Malý value[1] = 0x12; 45974479ba8SMichal Malý value[2] = leds; 46074479ba8SMichal Malý value[3] = 0x00; 46174479ba8SMichal Malý value[4] = 0x00; 46274479ba8SMichal Malý value[5] = 0x00; 46374479ba8SMichal Malý value[6] = 0x00; 464d8814272SBenjamin Tissoires hid_hw_request(hid, report, HID_REQ_SET_REPORT); 46522bcefdcSSimon Wood } 46622bcefdcSSimon Wood 46722bcefdcSSimon Wood static void lg4ff_led_set_brightness(struct led_classdev *led_cdev, 46822bcefdcSSimon Wood enum led_brightness value) 46922bcefdcSSimon Wood { 47022bcefdcSSimon Wood struct device *dev = led_cdev->dev->parent; 47122bcefdcSSimon Wood struct hid_device *hid = container_of(dev, struct hid_device, dev); 4724629fd16SAxel Lin struct lg_drv_data *drv_data = hid_get_drvdata(hid); 47322bcefdcSSimon Wood struct lg4ff_device_entry *entry; 47422bcefdcSSimon Wood int i, state = 0; 47522bcefdcSSimon Wood 47622bcefdcSSimon Wood if (!drv_data) { 47722bcefdcSSimon Wood hid_err(hid, "Device data not found."); 47822bcefdcSSimon Wood return; 47922bcefdcSSimon Wood } 48022bcefdcSSimon Wood 48122bcefdcSSimon Wood entry = (struct lg4ff_device_entry *)drv_data->device_props; 48222bcefdcSSimon Wood 48322bcefdcSSimon Wood if (!entry) { 48422bcefdcSSimon Wood hid_err(hid, "Device properties not found."); 48522bcefdcSSimon Wood return; 48622bcefdcSSimon Wood } 48722bcefdcSSimon Wood 48822bcefdcSSimon Wood for (i = 0; i < 5; i++) { 48922bcefdcSSimon Wood if (led_cdev != entry->led[i]) 49022bcefdcSSimon Wood continue; 49122bcefdcSSimon Wood state = (entry->led_state >> i) & 1; 49222bcefdcSSimon Wood if (value == LED_OFF && state) { 49322bcefdcSSimon Wood entry->led_state &= ~(1 << i); 49422bcefdcSSimon Wood lg4ff_set_leds(hid, entry->led_state); 49522bcefdcSSimon Wood } else if (value != LED_OFF && !state) { 49622bcefdcSSimon Wood entry->led_state |= 1 << i; 49722bcefdcSSimon Wood lg4ff_set_leds(hid, entry->led_state); 49822bcefdcSSimon Wood } 49922bcefdcSSimon Wood break; 50022bcefdcSSimon Wood } 50122bcefdcSSimon Wood } 50222bcefdcSSimon Wood 50322bcefdcSSimon Wood static enum led_brightness lg4ff_led_get_brightness(struct led_classdev *led_cdev) 50422bcefdcSSimon Wood { 50522bcefdcSSimon Wood struct device *dev = led_cdev->dev->parent; 50622bcefdcSSimon Wood struct hid_device *hid = container_of(dev, struct hid_device, dev); 5074629fd16SAxel Lin struct lg_drv_data *drv_data = hid_get_drvdata(hid); 50822bcefdcSSimon Wood struct lg4ff_device_entry *entry; 50922bcefdcSSimon Wood int i, value = 0; 51022bcefdcSSimon Wood 51122bcefdcSSimon Wood if (!drv_data) { 51222bcefdcSSimon Wood hid_err(hid, "Device data not found."); 51322bcefdcSSimon Wood return LED_OFF; 51422bcefdcSSimon Wood } 51522bcefdcSSimon Wood 51622bcefdcSSimon Wood entry = (struct lg4ff_device_entry *)drv_data->device_props; 51722bcefdcSSimon Wood 51822bcefdcSSimon Wood if (!entry) { 51922bcefdcSSimon Wood hid_err(hid, "Device properties not found."); 52022bcefdcSSimon Wood return LED_OFF; 52122bcefdcSSimon Wood } 52222bcefdcSSimon Wood 52322bcefdcSSimon Wood for (i = 0; i < 5; i++) 52422bcefdcSSimon Wood if (led_cdev == entry->led[i]) { 52522bcefdcSSimon Wood value = (entry->led_state >> i) & 1; 52622bcefdcSSimon Wood break; 52722bcefdcSSimon Wood } 52822bcefdcSSimon Wood 52922bcefdcSSimon Wood return value ? LED_FULL : LED_OFF; 53022bcefdcSSimon Wood } 53122bcefdcSSimon Wood #endif 53222bcefdcSSimon Wood 53332c88cbcSSimon Wood int lg4ff_init(struct hid_device *hid) 53432c88cbcSSimon Wood { 53532c88cbcSSimon Wood struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list); 53632c88cbcSSimon Wood struct input_dev *dev = hidinput->input; 53730bb75d7SMichal Malý struct lg4ff_device_entry *entry; 5383b6b17b7SMichal Malý struct lg_drv_data *drv_data; 53930bb75d7SMichal Malý struct usb_device_descriptor *udesc; 5407362cd22SMichal Malý int error, i, j; 54196440c8aSMichal Malý __u16 bcdDevice, rev_maj, rev_min; 54232c88cbcSSimon Wood 54332c88cbcSSimon Wood /* Check that the report looks ok */ 5440fb6bd06SKees Cook if (!hid_validate_values(hid, HID_OUTPUT_REPORT, 0, 0, 7)) 54532c88cbcSSimon Wood return -1; 54632c88cbcSSimon Wood 5477362cd22SMichal Malý /* Check what wheel has been connected */ 5487362cd22SMichal Malý for (i = 0; i < ARRAY_SIZE(lg4ff_devices); i++) { 5497362cd22SMichal Malý if (hid->product == lg4ff_devices[i].product_id) { 5507362cd22SMichal Malý dbg_hid("Found compatible device, product ID %04X\n", lg4ff_devices[i].product_id); 5517362cd22SMichal Malý break; 5527362cd22SMichal Malý } 5537362cd22SMichal Malý } 5547362cd22SMichal Malý 5557362cd22SMichal Malý if (i == ARRAY_SIZE(lg4ff_devices)) { 5567362cd22SMichal Malý hid_err(hid, "Device is not supported by lg4ff driver. If you think it should be, consider reporting a bug to" 5577362cd22SMichal Malý "LKML, Simon Wood <simon@mungewell.org> or Michal Maly <madcatxster@gmail.com>\n"); 5587362cd22SMichal Malý return -1; 5597362cd22SMichal Malý } 5607362cd22SMichal Malý 56196440c8aSMichal Malý /* Attempt to switch wheel to native mode when applicable */ 56296440c8aSMichal Malý udesc = &(hid_to_usb_dev(hid)->descriptor); 56396440c8aSMichal Malý if (!udesc) { 56496440c8aSMichal Malý hid_err(hid, "NULL USB device descriptor\n"); 56596440c8aSMichal Malý return -1; 56696440c8aSMichal Malý } 56796440c8aSMichal Malý bcdDevice = le16_to_cpu(udesc->bcdDevice); 56896440c8aSMichal Malý rev_maj = bcdDevice >> 8; 56996440c8aSMichal Malý rev_min = bcdDevice & 0xff; 57096440c8aSMichal Malý 57196440c8aSMichal Malý if (lg4ff_devices[i].product_id == USB_DEVICE_ID_LOGITECH_WHEEL) { 57296440c8aSMichal Malý dbg_hid("Generic wheel detected, can it do native?\n"); 57396440c8aSMichal Malý dbg_hid("USB revision: %2x.%02x\n", rev_maj, rev_min); 57496440c8aSMichal Malý 57596440c8aSMichal Malý for (j = 0; j < ARRAY_SIZE(lg4ff_revs); j++) { 57696440c8aSMichal Malý if (lg4ff_revs[j].rev_maj == rev_maj && lg4ff_revs[j].rev_min == rev_min) { 57796440c8aSMichal Malý hid_lg4ff_switch_native(hid, lg4ff_revs[j].command); 57896440c8aSMichal Malý hid_info(hid, "Switched to native mode\n"); 57996440c8aSMichal Malý } 58096440c8aSMichal Malý } 58196440c8aSMichal Malý } 58296440c8aSMichal Malý 5837362cd22SMichal Malý /* Set supported force feedback capabilities */ 5847362cd22SMichal Malý for (j = 0; lg4ff_devices[i].ff_effects[j] >= 0; j++) 5857362cd22SMichal Malý set_bit(lg4ff_devices[i].ff_effects[j], dev->ffbit); 58632c88cbcSSimon Wood 58732c88cbcSSimon Wood error = input_ff_create_memless(dev, NULL, hid_lg4ff_play); 58832c88cbcSSimon Wood 58932c88cbcSSimon Wood if (error) 59032c88cbcSSimon Wood return error; 59132c88cbcSSimon Wood 5926e2de8e0SMichal Malý /* Check if autocentering is available and 5936e2de8e0SMichal Malý * set the centering force to zero by default */ 5946e2de8e0SMichal Malý if (test_bit(FF_AUTOCENTER, dev->ffbit)) { 5956e2de8e0SMichal Malý if (rev_maj == FFEX_REV_MAJ && rev_min == FFEX_REV_MIN) /* Formula Force EX expects different autocentering command */ 5966e2de8e0SMichal Malý dev->ff->set_autocenter = hid_lg4ff_set_autocenter_ffex; 5976e2de8e0SMichal Malý else 5986e2de8e0SMichal Malý dev->ff->set_autocenter = hid_lg4ff_set_autocenter_default; 5996e2de8e0SMichal Malý 6006e2de8e0SMichal Malý dev->ff->set_autocenter(dev, 0); 6016e2de8e0SMichal Malý } 60232c88cbcSSimon Wood 6033b6b17b7SMichal Malý /* Get private driver data */ 6043b6b17b7SMichal Malý drv_data = hid_get_drvdata(hid); 6053b6b17b7SMichal Malý if (!drv_data) { 6063b6b17b7SMichal Malý hid_err(hid, "Cannot add device, private driver data not allocated\n"); 6073b6b17b7SMichal Malý return -1; 60830bb75d7SMichal Malý } 60930bb75d7SMichal Malý 6103b6b17b7SMichal Malý /* Initialize device properties */ 6118383c6bfSThomas Meyer entry = kzalloc(sizeof(struct lg4ff_device_entry), GFP_KERNEL); 61230bb75d7SMichal Malý if (!entry) { 6133b6b17b7SMichal Malý hid_err(hid, "Cannot add device, insufficient memory to allocate device properties.\n"); 61430bb75d7SMichal Malý return -ENOMEM; 61530bb75d7SMichal Malý } 6163b6b17b7SMichal Malý drv_data->device_props = entry; 6173b6b17b7SMichal Malý 6182b24a960SMichal Malý entry->product_id = lg4ff_devices[i].product_id; 61930bb75d7SMichal Malý entry->min_range = lg4ff_devices[i].min_range; 62030bb75d7SMichal Malý entry->max_range = lg4ff_devices[i].max_range; 62130bb75d7SMichal Malý entry->set_range = lg4ff_devices[i].set_range; 62230bb75d7SMichal Malý 62330bb75d7SMichal Malý /* Create sysfs interface */ 62430bb75d7SMichal Malý error = device_create_file(&hid->dev, &dev_attr_range); 62530bb75d7SMichal Malý if (error) 62630bb75d7SMichal Malý return error; 62730bb75d7SMichal Malý dbg_hid("sysfs interface created\n"); 62830bb75d7SMichal Malý 62930bb75d7SMichal Malý /* Set the maximum range to start with */ 63030bb75d7SMichal Malý entry->range = entry->max_range; 63130bb75d7SMichal Malý if (entry->set_range != NULL) 63230bb75d7SMichal Malý entry->set_range(hid, entry->range); 63330bb75d7SMichal Malý 63422bcefdcSSimon Wood #ifdef CONFIG_LEDS_CLASS 63522bcefdcSSimon Wood /* register led subsystem - G27 only */ 63622bcefdcSSimon Wood entry->led_state = 0; 63722bcefdcSSimon Wood for (j = 0; j < 5; j++) 63822bcefdcSSimon Wood entry->led[j] = NULL; 63922bcefdcSSimon Wood 64022bcefdcSSimon Wood if (lg4ff_devices[i].product_id == USB_DEVICE_ID_LOGITECH_G27_WHEEL) { 64122bcefdcSSimon Wood struct led_classdev *led; 64222bcefdcSSimon Wood size_t name_sz; 64322bcefdcSSimon Wood char *name; 64422bcefdcSSimon Wood 64522bcefdcSSimon Wood lg4ff_set_leds(hid, 0); 64622bcefdcSSimon Wood 64722bcefdcSSimon Wood name_sz = strlen(dev_name(&hid->dev)) + 8; 64822bcefdcSSimon Wood 64922bcefdcSSimon Wood for (j = 0; j < 5; j++) { 65022bcefdcSSimon Wood led = kzalloc(sizeof(struct led_classdev)+name_sz, GFP_KERNEL); 65122bcefdcSSimon Wood if (!led) { 65222bcefdcSSimon Wood hid_err(hid, "can't allocate memory for LED %d\n", j); 65322bcefdcSSimon Wood goto err; 65422bcefdcSSimon Wood } 65522bcefdcSSimon Wood 65622bcefdcSSimon Wood name = (void *)(&led[1]); 65722bcefdcSSimon Wood snprintf(name, name_sz, "%s::RPM%d", dev_name(&hid->dev), j+1); 65822bcefdcSSimon Wood led->name = name; 65922bcefdcSSimon Wood led->brightness = 0; 66022bcefdcSSimon Wood led->max_brightness = 1; 66122bcefdcSSimon Wood led->brightness_get = lg4ff_led_get_brightness; 66222bcefdcSSimon Wood led->brightness_set = lg4ff_led_set_brightness; 66322bcefdcSSimon Wood 66422bcefdcSSimon Wood entry->led[j] = led; 66522bcefdcSSimon Wood error = led_classdev_register(&hid->dev, led); 66622bcefdcSSimon Wood 66722bcefdcSSimon Wood if (error) { 66822bcefdcSSimon Wood hid_err(hid, "failed to register LED %d. Aborting.\n", j); 66922bcefdcSSimon Wood err: 67022bcefdcSSimon Wood /* Deregister LEDs (if any) */ 67122bcefdcSSimon Wood for (j = 0; j < 5; j++) { 67222bcefdcSSimon Wood led = entry->led[j]; 67322bcefdcSSimon Wood entry->led[j] = NULL; 67422bcefdcSSimon Wood if (!led) 67522bcefdcSSimon Wood continue; 67622bcefdcSSimon Wood led_classdev_unregister(led); 67722bcefdcSSimon Wood kfree(led); 67822bcefdcSSimon Wood } 67922bcefdcSSimon Wood goto out; /* Let the driver continue without LEDs */ 68022bcefdcSSimon Wood } 68122bcefdcSSimon Wood } 68222bcefdcSSimon Wood } 68322bcefdcSSimon Wood out: 684c6e6dc87SJiri Kosina #endif 68564013800SSimon Wood hid_info(hid, "Force feedback support for Logitech Gaming Wheels\n"); 68632c88cbcSSimon Wood return 0; 68732c88cbcSSimon Wood } 68832c88cbcSSimon Wood 6892b24a960SMichal Malý 6902b24a960SMichal Malý 69130bb75d7SMichal Malý int lg4ff_deinit(struct hid_device *hid) 69230bb75d7SMichal Malý { 69330bb75d7SMichal Malý struct lg4ff_device_entry *entry; 6943b6b17b7SMichal Malý struct lg_drv_data *drv_data; 6956a2e176bSMichal Malý 6966a2e176bSMichal Malý device_remove_file(&hid->dev, &dev_attr_range); 6976a2e176bSMichal Malý 6983b6b17b7SMichal Malý drv_data = hid_get_drvdata(hid); 6993b6b17b7SMichal Malý if (!drv_data) { 7003b6b17b7SMichal Malý hid_err(hid, "Error while deinitializing device, no private driver data.\n"); 70130bb75d7SMichal Malý return -1; 70230bb75d7SMichal Malý } 7033b6b17b7SMichal Malý entry = drv_data->device_props; 7043b6b17b7SMichal Malý if (!entry) { 7053b6b17b7SMichal Malý hid_err(hid, "Error while deinitializing device, no device properties data.\n"); 7063b6b17b7SMichal Malý return -1; 7073b6b17b7SMichal Malý } 70822bcefdcSSimon Wood 70922bcefdcSSimon Wood #ifdef CONFIG_LEDS_CLASS 71022bcefdcSSimon Wood { 71122bcefdcSSimon Wood int j; 71222bcefdcSSimon Wood struct led_classdev *led; 71322bcefdcSSimon Wood 71422bcefdcSSimon Wood /* Deregister LEDs (if any) */ 71522bcefdcSSimon Wood for (j = 0; j < 5; j++) { 71622bcefdcSSimon Wood 71722bcefdcSSimon Wood led = entry->led[j]; 71822bcefdcSSimon Wood entry->led[j] = NULL; 71922bcefdcSSimon Wood if (!led) 72022bcefdcSSimon Wood continue; 72122bcefdcSSimon Wood led_classdev_unregister(led); 72222bcefdcSSimon Wood kfree(led); 72322bcefdcSSimon Wood } 72422bcefdcSSimon Wood } 72522bcefdcSSimon Wood #endif 72622bcefdcSSimon Wood 7273b6b17b7SMichal Malý /* Deallocate memory */ 7283b6b17b7SMichal Malý kfree(entry); 72930bb75d7SMichal Malý 73030bb75d7SMichal Malý dbg_hid("Device successfully unregistered\n"); 73130bb75d7SMichal Malý return 0; 73230bb75d7SMichal Malý } 733