summaryrefstree
path: root/kernel/drivers/misc/tui/stui_inf.c
blob: 36861481eb40afa8c0a2d564d0bd85c1845e905d (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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/* tui/stui_inf.c
 *
 * Samsung TUI HW Handler driver.
 *
 * Copyright (c) 2015 Samsung Electronics
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
*/

#include <decon.h>
#include <linux/atomic.h>
#include <linux/delay.h>
#include <linux/fb.h>
#include <linux/module.h>
#include <linux/reboot.h>
#include <linux/spinlock.h>
#include <linux/types.h>
#ifdef USE_TEE_CLIENT_API
#include <tee_client_api.h>
#endif /* USE_TEE_CLIENT_API */
#include "stui_inf.h"

#define TUI_REE_EXTERNAL_EVENT      42
#define SESSION_CANCEL_DELAY        150
#define MAX_WAIT_CNT                10

static int tui_mode = STUI_MODE_OFF;
static int tui_blank_cnt;
static DEFINE_SPINLOCK(tui_lock);

#ifdef USE_TEE_CLIENT_API
static TEEC_UUID uuid = {
	.timeLow = 0x0,
	.timeMid = 0x0,
	.timeHiAndVersion = 0x0,
	.clockSeqAndNode = {0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0x0, 0x81},
};
#endif

int stui_inc_blank_ref(void)
{
	unsigned long fls;
	int ret_cnt;

	spin_lock_irqsave(&tui_lock, fls);
	ret_cnt = ++tui_blank_cnt;
	spin_unlock_irqrestore(&tui_lock, fls);
	return ret_cnt;
}
EXPORT_SYMBOL(stui_inc_blank_ref);

int stui_dec_blank_ref(void)
{
	unsigned long fls;
	int ret_cnt;

	spin_lock_irqsave(&tui_lock, fls);
	ret_cnt = --tui_blank_cnt;
	spin_unlock_irqrestore(&tui_lock, fls);
	return ret_cnt;
}
EXPORT_SYMBOL(stui_dec_blank_ref);

int stui_get_blank_ref(void)
{
	unsigned long fls;
	int ret_cnt;

	spin_lock_irqsave(&tui_lock, fls);
	ret_cnt = tui_blank_cnt;
	spin_unlock_irqrestore(&tui_lock, fls);
	return ret_cnt;
}
EXPORT_SYMBOL(stui_get_blank_ref);

void stui_set_blank_ref(int cnt)
{
	unsigned long fls;

	spin_lock_irqsave(&tui_lock, fls);
	tui_blank_cnt = cnt;
	spin_unlock_irqrestore(&tui_lock, fls);
}
EXPORT_SYMBOL(stui_set_blank_ref);

int stui_get_mode(void)
{
	unsigned long fls;
	int ret_mode;

	spin_lock_irqsave(&tui_lock, fls);
	ret_mode = tui_mode;
	spin_unlock_irqrestore(&tui_lock, fls);
	return ret_mode;
}
EXPORT_SYMBOL(stui_get_mode);

void stui_set_mode(int mode)
{
	unsigned long fls;

	spin_lock_irqsave(&tui_lock, fls);
	tui_mode = mode;
	spin_unlock_irqrestore(&tui_lock, fls);
}
EXPORT_SYMBOL(stui_set_mode);

int stui_set_mask(int mask)
{
	unsigned long fls;
	int ret_mode;

	spin_lock_irqsave(&tui_lock, fls);
	ret_mode = (tui_mode |= mask);
	spin_unlock_irqrestore(&tui_lock, fls);
	return ret_mode;
}
EXPORT_SYMBOL(stui_set_mask);

int stui_clear_mask(int mask)
{
	unsigned long fls;
	int ret_mode;

	spin_lock_irqsave(&tui_lock, fls);
	ret_mode = (tui_mode &= ~mask);
	spin_unlock_irqrestore(&tui_lock, fls);
	return ret_mode;
}
EXPORT_SYMBOL(stui_clear_mask);

#ifdef USE_TEE_CLIENT_API
static atomic_t canceling = ATOMIC_INIT(0);
int stui_cancel_session(void)
{
	TEEC_Context context;
	TEEC_Session session;
	TEEC_Result result;
	TEEC_Operation operation;
	int ret = -1;
	int count = 0;

	pr_info("[STUI] %s begin\n", __func__);

	if (!(STUI_MODE_ALL & stui_get_mode())) {
		pr_info("[STUI] session cancel is not needed\n");
		return 0;
	}

	if (atomic_cmpxchg(&canceling, 0, 1) != 0) {
		pr_info("[STUI] already canceling.\n");
		return 0;
	}

	result = TEEC_InitializeContext(NULL, &context);
	if (result != TEEC_SUCCESS) {
		pr_err("[STUI] TEEC_InitializeContext returned: 0x%x\n", result);
		goto out;
	}

	result = TEEC_OpenSession(&context, &session, &uuid, TEEC_LOGIN_PUBLIC, NULL, NULL, NULL);
	if (result != TEEC_SUCCESS) {
		pr_err("[STUI] TEEC_OpenSession returned: 0x%x\n", result);
		goto finalize_context;
	}

	operation.paramTypes = TEEC_PARAM_TYPES(TEEC_NONE, TEEC_NONE, TEEC_NONE, TEEC_NONE);

	result = TEEC_InvokeCommand(&session, TUI_REE_EXTERNAL_EVENT, &operation, NULL);
	if (result != TEEC_SUCCESS) {
		pr_err("[STUI] TEEC_InvokeCommand returned: 0x%x\n", result);
		goto close_session;
	} else {
		pr_info("[STUI] invoked cancel cmd\n");
	}

	TEEC_CloseSession(&session);
	TEEC_FinalizeContext(&context);

	while ((STUI_MODE_ALL & stui_get_mode()) && (count < MAX_WAIT_CNT)) {
		msleep(SESSION_CANCEL_DELAY);
		count++;
	}

	if (STUI_MODE_ALL & stui_get_mode()) {
		pr_err("[STUI] session was not cancelled yet\n");
	} else {
		pr_info("[STUI] session was cancelled successfully\n");
		ret = 0;
	}

	atomic_set(&canceling, 0);
	return ret;

close_session:
	TEEC_CloseSession(&session);
finalize_context:
	TEEC_FinalizeContext(&context);
out:
	atomic_set(&canceling, 0);
	pr_err("[STUI] %s end, ret=%d, result=0x%x\n", __func__, ret, result);

	return ret;
}
#else /* USE_TEE_CLIENT_API */
int stui_cancel_session(void)
{
	pr_err("[STUI] %s not supported\n", __func__);
	return -1;
}
#endif /* USE_TEE_CLIENT_API */

#ifdef CONFIG_SOC_EXYNOS3250

static int tui_mode_3250 = STUI_MODE_OFF;
static BLOCKING_NOTIFIER_HEAD(trustedui_nb_list);

int trustedui_nb_register(struct notifier_block *nb)
{
	return blocking_notifier_chain_register(&trustedui_nb_list, nb);
}

int trustedui_nb_unregister(struct notifier_block *nb)
{
	return blocking_notifier_chain_unregister(&trustedui_nb_list, nb);
}

int trustedui_nb_send_event(unsigned long val, void *v)
{
	pr_info("%s:cmd = %ld mode = %d\n", __func__, val, (*(int *)v));
	return blocking_notifier_call_chain(&trustedui_nb_list, val, v);
}

void trustedui_set_mode(int mode)
{
	unsigned long flags;

	spin_lock_irqsave(&tui_lock, flags);
	if (tui_mode_3250 == mode) {
		spin_unlock_irqrestore(&tui_lock, flags);
		return;
	}
	pr_info("%s:mode[%d->%d]\n", __func__, tui_mode_3250, mode);
	tui_mode_3250 = mode;
	spin_unlock_irqrestore(&tui_lock, flags);
	trustedui_nb_send_event(TRUSTEDUI_MODE_CHANGE, (void *)&mode);
}
EXPORT_SYMBOL(trustedui_set_mode);

#endif /* CONFIG_SOC_EXYNOS3250 */

static int stui_notifier_callback(struct notifier_block *self, unsigned long event, void *data);

static struct notifier_block stui_fb_notif = {
	.notifier_call = stui_notifier_callback,
};

static struct notifier_block stui_reboot_nb = {
	.notifier_call = stui_notifier_callback,
};

static int stui_is_decon_in_state_tui(const struct fb_info *const info)
{
	struct decon_win *win = NULL;
	struct decon_device *decon = NULL;

	if (info == NULL) {
		pr_err("[STUI] %s: fb_info is null.\n", __func__);
		return 0;
	}

	win = info->par;
	if (win == NULL) {
		pr_err("[STUI] %s: decon_win is null.\n", __func__);
		return 0;
	}

	decon = win->decon;
	if (decon == NULL) {
		pr_err("[STUI] %s: decon is null.\n", __func__);
		return 0;
	}

	pr_debug("[STUI] %s: decon=%d, state=%d\n", __func__, decon->id, decon->state);
	return (decon->state == DECON_STATE_TUI);
}

static int stui_notifier_callback(struct notifier_block *self, unsigned long event, void *data)
{
	(void) data;

	pr_debug("[STUI] %s (start): event=%lu\n", __func__, event);

	if (!(self == &stui_fb_notif && event == FB_EARLY_EVENT_BLANK)
		&& self != &stui_reboot_nb) {
		return NOTIFY_OK;
	}

	if (self == &stui_fb_notif && data != NULL) {
		const struct fb_event *event_data = data;
		const struct fb_info *info = event_data->info;

		if (stui_is_decon_in_state_tui(info) == 0) {
			pr_err("[STUI] %s is not state TUI\n", __func__);
			return NOTIFY_OK;
		}
	}

	stui_cancel_session();
	pr_debug("[STUI] %s (finish)\n", __func__);
	return NOTIFY_OK;
}

int stui_register_on_events(void)
{
	int ret = -1;
	pr_debug("[STUI] %s (start)\n", __func__);
	ret = fb_register_client(&stui_fb_notif);
	if (ret != 0) {
		pr_err("[STUI] Unable to register fb notifier (%d)\n", ret);
		return ret;
	}

	ret = register_reboot_notifier(&stui_reboot_nb);
	if (ret != 0) {
		pr_err("[STUI] Unable to register reboot notifier (%d)\n", ret);
		fb_unregister_client(&stui_fb_notif);
		return ret;
	}

	pr_debug("[STUI] %s (finish)\n", __func__);
	return ret;
}

void stui_unregister_from_events(void)
{
	pr_debug("[STUI] %s (start)\n", __func__);
	fb_unregister_client(&stui_fb_notif);
	unregister_reboot_notifier(&stui_reboot_nb);
	pr_debug("[STUI] %s (finish)\n", __func__);
}