1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Driver for the Intel SCU IPC mechanism
4  *
5  * (C) Copyright 2008-2010 Intel Corporation
6  * Author: Sreedhara DS (sreedhara.ds@intel.com)
7  *
8  * This driver provides IOCTL interfaces to call Intel SCU IPC driver API.
9  */
10 
11 #include <linux/errno.h>
12 #include <linux/fcntl.h>
13 #include <linux/fs.h>
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/sched.h>
17 #include <linux/slab.h>
18 #include <linux/types.h>
19 #include <linux/uaccess.h>
20 
21 #include <asm/intel_scu_ipc.h>
22 
23 static int major;
24 
25 /* IOCTL commands */
26 #define	INTE_SCU_IPC_REGISTER_READ	0
27 #define INTE_SCU_IPC_REGISTER_WRITE	1
28 #define INTE_SCU_IPC_REGISTER_UPDATE	2
29 
30 struct scu_ipc_data {
31 	u32     count;  /* No. of registers */
32 	u16     addr[5]; /* Register addresses */
33 	u8      data[5]; /* Register data */
34 	u8      mask; /* Valid for read-modify-write */
35 };
36 
37 /**
38  *	scu_reg_access		-	implement register access ioctls
39  *	@cmd: command we are doing (read/write/update)
40  *	@data: kernel copy of ioctl data
41  *
42  *	Allow the user to perform register accesses on the SCU via the
43  *	kernel interface
44  */
45 
46 static int scu_reg_access(u32 cmd, struct scu_ipc_data  *data)
47 {
48 	unsigned int count = data->count;
49 
50 	if (count == 0 || count == 3 || count > 4)
51 		return -EINVAL;
52 
53 	switch (cmd) {
54 	case INTE_SCU_IPC_REGISTER_READ:
55 		return intel_scu_ipc_readv(data->addr, data->data, count);
56 	case INTE_SCU_IPC_REGISTER_WRITE:
57 		return intel_scu_ipc_writev(data->addr, data->data, count);
58 	case INTE_SCU_IPC_REGISTER_UPDATE:
59 		return intel_scu_ipc_update_register(data->addr[0],
60 						    data->data[0], data->mask);
61 	default:
62 		return -ENOTTY;
63 	}
64 }
65 
66 /**
67  *	scu_ipc_ioctl		-	control ioctls for the SCU
68  *	@fp: file handle of the SCU device
69  *	@cmd: ioctl coce
70  *	@arg: pointer to user passed structure
71  *
72  *	Support the I/O and firmware flashing interfaces of the SCU
73  */
74 static long scu_ipc_ioctl(struct file *fp, unsigned int cmd,
75 							unsigned long arg)
76 {
77 	int ret;
78 	struct scu_ipc_data  data;
79 	void __user *argp = (void __user *)arg;
80 
81 	if (!capable(CAP_SYS_RAWIO))
82 		return -EPERM;
83 
84 	if (copy_from_user(&data, argp, sizeof(struct scu_ipc_data)))
85 		return -EFAULT;
86 	ret = scu_reg_access(cmd, &data);
87 	if (ret < 0)
88 		return ret;
89 	if (copy_to_user(argp, &data, sizeof(struct scu_ipc_data)))
90 		return -EFAULT;
91 	return 0;
92 }
93 
94 static const struct file_operations scu_ipc_fops = {
95 	.unlocked_ioctl = scu_ipc_ioctl,
96 };
97 
98 static int __init ipc_module_init(void)
99 {
100 	major = register_chrdev(0, "intel_mid_scu", &scu_ipc_fops);
101 	if (major < 0)
102 		return major;
103 
104 	return 0;
105 }
106 
107 static void __exit ipc_module_exit(void)
108 {
109 	unregister_chrdev(major, "intel_mid_scu");
110 }
111 
112 module_init(ipc_module_init);
113 module_exit(ipc_module_exit);
114 
115 MODULE_LICENSE("GPL v2");
116 MODULE_DESCRIPTION("Utility driver for intel scu ipc");
117 MODULE_AUTHOR("Sreedhara <sreedhara.ds@intel.com>");
118