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