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