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); |
Simon Wood | 32c88cb | 2010-09-22 13:19:42 +0200 | [diff] [blame] | 487 | struct input_dev *dev = hidinput->input; |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 488 | struct lg4ff_device_entry *entry; |
Michal Malý | 3b6b17b | 2012-04-09 09:08:49 +0200 | [diff] [blame] | 489 | struct lg_drv_data *drv_data; |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 490 | struct usb_device_descriptor *udesc; |
Michal Malý | 7362cd2 | 2011-08-04 16:16:09 +0200 | [diff] [blame] | 491 | int error, i, j; |
Michal Malý | 96440c8 | 2011-08-04 16:18:11 +0200 | [diff] [blame] | 492 | __u16 bcdDevice, rev_maj, rev_min; |
Simon Wood | 32c88cb | 2010-09-22 13:19:42 +0200 | [diff] [blame] | 493 | |
Simon Wood | 32c88cb | 2010-09-22 13:19:42 +0200 | [diff] [blame] | 494 | /* Check that the report looks ok */ |
Kees Cook | 0fb6bd0 | 2013-09-11 21:56:54 +0200 | [diff] [blame] | 495 | if (!hid_validate_values(hid, HID_OUTPUT_REPORT, 0, 0, 7)) |
Simon Wood | 32c88cb | 2010-09-22 13:19:42 +0200 | [diff] [blame] | 496 | return -1; |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 497 | |
Michal Malý | 7362cd2 | 2011-08-04 16:16:09 +0200 | [diff] [blame] | 498 | /* Check what wheel has been connected */ |
| 499 | for (i = 0; i < ARRAY_SIZE(lg4ff_devices); i++) { |
| 500 | if (hid->product == lg4ff_devices[i].product_id) { |
| 501 | dbg_hid("Found compatible device, product ID %04X\n", lg4ff_devices[i].product_id); |
| 502 | break; |
| 503 | } |
| 504 | } |
Simon Wood | 32c88cb | 2010-09-22 13:19:42 +0200 | [diff] [blame] | 505 | |
Michal Malý | 7362cd2 | 2011-08-04 16:16:09 +0200 | [diff] [blame] | 506 | if (i == ARRAY_SIZE(lg4ff_devices)) { |
| 507 | hid_err(hid, "Device is not supported by lg4ff driver. If you think it should be, consider reporting a bug to" |
| 508 | "LKML, Simon Wood <simon@mungewell.org> or Michal Maly <madcatxster@gmail.com>\n"); |
| 509 | return -1; |
| 510 | } |
| 511 | |
Michal Malý | 96440c8 | 2011-08-04 16:18:11 +0200 | [diff] [blame] | 512 | /* Attempt to switch wheel to native mode when applicable */ |
| 513 | udesc = &(hid_to_usb_dev(hid)->descriptor); |
| 514 | if (!udesc) { |
| 515 | hid_err(hid, "NULL USB device descriptor\n"); |
| 516 | return -1; |
| 517 | } |
| 518 | bcdDevice = le16_to_cpu(udesc->bcdDevice); |
| 519 | rev_maj = bcdDevice >> 8; |
| 520 | rev_min = bcdDevice & 0xff; |
| 521 | |
| 522 | if (lg4ff_devices[i].product_id == USB_DEVICE_ID_LOGITECH_WHEEL) { |
| 523 | dbg_hid("Generic wheel detected, can it do native?\n"); |
| 524 | dbg_hid("USB revision: %2x.%02x\n", rev_maj, rev_min); |
| 525 | |
| 526 | for (j = 0; j < ARRAY_SIZE(lg4ff_revs); j++) { |
| 527 | if (lg4ff_revs[j].rev_maj == rev_maj && lg4ff_revs[j].rev_min == rev_min) { |
| 528 | hid_lg4ff_switch_native(hid, lg4ff_revs[j].command); |
| 529 | hid_info(hid, "Switched to native mode\n"); |
| 530 | } |
| 531 | } |
| 532 | } |
| 533 | |
Michal Malý | 7362cd2 | 2011-08-04 16:16:09 +0200 | [diff] [blame] | 534 | /* Set supported force feedback capabilities */ |
| 535 | for (j = 0; lg4ff_devices[i].ff_effects[j] >= 0; j++) |
| 536 | set_bit(lg4ff_devices[i].ff_effects[j], dev->ffbit); |
Simon Wood | 32c88cb | 2010-09-22 13:19:42 +0200 | [diff] [blame] | 537 | |
| 538 | error = input_ff_create_memless(dev, NULL, hid_lg4ff_play); |
| 539 | |
| 540 | if (error) |
| 541 | return error; |
| 542 | |
Michal Malý | 6e2de8e | 2011-08-04 16:22:07 +0200 | [diff] [blame] | 543 | /* Check if autocentering is available and |
| 544 | * set the centering force to zero by default */ |
| 545 | if (test_bit(FF_AUTOCENTER, dev->ffbit)) { |
Michal Malý | 74479ba | 2012-09-24 01:09:30 +0200 | [diff] [blame] | 546 | 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] | 547 | dev->ff->set_autocenter = hid_lg4ff_set_autocenter_ffex; |
| 548 | else |
| 549 | dev->ff->set_autocenter = hid_lg4ff_set_autocenter_default; |
| 550 | |
| 551 | dev->ff->set_autocenter(dev, 0); |
| 552 | } |
Simon Wood | 32c88cb | 2010-09-22 13:19:42 +0200 | [diff] [blame] | 553 | |
Michal Malý | 3b6b17b | 2012-04-09 09:08:49 +0200 | [diff] [blame] | 554 | /* Get private driver data */ |
| 555 | drv_data = hid_get_drvdata(hid); |
| 556 | if (!drv_data) { |
| 557 | hid_err(hid, "Cannot add device, private driver data not allocated\n"); |
| 558 | return -1; |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 559 | } |
| 560 | |
Michal Malý | 3b6b17b | 2012-04-09 09:08:49 +0200 | [diff] [blame] | 561 | /* Initialize device properties */ |
Thomas Meyer | 8383c6b | 2011-11-08 20:14:14 +0100 | [diff] [blame] | 562 | entry = kzalloc(sizeof(struct lg4ff_device_entry), GFP_KERNEL); |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 563 | if (!entry) { |
Michal Malý | 3b6b17b | 2012-04-09 09:08:49 +0200 | [diff] [blame] | 564 | 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] | 565 | return -ENOMEM; |
| 566 | } |
Michal Malý | 3b6b17b | 2012-04-09 09:08:49 +0200 | [diff] [blame] | 567 | drv_data->device_props = entry; |
| 568 | |
Michal Malý | 2b24a96 | 2012-09-23 22:41:08 +0200 | [diff] [blame] | 569 | entry->product_id = lg4ff_devices[i].product_id; |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 570 | entry->min_range = lg4ff_devices[i].min_range; |
| 571 | entry->max_range = lg4ff_devices[i].max_range; |
| 572 | entry->set_range = lg4ff_devices[i].set_range; |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 573 | |
| 574 | /* Create sysfs interface */ |
| 575 | error = device_create_file(&hid->dev, &dev_attr_range); |
| 576 | if (error) |
| 577 | return error; |
| 578 | dbg_hid("sysfs interface created\n"); |
| 579 | |
| 580 | /* Set the maximum range to start with */ |
| 581 | entry->range = entry->max_range; |
| 582 | if (entry->set_range != NULL) |
| 583 | entry->set_range(hid, entry->range); |
| 584 | |
Simon Wood | 22bcefd | 2012-04-21 05:41:15 -0700 | [diff] [blame] | 585 | #ifdef CONFIG_LEDS_CLASS |
| 586 | /* register led subsystem - G27 only */ |
| 587 | entry->led_state = 0; |
| 588 | for (j = 0; j < 5; j++) |
| 589 | entry->led[j] = NULL; |
| 590 | |
| 591 | if (lg4ff_devices[i].product_id == USB_DEVICE_ID_LOGITECH_G27_WHEEL) { |
| 592 | struct led_classdev *led; |
| 593 | size_t name_sz; |
| 594 | char *name; |
| 595 | |
| 596 | lg4ff_set_leds(hid, 0); |
| 597 | |
| 598 | name_sz = strlen(dev_name(&hid->dev)) + 8; |
| 599 | |
| 600 | for (j = 0; j < 5; j++) { |
| 601 | led = kzalloc(sizeof(struct led_classdev)+name_sz, GFP_KERNEL); |
| 602 | if (!led) { |
| 603 | hid_err(hid, "can't allocate memory for LED %d\n", j); |
| 604 | goto err; |
| 605 | } |
| 606 | |
| 607 | name = (void *)(&led[1]); |
| 608 | snprintf(name, name_sz, "%s::RPM%d", dev_name(&hid->dev), j+1); |
| 609 | led->name = name; |
| 610 | led->brightness = 0; |
| 611 | led->max_brightness = 1; |
| 612 | led->brightness_get = lg4ff_led_get_brightness; |
| 613 | led->brightness_set = lg4ff_led_set_brightness; |
| 614 | |
| 615 | entry->led[j] = led; |
| 616 | error = led_classdev_register(&hid->dev, led); |
| 617 | |
| 618 | if (error) { |
| 619 | hid_err(hid, "failed to register LED %d. Aborting.\n", j); |
| 620 | err: |
| 621 | /* Deregister LEDs (if any) */ |
| 622 | for (j = 0; j < 5; j++) { |
| 623 | led = entry->led[j]; |
| 624 | entry->led[j] = NULL; |
| 625 | if (!led) |
| 626 | continue; |
| 627 | led_classdev_unregister(led); |
| 628 | kfree(led); |
| 629 | } |
| 630 | goto out; /* Let the driver continue without LEDs */ |
| 631 | } |
| 632 | } |
| 633 | } |
Simon Wood | 22bcefd | 2012-04-21 05:41:15 -0700 | [diff] [blame] | 634 | out: |
Jiri Kosina | c6e6dc8 | 2012-04-23 21:08:15 +0200 | [diff] [blame] | 635 | #endif |
Simon Wood | 6401380 | 2012-04-21 05:41:16 -0700 | [diff] [blame] | 636 | hid_info(hid, "Force feedback support for Logitech Gaming Wheels\n"); |
Simon Wood | 32c88cb | 2010-09-22 13:19:42 +0200 | [diff] [blame] | 637 | return 0; |
| 638 | } |
| 639 | |
Michal Malý | 2b24a96 | 2012-09-23 22:41:08 +0200 | [diff] [blame] | 640 | |
| 641 | |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 642 | int lg4ff_deinit(struct hid_device *hid) |
| 643 | { |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 644 | struct lg4ff_device_entry *entry; |
Michal Malý | 3b6b17b | 2012-04-09 09:08:49 +0200 | [diff] [blame] | 645 | struct lg_drv_data *drv_data; |
| 646 | |
Michal Malý | 6a2e176 | 2012-03-31 11:35:05 +0200 | [diff] [blame] | 647 | device_remove_file(&hid->dev, &dev_attr_range); |
| 648 | |
Michal Malý | 3b6b17b | 2012-04-09 09:08:49 +0200 | [diff] [blame] | 649 | drv_data = hid_get_drvdata(hid); |
| 650 | if (!drv_data) { |
| 651 | hid_err(hid, "Error while deinitializing device, no private driver data.\n"); |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 652 | return -1; |
| 653 | } |
Michal Malý | 3b6b17b | 2012-04-09 09:08:49 +0200 | [diff] [blame] | 654 | entry = drv_data->device_props; |
| 655 | if (!entry) { |
| 656 | hid_err(hid, "Error while deinitializing device, no device properties data.\n"); |
| 657 | return -1; |
| 658 | } |
Simon Wood | 22bcefd | 2012-04-21 05:41:15 -0700 | [diff] [blame] | 659 | |
| 660 | #ifdef CONFIG_LEDS_CLASS |
| 661 | { |
| 662 | int j; |
| 663 | struct led_classdev *led; |
| 664 | |
| 665 | /* Deregister LEDs (if any) */ |
| 666 | for (j = 0; j < 5; j++) { |
| 667 | |
| 668 | led = entry->led[j]; |
| 669 | entry->led[j] = NULL; |
| 670 | if (!led) |
| 671 | continue; |
| 672 | led_classdev_unregister(led); |
| 673 | kfree(led); |
| 674 | } |
| 675 | } |
| 676 | #endif |
| 677 | |
Michal Malý | 3b6b17b | 2012-04-09 09:08:49 +0200 | [diff] [blame] | 678 | /* Deallocate memory */ |
| 679 | kfree(entry); |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 680 | |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 681 | dbg_hid("Device successfully unregistered\n"); |
| 682 | return 0; |
| 683 | } |