1 From 512d9fadc272271e1e8e9bc81d445883940f81ca Mon Sep 17 00:00:00 2001
2 From: Phil Elwell <phil@raspberrypi.com>
3 Date: Mon, 11 Oct 2021 17:33:05 +0100
4 Subject: [PATCH] char: vcio: Rewrite as a firmware node child
6 The old vcio driver is a simple character device that manually locates
7 the firmware driver. Initialising it before the firmware driver causes
8 a failure, and no retries are attempted.
10 Rewrite vcio as a platform driver that depends on a DT node for its
11 instantiation and the location of the firmware driver, making use of
12 the miscdevice framework to reduce the code size.
14 N.B. Using miscdevice changes the udev SUBSYSTEM string, so a change
15 to the companion udev rule is required in order to continue to set
16 the correct device permissions, e.g.:
18 KERNEL="vcio", GROUP="video", MODE="0660"
20 See: https://github.com/raspberrypi/linux/issues/4620
22 Signed-off-by: Phil Elwell <phil@raspberrypi.com>
24 drivers/char/broadcom/vcio.c | 133 ++++++++++++++++-------------------
25 1 file changed, 62 insertions(+), 71 deletions(-)
27 --- a/drivers/char/broadcom/vcio.c
28 +++ b/drivers/char/broadcom/vcio.c
31 * Copyright (C) 2010 Broadcom
32 * Copyright (C) 2015 Noralf Trønnes
33 + * Copyright (C) 2021 Raspberry Pi (Trading) Ltd.
35 * This program is free software; you can redistribute it and/or modify
36 * it under the terms of the GNU General Public License version 2 as
41 -#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
43 #include <linux/cdev.h>
44 #include <linux/device.h>
47 #include <linux/slab.h>
48 #include <linux/uaccess.h>
49 #include <linux/compat.h>
50 +#include <linux/miscdevice.h>
51 #include <soc/bcm2835/raspberrypi-firmware.h>
53 -#define MBOX_CHAN_PROPERTY 8
55 +#define MODULE_NAME "vcio"
56 #define VCIO_IOC_MAGIC 100
57 #define IOCTL_MBOX_PROPERTY _IOWR(VCIO_IOC_MAGIC, 0, char *)
59 #define IOCTL_MBOX_PROPERTY32 _IOWR(VCIO_IOC_MAGIC, 0, compat_uptr_t)
65 - struct class *class;
67 struct rpi_firmware *fw;
69 + struct miscdevice misc_dev;
72 -static int vcio_user_property_list(void *user)
73 +static int vcio_user_property_list(struct vcio_data *vcio, void *user)
77 @@ -55,7 +52,7 @@ static int vcio_user_property_list(void
80 /* Strip off protocol encapsulation */
81 - ret = rpi_firmware_property_list(vcio.fw, &buf[2], size - 12);
82 + ret = rpi_firmware_property_list(vcio->fw, &buf[2], size - 12);
86 @@ -87,9 +84,12 @@ static int vcio_device_release(struct in
87 static long vcio_device_ioctl(struct file *file, unsigned int ioctl_num,
88 unsigned long ioctl_param)
90 + struct vcio_data *vcio = container_of(file->private_data,
91 + struct vcio_data, misc_dev);
94 case IOCTL_MBOX_PROPERTY:
95 - return vcio_user_property_list((void *)ioctl_param);
96 + return vcio_user_property_list(vcio, (void *)ioctl_param);
98 pr_err("unknown ioctl: %x\n", ioctl_num);
100 @@ -100,9 +100,12 @@ static long vcio_device_ioctl(struct fil
101 static long vcio_device_compat_ioctl(struct file *file, unsigned int ioctl_num,
102 unsigned long ioctl_param)
104 + struct vcio_data *vcio = container_of(file->private_data,
105 + struct vcio_data, misc_dev);
108 case IOCTL_MBOX_PROPERTY32:
109 - return vcio_user_property_list(compat_ptr(ioctl_param));
110 + return vcio_user_property_list(vcio, compat_ptr(ioctl_param));
112 pr_err("unknown ioctl: %x\n", ioctl_num);
114 @@ -119,77 +122,65 @@ const struct file_operations vcio_fops =
115 .release = vcio_device_release,
118 -static int __init vcio_init(void)
119 +static int vcio_probe(struct platform_device *pdev)
121 - struct device_node *np;
122 - static struct device *dev;
125 - np = of_find_compatible_node(NULL, NULL,
126 - "raspberrypi,bcm2835-firmware");
127 - if (!of_device_is_available(np))
130 - vcio.fw = rpi_firmware_get(np);
134 - ret = alloc_chrdev_region(&vcio.devt, 0, 1, "vcio");
136 - pr_err("failed to allocate device number\n");
140 - cdev_init(&vcio.cdev, &vcio_fops);
141 - vcio.cdev.owner = THIS_MODULE;
142 - ret = cdev_add(&vcio.cdev, vcio.devt, 1);
144 - pr_err("failed to register device\n");
145 - goto err_unregister_chardev;
147 + struct device *dev = &pdev->dev;
148 + struct device_node *np = dev->of_node;
149 + struct device_node *fw_node;
150 + struct rpi_firmware *fw;
151 + struct vcio_data *vcio;
154 - * Create sysfs entries
155 - * 'bcm2708_vcio' is used for backwards compatibility so we don't break
156 - * userspace. Raspian has a udev rule that changes the permissions.
158 - vcio.class = class_create(THIS_MODULE, "bcm2708_vcio");
159 - if (IS_ERR(vcio.class)) {
160 - ret = PTR_ERR(vcio.class);
161 - pr_err("failed to create class\n");
163 + fw_node = of_get_parent(np);
165 + dev_err(dev, "Missing firmware node\n");
169 - dev = device_create(vcio.class, NULL, vcio.devt, NULL, "vcio");
171 - ret = PTR_ERR(dev);
172 - pr_err("failed to create device\n");
173 - goto err_class_destroy;
175 + fw = rpi_firmware_get(fw_node);
176 + of_node_put(fw_node);
178 + return -EPROBE_DEFER;
181 + vcio = devm_kzalloc(dev, sizeof(struct vcio_data), GFP_KERNEL);
186 - class_destroy(vcio.class);
188 - cdev_del(&vcio.cdev);
189 -err_unregister_chardev:
190 - unregister_chrdev_region(vcio.devt, 1);
192 + vcio->misc_dev.fops = &vcio_fops;
193 + vcio->misc_dev.minor = MISC_DYNAMIC_MINOR;
194 + vcio->misc_dev.name = "vcio";
195 + vcio->misc_dev.parent = dev;
198 + return misc_register(&vcio->misc_dev);
200 -module_init(vcio_init);
202 -static void __exit vcio_exit(void)
203 +static int vcio_remove(struct platform_device *pdev)
205 - device_destroy(vcio.class, vcio.devt);
206 - class_destroy(vcio.class);
207 - cdev_del(&vcio.cdev);
208 - unregister_chrdev_region(vcio.devt, 1);
209 + struct device *dev = &pdev->dev;
211 + misc_deregister(dev_get_drvdata(dev));
214 -module_exit(vcio_exit);
216 +static const struct of_device_id vcio_ids[] = {
217 + { .compatible = "raspberrypi,vcio" },
220 +MODULE_DEVICE_TABLE(of, vcio_ids);
222 +static struct platform_driver vcio_driver = {
224 + .name = MODULE_NAME,
225 + .of_match_table = of_match_ptr(vcio_ids),
227 + .probe = vcio_probe,
228 + .remove = vcio_remove,
231 +module_platform_driver(vcio_driver);
233 MODULE_AUTHOR("Gray Girling");
234 MODULE_AUTHOR("Noralf Trønnes");
235 MODULE_DESCRIPTION("Mailbox userspace access");
236 MODULE_LICENSE("GPL");
237 +MODULE_ALIAS("platform:rpi-vcio");