public inbox for linux-media@vger.kernel.org
 help / color / mirror / Atom feed
From: Hans Verkuil <hverkuil@xs4all.nl>
To: linux-media <linux-media@vger.kernel.org>,
	Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Subject: [PATCH] libmediactl.c: add poor man's udev support
Date: Thu, 10 Dec 2015 11:44:43 +0100	[thread overview]
Message-ID: <5669579B.8050706@xs4all.nl> (raw)

If libudev is not available (android!), then just try to find the device in /dev.
It's a poor man's solution, but it is better than nothing.

Signed-off-by: Hans Verkuil <hans.verkuil@cisco.com>

diff --git a/utils/media-ctl/libmediactl.c b/utils/media-ctl/libmediactl.c
index 4a82d24..1577783 100644
--- a/utils/media-ctl/libmediactl.c
+++ b/utils/media-ctl/libmediactl.c
@@ -441,7 +441,10 @@ static int media_get_devname_udev(struct udev *udev,
 	return ret;
 }

-#else	/* HAVE_LIBUDEV */
+#else
+
+#include <dirent.h>
+#include <sys/stat.h>

 struct udev;

@@ -449,10 +452,36 @@ static inline int media_udev_open(struct udev **udev) { return 0; }

 static inline void media_udev_close(struct udev *udev) { }

-static inline int media_get_devname_udev(struct udev *udev,
-		struct media_entity *entity)
+static int media_get_devname_udev(struct udev *udev,
+				  struct media_entity *entity)
 {
-	return -ENOTSUP;
+	DIR *dp;
+	struct dirent *ep;
+	dev_t devnum;
+
+	dp = opendir("/dev");
+	if (dp == NULL) {
+		media_dbg(entity->media, "couldn't open /dev\n");
+		return -ENODEV;
+	}
+	devnum = makedev(entity->info.v4l.major, entity->info.v4l.minor);
+	while ((ep = readdir(dp))) {
+		struct stat st;
+		char fname[256];
+
+		snprintf(fname, sizeof(fname) - 1, "/dev/%s", ep->d_name);
+		fname[sizeof(fname) - 1] = 0;
+		stat(fname, &st);
+		if ((st.st_mode & S_IFMT) != S_IFCHR)
+			continue;
+		if (st.st_rdev == devnum) {
+			strncpy(entity->devname, fname, sizeof(entity->devname));
+                        entity->devname[sizeof(entity->devname) - 1] = '\0';
+			return 0;
+		}
+	}
+	closedir(dp);
+	return -ENODEV;
 }

 #endif	/* HAVE_LIBUDEV */

             reply	other threads:[~2015-12-10 10:41 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-12-10 10:44 Hans Verkuil [this message]
2015-12-13 19:29 ` [PATCH] libmediactl.c: add poor man's udev support Laurent Pinchart

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=5669579B.8050706@xs4all.nl \
    --to=hverkuil@xs4all.nl \
    --cc=laurent.pinchart@ideasonboard.com \
    --cc=linux-media@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox