import PULS_20160108
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / misc / mediatek / masp / mt8127 / module / sec_mod.c
CommitLineData
6fa3eb70
S
1/*
2* Copyright (C) 2011-2014 MediaTek Inc.
3*
4* This program is free software: you can redistribute it and/or modify it under the terms of the
5* GNU General Public License version 2 as published by the Free Software Foundation.
6*
7* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
8* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
9* See the GNU General Public License for more details.
10*
11* You should have received a copy of the GNU General Public License along with this program.
12* If not, see <http://www.gnu.org/licenses/>.
13*/
14
15/******************************************************************************
16 * INCLUDE LINUX HEADER
17 ******************************************************************************/
18#include <linux/kernel.h>
19#include <linux/module.h>
20#include <linux/init.h>
21#include <linux/moduleparam.h>
22#include <linux/slab.h>
23#include <linux/unistd.h>
24#include <linux/sched.h>
25#include <linux/fs.h>
26#include <asm/uaccess.h>
27#include <linux/version.h>
28#include <linux/spinlock.h>
29#include <linux/semaphore.h>
30#include <linux/delay.h>
31#include <linux/kthread.h>
32#include <linux/errno.h>
33#include <linux/cdev.h>
34#include <linux/kdev_t.h>
35#include <linux/platform_device.h>
36#include <linux/device.h>
37#include <linux/mutex.h>
38#include <linux/vmalloc.h>
39#include <linux/poll.h>
40#include <linux/proc_fs.h>
41#include <linux/string.h>
42#include <mach/memory.h>
43#include <asm/io.h>
44#include <linux/device.h>
45#include <linux/cdev.h>
46#include <linux/proc_fs.h>
47#include <linux/seq_file.h>
48
49/******************************************************************************
50 * INCLUDE LIBRARY
51 ******************************************************************************/
52#include <mach/sec_osal.h>
53#include "sec_mod.h"
54#ifdef MTK_SECURITY_MODULE_LITE
55#include "masp_version.h"
56#endif
57#include "sec_boot_core.h"
58
59#define SEC_DEV_NAME "sec"
60#define SEC_MAJOR 182
61#define MOD "MASP"
62
63#define TRACE_FUNC() MSG_FUNC(SEC_DEV_NAME)
64
65/**************************************************************************
66 * EXTERNAL VARIABLE
67 **************************************************************************/
68extern const struct sec_ops *sec_get_ops(void);
69extern bool bMsg;
70extern struct semaphore hacc_sem;
71
72/*************************************************************************
73 * GLOBAL VARIABLE
74 **************************************************************************/
75static struct sec_mod sec = {0};
76static struct cdev sec_dev;
77static struct class *sec_class;
78static struct device *sec_device;
79
80/**************************************************************************
81 * EXTERNAL FUNCTION
82 **************************************************************************/
83extern int sec_get_random_id(unsigned int *rid);
84extern long sec_core_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
85extern void sec_core_init (void);
86extern void sec_core_exit (void);
87
88/**************************************************************************
89 * SEC DRIVER OPEN
90 **************************************************************************/
91static int sec_open(struct inode *inode, struct file *file)
92{
93 return 0;
94}
95
96/**************************************************************************
97 * SEC DRIVER RELEASE
98 **************************************************************************/
99static int sec_release(struct inode *inode, struct file *file)
100{
101 return 0;
102}
103
104/**************************************************************************
105 * SEC DRIVER IOCTL
106 **************************************************************************/
107static long sec_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
108{
109#ifdef MTK_SECURITY_MODULE_LITE
110 return -EIO;
111#else
112 return sec_core_ioctl(file, cmd, arg);
113#endif
114}
115
116static struct file_operations sec_fops = {
117 .owner = THIS_MODULE,
118 .open = sec_open,
119 .release = sec_release,
120 .write = NULL,
121 .read = NULL,
122 .unlocked_ioctl = sec_ioctl
123};
124
125/**************************************************************************
126 * SEC RID PROC FUNCTION
127 **************************************************************************/
128static int sec_proc_rid_show(struct seq_file *m, void *v)
129{
130 unsigned int rid[4];
131 unsigned int i;
132 sec_get_random_id((unsigned int *)rid);
133 for(i=0;i<16;i++)
134 seq_putc(m,*((char*)rid+i));
135
136 return 0;
137}
138
139static int sec_proc_rid_open(struct inode *inode, struct file *file)
140{
141 return single_open(file, sec_proc_rid_show, NULL);
142}
143
144static const struct file_operations sec_proc_rid_fops = {
145 .open = sec_proc_rid_open,
146 .read = seq_read,
147 .llseek = seq_lseek,
148 .release= seq_release,
149};
150
151
152/**************************************************************************
153 * SEC MODULE PARAMETER
154 **************************************************************************/
155static uint recovery_done = 0;
156module_param(recovery_done, uint, S_IRUSR|S_IWUSR/*|S_IWGRP*/|S_IRGRP|S_IROTH); /* rw-r--r-- */
157MODULE_PARM_DESC(recovery_done, "A recovery sync parameter under sysfs (0=complete, 1=on-going, 2=error)");
158
159/**************************************************************************
160 * SEC DRIVER INIT
161 **************************************************************************/
162static int sec_init(struct platform_device * dev)
163{
164 int ret = 0;
165 dev_t id;
166
167 printk( "[%s] sec_init (%d)\n", SEC_DEV_NAME, ret);
168
169 id = MKDEV(SEC_MAJOR, 0);
170 ret = register_chrdev_region(id, 1, SEC_DEV_NAME);
171
172 if (ret)
173 {
174 printk(KERN_ERR "[%s] Regist Failed (%d)\n", SEC_DEV_NAME, ret);
175 return ret;
176 }
177
178 sec_class = class_create(THIS_MODULE, SEC_DEV_NAME);
179 if (NULL == sec_class)
180 {
181 printk(KERN_ERR "[%s] Create class failed(0x%x)\n", SEC_DEV_NAME, ret);
182 ret = -1;
183 return ret;
184 }
185
186 cdev_init(&sec_dev, &sec_fops);
187 sec_dev.owner = THIS_MODULE;
188
189 ret = cdev_add(&sec_dev, id, 1);
190 if (ret < 0)
191 {
192 goto exit;
193 }
194
195 sec_device = device_create(sec_class, NULL, id, NULL, SEC_DEV_NAME);
196 if (NULL == sec_class)
197 {
198 printk(KERN_ERR "[%s] Create device failed(0x%x)\n", SEC_DEV_NAME, ret);
199 class_destroy(sec_class);
200 ret = -1;
201 return ret;
202 }
203
204 sec.id = id;
205 sec.init = 1;
206 spin_lock_init(&sec.lock);
207
208 proc_create("rid", 0, NULL, &sec_proc_rid_fops);
209
210#ifdef MTK_SECURITY_MODULE_LITE
211 printk("[MASP Lite] version '%s%s', enter.\n",BUILD_TIME,BUILD_BRANCH);
212#endif
213
214exit:
215 if (ret != 0)
216 {
217 device_destroy(sec_class, id);
218 class_destroy(sec_class);
219 unregister_chrdev_region(id, 1);
220 memset(&sec, 0, sizeof(sec));
221 }
222
223 return ret;
224}
225
226
227/**************************************************************************
228 * SEC DRIVER EXIT
229 **************************************************************************/
230static void sec_exit(void)
231{
232 remove_proc_entry("rid", NULL);
233 cdev_del(&sec_dev);
234 unregister_chrdev_region(sec.id, 1);
235 memset(&sec, 0, sizeof(sec));
236
237#ifdef MTK_SECURITY_MODULE_LITE
238 printk("[MASP Lite] version '%s%s', exit.\n",BUILD_TIME,BUILD_BRANCH);
239#else
240 sec_core_exit();
241#endif
242}
243
244/**************************************************************************
245 * MASP PLATFORM DRIVER WRAPPER, FOR BUILD-IN SEQUENCE
246 **************************************************************************/
247int masp_probe(struct platform_device * dev)
248{
249 int ret = 0;
250 ret = sec_init(dev);
251 return ret;
252}
253
254int masp_remove(struct platform_device * dev)
255{
256 sec_exit();
257 return 0;
258}
259
260
261static struct platform_driver masp_driver = {
262 .probe = masp_probe,
263 .remove = masp_remove,
264 .driver = {
265 .name = "masp",
266 .owner = THIS_MODULE,
267 },
268};
269
270static int __init masp_init(void)
271{
272 int ret;
273
274 ret = platform_driver_register(&masp_driver);
275
276 if (ret)
277 {
278 printk(KERN_ERR "[%s] Reg platform driver failed (%d)\n", SEC_DEV_NAME, ret);
279 }
280
281 return ret;
282}
283
284
285static void __exit masp_exit(void)
286{
287 platform_driver_unregister(&masp_driver);
288}
289
290module_init(masp_init);
291module_exit(masp_exit);
292
293/**************************************************************************
294 * EXPORT FUNCTION
295 **************************************************************************/
296EXPORT_SYMBOL(sec_get_random_id);
297
298MODULE_LICENSE("GPL");
299MODULE_AUTHOR("MediaTek Inc.");
300#ifdef MTK_SECURITY_MODULE_LITE
301MODULE_DESCRIPTION("Mediatek Security Module Lite");
302#else
303MODULE_DESCRIPTION("Mediatek Security Module");
304#endif