From 57e0b0552e11b3f04e6d5704c1c7efea5f83ffe4 Mon Sep 17 00:00:00 2001 From: Simon Ser Date: Mon, 1 Mar 2021 10:30:57 +0100 Subject: [PATCH] xf86drm: add drmGetDeviceFromDevId This adds a function to get a drmDevicePtr from a dev_t identifier of a device. This is useful for Wayland that uses these to identify devices over the protocol. This is done by taking the implementation of drmGetDevice2, and removing the call to fstat to find the dev_t. Signed-off-by: Scott Anderson Signed-off-by: Simon Ser Reviewed-by: Daniel Stone --- core-symbols.txt | 1 + xf86drm.c | 64 ++++++++++++++++++++++++++++++------------------ xf86drm.h | 2 ++ 3 files changed, 43 insertions(+), 24 deletions(-) diff --git a/core-symbols.txt b/core-symbols.txt index 8e725132..31bbcf8f 100644 --- a/core-symbols.txt +++ b/core-symbols.txt @@ -58,6 +58,7 @@ drmGetContextPrivateMapping drmGetContextTag drmGetDevice drmGetDevice2 +drmGetDeviceFromDevId drmGetDeviceNameFromFd drmGetDeviceNameFromFd2 drmGetDevices diff --git a/xf86drm.c b/xf86drm.c index 2abc744e..17ff2882 100644 --- a/xf86drm.c +++ b/xf86drm.c @@ -4497,19 +4497,16 @@ drm_device_has_rdev(drmDevicePtr device, dev_t find_rdev) #define MAX_DRM_NODES 256 /** - * Get information about the opened drm device + * Get information about a device from its dev_t identifier * - * \param fd file descriptor of the drm device + * \param find_rdev dev_t identifier of the device * \param flags feature/behaviour bitmask * \param device the address of a drmDevicePtr where the information * will be allocated in stored * * \return zero on success, negative error code otherwise. - * - * \note Unlike drmGetDevice it does not retrieve the pci device revision field - * unless the DRM_DEVICE_GET_PCI_REVISION \p flag is set. */ -drm_public int drmGetDevice2(int fd, uint32_t flags, drmDevicePtr *device) +drm_public int drmGetDeviceFromDevId(dev_t find_rdev, uint32_t flags, drmDevicePtr *device) { #ifdef __OpenBSD__ /* @@ -4518,22 +4515,18 @@ drm_public int drmGetDevice2(int fd, uint32_t flags, drmDevicePtr *device) * Avoid stat'ing all of /dev needlessly by implementing this custom path. */ drmDevicePtr d; - struct stat sbuf; char node[PATH_MAX + 1]; const char *dev_name; int node_type, subsystem_type; int maj, min, n, ret; - if (fd == -1 || device == NULL) + if (device == NULL) return -EINVAL; - if (fstat(fd, &sbuf)) - return -errno; + maj = major(find_rdev); + min = minor(find_rdev); - maj = major(sbuf.st_rdev); - min = minor(sbuf.st_rdev); - - if (!drmNodeIsDRM(maj, min) || !S_ISCHR(sbuf.st_mode)) + if (!drmNodeIsDRM(maj, min)) return -EINVAL; node_type = drmGetMinorType(maj, min); @@ -4566,26 +4559,20 @@ drm_public int drmGetDevice2(int fd, uint32_t flags, drmDevicePtr *device) drmDevicePtr d; DIR *sysdir; struct dirent *dent; - struct stat sbuf; int subsystem_type; int maj, min; int ret, i, node_count; - dev_t find_rdev; if (drm_device_validate_flags(flags)) return -EINVAL; - if (fd == -1 || device == NULL) + if (device == NULL) return -EINVAL; - if (fstat(fd, &sbuf)) - return -errno; + maj = major(find_rdev); + min = minor(find_rdev); - find_rdev = sbuf.st_rdev; - maj = major(sbuf.st_rdev); - min = minor(sbuf.st_rdev); - - if (!drmNodeIsDRM(maj, min) || !S_ISCHR(sbuf.st_mode)) + if (!drmNodeIsDRM(maj, min)) return -EINVAL; subsystem_type = drmParseSubsystemType(maj, min); @@ -4634,6 +4621,35 @@ drm_public int drmGetDevice2(int fd, uint32_t flags, drmDevicePtr *device) #endif } +/** + * Get information about the opened drm device + * + * \param fd file descriptor of the drm device + * \param flags feature/behaviour bitmask + * \param device the address of a drmDevicePtr where the information + * will be allocated in stored + * + * \return zero on success, negative error code otherwise. + * + * \note Unlike drmGetDevice it does not retrieve the pci device revision field + * unless the DRM_DEVICE_GET_PCI_REVISION \p flag is set. + */ +drm_public int drmGetDevice2(int fd, uint32_t flags, drmDevicePtr *device) +{ + struct stat sbuf; + + if (fd == -1) + return -EINVAL; + + if (fstat(fd, &sbuf)) + return -errno; + + if (!S_ISCHR(sbuf.st_mode)) + return -EINVAL; + + return drmGetDeviceFromDevId(sbuf.st_rdev, flags, device); +} + /** * Get information about the opened drm device * diff --git a/xf86drm.h b/xf86drm.h index 31c1e97a..1631396a 100644 --- a/xf86drm.h +++ b/xf86drm.h @@ -917,6 +917,8 @@ extern void drmFreeDevices(drmDevicePtr devices[], int count); extern int drmGetDevice2(int fd, uint32_t flags, drmDevicePtr *device); extern int drmGetDevices2(uint32_t flags, drmDevicePtr devices[], int max_devices); +extern int drmGetDeviceFromDevId(dev_t dev_id, uint32_t flags, drmDevicePtr *device); + extern int drmDevicesEqual(drmDevicePtr a, drmDevicePtr b); extern int drmSyncobjCreate(int fd, uint32_t flags, uint32_t *handle);