Simon Wood | 32c88cb | 2010-09-22 13:19:42 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Force feedback support for Logitech Speed Force Wireless |
| 3 | * |
| 4 | * http://wiibrew.org/wiki/Logitech_USB_steering_wheel |
| 5 | * |
| 6 | * Copyright (c) 2010 Simon Wood <simon@mungewell.org> |
| 7 | */ |
| 8 | |
| 9 | /* |
| 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License as published by |
| 12 | * the Free Software Foundation; either version 2 of the License, or |
| 13 | * (at your option) any later version. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU General Public License |
| 21 | * along with this program; if not, write to the Free Software |
| 22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 23 | */ |
| 24 | |
| 25 | |
| 26 | #include <linux/input.h> |
| 27 | #include <linux/usb.h> |
| 28 | #include <linux/hid.h> |
| 29 | |
| 30 | #include "usbhid/usbhid.h" |
| 31 | #include "hid-lg.h" |
Michal Malý | 7362cd2 | 2011-08-04 16:16:09 +0200 | [diff] [blame] | 32 | #include "hid-ids.h" |
Simon Wood | 32c88cb | 2010-09-22 13:19:42 +0200 | [diff] [blame] | 33 | |
Michal Malý | 96440c8 | 2011-08-04 16:18:11 +0200 | [diff] [blame] | 34 | #define DFGT_REV_MAJ 0x13 |
| 35 | #define DFGT_REV_MIN 0x22 |
| 36 | #define DFP_REV_MAJ 0x11 |
| 37 | #define DFP_REV_MIN 0x06 |
| 38 | #define FFEX_REV_MAJ 0x21 |
| 39 | #define FFEX_REV_MIN 0x00 |
| 40 | #define G25_REV_MAJ 0x12 |
| 41 | #define G25_REV_MIN 0x22 |
| 42 | #define G27_REV_MAJ 0x12 |
| 43 | #define G27_REV_MIN 0x38 |
| 44 | |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame^] | 45 | #define to_hid_device(pdev) container_of(pdev, struct hid_device, dev) |
| 46 | |
| 47 | static void hid_lg4ff_set_range_dfp(struct hid_device *hid, u16 range); |
| 48 | static void hid_lg4ff_set_range_g25(struct hid_device *hid, u16 range); |
| 49 | static ssize_t lg4ff_range_show(struct device *dev, struct device_attribute *attr, char *buf); |
| 50 | static ssize_t lg4ff_range_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count); |
| 51 | |
| 52 | static DEVICE_ATTR(range, S_IRWXU | S_IRWXG | S_IRWXO, lg4ff_range_show, lg4ff_range_store); |
| 53 | |
| 54 | static bool list_inited; |
| 55 | |
| 56 | struct lg4ff_device_entry { |
| 57 | char *device_id; /* Use name in respective kobject structure's address as the ID */ |
| 58 | __u16 range; |
| 59 | __u16 min_range; |
| 60 | __u16 max_range; |
| 61 | __u8 leds; |
| 62 | struct list_head list; |
| 63 | void (*set_range)(struct hid_device *hid, u16 range); |
| 64 | }; |
| 65 | |
| 66 | static struct lg4ff_device_entry device_list; |
| 67 | |
Michal Malý | 7362cd2 | 2011-08-04 16:16:09 +0200 | [diff] [blame] | 68 | static const signed short lg4ff_wheel_effects[] = { |
Simon Wood | 32c88cb | 2010-09-22 13:19:42 +0200 | [diff] [blame] | 69 | FF_CONSTANT, |
| 70 | FF_AUTOCENTER, |
| 71 | -1 |
| 72 | }; |
| 73 | |
Michal Malý | 7362cd2 | 2011-08-04 16:16:09 +0200 | [diff] [blame] | 74 | struct lg4ff_wheel { |
| 75 | const __u32 product_id; |
| 76 | const signed short *ff_effects; |
| 77 | const __u16 min_range; |
| 78 | const __u16 max_range; |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame^] | 79 | void (*set_range)(struct hid_device *hid, u16 range); |
Michal Malý | 7362cd2 | 2011-08-04 16:16:09 +0200 | [diff] [blame] | 80 | }; |
| 81 | |
| 82 | static const struct lg4ff_wheel lg4ff_devices[] = { |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame^] | 83 | {USB_DEVICE_ID_LOGITECH_WHEEL, lg4ff_wheel_effects, 40, 270, NULL}, |
| 84 | {USB_DEVICE_ID_LOGITECH_MOMO_WHEEL, lg4ff_wheel_effects, 40, 270, NULL}, |
| 85 | {USB_DEVICE_ID_LOGITECH_DFP_WHEEL, lg4ff_wheel_effects, 40, 900, hid_lg4ff_set_range_dfp}, |
| 86 | {USB_DEVICE_ID_LOGITECH_G25_WHEEL, lg4ff_wheel_effects, 40, 900, hid_lg4ff_set_range_g25}, |
| 87 | {USB_DEVICE_ID_LOGITECH_DFGT_WHEEL, lg4ff_wheel_effects, 40, 900, hid_lg4ff_set_range_g25}, |
| 88 | {USB_DEVICE_ID_LOGITECH_G27_WHEEL, lg4ff_wheel_effects, 40, 900, hid_lg4ff_set_range_g25}, |
| 89 | {USB_DEVICE_ID_LOGITECH_MOMO_WHEEL2, lg4ff_wheel_effects, 40, 270, NULL}, |
| 90 | {USB_DEVICE_ID_LOGITECH_WII_WHEEL, lg4ff_wheel_effects, 40, 270, NULL} |
Michal Malý | 7362cd2 | 2011-08-04 16:16:09 +0200 | [diff] [blame] | 91 | }; |
| 92 | |
Michal Malý | 96440c8 | 2011-08-04 16:18:11 +0200 | [diff] [blame] | 93 | struct lg4ff_native_cmd { |
| 94 | const __u8 cmd_num; /* Number of commands to send */ |
| 95 | const __u8 cmd[]; |
| 96 | }; |
| 97 | |
| 98 | struct lg4ff_usb_revision { |
| 99 | const __u16 rev_maj; |
| 100 | const __u16 rev_min; |
| 101 | const struct lg4ff_native_cmd *command; |
| 102 | }; |
| 103 | |
| 104 | static const struct lg4ff_native_cmd native_dfp = { |
| 105 | 1, |
| 106 | {0xf8, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00} |
| 107 | }; |
| 108 | |
| 109 | static const struct lg4ff_native_cmd native_dfgt = { |
| 110 | 2, |
| 111 | {0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, /* 1st command */ |
| 112 | 0xf8, 0x09, 0x03, 0x01, 0x00, 0x00, 0x00} /* 2nd command */ |
| 113 | }; |
| 114 | |
| 115 | static const struct lg4ff_native_cmd native_g25 = { |
| 116 | 1, |
| 117 | {0xf8, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00} |
| 118 | }; |
| 119 | |
| 120 | static const struct lg4ff_native_cmd native_g27 = { |
| 121 | 2, |
| 122 | {0xf8, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, /* 1st command */ |
| 123 | 0xf8, 0x09, 0x04, 0x01, 0x00, 0x00, 0x00} /* 2nd command */ |
| 124 | }; |
| 125 | |
| 126 | static const struct lg4ff_usb_revision lg4ff_revs[] = { |
| 127 | {DFGT_REV_MAJ, DFGT_REV_MIN, &native_dfgt}, /* Driving Force GT */ |
| 128 | {DFP_REV_MAJ, DFP_REV_MIN, &native_dfp}, /* Driving Force Pro */ |
| 129 | {G25_REV_MAJ, G25_REV_MIN, &native_g25}, /* G25 */ |
| 130 | {G27_REV_MAJ, G27_REV_MIN, &native_g27}, /* G27 */ |
| 131 | }; |
| 132 | |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame^] | 133 | static int hid_lg4ff_play(struct input_dev *dev, void *data, struct ff_effect *effect) |
Simon Wood | 32c88cb | 2010-09-22 13:19:42 +0200 | [diff] [blame] | 134 | { |
| 135 | struct hid_device *hid = input_get_drvdata(dev); |
| 136 | struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list; |
| 137 | struct hid_report *report = list_entry(report_list->next, struct hid_report, list); |
| 138 | int x; |
| 139 | |
| 140 | #define CLAMP(x) if (x < 0) x = 0; if (x > 0xff) x = 0xff |
| 141 | |
| 142 | switch (effect->type) { |
| 143 | case FF_CONSTANT: |
| 144 | x = effect->u.ramp.start_level + 0x80; /* 0x80 is no force */ |
| 145 | CLAMP(x); |
| 146 | report->field[0]->value[0] = 0x11; /* Slot 1 */ |
Michal Malý | 7362cd2 | 2011-08-04 16:16:09 +0200 | [diff] [blame] | 147 | report->field[0]->value[1] = 0x08; |
Simon Wood | 32c88cb | 2010-09-22 13:19:42 +0200 | [diff] [blame] | 148 | report->field[0]->value[2] = x; |
Michal Malý | 7362cd2 | 2011-08-04 16:16:09 +0200 | [diff] [blame] | 149 | report->field[0]->value[3] = 0x80; |
Simon Wood | 32c88cb | 2010-09-22 13:19:42 +0200 | [diff] [blame] | 150 | report->field[0]->value[4] = 0x00; |
Michal Malý | 7362cd2 | 2011-08-04 16:16:09 +0200 | [diff] [blame] | 151 | report->field[0]->value[5] = 0x00; |
Simon Wood | 32c88cb | 2010-09-22 13:19:42 +0200 | [diff] [blame] | 152 | report->field[0]->value[6] = 0x00; |
Simon Wood | 32c88cb | 2010-09-22 13:19:42 +0200 | [diff] [blame] | 153 | |
| 154 | usbhid_submit_report(hid, report, USB_DIR_OUT); |
| 155 | break; |
| 156 | } |
| 157 | return 0; |
| 158 | } |
| 159 | |
| 160 | static void hid_lg4ff_set_autocenter(struct input_dev *dev, u16 magnitude) |
| 161 | { |
| 162 | struct hid_device *hid = input_get_drvdata(dev); |
| 163 | struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list; |
| 164 | struct hid_report *report = list_entry(report_list->next, struct hid_report, list); |
Simon Wood | 32c88cb | 2010-09-22 13:19:42 +0200 | [diff] [blame] | 165 | |
Michal Malý | 7362cd2 | 2011-08-04 16:16:09 +0200 | [diff] [blame] | 166 | report->field[0]->value[0] = 0xfe; |
| 167 | report->field[0]->value[1] = 0x0d; |
| 168 | report->field[0]->value[2] = magnitude >> 13; |
| 169 | report->field[0]->value[3] = magnitude >> 13; |
| 170 | report->field[0]->value[4] = magnitude >> 8; |
| 171 | report->field[0]->value[5] = 0x00; |
| 172 | report->field[0]->value[6] = 0x00; |
Simon Wood | 32c88cb | 2010-09-22 13:19:42 +0200 | [diff] [blame] | 173 | |
| 174 | usbhid_submit_report(hid, report, USB_DIR_OUT); |
| 175 | } |
| 176 | |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame^] | 177 | /* Sends command to set range compatible with G25/G27/Driving Force GT */ |
| 178 | static void hid_lg4ff_set_range_g25(struct hid_device *hid, u16 range) |
| 179 | { |
| 180 | struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list; |
| 181 | struct hid_report *report = list_entry(report_list->next, struct hid_report, list); |
| 182 | dbg_hid("G25/G27/DFGT: setting range to %u\n", range); |
| 183 | |
| 184 | report->field[0]->value[0] = 0xf8; |
| 185 | report->field[0]->value[1] = 0x81; |
| 186 | report->field[0]->value[2] = range & 0x00ff; |
| 187 | report->field[0]->value[3] = (range & 0xff00) >> 8; |
| 188 | report->field[0]->value[4] = 0x00; |
| 189 | report->field[0]->value[5] = 0x00; |
| 190 | report->field[0]->value[6] = 0x00; |
| 191 | |
| 192 | usbhid_submit_report(hid, report, USB_DIR_OUT); |
| 193 | } |
| 194 | |
| 195 | /* Sends commands to set range compatible with Driving Force Pro wheel */ |
| 196 | static void hid_lg4ff_set_range_dfp(struct hid_device *hid, __u16 range) |
| 197 | { |
| 198 | struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list; |
| 199 | struct hid_report *report = list_entry(report_list->next, struct hid_report, list); |
| 200 | int start_left, start_right, full_range; |
| 201 | dbg_hid("Driving Force Pro: setting range to %u\n", range); |
| 202 | |
| 203 | /* Prepare "coarse" limit command */ |
| 204 | report->field[0]->value[0] = 0xf8; |
| 205 | report->field[0]->value[1] = 0x00; /* Set later */ |
| 206 | report->field[0]->value[2] = 0x00; |
| 207 | report->field[0]->value[3] = 0x00; |
| 208 | report->field[0]->value[4] = 0x00; |
| 209 | report->field[0]->value[5] = 0x00; |
| 210 | report->field[0]->value[6] = 0x00; |
| 211 | |
| 212 | if (range > 200) { |
| 213 | report->field[0]->value[1] = 0x03; |
| 214 | full_range = 900; |
| 215 | } else { |
| 216 | report->field[0]->value[1] = 0x02; |
| 217 | full_range = 200; |
| 218 | } |
| 219 | usbhid_submit_report(hid, report, USB_DIR_OUT); |
| 220 | |
| 221 | /* Prepare "fine" limit command */ |
| 222 | report->field[0]->value[0] = 0x81; |
| 223 | report->field[0]->value[1] = 0x0b; |
| 224 | report->field[0]->value[2] = 0x00; |
| 225 | report->field[0]->value[3] = 0x00; |
| 226 | report->field[0]->value[4] = 0x00; |
| 227 | report->field[0]->value[5] = 0x00; |
| 228 | report->field[0]->value[6] = 0x00; |
| 229 | |
| 230 | if (range == 200 || range == 900) { /* Do not apply any fine limit */ |
| 231 | usbhid_submit_report(hid, report, USB_DIR_OUT); |
| 232 | return; |
| 233 | } |
| 234 | |
| 235 | /* Construct fine limit command */ |
| 236 | start_left = (((full_range - range + 1) * 2047) / full_range); |
| 237 | start_right = 0xfff - start_left; |
| 238 | |
| 239 | report->field[0]->value[2] = start_left >> 4; |
| 240 | report->field[0]->value[3] = start_right >> 4; |
| 241 | report->field[0]->value[4] = 0xff; |
| 242 | report->field[0]->value[5] = (start_right & 0xe) << 4 | (start_left & 0xe); |
| 243 | report->field[0]->value[6] = 0xff; |
| 244 | |
| 245 | usbhid_submit_report(hid, report, USB_DIR_OUT); |
| 246 | } |
| 247 | |
Michal Malý | 96440c8 | 2011-08-04 16:18:11 +0200 | [diff] [blame] | 248 | static void hid_lg4ff_switch_native(struct hid_device *hid, const struct lg4ff_native_cmd *cmd) |
| 249 | { |
| 250 | struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list; |
| 251 | struct hid_report *report = list_entry(report_list->next, struct hid_report, list); |
| 252 | __u8 i, j; |
| 253 | |
| 254 | j = 0; |
| 255 | while (j < 7*cmd->cmd_num) { |
| 256 | for (i = 0; i < 7; i++) |
| 257 | report->field[0]->value[i] = cmd->cmd[j++]; |
| 258 | |
| 259 | usbhid_submit_report(hid, report, USB_DIR_OUT); |
| 260 | } |
| 261 | } |
Simon Wood | 32c88cb | 2010-09-22 13:19:42 +0200 | [diff] [blame] | 262 | |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame^] | 263 | /* Read current range and display it in terminal */ |
| 264 | static ssize_t lg4ff_range_show(struct device *dev, struct device_attribute *attr, char *buf) |
| 265 | { |
| 266 | struct lg4ff_device_entry *entry = 0; |
| 267 | struct list_head *h; |
| 268 | struct hid_device *hid = to_hid_device(dev); |
| 269 | size_t count; |
| 270 | |
| 271 | list_for_each(h, &device_list.list) { |
| 272 | entry = list_entry(h, struct lg4ff_device_entry, list); |
| 273 | if (strcmp(entry->device_id, (&hid->dev)->kobj.name) == 0) |
| 274 | break; |
| 275 | } |
| 276 | if (h == &device_list.list) { |
| 277 | dbg_hid("Device not found!"); |
| 278 | return 0; |
| 279 | } |
| 280 | |
| 281 | count = scnprintf(buf, PAGE_SIZE, "%u\n", entry->range); |
| 282 | return count; |
| 283 | } |
| 284 | |
| 285 | /* Set range to user specified value, call appropriate function |
| 286 | * according to the type of the wheel */ |
| 287 | static ssize_t lg4ff_range_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) |
| 288 | { |
| 289 | struct lg4ff_device_entry *entry = 0; |
| 290 | struct list_head *h; |
| 291 | struct hid_device *hid = to_hid_device(dev); |
| 292 | __u16 range = simple_strtoul(buf, NULL, 10); |
| 293 | |
| 294 | list_for_each(h, &device_list.list) { |
| 295 | entry = list_entry(h, struct lg4ff_device_entry, list); |
| 296 | if (strcmp(entry->device_id, (&hid->dev)->kobj.name) == 0) |
| 297 | break; |
| 298 | } |
| 299 | if (h == &device_list.list) { |
| 300 | dbg_hid("Device not found!"); |
| 301 | return count; |
| 302 | } |
| 303 | |
| 304 | if (range == 0) |
| 305 | range = entry->max_range; |
| 306 | |
| 307 | /* Check if the wheel supports range setting |
| 308 | * and that the range is within limits for the wheel */ |
| 309 | if (entry->set_range != NULL && range >= entry->min_range && range <= entry->max_range) { |
| 310 | entry->set_range(hid, range); |
| 311 | entry->range = range; |
| 312 | } |
| 313 | |
| 314 | return count; |
| 315 | } |
| 316 | |
Simon Wood | 32c88cb | 2010-09-22 13:19:42 +0200 | [diff] [blame] | 317 | int lg4ff_init(struct hid_device *hid) |
| 318 | { |
| 319 | struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list); |
| 320 | struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list; |
| 321 | struct input_dev *dev = hidinput->input; |
| 322 | struct hid_report *report; |
| 323 | struct hid_field *field; |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame^] | 324 | struct lg4ff_device_entry *entry; |
| 325 | struct usb_device_descriptor *udesc; |
Michal Malý | 7362cd2 | 2011-08-04 16:16:09 +0200 | [diff] [blame] | 326 | int error, i, j; |
Michal Malý | 96440c8 | 2011-08-04 16:18:11 +0200 | [diff] [blame] | 327 | __u16 bcdDevice, rev_maj, rev_min; |
Simon Wood | 32c88cb | 2010-09-22 13:19:42 +0200 | [diff] [blame] | 328 | |
| 329 | /* Find the report to use */ |
| 330 | if (list_empty(report_list)) { |
Joe Perches | 4291ee3 | 2010-12-09 19:29:03 -0800 | [diff] [blame] | 331 | hid_err(hid, "No output report found\n"); |
Simon Wood | 32c88cb | 2010-09-22 13:19:42 +0200 | [diff] [blame] | 332 | return -1; |
| 333 | } |
| 334 | |
| 335 | /* Check that the report looks ok */ |
| 336 | report = list_entry(report_list->next, struct hid_report, list); |
| 337 | if (!report) { |
Joe Perches | 4291ee3 | 2010-12-09 19:29:03 -0800 | [diff] [blame] | 338 | hid_err(hid, "NULL output report\n"); |
Simon Wood | 32c88cb | 2010-09-22 13:19:42 +0200 | [diff] [blame] | 339 | return -1; |
| 340 | } |
| 341 | |
| 342 | field = report->field[0]; |
| 343 | if (!field) { |
Joe Perches | 4291ee3 | 2010-12-09 19:29:03 -0800 | [diff] [blame] | 344 | hid_err(hid, "NULL field\n"); |
Simon Wood | 32c88cb | 2010-09-22 13:19:42 +0200 | [diff] [blame] | 345 | return -1; |
| 346 | } |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame^] | 347 | |
Michal Malý | 7362cd2 | 2011-08-04 16:16:09 +0200 | [diff] [blame] | 348 | /* Check what wheel has been connected */ |
| 349 | for (i = 0; i < ARRAY_SIZE(lg4ff_devices); i++) { |
| 350 | if (hid->product == lg4ff_devices[i].product_id) { |
| 351 | dbg_hid("Found compatible device, product ID %04X\n", lg4ff_devices[i].product_id); |
| 352 | break; |
| 353 | } |
| 354 | } |
Simon Wood | 32c88cb | 2010-09-22 13:19:42 +0200 | [diff] [blame] | 355 | |
Michal Malý | 7362cd2 | 2011-08-04 16:16:09 +0200 | [diff] [blame] | 356 | if (i == ARRAY_SIZE(lg4ff_devices)) { |
| 357 | hid_err(hid, "Device is not supported by lg4ff driver. If you think it should be, consider reporting a bug to" |
| 358 | "LKML, Simon Wood <simon@mungewell.org> or Michal Maly <madcatxster@gmail.com>\n"); |
| 359 | return -1; |
| 360 | } |
| 361 | |
Michal Malý | 96440c8 | 2011-08-04 16:18:11 +0200 | [diff] [blame] | 362 | /* Attempt to switch wheel to native mode when applicable */ |
| 363 | udesc = &(hid_to_usb_dev(hid)->descriptor); |
| 364 | if (!udesc) { |
| 365 | hid_err(hid, "NULL USB device descriptor\n"); |
| 366 | return -1; |
| 367 | } |
| 368 | bcdDevice = le16_to_cpu(udesc->bcdDevice); |
| 369 | rev_maj = bcdDevice >> 8; |
| 370 | rev_min = bcdDevice & 0xff; |
| 371 | |
| 372 | if (lg4ff_devices[i].product_id == USB_DEVICE_ID_LOGITECH_WHEEL) { |
| 373 | dbg_hid("Generic wheel detected, can it do native?\n"); |
| 374 | dbg_hid("USB revision: %2x.%02x\n", rev_maj, rev_min); |
| 375 | |
| 376 | for (j = 0; j < ARRAY_SIZE(lg4ff_revs); j++) { |
| 377 | if (lg4ff_revs[j].rev_maj == rev_maj && lg4ff_revs[j].rev_min == rev_min) { |
| 378 | hid_lg4ff_switch_native(hid, lg4ff_revs[j].command); |
| 379 | hid_info(hid, "Switched to native mode\n"); |
| 380 | } |
| 381 | } |
| 382 | } |
| 383 | |
Michal Malý | 7362cd2 | 2011-08-04 16:16:09 +0200 | [diff] [blame] | 384 | /* Set supported force feedback capabilities */ |
| 385 | for (j = 0; lg4ff_devices[i].ff_effects[j] >= 0; j++) |
| 386 | set_bit(lg4ff_devices[i].ff_effects[j], dev->ffbit); |
Simon Wood | 32c88cb | 2010-09-22 13:19:42 +0200 | [diff] [blame] | 387 | |
| 388 | error = input_ff_create_memless(dev, NULL, hid_lg4ff_play); |
| 389 | |
| 390 | if (error) |
| 391 | return error; |
| 392 | |
| 393 | if (test_bit(FF_AUTOCENTER, dev->ffbit)) |
| 394 | dev->ff->set_autocenter = hid_lg4ff_set_autocenter; |
| 395 | |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame^] | 396 | /* Initialize device_list if this is the first device to handle by lg4ff */ |
| 397 | if (!list_inited) { |
| 398 | INIT_LIST_HEAD(&device_list.list); |
| 399 | list_inited = 1; |
| 400 | } |
| 401 | |
| 402 | /* Add the device to device_list */ |
| 403 | entry = (struct lg4ff_device_entry *)kzalloc(sizeof(struct lg4ff_device_entry), GFP_KERNEL); |
| 404 | if (!entry) { |
| 405 | hid_err(hid, "Cannot add device, insufficient memory.\n"); |
| 406 | return -ENOMEM; |
| 407 | } |
| 408 | entry->device_id = (char *)kzalloc(strlen((&hid->dev)->kobj.name) + 1, GFP_KERNEL); |
| 409 | if (!entry->device_id) { |
| 410 | hid_err(hid, "Cannot set device_id, insufficient memory.\n"); |
| 411 | return -ENOMEM; |
| 412 | } |
| 413 | strcpy(entry->device_id, (&hid->dev)->kobj.name); |
| 414 | entry->min_range = lg4ff_devices[i].min_range; |
| 415 | entry->max_range = lg4ff_devices[i].max_range; |
| 416 | entry->set_range = lg4ff_devices[i].set_range; |
| 417 | list_add(&entry->list, &device_list.list); |
| 418 | |
| 419 | /* Create sysfs interface */ |
| 420 | error = device_create_file(&hid->dev, &dev_attr_range); |
| 421 | if (error) |
| 422 | return error; |
| 423 | dbg_hid("sysfs interface created\n"); |
| 424 | |
| 425 | /* Set the maximum range to start with */ |
| 426 | entry->range = entry->max_range; |
| 427 | if (entry->set_range != NULL) |
| 428 | entry->set_range(hid, entry->range); |
| 429 | |
Joe Perches | 4291ee3 | 2010-12-09 19:29:03 -0800 | [diff] [blame] | 430 | hid_info(hid, "Force feedback for Logitech Speed Force Wireless by Simon Wood <simon@mungewell.org>\n"); |
Simon Wood | 32c88cb | 2010-09-22 13:19:42 +0200 | [diff] [blame] | 431 | return 0; |
| 432 | } |
| 433 | |
Michal Malý | 30bb75d | 2011-08-04 16:20:40 +0200 | [diff] [blame^] | 434 | int lg4ff_deinit(struct hid_device *hid) |
| 435 | { |
| 436 | bool found = 0; |
| 437 | struct lg4ff_device_entry *entry; |
| 438 | struct list_head *h, *g; |
| 439 | list_for_each_safe(h, g, &device_list.list) { |
| 440 | entry = list_entry(h, struct lg4ff_device_entry, list); |
| 441 | if (strcmp(entry->device_id, (&hid->dev)->kobj.name) == 0) { |
| 442 | list_del(h); |
| 443 | kfree(entry->device_id); |
| 444 | kfree(entry); |
| 445 | found = 1; |
| 446 | break; |
| 447 | } |
| 448 | } |
| 449 | |
| 450 | if (!found) { |
| 451 | dbg_hid("Device entry not found!\n"); |
| 452 | return -1; |
| 453 | } |
| 454 | |
| 455 | device_remove_file(&hid->dev, &dev_attr_range); |
| 456 | dbg_hid("Device successfully unregistered\n"); |
| 457 | return 0; |
| 458 | } |