xref: /openbmc/linux/drivers/hid/hid-lg4ff.c (revision 8ac727c1)
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"
32 
33 struct lg4ff_device {
34 	struct hid_report *report;
35 };
36 
37 static const signed short ff4_wheel_ac[] = {
38 	FF_CONSTANT,
39 	FF_AUTOCENTER,
40 	-1
41 };
42 
43 static int hid_lg4ff_play(struct input_dev *dev, void *data,
44 			 struct ff_effect *effect)
45 {
46 	struct hid_device *hid = input_get_drvdata(dev);
47 	struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
48 	struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
49 	int x;
50 
51 #define CLAMP(x) if (x < 0) x = 0; if (x > 0xff) x = 0xff
52 
53 	switch (effect->type) {
54 	case FF_CONSTANT:
55 		x = effect->u.ramp.start_level + 0x80;	/* 0x80 is no force */
56 		CLAMP(x);
57 		report->field[0]->value[0] = 0x11;	/* Slot 1 */
58 		report->field[0]->value[1] = 0x10;
59 		report->field[0]->value[2] = x;
60 		report->field[0]->value[3] = 0x00;
61 		report->field[0]->value[4] = 0x00;
62 		report->field[0]->value[5] = 0x08;
63 		report->field[0]->value[6] = 0x00;
64 		dbg_hid("Autocenter, x=0x%02X\n", x);
65 
66 		usbhid_submit_report(hid, report, USB_DIR_OUT);
67 		break;
68 	}
69 	return 0;
70 }
71 
72 static void hid_lg4ff_set_autocenter(struct input_dev *dev, u16 magnitude)
73 {
74 	struct hid_device *hid = input_get_drvdata(dev);
75 	struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
76 	struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
77 	__s32 *value = report->field[0]->value;
78 
79 	*value++ = 0xfe;
80 	*value++ = 0x0d;
81 	*value++ = 0x07;
82 	*value++ = 0x07;
83 	*value++ = (magnitude >> 8) & 0xff;
84 	*value++ = 0x00;
85 	*value = 0x00;
86 
87 	usbhid_submit_report(hid, report, USB_DIR_OUT);
88 }
89 
90 
91 int lg4ff_init(struct hid_device *hid)
92 {
93 	struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
94 	struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
95 	struct input_dev *dev = hidinput->input;
96 	struct hid_report *report;
97 	struct hid_field *field;
98 	const signed short *ff_bits = ff4_wheel_ac;
99 	int error;
100 	int i;
101 
102 	/* Find the report to use */
103 	if (list_empty(report_list)) {
104 		hid_err(hid, "No output report found\n");
105 		return -1;
106 	}
107 
108 	/* Check that the report looks ok */
109 	report = list_entry(report_list->next, struct hid_report, list);
110 	if (!report) {
111 		hid_err(hid, "NULL output report\n");
112 		return -1;
113 	}
114 
115 	field = report->field[0];
116 	if (!field) {
117 		hid_err(hid, "NULL field\n");
118 		return -1;
119 	}
120 
121 	for (i = 0; ff_bits[i] >= 0; i++)
122 		set_bit(ff_bits[i], dev->ffbit);
123 
124 	error = input_ff_create_memless(dev, NULL, hid_lg4ff_play);
125 
126 	if (error)
127 		return error;
128 
129 	if (test_bit(FF_AUTOCENTER, dev->ffbit))
130 		dev->ff->set_autocenter = hid_lg4ff_set_autocenter;
131 
132 	hid_info(hid, "Force feedback for Logitech Speed Force Wireless by Simon Wood <simon@mungewell.org>\n");
133 	return 0;
134 }
135 
136