v4l2-ctl: make VIDIOC_SUBDEV_S_CLIENT_CAP and VIDIOC_SUBDEV_QUERYCAP ioctl optional
v2 cover v4l2-ctl: make VIDIOC_SUBDEV_S_CLIENT_CAP and VIDIOC_SUBDEV_QUERYCAP ioctl optional
Some ioctls are optional and the logic around it handle it nicely, except that doioctl (via doioctl_name) sets app_result static variable to -1 if the ioctl fails (regardless of the errno, which could tell us whether the ioctl fails because it isn't implemented or because of it truly failing).
However, v4l2-ctl exit code is from app_result. So it is possible to have this tool perform the expected task (albeit failing to use an optional ioctl) and still report to the user that it failed to do so.
Let's switch doioctl to use test_ioctl for those optional ioctls.
This migrates two ioctl VIDIOC_SUBDEV_S_CLIENT_CAP and VIDIOC_SUBDEV_QUERYCAP to be optional ioctls.
Note:
- wondering if VIDIOC_SUBDEV_QUERYCAP really is optional considering the code in v4l2-compliance/v4l2-test-subdevs:
fail_on_test(doioctl(node, VIDIOC_SUBDEV_QUERYCAP, &caps)); which also has a nice comment above saying "// Must always be there"
- wondering if there aren't more optional ioctl considering how many doioctl calls aren't actually exiting early the tools, Should we add something like __attribute__ ((warn_unused_result)) so we know for sure doioctl shouldn't be used if we don't check its result, though that wouldn't catch if(do_ioctl(...)) for optional ioctls like VIDIOC_SUBDEV_S_CLIENT_CAP here for example,
- I assume we may want to backport those to stable branches? (e.g. Debian Trixie is using 1.30.1 and I experienced this bug with that package version)
- only tested the lack of VIDIOC_SUBDEV_S_CLIENT_CAP on a 6.1 kernel, didn't test missing VIDIOC_SUBDEV_QUERYCAP,
---
Changes in v2:
- removed first patch implementing a doioctl_opt,
- switched from doioctl_opt to use test_ioctl directly instead,
- added comment for VIDIOC_SUBDEV_S_CLIENT_CAP to highlight it's optional, which matches what the logic introduced in the original commit,
- reworded commit logs,
- Link to v1: https://lore.kernel.org/r/[email protected]
---
Quentin Schulz (2):
v4l2-ctl: do not fail on kernel not implementing VIDIOC_SUBDEV_S_CLIENT_CAP
v4l2-ctl: do not fail on kernel not implementing VIDIOC_SUBDEV_QUERYCAP
utils/v4l2-ctl/v4l2-ctl.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
---
base-commit: cfe205ade3d0d09fabe4efb4fc1326ce7371b3db
change-id: 20250905-opt-ioctl-387192021068
Best regards,
Signature · 1 line
Quentin Schulz <[email protected]>
PATCH v2 1/2 v4l2-ctl: do not fail on kernel not implementing VIDIOC_SUBDEV_S_CLIENT_CAP
From: Quentin Schulz <[email protected]>
VIDIOC_SUBDEV_S_CLIENT_CAP is only available in kernels 6.4 and later.
The current logic has a fallback in case the ioctl fails by simply stating there's no capabilities available for the subdev.
However, doioctl sets app_result on any ioctl fail (regardless of errno, which is set to ENOIOCTLCMD when this ioctl doesn't exist). Considering app_result is used as the exit code of the program, ignoring the return value of doioctl function is not enough to make this ioctl truly optional.
Let's handle this ioctl as optional to avoid kernels lacking it making v4l2-ctl command fail (but still doing what it's been asked to do) by calling test_ioctl directly.
--- utils/v4l2-ctl/v4l2-ctl.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
utils/v4l2-ctl/v4l2-ctl.cpp +3 -1
@@ -1371,7 +1371,9 @@ int main(int argc, char **argv) // exit if this ioctl returns an error. doioctl(fd, VIDIOC_SUBDEV_QUERYCAP, &subdevcap); subdevclientcap.capabilities = ~0ULL; - if (doioctl(fd, VIDIOC_SUBDEV_S_CLIENT_CAP, &subdevclientcap)) + // This ioctl was introduced in kernel 6.4, so don't + // exit if this ioctl returns an error. + if (test_ioctl(fd, VIDIOC_SUBDEV_S_CLIENT_CAP, &subdevclientcap)) subdevclientcap.capabilities = 0ULL; } if (!is_subdev) {
Signature · 1 line
2.51.0
PATCH v2 2/2 v4l2-ctl: do not fail on kernel not implementing VIDIOC_SUBDEV_QUERYCAP
In reply to Quentin SchulzFrom: Quentin Schulz <[email protected]>
The comment above the code snippet specifies that we do not want to fail on kernels not implementing VIDIOC_SUBDEV_QUERYCAP (5.9 and earlier) but calling doioctl will still set app_result to a negative value if the kernel doesn't implement it (this ioctl returning -1 with errno set to ENOTTY). Considering app_result is used as the exit code of the program, ignoring the return value of doioctl function is not enough to make this ioctl truly optional.
Let's handle this ioctl as optional to avoid kernels lacking it making v4l2-ctl command fail (but still doing what it's been asked to do) by calling test_ioctl directly.
--- utils/v4l2-ctl/v4l2-ctl.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
utils/v4l2-ctl/v4l2-ctl.cpp +1 -1
@@ -1369,7 +1369,7 @@ int main(int argc, char **argv) } else if (is_subdev) { // This ioctl was introduced in kernel 5.10, so don't // exit if this ioctl returns an error. - doioctl(fd, VIDIOC_SUBDEV_QUERYCAP, &subdevcap); + test_ioctl(fd, VIDIOC_SUBDEV_QUERYCAP, &subdevcap); subdevclientcap.capabilities = ~0ULL; // This ioctl was introduced in kernel 6.4, so don't // exit if this ioctl returns an error.
Signature · 1 line
2.51.0