Simon Wood | 32c88cb | 2010-09-22 13:19:42 +0200 | [diff] [blame] | 1 | /* |
Simon Wood | 6401380 | 2012-04-21 05:41:16 -0700 | [diff] [blame] | 2 | * Force feedback support for Logitech Gaming Wheels |
Simon Wood | 32c88cb | 2010-09-22 13:19:42 +0200 | [diff] [blame] | 3 | * |
Simon Wood | 6401380 | 2012-04-21 05:41:16 -0700 | [diff] [blame] | 4 | * Including G27, G25, DFP, DFGT, FFEX, Momo, Momo2 & |
| 5 | * Speed Force Wireless (WiiWheel) |
Simon Wood | 32c88cb | 2010-09-22 13:19:42 +0200 | [diff] [blame] | 6 | * |
| 7 | * Copyright (c) 2010 Simon Wood <simon@mungewell.org> |
| 8 | */ |
| 9 | |
| 10 | /* |
| 11 | * This program is free software; you can redistribute it and/or modify |
| 12 | * it under the terms of the GNU General Public License as published by |
| 13 | * the Free Software Foundation; either version 2 of the License, or |
| 14 | * (at your option) any later version. |
| 15 | * |
| 16 | * This program is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | * GNU General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License |
| 22 | * along with this program; if not, write to the Free Software |
| 23 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 24 | */ |
| 25 | |
| 26 | |
| 27 | #include <linux/input.h> |
| 28 | #include <linux/usb.h> |
| 29 | #include <linux/hid.h> |
| 30 | |
| 31 | #include "usbhid/usbhid.h" |
| 32 | #include "hid-lg.h" |
Michal Malý | 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 |
Simon Wood | 29fae1c | 2015-11-02 07:56:52 -0700 | [diff] [blame] | 48 | #define LG4FF_MODE_G29_IDX 6 |
| 49 | #define LG4FF_MODE_MAX_IDX 7 |
Michal Malý | b96d23e | 2015-02-18 17:59:21 +0100 | [diff] [blame] | 50 | |
| 51 | #define LG4FF_MODE_NATIVE BIT(LG4FF_MODE_NATIVE_IDX) |
| 52 | #define LG4FF_MODE_DFEX BIT(LG4FF_MODE_DFEX_IDX) |
| 53 | #define LG4FF_MODE_DFP BIT(LG4FF_MODE_DFP_IDX) |
| 54 | #define LG4FF_MODE_G25 BIT(LG4FF_MODE_G25_IDX) |
| 55 | #define LG4FF_MODE_DFGT BIT(LG4FF_MODE_DFGT_IDX) |
| 56 | #define LG4FF_MODE_G27 BIT(LG4FF_MODE_G27_IDX) |
Simon Wood | 29fae1c | 2015-11-02 07:56:52 -0700 | [diff] [blame] | 57 | #define LG4FF_MODE_G29 BIT(LG4FF_MODE_G29_IDX) |
Michal Malý | b96d23e | 2015-02-18 17:59:21 +0100 | [diff] [blame] | 58 | |
| 59 | #define LG4FF_DFEX_TAG "DF-EX" |
| 60 | #define LG4FF_DFEX_NAME "Driving Force / Formula EX" |
| 61 | #define LG4FF_DFP_TAG "DFP" |
| 62 | #define LG4FF_DFP_NAME "Driving Force Pro" |
| 63 | #define LG4FF_G25_TAG "G25" |
| 64 | #define LG4FF_G25_NAME "G25 Racing Wheel" |
| 65 | #define LG4FF_G27_TAG "G27" |
| 66 | #define LG4FF_G27_NAME "G27 Racing Wheel" |
Simon Wood | 29fae1c | 2015-11-02 07:56:52 -0700 | [diff] [blame] | 67 | #define LG4FF_G29_TAG "G29" |
| 68 | #define LG4FF_G29_NAME "G29 Racing Wheel" |
Michal Malý | b96d23e | 2015-02-18 17:59:21 +0100 | [diff] [blame] | 69 | #define LG4FF_DFGT_TAG "DFGT" |
| 70 | #define LG4FF_DFGT_NAME "Driving Force GT" |
| 71 | |
Michal Malý | e7c2344 | 2015-02-18 17:59:20 +0100 | [diff] [blame] | 72 | #define LG4FF_FFEX_REV_MAJ 0x21 |
| 73 | #define LG4FF_FFEX_REV_MIN 0x00 |
| 74 | |
Michal Malý | d0afd84 | 2015-04-08 22:56:40 +0200 | [diff] [blame] | 75 | static void lg4ff_set_range_dfp(struct hid_device *hid, u16 range); |
| 76 | static void lg4ff_set_range_g25(struct hid_device *hid, u16 range); |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 77 | |
Michal Malý | 72529c6 | 2015-04-08 22:56:46 +0200 | [diff] [blame] | 78 | struct lg4ff_wheel_data { |
Michal Malý | 5d9d60a | 2015-04-08 22:56:50 +0200 | [diff] [blame] | 79 | const u32 product_id; |
Michal Malý | 2a552c3 | 2015-04-08 22:56:39 +0200 | [diff] [blame] | 80 | u16 range; |
Michal Malý | 5d9d60a | 2015-04-08 22:56:50 +0200 | [diff] [blame] | 81 | const u16 min_range; |
| 82 | const u16 max_range; |
Simon Wood | 22bcefd | 2012-04-21 05:41:15 -0700 | [diff] [blame] | 83 | #ifdef CONFIG_LEDS_CLASS |
Michal Malý | 2a552c3 | 2015-04-08 22:56:39 +0200 | [diff] [blame] | 84 | u8 led_state; |
Simon Wood | 22bcefd | 2012-04-21 05:41:15 -0700 | [diff] [blame] | 85 | struct led_classdev *led[5]; |
| 86 | #endif |
Michal Malý | 5d9d60a | 2015-04-08 22:56:50 +0200 | [diff] [blame] | 87 | const u32 alternate_modes; |
| 88 | const char * const real_tag; |
| 89 | const char * const real_name; |
| 90 | const u16 real_product_id; |
Michal Malý | 72529c6 | 2015-04-08 22:56:46 +0200 | [diff] [blame] | 91 | |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 92 | void (*set_range)(struct hid_device *hid, u16 range); |
| 93 | }; |
| 94 | |
Michal Malý | 72529c6 | 2015-04-08 22:56:46 +0200 | [diff] [blame] | 95 | struct lg4ff_device_entry { |
Michal Malý | c918fe7 | 2015-04-08 22:56:48 +0200 | [diff] [blame] | 96 | spinlock_t report_lock; /* Protect output HID report */ |
Michal Malý | c28abd8 | 2015-04-08 22:56:49 +0200 | [diff] [blame] | 97 | struct hid_report *report; |
Michal Malý | 72529c6 | 2015-04-08 22:56:46 +0200 | [diff] [blame] | 98 | struct lg4ff_wheel_data wdata; |
| 99 | }; |
| 100 | |
Michal Malý | 7362cd2 | 2011-08-04 16:16:09 +0200 | [diff] [blame] | 101 | static const signed short lg4ff_wheel_effects[] = { |
Simon Wood | 32c88cb | 2010-09-22 13:19:42 +0200 | [diff] [blame] | 102 | FF_CONSTANT, |
| 103 | FF_AUTOCENTER, |
| 104 | -1 |
| 105 | }; |
| 106 | |
Michal Malý | 7362cd2 | 2011-08-04 16:16:09 +0200 | [diff] [blame] | 107 | struct lg4ff_wheel { |
Michal Malý | 2a552c3 | 2015-04-08 22:56:39 +0200 | [diff] [blame] | 108 | const u32 product_id; |
Michal Malý | 7362cd2 | 2011-08-04 16:16:09 +0200 | [diff] [blame] | 109 | const signed short *ff_effects; |
Michal Malý | 2a552c3 | 2015-04-08 22:56:39 +0200 | [diff] [blame] | 110 | const u16 min_range; |
| 111 | const u16 max_range; |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 112 | void (*set_range)(struct hid_device *hid, u16 range); |
Michal Malý | 7362cd2 | 2011-08-04 16:16:09 +0200 | [diff] [blame] | 113 | }; |
| 114 | |
Michal Malý | e7c2344 | 2015-02-18 17:59:20 +0100 | [diff] [blame] | 115 | struct lg4ff_compat_mode_switch { |
Michal Malý | 2a552c3 | 2015-04-08 22:56:39 +0200 | [diff] [blame] | 116 | const u8 cmd_count; /* Number of commands to send */ |
| 117 | const u8 cmd[]; |
Michal Malý | e7c2344 | 2015-02-18 17:59:20 +0100 | [diff] [blame] | 118 | }; |
| 119 | |
| 120 | struct lg4ff_wheel_ident_info { |
Simon Wood | bbec1bd | 2015-11-02 07:56:51 -0700 | [diff] [blame] | 121 | const u32 modes; |
Michal Malý | e7c2344 | 2015-02-18 17:59:20 +0100 | [diff] [blame] | 122 | const u16 mask; |
| 123 | const u16 result; |
| 124 | const u16 real_product_id; |
| 125 | }; |
| 126 | |
Michal Malý | b96d23e | 2015-02-18 17:59:21 +0100 | [diff] [blame] | 127 | struct lg4ff_multimode_wheel { |
| 128 | const u16 product_id; |
| 129 | const u32 alternate_modes; |
| 130 | const char *real_tag; |
| 131 | const char *real_name; |
| 132 | }; |
| 133 | |
| 134 | struct lg4ff_alternate_mode { |
| 135 | const u16 product_id; |
| 136 | const char *tag; |
| 137 | const char *name; |
| 138 | }; |
| 139 | |
Michal Malý | 7362cd2 | 2011-08-04 16:16:09 +0200 | [diff] [blame] | 140 | static const struct lg4ff_wheel lg4ff_devices[] = { |
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ý | d0afd84 | 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 | |
Michal Malý | 5d9d60a | 2015-04-08 22:56:50 +0200 | [diff] [blame] | 333 | static void lg4ff_init_wheel_data(struct lg4ff_wheel_data * const wdata, const struct lg4ff_wheel *wheel, |
| 334 | const struct lg4ff_multimode_wheel *mmode_wheel, |
| 335 | const u16 real_product_id) |
| 336 | { |
| 337 | u32 alternate_modes = 0; |
| 338 | const char *real_tag = NULL; |
| 339 | const char *real_name = NULL; |
| 340 | |
| 341 | if (mmode_wheel) { |
| 342 | alternate_modes = mmode_wheel->alternate_modes; |
| 343 | real_tag = mmode_wheel->real_tag; |
| 344 | real_name = mmode_wheel->real_name; |
| 345 | } |
| 346 | |
| 347 | { |
| 348 | struct lg4ff_wheel_data t_wdata = { .product_id = wheel->product_id, |
| 349 | .real_product_id = real_product_id, |
| 350 | .min_range = wheel->min_range, |
| 351 | .max_range = wheel->max_range, |
| 352 | .set_range = wheel->set_range, |
| 353 | .alternate_modes = alternate_modes, |
| 354 | .real_tag = real_tag, |
| 355 | .real_name = real_name }; |
| 356 | |
| 357 | memcpy(wdata, &t_wdata, sizeof(t_wdata)); |
| 358 | } |
| 359 | } |
| 360 | |
Michal Malý | d0afd84 | 2015-04-08 22:56:40 +0200 | [diff] [blame] | 361 | 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] | 362 | { |
| 363 | struct hid_device *hid = input_get_drvdata(dev); |
Michal Malý | c918fe7 | 2015-04-08 22:56:48 +0200 | [diff] [blame] | 364 | struct lg4ff_device_entry *entry; |
| 365 | struct lg_drv_data *drv_data; |
| 366 | unsigned long flags; |
Michal Malý | c28abd8 | 2015-04-08 22:56:49 +0200 | [diff] [blame] | 367 | s32 *value; |
Simon Wood | 32c88cb | 2010-09-22 13:19:42 +0200 | [diff] [blame] | 368 | int x; |
| 369 | |
Michal Malý | c918fe7 | 2015-04-08 22:56:48 +0200 | [diff] [blame] | 370 | drv_data = hid_get_drvdata(hid); |
| 371 | if (!drv_data) { |
| 372 | hid_err(hid, "Private driver data not found!\n"); |
| 373 | return -EINVAL; |
| 374 | } |
| 375 | |
| 376 | entry = drv_data->device_props; |
| 377 | if (!entry) { |
| 378 | hid_err(hid, "Device properties not found!\n"); |
| 379 | return -EINVAL; |
| 380 | } |
Michal Malý | c28abd8 | 2015-04-08 22:56:49 +0200 | [diff] [blame] | 381 | value = entry->report->field[0]->value; |
Michal Malý | c918fe7 | 2015-04-08 22:56:48 +0200 | [diff] [blame] | 382 | |
Michal Malý | a80fe5d | 2012-09-24 01:13:17 +0200 | [diff] [blame] | 383 | #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] | 384 | |
| 385 | switch (effect->type) { |
| 386 | case FF_CONSTANT: |
| 387 | x = effect->u.ramp.start_level + 0x80; /* 0x80 is no force */ |
| 388 | CLAMP(x); |
Simon Wood | 56930e7 | 2013-11-06 12:30:42 -0700 | [diff] [blame] | 389 | |
Michal Malý | c918fe7 | 2015-04-08 22:56:48 +0200 | [diff] [blame] | 390 | spin_lock_irqsave(&entry->report_lock, flags); |
Simon Wood | 56930e7 | 2013-11-06 12:30:42 -0700 | [diff] [blame] | 391 | if (x == 0x80) { |
| 392 | /* De-activate force in slot-1*/ |
| 393 | value[0] = 0x13; |
| 394 | value[1] = 0x00; |
| 395 | value[2] = 0x00; |
| 396 | value[3] = 0x00; |
| 397 | value[4] = 0x00; |
| 398 | value[5] = 0x00; |
| 399 | value[6] = 0x00; |
| 400 | |
Michal Malý | c28abd8 | 2015-04-08 22:56:49 +0200 | [diff] [blame] | 401 | hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT); |
Michal Malý | c918fe7 | 2015-04-08 22:56:48 +0200 | [diff] [blame] | 402 | spin_unlock_irqrestore(&entry->report_lock, flags); |
Simon Wood | 56930e7 | 2013-11-06 12:30:42 -0700 | [diff] [blame] | 403 | return 0; |
| 404 | } |
| 405 | |
Michal Malý | 74479ba | 2012-09-24 01:09:30 +0200 | [diff] [blame] | 406 | value[0] = 0x11; /* Slot 1 */ |
| 407 | value[1] = 0x08; |
| 408 | value[2] = x; |
| 409 | value[3] = 0x80; |
| 410 | value[4] = 0x00; |
| 411 | value[5] = 0x00; |
| 412 | value[6] = 0x00; |
Simon Wood | 32c88cb | 2010-09-22 13:19:42 +0200 | [diff] [blame] | 413 | |
Michal Malý | c28abd8 | 2015-04-08 22:56:49 +0200 | [diff] [blame] | 414 | hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT); |
Michal Malý | c918fe7 | 2015-04-08 22:56:48 +0200 | [diff] [blame] | 415 | spin_unlock_irqrestore(&entry->report_lock, flags); |
Simon Wood | 32c88cb | 2010-09-22 13:19:42 +0200 | [diff] [blame] | 416 | break; |
| 417 | } |
| 418 | return 0; |
| 419 | } |
| 420 | |
Michal Malý | 6e2de8e | 2011-08-04 16:22:07 +0200 | [diff] [blame] | 421 | /* Sends default autocentering command compatible with |
| 422 | * all wheels except Formula Force EX */ |
Michal Malý | d0afd84 | 2015-04-08 22:56:40 +0200 | [diff] [blame] | 423 | static void lg4ff_set_autocenter_default(struct input_dev *dev, u16 magnitude) |
Simon Wood | 32c88cb | 2010-09-22 13:19:42 +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; |
| 429 | u32 expand_a, expand_b; |
Simon Wood | 1859762 | 2013-11-06 12:30:44 -0700 | [diff] [blame] | 430 | struct lg4ff_device_entry *entry; |
| 431 | struct lg_drv_data *drv_data; |
Michal Malý | c918fe7 | 2015-04-08 22:56:48 +0200 | [diff] [blame] | 432 | unsigned long flags; |
Simon Wood | 1859762 | 2013-11-06 12:30:44 -0700 | [diff] [blame] | 433 | |
| 434 | drv_data = hid_get_drvdata(hid); |
| 435 | if (!drv_data) { |
| 436 | hid_err(hid, "Private driver data not found!\n"); |
| 437 | return; |
| 438 | } |
| 439 | |
| 440 | entry = drv_data->device_props; |
| 441 | if (!entry) { |
| 442 | hid_err(hid, "Device properties not found!\n"); |
| 443 | return; |
| 444 | } |
Michal Malý | c28abd8 | 2015-04-08 22:56:49 +0200 | [diff] [blame] | 445 | value = entry->report->field[0]->value; |
Simon Wood | f8c2315 | 2013-11-06 12:30:40 -0700 | [diff] [blame] | 446 | |
Simon Wood | d2c02da | 2013-11-06 12:30:41 -0700 | [diff] [blame] | 447 | /* De-activate Auto-Center */ |
Michal Malý | c918fe7 | 2015-04-08 22:56:48 +0200 | [diff] [blame] | 448 | spin_lock_irqsave(&entry->report_lock, flags); |
Simon Wood | d2c02da | 2013-11-06 12:30:41 -0700 | [diff] [blame] | 449 | if (magnitude == 0) { |
| 450 | value[0] = 0xf5; |
| 451 | value[1] = 0x00; |
| 452 | value[2] = 0x00; |
| 453 | value[3] = 0x00; |
| 454 | value[4] = 0x00; |
| 455 | value[5] = 0x00; |
| 456 | value[6] = 0x00; |
| 457 | |
Michal Malý | c28abd8 | 2015-04-08 22:56:49 +0200 | [diff] [blame] | 458 | hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT); |
Michal Malý | c918fe7 | 2015-04-08 22:56:48 +0200 | [diff] [blame] | 459 | spin_unlock_irqrestore(&entry->report_lock, flags); |
Simon Wood | d2c02da | 2013-11-06 12:30:41 -0700 | [diff] [blame] | 460 | return; |
| 461 | } |
| 462 | |
Simon Wood | f8c2315 | 2013-11-06 12:30:40 -0700 | [diff] [blame] | 463 | if (magnitude <= 0xaaaa) { |
| 464 | expand_a = 0x0c * magnitude; |
| 465 | expand_b = 0x80 * magnitude; |
| 466 | } else { |
| 467 | expand_a = (0x0c * 0xaaaa) + 0x06 * (magnitude - 0xaaaa); |
| 468 | expand_b = (0x80 * 0xaaaa) + 0xff * (magnitude - 0xaaaa); |
| 469 | } |
Simon Wood | 32c88cb | 2010-09-22 13:19:42 +0200 | [diff] [blame] | 470 | |
Simon Wood | 1859762 | 2013-11-06 12:30:44 -0700 | [diff] [blame] | 471 | /* Adjust for non-MOMO wheels */ |
Michal Malý | 72529c6 | 2015-04-08 22:56:46 +0200 | [diff] [blame] | 472 | switch (entry->wdata.product_id) { |
Simon Wood | 1859762 | 2013-11-06 12:30:44 -0700 | [diff] [blame] | 473 | case USB_DEVICE_ID_LOGITECH_MOMO_WHEEL: |
| 474 | case USB_DEVICE_ID_LOGITECH_MOMO_WHEEL2: |
| 475 | break; |
| 476 | default: |
| 477 | expand_a = expand_a >> 1; |
| 478 | break; |
| 479 | } |
| 480 | |
Michal Malý | 74479ba | 2012-09-24 01:09:30 +0200 | [diff] [blame] | 481 | value[0] = 0xfe; |
| 482 | value[1] = 0x0d; |
Simon Wood | f8c2315 | 2013-11-06 12:30:40 -0700 | [diff] [blame] | 483 | value[2] = expand_a / 0xaaaa; |
| 484 | value[3] = expand_a / 0xaaaa; |
| 485 | value[4] = expand_b / 0xaaaa; |
Michal Malý | 74479ba | 2012-09-24 01:09:30 +0200 | [diff] [blame] | 486 | value[5] = 0x00; |
| 487 | value[6] = 0x00; |
Simon Wood | 32c88cb | 2010-09-22 13:19:42 +0200 | [diff] [blame] | 488 | |
Michal Malý | c28abd8 | 2015-04-08 22:56:49 +0200 | [diff] [blame] | 489 | hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT); |
Simon Wood | d2c02da | 2013-11-06 12:30:41 -0700 | [diff] [blame] | 490 | |
| 491 | /* Activate Auto-Center */ |
| 492 | value[0] = 0x14; |
| 493 | value[1] = 0x00; |
| 494 | value[2] = 0x00; |
| 495 | value[3] = 0x00; |
| 496 | value[4] = 0x00; |
| 497 | value[5] = 0x00; |
| 498 | value[6] = 0x00; |
| 499 | |
Michal Malý | c28abd8 | 2015-04-08 22:56:49 +0200 | [diff] [blame] | 500 | hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT); |
Michal Malý | c918fe7 | 2015-04-08 22:56:48 +0200 | [diff] [blame] | 501 | spin_unlock_irqrestore(&entry->report_lock, flags); |
Simon Wood | 32c88cb | 2010-09-22 13:19:42 +0200 | [diff] [blame] | 502 | } |
| 503 | |
Michal Malý | 6e2de8e | 2011-08-04 16:22:07 +0200 | [diff] [blame] | 504 | /* Sends autocentering command compatible with Formula Force EX */ |
Michal Malý | d0afd84 | 2015-04-08 22:56:40 +0200 | [diff] [blame] | 505 | static void lg4ff_set_autocenter_ffex(struct input_dev *dev, u16 magnitude) |
Michal Malý | 6e2de8e | 2011-08-04 16:22:07 +0200 | [diff] [blame] | 506 | { |
| 507 | struct hid_device *hid = input_get_drvdata(dev); |
Michal Malý | c918fe7 | 2015-04-08 22:56:48 +0200 | [diff] [blame] | 508 | struct lg4ff_device_entry *entry; |
| 509 | struct lg_drv_data *drv_data; |
| 510 | unsigned long flags; |
Michal Malý | c28abd8 | 2015-04-08 22:56:49 +0200 | [diff] [blame] | 511 | s32 *value; |
Michal Malý | 6e2de8e | 2011-08-04 16:22:07 +0200 | [diff] [blame] | 512 | magnitude = magnitude * 90 / 65535; |
Michal Malý | 6e2de8e | 2011-08-04 16:22:07 +0200 | [diff] [blame] | 513 | |
Michal Malý | c918fe7 | 2015-04-08 22:56:48 +0200 | [diff] [blame] | 514 | drv_data = hid_get_drvdata(hid); |
| 515 | if (!drv_data) { |
| 516 | hid_err(hid, "Private driver data not found!\n"); |
| 517 | return; |
| 518 | } |
| 519 | |
| 520 | entry = drv_data->device_props; |
| 521 | if (!entry) { |
| 522 | hid_err(hid, "Device properties not found!\n"); |
| 523 | return; |
| 524 | } |
Michal Malý | c28abd8 | 2015-04-08 22:56:49 +0200 | [diff] [blame] | 525 | value = entry->report->field[0]->value; |
Michal Malý | c918fe7 | 2015-04-08 22:56:48 +0200 | [diff] [blame] | 526 | |
| 527 | spin_lock_irqsave(&entry->report_lock, flags); |
Michal Malý | 74479ba | 2012-09-24 01:09:30 +0200 | [diff] [blame] | 528 | value[0] = 0xfe; |
| 529 | value[1] = 0x03; |
| 530 | value[2] = magnitude >> 14; |
| 531 | value[3] = magnitude >> 14; |
| 532 | value[4] = magnitude; |
| 533 | value[5] = 0x00; |
| 534 | value[6] = 0x00; |
Michal Malý | 6e2de8e | 2011-08-04 16:22:07 +0200 | [diff] [blame] | 535 | |
Michal Malý | c28abd8 | 2015-04-08 22:56:49 +0200 | [diff] [blame] | 536 | hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT); |
Michal Malý | c918fe7 | 2015-04-08 22:56:48 +0200 | [diff] [blame] | 537 | spin_unlock_irqrestore(&entry->report_lock, flags); |
Michal Malý | 6e2de8e | 2011-08-04 16:22:07 +0200 | [diff] [blame] | 538 | } |
| 539 | |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 540 | /* Sends command to set range compatible with G25/G27/Driving Force GT */ |
Michal Malý | d0afd84 | 2015-04-08 22:56:40 +0200 | [diff] [blame] | 541 | static void lg4ff_set_range_g25(struct hid_device *hid, u16 range) |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 542 | { |
Michal Malý | c918fe7 | 2015-04-08 22:56:48 +0200 | [diff] [blame] | 543 | struct lg4ff_device_entry *entry; |
| 544 | struct lg_drv_data *drv_data; |
| 545 | unsigned long flags; |
Michal Malý | c28abd8 | 2015-04-08 22:56:49 +0200 | [diff] [blame] | 546 | s32 *value; |
Michal Malý | 74479ba | 2012-09-24 01:09:30 +0200 | [diff] [blame] | 547 | |
Michal Malý | c918fe7 | 2015-04-08 22:56:48 +0200 | [diff] [blame] | 548 | drv_data = hid_get_drvdata(hid); |
| 549 | if (!drv_data) { |
| 550 | hid_err(hid, "Private driver data not found!\n"); |
| 551 | return; |
| 552 | } |
| 553 | |
| 554 | entry = drv_data->device_props; |
| 555 | if (!entry) { |
| 556 | hid_err(hid, "Device properties not found!\n"); |
| 557 | return; |
| 558 | } |
Michal Malý | c28abd8 | 2015-04-08 22:56:49 +0200 | [diff] [blame] | 559 | value = entry->report->field[0]->value; |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 560 | dbg_hid("G25/G27/DFGT: setting range to %u\n", range); |
| 561 | |
Michal Malý | c918fe7 | 2015-04-08 22:56:48 +0200 | [diff] [blame] | 562 | spin_lock_irqsave(&entry->report_lock, flags); |
Michal Malý | 74479ba | 2012-09-24 01:09:30 +0200 | [diff] [blame] | 563 | value[0] = 0xf8; |
| 564 | value[1] = 0x81; |
| 565 | value[2] = range & 0x00ff; |
| 566 | value[3] = (range & 0xff00) >> 8; |
| 567 | value[4] = 0x00; |
| 568 | value[5] = 0x00; |
| 569 | value[6] = 0x00; |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 570 | |
Michal Malý | c28abd8 | 2015-04-08 22:56:49 +0200 | [diff] [blame] | 571 | hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT); |
Michal Malý | c918fe7 | 2015-04-08 22:56:48 +0200 | [diff] [blame] | 572 | spin_unlock_irqrestore(&entry->report_lock, flags); |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 573 | } |
| 574 | |
| 575 | /* Sends commands to set range compatible with Driving Force Pro wheel */ |
Michal Malý | d0afd84 | 2015-04-08 22:56:40 +0200 | [diff] [blame] | 576 | static void lg4ff_set_range_dfp(struct hid_device *hid, u16 range) |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 577 | { |
Michal Malý | c918fe7 | 2015-04-08 22:56:48 +0200 | [diff] [blame] | 578 | struct lg4ff_device_entry *entry; |
| 579 | struct lg_drv_data *drv_data; |
| 580 | unsigned long flags; |
Michal Malý | c28abd8 | 2015-04-08 22:56:49 +0200 | [diff] [blame] | 581 | int start_left, start_right, full_range; |
| 582 | s32 *value; |
Michal Malý | 74479ba | 2012-09-24 01:09:30 +0200 | [diff] [blame] | 583 | |
Michal Malý | c918fe7 | 2015-04-08 22:56:48 +0200 | [diff] [blame] | 584 | drv_data = hid_get_drvdata(hid); |
| 585 | if (!drv_data) { |
| 586 | hid_err(hid, "Private driver data not found!\n"); |
| 587 | return; |
| 588 | } |
| 589 | |
| 590 | entry = drv_data->device_props; |
| 591 | if (!entry) { |
| 592 | hid_err(hid, "Device properties not found!\n"); |
| 593 | return; |
| 594 | } |
Michal Malý | c28abd8 | 2015-04-08 22:56:49 +0200 | [diff] [blame] | 595 | value = entry->report->field[0]->value; |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 596 | dbg_hid("Driving Force Pro: setting range to %u\n", range); |
| 597 | |
| 598 | /* Prepare "coarse" limit command */ |
Michal Malý | c918fe7 | 2015-04-08 22:56:48 +0200 | [diff] [blame] | 599 | spin_lock_irqsave(&entry->report_lock, flags); |
Michal Malý | 74479ba | 2012-09-24 01:09:30 +0200 | [diff] [blame] | 600 | value[0] = 0xf8; |
Michal Malý | a80fe5d | 2012-09-24 01:13:17 +0200 | [diff] [blame] | 601 | value[1] = 0x00; /* Set later */ |
Michal Malý | 74479ba | 2012-09-24 01:09:30 +0200 | [diff] [blame] | 602 | value[2] = 0x00; |
| 603 | value[3] = 0x00; |
| 604 | value[4] = 0x00; |
| 605 | value[5] = 0x00; |
| 606 | value[6] = 0x00; |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 607 | |
| 608 | if (range > 200) { |
Michal Malý | c28abd8 | 2015-04-08 22:56:49 +0200 | [diff] [blame] | 609 | value[1] = 0x03; |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 610 | full_range = 900; |
| 611 | } else { |
Michal Malý | c28abd8 | 2015-04-08 22:56:49 +0200 | [diff] [blame] | 612 | value[1] = 0x02; |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 613 | full_range = 200; |
| 614 | } |
Michal Malý | c28abd8 | 2015-04-08 22:56:49 +0200 | [diff] [blame] | 615 | hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT); |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 616 | |
| 617 | /* Prepare "fine" limit command */ |
Michal Malý | 74479ba | 2012-09-24 01:09:30 +0200 | [diff] [blame] | 618 | value[0] = 0x81; |
| 619 | value[1] = 0x0b; |
| 620 | value[2] = 0x00; |
| 621 | value[3] = 0x00; |
| 622 | value[4] = 0x00; |
| 623 | value[5] = 0x00; |
| 624 | value[6] = 0x00; |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 625 | |
| 626 | if (range == 200 || range == 900) { /* Do not apply any fine limit */ |
Michal Malý | c28abd8 | 2015-04-08 22:56:49 +0200 | [diff] [blame] | 627 | hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT); |
Michal Malý | c918fe7 | 2015-04-08 22:56:48 +0200 | [diff] [blame] | 628 | spin_unlock_irqrestore(&entry->report_lock, flags); |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 629 | return; |
| 630 | } |
| 631 | |
| 632 | /* Construct fine limit command */ |
| 633 | start_left = (((full_range - range + 1) * 2047) / full_range); |
| 634 | start_right = 0xfff - start_left; |
| 635 | |
Michal Malý | 74479ba | 2012-09-24 01:09:30 +0200 | [diff] [blame] | 636 | value[2] = start_left >> 4; |
| 637 | value[3] = start_right >> 4; |
| 638 | value[4] = 0xff; |
| 639 | value[5] = (start_right & 0xe) << 4 | (start_left & 0xe); |
| 640 | value[6] = 0xff; |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 641 | |
Michal Malý | c28abd8 | 2015-04-08 22:56:49 +0200 | [diff] [blame] | 642 | hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT); |
Michal Malý | c918fe7 | 2015-04-08 22:56:48 +0200 | [diff] [blame] | 643 | spin_unlock_irqrestore(&entry->report_lock, flags); |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 644 | } |
| 645 | |
Michal Malý | f31a2de | 2015-02-18 17:59:23 +0100 | [diff] [blame] | 646 | static const struct lg4ff_compat_mode_switch *lg4ff_get_mode_switch_command(const u16 real_product_id, const u16 target_product_id) |
| 647 | { |
| 648 | switch (real_product_id) { |
| 649 | case USB_DEVICE_ID_LOGITECH_DFP_WHEEL: |
| 650 | switch (target_product_id) { |
| 651 | case USB_DEVICE_ID_LOGITECH_DFP_WHEEL: |
| 652 | return &lg4ff_mode_switch_ext01_dfp; |
| 653 | /* DFP can only be switched to its native mode */ |
| 654 | default: |
| 655 | return NULL; |
| 656 | } |
| 657 | break; |
| 658 | case USB_DEVICE_ID_LOGITECH_G25_WHEEL: |
| 659 | switch (target_product_id) { |
| 660 | case USB_DEVICE_ID_LOGITECH_DFP_WHEEL: |
| 661 | return &lg4ff_mode_switch_ext01_dfp; |
| 662 | case USB_DEVICE_ID_LOGITECH_G25_WHEEL: |
| 663 | return &lg4ff_mode_switch_ext16_g25; |
| 664 | /* G25 can only be switched to DFP mode or its native mode */ |
| 665 | default: |
| 666 | return NULL; |
| 667 | } |
| 668 | break; |
| 669 | case USB_DEVICE_ID_LOGITECH_G27_WHEEL: |
| 670 | switch (target_product_id) { |
| 671 | case USB_DEVICE_ID_LOGITECH_WHEEL: |
| 672 | return &lg4ff_mode_switch_ext09_dfex; |
| 673 | case USB_DEVICE_ID_LOGITECH_DFP_WHEEL: |
| 674 | return &lg4ff_mode_switch_ext09_dfp; |
| 675 | case USB_DEVICE_ID_LOGITECH_G25_WHEEL: |
| 676 | return &lg4ff_mode_switch_ext09_g25; |
| 677 | case USB_DEVICE_ID_LOGITECH_G27_WHEEL: |
| 678 | return &lg4ff_mode_switch_ext09_g27; |
| 679 | /* G27 can only be switched to DF-EX, DFP, G25 or its native mode */ |
| 680 | default: |
| 681 | return NULL; |
| 682 | } |
| 683 | break; |
Simon Wood | 29fae1c | 2015-11-02 07:56:52 -0700 | [diff] [blame] | 684 | case USB_DEVICE_ID_LOGITECH_G29_WHEEL: |
| 685 | switch (target_product_id) { |
| 686 | case USB_DEVICE_ID_LOGITECH_DFP_WHEEL: |
| 687 | return &lg4ff_mode_switch_ext09_dfp; |
| 688 | case USB_DEVICE_ID_LOGITECH_DFGT_WHEEL: |
| 689 | return &lg4ff_mode_switch_ext09_dfgt; |
| 690 | case USB_DEVICE_ID_LOGITECH_G25_WHEEL: |
| 691 | return &lg4ff_mode_switch_ext09_g25; |
| 692 | case USB_DEVICE_ID_LOGITECH_G27_WHEEL: |
| 693 | return &lg4ff_mode_switch_ext09_g27; |
| 694 | case USB_DEVICE_ID_LOGITECH_G29_WHEEL: |
| 695 | return &lg4ff_mode_switch_ext09_g29; |
| 696 | /* G29 can only be switched to DF-EX, DFP, DFGT, G25, G27 or its native mode */ |
| 697 | default: |
| 698 | return NULL; |
| 699 | } |
| 700 | break; |
Michal Malý | f31a2de | 2015-02-18 17:59:23 +0100 | [diff] [blame] | 701 | case USB_DEVICE_ID_LOGITECH_DFGT_WHEEL: |
| 702 | switch (target_product_id) { |
| 703 | case USB_DEVICE_ID_LOGITECH_WHEEL: |
| 704 | return &lg4ff_mode_switch_ext09_dfex; |
| 705 | case USB_DEVICE_ID_LOGITECH_DFP_WHEEL: |
| 706 | return &lg4ff_mode_switch_ext09_dfp; |
| 707 | case USB_DEVICE_ID_LOGITECH_DFGT_WHEEL: |
| 708 | return &lg4ff_mode_switch_ext09_dfgt; |
| 709 | /* DFGT can only be switched to DF-EX, DFP or its native mode */ |
| 710 | default: |
| 711 | return NULL; |
| 712 | } |
| 713 | break; |
| 714 | /* No other wheels have multiple modes */ |
| 715 | default: |
| 716 | return NULL; |
| 717 | } |
| 718 | } |
| 719 | |
Michal Malý | e7c2344 | 2015-02-18 17:59:20 +0100 | [diff] [blame] | 720 | 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] | 721 | { |
Michal Malý | c918fe7 | 2015-04-08 22:56:48 +0200 | [diff] [blame] | 722 | struct lg4ff_device_entry *entry; |
| 723 | struct lg_drv_data *drv_data; |
| 724 | unsigned long flags; |
Michal Malý | c28abd8 | 2015-04-08 22:56:49 +0200 | [diff] [blame] | 725 | s32 *value; |
Michal Malý | e7c2344 | 2015-02-18 17:59:20 +0100 | [diff] [blame] | 726 | u8 i; |
Michal Malý | 96440c8 | 2011-08-04 16:18:11 +0200 | [diff] [blame] | 727 | |
Michal Malý | c918fe7 | 2015-04-08 22:56:48 +0200 | [diff] [blame] | 728 | drv_data = hid_get_drvdata(hid); |
| 729 | if (!drv_data) { |
| 730 | hid_err(hid, "Private driver data not found!\n"); |
| 731 | return -EINVAL; |
| 732 | } |
| 733 | |
| 734 | entry = drv_data->device_props; |
| 735 | if (!entry) { |
| 736 | hid_err(hid, "Device properties not found!\n"); |
| 737 | return -EINVAL; |
| 738 | } |
Michal Malý | c28abd8 | 2015-04-08 22:56:49 +0200 | [diff] [blame] | 739 | value = entry->report->field[0]->value; |
Michal Malý | c918fe7 | 2015-04-08 22:56:48 +0200 | [diff] [blame] | 740 | |
| 741 | spin_lock_irqsave(&entry->report_lock, flags); |
Michal Malý | e7c2344 | 2015-02-18 17:59:20 +0100 | [diff] [blame] | 742 | for (i = 0; i < s->cmd_count; i++) { |
Michal Malý | c1740d1 | 2015-02-18 22:49:33 +0100 | [diff] [blame] | 743 | u8 j; |
Michal Malý | 96440c8 | 2011-08-04 16:18:11 +0200 | [diff] [blame] | 744 | |
Michal Malý | c1740d1 | 2015-02-18 22:49:33 +0100 | [diff] [blame] | 745 | for (j = 0; j < 7; j++) |
| 746 | value[j] = s->cmd[j + (7*i)]; |
| 747 | |
Michal Malý | c28abd8 | 2015-04-08 22:56:49 +0200 | [diff] [blame] | 748 | hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT); |
Michal Malý | 96440c8 | 2011-08-04 16:18:11 +0200 | [diff] [blame] | 749 | } |
Michal Malý | c918fe7 | 2015-04-08 22:56:48 +0200 | [diff] [blame] | 750 | spin_unlock_irqrestore(&entry->report_lock, flags); |
Michal Malý | c1740d1 | 2015-02-18 22:49:33 +0100 | [diff] [blame] | 751 | hid_hw_wait(hid); |
Michal Malý | e7c2344 | 2015-02-18 17:59:20 +0100 | [diff] [blame] | 752 | return 0; |
Michal Malý | 96440c8 | 2011-08-04 16:18:11 +0200 | [diff] [blame] | 753 | } |
Simon Wood | 32c88cb | 2010-09-22 13:19:42 +0200 | [diff] [blame] | 754 | |
Michal Malý | b96d23e | 2015-02-18 17:59:21 +0100 | [diff] [blame] | 755 | static ssize_t lg4ff_alternate_modes_show(struct device *dev, struct device_attribute *attr, char *buf) |
| 756 | { |
| 757 | struct hid_device *hid = to_hid_device(dev); |
| 758 | struct lg4ff_device_entry *entry; |
| 759 | struct lg_drv_data *drv_data; |
| 760 | ssize_t count = 0; |
| 761 | int i; |
| 762 | |
| 763 | drv_data = hid_get_drvdata(hid); |
| 764 | if (!drv_data) { |
| 765 | hid_err(hid, "Private driver data not found!\n"); |
| 766 | return 0; |
| 767 | } |
| 768 | |
| 769 | entry = drv_data->device_props; |
| 770 | if (!entry) { |
| 771 | hid_err(hid, "Device properties not found!\n"); |
| 772 | return 0; |
| 773 | } |
| 774 | |
Michal Malý | 72529c6 | 2015-04-08 22:56:46 +0200 | [diff] [blame] | 775 | if (!entry->wdata.real_name) { |
Michal Malý | b96d23e | 2015-02-18 17:59:21 +0100 | [diff] [blame] | 776 | hid_err(hid, "NULL pointer to string\n"); |
| 777 | return 0; |
| 778 | } |
| 779 | |
| 780 | for (i = 0; i < LG4FF_MODE_MAX_IDX; i++) { |
Michal Malý | 72529c6 | 2015-04-08 22:56:46 +0200 | [diff] [blame] | 781 | if (entry->wdata.alternate_modes & BIT(i)) { |
Michal Malý | b96d23e | 2015-02-18 17:59:21 +0100 | [diff] [blame] | 782 | /* Print tag and full name */ |
| 783 | count += scnprintf(buf + count, PAGE_SIZE - count, "%s: %s", |
| 784 | lg4ff_alternate_modes[i].tag, |
Michal Malý | 72529c6 | 2015-04-08 22:56:46 +0200 | [diff] [blame] | 785 | !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] | 786 | if (count >= PAGE_SIZE - 1) |
| 787 | return count; |
| 788 | |
| 789 | /* Mark the currently active mode with an asterisk */ |
Michal Malý | 72529c6 | 2015-04-08 22:56:46 +0200 | [diff] [blame] | 790 | if (lg4ff_alternate_modes[i].product_id == entry->wdata.product_id || |
| 791 | (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] | 792 | count += scnprintf(buf + count, PAGE_SIZE - count, " *\n"); |
| 793 | else |
| 794 | count += scnprintf(buf + count, PAGE_SIZE - count, "\n"); |
| 795 | |
| 796 | if (count >= PAGE_SIZE - 1) |
| 797 | return count; |
| 798 | } |
| 799 | } |
| 800 | |
| 801 | return count; |
| 802 | } |
| 803 | |
| 804 | static ssize_t lg4ff_alternate_modes_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) |
| 805 | { |
Michal Malý | f31a2de | 2015-02-18 17:59:23 +0100 | [diff] [blame] | 806 | struct hid_device *hid = to_hid_device(dev); |
| 807 | struct lg4ff_device_entry *entry; |
| 808 | struct lg_drv_data *drv_data; |
| 809 | const struct lg4ff_compat_mode_switch *s; |
| 810 | u16 target_product_id = 0; |
| 811 | int i, ret; |
| 812 | char *lbuf; |
| 813 | |
| 814 | drv_data = hid_get_drvdata(hid); |
| 815 | if (!drv_data) { |
| 816 | hid_err(hid, "Private driver data not found!\n"); |
| 817 | return -EINVAL; |
| 818 | } |
| 819 | |
| 820 | entry = drv_data->device_props; |
| 821 | if (!entry) { |
| 822 | hid_err(hid, "Device properties not found!\n"); |
| 823 | return -EINVAL; |
| 824 | } |
| 825 | |
| 826 | /* Allow \n at the end of the input parameter */ |
| 827 | lbuf = kasprintf(GFP_KERNEL, "%s", buf); |
| 828 | if (!lbuf) |
| 829 | return -ENOMEM; |
| 830 | |
| 831 | i = strlen(lbuf); |
| 832 | if (lbuf[i-1] == '\n') { |
| 833 | if (i == 1) { |
| 834 | kfree(lbuf); |
| 835 | return -EINVAL; |
| 836 | } |
| 837 | lbuf[i-1] = '\0'; |
| 838 | } |
| 839 | |
| 840 | for (i = 0; i < LG4FF_MODE_MAX_IDX; i++) { |
| 841 | const u16 mode_product_id = lg4ff_alternate_modes[i].product_id; |
| 842 | const char *tag = lg4ff_alternate_modes[i].tag; |
| 843 | |
Michal Malý | 72529c6 | 2015-04-08 22:56:46 +0200 | [diff] [blame] | 844 | if (entry->wdata.alternate_modes & BIT(i)) { |
Michal Malý | f31a2de | 2015-02-18 17:59:23 +0100 | [diff] [blame] | 845 | if (!strcmp(tag, lbuf)) { |
| 846 | if (!mode_product_id) |
Michal Malý | 72529c6 | 2015-04-08 22:56:46 +0200 | [diff] [blame] | 847 | target_product_id = entry->wdata.real_product_id; |
Michal Malý | f31a2de | 2015-02-18 17:59:23 +0100 | [diff] [blame] | 848 | else |
| 849 | target_product_id = mode_product_id; |
| 850 | break; |
| 851 | } |
| 852 | } |
| 853 | } |
| 854 | |
| 855 | if (i == LG4FF_MODE_MAX_IDX) { |
| 856 | hid_info(hid, "Requested mode \"%s\" is not supported by the device\n", lbuf); |
| 857 | kfree(lbuf); |
| 858 | return -EINVAL; |
| 859 | } |
| 860 | kfree(lbuf); /* Not needed anymore */ |
| 861 | |
Michal Malý | 72529c6 | 2015-04-08 22:56:46 +0200 | [diff] [blame] | 862 | if (target_product_id == entry->wdata.product_id) /* Nothing to do */ |
Michal Malý | f31a2de | 2015-02-18 17:59:23 +0100 | [diff] [blame] | 863 | return count; |
| 864 | |
| 865 | /* Automatic switching has to be disabled for the switch to DF-EX mode to work correctly */ |
| 866 | if (target_product_id == USB_DEVICE_ID_LOGITECH_WHEEL && !lg4ff_no_autoswitch) { |
| 867 | 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] | 868 | entry->wdata.real_name); |
Michal Malý | f31a2de | 2015-02-18 17:59:23 +0100 | [diff] [blame] | 869 | return -EINVAL; |
| 870 | } |
| 871 | |
| 872 | /* Take care of hardware limitations */ |
Michal Malý | 72529c6 | 2015-04-08 22:56:46 +0200 | [diff] [blame] | 873 | if ((entry->wdata.real_product_id == USB_DEVICE_ID_LOGITECH_DFP_WHEEL || entry->wdata.real_product_id == USB_DEVICE_ID_LOGITECH_G25_WHEEL) && |
| 874 | entry->wdata.product_id > target_product_id) { |
| 875 | 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] | 876 | return -EINVAL; |
| 877 | } |
| 878 | |
Michal Malý | 72529c6 | 2015-04-08 22:56:46 +0200 | [diff] [blame] | 879 | 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] | 880 | if (!s) { |
| 881 | hid_err(hid, "Invalid target product ID %X\n", target_product_id); |
| 882 | return -EINVAL; |
| 883 | } |
| 884 | |
| 885 | ret = lg4ff_switch_compatibility_mode(hid, s); |
| 886 | return (ret == 0 ? count : ret); |
Michal Malý | b96d23e | 2015-02-18 17:59:21 +0100 | [diff] [blame] | 887 | } |
| 888 | static DEVICE_ATTR(alternate_modes, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH, lg4ff_alternate_modes_show, lg4ff_alternate_modes_store); |
| 889 | |
Michal Malý | fbf85e2 | 2015-04-08 22:56:41 +0200 | [diff] [blame] | 890 | /* Export the currently set range of the wheel */ |
| 891 | static ssize_t lg4ff_range_show(struct device *dev, struct device_attribute *attr, |
| 892 | char *buf) |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 893 | { |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 894 | struct hid_device *hid = to_hid_device(dev); |
Michal Malý | 3b6b17b | 2012-04-09 09:08:49 +0200 | [diff] [blame] | 895 | struct lg4ff_device_entry *entry; |
| 896 | struct lg_drv_data *drv_data; |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 897 | size_t count; |
| 898 | |
Michal Malý | 3b6b17b | 2012-04-09 09:08:49 +0200 | [diff] [blame] | 899 | drv_data = hid_get_drvdata(hid); |
| 900 | if (!drv_data) { |
| 901 | hid_err(hid, "Private driver data not found!\n"); |
| 902 | return 0; |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 903 | } |
Michal Malý | 3b6b17b | 2012-04-09 09:08:49 +0200 | [diff] [blame] | 904 | |
| 905 | entry = drv_data->device_props; |
| 906 | if (!entry) { |
| 907 | hid_err(hid, "Device properties not found!\n"); |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 908 | return 0; |
| 909 | } |
| 910 | |
Michal Malý | 72529c6 | 2015-04-08 22:56:46 +0200 | [diff] [blame] | 911 | count = scnprintf(buf, PAGE_SIZE, "%u\n", entry->wdata.range); |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 912 | return count; |
| 913 | } |
| 914 | |
| 915 | /* Set range to user specified value, call appropriate function |
| 916 | * according to the type of the wheel */ |
Michal Malý | fbf85e2 | 2015-04-08 22:56:41 +0200 | [diff] [blame] | 917 | static ssize_t lg4ff_range_store(struct device *dev, struct device_attribute *attr, |
| 918 | const char *buf, size_t count) |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 919 | { |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 920 | struct hid_device *hid = to_hid_device(dev); |
Michal Malý | 3b6b17b | 2012-04-09 09:08:49 +0200 | [diff] [blame] | 921 | struct lg4ff_device_entry *entry; |
| 922 | struct lg_drv_data *drv_data; |
Michal Malý | 2a552c3 | 2015-04-08 22:56:39 +0200 | [diff] [blame] | 923 | u16 range = simple_strtoul(buf, NULL, 10); |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 924 | |
Michal Malý | 3b6b17b | 2012-04-09 09:08:49 +0200 | [diff] [blame] | 925 | drv_data = hid_get_drvdata(hid); |
| 926 | if (!drv_data) { |
| 927 | hid_err(hid, "Private driver data not found!\n"); |
Simon Wood | 29ff665 | 2014-08-14 20:43:01 -0600 | [diff] [blame] | 928 | return -EINVAL; |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 929 | } |
Michal Malý | 3b6b17b | 2012-04-09 09:08:49 +0200 | [diff] [blame] | 930 | |
| 931 | entry = drv_data->device_props; |
| 932 | if (!entry) { |
| 933 | hid_err(hid, "Device properties not found!\n"); |
Simon Wood | 29ff665 | 2014-08-14 20:43:01 -0600 | [diff] [blame] | 934 | return -EINVAL; |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 935 | } |
| 936 | |
| 937 | if (range == 0) |
Michal Malý | 72529c6 | 2015-04-08 22:56:46 +0200 | [diff] [blame] | 938 | range = entry->wdata.max_range; |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 939 | |
| 940 | /* Check if the wheel supports range setting |
| 941 | * and that the range is within limits for the wheel */ |
Michal Malý | 72529c6 | 2015-04-08 22:56:46 +0200 | [diff] [blame] | 942 | if (entry->wdata.set_range && range >= entry->wdata.min_range && range <= entry->wdata.max_range) { |
| 943 | entry->wdata.set_range(hid, range); |
| 944 | entry->wdata.range = range; |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 945 | } |
| 946 | |
| 947 | return count; |
| 948 | } |
Michal Malý | fbf85e2 | 2015-04-08 22:56:41 +0200 | [diff] [blame] | 949 | 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] | 950 | |
Michal Malý | b96d23e | 2015-02-18 17:59:21 +0100 | [diff] [blame] | 951 | static ssize_t lg4ff_real_id_show(struct device *dev, struct device_attribute *attr, char *buf) |
| 952 | { |
| 953 | struct hid_device *hid = to_hid_device(dev); |
| 954 | struct lg4ff_device_entry *entry; |
| 955 | struct lg_drv_data *drv_data; |
| 956 | size_t count; |
| 957 | |
| 958 | drv_data = hid_get_drvdata(hid); |
| 959 | if (!drv_data) { |
| 960 | hid_err(hid, "Private driver data not found!\n"); |
| 961 | return 0; |
| 962 | } |
| 963 | |
| 964 | entry = drv_data->device_props; |
| 965 | if (!entry) { |
| 966 | hid_err(hid, "Device properties not found!\n"); |
| 967 | return 0; |
| 968 | } |
| 969 | |
Michal Malý | 72529c6 | 2015-04-08 22:56:46 +0200 | [diff] [blame] | 970 | if (!entry->wdata.real_tag || !entry->wdata.real_name) { |
Michal Malý | b96d23e | 2015-02-18 17:59:21 +0100 | [diff] [blame] | 971 | hid_err(hid, "NULL pointer to string\n"); |
| 972 | return 0; |
| 973 | } |
| 974 | |
Michal Malý | 72529c6 | 2015-04-08 22:56:46 +0200 | [diff] [blame] | 975 | 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] | 976 | return count; |
| 977 | } |
| 978 | |
| 979 | static ssize_t lg4ff_real_id_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) |
| 980 | { |
| 981 | /* Real ID is a read-only value */ |
| 982 | return -EPERM; |
| 983 | } |
| 984 | static DEVICE_ATTR(real_id, S_IRUGO, lg4ff_real_id_show, lg4ff_real_id_store); |
| 985 | |
Simon Wood | 22bcefd | 2012-04-21 05:41:15 -0700 | [diff] [blame] | 986 | #ifdef CONFIG_LEDS_CLASS |
Michal Malý | 2a552c3 | 2015-04-08 22:56:39 +0200 | [diff] [blame] | 987 | static void lg4ff_set_leds(struct hid_device *hid, u8 leds) |
Simon Wood | 22bcefd | 2012-04-21 05:41:15 -0700 | [diff] [blame] | 988 | { |
Michal Malý | c918fe7 | 2015-04-08 22:56:48 +0200 | [diff] [blame] | 989 | struct lg_drv_data *drv_data; |
| 990 | struct lg4ff_device_entry *entry; |
| 991 | unsigned long flags; |
Michal Malý | c28abd8 | 2015-04-08 22:56:49 +0200 | [diff] [blame] | 992 | s32 *value; |
Simon Wood | 22bcefd | 2012-04-21 05:41:15 -0700 | [diff] [blame] | 993 | |
Michal Malý | c918fe7 | 2015-04-08 22:56:48 +0200 | [diff] [blame] | 994 | drv_data = hid_get_drvdata(hid); |
| 995 | if (!drv_data) { |
| 996 | hid_err(hid, "Private driver data not found!\n"); |
| 997 | return; |
| 998 | } |
| 999 | |
| 1000 | entry = drv_data->device_props; |
| 1001 | if (!entry) { |
| 1002 | hid_err(hid, "Device properties not found!\n"); |
| 1003 | return; |
| 1004 | } |
Michal Malý | c28abd8 | 2015-04-08 22:56:49 +0200 | [diff] [blame] | 1005 | value = entry->report->field[0]->value; |
Michal Malý | c918fe7 | 2015-04-08 22:56:48 +0200 | [diff] [blame] | 1006 | |
| 1007 | spin_lock_irqsave(&entry->report_lock, flags); |
Michal Malý | 74479ba | 2012-09-24 01:09:30 +0200 | [diff] [blame] | 1008 | value[0] = 0xf8; |
| 1009 | value[1] = 0x12; |
| 1010 | value[2] = leds; |
| 1011 | value[3] = 0x00; |
| 1012 | value[4] = 0x00; |
| 1013 | value[5] = 0x00; |
| 1014 | value[6] = 0x00; |
Michal Malý | c28abd8 | 2015-04-08 22:56:49 +0200 | [diff] [blame] | 1015 | hid_hw_request(hid, entry->report, HID_REQ_SET_REPORT); |
Michal Malý | c918fe7 | 2015-04-08 22:56:48 +0200 | [diff] [blame] | 1016 | spin_unlock_irqrestore(&entry->report_lock, flags); |
Simon Wood | 22bcefd | 2012-04-21 05:41:15 -0700 | [diff] [blame] | 1017 | } |
| 1018 | |
| 1019 | static void lg4ff_led_set_brightness(struct led_classdev *led_cdev, |
| 1020 | enum led_brightness value) |
| 1021 | { |
| 1022 | struct device *dev = led_cdev->dev->parent; |
| 1023 | struct hid_device *hid = container_of(dev, struct hid_device, dev); |
Axel Lin | 4629fd1 | 2012-09-13 14:09:33 +0800 | [diff] [blame] | 1024 | struct lg_drv_data *drv_data = hid_get_drvdata(hid); |
Simon Wood | 22bcefd | 2012-04-21 05:41:15 -0700 | [diff] [blame] | 1025 | struct lg4ff_device_entry *entry; |
| 1026 | int i, state = 0; |
| 1027 | |
| 1028 | if (!drv_data) { |
| 1029 | hid_err(hid, "Device data not found."); |
| 1030 | return; |
| 1031 | } |
| 1032 | |
Michal Malý | 371a1d9 | 2015-04-08 22:56:43 +0200 | [diff] [blame] | 1033 | entry = drv_data->device_props; |
Simon Wood | 22bcefd | 2012-04-21 05:41:15 -0700 | [diff] [blame] | 1034 | |
| 1035 | if (!entry) { |
| 1036 | hid_err(hid, "Device properties not found."); |
| 1037 | return; |
| 1038 | } |
| 1039 | |
| 1040 | for (i = 0; i < 5; i++) { |
Michal Malý | 72529c6 | 2015-04-08 22:56:46 +0200 | [diff] [blame] | 1041 | if (led_cdev != entry->wdata.led[i]) |
Simon Wood | 22bcefd | 2012-04-21 05:41:15 -0700 | [diff] [blame] | 1042 | continue; |
Michal Malý | 72529c6 | 2015-04-08 22:56:46 +0200 | [diff] [blame] | 1043 | state = (entry->wdata.led_state >> i) & 1; |
Simon Wood | 22bcefd | 2012-04-21 05:41:15 -0700 | [diff] [blame] | 1044 | if (value == LED_OFF && state) { |
Michal Malý | 72529c6 | 2015-04-08 22:56:46 +0200 | [diff] [blame] | 1045 | entry->wdata.led_state &= ~(1 << i); |
| 1046 | lg4ff_set_leds(hid, entry->wdata.led_state); |
Simon Wood | 22bcefd | 2012-04-21 05:41:15 -0700 | [diff] [blame] | 1047 | } else if (value != LED_OFF && !state) { |
Michal Malý | 72529c6 | 2015-04-08 22:56:46 +0200 | [diff] [blame] | 1048 | entry->wdata.led_state |= 1 << i; |
| 1049 | lg4ff_set_leds(hid, entry->wdata.led_state); |
Simon Wood | 22bcefd | 2012-04-21 05:41:15 -0700 | [diff] [blame] | 1050 | } |
| 1051 | break; |
| 1052 | } |
| 1053 | } |
| 1054 | |
| 1055 | static enum led_brightness lg4ff_led_get_brightness(struct led_classdev *led_cdev) |
| 1056 | { |
| 1057 | struct device *dev = led_cdev->dev->parent; |
| 1058 | struct hid_device *hid = container_of(dev, struct hid_device, dev); |
Axel Lin | 4629fd1 | 2012-09-13 14:09:33 +0800 | [diff] [blame] | 1059 | struct lg_drv_data *drv_data = hid_get_drvdata(hid); |
Simon Wood | 22bcefd | 2012-04-21 05:41:15 -0700 | [diff] [blame] | 1060 | struct lg4ff_device_entry *entry; |
| 1061 | int i, value = 0; |
| 1062 | |
| 1063 | if (!drv_data) { |
| 1064 | hid_err(hid, "Device data not found."); |
| 1065 | return LED_OFF; |
| 1066 | } |
| 1067 | |
Michal Malý | 371a1d9 | 2015-04-08 22:56:43 +0200 | [diff] [blame] | 1068 | entry = drv_data->device_props; |
Simon Wood | 22bcefd | 2012-04-21 05:41:15 -0700 | [diff] [blame] | 1069 | |
| 1070 | if (!entry) { |
| 1071 | hid_err(hid, "Device properties not found."); |
| 1072 | return LED_OFF; |
| 1073 | } |
| 1074 | |
| 1075 | for (i = 0; i < 5; i++) |
Michal Malý | 72529c6 | 2015-04-08 22:56:46 +0200 | [diff] [blame] | 1076 | if (led_cdev == entry->wdata.led[i]) { |
| 1077 | value = (entry->wdata.led_state >> i) & 1; |
Simon Wood | 22bcefd | 2012-04-21 05:41:15 -0700 | [diff] [blame] | 1078 | break; |
| 1079 | } |
| 1080 | |
| 1081 | return value ? LED_FULL : LED_OFF; |
| 1082 | } |
| 1083 | #endif |
| 1084 | |
Michal Malý | e7c2344 | 2015-02-18 17:59:20 +0100 | [diff] [blame] | 1085 | static u16 lg4ff_identify_multimode_wheel(struct hid_device *hid, const u16 reported_product_id, const u16 bcdDevice) |
| 1086 | { |
Simon Wood | bbec1bd | 2015-11-02 07:56:51 -0700 | [diff] [blame] | 1087 | u32 current_mode; |
| 1088 | int i; |
Michal Malý | e7c2344 | 2015-02-18 17:59:20 +0100 | [diff] [blame] | 1089 | |
Simon Wood | bbec1bd | 2015-11-02 07:56:51 -0700 | [diff] [blame] | 1090 | /* identify current mode from USB PID */ |
| 1091 | for (i = 1; i < ARRAY_SIZE(lg4ff_alternate_modes); i++) { |
| 1092 | dbg_hid("Testing whether PID is %X\n", lg4ff_alternate_modes[i].product_id); |
| 1093 | if (reported_product_id == lg4ff_alternate_modes[i].product_id) |
| 1094 | break; |
Michal Malý | e7c2344 | 2015-02-18 17:59:20 +0100 | [diff] [blame] | 1095 | } |
| 1096 | |
Simon Wood | bbec1bd | 2015-11-02 07:56:51 -0700 | [diff] [blame] | 1097 | if (i == ARRAY_SIZE(lg4ff_alternate_modes)) |
| 1098 | return 0; |
Michal Malý | e7c2344 | 2015-02-18 17:59:20 +0100 | [diff] [blame] | 1099 | |
Simon Wood | bbec1bd | 2015-11-02 07:56:51 -0700 | [diff] [blame] | 1100 | current_mode = BIT(i); |
| 1101 | |
| 1102 | for (i = 0; i < ARRAY_SIZE(lg4ff_main_checklist); i++) { |
| 1103 | const u16 mask = lg4ff_main_checklist[i]->mask; |
| 1104 | const u16 result = lg4ff_main_checklist[i]->result; |
| 1105 | const u16 real_product_id = lg4ff_main_checklist[i]->real_product_id; |
| 1106 | |
| 1107 | if ((current_mode & lg4ff_main_checklist[i]->modes) && \ |
| 1108 | (bcdDevice & mask) == result) { |
Michal Malý | e7c2344 | 2015-02-18 17:59:20 +0100 | [diff] [blame] | 1109 | dbg_hid("Found wheel with real PID %X whose reported PID is %X\n", real_product_id, reported_product_id); |
| 1110 | return real_product_id; |
| 1111 | } |
| 1112 | } |
| 1113 | |
Michal Malý | f31a2de | 2015-02-18 17:59:23 +0100 | [diff] [blame] | 1114 | /* No match found. This is either Driving Force or an unknown |
| 1115 | * wheel model, do not touch it */ |
Michal Malý | e7c2344 | 2015-02-18 17:59:20 +0100 | [diff] [blame] | 1116 | dbg_hid("Wheel with bcdDevice %X was not recognized as multimode wheel, leaving in its current mode\n", bcdDevice); |
| 1117 | return 0; |
| 1118 | } |
| 1119 | |
| 1120 | static int lg4ff_handle_multimode_wheel(struct hid_device *hid, u16 *real_product_id, const u16 bcdDevice) |
| 1121 | { |
| 1122 | const u16 reported_product_id = hid->product; |
| 1123 | int ret; |
| 1124 | |
| 1125 | *real_product_id = lg4ff_identify_multimode_wheel(hid, reported_product_id, bcdDevice); |
| 1126 | /* Probed wheel is not a multimode wheel */ |
| 1127 | if (!*real_product_id) { |
| 1128 | *real_product_id = reported_product_id; |
| 1129 | dbg_hid("Wheel is not a multimode wheel\n"); |
| 1130 | return LG4FF_MMODE_NOT_MULTIMODE; |
| 1131 | } |
| 1132 | |
| 1133 | /* Switch from "Driving Force" mode to native mode automatically. |
| 1134 | * Otherwise keep the wheel in its current mode */ |
| 1135 | if (reported_product_id == USB_DEVICE_ID_LOGITECH_WHEEL && |
Michal Malý | a54dc779 | 2015-02-18 17:59:22 +0100 | [diff] [blame] | 1136 | reported_product_id != *real_product_id && |
| 1137 | !lg4ff_no_autoswitch) { |
Michal Malý | f31a2de | 2015-02-18 17:59:23 +0100 | [diff] [blame] | 1138 | 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] | 1139 | |
Michal Malý | f31a2de | 2015-02-18 17:59:23 +0100 | [diff] [blame] | 1140 | if (!s) { |
Michal Malý | e7c2344 | 2015-02-18 17:59:20 +0100 | [diff] [blame] | 1141 | hid_err(hid, "Invalid product id %X\n", *real_product_id); |
Michal Malý | b96d23e | 2015-02-18 17:59:21 +0100 | [diff] [blame] | 1142 | return LG4FF_MMODE_NOT_MULTIMODE; |
Michal Malý | e7c2344 | 2015-02-18 17:59:20 +0100 | [diff] [blame] | 1143 | } |
| 1144 | |
| 1145 | ret = lg4ff_switch_compatibility_mode(hid, s); |
| 1146 | if (ret) { |
| 1147 | /* Wheel could not have been switched to native mode, |
| 1148 | * leave it in "Driving Force" mode and continue */ |
| 1149 | hid_err(hid, "Unable to switch wheel mode, errno %d\n", ret); |
Michal Malý | b96d23e | 2015-02-18 17:59:21 +0100 | [diff] [blame] | 1150 | return LG4FF_MMODE_IS_MULTIMODE; |
Michal Malý | e7c2344 | 2015-02-18 17:59:20 +0100 | [diff] [blame] | 1151 | } |
| 1152 | return LG4FF_MMODE_SWITCHED; |
| 1153 | } |
| 1154 | |
Michal Malý | b96d23e | 2015-02-18 17:59:21 +0100 | [diff] [blame] | 1155 | return LG4FF_MMODE_IS_MULTIMODE; |
Michal Malý | e7c2344 | 2015-02-18 17:59:20 +0100 | [diff] [blame] | 1156 | } |
| 1157 | |
| 1158 | |
Simon Wood | 32c88cb | 2010-09-22 13:19:42 +0200 | [diff] [blame] | 1159 | int lg4ff_init(struct hid_device *hid) |
| 1160 | { |
| 1161 | 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] | 1162 | struct input_dev *dev = hidinput->input; |
Michal Malý | c28abd8 | 2015-04-08 22:56:49 +0200 | [diff] [blame] | 1163 | struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list; |
| 1164 | 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] | 1165 | const struct usb_device_descriptor *udesc = &(hid_to_usb_dev(hid)->descriptor); |
| 1166 | const u16 bcdDevice = le16_to_cpu(udesc->bcdDevice); |
Michal Malý | 5d9d60a | 2015-04-08 22:56:50 +0200 | [diff] [blame] | 1167 | const struct lg4ff_multimode_wheel *mmode_wheel = NULL; |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 1168 | struct lg4ff_device_entry *entry; |
Michal Malý | 3b6b17b | 2012-04-09 09:08:49 +0200 | [diff] [blame] | 1169 | struct lg_drv_data *drv_data; |
Michal Malý | b96d23e | 2015-02-18 17:59:21 +0100 | [diff] [blame] | 1170 | int error, i, j; |
| 1171 | int mmode_ret, mmode_idx = -1; |
Michal Malý | e7c2344 | 2015-02-18 17:59:20 +0100 | [diff] [blame] | 1172 | u16 real_product_id; |
Simon Wood | 32c88cb | 2010-09-22 13:19:42 +0200 | [diff] [blame] | 1173 | |
Simon Wood | 32c88cb | 2010-09-22 13:19:42 +0200 | [diff] [blame] | 1174 | /* Check that the report looks ok */ |
Kees Cook | 0fb6bd0 | 2013-09-11 21:56:54 +0200 | [diff] [blame] | 1175 | if (!hid_validate_values(hid, HID_OUTPUT_REPORT, 0, 0, 7)) |
Simon Wood | 32c88cb | 2010-09-22 13:19:42 +0200 | [diff] [blame] | 1176 | return -1; |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 1177 | |
Michal Malý | 72529c6 | 2015-04-08 22:56:46 +0200 | [diff] [blame] | 1178 | drv_data = hid_get_drvdata(hid); |
| 1179 | if (!drv_data) { |
| 1180 | hid_err(hid, "Cannot add device, private driver data not allocated\n"); |
| 1181 | return -1; |
| 1182 | } |
| 1183 | entry = kzalloc(sizeof(*entry), GFP_KERNEL); |
| 1184 | if (!entry) |
| 1185 | return -ENOMEM; |
Michal Malý | c918fe7 | 2015-04-08 22:56:48 +0200 | [diff] [blame] | 1186 | spin_lock_init(&entry->report_lock); |
Michal Malý | c28abd8 | 2015-04-08 22:56:49 +0200 | [diff] [blame] | 1187 | entry->report = report; |
Michal Malý | 72529c6 | 2015-04-08 22:56:46 +0200 | [diff] [blame] | 1188 | drv_data->device_props = entry; |
| 1189 | |
Michal Malý | e7c2344 | 2015-02-18 17:59:20 +0100 | [diff] [blame] | 1190 | /* Check if a multimode wheel has been connected and |
| 1191 | * handle it appropriately */ |
Michal Malý | b96d23e | 2015-02-18 17:59:21 +0100 | [diff] [blame] | 1192 | mmode_ret = lg4ff_handle_multimode_wheel(hid, &real_product_id, bcdDevice); |
Michal Malý | e7c2344 | 2015-02-18 17:59:20 +0100 | [diff] [blame] | 1193 | |
| 1194 | /* Wheel has been told to switch to native mode. There is no point in going on |
| 1195 | * with the initialization as the wheel will do a USB reset when it switches mode |
| 1196 | */ |
Michal Malý | b96d23e | 2015-02-18 17:59:21 +0100 | [diff] [blame] | 1197 | if (mmode_ret == LG4FF_MMODE_SWITCHED) |
Michal Malý | e7c2344 | 2015-02-18 17:59:20 +0100 | [diff] [blame] | 1198 | return 0; |
Michal Malý | 72529c6 | 2015-04-08 22:56:46 +0200 | [diff] [blame] | 1199 | else if (mmode_ret < 0) { |
| 1200 | hid_err(hid, "Unable to switch device mode during initialization, errno %d\n", mmode_ret); |
| 1201 | error = mmode_ret; |
| 1202 | goto err_init; |
| 1203 | } |
Michal Malý | e7c2344 | 2015-02-18 17:59:20 +0100 | [diff] [blame] | 1204 | |
Michal Malý | 7362cd2 | 2011-08-04 16:16:09 +0200 | [diff] [blame] | 1205 | /* Check what wheel has been connected */ |
| 1206 | for (i = 0; i < ARRAY_SIZE(lg4ff_devices); i++) { |
| 1207 | if (hid->product == lg4ff_devices[i].product_id) { |
| 1208 | dbg_hid("Found compatible device, product ID %04X\n", lg4ff_devices[i].product_id); |
| 1209 | break; |
| 1210 | } |
| 1211 | } |
Simon Wood | 32c88cb | 2010-09-22 13:19:42 +0200 | [diff] [blame] | 1212 | |
Michal Malý | 7362cd2 | 2011-08-04 16:16:09 +0200 | [diff] [blame] | 1213 | if (i == ARRAY_SIZE(lg4ff_devices)) { |
Michal Malý | 9c2a6bd | 2015-04-08 22:56:44 +0200 | [diff] [blame] | 1214 | hid_err(hid, "This device is flagged to be handled by the lg4ff module but this module does not know how to handle it. " |
| 1215 | "Please report this as a bug to LKML, Simon Wood <simon@mungewell.org> or " |
| 1216 | "Michal Maly <madcatxster@devoid-pointer.net>\n"); |
Michal Malý | 72529c6 | 2015-04-08 22:56:46 +0200 | [diff] [blame] | 1217 | error = -1; |
| 1218 | goto err_init; |
Michal Malý | 7362cd2 | 2011-08-04 16:16:09 +0200 | [diff] [blame] | 1219 | } |
| 1220 | |
Michal Malý | b96d23e | 2015-02-18 17:59:21 +0100 | [diff] [blame] | 1221 | if (mmode_ret == LG4FF_MMODE_IS_MULTIMODE) { |
| 1222 | for (mmode_idx = 0; mmode_idx < ARRAY_SIZE(lg4ff_multimode_wheels); mmode_idx++) { |
| 1223 | if (real_product_id == lg4ff_multimode_wheels[mmode_idx].product_id) |
| 1224 | break; |
| 1225 | } |
| 1226 | |
| 1227 | if (mmode_idx == ARRAY_SIZE(lg4ff_multimode_wheels)) { |
| 1228 | 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] | 1229 | error = -1; |
| 1230 | goto err_init; |
Michal Malý | b96d23e | 2015-02-18 17:59:21 +0100 | [diff] [blame] | 1231 | } |
| 1232 | } |
| 1233 | |
Michal Malý | 7362cd2 | 2011-08-04 16:16:09 +0200 | [diff] [blame] | 1234 | /* Set supported force feedback capabilities */ |
| 1235 | for (j = 0; lg4ff_devices[i].ff_effects[j] >= 0; j++) |
| 1236 | set_bit(lg4ff_devices[i].ff_effects[j], dev->ffbit); |
Simon Wood | 32c88cb | 2010-09-22 13:19:42 +0200 | [diff] [blame] | 1237 | |
Michal Malý | d0afd84 | 2015-04-08 22:56:40 +0200 | [diff] [blame] | 1238 | error = input_ff_create_memless(dev, NULL, lg4ff_play); |
Simon Wood | 32c88cb | 2010-09-22 13:19:42 +0200 | [diff] [blame] | 1239 | |
| 1240 | if (error) |
Michal Malý | 72529c6 | 2015-04-08 22:56:46 +0200 | [diff] [blame] | 1241 | goto err_init; |
Simon Wood | 32c88cb | 2010-09-22 13:19:42 +0200 | [diff] [blame] | 1242 | |
Michal Malý | 5d9d60a | 2015-04-08 22:56:50 +0200 | [diff] [blame] | 1243 | /* Initialize device properties */ |
Michal Malý | b96d23e | 2015-02-18 17:59:21 +0100 | [diff] [blame] | 1244 | if (mmode_ret == LG4FF_MMODE_IS_MULTIMODE) { |
| 1245 | BUG_ON(mmode_idx == -1); |
Michal Malý | 5d9d60a | 2015-04-08 22:56:50 +0200 | [diff] [blame] | 1246 | mmode_wheel = &lg4ff_multimode_wheels[mmode_idx]; |
Michal Malý | b96d23e | 2015-02-18 17:59:21 +0100 | [diff] [blame] | 1247 | } |
Michal Malý | 5d9d60a | 2015-04-08 22:56:50 +0200 | [diff] [blame] | 1248 | 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] | 1249 | |
Simon Wood | 114a55c | 2013-11-06 12:30:43 -0700 | [diff] [blame] | 1250 | /* Check if autocentering is available and |
| 1251 | * set the centering force to zero by default */ |
| 1252 | if (test_bit(FF_AUTOCENTER, dev->ffbit)) { |
Michal Malý | e7c2344 | 2015-02-18 17:59:20 +0100 | [diff] [blame] | 1253 | /* Formula Force EX expects different autocentering command */ |
| 1254 | if ((bcdDevice >> 8) == LG4FF_FFEX_REV_MAJ && |
| 1255 | (bcdDevice & 0xff) == LG4FF_FFEX_REV_MIN) |
Michal Malý | d0afd84 | 2015-04-08 22:56:40 +0200 | [diff] [blame] | 1256 | dev->ff->set_autocenter = lg4ff_set_autocenter_ffex; |
Simon Wood | 114a55c | 2013-11-06 12:30:43 -0700 | [diff] [blame] | 1257 | else |
Michal Malý | d0afd84 | 2015-04-08 22:56:40 +0200 | [diff] [blame] | 1258 | dev->ff->set_autocenter = lg4ff_set_autocenter_default; |
Simon Wood | 114a55c | 2013-11-06 12:30:43 -0700 | [diff] [blame] | 1259 | |
| 1260 | dev->ff->set_autocenter(dev, 0); |
| 1261 | } |
| 1262 | |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 1263 | /* Create sysfs interface */ |
| 1264 | error = device_create_file(&hid->dev, &dev_attr_range); |
| 1265 | if (error) |
Michal Malý | d61a70ec | 2015-04-08 22:56:51 +0200 | [diff] [blame] | 1266 | 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] | 1267 | if (mmode_ret == LG4FF_MMODE_IS_MULTIMODE) { |
| 1268 | error = device_create_file(&hid->dev, &dev_attr_real_id); |
| 1269 | if (error) |
Michal Malý | d61a70ec | 2015-04-08 22:56:51 +0200 | [diff] [blame] | 1270 | 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] | 1271 | error = device_create_file(&hid->dev, &dev_attr_alternate_modes); |
| 1272 | if (error) |
Michal Malý | d61a70ec | 2015-04-08 22:56:51 +0200 | [diff] [blame] | 1273 | 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] | 1274 | } |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 1275 | dbg_hid("sysfs interface created\n"); |
| 1276 | |
| 1277 | /* Set the maximum range to start with */ |
Michal Malý | 72529c6 | 2015-04-08 22:56:46 +0200 | [diff] [blame] | 1278 | entry->wdata.range = entry->wdata.max_range; |
| 1279 | if (entry->wdata.set_range) |
| 1280 | entry->wdata.set_range(hid, entry->wdata.range); |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 1281 | |
Simon Wood | 22bcefd | 2012-04-21 05:41:15 -0700 | [diff] [blame] | 1282 | #ifdef CONFIG_LEDS_CLASS |
Simon Wood | 29fae1c | 2015-11-02 07:56:52 -0700 | [diff] [blame] | 1283 | /* register led subsystem - G27/G29 only */ |
Michal Malý | 72529c6 | 2015-04-08 22:56:46 +0200 | [diff] [blame] | 1284 | entry->wdata.led_state = 0; |
Simon Wood | 22bcefd | 2012-04-21 05:41:15 -0700 | [diff] [blame] | 1285 | for (j = 0; j < 5; j++) |
Michal Malý | 72529c6 | 2015-04-08 22:56:46 +0200 | [diff] [blame] | 1286 | entry->wdata.led[j] = NULL; |
Simon Wood | 22bcefd | 2012-04-21 05:41:15 -0700 | [diff] [blame] | 1287 | |
Simon Wood | 29fae1c | 2015-11-02 07:56:52 -0700 | [diff] [blame] | 1288 | if (lg4ff_devices[i].product_id == USB_DEVICE_ID_LOGITECH_G27_WHEEL || |
| 1289 | lg4ff_devices[i].product_id == USB_DEVICE_ID_LOGITECH_G29_WHEEL) { |
Simon Wood | 22bcefd | 2012-04-21 05:41:15 -0700 | [diff] [blame] | 1290 | struct led_classdev *led; |
| 1291 | size_t name_sz; |
| 1292 | char *name; |
| 1293 | |
| 1294 | lg4ff_set_leds(hid, 0); |
| 1295 | |
| 1296 | name_sz = strlen(dev_name(&hid->dev)) + 8; |
| 1297 | |
| 1298 | for (j = 0; j < 5; j++) { |
| 1299 | led = kzalloc(sizeof(struct led_classdev)+name_sz, GFP_KERNEL); |
| 1300 | if (!led) { |
| 1301 | hid_err(hid, "can't allocate memory for LED %d\n", j); |
Michal Malý | 72529c6 | 2015-04-08 22:56:46 +0200 | [diff] [blame] | 1302 | goto err_leds; |
Simon Wood | 22bcefd | 2012-04-21 05:41:15 -0700 | [diff] [blame] | 1303 | } |
| 1304 | |
| 1305 | name = (void *)(&led[1]); |
| 1306 | snprintf(name, name_sz, "%s::RPM%d", dev_name(&hid->dev), j+1); |
| 1307 | led->name = name; |
| 1308 | led->brightness = 0; |
| 1309 | led->max_brightness = 1; |
| 1310 | led->brightness_get = lg4ff_led_get_brightness; |
| 1311 | led->brightness_set = lg4ff_led_set_brightness; |
| 1312 | |
Michal Malý | 72529c6 | 2015-04-08 22:56:46 +0200 | [diff] [blame] | 1313 | entry->wdata.led[j] = led; |
Simon Wood | 22bcefd | 2012-04-21 05:41:15 -0700 | [diff] [blame] | 1314 | error = led_classdev_register(&hid->dev, led); |
| 1315 | |
| 1316 | if (error) { |
| 1317 | hid_err(hid, "failed to register LED %d. Aborting.\n", j); |
Michal Malý | 72529c6 | 2015-04-08 22:56:46 +0200 | [diff] [blame] | 1318 | err_leds: |
Simon Wood | 22bcefd | 2012-04-21 05:41:15 -0700 | [diff] [blame] | 1319 | /* Deregister LEDs (if any) */ |
| 1320 | for (j = 0; j < 5; j++) { |
Michal Malý | 72529c6 | 2015-04-08 22:56:46 +0200 | [diff] [blame] | 1321 | led = entry->wdata.led[j]; |
| 1322 | entry->wdata.led[j] = NULL; |
Simon Wood | 22bcefd | 2012-04-21 05:41:15 -0700 | [diff] [blame] | 1323 | if (!led) |
| 1324 | continue; |
| 1325 | led_classdev_unregister(led); |
| 1326 | kfree(led); |
| 1327 | } |
| 1328 | goto out; /* Let the driver continue without LEDs */ |
| 1329 | } |
| 1330 | } |
| 1331 | } |
Simon Wood | 22bcefd | 2012-04-21 05:41:15 -0700 | [diff] [blame] | 1332 | out: |
Jiri Kosina | c6e6dc8 | 2012-04-23 21:08:15 +0200 | [diff] [blame] | 1333 | #endif |
Simon Wood | 6401380 | 2012-04-21 05:41:16 -0700 | [diff] [blame] | 1334 | hid_info(hid, "Force feedback support for Logitech Gaming Wheels\n"); |
Simon Wood | 32c88cb | 2010-09-22 13:19:42 +0200 | [diff] [blame] | 1335 | return 0; |
Michal Malý | 72529c6 | 2015-04-08 22:56:46 +0200 | [diff] [blame] | 1336 | |
| 1337 | err_init: |
| 1338 | drv_data->device_props = NULL; |
| 1339 | kfree(entry); |
| 1340 | return error; |
Simon Wood | 32c88cb | 2010-09-22 13:19:42 +0200 | [diff] [blame] | 1341 | } |
| 1342 | |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 1343 | int lg4ff_deinit(struct hid_device *hid) |
| 1344 | { |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 1345 | struct lg4ff_device_entry *entry; |
Michal Malý | 3b6b17b | 2012-04-09 09:08:49 +0200 | [diff] [blame] | 1346 | struct lg_drv_data *drv_data; |
| 1347 | |
Michal Malý | 3b6b17b | 2012-04-09 09:08:49 +0200 | [diff] [blame] | 1348 | drv_data = hid_get_drvdata(hid); |
| 1349 | if (!drv_data) { |
| 1350 | hid_err(hid, "Error while deinitializing device, no private driver data.\n"); |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 1351 | return -1; |
| 1352 | } |
Michal Malý | 3b6b17b | 2012-04-09 09:08:49 +0200 | [diff] [blame] | 1353 | entry = drv_data->device_props; |
Michal Malý | e7c2344 | 2015-02-18 17:59:20 +0100 | [diff] [blame] | 1354 | if (!entry) |
| 1355 | goto out; /* Nothing more to do */ |
| 1356 | |
Michal Malý | b96d23e | 2015-02-18 17:59:21 +0100 | [diff] [blame] | 1357 | /* Multimode devices will have at least the "MODE_NATIVE" bit set */ |
Michal Malý | 72529c6 | 2015-04-08 22:56:46 +0200 | [diff] [blame] | 1358 | if (entry->wdata.alternate_modes) { |
Michal Malý | b96d23e | 2015-02-18 17:59:21 +0100 | [diff] [blame] | 1359 | device_remove_file(&hid->dev, &dev_attr_real_id); |
| 1360 | device_remove_file(&hid->dev, &dev_attr_alternate_modes); |
| 1361 | } |
| 1362 | |
Michal Malý | 72529c6 | 2015-04-08 22:56:46 +0200 | [diff] [blame] | 1363 | device_remove_file(&hid->dev, &dev_attr_range); |
Simon Wood | 22bcefd | 2012-04-21 05:41:15 -0700 | [diff] [blame] | 1364 | #ifdef CONFIG_LEDS_CLASS |
| 1365 | { |
| 1366 | int j; |
| 1367 | struct led_classdev *led; |
| 1368 | |
| 1369 | /* Deregister LEDs (if any) */ |
| 1370 | for (j = 0; j < 5; j++) { |
| 1371 | |
Michal Malý | 72529c6 | 2015-04-08 22:56:46 +0200 | [diff] [blame] | 1372 | led = entry->wdata.led[j]; |
| 1373 | entry->wdata.led[j] = NULL; |
Simon Wood | 22bcefd | 2012-04-21 05:41:15 -0700 | [diff] [blame] | 1374 | if (!led) |
| 1375 | continue; |
| 1376 | led_classdev_unregister(led); |
| 1377 | kfree(led); |
| 1378 | } |
| 1379 | } |
| 1380 | #endif |
Michal Malý | b211a63 | 2015-04-08 22:56:47 +0200 | [diff] [blame] | 1381 | hid_hw_stop(hid); |
| 1382 | drv_data->device_props = NULL; |
Simon Wood | 22bcefd | 2012-04-21 05:41:15 -0700 | [diff] [blame] | 1383 | |
Michal Malý | 3b6b17b | 2012-04-09 09:08:49 +0200 | [diff] [blame] | 1384 | kfree(entry); |
Michal Malý | e7c2344 | 2015-02-18 17:59:20 +0100 | [diff] [blame] | 1385 | out: |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame] | 1386 | dbg_hid("Device successfully unregistered\n"); |
| 1387 | return 0; |
| 1388 | } |