From bdf280f8e3e92911f51c52d4041c762c59d21abd Mon Sep 17 00:00:00 2001 From: Michał Winiarski Date: Wed, 13 Mar 2019 12:35:12 +0100 Subject: lib/igt_device: Introduce igt_device_get_card_index MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit And use it! But let's start small. Rather than going with "and by the way, here's the card index" from igt_sysfs_path, we're making things more explicit. v2: Drop idx comment. (Chris) Signed-off-by: Michał Winiarski Reviewed-by: Chris Wilson --- lib/igt_device.c | 19 +++++++++++++++++++ lib/igt_device.h | 2 ++ lib/igt_sysfs.c | 12 +++++++----- lib/igt_sysfs.h | 2 +- 4 files changed, 29 insertions(+), 6 deletions(-) (limited to 'lib') diff --git a/lib/igt_device.c b/lib/igt_device.c index 5b3722c8..08f39c8b 100644 --- a/lib/igt_device.c +++ b/lib/igt_device.c @@ -22,6 +22,8 @@ * */ +#include +#include #include "igt.h" #include "igt_device.h" @@ -84,3 +86,20 @@ void igt_device_drop_master(int fd) "Failed to drop DRM master.\n"); } } + +/** + * igt_device_get_card_index: + * @fd: the device + * + * Returns: + * Index (N) of /dev/dri/cardN or /dev/dri/renderDN corresponding with fd. + * + */ +int igt_device_get_card_index(int fd) +{ + struct stat st; + + igt_fail_on(fstat(fd, &st) || !S_ISCHR(st.st_mode)); + + return minor(st.st_rdev); +} diff --git a/lib/igt_device.h b/lib/igt_device.h index 2995f969..9d7dc2c3 100644 --- a/lib/igt_device.h +++ b/lib/igt_device.h @@ -31,4 +31,6 @@ void igt_device_set_master(int fd); int __igt_device_drop_master(int fd); void igt_device_drop_master(int fd); +int igt_device_get_card_index(int fd); + #endif /* __IGT_DEVICE_H__ */ diff --git a/lib/igt_sysfs.c b/lib/igt_sysfs.c index c57e4ae2..5a25d579 100644 --- a/lib/igt_sysfs.c +++ b/lib/igt_sysfs.c @@ -41,6 +41,7 @@ #include "igt_core.h" #include "igt_sysfs.h" +#include "igt_device.h" /** * SECTION:igt_sysfs @@ -89,14 +90,13 @@ static int writeN(int fd, const char *buf, int len) * @device: fd of the device * @path: buffer to fill with the sysfs path to the device * @pathlen: length of @path buffer - * @idx: optional pointer to store the card index of the opened device * * This finds the sysfs directory corresponding to @device. * * Returns: * The directory path, or NULL on failure. */ -char *igt_sysfs_path(int device, char *path, int pathlen, int *idx) +char *igt_sysfs_path(int device, char *path, int pathlen) { struct stat st; @@ -125,8 +125,7 @@ char *igt_sysfs_path(int device, char *path, int pathlen, int *idx) continue; path[len] = '\0'; - if (idx) - *idx = n; + return path; } @@ -148,9 +147,12 @@ int igt_sysfs_open(int device, int *idx) { char path[80]; - if (!igt_sysfs_path(device, path, sizeof(path), idx)) + if (!igt_sysfs_path(device, path, sizeof(path))) return -1; + if (idx) + *idx = igt_device_get_card_index(device); + return open(path, O_RDONLY); } diff --git a/lib/igt_sysfs.h b/lib/igt_sysfs.h index 2ce5b7bd..b181a95f 100644 --- a/lib/igt_sysfs.h +++ b/lib/igt_sysfs.h @@ -28,7 +28,7 @@ #include #include -char *igt_sysfs_path(int device, char *path, int pathlen, int *idx); +char *igt_sysfs_path(int device, char *path, int pathlen); int igt_sysfs_open(int device, int *idx); int igt_sysfs_open_parameters(int device); bool igt_sysfs_set_parameter(int device, -- cgit v1.2.3