brcm2708: switch to linux 4.4 and update patches
[openwrt.git] / target / linux / brcm2708 / patches-4.4 / 0040-Add-dev-gpiomem-device-for-rootless-user-GPIO-access.patch
1 From a582dfc468cbb9176054bdcc02dce40a0e0441e6 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 040/156] Add /dev/gpiomem device for rootless user GPIO access
5
6 Signed-off-by: Luke Wren <luke@raspberrypi.org>
7
8 bcm2835-gpiomem: Fix for ARCH_BCM2835 builds
9
10 Build on ARCH_BCM2835, and fail to probe if no IO resource.
11
12 See: https://github.com/raspberrypi/linux/issues/1154
13 ---
14  drivers/char/broadcom/Kconfig           |   9 ++
15  drivers/char/broadcom/Makefile          |   3 +
16  drivers/char/broadcom/bcm2835-gpiomem.c | 260 ++++++++++++++++++++++++++++++++
17  3 files changed, 272 insertions(+)
18  create mode 100644 drivers/char/broadcom/bcm2835-gpiomem.c
19
20 --- a/drivers/char/broadcom/Kconfig
21 +++ b/drivers/char/broadcom/Kconfig
22 @@ -32,3 +32,12 @@ config BCM_VC_SM
23         help
24         Support for the VC shared memory on the Broadcom reference
25         design. Uses the VCHIQ stack.
26 +
27 +config BCM2835_DEVGPIOMEM
28 +       tristate "/dev/gpiomem rootless GPIO access via mmap() on the BCM2835"
29 +       default m
30 +       help
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 +
35 --- a/drivers/char/broadcom/Makefile
36 +++ b/drivers/char/broadcom/Makefile
37 @@ -1,3 +1,6 @@
38  obj-$(CONFIG_BCM_VC_CMA)       += vc_cma/
39  obj-$(CONFIG_BCM2708_VCMEM)    += vc_mem.o
40  obj-$(CONFIG_BCM_VC_SM)         += vc_sm/
41 +
42 +obj-$(CONFIG_BCM2835_DEVGPIOMEM)+= bcm2835-gpiomem.o
43 +
44 --- /dev/null
45 +++ b/drivers/char/broadcom/bcm2835-gpiomem.c
46 @@ -0,0 +1,260 @@
47 +/**
48 + * GPIO memory device driver
49 + *
50 + * Creates a chardev /dev/gpiomem which will provide user access to
51 + * the BCM2835's GPIO registers when it is mmap()'d.
52 + * No longer need root for user GPIO access, but without relaxing permissions
53 + * on /dev/mem.
54 + *
55 + * Written by Luke Wren <luke@raspberrypi.org>
56 + * Copyright (c) 2015, Raspberry Pi (Trading) Ltd.
57 + *
58 + * Redistribution and use in source and binary forms, with or without
59 + * modification, are permitted provided that the following conditions
60 + * are met:
61 + * 1. Redistributions of source code must retain the above copyright
62 + *    notice, this list of conditions, and the following disclaimer,
63 + *    without modification.
64 + * 2. Redistributions in binary form must reproduce the above copyright
65 + *    notice, this list of conditions and the following disclaimer in the
66 + *    documentation and/or other materials provided with the distribution.
67 + * 3. The names of the above-listed copyright holders may not be used
68 + *    to endorse or promote products derived from this software without
69 + *    specific prior written permission.
70 + *
71 + * ALTERNATIVELY, this software may be distributed under the terms of the
72 + * GNU General Public License ("GPL") version 2, as published by the Free
73 + * Software Foundation.
74 + *
75 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
76 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
77 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
78 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
79 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
80 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
81 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
82 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
83 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
84 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
85 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
86 + */
87 +
88 +#include <linux/kernel.h>
89 +#include <linux/module.h>
90 +#include <linux/of.h>
91 +#include <linux/platform_device.h>
92 +#include <linux/mm.h>
93 +#include <linux/slab.h>
94 +#include <linux/cdev.h>
95 +#include <linux/pagemap.h>
96 +#include <linux/io.h>
97 +
98 +#define DEVICE_NAME "bcm2835-gpiomem"
99 +#define DRIVER_NAME "gpiomem-bcm2835"
100 +#define DEVICE_MINOR 0
101 +
102 +struct bcm2835_gpiomem_instance {
103 +       unsigned long gpio_regs_phys;
104 +       struct device *dev;
105 +};
106 +
107 +static struct cdev bcm2835_gpiomem_cdev;
108 +static dev_t bcm2835_gpiomem_devid;
109 +static struct class *bcm2835_gpiomem_class;
110 +static struct device *bcm2835_gpiomem_dev;
111 +static struct bcm2835_gpiomem_instance *inst;
112 +
113 +
114 +/****************************************************************************
115 +*
116 +*   GPIO mem chardev file ops
117 +*
118 +***************************************************************************/
119 +
120 +static int bcm2835_gpiomem_open(struct inode *inode, struct file *file)
121 +{
122 +       int dev = iminor(inode);
123 +       int ret = 0;
124 +
125 +       dev_info(inst->dev, "gpiomem device opened.");
126 +
127 +       if (dev != DEVICE_MINOR) {
128 +               dev_err(inst->dev, "Unknown minor device: %d", dev);
129 +               ret = -ENXIO;
130 +       }
131 +       return ret;
132 +}
133 +
134 +static int bcm2835_gpiomem_release(struct inode *inode, struct file *file)
135 +{
136 +       int dev = iminor(inode);
137 +       int ret = 0;
138 +
139 +       if (dev != DEVICE_MINOR) {
140 +               dev_err(inst->dev, "Unknown minor device %d", dev);
141 +               ret = -ENXIO;
142 +       }
143 +       return ret;
144 +}
145 +
146 +static const struct vm_operations_struct bcm2835_gpiomem_vm_ops = {
147 +#ifdef CONFIG_HAVE_IOREMAP_PROT
148 +       .access = generic_access_phys
149 +#endif
150 +};
151 +
152 +static int bcm2835_gpiomem_mmap(struct file *file, struct vm_area_struct *vma)
153 +{
154 +       /* Ignore what the user says - they're getting the GPIO regs
155 +          whether they like it or not! */
156 +       unsigned long gpio_page = inst->gpio_regs_phys >> PAGE_SHIFT;
157 +
158 +       vma->vm_page_prot = phys_mem_access_prot(file, gpio_page,
159 +                                                PAGE_SIZE,
160 +                                                vma->vm_page_prot);
161 +       vma->vm_ops = &bcm2835_gpiomem_vm_ops;
162 +       if (remap_pfn_range(vma, vma->vm_start,
163 +                       gpio_page,
164 +                       PAGE_SIZE,
165 +                       vma->vm_page_prot)) {
166 +               return -EAGAIN;
167 +       }
168 +       return 0;
169 +}
170 +
171 +static const struct file_operations
172 +bcm2835_gpiomem_fops = {
173 +       .owner = THIS_MODULE,
174 +       .open = bcm2835_gpiomem_open,
175 +       .release = bcm2835_gpiomem_release,
176 +       .mmap = bcm2835_gpiomem_mmap,
177 +};
178 +
179 +
180 + /****************************************************************************
181 +*
182 +*   Probe and remove functions
183 +*
184 +***************************************************************************/
185 +
186 +
187 +static int bcm2835_gpiomem_probe(struct platform_device *pdev)
188 +{
189 +       int err;
190 +       void *ptr_err;
191 +       struct device *dev = &pdev->dev;
192 +       struct resource *ioresource;
193 +
194 +       /* Allocate buffers and instance data */
195 +
196 +       inst = kzalloc(sizeof(struct bcm2835_gpiomem_instance), GFP_KERNEL);
197 +
198 +       if (!inst) {
199 +               err = -ENOMEM;
200 +               goto failed_inst_alloc;
201 +       }
202 +
203 +       inst->dev = dev;
204 +
205 +       ioresource = platform_get_resource(pdev, IORESOURCE_MEM, 0);
206 +       if (ioresource) {
207 +               inst->gpio_regs_phys = ioresource->start;
208 +       } else {
209 +               dev_err(inst->dev, "failed to get IO resource");
210 +               err = -ENOENT;
211 +               goto failed_get_resource;
212 +       }
213 +
214 +       /* Create character device entries */
215 +
216 +       err = alloc_chrdev_region(&bcm2835_gpiomem_devid,
217 +                                 DEVICE_MINOR, 1, DEVICE_NAME);
218 +       if (err != 0) {
219 +               dev_err(inst->dev, "unable to allocate device number");
220 +               goto failed_alloc_chrdev;
221 +       }
222 +       cdev_init(&bcm2835_gpiomem_cdev, &bcm2835_gpiomem_fops);
223 +       bcm2835_gpiomem_cdev.owner = THIS_MODULE;
224 +       err = cdev_add(&bcm2835_gpiomem_cdev, bcm2835_gpiomem_devid, 1);
225 +       if (err != 0) {
226 +               dev_err(inst->dev, "unable to register device");
227 +               goto failed_cdev_add;
228 +       }
229 +
230 +       /* Create sysfs entries */
231 +
232 +       bcm2835_gpiomem_class = class_create(THIS_MODULE, DEVICE_NAME);
233 +       ptr_err = bcm2835_gpiomem_class;
234 +       if (IS_ERR(ptr_err))
235 +               goto failed_class_create;
236 +
237 +       bcm2835_gpiomem_dev = device_create(bcm2835_gpiomem_class, NULL,
238 +                                       bcm2835_gpiomem_devid, NULL,
239 +                                       "gpiomem");
240 +       ptr_err = bcm2835_gpiomem_dev;
241 +       if (IS_ERR(ptr_err))
242 +               goto failed_device_create;
243 +
244 +       dev_info(inst->dev, "Initialised: Registers at 0x%08lx",
245 +               inst->gpio_regs_phys);
246 +
247 +       return 0;
248 +
249 +failed_device_create:
250 +       class_destroy(bcm2835_gpiomem_class);
251 +failed_class_create:
252 +       cdev_del(&bcm2835_gpiomem_cdev);
253 +       err = PTR_ERR(ptr_err);
254 +failed_cdev_add:
255 +       unregister_chrdev_region(bcm2835_gpiomem_devid, 1);
256 +failed_alloc_chrdev:
257 +failed_get_resource:
258 +       kfree(inst);
259 +failed_inst_alloc:
260 +       dev_err(inst->dev, "could not load bcm2835_gpiomem");
261 +       return err;
262 +}
263 +
264 +static int bcm2835_gpiomem_remove(struct platform_device *pdev)
265 +{
266 +       struct device *dev = inst->dev;
267 +
268 +       kfree(inst);
269 +       device_destroy(bcm2835_gpiomem_class, bcm2835_gpiomem_devid);
270 +       class_destroy(bcm2835_gpiomem_class);
271 +       cdev_del(&bcm2835_gpiomem_cdev);
272 +       unregister_chrdev_region(bcm2835_gpiomem_devid, 1);
273 +
274 +       dev_info(dev, "GPIO mem driver removed - OK");
275 +       return 0;
276 +}
277 +
278 + /****************************************************************************
279 +*
280 +*   Register the driver with device tree
281 +*
282 +***************************************************************************/
283 +
284 +static const struct of_device_id bcm2835_gpiomem_of_match[] = {
285 +       {.compatible = "brcm,bcm2835-gpiomem",},
286 +       { /* sentinel */ },
287 +};
288 +
289 +MODULE_DEVICE_TABLE(of, bcm2835_gpiomem_of_match);
290 +
291 +static struct platform_driver bcm2835_gpiomem_driver = {
292 +       .probe = bcm2835_gpiomem_probe,
293 +       .remove = bcm2835_gpiomem_remove,
294 +       .driver = {
295 +                  .name = DRIVER_NAME,
296 +                  .owner = THIS_MODULE,
297 +                  .of_match_table = bcm2835_gpiomem_of_match,
298 +                  },
299 +};
300 +
301 +module_platform_driver(bcm2835_gpiomem_driver);
302 +
303 +MODULE_ALIAS("platform:gpiomem-bcm2835");
304 +MODULE_LICENSE("GPL");
305 +MODULE_DESCRIPTION("gpiomem driver for accessing GPIO from userspace");
306 +MODULE_AUTHOR("Luke Wren <luke@raspberrypi.org>");