3 * User-level interface to DRM device
5 * \author Rickard E. (Rik) Faith <faith@valinux.com>
6 * \author Kevin E. Martin <martin@valinux.com>
10 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
11 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
12 * All Rights Reserved.
14 * Permission is hereby granted, free of charge, to any person obtaining a
15 * copy of this software and associated documentation files (the "Software"),
16 * to deal in the Software without restriction, including without limitation
17 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
18 * and/or sell copies of the Software, and to permit persons to whom the
19 * Software is furnished to do so, subject to the following conditions:
21 * The above copyright notice and this permission notice (including the next
22 * paragraph) shall be included in all copies or substantial portions of the
25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
28 * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
29 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
30 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
31 * DEALINGS IN THE SOFTWARE.
51 #include <sys/types.h>
53 #define stat_t struct stat
54 #include <sys/ioctl.h>
58 #include <sys/mkdev.h>
60 #ifdef MAJOR_IN_SYSMACROS
61 #include <sys/sysmacros.h>
65 /* Not all systems have MAP_FAILED defined */
67 #define MAP_FAILED ((void *)-1)
71 #include "libdrm_macros.h"
73 #include "util_math.h"
76 #define DRM_PRIMARY_MINOR_NAME "drm"
77 #define DRM_CONTROL_MINOR_NAME "drmC"
78 #define DRM_RENDER_MINOR_NAME "drmR"
80 #define DRM_PRIMARY_MINOR_NAME "card"
81 #define DRM_CONTROL_MINOR_NAME "controlD"
82 #define DRM_RENDER_MINOR_NAME "renderD"
85 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
99 #endif /* __OpenBSD__ */
102 #define DRM_MAJOR 226 /* Linux */
105 #define DRM_MSG_VERBOSITY 3
107 #define memclear(s) memset(&s, 0, sizeof(s))
109 static drmServerInfoPtr drm_server_info;
111 void drmSetServerInfo(drmServerInfoPtr info)
113 drm_server_info = info;
117 * Output a message to stderr.
119 * \param format printf() like format string.
122 * This function is a wrapper around vfprintf().
125 static int DRM_PRINTFLIKE(1, 0)
126 drmDebugPrint(const char *format, va_list ap)
128 return vfprintf(stderr, format, ap);
132 drmMsg(const char *format, ...)
136 if (((env = getenv("LIBGL_DEBUG")) && strstr(env, "verbose")) ||
137 (drm_server_info && drm_server_info->debug_print))
139 va_start(ap, format);
140 if (drm_server_info) {
141 drm_server_info->debug_print(format,ap);
143 drmDebugPrint(format, ap);
149 static void *drmHashTable = NULL; /* Context switch callbacks */
151 void *drmGetHashTable(void)
156 void *drmMalloc(int size)
158 return calloc(1, size);
161 void drmFree(void *pt)
167 * Call ioctl, restarting if it is interupted
170 drmIoctl(int fd, unsigned long request, void *arg)
175 ret = ioctl(fd, request, arg);
176 } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
180 static unsigned long drmGetKeyFromFd(int fd)
189 drmHashEntry *drmGetEntry(int fd)
191 unsigned long key = drmGetKeyFromFd(fd);
196 drmHashTable = drmHashCreate();
198 if (drmHashLookup(drmHashTable, key, &value)) {
199 entry = drmMalloc(sizeof(*entry));
202 entry->tagTable = drmHashCreate();
203 drmHashInsert(drmHashTable, key, entry);
211 * Compare two busid strings
216 * \return 1 if matched.
219 * This function compares two bus ID strings. It understands the older
220 * PCI:b:d:f format and the newer pci:oooo:bb:dd.f format. In the format, o is
221 * domain, b is bus, d is device, f is function.
223 static int drmMatchBusID(const char *id1, const char *id2, int pci_domain_ok)
225 /* First, check if the IDs are exactly the same */
226 if (strcasecmp(id1, id2) == 0)
229 /* Try to match old/new-style PCI bus IDs. */
230 if (strncasecmp(id1, "pci", 3) == 0) {
231 unsigned int o1, b1, d1, f1;
232 unsigned int o2, b2, d2, f2;
235 ret = sscanf(id1, "pci:%04x:%02x:%02x.%u", &o1, &b1, &d1, &f1);
238 ret = sscanf(id1, "PCI:%u:%u:%u", &b1, &d1, &f1);
243 ret = sscanf(id2, "pci:%04x:%02x:%02x.%u", &o2, &b2, &d2, &f2);
246 ret = sscanf(id2, "PCI:%u:%u:%u", &b2, &d2, &f2);
251 /* If domains aren't properly supported by the kernel interface,
252 * just ignore them, which sucks less than picking a totally random
253 * card with "open by name"
258 if ((o1 != o2) || (b1 != b2) || (d1 != d2) || (f1 != f2))
267 * Handles error checking for chown call.
269 * \param path to file.
270 * \param id of the new owner.
271 * \param id of the new group.
273 * \return zero if success or -1 if failure.
276 * Checks for failure. If failure was caused by signal call chown again.
277 * If any other failure happened then it will output error mesage using
281 static int chown_check_return(const char *path, uid_t owner, gid_t group)
286 rv = chown(path, owner, group);
287 } while (rv != 0 && errno == EINTR);
292 drmMsg("Failed to change owner or group for file %s! %d: %s\n",
293 path, errno, strerror(errno));
299 * Open the DRM device, creating it if necessary.
301 * \param dev major and minor numbers of the device.
302 * \param minor minor number of the device.
304 * \return a file descriptor on success, or a negative value on error.
307 * Assembles the device name from \p minor and opens it, creating the device
308 * special file node with the major and minor numbers specified by \p dev and
309 * parent directory if necessary and was called by root.
311 static int drmOpenDevice(dev_t dev, int minor, int type)
314 const char *dev_name;
317 mode_t devmode = DRM_DEV_MODE, serv_mode;
320 int isroot = !geteuid();
321 uid_t user = DRM_DEV_UID;
322 gid_t group = DRM_DEV_GID;
326 case DRM_NODE_PRIMARY:
327 dev_name = DRM_DEV_NAME;
329 case DRM_NODE_CONTROL:
330 dev_name = DRM_CONTROL_DEV_NAME;
332 case DRM_NODE_RENDER:
333 dev_name = DRM_RENDER_DEV_NAME;
339 sprintf(buf, dev_name, DRM_DIR_NAME, minor);
340 drmMsg("drmOpenDevice: node name is %s\n", buf);
342 if (drm_server_info && drm_server_info->get_perms) {
343 drm_server_info->get_perms(&serv_group, &serv_mode);
344 devmode = serv_mode ? serv_mode : DRM_DEV_MODE;
345 devmode &= ~(S_IXUSR|S_IXGRP|S_IXOTH);
349 if (stat(DRM_DIR_NAME, &st)) {
351 return DRM_ERR_NOT_ROOT;
352 mkdir(DRM_DIR_NAME, DRM_DEV_DIRMODE);
353 chown_check_return(DRM_DIR_NAME, 0, 0); /* root:root */
354 chmod(DRM_DIR_NAME, DRM_DEV_DIRMODE);
357 /* Check if the device node exists and create it if necessary. */
358 if (stat(buf, &st)) {
360 return DRM_ERR_NOT_ROOT;
362 mknod(buf, S_IFCHR | devmode, dev);
365 if (drm_server_info && drm_server_info->get_perms) {
366 group = ((int)serv_group >= 0) ? serv_group : DRM_DEV_GID;
367 chown_check_return(buf, user, group);
371 /* if we modprobed then wait for udev */
375 if (stat(DRM_DIR_NAME, &st)) {
379 if (udev_count == 50)
384 if (stat(buf, &st)) {
388 if (udev_count == 50)
395 fd = open(buf, O_RDWR, 0);
396 drmMsg("drmOpenDevice: open result is %d, (%s)\n",
397 fd, fd < 0 ? strerror(errno) : "OK");
402 /* Check if the device node is not what we expect it to be, and recreate it
403 * and try again if so.
405 if (st.st_rdev != dev) {
407 return DRM_ERR_NOT_ROOT;
409 mknod(buf, S_IFCHR | devmode, dev);
410 if (drm_server_info && drm_server_info->get_perms) {
411 chown_check_return(buf, user, group);
415 fd = open(buf, O_RDWR, 0);
416 drmMsg("drmOpenDevice: open result is %d, (%s)\n",
417 fd, fd < 0 ? strerror(errno) : "OK");
421 drmMsg("drmOpenDevice: Open failed\n");
429 * Open the DRM device
431 * \param minor device minor number.
432 * \param create allow to create the device if set.
434 * \return a file descriptor on success, or a negative value on error.
437 * Calls drmOpenDevice() if \p create is set, otherwise assembles the device
438 * name from \p minor and opens it.
440 static int drmOpenMinor(int minor, int create, int type)
444 const char *dev_name;
447 return drmOpenDevice(makedev(DRM_MAJOR, minor), minor, type);
450 case DRM_NODE_PRIMARY:
451 dev_name = DRM_DEV_NAME;
453 case DRM_NODE_CONTROL:
454 dev_name = DRM_CONTROL_DEV_NAME;
456 case DRM_NODE_RENDER:
457 dev_name = DRM_RENDER_DEV_NAME;
463 sprintf(buf, dev_name, DRM_DIR_NAME, minor);
464 if ((fd = open(buf, O_RDWR, 0)) >= 0)
471 * Determine whether the DRM kernel driver has been loaded.
473 * \return 1 if the DRM driver is loaded, 0 otherwise.
476 * Determine the presence of the kernel driver by attempting to open the 0
477 * minor and get version information. For backward compatibility with older
478 * Linux implementations, /proc/dri is also checked.
480 int drmAvailable(void)
482 drmVersionPtr version;
486 if ((fd = drmOpenMinor(0, 1, DRM_NODE_PRIMARY)) < 0) {
488 /* Try proc for backward Linux compatibility */
489 if (!access("/proc/dri/0", R_OK))
495 if ((version = drmGetVersion(fd))) {
497 drmFreeVersion(version);
504 static int drmGetMinorBase(int type)
507 case DRM_NODE_PRIMARY:
509 case DRM_NODE_CONTROL:
511 case DRM_NODE_RENDER:
518 static int drmGetMinorType(int minor)
520 int type = minor >> 6;
526 case DRM_NODE_PRIMARY:
527 case DRM_NODE_CONTROL:
528 case DRM_NODE_RENDER:
535 static const char *drmGetMinorName(int type)
538 case DRM_NODE_PRIMARY:
539 return DRM_PRIMARY_MINOR_NAME;
540 case DRM_NODE_CONTROL:
541 return DRM_CONTROL_MINOR_NAME;
542 case DRM_NODE_RENDER:
543 return DRM_RENDER_MINOR_NAME;
550 * Open the device by bus ID.
552 * \param busid bus ID.
553 * \param type device node type.
555 * \return a file descriptor on success, or a negative value on error.
558 * This function attempts to open every possible minor (up to DRM_MAX_MINOR),
559 * comparing the device bus ID with the one supplied.
561 * \sa drmOpenMinor() and drmGetBusid().
563 static int drmOpenByBusid(const char *busid, int type)
565 int i, pci_domain_ok = 1;
569 int base = drmGetMinorBase(type);
574 drmMsg("drmOpenByBusid: Searching for BusID %s\n", busid);
575 for (i = base; i < base + DRM_MAX_MINOR; i++) {
576 fd = drmOpenMinor(i, 1, type);
577 drmMsg("drmOpenByBusid: drmOpenMinor returns %d\n", fd);
579 /* We need to try for 1.4 first for proper PCI domain support
580 * and if that fails, we know the kernel is busted
584 sv.drm_dd_major = -1; /* Don't care */
585 sv.drm_dd_minor = -1; /* Don't care */
586 if (drmSetInterfaceVersion(fd, &sv)) {
592 sv.drm_dd_major = -1; /* Don't care */
593 sv.drm_dd_minor = -1; /* Don't care */
594 drmMsg("drmOpenByBusid: Interface 1.4 failed, trying 1.1\n");
595 drmSetInterfaceVersion(fd, &sv);
597 buf = drmGetBusid(fd);
598 drmMsg("drmOpenByBusid: drmGetBusid reports %s\n", buf);
599 if (buf && drmMatchBusID(buf, busid, pci_domain_ok)) {
613 * Open the device by name.
615 * \param name driver name.
616 * \param type the device node type.
618 * \return a file descriptor on success, or a negative value on error.
621 * This function opens the first minor number that matches the driver name and
622 * isn't already in use. If it's in use it then it will already have a bus ID
625 * \sa drmOpenMinor(), drmGetVersion() and drmGetBusid().
627 static int drmOpenByName(const char *name, int type)
631 drmVersionPtr version;
633 int base = drmGetMinorBase(type);
639 * Open the first minor number that matches the driver name and isn't
640 * already in use. If it's in use it will have a busid assigned already.
642 for (i = base; i < base + DRM_MAX_MINOR; i++) {
643 if ((fd = drmOpenMinor(i, 1, type)) >= 0) {
644 if ((version = drmGetVersion(fd))) {
645 if (!strcmp(version->name, name)) {
646 drmFreeVersion(version);
647 id = drmGetBusid(fd);
648 drmMsg("drmGetBusid returned '%s'\n", id ? id : "NULL");
657 drmFreeVersion(version);
665 /* Backward-compatibility /proc support */
666 for (i = 0; i < 8; i++) {
667 char proc_name[64], buf[512];
668 char *driver, *pt, *devstring;
671 sprintf(proc_name, "/proc/dri/%d/name", i);
672 if ((fd = open(proc_name, 0, 0)) >= 0) {
673 retcode = read(fd, buf, sizeof(buf)-1);
676 buf[retcode-1] = '\0';
677 for (driver = pt = buf; *pt && *pt != ' '; ++pt)
679 if (*pt) { /* Device is next */
681 if (!strcmp(driver, name)) { /* Match */
682 for (devstring = ++pt; *pt && *pt != ' '; ++pt)
684 if (*pt) { /* Found busid */
685 return drmOpenByBusid(++pt, type);
686 } else { /* No busid */
687 return drmOpenDevice(strtol(devstring, NULL, 0),i, type);
701 * Open the DRM device.
703 * Looks up the specified name and bus ID, and opens the device found. The
704 * entry in /dev/dri is created if necessary and if called by root.
706 * \param name driver name. Not referenced if bus ID is supplied.
707 * \param busid bus ID. Zero if not known.
709 * \return a file descriptor on success, or a negative value on error.
712 * It calls drmOpenByBusid() if \p busid is specified or drmOpenByName()
715 int drmOpen(const char *name, const char *busid)
717 return drmOpenWithType(name, busid, DRM_NODE_PRIMARY);
721 * Open the DRM device with specified type.
723 * Looks up the specified name and bus ID, and opens the device found. The
724 * entry in /dev/dri is created if necessary and if called by root.
726 * \param name driver name. Not referenced if bus ID is supplied.
727 * \param busid bus ID. Zero if not known.
728 * \param type the device node type to open, PRIMARY, CONTROL or RENDER
730 * \return a file descriptor on success, or a negative value on error.
733 * It calls drmOpenByBusid() if \p busid is specified or drmOpenByName()
736 int drmOpenWithType(const char *name, const char *busid, int type)
738 if (!drmAvailable() && name != NULL && drm_server_info &&
739 drm_server_info->load_module) {
740 /* try to load the kernel module */
741 if (!drm_server_info->load_module(name)) {
742 drmMsg("[drm] failed to load kernel module \"%s\"\n", name);
748 int fd = drmOpenByBusid(busid, type);
754 return drmOpenByName(name, type);
759 int drmOpenControl(int minor)
761 return drmOpenMinor(minor, 0, DRM_NODE_CONTROL);
764 int drmOpenRender(int minor)
766 return drmOpenMinor(minor, 0, DRM_NODE_RENDER);
770 * Free the version information returned by drmGetVersion().
772 * \param v pointer to the version information.
775 * It frees the memory pointed by \p %v as well as all the non-null strings
778 void drmFreeVersion(drmVersionPtr v)
790 * Free the non-public version information returned by the kernel.
792 * \param v pointer to the version information.
795 * Used by drmGetVersion() to free the memory pointed by \p %v as well as all
796 * the non-null strings pointers in it.
798 static void drmFreeKernelVersion(drm_version_t *v)
810 * Copy version information.
812 * \param d destination pointer.
813 * \param s source pointer.
816 * Used by drmGetVersion() to translate the information returned by the ioctl
817 * interface in a private structure into the public structure counterpart.
819 static void drmCopyVersion(drmVersionPtr d, const drm_version_t *s)
821 d->version_major = s->version_major;
822 d->version_minor = s->version_minor;
823 d->version_patchlevel = s->version_patchlevel;
824 d->name_len = s->name_len;
825 d->name = strdup(s->name);
826 d->date_len = s->date_len;
827 d->date = strdup(s->date);
828 d->desc_len = s->desc_len;
829 d->desc = strdup(s->desc);
834 * Query the driver version information.
836 * \param fd file descriptor.
838 * \return pointer to a drmVersion structure which should be freed with
841 * \note Similar information is available via /proc/dri.
844 * It gets the version information via successive DRM_IOCTL_VERSION ioctls,
845 * first with zeros to get the string lengths, and then the actually strings.
846 * It also null-terminates them since they might not be already.
848 drmVersionPtr drmGetVersion(int fd)
850 drmVersionPtr retval;
851 drm_version_t *version = drmMalloc(sizeof(*version));
855 if (drmIoctl(fd, DRM_IOCTL_VERSION, version)) {
856 drmFreeKernelVersion(version);
860 if (version->name_len)
861 version->name = drmMalloc(version->name_len + 1);
862 if (version->date_len)
863 version->date = drmMalloc(version->date_len + 1);
864 if (version->desc_len)
865 version->desc = drmMalloc(version->desc_len + 1);
867 if (drmIoctl(fd, DRM_IOCTL_VERSION, version)) {
868 drmMsg("DRM_IOCTL_VERSION: %s\n", strerror(errno));
869 drmFreeKernelVersion(version);
873 /* The results might not be null-terminated strings, so terminate them. */
874 if (version->name_len) version->name[version->name_len] = '\0';
875 if (version->date_len) version->date[version->date_len] = '\0';
876 if (version->desc_len) version->desc[version->desc_len] = '\0';
878 retval = drmMalloc(sizeof(*retval));
879 drmCopyVersion(retval, version);
880 drmFreeKernelVersion(version);
886 * Get version information for the DRM user space library.
888 * This version number is driver independent.
890 * \param fd file descriptor.
892 * \return version information.
895 * This function allocates and fills a drm_version structure with a hard coded
898 drmVersionPtr drmGetLibVersion(int fd)
900 drm_version_t *version = drmMalloc(sizeof(*version));
903 * NOTE THIS MUST NOT GO ABOVE VERSION 1.X due to drivers needing it
904 * revision 1.0.x = original DRM interface with no drmGetLibVersion
905 * entry point and many drm<Device> extensions
906 * revision 1.1.x = added drmCommand entry points for device extensions
907 * added drmGetLibVersion to identify libdrm.a version
908 * revision 1.2.x = added drmSetInterfaceVersion
909 * modified drmOpen to handle both busid and name
910 * revision 1.3.x = added server + memory manager
912 version->version_major = 1;
913 version->version_minor = 3;
914 version->version_patchlevel = 0;
916 return (drmVersionPtr)version;
919 int drmGetCap(int fd, uint64_t capability, uint64_t *value)
921 struct drm_get_cap cap;
925 cap.capability = capability;
927 ret = drmIoctl(fd, DRM_IOCTL_GET_CAP, &cap);
935 int drmSetClientCap(int fd, uint64_t capability, uint64_t value)
937 struct drm_set_client_cap cap;
940 cap.capability = capability;
943 return drmIoctl(fd, DRM_IOCTL_SET_CLIENT_CAP, &cap);
947 * Free the bus ID information.
949 * \param busid bus ID information string as given by drmGetBusid().
952 * This function is just frees the memory pointed by \p busid.
954 void drmFreeBusid(const char *busid)
956 drmFree((void *)busid);
961 * Get the bus ID of the device.
963 * \param fd file descriptor.
965 * \return bus ID string.
968 * This function gets the bus ID via successive DRM_IOCTL_GET_UNIQUE ioctls to
969 * get the string length and data, passing the arguments in a drm_unique
972 char *drmGetBusid(int fd)
978 if (drmIoctl(fd, DRM_IOCTL_GET_UNIQUE, &u))
980 u.unique = drmMalloc(u.unique_len + 1);
981 if (drmIoctl(fd, DRM_IOCTL_GET_UNIQUE, &u))
983 u.unique[u.unique_len] = '\0';
990 * Set the bus ID of the device.
992 * \param fd file descriptor.
993 * \param busid bus ID string.
995 * \return zero on success, negative on failure.
998 * This function is a wrapper around the DRM_IOCTL_SET_UNIQUE ioctl, passing
999 * the arguments in a drm_unique structure.
1001 int drmSetBusid(int fd, const char *busid)
1006 u.unique = (char *)busid;
1007 u.unique_len = strlen(busid);
1009 if (drmIoctl(fd, DRM_IOCTL_SET_UNIQUE, &u)) {
1015 int drmGetMagic(int fd, drm_magic_t * magic)
1022 if (drmIoctl(fd, DRM_IOCTL_GET_MAGIC, &auth))
1024 *magic = auth.magic;
1028 int drmAuthMagic(int fd, drm_magic_t magic)
1034 if (drmIoctl(fd, DRM_IOCTL_AUTH_MAGIC, &auth))
1040 * Specifies a range of memory that is available for mapping by a
1043 * \param fd file descriptor.
1044 * \param offset usually the physical address. The actual meaning depends of
1045 * the \p type parameter. See below.
1046 * \param size of the memory in bytes.
1047 * \param type type of the memory to be mapped.
1048 * \param flags combination of several flags to modify the function actions.
1049 * \param handle will be set to a value that may be used as the offset
1050 * parameter for mmap().
1052 * \return zero on success or a negative value on error.
1054 * \par Mapping the frame buffer
1055 * For the frame buffer
1056 * - \p offset will be the physical address of the start of the frame buffer,
1057 * - \p size will be the size of the frame buffer in bytes, and
1058 * - \p type will be DRM_FRAME_BUFFER.
1061 * The area mapped will be uncached. If MTRR support is available in the
1062 * kernel, the frame buffer area will be set to write combining.
1064 * \par Mapping the MMIO register area
1065 * For the MMIO register area,
1066 * - \p offset will be the physical address of the start of the register area,
1067 * - \p size will be the size of the register area bytes, and
1068 * - \p type will be DRM_REGISTERS.
1070 * The area mapped will be uncached.
1072 * \par Mapping the SAREA
1074 * - \p offset will be ignored and should be set to zero,
1075 * - \p size will be the desired size of the SAREA in bytes,
1076 * - \p type will be DRM_SHM.
1079 * A shared memory area of the requested size will be created and locked in
1080 * kernel memory. This area may be mapped into client-space by using the handle
1083 * \note May only be called by root.
1086 * This function is a wrapper around the DRM_IOCTL_ADD_MAP ioctl, passing
1087 * the arguments in a drm_map structure.
1089 int drmAddMap(int fd, drm_handle_t offset, drmSize size, drmMapType type,
1090 drmMapFlags flags, drm_handle_t *handle)
1095 map.offset = offset;
1099 if (drmIoctl(fd, DRM_IOCTL_ADD_MAP, &map))
1102 *handle = (drm_handle_t)(uintptr_t)map.handle;
1106 int drmRmMap(int fd, drm_handle_t handle)
1111 map.handle = (void *)(uintptr_t)handle;
1113 if(drmIoctl(fd, DRM_IOCTL_RM_MAP, &map))
1119 * Make buffers available for DMA transfers.
1121 * \param fd file descriptor.
1122 * \param count number of buffers.
1123 * \param size size of each buffer.
1124 * \param flags buffer allocation flags.
1125 * \param agp_offset offset in the AGP aperture
1127 * \return number of buffers allocated, negative on error.
1130 * This function is a wrapper around DRM_IOCTL_ADD_BUFS ioctl.
1134 int drmAddBufs(int fd, int count, int size, drmBufDescFlags flags,
1137 drm_buf_desc_t request;
1140 request.count = count;
1141 request.size = size;
1142 request.flags = flags;
1143 request.agp_start = agp_offset;
1145 if (drmIoctl(fd, DRM_IOCTL_ADD_BUFS, &request))
1147 return request.count;
1150 int drmMarkBufs(int fd, double low, double high)
1152 drm_buf_info_t info;
1157 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info))
1163 if (!(info.list = drmMalloc(info.count * sizeof(*info.list))))
1166 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info)) {
1167 int retval = -errno;
1172 for (i = 0; i < info.count; i++) {
1173 info.list[i].low_mark = low * info.list[i].count;
1174 info.list[i].high_mark = high * info.list[i].count;
1175 if (drmIoctl(fd, DRM_IOCTL_MARK_BUFS, &info.list[i])) {
1176 int retval = -errno;
1189 * \param fd file descriptor.
1190 * \param count number of buffers to free.
1191 * \param list list of buffers to be freed.
1193 * \return zero on success, or a negative value on failure.
1195 * \note This function is primarily used for debugging.
1198 * This function is a wrapper around the DRM_IOCTL_FREE_BUFS ioctl, passing
1199 * the arguments in a drm_buf_free structure.
1201 int drmFreeBufs(int fd, int count, int *list)
1203 drm_buf_free_t request;
1206 request.count = count;
1207 request.list = list;
1208 if (drmIoctl(fd, DRM_IOCTL_FREE_BUFS, &request))
1217 * \param fd file descriptor.
1220 * This function closes the file descriptor.
1222 int drmClose(int fd)
1224 unsigned long key = drmGetKeyFromFd(fd);
1225 drmHashEntry *entry = drmGetEntry(fd);
1227 drmHashDestroy(entry->tagTable);
1230 entry->tagTable = NULL;
1232 drmHashDelete(drmHashTable, key);
1240 * Map a region of memory.
1242 * \param fd file descriptor.
1243 * \param handle handle returned by drmAddMap().
1244 * \param size size in bytes. Must match the size used by drmAddMap().
1245 * \param address will contain the user-space virtual address where the mapping
1248 * \return zero on success, or a negative value on failure.
1251 * This function is a wrapper for mmap().
1253 int drmMap(int fd, drm_handle_t handle, drmSize size, drmAddressPtr address)
1255 static unsigned long pagesize_mask = 0;
1261 pagesize_mask = getpagesize() - 1;
1263 size = (size + pagesize_mask) & ~pagesize_mask;
1265 *address = drm_mmap(0, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, handle);
1266 if (*address == MAP_FAILED)
1273 * Unmap mappings obtained with drmMap().
1275 * \param address address as given by drmMap().
1276 * \param size size in bytes. Must match the size used by drmMap().
1278 * \return zero on success, or a negative value on failure.
1281 * This function is a wrapper for munmap().
1283 int drmUnmap(drmAddress address, drmSize size)
1285 return drm_munmap(address, size);
1288 drmBufInfoPtr drmGetBufInfo(int fd)
1290 drm_buf_info_t info;
1291 drmBufInfoPtr retval;
1296 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info))
1300 if (!(info.list = drmMalloc(info.count * sizeof(*info.list))))
1303 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info)) {
1308 retval = drmMalloc(sizeof(*retval));
1309 retval->count = info.count;
1310 retval->list = drmMalloc(info.count * sizeof(*retval->list));
1311 for (i = 0; i < info.count; i++) {
1312 retval->list[i].count = info.list[i].count;
1313 retval->list[i].size = info.list[i].size;
1314 retval->list[i].low_mark = info.list[i].low_mark;
1315 retval->list[i].high_mark = info.list[i].high_mark;
1324 * Map all DMA buffers into client-virtual space.
1326 * \param fd file descriptor.
1328 * \return a pointer to a ::drmBufMap structure.
1330 * \note The client may not use these buffers until obtaining buffer indices
1334 * This function calls the DRM_IOCTL_MAP_BUFS ioctl and copies the returned
1335 * information about the buffers in a drm_buf_map structure into the
1336 * client-visible data structures.
1338 drmBufMapPtr drmMapBufs(int fd)
1341 drmBufMapPtr retval;
1345 if (drmIoctl(fd, DRM_IOCTL_MAP_BUFS, &bufs))
1351 if (!(bufs.list = drmMalloc(bufs.count * sizeof(*bufs.list))))
1354 if (drmIoctl(fd, DRM_IOCTL_MAP_BUFS, &bufs)) {
1359 retval = drmMalloc(sizeof(*retval));
1360 retval->count = bufs.count;
1361 retval->list = drmMalloc(bufs.count * sizeof(*retval->list));
1362 for (i = 0; i < bufs.count; i++) {
1363 retval->list[i].idx = bufs.list[i].idx;
1364 retval->list[i].total = bufs.list[i].total;
1365 retval->list[i].used = 0;
1366 retval->list[i].address = bufs.list[i].address;
1375 * Unmap buffers allocated with drmMapBufs().
1377 * \return zero on success, or negative value on failure.
1380 * Calls munmap() for every buffer stored in \p bufs and frees the
1381 * memory allocated by drmMapBufs().
1383 int drmUnmapBufs(drmBufMapPtr bufs)
1387 for (i = 0; i < bufs->count; i++) {
1388 drm_munmap(bufs->list[i].address, bufs->list[i].total);
1391 drmFree(bufs->list);
1397 #define DRM_DMA_RETRY 16
1400 * Reserve DMA buffers.
1402 * \param fd file descriptor.
1405 * \return zero on success, or a negative value on failure.
1408 * Assemble the arguments into a drm_dma structure and keeps issuing the
1409 * DRM_IOCTL_DMA ioctl until success or until maximum number of retries.
1411 int drmDMA(int fd, drmDMAReqPtr request)
1416 dma.context = request->context;
1417 dma.send_count = request->send_count;
1418 dma.send_indices = request->send_list;
1419 dma.send_sizes = request->send_sizes;
1420 dma.flags = request->flags;
1421 dma.request_count = request->request_count;
1422 dma.request_size = request->request_size;
1423 dma.request_indices = request->request_list;
1424 dma.request_sizes = request->request_sizes;
1425 dma.granted_count = 0;
1428 ret = ioctl( fd, DRM_IOCTL_DMA, &dma );
1429 } while ( ret && errno == EAGAIN && i++ < DRM_DMA_RETRY );
1432 request->granted_count = dma.granted_count;
1441 * Obtain heavyweight hardware lock.
1443 * \param fd file descriptor.
1444 * \param context context.
1445 * \param flags flags that determine the sate of the hardware when the function
1448 * \return always zero.
1451 * This function translates the arguments into a drm_lock structure and issue
1452 * the DRM_IOCTL_LOCK ioctl until the lock is successfully acquired.
1454 int drmGetLock(int fd, drm_context_t context, drmLockFlags flags)
1459 lock.context = context;
1461 if (flags & DRM_LOCK_READY) lock.flags |= _DRM_LOCK_READY;
1462 if (flags & DRM_LOCK_QUIESCENT) lock.flags |= _DRM_LOCK_QUIESCENT;
1463 if (flags & DRM_LOCK_FLUSH) lock.flags |= _DRM_LOCK_FLUSH;
1464 if (flags & DRM_LOCK_FLUSH_ALL) lock.flags |= _DRM_LOCK_FLUSH_ALL;
1465 if (flags & DRM_HALT_ALL_QUEUES) lock.flags |= _DRM_HALT_ALL_QUEUES;
1466 if (flags & DRM_HALT_CUR_QUEUES) lock.flags |= _DRM_HALT_CUR_QUEUES;
1468 while (drmIoctl(fd, DRM_IOCTL_LOCK, &lock))
1474 * Release the hardware lock.
1476 * \param fd file descriptor.
1477 * \param context context.
1479 * \return zero on success, or a negative value on failure.
1482 * This function is a wrapper around the DRM_IOCTL_UNLOCK ioctl, passing the
1483 * argument in a drm_lock structure.
1485 int drmUnlock(int fd, drm_context_t context)
1490 lock.context = context;
1491 return drmIoctl(fd, DRM_IOCTL_UNLOCK, &lock);
1494 drm_context_t *drmGetReservedContextList(int fd, int *count)
1498 drm_context_t * retval;
1502 if (drmIoctl(fd, DRM_IOCTL_RES_CTX, &res))
1508 if (!(list = drmMalloc(res.count * sizeof(*list))))
1510 if (!(retval = drmMalloc(res.count * sizeof(*retval)))) {
1515 res.contexts = list;
1516 if (drmIoctl(fd, DRM_IOCTL_RES_CTX, &res))
1519 for (i = 0; i < res.count; i++)
1520 retval[i] = list[i].handle;
1527 void drmFreeReservedContextList(drm_context_t *pt)
1535 * Used by the X server during GLXContext initialization. This causes
1536 * per-context kernel-level resources to be allocated.
1538 * \param fd file descriptor.
1539 * \param handle is set on success. To be used by the client when requesting DMA
1540 * dispatch with drmDMA().
1542 * \return zero on success, or a negative value on failure.
1544 * \note May only be called by root.
1547 * This function is a wrapper around the DRM_IOCTL_ADD_CTX ioctl, passing the
1548 * argument in a drm_ctx structure.
1550 int drmCreateContext(int fd, drm_context_t *handle)
1555 if (drmIoctl(fd, DRM_IOCTL_ADD_CTX, &ctx))
1557 *handle = ctx.handle;
1561 int drmSwitchToContext(int fd, drm_context_t context)
1566 ctx.handle = context;
1567 if (drmIoctl(fd, DRM_IOCTL_SWITCH_CTX, &ctx))
1572 int drmSetContextFlags(int fd, drm_context_t context, drm_context_tFlags flags)
1577 * Context preserving means that no context switches are done between DMA
1578 * buffers from one context and the next. This is suitable for use in the
1579 * X server (which promises to maintain hardware context), or in the
1580 * client-side library when buffers are swapped on behalf of two threads.
1583 ctx.handle = context;
1584 if (flags & DRM_CONTEXT_PRESERVED)
1585 ctx.flags |= _DRM_CONTEXT_PRESERVED;
1586 if (flags & DRM_CONTEXT_2DONLY)
1587 ctx.flags |= _DRM_CONTEXT_2DONLY;
1588 if (drmIoctl(fd, DRM_IOCTL_MOD_CTX, &ctx))
1593 int drmGetContextFlags(int fd, drm_context_t context,
1594 drm_context_tFlagsPtr flags)
1599 ctx.handle = context;
1600 if (drmIoctl(fd, DRM_IOCTL_GET_CTX, &ctx))
1603 if (ctx.flags & _DRM_CONTEXT_PRESERVED)
1604 *flags |= DRM_CONTEXT_PRESERVED;
1605 if (ctx.flags & _DRM_CONTEXT_2DONLY)
1606 *flags |= DRM_CONTEXT_2DONLY;
1613 * Free any kernel-level resources allocated with drmCreateContext() associated
1616 * \param fd file descriptor.
1617 * \param handle handle given by drmCreateContext().
1619 * \return zero on success, or a negative value on failure.
1621 * \note May only be called by root.
1624 * This function is a wrapper around the DRM_IOCTL_RM_CTX ioctl, passing the
1625 * argument in a drm_ctx structure.
1627 int drmDestroyContext(int fd, drm_context_t handle)
1632 ctx.handle = handle;
1633 if (drmIoctl(fd, DRM_IOCTL_RM_CTX, &ctx))
1638 int drmCreateDrawable(int fd, drm_drawable_t *handle)
1643 if (drmIoctl(fd, DRM_IOCTL_ADD_DRAW, &draw))
1645 *handle = draw.handle;
1649 int drmDestroyDrawable(int fd, drm_drawable_t handle)
1654 draw.handle = handle;
1655 if (drmIoctl(fd, DRM_IOCTL_RM_DRAW, &draw))
1660 int drmUpdateDrawableInfo(int fd, drm_drawable_t handle,
1661 drm_drawable_info_type_t type, unsigned int num,
1664 drm_update_draw_t update;
1667 update.handle = handle;
1670 update.data = (unsigned long long)(unsigned long)data;
1672 if (drmIoctl(fd, DRM_IOCTL_UPDATE_DRAW, &update))
1679 * Acquire the AGP device.
1681 * Must be called before any of the other AGP related calls.
1683 * \param fd file descriptor.
1685 * \return zero on success, or a negative value on failure.
1688 * This function is a wrapper around the DRM_IOCTL_AGP_ACQUIRE ioctl.
1690 int drmAgpAcquire(int fd)
1692 if (drmIoctl(fd, DRM_IOCTL_AGP_ACQUIRE, NULL))
1699 * Release the AGP device.
1701 * \param fd file descriptor.
1703 * \return zero on success, or a negative value on failure.
1706 * This function is a wrapper around the DRM_IOCTL_AGP_RELEASE ioctl.
1708 int drmAgpRelease(int fd)
1710 if (drmIoctl(fd, DRM_IOCTL_AGP_RELEASE, NULL))
1719 * \param fd file descriptor.
1720 * \param mode AGP mode.
1722 * \return zero on success, or a negative value on failure.
1725 * This function is a wrapper around the DRM_IOCTL_AGP_ENABLE ioctl, passing the
1726 * argument in a drm_agp_mode structure.
1728 int drmAgpEnable(int fd, unsigned long mode)
1734 if (drmIoctl(fd, DRM_IOCTL_AGP_ENABLE, &m))
1741 * Allocate a chunk of AGP memory.
1743 * \param fd file descriptor.
1744 * \param size requested memory size in bytes. Will be rounded to page boundary.
1745 * \param type type of memory to allocate.
1746 * \param address if not zero, will be set to the physical address of the
1748 * \param handle on success will be set to a handle of the allocated memory.
1750 * \return zero on success, or a negative value on failure.
1753 * This function is a wrapper around the DRM_IOCTL_AGP_ALLOC ioctl, passing the
1754 * arguments in a drm_agp_buffer structure.
1756 int drmAgpAlloc(int fd, unsigned long size, unsigned long type,
1757 unsigned long *address, drm_handle_t *handle)
1762 *handle = DRM_AGP_NO_HANDLE;
1765 if (drmIoctl(fd, DRM_IOCTL_AGP_ALLOC, &b))
1768 *address = b.physical;
1775 * Free a chunk of AGP memory.
1777 * \param fd file descriptor.
1778 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1780 * \return zero on success, or a negative value on failure.
1783 * This function is a wrapper around the DRM_IOCTL_AGP_FREE ioctl, passing the
1784 * argument in a drm_agp_buffer structure.
1786 int drmAgpFree(int fd, drm_handle_t handle)
1792 if (drmIoctl(fd, DRM_IOCTL_AGP_FREE, &b))
1799 * Bind a chunk of AGP memory.
1801 * \param fd file descriptor.
1802 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1803 * \param offset offset in bytes. It will round to page boundary.
1805 * \return zero on success, or a negative value on failure.
1808 * This function is a wrapper around the DRM_IOCTL_AGP_BIND ioctl, passing the
1809 * argument in a drm_agp_binding structure.
1811 int drmAgpBind(int fd, drm_handle_t handle, unsigned long offset)
1813 drm_agp_binding_t b;
1818 if (drmIoctl(fd, DRM_IOCTL_AGP_BIND, &b))
1825 * Unbind a chunk of AGP memory.
1827 * \param fd file descriptor.
1828 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1830 * \return zero on success, or a negative value on failure.
1833 * This function is a wrapper around the DRM_IOCTL_AGP_UNBIND ioctl, passing
1834 * the argument in a drm_agp_binding structure.
1836 int drmAgpUnbind(int fd, drm_handle_t handle)
1838 drm_agp_binding_t b;
1842 if (drmIoctl(fd, DRM_IOCTL_AGP_UNBIND, &b))
1849 * Get AGP driver major version number.
1851 * \param fd file descriptor.
1853 * \return major version number on success, or a negative value on failure..
1856 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1857 * necessary information in a drm_agp_info structure.
1859 int drmAgpVersionMajor(int fd)
1865 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1867 return i.agp_version_major;
1872 * Get AGP driver minor version number.
1874 * \param fd file descriptor.
1876 * \return minor version number on success, or a negative value on failure.
1879 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1880 * necessary information in a drm_agp_info structure.
1882 int drmAgpVersionMinor(int fd)
1888 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1890 return i.agp_version_minor;
1897 * \param fd file descriptor.
1899 * \return mode on success, or zero on failure.
1902 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1903 * necessary information in a drm_agp_info structure.
1905 unsigned long drmAgpGetMode(int fd)
1911 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1918 * Get AGP aperture base.
1920 * \param fd file descriptor.
1922 * \return aperture base on success, zero on failure.
1925 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1926 * necessary information in a drm_agp_info structure.
1928 unsigned long drmAgpBase(int fd)
1934 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1936 return i.aperture_base;
1941 * Get AGP aperture size.
1943 * \param fd file descriptor.
1945 * \return aperture size on success, zero on failure.
1948 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1949 * necessary information in a drm_agp_info structure.
1951 unsigned long drmAgpSize(int fd)
1957 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1959 return i.aperture_size;
1964 * Get used AGP memory.
1966 * \param fd file descriptor.
1968 * \return memory used on success, or zero on failure.
1971 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1972 * necessary information in a drm_agp_info structure.
1974 unsigned long drmAgpMemoryUsed(int fd)
1980 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1982 return i.memory_used;
1987 * Get available AGP memory.
1989 * \param fd file descriptor.
1991 * \return memory available on success, or zero on failure.
1994 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1995 * necessary information in a drm_agp_info structure.
1997 unsigned long drmAgpMemoryAvail(int fd)
2003 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
2005 return i.memory_allowed;
2010 * Get hardware vendor ID.
2012 * \param fd file descriptor.
2014 * \return vendor ID on success, or zero on failure.
2017 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
2018 * necessary information in a drm_agp_info structure.
2020 unsigned int drmAgpVendorId(int fd)
2026 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
2033 * Get hardware device ID.
2035 * \param fd file descriptor.
2037 * \return zero on success, or zero on failure.
2040 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
2041 * necessary information in a drm_agp_info structure.
2043 unsigned int drmAgpDeviceId(int fd)
2049 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
2054 int drmScatterGatherAlloc(int fd, unsigned long size, drm_handle_t *handle)
2056 drm_scatter_gather_t sg;
2062 if (drmIoctl(fd, DRM_IOCTL_SG_ALLOC, &sg))
2064 *handle = sg.handle;
2068 int drmScatterGatherFree(int fd, drm_handle_t handle)
2070 drm_scatter_gather_t sg;
2074 if (drmIoctl(fd, DRM_IOCTL_SG_FREE, &sg))
2082 * \param fd file descriptor.
2083 * \param vbl pointer to a drmVBlank structure.
2085 * \return zero on success, or a negative value on failure.
2088 * This function is a wrapper around the DRM_IOCTL_WAIT_VBLANK ioctl.
2090 int drmWaitVBlank(int fd, drmVBlankPtr vbl)
2092 struct timespec timeout, cur;
2095 ret = clock_gettime(CLOCK_MONOTONIC, &timeout);
2097 fprintf(stderr, "clock_gettime failed: %s\n", strerror(errno));
2103 ret = ioctl(fd, DRM_IOCTL_WAIT_VBLANK, vbl);
2104 vbl->request.type &= ~DRM_VBLANK_RELATIVE;
2105 if (ret && errno == EINTR) {
2106 clock_gettime(CLOCK_MONOTONIC, &cur);
2107 /* Timeout after 1s */
2108 if (cur.tv_sec > timeout.tv_sec + 1 ||
2109 (cur.tv_sec == timeout.tv_sec && cur.tv_nsec >=
2116 } while (ret && errno == EINTR);
2122 int drmError(int err, const char *label)
2125 case DRM_ERR_NO_DEVICE:
2126 fprintf(stderr, "%s: no device\n", label);
2128 case DRM_ERR_NO_ACCESS:
2129 fprintf(stderr, "%s: no access\n", label);
2131 case DRM_ERR_NOT_ROOT:
2132 fprintf(stderr, "%s: not root\n", label);
2134 case DRM_ERR_INVALID:
2135 fprintf(stderr, "%s: invalid args\n", label);
2140 fprintf( stderr, "%s: error %d (%s)\n", label, err, strerror(err) );
2148 * Install IRQ handler.
2150 * \param fd file descriptor.
2151 * \param irq IRQ number.
2153 * \return zero on success, or a negative value on failure.
2156 * This function is a wrapper around the DRM_IOCTL_CONTROL ioctl, passing the
2157 * argument in a drm_control structure.
2159 int drmCtlInstHandler(int fd, int irq)
2164 ctl.func = DRM_INST_HANDLER;
2166 if (drmIoctl(fd, DRM_IOCTL_CONTROL, &ctl))
2173 * Uninstall IRQ handler.
2175 * \param fd file descriptor.
2177 * \return zero on success, or a negative value on failure.
2180 * This function is a wrapper around the DRM_IOCTL_CONTROL ioctl, passing the
2181 * argument in a drm_control structure.
2183 int drmCtlUninstHandler(int fd)
2188 ctl.func = DRM_UNINST_HANDLER;
2190 if (drmIoctl(fd, DRM_IOCTL_CONTROL, &ctl))
2195 int drmFinish(int fd, int context, drmLockFlags flags)
2200 lock.context = context;
2201 if (flags & DRM_LOCK_READY) lock.flags |= _DRM_LOCK_READY;
2202 if (flags & DRM_LOCK_QUIESCENT) lock.flags |= _DRM_LOCK_QUIESCENT;
2203 if (flags & DRM_LOCK_FLUSH) lock.flags |= _DRM_LOCK_FLUSH;
2204 if (flags & DRM_LOCK_FLUSH_ALL) lock.flags |= _DRM_LOCK_FLUSH_ALL;
2205 if (flags & DRM_HALT_ALL_QUEUES) lock.flags |= _DRM_HALT_ALL_QUEUES;
2206 if (flags & DRM_HALT_CUR_QUEUES) lock.flags |= _DRM_HALT_CUR_QUEUES;
2207 if (drmIoctl(fd, DRM_IOCTL_FINISH, &lock))
2213 * Get IRQ from bus ID.
2215 * \param fd file descriptor.
2216 * \param busnum bus number.
2217 * \param devnum device number.
2218 * \param funcnum function number.
2220 * \return IRQ number on success, or a negative value on failure.
2223 * This function is a wrapper around the DRM_IOCTL_IRQ_BUSID ioctl, passing the
2224 * arguments in a drm_irq_busid structure.
2226 int drmGetInterruptFromBusID(int fd, int busnum, int devnum, int funcnum)
2233 p.funcnum = funcnum;
2234 if (drmIoctl(fd, DRM_IOCTL_IRQ_BUSID, &p))
2239 int drmAddContextTag(int fd, drm_context_t context, void *tag)
2241 drmHashEntry *entry = drmGetEntry(fd);
2243 if (drmHashInsert(entry->tagTable, context, tag)) {
2244 drmHashDelete(entry->tagTable, context);
2245 drmHashInsert(entry->tagTable, context, tag);
2250 int drmDelContextTag(int fd, drm_context_t context)
2252 drmHashEntry *entry = drmGetEntry(fd);
2254 return drmHashDelete(entry->tagTable, context);
2257 void *drmGetContextTag(int fd, drm_context_t context)
2259 drmHashEntry *entry = drmGetEntry(fd);
2262 if (drmHashLookup(entry->tagTable, context, &value))
2268 int drmAddContextPrivateMapping(int fd, drm_context_t ctx_id,
2269 drm_handle_t handle)
2271 drm_ctx_priv_map_t map;
2274 map.ctx_id = ctx_id;
2275 map.handle = (void *)(uintptr_t)handle;
2277 if (drmIoctl(fd, DRM_IOCTL_SET_SAREA_CTX, &map))
2282 int drmGetContextPrivateMapping(int fd, drm_context_t ctx_id,
2283 drm_handle_t *handle)
2285 drm_ctx_priv_map_t map;
2288 map.ctx_id = ctx_id;
2290 if (drmIoctl(fd, DRM_IOCTL_GET_SAREA_CTX, &map))
2293 *handle = (drm_handle_t)(uintptr_t)map.handle;
2298 int drmGetMap(int fd, int idx, drm_handle_t *offset, drmSize *size,
2299 drmMapType *type, drmMapFlags *flags, drm_handle_t *handle,
2306 if (drmIoctl(fd, DRM_IOCTL_GET_MAP, &map))
2308 *offset = map.offset;
2312 *handle = (unsigned long)map.handle;
2317 int drmGetClient(int fd, int idx, int *auth, int *pid, int *uid,
2318 unsigned long *magic, unsigned long *iocs)
2320 drm_client_t client;
2324 if (drmIoctl(fd, DRM_IOCTL_GET_CLIENT, &client))
2326 *auth = client.auth;
2329 *magic = client.magic;
2330 *iocs = client.iocs;
2334 int drmGetStats(int fd, drmStatsT *stats)
2340 if (drmIoctl(fd, DRM_IOCTL_GET_STATS, &s))
2344 memset(stats, 0, sizeof(*stats));
2345 if (s.count > sizeof(stats->data)/sizeof(stats->data[0]))
2349 stats->data[i].long_format = "%-20.20s"; \
2350 stats->data[i].rate_format = "%8.8s"; \
2351 stats->data[i].isvalue = 1; \
2352 stats->data[i].verbose = 0
2355 stats->data[i].long_format = "%-20.20s"; \
2356 stats->data[i].rate_format = "%5.5s"; \
2357 stats->data[i].isvalue = 0; \
2358 stats->data[i].mult_names = "kgm"; \
2359 stats->data[i].mult = 1000; \
2360 stats->data[i].verbose = 0
2363 stats->data[i].long_format = "%-20.20s"; \
2364 stats->data[i].rate_format = "%5.5s"; \
2365 stats->data[i].isvalue = 0; \
2366 stats->data[i].mult_names = "KGM"; \
2367 stats->data[i].mult = 1024; \
2368 stats->data[i].verbose = 0
2371 stats->count = s.count;
2372 for (i = 0; i < s.count; i++) {
2373 stats->data[i].value = s.data[i].value;
2374 switch (s.data[i].type) {
2375 case _DRM_STAT_LOCK:
2376 stats->data[i].long_name = "Lock";
2377 stats->data[i].rate_name = "Lock";
2380 case _DRM_STAT_OPENS:
2381 stats->data[i].long_name = "Opens";
2382 stats->data[i].rate_name = "O";
2384 stats->data[i].verbose = 1;
2386 case _DRM_STAT_CLOSES:
2387 stats->data[i].long_name = "Closes";
2388 stats->data[i].rate_name = "Lock";
2390 stats->data[i].verbose = 1;
2392 case _DRM_STAT_IOCTLS:
2393 stats->data[i].long_name = "Ioctls";
2394 stats->data[i].rate_name = "Ioc/s";
2397 case _DRM_STAT_LOCKS:
2398 stats->data[i].long_name = "Locks";
2399 stats->data[i].rate_name = "Lck/s";
2402 case _DRM_STAT_UNLOCKS:
2403 stats->data[i].long_name = "Unlocks";
2404 stats->data[i].rate_name = "Unl/s";
2408 stats->data[i].long_name = "IRQs";
2409 stats->data[i].rate_name = "IRQ/s";
2412 case _DRM_STAT_PRIMARY:
2413 stats->data[i].long_name = "Primary Bytes";
2414 stats->data[i].rate_name = "PB/s";
2417 case _DRM_STAT_SECONDARY:
2418 stats->data[i].long_name = "Secondary Bytes";
2419 stats->data[i].rate_name = "SB/s";
2423 stats->data[i].long_name = "DMA";
2424 stats->data[i].rate_name = "DMA/s";
2427 case _DRM_STAT_SPECIAL:
2428 stats->data[i].long_name = "Special DMA";
2429 stats->data[i].rate_name = "dma/s";
2432 case _DRM_STAT_MISSED:
2433 stats->data[i].long_name = "Miss";
2434 stats->data[i].rate_name = "Ms/s";
2437 case _DRM_STAT_VALUE:
2438 stats->data[i].long_name = "Value";
2439 stats->data[i].rate_name = "Value";
2442 case _DRM_STAT_BYTE:
2443 stats->data[i].long_name = "Bytes";
2444 stats->data[i].rate_name = "B/s";
2447 case _DRM_STAT_COUNT:
2449 stats->data[i].long_name = "Count";
2450 stats->data[i].rate_name = "Cnt/s";
2459 * Issue a set-version ioctl.
2461 * \param fd file descriptor.
2462 * \param drmCommandIndex command index
2463 * \param data source pointer of the data to be read and written.
2464 * \param size size of the data to be read and written.
2466 * \return zero on success, or a negative value on failure.
2469 * It issues a read-write ioctl given by
2470 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2472 int drmSetInterfaceVersion(int fd, drmSetVersion *version)
2475 drm_set_version_t sv;
2478 sv.drm_di_major = version->drm_di_major;
2479 sv.drm_di_minor = version->drm_di_minor;
2480 sv.drm_dd_major = version->drm_dd_major;
2481 sv.drm_dd_minor = version->drm_dd_minor;
2483 if (drmIoctl(fd, DRM_IOCTL_SET_VERSION, &sv)) {
2487 version->drm_di_major = sv.drm_di_major;
2488 version->drm_di_minor = sv.drm_di_minor;
2489 version->drm_dd_major = sv.drm_dd_major;
2490 version->drm_dd_minor = sv.drm_dd_minor;
2496 * Send a device-specific command.
2498 * \param fd file descriptor.
2499 * \param drmCommandIndex command index
2501 * \return zero on success, or a negative value on failure.
2504 * It issues a ioctl given by
2505 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2507 int drmCommandNone(int fd, unsigned long drmCommandIndex)
2509 unsigned long request;
2511 request = DRM_IO( DRM_COMMAND_BASE + drmCommandIndex);
2513 if (drmIoctl(fd, request, NULL)) {
2521 * Send a device-specific read command.
2523 * \param fd file descriptor.
2524 * \param drmCommandIndex command index
2525 * \param data destination pointer of the data to be read.
2526 * \param size size of the data to be read.
2528 * \return zero on success, or a negative value on failure.
2531 * It issues a read ioctl given by
2532 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2534 int drmCommandRead(int fd, unsigned long drmCommandIndex, void *data,
2537 unsigned long request;
2539 request = DRM_IOC( DRM_IOC_READ, DRM_IOCTL_BASE,
2540 DRM_COMMAND_BASE + drmCommandIndex, size);
2542 if (drmIoctl(fd, request, data)) {
2550 * Send a device-specific write command.
2552 * \param fd file descriptor.
2553 * \param drmCommandIndex command index
2554 * \param data source pointer of the data to be written.
2555 * \param size size of the data to be written.
2557 * \return zero on success, or a negative value on failure.
2560 * It issues a write ioctl given by
2561 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2563 int drmCommandWrite(int fd, unsigned long drmCommandIndex, void *data,
2566 unsigned long request;
2568 request = DRM_IOC( DRM_IOC_WRITE, DRM_IOCTL_BASE,
2569 DRM_COMMAND_BASE + drmCommandIndex, size);
2571 if (drmIoctl(fd, request, data)) {
2579 * Send a device-specific read-write command.
2581 * \param fd file descriptor.
2582 * \param drmCommandIndex command index
2583 * \param data source pointer of the data to be read and written.
2584 * \param size size of the data to be read and written.
2586 * \return zero on success, or a negative value on failure.
2589 * It issues a read-write ioctl given by
2590 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2592 int drmCommandWriteRead(int fd, unsigned long drmCommandIndex, void *data,
2595 unsigned long request;
2597 request = DRM_IOC( DRM_IOC_READ|DRM_IOC_WRITE, DRM_IOCTL_BASE,
2598 DRM_COMMAND_BASE + drmCommandIndex, size);
2600 if (drmIoctl(fd, request, data))
2605 #define DRM_MAX_FDS 16
2611 } connection[DRM_MAX_FDS];
2613 static int nr_fds = 0;
2615 int drmOpenOnce(void *unused,
2619 return drmOpenOnceWithType(BusID, newlyopened, DRM_NODE_PRIMARY);
2622 int drmOpenOnceWithType(const char *BusID, int *newlyopened, int type)
2627 for (i = 0; i < nr_fds; i++)
2628 if ((strcmp(BusID, connection[i].BusID) == 0) &&
2629 (connection[i].type == type)) {
2630 connection[i].refcount++;
2632 return connection[i].fd;
2635 fd = drmOpenWithType(NULL, BusID, type);
2636 if (fd < 0 || nr_fds == DRM_MAX_FDS)
2639 connection[nr_fds].BusID = strdup(BusID);
2640 connection[nr_fds].fd = fd;
2641 connection[nr_fds].refcount = 1;
2642 connection[nr_fds].type = type;
2646 fprintf(stderr, "saved connection %d for %s %d\n",
2647 nr_fds, connection[nr_fds].BusID,
2648 strcmp(BusID, connection[nr_fds].BusID));
2655 void drmCloseOnce(int fd)
2659 for (i = 0; i < nr_fds; i++) {
2660 if (fd == connection[i].fd) {
2661 if (--connection[i].refcount == 0) {
2662 drmClose(connection[i].fd);
2663 free(connection[i].BusID);
2666 connection[i] = connection[nr_fds];
2674 int drmSetMaster(int fd)
2676 return drmIoctl(fd, DRM_IOCTL_SET_MASTER, NULL);
2679 int drmDropMaster(int fd)
2681 return drmIoctl(fd, DRM_IOCTL_DROP_MASTER, NULL);
2684 char *drmGetDeviceNameFromFd(int fd)
2691 /* The whole drmOpen thing is a fiasco and we need to find a way
2692 * back to just using open(2). For now, however, lets just make
2693 * things worse with even more ad hoc directory walking code to
2694 * discover the device file name. */
2699 for (i = 0; i < DRM_MAX_MINOR; i++) {
2700 snprintf(name, sizeof name, DRM_DEV_NAME, DRM_DIR_NAME, i);
2701 if (stat(name, &sbuf) == 0 && sbuf.st_rdev == d)
2704 if (i == DRM_MAX_MINOR)
2707 return strdup(name);
2710 int drmGetNodeTypeFromFd(int fd)
2715 if (fstat(fd, &sbuf))
2718 maj = major(sbuf.st_rdev);
2719 min = minor(sbuf.st_rdev);
2721 if (maj != DRM_MAJOR || !S_ISCHR(sbuf.st_mode)) {
2726 type = drmGetMinorType(min);
2732 int drmPrimeHandleToFD(int fd, uint32_t handle, uint32_t flags, int *prime_fd)
2734 struct drm_prime_handle args;
2739 args.handle = handle;
2741 ret = drmIoctl(fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &args);
2745 *prime_fd = args.fd;
2749 int drmPrimeFDToHandle(int fd, int prime_fd, uint32_t *handle)
2751 struct drm_prime_handle args;
2756 ret = drmIoctl(fd, DRM_IOCTL_PRIME_FD_TO_HANDLE, &args);
2760 *handle = args.handle;
2764 static char *drmGetMinorNameForFD(int fd, int type)
2768 struct dirent *pent, *ent;
2770 const char *name = drmGetMinorName(type);
2772 char dev_name[64], buf[64];
2781 if (fstat(fd, &sbuf))
2784 maj = major(sbuf.st_rdev);
2785 min = minor(sbuf.st_rdev);
2787 if (maj != DRM_MAJOR || !S_ISCHR(sbuf.st_mode))
2790 snprintf(buf, sizeof(buf), "/sys/dev/char/%d:%d/device/drm", maj, min);
2792 sysdir = opendir(buf);
2796 name_max = fpathconf(dirfd(sysdir), _PC_NAME_MAX);
2800 pent = malloc(offsetof(struct dirent, d_name) + name_max + 1);
2804 while (readdir_r(sysdir, pent, &ent) == 0 && ent != NULL) {
2805 if (strncmp(ent->d_name, name, len) == 0) {
2806 snprintf(dev_name, sizeof(dev_name), DRM_DIR_NAME "/%s",
2812 return strdup(dev_name);
2821 #warning "Missing implementation of drmGetMinorNameForFD"
2826 char *drmGetPrimaryDeviceNameFromFd(int fd)
2828 return drmGetMinorNameForFD(fd, DRM_NODE_PRIMARY);
2831 char *drmGetRenderDeviceNameFromFd(int fd)
2833 return drmGetMinorNameForFD(fd, DRM_NODE_RENDER);
2836 static int drmParseSubsystemType(int maj, int min)
2839 char path[PATH_MAX + 1];
2840 char link[PATH_MAX + 1] = "";
2843 snprintf(path, PATH_MAX, "/sys/dev/char/%d:%d/device/subsystem",
2846 if (readlink(path, link, PATH_MAX) < 0)
2849 name = strrchr(link, '/');
2853 if (strncmp(name, "/pci", 4) == 0)
2858 #warning "Missing implementation of drmParseSubsystemType"
2863 static int drmParsePciBusInfo(int maj, int min, drmPciBusInfoPtr info)
2866 char path[PATH_MAX + 1];
2869 int domain, bus, dev, func;
2872 snprintf(path, PATH_MAX, "/sys/dev/char/%d:%d/device/uevent", maj, min);
2873 fd = open(path, O_RDONLY);
2877 ret = read(fd, data, sizeof(data));
2878 data[sizeof(data)-1] = '\0';
2883 #define TAG "PCI_SLOT_NAME="
2884 str = strstr(data, TAG);
2888 if (sscanf(str, TAG "%04x:%02x:%02x.%1u",
2889 &domain, &bus, &dev, &func) != 4)
2893 info->domain = domain;
2900 #warning "Missing implementation of drmParsePciBusInfo"
2905 static int drmCompareBusInfo(drmDevicePtr a, drmDevicePtr b)
2907 if (a == NULL || b == NULL)
2910 if (a->bustype != b->bustype)
2913 switch (a->bustype) {
2915 return memcmp(a->businfo.pci, b->businfo.pci, sizeof(drmPciBusInfo));
2923 static int drmGetNodeType(const char *name)
2925 if (strncmp(name, DRM_PRIMARY_MINOR_NAME,
2926 sizeof(DRM_PRIMARY_MINOR_NAME) - 1) == 0)
2927 return DRM_NODE_PRIMARY;
2929 if (strncmp(name, DRM_CONTROL_MINOR_NAME,
2930 sizeof(DRM_CONTROL_MINOR_NAME ) - 1) == 0)
2931 return DRM_NODE_CONTROL;
2933 if (strncmp(name, DRM_RENDER_MINOR_NAME,
2934 sizeof(DRM_RENDER_MINOR_NAME) - 1) == 0)
2935 return DRM_NODE_RENDER;
2940 static int drmGetMaxNodeName(void)
2942 return sizeof(DRM_DIR_NAME) +
2943 MAX3(sizeof(DRM_PRIMARY_MINOR_NAME),
2944 sizeof(DRM_CONTROL_MINOR_NAME),
2945 sizeof(DRM_RENDER_MINOR_NAME)) +
2946 3 /* length of the node number */;
2949 static int drmParsePciDeviceInfo(const char *d_name,
2950 drmPciDeviceInfoPtr device)
2953 char path[PATH_MAX + 1];
2954 unsigned char config[64];
2957 snprintf(path, PATH_MAX, "/sys/class/drm/%s/device/config", d_name);
2958 fd = open(path, O_RDONLY);
2962 ret = read(fd, config, sizeof(config));
2967 device->vendor_id = config[0] | (config[1] << 8);
2968 device->device_id = config[2] | (config[3] << 8);
2969 device->revision_id = config[8];
2970 device->subvendor_id = config[44] | (config[45] << 8);
2971 device->subdevice_id = config[46] | (config[47] << 8);
2975 #warning "Missing implementation of drmParsePciDeviceInfo"
2980 void drmFreeDevice(drmDevicePtr *device)
2989 void drmFreeDevices(drmDevicePtr devices[], int count)
2993 if (devices == NULL)
2996 for (i = 0; i < count; i++)
2998 drmFreeDevice(&devices[i]);
3001 static int drmProcessPciDevice(drmDevicePtr *device, const char *d_name,
3002 const char *node, int node_type,
3003 int maj, int min, bool fetch_deviceinfo)
3005 const int max_node_str = ALIGN(drmGetMaxNodeName(), sizeof(void *));
3009 *device = calloc(1, sizeof(drmDevice) +
3010 (DRM_NODE_MAX * (sizeof(void *) + max_node_str)) +
3011 sizeof(drmPciBusInfo) +
3012 sizeof(drmPciDeviceInfo));
3016 addr = (char*)*device;
3018 (*device)->bustype = DRM_BUS_PCI;
3019 (*device)->available_nodes = 1 << node_type;
3021 addr += sizeof(drmDevice);
3022 (*device)->nodes = (char**)addr;
3024 addr += DRM_NODE_MAX * sizeof(void *);
3025 for (i = 0; i < DRM_NODE_MAX; i++) {
3026 (*device)->nodes[i] = addr;
3027 addr += max_node_str;
3029 memcpy((*device)->nodes[node_type], node, max_node_str);
3031 (*device)->businfo.pci = (drmPciBusInfoPtr)addr;
3033 ret = drmParsePciBusInfo(maj, min, (*device)->businfo.pci);
3037 // Fetch the device info if the user has requested it
3038 if (fetch_deviceinfo) {
3039 addr += sizeof(drmPciBusInfo);
3040 (*device)->deviceinfo.pci = (drmPciDeviceInfoPtr)addr;
3042 ret = drmParsePciDeviceInfo(d_name, (*device)->deviceinfo.pci);
3054 /* Consider devices located on the same bus as duplicate and fold the respective
3055 * entries into a single one.
3057 * Note: this leaves "gaps" in the array, while preserving the length.
3059 static void drmFoldDuplicatedDevices(drmDevicePtr local_devices[], int count)
3061 int node_type, i, j;
3063 for (i = 0; i < count; i++) {
3064 for (j = i + 1; j < count; j++) {
3065 if (drmCompareBusInfo(local_devices[i], local_devices[j]) == 0) {
3066 local_devices[i]->available_nodes |= local_devices[j]->available_nodes;
3067 node_type = log2(local_devices[j]->available_nodes);
3068 memcpy(local_devices[i]->nodes[node_type],
3069 local_devices[j]->nodes[node_type], drmGetMaxNodeName());
3070 drmFreeDevice(&local_devices[j]);
3077 * Get information about the opened drm device
3079 * \param fd file descriptor of the drm device
3080 * \param device the address of a drmDevicePtr where the information
3081 * will be allocated in stored
3083 * \return zero on success, negative error code otherwise.
3085 int drmGetDevice(int fd, drmDevicePtr *device)
3087 drmDevicePtr *local_devices;
3090 struct dirent *dent;
3092 char node[PATH_MAX + 1];
3093 int node_type, subsystem_type;
3095 int ret, i, node_count;
3099 if (fd == -1 || device == NULL)
3102 if (fstat(fd, &sbuf))
3105 find_rdev = sbuf.st_rdev;
3106 maj = major(sbuf.st_rdev);
3107 min = minor(sbuf.st_rdev);
3109 if (maj != DRM_MAJOR || !S_ISCHR(sbuf.st_mode))
3112 subsystem_type = drmParseSubsystemType(maj, min);
3114 local_devices = calloc(max_count, sizeof(drmDevicePtr));
3115 if (local_devices == NULL)
3118 sysdir = opendir(DRM_DIR_NAME);
3125 while ((dent = readdir(sysdir))) {
3126 node_type = drmGetNodeType(dent->d_name);
3130 snprintf(node, PATH_MAX, "%s/%s", DRM_DIR_NAME, dent->d_name);
3131 if (stat(node, &sbuf))
3134 maj = major(sbuf.st_rdev);
3135 min = minor(sbuf.st_rdev);
3137 if (maj != DRM_MAJOR || !S_ISCHR(sbuf.st_mode))
3140 if (drmParseSubsystemType(maj, min) != subsystem_type)
3143 switch (subsystem_type) {
3145 ret = drmProcessPciDevice(&d, dent->d_name, node, node_type,
3155 if (i >= max_count) {
3159 temp = realloc(local_devices, max_count * sizeof(drmDevicePtr));
3162 local_devices = temp;
3165 /* store target at local_devices[0] for ease to use below */
3166 if (find_rdev == sbuf.st_rdev && i) {
3167 local_devices[i] = local_devices[0];
3168 local_devices[0] = d;
3171 local_devices[i] = d;
3176 drmFoldDuplicatedDevices(local_devices, node_count);
3178 *device = local_devices[0];
3179 drmFreeDevices(&local_devices[1], node_count - 1);
3182 free(local_devices);
3183 if (*device == NULL)
3188 drmFreeDevices(local_devices, i);
3192 free(local_devices);
3197 * Get drm devices on the system
3199 * \param devices the array of devices with drmDevicePtr elements
3200 * can be NULL to get the device number first
3201 * \param max_devices the maximum number of devices for the array
3203 * \return on error - negative error code,
3204 * if devices is NULL - total number of devices available on the system,
3205 * alternatively the number of devices stored in devices[], which is
3206 * capped by the max_devices.
3208 int drmGetDevices(drmDevicePtr devices[], int max_devices)
3210 drmDevicePtr *local_devices;
3211 drmDevicePtr device;
3213 struct dirent *dent;
3215 char node[PATH_MAX + 1];
3216 int node_type, subsystem_type;
3218 int ret, i, node_count, device_count;
3221 local_devices = calloc(max_count, sizeof(drmDevicePtr));
3222 if (local_devices == NULL)
3225 sysdir = opendir(DRM_DIR_NAME);
3232 while ((dent = readdir(sysdir))) {
3233 node_type = drmGetNodeType(dent->d_name);
3237 snprintf(node, PATH_MAX, "%s/%s", DRM_DIR_NAME, dent->d_name);
3238 if (stat(node, &sbuf))
3241 maj = major(sbuf.st_rdev);
3242 min = minor(sbuf.st_rdev);
3244 if (maj != DRM_MAJOR || !S_ISCHR(sbuf.st_mode))
3247 subsystem_type = drmParseSubsystemType(maj, min);
3249 if (subsystem_type < 0)
3252 switch (subsystem_type) {
3254 ret = drmProcessPciDevice(&device, dent->d_name, node, node_type,
3255 maj, min, devices != NULL);
3264 if (i >= max_count) {
3268 temp = realloc(local_devices, max_count * sizeof(drmDevicePtr));
3271 local_devices = temp;
3274 local_devices[i] = device;
3279 drmFoldDuplicatedDevices(local_devices, node_count);
3282 for (i = 0; i < node_count; i++) {
3283 if (!local_devices[i])
3286 if ((devices != NULL) && (device_count < max_devices))
3287 devices[device_count] = local_devices[i];
3289 drmFreeDevice(&local_devices[i]);
3295 free(local_devices);
3296 return device_count;
3299 drmFreeDevices(local_devices, i);
3303 free(local_devices);
3307 char *drmGetDeviceNameFromFd2(int fd)
3311 char *device_name = NULL;
3312 unsigned int maj, min;
3315 static const char match[9] = "\nDEVNAME=";
3319 if (fstat(fd, &sbuf))
3322 maj = major(sbuf.st_rdev);
3323 min = minor(sbuf.st_rdev);
3325 if (maj != DRM_MAJOR || !S_ISCHR(sbuf.st_mode))
3328 snprintf(buf, sizeof(buf), "/sys/dev/char/%d:%d/uevent", maj, min);
3329 if (!(f = fopen(buf, "r")))
3332 while (expected < sizeof(match)) {
3338 } else if (c == match[expected] )
3344 strcpy(buf, "/dev/");
3345 if (fgets(buf + 5, sizeof(buf) - 5, f)) {
3346 buf[strcspn(buf, "\n")] = '\0';
3347 device_name = strdup(buf);
3353 #warning "Missing implementation of drmGetDeviceNameFromFd2"