xref: /openbmc/linux/drivers/hid/hid-lgff.c (revision 4800cd83)
1 /*
2  * Force feedback support for hid-compliant for some of the devices from
3  * Logitech, namely:
4  * - WingMan Cordless RumblePad
5  * - WingMan Force 3D
6  *
7  *  Copyright (c) 2002-2004 Johann Deneux
8  *  Copyright (c) 2006 Anssi Hannula <anssi.hannula@gmail.com>
9  */
10 
11 /*
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25  *
26  * Should you need to contact me, the author, you can do so by
27  * e-mail - mail your message to <johann.deneux@it.uu.se>
28  */
29 
30 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
31 
32 #include <linux/input.h>
33 #include <linux/usb.h>
34 #include <linux/hid.h>
35 
36 #include "usbhid/usbhid.h"
37 #include "hid-lg.h"
38 
39 struct dev_type {
40 	u16 idVendor;
41 	u16 idProduct;
42 	const signed short *ff;
43 };
44 
45 static const signed short ff_rumble[] = {
46 	FF_RUMBLE,
47 	-1
48 };
49 
50 static const signed short ff_joystick[] = {
51 	FF_CONSTANT,
52 	-1
53 };
54 
55 static const signed short ff_joystick_ac[] = {
56 	FF_CONSTANT,
57 	FF_AUTOCENTER,
58 	-1
59 };
60 
61 static const signed short ff_wheel[] = {
62 	FF_CONSTANT,
63 	FF_AUTOCENTER,
64 	-1
65 };
66 
67 static const struct dev_type devices[] = {
68 	{ 0x046d, 0xc211, ff_rumble },
69 	{ 0x046d, 0xc219, ff_rumble },
70 	{ 0x046d, 0xc283, ff_joystick },
71 	{ 0x046d, 0xc286, ff_joystick_ac },
72 	{ 0x046d, 0xc287, ff_joystick_ac },
73 	{ 0x046d, 0xc293, ff_joystick },
74 	{ 0x046d, 0xc294, ff_wheel },
75 	{ 0x046d, 0xc295, ff_joystick },
76 	{ 0x046d, 0xca03, ff_wheel },
77 };
78 
79 static int hid_lgff_play(struct input_dev *dev, void *data, struct ff_effect *effect)
80 {
81 	struct hid_device *hid = input_get_drvdata(dev);
82 	struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
83 	struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
84 	int x, y;
85 	unsigned int left, right;
86 
87 #define CLAMP(x) if (x < 0) x = 0; if (x > 0xff) x = 0xff
88 
89 	switch (effect->type) {
90 	case FF_CONSTANT:
91 		x = effect->u.ramp.start_level + 0x7f;	/* 0x7f is center */
92 		y = effect->u.ramp.end_level + 0x7f;
93 		CLAMP(x);
94 		CLAMP(y);
95 		report->field[0]->value[0] = 0x51;
96 		report->field[0]->value[1] = 0x08;
97 		report->field[0]->value[2] = x;
98 		report->field[0]->value[3] = y;
99 		dbg_hid("(x, y)=(%04x, %04x)\n", x, y);
100 		usbhid_submit_report(hid, report, USB_DIR_OUT);
101 		break;
102 
103 	case FF_RUMBLE:
104 		right = effect->u.rumble.strong_magnitude;
105 		left = effect->u.rumble.weak_magnitude;
106 		right = right * 0xff / 0xffff;
107 		left = left * 0xff / 0xffff;
108 		CLAMP(left);
109 		CLAMP(right);
110 		report->field[0]->value[0] = 0x42;
111 		report->field[0]->value[1] = 0x00;
112 		report->field[0]->value[2] = left;
113 		report->field[0]->value[3] = right;
114 		dbg_hid("(left, right)=(%04x, %04x)\n", left, right);
115 		usbhid_submit_report(hid, report, USB_DIR_OUT);
116 		break;
117 	}
118 	return 0;
119 }
120 
121 static void hid_lgff_set_autocenter(struct input_dev *dev, u16 magnitude)
122 {
123 	struct hid_device *hid = input_get_drvdata(dev);
124 	struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
125 	struct hid_report *report = list_entry(report_list->next, struct hid_report, list);
126 	__s32 *value = report->field[0]->value;
127 	magnitude = (magnitude >> 12) & 0xf;
128 	*value++ = 0xfe;
129 	*value++ = 0x0d;
130 	*value++ = magnitude;   /* clockwise strength */
131 	*value++ = magnitude;   /* counter-clockwise strength */
132 	*value++ = 0x80;
133 	*value++ = 0x00;
134 	*value = 0x00;
135 	usbhid_submit_report(hid, report, USB_DIR_OUT);
136 }
137 
138 int lgff_init(struct hid_device* hid)
139 {
140 	struct hid_input *hidinput = list_entry(hid->inputs.next, struct hid_input, list);
141 	struct list_head *report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
142 	struct input_dev *dev = hidinput->input;
143 	struct hid_report *report;
144 	struct hid_field *field;
145 	const signed short *ff_bits = ff_joystick;
146 	int error;
147 	int i;
148 
149 	/* Find the report to use */
150 	if (list_empty(report_list)) {
151 		hid_err(hid, "No output report found\n");
152 		return -1;
153 	}
154 
155 	/* Check that the report looks ok */
156 	report = list_entry(report_list->next, struct hid_report, list);
157 	field = report->field[0];
158 	if (!field) {
159 		hid_err(hid, "NULL field\n");
160 		return -1;
161 	}
162 
163 	for (i = 0; i < ARRAY_SIZE(devices); i++) {
164 		if (dev->id.vendor == devices[i].idVendor &&
165 		    dev->id.product == devices[i].idProduct) {
166 			ff_bits = devices[i].ff;
167 			break;
168 		}
169 	}
170 
171 	for (i = 0; ff_bits[i] >= 0; i++)
172 		set_bit(ff_bits[i], dev->ffbit);
173 
174 	error = input_ff_create_memless(dev, NULL, hid_lgff_play);
175 	if (error)
176 		return error;
177 
178 	if ( test_bit(FF_AUTOCENTER, dev->ffbit) )
179 		dev->ff->set_autocenter = hid_lgff_set_autocenter;
180 
181 	pr_info("Force feedback for Logitech force feedback devices by Johann Deneux <johann.deneux@it.uu.se>\n");
182 
183 	return 0;
184 }
185