/*1* Force feedback support for Logitech Flight System G9402*3* Copyright (c) 2009 Gary Stein <[email protected]>4*/56/*7* This program is free software; you can redistribute it and/or modify8* it under the terms of the GNU General Public License as published by9* the Free Software Foundation; either version 2 of the License, or10* (at your option) any later version.11*12* This program is distributed in the hope that it will be useful,13* but WITHOUT ANY WARRANTY; without even the implied warranty of14* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the15* GNU General Public License for more details.16*17* You should have received a copy of the GNU General Public License18* along with this program; if not, write to the Free Software19* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA20*/212223#include <linux/input.h>24#include <linux/usb.h>25#include <linux/hid.h>2627#include "usbhid/usbhid.h"28#include "hid-lg.h"2930/*31* G940 Theory of Operation (from experimentation)32*33* There are 63 fields (only 3 of them currently used)34* 0 - seems to be command field35* 1 - 30 deal with the x axis36* 31 -60 deal with the y axis37*38* Field 1 is x axis constant force39* Field 31 is y axis constant force40*41* other interesting fields 1,2,3,4 on x axis42* (same for 31,32,33,34 on y axis)43*44* 0 0 127 127 makes the joystick autocenter hard45*46* 127 0 127 127 makes the joystick loose on the right,47* but stops all movemnt left48*49* -127 0 -127 -127 makes the joystick loose on the left,50* but stops all movement right51*52* 0 0 -127 -127 makes the joystick rattle very hard53*54* I'm sure these are effects that I don't know enough about them55*/5657struct lg3ff_device {58struct hid_report *report;59};6061static int hid_lg3ff_play(struct input_dev *dev, void *data,62struct ff_effect *effect)63{64struct hid_device *hid = input_get_drvdata(dev);65struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;66struct hid_report *report = list_entry(report_list->next, struct hid_report, list);67int x, y;6869/*70* Maxusage should always be 63 (maximum fields)71* likely a better way to ensure this data is clean72*/73memset(report->field[0]->value, 0, sizeof(__s32)*report->field[0]->maxusage);7475switch (effect->type) {76case FF_CONSTANT:77/*78* Already clamped in ff_memless79* 0 is center (different then other logitech)80*/81x = effect->u.ramp.start_level;82y = effect->u.ramp.end_level;8384/* send command byte */85report->field[0]->value[0] = 0x51;8687/*88* Sign backwards from other Force3d pro89* which get recast here in two's complement 8 bits90*/91report->field[0]->value[1] = (unsigned char)(-x);92report->field[0]->value[31] = (unsigned char)(-y);9394usbhid_submit_report(hid, report, USB_DIR_OUT);95break;96}97return 0;98}99static void hid_lg3ff_set_autocenter(struct input_dev *dev, u16 magnitude)100{101struct hid_device *hid = input_get_drvdata(dev);102struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;103struct hid_report *report = list_entry(report_list->next, struct hid_report, list);104105/*106* Auto Centering probed from device107* NOTE: deadman's switch on G940 must be covered108* for effects to work109*/110report->field[0]->value[0] = 0x51;111report->field[0]->value[1] = 0x00;112report->field[0]->value[2] = 0x00;113report->field[0]->value[3] = 0x7F;114report->field[0]->value[4] = 0x7F;115report->field[0]->value[31] = 0x00;116report->field[0]->value[32] = 0x00;117report->field[0]->value[33] = 0x7F;118report->field[0]->value[34] = 0x7F;119120usbhid_submit_report(hid, report, USB_DIR_OUT);121}122123124static const signed short ff3_joystick_ac[] = {125FF_CONSTANT,126FF_AUTOCENTER,127-1128};129130int lg3ff_init(struct hid_device *hid)131{132struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);133struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;134struct input_dev *dev = hidinput->input;135struct hid_report *report;136struct hid_field *field;137const signed short *ff_bits = ff3_joystick_ac;138int error;139int i;140141/* Find the report to use */142if (list_empty(report_list)) {143hid_err(hid, "No output report found\n");144return -1;145}146147/* Check that the report looks ok */148report = list_entry(report_list->next, struct hid_report, list);149if (!report) {150hid_err(hid, "NULL output report\n");151return -1;152}153154field = report->field[0];155if (!field) {156hid_err(hid, "NULL field\n");157return -1;158}159160/* Assume single fixed device G940 */161for (i = 0; ff_bits[i] >= 0; i++)162set_bit(ff_bits[i], dev->ffbit);163164error = input_ff_create_memless(dev, NULL, hid_lg3ff_play);165if (error)166return error;167168if (test_bit(FF_AUTOCENTER, dev->ffbit))169dev->ff->set_autocenter = hid_lg3ff_set_autocenter;170171hid_info(hid, "Force feedback for Logitech Flight System G940 by Gary Stein <[email protected]>\n");172return 0;173}174175176177