summaryrefstree
path: root/kernel/drivers/misc/tzdev/extensions/irs.c
blob: 601ac5ee1c1ffdb66d1635bb0ace18cba6a2026b (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
/*
 * Copyright (C) 2016 Samsung Electronics, Inc.
 *
 * This software is licensed under the terms of the GNU General Public
 * License version 2, as published by the Free Software Foundation, and
 * may be copied, distributed, and modified under those terms.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

#include <linux/init.h>
#include <linux/ioctl.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/printk.h>
#include <linux/fs.h>
#include <linux/miscdevice.h>
#include <linux/uaccess.h>
#include <linux/mutex.h>

#include "tzdev_internal.h"
#include "core/cdev.h"
#include "core/platform.h"
#include "extensions/irs.h"

#if defined(CONFIG_ARCH_MSM) && defined(CONFIG_MSM_SCM)
#if defined(CONFIG_ARCH_MSM8939) || defined(CONFIG_ARCH_MSM8996)
#include <soc/qcom/scm.h>
#else
#error "Unsupported target! The only MSM8996 and MSM8939 are supported for Qualcomm chipset"
#endif
#endif

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Oleksii Mosolab");

#ifdef TZIRS_DEBUG
#define DBG(...)		pr_info("TZIRS DBG : " __VA_ARGS__)
#define ERR(...)		pr_alert("TZIRS ERR : " __VA_ARGS__)
#else
#define DBG(...)
#define ERR(...)		pr_alert("TZIRS ERR : " __VA_ARGS__)
#endif

/* Set appropriate command value */
#define SMC_IRS_CMD_RAW	(0x0000000B)
#define SMC_IRS_CMD TZDEV_SMC_COMMAND(SMC_IRS_CMD_RAW)

static int tzirs_fd_open = 0;
static DEFINE_MUTEX(tzirs_lock);

#if defined(CONFIG_ARCH_MSM) && defined(CONFIG_MSM_SCM)
#define SCM_BLOW_SW_FUSE_ID     0x01
#define SCM_IS_SW_FUSE_BLOWN_ID 0x02

static long __tzirs_smc_cmd(uint32_t p0, uint32_t *p1, uint32_t *p2, uint32_t *p3)
{
	unsigned int ret;

	struct scm_desc desc = {0};
	desc.args[0] = *p1;
	desc.arginfo = SCM_ARGS(1);

	switch(*p3) {
	case IRS_SET_FLAG_CMD:
		ret = scm_call2(SCM_SIP_FNID(p0, SCM_BLOW_SW_FUSE_ID), &desc);
		DBG("[SET_FLAG_CMD] : scm_call2 returned 0x%08x\n", ret);
		break;

	case IRS_GET_FLAG_VAL_CMD:
		ret = scm_call2(SCM_SIP_FNID(p0, SCM_IS_SW_FUSE_BLOWN_ID), &desc);
		if (ret) {
			ERR("[GET_FLAG_CMD] : scm_call2 failed ret = 0x%08x\n", ret);
			break;
		}
		DBG("[GET_FLAG_CMD] : desc.ret[0] = 0x%08x\n", (unsigned int) desc.ret[0]);
		*p2 = (uint32_t) desc.ret[0];
		break;

	default:
		ERR("Wrong SCM command ID\n");
		ret = IRS_INCORRECT_FLAG_TYPE;
		break;
	}

	return ret;
}
#else /* defined(CONFIG_ARCH_MSM) && defined(CONFIG_MSM_SCM) */
static long __tzirs_smc_cmd(unsigned long p0, unsigned long *p1, unsigned long *p2, unsigned long *p3)
{
	struct tzdev_smc_data data;

	data.args[0] = p0;
	data.args[1] = *p1;
	data.args[2] = *p2;
	data.args[3] = *p3;
	data.args[4] = 0;
	data.args[5] = 0;
	data.args[6] = 0;

	tzdev_platform_smc_call(&data);

	*p1 = data.args[1];
	*p2 = data.args[2];
	*p3 = data.args[3];

	return data.args[0];
}
#endif /* defined(CONFIG_ARCH_MSM) && defined(CONFIG_MSM_SCM) */

long tzirs_smc(unsigned long *p1, unsigned long *p2, unsigned long *p3)
{
#if defined(CONFIG_ARCH_MSM) && defined(CONFIG_MSM_SCM)
	return __tzirs_smc_cmd(SCM_SVC_FUSE, (uint32_t *)p1, (uint32_t *)p2 , (uint32_t *)p3);
#else
	return __tzirs_smc_cmd(SMC_IRS_CMD, p1, p2, p3);
#endif
}

static int tzirs_open(struct inode *n, struct file *f)
{
	int ret = 0;

	mutex_lock(&tzirs_lock);
	if (tzirs_fd_open) {
		ret = -EBUSY;
		goto out;
	}
	tzirs_fd_open++;
	DBG("open\n");

out:
	mutex_unlock(&tzirs_lock);
	return ret;
}

static inline int tzirs_release(struct inode *inode, struct file *file)
{
	mutex_lock(&tzirs_lock);
	if (tzirs_fd_open)
		tzirs_fd_open--;
	mutex_unlock(&tzirs_lock);
	DBG("release\n");
	return 0;
}

static long tzirs_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
	int ret = 0;
	unsigned long p1, p2, p3;

	struct irs_ctx __user *ioargp = (struct irs_ctx __user *) arg;
	struct irs_ctx ctx = {0};

	if ( _IOC_TYPE(cmd) != IOC_MAGIC ) {
		ERR("INVALID CMD = %d\n", cmd);
		return -ENOTTY;
	}
	switch (cmd) {
	case IOCTL_IRS_CMD:
		DBG("IOCT_IRS_CMD\n");
		/* get flag id */
		ret = copy_from_user(&ctx, ioargp, sizeof(struct irs_ctx));
		if (ret != 0) {
			ERR("IRS_CMD copy_from_user failed, ret = 0x%08x\n", ret);
			return -EFAULT;
		}

		p1 = ctx.id;
		p2 = ctx.value;
		p3 = ctx.func_cmd;

		DBG("IRS_CMD before: id = 0x%lx, value = 0x%lx, cmd = 0x%lx\n",
				(unsigned long)p1, (unsigned long)p2, (unsigned long)p3);

		ret = tzirs_smc(&p1, &p2, &p3);

		DBG("IRS_CMD after: id = 0x%lx, value = 0x%lx, cmd = 0x%lx\n",
				(unsigned long)p1, (unsigned long)p2, (unsigned long)p3);

		if (ret) {
			ERR("Unable to send IRS_CMD : id = 0x%lx, ret = %d\n",
					(unsigned long)p1, ret);
			return -EFAULT;
		}

		ctx.id = p1;
		ctx.value = p2;
		ctx.func_cmd = p3;

		ret = copy_to_user(ioargp, &ctx, sizeof(struct irs_ctx));
		if (ret != 0) {
			ERR("IRS_CMD copy_to_user failed, ret = 0x%08x\n", ret);
			return -EFAULT;
		}
		break;
	default:
		ERR("UNKNOWN CMD, cmd = 0x%08x\n", cmd);
		return -ENOTTY;
	}

	return 0;
}

static const struct file_operations tzirs_fops = {
	.owner = THIS_MODULE,
	.open = tzirs_open,
	.unlocked_ioctl = tzirs_unlocked_ioctl,
#ifdef CONFIG_COMPAT
	.compat_ioctl = tzirs_unlocked_ioctl,
#endif /* CONFIG_COMPAT */
	.release = tzirs_release,
};

static struct tz_cdev tzirs_cdev = {
	.name = "tzirs",
	.fops = &tzirs_fops,
	.owner = THIS_MODULE,
};

static int __init tzirs_init(void)
{
	int rc;

	rc = tz_cdev_register(&tzirs_cdev);
	if (rc) {
		ERR("Unable to register TZIRS driver, rc = 0x%08x\n", rc);
		return rc;
	}

	DBG("INSTALLED\n");
	return 0;
}

static void __exit tzirs_exit(void)
{
	tz_cdev_unregister(&tzirs_cdev);
	DBG("REMOVED\n");
}

module_init(tzirs_init);
module_exit(tzirs_exit);