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
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.
12 Signed-off-by: Jonathan Bell <jonathan@raspberrypi.org>
14 driver: char: rpivid: Destroy the legacy device on remove
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.
20 Fixes: "75f1d14 driver: char: rpivid - also support legacy name"
21 Signed-off-by: Dave Stevenson <dave.stevenson@raspberrypi.com>
23 driver: char: rpivid: Clean up error handling use of ERR_PTR/IS_ERR
25 The driver used an unnecessary intermediate void* variable so it
26 only called ERR_PTR once to convert to the error value.
28 Switch to converting as the error arises to remove these intermediate
31 Signed-off-by: Dave Stevenson <dave.stevenson@raspberrypi.com>
33 driver: char: rpivid: Add error handling to the legacy device load
35 The return value from device_create for the legacy device was never
36 checked or handled. Add the required error handling.
38 Signed-off-by: Dave Stevenson <dave.stevenson@raspberrypi.com>
40 driver: char: rpivid: Fix coding style whitespace issues.
42 Makes checkpatch happier.
44 Signed-off-by: Dave Stevenson <dave.stevenson@raspberrypi.com>
46 driver: char: rpimem: Add SPDX licence header.
48 Stops checkpatch complaining.
50 Signed-off-by: Dave Stevenson <dave.stevenson@raspberrypi.com>
52 driver: char: rpivid: Fix access to freed memory
54 The error path during probe frees the private memory block, and
55 then promptly dereferences it to log an error message.
57 Use the base device instead of the pointer to it in the private
60 Signed-off-by: Dave Stevenson <dave.stevenson@raspberrypi.com>
62 driver: char: rpivid: Remove legacy name support
64 Signed-off-by: Phil Elwell <phil@raspberrypi.com>
66 driver: char: rpivid: Don't map more than wanted
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.
71 Signed-off-by: Phil Elwell <phil@raspberrypi.com>
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
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.
87 + tristate "Character device driver for the Raspberry Pi RPIVid video decoder hardware"
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
101 +++ b/drivers/char/broadcom/rpivid-mem.c
103 +// SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
105 + * rpivid-mem.c - character device access to the RPiVid decoder registers
107 + * Based on bcm2835-gpiomem.c. Provides IO memory access to the decoder
108 + * register blocks such that ffmpeg plugins can access the hardware.
110 + * Jonathan Bell <jonathan@raspberrypi.org>
111 + * Copyright (c) 2019, Raspberry Pi (Trading) Ltd.
113 + * Redistribution and use in source and binary forms, with or without
114 + * modification, are permitted provided that the following conditions
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.
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.
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.
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>
154 +#define DRIVER_NAME "rpivid-mem"
155 +#define DEVICE_MINOR 0
157 +struct rpivid_mem_priv {
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;
167 +static int rpivid_mem_open(struct inode *inode, struct file *file)
169 + int dev = iminor(inode);
171 + struct rpivid_mem_priv *priv;
173 + if (dev != DEVICE_MINOR && dev != DEVICE_MINOR + 1)
176 + priv = container_of(inode->i_cdev, struct rpivid_mem_priv,
180 + file->private_data = priv;
184 +static int rpivid_mem_release(struct inode *inode, struct file *file)
186 + int dev = iminor(inode);
189 + if (dev != DEVICE_MINOR && dev != DEVICE_MINOR + 1)
195 +static const struct vm_operations_struct rpivid_mem_vm_ops = {
196 +#ifdef CONFIG_HAVE_IOREMAP_PROT
197 + .access = generic_access_phys
201 +static int rpivid_mem_mmap(struct file *file, struct vm_area_struct *vma)
203 + struct rpivid_mem_priv *priv;
204 + unsigned long pages;
207 + priv = file->private_data;
208 + pages = priv->regs_phys >> PAGE_SHIFT;
210 + * The address decode is far larger than the actual number of registers.
211 + * Just map the whole lot in.
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,
219 + vma->vm_page_prot)) {
225 +static const struct file_operations
227 + .owner = THIS_MODULE,
228 + .open = rpivid_mem_open,
229 + .release = rpivid_mem_release,
230 + .mmap = rpivid_mem_mmap,
233 +static const struct of_device_id rpivid_mem_of_match[];
234 +static int rpivid_mem_probe(struct platform_device *pdev)
237 + const struct of_device_id *id;
238 + struct device *dev = &pdev->dev;
239 + struct resource *ioresource;
240 + struct rpivid_mem_priv *priv;
242 + /* Allocate buffers and instance data */
244 + priv = kzalloc(sizeof(struct rpivid_mem_priv), GFP_KERNEL);
248 + goto failed_inst_alloc;
250 + platform_set_drvdata(pdev, priv);
253 + id = of_match_device(rpivid_mem_of_match, dev);
256 + priv->name = id->data;
258 + ioresource = platform_get_resource(pdev, IORESOURCE_MEM, 0);
260 + priv->regs_phys = ioresource->start;
261 + priv->mem_window_len = (ioresource->end + 1) - ioresource->start;
263 + dev_err(priv->dev, "failed to get IO resource");
265 + goto failed_get_resource;
268 + /* Create character device entries */
270 + err = alloc_chrdev_region(&priv->devid,
271 + DEVICE_MINOR, 2, priv->name);
273 + dev_err(priv->dev, "unable to allocate device number");
274 + goto failed_alloc_chrdev;
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);
280 + dev_err(priv->dev, "unable to register device");
281 + goto failed_cdev_add;
284 + /* Create sysfs entries */
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;
292 + dev = device_create(priv->class, NULL, priv->devid, NULL, priv->name);
294 + err = PTR_ERR(dev);
295 + goto failed_device_create;
298 + dev_info(priv->dev, "%s initialised: Registers at 0x%08lx length 0x%08lx",
299 + priv->name, priv->regs_phys, priv->mem_window_len);
303 +failed_device_create:
304 + class_destroy(priv->class);
305 +failed_class_create:
306 + cdev_del(&priv->rpivid_mem_cdev);
308 + unregister_chrdev_region(priv->devid, 1);
309 +failed_alloc_chrdev:
310 +failed_get_resource:
313 + dev_err(&pdev->dev, "could not load rpivid_mem");
317 +static int rpivid_mem_remove(struct platform_device *pdev)
319 + struct device *dev = &pdev->dev;
320 + struct rpivid_mem_priv *priv = platform_get_drvdata(pdev);
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);
328 + dev_info(dev, "%s driver removed - OK", priv->name);
332 +static const struct of_device_id rpivid_mem_of_match[] = {
334 + .compatible = "raspberrypi,rpivid-hevc-decoder",
335 + .data = "rpivid-hevcmem",
338 + .compatible = "raspberrypi,rpivid-h264-decoder",
339 + .data = "rpivid-h264mem",
342 + .compatible = "raspberrypi,rpivid-vp9-decoder",
343 + .data = "rpivid-vp9mem",
345 + /* The "intc" is included as this block of hardware contains the
346 + * "frame done" status flags.
349 + .compatible = "raspberrypi,rpivid-local-intc",
350 + .data = "rpivid-intcmem",
352 + { /* sentinel */ },
355 +MODULE_DEVICE_TABLE(of, rpivid_mem_of_match);
357 +static struct platform_driver rpivid_mem_driver = {
358 + .probe = rpivid_mem_probe,
359 + .remove = rpivid_mem_remove,
361 + .name = DRIVER_NAME,
362 + .owner = THIS_MODULE,
363 + .of_match_table = rpivid_mem_of_match,
367 +module_platform_driver(rpivid_mem_driver);
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>");