Skip to content

Commit bf9ebdf

Browse files
notropopcornmix
authored andcommitted
char: broadcom: Add vcio module
Add module for accessing the mailbox property channel through /dev/vcio. Was previously in bcm2708-vcio. Signed-off-by: Noralf Trønnes <[email protected]> char: vcio: Add compat ioctl handling There was no compat ioctl handler, so 32 bit userspace on a 64 bit kernel failed as IOCTL_MBOX_PROPERTY used the size of char*. Signed-off-by: Dave Stevenson <[email protected]> char: vcio: Fail probe if rpi_firmware is not found. Device Tree is now the only supported config mechanism, therefore uncomment the block of code that fails the probe if the firmware node can't be found. Signed-off-by: Dave Stevenson <[email protected]> drivers: char: vcio: Use common compat header The definition of compat_ptr is now common for most platforms, but requires the inclusion of <linux/compat.h>. Signed-off-by: Phil Elwell <[email protected]> char: vcio: Rewrite as a firmware node child The old vcio driver is a simple character device that manually locates the firmware driver. Initialising it before the firmware driver causes a failure, and no retries are attempted. Rewrite vcio as a platform driver that depends on a DT node for its instantiation and the location of the firmware driver, making use of the miscdevice framework to reduce the code size. N.B. Using miscdevice changes the udev SUBSYSTEM string, so a change to the companion udev rule is required in order to continue to set the correct device permissions, e.g.: KERNEL="vcio", GROUP="video", MODE="0660" See: #4620 Signed-off-by: Phil Elwell <[email protected]>
1 parent 20e92b8 commit bf9ebdf

File tree

3 files changed

+194
-0
lines changed

3 files changed

+194
-0
lines changed

drivers/char/broadcom/Kconfig

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ config BCM2708_VCMEM
1515
help
1616
Helper for videocore memory access and total size allocation.
1717

18+
config BCM_VCIO
19+
tristate "Mailbox userspace access"
20+
depends on BCM2835_MBOX
21+
help
22+
Gives access to the mailbox property channel from userspace.
23+
1824
endif
1925

2026
config BCM2835_SMI_DEV

drivers/char/broadcom/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
obj-$(CONFIG_BCM2708_VCMEM) += vc_mem.o
2+
obj-$(CONFIG_BCM_VCIO) += vcio.o
23
obj-$(CONFIG_BCM2835_SMI_DEV) += bcm2835_smi_dev.o

drivers/char/broadcom/vcio.c

Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
/*
2+
* Copyright (C) 2010 Broadcom
3+
* Copyright (C) 2015 Noralf Trønnes
4+
* Copyright (C) 2021 Raspberry Pi (Trading) Ltd.
5+
*
6+
* This program is free software; you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License version 2 as
8+
* published by the Free Software Foundation.
9+
*
10+
*/
11+
12+
#include <linux/cdev.h>
13+
#include <linux/device.h>
14+
#include <linux/fs.h>
15+
#include <linux/init.h>
16+
#include <linux/ioctl.h>
17+
#include <linux/module.h>
18+
#include <linux/slab.h>
19+
#include <linux/uaccess.h>
20+
#include <linux/compat.h>
21+
#include <linux/miscdevice.h>
22+
#include <linux/of.h>
23+
#include <linux/platform_device.h>
24+
#include <soc/bcm2835/raspberrypi-firmware.h>
25+
26+
#define MODULE_NAME "vcio"
27+
#define VCIO_IOC_MAGIC 100
28+
#define IOCTL_MBOX_PROPERTY _IOWR(VCIO_IOC_MAGIC, 0, char *)
29+
#ifdef CONFIG_COMPAT
30+
#define IOCTL_MBOX_PROPERTY32 _IOWR(VCIO_IOC_MAGIC, 0, compat_uptr_t)
31+
#endif
32+
33+
struct vcio_data {
34+
struct rpi_firmware *fw;
35+
struct miscdevice misc_dev;
36+
};
37+
38+
static int vcio_user_property_list(struct vcio_data *vcio, void *user)
39+
{
40+
u32 *buf, size;
41+
int ret;
42+
43+
/* The first 32-bit is the size of the buffer */
44+
if (copy_from_user(&size, user, sizeof(size)))
45+
return -EFAULT;
46+
47+
buf = kmalloc(size, GFP_KERNEL);
48+
if (!buf)
49+
return -ENOMEM;
50+
51+
if (copy_from_user(buf, user, size)) {
52+
kfree(buf);
53+
return -EFAULT;
54+
}
55+
56+
/* Strip off protocol encapsulation */
57+
ret = rpi_firmware_property_list(vcio->fw, &buf[2], size - 12);
58+
if (ret) {
59+
kfree(buf);
60+
return ret;
61+
}
62+
63+
buf[1] = RPI_FIRMWARE_STATUS_SUCCESS;
64+
if (copy_to_user(user, buf, size))
65+
ret = -EFAULT;
66+
67+
kfree(buf);
68+
69+
return ret;
70+
}
71+
72+
static int vcio_device_open(struct inode *inode, struct file *file)
73+
{
74+
try_module_get(THIS_MODULE);
75+
76+
return 0;
77+
}
78+
79+
static int vcio_device_release(struct inode *inode, struct file *file)
80+
{
81+
module_put(THIS_MODULE);
82+
83+
return 0;
84+
}
85+
86+
static long vcio_device_ioctl(struct file *file, unsigned int ioctl_num,
87+
unsigned long ioctl_param)
88+
{
89+
struct vcio_data *vcio = container_of(file->private_data,
90+
struct vcio_data, misc_dev);
91+
92+
switch (ioctl_num) {
93+
case IOCTL_MBOX_PROPERTY:
94+
return vcio_user_property_list(vcio, (void *)ioctl_param);
95+
default:
96+
pr_err("unknown ioctl: %x\n", ioctl_num);
97+
return -EINVAL;
98+
}
99+
}
100+
101+
#ifdef CONFIG_COMPAT
102+
static long vcio_device_compat_ioctl(struct file *file, unsigned int ioctl_num,
103+
unsigned long ioctl_param)
104+
{
105+
struct vcio_data *vcio = container_of(file->private_data,
106+
struct vcio_data, misc_dev);
107+
108+
switch (ioctl_num) {
109+
case IOCTL_MBOX_PROPERTY32:
110+
return vcio_user_property_list(vcio, compat_ptr(ioctl_param));
111+
default:
112+
pr_err("unknown ioctl: %x\n", ioctl_num);
113+
return -EINVAL;
114+
}
115+
}
116+
#endif
117+
118+
const struct file_operations vcio_fops = {
119+
.unlocked_ioctl = vcio_device_ioctl,
120+
#ifdef CONFIG_COMPAT
121+
.compat_ioctl = vcio_device_compat_ioctl,
122+
#endif
123+
.open = vcio_device_open,
124+
.release = vcio_device_release,
125+
};
126+
127+
static int vcio_probe(struct platform_device *pdev)
128+
{
129+
struct device *dev = &pdev->dev;
130+
struct device_node *np = dev->of_node;
131+
struct device_node *fw_node;
132+
struct rpi_firmware *fw;
133+
struct vcio_data *vcio;
134+
135+
fw_node = of_get_parent(np);
136+
if (!fw_node) {
137+
dev_err(dev, "Missing firmware node\n");
138+
return -ENOENT;
139+
}
140+
141+
fw = rpi_firmware_get(fw_node);
142+
of_node_put(fw_node);
143+
if (!fw)
144+
return -EPROBE_DEFER;
145+
146+
vcio = devm_kzalloc(dev, sizeof(struct vcio_data), GFP_KERNEL);
147+
if (!vcio)
148+
return -ENOMEM;
149+
150+
vcio->fw = fw;
151+
vcio->misc_dev.fops = &vcio_fops;
152+
vcio->misc_dev.minor = MISC_DYNAMIC_MINOR;
153+
vcio->misc_dev.name = "vcio";
154+
vcio->misc_dev.parent = dev;
155+
156+
return misc_register(&vcio->misc_dev);
157+
}
158+
159+
static void vcio_remove(struct platform_device *pdev)
160+
{
161+
struct device *dev = &pdev->dev;
162+
163+
misc_deregister(dev_get_drvdata(dev));
164+
}
165+
166+
static const struct of_device_id vcio_ids[] = {
167+
{ .compatible = "raspberrypi,vcio" },
168+
{ }
169+
};
170+
MODULE_DEVICE_TABLE(of, vcio_ids);
171+
172+
static struct platform_driver vcio_driver = {
173+
.driver = {
174+
.name = MODULE_NAME,
175+
.of_match_table = of_match_ptr(vcio_ids),
176+
},
177+
.probe = vcio_probe,
178+
.remove = vcio_remove,
179+
};
180+
181+
module_platform_driver(vcio_driver);
182+
183+
MODULE_AUTHOR("Gray Girling");
184+
MODULE_AUTHOR("Noralf Trønnes");
185+
MODULE_DESCRIPTION("Mailbox userspace access");
186+
MODULE_LICENSE("GPL");
187+
MODULE_ALIAS("platform:rpi-vcio");

0 commit comments

Comments
 (0)