Skip to content

Commit dffc950

Browse files
Jonathan Bellpopcornmix
authored andcommitted
drivers: char: add chardev for mmap'ing the RPiVid control registers
Based on the gpiomem driver, allow mapping of the decoder register spaces such that userspace can access control/status registers. This driver is intended for use with a custom ffmpeg backend accelerator prior to a v4l2 driver being written. Signed-off-by: Jonathan Bell <jonathan@raspberrypi.org> driver: char: rpivid: Destroy the legacy device on remove The legacy name support created a new device that was never destroyed. If the driver was unloaded and reloaded, it failed due to the device already existing. Fixes: "75f1d14 driver: char: rpivid - also support legacy name" Signed-off-by: Dave Stevenson <dave.stevenson@raspberrypi.com> driver: char: rpivid: Clean up error handling use of ERR_PTR/IS_ERR The driver used an unnecessary intermediate void* variable so it only called ERR_PTR once to convert to the error value. Switch to converting as the error arises to remove these intermediate variables. Signed-off-by: Dave Stevenson <dave.stevenson@raspberrypi.com> driver: char: rpivid: Add error handling to the legacy device load The return value from device_create for the legacy device was never checked or handled. Add the required error handling. Signed-off-by: Dave Stevenson <dave.stevenson@raspberrypi.com> driver: char: rpivid: Fix coding style whitespace issues. Makes checkpatch happier. Signed-off-by: Dave Stevenson <dave.stevenson@raspberrypi.com> driver: char: rpimem: Add SPDX licence header. Stops checkpatch complaining. Signed-off-by: Dave Stevenson <dave.stevenson@raspberrypi.com> driver: char: rpivid: Fix access to freed memory The error path during probe frees the private memory block, and then promptly dereferences it to log an error message. Use the base device instead of the pointer to it in the private structure. Signed-off-by: Dave Stevenson <dave.stevenson@raspberrypi.com>
1 parent 3e16085 commit dffc950

6 files changed

Lines changed: 311 additions & 10 deletions

File tree

‎drivers/char/broadcom/Kconfig‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,11 @@ config BCM2835_SMI_DEV
4949
This driver provides a character device interface (ioctl + read/write) to
5050
Broadcom's Secondary Memory interface. The low-level functionality is provided
5151
by the SMI driver itself.
52+
53+
config RPIVID_MEM
54+
tristate "Character device driver for the Raspberry Pi RPIVid video decoder hardware"
55+
default n
56+
help
57+
This driver provides a character device interface for memory-map operations
58+
so userspace tools can access the control and status registers of the
59+
Raspberry Pi RPiVid video decoder hardware.

‎drivers/char/broadcom/Makefile‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ obj-$(CONFIG_BCM_VC_SM) += vc_sm/
44

55
obj-$(CONFIG_BCM2835_DEVGPIOMEM)+= bcm2835-gpiomem.o
66
obj-$(CONFIG_BCM2835_SMI_DEV) += bcm2835_smi_dev.o
7+
obj-$(CONFIG_RPIVID_MEM) += rpivid-mem.o

‎drivers/char/broadcom/rpivid-mem.c‎

Lines changed: 292 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,292 @@
1+
// SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2+
/**
3+
* rpivid-mem.c - character device access to the RPiVid decoder registers
4+
*
5+
* Based on bcm2835-gpiomem.c. Provides IO memory access to the decoder
6+
* register blocks such that ffmpeg plugins can access the hardware.
7+
*
8+
* Jonathan Bell <jonathan@raspberrypi.org>
9+
* Copyright (c) 2019, Raspberry Pi (Trading) Ltd.
10+
*
11+
* Redistribution and use in source and binary forms, with or without
12+
* modification, are permitted provided that the following conditions
13+
* are met:
14+
* 1. Redistributions of source code must retain the above copyright
15+
* notice, this list of conditions, and the following disclaimer,
16+
* without modification.
17+
* 2. Redistributions in binary form must reproduce the above copyright
18+
* notice, this list of conditions and the following disclaimer in the
19+
* documentation and/or other materials provided with the distribution.
20+
* 3. The names of the above-listed copyright holders may not be used
21+
* to endorse or promote products derived from this software without
22+
* specific prior written permission.
23+
*
24+
* ALTERNATIVELY, this software may be distributed under the terms of the
25+
* GNU General Public License ("GPL") version 2, as published by the Free
26+
* Software Foundation.
27+
*
28+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
29+
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
30+
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
31+
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
32+
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
33+
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
34+
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
35+
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
36+
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
37+
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
38+
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39+
*/
40+
41+
#include <linux/kernel.h>
42+
#include <linux/module.h>
43+
#include <linux/of.h>
44+
#include <linux/of_device.h>
45+
#include <linux/platform_device.h>
46+
#include <linux/mm.h>
47+
#include <linux/slab.h>
48+
#include <linux/cdev.h>
49+
#include <linux/pagemap.h>
50+
#include <linux/io.h>
51+
52+
#define DRIVER_NAME "rpivid-mem"
53+
#define DEVICE_MINOR 0
54+
55+
struct rpivid_mem_priv {
56+
dev_t devid;
57+
struct class *class;
58+
struct cdev rpivid_mem_cdev;
59+
unsigned long regs_phys;
60+
unsigned long mem_window_len;
61+
struct device *dev;
62+
const char *name;
63+
};
64+
65+
static int rpivid_mem_open(struct inode *inode, struct file *file)
66+
{
67+
int dev = iminor(inode);
68+
int ret = 0;
69+
struct rpivid_mem_priv *priv;
70+
71+
if (dev != DEVICE_MINOR && dev != DEVICE_MINOR + 1)
72+
ret = -ENXIO;
73+
74+
priv = container_of(inode->i_cdev, struct rpivid_mem_priv,
75+
rpivid_mem_cdev);
76+
if (!priv)
77+
return -EINVAL;
78+
file->private_data = priv;
79+
return ret;
80+
}
81+
82+
static int rpivid_mem_release(struct inode *inode, struct file *file)
83+
{
84+
int dev = iminor(inode);
85+
int ret = 0;
86+
87+
if (dev != DEVICE_MINOR && dev != DEVICE_MINOR + 1)
88+
ret = -ENXIO;
89+
90+
return ret;
91+
}
92+
93+
static const struct vm_operations_struct rpivid_mem_vm_ops = {
94+
#ifdef CONFIG_HAVE_IOREMAP_PROT
95+
.access = generic_access_phys
96+
#endif
97+
};
98+
99+
static int rpivid_mem_mmap(struct file *file, struct vm_area_struct *vma)
100+
{
101+
struct rpivid_mem_priv *priv;
102+
unsigned long pages;
103+
104+
priv = file->private_data;
105+
pages = priv->regs_phys >> PAGE_SHIFT;
106+
/*
107+
* The address decode is far larger than the actual number of registers.
108+
* Just map the whole lot in.
109+
*/
110+
vma->vm_page_prot = phys_mem_access_prot(file, pages,
111+
priv->mem_window_len,
112+
vma->vm_page_prot);
113+
vma->vm_ops = &rpivid_mem_vm_ops;
114+
if (remap_pfn_range(vma, vma->vm_start,
115+
pages,
116+
priv->mem_window_len,
117+
vma->vm_page_prot)) {
118+
return -EAGAIN;
119+
}
120+
return 0;
121+
}
122+
123+
static const struct file_operations
124+
rpivid_mem_fops = {
125+
.owner = THIS_MODULE,
126+
.open = rpivid_mem_open,
127+
.release = rpivid_mem_release,
128+
.mmap = rpivid_mem_mmap,
129+
};
130+
131+
static const struct of_device_id rpivid_mem_of_match[];
132+
static int rpivid_mem_probe(struct platform_device *pdev)
133+
{
134+
int err;
135+
const struct of_device_id *id;
136+
struct device *dev = &pdev->dev;
137+
struct resource *ioresource;
138+
struct rpivid_mem_priv *priv;
139+
140+
/* Allocate buffers and instance data */
141+
142+
priv = kzalloc(sizeof(struct rpivid_mem_priv), GFP_KERNEL);
143+
144+
if (!priv) {
145+
err = -ENOMEM;
146+
goto failed_inst_alloc;
147+
}
148+
platform_set_drvdata(pdev, priv);
149+
150+
priv->dev = dev;
151+
id = of_match_device(rpivid_mem_of_match, dev);
152+
if (!id)
153+
return -EINVAL;
154+
priv->name = id->data;
155+
156+
ioresource = platform_get_resource(pdev, IORESOURCE_MEM, 0);
157+
if (ioresource) {
158+
priv->regs_phys = ioresource->start;
159+
priv->mem_window_len = ioresource->end - ioresource->start;
160+
} else {
161+
dev_err(priv->dev, "failed to get IO resource");
162+
err = -ENOENT;
163+
goto failed_get_resource;
164+
}
165+
166+
/* Create character device entries */
167+
168+
err = alloc_chrdev_region(&priv->devid,
169+
DEVICE_MINOR, 2, priv->name);
170+
if (err != 0) {
171+
dev_err(priv->dev, "unable to allocate device number");
172+
goto failed_alloc_chrdev;
173+
}
174+
cdev_init(&priv->rpivid_mem_cdev, &rpivid_mem_fops);
175+
priv->rpivid_mem_cdev.owner = THIS_MODULE;
176+
err = cdev_add(&priv->rpivid_mem_cdev, priv->devid, 2);
177+
if (err != 0) {
178+
dev_err(priv->dev, "unable to register device");
179+
goto failed_cdev_add;
180+
}
181+
182+
/* Create sysfs entries */
183+
184+
priv->class = class_create(THIS_MODULE, priv->name);
185+
if (IS_ERR(priv->class)) {
186+
err = PTR_ERR(priv->class);
187+
goto failed_class_create;
188+
}
189+
190+
dev = device_create(priv->class, NULL, priv->devid, NULL, priv->name);
191+
if (IS_ERR(dev)) {
192+
err = PTR_ERR(dev);
193+
goto failed_device_create;
194+
}
195+
196+
/* Legacy alias */
197+
{
198+
char *oldname = kstrdup(priv->name, GFP_KERNEL);
199+
200+
oldname[1] = 'a';
201+
oldname[2] = 'r';
202+
oldname[3] = 'g';
203+
oldname[4] = 'o';
204+
oldname[5] = 'n';
205+
dev = device_create(priv->class, NULL, priv->devid + 1, NULL,
206+
oldname + 1);
207+
kfree(oldname);
208+
209+
if (IS_ERR(dev)) {
210+
err = PTR_ERR(dev);
211+
goto failed_legacy_device_create;
212+
}
213+
}
214+
215+
dev_info(priv->dev, "%s initialised: Registers at 0x%08lx length 0x%08lx",
216+
priv->name, priv->regs_phys, priv->mem_window_len);
217+
218+
return 0;
219+
220+
failed_legacy_device_create:
221+
device_destroy(priv->class, priv->devid);
222+
failed_device_create:
223+
class_destroy(priv->class);
224+
failed_class_create:
225+
cdev_del(&priv->rpivid_mem_cdev);
226+
failed_cdev_add:
227+
unregister_chrdev_region(priv->devid, 1);
228+
failed_alloc_chrdev:
229+
failed_get_resource:
230+
kfree(priv);
231+
failed_inst_alloc:
232+
dev_err(&pdev->dev, "could not load rpivid_mem");
233+
return err;
234+
}
235+
236+
static int rpivid_mem_remove(struct platform_device *pdev)
237+
{
238+
struct device *dev = &pdev->dev;
239+
struct rpivid_mem_priv *priv = platform_get_drvdata(pdev);
240+
241+
device_destroy(priv->class, priv->devid + 1);
242+
device_destroy(priv->class, priv->devid);
243+
class_destroy(priv->class);
244+
cdev_del(&priv->rpivid_mem_cdev);
245+
unregister_chrdev_region(priv->devid, 1);
246+
kfree(priv);
247+
248+
dev_info(dev, "%s driver removed - OK", priv->name);
249+
return 0;
250+
}
251+
252+
static const struct of_device_id rpivid_mem_of_match[] = {
253+
{
254+
.compatible = "raspberrypi,rpivid-hevc-decoder",
255+
.data = "rpivid-hevcmem",
256+
},
257+
{
258+
.compatible = "raspberrypi,rpivid-h264-decoder",
259+
.data = "rpivid-h264mem",
260+
},
261+
{
262+
.compatible = "raspberrypi,rpivid-vp9-decoder",
263+
.data = "rpivid-vp9mem",
264+
},
265+
/* The "intc" is included as this block of hardware contains the
266+
* "frame done" status flags.
267+
*/
268+
{
269+
.compatible = "raspberrypi,rpivid-local-intc",
270+
.data = "rpivid-intcmem",
271+
},
272+
{ /* sentinel */ },
273+
};
274+
275+
MODULE_DEVICE_TABLE(of, rpivid_mem_of_match);
276+
277+
static struct platform_driver rpivid_mem_driver = {
278+
.probe = rpivid_mem_probe,
279+
.remove = rpivid_mem_remove,
280+
.driver = {
281+
.name = DRIVER_NAME,
282+
.owner = THIS_MODULE,
283+
.of_match_table = rpivid_mem_of_match,
284+
},
285+
};
286+
287+
module_platform_driver(rpivid_mem_driver);
288+
289+
MODULE_ALIAS("platform:rpivid-mem");
290+
MODULE_LICENSE("GPL");
291+
MODULE_DESCRIPTION("Driver for accessing RPiVid decoder registers from userspace");
292+
MODULE_AUTHOR("Jonathan Bell <jonathan@raspberrypi.org>");

‎drivers/mfd/bcm2835-pm.c‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,14 @@ static int bcm2835_pm_probe(struct platform_device *pdev)
5050
if (ret)
5151
return ret;
5252

53-
/* Map the ARGON ASB regs if present. */
53+
/* Map the RPiVid ASB regs if present. */
5454
res = platform_get_resource(pdev, IORESOURCE_MEM, 2);
5555
if (res) {
56-
pm->arg_asb = devm_ioremap_resource(dev, res);
57-
if (IS_ERR(pm->arg_asb)) {
58-
dev_err(dev, "Failed to map ARGON ASB: %ld\n",
59-
PTR_ERR(pm->arg_asb));
60-
return PTR_ERR(pm->arg_asb);
56+
pm->rpivid_asb = devm_ioremap_resource(dev, res);
57+
if (IS_ERR(pm->rpivid_asb)) {
58+
dev_err(dev, "Failed to map RPiVid ASB: %ld\n",
59+
PTR_ERR(pm->rpivid_asb));
60+
return PTR_ERR(pm->rpivid_asb);
6161
}
6262
}
6363

‎drivers/soc/bcm/bcm2835-power.c‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -637,15 +637,15 @@ static int bcm2835_power_probe(struct platform_device *pdev)
637637
power->base = pm->base;
638638
power->asb = pm->asb;
639639

640-
/* 2711 hack: the new ARGON ASB took over V3D, which is our
640+
/* 2711 hack: the new RPiVid ASB took over V3D, which is our
641641
* only consumer of this driver so far. The old ASB seems to
642642
* still be present with ISP and H264 bits but no V3D, but I
643643
* don't know if that's real or not. The V3D is in the same
644644
* place in the new ASB as the old one, so just poke the new
645645
* one for now.
646646
*/
647-
if (pm->arg_asb) {
648-
power->asb = pm->arg_asb;
647+
if (pm->rpivid_asb) {
648+
power->asb = pm->rpivid_asb;
649649
power->is_2711 = true;
650650
}
651651

‎include/linux/mfd/bcm2835-pm.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ struct bcm2835_pm {
99
struct device *dev;
1010
void __iomem *base;
1111
void __iomem *asb;
12-
void __iomem *arg_asb;
12+
void __iomem *rpivid_asb;
1313
};
1414

1515
#endif /* BCM2835_MFD_PM_H */

0 commit comments

Comments
 (0)