Simon Wood | 32c88cb | 2010-09-22 13:19:42 +0200 | [diff] [blame] | 1 | /* |
Simon Wood | 64013800 | 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 | 64013800 | 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ý | a54dc779 | 2015-02-18 17:59:22 +0100 | [diff] [blame] | 33 | #include "hid-lg4ff.h" |
Michal Malý | 7362cd2 | 2011-08-04 16:16:09 +0200 | [diff] [blame] | 34 | #include "hid-ids.h" |
Simon Wood | 32c88cb | 2010-09-22 13:19:42 +0200 | [diff] [blame] | 35 | |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 36 | #define to_hid_device(pdev) container_of(pdev, struct hid_device, dev) |
| 37 | |
Michal Malý | b96d23e | 2015-02-18 17:59:21 +0100 | [diff] [blame] | 38 | #define LG4FF_MMODE_IS_MULTIMODE 0 |
Michal Malý | e7c2344 | 2015-02-18 17:59:20 +0100 | [diff] [blame] | 39 | #define LG4FF_MMODE_SWITCHED 1 |
| 40 | #define LG4FF_MMODE_NOT_MULTIMODE 2 |
| 41 | |
Michal Malý | b96d23e | 2015-02-18 17:59:21 +0100 | [diff] [blame] | 42 | #define LG4FF_MODE_NATIVE_IDX 0 |
| 43 | #define LG4FF_MODE_DFEX_IDX 1 |
| 44 | #define LG4FF_MODE_DFP_IDX 2 |
| 45 | #define LG4FF_MODE_G25_IDX 3 |
| 46 | #define LG4FF_MODE_DFGT_IDX 4 |
| 47 | #define LG4FF_MODE_G27_IDX 5 |
| 48 | #define LG4FF_MODE_MAX_IDX 6 |
| 49 | |
| 50 | #define LG4FF_MODE_NATIVE BIT(LG4FF_MODE_NATIVE_IDX) |
| 51 | #define LG4FF_MODE_DFEX BIT(LG4FF_MODE_DFEX_IDX) |
| 52 | #define LG4FF_MODE_DFP BIT(LG4FF_MODE_DFP_IDX) |
| 53 | #define LG4FF_MODE_G25 BIT(LG4FF_MODE_G25_IDX) |
| 54 | #define LG4FF_MODE_DFGT BIT(LG4FF_MODE_DFGT_IDX) |
| 55 | #define LG4FF_MODE_G27 BIT(LG4FF_MODE_G27_IDX) |
| 56 | |
| 57 | #define LG4FF_DFEX_TAG "DF-EX" |
| 58 | #define LG4FF_DFEX_NAME "Driving Force / Formula EX" |
| 59 | #define LG4FF_DFP_TAG "DFP" |
| 60 | #define LG4FF_DFP_NAME "Driving Force Pro" |
| 61 | #define LG4FF_G25_TAG "G25" |
| 62 | #define LG4FF_G25_NAME "G25 Racing Wheel" |
| 63 | #define LG4FF_G27_TAG "G27" |
| 64 | #define LG4FF_G27_NAME "G27 Racing Wheel" |
| 65 | #define LG4FF_DFGT_TAG "DFGT" |
| 66 | #define LG4FF_DFGT_NAME "Driving Force GT" |
| 67 | |
Michal Malý | e7c2344 | 2015-02-18 17:59:20 +0100 | [diff] [blame] | 68 | #define LG4FF_FFEX_REV_MAJ 0x21 |
| 69 | #define LG4FF_FFEX_REV_MIN 0x00 |
| 70 | |
Michal Malý | d0afd8489 | 2015-04-08 22:56:40 +0200 | [diff] [blame] | 71 | static void lg4ff_set_range_dfp(struct hid_device *hid, u16 range); |
| 72 | static void lg4ff_set_range_g25(struct hid_device *hid, u16 range); |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 73 | |
Michal Malý | 72529c6 | 2015-04-08 22:56:46 +0200 | [diff] [blame] | 74 | struct lg4ff_wheel_data { |
Michal Malý | 2a552c3 | 2015-04-08 22:56:39 +0200 | [diff] [blame] | 75 | u32 product_id; |
| 76 | u16 range; |
| 77 | u16 min_range; |
| 78 | u16 max_range; |
Simon Wood | 22bcefd | 2012-04-21 05:41:15 -0700 | [diff] [blame] | 79 | #ifdef CONFIG_LEDS_CLASS |
Michal Malý | 2a552c3 | 2015-04-08 22:56:39 +0200 | [diff] [blame] | 80 | u8 led_state; |
Simon Wood | 22bcefd | 2012-04-21 05:41:15 -0700 | [diff] [blame] | 81 | struct led_classdev *led[5]; |
| 82 | #endif |
Michal Malý | b96d23e | 2015-02-18 17:59:21 +0100 | [diff] [blame] | 83 | u32 alternate_modes; |
| 84 | const char *real_tag; |
| 85 | const char *real_name; |
| 86 | u16 real_product_id; |
Michal Malý | 72529c6 | 2015-04-08 22:56:46 +0200 | [diff] [blame] | 87 | |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 88 | void (*set_range)(struct hid_device *hid, u16 range); |
| 89 | }; |
| 90 | |
Michal Malý | 72529c6 | 2015-04-08 22:56:46 +0200 | [diff] [blame] | 91 | struct lg4ff_device_entry { |
| 92 | struct lg4ff_wheel_data wdata; |
| 93 | }; |
| 94 | |
Michal Malý | 7362cd2 | 2011-08-04 16:16:09 +0200 | [diff] [blame] | 95 | static const signed short lg4ff_wheel_effects[] = { |
Simon Wood | 32c88cb | 2010-09-22 13:19:42 +0200 | [diff] [blame] | 96 | FF_CONSTANT, |
| 97 | FF_AUTOCENTER, |
| 98 | -1 |
| 99 | }; |
| 100 | |
Michal Malý | 7362cd2 | 2011-08-04 16:16:09 +0200 | [diff] [blame] | 101 | struct lg4ff_wheel { |
Michal Malý | 2a552c3 | 2015-04-08 22:56:39 +0200 | [diff] [blame] | 102 | const u32 product_id; |
Michal Malý | 7362cd2 | 2011-08-04 16:16:09 +0200 | [diff] [blame] | 103 | const signed short *ff_effects; |
Michal Malý | 2a552c3 | 2015-04-08 22:56:39 +0200 | [diff] [blame] | 104 | const u16 min_range; |
| 105 | const u16 max_range; |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 106 | void (*set_range)(struct hid_device *hid, u16 range); |
Michal Malý | 7362cd2 | 2011-08-04 16:16:09 +0200 | [diff] [blame] | 107 | }; |
| 108 | |
Michal Malý | e7c2344 | 2015-02-18 17:59:20 +0100 | [diff] [blame] | 109 | struct lg4ff_compat_mode_switch { |
Michal Malý | 2a552c3 | 2015-04-08 22:56:39 +0200 | [diff] [blame] | 110 | const u8 cmd_count; /* Number of commands to send */ |
| 111 | const u8 cmd[]; |
Michal Malý | e7c2344 | 2015-02-18 17:59:20 +0100 | [diff] [blame] | 112 | }; |
| 113 | |
| 114 | struct lg4ff_wheel_ident_info { |
| 115 | const u16 mask; |
| 116 | const u16 result; |
| 117 | const u16 real_product_id; |
| 118 | }; |
| 119 | |
| 120 | struct lg4ff_wheel_ident_checklist { |
| 121 | const u32 count; |
| 122 | const struct lg4ff_wheel_ident_info *models[]; |
| 123 | }; |
| 124 | |
Michal Malý | b96d23e | 2015-02-18 17:59:21 +0100 | [diff] [blame] | 125 | struct lg4ff_multimode_wheel { |
| 126 | const u16 product_id; |
| 127 | const u32 alternate_modes; |
| 128 | const char *real_tag; |
| 129 | const char *real_name; |
| 130 | }; |
| 131 | |
| 132 | struct lg4ff_alternate_mode { |
| 133 | const u16 product_id; |
| 134 | const char *tag; |
| 135 | const char *name; |
| 136 | }; |
| 137 | |
Michal Malý | 7362cd2 | 2011-08-04 16:16:09 +0200 | [diff] [blame] | 138 | static const struct lg4ff_wheel lg4ff_devices[] = { |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 139 | {USB_DEVICE_ID_LOGITECH_WHEEL, lg4ff_wheel_effects, 40, 270, NULL}, |
| 140 | {USB_DEVICE_ID_LOGITECH_MOMO_WHEEL, lg4ff_wheel_effects, 40, 270, NULL}, |
Michal Malý | d0afd8489 | 2015-04-08 22:56:40 +0200 | [diff] [blame] | 141 | {USB_DEVICE_ID_LOGITECH_DFP_WHEEL, lg4ff_wheel_effects, 40, 900, lg4ff_set_range_dfp}, |
| 142 | {USB_DEVICE_ID_LOGITECH_G25_WHEEL, lg4ff_wheel_effects, 40, 900, lg4ff_set_range_g25}, |
| 143 | {USB_DEVICE_ID_LOGITECH_DFGT_WHEEL, lg4ff_wheel_effects, 40, 900, lg4ff_set_range_g25}, |
| 144 | {USB_DEVICE_ID_LOGITECH_G27_WHEEL, lg4ff_wheel_effects, 40, 900, lg4ff_set_range_g25}, |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 145 | {USB_DEVICE_ID_LOGITECH_MOMO_WHEEL2, lg4ff_wheel_effects, 40, 270, NULL}, |
| 146 | {USB_DEVICE_ID_LOGITECH_WII_WHEEL, lg4ff_wheel_effects, 40, 270, NULL} |
Michal Malý | 7362cd2 | 2011-08-04 16:16:09 +0200 | [diff] [blame] | 147 | }; |
| 148 | |
Michal Malý | b96d23e | 2015-02-18 17:59:21 +0100 | [diff] [blame] | 149 | static const struct lg4ff_multimode_wheel lg4ff_multimode_wheels[] = { |
| 150 | {USB_DEVICE_ID_LOGITECH_DFP_WHEEL, |
| 151 | LG4FF_MODE_NATIVE | LG4FF_MODE_DFP | LG4FF_MODE_DFEX, |
| 152 | LG4FF_DFP_TAG, LG4FF_DFP_NAME}, |
| 153 | {USB_DEVICE_ID_LOGITECH_G25_WHEEL, |
| 154 | LG4FF_MODE_NATIVE | LG4FF_MODE_G25 | LG4FF_MODE_DFP | LG4FF_MODE_DFEX, |
| 155 | LG4FF_G25_TAG, LG4FF_G25_NAME}, |
| 156 | {USB_DEVICE_ID_LOGITECH_DFGT_WHEEL, |
| 157 | LG4FF_MODE_NATIVE | LG4FF_MODE_DFGT | LG4FF_MODE_DFP | LG4FF_MODE_DFEX, |
| 158 | LG4FF_DFGT_TAG, LG4FF_DFGT_NAME}, |
| 159 | {USB_DEVICE_ID_LOGITECH_G27_WHEEL, |
| 160 | LG4FF_MODE_NATIVE | LG4FF_MODE_G27 | LG4FF_MODE_G25 | LG4FF_MODE_DFP | LG4FF_MODE_DFEX, |
| 161 | LG4FF_G27_TAG, LG4FF_G27_NAME}, |
| 162 | }; |
| 163 | |
| 164 | static const struct lg4ff_alternate_mode lg4ff_alternate_modes[] = { |
| 165 | [LG4FF_MODE_NATIVE_IDX] = {0, "native", ""}, |
| 166 | [LG4FF_MODE_DFEX_IDX] = {USB_DEVICE_ID_LOGITECH_WHEEL, LG4FF_DFEX_TAG, LG4FF_DFEX_NAME}, |
| 167 | [LG4FF_MODE_DFP_IDX] = {USB_DEVICE_ID_LOGITECH_DFP_WHEEL, LG4FF_DFP_TAG, LG4FF_DFP_NAME}, |
| 168 | [LG4FF_MODE_G25_IDX] = {USB_DEVICE_ID_LOGITECH_G25_WHEEL, LG4FF_G25_TAG, LG4FF_G25_NAME}, |
| 169 | [LG4FF_MODE_DFGT_IDX] = {USB_DEVICE_ID_LOGITECH_DFGT_WHEEL, LG4FF_DFGT_TAG, LG4FF_DFGT_NAME}, |
| 170 | [LG4FF_MODE_G27_IDX] = {USB_DEVICE_ID_LOGITECH_G27_WHEEL, LG4FF_G27_TAG, LG4FF_G27_NAME} |
| 171 | }; |
| 172 | |
Michal Malý | e7c2344 | 2015-02-18 17:59:20 +0100 | [diff] [blame] | 173 | /* Multimode wheel identificators */ |
| 174 | static const struct lg4ff_wheel_ident_info lg4ff_dfp_ident_info = { |
| 175 | 0xf000, |
| 176 | 0x1000, |
| 177 | USB_DEVICE_ID_LOGITECH_DFP_WHEEL |
Michal Malý | 96440c8 | 2011-08-04 16:18:11 +0200 | [diff] [blame] | 178 | }; |
| 179 | |
Michal Malý | e7c2344 | 2015-02-18 17:59:20 +0100 | [diff] [blame] | 180 | static const struct lg4ff_wheel_ident_info lg4ff_g25_ident_info = { |
| 181 | 0xff00, |
| 182 | 0x1200, |
| 183 | USB_DEVICE_ID_LOGITECH_G25_WHEEL |
Michal Malý | 96440c8 | 2011-08-04 16:18:11 +0200 | [diff] [blame] | 184 | }; |
| 185 | |
Michal Malý | e7c2344 | 2015-02-18 17:59:20 +0100 | [diff] [blame] | 186 | static const struct lg4ff_wheel_ident_info lg4ff_g27_ident_info = { |
| 187 | 0xfff0, |
| 188 | 0x1230, |
| 189 | USB_DEVICE_ID_LOGITECH_G27_WHEEL |
| 190 | }; |
| 191 | |
| 192 | static const struct lg4ff_wheel_ident_info lg4ff_dfgt_ident_info = { |
| 193 | 0xff00, |
| 194 | 0x1300, |
| 195 | USB_DEVICE_ID_LOGITECH_DFGT_WHEEL |
| 196 | }; |
| 197 | |
| 198 | /* Multimode wheel identification checklists */ |
| 199 | static const struct lg4ff_wheel_ident_checklist lg4ff_main_checklist = { |
| 200 | 4, |
| 201 | {&lg4ff_dfgt_ident_info, |
| 202 | &lg4ff_g27_ident_info, |
| 203 | &lg4ff_g25_ident_info, |
| 204 | &lg4ff_dfp_ident_info} |
| 205 | }; |
| 206 | |
| 207 | /* Compatibility mode switching commands */ |
Michal Malý | f31a2de | 2015-02-18 17:59:23 +0100 | [diff] [blame] | 208 | /* EXT_CMD9 - Understood by G27 and DFGT */ |
| 209 | static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext09_dfex = { |
| 210 | 2, |
| 211 | {0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, /* Revert mode upon USB reset */ |
| 212 | 0xf8, 0x09, 0x00, 0x01, 0x00, 0x00, 0x00} /* Switch mode to DF-EX with detach */ |
| 213 | }; |
| 214 | |
| 215 | static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext09_dfp = { |
| 216 | 2, |
| 217 | {0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, /* Revert mode upon USB reset */ |
| 218 | 0xf8, 0x09, 0x01, 0x01, 0x00, 0x00, 0x00} /* Switch mode to DFP with detach */ |
| 219 | }; |
| 220 | |
| 221 | static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext09_g25 = { |
| 222 | 2, |
| 223 | {0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, /* Revert mode upon USB reset */ |
| 224 | 0xf8, 0x09, 0x02, 0x01, 0x00, 0x00, 0x00} /* Switch mode to G25 with detach */ |
| 225 | }; |
| 226 | |
| 227 | static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext09_dfgt = { |
| 228 | 2, |
| 229 | {0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, /* Revert mode upon USB reset */ |
| 230 | 0xf8, 0x09, 0x03, 0x01, 0x00, 0x00, 0x00} /* Switch mode to DFGT with detach */ |
| 231 | }; |
| 232 | |
| 233 | static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext09_g27 = { |
| 234 | 2, |
| 235 | {0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, /* Revert mode upon USB reset */ |
| 236 | 0xf8, 0x09, 0x04, 0x01, 0x00, 0x00, 0x00} /* Switch mode to G27 with detach */ |
| 237 | }; |
| 238 | |
| 239 | /* EXT_CMD1 - Understood by DFP, G25, G27 and DFGT */ |
| 240 | static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext01_dfp = { |
Michal Malý | 96440c8 | 2011-08-04 16:18:11 +0200 | [diff] [blame] | 241 | 1, |
| 242 | {0xf8, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00} |
| 243 | }; |
| 244 | |
Michal Malý | f31a2de | 2015-02-18 17:59:23 +0100 | [diff] [blame] | 245 | /* EXT_CMD16 - Understood by G25 and G27 */ |
| 246 | static const struct lg4ff_compat_mode_switch lg4ff_mode_switch_ext16_g25 = { |
Michal Malý | 96440c8 | 2011-08-04 16:18:11 +0200 | [diff] [blame] | 247 | 1, |
| 248 | {0xf8, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00} |
| 249 | }; |
| 250 | |
Michal Malý | 2b24a96 | 2012-09-23 22:41:08 +0200 | [diff] [blame] | 251 | /* Recalculates X axis value accordingly to currently selected range */ |
Michal Malý | 2a552c3 | 2015-04-08 22:56:39 +0200 | [diff] [blame] | 252 | static s32 lg4ff_adjust_dfp_x_axis(s32 value, u16 range) |
Michal Malý | 2b24a96 | 2012-09-23 22:41:08 +0200 | [diff] [blame] | 253 | { |
Michal Malý | 2a552c3 | 2015-04-08 22:56:39 +0200 | [diff] [blame] | 254 | u16 max_range; |
| 255 | s32 new_value; |
Michal Malý | 2b24a96 | 2012-09-23 22:41:08 +0200 | [diff] [blame] | 256 | |
| 257 | if (range == 900) |
| 258 | return value; |
| 259 | else if (range == 200) |
| 260 | return value; |
| 261 | else if (range < 200) |
| 262 | max_range = 200; |
| 263 | else |
| 264 | max_range = 900; |
| 265 | |
| 266 | new_value = 8192 + mult_frac(value - 8192, max_range, range); |
| 267 | if (new_value < 0) |
| 268 | return 0; |
| 269 | else if (new_value > 16383) |
| 270 | return 16383; |
| 271 | else |
| 272 | return new_value; |
| 273 | } |
| 274 | |
| 275 | int lg4ff_adjust_input_event(struct hid_device *hid, struct hid_field *field, |
Michal Malý | 2a552c3 | 2015-04-08 22:56:39 +0200 | [diff] [blame] | 276 | struct hid_usage *usage, s32 value, struct lg_drv_data *drv_data) |
Michal Malý | 2b24a96 | 2012-09-23 22:41:08 +0200 | [diff] [blame] | 277 | { |
| 278 | struct lg4ff_device_entry *entry = drv_data->device_props; |
Michal Malý | 2a552c3 | 2015-04-08 22:56:39 +0200 | [diff] [blame] | 279 | s32 new_value = 0; |
Michal Malý | 2b24a96 | 2012-09-23 22:41:08 +0200 | [diff] [blame] | 280 | |
| 281 | if (!entry) { |
| 282 | hid_err(hid, "Device properties not found"); |
| 283 | return 0; |
| 284 | } |
| 285 | |
Michal Malý | 72529c6 | 2015-04-08 22:56:46 +0200 | [diff] [blame] | 286 | switch (entry->wdata.product_id) { |
Michal Malý | 2b24a96 | 2012-09-23 22:41:08 +0200 | [diff] [blame] | 287 | case USB_DEVICE_ID_LOGITECH_DFP_WHEEL: |
| 288 | switch (usage->code) { |
| 289 | case ABS_X: |
Michal Malý | 72529c6 | 2015-04-08 22:56:46 +0200 | [diff] [blame] | 290 | new_value = lg4ff_adjust_dfp_x_axis(value, entry->wdata.range); |
Michal Malý | 2b24a96 | 2012-09-23 22:41:08 +0200 | [diff] [blame] | 291 | input_event(field->hidinput->input, usage->type, usage->code, new_value); |
| 292 | return 1; |
| 293 | default: |
| 294 | return 0; |
| 295 | } |
| 296 | default: |
| 297 | return 0; |
| 298 | } |
| 299 | } |
| 300 | |
Michal Malý | d0afd8489 | 2015-04-08 22:56:40 +0200 | [diff] [blame] | 301 | static int lg4ff_play(struct input_dev *dev, void *data, struct ff_effect *effect) |
Simon Wood | 32c88cb | 2010-09-22 13:19:42 +0200 | [diff] [blame] | 302 | { |
| 303 | struct hid_device *hid = input_get_drvdata(dev); |
| 304 | struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list; |
| 305 | struct hid_report *report = list_entry(report_list->next, struct hid_report, list); |
Michal Malý | 2a552c3 | 2015-04-08 22:56:39 +0200 | [diff] [blame] | 306 | s32 *value = report->field[0]->value; |
Simon Wood | 32c88cb | 2010-09-22 13:19:42 +0200 | [diff] [blame] | 307 | int x; |
| 308 | |
Michal Malý | a80fe5d | 2012-09-24 01:13:17 +0200 | [diff] [blame] | 309 | #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] | 310 | |
| 311 | switch (effect->type) { |
| 312 | case FF_CONSTANT: |
| 313 | x = effect->u.ramp.start_level + 0x80; /* 0x80 is no force */ |
| 314 | CLAMP(x); |
Simon Wood | 56930e7 | 2013-11-06 12:30:42 -0700 | [diff] [blame] | 315 | |
| 316 | if (x == 0x80) { |
| 317 | /* De-activate force in slot-1*/ |
| 318 | value[0] = 0x13; |
| 319 | value[1] = 0x00; |
| 320 | value[2] = 0x00; |
| 321 | value[3] = 0x00; |
| 322 | value[4] = 0x00; |
| 323 | value[5] = 0x00; |
| 324 | value[6] = 0x00; |
| 325 | |
| 326 | hid_hw_request(hid, report, HID_REQ_SET_REPORT); |
| 327 | return 0; |
| 328 | } |
| 329 | |
Michal Malý | 74479ba | 2012-09-24 01:09:30 +0200 | [diff] [blame] | 330 | value[0] = 0x11; /* Slot 1 */ |
| 331 | value[1] = 0x08; |
| 332 | value[2] = x; |
| 333 | value[3] = 0x80; |
| 334 | value[4] = 0x00; |
| 335 | value[5] = 0x00; |
| 336 | value[6] = 0x00; |
Simon Wood | 32c88cb | 2010-09-22 13:19:42 +0200 | [diff] [blame] | 337 | |
Benjamin Tissoires | d8814272 | 2013-02-25 11:31:46 +0100 | [diff] [blame] | 338 | hid_hw_request(hid, report, HID_REQ_SET_REPORT); |
Simon Wood | 32c88cb | 2010-09-22 13:19:42 +0200 | [diff] [blame] | 339 | break; |
| 340 | } |
| 341 | return 0; |
| 342 | } |
| 343 | |
Michal Malý | 6e2de8e | 2011-08-04 16:22:07 +0200 | [diff] [blame] | 344 | /* Sends default autocentering command compatible with |
| 345 | * all wheels except Formula Force EX */ |
Michal Malý | d0afd8489 | 2015-04-08 22:56:40 +0200 | [diff] [blame] | 346 | static void lg4ff_set_autocenter_default(struct input_dev *dev, u16 magnitude) |
Simon Wood | 32c88cb | 2010-09-22 13:19:42 +0200 | [diff] [blame] | 347 | { |
| 348 | struct hid_device *hid = input_get_drvdata(dev); |
| 349 | struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list; |
| 350 | struct hid_report *report = list_entry(report_list->next, struct hid_report, list); |
Michal Malý | 2a552c3 | 2015-04-08 22:56:39 +0200 | [diff] [blame] | 351 | s32 *value = report->field[0]->value; |
| 352 | u32 expand_a, expand_b; |
Simon Wood | 1859762 | 2013-11-06 12:30:44 -0700 | [diff] [blame] | 353 | struct lg4ff_device_entry *entry; |
| 354 | struct lg_drv_data *drv_data; |
| 355 | |
| 356 | drv_data = hid_get_drvdata(hid); |
| 357 | if (!drv_data) { |
| 358 | hid_err(hid, "Private driver data not found!\n"); |
| 359 | return; |
| 360 | } |
| 361 | |
| 362 | entry = drv_data->device_props; |
| 363 | if (!entry) { |
| 364 | hid_err(hid, "Device properties not found!\n"); |
| 365 | return; |
| 366 | } |
Simon Wood | f8c2315 | 2013-11-06 12:30:40 -0700 | [diff] [blame] | 367 | |
Simon Wood | d2c02da | 2013-11-06 12:30:41 -0700 | [diff] [blame] | 368 | /* De-activate Auto-Center */ |
| 369 | if (magnitude == 0) { |
| 370 | value[0] = 0xf5; |
| 371 | value[1] = 0x00; |
| 372 | value[2] = 0x00; |
| 373 | value[3] = 0x00; |
| 374 | value[4] = 0x00; |
| 375 | value[5] = 0x00; |
| 376 | value[6] = 0x00; |
| 377 | |
| 378 | hid_hw_request(hid, report, HID_REQ_SET_REPORT); |
| 379 | return; |
| 380 | } |
| 381 | |
Simon Wood | f8c2315 | 2013-11-06 12:30:40 -0700 | [diff] [blame] | 382 | if (magnitude <= 0xaaaa) { |
| 383 | expand_a = 0x0c * magnitude; |
| 384 | expand_b = 0x80 * magnitude; |
| 385 | } else { |
| 386 | expand_a = (0x0c * 0xaaaa) + 0x06 * (magnitude - 0xaaaa); |
| 387 | expand_b = (0x80 * 0xaaaa) + 0xff * (magnitude - 0xaaaa); |
| 388 | } |
Simon Wood | 32c88cb | 2010-09-22 13:19:42 +0200 | [diff] [blame] | 389 | |
Simon Wood | 1859762 | 2013-11-06 12:30:44 -0700 | [diff] [blame] | 390 | /* Adjust for non-MOMO wheels */ |
Michal Malý | 72529c6 | 2015-04-08 22:56:46 +0200 | [diff] [blame] | 391 | switch (entry->wdata.product_id) { |
Simon Wood | 1859762 | 2013-11-06 12:30:44 -0700 | [diff] [blame] | 392 | case USB_DEVICE_ID_LOGITECH_MOMO_WHEEL: |
| 393 | case USB_DEVICE_ID_LOGITECH_MOMO_WHEEL2: |
| 394 | break; |
| 395 | default: |
| 396 | expand_a = expand_a >> 1; |
| 397 | break; |
| 398 | } |
| 399 | |
Michal Malý | 74479ba | 2012-09-24 01:09:30 +0200 | [diff] [blame] | 400 | value[0] = 0xfe; |
| 401 | value[1] = 0x0d; |
Simon Wood | f8c2315 | 2013-11-06 12:30:40 -0700 | [diff] [blame] | 402 | value[2] = expand_a / 0xaaaa; |
| 403 | value[3] = expand_a / 0xaaaa; |
| 404 | value[4] = expand_b / 0xaaaa; |
Michal Malý | 74479ba | 2012-09-24 01:09:30 +0200 | [diff] [blame] | 405 | value[5] = 0x00; |
| 406 | value[6] = 0x00; |
Simon Wood | 32c88cb | 2010-09-22 13:19:42 +0200 | [diff] [blame] | 407 | |
Benjamin Tissoires | d8814272 | 2013-02-25 11:31:46 +0100 | [diff] [blame] | 408 | hid_hw_request(hid, report, HID_REQ_SET_REPORT); |
Simon Wood | d2c02da | 2013-11-06 12:30:41 -0700 | [diff] [blame] | 409 | |
| 410 | /* Activate Auto-Center */ |
| 411 | value[0] = 0x14; |
| 412 | value[1] = 0x00; |
| 413 | value[2] = 0x00; |
| 414 | value[3] = 0x00; |
| 415 | value[4] = 0x00; |
| 416 | value[5] = 0x00; |
| 417 | value[6] = 0x00; |
| 418 | |
| 419 | hid_hw_request(hid, report, HID_REQ_SET_REPORT); |
Simon Wood | 32c88cb | 2010-09-22 13:19:42 +0200 | [diff] [blame] | 420 | } |
| 421 | |
Michal Malý | 6e2de8e | 2011-08-04 16:22:07 +0200 | [diff] [blame] | 422 | /* Sends autocentering command compatible with Formula Force EX */ |
Michal Malý | d0afd8489 | 2015-04-08 22:56:40 +0200 | [diff] [blame] | 423 | static void lg4ff_set_autocenter_ffex(struct input_dev *dev, u16 magnitude) |
Michal Malý | 6e2de8e | 2011-08-04 16:22:07 +0200 | [diff] [blame] | 424 | { |
| 425 | struct hid_device *hid = input_get_drvdata(dev); |
| 426 | struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list; |
| 427 | struct hid_report *report = list_entry(report_list->next, struct hid_report, list); |
Michal Malý | 2a552c3 | 2015-04-08 22:56:39 +0200 | [diff] [blame] | 428 | s32 *value = report->field[0]->value; |
Michal Malý | 6e2de8e | 2011-08-04 16:22:07 +0200 | [diff] [blame] | 429 | magnitude = magnitude * 90 / 65535; |
Michal Malý | 6e2de8e | 2011-08-04 16:22:07 +0200 | [diff] [blame] | 430 | |
Michal Malý | 74479ba | 2012-09-24 01:09:30 +0200 | [diff] [blame] | 431 | value[0] = 0xfe; |
| 432 | value[1] = 0x03; |
| 433 | value[2] = magnitude >> 14; |
| 434 | value[3] = magnitude >> 14; |
| 435 | value[4] = magnitude; |
| 436 | value[5] = 0x00; |
| 437 | value[6] = 0x00; |
Michal Malý | 6e2de8e | 2011-08-04 16:22:07 +0200 | [diff] [blame] | 438 | |
Benjamin Tissoires | d8814272 | 2013-02-25 11:31:46 +0100 | [diff] [blame] | 439 | hid_hw_request(hid, report, HID_REQ_SET_REPORT); |
Michal Malý | 6e2de8e | 2011-08-04 16:22:07 +0200 | [diff] [blame] | 440 | } |
| 441 | |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 442 | /* Sends command to set range compatible with G25/G27/Driving Force GT */ |
Michal Malý | d0afd8489 | 2015-04-08 22:56:40 +0200 | [diff] [blame] | 443 | static void lg4ff_set_range_g25(struct hid_device *hid, u16 range) |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 444 | { |
| 445 | struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list; |
| 446 | struct hid_report *report = list_entry(report_list->next, struct hid_report, list); |
Michal Malý | 2a552c3 | 2015-04-08 22:56:39 +0200 | [diff] [blame] | 447 | s32 *value = report->field[0]->value; |
Michal Malý | 74479ba | 2012-09-24 01:09:30 +0200 | [diff] [blame] | 448 | |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 449 | dbg_hid("G25/G27/DFGT: setting range to %u\n", range); |
| 450 | |
Michal Malý | 74479ba | 2012-09-24 01:09:30 +0200 | [diff] [blame] | 451 | value[0] = 0xf8; |
| 452 | value[1] = 0x81; |
| 453 | value[2] = range & 0x00ff; |
| 454 | value[3] = (range & 0xff00) >> 8; |
| 455 | value[4] = 0x00; |
| 456 | value[5] = 0x00; |
| 457 | value[6] = 0x00; |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 458 | |
Benjamin Tissoires | d8814272 | 2013-02-25 11:31:46 +0100 | [diff] [blame] | 459 | hid_hw_request(hid, report, HID_REQ_SET_REPORT); |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | /* Sends commands to set range compatible with Driving Force Pro wheel */ |
Michal Malý | d0afd8489 | 2015-04-08 22:56:40 +0200 | [diff] [blame] | 463 | static void lg4ff_set_range_dfp(struct hid_device *hid, u16 range) |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 464 | { |
| 465 | struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list; |
| 466 | struct hid_report *report = list_entry(report_list->next, struct hid_report, list); |
| 467 | int start_left, start_right, full_range; |
Michal Malý | 2a552c3 | 2015-04-08 22:56:39 +0200 | [diff] [blame] | 468 | s32 *value = report->field[0]->value; |
Michal Malý | 74479ba | 2012-09-24 01:09:30 +0200 | [diff] [blame] | 469 | |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 470 | dbg_hid("Driving Force Pro: setting range to %u\n", range); |
| 471 | |
| 472 | /* Prepare "coarse" limit command */ |
Michal Malý | 74479ba | 2012-09-24 01:09:30 +0200 | [diff] [blame] | 473 | value[0] = 0xf8; |
Michal Malý | a80fe5d | 2012-09-24 01:13:17 +0200 | [diff] [blame] | 474 | value[1] = 0x00; /* Set later */ |
Michal Malý | 74479ba | 2012-09-24 01:09:30 +0200 | [diff] [blame] | 475 | value[2] = 0x00; |
| 476 | value[3] = 0x00; |
| 477 | value[4] = 0x00; |
| 478 | value[5] = 0x00; |
| 479 | value[6] = 0x00; |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 480 | |
| 481 | if (range > 200) { |
| 482 | report->field[0]->value[1] = 0x03; |
| 483 | full_range = 900; |
| 484 | } else { |
| 485 | report->field[0]->value[1] = 0x02; |
| 486 | full_range = 200; |
| 487 | } |
Benjamin Tissoires | d8814272 | 2013-02-25 11:31:46 +0100 | [diff] [blame] | 488 | hid_hw_request(hid, report, HID_REQ_SET_REPORT); |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 489 | |
| 490 | /* Prepare "fine" limit command */ |
Michal Malý | 74479ba | 2012-09-24 01:09:30 +0200 | [diff] [blame] | 491 | value[0] = 0x81; |
| 492 | value[1] = 0x0b; |
| 493 | value[2] = 0x00; |
| 494 | value[3] = 0x00; |
| 495 | value[4] = 0x00; |
| 496 | value[5] = 0x00; |
| 497 | value[6] = 0x00; |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 498 | |
| 499 | if (range == 200 || range == 900) { /* Do not apply any fine limit */ |
Benjamin Tissoires | d8814272 | 2013-02-25 11:31:46 +0100 | [diff] [blame] | 500 | hid_hw_request(hid, report, HID_REQ_SET_REPORT); |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 501 | return; |
| 502 | } |
| 503 | |
| 504 | /* Construct fine limit command */ |
| 505 | start_left = (((full_range - range + 1) * 2047) / full_range); |
| 506 | start_right = 0xfff - start_left; |
| 507 | |
Michal Malý | 74479ba | 2012-09-24 01:09:30 +0200 | [diff] [blame] | 508 | value[2] = start_left >> 4; |
| 509 | value[3] = start_right >> 4; |
| 510 | value[4] = 0xff; |
| 511 | value[5] = (start_right & 0xe) << 4 | (start_left & 0xe); |
| 512 | value[6] = 0xff; |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 513 | |
Benjamin Tissoires | d8814272 | 2013-02-25 11:31:46 +0100 | [diff] [blame] | 514 | hid_hw_request(hid, report, HID_REQ_SET_REPORT); |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 515 | } |
| 516 | |
Michal Malý | f31a2de | 2015-02-18 17:59:23 +0100 | [diff] [blame] | 517 | static const struct lg4ff_compat_mode_switch *lg4ff_get_mode_switch_command(const u16 real_product_id, const u16 target_product_id) |
| 518 | { |
| 519 | switch (real_product_id) { |
| 520 | case USB_DEVICE_ID_LOGITECH_DFP_WHEEL: |
| 521 | switch (target_product_id) { |
| 522 | case USB_DEVICE_ID_LOGITECH_DFP_WHEEL: |
| 523 | return &lg4ff_mode_switch_ext01_dfp; |
| 524 | /* DFP can only be switched to its native mode */ |
| 525 | default: |
| 526 | return NULL; |
| 527 | } |
| 528 | break; |
| 529 | case USB_DEVICE_ID_LOGITECH_G25_WHEEL: |
| 530 | switch (target_product_id) { |
| 531 | case USB_DEVICE_ID_LOGITECH_DFP_WHEEL: |
| 532 | return &lg4ff_mode_switch_ext01_dfp; |
| 533 | case USB_DEVICE_ID_LOGITECH_G25_WHEEL: |
| 534 | return &lg4ff_mode_switch_ext16_g25; |
| 535 | /* G25 can only be switched to DFP mode or its native mode */ |
| 536 | default: |
| 537 | return NULL; |
| 538 | } |
| 539 | break; |
| 540 | case USB_DEVICE_ID_LOGITECH_G27_WHEEL: |
| 541 | switch (target_product_id) { |
| 542 | case USB_DEVICE_ID_LOGITECH_WHEEL: |
| 543 | return &lg4ff_mode_switch_ext09_dfex; |
| 544 | case USB_DEVICE_ID_LOGITECH_DFP_WHEEL: |
| 545 | return &lg4ff_mode_switch_ext09_dfp; |
| 546 | case USB_DEVICE_ID_LOGITECH_G25_WHEEL: |
| 547 | return &lg4ff_mode_switch_ext09_g25; |
| 548 | case USB_DEVICE_ID_LOGITECH_G27_WHEEL: |
| 549 | return &lg4ff_mode_switch_ext09_g27; |
| 550 | /* G27 can only be switched to DF-EX, DFP, G25 or its native mode */ |
| 551 | default: |
| 552 | return NULL; |
| 553 | } |
| 554 | break; |
| 555 | case USB_DEVICE_ID_LOGITECH_DFGT_WHEEL: |
| 556 | switch (target_product_id) { |
| 557 | case USB_DEVICE_ID_LOGITECH_WHEEL: |
| 558 | return &lg4ff_mode_switch_ext09_dfex; |
| 559 | case USB_DEVICE_ID_LOGITECH_DFP_WHEEL: |
| 560 | return &lg4ff_mode_switch_ext09_dfp; |
| 561 | case USB_DEVICE_ID_LOGITECH_DFGT_WHEEL: |
| 562 | return &lg4ff_mode_switch_ext09_dfgt; |
| 563 | /* DFGT can only be switched to DF-EX, DFP or its native mode */ |
| 564 | default: |
| 565 | return NULL; |
| 566 | } |
| 567 | break; |
| 568 | /* No other wheels have multiple modes */ |
| 569 | default: |
| 570 | return NULL; |
| 571 | } |
| 572 | } |
| 573 | |
Michal Malý | e7c2344 | 2015-02-18 17:59:20 +0100 | [diff] [blame] | 574 | static int lg4ff_switch_compatibility_mode(struct hid_device *hid, const struct lg4ff_compat_mode_switch *s) |
Michal Malý | 96440c8 | 2011-08-04 16:18:11 +0200 | [diff] [blame] | 575 | { |
Michal Malý | c1740d1 | 2015-02-18 22:49:33 +0100 | [diff] [blame] | 576 | struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list; |
| 577 | struct hid_report *report = list_entry(report_list->next, struct hid_report, list); |
Michal Malý | 2a552c3 | 2015-04-08 22:56:39 +0200 | [diff] [blame] | 578 | s32 *value = report->field[0]->value; |
Michal Malý | e7c2344 | 2015-02-18 17:59:20 +0100 | [diff] [blame] | 579 | u8 i; |
Michal Malý | 96440c8 | 2011-08-04 16:18:11 +0200 | [diff] [blame] | 580 | |
Michal Malý | e7c2344 | 2015-02-18 17:59:20 +0100 | [diff] [blame] | 581 | for (i = 0; i < s->cmd_count; i++) { |
Michal Malý | c1740d1 | 2015-02-18 22:49:33 +0100 | [diff] [blame] | 582 | u8 j; |
Michal Malý | 96440c8 | 2011-08-04 16:18:11 +0200 | [diff] [blame] | 583 | |
Michal Malý | c1740d1 | 2015-02-18 22:49:33 +0100 | [diff] [blame] | 584 | for (j = 0; j < 7; j++) |
| 585 | value[j] = s->cmd[j + (7*i)]; |
| 586 | |
| 587 | hid_hw_request(hid, report, HID_REQ_SET_REPORT); |
Michal Malý | 96440c8 | 2011-08-04 16:18:11 +0200 | [diff] [blame] | 588 | } |
Michal Malý | c1740d1 | 2015-02-18 22:49:33 +0100 | [diff] [blame] | 589 | hid_hw_wait(hid); |
Michal Malý | e7c2344 | 2015-02-18 17:59:20 +0100 | [diff] [blame] | 590 | return 0; |
Michal Malý | 96440c8 | 2011-08-04 16:18:11 +0200 | [diff] [blame] | 591 | } |
Simon Wood | 32c88cb | 2010-09-22 13:19:42 +0200 | [diff] [blame] | 592 | |
Michal Malý | b96d23e | 2015-02-18 17:59:21 +0100 | [diff] [blame] | 593 | static ssize_t lg4ff_alternate_modes_show(struct device *dev, struct device_attribute *attr, char *buf) |
| 594 | { |
| 595 | struct hid_device *hid = to_hid_device(dev); |
| 596 | struct lg4ff_device_entry *entry; |
| 597 | struct lg_drv_data *drv_data; |
| 598 | ssize_t count = 0; |
| 599 | int i; |
| 600 | |
| 601 | drv_data = hid_get_drvdata(hid); |
| 602 | if (!drv_data) { |
| 603 | hid_err(hid, "Private driver data not found!\n"); |
| 604 | return 0; |
| 605 | } |
| 606 | |
| 607 | entry = drv_data->device_props; |
| 608 | if (!entry) { |
| 609 | hid_err(hid, "Device properties not found!\n"); |
| 610 | return 0; |
| 611 | } |
| 612 | |
Michal Malý | 72529c6 | 2015-04-08 22:56:46 +0200 | [diff] [blame] | 613 | if (!entry->wdata.real_name) { |
Michal Malý | b96d23e | 2015-02-18 17:59:21 +0100 | [diff] [blame] | 614 | hid_err(hid, "NULL pointer to string\n"); |
| 615 | return 0; |
| 616 | } |
| 617 | |
| 618 | for (i = 0; i < LG4FF_MODE_MAX_IDX; i++) { |
Michal Malý | 72529c6 | 2015-04-08 22:56:46 +0200 | [diff] [blame] | 619 | if (entry->wdata.alternate_modes & BIT(i)) { |
Michal Malý | b96d23e | 2015-02-18 17:59:21 +0100 | [diff] [blame] | 620 | /* Print tag and full name */ |
| 621 | count += scnprintf(buf + count, PAGE_SIZE - count, "%s: %s", |
| 622 | lg4ff_alternate_modes[i].tag, |
Michal Malý | 72529c6 | 2015-04-08 22:56:46 +0200 | [diff] [blame] | 623 | !lg4ff_alternate_modes[i].product_id ? entry->wdata.real_name : lg4ff_alternate_modes[i].name); |
Michal Malý | b96d23e | 2015-02-18 17:59:21 +0100 | [diff] [blame] | 624 | if (count >= PAGE_SIZE - 1) |
| 625 | return count; |
| 626 | |
| 627 | /* Mark the currently active mode with an asterisk */ |
Michal Malý | 72529c6 | 2015-04-08 22:56:46 +0200 | [diff] [blame] | 628 | if (lg4ff_alternate_modes[i].product_id == entry->wdata.product_id || |
| 629 | (lg4ff_alternate_modes[i].product_id == 0 && entry->wdata.product_id == entry->wdata.real_product_id)) |
Michal Malý | b96d23e | 2015-02-18 17:59:21 +0100 | [diff] [blame] | 630 | count += scnprintf(buf + count, PAGE_SIZE - count, " *\n"); |
| 631 | else |
| 632 | count += scnprintf(buf + count, PAGE_SIZE - count, "\n"); |
| 633 | |
| 634 | if (count >= PAGE_SIZE - 1) |
| 635 | return count; |
| 636 | } |
| 637 | } |
| 638 | |
| 639 | return count; |
| 640 | } |
| 641 | |
| 642 | static ssize_t lg4ff_alternate_modes_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) |
| 643 | { |
Michal Malý | f31a2de | 2015-02-18 17:59:23 +0100 | [diff] [blame] | 644 | struct hid_device *hid = to_hid_device(dev); |
| 645 | struct lg4ff_device_entry *entry; |
| 646 | struct lg_drv_data *drv_data; |
| 647 | const struct lg4ff_compat_mode_switch *s; |
| 648 | u16 target_product_id = 0; |
| 649 | int i, ret; |
| 650 | char *lbuf; |
| 651 | |
| 652 | drv_data = hid_get_drvdata(hid); |
| 653 | if (!drv_data) { |
| 654 | hid_err(hid, "Private driver data not found!\n"); |
| 655 | return -EINVAL; |
| 656 | } |
| 657 | |
| 658 | entry = drv_data->device_props; |
| 659 | if (!entry) { |
| 660 | hid_err(hid, "Device properties not found!\n"); |
| 661 | return -EINVAL; |
| 662 | } |
| 663 | |
| 664 | /* Allow \n at the end of the input parameter */ |
| 665 | lbuf = kasprintf(GFP_KERNEL, "%s", buf); |
| 666 | if (!lbuf) |
| 667 | return -ENOMEM; |
| 668 | |
| 669 | i = strlen(lbuf); |
| 670 | if (lbuf[i-1] == '\n') { |
| 671 | if (i == 1) { |
| 672 | kfree(lbuf); |
| 673 | return -EINVAL; |
| 674 | } |
| 675 | lbuf[i-1] = '\0'; |
| 676 | } |
| 677 | |
| 678 | for (i = 0; i < LG4FF_MODE_MAX_IDX; i++) { |
| 679 | const u16 mode_product_id = lg4ff_alternate_modes[i].product_id; |
| 680 | const char *tag = lg4ff_alternate_modes[i].tag; |
| 681 | |
Michal Malý | 72529c6 | 2015-04-08 22:56:46 +0200 | [diff] [blame] | 682 | if (entry->wdata.alternate_modes & BIT(i)) { |
Michal Malý | f31a2de | 2015-02-18 17:59:23 +0100 | [diff] [blame] | 683 | if (!strcmp(tag, lbuf)) { |
| 684 | if (!mode_product_id) |
Michal Malý | 72529c6 | 2015-04-08 22:56:46 +0200 | [diff] [blame] | 685 | target_product_id = entry->wdata.real_product_id; |
Michal Malý | f31a2de | 2015-02-18 17:59:23 +0100 | [diff] [blame] | 686 | else |
| 687 | target_product_id = mode_product_id; |
| 688 | break; |
| 689 | } |
| 690 | } |
| 691 | } |
| 692 | |
| 693 | if (i == LG4FF_MODE_MAX_IDX) { |
| 694 | hid_info(hid, "Requested mode \"%s\" is not supported by the device\n", lbuf); |
| 695 | kfree(lbuf); |
| 696 | return -EINVAL; |
| 697 | } |
| 698 | kfree(lbuf); /* Not needed anymore */ |
| 699 | |
Michal Malý | 72529c6 | 2015-04-08 22:56:46 +0200 | [diff] [blame] | 700 | if (target_product_id == entry->wdata.product_id) /* Nothing to do */ |
Michal Malý | f31a2de | 2015-02-18 17:59:23 +0100 | [diff] [blame] | 701 | return count; |
| 702 | |
| 703 | /* Automatic switching has to be disabled for the switch to DF-EX mode to work correctly */ |
| 704 | if (target_product_id == USB_DEVICE_ID_LOGITECH_WHEEL && !lg4ff_no_autoswitch) { |
| 705 | hid_info(hid, "\"%s\" cannot be switched to \"DF-EX\" mode. Load the \"hid_logitech\" module with \"lg4ff_no_autoswitch=1\" parameter set and try again\n", |
Michal Malý | 72529c6 | 2015-04-08 22:56:46 +0200 | [diff] [blame] | 706 | entry->wdata.real_name); |
Michal Malý | f31a2de | 2015-02-18 17:59:23 +0100 | [diff] [blame] | 707 | return -EINVAL; |
| 708 | } |
| 709 | |
| 710 | /* Take care of hardware limitations */ |
Michal Malý | 72529c6 | 2015-04-08 22:56:46 +0200 | [diff] [blame] | 711 | if ((entry->wdata.real_product_id == USB_DEVICE_ID_LOGITECH_DFP_WHEEL || entry->wdata.real_product_id == USB_DEVICE_ID_LOGITECH_G25_WHEEL) && |
| 712 | entry->wdata.product_id > target_product_id) { |
| 713 | hid_info(hid, "\"%s\" cannot be switched back into \"%s\" mode\n", entry->wdata.real_name, lg4ff_alternate_modes[i].name); |
Michal Malý | f31a2de | 2015-02-18 17:59:23 +0100 | [diff] [blame] | 714 | return -EINVAL; |
| 715 | } |
| 716 | |
Michal Malý | 72529c6 | 2015-04-08 22:56:46 +0200 | [diff] [blame] | 717 | s = lg4ff_get_mode_switch_command(entry->wdata.real_product_id, target_product_id); |
Michal Malý | f31a2de | 2015-02-18 17:59:23 +0100 | [diff] [blame] | 718 | if (!s) { |
| 719 | hid_err(hid, "Invalid target product ID %X\n", target_product_id); |
| 720 | return -EINVAL; |
| 721 | } |
| 722 | |
| 723 | ret = lg4ff_switch_compatibility_mode(hid, s); |
| 724 | return (ret == 0 ? count : ret); |
Michal Malý | b96d23e | 2015-02-18 17:59:21 +0100 | [diff] [blame] | 725 | } |
| 726 | static DEVICE_ATTR(alternate_modes, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH, lg4ff_alternate_modes_show, lg4ff_alternate_modes_store); |
| 727 | |
Michal Malý | fbf85e2 | 2015-04-08 22:56:41 +0200 | [diff] [blame] | 728 | /* Export the currently set range of the wheel */ |
| 729 | static ssize_t lg4ff_range_show(struct device *dev, struct device_attribute *attr, |
| 730 | char *buf) |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 731 | { |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 732 | struct hid_device *hid = to_hid_device(dev); |
Michal Malý | 3b6b17b | 2012-04-09 09:08:49 +0200 | [diff] [blame] | 733 | struct lg4ff_device_entry *entry; |
| 734 | struct lg_drv_data *drv_data; |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 735 | size_t count; |
| 736 | |
Michal Malý | 3b6b17b | 2012-04-09 09:08:49 +0200 | [diff] [blame] | 737 | drv_data = hid_get_drvdata(hid); |
| 738 | if (!drv_data) { |
| 739 | hid_err(hid, "Private driver data not found!\n"); |
| 740 | return 0; |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 741 | } |
Michal Malý | 3b6b17b | 2012-04-09 09:08:49 +0200 | [diff] [blame] | 742 | |
| 743 | entry = drv_data->device_props; |
| 744 | if (!entry) { |
| 745 | hid_err(hid, "Device properties not found!\n"); |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 746 | return 0; |
| 747 | } |
| 748 | |
Michal Malý | 72529c6 | 2015-04-08 22:56:46 +0200 | [diff] [blame] | 749 | count = scnprintf(buf, PAGE_SIZE, "%u\n", entry->wdata.range); |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 750 | return count; |
| 751 | } |
| 752 | |
| 753 | /* Set range to user specified value, call appropriate function |
| 754 | * according to the type of the wheel */ |
Michal Malý | fbf85e2 | 2015-04-08 22:56:41 +0200 | [diff] [blame] | 755 | static ssize_t lg4ff_range_store(struct device *dev, struct device_attribute *attr, |
| 756 | const char *buf, size_t count) |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 757 | { |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 758 | struct hid_device *hid = to_hid_device(dev); |
Michal Malý | 3b6b17b | 2012-04-09 09:08:49 +0200 | [diff] [blame] | 759 | struct lg4ff_device_entry *entry; |
| 760 | struct lg_drv_data *drv_data; |
Michal Malý | 2a552c3 | 2015-04-08 22:56:39 +0200 | [diff] [blame] | 761 | u16 range = simple_strtoul(buf, NULL, 10); |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 762 | |
Michal Malý | 3b6b17b | 2012-04-09 09:08:49 +0200 | [diff] [blame] | 763 | drv_data = hid_get_drvdata(hid); |
| 764 | if (!drv_data) { |
| 765 | hid_err(hid, "Private driver data not found!\n"); |
Simon Wood | 29ff665 | 2014-08-14 20:43:01 -0600 | [diff] [blame] | 766 | return -EINVAL; |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 767 | } |
Michal Malý | 3b6b17b | 2012-04-09 09:08:49 +0200 | [diff] [blame] | 768 | |
| 769 | entry = drv_data->device_props; |
| 770 | if (!entry) { |
| 771 | hid_err(hid, "Device properties not found!\n"); |
Simon Wood | 29ff665 | 2014-08-14 20:43:01 -0600 | [diff] [blame] | 772 | return -EINVAL; |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 773 | } |
| 774 | |
| 775 | if (range == 0) |
Michal Malý | 72529c6 | 2015-04-08 22:56:46 +0200 | [diff] [blame] | 776 | range = entry->wdata.max_range; |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 777 | |
| 778 | /* Check if the wheel supports range setting |
| 779 | * and that the range is within limits for the wheel */ |
Michal Malý | 72529c6 | 2015-04-08 22:56:46 +0200 | [diff] [blame] | 780 | if (entry->wdata.set_range && range >= entry->wdata.min_range && range <= entry->wdata.max_range) { |
| 781 | entry->wdata.set_range(hid, range); |
| 782 | entry->wdata.range = range; |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 783 | } |
| 784 | |
| 785 | return count; |
| 786 | } |
Michal Malý | fbf85e2 | 2015-04-08 22:56:41 +0200 | [diff] [blame] | 787 | static DEVICE_ATTR(range, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH, lg4ff_range_show, lg4ff_range_store); |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 788 | |
Michal Malý | b96d23e | 2015-02-18 17:59:21 +0100 | [diff] [blame] | 789 | static ssize_t lg4ff_real_id_show(struct device *dev, struct device_attribute *attr, char *buf) |
| 790 | { |
| 791 | struct hid_device *hid = to_hid_device(dev); |
| 792 | struct lg4ff_device_entry *entry; |
| 793 | struct lg_drv_data *drv_data; |
| 794 | size_t count; |
| 795 | |
| 796 | drv_data = hid_get_drvdata(hid); |
| 797 | if (!drv_data) { |
| 798 | hid_err(hid, "Private driver data not found!\n"); |
| 799 | return 0; |
| 800 | } |
| 801 | |
| 802 | entry = drv_data->device_props; |
| 803 | if (!entry) { |
| 804 | hid_err(hid, "Device properties not found!\n"); |
| 805 | return 0; |
| 806 | } |
| 807 | |
Michal Malý | 72529c6 | 2015-04-08 22:56:46 +0200 | [diff] [blame] | 808 | if (!entry->wdata.real_tag || !entry->wdata.real_name) { |
Michal Malý | b96d23e | 2015-02-18 17:59:21 +0100 | [diff] [blame] | 809 | hid_err(hid, "NULL pointer to string\n"); |
| 810 | return 0; |
| 811 | } |
| 812 | |
Michal Malý | 72529c6 | 2015-04-08 22:56:46 +0200 | [diff] [blame] | 813 | count = scnprintf(buf, PAGE_SIZE, "%s: %s\n", entry->wdata.real_tag, entry->wdata.real_name); |
Michal Malý | b96d23e | 2015-02-18 17:59:21 +0100 | [diff] [blame] | 814 | return count; |
| 815 | } |
| 816 | |
| 817 | static ssize_t lg4ff_real_id_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) |
| 818 | { |
| 819 | /* Real ID is a read-only value */ |
| 820 | return -EPERM; |
| 821 | } |
| 822 | static DEVICE_ATTR(real_id, S_IRUGO, lg4ff_real_id_show, lg4ff_real_id_store); |
| 823 | |
Simon Wood | 22bcefd | 2012-04-21 05:41:15 -0700 | [diff] [blame] | 824 | #ifdef CONFIG_LEDS_CLASS |
Michal Malý | 2a552c3 | 2015-04-08 22:56:39 +0200 | [diff] [blame] | 825 | static void lg4ff_set_leds(struct hid_device *hid, u8 leds) |
Simon Wood | 22bcefd | 2012-04-21 05:41:15 -0700 | [diff] [blame] | 826 | { |
| 827 | struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list; |
| 828 | struct hid_report *report = list_entry(report_list->next, struct hid_report, list); |
Michal Malý | 2a552c3 | 2015-04-08 22:56:39 +0200 | [diff] [blame] | 829 | s32 *value = report->field[0]->value; |
Simon Wood | 22bcefd | 2012-04-21 05:41:15 -0700 | [diff] [blame] | 830 | |
Michal Malý | 74479ba | 2012-09-24 01:09:30 +0200 | [diff] [blame] | 831 | value[0] = 0xf8; |
| 832 | value[1] = 0x12; |
| 833 | value[2] = leds; |
| 834 | value[3] = 0x00; |
| 835 | value[4] = 0x00; |
| 836 | value[5] = 0x00; |
| 837 | value[6] = 0x00; |
Benjamin Tissoires | d8814272 | 2013-02-25 11:31:46 +0100 | [diff] [blame] | 838 | hid_hw_request(hid, report, HID_REQ_SET_REPORT); |
Simon Wood | 22bcefd | 2012-04-21 05:41:15 -0700 | [diff] [blame] | 839 | } |
| 840 | |
| 841 | static void lg4ff_led_set_brightness(struct led_classdev *led_cdev, |
| 842 | enum led_brightness value) |
| 843 | { |
| 844 | struct device *dev = led_cdev->dev->parent; |
| 845 | struct hid_device *hid = container_of(dev, struct hid_device, dev); |
Axel Lin | 4629fd1 | 2012-09-13 14:09:33 +0800 | [diff] [blame] | 846 | struct lg_drv_data *drv_data = hid_get_drvdata(hid); |
Simon Wood | 22bcefd | 2012-04-21 05:41:15 -0700 | [diff] [blame] | 847 | struct lg4ff_device_entry *entry; |
| 848 | int i, state = 0; |
| 849 | |
| 850 | if (!drv_data) { |
| 851 | hid_err(hid, "Device data not found."); |
| 852 | return; |
| 853 | } |
| 854 | |
Michal Malý | 371a1d9 | 2015-04-08 22:56:43 +0200 | [diff] [blame] | 855 | entry = drv_data->device_props; |
Simon Wood | 22bcefd | 2012-04-21 05:41:15 -0700 | [diff] [blame] | 856 | |
| 857 | if (!entry) { |
| 858 | hid_err(hid, "Device properties not found."); |
| 859 | return; |
| 860 | } |
| 861 | |
| 862 | for (i = 0; i < 5; i++) { |
Michal Malý | 72529c6 | 2015-04-08 22:56:46 +0200 | [diff] [blame] | 863 | if (led_cdev != entry->wdata.led[i]) |
Simon Wood | 22bcefd | 2012-04-21 05:41:15 -0700 | [diff] [blame] | 864 | continue; |
Michal Malý | 72529c6 | 2015-04-08 22:56:46 +0200 | [diff] [blame] | 865 | state = (entry->wdata.led_state >> i) & 1; |
Simon Wood | 22bcefd | 2012-04-21 05:41:15 -0700 | [diff] [blame] | 866 | if (value == LED_OFF && state) { |
Michal Malý | 72529c6 | 2015-04-08 22:56:46 +0200 | [diff] [blame] | 867 | entry->wdata.led_state &= ~(1 << i); |
| 868 | lg4ff_set_leds(hid, entry->wdata.led_state); |
Simon Wood | 22bcefd | 2012-04-21 05:41:15 -0700 | [diff] [blame] | 869 | } else if (value != LED_OFF && !state) { |
Michal Malý | 72529c6 | 2015-04-08 22:56:46 +0200 | [diff] [blame] | 870 | entry->wdata.led_state |= 1 << i; |
| 871 | lg4ff_set_leds(hid, entry->wdata.led_state); |
Simon Wood | 22bcefd | 2012-04-21 05:41:15 -0700 | [diff] [blame] | 872 | } |
| 873 | break; |
| 874 | } |
| 875 | } |
| 876 | |
| 877 | static enum led_brightness lg4ff_led_get_brightness(struct led_classdev *led_cdev) |
| 878 | { |
| 879 | struct device *dev = led_cdev->dev->parent; |
| 880 | struct hid_device *hid = container_of(dev, struct hid_device, dev); |
Axel Lin | 4629fd1 | 2012-09-13 14:09:33 +0800 | [diff] [blame] | 881 | struct lg_drv_data *drv_data = hid_get_drvdata(hid); |
Simon Wood | 22bcefd | 2012-04-21 05:41:15 -0700 | [diff] [blame] | 882 | struct lg4ff_device_entry *entry; |
| 883 | int i, value = 0; |
| 884 | |
| 885 | if (!drv_data) { |
| 886 | hid_err(hid, "Device data not found."); |
| 887 | return LED_OFF; |
| 888 | } |
| 889 | |
Michal Malý | 371a1d9 | 2015-04-08 22:56:43 +0200 | [diff] [blame] | 890 | entry = drv_data->device_props; |
Simon Wood | 22bcefd | 2012-04-21 05:41:15 -0700 | [diff] [blame] | 891 | |
| 892 | if (!entry) { |
| 893 | hid_err(hid, "Device properties not found."); |
| 894 | return LED_OFF; |
| 895 | } |
| 896 | |
| 897 | for (i = 0; i < 5; i++) |
Michal Malý | 72529c6 | 2015-04-08 22:56:46 +0200 | [diff] [blame] | 898 | if (led_cdev == entry->wdata.led[i]) { |
| 899 | value = (entry->wdata.led_state >> i) & 1; |
Simon Wood | 22bcefd | 2012-04-21 05:41:15 -0700 | [diff] [blame] | 900 | break; |
| 901 | } |
| 902 | |
| 903 | return value ? LED_FULL : LED_OFF; |
| 904 | } |
| 905 | #endif |
| 906 | |
Michal Malý | e7c2344 | 2015-02-18 17:59:20 +0100 | [diff] [blame] | 907 | static u16 lg4ff_identify_multimode_wheel(struct hid_device *hid, const u16 reported_product_id, const u16 bcdDevice) |
| 908 | { |
| 909 | const struct lg4ff_wheel_ident_checklist *checklist; |
| 910 | int i, from_idx, to_idx; |
| 911 | |
| 912 | switch (reported_product_id) { |
| 913 | case USB_DEVICE_ID_LOGITECH_WHEEL: |
| 914 | case USB_DEVICE_ID_LOGITECH_DFP_WHEEL: |
| 915 | checklist = &lg4ff_main_checklist; |
| 916 | from_idx = 0; |
| 917 | to_idx = checklist->count - 1; |
| 918 | break; |
| 919 | case USB_DEVICE_ID_LOGITECH_G25_WHEEL: |
| 920 | checklist = &lg4ff_main_checklist; |
| 921 | from_idx = 0; |
| 922 | to_idx = checklist->count - 2; /* End identity check at G25 */ |
| 923 | break; |
| 924 | case USB_DEVICE_ID_LOGITECH_G27_WHEEL: |
| 925 | checklist = &lg4ff_main_checklist; |
| 926 | from_idx = 1; /* Start identity check at G27 */ |
| 927 | to_idx = checklist->count - 3; /* End identity check at G27 */ |
| 928 | break; |
| 929 | case USB_DEVICE_ID_LOGITECH_DFGT_WHEEL: |
| 930 | checklist = &lg4ff_main_checklist; |
| 931 | from_idx = 0; |
| 932 | to_idx = checklist->count - 4; /* End identity check at DFGT */ |
| 933 | break; |
| 934 | default: |
| 935 | return 0; |
| 936 | } |
| 937 | |
| 938 | for (i = from_idx; i <= to_idx; i++) { |
| 939 | const u16 mask = checklist->models[i]->mask; |
| 940 | const u16 result = checklist->models[i]->result; |
| 941 | const u16 real_product_id = checklist->models[i]->real_product_id; |
| 942 | |
| 943 | if ((bcdDevice & mask) == result) { |
| 944 | dbg_hid("Found wheel with real PID %X whose reported PID is %X\n", real_product_id, reported_product_id); |
| 945 | return real_product_id; |
| 946 | } |
| 947 | } |
| 948 | |
Michal Malý | f31a2de | 2015-02-18 17:59:23 +0100 | [diff] [blame] | 949 | /* No match found. This is either Driving Force or an unknown |
| 950 | * wheel model, do not touch it */ |
Michal Malý | e7c2344 | 2015-02-18 17:59:20 +0100 | [diff] [blame] | 951 | dbg_hid("Wheel with bcdDevice %X was not recognized as multimode wheel, leaving in its current mode\n", bcdDevice); |
| 952 | return 0; |
| 953 | } |
| 954 | |
| 955 | static int lg4ff_handle_multimode_wheel(struct hid_device *hid, u16 *real_product_id, const u16 bcdDevice) |
| 956 | { |
| 957 | const u16 reported_product_id = hid->product; |
| 958 | int ret; |
| 959 | |
| 960 | *real_product_id = lg4ff_identify_multimode_wheel(hid, reported_product_id, bcdDevice); |
| 961 | /* Probed wheel is not a multimode wheel */ |
| 962 | if (!*real_product_id) { |
| 963 | *real_product_id = reported_product_id; |
| 964 | dbg_hid("Wheel is not a multimode wheel\n"); |
| 965 | return LG4FF_MMODE_NOT_MULTIMODE; |
| 966 | } |
| 967 | |
| 968 | /* Switch from "Driving Force" mode to native mode automatically. |
| 969 | * Otherwise keep the wheel in its current mode */ |
| 970 | if (reported_product_id == USB_DEVICE_ID_LOGITECH_WHEEL && |
Michal Malý | a54dc779 | 2015-02-18 17:59:22 +0100 | [diff] [blame] | 971 | reported_product_id != *real_product_id && |
| 972 | !lg4ff_no_autoswitch) { |
Michal Malý | f31a2de | 2015-02-18 17:59:23 +0100 | [diff] [blame] | 973 | const struct lg4ff_compat_mode_switch *s = lg4ff_get_mode_switch_command(*real_product_id, *real_product_id); |
Michal Malý | e7c2344 | 2015-02-18 17:59:20 +0100 | [diff] [blame] | 974 | |
Michal Malý | f31a2de | 2015-02-18 17:59:23 +0100 | [diff] [blame] | 975 | if (!s) { |
Michal Malý | e7c2344 | 2015-02-18 17:59:20 +0100 | [diff] [blame] | 976 | hid_err(hid, "Invalid product id %X\n", *real_product_id); |
Michal Malý | b96d23e | 2015-02-18 17:59:21 +0100 | [diff] [blame] | 977 | return LG4FF_MMODE_NOT_MULTIMODE; |
Michal Malý | e7c2344 | 2015-02-18 17:59:20 +0100 | [diff] [blame] | 978 | } |
| 979 | |
| 980 | ret = lg4ff_switch_compatibility_mode(hid, s); |
| 981 | if (ret) { |
| 982 | /* Wheel could not have been switched to native mode, |
| 983 | * leave it in "Driving Force" mode and continue */ |
| 984 | hid_err(hid, "Unable to switch wheel mode, errno %d\n", ret); |
Michal Malý | b96d23e | 2015-02-18 17:59:21 +0100 | [diff] [blame] | 985 | return LG4FF_MMODE_IS_MULTIMODE; |
Michal Malý | e7c2344 | 2015-02-18 17:59:20 +0100 | [diff] [blame] | 986 | } |
| 987 | return LG4FF_MMODE_SWITCHED; |
| 988 | } |
| 989 | |
Michal Malý | b96d23e | 2015-02-18 17:59:21 +0100 | [diff] [blame] | 990 | return LG4FF_MMODE_IS_MULTIMODE; |
Michal Malý | e7c2344 | 2015-02-18 17:59:20 +0100 | [diff] [blame] | 991 | } |
| 992 | |
| 993 | |
Simon Wood | 32c88cb | 2010-09-22 13:19:42 +0200 | [diff] [blame] | 994 | int lg4ff_init(struct hid_device *hid) |
| 995 | { |
| 996 | 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] | 997 | struct input_dev *dev = hidinput->input; |
Michal Malý | e7c2344 | 2015-02-18 17:59:20 +0100 | [diff] [blame] | 998 | const struct usb_device_descriptor *udesc = &(hid_to_usb_dev(hid)->descriptor); |
| 999 | const u16 bcdDevice = le16_to_cpu(udesc->bcdDevice); |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 1000 | struct lg4ff_device_entry *entry; |
Michal Malý | 3b6b17b | 2012-04-09 09:08:49 +0200 | [diff] [blame] | 1001 | struct lg_drv_data *drv_data; |
Michal Malý | b96d23e | 2015-02-18 17:59:21 +0100 | [diff] [blame] | 1002 | int error, i, j; |
| 1003 | int mmode_ret, mmode_idx = -1; |
Michal Malý | e7c2344 | 2015-02-18 17:59:20 +0100 | [diff] [blame] | 1004 | u16 real_product_id; |
Simon Wood | 32c88cb | 2010-09-22 13:19:42 +0200 | [diff] [blame] | 1005 | |
Simon Wood | 32c88cb | 2010-09-22 13:19:42 +0200 | [diff] [blame] | 1006 | /* Check that the report looks ok */ |
Kees Cook | 0fb6bd0 | 2013-09-11 21:56:54 +0200 | [diff] [blame] | 1007 | if (!hid_validate_values(hid, HID_OUTPUT_REPORT, 0, 0, 7)) |
Simon Wood | 32c88cb | 2010-09-22 13:19:42 +0200 | [diff] [blame] | 1008 | return -1; |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 1009 | |
Michal Malý | 72529c6 | 2015-04-08 22:56:46 +0200 | [diff] [blame] | 1010 | drv_data = hid_get_drvdata(hid); |
| 1011 | if (!drv_data) { |
| 1012 | hid_err(hid, "Cannot add device, private driver data not allocated\n"); |
| 1013 | return -1; |
| 1014 | } |
| 1015 | entry = kzalloc(sizeof(*entry), GFP_KERNEL); |
| 1016 | if (!entry) |
| 1017 | return -ENOMEM; |
| 1018 | drv_data->device_props = entry; |
| 1019 | |
Michal Malý | e7c2344 | 2015-02-18 17:59:20 +0100 | [diff] [blame] | 1020 | /* Check if a multimode wheel has been connected and |
| 1021 | * handle it appropriately */ |
Michal Malý | b96d23e | 2015-02-18 17:59:21 +0100 | [diff] [blame] | 1022 | mmode_ret = lg4ff_handle_multimode_wheel(hid, &real_product_id, bcdDevice); |
Michal Malý | e7c2344 | 2015-02-18 17:59:20 +0100 | [diff] [blame] | 1023 | |
| 1024 | /* Wheel has been told to switch to native mode. There is no point in going on |
| 1025 | * with the initialization as the wheel will do a USB reset when it switches mode |
| 1026 | */ |
Michal Malý | b96d23e | 2015-02-18 17:59:21 +0100 | [diff] [blame] | 1027 | if (mmode_ret == LG4FF_MMODE_SWITCHED) |
Michal Malý | e7c2344 | 2015-02-18 17:59:20 +0100 | [diff] [blame] | 1028 | return 0; |
Michal Malý | 72529c6 | 2015-04-08 22:56:46 +0200 | [diff] [blame] | 1029 | else if (mmode_ret < 0) { |
| 1030 | hid_err(hid, "Unable to switch device mode during initialization, errno %d\n", mmode_ret); |
| 1031 | error = mmode_ret; |
| 1032 | goto err_init; |
| 1033 | } |
Michal Malý | e7c2344 | 2015-02-18 17:59:20 +0100 | [diff] [blame] | 1034 | |
Michal Malý | 7362cd2 | 2011-08-04 16:16:09 +0200 | [diff] [blame] | 1035 | /* Check what wheel has been connected */ |
| 1036 | for (i = 0; i < ARRAY_SIZE(lg4ff_devices); i++) { |
| 1037 | if (hid->product == lg4ff_devices[i].product_id) { |
| 1038 | dbg_hid("Found compatible device, product ID %04X\n", lg4ff_devices[i].product_id); |
| 1039 | break; |
| 1040 | } |
| 1041 | } |
Simon Wood | 32c88cb | 2010-09-22 13:19:42 +0200 | [diff] [blame] | 1042 | |
Michal Malý | 7362cd2 | 2011-08-04 16:16:09 +0200 | [diff] [blame] | 1043 | if (i == ARRAY_SIZE(lg4ff_devices)) { |
Michal Malý | 9c2a6bd | 2015-04-08 22:56:44 +0200 | [diff] [blame] | 1044 | hid_err(hid, "This device is flagged to be handled by the lg4ff module but this module does not know how to handle it. " |
| 1045 | "Please report this as a bug to LKML, Simon Wood <simon@mungewell.org> or " |
| 1046 | "Michal Maly <madcatxster@devoid-pointer.net>\n"); |
Michal Malý | 72529c6 | 2015-04-08 22:56:46 +0200 | [diff] [blame] | 1047 | error = -1; |
| 1048 | goto err_init; |
Michal Malý | 7362cd2 | 2011-08-04 16:16:09 +0200 | [diff] [blame] | 1049 | } |
| 1050 | |
Michal Malý | b96d23e | 2015-02-18 17:59:21 +0100 | [diff] [blame] | 1051 | if (mmode_ret == LG4FF_MMODE_IS_MULTIMODE) { |
| 1052 | for (mmode_idx = 0; mmode_idx < ARRAY_SIZE(lg4ff_multimode_wheels); mmode_idx++) { |
| 1053 | if (real_product_id == lg4ff_multimode_wheels[mmode_idx].product_id) |
| 1054 | break; |
| 1055 | } |
| 1056 | |
| 1057 | if (mmode_idx == ARRAY_SIZE(lg4ff_multimode_wheels)) { |
| 1058 | hid_err(hid, "Device product ID %X is not listed as a multimode wheel", real_product_id); |
Michal Malý | 72529c6 | 2015-04-08 22:56:46 +0200 | [diff] [blame] | 1059 | error = -1; |
| 1060 | goto err_init; |
Michal Malý | b96d23e | 2015-02-18 17:59:21 +0100 | [diff] [blame] | 1061 | } |
| 1062 | } |
| 1063 | |
Michal Malý | 7362cd2 | 2011-08-04 16:16:09 +0200 | [diff] [blame] | 1064 | /* Set supported force feedback capabilities */ |
| 1065 | for (j = 0; lg4ff_devices[i].ff_effects[j] >= 0; j++) |
| 1066 | set_bit(lg4ff_devices[i].ff_effects[j], dev->ffbit); |
Simon Wood | 32c88cb | 2010-09-22 13:19:42 +0200 | [diff] [blame] | 1067 | |
Michal Malý | d0afd8489 | 2015-04-08 22:56:40 +0200 | [diff] [blame] | 1068 | error = input_ff_create_memless(dev, NULL, lg4ff_play); |
Simon Wood | 32c88cb | 2010-09-22 13:19:42 +0200 | [diff] [blame] | 1069 | |
| 1070 | if (error) |
Michal Malý | 72529c6 | 2015-04-08 22:56:46 +0200 | [diff] [blame] | 1071 | goto err_init; |
Simon Wood | 32c88cb | 2010-09-22 13:19:42 +0200 | [diff] [blame] | 1072 | |
Michal Malý | 72529c6 | 2015-04-08 22:56:46 +0200 | [diff] [blame] | 1073 | entry->wdata.product_id = lg4ff_devices[i].product_id; |
| 1074 | entry->wdata.real_product_id = real_product_id; |
| 1075 | entry->wdata.min_range = lg4ff_devices[i].min_range; |
| 1076 | entry->wdata.max_range = lg4ff_devices[i].max_range; |
| 1077 | entry->wdata.set_range = lg4ff_devices[i].set_range; |
Michal Malý | b96d23e | 2015-02-18 17:59:21 +0100 | [diff] [blame] | 1078 | if (mmode_ret == LG4FF_MMODE_IS_MULTIMODE) { |
| 1079 | BUG_ON(mmode_idx == -1); |
Michal Malý | 72529c6 | 2015-04-08 22:56:46 +0200 | [diff] [blame] | 1080 | entry->wdata.alternate_modes = lg4ff_multimode_wheels[mmode_idx].alternate_modes; |
| 1081 | entry->wdata.real_tag = lg4ff_multimode_wheels[mmode_idx].real_tag; |
| 1082 | entry->wdata.real_name = lg4ff_multimode_wheels[mmode_idx].real_name; |
Michal Malý | b96d23e | 2015-02-18 17:59:21 +0100 | [diff] [blame] | 1083 | } |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 1084 | |
Simon Wood | 114a55c | 2013-11-06 12:30:43 -0700 | [diff] [blame] | 1085 | /* Check if autocentering is available and |
| 1086 | * set the centering force to zero by default */ |
| 1087 | if (test_bit(FF_AUTOCENTER, dev->ffbit)) { |
Michal Malý | e7c2344 | 2015-02-18 17:59:20 +0100 | [diff] [blame] | 1088 | /* Formula Force EX expects different autocentering command */ |
| 1089 | if ((bcdDevice >> 8) == LG4FF_FFEX_REV_MAJ && |
| 1090 | (bcdDevice & 0xff) == LG4FF_FFEX_REV_MIN) |
Michal Malý | d0afd8489 | 2015-04-08 22:56:40 +0200 | [diff] [blame] | 1091 | dev->ff->set_autocenter = lg4ff_set_autocenter_ffex; |
Simon Wood | 114a55c | 2013-11-06 12:30:43 -0700 | [diff] [blame] | 1092 | else |
Michal Malý | d0afd8489 | 2015-04-08 22:56:40 +0200 | [diff] [blame] | 1093 | dev->ff->set_autocenter = lg4ff_set_autocenter_default; |
Simon Wood | 114a55c | 2013-11-06 12:30:43 -0700 | [diff] [blame] | 1094 | |
| 1095 | dev->ff->set_autocenter(dev, 0); |
| 1096 | } |
| 1097 | |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 1098 | /* Create sysfs interface */ |
| 1099 | error = device_create_file(&hid->dev, &dev_attr_range); |
| 1100 | if (error) |
Michal Malý | 72529c6 | 2015-04-08 22:56:46 +0200 | [diff] [blame] | 1101 | goto err_init; |
Michal Malý | b96d23e | 2015-02-18 17:59:21 +0100 | [diff] [blame] | 1102 | if (mmode_ret == LG4FF_MMODE_IS_MULTIMODE) { |
| 1103 | error = device_create_file(&hid->dev, &dev_attr_real_id); |
| 1104 | if (error) |
Michal Malý | 72529c6 | 2015-04-08 22:56:46 +0200 | [diff] [blame] | 1105 | goto err_init; |
| 1106 | |
Michal Malý | b96d23e | 2015-02-18 17:59:21 +0100 | [diff] [blame] | 1107 | error = device_create_file(&hid->dev, &dev_attr_alternate_modes); |
| 1108 | if (error) |
Michal Malý | 72529c6 | 2015-04-08 22:56:46 +0200 | [diff] [blame] | 1109 | goto err_init; |
Michal Malý | b96d23e | 2015-02-18 17:59:21 +0100 | [diff] [blame] | 1110 | } |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 1111 | dbg_hid("sysfs interface created\n"); |
| 1112 | |
| 1113 | /* Set the maximum range to start with */ |
Michal Malý | 72529c6 | 2015-04-08 22:56:46 +0200 | [diff] [blame] | 1114 | entry->wdata.range = entry->wdata.max_range; |
| 1115 | if (entry->wdata.set_range) |
| 1116 | entry->wdata.set_range(hid, entry->wdata.range); |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 1117 | |
Simon Wood | 22bcefd | 2012-04-21 05:41:15 -0700 | [diff] [blame] | 1118 | #ifdef CONFIG_LEDS_CLASS |
| 1119 | /* register led subsystem - G27 only */ |
Michal Malý | 72529c6 | 2015-04-08 22:56:46 +0200 | [diff] [blame] | 1120 | entry->wdata.led_state = 0; |
Simon Wood | 22bcefd | 2012-04-21 05:41:15 -0700 | [diff] [blame] | 1121 | for (j = 0; j < 5; j++) |
Michal Malý | 72529c6 | 2015-04-08 22:56:46 +0200 | [diff] [blame] | 1122 | entry->wdata.led[j] = NULL; |
Simon Wood | 22bcefd | 2012-04-21 05:41:15 -0700 | [diff] [blame] | 1123 | |
| 1124 | if (lg4ff_devices[i].product_id == USB_DEVICE_ID_LOGITECH_G27_WHEEL) { |
| 1125 | struct led_classdev *led; |
| 1126 | size_t name_sz; |
| 1127 | char *name; |
| 1128 | |
| 1129 | lg4ff_set_leds(hid, 0); |
| 1130 | |
| 1131 | name_sz = strlen(dev_name(&hid->dev)) + 8; |
| 1132 | |
| 1133 | for (j = 0; j < 5; j++) { |
| 1134 | led = kzalloc(sizeof(struct led_classdev)+name_sz, GFP_KERNEL); |
| 1135 | if (!led) { |
| 1136 | hid_err(hid, "can't allocate memory for LED %d\n", j); |
Michal Malý | 72529c6 | 2015-04-08 22:56:46 +0200 | [diff] [blame] | 1137 | goto err_leds; |
Simon Wood | 22bcefd | 2012-04-21 05:41:15 -0700 | [diff] [blame] | 1138 | } |
| 1139 | |
| 1140 | name = (void *)(&led[1]); |
| 1141 | snprintf(name, name_sz, "%s::RPM%d", dev_name(&hid->dev), j+1); |
| 1142 | led->name = name; |
| 1143 | led->brightness = 0; |
| 1144 | led->max_brightness = 1; |
| 1145 | led->brightness_get = lg4ff_led_get_brightness; |
| 1146 | led->brightness_set = lg4ff_led_set_brightness; |
| 1147 | |
Michal Malý | 72529c6 | 2015-04-08 22:56:46 +0200 | [diff] [blame] | 1148 | entry->wdata.led[j] = led; |
Simon Wood | 22bcefd | 2012-04-21 05:41:15 -0700 | [diff] [blame] | 1149 | error = led_classdev_register(&hid->dev, led); |
| 1150 | |
| 1151 | if (error) { |
| 1152 | hid_err(hid, "failed to register LED %d. Aborting.\n", j); |
Michal Malý | 72529c6 | 2015-04-08 22:56:46 +0200 | [diff] [blame] | 1153 | err_leds: |
Simon Wood | 22bcefd | 2012-04-21 05:41:15 -0700 | [diff] [blame] | 1154 | /* Deregister LEDs (if any) */ |
| 1155 | for (j = 0; j < 5; j++) { |
Michal Malý | 72529c6 | 2015-04-08 22:56:46 +0200 | [diff] [blame] | 1156 | led = entry->wdata.led[j]; |
| 1157 | entry->wdata.led[j] = NULL; |
Simon Wood | 22bcefd | 2012-04-21 05:41:15 -0700 | [diff] [blame] | 1158 | if (!led) |
| 1159 | continue; |
| 1160 | led_classdev_unregister(led); |
| 1161 | kfree(led); |
| 1162 | } |
| 1163 | goto out; /* Let the driver continue without LEDs */ |
| 1164 | } |
| 1165 | } |
| 1166 | } |
Simon Wood | 22bcefd | 2012-04-21 05:41:15 -0700 | [diff] [blame] | 1167 | out: |
Jiri Kosina | c6e6dc8 | 2012-04-23 21:08:15 +0200 | [diff] [blame] | 1168 | #endif |
Simon Wood | 64013800 | 2012-04-21 05:41:16 -0700 | [diff] [blame] | 1169 | hid_info(hid, "Force feedback support for Logitech Gaming Wheels\n"); |
Simon Wood | 32c88cb | 2010-09-22 13:19:42 +0200 | [diff] [blame] | 1170 | return 0; |
Michal Malý | 72529c6 | 2015-04-08 22:56:46 +0200 | [diff] [blame] | 1171 | |
| 1172 | err_init: |
| 1173 | drv_data->device_props = NULL; |
| 1174 | kfree(entry); |
| 1175 | return error; |
Simon Wood | 32c88cb | 2010-09-22 13:19:42 +0200 | [diff] [blame] | 1176 | } |
| 1177 | |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 1178 | int lg4ff_deinit(struct hid_device *hid) |
| 1179 | { |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 1180 | struct lg4ff_device_entry *entry; |
Michal Malý | 3b6b17b | 2012-04-09 09:08:49 +0200 | [diff] [blame] | 1181 | struct lg_drv_data *drv_data; |
| 1182 | |
Michal Malý | 3b6b17b | 2012-04-09 09:08:49 +0200 | [diff] [blame] | 1183 | drv_data = hid_get_drvdata(hid); |
| 1184 | if (!drv_data) { |
| 1185 | hid_err(hid, "Error while deinitializing device, no private driver data.\n"); |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 1186 | return -1; |
| 1187 | } |
Michal Malý | 3b6b17b | 2012-04-09 09:08:49 +0200 | [diff] [blame] | 1188 | entry = drv_data->device_props; |
Michal Malý | e7c2344 | 2015-02-18 17:59:20 +0100 | [diff] [blame] | 1189 | if (!entry) |
| 1190 | goto out; /* Nothing more to do */ |
| 1191 | |
Michal Malý | b96d23e | 2015-02-18 17:59:21 +0100 | [diff] [blame] | 1192 | /* Multimode devices will have at least the "MODE_NATIVE" bit set */ |
Michal Malý | 72529c6 | 2015-04-08 22:56:46 +0200 | [diff] [blame] | 1193 | if (entry->wdata.alternate_modes) { |
Michal Malý | b96d23e | 2015-02-18 17:59:21 +0100 | [diff] [blame] | 1194 | device_remove_file(&hid->dev, &dev_attr_real_id); |
| 1195 | device_remove_file(&hid->dev, &dev_attr_alternate_modes); |
| 1196 | } |
| 1197 | |
Michal Malý | 72529c6 | 2015-04-08 22:56:46 +0200 | [diff] [blame] | 1198 | device_remove_file(&hid->dev, &dev_attr_range); |
Simon Wood | 22bcefd | 2012-04-21 05:41:15 -0700 | [diff] [blame] | 1199 | #ifdef CONFIG_LEDS_CLASS |
| 1200 | { |
| 1201 | int j; |
| 1202 | struct led_classdev *led; |
| 1203 | |
| 1204 | /* Deregister LEDs (if any) */ |
| 1205 | for (j = 0; j < 5; j++) { |
| 1206 | |
Michal Malý | 72529c6 | 2015-04-08 22:56:46 +0200 | [diff] [blame] | 1207 | led = entry->wdata.led[j]; |
| 1208 | entry->wdata.led[j] = NULL; |
Simon Wood | 22bcefd | 2012-04-21 05:41:15 -0700 | [diff] [blame] | 1209 | if (!led) |
| 1210 | continue; |
| 1211 | led_classdev_unregister(led); |
| 1212 | kfree(led); |
| 1213 | } |
| 1214 | } |
| 1215 | #endif |
Michal Malý | b211a63 | 2015-04-08 22:56:47 +0200 | [diff] [blame^] | 1216 | hid_hw_stop(hid); |
| 1217 | drv_data->device_props = NULL; |
Simon Wood | 22bcefd | 2012-04-21 05:41:15 -0700 | [diff] [blame] | 1218 | |
Michal Malý | 3b6b17b | 2012-04-09 09:08:49 +0200 | [diff] [blame] | 1219 | kfree(entry); |
Michal Malý | e7c2344 | 2015-02-18 17:59:20 +0100 | [diff] [blame] | 1220 | out: |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 1221 | dbg_hid("Device successfully unregistered\n"); |
| 1222 | return 0; |
| 1223 | } |