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
|
/*
* Copyright (C) 2015 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 "tzirs.h"
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Oleksii Mosolab");
static int tzirs_fd_open = 0;
static DEFINE_MUTEX(tzirs_lock);
static int tzisr_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 tzisr_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 tzisr_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
int ret = 0;
void __user *ioargp = (void __user *) arg;
isr_ctx_t 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(ctx));
if (ret != 0) {
ERR("IRS_CMD copy_from_user failed, ret = 0x%08x\n", ret);
return -EFAULT;
}
DBG("IRS_CMD before: id = 0x%08x, cmd = 0x%08x, value = 0x%08x\n", ctx.id, ctx.func_cmd, ctx.value);
ret = tzirs_smc(&(ctx.id), &(ctx.value), &(ctx.func_cmd));
if (ret) {
ERR("Unable to send IRS_CMD : id = 0x%08x, ret = %d\n", ctx.id, ret);
return -EFAULT;
}
DBG("IRS_CMD after: id = 0x%08x, cmd = 0x%08x, value = 0x%08x\n", ctx.id, ctx.func_cmd, ctx.value);
ret = copy_to_user(ioargp, &ctx, sizeof(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 misc_fops = {
.owner = THIS_MODULE,
.open = tzisr_open,
.unlocked_ioctl = tzisr_unlocked_ioctl,
#ifdef CONFIG_COMPAT
.compat_ioctl = tzisr_unlocked_ioctl,
#endif /* CONFIG_COMPAT */
.release = tzisr_release,
};
static struct miscdevice tzisr_dev = {
.minor = MISC_DYNAMIC_MINOR,
TZIRS_NAME,
&misc_fops,
};
static int __init test_init(void)
{
int ret = 0;
ret = misc_register(&tzisr_dev);
if (ret) {
ERR("Unable to register TZIRS driver, minor = 0x%08x, ret = 0x%08x\n", tzisr_dev.minor, ret);
return ret;
}
DBG("INSTALLED\n");
return 0;
}
static void __exit test_exit(void)
{
misc_deregister(&tzisr_dev);
DBG("REMOVED\n");
}
module_init(test_init);
module_exit(test_exit);
|