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