1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Force feedback support for Holtek On Line Grip based gamepads
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".
8 * Copyright (c) 2011 Anssi Hannula <anssi.hannula@iki.fi>
14 #include <linux/hid.h>
15 #include <linux/input.h>
16 #include <linux/module.h>
17 #include <linux/slab.h>
21 #ifdef CONFIG_HOLTEK_FF
24 * These commands and parameters are currently known:
27 * 01 set effect parameters
28 * 02 play specified effect
29 * 03 stop specified effect
32 * (the difference between 04 and 06 isn't known; win driver
33 * sends 06,04 on application init, and 06 otherwise)
35 * Commands 01 and 02 need to be sent as pairs, i.e. you need to send 01
38 * The rest of the bytes are parameters. Command 01 takes all of them, and
39 * commands 02,03 take only the effect id.
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
53 * b: very strong periodic rumble, very short intervals
55 * bit 6: right (weak) motor enabled
56 * bit 7: left (strong) motor enabled
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)
62 * bits 0-3: effect magnitude
65 #define HOLTEKFF_MSG_LENGTH 7
67 static const u8 start_effect_1[] = { 0x02, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00 };
68 static const u8 stop_all4[] = { 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
69 static const u8 stop_all6[] = { 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
71 struct holtekff_device {
72 struct hid_field *field;
75 static void holtekff_send(struct holtekff_device *holtekff,
76 struct hid_device *hid,
77 const u8 data[HOLTEKFF_MSG_LENGTH])
81 for (i = 0; i < HOLTEKFF_MSG_LENGTH; i++) {
82 holtekff->field->value[i] = data[i];
85 dbg_hid("sending %7ph\n", data);
87 hid_hw_request(hid, holtekff->field->report, HID_REQ_SET_REPORT);
90 static int holtekff_play(struct input_dev *dev, void *data,
91 struct ff_effect *effect)
93 struct hid_device *hid = input_get_drvdata(dev);
94 struct holtekff_device *holtekff = data;
96 /* effect type 1, length 65535 msec */
97 u8 buf[HOLTEKFF_MSG_LENGTH] =
98 { 0x01, 0x01, 0xff, 0xff, 0x10, 0xe0, 0x00 };
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);
104 if (!left && !right) {
105 holtekff_send(holtekff, hid, stop_all6);
114 /* The device takes a single magnitude, so we just sum them up. */
115 buf[6] = min(0xf, (left >> 12) + (right >> 12));
117 holtekff_send(holtekff, hid, buf);
118 holtekff_send(holtekff, hid, start_effect_1);
123 static int holtekff_init(struct hid_device *hid)
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;
133 if (list_empty(&hid->inputs)) {
134 hid_err(hid, "no inputs found\n");
137 hidinput = list_entry(hid->inputs.next, struct hid_input, list);
138 dev = hidinput->input;
140 if (list_empty(report_list)) {
141 hid_err(hid, "no output report found\n");
145 report = list_entry(report_list->next, struct hid_report, list);
147 if (report->maxfield < 1 || report->field[0]->report_count != 7) {
148 hid_err(hid, "unexpected output report layout\n");
152 holtekff = kzalloc(sizeof(*holtekff), GFP_KERNEL);
156 set_bit(FF_RUMBLE, dev->ffbit);
158 holtekff->field = report->field[0];
160 /* initialize the same way as win driver does */
161 holtekff_send(holtekff, hid, stop_all4);
162 holtekff_send(holtekff, hid, stop_all6);
164 error = input_ff_create_memless(dev, holtekff, holtekff_play);
170 hid_info(hid, "Force feedback for Holtek On Line Grip based devices by Anssi Hannula <anssi.hannula@iki.fi>\n");
175 static inline int holtekff_init(struct hid_device *hid)
181 static int holtek_probe(struct hid_device *hdev, const struct hid_device_id *id)
185 ret = hid_parse(hdev);
187 hid_err(hdev, "parse failed\n");
191 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT & ~HID_CONNECT_FF);
193 hid_err(hdev, "hw start failed\n");
204 static const struct hid_device_id holtek_devices[] = {
205 { HID_USB_DEVICE(USB_VENDOR_ID_HOLTEK, USB_DEVICE_ID_HOLTEK_ON_LINE_GRIP) },
208 MODULE_DEVICE_TABLE(hid, holtek_devices);
210 static struct hid_driver holtek_driver = {
212 .id_table = holtek_devices,
213 .probe = holtek_probe,
215 module_hid_driver(holtek_driver);
217 MODULE_LICENSE("GPL");
218 MODULE_AUTHOR("Anssi Hannula <anssi.hannula@iki.fi>");
219 MODULE_DESCRIPTION("Force feedback support for Holtek On Line Grip based devices");