@@ -372,14 +372,43 @@ open_master_sysfs_dir(int drm_fd)
{
char path[128];
struct stat st;
+ int sysfs;
if (fstat(drm_fd, &st) || !S_ISCHR(st.st_mode))
return -1;
- snprintf(path, sizeof(path), "/sys/dev/char/%d:0",
- major(st.st_rdev));
+ snprintf(path, sizeof(path), "/sys/dev/char/%d:%d", major(st.st_rdev), minor(st.st_rdev));
+ sysfs = open(path, O_DIRECTORY);
+ if (sysfs < 0)
+ return sysfs;
- return open(path, O_DIRECTORY);
+ if (minor(st.st_rdev) >= 128) {
+ /* If we were given a renderD* drm_fd, find it's associated cardX node. */
+ char device[100], cmp[100];
+ int device_len, cmp_len, i;
+
+ device_len = readlinkat(sysfs, "device", device, sizeof(device));
+ close(sysfs);
+ if (device_len < 0)
+ return device_len;
+
+ for (i = 0; i < 128; i++) {
+
+ snprintf(path, sizeof(path), "/sys/dev/char/%d:%d", major(st.st_rdev), i);
+ sysfs = open(path, O_DIRECTORY);
+ if (sysfs < 0)
+ continue;
+
+ cmp_len = readlinkat(sysfs, "device", cmp, sizeof(cmp));
+ if (cmp_len == device_len && !memcmp(cmp, device, cmp_len))
+ break;
+
+ close(sysfs);
+ sysfs = -1;
+ }
+ }
+
+ return sysfs;
}
struct intel_perf *