From owner-svn-ports-all@freebsd.org Sat Apr 8 15:49:27 2017 Return-Path: Delivered-To: svn-ports-all@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id AAFABD35512; Sat, 8 Apr 2017 15:49:27 +0000 (UTC) (envelope-from rezny@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 4753B284; Sat, 8 Apr 2017 15:49:27 +0000 (UTC) (envelope-from rezny@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id v38FnQXk025181; Sat, 8 Apr 2017 15:49:26 GMT (envelope-from rezny@FreeBSD.org) Received: (from rezny@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id v38FnQga025176; Sat, 8 Apr 2017 15:49:26 GMT (envelope-from rezny@FreeBSD.org) Message-Id: <201704081549.v38FnQga025176@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: rezny set sender to rezny@FreeBSD.org using -f From: Matthew Rezny Date: Sat, 8 Apr 2017 15:49:26 +0000 (UTC) To: ports-committers@freebsd.org, svn-ports-all@freebsd.org, svn-ports-head@freebsd.org Subject: svn commit: r438051 - in head/graphics/libdrm: . files X-SVN-Group: ports-head MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-ports-all@freebsd.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: SVN commit messages for the ports tree List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 08 Apr 2017 15:49:27 -0000 Author: rezny Date: Sat Apr 8 15:49:25 2017 New Revision: 438051 URL: https://svnweb.freebsd.org/changeset/ports/438051 Log: Update to 2.4.78 and remove the dependency on libdevq The functionality previously provided by libdevq is now implemented directly in libdrm. This direct implementation should be more robust than the prior integration. Initial testing has shown improvements in some scenarios while remaining on-par elsewhere. The ultimate goal of this effort is to have our platform support integrated upstream, which will be easier without the extra dependency. With that in mind, please quickly report any regressions found. Reviewed by: dumbbell (x11), bapt (x11) Approved by: swills (mentor), dumbbell (x11) Differential Revision: https://reviews.freebsd.org/D10184 Added: head/graphics/libdrm/files/patch-xf86drm.h (contents, props changed) Deleted: head/graphics/libdrm/files/Makefile.am head/graphics/libdrm/files/configure.ac head/graphics/libdrm/files/patch-Makefile.in head/graphics/libdrm/files/patch-config.h.in head/graphics/libdrm/files/patch-configure Modified: head/graphics/libdrm/Makefile head/graphics/libdrm/distinfo head/graphics/libdrm/files/extra-xf86drm.c head/graphics/libdrm/files/patch-xf86drm.c head/graphics/libdrm/files/patch-xf86drmMode.c Modified: head/graphics/libdrm/Makefile ============================================================================== --- head/graphics/libdrm/Makefile Sat Apr 8 15:47:03 2017 (r438050) +++ head/graphics/libdrm/Makefile Sat Apr 8 15:49:25 2017 (r438051) @@ -2,9 +2,8 @@ # $FreeBSD$ PORTNAME= libdrm -PORTVERSION= 2.4.75 +PORTVERSION= 2.4.78 PORTEPOCH= 1 -PORTREVISION= 3 CATEGORIES= graphics x11 MASTER_SITES= http://dri.freedesktop.org/libdrm/ @@ -14,8 +13,7 @@ COMMENT= Userspace interface to kernel D LICENSE= MIT BUILD_DEPENDS= ${LOCALBASE}/libdata/pkgconfig/pthread-stubs.pc:devel/libpthread-stubs -LIB_DEPENDS= libpciaccess.so:devel/libpciaccess \ - libdevq.so:devel/libdevq +LIB_DEPENDS= libpciaccess.so:devel/libpciaccess USES= libtool pkgconfig tar:bzip2 GNU_CONFIGURE= yes @@ -74,7 +72,7 @@ PLIST_SUB+= RADEON_DRIVERS="@comment " # This variable is not for users. So use a non-default name, intended for # people working on DRM kernel module development. -.if !defined(LIBDRM_SKIP_RETURN_PATCH) +.if ${OPSYS} == FreeBSD && !defined(LIBDRM_SKIP_RETURN_PATCH) EXTRA_PATCHES+= ${FILESDIR}/extra-xf86drm.c .endif Modified: head/graphics/libdrm/distinfo ============================================================================== --- head/graphics/libdrm/distinfo Sat Apr 8 15:47:03 2017 (r438050) +++ head/graphics/libdrm/distinfo Sat Apr 8 15:49:25 2017 (r438051) @@ -1,3 +1,3 @@ -TIMESTAMP = 1485779860 -SHA256 (libdrm-2.4.75.tar.bz2) = 2d5a500eef412cc287d12268eed79d571e262d4957a2ec9258073f305985054f -SIZE (libdrm-2.4.75.tar.bz2) = 774589 +TIMESTAMP = 1491604820 +SHA256 (libdrm-2.4.78.tar.bz2) = 3dbe2ed3e2ee6ebe8a848195286ef362f66675a7102958e42906d734aa6925d8 +SIZE (libdrm-2.4.78.tar.bz2) = 772306 Modified: head/graphics/libdrm/files/extra-xf86drm.c ============================================================================== --- head/graphics/libdrm/files/extra-xf86drm.c Sat Apr 8 15:47:03 2017 (r438050) +++ head/graphics/libdrm/files/extra-xf86drm.c Sat Apr 8 15:49:25 2017 (r438051) @@ -4,7 +4,7 @@ for now. Newer kernel modules will have dragonfly fixed this issue in, http://gitweb.dragonflybsd.org/dragonfly.git/commit/b922632f623ee2cc2c1346bb3a6894a7756676aa -which will be included in the 4.4 release when it is released. +which has been included since the 4.4 release. --- xf86drm.c.orig 2017-01-30 13:59:15.919081000 +0100 +++ xf86drm.c Modified: head/graphics/libdrm/files/patch-xf86drm.c ============================================================================== --- head/graphics/libdrm/files/patch-xf86drm.c Sat Apr 8 15:47:03 2017 (r438050) +++ head/graphics/libdrm/files/patch-xf86drm.c Sat Apr 8 15:49:25 2017 (r438051) @@ -1,73 +1,411 @@ ---- xf86drm.c.orig 2017-01-30 19:04:05 UTC +--- xf86drm.c.orig 2017-04-07 08:49:13 UTC +++ xf86drm.c -@@ -62,6 +62,10 @@ +@@ -49,6 +49,9 @@ + #include + #include + #include ++#ifdef HAVE_SYS_SYSCTL_H ++#include ++#endif + #include + #define stat_t struct stat + #include +@@ -62,6 +65,10 @@ #endif #include -+#ifdef HAVE_LIBDEVQ -+#include "libdevq.h" ++#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) ++#include +#endif + /* Not all systems have MAP_FAILED defined */ #ifndef MAP_FAILED #define MAP_FAILED ((void *)-1) -@@ -82,8 +86,12 @@ - #define DRM_RENDER_MINOR_NAME "renderD" - #endif +@@ -72,18 +79,8 @@ --#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) + #include "util_math.h" + +-#ifdef __OpenBSD__ +-#define DRM_PRIMARY_MINOR_NAME "drm" +-#define DRM_CONTROL_MINOR_NAME "drmC" +-#define DRM_RENDER_MINOR_NAME "drmR" +-#else +-#define DRM_PRIMARY_MINOR_NAME "card" +-#define DRM_CONTROL_MINOR_NAME "controlD" +-#define DRM_RENDER_MINOR_NAME "renderD" +-#endif +- + #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) -#define DRM_MAJOR 145 -+#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) -+#define DRM_MAJOR 0 -+#endif -+ -+#if defined(__DragonFly__) -+#define DRM_MAJOR 65 /* was 145 XXX needs checking */ ++#define DRM_MAJOR 0 /* Major ID unused on systems with devfs */ #endif #ifdef __NetBSD__ -@@ -548,6 +556,7 @@ static int drmGetMinorType(int minor) - } +@@ -180,7 +177,7 @@ void drmFree(void *pt) } -+#if !defined(__FreeBSD__) && !defined(__FreeBSD_kernel__) && !defined(__DragonFly__) - static const char *drmGetMinorName(int type) - { - switch (type) { -@@ -561,6 +570,7 @@ static const char *drmGetMinorName(int t - return NULL; - } + /** +- * Call ioctl, restarting if it is interupted ++ * Call ioctl, restarting if it is interrupted + */ + int + drmIoctl(int fd, unsigned long request, void *arg) +@@ -223,6 +220,89 @@ drmHashEntry *drmGetEntry(int fd) + return entry; } + ++static int drmGetMinorBase(int type) ++{ ++ switch (type) { ++ case DRM_NODE_PRIMARY: ++ case DRM_NODE_CONTROL: ++ case DRM_NODE_RENDER: ++ return type << 6; ++ default: ++ return -1; ++ }; ++} ++ ++static int drmGetMinorType(int minor) ++{ ++ if (minor < 0) ++ return -1; ++ ++ int type = minor >> 6; ++ switch (type) { ++ case DRM_NODE_PRIMARY: ++ case DRM_NODE_CONTROL: ++ case DRM_NODE_RENDER: ++ return type; ++ default: ++ return -1; ++ } ++} ++ ++#ifdef __linux__ ++static const char *drmGetMinorName(int type) ++{ ++ switch (type) { ++ case DRM_NODE_PRIMARY: ++ return DRM_PRIMARY_MINOR_NAME; ++ case DRM_NODE_CONTROL: ++ return DRM_CONTROL_MINOR_NAME; ++ case DRM_NODE_RENDER: ++ return DRM_RENDER_MINOR_NAME; ++ default: ++ return NULL; ++ } ++} ++#endif ++ ++static const char *drmGetDeviceName(int type) ++{ ++ switch (type) { ++ case DRM_NODE_PRIMARY: ++ return DRM_DEV_NAME; ++ case DRM_NODE_CONTROL: ++ return DRM_CONTROL_DEV_NAME; ++ case DRM_NODE_RENDER: ++ return DRM_RENDER_DEV_NAME; ++ default: ++ return NULL; ++ } ++} ++ ++static int drmGetNodeNumber(const char *name) ++{ ++ size_t name_len = strnlen(name, DRM_NODE_NAME_MAX); ++ while (name_len && isdigit(name[name_len - 1])) ++ --name_len; ++ return strtol(name + name_len, NULL, 10); ++} ++ ++static int drmGetNodeType(const char *name) ++{ ++ if (strncmp(name, DRM_PRIMARY_MINOR_NAME, ++ sizeof(DRM_PRIMARY_MINOR_NAME) - 1) == 0) ++ return DRM_NODE_PRIMARY; ++ ++ if (strncmp(name, DRM_CONTROL_MINOR_NAME, ++ sizeof(DRM_CONTROL_MINOR_NAME ) - 1) == 0) ++ return DRM_NODE_CONTROL; ++ ++ if (strncmp(name, DRM_RENDER_MINOR_NAME, ++ sizeof(DRM_RENDER_MINOR_NAME) - 1) == 0) ++ return DRM_NODE_RENDER; ++ ++ return -1; ++} ++ + /** + * Compare two busid strings + * +@@ -290,7 +370,7 @@ static int drmMatchBusID(const char *id1 + * + * \internal + * Checks for failure. If failure was caused by signal call chown again. +- * If any other failure happened then it will output error mesage using ++ * If any other failure happened then it will output error message using + * drmMsg() call. + */ + #if !defined(UDEV) +@@ -327,8 +407,8 @@ static int chown_check_return(const char + static int drmOpenDevice(dev_t dev, int minor, int type) + { + stat_t st; +- const char *dev_name; +- char buf[64]; ++ const char *dev_name = drmGetDeviceName(type); ++ char buf[DRM_NODE_NAME_MAX]; + int fd; + mode_t devmode = DRM_DEV_MODE, serv_mode; + gid_t serv_group; +@@ -338,21 +418,14 @@ static int drmOpenDevice(dev_t dev, int + gid_t group = DRM_DEV_GID; + #endif + +- switch (type) { +- case DRM_NODE_PRIMARY: +- dev_name = DRM_DEV_NAME; +- break; +- case DRM_NODE_CONTROL: +- dev_name = DRM_CONTROL_DEV_NAME; +- break; +- case DRM_NODE_RENDER: +- dev_name = DRM_RENDER_DEV_NAME; +- break; +- default: ++ if (!dev_name) + return -EINVAL; +- }; + ++#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) ++ sprintf(buf, dev_name, DRM_DIR_NAME, minor + drmGetMinorBase(type)); ++#else + sprintf(buf, dev_name, DRM_DIR_NAME, minor); +#endif + drmMsg("drmOpenDevice: node name is %s\n", buf); + if (drm_server_info && drm_server_info->get_perms) { +@@ -456,27 +529,20 @@ wait_for_udev: + static int drmOpenMinor(int minor, int create, int type) + { + int fd; +- char buf[64]; +- const char *dev_name; ++ char buf[DRM_NODE_NAME_MAX]; ++ const char *dev_name = drmGetDeviceName(type); + + if (create) + return drmOpenDevice(makedev(DRM_MAJOR, minor), minor, type); + +- switch (type) { +- case DRM_NODE_PRIMARY: +- dev_name = DRM_DEV_NAME; +- break; +- case DRM_NODE_CONTROL: +- dev_name = DRM_CONTROL_DEV_NAME; +- break; +- case DRM_NODE_RENDER: +- dev_name = DRM_RENDER_DEV_NAME; +- break; +- default: ++ if (!dev_name) + return -EINVAL; +- }; + ++#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) ++ sprintf(buf, dev_name, DRM_DIR_NAME, minor + drmGetMinorBase(type)); ++#else + sprintf(buf, dev_name, DRM_DIR_NAME, minor); ++#endif + if ((fd = open(buf, O_RDWR, 0)) >= 0) + return fd; + return -errno; +@@ -517,51 +583,6 @@ int drmAvailable(void) + return retval; + } + +-static int drmGetMinorBase(int type) +-{ +- switch (type) { +- case DRM_NODE_PRIMARY: +- return 0; +- case DRM_NODE_CONTROL: +- return 64; +- case DRM_NODE_RENDER: +- return 128; +- default: +- return -1; +- }; +-} +- +-static int drmGetMinorType(int minor) +-{ +- int type = minor >> 6; +- +- if (minor < 0) +- return -1; +- +- switch (type) { +- case DRM_NODE_PRIMARY: +- case DRM_NODE_CONTROL: +- case DRM_NODE_RENDER: +- return type; +- default: +- return -1; +- } +-} +- +-static const char *drmGetMinorName(int type) +-{ +- switch (type) { +- case DRM_NODE_PRIMARY: +- return DRM_PRIMARY_MINOR_NAME; +- case DRM_NODE_CONTROL: +- return DRM_CONTROL_MINOR_NAME; +- case DRM_NODE_RENDER: +- return DRM_RENDER_MINOR_NAME; +- default: +- return NULL; +- } +-} +- /** * Open the device by bus ID. -@@ -2734,7 +2744,7 @@ int drmGetNodeTypeFromFd(int fd) - maj = major(sbuf.st_rdev); - min = minor(sbuf.st_rdev); + * +@@ -2705,33 +2726,40 @@ int drmDropMaster(int fd) -- if (maj != DRM_MAJOR || !S_ISCHR(sbuf.st_mode)) { -+ if (DRM_MAJOR && maj != DRM_MAJOR || !S_ISCHR(sbuf.st_mode)) { - errno = EINVAL; - return -1; + char *drmGetDeviceNameFromFd(int fd) + { +- char name[128]; +- struct stat sbuf; +- dev_t d; +- int i; +- + /* The whole drmOpen thing is a fiasco and we need to find a way + * back to just using open(2). For now, however, lets just make + * things worse with even more ad hoc directory walking code to + * discover the device file name. */ + ++ stat_t sbuf; + fstat(fd, &sbuf); +- d = sbuf.st_rdev; ++ dev_t d = sbuf.st_rdev; + +- for (i = 0; i < DRM_MAX_MINOR; i++) { ++ for (int i = 0; i < DRM_MAX_MINOR; i++) { ++ char name[DRM_NODE_NAME_MAX]; + snprintf(name, sizeof name, DRM_DEV_NAME, DRM_DIR_NAME, i); + if (stat(name, &sbuf) == 0 && sbuf.st_rdev == d) +- break; ++ return strdup(name); } -@@ -2833,6 +2843,15 @@ static char *drmGetMinorNameForFD(int fd +- if (i == DRM_MAX_MINOR) +- return NULL; +- +- return strdup(name); ++ return NULL; + } + + int drmGetNodeTypeFromFd(int fd) + { +- struct stat sbuf; ++#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) ++ char *name = drmGetDeviceNameFromFd2(fd); ++ if (!name) { ++ errno = ENODEV; ++ return -1; ++ } ++ ++ int type = drmGetNodeType(name); ++ free(name); ++ if (type < 0) ++ errno = ENODEV; ++ return type; ++#else ++ stat_t sbuf; + int maj, min, type; + + if (fstat(fd, &sbuf)) +@@ -2749,6 +2777,7 @@ int drmGetNodeTypeFromFd(int fd) + if (type == -1) + errno = ENODEV; + return type; ++#endif + } + + int drmPrimeHandleToFD(int fd, uint32_t handle, uint32_t flags, int *prime_fd) +@@ -2788,7 +2817,7 @@ static char *drmGetMinorNameForFD(int fd + #ifdef __linux__ + DIR *sysdir; + struct dirent *pent, *ent; +- struct stat sbuf; ++ stat_t sbuf; + const char *name = drmGetMinorName(type); + int len; + char dev_name[64], buf[64]; +@@ -2839,13 +2868,35 @@ static char *drmGetMinorNameForFD(int fd out_close_dir: closedir(sysdir); +#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) -+ struct stat buf; -+ char name[64]; -+ -+ fstat(fd, &buf); -+ snprintf(name, sizeof(name), "/dev/%s", -+ devname(buf.st_rdev, S_IFCHR)); ++ const char *dev_name = drmGetDeviceName(type); ++ if (!dev_name) ++ return NULL; ++ ++ char *name = drmGetDeviceNameFromFd2(fd); ++ if (!name) ++ return NULL; ++ ++ int oldnum = drmGetNodeNumber(name); ++ int oldtype = drmGetMinorType(oldnum); ++ if (oldtype < 0) { ++ free(name); ++ return NULL; ++ } + -+ return strdup(name); ++ int newnum = oldnum - drmGetMinorBase(oldtype) + drmGetMinorBase(type); ++ snprintf(name, DRM_NODE_NAME_MAX, dev_name, DRM_DIR_NAME, newnum); ++ return name; #else - struct stat sbuf; - char buf[PATH_MAX + 1]; -@@ -2960,7 +2979,7 @@ static int drmParseSubsystemType(int maj +- struct stat sbuf; +- char buf[PATH_MAX + 1]; +- const char *dev_name; ++ stat_t sbuf; ++ char buf[DRM_NODE_NAME_MAX]; ++ const char *dev_name = drmGetDeviceName(type); + unsigned int maj, min; + int n, base; + ++ if (!dev_name) ++ return NULL; ++ + if (fstat(fd, &sbuf)) + return NULL; + +@@ -2855,20 +2906,6 @@ out_close_dir: + if (maj != DRM_MAJOR || !S_ISCHR(sbuf.st_mode)) + return NULL; + +- switch (type) { +- case DRM_NODE_PRIMARY: +- dev_name = DRM_DEV_NAME; +- break; +- case DRM_NODE_CONTROL: +- dev_name = DRM_CONTROL_DEV_NAME; +- break; +- case DRM_NODE_RENDER: +- dev_name = DRM_RENDER_DEV_NAME; +- break; +- default: +- return NULL; +- }; +- + base = drmGetMinorBase(type); + if (base < 0) + return NULL; +@@ -2966,7 +3003,7 @@ static int drmParseSubsystemType(int maj return DRM_BUS_HOST1X; return -EINVAL; @@ -76,163 +414,389 @@ return DRM_BUS_PCI; #else #warning "Missing implementation of drmParseSubsystemType" -@@ -3023,6 +3042,61 @@ static int drmParsePciBusInfo(int maj, i +@@ -2974,7 +3011,8 @@ static int drmParseSubsystemType(int maj #endif } -+#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) -+/* -+ * XXX temporary workaround, because FreeBSD doesn't provide -+ * pcibus device sysctl trees for renderD and controlD nodes (yet) -+ */ -+static void -+drmBSDDeviceNameHack(const char *path, char *hacked_path, int length) -+{ -+ int start, number; -+ const char *errstr; -+ -+ if (strcmp(path, DRM_DIR_NAME "/controlD") > 0) { -+ start = 17; -+ number = strtonum(&path[start], 0, 256, &errstr) - 64; -+ snprintf(hacked_path, length, "%s/card%i", DRM_DIR_NAME, number); -+ } else if (strcmp(path, DRM_DIR_NAME "/renderD") > 0) { -+ start = 16; -+ number = strtonum(&path[start], 0, 256, &errstr) - 128; -+ snprintf(hacked_path, length, "%s/card%i", DRM_DIR_NAME, number); -+ } else -+ snprintf(hacked_path, length, "%s", path); -+ -+ return; -+} -+ -+static int -+drmParsePciBusInfoBSD(const char *path, drmPciBusInfoPtr info) -+{ -+ int fd, ret; -+ int domain = 0, bus = 0, slot = 0, function = 0; -+ char hacked_path[PATH_MAX + 1]; +-static int drmParsePciBusInfo(int maj, int min, drmPciBusInfoPtr info) ++static int drmParsePciBusInfo(const char *node, int node_type, ++ int maj, int min, drmPciBusInfoPtr info) + { + #ifdef __linux__ + unsigned int domain, bus, dev, func; +@@ -3023,6 +3061,60 @@ static int drmParsePciBusInfo(int maj, i + info->func = pinfo.func; + + return 0; ++#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) ++ /* ++ * Only the primary nodes can be mapped to hw.dri.%i via major/minor ++ * Determine the primary node by offset and use its major/minor pair ++ */ ++ if (node_type != DRM_NODE_PRIMARY) { ++ char name[DRM_NODE_NAME_MAX]; ++ snprintf(name, sizeof(name), DRM_DEV_NAME, DRM_DIR_NAME, ++ drmGetNodeNumber(node) - drmGetMinorBase(node_type)); ++ ++ stat_t sbuf; ++ if (stat(name, &sbuf)) ++ return -errno; + -+ drmBSDDeviceNameHack(path, hacked_path, PATH_MAX); -+ fd = open(hacked_path, O_RDONLY); ++ maj = major(sbuf.st_rdev); ++ min = minor(sbuf.st_rdev); ++ } ++ /* ++ * Major/minor appear after the driver name in the hw.dri.%i.name node ++ * Find the node with matching major/minor pair and parse the bus ID, ++ * which may be after the name or may be alone in hw.dri.%i.busid ++ */ ++ #define bus_fmt "pci:%04x:%02x:%02x.%u" ++ #define name_fmt "%*s %x " bus_fmt ++ for (int i = 0; i < DRM_MAX_MINOR; ++i) { ++ char name[16], value[256]; ++ size_t length = sizeof(value); ++ snprintf(name, sizeof(name), "hw.dri.%i.name", i); ++ if (sysctlbyname(name, value, &length, NULL, 0)) ++ continue; ++ ++ value[length] = '\0'; ++ unsigned int rdev = 0, domain = 0, bus = 0, slot = 0, func = 0; ++ int vals = sscanf(value, name_fmt, &rdev, &domain, &bus, &slot, &func); ++ ++ if (vals >= 1 && rdev == makedev(maj,min)) { ++ if (vals < 5) { ++ /* busid not in the name, try busid */ ++ length = sizeof(value); ++ snprintf(name, sizeof(name), "hw.dri.%i.busid", i); ++ if (sysctlbyname(name, value, &length, NULL, 0)) ++ break; ++ value[length] = '\0'; ++ if (sscanf(value, bus_fmt, &domain, &bus, &slot, &func) != 4) ++ break; ++ } ++ info->domain = domain; ++ info->bus = bus; ++ info->dev = slot; ++ info->func = func; ++ return 0; ++ } ++ } ++ return -ENODEV; + #else + #warning "Missing implementation of drmParsePciBusInfo" + return -EINVAL; +@@ -3057,32 +3149,6 @@ static int drmCompareBusInfo(drmDevicePt + return -1; + } + +-static int drmGetNodeType(const char *name) +-{ +- if (strncmp(name, DRM_PRIMARY_MINOR_NAME, +- sizeof(DRM_PRIMARY_MINOR_NAME) - 1) == 0) +- return DRM_NODE_PRIMARY; +- +- if (strncmp(name, DRM_CONTROL_MINOR_NAME, +- sizeof(DRM_CONTROL_MINOR_NAME ) - 1) == 0) +- return DRM_NODE_CONTROL; +- +- if (strncmp(name, DRM_RENDER_MINOR_NAME, +- sizeof(DRM_RENDER_MINOR_NAME) - 1) == 0) +- return DRM_NODE_RENDER; +- +- return -EINVAL; +-} +- +-static int drmGetMaxNodeName(void) +-{ +- return sizeof(DRM_DIR_NAME) + +- MAX3(sizeof(DRM_PRIMARY_MINOR_NAME), +- sizeof(DRM_CONTROL_MINOR_NAME), +- sizeof(DRM_RENDER_MINOR_NAME)) + +- 3 /* length of the node number */; +-} +- + #ifdef __linux__ + static int parse_separate_sysfs_files(int maj, int min, + drmPciDeviceInfoPtr device, +@@ -3152,6 +3218,7 @@ static int parse_config_sysfs_file(int m + #endif + + static int drmParsePciDeviceInfo(int maj, int min, ++ drmPciBusInfoPtr info, + drmPciDeviceInfoPtr device, + uint32_t flags) + { +@@ -3188,6 +3255,43 @@ static int drmParsePciDeviceInfo(int maj + device->subdevice_id = pinfo.subdevice_id; + + return 0; ++#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) ++ struct pci_conf_io pc; ++ struct pci_match_conf patterns[1]; ++ struct pci_conf results[1]; + ++ int fd = open("/dev/pci", O_RDONLY, 0); + if (fd < 0) + return -errno; + -+ ret = devq_device_get_pcibusaddr(fd, &domain, &bus, &slot, &function); ++ bzero(&patterns, sizeof(patterns)); ++ patterns[0].pc_sel.pc_domain = info->domain; ++ patterns[0].pc_sel.pc_bus = info->bus; ++ patterns[0].pc_sel.pc_dev = info->dev; ++ patterns[0].pc_sel.pc_func = info->func; ++ patterns[0].flags = PCI_GETCONF_MATCH_DOMAIN | PCI_GETCONF_MATCH_BUS ++ | PCI_GETCONF_MATCH_DEV | PCI_GETCONF_MATCH_FUNC; ++ bzero(&pc, sizeof(struct pci_conf_io)); ++ pc.num_patterns = 1; ++ pc.pat_buf_len = sizeof(patterns); ++ pc.patterns = patterns; ++ pc.match_buf_len = sizeof(results); ++ pc.matches = results; + -+ if (ret < 0) { ++ if (ioctl(fd, PCIOCGETCONF, &pc) || pc.status == PCI_GETCONF_ERROR) { ++ int error = errno; + close(fd); -+ return -1; ++ return -error; + } -+ -+ info->domain = (uint16_t) domain; -+ info->bus = (uint8_t) bus; -+ info->dev = (uint8_t) slot; -+ info->func = (uint8_t) function; -+ + close(fd); -+ return 0; -+} -+#endif -+ - static int drmCompareBusInfo(drmDevicePtr a, drmDevicePtr b) - { - if (a == NULL || b == NULL) -@@ -3188,6 +3262,36 @@ static int drmParsePciDeviceInfo(int maj - #endif - } - -+#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) -+static int drmParsePciDeviceInfoBSD(const char *path, -+ drmPciDeviceInfoPtr device, -+ uint32_t flags) -+{ -+ int fd, vendor_id = 0, device_id = 0, subvendor_id = 0, -+ subdevice_id = 0, revision_id = 0; -+ char hacked_path[PATH_MAX + 1]; + -+ drmBSDDeviceNameHack(path, hacked_path, PATH_MAX); ++ device->vendor_id = results[0].pc_vendor; ++ device->device_id = results[0].pc_device; ++ device->subvendor_id = results[0].pc_subvendor; ++ device->subdevice_id = results[0].pc_subdevice; ++ device->revision_id = results[0].pc_revid; + -+ fd = open(hacked_path, O_RDONLY); -+ -+ if (fd < 0) -+ return -errno; -+ -+ devq_device_get_pciid_full_from_fd(fd, &vendor_id, &device_id, -+ &subvendor_id, &subdevice_id, &revision_id); -+ -+ device->vendor_id = (uint16_t) vendor_id; -+ device->device_id = (uint16_t) device_id; -+ device->subvendor_id = (uint16_t) subvendor_id; -+ device->subdevice_id = (uint16_t) subdevice_id; -+ device->revision_id = (uint8_t) revision_id; -+ -+ close(fd); + return 0; -+} -+#endif -+ - static void drmFreePlatformDevice(drmDevicePtr device) - { - if (device->deviceinfo.platform) { -@@ -3308,7 +3412,11 @@ static int drmProcessPciDevice(drmDevice + #else + #warning "Missing implementation of drmParsePciDeviceInfo" + return -EINVAL; +@@ -3268,7 +3372,7 @@ static drmDevicePtr drmDeviceAlloc(unsig + unsigned int i; + char *ptr; + +- max_node_length = ALIGN(drmGetMaxNodeName(), sizeof(void *)); ++ max_node_length = ALIGN(DRM_NODE_NAME_MAX, sizeof(void *)); + extra = DRM_NODE_MAX * (sizeof(void *) + max_node_length); + + size = sizeof(*device) + extra + bus_size + device_size; +@@ -3314,7 +3418,7 @@ static int drmProcessPciDevice(drmDevice dev->businfo.pci = (drmPciBusInfoPtr)addr; -+#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) -+ ret = drmParsePciBusInfoBSD(node, dev->businfo.pci); -+#else - ret = drmParsePciBusInfo(maj, min, dev->businfo.pci); -+#endif +- ret = drmParsePciBusInfo(maj, min, dev->businfo.pci); ++ ret = drmParsePciBusInfo(node, node_type, maj, min, dev->businfo.pci); if (ret) goto free_device; -@@ -3316,8 +3424,11 @@ static int drmProcessPciDevice(drmDevice - if (fetch_deviceinfo) { +@@ -3323,7 +3427,7 @@ static int drmProcessPciDevice(drmDevice addr += sizeof(drmPciBusInfo); dev->deviceinfo.pci = (drmPciDeviceInfoPtr)addr; -- -+#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) -+ ret = drmParsePciDeviceInfoBSD(node, dev->deviceinfo.pci, flags); -+#else - ret = drmParsePciDeviceInfo(maj, min, dev->deviceinfo.pci, flags); -+#endif + +- ret = drmParsePciDeviceInfo(maj, min, dev->deviceinfo.pci, flags); ++ ret = drmParsePciDeviceInfo(maj, min, dev->businfo.pci, dev->deviceinfo.pci, flags); if (ret) goto free_device; } -@@ -3786,7 +3897,7 @@ int drmGetDevice2(int fd, uint32_t flags +@@ -3673,7 +3777,7 @@ static void drmFoldDuplicatedDevices(drm + local_devices[i]->available_nodes |= local_devices[j]->available_nodes; + node_type = log2(local_devices[j]->available_nodes); + memcpy(local_devices[i]->nodes[node_type], +- local_devices[j]->nodes[node_type], drmGetMaxNodeName()); ++ local_devices[j]->nodes[node_type], DRM_NODE_NAME_MAX); + drmFreeDevice(&local_devices[j]); + } + } +@@ -3691,7 +3795,7 @@ drm_device_validate_flags(uint32_t flags + * Get information about the opened drm device + * + * \param fd file descriptor of the drm device +- * \param flags feature/behaviour bitmask ++ * \param flags feature/behavior bitmask + * \param device the address of a drmDevicePtr where the information + * will be allocated in stored + * +@@ -3709,8 +3813,8 @@ int drmGetDevice2(int fd, uint32_t flags + * Avoid stat'ing all of /dev needlessly by implementing this custom path. + */ + drmDevicePtr d; +- struct stat sbuf; +- char node[PATH_MAX + 1]; ++ stat_t sbuf; ++ char node[DRM_NODE_NAME_MAX]; + const char *dev_name; + int node_type, subsystem_type; + int maj, min, n, ret, base; +@@ -3731,26 +3835,16 @@ int drmGetDevice2(int fd, uint32_t flags + if (node_type == -1) + return -ENODEV; + +- switch (node_type) { +- case DRM_NODE_PRIMARY: +- dev_name = DRM_DEV_NAME; +- break; +- case DRM_NODE_CONTROL: +- dev_name = DRM_CONTROL_DEV_NAME; +- break; +- case DRM_NODE_RENDER: +- dev_name = DRM_RENDER_DEV_NAME; +- break; +- default: ++ dev_name = drmGetDeviceName(node_type); ++ if (!dev_name) + return -EINVAL; +- }; + + base = drmGetMinorBase(node_type); + if (base < 0) + return -EINVAL; + +- n = snprintf(node, PATH_MAX, dev_name, DRM_DIR_NAME, min - base); +- if (n == -1 || n >= PATH_MAX) ++ n = snprintf(node, sizeof(node), dev_name, DRM_DIR_NAME, min - base); ++ if (n == -1 || n >= sizeof(node)) + return -errno; + if (stat(node, &sbuf)) + return -EINVAL; +@@ -3771,8 +3865,8 @@ int drmGetDevice2(int fd, uint32_t flags + drmDevicePtr d; + DIR *sysdir; + struct dirent *dent; +- struct stat sbuf; +- char node[PATH_MAX + 1]; ++ stat_t sbuf; ++ char node[DRM_NODE_NAME_MAX]; + int node_type, subsystem_type; + int maj, min; + int ret, i, node_count; +@@ -3792,7 +3886,7 @@ int drmGetDevice2(int fd, uint32_t flags maj = major(sbuf.st_rdev); min = minor(sbuf.st_rdev); - if (maj != DRM_MAJOR || !S_ISCHR(sbuf.st_mode)) -+ if (DRM_MAJOR && maj != DRM_MAJOR || !S_ISCHR(sbuf.st_mode)) ++ if ((DRM_MAJOR && maj != DRM_MAJOR) || !S_ISCHR(sbuf.st_mode)) return -EINVAL; subsystem_type = drmParseSubsystemType(maj, min); -@@ -3814,7 +3925,7 @@ int drmGetDevice2(int fd, uint32_t flags +@@ -3813,14 +3907,14 @@ int drmGetDevice2(int fd, uint32_t flags + if (node_type < 0) + continue; + +- snprintf(node, PATH_MAX, "%s/%s", DRM_DIR_NAME, dent->d_name); ++ snprintf(node, sizeof(node), "%s/%s", DRM_DIR_NAME, dent->d_name); + if (stat(node, &sbuf)) + continue; + maj = major(sbuf.st_rdev); min = minor(sbuf.st_rdev); - if (maj != DRM_MAJOR || !S_ISCHR(sbuf.st_mode)) -+ if (DRM_MAJOR && maj != DRM_MAJOR || !S_ISCHR(sbuf.st_mode)) ++ if ((DRM_MAJOR && maj != DRM_MAJOR) || !S_ISCHR(sbuf.st_mode)) continue; if (drmParseSubsystemType(maj, min) != subsystem_type) -@@ -3964,7 +4075,7 @@ int drmGetDevices2(uint32_t flags, drmDe +@@ -3918,7 +4012,7 @@ int drmGetDevice(int fd, drmDevicePtr *d + /** + * Get drm devices on the system + * +- * \param flags feature/behaviour bitmask ++ * \param flags feature/behavior bitmask + * \param devices the array of devices with drmDevicePtr elements + * can be NULL to get the device number first + * \param max_devices the maximum number of devices for the array +@@ -3937,8 +4031,8 @@ int drmGetDevices2(uint32_t flags, drmDe + drmDevicePtr device; + DIR *sysdir; + struct dirent *dent; +- struct stat sbuf; +- char node[PATH_MAX + 1]; ++ stat_t sbuf; ++ char node[DRM_NODE_NAME_MAX]; + int node_type, subsystem_type; + int maj, min; + int ret, i, node_count, device_count; +@@ -3963,14 +4057,14 @@ int drmGetDevices2(uint32_t flags, drmDe + if (node_type < 0) + continue; + +- snprintf(node, PATH_MAX, "%s/%s", DRM_DIR_NAME, dent->d_name); ++ snprintf(node, sizeof(node), "%s/%s", DRM_DIR_NAME, dent->d_name); + if (stat(node, &sbuf)) + continue; + maj = major(sbuf.st_rdev); min = minor(sbuf.st_rdev); - if (maj != DRM_MAJOR || !S_ISCHR(sbuf.st_mode)) -+ if (DRM_MAJOR && maj != DRM_MAJOR || !S_ISCHR(sbuf.st_mode)) ++ if ((DRM_MAJOR && maj != DRM_MAJOR) || !S_ISCHR(sbuf.st_mode)) continue; subsystem_type = drmParseSubsystemType(maj, min); -@@ -4108,7 +4219,7 @@ char *drmGetDeviceNameFromFd2(int fd) - maj = major(sbuf.st_rdev); - min = minor(sbuf.st_rdev); +@@ -4078,7 +4172,7 @@ int drmGetDevices(drmDevicePtr devices[] + char *drmGetDeviceNameFromFd2(int fd) + { + #ifdef __linux__ +- struct stat sbuf; ++ stat_t sbuf; + char path[PATH_MAX + 1], *value; + unsigned int maj, min; -- if (maj != DRM_MAJOR || !S_ISCHR(sbuf.st_mode)) -+ if (DRM_MAJOR && maj != DRM_MAJOR || !S_ISCHR(sbuf.st_mode)) +@@ -4101,9 +4195,26 @@ char *drmGetDeviceNameFromFd2(int fd) + free(value); + + return strdup(path); ++#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) ++ stat_t sbuf; ++ if (fstat(fd, &sbuf)) ++ return NULL; ++ ++ dev_t rdev = sbuf.st_rdev; ++ /* minor numbers don't depend on node name suffix, search for a match */ ++ for (int i = 0; i < DRM_MAX_MINOR; ++i) { ++ char node[DRM_NODE_NAME_MAX]; ++ for (int j = 0; j < DRM_NODE_MAX; ++j) { ++ snprintf(node, sizeof(node), drmGetDeviceName(j), ++ DRM_DIR_NAME, drmGetMinorBase(j) + i); ++ if (stat(node, &sbuf) == 0 && sbuf.st_rdev == rdev) ++ return strdup(node); ++ } ++ } ++ return NULL; + #else +- struct stat sbuf; +- char node[PATH_MAX + 1]; ++ stat_t sbuf; ++ char node[DRM_NODE_NAME_MAX]; + const char *dev_name; + int node_type; + int maj, min, n, base; +@@ -4121,26 +4232,16 @@ char *drmGetDeviceNameFromFd2(int fd) + if (node_type == -1) return NULL; - node_type = drmGetMinorType(min); +- switch (node_type) { +- case DRM_NODE_PRIMARY: +- dev_name = DRM_DEV_NAME; +- break; +- case DRM_NODE_CONTROL: +- dev_name = DRM_CONTROL_DEV_NAME; +- break; +- case DRM_NODE_RENDER: +- dev_name = DRM_RENDER_DEV_NAME; +- break; +- default: ++ dev_name = drmGetDeviceName(node_type); ++ if (!dev_name) + return NULL; +- }; + + base = drmGetMinorBase(node_type); + if (base < 0) + return NULL; + *** DIFF OUTPUT TRUNCATED AT 1000 LINES ***