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 3796440c8aSMichal Malý #define DFP_REV_MAJ 0x11 3896440c8aSMichal Malý #define DFP_REV_MIN 0x06 3996440c8aSMichal Malý #define FFEX_REV_MAJ 0x21 4096440c8aSMichal Malý #define FFEX_REV_MIN 0x00 4196440c8aSMichal Malý #define G25_REV_MAJ 0x12 4296440c8aSMichal Malý #define G25_REV_MIN 0x22 4396440c8aSMichal Malý #define G27_REV_MAJ 0x12 4496440c8aSMichal Malý #define G27_REV_MIN 0x38 4596440c8aSMichal Malý 4630bb75d7SMichal Malý #define to_hid_device(pdev) container_of(pdev, struct hid_device, dev) 4730bb75d7SMichal Malý 4830bb75d7SMichal Malý static void hid_lg4ff_set_range_dfp(struct hid_device *hid, u16 range); 4930bb75d7SMichal Malý static void hid_lg4ff_set_range_g25(struct hid_device *hid, u16 range); 5030bb75d7SMichal Malý static ssize_t lg4ff_range_show(struct device *dev, struct device_attribute *attr, char *buf); 5130bb75d7SMichal Malý static ssize_t lg4ff_range_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count); 5230bb75d7SMichal Malý 5330bb75d7SMichal Malý static DEVICE_ATTR(range, S_IRWXU | S_IRWXG | S_IRWXO, lg4ff_range_show, lg4ff_range_store); 5430bb75d7SMichal Malý 5530bb75d7SMichal Malý struct lg4ff_device_entry { 5630bb75d7SMichal Malý __u16 range; 5730bb75d7SMichal Malý __u16 min_range; 5830bb75d7SMichal Malý __u16 max_range; 5922bcefdcSSimon Wood #ifdef CONFIG_LEDS_CLASS 6022bcefdcSSimon Wood __u8 led_state; 6122bcefdcSSimon Wood struct led_classdev *led[5]; 6222bcefdcSSimon Wood #endif 6330bb75d7SMichal Malý struct list_head list; 6430bb75d7SMichal Malý void (*set_range)(struct hid_device *hid, u16 range); 6530bb75d7SMichal Malý }; 6630bb75d7SMichal Malý 677362cd22SMichal Malý static const signed short lg4ff_wheel_effects[] = { 6832c88cbcSSimon Wood FF_CONSTANT, 6932c88cbcSSimon Wood FF_AUTOCENTER, 7032c88cbcSSimon Wood -1 7132c88cbcSSimon Wood }; 7232c88cbcSSimon Wood 737362cd22SMichal Malý struct lg4ff_wheel { 747362cd22SMichal Malý const __u32 product_id; 757362cd22SMichal Malý const signed short *ff_effects; 767362cd22SMichal Malý const __u16 min_range; 777362cd22SMichal Malý const __u16 max_range; 7830bb75d7SMichal Malý void (*set_range)(struct hid_device *hid, u16 range); 797362cd22SMichal Malý }; 807362cd22SMichal Malý 817362cd22SMichal Malý static const struct lg4ff_wheel lg4ff_devices[] = { 8230bb75d7SMichal Malý {USB_DEVICE_ID_LOGITECH_WHEEL, lg4ff_wheel_effects, 40, 270, NULL}, 8330bb75d7SMichal Malý {USB_DEVICE_ID_LOGITECH_MOMO_WHEEL, lg4ff_wheel_effects, 40, 270, NULL}, 8430bb75d7SMichal Malý {USB_DEVICE_ID_LOGITECH_DFP_WHEEL, lg4ff_wheel_effects, 40, 900, hid_lg4ff_set_range_dfp}, 8530bb75d7SMichal Malý {USB_DEVICE_ID_LOGITECH_G25_WHEEL, lg4ff_wheel_effects, 40, 900, hid_lg4ff_set_range_g25}, 8630bb75d7SMichal Malý {USB_DEVICE_ID_LOGITECH_DFGT_WHEEL, lg4ff_wheel_effects, 40, 900, hid_lg4ff_set_range_g25}, 8730bb75d7SMichal Malý {USB_DEVICE_ID_LOGITECH_G27_WHEEL, lg4ff_wheel_effects, 40, 900, hid_lg4ff_set_range_g25}, 8830bb75d7SMichal Malý {USB_DEVICE_ID_LOGITECH_MOMO_WHEEL2, lg4ff_wheel_effects, 40, 270, NULL}, 8930bb75d7SMichal Malý {USB_DEVICE_ID_LOGITECH_WII_WHEEL, lg4ff_wheel_effects, 40, 270, NULL} 907362cd22SMichal Malý }; 917362cd22SMichal Malý 9296440c8aSMichal Malý struct lg4ff_native_cmd { 9396440c8aSMichal Malý const __u8 cmd_num; /* Number of commands to send */ 9496440c8aSMichal Malý const __u8 cmd[]; 9596440c8aSMichal Malý }; 9696440c8aSMichal Malý 9796440c8aSMichal Malý struct lg4ff_usb_revision { 9896440c8aSMichal Malý const __u16 rev_maj; 9996440c8aSMichal Malý const __u16 rev_min; 10096440c8aSMichal Malý const struct lg4ff_native_cmd *command; 10196440c8aSMichal Malý }; 10296440c8aSMichal Malý 10396440c8aSMichal Malý static const struct lg4ff_native_cmd native_dfp = { 10496440c8aSMichal Malý 1, 10596440c8aSMichal Malý {0xf8, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00} 10696440c8aSMichal Malý }; 10796440c8aSMichal Malý 10896440c8aSMichal Malý static const struct lg4ff_native_cmd native_dfgt = { 10996440c8aSMichal Malý 2, 11096440c8aSMichal Malý {0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, /* 1st command */ 11196440c8aSMichal Malý 0xf8, 0x09, 0x03, 0x01, 0x00, 0x00, 0x00} /* 2nd command */ 11296440c8aSMichal Malý }; 11396440c8aSMichal Malý 11496440c8aSMichal Malý static const struct lg4ff_native_cmd native_g25 = { 11596440c8aSMichal Malý 1, 11696440c8aSMichal Malý {0xf8, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00} 11796440c8aSMichal Malý }; 11896440c8aSMichal Malý 11996440c8aSMichal Malý static const struct lg4ff_native_cmd native_g27 = { 12096440c8aSMichal Malý 2, 12196440c8aSMichal Malý {0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, /* 1st command */ 12296440c8aSMichal Malý 0xf8, 0x09, 0x04, 0x01, 0x00, 0x00, 0x00} /* 2nd command */ 12396440c8aSMichal Malý }; 12496440c8aSMichal Malý 12596440c8aSMichal Malý static const struct lg4ff_usb_revision lg4ff_revs[] = { 12696440c8aSMichal Malý {DFGT_REV_MAJ, DFGT_REV_MIN, &native_dfgt}, /* Driving Force GT */ 12796440c8aSMichal Malý {DFP_REV_MAJ, DFP_REV_MIN, &native_dfp}, /* Driving Force Pro */ 12896440c8aSMichal Malý {G25_REV_MAJ, G25_REV_MIN, &native_g25}, /* G25 */ 12996440c8aSMichal Malý {G27_REV_MAJ, G27_REV_MIN, &native_g27}, /* G27 */ 13096440c8aSMichal Malý }; 13196440c8aSMichal Malý 13230bb75d7SMichal Malý static int hid_lg4ff_play(struct input_dev *dev, void *data, struct ff_effect *effect) 13332c88cbcSSimon Wood { 13432c88cbcSSimon Wood struct hid_device *hid = input_get_drvdata(dev); 13532c88cbcSSimon Wood struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list; 13632c88cbcSSimon Wood struct hid_report *report = list_entry(report_list->next, struct hid_report, list); 13732c88cbcSSimon Wood int x; 13832c88cbcSSimon Wood 13932c88cbcSSimon Wood #define CLAMP(x) if (x < 0) x = 0; if (x > 0xff) x = 0xff 14032c88cbcSSimon Wood 14132c88cbcSSimon Wood switch (effect->type) { 14232c88cbcSSimon Wood case FF_CONSTANT: 14332c88cbcSSimon Wood x = effect->u.ramp.start_level + 0x80; /* 0x80 is no force */ 14432c88cbcSSimon Wood CLAMP(x); 14532c88cbcSSimon Wood report->field[0]->value[0] = 0x11; /* Slot 1 */ 1467362cd22SMichal Malý report->field[0]->value[1] = 0x08; 14732c88cbcSSimon Wood report->field[0]->value[2] = x; 1487362cd22SMichal Malý report->field[0]->value[3] = 0x80; 14932c88cbcSSimon Wood report->field[0]->value[4] = 0x00; 1507362cd22SMichal Malý report->field[0]->value[5] = 0x00; 15132c88cbcSSimon Wood report->field[0]->value[6] = 0x00; 15232c88cbcSSimon Wood 15332c88cbcSSimon Wood usbhid_submit_report(hid, report, USB_DIR_OUT); 15432c88cbcSSimon Wood break; 15532c88cbcSSimon Wood } 15632c88cbcSSimon Wood return 0; 15732c88cbcSSimon Wood } 15832c88cbcSSimon Wood 1596e2de8e0SMichal Malý /* Sends default autocentering command compatible with 1606e2de8e0SMichal Malý * all wheels except Formula Force EX */ 1616e2de8e0SMichal Malý static void hid_lg4ff_set_autocenter_default(struct input_dev *dev, u16 magnitude) 16232c88cbcSSimon Wood { 16332c88cbcSSimon Wood struct hid_device *hid = input_get_drvdata(dev); 16432c88cbcSSimon Wood struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list; 16532c88cbcSSimon Wood struct hid_report *report = list_entry(report_list->next, struct hid_report, list); 16632c88cbcSSimon Wood 1677362cd22SMichal Malý report->field[0]->value[0] = 0xfe; 1687362cd22SMichal Malý report->field[0]->value[1] = 0x0d; 1697362cd22SMichal Malý report->field[0]->value[2] = magnitude >> 13; 1707362cd22SMichal Malý report->field[0]->value[3] = magnitude >> 13; 1717362cd22SMichal Malý report->field[0]->value[4] = magnitude >> 8; 1727362cd22SMichal Malý report->field[0]->value[5] = 0x00; 1737362cd22SMichal Malý report->field[0]->value[6] = 0x00; 17432c88cbcSSimon Wood 17532c88cbcSSimon Wood usbhid_submit_report(hid, report, USB_DIR_OUT); 17632c88cbcSSimon Wood } 17732c88cbcSSimon Wood 1786e2de8e0SMichal Malý /* Sends autocentering command compatible with Formula Force EX */ 1796e2de8e0SMichal Malý static void hid_lg4ff_set_autocenter_ffex(struct input_dev *dev, u16 magnitude) 1806e2de8e0SMichal Malý { 1816e2de8e0SMichal Malý struct hid_device *hid = input_get_drvdata(dev); 1826e2de8e0SMichal Malý struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list; 1836e2de8e0SMichal Malý struct hid_report *report = list_entry(report_list->next, struct hid_report, list); 1846e2de8e0SMichal Malý magnitude = magnitude * 90 / 65535; 1856e2de8e0SMichal Malý 1866e2de8e0SMichal Malý 1876e2de8e0SMichal Malý report->field[0]->value[0] = 0xfe; 1886e2de8e0SMichal Malý report->field[0]->value[1] = 0x03; 1896e2de8e0SMichal Malý report->field[0]->value[2] = magnitude >> 14; 1906e2de8e0SMichal Malý report->field[0]->value[3] = magnitude >> 14; 1916e2de8e0SMichal Malý report->field[0]->value[4] = magnitude; 1926e2de8e0SMichal Malý report->field[0]->value[5] = 0x00; 1936e2de8e0SMichal Malý report->field[0]->value[6] = 0x00; 1946e2de8e0SMichal Malý 1956e2de8e0SMichal Malý usbhid_submit_report(hid, report, USB_DIR_OUT); 1966e2de8e0SMichal Malý } 1976e2de8e0SMichal Malý 19830bb75d7SMichal Malý /* Sends command to set range compatible with G25/G27/Driving Force GT */ 19930bb75d7SMichal Malý static void hid_lg4ff_set_range_g25(struct hid_device *hid, u16 range) 20030bb75d7SMichal Malý { 20130bb75d7SMichal Malý struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list; 20230bb75d7SMichal Malý struct hid_report *report = list_entry(report_list->next, struct hid_report, list); 20330bb75d7SMichal Malý dbg_hid("G25/G27/DFGT: setting range to %u\n", range); 20430bb75d7SMichal Malý 20530bb75d7SMichal Malý report->field[0]->value[0] = 0xf8; 20630bb75d7SMichal Malý report->field[0]->value[1] = 0x81; 20730bb75d7SMichal Malý report->field[0]->value[2] = range & 0x00ff; 20830bb75d7SMichal Malý report->field[0]->value[3] = (range & 0xff00) >> 8; 20930bb75d7SMichal Malý report->field[0]->value[4] = 0x00; 21030bb75d7SMichal Malý report->field[0]->value[5] = 0x00; 21130bb75d7SMichal Malý report->field[0]->value[6] = 0x00; 21230bb75d7SMichal Malý 21330bb75d7SMichal Malý usbhid_submit_report(hid, report, USB_DIR_OUT); 21430bb75d7SMichal Malý } 21530bb75d7SMichal Malý 21630bb75d7SMichal Malý /* Sends commands to set range compatible with Driving Force Pro wheel */ 21730bb75d7SMichal Malý static void hid_lg4ff_set_range_dfp(struct hid_device *hid, __u16 range) 21830bb75d7SMichal Malý { 21930bb75d7SMichal Malý struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list; 22030bb75d7SMichal Malý struct hid_report *report = list_entry(report_list->next, struct hid_report, list); 22130bb75d7SMichal Malý int start_left, start_right, full_range; 22230bb75d7SMichal Malý dbg_hid("Driving Force Pro: setting range to %u\n", range); 22330bb75d7SMichal Malý 22430bb75d7SMichal Malý /* Prepare "coarse" limit command */ 22530bb75d7SMichal Malý report->field[0]->value[0] = 0xf8; 22630bb75d7SMichal Malý report->field[0]->value[1] = 0x00; /* Set later */ 22730bb75d7SMichal Malý report->field[0]->value[2] = 0x00; 22830bb75d7SMichal Malý report->field[0]->value[3] = 0x00; 22930bb75d7SMichal Malý report->field[0]->value[4] = 0x00; 23030bb75d7SMichal Malý report->field[0]->value[5] = 0x00; 23130bb75d7SMichal Malý report->field[0]->value[6] = 0x00; 23230bb75d7SMichal Malý 23330bb75d7SMichal Malý if (range > 200) { 23430bb75d7SMichal Malý report->field[0]->value[1] = 0x03; 23530bb75d7SMichal Malý full_range = 900; 23630bb75d7SMichal Malý } else { 23730bb75d7SMichal Malý report->field[0]->value[1] = 0x02; 23830bb75d7SMichal Malý full_range = 200; 23930bb75d7SMichal Malý } 24030bb75d7SMichal Malý usbhid_submit_report(hid, report, USB_DIR_OUT); 24130bb75d7SMichal Malý 24230bb75d7SMichal Malý /* Prepare "fine" limit command */ 24330bb75d7SMichal Malý report->field[0]->value[0] = 0x81; 24430bb75d7SMichal Malý report->field[0]->value[1] = 0x0b; 24530bb75d7SMichal Malý report->field[0]->value[2] = 0x00; 24630bb75d7SMichal Malý report->field[0]->value[3] = 0x00; 24730bb75d7SMichal Malý report->field[0]->value[4] = 0x00; 24830bb75d7SMichal Malý report->field[0]->value[5] = 0x00; 24930bb75d7SMichal Malý report->field[0]->value[6] = 0x00; 25030bb75d7SMichal Malý 25130bb75d7SMichal Malý if (range == 200 || range == 900) { /* Do not apply any fine limit */ 25230bb75d7SMichal Malý usbhid_submit_report(hid, report, USB_DIR_OUT); 25330bb75d7SMichal Malý return; 25430bb75d7SMichal Malý } 25530bb75d7SMichal Malý 25630bb75d7SMichal Malý /* Construct fine limit command */ 25730bb75d7SMichal Malý start_left = (((full_range - range + 1) * 2047) / full_range); 25830bb75d7SMichal Malý start_right = 0xfff - start_left; 25930bb75d7SMichal Malý 26030bb75d7SMichal Malý report->field[0]->value[2] = start_left >> 4; 26130bb75d7SMichal Malý report->field[0]->value[3] = start_right >> 4; 26230bb75d7SMichal Malý report->field[0]->value[4] = 0xff; 26330bb75d7SMichal Malý report->field[0]->value[5] = (start_right & 0xe) << 4 | (start_left & 0xe); 26430bb75d7SMichal Malý report->field[0]->value[6] = 0xff; 26530bb75d7SMichal Malý 26630bb75d7SMichal Malý usbhid_submit_report(hid, report, USB_DIR_OUT); 26730bb75d7SMichal Malý } 26830bb75d7SMichal Malý 26996440c8aSMichal Malý static void hid_lg4ff_switch_native(struct hid_device *hid, const struct lg4ff_native_cmd *cmd) 27096440c8aSMichal Malý { 27196440c8aSMichal Malý struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list; 27296440c8aSMichal Malý struct hid_report *report = list_entry(report_list->next, struct hid_report, list); 27396440c8aSMichal Malý __u8 i, j; 27496440c8aSMichal Malý 27596440c8aSMichal Malý j = 0; 27696440c8aSMichal Malý while (j < 7*cmd->cmd_num) { 27796440c8aSMichal Malý for (i = 0; i < 7; i++) 27896440c8aSMichal Malý report->field[0]->value[i] = cmd->cmd[j++]; 27996440c8aSMichal Malý 28096440c8aSMichal Malý usbhid_submit_report(hid, report, USB_DIR_OUT); 28196440c8aSMichal Malý } 28296440c8aSMichal Malý } 28332c88cbcSSimon Wood 28430bb75d7SMichal Malý /* Read current range and display it in terminal */ 28530bb75d7SMichal Malý static ssize_t lg4ff_range_show(struct device *dev, struct device_attribute *attr, char *buf) 28630bb75d7SMichal Malý { 28730bb75d7SMichal Malý struct hid_device *hid = to_hid_device(dev); 2883b6b17b7SMichal Malý struct lg4ff_device_entry *entry; 2893b6b17b7SMichal Malý struct lg_drv_data *drv_data; 29030bb75d7SMichal Malý size_t count; 29130bb75d7SMichal Malý 2923b6b17b7SMichal Malý drv_data = hid_get_drvdata(hid); 2933b6b17b7SMichal Malý if (!drv_data) { 2943b6b17b7SMichal Malý hid_err(hid, "Private driver data not found!\n"); 2953b6b17b7SMichal Malý return 0; 29630bb75d7SMichal Malý } 2973b6b17b7SMichal Malý 2983b6b17b7SMichal Malý entry = drv_data->device_props; 2993b6b17b7SMichal Malý if (!entry) { 3003b6b17b7SMichal Malý hid_err(hid, "Device properties not found!\n"); 30130bb75d7SMichal Malý return 0; 30230bb75d7SMichal Malý } 30330bb75d7SMichal Malý 30430bb75d7SMichal Malý count = scnprintf(buf, PAGE_SIZE, "%u\n", entry->range); 30530bb75d7SMichal Malý return count; 30630bb75d7SMichal Malý } 30730bb75d7SMichal Malý 30830bb75d7SMichal Malý /* Set range to user specified value, call appropriate function 30930bb75d7SMichal Malý * according to the type of the wheel */ 31030bb75d7SMichal Malý static ssize_t lg4ff_range_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) 31130bb75d7SMichal Malý { 31230bb75d7SMichal Malý struct hid_device *hid = to_hid_device(dev); 3133b6b17b7SMichal Malý struct lg4ff_device_entry *entry; 3143b6b17b7SMichal Malý struct lg_drv_data *drv_data; 31530bb75d7SMichal Malý __u16 range = simple_strtoul(buf, NULL, 10); 31630bb75d7SMichal Malý 3173b6b17b7SMichal Malý drv_data = hid_get_drvdata(hid); 3183b6b17b7SMichal Malý if (!drv_data) { 3193b6b17b7SMichal Malý hid_err(hid, "Private driver data not found!\n"); 3203b6b17b7SMichal Malý return 0; 32130bb75d7SMichal Malý } 3223b6b17b7SMichal Malý 3233b6b17b7SMichal Malý entry = drv_data->device_props; 3243b6b17b7SMichal Malý if (!entry) { 3253b6b17b7SMichal Malý hid_err(hid, "Device properties not found!\n"); 3263b6b17b7SMichal Malý return 0; 32730bb75d7SMichal Malý } 32830bb75d7SMichal Malý 32930bb75d7SMichal Malý if (range == 0) 33030bb75d7SMichal Malý range = entry->max_range; 33130bb75d7SMichal Malý 33230bb75d7SMichal Malý /* Check if the wheel supports range setting 33330bb75d7SMichal Malý * and that the range is within limits for the wheel */ 33430bb75d7SMichal Malý if (entry->set_range != NULL && range >= entry->min_range && range <= entry->max_range) { 33530bb75d7SMichal Malý entry->set_range(hid, range); 33630bb75d7SMichal Malý entry->range = range; 33730bb75d7SMichal Malý } 33830bb75d7SMichal Malý 33930bb75d7SMichal Malý return count; 34030bb75d7SMichal Malý } 34130bb75d7SMichal Malý 34222bcefdcSSimon Wood #ifdef CONFIG_LEDS_CLASS 34322bcefdcSSimon Wood static void lg4ff_set_leds(struct hid_device *hid, __u8 leds) 34422bcefdcSSimon Wood { 34522bcefdcSSimon Wood struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list; 34622bcefdcSSimon Wood struct hid_report *report = list_entry(report_list->next, struct hid_report, list); 34722bcefdcSSimon Wood 34822bcefdcSSimon Wood report->field[0]->value[0] = 0xf8; 34922bcefdcSSimon Wood report->field[0]->value[1] = 0x12; 35022bcefdcSSimon Wood report->field[0]->value[2] = leds; 35122bcefdcSSimon Wood report->field[0]->value[3] = 0x00; 35222bcefdcSSimon Wood report->field[0]->value[4] = 0x00; 35322bcefdcSSimon Wood report->field[0]->value[5] = 0x00; 35422bcefdcSSimon Wood report->field[0]->value[6] = 0x00; 35522bcefdcSSimon Wood usbhid_submit_report(hid, report, USB_DIR_OUT); 35622bcefdcSSimon Wood } 35722bcefdcSSimon Wood 35822bcefdcSSimon Wood static void lg4ff_led_set_brightness(struct led_classdev *led_cdev, 35922bcefdcSSimon Wood enum led_brightness value) 36022bcefdcSSimon Wood { 36122bcefdcSSimon Wood struct device *dev = led_cdev->dev->parent; 36222bcefdcSSimon Wood struct hid_device *hid = container_of(dev, struct hid_device, dev); 363*4629fd16SAxel Lin struct lg_drv_data *drv_data = hid_get_drvdata(hid); 36422bcefdcSSimon Wood struct lg4ff_device_entry *entry; 36522bcefdcSSimon Wood int i, state = 0; 36622bcefdcSSimon Wood 36722bcefdcSSimon Wood if (!drv_data) { 36822bcefdcSSimon Wood hid_err(hid, "Device data not found."); 36922bcefdcSSimon Wood return; 37022bcefdcSSimon Wood } 37122bcefdcSSimon Wood 37222bcefdcSSimon Wood entry = (struct lg4ff_device_entry *)drv_data->device_props; 37322bcefdcSSimon Wood 37422bcefdcSSimon Wood if (!entry) { 37522bcefdcSSimon Wood hid_err(hid, "Device properties not found."); 37622bcefdcSSimon Wood return; 37722bcefdcSSimon Wood } 37822bcefdcSSimon Wood 37922bcefdcSSimon Wood for (i = 0; i < 5; i++) { 38022bcefdcSSimon Wood if (led_cdev != entry->led[i]) 38122bcefdcSSimon Wood continue; 38222bcefdcSSimon Wood state = (entry->led_state >> i) & 1; 38322bcefdcSSimon Wood if (value == LED_OFF && state) { 38422bcefdcSSimon Wood entry->led_state &= ~(1 << i); 38522bcefdcSSimon Wood lg4ff_set_leds(hid, entry->led_state); 38622bcefdcSSimon Wood } else if (value != LED_OFF && !state) { 38722bcefdcSSimon Wood entry->led_state |= 1 << i; 38822bcefdcSSimon Wood lg4ff_set_leds(hid, entry->led_state); 38922bcefdcSSimon Wood } 39022bcefdcSSimon Wood break; 39122bcefdcSSimon Wood } 39222bcefdcSSimon Wood } 39322bcefdcSSimon Wood 39422bcefdcSSimon Wood static enum led_brightness lg4ff_led_get_brightness(struct led_classdev *led_cdev) 39522bcefdcSSimon Wood { 39622bcefdcSSimon Wood struct device *dev = led_cdev->dev->parent; 39722bcefdcSSimon Wood struct hid_device *hid = container_of(dev, struct hid_device, dev); 398*4629fd16SAxel Lin struct lg_drv_data *drv_data = hid_get_drvdata(hid); 39922bcefdcSSimon Wood struct lg4ff_device_entry *entry; 40022bcefdcSSimon Wood int i, value = 0; 40122bcefdcSSimon Wood 40222bcefdcSSimon Wood if (!drv_data) { 40322bcefdcSSimon Wood hid_err(hid, "Device data not found."); 40422bcefdcSSimon Wood return LED_OFF; 40522bcefdcSSimon Wood } 40622bcefdcSSimon Wood 40722bcefdcSSimon Wood entry = (struct lg4ff_device_entry *)drv_data->device_props; 40822bcefdcSSimon Wood 40922bcefdcSSimon Wood if (!entry) { 41022bcefdcSSimon Wood hid_err(hid, "Device properties not found."); 41122bcefdcSSimon Wood return LED_OFF; 41222bcefdcSSimon Wood } 41322bcefdcSSimon Wood 41422bcefdcSSimon Wood for (i = 0; i < 5; i++) 41522bcefdcSSimon Wood if (led_cdev == entry->led[i]) { 41622bcefdcSSimon Wood value = (entry->led_state >> i) & 1; 41722bcefdcSSimon Wood break; 41822bcefdcSSimon Wood } 41922bcefdcSSimon Wood 42022bcefdcSSimon Wood return value ? LED_FULL : LED_OFF; 42122bcefdcSSimon Wood } 42222bcefdcSSimon Wood #endif 42322bcefdcSSimon Wood 42432c88cbcSSimon Wood int lg4ff_init(struct hid_device *hid) 42532c88cbcSSimon Wood { 42632c88cbcSSimon Wood struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list); 42732c88cbcSSimon Wood struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list; 42832c88cbcSSimon Wood struct input_dev *dev = hidinput->input; 42932c88cbcSSimon Wood struct hid_report *report; 43032c88cbcSSimon Wood struct hid_field *field; 43130bb75d7SMichal Malý struct lg4ff_device_entry *entry; 4323b6b17b7SMichal Malý struct lg_drv_data *drv_data; 43330bb75d7SMichal Malý struct usb_device_descriptor *udesc; 4347362cd22SMichal Malý int error, i, j; 43596440c8aSMichal Malý __u16 bcdDevice, rev_maj, rev_min; 43632c88cbcSSimon Wood 43732c88cbcSSimon Wood /* Find the report to use */ 43832c88cbcSSimon Wood if (list_empty(report_list)) { 4394291ee30SJoe Perches hid_err(hid, "No output report found\n"); 44032c88cbcSSimon Wood return -1; 44132c88cbcSSimon Wood } 44232c88cbcSSimon Wood 44332c88cbcSSimon Wood /* Check that the report looks ok */ 44432c88cbcSSimon Wood report = list_entry(report_list->next, struct hid_report, list); 44532c88cbcSSimon Wood if (!report) { 4464291ee30SJoe Perches hid_err(hid, "NULL output report\n"); 44732c88cbcSSimon Wood return -1; 44832c88cbcSSimon Wood } 44932c88cbcSSimon Wood 45032c88cbcSSimon Wood field = report->field[0]; 45132c88cbcSSimon Wood if (!field) { 4524291ee30SJoe Perches hid_err(hid, "NULL field\n"); 45332c88cbcSSimon Wood return -1; 45432c88cbcSSimon Wood } 45532c88cbcSSimon Wood 4567362cd22SMichal Malý /* Check what wheel has been connected */ 4577362cd22SMichal Malý for (i = 0; i < ARRAY_SIZE(lg4ff_devices); i++) { 4587362cd22SMichal Malý if (hid->product == lg4ff_devices[i].product_id) { 4597362cd22SMichal Malý dbg_hid("Found compatible device, product ID %04X\n", lg4ff_devices[i].product_id); 4607362cd22SMichal Malý break; 4617362cd22SMichal Malý } 4627362cd22SMichal Malý } 4637362cd22SMichal Malý 4647362cd22SMichal Malý if (i == ARRAY_SIZE(lg4ff_devices)) { 4657362cd22SMichal Malý hid_err(hid, "Device is not supported by lg4ff driver. If you think it should be, consider reporting a bug to" 4667362cd22SMichal Malý "LKML, Simon Wood <simon@mungewell.org> or Michal Maly <madcatxster@gmail.com>\n"); 4677362cd22SMichal Malý return -1; 4687362cd22SMichal Malý } 4697362cd22SMichal Malý 47096440c8aSMichal Malý /* Attempt to switch wheel to native mode when applicable */ 47196440c8aSMichal Malý udesc = &(hid_to_usb_dev(hid)->descriptor); 47296440c8aSMichal Malý if (!udesc) { 47396440c8aSMichal Malý hid_err(hid, "NULL USB device descriptor\n"); 47496440c8aSMichal Malý return -1; 47596440c8aSMichal Malý } 47696440c8aSMichal Malý bcdDevice = le16_to_cpu(udesc->bcdDevice); 47796440c8aSMichal Malý rev_maj = bcdDevice >> 8; 47896440c8aSMichal Malý rev_min = bcdDevice & 0xff; 47996440c8aSMichal Malý 48096440c8aSMichal Malý if (lg4ff_devices[i].product_id == USB_DEVICE_ID_LOGITECH_WHEEL) { 48196440c8aSMichal Malý dbg_hid("Generic wheel detected, can it do native?\n"); 48296440c8aSMichal Malý dbg_hid("USB revision: %2x.%02x\n", rev_maj, rev_min); 48396440c8aSMichal Malý 48496440c8aSMichal Malý for (j = 0; j < ARRAY_SIZE(lg4ff_revs); j++) { 48596440c8aSMichal Malý if (lg4ff_revs[j].rev_maj == rev_maj && lg4ff_revs[j].rev_min == rev_min) { 48696440c8aSMichal Malý hid_lg4ff_switch_native(hid, lg4ff_revs[j].command); 48796440c8aSMichal Malý hid_info(hid, "Switched to native mode\n"); 48896440c8aSMichal Malý } 48996440c8aSMichal Malý } 49096440c8aSMichal Malý } 49196440c8aSMichal Malý 4927362cd22SMichal Malý /* Set supported force feedback capabilities */ 4937362cd22SMichal Malý for (j = 0; lg4ff_devices[i].ff_effects[j] >= 0; j++) 4947362cd22SMichal Malý set_bit(lg4ff_devices[i].ff_effects[j], dev->ffbit); 49532c88cbcSSimon Wood 49632c88cbcSSimon Wood error = input_ff_create_memless(dev, NULL, hid_lg4ff_play); 49732c88cbcSSimon Wood 49832c88cbcSSimon Wood if (error) 49932c88cbcSSimon Wood return error; 50032c88cbcSSimon Wood 5016e2de8e0SMichal Malý /* Check if autocentering is available and 5026e2de8e0SMichal Malý * set the centering force to zero by default */ 5036e2de8e0SMichal Malý if (test_bit(FF_AUTOCENTER, dev->ffbit)) { 5046e2de8e0SMichal Malý if(rev_maj == FFEX_REV_MAJ && rev_min == FFEX_REV_MIN) /* Formula Force EX expects different autocentering command */ 5056e2de8e0SMichal Malý dev->ff->set_autocenter = hid_lg4ff_set_autocenter_ffex; 5066e2de8e0SMichal Malý else 5076e2de8e0SMichal Malý dev->ff->set_autocenter = hid_lg4ff_set_autocenter_default; 5086e2de8e0SMichal Malý 5096e2de8e0SMichal Malý dev->ff->set_autocenter(dev, 0); 5106e2de8e0SMichal Malý } 51132c88cbcSSimon Wood 5123b6b17b7SMichal Malý /* Get private driver data */ 5133b6b17b7SMichal Malý drv_data = hid_get_drvdata(hid); 5143b6b17b7SMichal Malý if (!drv_data) { 5153b6b17b7SMichal Malý hid_err(hid, "Cannot add device, private driver data not allocated\n"); 5163b6b17b7SMichal Malý return -1; 51730bb75d7SMichal Malý } 51830bb75d7SMichal Malý 5193b6b17b7SMichal Malý /* Initialize device properties */ 5208383c6bfSThomas Meyer entry = kzalloc(sizeof(struct lg4ff_device_entry), GFP_KERNEL); 52130bb75d7SMichal Malý if (!entry) { 5223b6b17b7SMichal Malý hid_err(hid, "Cannot add device, insufficient memory to allocate device properties.\n"); 52330bb75d7SMichal Malý return -ENOMEM; 52430bb75d7SMichal Malý } 5253b6b17b7SMichal Malý drv_data->device_props = entry; 5263b6b17b7SMichal Malý 52730bb75d7SMichal Malý entry->min_range = lg4ff_devices[i].min_range; 52830bb75d7SMichal Malý entry->max_range = lg4ff_devices[i].max_range; 52930bb75d7SMichal Malý entry->set_range = lg4ff_devices[i].set_range; 53030bb75d7SMichal Malý 53130bb75d7SMichal Malý /* Create sysfs interface */ 53230bb75d7SMichal Malý error = device_create_file(&hid->dev, &dev_attr_range); 53330bb75d7SMichal Malý if (error) 53430bb75d7SMichal Malý return error; 53530bb75d7SMichal Malý dbg_hid("sysfs interface created\n"); 53630bb75d7SMichal Malý 53730bb75d7SMichal Malý /* Set the maximum range to start with */ 53830bb75d7SMichal Malý entry->range = entry->max_range; 53930bb75d7SMichal Malý if (entry->set_range != NULL) 54030bb75d7SMichal Malý entry->set_range(hid, entry->range); 54130bb75d7SMichal Malý 54222bcefdcSSimon Wood #ifdef CONFIG_LEDS_CLASS 54322bcefdcSSimon Wood /* register led subsystem - G27 only */ 54422bcefdcSSimon Wood entry->led_state = 0; 54522bcefdcSSimon Wood for (j = 0; j < 5; j++) 54622bcefdcSSimon Wood entry->led[j] = NULL; 54722bcefdcSSimon Wood 54822bcefdcSSimon Wood if (lg4ff_devices[i].product_id == USB_DEVICE_ID_LOGITECH_G27_WHEEL) { 54922bcefdcSSimon Wood struct led_classdev *led; 55022bcefdcSSimon Wood size_t name_sz; 55122bcefdcSSimon Wood char *name; 55222bcefdcSSimon Wood 55322bcefdcSSimon Wood lg4ff_set_leds(hid, 0); 55422bcefdcSSimon Wood 55522bcefdcSSimon Wood name_sz = strlen(dev_name(&hid->dev)) + 8; 55622bcefdcSSimon Wood 55722bcefdcSSimon Wood for (j = 0; j < 5; j++) { 55822bcefdcSSimon Wood led = kzalloc(sizeof(struct led_classdev)+name_sz, GFP_KERNEL); 55922bcefdcSSimon Wood if (!led) { 56022bcefdcSSimon Wood hid_err(hid, "can't allocate memory for LED %d\n", j); 56122bcefdcSSimon Wood goto err; 56222bcefdcSSimon Wood } 56322bcefdcSSimon Wood 56422bcefdcSSimon Wood name = (void *)(&led[1]); 56522bcefdcSSimon Wood snprintf(name, name_sz, "%s::RPM%d", dev_name(&hid->dev), j+1); 56622bcefdcSSimon Wood led->name = name; 56722bcefdcSSimon Wood led->brightness = 0; 56822bcefdcSSimon Wood led->max_brightness = 1; 56922bcefdcSSimon Wood led->brightness_get = lg4ff_led_get_brightness; 57022bcefdcSSimon Wood led->brightness_set = lg4ff_led_set_brightness; 57122bcefdcSSimon Wood 57222bcefdcSSimon Wood entry->led[j] = led; 57322bcefdcSSimon Wood error = led_classdev_register(&hid->dev, led); 57422bcefdcSSimon Wood 57522bcefdcSSimon Wood if (error) { 57622bcefdcSSimon Wood hid_err(hid, "failed to register LED %d. Aborting.\n", j); 57722bcefdcSSimon Wood err: 57822bcefdcSSimon Wood /* Deregister LEDs (if any) */ 57922bcefdcSSimon Wood for (j = 0; j < 5; j++) { 58022bcefdcSSimon Wood led = entry->led[j]; 58122bcefdcSSimon Wood entry->led[j] = NULL; 58222bcefdcSSimon Wood if (!led) 58322bcefdcSSimon Wood continue; 58422bcefdcSSimon Wood led_classdev_unregister(led); 58522bcefdcSSimon Wood kfree(led); 58622bcefdcSSimon Wood } 58722bcefdcSSimon Wood goto out; /* Let the driver continue without LEDs */ 58822bcefdcSSimon Wood } 58922bcefdcSSimon Wood } 59022bcefdcSSimon Wood } 59122bcefdcSSimon Wood out: 592c6e6dc87SJiri Kosina #endif 59364013800SSimon Wood hid_info(hid, "Force feedback support for Logitech Gaming Wheels\n"); 59432c88cbcSSimon Wood return 0; 59532c88cbcSSimon Wood } 59632c88cbcSSimon Wood 59730bb75d7SMichal Malý int lg4ff_deinit(struct hid_device *hid) 59830bb75d7SMichal Malý { 59930bb75d7SMichal Malý struct lg4ff_device_entry *entry; 6003b6b17b7SMichal Malý struct lg_drv_data *drv_data; 6016a2e176bSMichal Malý 6026a2e176bSMichal Malý device_remove_file(&hid->dev, &dev_attr_range); 6036a2e176bSMichal Malý 6043b6b17b7SMichal Malý drv_data = hid_get_drvdata(hid); 6053b6b17b7SMichal Malý if (!drv_data) { 6063b6b17b7SMichal Malý hid_err(hid, "Error while deinitializing device, no private driver data.\n"); 60730bb75d7SMichal Malý return -1; 60830bb75d7SMichal Malý } 6093b6b17b7SMichal Malý entry = drv_data->device_props; 6103b6b17b7SMichal Malý if (!entry) { 6113b6b17b7SMichal Malý hid_err(hid, "Error while deinitializing device, no device properties data.\n"); 6123b6b17b7SMichal Malý return -1; 6133b6b17b7SMichal Malý } 61422bcefdcSSimon Wood 61522bcefdcSSimon Wood #ifdef CONFIG_LEDS_CLASS 61622bcefdcSSimon Wood { 61722bcefdcSSimon Wood int j; 61822bcefdcSSimon Wood struct led_classdev *led; 61922bcefdcSSimon Wood 62022bcefdcSSimon Wood /* Deregister LEDs (if any) */ 62122bcefdcSSimon Wood for (j = 0; j < 5; j++) { 62222bcefdcSSimon Wood 62322bcefdcSSimon Wood led = entry->led[j]; 62422bcefdcSSimon Wood entry->led[j] = NULL; 62522bcefdcSSimon Wood if (!led) 62622bcefdcSSimon Wood continue; 62722bcefdcSSimon Wood led_classdev_unregister(led); 62822bcefdcSSimon Wood kfree(led); 62922bcefdcSSimon Wood } 63022bcefdcSSimon Wood } 63122bcefdcSSimon Wood #endif 63222bcefdcSSimon Wood 6333b6b17b7SMichal Malý /* Deallocate memory */ 6343b6b17b7SMichal Malý kfree(entry); 63530bb75d7SMichal Malý 63630bb75d7SMichal Malý dbg_hid("Device successfully unregistered\n"); 63730bb75d7SMichal Malý return 0; 63830bb75d7SMichal Malý } 639