All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] Rudimentary support for mi_media_detect_type on FreeBSD.
@ 2021-04-04 13:44 Trenton Schulz
  2021-04-06 13:12 ` Hans Petter Selasky
  0 siblings, 1 reply; 4+ messages in thread
From: Trenton Schulz @ 2021-04-04 13:44 UTC (permalink / raw)
  To: linux-media

FreeBSD doesn't have the same uevent and sys filesystem that Linux
does. So, use the VIDIOC_QUERYCAP ioctl to find out basic capabilities
for a device. The ioctl doesn't give us as much information, but it
gets things like webcams, VBIs, and radios. This is better than what
was there previously, which was returning unknown.

This makes some v4l-utils like v4l2-ctl a little more useful.

Signed-off-by: Trenton Schulz <trenton@norwegianrockcat.com>
---
 utils/common/media-info.cpp | 22 +++++++++++++++++++++-
 1 file changed, 21 insertions(+), 1 deletion(-)

diff --git a/utils/common/media-info.cpp b/utils/common/media-info.cpp
index 29a43fb3..3fed5a46 100644
--- a/utils/common/media-info.cpp
+++ b/utils/common/media-info.cpp
@@ -17,6 +17,10 @@
 #include <linux/media.h>
 
 #include <media-info.h>
+#ifndef __linux__
+#include <linux/videodev2.h>
+#include <fcntl.h>
+#endif
 
 static std::string num2s(unsigned num, bool is_hex = true)
 {
@@ -54,7 +58,7 @@ media_type mi_media_detect_type(const char *device)
 
 	if (stat(device, &sb) == -1)
 		return MEDIA_TYPE_CANT_STAT;
-
+#ifdef __linux__
 	std::string uevent_path("/sys/dev/char/");
 
 	uevent_path += num2s(major(sb.st_rdev), false) + ":" +
@@ -90,6 +94,22 @@ media_type mi_media_detect_type(const char *device)
 	}
 
 	uevent_file.close();
+#else // Not Linux
+	int fd = open(device, O_RDONLY);
+	if (fd >= 0) {
+		struct v4l2_capability caps;
+		if (ioctl(fd, VIDIOC_QUERYCAP, &caps) == 0) {
+			if (caps.device_caps & V4L2_CAP_VIDEO_CAPTURE) {
+				return MEDIA_TYPE_VIDEO;
+			} else if (caps.device_caps & V4L2_CAP_VBI_CAPTURE) {
+				return MEDIA_TYPE_VBI;
+			} else if (caps.device_caps & V4L2_CAP_RADIO) {
+				return MEDIA_TYPE_RADIO;
+			}
+		}
+		close(fd);
+	}
+#endif
 	return MEDIA_TYPE_UNKNOWN;
 }
 
-- 
2.31.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] Rudimentary support for mi_media_detect_type on FreeBSD.
  2021-04-04 13:44 [PATCH] Rudimentary support for mi_media_detect_type on FreeBSD Trenton Schulz
@ 2021-04-06 13:12 ` Hans Petter Selasky
  2021-04-06 17:13   ` Trenton Schulz
  0 siblings, 1 reply; 4+ messages in thread
From: Hans Petter Selasky @ 2021-04-06 13:12 UTC (permalink / raw)
  To: Trenton Schulz, linux-media

On 4/4/21 3:44 PM, Trenton Schulz wrote:
> +#else // Not Linux
> +	int fd = open(device, O_RDONLY);
> +	if (fd >= 0) {
> +		struct v4l2_capability caps;
> +		if (ioctl(fd, VIDIOC_QUERYCAP, &caps) == 0) {

Hi,

You should close the "fd" before all returns, in this function. Else it 
might leak.

int error;
error = ioctl(fd ...);
close(fd);

if (error == 0) {
...

--HPS

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] Rudimentary support for mi_media_detect_type on FreeBSD.
  2021-04-06 13:12 ` Hans Petter Selasky
@ 2021-04-06 17:13   ` Trenton Schulz
  2021-04-06 17:38     ` Trenton Schulz
  0 siblings, 1 reply; 4+ messages in thread
From: Trenton Schulz @ 2021-04-06 17:13 UTC (permalink / raw)
  To: Hans Petter Selasky, linux-media

Thank you for the catch. Patch is updated.
FreeBSD doesn't have the same uevent and sys filesystem that Linux
does. So, use the VIDIOC_QUERYCAP ioctl to find out basic capabilities
for a device. The ioctl doesn't give us as much information, but it
gets things like webcams, VBIs, and radios. This is better than what
was there previously, which was returning unknown.

This makes some v4l-utils, like v4l2-ctl, a little more useful.

Signed-off-by: Trenton Schulz <trenton@norwegianrockcat.com>
---
 utils/common/media-info.cpp | 24 +++++++++++++++++++++++-
 1 file changed, 23 insertions(+), 1 deletion(-)

diff --git a/utils/common/media-info.cpp b/utils/common/media-info.cpp
index 29a43fb3..299591bf 100644
--- a/utils/common/media-info.cpp
+++ b/utils/common/media-info.cpp
@@ -17,6 +17,10 @@
 #include <linux/media.h>
 
 #include <media-info.h>
+#ifndef __linux__
+#include <linux/videodev2.h>
+#include <fcntl.h>
+#endif
 
 static std::string num2s(unsigned num, bool is_hex = true)
 {
@@ -54,7 +58,7 @@ media_type mi_media_detect_type(const char *device)
 
 	if (stat(device, &sb) == -1)
 		return MEDIA_TYPE_CANT_STAT;
-
+#ifdef __linux__
 	std::string uevent_path("/sys/dev/char/");
 
 	uevent_path += num2s(major(sb.st_rdev), false) + ":" +
@@ -90,6 +94,24 @@ media_type mi_media_detect_type(const char *device)
 	}
 
 	uevent_file.close();
+#else // Not Linux
+	int fd = open(device, O_RDONLY);
+	if (fd >= 0) {
+		struct v4l2_capability caps;
+		int error = ioctl(fd, VIDIOC_QUERYCAP, &caps);
+		close(fd);
+		if (error == 0) {
+			if (caps.device_caps & V4L2_CAP_VIDEO_CAPTURE)
{
+				return MEDIA_TYPE_VIDEO;
+			} else if (caps.device_caps &
V4L2_CAP_VBI_CAPTURE) {
+				return MEDIA_TYPE_VBI;
+			} else if (caps.device_caps & V4L2_CAP_RADIO)
{
+				return MEDIA_TYPE_RADIO;
+			}
+		}
+		close(fd);
+	}
+#endif
 	return MEDIA_TYPE_UNKNOWN;
 }
 
-- 
2.30.1




^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] Rudimentary support for mi_media_detect_type on FreeBSD.
  2021-04-06 17:13   ` Trenton Schulz
@ 2021-04-06 17:38     ` Trenton Schulz
  0 siblings, 0 replies; 4+ messages in thread
From: Trenton Schulz @ 2021-04-06 17:38 UTC (permalink / raw)
  To: Hans Petter Selasky, linux-media

And of course, we don't need the second close.

FreeBSD doesn't have the same uevent and sys filesystem that Linux
does. So, use the VIDIOC_QUERYCAP ioctl to find out basic capabilities
for a device. The ioctl doesn't give us as much information, but it
gets things like webcams, VBIs, and radios. This is better than what
was there previously, which was returning unknown.

This makes some v4l-utils like v4l2-ctl a little more useful.

Signed-off-by: Trenton Schulz <trenton@norwegianrockcat.com>
---
 utils/common/media-info.cpp | 23 ++++++++++++++++++++++-
 1 file changed, 22 insertions(+), 1 deletion(-)

diff --git a/utils/common/media-info.cpp b/utils/common/media-info.cpp
index 29a43fb3..6138eda4 100644
--- a/utils/common/media-info.cpp
+++ b/utils/common/media-info.cpp
@@ -17,6 +17,10 @@
 #include <linux/media.h>
 
 #include <media-info.h>
+#ifndef __linux__
+#include <linux/videodev2.h>
+#include <fcntl.h>
+#endif
 
 static std::string num2s(unsigned num, bool is_hex = true)
 {
@@ -54,7 +58,7 @@ media_type mi_media_detect_type(const char *device)
 
 	if (stat(device, &sb) == -1)
 		return MEDIA_TYPE_CANT_STAT;
-
+#ifdef __linux__
 	std::string uevent_path("/sys/dev/char/");
 
 	uevent_path += num2s(major(sb.st_rdev), false) + ":" +
@@ -90,6 +94,23 @@ media_type mi_media_detect_type(const char *device)
 	}
 
 	uevent_file.close();
+#else // Not Linux
+	int fd = open(device, O_RDONLY);
+	if (fd >= 0) {
+		struct v4l2_capability caps;
+		int error = ioctl(fd, VIDIOC_QUERYCAP, &caps);
+		close(fd);
+		if (error == 0) {
+			if (caps.device_caps & V4L2_CAP_VIDEO_CAPTURE)
{
+				return MEDIA_TYPE_VIDEO;
+			} else if (caps.device_caps &
V4L2_CAP_VBI_CAPTURE) {
+				return MEDIA_TYPE_VBI;
+			} else if (caps.device_caps & V4L2_CAP_RADIO)
{
+				return MEDIA_TYPE_RADIO;
+			}
+		}
+	}
+#endif
 	return MEDIA_TYPE_UNKNOWN;
 }
 
-- 
2.30.1




^ permalink raw reply related	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2021-04-06 17:38 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-04-04 13:44 [PATCH] Rudimentary support for mi_media_detect_type on FreeBSD Trenton Schulz
2021-04-06 13:12 ` Hans Petter Selasky
2021-04-06 17:13   ` Trenton Schulz
2021-04-06 17:38     ` Trenton Schulz

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.