summaryrefstree
path: root/kernel/drivers/misc/tzdev/tz_mmap_test.c
blob: e8f7baee039eb776d45227a65ac7ee28ba5de97f (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
/*
 * 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/dma-mapping.h>
#include <linux/fs.h>
#include <linux/ioctl.h>
#include <linux/miscdevice.h>
#include <linux/module.h>

#include "tz_mmap_test.h"

MODULE_AUTHOR("Alex Matveev <alex.matveev@samsung.com>");
MODULE_LICENSE("GPL");

static char tz_mmap_test_stat[PAGE_SIZE];
static void *tz_mmap_test_dyn_v;
static struct page *tz_mmap_test_page;

static long tz_mmap_test_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
	switch (cmd) {
	case TZ_MMAP_GET_PADDR_STATIC:
		return virt_to_phys(&tz_mmap_test_stat);
	case TZ_MMAP_GET_PADDR_DYNAMIC:
		return page_to_phys(tz_mmap_test_page);
	case TZ_MMAP_FILL_STATIC:
		memset(tz_mmap_test_stat, arg, PAGE_SIZE);
		return 0;
	case TZ_MMAP_FILL_DYNAMIC:
		memset(tz_mmap_test_dyn_v, arg, PAGE_SIZE);
		return 0;
	}

	return -ENOTTY;
}

static const struct file_operations tz_mmap_test_fops = {
	.owner = THIS_MODULE,
	.unlocked_ioctl = tz_mmap_test_unlocked_ioctl,
#ifdef CONFIG_COMPAT
	.compat_ioctl = tz_mmap_test_unlocked_ioctl,
#endif /* CONFIG_COMPAT */
};

static struct miscdevice tz_mmap_test = {
	.minor = MISC_DYNAMIC_MINOR,
	.name = "tz_mmap_test",
	.fops = &tz_mmap_test_fops,
};

static int __init tz_mmap_test_init(void)
{
	int rc;

	rc = misc_register(&tz_mmap_test);
	if (unlikely(rc))
		return rc;

	tz_mmap_test_page = alloc_page(GFP_KERNEL);
	if (!tz_mmap_test_page)
		return -ENOMEM;

	tz_mmap_test_dyn_v = page_address(tz_mmap_test_page);

	return 0;
}

static void __exit tz_mmap_test_exit(void)
{
	__free_page(tz_mmap_test_page);
	misc_deregister(&tz_mmap_test);
}

module_init(tz_mmap_test_init);
module_exit(tz_mmap_test_exit);