v4l2-sysfs-path: print device minor/major of the associated devices
authorMauro Carvalho Chehab <mchehab@redhat.com>
Sun, 23 Aug 2009 15:45:08 +0000 (12:45 -0300)
committerMauro Carvalho Chehab <mchehab@redhat.com>
Sun, 23 Aug 2009 15:45:08 +0000 (12:45 -0300)
From: Mauro Carvalho Chehab <mchehab@redhat.com>

Instead of just printing the associated devices, go further and display
the device major/minors and the associated event interface. The output
will look like:

device     = /dev/video0
bus info   = usb-0000:00:1d.7-8
sysfs path = /sys/devices/pci0000:00/0000:00:1d.7/usb1/1-8
Associated devices:
        usb_endpoint:usbdev1.8_ep00 (dev 252,20)
        i2c-adapter:i2c-4
        input:input9:event6 (dev 13,70)
        sound:pcmC1D0c (dev 116,9)
        sound:dsp1 (dev 14,19)
        sound:audio1 (dev 14,20)
        sound:controlC1 (dev 116,10)
        sound:mixer1 (dev 14,16)
        dvb:dvb0.frontend0 (dev 212,0)
        dvb:dvb0.demux0 (dev 212,1)
        dvb:dvb0.dvr0 (dev 212,2)
        dvb:dvb0.net0 (dev 212,3)

Signed-off-by: Mauro Carvalho Chehab <mchehab@redhat.com>
utils/v4l2-sysfs-path.c

index 90157fa..2a6ab0c 100644 (file)
@@ -136,6 +136,75 @@ err:
        return NULL;
 }
 
+char *seek_name(char *path, char *match)
+{
+       DIR             *dir;
+       struct dirent   *entry;
+       struct stat     st;
+       char            *p;
+       static char     name[1024];
+       int             major, minor;
+
+       dir = opendir(path);
+       if (!dir)
+               return NULL;
+
+       strcpy(name, path);
+       strcat(name, "/");
+       p = name + strlen(name);
+
+       entry = readdir(dir);
+       while (entry) {
+               if (!strncmp(entry->d_name, match, strlen(match))) {
+
+                       strcpy(name, entry->d_name);
+                       closedir(dir);
+                       return name;
+               }
+               entry = readdir(dir);
+       }
+       closedir(dir);
+       return NULL;
+}
+
+int get_dev(char *class, int *major, int *minor, char *extra)
+{
+       char            path[1024];
+       char            *name;
+       FILE            *fp;
+
+       name = strchr(class,':');
+       if (!name)
+               return -1;
+       *name = 0;
+       name++;
+
+       *extra = 0;
+
+       if (!strcmp(class, "input")) {
+               char *event;
+
+               sprintf(path, "/sys/class/%s/%s/", class, name);
+               event = seek_name(path, "event");
+               if (!event)
+                       return -1;
+
+               strcpy(extra, event);
+
+               sprintf(path, "/sys/class/%s/%s/%s/dev", class, name, event);
+
+       } else
+               sprintf(path, "/sys/class/%s/%s/dev", class, name);
+
+       fp = fopen(path, "r");
+       if (!fp)
+               return -1;
+
+       fscanf(fp, "%d:%d", major, minor);
+
+       return 0;
+}
+
 /*
  Examples of subdevs:
        sound:audio1
@@ -156,7 +225,8 @@ void get_subdevs(char *path)
        DIR             *dir;
        struct dirent   *entry;
        struct stat     st;
-        char            *p, name[1024];
+       char            *p, name[1024], extra[20];
+       int             major, minor;
 
        dir = opendir(path);
        if (!dir)
@@ -166,20 +236,28 @@ void get_subdevs(char *path)
        strcat(name, "/");
        p = name + strlen(name);
 
-       printf("Subdevs: ");
+       printf("Associated devices:\n");
        entry = readdir(dir);
        while (entry) {
                strcpy(p, entry->d_name);
-               if ((lstat(name, &st) == 0) && 
+               if ((lstat(name, &st) == 0) &&
                        !S_ISDIR(st.st_mode)) {
                        char *s = strchr(entry->d_name, ':');
-                       if (s)
-                               printf("%s ", entry->d_name);
+                       if (s) {
+                               printf("\t%s", entry->d_name);
+                               if (!get_dev(entry->d_name, &major, &minor, extra))
+                                       if (*extra)
+                                               printf(":%s (dev %d,%d)",
+                                                       extra, major, minor);
+                                       else
+                                               printf(" (dev %d,%d)",
+                                                       major, minor);
+                               printf("\n");
+                       }
                }
                entry = readdir(dir);
        }
        closedir(dir);
-       printf("\n");
 }
 
 void get_sysfs(char *fname)