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