hc
2023-12-11 d2ccde1c8e90d38cee87a1b0309ad2827f3fd30d
kernel/drivers/soc/qcom/rmtfs_mem.c
....@@ -1,14 +1,6 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /*
23 * Copyright (c) 2017 Linaro Ltd.
3
- *
4
- * This program is free software; you can redistribute it and/or modify
5
- * it under the terms of the GNU General Public License version 2 and
6
- * only version 2 as published by the Free Software Foundation.
7
- *
8
- * This program is distributed in the hope that it will be useful,
9
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
- * GNU General Public License for more details.
124 */
135
146 #include <linux/kernel.h>
....@@ -45,9 +37,9 @@
4537 struct device_attribute *attr,
4638 char *buf);
4739
48
-static DEVICE_ATTR(phys_addr, 0400, qcom_rmtfs_mem_show, NULL);
49
-static DEVICE_ATTR(size, 0400, qcom_rmtfs_mem_show, NULL);
50
-static DEVICE_ATTR(client_id, 0400, qcom_rmtfs_mem_show, NULL);
40
+static DEVICE_ATTR(phys_addr, 0444, qcom_rmtfs_mem_show, NULL);
41
+static DEVICE_ATTR(size, 0444, qcom_rmtfs_mem_show, NULL);
42
+static DEVICE_ATTR(client_id, 0444, qcom_rmtfs_mem_show, NULL);
5143
5244 static ssize_t qcom_rmtfs_mem_show(struct device *dev,
5345 struct device_attribute *attr,
....@@ -132,6 +124,31 @@
132124 return 0;
133125 }
134126
127
+static struct class rmtfs_class = {
128
+ .owner = THIS_MODULE,
129
+ .name = "rmtfs",
130
+};
131
+
132
+static int qcom_rmtfs_mem_mmap(struct file *filep, struct vm_area_struct *vma)
133
+{
134
+ struct qcom_rmtfs_mem *rmtfs_mem = filep->private_data;
135
+
136
+ if (vma->vm_end - vma->vm_start > rmtfs_mem->size) {
137
+ dev_dbg(&rmtfs_mem->dev,
138
+ "vm_end[%lu] - vm_start[%lu] [%lu] > mem->size[%pa]\n",
139
+ vma->vm_end, vma->vm_start,
140
+ (vma->vm_end - vma->vm_start), &rmtfs_mem->size);
141
+ return -EINVAL;
142
+ }
143
+
144
+ vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
145
+ return remap_pfn_range(vma,
146
+ vma->vm_start,
147
+ rmtfs_mem->addr >> PAGE_SHIFT,
148
+ vma->vm_end - vma->vm_start,
149
+ vma->vm_page_prot);
150
+}
151
+
135152 static const struct file_operations qcom_rmtfs_mem_fops = {
136153 .owner = THIS_MODULE,
137154 .open = qcom_rmtfs_mem_open,
....@@ -139,6 +156,7 @@
139156 .write = qcom_rmtfs_mem_write,
140157 .release = qcom_rmtfs_mem_release,
141158 .llseek = default_llseek,
159
+ .mmap = qcom_rmtfs_mem_mmap,
142160 };
143161
144162 static void qcom_rmtfs_mem_release_device(struct device *dev)
....@@ -199,6 +217,7 @@
199217
200218 dev_set_name(&rmtfs_mem->dev, "qcom_rmtfs_mem%d", client_id);
201219 rmtfs_mem->dev.id = client_id;
220
+ rmtfs_mem->dev.class = &rmtfs_class;
202221 rmtfs_mem->dev.devt = MKDEV(MAJOR(qcom_rmtfs_mem_major), client_id);
203222
204223 ret = cdev_device_add(&rmtfs_mem->cdev, &rmtfs_mem->dev);
....@@ -277,32 +296,42 @@
277296 },
278297 };
279298
280
-static int qcom_rmtfs_mem_init(void)
299
+static int __init qcom_rmtfs_mem_init(void)
281300 {
282301 int ret;
302
+
303
+ ret = class_register(&rmtfs_class);
304
+ if (ret)
305
+ return ret;
283306
284307 ret = alloc_chrdev_region(&qcom_rmtfs_mem_major, 0,
285308 QCOM_RMTFS_MEM_DEV_MAX, "qcom_rmtfs_mem");
286309 if (ret < 0) {
287310 pr_err("qcom_rmtfs_mem: failed to allocate char dev region\n");
288
- return ret;
311
+ goto unregister_class;
289312 }
290313
291314 ret = platform_driver_register(&qcom_rmtfs_mem_driver);
292315 if (ret < 0) {
293316 pr_err("qcom_rmtfs_mem: failed to register rmtfs_mem driver\n");
294
- unregister_chrdev_region(qcom_rmtfs_mem_major,
295
- QCOM_RMTFS_MEM_DEV_MAX);
317
+ goto unregister_chrdev;
296318 }
297319
320
+ return 0;
321
+
322
+unregister_chrdev:
323
+ unregister_chrdev_region(qcom_rmtfs_mem_major, QCOM_RMTFS_MEM_DEV_MAX);
324
+unregister_class:
325
+ class_unregister(&rmtfs_class);
298326 return ret;
299327 }
300328 module_init(qcom_rmtfs_mem_init);
301329
302
-static void qcom_rmtfs_mem_exit(void)
330
+static void __exit qcom_rmtfs_mem_exit(void)
303331 {
304332 platform_driver_unregister(&qcom_rmtfs_mem_driver);
305333 unregister_chrdev_region(qcom_rmtfs_mem_major, QCOM_RMTFS_MEM_DEV_MAX);
334
+ class_unregister(&rmtfs_class);
306335 }
307336 module_exit(qcom_rmtfs_mem_exit);
308337