forked from mishurov/linux_elan1200_touchpad
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hid-elan.c
549 lines (455 loc) · 12.5 KB
/
hid-elan.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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
#include <linux/hid.h>
#include <linux/module.h>
#include <linux/input/mt.h>
MODULE_AUTHOR("Alexander Mishurov <ammishurov@gmail.com>");
MODULE_DESCRIPTION("Elan1200 TouchPad");
#define INPUT_REPORT_ID 0x04
#define INPUT_REPORT_SIZE 14
#define MAX_X 3200
#define MAX_Y 2198
#define RESOLUTION 31
#define MAX_CONTACTS 5
#define RELEASE_TIMEOUT 22
#define MAX_TIMESTAMP 1000000
#define INPUT_MODE_TOUCHPAD 0x03
#define LATENCY_MODE_NORMAL 0x00
#define USB_VENDOR_ID_ELANTECH 0x04f3
#define USB_DEVICE_ID_ELAN1200_I2C_TOUCHPAD 0x3022
struct slot {
struct input_mt_pos coords;
bool in_report;
bool in_touch;
bool delayed;
};
struct elan_drvdata {
struct input_dev *input;
struct hid_device *hdev;
int num_expected;
int num_received;
int prev_time;
int timestamp;
struct timer_list release_timer;
struct slot *slots;
__s16 inputmode;
__s16 inputmode_index;
__s16 maxcontact_report_id;
__s16 latency_report_id;
__s16 latency_index;
};
static void elan_report_contact(struct elan_drvdata *td,
struct input_dev *input, int slot_id,
bool in_touch)
{
input_mt_slot(input, slot_id);
input_mt_report_slot_state(input,
MT_TOOL_FINGER, in_touch);
if (in_touch) {
input_report_abs(input, ABS_MT_POSITION_X,
td->slots[slot_id].coords.x);
input_report_abs(input, ABS_MT_POSITION_Y,
td->slots[slot_id].coords.y);
}
}
static void elan_release_contacts(struct hid_device *hdev)
{
struct elan_drvdata *td = hid_get_drvdata(hdev);
struct input_dev *input = td->input;
int i;
for (i = 0; i < MAX_CONTACTS; i++) {
td->slots[i].in_touch = false;
td->slots[i].in_report = false;
td->slots[i].delayed = false;
elan_report_contact(td, input, i, false);
}
input_mt_sync_frame(input);
input_sync(input);
}
static void elan_release_timeout(struct timer_list *t)
{
struct elan_drvdata *td = from_timer(td, t, release_timer);
struct hid_device *hdev = td->hdev;
elan_release_contacts(hdev);
}
static void elan_report_contacts(struct elan_drvdata *td,
struct input_dev *input)
{
int i;
for (i = 0; i < MAX_CONTACTS; i++) {
if (td->slots[i].in_report && td->slots[i].delayed) {
elan_report_contact(td, input, i, true);
} else {
elan_report_contact(
td, input, i, td->slots[i].in_touch
);
}
td->slots[i].in_report = false;
td->slots[i].delayed = false;
}
input_mt_sync_frame(input);
input_event(input, EV_MSC, MSC_TIMESTAMP, td->timestamp);
input_sync(input);
mod_timer(&td->release_timer,
jiffies + msecs_to_jiffies(RELEASE_TIMEOUT));
}
static void elan_report_input(struct elan_drvdata *td, u8 *data)
{
struct input_dev *input = td->input;
int num_contacts = data[8];
int slot_id = data[1] >> 4;
bool is_touch = (data[1] & 0x0f) == 3;
bool is_release = (data[1] & 0x0f) == 1;
int ts, delta;
if (!(is_touch || is_release) ||
slot_id < 0 || slot_id >= MAX_CONTACTS)
return;
if (is_touch)
del_timer(&td->release_timer);
td->slots[slot_id].in_report = true;
td->slots[slot_id].in_touch = is_touch;
//width = data[11] & 0x0f;
//height = data[11] >> 4;
ts = (data[7] << 8) + data[6];
delta = ts - td->prev_time;
if (delta <= 0) delta = 1;
td->timestamp += delta;
td->prev_time = ts;
td->slots[slot_id].coords.x = (data[3] << 8) | data[2];
td->slots[slot_id].coords.y = (data[5] << 8) | data[4];
if (is_release)
td->slots[slot_id].delayed = true;
input_report_key(input, BTN_LEFT, data[9] & 0x01);
if (num_contacts > MAX_CONTACTS)
num_contacts = MAX_CONTACTS;
if (num_contacts > 0)
td->num_expected = num_contacts;
td->num_received++;
}
static void elan_report(struct hid_device *hdev, struct hid_report *report)
{
struct elan_drvdata *td = hid_get_drvdata(hdev);
struct input_dev *input = td->input;
struct hid_field *field = report->field[0];
if (!(hdev->claimed & HID_CLAIMED_INPUT))
return;
if (field && field->hidinput && field->hidinput->input &&
td->num_received >= td->num_expected) {
elan_report_contacts(td, input);
td->num_received = 0;
if (td->timestamp > MAX_TIMESTAMP)
td->timestamp = 0;
}
}
static int elan_raw_event(struct hid_device *hdev,
struct hid_report *report, u8 *data, int size)
{
struct elan_drvdata *td = hid_get_drvdata(hdev);
if (data[0] == INPUT_REPORT_ID && size == INPUT_REPORT_SIZE) {
elan_report_input(td, data);
return 1;
}
return 0;
}
static int elan_event(struct hid_device *hdev, struct hid_field *field,
struct hid_usage *usage, __s32 value)
{
if (field->report->id == INPUT_REPORT_ID) {
if (hdev->claimed & HID_CLAIMED_HIDDEV &&
hdev->hiddev_hid_event) {
hdev->hiddev_hid_event(hdev, field, usage, value);
}
return 1;
}
return 0;
}
static int elan_input_configured(struct hid_device *hdev, struct hid_input *hi)
{
struct input_dev *input = hi->input;
struct elan_drvdata *td = hid_get_drvdata(hdev);
int ret;
input_set_abs_params(input, ABS_MT_POSITION_X, 0, MAX_X, 0, 0);
input_set_abs_params(input, ABS_MT_POSITION_Y, 0, MAX_Y, 0, 0);
input_abs_set_res(input, ABS_MT_POSITION_X, RESOLUTION);
input_abs_set_res(input, ABS_MT_POSITION_Y, RESOLUTION);
__set_bit(INPUT_PROP_BUTTONPAD, input->propbit);
__set_bit(BTN_LEFT, input->keybit);
ret = input_mt_init_slots(input, MAX_CONTACTS, INPUT_MT_POINTER);
if (ret) {
hid_err(hdev, "Elan input mt init slots failed: %d\n", ret);
return ret;
}
td->input = input;
return 0;
}
static void elan_set_maxcontacts(struct hid_device *hdev)
{
struct elan_drvdata *td = hid_get_drvdata(hdev);
struct hid_report *r;
struct hid_report_enum *re;
if (td->maxcontact_report_id < 0)
return;
re = &hdev->report_enum[HID_FEATURE_REPORT];
r = re->report_id_hash[td->maxcontact_report_id];
if (r) {
if (r->field[0]->value[0] != MAX_CONTACTS) {
r->field[0]->value[0] = MAX_CONTACTS;
hid_hw_request(hdev, r, HID_REQ_SET_REPORT);
}
}
}
static void elan_set_latency(struct hid_device *hdev)
{
struct elan_drvdata *td = hid_get_drvdata(hdev);
struct hid_report *r;
struct hid_report_enum *re;
if (td->latency_report_id < 0)
return;
re = &(hdev->report_enum[HID_FEATURE_REPORT]);
r = re->report_id_hash[td->latency_report_id];
if (r) {
r->field[0]->value[td->latency_index] = LATENCY_MODE_NORMAL;
hid_hw_request(hdev, r, HID_REQ_SET_REPORT);
}
}
static void elan_set_input_mode(struct hid_device *hdev)
{
struct elan_drvdata *td = hid_get_drvdata(hdev);
struct hid_report *r;
struct hid_report_enum *re;
if (td->inputmode < 0)
return;
re = &(hdev->report_enum[HID_FEATURE_REPORT]);
r = re->report_id_hash[td->inputmode];
if (r) {
r->field[0]->value[td->inputmode_index] = INPUT_MODE_TOUCHPAD;
hid_hw_request(hdev, r, HID_REQ_SET_REPORT);
}
}
static void elan_get_feature(struct hid_device *hdev, struct hid_report *report)
{
int ret, size = hid_report_len(report);
u8 *buf;
buf = hid_alloc_report_buf(report, GFP_KERNEL);
if (!buf)
return;
ret = hid_hw_raw_request(hdev, report->id, buf, size,
HID_FEATURE_REPORT, HID_REQ_GET_REPORT);
if (ret < 0) {
dev_warn(&hdev->dev, "failed to fetch feature %d\n",
report->id);
} else {
ret = hid_report_raw_event(hdev, HID_FEATURE_REPORT, buf,
size, 0);
if (ret)
dev_warn(&hdev->dev, "failed to report feature\n");
}
kfree(buf);
}
static void elan_feature_mapping(struct hid_device *hdev,
struct hid_field *field, struct hid_usage *usage)
{
struct elan_drvdata *td = hid_get_drvdata(hdev);
switch (usage->hid) {
case HID_DG_INPUTMODE:
if (td->inputmode < 0) {
td->inputmode = field->report->id;
td->inputmode_index = usage->usage_index;
}
break;
case HID_DG_CONTACTMAX:
elan_get_feature(hdev, field->report);
td->maxcontact_report_id = field->report->id;
break;
case 0xd0060:
// latency mode usage (Page 0x0D, Usage 0x60)
td->latency_report_id = field->report->id;
td->latency_index = usage->usage_index;
break;
default:
if (usage->usage_index == 0) {
elan_get_feature(hdev, field->report);
}
}
}
#ifdef CONFIG_PM
static int elan_reset_resume(struct hid_device *hdev)
{
elan_release_contacts(hdev);
elan_set_latency(hdev);
elan_set_maxcontacts(hdev);
elan_set_input_mode(hdev);
return 0;
}
static int elan_resume(struct hid_device *hdev)
{
hid_hw_idle(hdev, 0, 0, HID_REQ_SET_IDLE);
return 0;
}
#endif
static int elan_probe(struct hid_device *hdev, const struct hid_device_id *id)
{
int ret, i;
struct elan_drvdata *td;
td = devm_kzalloc(&hdev->dev, sizeof(*td), GFP_KERNEL);
if (td == NULL) {
hid_err(hdev, "Can't alloc Elan descriptor\n");
return -ENOMEM;
}
td->inputmode = -1;
td->inputmode_index = -1;
td->maxcontact_report_id = -1;
td->latency_report_id = -1;
td->latency_index = -1;
td->slots = devm_kmalloc_array(&hdev->dev, MAX_CONTACTS,
sizeof(struct slot),
GFP_KERNEL);
if (td == NULL) {
hid_err(hdev, "Can't alloc slots data\n");
return -ENOMEM;
}
for (i = 0; i < MAX_CONTACTS; i++) {
td->slots[i].delayed = false;
td->slots[i].in_report = false;
td->slots[i].in_touch = false;
td->slots[i].coords.x = 0;
td->slots[i].coords.y = 0;
}
timer_setup(&td->release_timer, elan_release_timeout, 0);
hid_set_drvdata(hdev, td);
td->hdev = hdev;
hdev->quirks |= HID_QUIRK_NO_INIT_REPORTS;
hdev->quirks |= HID_QUIRK_NO_INPUT_SYNC;
//hdev->quirks |= HID_QUIRK_INPUT_PER_APP;
ret = hid_parse(hdev);
if (ret) {
hid_err(hdev, "Elan hid parse failed: %d\n", ret);
return ret;
}
ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
if (ret) {
hid_err(hdev, "Elan hw start failed: %d\n", ret);
return ret;
}
if (!td->input) {
hid_err(hdev, "Elan input not registered\n");
ret = -ENOMEM;
goto err_stop_hw;
}
td->input->name = "Elan TouchPad";
elan_set_latency(hdev);
elan_set_maxcontacts(hdev);
elan_set_input_mode(hdev);
return 0;
err_stop_hw:
hid_hw_stop(hdev);
return ret;
}
static void elan_remove(struct hid_device *hdev)
{
struct elan_drvdata *td = hid_get_drvdata(hdev);
del_timer_sync(&td->release_timer);
hid_hw_stop(hdev);
}
static int elan_input_mapping(struct hid_device *hdev,
struct hid_input *hi, struct hid_field *field,
struct hid_usage *usage, unsigned long **bit,
int *max)
{
int code;
if (field->application != HID_DG_TOUCHSCREEN &&
field->application != HID_DG_PEN &&
field->application != HID_DG_TOUCHPAD &&
field->application != HID_GD_KEYBOARD &&
field->application != HID_CP_CONSUMER_CONTROL)
return -1;
if (field->physical == HID_DG_STYLUS)
return 0;
else if (field->physical == 0 &&
field->report->id != INPUT_REPORT_ID)
return 0;
if (field->application == HID_DG_TOUCHSCREEN ||
field->application == HID_DG_TOUCHPAD) {
switch (usage->hid & HID_USAGE_PAGE) {
case HID_UP_GENDESK:
switch (usage->hid) {
case HID_GD_X:
return 1;
case HID_GD_Y:
return 1;
}
return 0;
case HID_UP_DIGITIZER:
switch (usage->hid) {
case HID_DG_INRANGE:
return 1;
case HID_DG_CONFIDENCE:
return 1;
case HID_DG_TIPSWITCH:
return 1;
case HID_DG_CONTACTID:
return 1;
case HID_DG_CONTACTCOUNT:
return 1;
case HID_DG_CONTACTMAX:
return -1;
case HID_DG_TOUCH:
return -1;
}
return 0;
case HID_UP_BUTTON:
code = BTN_MOUSE + ((usage->hid - 1) & HID_USAGE);
if (field->application == HID_DG_TOUCHPAD &&
(usage->hid & HID_USAGE) > 1)
code--;
hid_map_usage(hi, usage, bit, max, EV_KEY, code);
return 1;
case 0xff000000:
return -1;
}
}
return 0;
}
static int elan_input_mapped(struct hid_device *hdev,
struct hid_input *hi, struct hid_field *field,
struct hid_usage *usage, unsigned long **bit,
int *max)
{
if (field->physical == HID_DG_STYLUS)
return 0;
if (field->application == HID_DG_TOUCHSCREEN ||
field->application == HID_DG_TOUCHPAD) {
if (usage->type == EV_KEY || usage->type == EV_ABS)
set_bit(usage->type, hi->input->evbit);
return -1;
}
return 0;
}
static const struct hid_device_id elan_devices[] = {
{ HID_I2C_DEVICE(USB_VENDOR_ID_ELANTECH,
USB_DEVICE_ID_ELAN1200_I2C_TOUCHPAD), 0 },
{ }
};
MODULE_DEVICE_TABLE(hid, elan_devices);
static const struct hid_usage_id elan_grabbed_usages[] = {
{ HID_ANY_ID, HID_ANY_ID, HID_ANY_ID },
{ HID_ANY_ID - 1, HID_ANY_ID - 1, HID_ANY_ID - 1}
};
static struct hid_driver elan_driver = {
.name = "hid-elan",
.id_table = elan_devices,
.probe = elan_probe,
.remove = elan_remove,
.input_mapping = elan_input_mapping,
.input_mapped = elan_input_mapped,
.input_configured = elan_input_configured,
.feature_mapping = elan_feature_mapping,
.usage_table = elan_grabbed_usages,
.raw_event = elan_raw_event,
.event = elan_event,
.report = elan_report,
#ifdef CONFIG_PM
.reset_resume = elan_reset_resume,
.resume = elan_resume,
#endif
};
module_hid_driver(elan_driver);
MODULE_LICENSE("GPL");