1 /*
2  *  Force feedback support for Holtek On Line Grip based gamepads
3  *
4  *  These include at least a Brazilian "Clone Joypad Super Power Fire"
5  *  which uses vendor ID 0x1241 and identifies as "HOLTEK On Line Grip".
6  *
7  *  Copyright (c) 2011 Anssi Hannula <anssi.hannula@iki.fi>
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 #include <linux/hid.h>
27 #include <linux/input.h>
28 #include <linux/module.h>
29 #include <linux/slab.h>
30 
31 #include "hid-ids.h"
32 
33 #ifdef CONFIG_HOLTEK_FF
34 
35 /*
36  * These commands and parameters are currently known:
37  *
38  * byte 0: command id:
39  * 	01  set effect parameters
40  * 	02  play specified effect
41  * 	03  stop specified effect
42  * 	04  stop all effects
43  * 	06  stop all effects
44  * 	(the difference between 04 and 06 isn't known; win driver
45  * 	 sends 06,04 on application init, and 06 otherwise)
46  *
47  * Commands 01 and 02 need to be sent as pairs, i.e. you need to send 01
48  * before each 02.
49  *
50  * The rest of the bytes are parameters. Command 01 takes all of them, and
51  * commands 02,03 take only the effect id.
52  *
53  * byte 1:
54  *	bits 0-3: effect id:
55  * 		1: very strong rumble
56  * 		2: periodic rumble, short intervals
57  * 		3: very strong rumble
58  * 		4: periodic rumble, long intervals
59  * 		5: weak periodic rumble, long intervals
60  * 		6: weak periodic rumble, short intervals
61  * 		7: periodic rumble, short intervals
62  * 		8: strong periodic rumble, short intervals
63  * 		9: very strong rumble
64  * 		a: causes an error
65  * 		b: very strong periodic rumble, very short intervals
66  * 		c-f: nothing
67  *	bit 6: right (weak) motor enabled
68  *	bit 7: left (strong) motor enabled
69  *
70  * bytes 2-3:  time in milliseconds, big-endian
71  * bytes 5-6:  unknown (win driver seems to use at least 10e0 with effect 1
72  * 		       and 0014 with effect 6)
73  * byte 7:
74  *	bits 0-3: effect magnitude
75  */
76 
77 #define HOLTEKFF_MSG_LENGTH     7
78 
79 static const u8 start_effect_1[] = { 0x02, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00 };
80 static const u8 stop_all4[] =	   { 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
81 static const u8 stop_all6[] =	   { 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
82 
83 struct holtekff_device {
84 	struct hid_field *field;
85 };
86 
holtekff_send(struct holtekff_device * holtekff,struct hid_device * hid,const u8 data[HOLTEKFF_MSG_LENGTH])87 static void holtekff_send(struct holtekff_device *holtekff,
88 			  struct hid_device *hid,
89 			  const u8 data[HOLTEKFF_MSG_LENGTH])
90 {
91 	int i;
92 
93 	for (i = 0; i < HOLTEKFF_MSG_LENGTH; i++) {
94 		holtekff->field->value[i] = data[i];
95 	}
96 
97 	dbg_hid("sending %7ph\n", data);
98 
99 	hid_hw_request(hid, holtekff->field->report, HID_REQ_SET_REPORT);
100 }
101 
holtekff_play(struct input_dev * dev,void * data,struct ff_effect * effect)102 static int holtekff_play(struct input_dev *dev, void *data,
103 			 struct ff_effect *effect)
104 {
105 	struct hid_device *hid = input_get_drvdata(dev);
106 	struct holtekff_device *holtekff = data;
107 	int left, right;
108 	/* effect type 1, length 65535 msec */
109 	u8 buf[HOLTEKFF_MSG_LENGTH] =
110 		{ 0x01, 0x01, 0xff, 0xff, 0x10, 0xe0, 0x00 };
111 
112 	left = effect->u.rumble.strong_magnitude;
113 	right = effect->u.rumble.weak_magnitude;
114 	dbg_hid("called with 0x%04x 0x%04x\n", left, right);
115 
116 	if (!left && !right) {
117 		holtekff_send(holtekff, hid, stop_all6);
118 		return 0;
119 	}
120 
121 	if (left)
122 		buf[1] |= 0x80;
123 	if (right)
124 		buf[1] |= 0x40;
125 
126 	/* The device takes a single magnitude, so we just sum them up. */
127 	buf[6] = min(0xf, (left >> 12) + (right >> 12));
128 
129 	holtekff_send(holtekff, hid, buf);
130 	holtekff_send(holtekff, hid, start_effect_1);
131 
132 	return 0;
133 }
134 
holtekff_init(struct hid_device * hid)135 static int holtekff_init(struct hid_device *hid)
136 {
137 	struct holtekff_device *holtekff;
138 	struct hid_report *report;
139 	struct hid_input *hidinput;
140 	struct list_head *report_list =
141 			&hid->report_enum[HID_OUTPUT_REPORT].report_list;
142 	struct input_dev *dev;
143 	int error;
144 
145 	if (list_empty(&hid->inputs)) {
146 		hid_err(hid, "no inputs found\n");
147 		return -ENODEV;
148 	}
149 	hidinput = list_entry(hid->inputs.next, struct hid_input, list);
150 	dev = hidinput->input;
151 
152 	if (list_empty(report_list)) {
153 		hid_err(hid, "no output report found\n");
154 		return -ENODEV;
155 	}
156 
157 	report = list_entry(report_list->next, struct hid_report, list);
158 
159 	if (report->maxfield < 1 || report->field[0]->report_count != 7) {
160 		hid_err(hid, "unexpected output report layout\n");
161 		return -ENODEV;
162 	}
163 
164 	holtekff = kzalloc(sizeof(*holtekff), GFP_KERNEL);
165 	if (!holtekff)
166 		return -ENOMEM;
167 
168 	set_bit(FF_RUMBLE, dev->ffbit);
169 
170 	holtekff->field = report->field[0];
171 
172 	/* initialize the same way as win driver does */
173 	holtekff_send(holtekff, hid, stop_all4);
174 	holtekff_send(holtekff, hid, stop_all6);
175 
176 	error = input_ff_create_memless(dev, holtekff, holtekff_play);
177 	if (error) {
178 		kfree(holtekff);
179 		return error;
180 	}
181 
182 	hid_info(hid, "Force feedback for Holtek On Line Grip based devices by Anssi Hannula <anssi.hannula@iki.fi>\n");
183 
184 	return 0;
185 }
186 #else
holtekff_init(struct hid_device * hid)187 static inline int holtekff_init(struct hid_device *hid)
188 {
189 	return 0;
190 }
191 #endif
192 
holtek_probe(struct hid_device * hdev,const struct hid_device_id * id)193 static int holtek_probe(struct hid_device *hdev, const struct hid_device_id *id)
194 {
195 	int ret;
196 
197 	ret = hid_parse(hdev);
198 	if (ret) {
199 		hid_err(hdev, "parse failed\n");
200 		goto err;
201 	}
202 
203 	ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT & ~HID_CONNECT_FF);
204 	if (ret) {
205 		hid_err(hdev, "hw start failed\n");
206 		goto err;
207 	}
208 
209 	holtekff_init(hdev);
210 
211 	return 0;
212 err:
213 	return ret;
214 }
215 
216 static const struct hid_device_id holtek_devices[] = {
217 	{ HID_USB_DEVICE(USB_VENDOR_ID_HOLTEK, USB_DEVICE_ID_HOLTEK_ON_LINE_GRIP) },
218 	{ }
219 };
220 MODULE_DEVICE_TABLE(hid, holtek_devices);
221 
222 static struct hid_driver holtek_driver = {
223 	.name = "holtek",
224 	.id_table = holtek_devices,
225 	.probe = holtek_probe,
226 };
227 module_hid_driver(holtek_driver);
228 
229 MODULE_LICENSE("GPL");
230 MODULE_AUTHOR("Anssi Hannula <anssi.hannula@iki.fi>");
231 MODULE_DESCRIPTION("Force feedback support for Holtek On Line Grip based devices");
232