]> git.ipfire.org Git - thirdparty/openwrt.git/blob
e5a44a25dba6860af3946d855f036fa51691f6a4
[thirdparty/openwrt.git] /
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
5 access
6
7 Signed-off-by: Luke Wren <luke@raspberrypi.org>
8
9 bcm2835-gpiomem: Fix for ARCH_BCM2835 builds
10
11 Build on ARCH_BCM2835, and fail to probe if no IO resource.
12
13 See: https://github.com/raspberrypi/linux/issues/1154
14 ---
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
20
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.
25
26 endif
27 +
28 +config BCM2835_DEVGPIOMEM
29 + tristate "/dev/gpiomem rootless GPIO access via mmap() on the BCM2835"
30 + default m
31 + help
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
37 @@ -1 +1,2 @@
38 obj-$(CONFIG_BCM2708_VCMEM) += vc_mem.o
39 +obj-$(CONFIG_BCM2835_DEVGPIOMEM)+= bcm2835-gpiomem.o
40 --- /dev/null
41 +++ b/drivers/char/broadcom/bcm2835-gpiomem.c
42 @@ -0,0 +1,258 @@
43 +/**
44 + * GPIO memory device driver
45 + *
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
49 + * on /dev/mem.
50 + *
51 + * Written by Luke Wren <luke@raspberrypi.org>
52 + * Copyright (c) 2015, Raspberry Pi (Trading) Ltd.
53 + *
54 + * Redistribution and use in source and binary forms, with or without
55 + * modification, are permitted provided that the following conditions
56 + * are met:
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.
66 + *
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.
70 + *
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.
82 + */
83 +
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>
93 +
94 +#define DEVICE_NAME "bcm2835-gpiomem"
95 +#define DRIVER_NAME "gpiomem-bcm2835"
96 +#define DEVICE_MINOR 0
97 +
98 +struct bcm2835_gpiomem_instance {
99 + unsigned long gpio_regs_phys;
100 + struct device *dev;
101 +};
102 +
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;
108 +
109 +
110 +/****************************************************************************
111 +*
112 +* GPIO mem chardev file ops
113 +*
114 +***************************************************************************/
115 +
116 +static int bcm2835_gpiomem_open(struct inode *inode, struct file *file)
117 +{
118 + int dev = iminor(inode);
119 + int ret = 0;
120 +
121 + if (dev != DEVICE_MINOR) {
122 + dev_err(inst->dev, "Unknown minor device: %d", dev);
123 + ret = -ENXIO;
124 + }
125 + return ret;
126 +}
127 +
128 +static int bcm2835_gpiomem_release(struct inode *inode, struct file *file)
129 +{
130 + int dev = iminor(inode);
131 + int ret = 0;
132 +
133 + if (dev != DEVICE_MINOR) {
134 + dev_err(inst->dev, "Unknown minor device %d", dev);
135 + ret = -ENXIO;
136 + }
137 + return ret;
138 +}
139 +
140 +static const struct vm_operations_struct bcm2835_gpiomem_vm_ops = {
141 +#ifdef CONFIG_HAVE_IOREMAP_PROT
142 + .access = generic_access_phys
143 +#endif
144 +};
145 +
146 +static int bcm2835_gpiomem_mmap(struct file *file, struct vm_area_struct *vma)
147 +{
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;
151 +
152 + vma->vm_page_prot = phys_mem_access_prot(file, gpio_page,
153 + PAGE_SIZE,
154 + vma->vm_page_prot);
155 + vma->vm_ops = &bcm2835_gpiomem_vm_ops;
156 + if (remap_pfn_range(vma, vma->vm_start,
157 + gpio_page,
158 + PAGE_SIZE,
159 + vma->vm_page_prot)) {
160 + return -EAGAIN;
161 + }
162 + return 0;
163 +}
164 +
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,
171 +};
172 +
173 +
174 + /****************************************************************************
175 +*
176 +* Probe and remove functions
177 +*
178 +***************************************************************************/
179 +
180 +
181 +static int bcm2835_gpiomem_probe(struct platform_device *pdev)
182 +{
183 + int err;
184 + void *ptr_err;
185 + struct device *dev = &pdev->dev;
186 + struct resource *ioresource;
187 +
188 + /* Allocate buffers and instance data */
189 +
190 + inst = kzalloc(sizeof(struct bcm2835_gpiomem_instance), GFP_KERNEL);
191 +
192 + if (!inst) {
193 + err = -ENOMEM;
194 + goto failed_inst_alloc;
195 + }
196 +
197 + inst->dev = dev;
198 +
199 + ioresource = platform_get_resource(pdev, IORESOURCE_MEM, 0);
200 + if (ioresource) {
201 + inst->gpio_regs_phys = ioresource->start;
202 + } else {
203 + dev_err(inst->dev, "failed to get IO resource");
204 + err = -ENOENT;
205 + goto failed_get_resource;
206 + }
207 +
208 + /* Create character device entries */
209 +
210 + err = alloc_chrdev_region(&bcm2835_gpiomem_devid,
211 + DEVICE_MINOR, 1, DEVICE_NAME);
212 + if (err != 0) {
213 + dev_err(inst->dev, "unable to allocate device number");
214 + goto failed_alloc_chrdev;
215 + }
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);
219 + if (err != 0) {
220 + dev_err(inst->dev, "unable to register device");
221 + goto failed_cdev_add;
222 + }
223 +
224 + /* Create sysfs entries */
225 +
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;
230 +
231 + bcm2835_gpiomem_dev = device_create(bcm2835_gpiomem_class, NULL,
232 + bcm2835_gpiomem_devid, NULL,
233 + "gpiomem");
234 + ptr_err = bcm2835_gpiomem_dev;
235 + if (IS_ERR(ptr_err))
236 + goto failed_device_create;
237 +
238 + dev_info(inst->dev, "Initialised: Registers at 0x%08lx",
239 + inst->gpio_regs_phys);
240 +
241 + return 0;
242 +
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);
248 +failed_cdev_add:
249 + unregister_chrdev_region(bcm2835_gpiomem_devid, 1);
250 +failed_alloc_chrdev:
251 +failed_get_resource:
252 + kfree(inst);
253 +failed_inst_alloc:
254 + dev_err(inst->dev, "could not load bcm2835_gpiomem");
255 + return err;
256 +}
257 +
258 +static int bcm2835_gpiomem_remove(struct platform_device *pdev)
259 +{
260 + struct device *dev = inst->dev;
261 +
262 + kfree(inst);
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);
267 +
268 + dev_info(dev, "GPIO mem driver removed - OK");
269 + return 0;
270 +}
271 +
272 + /****************************************************************************
273 +*
274 +* Register the driver with device tree
275 +*
276 +***************************************************************************/
277 +
278 +static const struct of_device_id bcm2835_gpiomem_of_match[] = {
279 + {.compatible = "brcm,bcm2835-gpiomem",},
280 + { /* sentinel */ },
281 +};
282 +
283 +MODULE_DEVICE_TABLE(of, bcm2835_gpiomem_of_match);
284 +
285 +static struct platform_driver bcm2835_gpiomem_driver = {
286 + .probe = bcm2835_gpiomem_probe,
287 + .remove = bcm2835_gpiomem_remove,
288 + .driver = {
289 + .name = DRIVER_NAME,
290 + .owner = THIS_MODULE,
291 + .of_match_table = bcm2835_gpiomem_of_match,
292 + },
293 +};
294 +
295 +module_platform_driver(bcm2835_gpiomem_driver);
296 +
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>");