Simon Wood | 32c88cb | 2010-09-22 13:19:42 +0200 | [diff] [blame] | 1 | /* |
Simon Wood | 6401380 | 2012-04-21 05:41:16 -0700 | [diff] [blame] | 2 | * Force feedback support for Logitech Gaming Wheels |
Simon Wood | 32c88cb | 2010-09-22 13:19:42 +0200 | [diff] [blame] | 3 | * |
Simon Wood | 6401380 | 2012-04-21 05:41:16 -0700 | [diff] [blame] | 4 | * Including G27, G25, DFP, DFGT, FFEX, Momo, Momo2 & |
| 5 | * Speed Force Wireless (WiiWheel) |
Simon Wood | 32c88cb | 2010-09-22 13:19:42 +0200 | [diff] [blame] | 6 | * |
| 7 | * Copyright (c) 2010 Simon Wood <simon@mungewell.org> |
| 8 | */ |
| 9 | |
| 10 | /* |
| 11 | * This program is free software; you can redistribute it and/or modify |
| 12 | * it under the terms of the GNU General Public License as published by |
| 13 | * the Free Software Foundation; either version 2 of the License, or |
| 14 | * (at your option) any later version. |
| 15 | * |
| 16 | * This program is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | * GNU General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License |
| 22 | * along with this program; if not, write to the Free Software |
| 23 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 24 | */ |
| 25 | |
| 26 | |
| 27 | #include <linux/input.h> |
| 28 | #include <linux/usb.h> |
| 29 | #include <linux/hid.h> |
| 30 | |
| 31 | #include "usbhid/usbhid.h" |
| 32 | #include "hid-lg.h" |
Michal Malý | 7362cd2 | 2011-08-04 16:16:09 +0200 | [diff] [blame] | 33 | #include "hid-ids.h" |
Simon Wood | 32c88cb | 2010-09-22 13:19:42 +0200 | [diff] [blame] | 34 | |
Michal Malý | 96440c8 | 2011-08-04 16:18:11 +0200 | [diff] [blame] | 35 | #define DFGT_REV_MAJ 0x13 |
| 36 | #define DFGT_REV_MIN 0x22 |
Simon Wood | d991938 | 2013-04-26 19:29:54 -0600 | [diff] [blame] | 37 | #define DFGT2_REV_MIN 0x26 |
Michal Malý | 96440c8 | 2011-08-04 16:18:11 +0200 | [diff] [blame] | 38 | #define DFP_REV_MAJ 0x11 |
| 39 | #define DFP_REV_MIN 0x06 |
| 40 | #define FFEX_REV_MAJ 0x21 |
| 41 | #define FFEX_REV_MIN 0x00 |
| 42 | #define G25_REV_MAJ 0x12 |
| 43 | #define G25_REV_MIN 0x22 |
| 44 | #define G27_REV_MAJ 0x12 |
| 45 | #define G27_REV_MIN 0x38 |
| 46 | |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 47 | #define to_hid_device(pdev) container_of(pdev, struct hid_device, dev) |
| 48 | |
| 49 | static void hid_lg4ff_set_range_dfp(struct hid_device *hid, u16 range); |
| 50 | static void hid_lg4ff_set_range_g25(struct hid_device *hid, u16 range); |
| 51 | static ssize_t lg4ff_range_show(struct device *dev, struct device_attribute *attr, char *buf); |
| 52 | static ssize_t lg4ff_range_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count); |
| 53 | |
| 54 | static DEVICE_ATTR(range, S_IRWXU | S_IRWXG | S_IRWXO, lg4ff_range_show, lg4ff_range_store); |
| 55 | |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 56 | struct lg4ff_device_entry { |
Michal Malý | 2b24a96 | 2012-09-23 22:41:08 +0200 | [diff] [blame] | 57 | __u32 product_id; |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 58 | __u16 range; |
| 59 | __u16 min_range; |
| 60 | __u16 max_range; |
Simon Wood | 22bcefd | 2012-04-21 05:41:15 -0700 | [diff] [blame] | 61 | #ifdef CONFIG_LEDS_CLASS |
| 62 | __u8 led_state; |
| 63 | struct led_classdev *led[5]; |
| 64 | #endif |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 65 | struct list_head list; |
| 66 | void (*set_range)(struct hid_device *hid, u16 range); |
| 67 | }; |
| 68 | |
Michal Malý | 7362cd2 | 2011-08-04 16:16:09 +0200 | [diff] [blame] | 69 | static const signed short lg4ff_wheel_effects[] = { |
Simon Wood | 32c88cb | 2010-09-22 13:19:42 +0200 | [diff] [blame] | 70 | FF_CONSTANT, |
| 71 | FF_AUTOCENTER, |
| 72 | -1 |
| 73 | }; |
| 74 | |
Michal Malý | 7362cd2 | 2011-08-04 16:16:09 +0200 | [diff] [blame] | 75 | struct lg4ff_wheel { |
| 76 | const __u32 product_id; |
| 77 | const signed short *ff_effects; |
| 78 | const __u16 min_range; |
| 79 | const __u16 max_range; |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 80 | void (*set_range)(struct hid_device *hid, u16 range); |
Michal Malý | 7362cd2 | 2011-08-04 16:16:09 +0200 | [diff] [blame] | 81 | }; |
| 82 | |
| 83 | static const struct lg4ff_wheel lg4ff_devices[] = { |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 84 | {USB_DEVICE_ID_LOGITECH_WHEEL, lg4ff_wheel_effects, 40, 270, NULL}, |
| 85 | {USB_DEVICE_ID_LOGITECH_MOMO_WHEEL, lg4ff_wheel_effects, 40, 270, NULL}, |
| 86 | {USB_DEVICE_ID_LOGITECH_DFP_WHEEL, lg4ff_wheel_effects, 40, 900, hid_lg4ff_set_range_dfp}, |
| 87 | {USB_DEVICE_ID_LOGITECH_G25_WHEEL, lg4ff_wheel_effects, 40, 900, hid_lg4ff_set_range_g25}, |
| 88 | {USB_DEVICE_ID_LOGITECH_DFGT_WHEEL, lg4ff_wheel_effects, 40, 900, hid_lg4ff_set_range_g25}, |
| 89 | {USB_DEVICE_ID_LOGITECH_G27_WHEEL, lg4ff_wheel_effects, 40, 900, hid_lg4ff_set_range_g25}, |
| 90 | {USB_DEVICE_ID_LOGITECH_MOMO_WHEEL2, lg4ff_wheel_effects, 40, 270, NULL}, |
| 91 | {USB_DEVICE_ID_LOGITECH_WII_WHEEL, lg4ff_wheel_effects, 40, 270, NULL} |
Michal Malý | 7362cd2 | 2011-08-04 16:16:09 +0200 | [diff] [blame] | 92 | }; |
| 93 | |
Michal Malý | 96440c8 | 2011-08-04 16:18:11 +0200 | [diff] [blame] | 94 | struct lg4ff_native_cmd { |
| 95 | const __u8 cmd_num; /* Number of commands to send */ |
| 96 | const __u8 cmd[]; |
| 97 | }; |
| 98 | |
| 99 | struct lg4ff_usb_revision { |
| 100 | const __u16 rev_maj; |
| 101 | const __u16 rev_min; |
| 102 | const struct lg4ff_native_cmd *command; |
| 103 | }; |
| 104 | |
| 105 | static const struct lg4ff_native_cmd native_dfp = { |
| 106 | 1, |
| 107 | {0xf8, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00} |
| 108 | }; |
| 109 | |
| 110 | static const struct lg4ff_native_cmd native_dfgt = { |
| 111 | 2, |
| 112 | {0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, /* 1st command */ |
| 113 | 0xf8, 0x09, 0x03, 0x01, 0x00, 0x00, 0x00} /* 2nd command */ |
| 114 | }; |
| 115 | |
| 116 | static const struct lg4ff_native_cmd native_g25 = { |
| 117 | 1, |
| 118 | {0xf8, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00} |
| 119 | }; |
| 120 | |
| 121 | static const struct lg4ff_native_cmd native_g27 = { |
| 122 | 2, |
| 123 | {0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, /* 1st command */ |
| 124 | 0xf8, 0x09, 0x04, 0x01, 0x00, 0x00, 0x00} /* 2nd command */ |
| 125 | }; |
| 126 | |
| 127 | static const struct lg4ff_usb_revision lg4ff_revs[] = { |
| 128 | {DFGT_REV_MAJ, DFGT_REV_MIN, &native_dfgt}, /* Driving Force GT */ |
Simon Wood | d991938 | 2013-04-26 19:29:54 -0600 | [diff] [blame] | 129 | {DFGT_REV_MAJ, DFGT2_REV_MIN, &native_dfgt}, /* Driving Force GT v2 */ |
Michal Malý | 96440c8 | 2011-08-04 16:18:11 +0200 | [diff] [blame] | 130 | {DFP_REV_MAJ, DFP_REV_MIN, &native_dfp}, /* Driving Force Pro */ |
| 131 | {G25_REV_MAJ, G25_REV_MIN, &native_g25}, /* G25 */ |
| 132 | {G27_REV_MAJ, G27_REV_MIN, &native_g27}, /* G27 */ |
| 133 | }; |
| 134 | |
Michal Malý | 2b24a96 | 2012-09-23 22:41:08 +0200 | [diff] [blame] | 135 | /* Recalculates X axis value accordingly to currently selected range */ |
| 136 | static __s32 lg4ff_adjust_dfp_x_axis(__s32 value, __u16 range) |
| 137 | { |
| 138 | __u16 max_range; |
| 139 | __s32 new_value; |
| 140 | |
| 141 | if (range == 900) |
| 142 | return value; |
| 143 | else if (range == 200) |
| 144 | return value; |
| 145 | else if (range < 200) |
| 146 | max_range = 200; |
| 147 | else |
| 148 | max_range = 900; |
| 149 | |
| 150 | new_value = 8192 + mult_frac(value - 8192, max_range, range); |
| 151 | if (new_value < 0) |
| 152 | return 0; |
| 153 | else if (new_value > 16383) |
| 154 | return 16383; |
| 155 | else |
| 156 | return new_value; |
| 157 | } |
| 158 | |
| 159 | int lg4ff_adjust_input_event(struct hid_device *hid, struct hid_field *field, |
| 160 | struct hid_usage *usage, __s32 value, struct lg_drv_data *drv_data) |
| 161 | { |
| 162 | struct lg4ff_device_entry *entry = drv_data->device_props; |
| 163 | __s32 new_value = 0; |
| 164 | |
| 165 | if (!entry) { |
| 166 | hid_err(hid, "Device properties not found"); |
| 167 | return 0; |
| 168 | } |
| 169 | |
| 170 | switch (entry->product_id) { |
| 171 | case USB_DEVICE_ID_LOGITECH_DFP_WHEEL: |
| 172 | switch (usage->code) { |
| 173 | case ABS_X: |
| 174 | new_value = lg4ff_adjust_dfp_x_axis(value, entry->range); |
| 175 | input_event(field->hidinput->input, usage->type, usage->code, new_value); |
| 176 | return 1; |
| 177 | default: |
| 178 | return 0; |
| 179 | } |
| 180 | default: |
| 181 | return 0; |
| 182 | } |
| 183 | } |
| 184 | |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 185 | static int hid_lg4ff_play(struct input_dev *dev, void *data, struct ff_effect *effect) |
Simon Wood | 32c88cb | 2010-09-22 13:19:42 +0200 | [diff] [blame] | 186 | { |
| 187 | struct hid_device *hid = input_get_drvdata(dev); |
| 188 | struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list; |
| 189 | struct hid_report *report = list_entry(report_list->next, struct hid_report, list); |
Michal Malý | 74479ba | 2012-09-24 01:09:30 +0200 | [diff] [blame] | 190 | __s32 *value = report->field[0]->value; |
Simon Wood | 32c88cb | 2010-09-22 13:19:42 +0200 | [diff] [blame] | 191 | int x; |
| 192 | |
Michal Malý | a80fe5d | 2012-09-24 01:13:17 +0200 | [diff] [blame] | 193 | #define CLAMP(x) do { if (x < 0) x = 0; else if (x > 0xff) x = 0xff; } while (0) |
Simon Wood | 32c88cb | 2010-09-22 13:19:42 +0200 | [diff] [blame] | 194 | |
| 195 | switch (effect->type) { |
| 196 | case FF_CONSTANT: |
| 197 | x = effect->u.ramp.start_level + 0x80; /* 0x80 is no force */ |
| 198 | CLAMP(x); |
Michal Malý | 74479ba | 2012-09-24 01:09:30 +0200 | [diff] [blame] | 199 | value[0] = 0x11; /* Slot 1 */ |
| 200 | value[1] = 0x08; |
| 201 | value[2] = x; |
| 202 | value[3] = 0x80; |
| 203 | value[4] = 0x00; |
| 204 | value[5] = 0x00; |
| 205 | value[6] = 0x00; |
Simon Wood | 32c88cb | 2010-09-22 13:19:42 +0200 | [diff] [blame] | 206 | |
Benjamin Tissoires | d8814272 | 2013-02-25 11:31:46 +0100 | [diff] [blame] | 207 | hid_hw_request(hid, report, HID_REQ_SET_REPORT); |
Simon Wood | 32c88cb | 2010-09-22 13:19:42 +0200 | [diff] [blame] | 208 | break; |
| 209 | } |
| 210 | return 0; |
| 211 | } |
| 212 | |
Michal Malý | 6e2de8e | 2011-08-04 16:22:07 +0200 | [diff] [blame] | 213 | /* Sends default autocentering command compatible with |
| 214 | * all wheels except Formula Force EX */ |
| 215 | static void hid_lg4ff_set_autocenter_default(struct input_dev *dev, u16 magnitude) |
Simon Wood | 32c88cb | 2010-09-22 13:19:42 +0200 | [diff] [blame] | 216 | { |
| 217 | struct hid_device *hid = input_get_drvdata(dev); |
| 218 | struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list; |
| 219 | struct hid_report *report = list_entry(report_list->next, struct hid_report, list); |
Michal Malý | 74479ba | 2012-09-24 01:09:30 +0200 | [diff] [blame] | 220 | __s32 *value = report->field[0]->value; |
Simon Wood | 32c88cb | 2010-09-22 13:19:42 +0200 | [diff] [blame] | 221 | |
Michal Malý | 74479ba | 2012-09-24 01:09:30 +0200 | [diff] [blame] | 222 | value[0] = 0xfe; |
| 223 | value[1] = 0x0d; |
| 224 | value[2] = magnitude >> 13; |
| 225 | value[3] = magnitude >> 13; |
| 226 | value[4] = magnitude >> 8; |
| 227 | value[5] = 0x00; |
| 228 | value[6] = 0x00; |
Simon Wood | 32c88cb | 2010-09-22 13:19:42 +0200 | [diff] [blame] | 229 | |
Benjamin Tissoires | d8814272 | 2013-02-25 11:31:46 +0100 | [diff] [blame] | 230 | hid_hw_request(hid, report, HID_REQ_SET_REPORT); |
Simon Wood | 32c88cb | 2010-09-22 13:19:42 +0200 | [diff] [blame] | 231 | } |
| 232 | |
Michal Malý | 6e2de8e | 2011-08-04 16:22:07 +0200 | [diff] [blame] | 233 | /* Sends autocentering command compatible with Formula Force EX */ |
| 234 | static void hid_lg4ff_set_autocenter_ffex(struct input_dev *dev, u16 magnitude) |
| 235 | { |
| 236 | struct hid_device *hid = input_get_drvdata(dev); |
| 237 | struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list; |
| 238 | struct hid_report *report = list_entry(report_list->next, struct hid_report, list); |
Michal Malý | 74479ba | 2012-09-24 01:09:30 +0200 | [diff] [blame] | 239 | __s32 *value = report->field[0]->value; |
Michal Malý | 6e2de8e | 2011-08-04 16:22:07 +0200 | [diff] [blame] | 240 | magnitude = magnitude * 90 / 65535; |
Michal Malý | 6e2de8e | 2011-08-04 16:22:07 +0200 | [diff] [blame] | 241 | |
Michal Malý | 74479ba | 2012-09-24 01:09:30 +0200 | [diff] [blame] | 242 | value[0] = 0xfe; |
| 243 | value[1] = 0x03; |
| 244 | value[2] = magnitude >> 14; |
| 245 | value[3] = magnitude >> 14; |
| 246 | value[4] = magnitude; |
| 247 | value[5] = 0x00; |
| 248 | value[6] = 0x00; |
Michal Malý | 6e2de8e | 2011-08-04 16:22:07 +0200 | [diff] [blame] | 249 | |
Benjamin Tissoires | d8814272 | 2013-02-25 11:31:46 +0100 | [diff] [blame] | 250 | hid_hw_request(hid, report, HID_REQ_SET_REPORT); |
Michal Malý | 6e2de8e | 2011-08-04 16:22:07 +0200 | [diff] [blame] | 251 | } |
| 252 | |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 253 | /* Sends command to set range compatible with G25/G27/Driving Force GT */ |
| 254 | static void hid_lg4ff_set_range_g25(struct hid_device *hid, u16 range) |
| 255 | { |
| 256 | struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list; |
| 257 | struct hid_report *report = list_entry(report_list->next, struct hid_report, list); |
Michal Malý | 74479ba | 2012-09-24 01:09:30 +0200 | [diff] [blame] | 258 | __s32 *value = report->field[0]->value; |
| 259 | |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 260 | dbg_hid("G25/G27/DFGT: setting range to %u\n", range); |
| 261 | |
Michal Malý | 74479ba | 2012-09-24 01:09:30 +0200 | [diff] [blame] | 262 | value[0] = 0xf8; |
| 263 | value[1] = 0x81; |
| 264 | value[2] = range & 0x00ff; |
| 265 | value[3] = (range & 0xff00) >> 8; |
| 266 | value[4] = 0x00; |
| 267 | value[5] = 0x00; |
| 268 | value[6] = 0x00; |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 269 | |
Benjamin Tissoires | d8814272 | 2013-02-25 11:31:46 +0100 | [diff] [blame] | 270 | hid_hw_request(hid, report, HID_REQ_SET_REPORT); |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | /* Sends commands to set range compatible with Driving Force Pro wheel */ |
| 274 | static void hid_lg4ff_set_range_dfp(struct hid_device *hid, __u16 range) |
| 275 | { |
| 276 | struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list; |
| 277 | struct hid_report *report = list_entry(report_list->next, struct hid_report, list); |
| 278 | int start_left, start_right, full_range; |
Michal Malý | 74479ba | 2012-09-24 01:09:30 +0200 | [diff] [blame] | 279 | __s32 *value = report->field[0]->value; |
| 280 | |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 281 | dbg_hid("Driving Force Pro: setting range to %u\n", range); |
| 282 | |
| 283 | /* Prepare "coarse" limit command */ |
Michal Malý | 74479ba | 2012-09-24 01:09:30 +0200 | [diff] [blame] | 284 | value[0] = 0xf8; |
Michal Malý | a80fe5d | 2012-09-24 01:13:17 +0200 | [diff] [blame] | 285 | value[1] = 0x00; /* Set later */ |
Michal Malý | 74479ba | 2012-09-24 01:09:30 +0200 | [diff] [blame] | 286 | value[2] = 0x00; |
| 287 | value[3] = 0x00; |
| 288 | value[4] = 0x00; |
| 289 | value[5] = 0x00; |
| 290 | value[6] = 0x00; |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 291 | |
| 292 | if (range > 200) { |
| 293 | report->field[0]->value[1] = 0x03; |
| 294 | full_range = 900; |
| 295 | } else { |
| 296 | report->field[0]->value[1] = 0x02; |
| 297 | full_range = 200; |
| 298 | } |
Benjamin Tissoires | d8814272 | 2013-02-25 11:31:46 +0100 | [diff] [blame] | 299 | hid_hw_request(hid, report, HID_REQ_SET_REPORT); |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 300 | |
| 301 | /* Prepare "fine" limit command */ |
Michal Malý | 74479ba | 2012-09-24 01:09:30 +0200 | [diff] [blame] | 302 | value[0] = 0x81; |
| 303 | value[1] = 0x0b; |
| 304 | value[2] = 0x00; |
| 305 | value[3] = 0x00; |
| 306 | value[4] = 0x00; |
| 307 | value[5] = 0x00; |
| 308 | value[6] = 0x00; |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 309 | |
| 310 | if (range == 200 || range == 900) { /* Do not apply any fine limit */ |
Benjamin Tissoires | d8814272 | 2013-02-25 11:31:46 +0100 | [diff] [blame] | 311 | hid_hw_request(hid, report, HID_REQ_SET_REPORT); |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 312 | return; |
| 313 | } |
| 314 | |
| 315 | /* Construct fine limit command */ |
| 316 | start_left = (((full_range - range + 1) * 2047) / full_range); |
| 317 | start_right = 0xfff - start_left; |
| 318 | |
Michal Malý | 74479ba | 2012-09-24 01:09:30 +0200 | [diff] [blame] | 319 | value[2] = start_left >> 4; |
| 320 | value[3] = start_right >> 4; |
| 321 | value[4] = 0xff; |
| 322 | value[5] = (start_right & 0xe) << 4 | (start_left & 0xe); |
| 323 | value[6] = 0xff; |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 324 | |
Benjamin Tissoires | d8814272 | 2013-02-25 11:31:46 +0100 | [diff] [blame] | 325 | hid_hw_request(hid, report, HID_REQ_SET_REPORT); |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 326 | } |
| 327 | |
Michal Malý | 96440c8 | 2011-08-04 16:18:11 +0200 | [diff] [blame] | 328 | static void hid_lg4ff_switch_native(struct hid_device *hid, const struct lg4ff_native_cmd *cmd) |
| 329 | { |
| 330 | struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list; |
| 331 | struct hid_report *report = list_entry(report_list->next, struct hid_report, list); |
| 332 | __u8 i, j; |
| 333 | |
| 334 | j = 0; |
| 335 | while (j < 7*cmd->cmd_num) { |
| 336 | for (i = 0; i < 7; i++) |
| 337 | report->field[0]->value[i] = cmd->cmd[j++]; |
| 338 | |
Benjamin Tissoires | d8814272 | 2013-02-25 11:31:46 +0100 | [diff] [blame] | 339 | hid_hw_request(hid, report, HID_REQ_SET_REPORT); |
Michal Malý | 96440c8 | 2011-08-04 16:18:11 +0200 | [diff] [blame] | 340 | } |
| 341 | } |
Simon Wood | 32c88cb | 2010-09-22 13:19:42 +0200 | [diff] [blame] | 342 | |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 343 | /* Read current range and display it in terminal */ |
| 344 | static ssize_t lg4ff_range_show(struct device *dev, struct device_attribute *attr, char *buf) |
| 345 | { |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 346 | struct hid_device *hid = to_hid_device(dev); |
Michal Malý | 3b6b17b | 2012-04-09 09:08:49 +0200 | [diff] [blame] | 347 | struct lg4ff_device_entry *entry; |
| 348 | struct lg_drv_data *drv_data; |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 349 | size_t count; |
| 350 | |
Michal Malý | 3b6b17b | 2012-04-09 09:08:49 +0200 | [diff] [blame] | 351 | drv_data = hid_get_drvdata(hid); |
| 352 | if (!drv_data) { |
| 353 | hid_err(hid, "Private driver data not found!\n"); |
| 354 | return 0; |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 355 | } |
Michal Malý | 3b6b17b | 2012-04-09 09:08:49 +0200 | [diff] [blame] | 356 | |
| 357 | entry = drv_data->device_props; |
| 358 | if (!entry) { |
| 359 | hid_err(hid, "Device properties not found!\n"); |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 360 | return 0; |
| 361 | } |
| 362 | |
| 363 | count = scnprintf(buf, PAGE_SIZE, "%u\n", entry->range); |
| 364 | return count; |
| 365 | } |
| 366 | |
| 367 | /* Set range to user specified value, call appropriate function |
| 368 | * according to the type of the wheel */ |
| 369 | static ssize_t lg4ff_range_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) |
| 370 | { |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 371 | struct hid_device *hid = to_hid_device(dev); |
Michal Malý | 3b6b17b | 2012-04-09 09:08:49 +0200 | [diff] [blame] | 372 | struct lg4ff_device_entry *entry; |
| 373 | struct lg_drv_data *drv_data; |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 374 | __u16 range = simple_strtoul(buf, NULL, 10); |
| 375 | |
Michal Malý | 3b6b17b | 2012-04-09 09:08:49 +0200 | [diff] [blame] | 376 | drv_data = hid_get_drvdata(hid); |
| 377 | if (!drv_data) { |
| 378 | hid_err(hid, "Private driver data not found!\n"); |
| 379 | return 0; |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 380 | } |
Michal Malý | 3b6b17b | 2012-04-09 09:08:49 +0200 | [diff] [blame] | 381 | |
| 382 | entry = drv_data->device_props; |
| 383 | if (!entry) { |
| 384 | hid_err(hid, "Device properties not found!\n"); |
| 385 | return 0; |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | if (range == 0) |
| 389 | range = entry->max_range; |
| 390 | |
| 391 | /* Check if the wheel supports range setting |
| 392 | * and that the range is within limits for the wheel */ |
| 393 | if (entry->set_range != NULL && range >= entry->min_range && range <= entry->max_range) { |
| 394 | entry->set_range(hid, range); |
| 395 | entry->range = range; |
| 396 | } |
| 397 | |
| 398 | return count; |
| 399 | } |
| 400 | |
Simon Wood | 22bcefd | 2012-04-21 05:41:15 -0700 | [diff] [blame] | 401 | #ifdef CONFIG_LEDS_CLASS |
| 402 | static void lg4ff_set_leds(struct hid_device *hid, __u8 leds) |
| 403 | { |
| 404 | struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list; |
| 405 | struct hid_report *report = list_entry(report_list->next, struct hid_report, list); |
Michal Malý | 74479ba | 2012-09-24 01:09:30 +0200 | [diff] [blame] | 406 | __s32 *value = report->field[0]->value; |
Simon Wood | 22bcefd | 2012-04-21 05:41:15 -0700 | [diff] [blame] | 407 | |
Michal Malý | 74479ba | 2012-09-24 01:09:30 +0200 | [diff] [blame] | 408 | value[0] = 0xf8; |
| 409 | value[1] = 0x12; |
| 410 | value[2] = leds; |
| 411 | value[3] = 0x00; |
| 412 | value[4] = 0x00; |
| 413 | value[5] = 0x00; |
| 414 | value[6] = 0x00; |
Benjamin Tissoires | d8814272 | 2013-02-25 11:31:46 +0100 | [diff] [blame] | 415 | hid_hw_request(hid, report, HID_REQ_SET_REPORT); |
Simon Wood | 22bcefd | 2012-04-21 05:41:15 -0700 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | static void lg4ff_led_set_brightness(struct led_classdev *led_cdev, |
| 419 | enum led_brightness value) |
| 420 | { |
| 421 | struct device *dev = led_cdev->dev->parent; |
| 422 | struct hid_device *hid = container_of(dev, struct hid_device, dev); |
Axel Lin | 4629fd1 | 2012-09-13 14:09:33 +0800 | [diff] [blame] | 423 | struct lg_drv_data *drv_data = hid_get_drvdata(hid); |
Simon Wood | 22bcefd | 2012-04-21 05:41:15 -0700 | [diff] [blame] | 424 | struct lg4ff_device_entry *entry; |
| 425 | int i, state = 0; |
| 426 | |
| 427 | if (!drv_data) { |
| 428 | hid_err(hid, "Device data not found."); |
| 429 | return; |
| 430 | } |
| 431 | |
| 432 | entry = (struct lg4ff_device_entry *)drv_data->device_props; |
| 433 | |
| 434 | if (!entry) { |
| 435 | hid_err(hid, "Device properties not found."); |
| 436 | return; |
| 437 | } |
| 438 | |
| 439 | for (i = 0; i < 5; i++) { |
| 440 | if (led_cdev != entry->led[i]) |
| 441 | continue; |
| 442 | state = (entry->led_state >> i) & 1; |
| 443 | if (value == LED_OFF && state) { |
| 444 | entry->led_state &= ~(1 << i); |
| 445 | lg4ff_set_leds(hid, entry->led_state); |
| 446 | } else if (value != LED_OFF && !state) { |
| 447 | entry->led_state |= 1 << i; |
| 448 | lg4ff_set_leds(hid, entry->led_state); |
| 449 | } |
| 450 | break; |
| 451 | } |
| 452 | } |
| 453 | |
| 454 | static enum led_brightness lg4ff_led_get_brightness(struct led_classdev *led_cdev) |
| 455 | { |
| 456 | struct device *dev = led_cdev->dev->parent; |
| 457 | struct hid_device *hid = container_of(dev, struct hid_device, dev); |
Axel Lin | 4629fd1 | 2012-09-13 14:09:33 +0800 | [diff] [blame] | 458 | struct lg_drv_data *drv_data = hid_get_drvdata(hid); |
Simon Wood | 22bcefd | 2012-04-21 05:41:15 -0700 | [diff] [blame] | 459 | struct lg4ff_device_entry *entry; |
| 460 | int i, value = 0; |
| 461 | |
| 462 | if (!drv_data) { |
| 463 | hid_err(hid, "Device data not found."); |
| 464 | return LED_OFF; |
| 465 | } |
| 466 | |
| 467 | entry = (struct lg4ff_device_entry *)drv_data->device_props; |
| 468 | |
| 469 | if (!entry) { |
| 470 | hid_err(hid, "Device properties not found."); |
| 471 | return LED_OFF; |
| 472 | } |
| 473 | |
| 474 | for (i = 0; i < 5; i++) |
| 475 | if (led_cdev == entry->led[i]) { |
| 476 | value = (entry->led_state >> i) & 1; |
| 477 | break; |
| 478 | } |
| 479 | |
| 480 | return value ? LED_FULL : LED_OFF; |
| 481 | } |
| 482 | #endif |
| 483 | |
Simon Wood | 32c88cb | 2010-09-22 13:19:42 +0200 | [diff] [blame] | 484 | int lg4ff_init(struct hid_device *hid) |
| 485 | { |
| 486 | struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list); |
| 487 | struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list; |
| 488 | struct input_dev *dev = hidinput->input; |
| 489 | struct hid_report *report; |
| 490 | struct hid_field *field; |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 491 | struct lg4ff_device_entry *entry; |
Michal Malý | 3b6b17b | 2012-04-09 09:08:49 +0200 | [diff] [blame] | 492 | struct lg_drv_data *drv_data; |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 493 | struct usb_device_descriptor *udesc; |
Michal Malý | 7362cd2 | 2011-08-04 16:16:09 +0200 | [diff] [blame] | 494 | int error, i, j; |
Michal Malý | 96440c8 | 2011-08-04 16:18:11 +0200 | [diff] [blame] | 495 | __u16 bcdDevice, rev_maj, rev_min; |
Simon Wood | 32c88cb | 2010-09-22 13:19:42 +0200 | [diff] [blame] | 496 | |
| 497 | /* Find the report to use */ |
| 498 | if (list_empty(report_list)) { |
Joe Perches | 4291ee3 | 2010-12-09 19:29:03 -0800 | [diff] [blame] | 499 | hid_err(hid, "No output report found\n"); |
Simon Wood | 32c88cb | 2010-09-22 13:19:42 +0200 | [diff] [blame] | 500 | return -1; |
| 501 | } |
| 502 | |
| 503 | /* Check that the report looks ok */ |
| 504 | report = list_entry(report_list->next, struct hid_report, list); |
| 505 | if (!report) { |
Joe Perches | 4291ee3 | 2010-12-09 19:29:03 -0800 | [diff] [blame] | 506 | hid_err(hid, "NULL output report\n"); |
Simon Wood | 32c88cb | 2010-09-22 13:19:42 +0200 | [diff] [blame] | 507 | return -1; |
| 508 | } |
| 509 | |
| 510 | field = report->field[0]; |
| 511 | if (!field) { |
Joe Perches | 4291ee3 | 2010-12-09 19:29:03 -0800 | [diff] [blame] | 512 | hid_err(hid, "NULL field\n"); |
Simon Wood | 32c88cb | 2010-09-22 13:19:42 +0200 | [diff] [blame] | 513 | return -1; |
| 514 | } |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 515 | |
Michal Malý | 7362cd2 | 2011-08-04 16:16:09 +0200 | [diff] [blame] | 516 | /* Check what wheel has been connected */ |
| 517 | for (i = 0; i < ARRAY_SIZE(lg4ff_devices); i++) { |
| 518 | if (hid->product == lg4ff_devices[i].product_id) { |
| 519 | dbg_hid("Found compatible device, product ID %04X\n", lg4ff_devices[i].product_id); |
| 520 | break; |
| 521 | } |
| 522 | } |
Simon Wood | 32c88cb | 2010-09-22 13:19:42 +0200 | [diff] [blame] | 523 | |
Michal Malý | 7362cd2 | 2011-08-04 16:16:09 +0200 | [diff] [blame] | 524 | if (i == ARRAY_SIZE(lg4ff_devices)) { |
| 525 | hid_err(hid, "Device is not supported by lg4ff driver. If you think it should be, consider reporting a bug to" |
| 526 | "LKML, Simon Wood <simon@mungewell.org> or Michal Maly <madcatxster@gmail.com>\n"); |
| 527 | return -1; |
| 528 | } |
| 529 | |
Michal Malý | 96440c8 | 2011-08-04 16:18:11 +0200 | [diff] [blame] | 530 | /* Attempt to switch wheel to native mode when applicable */ |
| 531 | udesc = &(hid_to_usb_dev(hid)->descriptor); |
| 532 | if (!udesc) { |
| 533 | hid_err(hid, "NULL USB device descriptor\n"); |
| 534 | return -1; |
| 535 | } |
| 536 | bcdDevice = le16_to_cpu(udesc->bcdDevice); |
| 537 | rev_maj = bcdDevice >> 8; |
| 538 | rev_min = bcdDevice & 0xff; |
| 539 | |
| 540 | if (lg4ff_devices[i].product_id == USB_DEVICE_ID_LOGITECH_WHEEL) { |
| 541 | dbg_hid("Generic wheel detected, can it do native?\n"); |
| 542 | dbg_hid("USB revision: %2x.%02x\n", rev_maj, rev_min); |
| 543 | |
| 544 | for (j = 0; j < ARRAY_SIZE(lg4ff_revs); j++) { |
| 545 | if (lg4ff_revs[j].rev_maj == rev_maj && lg4ff_revs[j].rev_min == rev_min) { |
| 546 | hid_lg4ff_switch_native(hid, lg4ff_revs[j].command); |
| 547 | hid_info(hid, "Switched to native mode\n"); |
| 548 | } |
| 549 | } |
| 550 | } |
| 551 | |
Michal Malý | 7362cd2 | 2011-08-04 16:16:09 +0200 | [diff] [blame] | 552 | /* Set supported force feedback capabilities */ |
| 553 | for (j = 0; lg4ff_devices[i].ff_effects[j] >= 0; j++) |
| 554 | set_bit(lg4ff_devices[i].ff_effects[j], dev->ffbit); |
Simon Wood | 32c88cb | 2010-09-22 13:19:42 +0200 | [diff] [blame] | 555 | |
| 556 | error = input_ff_create_memless(dev, NULL, hid_lg4ff_play); |
| 557 | |
| 558 | if (error) |
| 559 | return error; |
| 560 | |
Michal Malý | 6e2de8e | 2011-08-04 16:22:07 +0200 | [diff] [blame] | 561 | /* Check if autocentering is available and |
| 562 | * set the centering force to zero by default */ |
| 563 | if (test_bit(FF_AUTOCENTER, dev->ffbit)) { |
Michal Malý | 74479ba | 2012-09-24 01:09:30 +0200 | [diff] [blame] | 564 | if (rev_maj == FFEX_REV_MAJ && rev_min == FFEX_REV_MIN) /* Formula Force EX expects different autocentering command */ |
Michal Malý | 6e2de8e | 2011-08-04 16:22:07 +0200 | [diff] [blame] | 565 | dev->ff->set_autocenter = hid_lg4ff_set_autocenter_ffex; |
| 566 | else |
| 567 | dev->ff->set_autocenter = hid_lg4ff_set_autocenter_default; |
| 568 | |
| 569 | dev->ff->set_autocenter(dev, 0); |
| 570 | } |
Simon Wood | 32c88cb | 2010-09-22 13:19:42 +0200 | [diff] [blame] | 571 | |
Michal Malý | 3b6b17b | 2012-04-09 09:08:49 +0200 | [diff] [blame] | 572 | /* Get private driver data */ |
| 573 | drv_data = hid_get_drvdata(hid); |
| 574 | if (!drv_data) { |
| 575 | hid_err(hid, "Cannot add device, private driver data not allocated\n"); |
| 576 | return -1; |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 577 | } |
| 578 | |
Michal Malý | 3b6b17b | 2012-04-09 09:08:49 +0200 | [diff] [blame] | 579 | /* Initialize device properties */ |
Thomas Meyer | 8383c6b | 2011-11-08 20:14:14 +0100 | [diff] [blame] | 580 | entry = kzalloc(sizeof(struct lg4ff_device_entry), GFP_KERNEL); |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 581 | if (!entry) { |
Michal Malý | 3b6b17b | 2012-04-09 09:08:49 +0200 | [diff] [blame] | 582 | hid_err(hid, "Cannot add device, insufficient memory to allocate device properties.\n"); |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 583 | return -ENOMEM; |
| 584 | } |
Michal Malý | 3b6b17b | 2012-04-09 09:08:49 +0200 | [diff] [blame] | 585 | drv_data->device_props = entry; |
| 586 | |
Michal Malý | 2b24a96 | 2012-09-23 22:41:08 +0200 | [diff] [blame] | 587 | entry->product_id = lg4ff_devices[i].product_id; |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 588 | entry->min_range = lg4ff_devices[i].min_range; |
| 589 | entry->max_range = lg4ff_devices[i].max_range; |
| 590 | entry->set_range = lg4ff_devices[i].set_range; |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 591 | |
| 592 | /* Create sysfs interface */ |
| 593 | error = device_create_file(&hid->dev, &dev_attr_range); |
| 594 | if (error) |
| 595 | return error; |
| 596 | dbg_hid("sysfs interface created\n"); |
| 597 | |
| 598 | /* Set the maximum range to start with */ |
| 599 | entry->range = entry->max_range; |
| 600 | if (entry->set_range != NULL) |
| 601 | entry->set_range(hid, entry->range); |
| 602 | |
Simon Wood | 22bcefd | 2012-04-21 05:41:15 -0700 | [diff] [blame] | 603 | #ifdef CONFIG_LEDS_CLASS |
| 604 | /* register led subsystem - G27 only */ |
| 605 | entry->led_state = 0; |
| 606 | for (j = 0; j < 5; j++) |
| 607 | entry->led[j] = NULL; |
| 608 | |
| 609 | if (lg4ff_devices[i].product_id == USB_DEVICE_ID_LOGITECH_G27_WHEEL) { |
| 610 | struct led_classdev *led; |
| 611 | size_t name_sz; |
| 612 | char *name; |
| 613 | |
| 614 | lg4ff_set_leds(hid, 0); |
| 615 | |
| 616 | name_sz = strlen(dev_name(&hid->dev)) + 8; |
| 617 | |
| 618 | for (j = 0; j < 5; j++) { |
| 619 | led = kzalloc(sizeof(struct led_classdev)+name_sz, GFP_KERNEL); |
| 620 | if (!led) { |
| 621 | hid_err(hid, "can't allocate memory for LED %d\n", j); |
| 622 | goto err; |
| 623 | } |
| 624 | |
| 625 | name = (void *)(&led[1]); |
| 626 | snprintf(name, name_sz, "%s::RPM%d", dev_name(&hid->dev), j+1); |
| 627 | led->name = name; |
| 628 | led->brightness = 0; |
| 629 | led->max_brightness = 1; |
| 630 | led->brightness_get = lg4ff_led_get_brightness; |
| 631 | led->brightness_set = lg4ff_led_set_brightness; |
| 632 | |
| 633 | entry->led[j] = led; |
| 634 | error = led_classdev_register(&hid->dev, led); |
| 635 | |
| 636 | if (error) { |
| 637 | hid_err(hid, "failed to register LED %d. Aborting.\n", j); |
| 638 | err: |
| 639 | /* Deregister LEDs (if any) */ |
| 640 | for (j = 0; j < 5; j++) { |
| 641 | led = entry->led[j]; |
| 642 | entry->led[j] = NULL; |
| 643 | if (!led) |
| 644 | continue; |
| 645 | led_classdev_unregister(led); |
| 646 | kfree(led); |
| 647 | } |
| 648 | goto out; /* Let the driver continue without LEDs */ |
| 649 | } |
| 650 | } |
| 651 | } |
Simon Wood | 22bcefd | 2012-04-21 05:41:15 -0700 | [diff] [blame] | 652 | out: |
Jiri Kosina | c6e6dc8 | 2012-04-23 21:08:15 +0200 | [diff] [blame] | 653 | #endif |
Simon Wood | 6401380 | 2012-04-21 05:41:16 -0700 | [diff] [blame] | 654 | hid_info(hid, "Force feedback support for Logitech Gaming Wheels\n"); |
Simon Wood | 32c88cb | 2010-09-22 13:19:42 +0200 | [diff] [blame] | 655 | return 0; |
| 656 | } |
| 657 | |
Michal Malý | 2b24a96 | 2012-09-23 22:41:08 +0200 | [diff] [blame] | 658 | |
| 659 | |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 660 | int lg4ff_deinit(struct hid_device *hid) |
| 661 | { |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 662 | struct lg4ff_device_entry *entry; |
Michal Malý | 3b6b17b | 2012-04-09 09:08:49 +0200 | [diff] [blame] | 663 | struct lg_drv_data *drv_data; |
| 664 | |
Michal Malý | 6a2e176 | 2012-03-31 11:35:05 +0200 | [diff] [blame] | 665 | device_remove_file(&hid->dev, &dev_attr_range); |
| 666 | |
Michal Malý | 3b6b17b | 2012-04-09 09:08:49 +0200 | [diff] [blame] | 667 | drv_data = hid_get_drvdata(hid); |
| 668 | if (!drv_data) { |
| 669 | hid_err(hid, "Error while deinitializing device, no private driver data.\n"); |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 670 | return -1; |
| 671 | } |
Michal Malý | 3b6b17b | 2012-04-09 09:08:49 +0200 | [diff] [blame] | 672 | entry = drv_data->device_props; |
| 673 | if (!entry) { |
| 674 | hid_err(hid, "Error while deinitializing device, no device properties data.\n"); |
| 675 | return -1; |
| 676 | } |
Simon Wood | 22bcefd | 2012-04-21 05:41:15 -0700 | [diff] [blame] | 677 | |
| 678 | #ifdef CONFIG_LEDS_CLASS |
| 679 | { |
| 680 | int j; |
| 681 | struct led_classdev *led; |
| 682 | |
| 683 | /* Deregister LEDs (if any) */ |
| 684 | for (j = 0; j < 5; j++) { |
| 685 | |
| 686 | led = entry->led[j]; |
| 687 | entry->led[j] = NULL; |
| 688 | if (!led) |
| 689 | continue; |
| 690 | led_classdev_unregister(led); |
| 691 | kfree(led); |
| 692 | } |
| 693 | } |
| 694 | #endif |
| 695 | |
Michal Malý | 3b6b17b | 2012-04-09 09:08:49 +0200 | [diff] [blame] | 696 | /* Deallocate memory */ |
| 697 | kfree(entry); |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 698 | |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 699 | dbg_hid("Device successfully unregistered\n"); |
| 700 | return 0; |
| 701 | } |