.. | .. |
---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
---|
1 | 2 | /* |
---|
2 | 3 | * 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. |
---|
12 | 4 | */ |
---|
13 | 5 | |
---|
14 | 6 | #include <linux/kernel.h> |
---|
.. | .. |
---|
45 | 37 | struct device_attribute *attr, |
---|
46 | 38 | char *buf); |
---|
47 | 39 | |
---|
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); |
---|
51 | 43 | |
---|
52 | 44 | static ssize_t qcom_rmtfs_mem_show(struct device *dev, |
---|
53 | 45 | struct device_attribute *attr, |
---|
.. | .. |
---|
132 | 124 | return 0; |
---|
133 | 125 | } |
---|
134 | 126 | |
---|
| 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 | + |
---|
135 | 152 | static const struct file_operations qcom_rmtfs_mem_fops = { |
---|
136 | 153 | .owner = THIS_MODULE, |
---|
137 | 154 | .open = qcom_rmtfs_mem_open, |
---|
.. | .. |
---|
139 | 156 | .write = qcom_rmtfs_mem_write, |
---|
140 | 157 | .release = qcom_rmtfs_mem_release, |
---|
141 | 158 | .llseek = default_llseek, |
---|
| 159 | + .mmap = qcom_rmtfs_mem_mmap, |
---|
142 | 160 | }; |
---|
143 | 161 | |
---|
144 | 162 | static void qcom_rmtfs_mem_release_device(struct device *dev) |
---|
.. | .. |
---|
199 | 217 | |
---|
200 | 218 | dev_set_name(&rmtfs_mem->dev, "qcom_rmtfs_mem%d", client_id); |
---|
201 | 219 | rmtfs_mem->dev.id = client_id; |
---|
| 220 | + rmtfs_mem->dev.class = &rmtfs_class; |
---|
202 | 221 | rmtfs_mem->dev.devt = MKDEV(MAJOR(qcom_rmtfs_mem_major), client_id); |
---|
203 | 222 | |
---|
204 | 223 | ret = cdev_device_add(&rmtfs_mem->cdev, &rmtfs_mem->dev); |
---|
.. | .. |
---|
277 | 296 | }, |
---|
278 | 297 | }; |
---|
279 | 298 | |
---|
280 | | -static int qcom_rmtfs_mem_init(void) |
---|
| 299 | +static int __init qcom_rmtfs_mem_init(void) |
---|
281 | 300 | { |
---|
282 | 301 | int ret; |
---|
| 302 | + |
---|
| 303 | + ret = class_register(&rmtfs_class); |
---|
| 304 | + if (ret) |
---|
| 305 | + return ret; |
---|
283 | 306 | |
---|
284 | 307 | ret = alloc_chrdev_region(&qcom_rmtfs_mem_major, 0, |
---|
285 | 308 | QCOM_RMTFS_MEM_DEV_MAX, "qcom_rmtfs_mem"); |
---|
286 | 309 | if (ret < 0) { |
---|
287 | 310 | pr_err("qcom_rmtfs_mem: failed to allocate char dev region\n"); |
---|
288 | | - return ret; |
---|
| 311 | + goto unregister_class; |
---|
289 | 312 | } |
---|
290 | 313 | |
---|
291 | 314 | ret = platform_driver_register(&qcom_rmtfs_mem_driver); |
---|
292 | 315 | if (ret < 0) { |
---|
293 | 316 | 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; |
---|
296 | 318 | } |
---|
297 | 319 | |
---|
| 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); |
---|
298 | 326 | return ret; |
---|
299 | 327 | } |
---|
300 | 328 | module_init(qcom_rmtfs_mem_init); |
---|
301 | 329 | |
---|
302 | | -static void qcom_rmtfs_mem_exit(void) |
---|
| 330 | +static void __exit qcom_rmtfs_mem_exit(void) |
---|
303 | 331 | { |
---|
304 | 332 | platform_driver_unregister(&qcom_rmtfs_mem_driver); |
---|
305 | 333 | unregister_chrdev_region(qcom_rmtfs_mem_major, QCOM_RMTFS_MEM_DEV_MAX); |
---|
| 334 | + class_unregister(&rmtfs_class); |
---|
306 | 335 | } |
---|
307 | 336 | module_exit(qcom_rmtfs_mem_exit); |
---|
308 | 337 | |
---|