1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
35MODULE_LICENSE("GPL");
36MODULE_AUTHOR("Anssi Hannula <anssi.hannula@iki.fi>");
37MODULE_DESCRIPTION("Force feedback support for Holtek On Line Grip based devices");
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81#define HOLTEKFF_MSG_LENGTH 7
82
83static const u8 start_effect_1[] = { 0x02, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00 };
84static const u8 stop_all4[] = { 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
85static const u8 stop_all6[] = { 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
86
87struct holtekff_device {
88 struct hid_field *field;
89};
90
91static void holtekff_send(struct holtekff_device *holtekff,
92 struct hid_device *hid,
93 const u8 data[HOLTEKFF_MSG_LENGTH])
94{
95 int i;
96
97 for (i = 0; i < HOLTEKFF_MSG_LENGTH; i++) {
98 holtekff->field->value[i] = data[i];
99 }
100
101 dbg_hid("sending %7ph\n", data);
102
103 hid_hw_request(hid, holtekff->field->report, HID_REQ_SET_REPORT);
104}
105
106static int holtekff_play(struct input_dev *dev, void *data,
107 struct ff_effect *effect)
108{
109 struct hid_device *hid = input_get_drvdata(dev);
110 struct holtekff_device *holtekff = data;
111 int left, right;
112
113 u8 buf[HOLTEKFF_MSG_LENGTH] =
114 { 0x01, 0x01, 0xff, 0xff, 0x10, 0xe0, 0x00 };
115
116 left = effect->u.rumble.strong_magnitude;
117 right = effect->u.rumble.weak_magnitude;
118 dbg_hid("called with 0x%04x 0x%04x\n", left, right);
119
120 if (!left && !right) {
121 holtekff_send(holtekff, hid, stop_all6);
122 return 0;
123 }
124
125 if (left)
126 buf[1] |= 0x80;
127 if (right)
128 buf[1] |= 0x40;
129
130
131 buf[6] = min(0xf, (left >> 12) + (right >> 12));
132
133 holtekff_send(holtekff, hid, buf);
134 holtekff_send(holtekff, hid, start_effect_1);
135
136 return 0;
137}
138
139static int holtekff_init(struct hid_device *hid)
140{
141 struct holtekff_device *holtekff;
142 struct hid_report *report;
143 struct hid_input *hidinput = list_entry(hid->inputs.next,
144 struct hid_input, list);
145 struct list_head *report_list =
146 &hid->report_enum[HID_OUTPUT_REPORT].report_list;
147 struct input_dev *dev = hidinput->input;
148 int error;
149
150 if (list_empty(report_list)) {
151 hid_err(hid, "no output report found\n");
152 return -ENODEV;
153 }
154
155 report = list_entry(report_list->next, struct hid_report, list);
156
157 if (report->maxfield < 1 || report->field[0]->report_count != 7) {
158 hid_err(hid, "unexpected output report layout\n");
159 return -ENODEV;
160 }
161
162 holtekff = kzalloc(sizeof(*holtekff), GFP_KERNEL);
163 if (!holtekff)
164 return -ENOMEM;
165
166 set_bit(FF_RUMBLE, dev->ffbit);
167
168 holtekff->field = report->field[0];
169
170
171 holtekff_send(holtekff, hid, stop_all4);
172 holtekff_send(holtekff, hid, stop_all6);
173
174 error = input_ff_create_memless(dev, holtekff, holtekff_play);
175 if (error) {
176 kfree(holtekff);
177 return error;
178 }
179
180 hid_info(hid, "Force feedback for Holtek On Line Grip based devices by Anssi Hannula <anssi.hannula@iki.fi>\n");
181
182 return 0;
183}
184#else
185static inline int holtekff_init(struct hid_device *hid)
186{
187 return 0;
188}
189#endif
190
191static int holtek_probe(struct hid_device *hdev, const struct hid_device_id *id)
192{
193 int ret;
194
195 ret = hid_parse(hdev);
196 if (ret) {
197 hid_err(hdev, "parse failed\n");
198 goto err;
199 }
200
201 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT & ~HID_CONNECT_FF);
202 if (ret) {
203 hid_err(hdev, "hw start failed\n");
204 goto err;
205 }
206
207 holtekff_init(hdev);
208
209 return 0;
210err:
211 return ret;
212}
213
214static const struct hid_device_id holtek_devices[] = {
215 { HID_USB_DEVICE(USB_VENDOR_ID_HOLTEK, USB_DEVICE_ID_HOLTEK_ON_LINE_GRIP) },
216 { }
217};
218MODULE_DEVICE_TABLE(hid, holtek_devices);
219
220static struct hid_driver holtek_driver = {
221 .name = "holtek",
222 .id_table = holtek_devices,
223 .probe = holtek_probe,
224};
225module_hid_driver(holtek_driver);
226