1 From 6e169c17c0de4503264186d90aa51639924c6e9c 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] 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 @@ -15,6 +15,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_BCM2708_VCMEM) += vc_mem.o
39 +obj-$(CONFIG_BCM_VCIO) += vcio.o
40 obj-$(CONFIG_BCM_VC_SM) += vc_sm/
42 obj-$(CONFIG_BCM2835_DEVGPIOMEM)+= bcm2835-gpiomem.o
44 +++ b/drivers/char/broadcom/vcio.c
47 + * Copyright (C) 2010 Broadcom
48 + * Copyright (C) 2015 Noralf Trønnes
50 + * This program is free software; you can redistribute it and/or modify
51 + * it under the terms of the GNU General Public License version 2 as
52 + * published by the Free Software Foundation.
56 +#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
58 +#include <linux/cdev.h>
59 +#include <linux/device.h>
60 +#include <linux/fs.h>
61 +#include <linux/init.h>
62 +#include <linux/ioctl.h>
63 +#include <linux/module.h>
64 +#include <linux/slab.h>
65 +#include <linux/uaccess.h>
66 +#include <soc/bcm2835/raspberrypi-firmware.h>
68 +#define MBOX_CHAN_PROPERTY 8
70 +#define VCIO_IOC_MAGIC 100
71 +#define IOCTL_MBOX_PROPERTY _IOWR(VCIO_IOC_MAGIC, 0, char *)
76 + struct class *class;
77 + struct rpi_firmware *fw;
80 +static int vcio_user_property_list(void *user)
85 + /* The first 32-bit is the size of the buffer */
86 + if (copy_from_user(&size, user, sizeof(size)))
89 + buf = kmalloc(size, GFP_KERNEL);
93 + if (copy_from_user(buf, user, size)) {
98 + /* Strip off protocol encapsulation */
99 + ret = rpi_firmware_property_list(vcio.fw, &buf[2], size - 12);
105 + buf[1] = RPI_FIRMWARE_STATUS_SUCCESS;
106 + if (copy_to_user(user, buf, size))
114 +static int vcio_device_open(struct inode *inode, struct file *file)
116 + try_module_get(THIS_MODULE);
121 +static int vcio_device_release(struct inode *inode, struct file *file)
123 + module_put(THIS_MODULE);
128 +static long vcio_device_ioctl(struct file *file, unsigned int ioctl_num,
129 + unsigned long ioctl_param)
131 + switch (ioctl_num) {
132 + case IOCTL_MBOX_PROPERTY:
133 + return vcio_user_property_list((void *)ioctl_param);
135 + pr_err("unknown ioctl: %d\n", ioctl_num);
140 +const struct file_operations vcio_fops = {
141 + .unlocked_ioctl = vcio_device_ioctl,
142 + .open = vcio_device_open,
143 + .release = vcio_device_release,
146 +static int __init vcio_init(void)
148 + struct device_node *np;
149 + static struct device *dev;
152 + np = of_find_compatible_node(NULL, NULL,
153 + "raspberrypi,bcm2835-firmware");
154 +/* Uncomment this when we only boot with Device Tree
155 + if (!of_device_is_available(np))
158 + vcio.fw = rpi_firmware_get(np);
162 + ret = alloc_chrdev_region(&vcio.devt, 0, 1, "vcio");
164 + pr_err("failed to allocate device number\n");
168 + cdev_init(&vcio.cdev, &vcio_fops);
169 + vcio.cdev.owner = THIS_MODULE;
170 + ret = cdev_add(&vcio.cdev, vcio.devt, 1);
172 + pr_err("failed to register device\n");
173 + goto err_unregister_chardev;
177 + * Create sysfs entries
178 + * 'bcm2708_vcio' is used for backwards compatibility so we don't break
179 + * userspace. Raspian has a udev rule that changes the permissions.
181 + vcio.class = class_create(THIS_MODULE, "bcm2708_vcio");
182 + if (IS_ERR(vcio.class)) {
183 + ret = PTR_ERR(vcio.class);
184 + pr_err("failed to create class\n");
188 + dev = device_create(vcio.class, NULL, vcio.devt, NULL, "vcio");
190 + ret = PTR_ERR(dev);
191 + pr_err("failed to create device\n");
192 + goto err_class_destroy;
198 + class_destroy(vcio.class);
200 + cdev_del(&vcio.cdev);
201 +err_unregister_chardev:
202 + unregister_chrdev_region(vcio.devt, 1);
206 +module_init(vcio_init);
208 +static void __exit vcio_exit(void)
210 + device_destroy(vcio.class, vcio.devt);
211 + class_destroy(vcio.class);
212 + cdev_del(&vcio.cdev);
213 + unregister_chrdev_region(vcio.devt, 1);
215 +module_exit(vcio_exit);
217 +MODULE_AUTHOR("Gray Girling");
218 +MODULE_AUTHOR("Noralf Trønnes");
219 +MODULE_DESCRIPTION("Mailbox userspace access");
220 +MODULE_LICENSE("GPL");