| xj | b04a402 | 2021-11-25 15:01:52 +0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * Copyright (c) 2020 MediaTek Inc. |
| 4 | */ |
| 5 | #include <linux/seq_file.h> |
| 6 | #include <linux/ctype.h> |
| 7 | #include <linux/proc_fs.h> |
| 8 | #include <linux/uaccess.h> |
| 9 | |
| 10 | #include "proc.h" |
| 11 | #include "safexcel.h" |
| 12 | |
| 13 | #define PROCREG_DIR "safexcel" |
| 14 | #define PROCREG_DISABLE_EIP97 "disable_EIP97" |
| 15 | #define PROCREG_ENABLE_LOG "enable_Log" |
| 16 | #define PROCREG_REF_CNT "ref_cnt" |
| 17 | #define PROCREG_VCORE "vcore" |
| 18 | |
| 19 | static struct proc_dir_entry *eip97_proc_dir; |
| 20 | static struct proc_dir_entry *proc_disable_eip97; |
| 21 | static struct proc_dir_entry *proc_enable_log; |
| 22 | static struct proc_dir_entry *proc_ref_cnt; |
| 23 | static struct proc_dir_entry *proc_eip97_vcore; |
| 24 | |
| 25 | static debug_proc_update_func g_callback = NULL; |
| 26 | static void *g_priv = NULL; |
| 27 | |
| 28 | int dbg_disable_eip97 = DEFAULT_DISABLE_EIP97; |
| 29 | EXPORT_SYMBOL(dbg_disable_eip97); |
| 30 | |
| 31 | int dbg_enable_log = DEFAULT_ENABLE_LOG; |
| 32 | EXPORT_SYMBOL(dbg_enable_log); |
| 33 | |
| 34 | int dbg_eip97_vcore_max = DEFAULT_VCORE_MAX; |
| 35 | EXPORT_SYMBOL(dbg_eip97_vcore_max); |
| 36 | |
| 37 | int dbg_eip97_vcore_min = DEFAULT_VCORE_MIN; |
| 38 | EXPORT_SYMBOL(dbg_eip97_vcore_min); |
| 39 | |
| 40 | int enable_log_read(struct seq_file *seq, void *v) |
| 41 | { |
| 42 | seq_puts(seq, dbg_enable_log ? "true\n" : "false\n"); |
| 43 | return 0; |
| 44 | } |
| 45 | |
| 46 | ssize_t enable_log_write(struct file *file, const char __user *buffer, |
| 47 | size_t count, loff_t *data) |
| 48 | { |
| 49 | pr_info("enable_log_write in!!\n"); |
| 50 | |
| 51 | if (count > 0) { |
| 52 | char c; |
| 53 | int val, changed; |
| 54 | |
| 55 | if (get_user(c, buffer)) |
| 56 | return -EFAULT; |
| 57 | |
| 58 | val = (c != '0'); |
| 59 | changed = val != dbg_enable_log; |
| 60 | dbg_enable_log = val; |
| 61 | |
| 62 | if (changed && g_callback) |
| 63 | g_callback(PROC_UPDATE_ENABLE_LOG, g_priv); |
| 64 | |
| 65 | pr_info("enable_log_write: c=%c, dbg_enable_log = %d\n", c, dbg_enable_log); |
| 66 | } |
| 67 | |
| 68 | return count; |
| 69 | } |
| 70 | |
| 71 | static int enable_log_open(struct inode *inode, struct file *file) |
| 72 | { |
| 73 | return single_open(file, enable_log_read, NULL); |
| 74 | } |
| 75 | |
| 76 | |
| 77 | static const struct file_operations enable_log_fops = { |
| 78 | .owner = THIS_MODULE, |
| 79 | .open = enable_log_open, |
| 80 | .read = seq_read, |
| 81 | .llseek = seq_lseek, |
| 82 | .write = enable_log_write, |
| 83 | .release = single_release |
| 84 | }; |
| 85 | |
| 86 | |
| 87 | int disable_eip97_read(struct seq_file *seq, void *v) |
| 88 | { |
| 89 | seq_puts(seq, dbg_disable_eip97 ? "true\n" : "false\n"); |
| 90 | return 0; |
| 91 | } |
| 92 | |
| 93 | ssize_t disable_eip97_write(struct file *file, const char __user *buffer, |
| 94 | size_t count, loff_t *data) |
| 95 | { |
| 96 | pr_info("disable_eip97_write in!!\n"); |
| 97 | |
| 98 | if (count > 0) { |
| 99 | char c; |
| 100 | int val, changed; |
| 101 | |
| 102 | if (get_user(c, buffer)) |
| 103 | return -EFAULT; |
| 104 | |
| 105 | val = (c != '0'); |
| 106 | changed = val != dbg_disable_eip97; |
| 107 | dbg_disable_eip97 = val; |
| 108 | |
| 109 | if (changed && g_callback) |
| 110 | g_callback(PROC_UPDATE_DISABLE_EIP97, g_priv); |
| 111 | |
| 112 | pr_info("disable_eip97_write: c=%c, dbg_disable_eip97 = %d\n", c, dbg_disable_eip97); |
| 113 | } |
| 114 | |
| 115 | return count; |
| 116 | } |
| 117 | |
| 118 | static int disable_eip97_open(struct inode *inode, struct file *file) |
| 119 | { |
| 120 | return single_open(file, disable_eip97_read, NULL); |
| 121 | } |
| 122 | |
| 123 | |
| 124 | static const struct file_operations disable_eip97_fops = { |
| 125 | .owner = THIS_MODULE, |
| 126 | .open = disable_eip97_open, |
| 127 | .read = seq_read, |
| 128 | .llseek = seq_lseek, |
| 129 | .write = disable_eip97_write, |
| 130 | .release = single_release |
| 131 | }; |
| 132 | |
| 133 | |
| 134 | static int ref_cnt_read(struct seq_file *seq, void *v) |
| 135 | { |
| 136 | struct safexcel_crypto_priv *crypto_priv = g_priv; |
| 137 | |
| 138 | seq_printf(seq, "ref_cnt = %d\n", crypto_priv->ref_cnt); |
| 139 | return 0; |
| 140 | } |
| 141 | |
| 142 | static int ref_cnt_open(struct inode *inode, struct file *file) |
| 143 | { |
| 144 | return single_open(file, ref_cnt_read, NULL); |
| 145 | } |
| 146 | |
| 147 | static const struct file_operations ref_cnt_fops = { |
| 148 | .owner = THIS_MODULE, |
| 149 | .open = ref_cnt_open, |
| 150 | .read = seq_read, |
| 151 | .llseek = seq_lseek, |
| 152 | .release = single_release |
| 153 | }; |
| 154 | |
| 155 | |
| 156 | static int eip97_vcore_read(struct seq_file *seq, void *v) |
| 157 | { |
| 158 | seq_printf(seq, "vcore min = %d, max = INT_MAX\n", dbg_eip97_vcore_min); |
| 159 | |
| 160 | return 0; |
| 161 | } |
| 162 | |
| 163 | static ssize_t eip97_vcore_write(struct file *file, const char __user *ptr, |
| 164 | size_t len, loff_t *data) |
| 165 | { |
| 166 | if (len > 0) { |
| 167 | int val, changed, ret; |
| 168 | char buf[32]; |
| 169 | |
| 170 | if (len > sizeof(buf) - 1) |
| 171 | len = sizeof(buf) - 1; |
| 172 | |
| 173 | ret = strncpy_from_user(buf, ptr, len); |
| 174 | if (ret < 0) |
| 175 | return ret; |
| 176 | buf[len] = '\0'; |
| 177 | |
| 178 | if (kstrtoint(buf, 10, &val)) |
| 179 | return -EINVAL; |
| 180 | |
| 181 | changed = val != dbg_eip97_vcore_min; |
| 182 | dbg_eip97_vcore_min = val; |
| 183 | |
| 184 | if (changed && g_callback) |
| 185 | g_callback(PROC_UPDATE_EIP97_VCORE, g_priv); |
| 186 | } |
| 187 | |
| 188 | return len; |
| 189 | } |
| 190 | |
| 191 | static int eip97_vcore_open(struct inode *inode, struct file *file) |
| 192 | { |
| 193 | return single_open(file, eip97_vcore_read, NULL); |
| 194 | } |
| 195 | |
| 196 | |
| 197 | static const struct file_operations eip97_vcore_fops = { |
| 198 | .owner = THIS_MODULE, |
| 199 | .open = eip97_vcore_open, |
| 200 | .read = seq_read, |
| 201 | .llseek = seq_lseek, |
| 202 | .write = eip97_vcore_write, |
| 203 | .release = single_release |
| 204 | }; |
| 205 | |
| 206 | int safexcel_proc_init(debug_proc_update_func callback, void *priv) |
| 207 | { |
| 208 | g_callback = callback; |
| 209 | g_priv = priv; |
| 210 | |
| 211 | if (!eip97_proc_dir) |
| 212 | eip97_proc_dir = proc_mkdir(PROCREG_DIR, NULL); |
| 213 | |
| 214 | dbg_disable_eip97 = DEFAULT_DISABLE_EIP97; |
| 215 | proc_disable_eip97 = proc_create(PROCREG_DISABLE_EIP97, 0, |
| 216 | eip97_proc_dir, &disable_eip97_fops); |
| 217 | if (!proc_disable_eip97) |
| 218 | pr_info("!! FAIL to create %s PROC !!\n", PROCREG_DISABLE_EIP97); |
| 219 | |
| 220 | dbg_enable_log = DEFAULT_ENABLE_LOG; |
| 221 | proc_enable_log = proc_create(PROCREG_ENABLE_LOG, 0, |
| 222 | eip97_proc_dir, &enable_log_fops); |
| 223 | if (!proc_enable_log) |
| 224 | pr_info("!! FAIL to create %s PROC !!\n", PROCREG_ENABLE_LOG); |
| 225 | |
| 226 | proc_ref_cnt = proc_create(PROCREG_REF_CNT, 0, |
| 227 | eip97_proc_dir, &ref_cnt_fops); |
| 228 | if (!proc_ref_cnt) |
| 229 | pr_info("!! FAIL to create %s PROC !!\n", PROCREG_REF_CNT); |
| 230 | |
| 231 | dbg_eip97_vcore_min = DEFAULT_VCORE_MIN; |
| 232 | proc_eip97_vcore = proc_create(PROCREG_VCORE, 0, |
| 233 | eip97_proc_dir, &eip97_vcore_fops); |
| 234 | if (!proc_eip97_vcore) |
| 235 | pr_info("!! FAIL to create %s PROC !!\n", PROCREG_VCORE); |
| 236 | |
| 237 | return 0; |
| 238 | } |
| 239 | |
| 240 | void safexcel_proc_exit(void) |
| 241 | { |
| 242 | pr_info("proc exit\n"); |
| 243 | |
| 244 | if (!eip97_proc_dir) |
| 245 | return; |
| 246 | |
| 247 | if (proc_disable_eip97) |
| 248 | remove_proc_entry(PROCREG_DISABLE_EIP97, eip97_proc_dir); |
| 249 | |
| 250 | if (proc_enable_log) |
| 251 | remove_proc_entry(PROCREG_ENABLE_LOG, eip97_proc_dir); |
| 252 | |
| 253 | if (proc_ref_cnt) |
| 254 | remove_proc_entry(PROCREG_REF_CNT, eip97_proc_dir); |
| 255 | |
| 256 | if (proc_eip97_vcore) |
| 257 | remove_proc_entry(PROCREG_VCORE, eip97_proc_dir); |
| 258 | |
| 259 | remove_proc_entry(PROCREG_DIR, NULL); |
| 260 | } |
| 261 | |