summaryrefstree
path: root/kernel/drivers/input/input_boost/evdev_booster.c
blob: e4a58c5aa381da7f81e36eef058927f300be7987 (plain)
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
#define ITAG " [Evdev Booster] "
#include <linux/input/input_booster.h>

#include <linux/input.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/device.h>
#include <linux/syscalls.h>

#if IS_ENABLED(CONFIG_SEC_INPUT_BOOSTER)
spinlock_t ib_ev_lock;
struct workqueue_struct *ev_unbound_wq;
static struct device *evbst_dev;

void input_booster(struct ib_event_data *ib_ev_data);

int evdev_ib_notify_callback(struct notifier_block *nb,
	unsigned long event_type, void *data)
{
	if (ib_init_succeed) {
		spin_lock(&ib_ev_lock);
		struct ib_event_data *ib_ev_data = data;

		switch (event_type) {
		case IB_EVENT_TOUCH_BOOSTER:
			input_booster(ib_ev_data);
			break;
		default:
			break;
		}
		spin_unlock(&ib_ev_lock);
	}

	return 0;
}

static struct notifier_block ib_event_notifier = {
	 .notifier_call = evdev_ib_notify_callback,
	 .priority = 1,
};

int chk_next_data(const struct input_value *vals, int next_idx, int input_type)
{
	int ret_val = 0;
	int next_type = -1;
	int next_code = -1;

	next_type = vals[next_idx].type;
	next_code = vals[next_idx].code;

	switch (input_type) {
	case BTN_TOUCH:
		if ((next_type == EV_ABS) && (next_code == ABS_PRESSURE))
			ret_val = 1;
		break;
	case EV_KEY:
		ret_val = 1;
		break;
	default:
		break;
	}

	return ret_val;
}

int chk_boost_on_off(const struct input_value *vals, int idx, int dev_type)
{
	int ret_val = -1;

	if (dev_type < 0)
		return ret_val;

	/* In case of SPEN or HOVER, it must be empty multi event
	 * Before starting input booster.
	 */
	if (dev_type == SPEN || dev_type == HOVER) {
		if (!evdev_mt_event[dev_type] && vals[idx].value)
			ret_val = 1;
		else if (evdev_mt_event[dev_type] && !vals[idx].value)
			ret_val = 0;
	} else if (dev_type == TOUCH || dev_type == MULTI_TOUCH) {
		if (vals[idx].value >= 0)
			ret_val = 1;
		else
			ret_val = 0;
	} else if (vals[idx].value > 0)
		ret_val = 1;
	else if (vals[idx].value <= 0)
		ret_val = 0;

	return ret_val;
}

/*
 * get_device_type : Define type of device for input_booster.
 * device_type : Current device that in which input events triggered.
 * keyId : Each device get given unique keyid using Type, Code, Slot values
 *  to identify which booster will be triggered.
 * cur_idx : Pointing current handling index from input booster.
 *  cur_idx will be updated when cur_idx is same as head.
 * vals : Event set to determine what booster would be triggered.
 * evt_cnt : Total count of event in this time.
 */
int get_device_type(int *device_type, unsigned int *keyId,
					int *cur_idx, const struct input_value *vals, int evt_cnt)
{
	int i;
	int ret_val = -1;
	int dev_type = NONE_TYPE_DEVICE;
	int uniq_slot = 0;
	int next_idx = 0 ;
	int target_idx = 0;

	if (evt_cnt > MAX_EVENTS) {
		dev_warn_ratelimited(evbst_dev, "Exceed max event number\n");
		return ret_val;
	}

	/* Initializing device type before finding the proper device type. */
	*device_type = dev_type;

	for (i = *cur_idx; i < evt_cnt; i++) {
		pr_booster(" %s Type : %d, Code : %d, Value : %d, idx : %d\n",
			"Input Data || ", vals[i].type,
			vals[i].code, vals[i].value, i);

		if (vals[i].type == EV_SYN || vals[i].code == SYN_REPORT) {
			break;
		}

		if (vals[i].type == EV_KEY) {
			target_idx = i;
			switch (vals[i].code) {
			case BTN_TOUCH:
				next_idx = i+1;
				if (!chk_next_data(vals, next_idx, BTN_TOUCH))
					break;
				dev_type = SPEN;
				break;
			case BTN_TOOL_PEN:
				dev_type = HOVER;
				break;
			case KEY_BACK:
			case KEY_HOMEPAGE:
				dev_type = TOUCH_KEY;
				break;
			case KEY_VOLUMEUP:
			case KEY_VOLUMEDOWN:
			case KEY_POWER:
				dev_type = KEY;
				break;
			default:
				break;
			}

		} else if (vals[i].type == EV_ABS) {
			target_idx = i;
			switch (vals[i].code) {
			case ABS_MT_TRACKING_ID:
				if (vals[i].value >= 0) {
					evdev_mt_slot++;
				} else {
					evdev_mt_slot--;
					if (evdev_mt_slot < 0) {
						evdev_mt_slot = 0;
						break;
					}
				}

				if (vals[i].value >= 0) {
					if (evdev_mt_slot == 1) {
						dev_type = TOUCH;
						uniq_slot = 1;
					} else if (evdev_mt_slot == 2) {
						dev_type = MULTI_TOUCH;
						uniq_slot = 2;
					}
				} else if (vals[i].value < 0) {
					if (evdev_mt_slot == 0) {
						dev_type = TOUCH;
						uniq_slot = 1;
					} else if (evdev_mt_slot == 1) {
						dev_type = MULTI_TOUCH;
						uniq_slot = 2;
					}
				}

				pr_booster("Touch Booster Trigger(%d), T(%d) C(%d) V(%d), Idx(%d), Cnt(%d)",
							evdev_mt_slot,
							vals[i].type, vals[i].code, vals[i].value,
							i, evt_cnt);

				break;
			}
		} else if (vals[i].type == EV_MSC &&
					vals[i].code == MSC_SCAN) {
			next_idx = i++;
			if (!chk_next_data(vals, next_idx, EV_KEY))
				break;

			next_idx = i++;
			target_idx = next_idx;
			switch (vals[next_idx].code) {
			case BTN_LEFT: /* Checking Touch Button Event */
			case BTN_RIGHT:
			case BTN_MIDDLE:
				dev_type = MOUSE;
				//Remain the last of CODE value as a uniq_slot to recognize BTN Type (LEFT, RIGHT, MIDDLE)
				uniq_slot = vals[next_idx].code;
				break;
			default: /* Checking Keyboard Event */
				dev_type = KEYBOARD;
				uniq_slot = vals[next_idx].code;

				pr_booster("KBD Booster Trigger(%d), Type(%d), Code(%d), Val(%d), Idx(%d), Cnt(%d)\n",
						vals[next_idx].code,
						vals[i].type, vals[i].code, vals[i].value,
						i, evt_cnt);
				break;
			}
		}

		if (dev_type != NONE_TYPE_DEVICE ) {
			*keyId = create_uniq_id(vals[i].type, vals[i].code, uniq_slot);
			ret_val = chk_boost_on_off(vals, target_idx, dev_type);
			pr_booster("Dev type Find(%d), KeyID(%d), enable(%d), Target(%d)\n",
				dev_type, *keyId, ret_val, target_idx);
			break;
		}

	}

	*cur_idx = i+1;

	*device_type = dev_type;
	return ret_val;
}

// ********** Detect Events ********** //
//void input_booster(int head, int bufsize)
void input_booster(struct ib_event_data *ib_ev_data)
{
#if IS_ENABLED(CONFIG_SEC_INPUT_BOOSTER_QC) || \
	IS_ENABLED(CONFIG_SEC_INPUT_BOOSTER_SLSI) || \
	IS_ENABLED(CONFIG_SEC_INPUT_BOOSTER_MTK)
	int dev_type = 0;
	int keyId = 0;
	int cur_idx = -1;

	pr_booster("%s Triggered :: evt_cnt(%d), ib_init_succeed(%d)",
		__func__, ib_ev_data->evt_cnt, ib_init_succeed);

	if (!ib_init_succeed || ib_ev_data->evt_cnt == 0) {
		pr_err(ITAG"ev_cnt(%d) dev is Null OR dt_infor hasn't mem alloc", ib_ev_data->evt_cnt);
		return;
	}

	cur_idx = 0;

	while (cur_idx < ib_ev_data->evt_cnt) {
		
		keyId = 0;

		int enable = get_device_type(&dev_type, &keyId, &cur_idx, ib_ev_data->vals, ib_ev_data->evt_cnt);
		if (enable < 0 || keyId == 0) {
			continue;
		}

		if (dev_type <= NONE_TYPE_DEVICE || dev_type >= MAX_DEVICE_TYPE_NUM) {
			continue;
		}

		if (enable == BOOSTER_ON) {
			evdev_mt_event[dev_type]++;
		} else {
			evdev_mt_event[dev_type]--;
		}

		pr_booster("Device_Type(%d), KeyId(%d), IB_Cnt(%d), enable(%d)",
			dev_type, keyId, trigger_cnt, enable);

		ib_trigger[trigger_cnt].key_id = keyId;
		ib_trigger[trigger_cnt].event_type = enable;
		ib_trigger[trigger_cnt].dev_type = dev_type;

		queue_work(ev_unbound_wq, &(ib_trigger[trigger_cnt++].ib_trigger_work));
		trigger_cnt = (trigger_cnt == MAX_IB_COUNT) ? 0 : trigger_cnt;
	}
#endif
}

static int __init ev_boost_init(void)
{
	pr_info(ITAG" Input Booster Module Init\n");
	input_booster_init();
	spin_lock_init(&ib_ev_lock);
	ib_notifier_register(&ib_event_notifier);
	ev_unbound_wq =
		alloc_ordered_workqueue("ev_unbound_wq", WQ_HIGHPRI);

	evbst_dev = kzalloc(sizeof(struct device), GFP_KERNEL);
	dev_set_name(evbst_dev, "evdev_booster_dev");
	evbst_dev->release = NULL;
	device_register(evbst_dev);

	return 0;
}

static void __exit ev_boost_exit(void)
{
	ib_notifier_unregister(&ib_event_notifier);
	device_unregister(evbst_dev);
	kfree(evbst_dev);
	input_booster_exit();
}

late_initcall(ev_boost_init);
module_exit(ev_boost_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("hongc.shim");
MODULE_DESCRIPTION("EVDEV Booster in kernel");
#endif //--CONFIG_SEC_INPUT_BOOSTER