1 From 99746de63f51250ee6df9e2d24ac1e587061ca80 Mon Sep 17 00:00:00 2001
2 From: Luke Wren <luke@raspberrypi.org>
3 Date: Fri, 21 Aug 2015 23:14:48 +0100
4 Subject: [PATCH 166/222] Add /dev/gpiomem device for rootless user GPIO access
6 Signed-off-by: Luke Wren <luke@raspberrypi.org>
8 arch/arm/boot/dts/bcm2708.dtsi | 6 +
9 arch/arm/boot/dts/bcm2709.dtsi | 6 +
10 drivers/char/broadcom/Kconfig | 9 ++
11 drivers/char/broadcom/Makefile | 3 +
12 drivers/char/broadcom/bcm2835-gpiomem.c | 265 ++++++++++++++++++++++++++++++++
13 5 files changed, 289 insertions(+)
14 create mode 100644 drivers/char/broadcom/bcm2835-gpiomem.c
16 --- a/arch/arm/boot/dts/bcm2708.dtsi
17 +++ b/arch/arm/boot/dts/bcm2708.dtsi
20 compatible = "arm,arm1176-pmu";
24 + compatible = "brcm,bcm2835-gpiomem";
25 + reg = <0x7e200000 0x1000>;
30 --- a/arch/arm/boot/dts/bcm2709.dtsi
31 +++ b/arch/arm/boot/dts/bcm2709.dtsi
33 compatible = "arm,cortex-a7-pmu";
38 + compatible = "brcm,bcm2835-gpiomem";
39 + reg = <0x7e200000 0x1000>;
45 --- a/drivers/char/broadcom/Kconfig
46 +++ b/drivers/char/broadcom/Kconfig
47 @@ -38,3 +38,12 @@ config BCM_VC_SM
49 Support for the VC shared memory on the Broadcom reference
50 design. Uses the VCHIQ stack.
52 +config BCM2835_DEVGPIOMEM
53 + tristate "/dev/gpiomem rootless GPIO access via mmap() on the BCM2835"
56 + Provides users with root-free access to the GPIO registers
57 + on the 2835. Calling mmap(/dev/gpiomem) will map the GPIO
58 + register page to the user's pointer.
60 --- a/drivers/char/broadcom/Makefile
61 +++ b/drivers/char/broadcom/Makefile
62 @@ -2,3 +2,6 @@ obj-$(CONFIG_BCM_VC_CMA) += vc_cma/
63 obj-$(CONFIG_BCM2708_VCMEM) += vc_mem.o
64 obj-$(CONFIG_BCM_VCIO) += vcio.o
65 obj-$(CONFIG_BCM_VC_SM) += vc_sm/
67 +obj-$(CONFIG_BCM2835_DEVGPIOMEM)+= bcm2835-gpiomem.o
70 +++ b/drivers/char/broadcom/bcm2835-gpiomem.c
73 + * GPIO memory device driver
75 + * Creates a chardev /dev/gpiomem which will provide user access to
76 + * the BCM2835's GPIO registers when it is mmap()'d.
77 + * No longer need root for user GPIO access, but without relaxing permissions
80 + * Written by Luke Wren <luke@raspberrypi.org>
81 + * Copyright (c) 2015, Raspberry Pi (Trading) Ltd.
83 + * Redistribution and use in source and binary forms, with or without
84 + * modification, are permitted provided that the following conditions
86 + * 1. Redistributions of source code must retain the above copyright
87 + * notice, this list of conditions, and the following disclaimer,
88 + * without modification.
89 + * 2. Redistributions in binary form must reproduce the above copyright
90 + * notice, this list of conditions and the following disclaimer in the
91 + * documentation and/or other materials provided with the distribution.
92 + * 3. The names of the above-listed copyright holders may not be used
93 + * to endorse or promote products derived from this software without
94 + * specific prior written permission.
96 + * ALTERNATIVELY, this software may be distributed under the terms of the
97 + * GNU General Public License ("GPL") version 2, as published by the Free
98 + * Software Foundation.
100 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
101 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
102 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
103 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
104 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
105 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
106 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
107 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
108 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
109 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
110 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
113 +#include <linux/kernel.h>
114 +#include <linux/module.h>
115 +#include <linux/of.h>
116 +#include <linux/platform_device.h>
117 +#include <linux/mm.h>
118 +#include <linux/slab.h>
119 +#include <linux/cdev.h>
120 +#include <linux/pagemap.h>
121 +#include <linux/io.h>
123 +#define DEVICE_NAME "bcm2835-gpiomem"
124 +#define DRIVER_NAME "gpiomem-bcm2835"
125 +#define DEVICE_MINOR 0
127 +struct bcm2835_gpiomem_instance {
128 + unsigned long gpio_regs_phys;
129 + struct device *dev;
132 +static struct cdev bcm2835_gpiomem_cdev;
133 +static dev_t bcm2835_gpiomem_devid;
134 +static struct class *bcm2835_gpiomem_class;
135 +static struct device *bcm2835_gpiomem_dev;
136 +static struct bcm2835_gpiomem_instance *inst;
139 +/****************************************************************************
141 +* GPIO mem chardev file ops
143 +***************************************************************************/
145 +static int bcm2835_gpiomem_open(struct inode *inode, struct file *file)
147 + int dev = iminor(inode);
150 + dev_info(inst->dev, "gpiomem device opened.");
152 + if (dev != DEVICE_MINOR) {
153 + dev_err(inst->dev, "Unknown minor device: %d", dev);
159 +static int bcm2835_gpiomem_release(struct inode *inode, struct file *file)
161 + int dev = iminor(inode);
164 + if (dev != DEVICE_MINOR) {
165 + dev_err(inst->dev, "Unknown minor device %d", dev);
171 +static const struct vm_operations_struct bcm2835_gpiomem_vm_ops = {
172 +#ifdef CONFIG_HAVE_IOREMAP_PROT
173 + .access = generic_access_phys
177 +static int bcm2835_gpiomem_mmap(struct file *file, struct vm_area_struct *vma)
179 + /* Ignore what the user says - they're getting the GPIO regs
180 + whether they like it or not! */
181 + unsigned long gpio_page = inst->gpio_regs_phys >> PAGE_SHIFT;
183 + vma->vm_page_prot = phys_mem_access_prot(file, gpio_page,
185 + vma->vm_page_prot);
186 + vma->vm_ops = &bcm2835_gpiomem_vm_ops;
187 + if (remap_pfn_range(vma, vma->vm_start,
190 + vma->vm_page_prot)) {
196 +static const struct file_operations
197 +bcm2835_gpiomem_fops = {
198 + .owner = THIS_MODULE,
199 + .open = bcm2835_gpiomem_open,
200 + .release = bcm2835_gpiomem_release,
201 + .mmap = bcm2835_gpiomem_mmap,
205 + /****************************************************************************
207 +* Probe and remove functions
209 +***************************************************************************/
212 +static int bcm2835_gpiomem_probe(struct platform_device *pdev)
216 + struct device *dev = &pdev->dev;
217 + struct device_node *node = dev->of_node;
218 + struct resource *ioresource;
220 + /* Allocate buffers and instance data */
222 + inst = kzalloc(sizeof(struct bcm2835_gpiomem_instance), GFP_KERNEL);
226 + goto failed_inst_alloc;
231 + /* Create character device entries */
233 + err = alloc_chrdev_region(&bcm2835_gpiomem_devid,
234 + DEVICE_MINOR, 1, DEVICE_NAME);
236 + dev_err(inst->dev, "unable to allocate device number");
237 + goto failed_alloc_chrdev;
239 + cdev_init(&bcm2835_gpiomem_cdev, &bcm2835_gpiomem_fops);
240 + bcm2835_gpiomem_cdev.owner = THIS_MODULE;
241 + err = cdev_add(&bcm2835_gpiomem_cdev, bcm2835_gpiomem_devid, 1);
243 + dev_err(inst->dev, "unable to register device");
244 + goto failed_cdev_add;
247 + /* Create sysfs entries */
249 + bcm2835_gpiomem_class = class_create(THIS_MODULE, DEVICE_NAME);
250 + ptr_err = bcm2835_gpiomem_class;
251 + if (IS_ERR(ptr_err))
252 + goto failed_class_create;
254 + bcm2835_gpiomem_dev = device_create(bcm2835_gpiomem_class, NULL,
255 + bcm2835_gpiomem_devid, NULL,
257 + ptr_err = bcm2835_gpiomem_dev;
258 + if (IS_ERR(ptr_err))
259 + goto failed_device_create;
261 + /* Get address from device tree if available (*_resource() correctly
262 + converts the bus address in device tree to a physical address),
263 + or use hardcoded offset + BCM2708_PERI_BASE if not.
264 + (In spite of its name 2708 actually seems to have the correct
265 + mach-dependent value on 2709 etc, as it is defined in
266 + mach-bcm270x/platform.h) */
269 + ioresource = platform_get_resource(pdev, IORESOURCE_MEM, 0);
270 + inst->gpio_regs_phys = ioresource->start;
272 + inst->gpio_regs_phys = GPIO_BASE;
275 + dev_info(inst->dev, "Initialised: Registers at 0x%08lx",
276 + inst->gpio_regs_phys);
280 +failed_device_create:
281 + class_destroy(bcm2835_gpiomem_class);
282 +failed_class_create:
283 + cdev_del(&bcm2835_gpiomem_cdev);
284 + err = PTR_ERR(ptr_err);
286 + unregister_chrdev_region(bcm2835_gpiomem_devid, 1);
287 +failed_alloc_chrdev:
290 + dev_err(inst->dev, "could not load bcm2835_gpiomem");
294 +static int bcm2835_gpiomem_remove(struct platform_device *pdev)
296 + struct device *dev = inst->dev;
299 + device_destroy(bcm2835_gpiomem_class, bcm2835_gpiomem_devid);
300 + class_destroy(bcm2835_gpiomem_class);
301 + cdev_del(&bcm2835_gpiomem_cdev);
302 + unregister_chrdev_region(bcm2835_gpiomem_devid, 1);
304 + dev_info(dev, "GPIO mem driver removed - OK");
308 + /****************************************************************************
310 +* Register the driver with device tree
312 +***************************************************************************/
314 +static const struct of_device_id bcm2835_gpiomem_of_match[] = {
315 + {.compatible = "brcm,bcm2835-gpiomem",},
316 + { /* sentinel */ },
319 +MODULE_DEVICE_TABLE(of, bcm2835_gpiomem_of_match);
321 +static struct platform_driver bcm2835_gpiomem_driver = {
322 + .probe = bcm2835_gpiomem_probe,
323 + .remove = bcm2835_gpiomem_remove,
325 + .name = DRIVER_NAME,
326 + .owner = THIS_MODULE,
327 + .of_match_table = bcm2835_gpiomem_of_match,
331 +module_platform_driver(bcm2835_gpiomem_driver);
333 +MODULE_ALIAS("platform:gpiomem-bcm2835");
334 +MODULE_LICENSE("GPL");
335 +MODULE_DESCRIPTION("gpiomem driver for accessing GPIO from userspace");
336 +MODULE_AUTHOR("Luke Wren <luke@raspberrypi.org>");