2023-01-19 12:04:17

by Umang Jain

[permalink] [raw]
Subject: [PATCH v5 0/6] staging: vc04_services: vchiq: Register devices with a custom bus_type

The main patch (6/6) is laregly unchanged from v3.
Specific details are elaborated in its commit message.

This series just introduces five extra patches for dropping include
directives from Makefiles (suggested by Greg KH) and rebased.

Changes in v5:
- Fixup missing "staging: " in commits' subject line
- No code changes from v4

Changes in v4:
- Introduce patches to drop include directives from Makefile

Changes in v3:
- Rework entirely to replace platform devices/driver model

-v2:
https://lore.kernel.org/all/[email protected]/

-v1:
https://lore.kernel.org/all/[email protected]/

Umang Jain (6):
staging: vc04_services: Drop __VCCOREVER__ remnants
staging: vc04_services: bcm2835-audio: Drop include Makefile directive
staging: vc04_services: bcm2835-camera: Drop include Makefile
directive
staging: vc04_services: vchiq-mmal: Drop include Makefile directive
staging: vc04_services: interface: Drop include Makefile directive
staging: vc04_services: vchiq: Register devices with a custom bus_type

drivers/staging/vc04_services/Makefile | 2 -
.../vc04_services/bcm2835-audio/Makefile | 2 -
.../vc04_services/bcm2835-audio/bcm2835.c | 19 ++-
.../vc04_services/bcm2835-audio/bcm2835.h | 3 +-
.../vc04_services/bcm2835-camera/Makefile | 5 -
.../bcm2835-camera/bcm2835-camera.c | 27 ++--
.../vc04_services/bcm2835-camera/controls.c | 6 +-
.../interface/vchiq_arm/vchiq_arm.c | 121 +++++++++++++++---
.../interface/vchiq_arm/vchiq_arm.h | 1 +
.../interface/vchiq_arm/vchiq_core.h | 2 +-
.../interface/vchiq_arm/vchiq_ioctl.h | 3 +-
.../staging/vc04_services/vchiq-mmal/Makefile | 5 -
.../vc04_services/vchiq-mmal/mmal-vchiq.c | 2 +-
13 files changed, 131 insertions(+), 67 deletions(-)

--
2.38.1


2023-01-19 12:05:47

by Umang Jain

[permalink] [raw]
Subject: [PATCH v5 2/6] staging: vc04_services: bcm2835-audio: Drop include Makefile directive

Drop the include directive they can break the build one only wants to
build a subdirectory. Replace with "../" for the includes, in the
bcm2835.h instead.

Suggested-by: Greg Kroah-Hartman <[email protected]>
Signed-off-by: Umang Jain <[email protected]>
---
drivers/staging/vc04_services/bcm2835-audio/Makefile | 2 --
drivers/staging/vc04_services/bcm2835-audio/bcm2835.h | 3 ++-
2 files changed, 2 insertions(+), 3 deletions(-)

diff --git a/drivers/staging/vc04_services/bcm2835-audio/Makefile b/drivers/staging/vc04_services/bcm2835-audio/Makefile
index fc7ac6112a3e..01ceebdf88e7 100644
--- a/drivers/staging/vc04_services/bcm2835-audio/Makefile
+++ b/drivers/staging/vc04_services/bcm2835-audio/Makefile
@@ -1,5 +1,3 @@
# SPDX-License-Identifier: GPL-2.0
obj-$(CONFIG_SND_BCM2835) += snd-bcm2835.o
snd-bcm2835-objs := bcm2835.o bcm2835-ctl.o bcm2835-pcm.o bcm2835-vchiq.o
-
-ccflags-y += -I $(srctree)/$(src)/../include
diff --git a/drivers/staging/vc04_services/bcm2835-audio/bcm2835.h b/drivers/staging/vc04_services/bcm2835-audio/bcm2835.h
index 38b7451d77b2..0a81383c475a 100644
--- a/drivers/staging/vc04_services/bcm2835-audio/bcm2835.h
+++ b/drivers/staging/vc04_services/bcm2835-audio/bcm2835.h
@@ -6,11 +6,12 @@

#include <linux/device.h>
#include <linux/wait.h>
-#include <linux/raspberrypi/vchiq.h>
#include <sound/core.h>
#include <sound/pcm.h>
#include <sound/pcm-indirect.h>

+#include "../include/linux/raspberrypi/vchiq.h"
+
#define MAX_SUBSTREAMS (8)
#define AVAIL_SUBSTREAMS_MASK (0xff)

--
2.38.1

2023-01-19 12:06:35

by Umang Jain

[permalink] [raw]
Subject: [PATCH v5 4/6] staging: vc04_services: vchiq-mmal: Drop include Makefile directive

Drop the include directive they can break the build one only wants to
build a subdirectory. Replace with "../" for the includes, in the
mmal-vchiq.c instead.

Suggested-by: Greg Kroah-Hartman <[email protected]>
Signed-off-by: Umang Jain <[email protected]>
---
drivers/staging/vc04_services/vchiq-mmal/Makefile | 4 ----
drivers/staging/vc04_services/vchiq-mmal/mmal-vchiq.c | 2 +-
2 files changed, 1 insertion(+), 5 deletions(-)

diff --git a/drivers/staging/vc04_services/vchiq-mmal/Makefile b/drivers/staging/vc04_services/vchiq-mmal/Makefile
index c7d3b06e17ce..6937f6534c26 100644
--- a/drivers/staging/vc04_services/vchiq-mmal/Makefile
+++ b/drivers/staging/vc04_services/vchiq-mmal/Makefile
@@ -2,7 +2,3 @@
bcm2835-mmal-vchiq-objs := mmal-vchiq.o

obj-$(CONFIG_BCM2835_VCHIQ_MMAL) += bcm2835-mmal-vchiq.o
-
-ccflags-y += \
- -I$(srctree)/$(src)/.. \
- -I$(srctree)/$(src)/../include
diff --git a/drivers/staging/vc04_services/vchiq-mmal/mmal-vchiq.c b/drivers/staging/vc04_services/vchiq-mmal/mmal-vchiq.c
index 6b5879a33780..234e3764db64 100644
--- a/drivers/staging/vc04_services/vchiq-mmal/mmal-vchiq.c
+++ b/drivers/staging/vc04_services/vchiq-mmal/mmal-vchiq.c
@@ -23,9 +23,9 @@
#include <linux/slab.h>
#include <linux/completion.h>
#include <linux/vmalloc.h>
-#include <linux/raspberrypi/vchiq.h>
#include <media/videobuf2-vmalloc.h>

+#include "../include/linux/raspberrypi/vchiq.h"
#include "mmal-common.h"
#include "mmal-vchiq.h"
#include "mmal-msg.h"
--
2.38.1

2023-01-19 12:07:42

by Umang Jain

[permalink] [raw]
Subject: [PATCH v5 1/6] staging: vc04_services: Drop __VCCOREVER__ remnants

Commit 8ba5f91bab63("staging: vc04_services: remove __VCCOREVER__")
was meant to remove all of __VCCOREVER__ definitions but missed to
remove a few. Hence, drop them now.

Signed-off-by: Umang Jain <[email protected]>
---
drivers/staging/vc04_services/bcm2835-audio/Makefile | 2 +-
drivers/staging/vc04_services/bcm2835-camera/Makefile | 3 +--
drivers/staging/vc04_services/vchiq-mmal/Makefile | 3 +--
3 files changed, 3 insertions(+), 5 deletions(-)

diff --git a/drivers/staging/vc04_services/bcm2835-audio/Makefile b/drivers/staging/vc04_services/bcm2835-audio/Makefile
index d59fe4dde615..fc7ac6112a3e 100644
--- a/drivers/staging/vc04_services/bcm2835-audio/Makefile
+++ b/drivers/staging/vc04_services/bcm2835-audio/Makefile
@@ -2,4 +2,4 @@
obj-$(CONFIG_SND_BCM2835) += snd-bcm2835.o
snd-bcm2835-objs := bcm2835.o bcm2835-ctl.o bcm2835-pcm.o bcm2835-vchiq.o

-ccflags-y += -I $(srctree)/$(src)/../include -D__VCCOREVER__=0x04000000
+ccflags-y += -I $(srctree)/$(src)/../include
diff --git a/drivers/staging/vc04_services/bcm2835-camera/Makefile b/drivers/staging/vc04_services/bcm2835-camera/Makefile
index 3a76d6ade428..3494c82b271a 100644
--- a/drivers/staging/vc04_services/bcm2835-camera/Makefile
+++ b/drivers/staging/vc04_services/bcm2835-camera/Makefile
@@ -7,5 +7,4 @@ obj-$(CONFIG_VIDEO_BCM2835) += bcm2835-v4l2.o

ccflags-y += \
-I $(srctree)/$(src)/.. \
- -I $(srctree)/$(src)/../vchiq-mmal/ \
- -D__VCCOREVER__=0x04000000
+ -I $(srctree)/$(src)/../vchiq-mmal/
diff --git a/drivers/staging/vc04_services/vchiq-mmal/Makefile b/drivers/staging/vc04_services/vchiq-mmal/Makefile
index b2a830f48acc..c7d3b06e17ce 100644
--- a/drivers/staging/vc04_services/vchiq-mmal/Makefile
+++ b/drivers/staging/vc04_services/vchiq-mmal/Makefile
@@ -5,5 +5,4 @@ obj-$(CONFIG_BCM2835_VCHIQ_MMAL) += bcm2835-mmal-vchiq.o

ccflags-y += \
-I$(srctree)/$(src)/.. \
- -I$(srctree)/$(src)/../include \
- -D__VCCOREVER__=0x04000000
+ -I$(srctree)/$(src)/../include
--
2.38.1

2023-01-19 12:18:04

by Umang Jain

[permalink] [raw]
Subject: [PATCH v5 5/6] staging: vc04_services: interface: Drop include Makefile directive

Drop the include directive they can break the build one only wants to
build a subdirectory. Replace with "../" for the includes, in the
interface/ files instead.

Suggested-by: Greg Kroah-Hartman <[email protected]>
Signed-off-by: Umang Jain <[email protected]>
---
drivers/staging/vc04_services/Makefile | 2 --
drivers/staging/vc04_services/interface/vchiq_arm/vchiq_core.h | 2 +-
.../staging/vc04_services/interface/vchiq_arm/vchiq_ioctl.h | 3 ++-
3 files changed, 3 insertions(+), 4 deletions(-)

diff --git a/drivers/staging/vc04_services/Makefile b/drivers/staging/vc04_services/Makefile
index 1fd191e2e2a5..44794bdf6173 100644
--- a/drivers/staging/vc04_services/Makefile
+++ b/drivers/staging/vc04_services/Makefile
@@ -15,5 +15,3 @@ obj-$(CONFIG_SND_BCM2835) += bcm2835-audio/
obj-$(CONFIG_VIDEO_BCM2835) += bcm2835-camera/
obj-$(CONFIG_BCM2835_VCHIQ_MMAL) += vchiq-mmal/

-ccflags-y += -I $(srctree)/$(src)/include
-
diff --git a/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_core.h b/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_core.h
index 3c198eb1c77a..ec1a3caefaea 100644
--- a/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_core.h
+++ b/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_core.h
@@ -10,8 +10,8 @@
#include <linux/kref.h>
#include <linux/rcupdate.h>
#include <linux/wait.h>
-#include <linux/raspberrypi/vchiq.h>

+#include "../../include/linux/raspberrypi/vchiq.h"
#include "vchiq_cfg.h"

/* Do this so that we can test-build the code on non-rpi systems */
diff --git a/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_ioctl.h b/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_ioctl.h
index 96f50beace44..17550831f86c 100644
--- a/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_ioctl.h
+++ b/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_ioctl.h
@@ -5,7 +5,8 @@
#define VCHIQ_IOCTLS_H

#include <linux/ioctl.h>
-#include <linux/raspberrypi/vchiq.h>
+
+#include "../../include/linux/raspberrypi/vchiq.h"

#define VCHIQ_IOC_MAGIC 0xc4
#define VCHIQ_INVALID_HANDLE (~0)
--
2.38.1

2023-01-19 12:18:55

by Umang Jain

[permalink] [raw]
Subject: [PATCH v5 6/6] staging: vc04_services: vchiq: Register devices with a custom bus_type

The devices that the vchiq interface registers(bcm2835-audio,
bcm2835-camera) are implemented and exposed by the VC04 firmware.
The device tree describes the VC04 itself with the resources
required to communicate with it through a mailbox interface. However,
the vchiq interface registers these devices as platform devices. This
also means the specific drivers for these devices are also getting
registered as platform drivers. This is not correct and a blatant
abuse of platform device/driver.

Replace the platform device/driver model with a standard device driver
model. A custom bus_type, vchiq_bus_type, is created in the vchiq
interface which matches the devices to their specific device drivers
thereby, establishing driver binding. A struct vchiq_device wraps the
struct device for each device being registered on the bus by the vchiq
interface.

Each device registered will expose a 'name' read-only device attribute
in sysfs (/sys/bus/vchiq-bus/devices). New devices and drivers can be
added by registering on vchiq_bus_type and adding a corresponding
device name entry in the static list of devices, vchiq_devices. There
is currently no way to enumerate the VCHIQ devices that are available
from the firmware.

Signed-off-by: Umang Jain <[email protected]>
---
.../vc04_services/bcm2835-audio/bcm2835.c | 19 ++-
.../bcm2835-camera/bcm2835-camera.c | 17 ++-
.../interface/vchiq_arm/vchiq_arm.c | 121 +++++++++++++++---
.../interface/vchiq_arm/vchiq_arm.h | 1 +
4 files changed, 117 insertions(+), 41 deletions(-)

diff --git a/drivers/staging/vc04_services/bcm2835-audio/bcm2835.c b/drivers/staging/vc04_services/bcm2835-audio/bcm2835.c
index 00bc898b0189..9f3af84f5d5d 100644
--- a/drivers/staging/vc04_services/bcm2835-audio/bcm2835.c
+++ b/drivers/staging/vc04_services/bcm2835-audio/bcm2835.c
@@ -1,12 +1,11 @@
// SPDX-License-Identifier: GPL-2.0
/* Copyright 2011 Broadcom Corporation. All rights reserved. */

-#include <linux/platform_device.h>
-
#include <linux/init.h>
#include <linux/slab.h>
#include <linux/module.h>

+#include "../interface/vchiq_arm/vchiq_arm.h"
#include "bcm2835.h"

static bool enable_hdmi;
@@ -268,9 +267,8 @@ static int snd_add_child_devices(struct device *device, u32 numchans)
return 0;
}

-static int snd_bcm2835_alsa_probe(struct platform_device *pdev)
+static int snd_bcm2835_alsa_probe(struct device *dev)
{
- struct device *dev = &pdev->dev;
int err;

if (num_channels <= 0 || num_channels > MAX_SUBSTREAMS) {
@@ -292,30 +290,29 @@ static int snd_bcm2835_alsa_probe(struct platform_device *pdev)

#ifdef CONFIG_PM

-static int snd_bcm2835_alsa_suspend(struct platform_device *pdev,
+static int snd_bcm2835_alsa_suspend(struct device *pdev,
pm_message_t state)
{
return 0;
}

-static int snd_bcm2835_alsa_resume(struct platform_device *pdev)
+static int snd_bcm2835_alsa_resume(struct device *pdev)
{
return 0;
}

#endif

-static struct platform_driver bcm2835_alsa_driver = {
+static struct device_driver bcm2835_alsa_driver = {
.probe = snd_bcm2835_alsa_probe,
#ifdef CONFIG_PM
.suspend = snd_bcm2835_alsa_suspend,
.resume = snd_bcm2835_alsa_resume,
#endif
- .driver = {
- .name = "bcm2835_audio",
- },
+ .name = "bcm2835_audio",
+ .bus = &vchiq_bus_type,
};
-module_platform_driver(bcm2835_alsa_driver);
+module_driver(bcm2835_alsa_driver, driver_register, driver_unregister);

MODULE_AUTHOR("Dom Cobley");
MODULE_DESCRIPTION("Alsa driver for BCM2835 chip");
diff --git a/drivers/staging/vc04_services/bcm2835-camera/bcm2835-camera.c b/drivers/staging/vc04_services/bcm2835-camera/bcm2835-camera.c
index 4f81765912ea..199a49f9ec1e 100644
--- a/drivers/staging/vc04_services/bcm2835-camera/bcm2835-camera.c
+++ b/drivers/staging/vc04_services/bcm2835-camera/bcm2835-camera.c
@@ -24,8 +24,8 @@
#include <media/v4l2-event.h>
#include <media/v4l2-common.h>
#include <linux/delay.h>
-#include <linux/platform_device.h>

+#include "../interface/vchiq_arm/vchiq_arm.h"
#include "../vchiq-mmal/mmal-common.h"
#include "../vchiq-mmal/mmal-encodings.h"
#include "../vchiq-mmal/mmal-vchiq.h"
@@ -1841,7 +1841,7 @@ static struct v4l2_format default_v4l2_format = {
.fmt.pix.sizeimage = 1024 * 768,
};

-static int bcm2835_mmal_probe(struct platform_device *pdev)
+static int bcm2835_mmal_probe(struct device *device)
{
int ret;
struct bcm2835_mmal_dev *dev;
@@ -1896,7 +1896,7 @@ static int bcm2835_mmal_probe(struct platform_device *pdev)
&camera_instance);
ret = v4l2_device_register(NULL, &dev->v4l2_dev);
if (ret) {
- dev_err(&pdev->dev, "%s: could not register V4L2 device: %d\n",
+ dev_err(device, "%s: could not register V4L2 device: %d\n",
__func__, ret);
goto free_dev;
}
@@ -1976,7 +1976,7 @@ static int bcm2835_mmal_probe(struct platform_device *pdev)
return ret;
}

-static int bcm2835_mmal_remove(struct platform_device *pdev)
+static int bcm2835_mmal_remove(struct device *device)
{
int camera;
struct vchiq_mmal_instance *instance = gdev[0]->instance;
@@ -1990,15 +1990,14 @@ static int bcm2835_mmal_remove(struct platform_device *pdev)
return 0;
}

-static struct platform_driver bcm2835_camera_driver = {
+static struct device_driver bcm2835_camera_driver = {
+ .name = "bcm2835-camera",
.probe = bcm2835_mmal_probe,
.remove = bcm2835_mmal_remove,
- .driver = {
- .name = "bcm2835-camera",
- },
+ .bus = &vchiq_bus_type,
};

-module_platform_driver(bcm2835_camera_driver)
+module_driver(bcm2835_camera_driver, driver_register, driver_unregister)

MODULE_DESCRIPTION("Broadcom 2835 MMAL video capture");
MODULE_AUTHOR("Vincent Sanders");
diff --git a/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.c b/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.c
index 22de23f3af02..86c8e5df7cf6 100644
--- a/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.c
+++ b/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.c
@@ -12,6 +12,8 @@
#include <linux/cdev.h>
#include <linux/fs.h>
#include <linux/device.h>
+#include <linux/device/bus.h>
+#include <linux/string.h>
#include <linux/mm.h>
#include <linux/highmem.h>
#include <linux/pagemap.h>
@@ -65,9 +67,6 @@ int vchiq_susp_log_level = VCHIQ_LOG_ERROR;
DEFINE_SPINLOCK(msg_queue_spinlock);
struct vchiq_state g_state;

-static struct platform_device *bcm2835_camera;
-static struct platform_device *bcm2835_audio;
-
struct vchiq_drvdata {
const unsigned int cache_line_size;
struct rpi_firmware *fw;
@@ -132,6 +131,51 @@ struct vchiq_pagelist_info {
unsigned int scatterlist_mapped;
};

+struct vchiq_device {
+ const char *name;
+ struct device dev;
+};
+
+static ssize_t vchiq_dev_show(struct device *dev,
+ struct device_attribute *attr, char *buf)
+{
+ struct vchiq_device *device = container_of(dev, struct vchiq_device, dev);
+
+ return sprintf(buf, "%s", device->name);
+}
+
+static DEVICE_ATTR_RO(vchiq_dev);
+
+static struct attribute *vchiq_dev_attrs[] = {
+ &dev_attr_vchiq_dev.attr,
+ NULL
+};
+
+ATTRIBUTE_GROUPS(vchiq_dev);
+
+static const struct device_type vchiq_device_type = {
+ .groups = vchiq_dev_groups
+};
+
+static int vchiq_bus_type_match(struct device *dev, struct device_driver *drv)
+{
+ if (dev->bus == &vchiq_bus_type &&
+ strcmp(dev_name(dev), drv->name) == 0)
+ return 1;
+ return 0;
+}
+
+struct bus_type vchiq_bus_type = {
+ .name = "vchiq-bus",
+ .match = vchiq_bus_type_match,
+};
+EXPORT_SYMBOL(vchiq_bus_type);
+
+static const char *const vchiq_devices[] = {
+ "bcm2835_audio",
+ "bcm2835-camera",
+};
+
static void __iomem *g_regs;
/* This value is the size of the L2 cache lines as understood by the
* VPU firmware, which determines the required alignment of the
@@ -1763,26 +1807,52 @@ static const struct of_device_id vchiq_of_match[] = {
};
MODULE_DEVICE_TABLE(of, vchiq_of_match);

-static struct platform_device *
+static void
+vchiq_release_device(struct device *dev)
+{
+ struct vchiq_device *device;
+
+ device = container_of(dev, struct vchiq_device, dev);
+ kfree(device);
+}
+
+static int
vchiq_register_child(struct platform_device *pdev, const char *name)
{
- struct platform_device_info pdevinfo;
- struct platform_device *child;
+ struct vchiq_device *device = NULL;
+ int ret;

- memset(&pdevinfo, 0, sizeof(pdevinfo));
+ device = kzalloc(sizeof(*device), GFP_KERNEL);
+ if (!device)
+ return -ENOMEM;

- pdevinfo.parent = &pdev->dev;
- pdevinfo.name = name;
- pdevinfo.id = PLATFORM_DEVID_NONE;
- pdevinfo.dma_mask = DMA_BIT_MASK(32);
+ device->name = name;
+ device->dev.init_name = name;
+ device->dev.parent = &pdev->dev;
+ device->dev.bus = &vchiq_bus_type;
+ device->dev.type = &vchiq_device_type;
+ device->dev.release = vchiq_release_device;
+
+ ret = dma_set_mask_and_coherent(&device->dev, DMA_BIT_MASK(32));
+ if (ret < 0) {
+ vchiq_release_device(&device->dev);
+ return ret;
+ }

- child = platform_device_register_full(&pdevinfo);
- if (IS_ERR(child)) {
- dev_warn(&pdev->dev, "%s not registered\n", name);
- child = NULL;
+ ret = device_register(&device->dev);
+ if (ret) {
+ put_device(&device->dev);
+ return -EINVAL;
}

- return child;
+ return 0;
+}
+
+static int
+vchiq_unregister_child(struct device *dev, void *data)
+{
+ device_unregister(dev);
+ return 0;
}

static int vchiq_probe(struct platform_device *pdev)
@@ -1790,7 +1860,7 @@ static int vchiq_probe(struct platform_device *pdev)
struct device_node *fw_node;
const struct of_device_id *of_id;
struct vchiq_drvdata *drvdata;
- int err;
+ int i, err;

of_id = of_match_node(vchiq_of_match, pdev->dev.of_node);
drvdata = (struct vchiq_drvdata *)of_id->data;
@@ -1832,8 +1902,12 @@ static int vchiq_probe(struct platform_device *pdev)
goto error_exit;
}

- bcm2835_camera = vchiq_register_child(pdev, "bcm2835-camera");
- bcm2835_audio = vchiq_register_child(pdev, "bcm2835_audio");
+ for (i = 0; i < ARRAY_SIZE(vchiq_devices); i++) {
+ err = vchiq_register_child(pdev, vchiq_devices[i]);
+ if (!err)
+ dev_err(&pdev->dev, "Failed to register %s vchiq device\n",
+ vchiq_devices[i]);
+ }

return 0;

@@ -1845,8 +1919,8 @@ static int vchiq_probe(struct platform_device *pdev)

static int vchiq_remove(struct platform_device *pdev)
{
- platform_device_unregister(bcm2835_audio);
- platform_device_unregister(bcm2835_camera);
+ bus_for_each_dev(&vchiq_bus_type, NULL, NULL, vchiq_unregister_child);
+
vchiq_debugfs_deinit();
vchiq_deregister_chrdev();

@@ -1866,6 +1940,10 @@ static int __init vchiq_driver_init(void)
{
int ret;

+ ret = bus_register(&vchiq_bus_type);
+ if (ret)
+ pr_err("Failed to register %s\n", vchiq_bus_type.name);
+
ret = platform_driver_register(&vchiq_driver);
if (ret)
pr_err("Failed to register vchiq driver\n");
@@ -1876,6 +1954,7 @@ module_init(vchiq_driver_init);

static void __exit vchiq_driver_exit(void)
{
+ bus_unregister(&vchiq_bus_type);
platform_driver_unregister(&vchiq_driver);
}
module_exit(vchiq_driver_exit);
diff --git a/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.h b/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.h
index 2fb31f9b527f..98c3af32774a 100644
--- a/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.h
+++ b/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.h
@@ -81,6 +81,7 @@ extern int vchiq_susp_log_level;

extern spinlock_t msg_queue_spinlock;
extern struct vchiq_state g_state;
+extern struct bus_type vchiq_bus_type;

extern struct vchiq_state *
vchiq_get_state(void);
--
2.38.1

2023-01-19 12:23:00

by Umang Jain

[permalink] [raw]
Subject: [PATCH v5 3/6] staging: vc04_services: bcm2835-camera: Drop include Makefile directive

Drop the include directive they can break the build one only wants to
build a subdirectory. Replace with "../" for the includes, in the
bcm2835-camera files instead.

Suggested-by: Greg Kroah-Hartman <[email protected]>
Signed-off-by: Umang Jain <[email protected]>
---
drivers/staging/vc04_services/bcm2835-camera/Makefile | 4 ----
.../vc04_services/bcm2835-camera/bcm2835-camera.c | 10 +++++-----
.../staging/vc04_services/bcm2835-camera/controls.c | 6 +++---
3 files changed, 8 insertions(+), 12 deletions(-)

diff --git a/drivers/staging/vc04_services/bcm2835-camera/Makefile b/drivers/staging/vc04_services/bcm2835-camera/Makefile
index 3494c82b271a..203b93899b20 100644
--- a/drivers/staging/vc04_services/bcm2835-camera/Makefile
+++ b/drivers/staging/vc04_services/bcm2835-camera/Makefile
@@ -4,7 +4,3 @@ bcm2835-v4l2-$(CONFIG_VIDEO_BCM2835) := \
controls.o

obj-$(CONFIG_VIDEO_BCM2835) += bcm2835-v4l2.o
-
-ccflags-y += \
- -I $(srctree)/$(src)/.. \
- -I $(srctree)/$(src)/../vchiq-mmal/
diff --git a/drivers/staging/vc04_services/bcm2835-camera/bcm2835-camera.c b/drivers/staging/vc04_services/bcm2835-camera/bcm2835-camera.c
index 797ebe2a973a..4f81765912ea 100644
--- a/drivers/staging/vc04_services/bcm2835-camera/bcm2835-camera.c
+++ b/drivers/staging/vc04_services/bcm2835-camera/bcm2835-camera.c
@@ -26,11 +26,11 @@
#include <linux/delay.h>
#include <linux/platform_device.h>

-#include "mmal-common.h"
-#include "mmal-encodings.h"
-#include "mmal-vchiq.h"
-#include "mmal-msg.h"
-#include "mmal-parameters.h"
+#include "../vchiq-mmal/mmal-common.h"
+#include "../vchiq-mmal/mmal-encodings.h"
+#include "../vchiq-mmal/mmal-vchiq.h"
+#include "../vchiq-mmal/mmal-msg.h"
+#include "../vchiq-mmal/mmal-parameters.h"
#include "bcm2835-camera.h"

#define MIN_WIDTH 32
diff --git a/drivers/staging/vc04_services/bcm2835-camera/controls.c b/drivers/staging/vc04_services/bcm2835-camera/controls.c
index 5644d1d457b9..6bce45925bf1 100644
--- a/drivers/staging/vc04_services/bcm2835-camera/controls.c
+++ b/drivers/staging/vc04_services/bcm2835-camera/controls.c
@@ -23,9 +23,9 @@
#include <media/v4l2-event.h>
#include <media/v4l2-common.h>

-#include "mmal-common.h"
-#include "mmal-vchiq.h"
-#include "mmal-parameters.h"
+#include "../vchiq-mmal/mmal-common.h"
+#include "../vchiq-mmal/mmal-vchiq.h"
+#include "../vchiq-mmal/mmal-parameters.h"
#include "bcm2835-camera.h"

/* The supported V4L2_CID_AUTO_EXPOSURE_BIAS values are from -4.0 to +4.0.
--
2.38.1

2023-01-20 01:44:53

by Laurent Pinchart

[permalink] [raw]
Subject: Re: [PATCH v5 5/6] staging: vc04_services: interface: Drop include Makefile directive

Hi Umang,

Thank you for the patch.

On Thu, Jan 19, 2023 at 05:25:02PM +0530, Umang Jain wrote:
> Drop the include directive they can break the build one only wants to
> build a subdirectory. Replace with "../" for the includes, in the
> interface/ files instead.
>
> Suggested-by: Greg Kroah-Hartman <[email protected]>
> Signed-off-by: Umang Jain <[email protected]>
> ---
> drivers/staging/vc04_services/Makefile | 2 --
> drivers/staging/vc04_services/interface/vchiq_arm/vchiq_core.h | 2 +-
> .../staging/vc04_services/interface/vchiq_arm/vchiq_ioctl.h | 3 ++-
> 3 files changed, 3 insertions(+), 4 deletions(-)
>
> diff --git a/drivers/staging/vc04_services/Makefile b/drivers/staging/vc04_services/Makefile
> index 1fd191e2e2a5..44794bdf6173 100644
> --- a/drivers/staging/vc04_services/Makefile
> +++ b/drivers/staging/vc04_services/Makefile
> @@ -15,5 +15,3 @@ obj-$(CONFIG_SND_BCM2835) += bcm2835-audio/
> obj-$(CONFIG_VIDEO_BCM2835) += bcm2835-camera/
> obj-$(CONFIG_BCM2835_VCHIQ_MMAL) += vchiq-mmal/
>
> -ccflags-y += -I $(srctree)/$(src)/include
> -

Same comment as for 2/6. Personally, I think I'd prefer dropping 2/6,
4/6 and 5/6 until it's time to move vchiq out of staging, at which point
the ccflags-y will just be dropped, without requiring other changes.

> diff --git a/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_core.h b/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_core.h
> index 3c198eb1c77a..ec1a3caefaea 100644
> --- a/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_core.h
> +++ b/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_core.h
> @@ -10,8 +10,8 @@
> #include <linux/kref.h>
> #include <linux/rcupdate.h>
> #include <linux/wait.h>
> -#include <linux/raspberrypi/vchiq.h>
>
> +#include "../../include/linux/raspberrypi/vchiq.h"
> #include "vchiq_cfg.h"
>
> /* Do this so that we can test-build the code on non-rpi systems */
> diff --git a/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_ioctl.h b/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_ioctl.h
> index 96f50beace44..17550831f86c 100644
> --- a/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_ioctl.h
> +++ b/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_ioctl.h
> @@ -5,7 +5,8 @@
> #define VCHIQ_IOCTLS_H
>
> #include <linux/ioctl.h>
> -#include <linux/raspberrypi/vchiq.h>
> +
> +#include "../../include/linux/raspberrypi/vchiq.h"
>
> #define VCHIQ_IOC_MAGIC 0xc4
> #define VCHIQ_INVALID_HANDLE (~0)

--
Regards,

Laurent Pinchart

2023-01-20 02:07:00

by Laurent Pinchart

[permalink] [raw]
Subject: Re: [PATCH v5 1/6] staging: vc04_services: Drop __VCCOREVER__ remnants

Hi Umang,

Thank you for the patch.

On Thu, Jan 19, 2023 at 05:24:58PM +0530, Umang Jain wrote:
> Commit 8ba5f91bab63("staging: vc04_services: remove __VCCOREVER__")

With a space before '(',

Reviewed-by: Laurent Pinchart <[email protected]>

> was meant to remove all of __VCCOREVER__ definitions but missed to
> remove a few. Hence, drop them now.
>
> Signed-off-by: Umang Jain <[email protected]>
> ---
> drivers/staging/vc04_services/bcm2835-audio/Makefile | 2 +-
> drivers/staging/vc04_services/bcm2835-camera/Makefile | 3 +--
> drivers/staging/vc04_services/vchiq-mmal/Makefile | 3 +--
> 3 files changed, 3 insertions(+), 5 deletions(-)
>
> diff --git a/drivers/staging/vc04_services/bcm2835-audio/Makefile b/drivers/staging/vc04_services/bcm2835-audio/Makefile
> index d59fe4dde615..fc7ac6112a3e 100644
> --- a/drivers/staging/vc04_services/bcm2835-audio/Makefile
> +++ b/drivers/staging/vc04_services/bcm2835-audio/Makefile
> @@ -2,4 +2,4 @@
> obj-$(CONFIG_SND_BCM2835) += snd-bcm2835.o
> snd-bcm2835-objs := bcm2835.o bcm2835-ctl.o bcm2835-pcm.o bcm2835-vchiq.o
>
> -ccflags-y += -I $(srctree)/$(src)/../include -D__VCCOREVER__=0x04000000
> +ccflags-y += -I $(srctree)/$(src)/../include
> diff --git a/drivers/staging/vc04_services/bcm2835-camera/Makefile b/drivers/staging/vc04_services/bcm2835-camera/Makefile
> index 3a76d6ade428..3494c82b271a 100644
> --- a/drivers/staging/vc04_services/bcm2835-camera/Makefile
> +++ b/drivers/staging/vc04_services/bcm2835-camera/Makefile
> @@ -7,5 +7,4 @@ obj-$(CONFIG_VIDEO_BCM2835) += bcm2835-v4l2.o
>
> ccflags-y += \
> -I $(srctree)/$(src)/.. \
> - -I $(srctree)/$(src)/../vchiq-mmal/ \
> - -D__VCCOREVER__=0x04000000
> + -I $(srctree)/$(src)/../vchiq-mmal/
> diff --git a/drivers/staging/vc04_services/vchiq-mmal/Makefile b/drivers/staging/vc04_services/vchiq-mmal/Makefile
> index b2a830f48acc..c7d3b06e17ce 100644
> --- a/drivers/staging/vc04_services/vchiq-mmal/Makefile
> +++ b/drivers/staging/vc04_services/vchiq-mmal/Makefile
> @@ -5,5 +5,4 @@ obj-$(CONFIG_BCM2835_VCHIQ_MMAL) += bcm2835-mmal-vchiq.o
>
> ccflags-y += \
> -I$(srctree)/$(src)/.. \
> - -I$(srctree)/$(src)/../include \
> - -D__VCCOREVER__=0x04000000
> + -I$(srctree)/$(src)/../include

--
Regards,

Laurent Pinchart

2023-01-20 02:08:46

by Laurent Pinchart

[permalink] [raw]
Subject: Re: [PATCH v5 2/6] staging: vc04_services: bcm2835-audio: Drop include Makefile directive

Hi Umang,

Thank you for the patch.

On Thu, Jan 19, 2023 at 05:24:59PM +0530, Umang Jain wrote:
> Drop the include directive they can break the build one only wants to
> build a subdirectory. Replace with "../" for the includes, in the
> bcm2835.h instead.

I assume you meant

Drop the include directive. They can break the build, when one only
wants to build a subdirectory.

> Suggested-by: Greg Kroah-Hartman <[email protected]>
> Signed-off-by: Umang Jain <[email protected]>
> ---
> drivers/staging/vc04_services/bcm2835-audio/Makefile | 2 --
> drivers/staging/vc04_services/bcm2835-audio/bcm2835.h | 3 ++-
> 2 files changed, 2 insertions(+), 3 deletions(-)
>
> diff --git a/drivers/staging/vc04_services/bcm2835-audio/Makefile b/drivers/staging/vc04_services/bcm2835-audio/Makefile
> index fc7ac6112a3e..01ceebdf88e7 100644
> --- a/drivers/staging/vc04_services/bcm2835-audio/Makefile
> +++ b/drivers/staging/vc04_services/bcm2835-audio/Makefile
> @@ -1,5 +1,3 @@
> # SPDX-License-Identifier: GPL-2.0
> obj-$(CONFIG_SND_BCM2835) += snd-bcm2835.o
> snd-bcm2835-objs := bcm2835.o bcm2835-ctl.o bcm2835-pcm.o bcm2835-vchiq.o
> -
> -ccflags-y += -I $(srctree)/$(src)/../include

The reason for this, I assume, is that the driver is in staging. The
vchiq.h file should live in include/linux/raspberrypi/, not
drivers/staging/vc04_services/include/linux/raspberrypi/, so an
additional include directory is added in order to use

#include <linux/raspberrypi/vchiq.h>

When the code will get out of staging, vchiq.h will go to
include/linux/raspberrypi/, the extra include directory will be dropped,
and all will be well without having to change any source file.

With this patch, we'll have to undo the change below to
drivers/staging/vc04_services/bcm2835-audio/bcm2835.h when vc04_services
will get out of staging.

Greg, is that what you prefer ?

> diff --git a/drivers/staging/vc04_services/bcm2835-audio/bcm2835.h b/drivers/staging/vc04_services/bcm2835-audio/bcm2835.h
> index 38b7451d77b2..0a81383c475a 100644
> --- a/drivers/staging/vc04_services/bcm2835-audio/bcm2835.h
> +++ b/drivers/staging/vc04_services/bcm2835-audio/bcm2835.h
> @@ -6,11 +6,12 @@
>
> #include <linux/device.h>
> #include <linux/wait.h>
> -#include <linux/raspberrypi/vchiq.h>
> #include <sound/core.h>
> #include <sound/pcm.h>
> #include <sound/pcm-indirect.h>
>
> +#include "../include/linux/raspberrypi/vchiq.h"
> +
> #define MAX_SUBSTREAMS (8)
> #define AVAIL_SUBSTREAMS_MASK (0xff)
>

--
Regards,

Laurent Pinchart

2023-01-20 02:12:21

by Laurent Pinchart

[permalink] [raw]
Subject: Re: [PATCH v5 4/6] staging: vc04_services: vchiq-mmal: Drop include Makefile directive

Hi Umang,

Thank you for the patch.

On Thu, Jan 19, 2023 at 05:25:01PM +0530, Umang Jain wrote:
> Drop the include directive they can break the build one only wants to
> build a subdirectory. Replace with "../" for the includes, in the
> mmal-vchiq.c instead.
>
> Suggested-by: Greg Kroah-Hartman <[email protected]>
> Signed-off-by: Umang Jain <[email protected]>
> ---
> drivers/staging/vc04_services/vchiq-mmal/Makefile | 4 ----
> drivers/staging/vc04_services/vchiq-mmal/mmal-vchiq.c | 2 +-
> 2 files changed, 1 insertion(+), 5 deletions(-)
>
> diff --git a/drivers/staging/vc04_services/vchiq-mmal/Makefile b/drivers/staging/vc04_services/vchiq-mmal/Makefile
> index c7d3b06e17ce..6937f6534c26 100644
> --- a/drivers/staging/vc04_services/vchiq-mmal/Makefile
> +++ b/drivers/staging/vc04_services/vchiq-mmal/Makefile
> @@ -2,7 +2,3 @@
> bcm2835-mmal-vchiq-objs := mmal-vchiq.o
>
> obj-$(CONFIG_BCM2835_VCHIQ_MMAL) += bcm2835-mmal-vchiq.o
> -
> -ccflags-y += \
> - -I$(srctree)/$(src)/.. \
> - -I$(srctree)/$(src)/../include

Same comments as for 2/6, although, if we decide to drop 2/6, you could
still remove the first of these two -I entries.

> diff --git a/drivers/staging/vc04_services/vchiq-mmal/mmal-vchiq.c b/drivers/staging/vc04_services/vchiq-mmal/mmal-vchiq.c
> index 6b5879a33780..234e3764db64 100644
> --- a/drivers/staging/vc04_services/vchiq-mmal/mmal-vchiq.c
> +++ b/drivers/staging/vc04_services/vchiq-mmal/mmal-vchiq.c
> @@ -23,9 +23,9 @@
> #include <linux/slab.h>
> #include <linux/completion.h>
> #include <linux/vmalloc.h>
> -#include <linux/raspberrypi/vchiq.h>
> #include <media/videobuf2-vmalloc.h>
>
> +#include "../include/linux/raspberrypi/vchiq.h"
> #include "mmal-common.h"
> #include "mmal-vchiq.h"
> #include "mmal-msg.h"

--
Regards,

Laurent Pinchart

2023-01-20 02:28:56

by Laurent Pinchart

[permalink] [raw]
Subject: Re: [PATCH v5 3/6] staging: vc04_services: bcm2835-camera: Drop include Makefile directive

Hi Umang,

Thank you for the patch.

On Thu, Jan 19, 2023 at 05:25:00PM +0530, Umang Jain wrote:
> Drop the include directive they can break the build one only wants to
> build a subdirectory. Replace with "../" for the includes, in the
> bcm2835-camera files instead.

Same comment as in 2/6. I expect it applies to other patches in the
series too.

> Suggested-by: Greg Kroah-Hartman <[email protected]>
> Signed-off-by: Umang Jain <[email protected]>
> ---
> drivers/staging/vc04_services/bcm2835-camera/Makefile | 4 ----
> .../vc04_services/bcm2835-camera/bcm2835-camera.c | 10 +++++-----
> .../staging/vc04_services/bcm2835-camera/controls.c | 6 +++---
> 3 files changed, 8 insertions(+), 12 deletions(-)
>
> diff --git a/drivers/staging/vc04_services/bcm2835-camera/Makefile b/drivers/staging/vc04_services/bcm2835-camera/Makefile
> index 3494c82b271a..203b93899b20 100644
> --- a/drivers/staging/vc04_services/bcm2835-camera/Makefile
> +++ b/drivers/staging/vc04_services/bcm2835-camera/Makefile
> @@ -4,7 +4,3 @@ bcm2835-v4l2-$(CONFIG_VIDEO_BCM2835) := \
> controls.o
>
> obj-$(CONFIG_VIDEO_BCM2835) += bcm2835-v4l2.o
> -
> -ccflags-y += \
> - -I $(srctree)/$(src)/.. \
> - -I $(srctree)/$(src)/../vchiq-mmal/
> diff --git a/drivers/staging/vc04_services/bcm2835-camera/bcm2835-camera.c b/drivers/staging/vc04_services/bcm2835-camera/bcm2835-camera.c
> index 797ebe2a973a..4f81765912ea 100644
> --- a/drivers/staging/vc04_services/bcm2835-camera/bcm2835-camera.c
> +++ b/drivers/staging/vc04_services/bcm2835-camera/bcm2835-camera.c
> @@ -26,11 +26,11 @@
> #include <linux/delay.h>
> #include <linux/platform_device.h>
>
> -#include "mmal-common.h"
> -#include "mmal-encodings.h"
> -#include "mmal-vchiq.h"
> -#include "mmal-msg.h"
> -#include "mmal-parameters.h"
> +#include "../vchiq-mmal/mmal-common.h"
> +#include "../vchiq-mmal/mmal-encodings.h"
> +#include "../vchiq-mmal/mmal-vchiq.h"
> +#include "../vchiq-mmal/mmal-msg.h"
> +#include "../vchiq-mmal/mmal-parameters.h"

Unlike the change in 2/6 that we may want to reconsider, this looks good
to me, even though it will be interesting to see what happens if we move
the vchiq core out of staging first. I expect in that case that the
headers will go to a directory under include/ (likely in
include/linux/soc/ ?), and this will be modified accordingly.

In the meantime, with the commit message updated,

Reviewed-by: Laurent Pinchart <[email protected]>

> #include "bcm2835-camera.h"
>
> #define MIN_WIDTH 32
> diff --git a/drivers/staging/vc04_services/bcm2835-camera/controls.c b/drivers/staging/vc04_services/bcm2835-camera/controls.c
> index 5644d1d457b9..6bce45925bf1 100644
> --- a/drivers/staging/vc04_services/bcm2835-camera/controls.c
> +++ b/drivers/staging/vc04_services/bcm2835-camera/controls.c
> @@ -23,9 +23,9 @@
> #include <media/v4l2-event.h>
> #include <media/v4l2-common.h>
>
> -#include "mmal-common.h"
> -#include "mmal-vchiq.h"
> -#include "mmal-parameters.h"
> +#include "../vchiq-mmal/mmal-common.h"
> +#include "../vchiq-mmal/mmal-vchiq.h"
> +#include "../vchiq-mmal/mmal-parameters.h"
> #include "bcm2835-camera.h"
>
> /* The supported V4L2_CID_AUTO_EXPOSURE_BIAS values are from -4.0 to +4.0.

--
Regards,

Laurent Pinchart

2023-01-20 02:31:10

by Laurent Pinchart

[permalink] [raw]
Subject: Re: [PATCH v5 6/6] staging: vc04_services: vchiq: Register devices with a custom bus_type

Hi Umang,

Thank you for the patch.

On Thu, Jan 19, 2023 at 05:25:03PM +0530, Umang Jain wrote:
> The devices that the vchiq interface registers(bcm2835-audio,

Missing space before '('.

> bcm2835-camera) are implemented and exposed by the VC04 firmware.
> The device tree describes the VC04 itself with the resources
> required to communicate with it through a mailbox interface. However,
> the vchiq interface registers these devices as platform devices. This
> also means the specific drivers for these devices are also getting

Drop one of the two "also".

> registered as platform drivers. This is not correct and a blatant
> abuse of platform device/driver.
>
> Replace the platform device/driver model with a standard device driver
> model. A custom bus_type, vchiq_bus_type, is created in the vchiq
> interface which matches the devices to their specific device drivers
> thereby, establishing driver binding. A struct vchiq_device wraps the
> struct device for each device being registered on the bus by the vchiq
> interface.
>
> Each device registered will expose a 'name' read-only device attribute
> in sysfs (/sys/bus/vchiq-bus/devices). New devices and drivers can be
> added by registering on vchiq_bus_type and adding a corresponding
> device name entry in the static list of devices, vchiq_devices. There
> is currently no way to enumerate the VCHIQ devices that are available
> from the firmware.

Greg, I don't know if you've followed the conversation in earlier mail
threads, so I'll try to summarize it here.

There are two layers involved: the VCHIQ layer, which has two clients
(audio and MMAL), and the MMAL layer, which has multiple clients
(camera, codec, ISP). The reason for this is that audio and mmal are
separate hardware, while camera, codec and ISP share some hardware
blocks.

The VCHIQ layer provides a mailbox API to its clients to communicate
with the firmware, and the MMAL layer provides another API implemented
on top of the VCHIQ layer. Neither APIs offer a way to discover devices
dynamically (that's not a feature implemented by the firmware). We've
decided that implementing two buses would be overkill, so Umang went for
a single vchiq_bus_type. The only value it provides is to stop abusing
platform_device. That's pretty much it.

Given the above explanation, do you still think the additional
complexity introduced by the vchiq bus type is worth it (it more or less
duplicates a small subset of the platform bus type implementation), and
are you fine with a single bus type, even if it doesn't exactly match
the firmware layers ?

> Signed-off-by: Umang Jain <[email protected]>
> ---
> .../vc04_services/bcm2835-audio/bcm2835.c | 19 ++-
> .../bcm2835-camera/bcm2835-camera.c | 17 ++-
> .../interface/vchiq_arm/vchiq_arm.c | 121 +++++++++++++++---
> .../interface/vchiq_arm/vchiq_arm.h | 1 +
> 4 files changed, 117 insertions(+), 41 deletions(-)
>
> diff --git a/drivers/staging/vc04_services/bcm2835-audio/bcm2835.c b/drivers/staging/vc04_services/bcm2835-audio/bcm2835.c
> index 00bc898b0189..9f3af84f5d5d 100644
> --- a/drivers/staging/vc04_services/bcm2835-audio/bcm2835.c
> +++ b/drivers/staging/vc04_services/bcm2835-audio/bcm2835.c
> @@ -1,12 +1,11 @@
> // SPDX-License-Identifier: GPL-2.0
> /* Copyright 2011 Broadcom Corporation. All rights reserved. */
>
> -#include <linux/platform_device.h>
> -
> #include <linux/init.h>
> #include <linux/slab.h>
> #include <linux/module.h>
>
> +#include "../interface/vchiq_arm/vchiq_arm.h"
> #include "bcm2835.h"
>
> static bool enable_hdmi;
> @@ -268,9 +267,8 @@ static int snd_add_child_devices(struct device *device, u32 numchans)
> return 0;
> }
>
> -static int snd_bcm2835_alsa_probe(struct platform_device *pdev)
> +static int snd_bcm2835_alsa_probe(struct device *dev)
> {
> - struct device *dev = &pdev->dev;
> int err;
>
> if (num_channels <= 0 || num_channels > MAX_SUBSTREAMS) {
> @@ -292,30 +290,29 @@ static int snd_bcm2835_alsa_probe(struct platform_device *pdev)
>
> #ifdef CONFIG_PM
>
> -static int snd_bcm2835_alsa_suspend(struct platform_device *pdev,
> +static int snd_bcm2835_alsa_suspend(struct device *pdev,
> pm_message_t state)
> {
> return 0;
> }
>
> -static int snd_bcm2835_alsa_resume(struct platform_device *pdev)
> +static int snd_bcm2835_alsa_resume(struct device *pdev)
> {
> return 0;
> }
>
> #endif
>
> -static struct platform_driver bcm2835_alsa_driver = {
> +static struct device_driver bcm2835_alsa_driver = {
> .probe = snd_bcm2835_alsa_probe,
> #ifdef CONFIG_PM
> .suspend = snd_bcm2835_alsa_suspend,
> .resume = snd_bcm2835_alsa_resume,
> #endif
> - .driver = {
> - .name = "bcm2835_audio",
> - },
> + .name = "bcm2835_audio",
> + .bus = &vchiq_bus_type,
> };
> -module_platform_driver(bcm2835_alsa_driver);
> +module_driver(bcm2835_alsa_driver, driver_register, driver_unregister);

Shouldn't you create a struct vchiq_device that wraps struct device, a
struct vchiq_driver that wraps struct device_driver, and a
module_vchiq_driver() macro ? It shouldn't be up to individual drivers
to deal with the plumbing.

>
> MODULE_AUTHOR("Dom Cobley");
> MODULE_DESCRIPTION("Alsa driver for BCM2835 chip");
> diff --git a/drivers/staging/vc04_services/bcm2835-camera/bcm2835-camera.c b/drivers/staging/vc04_services/bcm2835-camera/bcm2835-camera.c
> index 4f81765912ea..199a49f9ec1e 100644
> --- a/drivers/staging/vc04_services/bcm2835-camera/bcm2835-camera.c
> +++ b/drivers/staging/vc04_services/bcm2835-camera/bcm2835-camera.c
> @@ -24,8 +24,8 @@
> #include <media/v4l2-event.h>
> #include <media/v4l2-common.h>
> #include <linux/delay.h>
> -#include <linux/platform_device.h>
>
> +#include "../interface/vchiq_arm/vchiq_arm.h"
> #include "../vchiq-mmal/mmal-common.h"
> #include "../vchiq-mmal/mmal-encodings.h"
> #include "../vchiq-mmal/mmal-vchiq.h"
> @@ -1841,7 +1841,7 @@ static struct v4l2_format default_v4l2_format = {
> .fmt.pix.sizeimage = 1024 * 768,
> };
>
> -static int bcm2835_mmal_probe(struct platform_device *pdev)
> +static int bcm2835_mmal_probe(struct device *device)
> {
> int ret;
> struct bcm2835_mmal_dev *dev;
> @@ -1896,7 +1896,7 @@ static int bcm2835_mmal_probe(struct platform_device *pdev)
> &camera_instance);
> ret = v4l2_device_register(NULL, &dev->v4l2_dev);
> if (ret) {
> - dev_err(&pdev->dev, "%s: could not register V4L2 device: %d\n",
> + dev_err(device, "%s: could not register V4L2 device: %d\n",
> __func__, ret);
> goto free_dev;
> }
> @@ -1976,7 +1976,7 @@ static int bcm2835_mmal_probe(struct platform_device *pdev)
> return ret;
> }
>
> -static int bcm2835_mmal_remove(struct platform_device *pdev)
> +static int bcm2835_mmal_remove(struct device *device)
> {
> int camera;
> struct vchiq_mmal_instance *instance = gdev[0]->instance;
> @@ -1990,15 +1990,14 @@ static int bcm2835_mmal_remove(struct platform_device *pdev)
> return 0;
> }
>
> -static struct platform_driver bcm2835_camera_driver = {
> +static struct device_driver bcm2835_camera_driver = {
> + .name = "bcm2835-camera",
> .probe = bcm2835_mmal_probe,
> .remove = bcm2835_mmal_remove,
> - .driver = {
> - .name = "bcm2835-camera",
> - },
> + .bus = &vchiq_bus_type,
> };
>
> -module_platform_driver(bcm2835_camera_driver)
> +module_driver(bcm2835_camera_driver, driver_register, driver_unregister)
>
> MODULE_DESCRIPTION("Broadcom 2835 MMAL video capture");
> MODULE_AUTHOR("Vincent Sanders");
> diff --git a/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.c b/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.c
> index 22de23f3af02..86c8e5df7cf6 100644
> --- a/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.c
> +++ b/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.c
> @@ -12,6 +12,8 @@
> #include <linux/cdev.h>
> #include <linux/fs.h>
> #include <linux/device.h>
> +#include <linux/device/bus.h>
> +#include <linux/string.h>
> #include <linux/mm.h>
> #include <linux/highmem.h>
> #include <linux/pagemap.h>
> @@ -65,9 +67,6 @@ int vchiq_susp_log_level = VCHIQ_LOG_ERROR;
> DEFINE_SPINLOCK(msg_queue_spinlock);
> struct vchiq_state g_state;
>
> -static struct platform_device *bcm2835_camera;
> -static struct platform_device *bcm2835_audio;
> -
> struct vchiq_drvdata {
> const unsigned int cache_line_size;
> struct rpi_firmware *fw;
> @@ -132,6 +131,51 @@ struct vchiq_pagelist_info {
> unsigned int scatterlist_mapped;
> };
>
> +struct vchiq_device {
> + const char *name;
> + struct device dev;
> +};

Ah there we go :-) Move this structure to a header file that drivers can
include. I'd name it vchiq_device.h. The code below should go to
vchiq_device.c.

I would also move the dev field first.

> +
> +static ssize_t vchiq_dev_show(struct device *dev,
> + struct device_attribute *attr, char *buf)
> +{
> + struct vchiq_device *device = container_of(dev, struct vchiq_device, dev);
> +
> + return sprintf(buf, "%s", device->name);
> +}
> +
> +static DEVICE_ATTR_RO(vchiq_dev);
> +
> +static struct attribute *vchiq_dev_attrs[] = {
> + &dev_attr_vchiq_dev.attr,
> + NULL
> +};
> +
> +ATTRIBUTE_GROUPS(vchiq_dev);
> +
> +static const struct device_type vchiq_device_type = {
> + .groups = vchiq_dev_groups
> +};
> +
> +static int vchiq_bus_type_match(struct device *dev, struct device_driver *drv)
> +{
> + if (dev->bus == &vchiq_bus_type &&
> + strcmp(dev_name(dev), drv->name) == 0)
> + return 1;
> + return 0;
> +}
> +
> +struct bus_type vchiq_bus_type = {
> + .name = "vchiq-bus",
> + .match = vchiq_bus_type_match,
> +};
> +EXPORT_SYMBOL(vchiq_bus_type);

EXPORT_SYMBOL_GPL ?

> +
> +static const char *const vchiq_devices[] = {
> + "bcm2835_audio",
> + "bcm2835-camera",
> +};

This however should stay in this file.

> +
> static void __iomem *g_regs;
> /* This value is the size of the L2 cache lines as understood by the
> * VPU firmware, which determines the required alignment of the
> @@ -1763,26 +1807,52 @@ static const struct of_device_id vchiq_of_match[] = {
> };
> MODULE_DEVICE_TABLE(of, vchiq_of_match);
>
> -static struct platform_device *
> +static void
> +vchiq_release_device(struct device *dev)
> +{
> + struct vchiq_device *device;
> +
> + device = container_of(dev, struct vchiq_device, dev);
> + kfree(device);
> +}
> +
> +static int
> vchiq_register_child(struct platform_device *pdev, const char *name)

Pass a struct device * for the first argument, you don't need a platform
device. I'd also name the function vchiq_register_device, and rename the
pdev parameter to parent.

> {
> - struct platform_device_info pdevinfo;
> - struct platform_device *child;
> + struct vchiq_device *device = NULL;
> + int ret;
>
> - memset(&pdevinfo, 0, sizeof(pdevinfo));
> + device = kzalloc(sizeof(*device), GFP_KERNEL);
> + if (!device)
> + return -ENOMEM;
>
> - pdevinfo.parent = &pdev->dev;
> - pdevinfo.name = name;
> - pdevinfo.id = PLATFORM_DEVID_NONE;
> - pdevinfo.dma_mask = DMA_BIT_MASK(32);
> + device->name = name;
> + device->dev.init_name = name;
> + device->dev.parent = &pdev->dev;
> + device->dev.bus = &vchiq_bus_type;
> + device->dev.type = &vchiq_device_type;
> + device->dev.release = vchiq_release_device;
> +
> + ret = dma_set_mask_and_coherent(&device->dev, DMA_BIT_MASK(32));

Do vchiq devices perform DMA ?

> + if (ret < 0) {
> + vchiq_release_device(&device->dev);
> + return ret;
> + }
>
> - child = platform_device_register_full(&pdevinfo);
> - if (IS_ERR(child)) {
> - dev_warn(&pdev->dev, "%s not registered\n", name);
> - child = NULL;
> + ret = device_register(&device->dev);
> + if (ret) {
> + put_device(&device->dev);
> + return -EINVAL;
> }
>
> - return child;
> + return 0;
> +}
> +
> +static int
> +vchiq_unregister_child(struct device *dev, void *data)
> +{
> + device_unregister(dev);
> + return 0;
> }
>
> static int vchiq_probe(struct platform_device *pdev)
> @@ -1790,7 +1860,7 @@ static int vchiq_probe(struct platform_device *pdev)
> struct device_node *fw_node;
> const struct of_device_id *of_id;
> struct vchiq_drvdata *drvdata;
> - int err;
> + int i, err;

i can be an unsigned int.

>
> of_id = of_match_node(vchiq_of_match, pdev->dev.of_node);
> drvdata = (struct vchiq_drvdata *)of_id->data;
> @@ -1832,8 +1902,12 @@ static int vchiq_probe(struct platform_device *pdev)
> goto error_exit;
> }
>
> - bcm2835_camera = vchiq_register_child(pdev, "bcm2835-camera");
> - bcm2835_audio = vchiq_register_child(pdev, "bcm2835_audio");
> + for (i = 0; i < ARRAY_SIZE(vchiq_devices); i++) {
> + err = vchiq_register_child(pdev, vchiq_devices[i]);
> + if (!err)
> + dev_err(&pdev->dev, "Failed to register %s vchiq device\n",
> + vchiq_devices[i]);
> + }
>
> return 0;
>
> @@ -1845,8 +1919,8 @@ static int vchiq_probe(struct platform_device *pdev)
>
> static int vchiq_remove(struct platform_device *pdev)
> {
> - platform_device_unregister(bcm2835_audio);
> - platform_device_unregister(bcm2835_camera);
> + bus_for_each_dev(&vchiq_bus_type, NULL, NULL, vchiq_unregister_child);
> +
> vchiq_debugfs_deinit();
> vchiq_deregister_chrdev();
>
> @@ -1866,6 +1940,10 @@ static int __init vchiq_driver_init(void)
> {
> int ret;
>
> + ret = bus_register(&vchiq_bus_type);
> + if (ret)
> + pr_err("Failed to register %s\n", vchiq_bus_type.name);

This should be a fatal error, you should return an error value.

> +
> ret = platform_driver_register(&vchiq_driver);
> if (ret)
> pr_err("Failed to register vchiq driver\n");
> @@ -1876,6 +1954,7 @@ module_init(vchiq_driver_init);
>
> static void __exit vchiq_driver_exit(void)
> {
> + bus_unregister(&vchiq_bus_type);
> platform_driver_unregister(&vchiq_driver);
> }
> module_exit(vchiq_driver_exit);
> diff --git a/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.h b/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.h
> index 2fb31f9b527f..98c3af32774a 100644
> --- a/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.h
> +++ b/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.h
> @@ -81,6 +81,7 @@ extern int vchiq_susp_log_level;
>
> extern spinlock_t msg_queue_spinlock;
> extern struct vchiq_state g_state;
> +extern struct bus_type vchiq_bus_type;
>
> extern struct vchiq_state *
> vchiq_get_state(void);

--
Regards,

Laurent Pinchart

2023-01-20 05:04:41

by [email protected]

[permalink] [raw]
Subject: Re: [PATCH v5 1/6] staging: vc04_services: Drop __VCCOREVER__ remnants

On Thu, Jan 19, 2023 at 05:24:58PM +0530, Umang Jain wrote:
> Commit 8ba5f91bab63("staging: vc04_services: remove __VCCOREVER__")

Nit, you need a space before the '(' character :(

2023-01-20 07:07:08

by [email protected]

[permalink] [raw]
Subject: Re: [PATCH v5 2/6] staging: vc04_services: bcm2835-audio: Drop include Makefile directive

On Fri, Jan 20, 2023 at 03:28:05AM +0200, Laurent Pinchart wrote:
> Hi Umang,
>
> Thank you for the patch.
>
> On Thu, Jan 19, 2023 at 05:24:59PM +0530, Umang Jain wrote:
> > Drop the include directive they can break the build one only wants to
> > build a subdirectory. Replace with "../" for the includes, in the
> > bcm2835.h instead.
>
> I assume you meant
>
> Drop the include directive. They can break the build, when one only
> wants to build a subdirectory.
>
> > Suggested-by: Greg Kroah-Hartman <[email protected]>
> > Signed-off-by: Umang Jain <[email protected]>
> > ---
> > drivers/staging/vc04_services/bcm2835-audio/Makefile | 2 --
> > drivers/staging/vc04_services/bcm2835-audio/bcm2835.h | 3 ++-
> > 2 files changed, 2 insertions(+), 3 deletions(-)
> >
> > diff --git a/drivers/staging/vc04_services/bcm2835-audio/Makefile b/drivers/staging/vc04_services/bcm2835-audio/Makefile
> > index fc7ac6112a3e..01ceebdf88e7 100644
> > --- a/drivers/staging/vc04_services/bcm2835-audio/Makefile
> > +++ b/drivers/staging/vc04_services/bcm2835-audio/Makefile
> > @@ -1,5 +1,3 @@
> > # SPDX-License-Identifier: GPL-2.0
> > obj-$(CONFIG_SND_BCM2835) += snd-bcm2835.o
> > snd-bcm2835-objs := bcm2835.o bcm2835-ctl.o bcm2835-pcm.o bcm2835-vchiq.o
> > -
> > -ccflags-y += -I $(srctree)/$(src)/../include
>
> The reason for this, I assume, is that the driver is in staging. The
> vchiq.h file should live in include/linux/raspberrypi/, not
> drivers/staging/vc04_services/include/linux/raspberrypi/, so an
> additional include directory is added in order to use
>
> #include <linux/raspberrypi/vchiq.h>
>
> When the code will get out of staging, vchiq.h will go to
> include/linux/raspberrypi/, the extra include directory will be dropped,
> and all will be well without having to change any source file.
>
> With this patch, we'll have to undo the change below to
> drivers/staging/vc04_services/bcm2835-audio/bcm2835.h when vc04_services
> will get out of staging.
>
> Greg, is that what you prefer ?

I prefer the drivers to NOT use include ccflags in the kernel as it
breaks the build when trying to build just a subdirectory. If/when this
code ever gets out of staging, then the include lines can be fixed up to
point to the correct location of wherever the files move to.

So I like this patch, but as I couldn't take the first one, this and the
rest did not apply so I'll wait for them to be resubmitted.

thanks,

greg k-h

2023-01-20 07:38:46

by [email protected]

[permalink] [raw]
Subject: Re: [PATCH v5 6/6] staging: vc04_services: vchiq: Register devices with a custom bus_type

On Fri, Jan 20, 2023 at 03:52:22AM +0200, Laurent Pinchart wrote:
> Hi Umang,
>
> Thank you for the patch.
>
> On Thu, Jan 19, 2023 at 05:25:03PM +0530, Umang Jain wrote:
> > The devices that the vchiq interface registers(bcm2835-audio,
>
> Missing space before '('.
>
> > bcm2835-camera) are implemented and exposed by the VC04 firmware.
> > The device tree describes the VC04 itself with the resources
> > required to communicate with it through a mailbox interface. However,
> > the vchiq interface registers these devices as platform devices. This
> > also means the specific drivers for these devices are also getting
>
> Drop one of the two "also".
>
> > registered as platform drivers. This is not correct and a blatant
> > abuse of platform device/driver.
> >
> > Replace the platform device/driver model with a standard device driver
> > model. A custom bus_type, vchiq_bus_type, is created in the vchiq
> > interface which matches the devices to their specific device drivers
> > thereby, establishing driver binding. A struct vchiq_device wraps the
> > struct device for each device being registered on the bus by the vchiq
> > interface.
> >
> > Each device registered will expose a 'name' read-only device attribute
> > in sysfs (/sys/bus/vchiq-bus/devices). New devices and drivers can be
> > added by registering on vchiq_bus_type and adding a corresponding
> > device name entry in the static list of devices, vchiq_devices. There
> > is currently no way to enumerate the VCHIQ devices that are available
> > from the firmware.
>
> Greg, I don't know if you've followed the conversation in earlier mail
> threads, so I'll try to summarize it here.
>
> There are two layers involved: the VCHIQ layer, which has two clients
> (audio and MMAL), and the MMAL layer, which has multiple clients
> (camera, codec, ISP). The reason for this is that audio and mmal are
> separate hardware, while camera, codec and ISP share some hardware
> blocks.
>
> The VCHIQ layer provides a mailbox API to its clients to communicate
> with the firmware, and the MMAL layer provides another API implemented
> on top of the VCHIQ layer. Neither APIs offer a way to discover devices
> dynamically (that's not a feature implemented by the firmware). We've
> decided that implementing two buses would be overkill, so Umang went for
> a single vchiq_bus_type. The only value it provides is to stop abusing
> platform_device. That's pretty much it.
>
> Given the above explanation, do you still think the additional
> complexity introduced by the vchiq bus type is worth it (it more or less
> duplicates a small subset of the platform bus type implementation), and
> are you fine with a single bus type, even if it doesn't exactly match
> the firmware layers ?

Yes, this is the correct way forward. I didn't review the changes yet,
but I see you just gave a good first pass, so I'll wait for the next
revision.

thanks,

greg k-h

2023-01-20 11:15:02

by Umang Jain

[permalink] [raw]
Subject: Re: [PATCH v5 6/6] staging: vc04_services: vchiq: Register devices with a custom bus_type

Hi Laurent,

On 1/20/23 7:22 AM, Laurent Pinchart wrote:
> Hi Umang,
>
> Thank you for the patch.
>
> On Thu, Jan 19, 2023 at 05:25:03PM +0530, Umang Jain wrote:
>> The devices that the vchiq interface registers(bcm2835-audio,
> Missing space before '('.
>
>> bcm2835-camera) are implemented and exposed by the VC04 firmware.
>> The device tree describes the VC04 itself with the resources
>> required to communicate with it through a mailbox interface. However,
>> the vchiq interface registers these devices as platform devices. This
>> also means the specific drivers for these devices are also getting
> Drop one of the two "also".
>
>> registered as platform drivers. This is not correct and a blatant
>> abuse of platform device/driver.
>>
>> Replace the platform device/driver model with a standard device driver
>> model. A custom bus_type, vchiq_bus_type, is created in the vchiq
>> interface which matches the devices to their specific device drivers
>> thereby, establishing driver binding. A struct vchiq_device wraps the
>> struct device for each device being registered on the bus by the vchiq
>> interface.
>>
>> Each device registered will expose a 'name' read-only device attribute
>> in sysfs (/sys/bus/vchiq-bus/devices). New devices and drivers can be
>> added by registering on vchiq_bus_type and adding a corresponding
>> device name entry in the static list of devices, vchiq_devices. There
>> is currently no way to enumerate the VCHIQ devices that are available
>> from the firmware.
> Greg, I don't know if you've followed the conversation in earlier mail
> threads, so I'll try to summarize it here.
>
> There are two layers involved: the VCHIQ layer, which has two clients
> (audio and MMAL), and the MMAL layer, which has multiple clients
> (camera, codec, ISP). The reason for this is that audio and mmal are
> separate hardware, while camera, codec and ISP share some hardware
> blocks.
>
> The VCHIQ layer provides a mailbox API to its clients to communicate
> with the firmware, and the MMAL layer provides another API implemented
> on top of the VCHIQ layer. Neither APIs offer a way to discover devices
> dynamically (that's not a feature implemented by the firmware). We've
> decided that implementing two buses would be overkill, so Umang went for
> a single vchiq_bus_type. The only value it provides is to stop abusing
> platform_device. That's pretty much it.
>
> Given the above explanation, do you still think the additional
> complexity introduced by the vchiq bus type is worth it (it more or less
> duplicates a small subset of the platform bus type implementation), and
> are you fine with a single bus type, even if it doesn't exactly match
> the firmware layers ?
>
>> Signed-off-by: Umang Jain <[email protected]>
>> ---
>> .../vc04_services/bcm2835-audio/bcm2835.c | 19 ++-
>> .../bcm2835-camera/bcm2835-camera.c | 17 ++-
>> .../interface/vchiq_arm/vchiq_arm.c | 121 +++++++++++++++---
>> .../interface/vchiq_arm/vchiq_arm.h | 1 +
>> 4 files changed, 117 insertions(+), 41 deletions(-)
>>
>> diff --git a/drivers/staging/vc04_services/bcm2835-audio/bcm2835.c b/drivers/staging/vc04_services/bcm2835-audio/bcm2835.c
>> index 00bc898b0189..9f3af84f5d5d 100644
>> --- a/drivers/staging/vc04_services/bcm2835-audio/bcm2835.c
>> +++ b/drivers/staging/vc04_services/bcm2835-audio/bcm2835.c
>> @@ -1,12 +1,11 @@
>> // SPDX-License-Identifier: GPL-2.0
>> /* Copyright 2011 Broadcom Corporation. All rights reserved. */
>>
>> -#include <linux/platform_device.h>
>> -
>> #include <linux/init.h>
>> #include <linux/slab.h>
>> #include <linux/module.h>
>>
>> +#include "../interface/vchiq_arm/vchiq_arm.h"
>> #include "bcm2835.h"
>>
>> static bool enable_hdmi;
>> @@ -268,9 +267,8 @@ static int snd_add_child_devices(struct device *device, u32 numchans)
>> return 0;
>> }
>>
>> -static int snd_bcm2835_alsa_probe(struct platform_device *pdev)
>> +static int snd_bcm2835_alsa_probe(struct device *dev)
>> {
>> - struct device *dev = &pdev->dev;
>> int err;
>>
>> if (num_channels <= 0 || num_channels > MAX_SUBSTREAMS) {
>> @@ -292,30 +290,29 @@ static int snd_bcm2835_alsa_probe(struct platform_device *pdev)
>>
>> #ifdef CONFIG_PM
>>
>> -static int snd_bcm2835_alsa_suspend(struct platform_device *pdev,
>> +static int snd_bcm2835_alsa_suspend(struct device *pdev,
>> pm_message_t state)
>> {
>> return 0;
>> }
>>
>> -static int snd_bcm2835_alsa_resume(struct platform_device *pdev)
>> +static int snd_bcm2835_alsa_resume(struct device *pdev)
>> {
>> return 0;
>> }
>>
>> #endif
>>
>> -static struct platform_driver bcm2835_alsa_driver = {
>> +static struct device_driver bcm2835_alsa_driver = {
>> .probe = snd_bcm2835_alsa_probe,
>> #ifdef CONFIG_PM
>> .suspend = snd_bcm2835_alsa_suspend,
>> .resume = snd_bcm2835_alsa_resume,
>> #endif
>> - .driver = {
>> - .name = "bcm2835_audio",
>> - },
>> + .name = "bcm2835_audio",
>> + .bus = &vchiq_bus_type,
>> };
>> -module_platform_driver(bcm2835_alsa_driver);
>> +module_driver(bcm2835_alsa_driver, driver_register, driver_unregister);
> Shouldn't you create a struct vchiq_device that wraps struct device, a
> struct vchiq_driver that wraps struct device_driver, and a
> module_vchiq_driver() macro ? It shouldn't be up to individual drivers
> to deal with the plumbing.
>
>>
>> MODULE_AUTHOR("Dom Cobley");
>> MODULE_DESCRIPTION("Alsa driver for BCM2835 chip");
>> diff --git a/drivers/staging/vc04_services/bcm2835-camera/bcm2835-camera.c b/drivers/staging/vc04_services/bcm2835-camera/bcm2835-camera.c
>> index 4f81765912ea..199a49f9ec1e 100644
>> --- a/drivers/staging/vc04_services/bcm2835-camera/bcm2835-camera.c
>> +++ b/drivers/staging/vc04_services/bcm2835-camera/bcm2835-camera.c
>> @@ -24,8 +24,8 @@
>> #include <media/v4l2-event.h>
>> #include <media/v4l2-common.h>
>> #include <linux/delay.h>
>> -#include <linux/platform_device.h>
>>
>> +#include "../interface/vchiq_arm/vchiq_arm.h"
>> #include "../vchiq-mmal/mmal-common.h"
>> #include "../vchiq-mmal/mmal-encodings.h"
>> #include "../vchiq-mmal/mmal-vchiq.h"
>> @@ -1841,7 +1841,7 @@ static struct v4l2_format default_v4l2_format = {
>> .fmt.pix.sizeimage = 1024 * 768,
>> };
>>
>> -static int bcm2835_mmal_probe(struct platform_device *pdev)
>> +static int bcm2835_mmal_probe(struct device *device)
>> {
>> int ret;
>> struct bcm2835_mmal_dev *dev;
>> @@ -1896,7 +1896,7 @@ static int bcm2835_mmal_probe(struct platform_device *pdev)
>> &camera_instance);
>> ret = v4l2_device_register(NULL, &dev->v4l2_dev);
>> if (ret) {
>> - dev_err(&pdev->dev, "%s: could not register V4L2 device: %d\n",
>> + dev_err(device, "%s: could not register V4L2 device: %d\n",
>> __func__, ret);
>> goto free_dev;
>> }
>> @@ -1976,7 +1976,7 @@ static int bcm2835_mmal_probe(struct platform_device *pdev)
>> return ret;
>> }
>>
>> -static int bcm2835_mmal_remove(struct platform_device *pdev)
>> +static int bcm2835_mmal_remove(struct device *device)
>> {
>> int camera;
>> struct vchiq_mmal_instance *instance = gdev[0]->instance;
>> @@ -1990,15 +1990,14 @@ static int bcm2835_mmal_remove(struct platform_device *pdev)
>> return 0;
>> }
>>
>> -static struct platform_driver bcm2835_camera_driver = {
>> +static struct device_driver bcm2835_camera_driver = {
>> + .name = "bcm2835-camera",
>> .probe = bcm2835_mmal_probe,
>> .remove = bcm2835_mmal_remove,
>> - .driver = {
>> - .name = "bcm2835-camera",
>> - },
>> + .bus = &vchiq_bus_type,
>> };
>>
>> -module_platform_driver(bcm2835_camera_driver)
>> +module_driver(bcm2835_camera_driver, driver_register, driver_unregister)
>>
>> MODULE_DESCRIPTION("Broadcom 2835 MMAL video capture");
>> MODULE_AUTHOR("Vincent Sanders");
>> diff --git a/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.c b/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.c
>> index 22de23f3af02..86c8e5df7cf6 100644
>> --- a/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.c
>> +++ b/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.c
>> @@ -12,6 +12,8 @@
>> #include <linux/cdev.h>
>> #include <linux/fs.h>
>> #include <linux/device.h>
>> +#include <linux/device/bus.h>
>> +#include <linux/string.h>
>> #include <linux/mm.h>
>> #include <linux/highmem.h>
>> #include <linux/pagemap.h>
>> @@ -65,9 +67,6 @@ int vchiq_susp_log_level = VCHIQ_LOG_ERROR;
>> DEFINE_SPINLOCK(msg_queue_spinlock);
>> struct vchiq_state g_state;
>>
>> -static struct platform_device *bcm2835_camera;
>> -static struct platform_device *bcm2835_audio;
>> -
>> struct vchiq_drvdata {
>> const unsigned int cache_line_size;
>> struct rpi_firmware *fw;
>> @@ -132,6 +131,51 @@ struct vchiq_pagelist_info {
>> unsigned int scatterlist_mapped;
>> };
>>
>> +struct vchiq_device {
>> + const char *name;
>> + struct device dev;
>> +};
> Ah there we go :-) Move this structure to a header file that drivers can
> include. I'd name it vchiq_device.h. The code below should go to
> vchiq_device.c.
>
> I would also move the dev field first.
>
>> +
>> +static ssize_t vchiq_dev_show(struct device *dev,
>> + struct device_attribute *attr, char *buf)
>> +{
>> + struct vchiq_device *device = container_of(dev, struct vchiq_device, dev);
>> +
>> + return sprintf(buf, "%s", device->name);
>> +}
>> +
>> +static DEVICE_ATTR_RO(vchiq_dev);
>> +
>> +static struct attribute *vchiq_dev_attrs[] = {
>> + &dev_attr_vchiq_dev.attr,
>> + NULL
>> +};
>> +
>> +ATTRIBUTE_GROUPS(vchiq_dev);
>> +
>> +static const struct device_type vchiq_device_type = {
>> + .groups = vchiq_dev_groups
>> +};
>> +
>> +static int vchiq_bus_type_match(struct device *dev, struct device_driver *drv)
>> +{
>> + if (dev->bus == &vchiq_bus_type &&
>> + strcmp(dev_name(dev), drv->name) == 0)
>> + return 1;
>> + return 0;
>> +}
>> +
>> +struct bus_type vchiq_bus_type = {
>> + .name = "vchiq-bus",
>> + .match = vchiq_bus_type_match,
>> +};
>> +EXPORT_SYMBOL(vchiq_bus_type);
> EXPORT_SYMBOL_GPL ?
>
>> +
>> +static const char *const vchiq_devices[] = {
>> + "bcm2835_audio",
>> + "bcm2835-camera",
>> +};
> This however should stay in this file.
>
>> +
>> static void __iomem *g_regs;
>> /* This value is the size of the L2 cache lines as understood by the
>> * VPU firmware, which determines the required alignment of the
>> @@ -1763,26 +1807,52 @@ static const struct of_device_id vchiq_of_match[] = {
>> };
>> MODULE_DEVICE_TABLE(of, vchiq_of_match);
>>
>> -static struct platform_device *
>> +static void
>> +vchiq_release_device(struct device *dev)
>> +{
>> + struct vchiq_device *device;
>> +
>> + device = container_of(dev, struct vchiq_device, dev);
>> + kfree(device);
>> +}
>> +
>> +static int
>> vchiq_register_child(struct platform_device *pdev, const char *name)
> Pass a struct device * for the first argument, you don't need a platform
> device. I'd also name the function vchiq_register_device, and rename the
> pdev parameter to parent.
>
>> {
>> - struct platform_device_info pdevinfo;
>> - struct platform_device *child;
>> + struct vchiq_device *device = NULL;
>> + int ret;
>>
>> - memset(&pdevinfo, 0, sizeof(pdevinfo));
>> + device = kzalloc(sizeof(*device), GFP_KERNEL);
>> + if (!device)
>> + return -ENOMEM;
>>
>> - pdevinfo.parent = &pdev->dev;
>> - pdevinfo.name = name;
>> - pdevinfo.id = PLATFORM_DEVID_NONE;
>> - pdevinfo.dma_mask = DMA_BIT_MASK(32);
>> + device->name = name;
>> + device->dev.init_name = name;
>> + device->dev.parent = &pdev->dev;
>> + device->dev.bus = &vchiq_bus_type;
>> + device->dev.type = &vchiq_device_type;
>> + device->dev.release = vchiq_release_device;
>> +
>> + ret = dma_set_mask_and_coherent(&device->dev, DMA_BIT_MASK(32));
> Do vchiq devices perform DMA ?

As far as I can tell, the devices right now (in the mainline) don't use
DMA allocation. So it's not relevant right now.

But looking at RPi's BSP - the vc-sm-cma driver does the DMA. So
probably we want to dma_set_mask_and_coherent() for that only?

>
>> + if (ret < 0) {
>> + vchiq_release_device(&device->dev);
>> + return ret;
>> + }
>>
>> - child = platform_device_register_full(&pdevinfo);
>> - if (IS_ERR(child)) {
>> - dev_warn(&pdev->dev, "%s not registered\n", name);
>> - child = NULL;
>> + ret = device_register(&device->dev);
>> + if (ret) {
>> + put_device(&device->dev);
>> + return -EINVAL;
>> }
>>
>> - return child;
>> + return 0;
>> +}
>> +
>> +static int
>> +vchiq_unregister_child(struct device *dev, void *data)
>> +{
>> + device_unregister(dev);
>> + return 0;
>> }
>>
>> static int vchiq_probe(struct platform_device *pdev)
>> @@ -1790,7 +1860,7 @@ static int vchiq_probe(struct platform_device *pdev)
>> struct device_node *fw_node;
>> const struct of_device_id *of_id;
>> struct vchiq_drvdata *drvdata;
>> - int err;
>> + int i, err;
> i can be an unsigned int.
>
>>
>> of_id = of_match_node(vchiq_of_match, pdev->dev.of_node);
>> drvdata = (struct vchiq_drvdata *)of_id->data;
>> @@ -1832,8 +1902,12 @@ static int vchiq_probe(struct platform_device *pdev)
>> goto error_exit;
>> }
>>
>> - bcm2835_camera = vchiq_register_child(pdev, "bcm2835-camera");
>> - bcm2835_audio = vchiq_register_child(pdev, "bcm2835_audio");
>> + for (i = 0; i < ARRAY_SIZE(vchiq_devices); i++) {
>> + err = vchiq_register_child(pdev, vchiq_devices[i]);
>> + if (!err)
>> + dev_err(&pdev->dev, "Failed to register %s vchiq device\n",
>> + vchiq_devices[i]);
>> + }
>>
>> return 0;
>>
>> @@ -1845,8 +1919,8 @@ static int vchiq_probe(struct platform_device *pdev)
>>
>> static int vchiq_remove(struct platform_device *pdev)
>> {
>> - platform_device_unregister(bcm2835_audio);
>> - platform_device_unregister(bcm2835_camera);
>> + bus_for_each_dev(&vchiq_bus_type, NULL, NULL, vchiq_unregister_child);
>> +
>> vchiq_debugfs_deinit();
>> vchiq_deregister_chrdev();
>>
>> @@ -1866,6 +1940,10 @@ static int __init vchiq_driver_init(void)
>> {
>> int ret;
>>
>> + ret = bus_register(&vchiq_bus_type);
>> + if (ret)
>> + pr_err("Failed to register %s\n", vchiq_bus_type.name);
> This should be a fatal error, you should return an error value.
>
>> +
>> ret = platform_driver_register(&vchiq_driver);
>> if (ret)
>> pr_err("Failed to register vchiq driver\n");
>> @@ -1876,6 +1954,7 @@ module_init(vchiq_driver_init);
>>
>> static void __exit vchiq_driver_exit(void)
>> {
>> + bus_unregister(&vchiq_bus_type);
>> platform_driver_unregister(&vchiq_driver);
>> }
>> module_exit(vchiq_driver_exit);
>> diff --git a/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.h b/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.h
>> index 2fb31f9b527f..98c3af32774a 100644
>> --- a/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.h
>> +++ b/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.h
>> @@ -81,6 +81,7 @@ extern int vchiq_susp_log_level;
>>
>> extern spinlock_t msg_queue_spinlock;
>> extern struct vchiq_state g_state;
>> +extern struct bus_type vchiq_bus_type;
>>
>> extern struct vchiq_state *
>> vchiq_get_state(void);

2023-01-20 15:36:28

by Laurent Pinchart

[permalink] [raw]
Subject: Re: [PATCH v5 6/6] staging: vc04_services: vchiq: Register devices with a custom bus_type

Hi Umang,

On Fri, Jan 20, 2023 at 04:26:00PM +0530, Umang Jain wrote:
> On 1/20/23 7:22 AM, Laurent Pinchart wrote:
> > On Thu, Jan 19, 2023 at 05:25:03PM +0530, Umang Jain wrote:
> >> The devices that the vchiq interface registers(bcm2835-audio,
> >
> > Missing space before '('.
> >
> >> bcm2835-camera) are implemented and exposed by the VC04 firmware.
> >> The device tree describes the VC04 itself with the resources
> >> required to communicate with it through a mailbox interface. However,
> >> the vchiq interface registers these devices as platform devices. This
> >> also means the specific drivers for these devices are also getting
> >
> > Drop one of the two "also".
> >
> >> registered as platform drivers. This is not correct and a blatant
> >> abuse of platform device/driver.
> >>
> >> Replace the platform device/driver model with a standard device driver
> >> model. A custom bus_type, vchiq_bus_type, is created in the vchiq
> >> interface which matches the devices to their specific device drivers
> >> thereby, establishing driver binding. A struct vchiq_device wraps the
> >> struct device for each device being registered on the bus by the vchiq
> >> interface.
> >>
> >> Each device registered will expose a 'name' read-only device attribute
> >> in sysfs (/sys/bus/vchiq-bus/devices). New devices and drivers can be
> >> added by registering on vchiq_bus_type and adding a corresponding
> >> device name entry in the static list of devices, vchiq_devices. There
> >> is currently no way to enumerate the VCHIQ devices that are available
> >> from the firmware.
> >
> > Greg, I don't know if you've followed the conversation in earlier mail
> > threads, so I'll try to summarize it here.
> >
> > There are two layers involved: the VCHIQ layer, which has two clients
> > (audio and MMAL), and the MMAL layer, which has multiple clients
> > (camera, codec, ISP). The reason for this is that audio and mmal are
> > separate hardware, while camera, codec and ISP share some hardware
> > blocks.
> >
> > The VCHIQ layer provides a mailbox API to its clients to communicate
> > with the firmware, and the MMAL layer provides another API implemented
> > on top of the VCHIQ layer. Neither APIs offer a way to discover devices
> > dynamically (that's not a feature implemented by the firmware). We've
> > decided that implementing two buses would be overkill, so Umang went for
> > a single vchiq_bus_type. The only value it provides is to stop abusing
> > platform_device. That's pretty much it.
> >
> > Given the above explanation, do you still think the additional
> > complexity introduced by the vchiq bus type is worth it (it more or less
> > duplicates a small subset of the platform bus type implementation), and
> > are you fine with a single bus type, even if it doesn't exactly match
> > the firmware layers ?
> >
> >> Signed-off-by: Umang Jain <[email protected]>
> >> ---
> >> .../vc04_services/bcm2835-audio/bcm2835.c | 19 ++-
> >> .../bcm2835-camera/bcm2835-camera.c | 17 ++-
> >> .../interface/vchiq_arm/vchiq_arm.c | 121 +++++++++++++++---
> >> .../interface/vchiq_arm/vchiq_arm.h | 1 +
> >> 4 files changed, 117 insertions(+), 41 deletions(-)
> >>
> >> diff --git a/drivers/staging/vc04_services/bcm2835-audio/bcm2835.c b/drivers/staging/vc04_services/bcm2835-audio/bcm2835.c
> >> index 00bc898b0189..9f3af84f5d5d 100644
> >> --- a/drivers/staging/vc04_services/bcm2835-audio/bcm2835.c
> >> +++ b/drivers/staging/vc04_services/bcm2835-audio/bcm2835.c
> >> @@ -1,12 +1,11 @@
> >> // SPDX-License-Identifier: GPL-2.0
> >> /* Copyright 2011 Broadcom Corporation. All rights reserved. */
> >>
> >> -#include <linux/platform_device.h>
> >> -
> >> #include <linux/init.h>
> >> #include <linux/slab.h>
> >> #include <linux/module.h>
> >>
> >> +#include "../interface/vchiq_arm/vchiq_arm.h"
> >> #include "bcm2835.h"
> >>
> >> static bool enable_hdmi;
> >> @@ -268,9 +267,8 @@ static int snd_add_child_devices(struct device *device, u32 numchans)
> >> return 0;
> >> }
> >>
> >> -static int snd_bcm2835_alsa_probe(struct platform_device *pdev)
> >> +static int snd_bcm2835_alsa_probe(struct device *dev)
> >> {
> >> - struct device *dev = &pdev->dev;
> >> int err;
> >>
> >> if (num_channels <= 0 || num_channels > MAX_SUBSTREAMS) {
> >> @@ -292,30 +290,29 @@ static int snd_bcm2835_alsa_probe(struct platform_device *pdev)
> >>
> >> #ifdef CONFIG_PM
> >>
> >> -static int snd_bcm2835_alsa_suspend(struct platform_device *pdev,
> >> +static int snd_bcm2835_alsa_suspend(struct device *pdev,
> >> pm_message_t state)
> >> {
> >> return 0;
> >> }
> >>
> >> -static int snd_bcm2835_alsa_resume(struct platform_device *pdev)
> >> +static int snd_bcm2835_alsa_resume(struct device *pdev)
> >> {
> >> return 0;
> >> }
> >>
> >> #endif
> >>
> >> -static struct platform_driver bcm2835_alsa_driver = {
> >> +static struct device_driver bcm2835_alsa_driver = {
> >> .probe = snd_bcm2835_alsa_probe,
> >> #ifdef CONFIG_PM
> >> .suspend = snd_bcm2835_alsa_suspend,
> >> .resume = snd_bcm2835_alsa_resume,
> >> #endif
> >> - .driver = {
> >> - .name = "bcm2835_audio",
> >> - },
> >> + .name = "bcm2835_audio",
> >> + .bus = &vchiq_bus_type,
> >> };
> >> -module_platform_driver(bcm2835_alsa_driver);
> >> +module_driver(bcm2835_alsa_driver, driver_register, driver_unregister);
> >
> > Shouldn't you create a struct vchiq_device that wraps struct device, a
> > struct vchiq_driver that wraps struct device_driver, and a
> > module_vchiq_driver() macro ? It shouldn't be up to individual drivers
> > to deal with the plumbing.
> >
> >>
> >> MODULE_AUTHOR("Dom Cobley");
> >> MODULE_DESCRIPTION("Alsa driver for BCM2835 chip");
> >> diff --git a/drivers/staging/vc04_services/bcm2835-camera/bcm2835-camera.c b/drivers/staging/vc04_services/bcm2835-camera/bcm2835-camera.c
> >> index 4f81765912ea..199a49f9ec1e 100644
> >> --- a/drivers/staging/vc04_services/bcm2835-camera/bcm2835-camera.c
> >> +++ b/drivers/staging/vc04_services/bcm2835-camera/bcm2835-camera.c
> >> @@ -24,8 +24,8 @@
> >> #include <media/v4l2-event.h>
> >> #include <media/v4l2-common.h>
> >> #include <linux/delay.h>
> >> -#include <linux/platform_device.h>
> >>
> >> +#include "../interface/vchiq_arm/vchiq_arm.h"
> >> #include "../vchiq-mmal/mmal-common.h"
> >> #include "../vchiq-mmal/mmal-encodings.h"
> >> #include "../vchiq-mmal/mmal-vchiq.h"
> >> @@ -1841,7 +1841,7 @@ static struct v4l2_format default_v4l2_format = {
> >> .fmt.pix.sizeimage = 1024 * 768,
> >> };
> >>
> >> -static int bcm2835_mmal_probe(struct platform_device *pdev)
> >> +static int bcm2835_mmal_probe(struct device *device)
> >> {
> >> int ret;
> >> struct bcm2835_mmal_dev *dev;
> >> @@ -1896,7 +1896,7 @@ static int bcm2835_mmal_probe(struct platform_device *pdev)
> >> &camera_instance);
> >> ret = v4l2_device_register(NULL, &dev->v4l2_dev);
> >> if (ret) {
> >> - dev_err(&pdev->dev, "%s: could not register V4L2 device: %d\n",
> >> + dev_err(device, "%s: could not register V4L2 device: %d\n",
> >> __func__, ret);
> >> goto free_dev;
> >> }
> >> @@ -1976,7 +1976,7 @@ static int bcm2835_mmal_probe(struct platform_device *pdev)
> >> return ret;
> >> }
> >>
> >> -static int bcm2835_mmal_remove(struct platform_device *pdev)
> >> +static int bcm2835_mmal_remove(struct device *device)
> >> {
> >> int camera;
> >> struct vchiq_mmal_instance *instance = gdev[0]->instance;
> >> @@ -1990,15 +1990,14 @@ static int bcm2835_mmal_remove(struct platform_device *pdev)
> >> return 0;
> >> }
> >>
> >> -static struct platform_driver bcm2835_camera_driver = {
> >> +static struct device_driver bcm2835_camera_driver = {
> >> + .name = "bcm2835-camera",
> >> .probe = bcm2835_mmal_probe,
> >> .remove = bcm2835_mmal_remove,
> >> - .driver = {
> >> - .name = "bcm2835-camera",
> >> - },
> >> + .bus = &vchiq_bus_type,
> >> };
> >>
> >> -module_platform_driver(bcm2835_camera_driver)
> >> +module_driver(bcm2835_camera_driver, driver_register, driver_unregister)
> >>
> >> MODULE_DESCRIPTION("Broadcom 2835 MMAL video capture");
> >> MODULE_AUTHOR("Vincent Sanders");
> >> diff --git a/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.c b/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.c
> >> index 22de23f3af02..86c8e5df7cf6 100644
> >> --- a/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.c
> >> +++ b/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.c
> >> @@ -12,6 +12,8 @@
> >> #include <linux/cdev.h>
> >> #include <linux/fs.h>
> >> #include <linux/device.h>
> >> +#include <linux/device/bus.h>
> >> +#include <linux/string.h>
> >> #include <linux/mm.h>
> >> #include <linux/highmem.h>
> >> #include <linux/pagemap.h>
> >> @@ -65,9 +67,6 @@ int vchiq_susp_log_level = VCHIQ_LOG_ERROR;
> >> DEFINE_SPINLOCK(msg_queue_spinlock);
> >> struct vchiq_state g_state;
> >>
> >> -static struct platform_device *bcm2835_camera;
> >> -static struct platform_device *bcm2835_audio;
> >> -
> >> struct vchiq_drvdata {
> >> const unsigned int cache_line_size;
> >> struct rpi_firmware *fw;
> >> @@ -132,6 +131,51 @@ struct vchiq_pagelist_info {
> >> unsigned int scatterlist_mapped;
> >> };
> >>
> >> +struct vchiq_device {
> >> + const char *name;
> >> + struct device dev;
> >> +};
> >
> > Ah there we go :-) Move this structure to a header file that drivers can
> > include. I'd name it vchiq_device.h. The code below should go to
> > vchiq_device.c.
> >
> > I would also move the dev field first.
> >
> >> +
> >> +static ssize_t vchiq_dev_show(struct device *dev,
> >> + struct device_attribute *attr, char *buf)
> >> +{
> >> + struct vchiq_device *device = container_of(dev, struct vchiq_device, dev);
> >> +
> >> + return sprintf(buf, "%s", device->name);
> >> +}
> >> +
> >> +static DEVICE_ATTR_RO(vchiq_dev);
> >> +
> >> +static struct attribute *vchiq_dev_attrs[] = {
> >> + &dev_attr_vchiq_dev.attr,
> >> + NULL
> >> +};
> >> +
> >> +ATTRIBUTE_GROUPS(vchiq_dev);
> >> +
> >> +static const struct device_type vchiq_device_type = {
> >> + .groups = vchiq_dev_groups
> >> +};
> >> +
> >> +static int vchiq_bus_type_match(struct device *dev, struct device_driver *drv)
> >> +{
> >> + if (dev->bus == &vchiq_bus_type &&
> >> + strcmp(dev_name(dev), drv->name) == 0)
> >> + return 1;
> >> + return 0;
> >> +}
> >> +
> >> +struct bus_type vchiq_bus_type = {
> >> + .name = "vchiq-bus",
> >> + .match = vchiq_bus_type_match,
> >> +};
> >> +EXPORT_SYMBOL(vchiq_bus_type);
> >
> > EXPORT_SYMBOL_GPL ?
> >
> >> +
> >> +static const char *const vchiq_devices[] = {
> >> + "bcm2835_audio",
> >> + "bcm2835-camera",
> >> +};
> >
> > This however should stay in this file.
> >
> >> +
> >> static void __iomem *g_regs;
> >> /* This value is the size of the L2 cache lines as understood by the
> >> * VPU firmware, which determines the required alignment of the
> >> @@ -1763,26 +1807,52 @@ static const struct of_device_id vchiq_of_match[] = {
> >> };
> >> MODULE_DEVICE_TABLE(of, vchiq_of_match);
> >>
> >> -static struct platform_device *
> >> +static void
> >> +vchiq_release_device(struct device *dev)
> >> +{
> >> + struct vchiq_device *device;
> >> +
> >> + device = container_of(dev, struct vchiq_device, dev);
> >> + kfree(device);
> >> +}
> >> +
> >> +static int
> >> vchiq_register_child(struct platform_device *pdev, const char *name)
> >
> > Pass a struct device * for the first argument, you don't need a platform
> > device. I'd also name the function vchiq_register_device, and rename the
> > pdev parameter to parent.
> >
> >> {
> >> - struct platform_device_info pdevinfo;
> >> - struct platform_device *child;
> >> + struct vchiq_device *device = NULL;
> >> + int ret;
> >>
> >> - memset(&pdevinfo, 0, sizeof(pdevinfo));
> >> + device = kzalloc(sizeof(*device), GFP_KERNEL);
> >> + if (!device)
> >> + return -ENOMEM;
> >>
> >> - pdevinfo.parent = &pdev->dev;
> >> - pdevinfo.name = name;
> >> - pdevinfo.id = PLATFORM_DEVID_NONE;
> >> - pdevinfo.dma_mask = DMA_BIT_MASK(32);
> >> + device->name = name;
> >> + device->dev.init_name = name;
> >> + device->dev.parent = &pdev->dev;
> >> + device->dev.bus = &vchiq_bus_type;
> >> + device->dev.type = &vchiq_device_type;
> >> + device->dev.release = vchiq_release_device;
> >> +
> >> + ret = dma_set_mask_and_coherent(&device->dev, DMA_BIT_MASK(32));
> >
> > Do vchiq devices perform DMA ?
>
> As far as I can tell, the devices right now (in the mainline) don't use
> DMA allocation. So it's not relevant right now.
>
> But looking at RPi's BSP - the vc-sm-cma driver does the DMA. So
> probably we want to dma_set_mask_and_coherent() for that only?

I wonder if even that is required. The vc-sm-cma driver uses a platform
device creates as a child of the vchiq, which already has its DMA mask
set to 32-bit.

We can figure this out later when we'll deal with the vc-sm-cma driver.

> >> + if (ret < 0) {
> >> + vchiq_release_device(&device->dev);
> >> + return ret;
> >> + }
> >>
> >> - child = platform_device_register_full(&pdevinfo);
> >> - if (IS_ERR(child)) {
> >> - dev_warn(&pdev->dev, "%s not registered\n", name);
> >> - child = NULL;
> >> + ret = device_register(&device->dev);
> >> + if (ret) {
> >> + put_device(&device->dev);
> >> + return -EINVAL;
> >> }
> >>
> >> - return child;
> >> + return 0;
> >> +}
> >> +
> >> +static int
> >> +vchiq_unregister_child(struct device *dev, void *data)
> >> +{
> >> + device_unregister(dev);
> >> + return 0;
> >> }
> >>
> >> static int vchiq_probe(struct platform_device *pdev)
> >> @@ -1790,7 +1860,7 @@ static int vchiq_probe(struct platform_device *pdev)
> >> struct device_node *fw_node;
> >> const struct of_device_id *of_id;
> >> struct vchiq_drvdata *drvdata;
> >> - int err;
> >> + int i, err;
> >
> > i can be an unsigned int.
> >
> >>
> >> of_id = of_match_node(vchiq_of_match, pdev->dev.of_node);
> >> drvdata = (struct vchiq_drvdata *)of_id->data;
> >> @@ -1832,8 +1902,12 @@ static int vchiq_probe(struct platform_device *pdev)
> >> goto error_exit;
> >> }
> >>
> >> - bcm2835_camera = vchiq_register_child(pdev, "bcm2835-camera");
> >> - bcm2835_audio = vchiq_register_child(pdev, "bcm2835_audio");
> >> + for (i = 0; i < ARRAY_SIZE(vchiq_devices); i++) {
> >> + err = vchiq_register_child(pdev, vchiq_devices[i]);
> >> + if (!err)
> >> + dev_err(&pdev->dev, "Failed to register %s vchiq device\n",
> >> + vchiq_devices[i]);
> >> + }
> >>
> >> return 0;
> >>
> >> @@ -1845,8 +1919,8 @@ static int vchiq_probe(struct platform_device *pdev)
> >>
> >> static int vchiq_remove(struct platform_device *pdev)
> >> {
> >> - platform_device_unregister(bcm2835_audio);
> >> - platform_device_unregister(bcm2835_camera);
> >> + bus_for_each_dev(&vchiq_bus_type, NULL, NULL, vchiq_unregister_child);
> >> +
> >> vchiq_debugfs_deinit();
> >> vchiq_deregister_chrdev();
> >>
> >> @@ -1866,6 +1940,10 @@ static int __init vchiq_driver_init(void)
> >> {
> >> int ret;
> >>
> >> + ret = bus_register(&vchiq_bus_type);
> >> + if (ret)
> >> + pr_err("Failed to register %s\n", vchiq_bus_type.name);
> >
> > This should be a fatal error, you should return an error value.
> >
> >> +
> >> ret = platform_driver_register(&vchiq_driver);
> >> if (ret)
> >> pr_err("Failed to register vchiq driver\n");
> >> @@ -1876,6 +1954,7 @@ module_init(vchiq_driver_init);
> >>
> >> static void __exit vchiq_driver_exit(void)
> >> {
> >> + bus_unregister(&vchiq_bus_type);
> >> platform_driver_unregister(&vchiq_driver);
> >> }
> >> module_exit(vchiq_driver_exit);
> >> diff --git a/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.h b/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.h
> >> index 2fb31f9b527f..98c3af32774a 100644
> >> --- a/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.h
> >> +++ b/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.h
> >> @@ -81,6 +81,7 @@ extern int vchiq_susp_log_level;
> >>
> >> extern spinlock_t msg_queue_spinlock;
> >> extern struct vchiq_state g_state;
> >> +extern struct bus_type vchiq_bus_type;
> >>
> >> extern struct vchiq_state *
> >> vchiq_get_state(void);

--
Regards,

Laurent Pinchart