-
Notifications
You must be signed in to change notification settings - Fork 21
/
hid-ftecff-tuning.c
350 lines (297 loc) · 10.1 KB
/
hid-ftecff-tuning.c
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
27
28
29
30
31
32
33
34
35
36
37
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
#include <linux/module.h>
#include <linux/hid.h>
#include "hid-ftec.h"
struct ftec_tuning_attr_t {
const char* name;
const enum ftec_tuning_attrs_enum id;
const u8 addr;
const char* description;
int (*conv_to)(struct ftec_drv_data *, u8);
u8 (*conv_from)(struct ftec_drv_data *, int);
const int min;
const int max;
};
static int ftec_conv_sens_to(struct ftec_drv_data *drv_data, u8 val) {
if (drv_data->max_range <= 1090) {
return val * 10;
}
if (val < (u8)0x8a) {
return 1080 + 10 * (0x100 + val - 0xed);
} else if (val >= (u8)0xed) {
return 1080 + 10 * (val - 0xed);
}
return 90 + 10 * (val - 0x8a);
};
static u8 ftec_conv_sens_from(struct ftec_drv_data *drv_data, int val) {
if (drv_data->max_range <= 1090) {
return val / 10;
}
if (val >= 1080) {
// overflow of u8 is expected behavior
return 0xed + ((val - 1080) / 10);
}
return 0x8a + (val - 90) / 10;
};
static int ftec_conv_times_ten(struct ftec_drv_data *drv_data, u8 val) {
return val * 10;
};
static u8 ftec_conv_div_ten(struct ftec_drv_data *drv_data, int val) {
return val / 10;
};
static u8 ftec_conv_steps_ten(struct ftec_drv_data *drv_data, int val) {
return 10 * (val / 10);
}
static int ftec_conv_signed_to(struct ftec_drv_data *drv_data, u8 val) {
return (s8)val;
};
static int ftec_conv_noop_to(struct ftec_drv_data *drv_data, u8 val) {
return val;
};
static u8 ftec_conv_noop_from(struct ftec_drv_data *drv_data, int val) {
return val;
};
static const struct ftec_tuning_attr_t ftec_tuning_attrs[] = {
#define FTEC_TUNING_ATTR(id, addr, desc, conv_to, conv_from, min, max) \
{ #id, id, addr, desc, conv_to, conv_from, min, max },
FTEC_TUNING_ATTRS
#undef FTEC_TUNING_ATTR
};
static int ftec_tuning_write(struct hid_device *hid, int addr, int val) {
struct ftec_drv_data *drv_data = hid_get_drvdata(hid);
drv_data->tuning.ftec_tuning_data[0] = 0xff;
drv_data->tuning.ftec_tuning_data[1] = 0x03;
drv_data->tuning.ftec_tuning_data[2] = 0x00;
drv_data->tuning.ftec_tuning_data[addr+1] = val;
return hid_hw_output_report(hid, &drv_data->tuning.ftec_tuning_data[0], FTEC_TUNING_REPORT_SIZE);
}
static int ftec_tuning_select(struct hid_device *hid, int slot) {
u8 *buf = kcalloc(FTEC_TUNING_REPORT_SIZE, sizeof(u8), GFP_KERNEL);
int ret;
buf[0] = 0xff;
buf[1] = 0x03;
buf[2] = 0x01;
buf[3] = slot&0xff;
ret = hid_hw_output_report(hid, buf, FTEC_TUNING_REPORT_SIZE);
kfree(buf);
return ret;
}
static enum ftec_tuning_attrs_enum ftec_tuning_get_id(struct device_attribute *attr) {
int idx = 0;
for (; idx < sizeof(ftec_tuning_attrs); idx++) {
if (strcmp(attr->attr.name, ftec_tuning_attrs[idx].name) == 0) {
return ftec_tuning_attrs[idx].id;
}
}
dbg_hid("Unknown attribute %s\n", attr->attr.name);
return FTEC_TUNING_ATTR_NONE;
}
ssize_t _ftec_tuning_show(struct device *dev, enum ftec_tuning_attrs_enum id, char *buf)
{
struct hid_device *hid = to_hid_device(dev->parent);
struct ftec_drv_data *drv_data = hid_get_drvdata(hid);
const struct ftec_tuning_attr_t *tuning_attr = &ftec_tuning_attrs[id];
return scnprintf(buf, PAGE_SIZE, "%i\n", tuning_attr->conv_to(drv_data, drv_data->tuning.ftec_tuning_data[tuning_attr->addr+1]));
}
static ssize_t ftec_tuning_show(struct device *dev, struct device_attribute *attr, char *buf)
{
enum ftec_tuning_attrs_enum id = ftec_tuning_get_id(attr);
if (id == FTEC_TUNING_ATTR_NONE) {
return -EINVAL;
}
return _ftec_tuning_show(dev, id, buf);
}
ssize_t _ftec_tuning_store(struct device *dev, enum ftec_tuning_attrs_enum id,
const char *buf, size_t count)
{
struct hid_device *hid = to_hid_device(dev->parent);
struct ftec_drv_data *drv_data = hid_get_drvdata(hid);
const struct ftec_tuning_attr_t *tuning_attr = &ftec_tuning_attrs[id];
int val, _max = tuning_attr->max;
/* guard from writing w/o having read values */
if (drv_data->tuning.ftec_tuning_data[ftec_tuning_attrs[SLOT].addr+1] == 0x0) {
hid_err(hid, "Cannot write to tuning-menu, not yet read data from device.\n");
return -EINVAL;
}
if (kstrtos32(buf, 0, &val) != 0) {
hid_err(hid, "Invalid value %s!\n", buf);
return -EINVAL;
}
/* special case for SEN, max value is device specific */
if (id == SEN) {
_max = drv_data->max_range;
/* set max value if 0 is given */
if (val == 0) {
val = _max;
}
}
/* check if value is in range */
if (val < tuning_attr->min || val > _max) {
hid_err(hid, "Value %i out of range [%i, %i]!\n", val, tuning_attr->min, _max);
return -EINVAL;
}
/* convert value to device specific value */
val = tuning_attr->conv_from(drv_data, val);
dbg_hid(" ... ftec_tuning_store %s %i\n", tuning_attr->name, val);
if (id == SLOT) {
if (ftec_tuning_select(hid, val) < 0) {
return -EIO;
}
} else {
if (ftec_tuning_write(hid, tuning_attr->addr, val) < 0) {
return -EIO;
}
}
return count;
}
static ssize_t ftec_tuning_store(struct device *dev, struct device_attribute *attr,
const char *buf, size_t count)
{
enum ftec_tuning_attrs_enum id = ftec_tuning_get_id(attr);
if (id == FTEC_TUNING_ATTR_NONE) {
return -EINVAL;
}
return _ftec_tuning_store(dev, id, buf, count);
}
static ssize_t ftec_tuning_reset(struct device *dev, struct device_attribute *attr,
const char *buf, size_t count)
{
struct hid_device *hid = to_hid_device(dev->parent);
u8 *buffer = kcalloc(FTEC_TUNING_REPORT_SIZE, sizeof(u8), GFP_KERNEL);
int ret;
// request current values
buffer[0] = 0xff;
buffer[1] = 0x03;
buffer[2] = 0x04;
ret = hid_hw_output_report(hid, buffer, FTEC_TUNING_REPORT_SIZE);
return count;
}
static DEVICE_ATTR(RESET, S_IWUSR | S_IWGRP, NULL, ftec_tuning_reset);
#define FTEC_TUNING_ATTR(id, addr, desc, conv_to, conv_from, min, max) \
static DEVICE_ATTR(id, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH, ftec_tuning_show, ftec_tuning_store);
FTEC_TUNING_ATTRS
#undef FTEC_TUNING_ATTR
static ssize_t ftec_tuning_advanced_mode_show(struct device *dev, struct device_attribute *attr, char *buf)
{
struct hid_device *hid = to_hid_device(dev->parent);
struct ftec_drv_data *drv_data = hid_get_drvdata(hid);
if (!drv_data) {
hid_err(hid, "Private driver data not found!\n");
return 0;
}
return scnprintf(buf, PAGE_SIZE, "%u\n", drv_data->tuning.advanced_mode);
}
static ssize_t ftec_tuning_advanced_mode_store(struct device *dev, struct device_attribute *attr,
const char *buf, size_t count)
{
struct hid_device *hid = to_hid_device(dev->parent);
struct ftec_drv_data *drv_data = hid_get_drvdata(hid);
u8 advanced_mode;
if (!drv_data) {
hid_err(hid, "Private driver data not found!\n");
return 0;
}
if (kstrtou8(buf, 0, &advanced_mode) == 0) {
if (advanced_mode != drv_data->tuning.advanced_mode) {
u8 *buffer = kcalloc(FTEC_TUNING_REPORT_SIZE, sizeof(u8), GFP_KERNEL);
buffer[0] = 0xff;
buffer[1] = 0x03;
buffer[2] = 0x06;
(void)hid_hw_output_report(hid, buffer, FTEC_TUNING_REPORT_SIZE);
}
}
return count;
}
static DEVICE_ATTR(advanced_mode, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH, ftec_tuning_advanced_mode_show, ftec_tuning_advanced_mode_store);
static struct class ftec_tuning_class = {
.name = "ftec_tuning",
};
int ftec_tuning_classdev_register(struct device *parent,
struct ftec_tuning_classdev *ftec_tuning_cdev)
{
struct hid_device *hdev = to_hid_device(parent);
int ret;
ret = class_register(&ftec_tuning_class);
if (ret)
return 0;
ftec_tuning_cdev->dev = device_create(&ftec_tuning_class, parent, 0, NULL, "%s", dev_name(&hdev->dev));
#define CREATE_SYSFS_FILE(name) \
ret = device_create_file(ftec_tuning_cdev->dev, &dev_attr_##name); \
if (ret) \
hid_warn(hdev, "Unable to create sysfs interface for '%s', errno %d\n", #name, ret); \
CREATE_SYSFS_FILE(advanced_mode)
CREATE_SYSFS_FILE(RESET)
CREATE_SYSFS_FILE(SLOT)
CREATE_SYSFS_FILE(SEN)
CREATE_SYSFS_FILE(FF)
CREATE_SYSFS_FILE(FEI)
CREATE_SYSFS_FILE(FOR)
CREATE_SYSFS_FILE(SPR)
CREATE_SYSFS_FILE(DPR)
if (hdev->product == CSL_ELITE_WHEELBASE_DEVICE_ID ||
hdev->product == CSL_ELITE_PS4_WHEELBASE_DEVICE_ID) {
CREATE_SYSFS_FILE(DRI)
}
if (hdev->product == CSL_ELITE_WHEELBASE_DEVICE_ID ||
hdev->product == CSL_ELITE_PS4_WHEELBASE_DEVICE_ID ||
hdev->product == CSL_DD_WHEELBASE_DEVICE_ID ||
hdev->product == PODIUM_WHEELBASE_DD1_DEVICE_ID ||
hdev->product == PODIUM_WHEELBASE_DD2_DEVICE_ID) {
CREATE_SYSFS_FILE(BLI)
CREATE_SYSFS_FILE(SHO)
}
if (hdev->product == CSL_DD_WHEELBASE_DEVICE_ID ||
hdev->product == PODIUM_WHEELBASE_DD1_DEVICE_ID ||
hdev->product == PODIUM_WHEELBASE_DD2_DEVICE_ID) {
CREATE_SYSFS_FILE(NDP)
CREATE_SYSFS_FILE(NFR)
CREATE_SYSFS_FILE(NIN)
CREATE_SYSFS_FILE(INT)
}
// FIXME: this is ClubSport DD specific, but not yet understood how
// to discriminate these
if (hdev->product == CSL_DD_WHEELBASE_DEVICE_ID) {
CREATE_SYSFS_FILE(FUL)
}
return 0;
}
void ftec_tuning_classdev_unregister(struct ftec_tuning_classdev *ftec_tuning_cdev)
{
struct hid_device *hdev = to_hid_device(ftec_tuning_cdev->dev->parent);
if (IS_ERR_OR_NULL(ftec_tuning_cdev->dev))
return;
#define REMOVE_SYSFS_FILE(name) device_remove_file(ftec_tuning_cdev->dev, &dev_attr_##name); \
REMOVE_SYSFS_FILE(advanced_mode)
REMOVE_SYSFS_FILE(RESET)
REMOVE_SYSFS_FILE(SLOT)
REMOVE_SYSFS_FILE(SEN)
REMOVE_SYSFS_FILE(FF)
REMOVE_SYSFS_FILE(FEI)
REMOVE_SYSFS_FILE(FOR)
REMOVE_SYSFS_FILE(SPR)
REMOVE_SYSFS_FILE(DPR)
if (hdev->product == CSL_ELITE_WHEELBASE_DEVICE_ID ||
hdev->product == CSL_ELITE_PS4_WHEELBASE_DEVICE_ID) {
REMOVE_SYSFS_FILE(DRI)
}
if (hdev->product == CSL_ELITE_WHEELBASE_DEVICE_ID ||
hdev->product == CSL_ELITE_PS4_WHEELBASE_DEVICE_ID ||
hdev->product == CSL_DD_WHEELBASE_DEVICE_ID ||
hdev->product == PODIUM_WHEELBASE_DD1_DEVICE_ID ||
hdev->product == PODIUM_WHEELBASE_DD2_DEVICE_ID) {
REMOVE_SYSFS_FILE(BLI)
REMOVE_SYSFS_FILE(SHO)
}
if (hdev->product == CSL_DD_WHEELBASE_DEVICE_ID ||
hdev->product == PODIUM_WHEELBASE_DD1_DEVICE_ID ||
hdev->product == PODIUM_WHEELBASE_DD2_DEVICE_ID) {
REMOVE_SYSFS_FILE(NDP)
REMOVE_SYSFS_FILE(NFR)
REMOVE_SYSFS_FILE(NIN)
REMOVE_SYSFS_FILE(INT)
}
if (hdev->product == CSL_DD_WHEELBASE_DEVICE_ID) {
REMOVE_SYSFS_FILE(FUL)
}
device_unregister(ftec_tuning_cdev->dev);
class_unregister(&ftec_tuning_class);
}