1 From 13d6f1d6ccff0a381bd1e8149b4964158627d5cc 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] Add /dev/gpiomem device for rootless user GPIO access
6 Signed-off-by: Luke Wren <luke@raspberrypi.org>
8 bcm2835-gpiomem: Fix for ARCH_BCM2835 builds
10 Build on ARCH_BCM2835, and fail to probe if no IO resource.
12 See: https://github.com/raspberrypi/linux/issues/1154
14 drivers/char/broadcom/Kconfig | 8 +
15 drivers/char/broadcom/Makefile | 1 +
16 drivers/char/broadcom/bcm2835-gpiomem.c | 258 ++++++++++++++++++++++++
17 3 files changed, 267 insertions(+)
18 create mode 100644 drivers/char/broadcom/bcm2835-gpiomem.c
20 --- a/drivers/char/broadcom/Kconfig
21 +++ b/drivers/char/broadcom/Kconfig
22 @@ -16,3 +16,11 @@ config BCM2708_VCMEM
23 Helper for videocore memory access and total size allocation.
27 +config BCM2835_DEVGPIOMEM
28 + tristate "/dev/gpiomem rootless GPIO access via mmap() on the BCM2835"
31 + Provides users with root-free access to the GPIO registers
32 + on the 2835. Calling mmap(/dev/gpiomem) will map the GPIO
33 + register page to the user's pointer.
34 --- a/drivers/char/broadcom/Makefile
35 +++ b/drivers/char/broadcom/Makefile
37 obj-$(CONFIG_BCM2708_VCMEM) += vc_mem.o
38 +obj-$(CONFIG_BCM2835_DEVGPIOMEM)+= bcm2835-gpiomem.o
40 +++ b/drivers/char/broadcom/bcm2835-gpiomem.c
43 + * GPIO memory device driver
45 + * Creates a chardev /dev/gpiomem which will provide user access to
46 + * the BCM2835's GPIO registers when it is mmap()'d.
47 + * No longer need root for user GPIO access, but without relaxing permissions
50 + * Written by Luke Wren <luke@raspberrypi.org>
51 + * Copyright (c) 2015, Raspberry Pi (Trading) Ltd.
53 + * Redistribution and use in source and binary forms, with or without
54 + * modification, are permitted provided that the following conditions
56 + * 1. Redistributions of source code must retain the above copyright
57 + * notice, this list of conditions, and the following disclaimer,
58 + * without modification.
59 + * 2. Redistributions in binary form must reproduce the above copyright
60 + * notice, this list of conditions and the following disclaimer in the
61 + * documentation and/or other materials provided with the distribution.
62 + * 3. The names of the above-listed copyright holders may not be used
63 + * to endorse or promote products derived from this software without
64 + * specific prior written permission.
66 + * ALTERNATIVELY, this software may be distributed under the terms of the
67 + * GNU General Public License ("GPL") version 2, as published by the Free
68 + * Software Foundation.
70 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
71 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
72 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
73 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
74 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
75 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
76 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
77 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
78 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
79 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
80 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
83 +#include <linux/kernel.h>
84 +#include <linux/module.h>
85 +#include <linux/of.h>
86 +#include <linux/platform_device.h>
87 +#include <linux/mm.h>
88 +#include <linux/slab.h>
89 +#include <linux/cdev.h>
90 +#include <linux/pagemap.h>
91 +#include <linux/io.h>
93 +#define DEVICE_NAME "bcm2835-gpiomem"
94 +#define DRIVER_NAME "gpiomem-bcm2835"
95 +#define DEVICE_MINOR 0
97 +struct bcm2835_gpiomem_instance {
98 + unsigned long gpio_regs_phys;
102 +static struct cdev bcm2835_gpiomem_cdev;
103 +static dev_t bcm2835_gpiomem_devid;
104 +static struct class *bcm2835_gpiomem_class;
105 +static struct device *bcm2835_gpiomem_dev;
106 +static struct bcm2835_gpiomem_instance *inst;
109 +/****************************************************************************
111 +* GPIO mem chardev file ops
113 +***************************************************************************/
115 +static int bcm2835_gpiomem_open(struct inode *inode, struct file *file)
117 + int dev = iminor(inode);
120 + if (dev != DEVICE_MINOR) {
121 + dev_err(inst->dev, "Unknown minor device: %d", dev);
127 +static int bcm2835_gpiomem_release(struct inode *inode, struct file *file)
129 + int dev = iminor(inode);
132 + if (dev != DEVICE_MINOR) {
133 + dev_err(inst->dev, "Unknown minor device %d", dev);
139 +static const struct vm_operations_struct bcm2835_gpiomem_vm_ops = {
140 +#ifdef CONFIG_HAVE_IOREMAP_PROT
141 + .access = generic_access_phys
145 +static int bcm2835_gpiomem_mmap(struct file *file, struct vm_area_struct *vma)
147 + /* Ignore what the user says - they're getting the GPIO regs
148 + whether they like it or not! */
149 + unsigned long gpio_page = inst->gpio_regs_phys >> PAGE_SHIFT;
151 + vma->vm_page_prot = phys_mem_access_prot(file, gpio_page,
153 + vma->vm_page_prot);
154 + vma->vm_ops = &bcm2835_gpiomem_vm_ops;
155 + if (remap_pfn_range(vma, vma->vm_start,
158 + vma->vm_page_prot)) {
164 +static const struct file_operations
165 +bcm2835_gpiomem_fops = {
166 + .owner = THIS_MODULE,
167 + .open = bcm2835_gpiomem_open,
168 + .release = bcm2835_gpiomem_release,
169 + .mmap = bcm2835_gpiomem_mmap,
173 + /****************************************************************************
175 +* Probe and remove functions
177 +***************************************************************************/
180 +static int bcm2835_gpiomem_probe(struct platform_device *pdev)
184 + struct device *dev = &pdev->dev;
185 + struct resource *ioresource;
187 + /* Allocate buffers and instance data */
189 + inst = kzalloc(sizeof(struct bcm2835_gpiomem_instance), GFP_KERNEL);
193 + goto failed_inst_alloc;
198 + ioresource = platform_get_resource(pdev, IORESOURCE_MEM, 0);
200 + inst->gpio_regs_phys = ioresource->start;
202 + dev_err(inst->dev, "failed to get IO resource");
204 + goto failed_get_resource;
207 + /* Create character device entries */
209 + err = alloc_chrdev_region(&bcm2835_gpiomem_devid,
210 + DEVICE_MINOR, 1, DEVICE_NAME);
212 + dev_err(inst->dev, "unable to allocate device number");
213 + goto failed_alloc_chrdev;
215 + cdev_init(&bcm2835_gpiomem_cdev, &bcm2835_gpiomem_fops);
216 + bcm2835_gpiomem_cdev.owner = THIS_MODULE;
217 + err = cdev_add(&bcm2835_gpiomem_cdev, bcm2835_gpiomem_devid, 1);
219 + dev_err(inst->dev, "unable to register device");
220 + goto failed_cdev_add;
223 + /* Create sysfs entries */
225 + bcm2835_gpiomem_class = class_create(THIS_MODULE, DEVICE_NAME);
226 + ptr_err = bcm2835_gpiomem_class;
227 + if (IS_ERR(ptr_err))
228 + goto failed_class_create;
230 + bcm2835_gpiomem_dev = device_create(bcm2835_gpiomem_class, NULL,
231 + bcm2835_gpiomem_devid, NULL,
233 + ptr_err = bcm2835_gpiomem_dev;
234 + if (IS_ERR(ptr_err))
235 + goto failed_device_create;
237 + dev_info(inst->dev, "Initialised: Registers at 0x%08lx",
238 + inst->gpio_regs_phys);
242 +failed_device_create:
243 + class_destroy(bcm2835_gpiomem_class);
244 +failed_class_create:
245 + cdev_del(&bcm2835_gpiomem_cdev);
246 + err = PTR_ERR(ptr_err);
248 + unregister_chrdev_region(bcm2835_gpiomem_devid, 1);
249 +failed_alloc_chrdev:
250 +failed_get_resource:
253 + dev_err(inst->dev, "could not load bcm2835_gpiomem");
257 +static int bcm2835_gpiomem_remove(struct platform_device *pdev)
259 + struct device *dev = inst->dev;
262 + device_destroy(bcm2835_gpiomem_class, bcm2835_gpiomem_devid);
263 + class_destroy(bcm2835_gpiomem_class);
264 + cdev_del(&bcm2835_gpiomem_cdev);
265 + unregister_chrdev_region(bcm2835_gpiomem_devid, 1);
267 + dev_info(dev, "GPIO mem driver removed - OK");
271 + /****************************************************************************
273 +* Register the driver with device tree
275 +***************************************************************************/
277 +static const struct of_device_id bcm2835_gpiomem_of_match[] = {
278 + {.compatible = "brcm,bcm2835-gpiomem",},
279 + { /* sentinel */ },
282 +MODULE_DEVICE_TABLE(of, bcm2835_gpiomem_of_match);
284 +static struct platform_driver bcm2835_gpiomem_driver = {
285 + .probe = bcm2835_gpiomem_probe,
286 + .remove = bcm2835_gpiomem_remove,
288 + .name = DRIVER_NAME,
289 + .owner = THIS_MODULE,
290 + .of_match_table = bcm2835_gpiomem_of_match,
294 +module_platform_driver(bcm2835_gpiomem_driver);
296 +MODULE_ALIAS("platform:gpiomem-bcm2835");
297 +MODULE_LICENSE("GPL");
298 +MODULE_DESCRIPTION("gpiomem driver for accessing GPIO from userspace");
299 +MODULE_AUTHOR("Luke Wren <luke@raspberrypi.org>");