1 From a7bebe53ea70778389d162a6a25aa5707f083747 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 0096/1085] Add /dev/gpiomem device for rootless user GPIO
7 Signed-off-by: Luke Wren <luke@raspberrypi.org>
9 bcm2835-gpiomem: Fix for ARCH_BCM2835 builds
11 Build on ARCH_BCM2835, and fail to probe if no IO resource.
13 See: https://github.com/raspberrypi/linux/issues/1154
15 drivers/char/broadcom/Kconfig | 8 +
16 drivers/char/broadcom/Makefile | 1 +
17 drivers/char/broadcom/bcm2835-gpiomem.c | 258 ++++++++++++++++++++++++
18 3 files changed, 267 insertions(+)
19 create mode 100644 drivers/char/broadcom/bcm2835-gpiomem.c
21 --- a/drivers/char/broadcom/Kconfig
22 +++ b/drivers/char/broadcom/Kconfig
23 @@ -16,3 +16,11 @@ config BCM2708_VCMEM
24 Helper for videocore memory access and total size allocation.
28 +config BCM2835_DEVGPIOMEM
29 + tristate "/dev/gpiomem rootless GPIO access via mmap() on the BCM2835"
32 + Provides users with root-free access to the GPIO registers
33 + on the 2835. Calling mmap(/dev/gpiomem) will map the GPIO
34 + 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_BCM2835_DEVGPIOMEM)+= bcm2835-gpiomem.o
41 +++ b/drivers/char/broadcom/bcm2835-gpiomem.c
44 + * GPIO memory device driver
46 + * Creates a chardev /dev/gpiomem which will provide user access to
47 + * the BCM2835's GPIO registers when it is mmap()'d.
48 + * No longer need root for user GPIO access, but without relaxing permissions
51 + * Written by Luke Wren <luke@raspberrypi.org>
52 + * Copyright (c) 2015, Raspberry Pi (Trading) Ltd.
54 + * Redistribution and use in source and binary forms, with or without
55 + * modification, are permitted provided that the following conditions
57 + * 1. Redistributions of source code must retain the above copyright
58 + * notice, this list of conditions, and the following disclaimer,
59 + * without modification.
60 + * 2. Redistributions in binary form must reproduce the above copyright
61 + * notice, this list of conditions and the following disclaimer in the
62 + * documentation and/or other materials provided with the distribution.
63 + * 3. The names of the above-listed copyright holders may not be used
64 + * to endorse or promote products derived from this software without
65 + * specific prior written permission.
67 + * ALTERNATIVELY, this software may be distributed under the terms of the
68 + * GNU General Public License ("GPL") version 2, as published by the Free
69 + * Software Foundation.
71 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
72 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
73 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
74 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
75 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
76 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
77 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
78 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
79 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
80 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
81 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
84 +#include <linux/kernel.h>
85 +#include <linux/module.h>
86 +#include <linux/of.h>
87 +#include <linux/platform_device.h>
88 +#include <linux/mm.h>
89 +#include <linux/slab.h>
90 +#include <linux/cdev.h>
91 +#include <linux/pagemap.h>
92 +#include <linux/io.h>
94 +#define DEVICE_NAME "bcm2835-gpiomem"
95 +#define DRIVER_NAME "gpiomem-bcm2835"
96 +#define DEVICE_MINOR 0
98 +struct bcm2835_gpiomem_instance {
99 + unsigned long gpio_regs_phys;
100 + struct device *dev;
103 +static struct cdev bcm2835_gpiomem_cdev;
104 +static dev_t bcm2835_gpiomem_devid;
105 +static struct class *bcm2835_gpiomem_class;
106 +static struct device *bcm2835_gpiomem_dev;
107 +static struct bcm2835_gpiomem_instance *inst;
110 +/****************************************************************************
112 +* GPIO mem chardev file ops
114 +***************************************************************************/
116 +static int bcm2835_gpiomem_open(struct inode *inode, struct file *file)
118 + int dev = iminor(inode);
121 + if (dev != DEVICE_MINOR) {
122 + dev_err(inst->dev, "Unknown minor device: %d", dev);
128 +static int bcm2835_gpiomem_release(struct inode *inode, struct file *file)
130 + int dev = iminor(inode);
133 + if (dev != DEVICE_MINOR) {
134 + dev_err(inst->dev, "Unknown minor device %d", dev);
140 +static const struct vm_operations_struct bcm2835_gpiomem_vm_ops = {
141 +#ifdef CONFIG_HAVE_IOREMAP_PROT
142 + .access = generic_access_phys
146 +static int bcm2835_gpiomem_mmap(struct file *file, struct vm_area_struct *vma)
148 + /* Ignore what the user says - they're getting the GPIO regs
149 + whether they like it or not! */
150 + unsigned long gpio_page = inst->gpio_regs_phys >> PAGE_SHIFT;
152 + vma->vm_page_prot = phys_mem_access_prot(file, gpio_page,
154 + vma->vm_page_prot);
155 + vma->vm_ops = &bcm2835_gpiomem_vm_ops;
156 + if (remap_pfn_range(vma, vma->vm_start,
159 + vma->vm_page_prot)) {
165 +static const struct file_operations
166 +bcm2835_gpiomem_fops = {
167 + .owner = THIS_MODULE,
168 + .open = bcm2835_gpiomem_open,
169 + .release = bcm2835_gpiomem_release,
170 + .mmap = bcm2835_gpiomem_mmap,
174 + /****************************************************************************
176 +* Probe and remove functions
178 +***************************************************************************/
181 +static int bcm2835_gpiomem_probe(struct platform_device *pdev)
185 + struct device *dev = &pdev->dev;
186 + struct resource *ioresource;
188 + /* Allocate buffers and instance data */
190 + inst = kzalloc(sizeof(struct bcm2835_gpiomem_instance), GFP_KERNEL);
194 + goto failed_inst_alloc;
199 + ioresource = platform_get_resource(pdev, IORESOURCE_MEM, 0);
201 + inst->gpio_regs_phys = ioresource->start;
203 + dev_err(inst->dev, "failed to get IO resource");
205 + goto failed_get_resource;
208 + /* Create character device entries */
210 + err = alloc_chrdev_region(&bcm2835_gpiomem_devid,
211 + DEVICE_MINOR, 1, DEVICE_NAME);
213 + dev_err(inst->dev, "unable to allocate device number");
214 + goto failed_alloc_chrdev;
216 + cdev_init(&bcm2835_gpiomem_cdev, &bcm2835_gpiomem_fops);
217 + bcm2835_gpiomem_cdev.owner = THIS_MODULE;
218 + err = cdev_add(&bcm2835_gpiomem_cdev, bcm2835_gpiomem_devid, 1);
220 + dev_err(inst->dev, "unable to register device");
221 + goto failed_cdev_add;
224 + /* Create sysfs entries */
226 + bcm2835_gpiomem_class = class_create(DEVICE_NAME);
227 + ptr_err = bcm2835_gpiomem_class;
228 + if (IS_ERR(ptr_err))
229 + goto failed_class_create;
231 + bcm2835_gpiomem_dev = device_create(bcm2835_gpiomem_class, NULL,
232 + bcm2835_gpiomem_devid, NULL,
234 + ptr_err = bcm2835_gpiomem_dev;
235 + if (IS_ERR(ptr_err))
236 + goto failed_device_create;
238 + dev_info(inst->dev, "Initialised: Registers at 0x%08lx",
239 + inst->gpio_regs_phys);
243 +failed_device_create:
244 + class_destroy(bcm2835_gpiomem_class);
245 +failed_class_create:
246 + cdev_del(&bcm2835_gpiomem_cdev);
247 + err = PTR_ERR(ptr_err);
249 + unregister_chrdev_region(bcm2835_gpiomem_devid, 1);
250 +failed_alloc_chrdev:
251 +failed_get_resource:
254 + dev_err(inst->dev, "could not load bcm2835_gpiomem");
258 +static int bcm2835_gpiomem_remove(struct platform_device *pdev)
260 + struct device *dev = inst->dev;
263 + device_destroy(bcm2835_gpiomem_class, bcm2835_gpiomem_devid);
264 + class_destroy(bcm2835_gpiomem_class);
265 + cdev_del(&bcm2835_gpiomem_cdev);
266 + unregister_chrdev_region(bcm2835_gpiomem_devid, 1);
268 + dev_info(dev, "GPIO mem driver removed - OK");
272 + /****************************************************************************
274 +* Register the driver with device tree
276 +***************************************************************************/
278 +static const struct of_device_id bcm2835_gpiomem_of_match[] = {
279 + {.compatible = "brcm,bcm2835-gpiomem",},
280 + { /* sentinel */ },
283 +MODULE_DEVICE_TABLE(of, bcm2835_gpiomem_of_match);
285 +static struct platform_driver bcm2835_gpiomem_driver = {
286 + .probe = bcm2835_gpiomem_probe,
287 + .remove = bcm2835_gpiomem_remove,
289 + .name = DRIVER_NAME,
290 + .owner = THIS_MODULE,
291 + .of_match_table = bcm2835_gpiomem_of_match,
295 +module_platform_driver(bcm2835_gpiomem_driver);
297 +MODULE_ALIAS("platform:gpiomem-bcm2835");
298 +MODULE_LICENSE("GPL");
299 +MODULE_DESCRIPTION("gpiomem driver for accessing GPIO from userspace");
300 +MODULE_AUTHOR("Luke Wren <luke@raspberrypi.org>");