1 From 818357c73f5869dfc9e98224f91e116fd23d5246 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 044/454] 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 | 9 +
15 drivers/char/broadcom/Makefile | 3 +
16 drivers/char/broadcom/bcm2835-gpiomem.c | 258 ++++++++++++++++++++++++
17 3 files changed, 270 insertions(+)
18 create mode 100644 drivers/char/broadcom/bcm2835-gpiomem.c
20 --- a/drivers/char/broadcom/Kconfig
21 +++ b/drivers/char/broadcom/Kconfig
22 @@ -26,3 +26,12 @@ config BCM_VC_SM
24 Support for the VC shared memory on the Broadcom reference
25 design. Uses the VCHIQ stack.
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.
35 --- a/drivers/char/broadcom/Makefile
36 +++ b/drivers/char/broadcom/Makefile
38 obj-$(CONFIG_BCM2708_VCMEM) += vc_mem.o
39 obj-$(CONFIG_BCM_VC_SM) += vc_sm/
41 +obj-$(CONFIG_BCM2835_DEVGPIOMEM)+= bcm2835-gpiomem.o
44 +++ b/drivers/char/broadcom/bcm2835-gpiomem.c
47 + * GPIO memory device driver
49 + * Creates a chardev /dev/gpiomem which will provide user access to
50 + * the BCM2835's GPIO registers when it is mmap()'d.
51 + * No longer need root for user GPIO access, but without relaxing permissions
54 + * Written by Luke Wren <luke@raspberrypi.org>
55 + * Copyright (c) 2015, Raspberry Pi (Trading) Ltd.
57 + * Redistribution and use in source and binary forms, with or without
58 + * modification, are permitted provided that the following conditions
60 + * 1. Redistributions of source code must retain the above copyright
61 + * notice, this list of conditions, and the following disclaimer,
62 + * without modification.
63 + * 2. Redistributions in binary form must reproduce the above copyright
64 + * notice, this list of conditions and the following disclaimer in the
65 + * documentation and/or other materials provided with the distribution.
66 + * 3. The names of the above-listed copyright holders may not be used
67 + * to endorse or promote products derived from this software without
68 + * specific prior written permission.
70 + * ALTERNATIVELY, this software may be distributed under the terms of the
71 + * GNU General Public License ("GPL") version 2, as published by the Free
72 + * Software Foundation.
74 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
75 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
76 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
77 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
78 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
79 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
80 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
81 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
82 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
83 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
84 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
87 +#include <linux/kernel.h>
88 +#include <linux/module.h>
89 +#include <linux/of.h>
90 +#include <linux/platform_device.h>
91 +#include <linux/mm.h>
92 +#include <linux/slab.h>
93 +#include <linux/cdev.h>
94 +#include <linux/pagemap.h>
95 +#include <linux/io.h>
97 +#define DEVICE_NAME "bcm2835-gpiomem"
98 +#define DRIVER_NAME "gpiomem-bcm2835"
99 +#define DEVICE_MINOR 0
101 +struct bcm2835_gpiomem_instance {
102 + unsigned long gpio_regs_phys;
103 + struct device *dev;
106 +static struct cdev bcm2835_gpiomem_cdev;
107 +static dev_t bcm2835_gpiomem_devid;
108 +static struct class *bcm2835_gpiomem_class;
109 +static struct device *bcm2835_gpiomem_dev;
110 +static struct bcm2835_gpiomem_instance *inst;
113 +/****************************************************************************
115 +* GPIO mem chardev file ops
117 +***************************************************************************/
119 +static int bcm2835_gpiomem_open(struct inode *inode, struct file *file)
121 + int dev = iminor(inode);
124 + if (dev != DEVICE_MINOR) {
125 + dev_err(inst->dev, "Unknown minor device: %d", dev);
131 +static int bcm2835_gpiomem_release(struct inode *inode, struct file *file)
133 + int dev = iminor(inode);
136 + if (dev != DEVICE_MINOR) {
137 + dev_err(inst->dev, "Unknown minor device %d", dev);
143 +static const struct vm_operations_struct bcm2835_gpiomem_vm_ops = {
144 +#ifdef CONFIG_HAVE_IOREMAP_PROT
145 + .access = generic_access_phys
149 +static int bcm2835_gpiomem_mmap(struct file *file, struct vm_area_struct *vma)
151 + /* Ignore what the user says - they're getting the GPIO regs
152 + whether they like it or not! */
153 + unsigned long gpio_page = inst->gpio_regs_phys >> PAGE_SHIFT;
155 + vma->vm_page_prot = phys_mem_access_prot(file, gpio_page,
157 + vma->vm_page_prot);
158 + vma->vm_ops = &bcm2835_gpiomem_vm_ops;
159 + if (remap_pfn_range(vma, vma->vm_start,
162 + vma->vm_page_prot)) {
168 +static const struct file_operations
169 +bcm2835_gpiomem_fops = {
170 + .owner = THIS_MODULE,
171 + .open = bcm2835_gpiomem_open,
172 + .release = bcm2835_gpiomem_release,
173 + .mmap = bcm2835_gpiomem_mmap,
177 + /****************************************************************************
179 +* Probe and remove functions
181 +***************************************************************************/
184 +static int bcm2835_gpiomem_probe(struct platform_device *pdev)
188 + struct device *dev = &pdev->dev;
189 + struct resource *ioresource;
191 + /* Allocate buffers and instance data */
193 + inst = kzalloc(sizeof(struct bcm2835_gpiomem_instance), GFP_KERNEL);
197 + goto failed_inst_alloc;
202 + ioresource = platform_get_resource(pdev, IORESOURCE_MEM, 0);
204 + inst->gpio_regs_phys = ioresource->start;
206 + dev_err(inst->dev, "failed to get IO resource");
208 + goto failed_get_resource;
211 + /* Create character device entries */
213 + err = alloc_chrdev_region(&bcm2835_gpiomem_devid,
214 + DEVICE_MINOR, 1, DEVICE_NAME);
216 + dev_err(inst->dev, "unable to allocate device number");
217 + goto failed_alloc_chrdev;
219 + cdev_init(&bcm2835_gpiomem_cdev, &bcm2835_gpiomem_fops);
220 + bcm2835_gpiomem_cdev.owner = THIS_MODULE;
221 + err = cdev_add(&bcm2835_gpiomem_cdev, bcm2835_gpiomem_devid, 1);
223 + dev_err(inst->dev, "unable to register device");
224 + goto failed_cdev_add;
227 + /* Create sysfs entries */
229 + bcm2835_gpiomem_class = class_create(THIS_MODULE, DEVICE_NAME);
230 + ptr_err = bcm2835_gpiomem_class;
231 + if (IS_ERR(ptr_err))
232 + goto failed_class_create;
234 + bcm2835_gpiomem_dev = device_create(bcm2835_gpiomem_class, NULL,
235 + bcm2835_gpiomem_devid, NULL,
237 + ptr_err = bcm2835_gpiomem_dev;
238 + if (IS_ERR(ptr_err))
239 + goto failed_device_create;
241 + dev_info(inst->dev, "Initialised: Registers at 0x%08lx",
242 + inst->gpio_regs_phys);
246 +failed_device_create:
247 + class_destroy(bcm2835_gpiomem_class);
248 +failed_class_create:
249 + cdev_del(&bcm2835_gpiomem_cdev);
250 + err = PTR_ERR(ptr_err);
252 + unregister_chrdev_region(bcm2835_gpiomem_devid, 1);
253 +failed_alloc_chrdev:
254 +failed_get_resource:
257 + dev_err(inst->dev, "could not load bcm2835_gpiomem");
261 +static int bcm2835_gpiomem_remove(struct platform_device *pdev)
263 + struct device *dev = inst->dev;
266 + device_destroy(bcm2835_gpiomem_class, bcm2835_gpiomem_devid);
267 + class_destroy(bcm2835_gpiomem_class);
268 + cdev_del(&bcm2835_gpiomem_cdev);
269 + unregister_chrdev_region(bcm2835_gpiomem_devid, 1);
271 + dev_info(dev, "GPIO mem driver removed - OK");
275 + /****************************************************************************
277 +* Register the driver with device tree
279 +***************************************************************************/
281 +static const struct of_device_id bcm2835_gpiomem_of_match[] = {
282 + {.compatible = "brcm,bcm2835-gpiomem",},
283 + { /* sentinel */ },
286 +MODULE_DEVICE_TABLE(of, bcm2835_gpiomem_of_match);
288 +static struct platform_driver bcm2835_gpiomem_driver = {
289 + .probe = bcm2835_gpiomem_probe,
290 + .remove = bcm2835_gpiomem_remove,
292 + .name = DRIVER_NAME,
293 + .owner = THIS_MODULE,
294 + .of_match_table = bcm2835_gpiomem_of_match,
298 +module_platform_driver(bcm2835_gpiomem_driver);
300 +MODULE_ALIAS("platform:gpiomem-bcm2835");
301 +MODULE_LICENSE("GPL");
302 +MODULE_DESCRIPTION("gpiomem driver for accessing GPIO from userspace");
303 +MODULE_AUTHOR("Luke Wren <luke@raspberrypi.org>");