571ec80cc46ed3bddef00a466aa5baa47dffcd3c
[openwrt/staging/blocktrron.git] /
1 From 7fc828638fd97e3fc077ccf777351dab909afd04 Mon Sep 17 00:00:00 2001
2 From: Jonathan Bell <jonathan@raspberrypi.org>
3 Date: Thu, 9 May 2019 14:30:37 +0100
4 Subject: [PATCH] drivers: char: add chardev for mmap'ing the RPiVid
5 control registers
6
7 Based on the gpiomem driver, allow mapping of the decoder register
8 spaces such that userspace can access control/status registers.
9 This driver is intended for use with a custom ffmpeg backend accelerator
10 prior to a v4l2 driver being written.
11
12 Signed-off-by: Jonathan Bell <jonathan@raspberrypi.org>
13
14 driver: char: rpivid: Destroy the legacy device on remove
15
16 The legacy name support created a new device that was never destroyed.
17 If the driver was unloaded and reloaded, it failed due to the
18 device already existing.
19
20 Fixes: "75f1d14 driver: char: rpivid - also support legacy name"
21 Signed-off-by: Dave Stevenson <dave.stevenson@raspberrypi.com>
22
23 driver: char: rpivid: Clean up error handling use of ERR_PTR/IS_ERR
24
25 The driver used an unnecessary intermediate void* variable so it
26 only called ERR_PTR once to convert to the error value.
27
28 Switch to converting as the error arises to remove these intermediate
29 variables.
30
31 Signed-off-by: Dave Stevenson <dave.stevenson@raspberrypi.com>
32
33 driver: char: rpivid: Add error handling to the legacy device load
34
35 The return value from device_create for the legacy device was never
36 checked or handled. Add the required error handling.
37
38 Signed-off-by: Dave Stevenson <dave.stevenson@raspberrypi.com>
39
40 driver: char: rpivid: Fix coding style whitespace issues.
41
42 Makes checkpatch happier.
43
44 Signed-off-by: Dave Stevenson <dave.stevenson@raspberrypi.com>
45
46 driver: char: rpimem: Add SPDX licence header.
47
48 Stops checkpatch complaining.
49
50 Signed-off-by: Dave Stevenson <dave.stevenson@raspberrypi.com>
51
52 driver: char: rpivid: Fix access to freed memory
53
54 The error path during probe frees the private memory block, and
55 then promptly dereferences it to log an error message.
56
57 Use the base device instead of the pointer to it in the private
58 structure.
59
60 Signed-off-by: Dave Stevenson <dave.stevenson@raspberrypi.com>
61
62 driver: char: rpivid: Remove legacy name support
63
64 Signed-off-by: Phil Elwell <phil@raspberrypi.com>
65
66 driver: char: rpivid: Don't map more than wanted
67
68 Limit mappings to the permitted range, but don't map more than asked
69 for otherwise we walk off the end of the allocated VMA.
70
71 Signed-off-by: Phil Elwell <phil@raspberrypi.com>
72 ---
73 drivers/char/broadcom/Kconfig | 8 +
74 drivers/char/broadcom/Makefile | 1 +
75 drivers/char/broadcom/rpivid-mem.c | 270 +++++++++++++++++++++++++++++
76 3 files changed, 279 insertions(+)
77 create mode 100644 drivers/char/broadcom/rpivid-mem.c
78
79 --- a/drivers/char/broadcom/Kconfig
80 +++ b/drivers/char/broadcom/Kconfig
81 @@ -39,3 +39,11 @@ config BCM2835_SMI_DEV
82 This driver provides a character device interface (ioctl + read/write) to
83 Broadcom's Secondary Memory interface. The low-level functionality is provided
84 by the SMI driver itself.
85 +
86 +config RPIVID_MEM
87 + tristate "Character device driver for the Raspberry Pi RPIVid video decoder hardware"
88 + default n
89 + help
90 + This driver provides a character device interface for memory-map operations
91 + so userspace tools can access the control and status registers of the
92 + Raspberry Pi RPiVid video decoder hardware.
93 --- a/drivers/char/broadcom/Makefile
94 +++ b/drivers/char/broadcom/Makefile
95 @@ -2,3 +2,4 @@ obj-$(CONFIG_BCM2708_VCMEM) += vc_mem.o
96 obj-$(CONFIG_BCM_VCIO) += vcio.o
97 obj-$(CONFIG_BCM2835_DEVGPIOMEM)+= bcm2835-gpiomem.o
98 obj-$(CONFIG_BCM2835_SMI_DEV) += bcm2835_smi_dev.o
99 +obj-$(CONFIG_RPIVID_MEM) += rpivid-mem.o
100 --- /dev/null
101 +++ b/drivers/char/broadcom/rpivid-mem.c
102 @@ -0,0 +1,270 @@
103 +// SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
104 +/**
105 + * rpivid-mem.c - character device access to the RPiVid decoder registers
106 + *
107 + * Based on bcm2835-gpiomem.c. Provides IO memory access to the decoder
108 + * register blocks such that ffmpeg plugins can access the hardware.
109 + *
110 + * Jonathan Bell <jonathan@raspberrypi.org>
111 + * Copyright (c) 2019, Raspberry Pi (Trading) Ltd.
112 + *
113 + * Redistribution and use in source and binary forms, with or without
114 + * modification, are permitted provided that the following conditions
115 + * are met:
116 + * 1. Redistributions of source code must retain the above copyright
117 + * notice, this list of conditions, and the following disclaimer,
118 + * without modification.
119 + * 2. Redistributions in binary form must reproduce the above copyright
120 + * notice, this list of conditions and the following disclaimer in the
121 + * documentation and/or other materials provided with the distribution.
122 + * 3. The names of the above-listed copyright holders may not be used
123 + * to endorse or promote products derived from this software without
124 + * specific prior written permission.
125 + *
126 + * ALTERNATIVELY, this software may be distributed under the terms of the
127 + * GNU General Public License ("GPL") version 2, as published by the Free
128 + * Software Foundation.
129 + *
130 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
131 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
132 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
133 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
134 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
135 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
136 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
137 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
138 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
139 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
140 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
141 + */
142 +
143 +#include <linux/kernel.h>
144 +#include <linux/module.h>
145 +#include <linux/of.h>
146 +#include <linux/of_device.h>
147 +#include <linux/platform_device.h>
148 +#include <linux/mm.h>
149 +#include <linux/slab.h>
150 +#include <linux/cdev.h>
151 +#include <linux/pagemap.h>
152 +#include <linux/io.h>
153 +
154 +#define DRIVER_NAME "rpivid-mem"
155 +#define DEVICE_MINOR 0
156 +
157 +struct rpivid_mem_priv {
158 + dev_t devid;
159 + struct class *class;
160 + struct cdev rpivid_mem_cdev;
161 + unsigned long regs_phys;
162 + unsigned long mem_window_len;
163 + struct device *dev;
164 + const char *name;
165 +};
166 +
167 +static int rpivid_mem_open(struct inode *inode, struct file *file)
168 +{
169 + int dev = iminor(inode);
170 + int ret = 0;
171 + struct rpivid_mem_priv *priv;
172 +
173 + if (dev != DEVICE_MINOR && dev != DEVICE_MINOR + 1)
174 + ret = -ENXIO;
175 +
176 + priv = container_of(inode->i_cdev, struct rpivid_mem_priv,
177 + rpivid_mem_cdev);
178 + if (!priv)
179 + return -EINVAL;
180 + file->private_data = priv;
181 + return ret;
182 +}
183 +
184 +static int rpivid_mem_release(struct inode *inode, struct file *file)
185 +{
186 + int dev = iminor(inode);
187 + int ret = 0;
188 +
189 + if (dev != DEVICE_MINOR && dev != DEVICE_MINOR + 1)
190 + ret = -ENXIO;
191 +
192 + return ret;
193 +}
194 +
195 +static const struct vm_operations_struct rpivid_mem_vm_ops = {
196 +#ifdef CONFIG_HAVE_IOREMAP_PROT
197 + .access = generic_access_phys
198 +#endif
199 +};
200 +
201 +static int rpivid_mem_mmap(struct file *file, struct vm_area_struct *vma)
202 +{
203 + struct rpivid_mem_priv *priv;
204 + unsigned long pages;
205 + unsigned long len;
206 +
207 + priv = file->private_data;
208 + pages = priv->regs_phys >> PAGE_SHIFT;
209 + /*
210 + * The address decode is far larger than the actual number of registers.
211 + * Just map the whole lot in.
212 + */
213 + len = min(vma->vm_end - vma->vm_start, priv->mem_window_len);
214 + vma->vm_page_prot = phys_mem_access_prot(file, pages, len,
215 + vma->vm_page_prot);
216 + vma->vm_ops = &rpivid_mem_vm_ops;
217 + if (remap_pfn_range(vma, vma->vm_start,
218 + pages, len,
219 + vma->vm_page_prot)) {
220 + return -EAGAIN;
221 + }
222 + return 0;
223 +}
224 +
225 +static const struct file_operations
226 +rpivid_mem_fops = {
227 + .owner = THIS_MODULE,
228 + .open = rpivid_mem_open,
229 + .release = rpivid_mem_release,
230 + .mmap = rpivid_mem_mmap,
231 +};
232 +
233 +static const struct of_device_id rpivid_mem_of_match[];
234 +static int rpivid_mem_probe(struct platform_device *pdev)
235 +{
236 + int err;
237 + const struct of_device_id *id;
238 + struct device *dev = &pdev->dev;
239 + struct resource *ioresource;
240 + struct rpivid_mem_priv *priv;
241 +
242 + /* Allocate buffers and instance data */
243 +
244 + priv = kzalloc(sizeof(struct rpivid_mem_priv), GFP_KERNEL);
245 +
246 + if (!priv) {
247 + err = -ENOMEM;
248 + goto failed_inst_alloc;
249 + }
250 + platform_set_drvdata(pdev, priv);
251 +
252 + priv->dev = dev;
253 + id = of_match_device(rpivid_mem_of_match, dev);
254 + if (!id)
255 + return -EINVAL;
256 + priv->name = id->data;
257 +
258 + ioresource = platform_get_resource(pdev, IORESOURCE_MEM, 0);
259 + if (ioresource) {
260 + priv->regs_phys = ioresource->start;
261 + priv->mem_window_len = (ioresource->end + 1) - ioresource->start;
262 + } else {
263 + dev_err(priv->dev, "failed to get IO resource");
264 + err = -ENOENT;
265 + goto failed_get_resource;
266 + }
267 +
268 + /* Create character device entries */
269 +
270 + err = alloc_chrdev_region(&priv->devid,
271 + DEVICE_MINOR, 2, priv->name);
272 + if (err != 0) {
273 + dev_err(priv->dev, "unable to allocate device number");
274 + goto failed_alloc_chrdev;
275 + }
276 + cdev_init(&priv->rpivid_mem_cdev, &rpivid_mem_fops);
277 + priv->rpivid_mem_cdev.owner = THIS_MODULE;
278 + err = cdev_add(&priv->rpivid_mem_cdev, priv->devid, 2);
279 + if (err != 0) {
280 + dev_err(priv->dev, "unable to register device");
281 + goto failed_cdev_add;
282 + }
283 +
284 + /* Create sysfs entries */
285 +
286 + priv->class = class_create(THIS_MODULE, priv->name);
287 + if (IS_ERR(priv->class)) {
288 + err = PTR_ERR(priv->class);
289 + goto failed_class_create;
290 + }
291 +
292 + dev = device_create(priv->class, NULL, priv->devid, NULL, priv->name);
293 + if (IS_ERR(dev)) {
294 + err = PTR_ERR(dev);
295 + goto failed_device_create;
296 + }
297 +
298 + dev_info(priv->dev, "%s initialised: Registers at 0x%08lx length 0x%08lx",
299 + priv->name, priv->regs_phys, priv->mem_window_len);
300 +
301 + return 0;
302 +
303 +failed_device_create:
304 + class_destroy(priv->class);
305 +failed_class_create:
306 + cdev_del(&priv->rpivid_mem_cdev);
307 +failed_cdev_add:
308 + unregister_chrdev_region(priv->devid, 1);
309 +failed_alloc_chrdev:
310 +failed_get_resource:
311 + kfree(priv);
312 +failed_inst_alloc:
313 + dev_err(&pdev->dev, "could not load rpivid_mem");
314 + return err;
315 +}
316 +
317 +static int rpivid_mem_remove(struct platform_device *pdev)
318 +{
319 + struct device *dev = &pdev->dev;
320 + struct rpivid_mem_priv *priv = platform_get_drvdata(pdev);
321 +
322 + device_destroy(priv->class, priv->devid);
323 + class_destroy(priv->class);
324 + cdev_del(&priv->rpivid_mem_cdev);
325 + unregister_chrdev_region(priv->devid, 1);
326 + kfree(priv);
327 +
328 + dev_info(dev, "%s driver removed - OK", priv->name);
329 + return 0;
330 +}
331 +
332 +static const struct of_device_id rpivid_mem_of_match[] = {
333 + {
334 + .compatible = "raspberrypi,rpivid-hevc-decoder",
335 + .data = "rpivid-hevcmem",
336 + },
337 + {
338 + .compatible = "raspberrypi,rpivid-h264-decoder",
339 + .data = "rpivid-h264mem",
340 + },
341 + {
342 + .compatible = "raspberrypi,rpivid-vp9-decoder",
343 + .data = "rpivid-vp9mem",
344 + },
345 + /* The "intc" is included as this block of hardware contains the
346 + * "frame done" status flags.
347 + */
348 + {
349 + .compatible = "raspberrypi,rpivid-local-intc",
350 + .data = "rpivid-intcmem",
351 + },
352 + { /* sentinel */ },
353 +};
354 +
355 +MODULE_DEVICE_TABLE(of, rpivid_mem_of_match);
356 +
357 +static struct platform_driver rpivid_mem_driver = {
358 + .probe = rpivid_mem_probe,
359 + .remove = rpivid_mem_remove,
360 + .driver = {
361 + .name = DRIVER_NAME,
362 + .owner = THIS_MODULE,
363 + .of_match_table = rpivid_mem_of_match,
364 + },
365 +};
366 +
367 +module_platform_driver(rpivid_mem_driver);
368 +
369 +MODULE_ALIAS("platform:rpivid-mem");
370 +MODULE_LICENSE("GPL");
371 +MODULE_DESCRIPTION("Driver for accessing RPiVid decoder registers from userspace");
372 +MODULE_AUTHOR("Jonathan Bell <jonathan@raspberrypi.org>");