c6b8b7042c887dc5eb3787b7c162ec0e27f4b6f2
[oweals/openwrt.git] /
1 From 08f9a940fe1b035594d00429b9f6d419e8cd3462 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 048/773] 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 | 258 ++++++++++++++++++++++++
17  3 files changed, 270 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 @@ -26,3 +26,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,2 +1,5 @@
38  obj-$(CONFIG_BCM2708_VCMEM)    += vc_mem.o
39  obj-$(CONFIG_BCM_VC_SM)         += vc_sm/
40 +
41 +obj-$(CONFIG_BCM2835_DEVGPIOMEM)+= bcm2835-gpiomem.o
42 +
43 --- /dev/null
44 +++ b/drivers/char/broadcom/bcm2835-gpiomem.c
45 @@ -0,0 +1,258 @@
46 +/**
47 + * GPIO memory device driver
48 + *
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
52 + * on /dev/mem.
53 + *
54 + * Written by Luke Wren <luke@raspberrypi.org>
55 + * Copyright (c) 2015, Raspberry Pi (Trading) Ltd.
56 + *
57 + * Redistribution and use in source and binary forms, with or without
58 + * modification, are permitted provided that the following conditions
59 + * are met:
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.
69 + *
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.
73 + *
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.
85 + */
86 +
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>
96 +
97 +#define DEVICE_NAME "bcm2835-gpiomem"
98 +#define DRIVER_NAME "gpiomem-bcm2835"
99 +#define DEVICE_MINOR 0
100 +
101 +struct bcm2835_gpiomem_instance {
102 +       unsigned long gpio_regs_phys;
103 +       struct device *dev;
104 +};
105 +
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;
111 +
112 +
113 +/****************************************************************************
114 +*
115 +*   GPIO mem chardev file ops
116 +*
117 +***************************************************************************/
118 +
119 +static int bcm2835_gpiomem_open(struct inode *inode, struct file *file)
120 +{
121 +       int dev = iminor(inode);
122 +       int ret = 0;
123 +
124 +       if (dev != DEVICE_MINOR) {
125 +               dev_err(inst->dev, "Unknown minor device: %d", dev);
126 +               ret = -ENXIO;
127 +       }
128 +       return ret;
129 +}
130 +
131 +static int bcm2835_gpiomem_release(struct inode *inode, struct file *file)
132 +{
133 +       int dev = iminor(inode);
134 +       int ret = 0;
135 +
136 +       if (dev != DEVICE_MINOR) {
137 +               dev_err(inst->dev, "Unknown minor device %d", dev);
138 +               ret = -ENXIO;
139 +       }
140 +       return ret;
141 +}
142 +
143 +static const struct vm_operations_struct bcm2835_gpiomem_vm_ops = {
144 +#ifdef CONFIG_HAVE_IOREMAP_PROT
145 +       .access = generic_access_phys
146 +#endif
147 +};
148 +
149 +static int bcm2835_gpiomem_mmap(struct file *file, struct vm_area_struct *vma)
150 +{
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;
154 +
155 +       vma->vm_page_prot = phys_mem_access_prot(file, gpio_page,
156 +                                                PAGE_SIZE,
157 +                                                vma->vm_page_prot);
158 +       vma->vm_ops = &bcm2835_gpiomem_vm_ops;
159 +       if (remap_pfn_range(vma, vma->vm_start,
160 +                       gpio_page,
161 +                       PAGE_SIZE,
162 +                       vma->vm_page_prot)) {
163 +               return -EAGAIN;
164 +       }
165 +       return 0;
166 +}
167 +
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,
174 +};
175 +
176 +
177 + /****************************************************************************
178 +*
179 +*   Probe and remove functions
180 +*
181 +***************************************************************************/
182 +
183 +
184 +static int bcm2835_gpiomem_probe(struct platform_device *pdev)
185 +{
186 +       int err;
187 +       void *ptr_err;
188 +       struct device *dev = &pdev->dev;
189 +       struct resource *ioresource;
190 +
191 +       /* Allocate buffers and instance data */
192 +
193 +       inst = kzalloc(sizeof(struct bcm2835_gpiomem_instance), GFP_KERNEL);
194 +
195 +       if (!inst) {
196 +               err = -ENOMEM;
197 +               goto failed_inst_alloc;
198 +       }
199 +
200 +       inst->dev = dev;
201 +
202 +       ioresource = platform_get_resource(pdev, IORESOURCE_MEM, 0);
203 +       if (ioresource) {
204 +               inst->gpio_regs_phys = ioresource->start;
205 +       } else {
206 +               dev_err(inst->dev, "failed to get IO resource");
207 +               err = -ENOENT;
208 +               goto failed_get_resource;
209 +       }
210 +
211 +       /* Create character device entries */
212 +
213 +       err = alloc_chrdev_region(&bcm2835_gpiomem_devid,
214 +                                 DEVICE_MINOR, 1, DEVICE_NAME);
215 +       if (err != 0) {
216 +               dev_err(inst->dev, "unable to allocate device number");
217 +               goto failed_alloc_chrdev;
218 +       }
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);
222 +       if (err != 0) {
223 +               dev_err(inst->dev, "unable to register device");
224 +               goto failed_cdev_add;
225 +       }
226 +
227 +       /* Create sysfs entries */
228 +
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;
233 +
234 +       bcm2835_gpiomem_dev = device_create(bcm2835_gpiomem_class, NULL,
235 +                                       bcm2835_gpiomem_devid, NULL,
236 +                                       "gpiomem");
237 +       ptr_err = bcm2835_gpiomem_dev;
238 +       if (IS_ERR(ptr_err))
239 +               goto failed_device_create;
240 +
241 +       dev_info(inst->dev, "Initialised: Registers at 0x%08lx",
242 +               inst->gpio_regs_phys);
243 +
244 +       return 0;
245 +
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);
251 +failed_cdev_add:
252 +       unregister_chrdev_region(bcm2835_gpiomem_devid, 1);
253 +failed_alloc_chrdev:
254 +failed_get_resource:
255 +       kfree(inst);
256 +failed_inst_alloc:
257 +       dev_err(inst->dev, "could not load bcm2835_gpiomem");
258 +       return err;
259 +}
260 +
261 +static int bcm2835_gpiomem_remove(struct platform_device *pdev)
262 +{
263 +       struct device *dev = inst->dev;
264 +
265 +       kfree(inst);
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);
270 +
271 +       dev_info(dev, "GPIO mem driver removed - OK");
272 +       return 0;
273 +}
274 +
275 + /****************************************************************************
276 +*
277 +*   Register the driver with device tree
278 +*
279 +***************************************************************************/
280 +
281 +static const struct of_device_id bcm2835_gpiomem_of_match[] = {
282 +       {.compatible = "brcm,bcm2835-gpiomem",},
283 +       { /* sentinel */ },
284 +};
285 +
286 +MODULE_DEVICE_TABLE(of, bcm2835_gpiomem_of_match);
287 +
288 +static struct platform_driver bcm2835_gpiomem_driver = {
289 +       .probe = bcm2835_gpiomem_probe,
290 +       .remove = bcm2835_gpiomem_remove,
291 +       .driver = {
292 +                  .name = DRIVER_NAME,
293 +                  .owner = THIS_MODULE,
294 +                  .of_match_table = bcm2835_gpiomem_of_match,
295 +                  },
296 +};
297 +
298 +module_platform_driver(bcm2835_gpiomem_driver);
299 +
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>");