bcm27xx: add support for linux v5.15
[openwrt/openwrt.git] / target / linux / bcm27xx / patches-5.15 / 950-0530-char-vcio-Rewrite-as-a-firmware-node-child.patch
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
5
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.
9
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.
13
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.:
17
18 KERNEL="vcio", GROUP="video", MODE="0660"
19
20 See: https://github.com/raspberrypi/linux/issues/4620
21
22 Signed-off-by: Phil Elwell <phil@raspberrypi.com>
23 ---
24 drivers/char/broadcom/vcio.c | 133 ++++++++++++++++-------------------
25 1 file changed, 62 insertions(+), 71 deletions(-)
26
27 --- a/drivers/char/broadcom/vcio.c
28 +++ b/drivers/char/broadcom/vcio.c
29 @@ -1,6 +1,7 @@
30 /*
31 * Copyright (C) 2010 Broadcom
32 * Copyright (C) 2015 Noralf Trønnes
33 + * Copyright (C) 2021 Raspberry Pi (Trading) Ltd.
34 *
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
37 @@ -8,8 +9,6 @@
38 *
39 */
40
41 -#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
42 -
43 #include <linux/cdev.h>
44 #include <linux/device.h>
45 #include <linux/fs.h>
46 @@ -19,24 +18,22 @@
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>
52
53 -#define MBOX_CHAN_PROPERTY 8
54 -
55 +#define MODULE_NAME "vcio"
56 #define VCIO_IOC_MAGIC 100
57 #define IOCTL_MBOX_PROPERTY _IOWR(VCIO_IOC_MAGIC, 0, char *)
58 #ifdef CONFIG_COMPAT
59 #define IOCTL_MBOX_PROPERTY32 _IOWR(VCIO_IOC_MAGIC, 0, compat_uptr_t)
60 #endif
61
62 -static struct {
63 - dev_t devt;
64 - struct cdev cdev;
65 - struct class *class;
66 +struct vcio_data {
67 struct rpi_firmware *fw;
68 -} vcio;
69 + struct miscdevice misc_dev;
70 +};
71
72 -static int vcio_user_property_list(void *user)
73 +static int vcio_user_property_list(struct vcio_data *vcio, void *user)
74 {
75 u32 *buf, size;
76 int ret;
77 @@ -55,7 +52,7 @@ static int vcio_user_property_list(void
78 }
79
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);
83 if (ret) {
84 kfree(buf);
85 return ret;
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)
89 {
90 + struct vcio_data *vcio = container_of(file->private_data,
91 + struct vcio_data, misc_dev);
92 +
93 switch (ioctl_num) {
94 case IOCTL_MBOX_PROPERTY:
95 - return vcio_user_property_list((void *)ioctl_param);
96 + return vcio_user_property_list(vcio, (void *)ioctl_param);
97 default:
98 pr_err("unknown ioctl: %x\n", ioctl_num);
99 return -EINVAL;
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)
103 {
104 + struct vcio_data *vcio = container_of(file->private_data,
105 + struct vcio_data, misc_dev);
106 +
107 switch (ioctl_num) {
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));
111 default:
112 pr_err("unknown ioctl: %x\n", ioctl_num);
113 return -EINVAL;
114 @@ -119,77 +122,65 @@ const struct file_operations vcio_fops =
115 .release = vcio_device_release,
116 };
117
118 -static int __init vcio_init(void)
119 +static int vcio_probe(struct platform_device *pdev)
120 {
121 - struct device_node *np;
122 - static struct device *dev;
123 - int ret;
124 -
125 - np = of_find_compatible_node(NULL, NULL,
126 - "raspberrypi,bcm2835-firmware");
127 - if (!of_device_is_available(np))
128 - return -ENODEV;
129 -
130 - vcio.fw = rpi_firmware_get(np);
131 - if (!vcio.fw)
132 - return -ENODEV;
133 -
134 - ret = alloc_chrdev_region(&vcio.devt, 0, 1, "vcio");
135 - if (ret) {
136 - pr_err("failed to allocate device number\n");
137 - return ret;
138 - }
139 -
140 - cdev_init(&vcio.cdev, &vcio_fops);
141 - vcio.cdev.owner = THIS_MODULE;
142 - ret = cdev_add(&vcio.cdev, vcio.devt, 1);
143 - if (ret) {
144 - pr_err("failed to register device\n");
145 - goto err_unregister_chardev;
146 - }
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;
152
153 - /*
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.
157 - */
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");
162 - goto err_cdev_del;
163 + fw_node = of_get_parent(np);
164 + if (!fw_node) {
165 + dev_err(dev, "Missing firmware node\n");
166 + return -ENOENT;
167 }
168
169 - dev = device_create(vcio.class, NULL, vcio.devt, NULL, "vcio");
170 - if (IS_ERR(dev)) {
171 - ret = PTR_ERR(dev);
172 - pr_err("failed to create device\n");
173 - goto err_class_destroy;
174 - }
175 + fw = rpi_firmware_get(fw_node);
176 + of_node_put(fw_node);
177 + if (!fw)
178 + return -EPROBE_DEFER;
179
180 - return 0;
181 + vcio = devm_kzalloc(dev, sizeof(struct vcio_data), GFP_KERNEL);
182 + if (!vcio)
183 + return -ENOMEM;
184
185 -err_class_destroy:
186 - class_destroy(vcio.class);
187 -err_cdev_del:
188 - cdev_del(&vcio.cdev);
189 -err_unregister_chardev:
190 - unregister_chrdev_region(vcio.devt, 1);
191 + vcio->fw = fw;
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;
196
197 - return ret;
198 + return misc_register(&vcio->misc_dev);
199 }
200 -module_init(vcio_init);
201
202 -static void __exit vcio_exit(void)
203 +static int vcio_remove(struct platform_device *pdev)
204 {
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;
210 +
211 + misc_deregister(dev_get_drvdata(dev));
212 + return 0;
213 }
214 -module_exit(vcio_exit);
215 +
216 +static const struct of_device_id vcio_ids[] = {
217 + { .compatible = "raspberrypi,vcio" },
218 + { }
219 +};
220 +MODULE_DEVICE_TABLE(of, vcio_ids);
221 +
222 +static struct platform_driver vcio_driver = {
223 + .driver = {
224 + .name = MODULE_NAME,
225 + .of_match_table = of_match_ptr(vcio_ids),
226 + },
227 + .probe = vcio_probe,
228 + .remove = vcio_remove,
229 +};
230 +
231 +module_platform_driver(vcio_driver);
232
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");