Add a --bus-info option to media-ctl which opens the media device
that has this bus info string. That makes it possible to open a specific
media device without having to know the name of the media device.

Similar functionality has been implemented for v4l2-ctl and v4l2-compliance,
and for the cec utilities.

This allows scripts that no longer need to care about the name of a device
node, instead they can find it based on a unique string.

Also extend the -d option to support -d0 as a shorthand for /dev/media0 to
make it consistent with the other utils.

Signed-off-by: Hans Verkuil <hverkuil-ci...@xs4all.nl>
---
diff --git a/utils/media-ctl/options.c b/utils/media-ctl/options.c
index 16367857..0430b8bc 100644
--- a/utils/media-ctl/options.c
+++ b/utils/media-ctl/options.c
@@ -19,13 +19,18 @@
  * along with this program. If not, see <http://www.gnu.org/licenses/>.
  */

+#include <ctype.h>
+#include <dirent.h>
+#include <fcntl.h>
 #include <getopt.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <sys/ioctl.h>
 #include <unistd.h>
 #include <v4l2subdev.h>

+#include <linux/media.h>
 #include <linux/videodev2.h>

 #include "options.h"
@@ -42,7 +47,9 @@ static void usage(const char *argv0)
        unsigned int i;

        printf("%s [options]\n", argv0);
+       printf("-b, --bus-info name     Use the media device with bus info 
equal to name\n");
        printf("-d, --device dev        Media device name (default: %s)\n", 
MEDIA_DEVNAME_DEFAULT);
+       printf("                        If <dev> starts with a digit, then 
/dev/media<dev> is used.\n");
        printf("-e, --entity name       Print the device name associated with 
the given entity\n");
        printf("-V, --set-v4l2 v4l2     Comma-separated list of formats to 
setup\n");
        printf("    --get-v4l2 pad      Print the active format on a given 
pad\n");
@@ -121,6 +128,7 @@ static void usage(const char *argv0)
 #define OPT_GET_DV                     260

 static struct option opts[] = {
+       {"bus-info", 1, 0, 'b'},
        {"device", 1, 0, 'd'},
        {"entity", 1, 0, 'e'},
        {"set-format", 1, 0, 'f'},
@@ -161,6 +169,51 @@ static void list_known_mbus_formats(void)
        }
 }

+static const char *find_bus_info(const char *bus_info)
+{
+       static char newdev[300];
+       struct dirent *ep;
+       DIR *dp;
+
+       dp = opendir("/dev");
+       if (dp == NULL)
+               return NULL;
+
+       while ((ep = readdir(dp))) {
+               const char *name = ep->d_name;
+
+               if (!memcmp(name, "media", 5) && isdigit(name[5])) {
+                       struct media_device_info mdi;
+                       int ret;
+                       int fd;
+
+                       snprintf(newdev, sizeof(newdev), "/dev/%s", name);
+                       fd = open(newdev, O_RDWR);
+                       if (fd < 0)
+                               continue;
+                       ret = ioctl(fd, MEDIA_IOC_DEVICE_INFO, &mdi);
+                       close(fd);
+                       if (!ret && !strcmp(bus_info, mdi.bus_info)) {
+                               closedir(dp);
+                               return newdev;
+                       }
+               }
+       }
+       closedir(dp);
+       return NULL;
+}
+
+static const char *make_devname(const char *device)
+{
+       static char newdev[300];
+
+       if (device[0] >= '0' && device[0] <= '9' && strlen(device) <= 3) {
+               snprintf(newdev, sizeof(newdev), "/dev/media%s", device);
+               return newdev;
+       }
+       return device;
+}
+
 int parse_cmdline(int argc, char **argv)
 {
        int opt;
@@ -171,11 +224,20 @@ int parse_cmdline(int argc, char **argv)
        }

        /* parse options */
-       while ((opt = getopt_long(argc, argv, "d:e:f:hil:prvV:",
+       while ((opt = getopt_long(argc, argv, "b:d:e:f:hil:prvV:",
                                  opts, NULL)) != -1) {
                switch (opt) {
+               case 'b':
+                       media_opts.devname = find_bus_info(optarg);
+                       if (!media_opts.devname) {
+                               fprintf(stderr, "Error: no media device with 
bus info '%s' found\n",
+                                       optarg);
+                               return 1;
+                       }
+                       break;
+
                case 'd':
-                       media_opts.devname = optarg;
+                       media_opts.devname = make_devname(optarg);
                        break;

                case 'e':

Reply via email to