1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Provides user-space access to the SSAM EC via the /dev/surface/aggregator
4  * misc device. Intended for debugging and development.
5  *
6  * Copyright (C) 2020 Maximilian Luz <[email protected]>
7  */
8 
9 #include <linux/fs.h>
10 #include <linux/kernel.h>
11 #include <linux/kref.h>
12 #include <linux/miscdevice.h>
13 #include <linux/module.h>
14 #include <linux/platform_device.h>
15 #include <linux/rwsem.h>
16 #include <linux/slab.h>
17 #include <linux/uaccess.h>
18 
19 #include <linux/surface_aggregator/cdev.h>
20 #include <linux/surface_aggregator/controller.h>
21 
22 #define SSAM_CDEV_DEVICE_NAME	"surface_aggregator_cdev"
23 
24 struct ssam_cdev {
25 	struct kref kref;
26 	struct rw_semaphore lock;
27 	struct ssam_controller *ctrl;
28 	struct miscdevice mdev;
29 };
30 
31 static void __ssam_cdev_release(struct kref *kref)
32 {
33 	kfree(container_of(kref, struct ssam_cdev, kref));
34 }
35 
36 static struct ssam_cdev *ssam_cdev_get(struct ssam_cdev *cdev)
37 {
38 	if (cdev)
39 		kref_get(&cdev->kref);
40 
41 	return cdev;
42 }
43 
44 static void ssam_cdev_put(struct ssam_cdev *cdev)
45 {
46 	if (cdev)
47 		kref_put(&cdev->kref, __ssam_cdev_release);
48 }
49 
50 static int ssam_cdev_device_open(struct inode *inode, struct file *filp)
51 {
52 	struct miscdevice *mdev = filp->private_data;
53 	struct ssam_cdev *cdev = container_of(mdev, struct ssam_cdev, mdev);
54 
55 	filp->private_data = ssam_cdev_get(cdev);
56 	return stream_open(inode, filp);
57 }
58 
59 static int ssam_cdev_device_release(struct inode *inode, struct file *filp)
60 {
61 	ssam_cdev_put(filp->private_data);
62 	return 0;
63 }
64 
65 static long ssam_cdev_request(struct ssam_cdev *cdev, unsigned long arg)
66 {
67 	struct ssam_cdev_request __user *r;
68 	struct ssam_cdev_request rqst;
69 	struct ssam_request spec = {};
70 	struct ssam_response rsp = {};
71 	const void __user *plddata;
72 	void __user *rspdata;
73 	int status = 0, ret = 0, tmp;
74 
75 	r = (struct ssam_cdev_request __user *)arg;
76 	ret = copy_struct_from_user(&rqst, sizeof(rqst), r, sizeof(*r));
77 	if (ret)
78 		goto out;
79 
80 	plddata = u64_to_user_ptr(rqst.payload.data);
81 	rspdata = u64_to_user_ptr(rqst.response.data);
82 
83 	/* Setup basic request fields. */
84 	spec.target_category = rqst.target_category;
85 	spec.target_id = rqst.target_id;
86 	spec.command_id = rqst.command_id;
87 	spec.instance_id = rqst.instance_id;
88 	spec.flags = 0;
89 	spec.length = rqst.payload.length;
90 	spec.payload = NULL;
91 
92 	if (rqst.flags & SSAM_CDEV_REQUEST_HAS_RESPONSE)
93 		spec.flags |= SSAM_REQUEST_HAS_RESPONSE;
94 
95 	if (rqst.flags & SSAM_CDEV_REQUEST_UNSEQUENCED)
96 		spec.flags |= SSAM_REQUEST_UNSEQUENCED;
97 
98 	rsp.capacity = rqst.response.length;
99 	rsp.length = 0;
100 	rsp.pointer = NULL;
101 
102 	/* Get request payload from user-space. */
103 	if (spec.length) {
104 		if (!plddata) {
105 			ret = -EINVAL;
106 			goto out;
107 		}
108 
109 		spec.payload = kzalloc(spec.length, GFP_KERNEL);
110 		if (!spec.payload) {
111 			ret = -ENOMEM;
112 			goto out;
113 		}
114 
115 		if (copy_from_user((void *)spec.payload, plddata, spec.length)) {
116 			ret = -EFAULT;
117 			goto out;
118 		}
119 	}
120 
121 	/* Allocate response buffer. */
122 	if (rsp.capacity) {
123 		if (!rspdata) {
124 			ret = -EINVAL;
125 			goto out;
126 		}
127 
128 		rsp.pointer = kzalloc(rsp.capacity, GFP_KERNEL);
129 		if (!rsp.pointer) {
130 			ret = -ENOMEM;
131 			goto out;
132 		}
133 	}
134 
135 	/* Perform request. */
136 	status = ssam_request_sync(cdev->ctrl, &spec, &rsp);
137 	if (status)
138 		goto out;
139 
140 	/* Copy response to user-space. */
141 	if (rsp.length && copy_to_user(rspdata, rsp.pointer, rsp.length))
142 		ret = -EFAULT;
143 
144 out:
145 	/* Always try to set response-length and status. */
146 	tmp = put_user(rsp.length, &r->response.length);
147 	if (tmp)
148 		ret = tmp;
149 
150 	tmp = put_user(status, &r->status);
151 	if (tmp)
152 		ret = tmp;
153 
154 	/* Cleanup. */
155 	kfree(spec.payload);
156 	kfree(rsp.pointer);
157 
158 	return ret;
159 }
160 
161 static long __ssam_cdev_device_ioctl(struct ssam_cdev *cdev, unsigned int cmd,
162 				     unsigned long arg)
163 {
164 	switch (cmd) {
165 	case SSAM_CDEV_REQUEST:
166 		return ssam_cdev_request(cdev, arg);
167 
168 	default:
169 		return -ENOTTY;
170 	}
171 }
172 
173 static long ssam_cdev_device_ioctl(struct file *file, unsigned int cmd,
174 				   unsigned long arg)
175 {
176 	struct ssam_cdev *cdev = file->private_data;
177 	long status;
178 
179 	/* Ensure that controller is valid for as long as we need it. */
180 	if (down_read_killable(&cdev->lock))
181 		return -ERESTARTSYS;
182 
183 	if (!cdev->ctrl) {
184 		up_read(&cdev->lock);
185 		return -ENODEV;
186 	}
187 
188 	status = __ssam_cdev_device_ioctl(cdev, cmd, arg);
189 
190 	up_read(&cdev->lock);
191 	return status;
192 }
193 
194 static const struct file_operations ssam_controller_fops = {
195 	.owner          = THIS_MODULE,
196 	.open           = ssam_cdev_device_open,
197 	.release        = ssam_cdev_device_release,
198 	.unlocked_ioctl = ssam_cdev_device_ioctl,
199 	.compat_ioctl   = ssam_cdev_device_ioctl,
200 	.llseek         = noop_llseek,
201 };
202 
203 static int ssam_dbg_device_probe(struct platform_device *pdev)
204 {
205 	struct ssam_controller *ctrl;
206 	struct ssam_cdev *cdev;
207 	int status;
208 
209 	ctrl = ssam_client_bind(&pdev->dev);
210 	if (IS_ERR(ctrl))
211 		return PTR_ERR(ctrl) == -ENODEV ? -EPROBE_DEFER : PTR_ERR(ctrl);
212 
213 	cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
214 	if (!cdev)
215 		return -ENOMEM;
216 
217 	kref_init(&cdev->kref);
218 	init_rwsem(&cdev->lock);
219 	cdev->ctrl = ctrl;
220 
221 	cdev->mdev.parent   = &pdev->dev;
222 	cdev->mdev.minor    = MISC_DYNAMIC_MINOR;
223 	cdev->mdev.name     = "surface_aggregator";
224 	cdev->mdev.nodename = "surface/aggregator";
225 	cdev->mdev.fops     = &ssam_controller_fops;
226 
227 	status = misc_register(&cdev->mdev);
228 	if (status) {
229 		kfree(cdev);
230 		return status;
231 	}
232 
233 	platform_set_drvdata(pdev, cdev);
234 	return 0;
235 }
236 
237 static int ssam_dbg_device_remove(struct platform_device *pdev)
238 {
239 	struct ssam_cdev *cdev = platform_get_drvdata(pdev);
240 
241 	misc_deregister(&cdev->mdev);
242 
243 	/*
244 	 * The controller is only guaranteed to be valid for as long as the
245 	 * driver is bound. Remove controller so that any lingering open files
246 	 * cannot access it any more after we're gone.
247 	 */
248 	down_write(&cdev->lock);
249 	cdev->ctrl = NULL;
250 	up_write(&cdev->lock);
251 
252 	ssam_cdev_put(cdev);
253 	return 0;
254 }
255 
256 static struct platform_device *ssam_cdev_device;
257 
258 static struct platform_driver ssam_cdev_driver = {
259 	.probe = ssam_dbg_device_probe,
260 	.remove = ssam_dbg_device_remove,
261 	.driver = {
262 		.name = SSAM_CDEV_DEVICE_NAME,
263 		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
264 	},
265 };
266 
267 static int __init ssam_debug_init(void)
268 {
269 	int status;
270 
271 	ssam_cdev_device = platform_device_alloc(SSAM_CDEV_DEVICE_NAME,
272 						 PLATFORM_DEVID_NONE);
273 	if (!ssam_cdev_device)
274 		return -ENOMEM;
275 
276 	status = platform_device_add(ssam_cdev_device);
277 	if (status)
278 		goto err_device;
279 
280 	status = platform_driver_register(&ssam_cdev_driver);
281 	if (status)
282 		goto err_driver;
283 
284 	return 0;
285 
286 err_driver:
287 	platform_device_del(ssam_cdev_device);
288 err_device:
289 	platform_device_put(ssam_cdev_device);
290 	return status;
291 }
292 module_init(ssam_debug_init);
293 
294 static void __exit ssam_debug_exit(void)
295 {
296 	platform_driver_unregister(&ssam_cdev_driver);
297 	platform_device_unregister(ssam_cdev_device);
298 }
299 module_exit(ssam_debug_exit);
300 
301 MODULE_AUTHOR("Maximilian Luz <[email protected]>");
302 MODULE_DESCRIPTION("User-space interface for Surface System Aggregator Module");
303 MODULE_LICENSE("GPL");
304