1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Copyright (c) 2023 Huawei Device Co., Ltd.
4  */
5 #include <linux/fs.h>
6 #include <linux/miscdevice.h>
7 #include <linux/module.h>
8 #include <linux/uaccess.h>
9 
10 #ifdef CONFIG_COMPAT
11 #include <linux/compat.h>
12 #endif // CONFIG_COMPAT
13 
14 #include "ucollection_process_cpu.h"
15 
16 static long (*unified_collection_ioctl_cb[])(unsigned int cmd, void __user *argp) = {
17 	unified_collection_collect_process_cpu       /* IOCTRL_COLLECT_CPU */
18 };
19 
unified_collection_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)20 static long unified_collection_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
21 {
22 	void __user *argp = u64_to_user_ptr(arg);
23 	const char *comm = NULL;
24 
25 	if ((_IOC_TYPE(cmd) >= ARRAY_SIZE(unified_collection_ioctl_cb)) ||
26 		(unified_collection_ioctl_cb[_IOC_TYPE(cmd)] == NULL)) {
27 			pr_err("invalid ioctrl cmd %u, _IOC_TYPE(cmd)=%d", cmd, _IOC_TYPE(cmd));
28 			return -EINVAL;
29 	}
30 
31 	return unified_collection_ioctl_cb[_IOC_TYPE(cmd)](cmd, argp);
32 }
33 
34 #ifdef CONFIG_COMPAT
unified_collection_compat_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)35 static long unified_collection_compat_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
36 {
37 	return unified_collection_ioctl(filp, cmd, (unsigned long) compat_ptr(arg));
38 }
39 #endif // CONFIG_COMPAT
40 
unified_collection_open(struct inode *inode, struct file *filp)41 static int unified_collection_open(struct inode *inode, struct file *filp)
42 {
43 	return 0;
44 }
45 
unified_collection_release(struct inode *inode, struct file *filp)46 static int unified_collection_release(struct inode *inode, struct file *filp)
47 {
48 	return 0;
49 }
50 
51 static const struct file_operations unified_collection_device_fops = {
52 	.owner = THIS_MODULE,
53 	.unlocked_ioctl = unified_collection_ioctl,
54 #ifdef CONFIG_COMPAT
55 	.compat_ioctl = unified_collection_compat_ioctl,
56 #endif // CONFIG_COMPAT
57 	.open = unified_collection_open,
58 	.release = unified_collection_release,
59 };
60 
61 static struct miscdevice unified_collection_device = {
62 	.name = "ucollection",
63 	.fops = &unified_collection_device_fops,
64 	.minor = MISC_DYNAMIC_MINOR,
65 };
66 
unified_collection_init(void)67 static int __init unified_collection_init(void)
68 {
69 	int ret = misc_register(&unified_collection_device);
70 	if (ret) {
71 		pr_err("failed to register unified collection device");
72 		return ret;
73 	}
74 
75 	pr_info("register unified collection device successful");
76 	return 0;
77 }
78 
unified_collection_exit(void)79 static void __exit unified_collection_exit(void)
80 {
81 	pr_info("deregister unified collection device successful");
82 	misc_deregister(&unified_collection_device);
83 }
84 
85 module_init(unified_collection_init);
86 module_exit(unified_collection_exit);
87 
88 MODULE_AUTHOR("OHOS");
89 MODULE_DESCRIPTION("Unified Collection Driver");
90 MODULE_LICENSE("GPL");