5032a6f7cb22b2e467c22d93d82c9279834bee89
[openwrt/openwrt.git] / target / linux / brcm2708 / patches-4.1 / 0116-char-broadcom-Add-vcio-module.patch
1 From becc3412eae55ac3b1642ddc074cb9ca2cbc2e11 Mon Sep 17 00:00:00 2001
2 From: =?UTF-8?q?Noralf=20Tr=C3=B8nnes?= <noralf@tronnes.org>
3 Date: Fri, 26 Jun 2015 14:27:06 +0200
4 Subject: [PATCH 116/121] char: broadcom: Add vcio module
5 MIME-Version: 1.0
6 Content-Type: text/plain; charset=UTF-8
7 Content-Transfer-Encoding: 8bit
8
9 Add module for accessing the mailbox property channel through
10 /dev/vcio. Was previously in bcm2708-vcio.
11
12 Signed-off-by: Noralf Trønnes <noralf@tronnes.org>
13 ---
14 drivers/char/broadcom/Kconfig | 6 ++
15 drivers/char/broadcom/Makefile | 1 +
16 drivers/char/broadcom/vcio.c | 175 +++++++++++++++++++++++++++++++++++++++++
17 3 files changed, 182 insertions(+)
18 create mode 100644 drivers/char/broadcom/vcio.c
19
20 --- a/drivers/char/broadcom/Kconfig
21 +++ b/drivers/char/broadcom/Kconfig
22 @@ -22,6 +22,12 @@ config BCM2708_VCMEM
23 help
24 Helper for videocore memory access and total size allocation.
25
26 +config BCM_VCIO
27 + tristate "Mailbox userspace access"
28 + depends on BCM2835_MBOX
29 + help
30 + Gives access to the mailbox property channel from userspace.
31 +
32 endif
33
34 config BCM_VC_SM
35 --- a/drivers/char/broadcom/Makefile
36 +++ b/drivers/char/broadcom/Makefile
37 @@ -1,3 +1,4 @@
38 obj-$(CONFIG_BCM_VC_CMA) += vc_cma/
39 obj-$(CONFIG_BCM2708_VCMEM) += vc_mem.o
40 +obj-$(CONFIG_BCM_VCIO) += vcio.o
41 obj-$(CONFIG_BCM_VC_SM) += vc_sm/
42 --- /dev/null
43 +++ b/drivers/char/broadcom/vcio.c
44 @@ -0,0 +1,175 @@
45 +/*
46 + * Copyright (C) 2010 Broadcom
47 + * Copyright (C) 2015 Noralf Trønnes
48 + *
49 + * This program is free software; you can redistribute it and/or modify
50 + * it under the terms of the GNU General Public License version 2 as
51 + * published by the Free Software Foundation.
52 + *
53 + */
54 +
55 +#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
56 +
57 +#include <linux/cdev.h>
58 +#include <linux/device.h>
59 +#include <linux/fs.h>
60 +#include <linux/init.h>
61 +#include <linux/ioctl.h>
62 +#include <linux/module.h>
63 +#include <linux/slab.h>
64 +#include <linux/uaccess.h>
65 +#include <soc/bcm2835/raspberrypi-firmware.h>
66 +
67 +#define MBOX_CHAN_PROPERTY 8
68 +
69 +#define VCIO_IOC_MAGIC 100
70 +#define IOCTL_MBOX_PROPERTY _IOWR(VCIO_IOC_MAGIC, 0, char *)
71 +
72 +static struct {
73 + dev_t devt;
74 + struct cdev cdev;
75 + struct class *class;
76 + struct rpi_firmware *fw;
77 +} vcio;
78 +
79 +static int vcio_user_property_list(void *user)
80 +{
81 + u32 *buf, size;
82 + int ret;
83 +
84 + /* The first 32-bit is the size of the buffer */
85 + if (copy_from_user(&size, user, sizeof(size)))
86 + return -EFAULT;
87 +
88 + buf = kmalloc(size, GFP_KERNEL);
89 + if (!buf)
90 + return -ENOMEM;
91 +
92 + if (copy_from_user(buf, user, size)) {
93 + kfree(buf);
94 + return -EFAULT;
95 + }
96 +
97 + /* Strip off protocol encapsulation */
98 + ret = rpi_firmware_property_list(vcio.fw, &buf[2], size - 12);
99 + if (ret) {
100 + kfree(buf);
101 + return ret;
102 + }
103 +
104 + buf[1] = RPI_FIRMWARE_STATUS_SUCCESS;
105 + if (copy_to_user(user, buf, size))
106 + ret = -EFAULT;
107 +
108 + kfree(buf);
109 +
110 + return ret;
111 +}
112 +
113 +static int vcio_device_open(struct inode *inode, struct file *file)
114 +{
115 + try_module_get(THIS_MODULE);
116 +
117 + return 0;
118 +}
119 +
120 +static int vcio_device_release(struct inode *inode, struct file *file)
121 +{
122 + module_put(THIS_MODULE);
123 +
124 + return 0;
125 +}
126 +
127 +static long vcio_device_ioctl(struct file *file, unsigned int ioctl_num,
128 + unsigned long ioctl_param)
129 +{
130 + switch (ioctl_num) {
131 + case IOCTL_MBOX_PROPERTY:
132 + return vcio_user_property_list((void *)ioctl_param);
133 + default:
134 + pr_err("unknown ioctl: %d\n", ioctl_num);
135 + return -EINVAL;
136 + }
137 +}
138 +
139 +const struct file_operations vcio_fops = {
140 + .unlocked_ioctl = vcio_device_ioctl,
141 + .open = vcio_device_open,
142 + .release = vcio_device_release,
143 +};
144 +
145 +static int __init vcio_init(void)
146 +{
147 + struct device_node *np;
148 + static struct device *dev;
149 + int ret;
150 +
151 + np = of_find_compatible_node(NULL, NULL,
152 + "raspberrypi,bcm2835-firmware");
153 +/* Uncomment this when we only boot with Device Tree
154 + if (!of_device_is_available(np))
155 + return -ENODEV;
156 +*/
157 + vcio.fw = rpi_firmware_get(np);
158 + if (!vcio.fw)
159 + return -ENODEV;
160 +
161 + ret = alloc_chrdev_region(&vcio.devt, 0, 1, "vcio");
162 + if (ret) {
163 + pr_err("failed to allocate device number\n");
164 + return ret;
165 + }
166 +
167 + cdev_init(&vcio.cdev, &vcio_fops);
168 + vcio.cdev.owner = THIS_MODULE;
169 + ret = cdev_add(&vcio.cdev, vcio.devt, 1);
170 + if (ret) {
171 + pr_err("failed to register device\n");
172 + goto err_unregister_chardev;
173 + }
174 +
175 + /*
176 + * Create sysfs entries
177 + * 'bcm2708_vcio' is used for backwards compatibility so we don't break
178 + * userspace. Raspian has a udev rule that changes the permissions.
179 + */
180 + vcio.class = class_create(THIS_MODULE, "bcm2708_vcio");
181 + if (IS_ERR(vcio.class)) {
182 + ret = PTR_ERR(vcio.class);
183 + pr_err("failed to create class\n");
184 + goto err_cdev_del;
185 + }
186 +
187 + dev = device_create(vcio.class, NULL, vcio.devt, NULL, "vcio");
188 + if (IS_ERR(dev)) {
189 + ret = PTR_ERR(dev);
190 + pr_err("failed to create device\n");
191 + goto err_class_destroy;
192 + }
193 +
194 + return 0;
195 +
196 +err_class_destroy:
197 + class_destroy(vcio.class);
198 +err_cdev_del:
199 + cdev_del(&vcio.cdev);
200 +err_unregister_chardev:
201 + unregister_chrdev_region(vcio.devt, 1);
202 +
203 + return ret;
204 +}
205 +module_init(vcio_init);
206 +
207 +static void __exit vcio_exit(void)
208 +{
209 + device_destroy(vcio.class, vcio.devt);
210 + class_destroy(vcio.class);
211 + cdev_del(&vcio.cdev);
212 + unregister_chrdev_region(vcio.devt, 1);
213 +}
214 +module_exit(vcio_exit);
215 +
216 +MODULE_AUTHOR("Gray Girling");
217 +MODULE_AUTHOR("Noralf Trønnes");
218 +MODULE_DESCRIPTION("Mailbox userspace access");
219 +MODULE_LICENSE("GPL");