1 From 7de8f9adc447ba0709ad29ad8af13da0085c8491 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/203] char: broadcom: Add vcio module
6 Content-Type: text/plain; charset=UTF-8
7 Content-Transfer-Encoding: 8bit
9 Add module for accessing the mailbox property channel through
10 /dev/vcio. Was previously in bcm2708-vcio.
12 Signed-off-by: Noralf Trønnes <noralf@tronnes.org>
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
20 --- a/drivers/char/broadcom/Kconfig
21 +++ b/drivers/char/broadcom/Kconfig
22 @@ -22,6 +22,12 @@ config BCM2708_VCMEM
24 Helper for videocore memory access and total size allocation.
27 + tristate "Mailbox userspace access"
28 + depends on BCM2835_MBOX
30 + Gives access to the mailbox property channel from userspace.
35 --- a/drivers/char/broadcom/Makefile
36 +++ b/drivers/char/broadcom/Makefile
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/
43 +++ b/drivers/char/broadcom/vcio.c
46 + * Copyright (C) 2010 Broadcom
47 + * Copyright (C) 2015 Noralf Trønnes
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.
55 +#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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>
67 +#define MBOX_CHAN_PROPERTY 8
69 +#define VCIO_IOC_MAGIC 100
70 +#define IOCTL_MBOX_PROPERTY _IOWR(VCIO_IOC_MAGIC, 0, char *)
75 + struct class *class;
76 + struct rpi_firmware *fw;
79 +static int vcio_user_property_list(void *user)
84 + /* The first 32-bit is the size of the buffer */
85 + if (copy_from_user(&size, user, sizeof(size)))
88 + buf = kmalloc(size, GFP_KERNEL);
92 + if (copy_from_user(buf, user, size)) {
97 + /* Strip off protocol encapsulation */
98 + ret = rpi_firmware_property_list(vcio.fw, &buf[2], size - 12);
104 + buf[1] = RPI_FIRMWARE_STATUS_SUCCESS;
105 + if (copy_to_user(user, buf, size))
113 +static int vcio_device_open(struct inode *inode, struct file *file)
115 + try_module_get(THIS_MODULE);
120 +static int vcio_device_release(struct inode *inode, struct file *file)
122 + module_put(THIS_MODULE);
127 +static long vcio_device_ioctl(struct file *file, unsigned int ioctl_num,
128 + unsigned long ioctl_param)
130 + switch (ioctl_num) {
131 + case IOCTL_MBOX_PROPERTY:
132 + return vcio_user_property_list((void *)ioctl_param);
134 + pr_err("unknown ioctl: %d\n", ioctl_num);
139 +const struct file_operations vcio_fops = {
140 + .unlocked_ioctl = vcio_device_ioctl,
141 + .open = vcio_device_open,
142 + .release = vcio_device_release,
145 +static int __init vcio_init(void)
147 + struct device_node *np;
148 + static struct device *dev;
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))
157 + vcio.fw = rpi_firmware_get(np);
161 + ret = alloc_chrdev_region(&vcio.devt, 0, 1, "vcio");
163 + pr_err("failed to allocate device number\n");
167 + cdev_init(&vcio.cdev, &vcio_fops);
168 + vcio.cdev.owner = THIS_MODULE;
169 + ret = cdev_add(&vcio.cdev, vcio.devt, 1);
171 + pr_err("failed to register device\n");
172 + goto err_unregister_chardev;
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.
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");
187 + dev = device_create(vcio.class, NULL, vcio.devt, NULL, "vcio");
189 + ret = PTR_ERR(dev);
190 + pr_err("failed to create device\n");
191 + goto err_class_destroy;
197 + class_destroy(vcio.class);
199 + cdev_del(&vcio.cdev);
200 +err_unregister_chardev:
201 + unregister_chrdev_region(vcio.devt, 1);
205 +module_init(vcio_init);
207 +static void __exit vcio_exit(void)
209 + device_destroy(vcio.class, vcio.devt);
210 + class_destroy(vcio.class);
211 + cdev_del(&vcio.cdev);
212 + unregister_chrdev_region(vcio.devt, 1);
214 +module_exit(vcio_exit);
216 +MODULE_AUTHOR("Gray Girling");
217 +MODULE_AUTHOR("Noralf Trønnes");
218 +MODULE_DESCRIPTION("Mailbox userspace access");
219 +MODULE_LICENSE("GPL");