Blame xf86drm.c

Packit 631bab
/**
Packit 631bab
 * \file xf86drm.c
Packit 631bab
 * User-level interface to DRM device
Packit 631bab
 *
Packit 631bab
 * \author Rickard E. (Rik) Faith <faith@valinux.com>
Packit 631bab
 * \author Kevin E. Martin <martin@valinux.com>
Packit 631bab
 */
Packit 631bab
Packit 631bab
/*
Packit 631bab
 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
Packit 631bab
 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
Packit 631bab
 * All Rights Reserved.
Packit 631bab
 *
Packit 631bab
 * Permission is hereby granted, free of charge, to any person obtaining a
Packit 631bab
 * copy of this software and associated documentation files (the "Software"),
Packit 631bab
 * to deal in the Software without restriction, including without limitation
Packit 631bab
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
Packit 631bab
 * and/or sell copies of the Software, and to permit persons to whom the
Packit 631bab
 * Software is furnished to do so, subject to the following conditions:
Packit 631bab
 *
Packit 631bab
 * The above copyright notice and this permission notice (including the next
Packit 631bab
 * paragraph) shall be included in all copies or substantial portions of the
Packit 631bab
 * Software.
Packit 631bab
 *
Packit 631bab
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Packit 631bab
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Packit 631bab
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
Packit 631bab
 * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
Packit 631bab
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
Packit 631bab
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
Packit 631bab
 * DEALINGS IN THE SOFTWARE.
Packit 631bab
 */
Packit 631bab
Packit 631bab
#include <stdio.h>
Packit 631bab
#include <stdlib.h>
Packit 631bab
#include <stdbool.h>
Packit 631bab
#include <unistd.h>
Packit 631bab
#include <string.h>
Packit 631bab
#include <strings.h>
Packit 631bab
#include <ctype.h>
Packit 631bab
#include <dirent.h>
Packit 631bab
#include <stddef.h>
Packit 631bab
#include <fcntl.h>
Packit 631bab
#include <errno.h>
Packit 631bab
#include <limits.h>
Packit 631bab
#include <signal.h>
Packit 631bab
#include <time.h>
Packit 631bab
#include <sys/types.h>
Packit 631bab
#include <sys/stat.h>
Packit 631bab
#define stat_t struct stat
Packit 631bab
#include <sys/ioctl.h>
Packit 631bab
#include <sys/time.h>
Packit 631bab
#include <stdarg.h>
Packit 631bab
#ifdef MAJOR_IN_MKDEV
Packit 631bab
#include <sys/mkdev.h>
Packit 631bab
#endif
Packit 631bab
#ifdef MAJOR_IN_SYSMACROS
Packit 631bab
#include <sys/sysmacros.h>
Packit 631bab
#endif
Packit 631bab
#include <math.h>
Packit 631bab
Packit 631bab
#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
Packit 631bab
Packit 631bab
/* Not all systems have MAP_FAILED defined */
Packit 631bab
#ifndef MAP_FAILED
Packit 631bab
#define MAP_FAILED ((void *)-1)
Packit 631bab
#endif
Packit 631bab
Packit 631bab
#include "xf86drm.h"
Packit 631bab
#include "libdrm_macros.h"
Packit 631bab
Packit 631bab
#include "util_math.h"
Packit 631bab
Packit 631bab
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
Packit 631bab
#define DRM_MAJOR 145
Packit 631bab
#endif
Packit 631bab
Packit 631bab
#ifdef __NetBSD__
Packit 631bab
#define DRM_MAJOR 34
Packit 631bab
#endif
Packit 631bab
Packit 631bab
#ifdef __OpenBSD__
Packit 631bab
#ifdef __i386__
Packit 631bab
#define DRM_MAJOR 88
Packit 631bab
#else
Packit 631bab
#define DRM_MAJOR 87
Packit 631bab
#endif
Packit 631bab
#endif /* __OpenBSD__ */
Packit 631bab
Packit 631bab
#ifndef DRM_MAJOR
Packit 631bab
#define DRM_MAJOR 226 /* Linux */
Packit 631bab
#endif
Packit 631bab
Packit 631bab
#if defined(__OpenBSD__) || defined(__DragonFly__)
Packit 631bab
struct drm_pciinfo {
Packit 631bab
	uint16_t	domain;
Packit 631bab
	uint8_t		bus;
Packit 631bab
	uint8_t		dev;
Packit 631bab
	uint8_t		func;
Packit 631bab
	uint16_t	vendor_id;
Packit 631bab
	uint16_t	device_id;
Packit 631bab
	uint16_t	subvendor_id;
Packit 631bab
	uint16_t	subdevice_id;
Packit 631bab
	uint8_t		revision_id;
Packit 631bab
};
Packit 631bab
Packit 631bab
#define DRM_IOCTL_GET_PCIINFO	DRM_IOR(0x15, struct drm_pciinfo)
Packit 631bab
#endif
Packit 631bab
Packit 631bab
#define DRM_MSG_VERBOSITY 3
Packit 631bab
Packit 631bab
#define memclear(s) memset(&s, 0, sizeof(s))
Packit 631bab
Packit 631bab
static drmServerInfoPtr drm_server_info;
Packit 631bab
Packit 631bab
drm_public void drmSetServerInfo(drmServerInfoPtr info)
Packit 631bab
{
Packit 631bab
    drm_server_info = info;
Packit 631bab
}
Packit 631bab
Packit 631bab
/**
Packit 631bab
 * Output a message to stderr.
Packit 631bab
 *
Packit 631bab
 * \param format printf() like format string.
Packit 631bab
 *
Packit 631bab
 * \internal
Packit 631bab
 * This function is a wrapper around vfprintf().
Packit 631bab
 */
Packit 631bab
Packit 631bab
static int DRM_PRINTFLIKE(1, 0)
Packit 631bab
drmDebugPrint(const char *format, va_list ap)
Packit 631bab
{
Packit 631bab
    return vfprintf(stderr, format, ap);
Packit 631bab
}
Packit 631bab
Packit 631bab
drm_public void
Packit 631bab
drmMsg(const char *format, ...)
Packit 631bab
{
Packit 631bab
    va_list ap;
Packit 631bab
    const char *env;
Packit 631bab
    if (((env = getenv("LIBGL_DEBUG")) && strstr(env, "verbose")) ||
Packit 631bab
        (drm_server_info && drm_server_info->debug_print))
Packit 631bab
    {
Packit 631bab
        va_start(ap, format);
Packit 631bab
        if (drm_server_info) {
Packit 631bab
            drm_server_info->debug_print(format,ap);
Packit 631bab
        } else {
Packit 631bab
            drmDebugPrint(format, ap);
Packit 631bab
        }
Packit 631bab
        va_end(ap);
Packit 631bab
    }
Packit 631bab
}
Packit 631bab
Packit 631bab
static void *drmHashTable = NULL; /* Context switch callbacks */
Packit 631bab
Packit 631bab
drm_public void *drmGetHashTable(void)
Packit 631bab
{
Packit 631bab
    return drmHashTable;
Packit 631bab
}
Packit 631bab
Packit 631bab
drm_public void *drmMalloc(int size)
Packit 631bab
{
Packit 631bab
    return calloc(1, size);
Packit 631bab
}
Packit 631bab
Packit 631bab
drm_public void drmFree(void *pt)
Packit 631bab
{
Packit 631bab
    free(pt);
Packit 631bab
}
Packit 631bab
Packit 631bab
/**
Packit 631bab
 * Call ioctl, restarting if it is interrupted
Packit 631bab
 */
Packit 631bab
drm_public int
Packit 631bab
drmIoctl(int fd, unsigned long request, void *arg)
Packit 631bab
{
Packit 631bab
    int ret;
Packit 631bab
Packit 631bab
    do {
Packit 631bab
        ret = ioctl(fd, request, arg);
Packit 631bab
    } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
Packit 631bab
    return ret;
Packit 631bab
}
Packit 631bab
Packit 631bab
static unsigned long drmGetKeyFromFd(int fd)
Packit 631bab
{
Packit 631bab
    stat_t     st;
Packit 631bab
Packit 631bab
    st.st_rdev = 0;
Packit 631bab
    fstat(fd, &st);
Packit 631bab
    return st.st_rdev;
Packit 631bab
}
Packit 631bab
Packit 631bab
drm_public drmHashEntry *drmGetEntry(int fd)
Packit 631bab
{
Packit 631bab
    unsigned long key = drmGetKeyFromFd(fd);
Packit 631bab
    void          *value;
Packit 631bab
    drmHashEntry  *entry;
Packit 631bab
Packit 631bab
    if (!drmHashTable)
Packit 631bab
        drmHashTable = drmHashCreate();
Packit 631bab
Packit 631bab
    if (drmHashLookup(drmHashTable, key, &value)) {
Packit 631bab
        entry           = drmMalloc(sizeof(*entry));
Packit 631bab
        entry->fd       = fd;
Packit 631bab
        entry->f        = NULL;
Packit 631bab
        entry->tagTable = drmHashCreate();
Packit 631bab
        drmHashInsert(drmHashTable, key, entry);
Packit 631bab
    } else {
Packit 631bab
        entry = value;
Packit 631bab
    }
Packit 631bab
    return entry;
Packit 631bab
}
Packit 631bab
Packit 631bab
/**
Packit 631bab
 * Compare two busid strings
Packit 631bab
 *
Packit 631bab
 * \param first
Packit 631bab
 * \param second
Packit 631bab
 *
Packit 631bab
 * \return 1 if matched.
Packit 631bab
 *
Packit 631bab
 * \internal
Packit 631bab
 * This function compares two bus ID strings.  It understands the older
Packit 631bab
 * PCI:b:d:f format and the newer pci:oooo:bb:dd.f format.  In the format, o is
Packit 631bab
 * domain, b is bus, d is device, f is function.
Packit 631bab
 */
Packit 631bab
static int drmMatchBusID(const char *id1, const char *id2, int pci_domain_ok)
Packit 631bab
{
Packit 631bab
    /* First, check if the IDs are exactly the same */
Packit 631bab
    if (strcasecmp(id1, id2) == 0)
Packit 631bab
        return 1;
Packit 631bab
Packit 631bab
    /* Try to match old/new-style PCI bus IDs. */
Packit 631bab
    if (strncasecmp(id1, "pci", 3) == 0) {
Packit 631bab
        unsigned int o1, b1, d1, f1;
Packit 631bab
        unsigned int o2, b2, d2, f2;
Packit 631bab
        int ret;
Packit 631bab
Packit 631bab
        ret = sscanf(id1, "pci:%04x:%02x:%02x.%u", &o1, &b1, &d1, &f1;;
Packit 631bab
        if (ret != 4) {
Packit 631bab
            o1 = 0;
Packit 631bab
            ret = sscanf(id1, "PCI:%u:%u:%u", &b1, &d1, &f1;;
Packit 631bab
            if (ret != 3)
Packit 631bab
                return 0;
Packit 631bab
        }
Packit 631bab
Packit 631bab
        ret = sscanf(id2, "pci:%04x:%02x:%02x.%u", &o2, &b2, &d2, &f2;;
Packit 631bab
        if (ret != 4) {
Packit 631bab
            o2 = 0;
Packit 631bab
            ret = sscanf(id2, "PCI:%u:%u:%u", &b2, &d2, &f2;;
Packit 631bab
            if (ret != 3)
Packit 631bab
                return 0;
Packit 631bab
        }
Packit 631bab
Packit 631bab
        /* If domains aren't properly supported by the kernel interface,
Packit 631bab
         * just ignore them, which sucks less than picking a totally random
Packit 631bab
         * card with "open by name"
Packit 631bab
         */
Packit 631bab
        if (!pci_domain_ok)
Packit 631bab
            o1 = o2 = 0;
Packit 631bab
Packit 631bab
        if ((o1 != o2) || (b1 != b2) || (d1 != d2) || (f1 != f2))
Packit 631bab
            return 0;
Packit 631bab
        else
Packit 631bab
            return 1;
Packit 631bab
    }
Packit 631bab
    return 0;
Packit 631bab
}
Packit 631bab
Packit 631bab
/**
Packit 631bab
 * Handles error checking for chown call.
Packit 631bab
 *
Packit 631bab
 * \param path to file.
Packit 631bab
 * \param id of the new owner.
Packit 631bab
 * \param id of the new group.
Packit 631bab
 *
Packit 631bab
 * \return zero if success or -1 if failure.
Packit 631bab
 *
Packit 631bab
 * \internal
Packit 631bab
 * Checks for failure. If failure was caused by signal call chown again.
Packit 631bab
 * If any other failure happened then it will output error message using
Packit 631bab
 * drmMsg() call.
Packit 631bab
 */
Packit 631bab
#if !UDEV
Packit 631bab
static int chown_check_return(const char *path, uid_t owner, gid_t group)
Packit 631bab
{
Packit 631bab
        int rv;
Packit 631bab
Packit 631bab
        do {
Packit 631bab
            rv = chown(path, owner, group);
Packit 631bab
        } while (rv != 0 && errno == EINTR);
Packit 631bab
Packit 631bab
        if (rv == 0)
Packit 631bab
            return 0;
Packit 631bab
Packit 631bab
        drmMsg("Failed to change owner or group for file %s! %d: %s\n",
Packit 631bab
               path, errno, strerror(errno));
Packit 631bab
        return -1;
Packit 631bab
}
Packit 631bab
#endif
Packit 631bab
Packit 631bab
static const char *drmGetDeviceName(int type)
Packit 631bab
{
Packit 631bab
    switch (type) {
Packit 631bab
    case DRM_NODE_PRIMARY:
Packit 631bab
        return DRM_DEV_NAME;
Packit 631bab
    case DRM_NODE_CONTROL:
Packit 631bab
        return DRM_CONTROL_DEV_NAME;
Packit 631bab
    case DRM_NODE_RENDER:
Packit 631bab
        return DRM_RENDER_DEV_NAME;
Packit 631bab
    }
Packit 631bab
    return NULL;
Packit 631bab
}
Packit 631bab
Packit 631bab
/**
Packit 631bab
 * Open the DRM device, creating it if necessary.
Packit 631bab
 *
Packit 631bab
 * \param dev major and minor numbers of the device.
Packit 631bab
 * \param minor minor number of the device.
Packit 631bab
 *
Packit 631bab
 * \return a file descriptor on success, or a negative value on error.
Packit 631bab
 *
Packit 631bab
 * \internal
Packit 631bab
 * Assembles the device name from \p minor and opens it, creating the device
Packit 631bab
 * special file node with the major and minor numbers specified by \p dev and
Packit 631bab
 * parent directory if necessary and was called by root.
Packit 631bab
 */
Packit 631bab
static int drmOpenDevice(dev_t dev, int minor, int type)
Packit 631bab
{
Packit 631bab
    stat_t          st;
Packit 631bab
    const char      *dev_name = drmGetDeviceName(type);
Packit 631bab
    char            buf[DRM_NODE_NAME_MAX];
Packit 631bab
    int             fd;
Packit 631bab
    mode_t          devmode = DRM_DEV_MODE, serv_mode;
Packit 631bab
    gid_t           serv_group;
Packit 631bab
#if !UDEV
Packit 631bab
    int             isroot  = !geteuid();
Packit 631bab
    uid_t           user    = DRM_DEV_UID;
Packit 631bab
    gid_t           group   = DRM_DEV_GID;
Packit 631bab
#endif
Packit 631bab
Packit 631bab
    if (!dev_name)
Packit 631bab
        return -EINVAL;
Packit 631bab
Packit 631bab
    sprintf(buf, dev_name, DRM_DIR_NAME, minor);
Packit 631bab
    drmMsg("drmOpenDevice: node name is %s\n", buf);
Packit 631bab
Packit 631bab
    if (drm_server_info && drm_server_info->get_perms) {
Packit 631bab
        drm_server_info->get_perms(&serv_group, &serv_mode);
Packit 631bab
        devmode  = serv_mode ? serv_mode : DRM_DEV_MODE;
Packit 631bab
        devmode &= ~(S_IXUSR|S_IXGRP|S_IXOTH);
Packit 631bab
    }
Packit 631bab
Packit 631bab
#if !UDEV
Packit 631bab
    if (stat(DRM_DIR_NAME, &st)) {
Packit 631bab
        if (!isroot)
Packit 631bab
            return DRM_ERR_NOT_ROOT;
Packit 631bab
        mkdir(DRM_DIR_NAME, DRM_DEV_DIRMODE);
Packit 631bab
        chown_check_return(DRM_DIR_NAME, 0, 0); /* root:root */
Packit 631bab
        chmod(DRM_DIR_NAME, DRM_DEV_DIRMODE);
Packit 631bab
    }
Packit 631bab
Packit 631bab
    /* Check if the device node exists and create it if necessary. */
Packit 631bab
    if (stat(buf, &st)) {
Packit 631bab
        if (!isroot)
Packit 631bab
            return DRM_ERR_NOT_ROOT;
Packit 631bab
        remove(buf);
Packit 631bab
        mknod(buf, S_IFCHR | devmode, dev);
Packit 631bab
    }
Packit 631bab
Packit 631bab
    if (drm_server_info && drm_server_info->get_perms) {
Packit 631bab
        group = ((int)serv_group >= 0) ? serv_group : DRM_DEV_GID;
Packit 631bab
        chown_check_return(buf, user, group);
Packit 631bab
        chmod(buf, devmode);
Packit 631bab
    }
Packit 631bab
#else
Packit 631bab
    /* if we modprobed then wait for udev */
Packit 631bab
    {
Packit 631bab
        int udev_count = 0;
Packit 631bab
wait_for_udev:
Packit 631bab
        if (stat(DRM_DIR_NAME, &st)) {
Packit 631bab
            usleep(20);
Packit 631bab
            udev_count++;
Packit 631bab
Packit 631bab
            if (udev_count == 50)
Packit 631bab
                return -1;
Packit 631bab
            goto wait_for_udev;
Packit 631bab
        }
Packit 631bab
Packit 631bab
        if (stat(buf, &st)) {
Packit 631bab
            usleep(20);
Packit 631bab
            udev_count++;
Packit 631bab
Packit 631bab
            if (udev_count == 50)
Packit 631bab
                return -1;
Packit 631bab
            goto wait_for_udev;
Packit 631bab
        }
Packit 631bab
    }
Packit 631bab
#endif
Packit 631bab
Packit 631bab
    fd = open(buf, O_RDWR | O_CLOEXEC, 0);
Packit 631bab
    drmMsg("drmOpenDevice: open result is %d, (%s)\n",
Packit 631bab
           fd, fd < 0 ? strerror(errno) : "OK");
Packit 631bab
    if (fd >= 0)
Packit 631bab
        return fd;
Packit 631bab
Packit 631bab
#if !UDEV
Packit 631bab
    /* Check if the device node is not what we expect it to be, and recreate it
Packit 631bab
     * and try again if so.
Packit 631bab
     */
Packit 631bab
    if (st.st_rdev != dev) {
Packit 631bab
        if (!isroot)
Packit 631bab
            return DRM_ERR_NOT_ROOT;
Packit 631bab
        remove(buf);
Packit 631bab
        mknod(buf, S_IFCHR | devmode, dev);
Packit 631bab
        if (drm_server_info && drm_server_info->get_perms) {
Packit 631bab
            chown_check_return(buf, user, group);
Packit 631bab
            chmod(buf, devmode);
Packit 631bab
        }
Packit 631bab
    }
Packit 631bab
    fd = open(buf, O_RDWR | O_CLOEXEC, 0);
Packit 631bab
    drmMsg("drmOpenDevice: open result is %d, (%s)\n",
Packit 631bab
           fd, fd < 0 ? strerror(errno) : "OK");
Packit 631bab
    if (fd >= 0)
Packit 631bab
        return fd;
Packit 631bab
Packit 631bab
    drmMsg("drmOpenDevice: Open failed\n");
Packit 631bab
    remove(buf);
Packit 631bab
#endif
Packit 631bab
    return -errno;
Packit 631bab
}
Packit 631bab
Packit 631bab
Packit 631bab
/**
Packit 631bab
 * Open the DRM device
Packit 631bab
 *
Packit 631bab
 * \param minor device minor number.
Packit 631bab
 * \param create allow to create the device if set.
Packit 631bab
 *
Packit 631bab
 * \return a file descriptor on success, or a negative value on error.
Packit 631bab
 *
Packit 631bab
 * \internal
Packit 631bab
 * Calls drmOpenDevice() if \p create is set, otherwise assembles the device
Packit 631bab
 * name from \p minor and opens it.
Packit 631bab
 */
Packit 631bab
static int drmOpenMinor(int minor, int create, int type)
Packit 631bab
{
Packit 631bab
    int  fd;
Packit 631bab
    char buf[DRM_NODE_NAME_MAX];
Packit 631bab
    const char *dev_name = drmGetDeviceName(type);
Packit 631bab
Packit 631bab
    if (create)
Packit 631bab
        return drmOpenDevice(makedev(DRM_MAJOR, minor), minor, type);
Packit 631bab
Packit 631bab
    if (!dev_name)
Packit 631bab
        return -EINVAL;
Packit 631bab
Packit 631bab
    sprintf(buf, dev_name, DRM_DIR_NAME, minor);
Packit 631bab
    if ((fd = open(buf, O_RDWR | O_CLOEXEC, 0)) >= 0)
Packit 631bab
        return fd;
Packit 631bab
    return -errno;
Packit 631bab
}
Packit 631bab
Packit 631bab
Packit 631bab
/**
Packit 631bab
 * Determine whether the DRM kernel driver has been loaded.
Packit 631bab
 *
Packit 631bab
 * \return 1 if the DRM driver is loaded, 0 otherwise.
Packit 631bab
 *
Packit 631bab
 * \internal
Packit 631bab
 * Determine the presence of the kernel driver by attempting to open the 0
Packit 631bab
 * minor and get version information.  For backward compatibility with older
Packit 631bab
 * Linux implementations, /proc/dri is also checked.
Packit 631bab
 */
Packit 631bab
drm_public int drmAvailable(void)
Packit 631bab
{
Packit 631bab
    drmVersionPtr version;
Packit 631bab
    int           retval = 0;
Packit 631bab
    int           fd;
Packit 631bab
Packit 631bab
    if ((fd = drmOpenMinor(0, 1, DRM_NODE_PRIMARY)) < 0) {
Packit 631bab
        return 0;
Packit 631bab
    }
Packit 631bab
Packit 631bab
    if ((version = drmGetVersion(fd))) {
Packit 631bab
        retval = 1;
Packit 631bab
        drmFreeVersion(version);
Packit 631bab
    }
Packit 631bab
    close(fd);
Packit 631bab
Packit 631bab
    return retval;
Packit 631bab
}
Packit 631bab
Packit 631bab
static int drmGetMinorBase(int type)
Packit 631bab
{
Packit 631bab
    switch (type) {
Packit 631bab
    case DRM_NODE_PRIMARY:
Packit 631bab
        return 0;
Packit 631bab
    case DRM_NODE_CONTROL:
Packit 631bab
        return 64;
Packit 631bab
    case DRM_NODE_RENDER:
Packit 631bab
        return 128;
Packit 631bab
    default:
Packit 631bab
        return -1;
Packit 631bab
    };
Packit 631bab
}
Packit 631bab
Packit 631bab
static int drmGetMinorType(int minor)
Packit 631bab
{
Packit 631bab
    int type = minor >> 6;
Packit 631bab
Packit 631bab
    if (minor < 0)
Packit 631bab
        return -1;
Packit 631bab
Packit 631bab
    switch (type) {
Packit 631bab
    case DRM_NODE_PRIMARY:
Packit 631bab
    case DRM_NODE_CONTROL:
Packit 631bab
    case DRM_NODE_RENDER:
Packit 631bab
        return type;
Packit 631bab
    default:
Packit 631bab
        return -1;
Packit 631bab
    }
Packit 631bab
}
Packit 631bab
Packit 631bab
static const char *drmGetMinorName(int type)
Packit 631bab
{
Packit 631bab
    switch (type) {
Packit 631bab
    case DRM_NODE_PRIMARY:
Packit 631bab
        return DRM_PRIMARY_MINOR_NAME;
Packit 631bab
    case DRM_NODE_CONTROL:
Packit 631bab
        return DRM_CONTROL_MINOR_NAME;
Packit 631bab
    case DRM_NODE_RENDER:
Packit 631bab
        return DRM_RENDER_MINOR_NAME;
Packit 631bab
    default:
Packit 631bab
        return NULL;
Packit 631bab
    }
Packit 631bab
}
Packit 631bab
Packit 631bab
/**
Packit 631bab
 * Open the device by bus ID.
Packit 631bab
 *
Packit 631bab
 * \param busid bus ID.
Packit 631bab
 * \param type device node type.
Packit 631bab
 *
Packit 631bab
 * \return a file descriptor on success, or a negative value on error.
Packit 631bab
 *
Packit 631bab
 * \internal
Packit 631bab
 * This function attempts to open every possible minor (up to DRM_MAX_MINOR),
Packit 631bab
 * comparing the device bus ID with the one supplied.
Packit 631bab
 *
Packit 631bab
 * \sa drmOpenMinor() and drmGetBusid().
Packit 631bab
 */
Packit 631bab
static int drmOpenByBusid(const char *busid, int type)
Packit 631bab
{
Packit 631bab
    int        i, pci_domain_ok = 1;
Packit 631bab
    int        fd;
Packit 631bab
    const char *buf;
Packit 631bab
    drmSetVersion sv;
Packit 631bab
    int        base = drmGetMinorBase(type);
Packit 631bab
Packit 631bab
    if (base < 0)
Packit 631bab
        return -1;
Packit 631bab
Packit 631bab
    drmMsg("drmOpenByBusid: Searching for BusID %s\n", busid);
Packit 631bab
    for (i = base; i < base + DRM_MAX_MINOR; i++) {
Packit 631bab
        fd = drmOpenMinor(i, 1, type);
Packit 631bab
        drmMsg("drmOpenByBusid: drmOpenMinor returns %d\n", fd);
Packit 631bab
        if (fd >= 0) {
Packit 631bab
            /* We need to try for 1.4 first for proper PCI domain support
Packit 631bab
             * and if that fails, we know the kernel is busted
Packit 631bab
             */
Packit 631bab
            sv.drm_di_major = 1;
Packit 631bab
            sv.drm_di_minor = 4;
Packit 631bab
            sv.drm_dd_major = -1;        /* Don't care */
Packit 631bab
            sv.drm_dd_minor = -1;        /* Don't care */
Packit 631bab
            if (drmSetInterfaceVersion(fd, &sv)) {
Packit 631bab
#ifndef __alpha__
Packit 631bab
                pci_domain_ok = 0;
Packit 631bab
#endif
Packit 631bab
                sv.drm_di_major = 1;
Packit 631bab
                sv.drm_di_minor = 1;
Packit 631bab
                sv.drm_dd_major = -1;       /* Don't care */
Packit 631bab
                sv.drm_dd_minor = -1;       /* Don't care */
Packit 631bab
                drmMsg("drmOpenByBusid: Interface 1.4 failed, trying 1.1\n");
Packit 631bab
                drmSetInterfaceVersion(fd, &sv;;
Packit 631bab
            }
Packit 631bab
            buf = drmGetBusid(fd);
Packit 631bab
            drmMsg("drmOpenByBusid: drmGetBusid reports %s\n", buf);
Packit 631bab
            if (buf && drmMatchBusID(buf, busid, pci_domain_ok)) {
Packit 631bab
                drmFreeBusid(buf);
Packit 631bab
                return fd;
Packit 631bab
            }
Packit 631bab
            if (buf)
Packit 631bab
                drmFreeBusid(buf);
Packit 631bab
            close(fd);
Packit 631bab
        }
Packit 631bab
    }
Packit 631bab
    return -1;
Packit 631bab
}
Packit 631bab
Packit 631bab
Packit 631bab
/**
Packit 631bab
 * Open the device by name.
Packit 631bab
 *
Packit 631bab
 * \param name driver name.
Packit 631bab
 * \param type the device node type.
Packit 631bab
 *
Packit 631bab
 * \return a file descriptor on success, or a negative value on error.
Packit 631bab
 *
Packit 631bab
 * \internal
Packit 631bab
 * This function opens the first minor number that matches the driver name and
Packit 631bab
 * isn't already in use.  If it's in use it then it will already have a bus ID
Packit 631bab
 * assigned.
Packit 631bab
 *
Packit 631bab
 * \sa drmOpenMinor(), drmGetVersion() and drmGetBusid().
Packit 631bab
 */
Packit 631bab
static int drmOpenByName(const char *name, int type)
Packit 631bab
{
Packit 631bab
    int           i;
Packit 631bab
    int           fd;
Packit 631bab
    drmVersionPtr version;
Packit 631bab
    char *        id;
Packit 631bab
    int           base = drmGetMinorBase(type);
Packit 631bab
Packit 631bab
    if (base < 0)
Packit 631bab
        return -1;
Packit 631bab
Packit 631bab
    /*
Packit 631bab
     * Open the first minor number that matches the driver name and isn't
Packit 631bab
     * already in use.  If it's in use it will have a busid assigned already.
Packit 631bab
     */
Packit 631bab
    for (i = base; i < base + DRM_MAX_MINOR; i++) {
Packit 631bab
        if ((fd = drmOpenMinor(i, 1, type)) >= 0) {
Packit 631bab
            if ((version = drmGetVersion(fd))) {
Packit 631bab
                if (!strcmp(version->name, name)) {
Packit 631bab
                    drmFreeVersion(version);
Packit 631bab
                    id = drmGetBusid(fd);
Packit 631bab
                    drmMsg("drmGetBusid returned '%s'\n", id ? id : "NULL");
Packit 631bab
                    if (!id || !*id) {
Packit 631bab
                        if (id)
Packit 631bab
                            drmFreeBusid(id);
Packit 631bab
                        return fd;
Packit 631bab
                    } else {
Packit 631bab
                        drmFreeBusid(id);
Packit 631bab
                    }
Packit 631bab
                } else {
Packit 631bab
                    drmFreeVersion(version);
Packit 631bab
                }
Packit 631bab
            }
Packit 631bab
            close(fd);
Packit 631bab
        }
Packit 631bab
    }
Packit 631bab
Packit 631bab
    return -1;
Packit 631bab
}
Packit 631bab
Packit 631bab
Packit 631bab
/**
Packit 631bab
 * Open the DRM device.
Packit 631bab
 *
Packit 631bab
 * Looks up the specified name and bus ID, and opens the device found.  The
Packit 631bab
 * entry in /dev/dri is created if necessary and if called by root.
Packit 631bab
 *
Packit 631bab
 * \param name driver name. Not referenced if bus ID is supplied.
Packit 631bab
 * \param busid bus ID. Zero if not known.
Packit 631bab
 *
Packit 631bab
 * \return a file descriptor on success, or a negative value on error.
Packit 631bab
 *
Packit 631bab
 * \internal
Packit 631bab
 * It calls drmOpenByBusid() if \p busid is specified or drmOpenByName()
Packit 631bab
 * otherwise.
Packit 631bab
 */
Packit 631bab
drm_public int drmOpen(const char *name, const char *busid)
Packit 631bab
{
Packit 631bab
    return drmOpenWithType(name, busid, DRM_NODE_PRIMARY);
Packit 631bab
}
Packit 631bab
Packit 631bab
/**
Packit 631bab
 * Open the DRM device with specified type.
Packit 631bab
 *
Packit 631bab
 * Looks up the specified name and bus ID, and opens the device found.  The
Packit 631bab
 * entry in /dev/dri is created if necessary and if called by root.
Packit 631bab
 *
Packit 631bab
 * \param name driver name. Not referenced if bus ID is supplied.
Packit 631bab
 * \param busid bus ID. Zero if not known.
Packit 631bab
 * \param type the device node type to open, PRIMARY, CONTROL or RENDER
Packit 631bab
 *
Packit 631bab
 * \return a file descriptor on success, or a negative value on error.
Packit 631bab
 *
Packit 631bab
 * \internal
Packit 631bab
 * It calls drmOpenByBusid() if \p busid is specified or drmOpenByName()
Packit 631bab
 * otherwise.
Packit 631bab
 */
Packit 631bab
drm_public int drmOpenWithType(const char *name, const char *busid, int type)
Packit 631bab
{
Packit 631bab
    if (name != NULL && drm_server_info &&
Packit 631bab
        drm_server_info->load_module && !drmAvailable()) {
Packit 631bab
        /* try to load the kernel module */
Packit 631bab
        if (!drm_server_info->load_module(name)) {
Packit 631bab
            drmMsg("[drm] failed to load kernel module \"%s\"\n", name);
Packit 631bab
            return -1;
Packit 631bab
        }
Packit 631bab
    }
Packit 631bab
Packit 631bab
    if (busid) {
Packit 631bab
        int fd = drmOpenByBusid(busid, type);
Packit 631bab
        if (fd >= 0)
Packit 631bab
            return fd;
Packit 631bab
    }
Packit 631bab
Packit 631bab
    if (name)
Packit 631bab
        return drmOpenByName(name, type);
Packit 631bab
Packit 631bab
    return -1;
Packit 631bab
}
Packit 631bab
Packit 631bab
drm_public int drmOpenControl(int minor)
Packit 631bab
{
Packit 631bab
    return drmOpenMinor(minor, 0, DRM_NODE_CONTROL);
Packit 631bab
}
Packit 631bab
Packit 631bab
drm_public int drmOpenRender(int minor)
Packit 631bab
{
Packit 631bab
    return drmOpenMinor(minor, 0, DRM_NODE_RENDER);
Packit 631bab
}
Packit 631bab
Packit 631bab
/**
Packit 631bab
 * Free the version information returned by drmGetVersion().
Packit 631bab
 *
Packit 631bab
 * \param v pointer to the version information.
Packit 631bab
 *
Packit 631bab
 * \internal
Packit 631bab
 * It frees the memory pointed by \p %v as well as all the non-null strings
Packit 631bab
 * pointers in it.
Packit 631bab
 */
Packit 631bab
drm_public void drmFreeVersion(drmVersionPtr v)
Packit 631bab
{
Packit 631bab
    if (!v)
Packit 631bab
        return;
Packit 631bab
    drmFree(v->name);
Packit 631bab
    drmFree(v->date);
Packit 631bab
    drmFree(v->desc);
Packit 631bab
    drmFree(v);
Packit 631bab
}
Packit 631bab
Packit 631bab
Packit 631bab
/**
Packit 631bab
 * Free the non-public version information returned by the kernel.
Packit 631bab
 *
Packit 631bab
 * \param v pointer to the version information.
Packit 631bab
 *
Packit 631bab
 * \internal
Packit 631bab
 * Used by drmGetVersion() to free the memory pointed by \p %v as well as all
Packit 631bab
 * the non-null strings pointers in it.
Packit 631bab
 */
Packit 631bab
static void drmFreeKernelVersion(drm_version_t *v)
Packit 631bab
{
Packit 631bab
    if (!v)
Packit 631bab
        return;
Packit 631bab
    drmFree(v->name);
Packit 631bab
    drmFree(v->date);
Packit 631bab
    drmFree(v->desc);
Packit 631bab
    drmFree(v);
Packit 631bab
}
Packit 631bab
Packit 631bab
Packit 631bab
/**
Packit 631bab
 * Copy version information.
Packit 631bab
 *
Packit 631bab
 * \param d destination pointer.
Packit 631bab
 * \param s source pointer.
Packit 631bab
 *
Packit 631bab
 * \internal
Packit 631bab
 * Used by drmGetVersion() to translate the information returned by the ioctl
Packit 631bab
 * interface in a private structure into the public structure counterpart.
Packit 631bab
 */
Packit 631bab
static void drmCopyVersion(drmVersionPtr d, const drm_version_t *s)
Packit 631bab
{
Packit 631bab
    d->version_major      = s->version_major;
Packit 631bab
    d->version_minor      = s->version_minor;
Packit 631bab
    d->version_patchlevel = s->version_patchlevel;
Packit 631bab
    d->name_len           = s->name_len;
Packit 631bab
    d->name               = strdup(s->name);
Packit 631bab
    d->date_len           = s->date_len;
Packit 631bab
    d->date               = strdup(s->date);
Packit 631bab
    d->desc_len           = s->desc_len;
Packit 631bab
    d->desc               = strdup(s->desc);
Packit 631bab
}
Packit 631bab
Packit 631bab
Packit 631bab
/**
Packit 631bab
 * Query the driver version information.
Packit 631bab
 *
Packit 631bab
 * \param fd file descriptor.
Packit 631bab
 *
Packit 631bab
 * \return pointer to a drmVersion structure which should be freed with
Packit 631bab
 * drmFreeVersion().
Packit 631bab
 *
Packit 631bab
 * \note Similar information is available via /proc/dri.
Packit 631bab
 *
Packit 631bab
 * \internal
Packit 631bab
 * It gets the version information via successive DRM_IOCTL_VERSION ioctls,
Packit 631bab
 * first with zeros to get the string lengths, and then the actually strings.
Packit 631bab
 * It also null-terminates them since they might not be already.
Packit 631bab
 */
Packit 631bab
drm_public drmVersionPtr drmGetVersion(int fd)
Packit 631bab
{
Packit 631bab
    drmVersionPtr retval;
Packit 631bab
    drm_version_t *version = drmMalloc(sizeof(*version));
Packit 631bab
Packit 631bab
    if (drmIoctl(fd, DRM_IOCTL_VERSION, version)) {
Packit 631bab
        drmFreeKernelVersion(version);
Packit 631bab
        return NULL;
Packit 631bab
    }
Packit 631bab
Packit 631bab
    if (version->name_len)
Packit 631bab
        version->name    = drmMalloc(version->name_len + 1);
Packit 631bab
    if (version->date_len)
Packit 631bab
        version->date    = drmMalloc(version->date_len + 1);
Packit 631bab
    if (version->desc_len)
Packit 631bab
        version->desc    = drmMalloc(version->desc_len + 1);
Packit 631bab
Packit 631bab
    if (drmIoctl(fd, DRM_IOCTL_VERSION, version)) {
Packit 631bab
        drmMsg("DRM_IOCTL_VERSION: %s\n", strerror(errno));
Packit 631bab
        drmFreeKernelVersion(version);
Packit 631bab
        return NULL;
Packit 631bab
    }
Packit 631bab
Packit 631bab
    /* The results might not be null-terminated strings, so terminate them. */
Packit 631bab
    if (version->name_len) version->name[version->name_len] = '\0';
Packit 631bab
    if (version->date_len) version->date[version->date_len] = '\0';
Packit 631bab
    if (version->desc_len) version->desc[version->desc_len] = '\0';
Packit 631bab
Packit 631bab
    retval = drmMalloc(sizeof(*retval));
Packit 631bab
    drmCopyVersion(retval, version);
Packit 631bab
    drmFreeKernelVersion(version);
Packit 631bab
    return retval;
Packit 631bab
}
Packit 631bab
Packit 631bab
Packit 631bab
/**
Packit 631bab
 * Get version information for the DRM user space library.
Packit 631bab
 *
Packit 631bab
 * This version number is driver independent.
Packit 631bab
 *
Packit 631bab
 * \param fd file descriptor.
Packit 631bab
 *
Packit 631bab
 * \return version information.
Packit 631bab
 *
Packit 631bab
 * \internal
Packit 631bab
 * This function allocates and fills a drm_version structure with a hard coded
Packit 631bab
 * version number.
Packit 631bab
 */
Packit 631bab
drm_public drmVersionPtr drmGetLibVersion(int fd)
Packit 631bab
{
Packit 631bab
    drm_version_t *version = drmMalloc(sizeof(*version));
Packit 631bab
Packit 631bab
    /* Version history:
Packit 631bab
     *   NOTE THIS MUST NOT GO ABOVE VERSION 1.X due to drivers needing it
Packit 631bab
     *   revision 1.0.x = original DRM interface with no drmGetLibVersion
Packit 631bab
     *                    entry point and many drm<Device> extensions
Packit 631bab
     *   revision 1.1.x = added drmCommand entry points for device extensions
Packit 631bab
     *                    added drmGetLibVersion to identify libdrm.a version
Packit 631bab
     *   revision 1.2.x = added drmSetInterfaceVersion
Packit 631bab
     *                    modified drmOpen to handle both busid and name
Packit 631bab
     *   revision 1.3.x = added server + memory manager
Packit 631bab
     */
Packit 631bab
    version->version_major      = 1;
Packit 631bab
    version->version_minor      = 3;
Packit 631bab
    version->version_patchlevel = 0;
Packit 631bab
Packit 631bab
    return (drmVersionPtr)version;
Packit 631bab
}
Packit 631bab
Packit 631bab
drm_public int drmGetCap(int fd, uint64_t capability, uint64_t *value)
Packit 631bab
{
Packit 631bab
    struct drm_get_cap cap;
Packit 631bab
    int ret;
Packit 631bab
Packit 631bab
    memclear(cap);
Packit 631bab
    cap.capability = capability;
Packit 631bab
Packit 631bab
    ret = drmIoctl(fd, DRM_IOCTL_GET_CAP, &cap);
Packit 631bab
    if (ret)
Packit 631bab
        return ret;
Packit 631bab
Packit 631bab
    *value = cap.value;
Packit 631bab
    return 0;
Packit 631bab
}
Packit 631bab
Packit 631bab
drm_public int drmSetClientCap(int fd, uint64_t capability, uint64_t value)
Packit 631bab
{
Packit 631bab
    struct drm_set_client_cap cap;
Packit 631bab
Packit 631bab
    memclear(cap);
Packit 631bab
    cap.capability = capability;
Packit 631bab
    cap.value = value;
Packit 631bab
Packit 631bab
    return drmIoctl(fd, DRM_IOCTL_SET_CLIENT_CAP, &cap);
Packit 631bab
}
Packit 631bab
Packit 631bab
/**
Packit 631bab
 * Free the bus ID information.
Packit 631bab
 *
Packit 631bab
 * \param busid bus ID information string as given by drmGetBusid().
Packit 631bab
 *
Packit 631bab
 * \internal
Packit 631bab
 * This function is just frees the memory pointed by \p busid.
Packit 631bab
 */
Packit 631bab
drm_public void drmFreeBusid(const char *busid)
Packit 631bab
{
Packit 631bab
    drmFree((void *)busid);
Packit 631bab
}
Packit 631bab
Packit 631bab
Packit 631bab
/**
Packit 631bab
 * Get the bus ID of the device.
Packit 631bab
 *
Packit 631bab
 * \param fd file descriptor.
Packit 631bab
 *
Packit 631bab
 * \return bus ID string.
Packit 631bab
 *
Packit 631bab
 * \internal
Packit 631bab
 * This function gets the bus ID via successive DRM_IOCTL_GET_UNIQUE ioctls to
Packit 631bab
 * get the string length and data, passing the arguments in a drm_unique
Packit 631bab
 * structure.
Packit 631bab
 */
Packit 631bab
drm_public char *drmGetBusid(int fd)
Packit 631bab
{
Packit 631bab
    drm_unique_t u;
Packit 631bab
Packit 631bab
    memclear(u);
Packit 631bab
Packit 631bab
    if (drmIoctl(fd, DRM_IOCTL_GET_UNIQUE, &u))
Packit 631bab
        return NULL;
Packit 631bab
    u.unique = drmMalloc(u.unique_len + 1);
Packit 631bab
    if (drmIoctl(fd, DRM_IOCTL_GET_UNIQUE, &u)) {
Packit 631bab
        drmFree(u.unique);
Packit 631bab
        return NULL;
Packit 631bab
    }
Packit 631bab
    u.unique[u.unique_len] = '\0';
Packit 631bab
Packit 631bab
    return u.unique;
Packit 631bab
}
Packit 631bab
Packit 631bab
Packit 631bab
/**
Packit 631bab
 * Set the bus ID of the device.
Packit 631bab
 *
Packit 631bab
 * \param fd file descriptor.
Packit 631bab
 * \param busid bus ID string.
Packit 631bab
 *
Packit 631bab
 * \return zero on success, negative on failure.
Packit 631bab
 *
Packit 631bab
 * \internal
Packit 631bab
 * This function is a wrapper around the DRM_IOCTL_SET_UNIQUE ioctl, passing
Packit 631bab
 * the arguments in a drm_unique structure.
Packit 631bab
 */
Packit 631bab
drm_public int drmSetBusid(int fd, const char *busid)
Packit 631bab
{
Packit 631bab
    drm_unique_t u;
Packit 631bab
Packit 631bab
    memclear(u);
Packit 631bab
    u.unique     = (char *)busid;
Packit 631bab
    u.unique_len = strlen(busid);
Packit 631bab
Packit 631bab
    if (drmIoctl(fd, DRM_IOCTL_SET_UNIQUE, &u)) {
Packit 631bab
        return -errno;
Packit 631bab
    }
Packit 631bab
    return 0;
Packit 631bab
}
Packit 631bab
Packit 631bab
drm_public int drmGetMagic(int fd, drm_magic_t * magic)
Packit 631bab
{
Packit 631bab
    drm_auth_t auth;
Packit 631bab
Packit 631bab
    memclear(auth);
Packit 631bab
Packit 631bab
    *magic = 0;
Packit 631bab
    if (drmIoctl(fd, DRM_IOCTL_GET_MAGIC, &auth))
Packit 631bab
        return -errno;
Packit 631bab
    *magic = auth.magic;
Packit 631bab
    return 0;
Packit 631bab
}
Packit 631bab
Packit 631bab
drm_public int drmAuthMagic(int fd, drm_magic_t magic)
Packit 631bab
{
Packit 631bab
    drm_auth_t auth;
Packit 631bab
Packit 631bab
    memclear(auth);
Packit 631bab
    auth.magic = magic;
Packit 631bab
    if (drmIoctl(fd, DRM_IOCTL_AUTH_MAGIC, &auth))
Packit 631bab
        return -errno;
Packit 631bab
    return 0;
Packit 631bab
}
Packit 631bab
Packit 631bab
/**
Packit 631bab
 * Specifies a range of memory that is available for mapping by a
Packit 631bab
 * non-root process.
Packit 631bab
 *
Packit 631bab
 * \param fd file descriptor.
Packit 631bab
 * \param offset usually the physical address. The actual meaning depends of
Packit 631bab
 * the \p type parameter. See below.
Packit 631bab
 * \param size of the memory in bytes.
Packit 631bab
 * \param type type of the memory to be mapped.
Packit 631bab
 * \param flags combination of several flags to modify the function actions.
Packit 631bab
 * \param handle will be set to a value that may be used as the offset
Packit 631bab
 * parameter for mmap().
Packit 631bab
 *
Packit 631bab
 * \return zero on success or a negative value on error.
Packit 631bab
 *
Packit 631bab
 * \par Mapping the frame buffer
Packit 631bab
 * For the frame buffer
Packit 631bab
 * - \p offset will be the physical address of the start of the frame buffer,
Packit 631bab
 * - \p size will be the size of the frame buffer in bytes, and
Packit 631bab
 * - \p type will be DRM_FRAME_BUFFER.
Packit 631bab
 *
Packit 631bab
 * \par
Packit 631bab
 * The area mapped will be uncached. If MTRR support is available in the
Packit 631bab
 * kernel, the frame buffer area will be set to write combining.
Packit 631bab
 *
Packit 631bab
 * \par Mapping the MMIO register area
Packit 631bab
 * For the MMIO register area,
Packit 631bab
 * - \p offset will be the physical address of the start of the register area,
Packit 631bab
 * - \p size will be the size of the register area bytes, and
Packit 631bab
 * - \p type will be DRM_REGISTERS.
Packit 631bab
 * \par
Packit 631bab
 * The area mapped will be uncached.
Packit 631bab
 *
Packit 631bab
 * \par Mapping the SAREA
Packit 631bab
 * For the SAREA,
Packit 631bab
 * - \p offset will be ignored and should be set to zero,
Packit 631bab
 * - \p size will be the desired size of the SAREA in bytes,
Packit 631bab
 * - \p type will be DRM_SHM.
Packit 631bab
 *
Packit 631bab
 * \par
Packit 631bab
 * A shared memory area of the requested size will be created and locked in
Packit 631bab
 * kernel memory. This area may be mapped into client-space by using the handle
Packit 631bab
 * returned.
Packit 631bab
 *
Packit 631bab
 * \note May only be called by root.
Packit 631bab
 *
Packit 631bab
 * \internal
Packit 631bab
 * This function is a wrapper around the DRM_IOCTL_ADD_MAP ioctl, passing
Packit 631bab
 * the arguments in a drm_map structure.
Packit 631bab
 */
Packit 631bab
drm_public int drmAddMap(int fd, drm_handle_t offset, drmSize size, drmMapType type,
Packit 631bab
                         drmMapFlags flags, drm_handle_t *handle)
Packit 631bab
{
Packit 631bab
    drm_map_t map;
Packit 631bab
Packit 631bab
    memclear(map);
Packit 631bab
    map.offset  = offset;
Packit 631bab
    map.size    = size;
Packit 631bab
    map.type    = type;
Packit 631bab
    map.flags   = flags;
Packit 631bab
    if (drmIoctl(fd, DRM_IOCTL_ADD_MAP, &map))
Packit 631bab
        return -errno;
Packit 631bab
    if (handle)
Packit 631bab
        *handle = (drm_handle_t)(uintptr_t)map.handle;
Packit 631bab
    return 0;
Packit 631bab
}
Packit 631bab
Packit 631bab
drm_public int drmRmMap(int fd, drm_handle_t handle)
Packit 631bab
{
Packit 631bab
    drm_map_t map;
Packit 631bab
Packit 631bab
    memclear(map);
Packit 631bab
    map.handle = (void *)(uintptr_t)handle;
Packit 631bab
Packit 631bab
    if(drmIoctl(fd, DRM_IOCTL_RM_MAP, &map))
Packit 631bab
        return -errno;
Packit 631bab
    return 0;
Packit 631bab
}
Packit 631bab
Packit 631bab
/**
Packit 631bab
 * Make buffers available for DMA transfers.
Packit 631bab
 *
Packit 631bab
 * \param fd file descriptor.
Packit 631bab
 * \param count number of buffers.
Packit 631bab
 * \param size size of each buffer.
Packit 631bab
 * \param flags buffer allocation flags.
Packit 631bab
 * \param agp_offset offset in the AGP aperture
Packit 631bab
 *
Packit 631bab
 * \return number of buffers allocated, negative on error.
Packit 631bab
 *
Packit 631bab
 * \internal
Packit 631bab
 * This function is a wrapper around DRM_IOCTL_ADD_BUFS ioctl.
Packit 631bab
 *
Packit 631bab
 * \sa drm_buf_desc.
Packit 631bab
 */
Packit 631bab
drm_public int drmAddBufs(int fd, int count, int size, drmBufDescFlags flags,
Packit 631bab
                          int agp_offset)
Packit 631bab
{
Packit 631bab
    drm_buf_desc_t request;
Packit 631bab
Packit 631bab
    memclear(request);
Packit 631bab
    request.count     = count;
Packit 631bab
    request.size      = size;
Packit 631bab
    request.flags     = flags;
Packit 631bab
    request.agp_start = agp_offset;
Packit 631bab
Packit 631bab
    if (drmIoctl(fd, DRM_IOCTL_ADD_BUFS, &request))
Packit 631bab
        return -errno;
Packit 631bab
    return request.count;
Packit 631bab
}
Packit 631bab
Packit 631bab
drm_public int drmMarkBufs(int fd, double low, double high)
Packit 631bab
{
Packit 631bab
    drm_buf_info_t info;
Packit 631bab
    int            i;
Packit 631bab
Packit 631bab
    memclear(info);
Packit 631bab
Packit 631bab
    if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info))
Packit 631bab
        return -EINVAL;
Packit 631bab
Packit 631bab
    if (!info.count)
Packit 631bab
        return -EINVAL;
Packit 631bab
Packit 631bab
    if (!(info.list = drmMalloc(info.count * sizeof(*info.list))))
Packit 631bab
        return -ENOMEM;
Packit 631bab
Packit 631bab
    if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info)) {
Packit 631bab
        int retval = -errno;
Packit 631bab
        drmFree(info.list);
Packit 631bab
        return retval;
Packit 631bab
    }
Packit 631bab
Packit 631bab
    for (i = 0; i < info.count; i++) {
Packit 631bab
        info.list[i].low_mark  = low  * info.list[i].count;
Packit 631bab
        info.list[i].high_mark = high * info.list[i].count;
Packit 631bab
        if (drmIoctl(fd, DRM_IOCTL_MARK_BUFS, &info.list[i])) {
Packit 631bab
            int retval = -errno;
Packit 631bab
            drmFree(info.list);
Packit 631bab
            return retval;
Packit 631bab
        }
Packit 631bab
    }
Packit 631bab
    drmFree(info.list);
Packit 631bab
Packit 631bab
    return 0;
Packit 631bab
}
Packit 631bab
Packit 631bab
/**
Packit 631bab
 * Free buffers.
Packit 631bab
 *
Packit 631bab
 * \param fd file descriptor.
Packit 631bab
 * \param count number of buffers to free.
Packit 631bab
 * \param list list of buffers to be freed.
Packit 631bab
 *
Packit 631bab
 * \return zero on success, or a negative value on failure.
Packit 631bab
 *
Packit 631bab
 * \note This function is primarily used for debugging.
Packit 631bab
 *
Packit 631bab
 * \internal
Packit 631bab
 * This function is a wrapper around the DRM_IOCTL_FREE_BUFS ioctl, passing
Packit 631bab
 * the arguments in a drm_buf_free structure.
Packit 631bab
 */
Packit 631bab
drm_public int drmFreeBufs(int fd, int count, int *list)
Packit 631bab
{
Packit 631bab
    drm_buf_free_t request;
Packit 631bab
Packit 631bab
    memclear(request);
Packit 631bab
    request.count = count;
Packit 631bab
    request.list  = list;
Packit 631bab
    if (drmIoctl(fd, DRM_IOCTL_FREE_BUFS, &request))
Packit 631bab
        return -errno;
Packit 631bab
    return 0;
Packit 631bab
}
Packit 631bab
Packit 631bab
Packit 631bab
/**
Packit 631bab
 * Close the device.
Packit 631bab
 *
Packit 631bab
 * \param fd file descriptor.
Packit 631bab
 *
Packit 631bab
 * \internal
Packit 631bab
 * This function closes the file descriptor.
Packit 631bab
 */
Packit 631bab
drm_public int drmClose(int fd)
Packit 631bab
{
Packit 631bab
    unsigned long key    = drmGetKeyFromFd(fd);
Packit 631bab
    drmHashEntry  *entry = drmGetEntry(fd);
Packit 631bab
Packit 631bab
    drmHashDestroy(entry->tagTable);
Packit 631bab
    entry->fd       = 0;
Packit 631bab
    entry->f        = NULL;
Packit 631bab
    entry->tagTable = NULL;
Packit 631bab
Packit 631bab
    drmHashDelete(drmHashTable, key);
Packit 631bab
    drmFree(entry);
Packit 631bab
Packit 631bab
    return close(fd);
Packit 631bab
}
Packit 631bab
Packit 631bab
Packit 631bab
/**
Packit 631bab
 * Map a region of memory.
Packit 631bab
 *
Packit 631bab
 * \param fd file descriptor.
Packit 631bab
 * \param handle handle returned by drmAddMap().
Packit 631bab
 * \param size size in bytes. Must match the size used by drmAddMap().
Packit 631bab
 * \param address will contain the user-space virtual address where the mapping
Packit 631bab
 * begins.
Packit 631bab
 *
Packit 631bab
 * \return zero on success, or a negative value on failure.
Packit 631bab
 *
Packit 631bab
 * \internal
Packit 631bab
 * This function is a wrapper for mmap().
Packit 631bab
 */
Packit 631bab
drm_public int drmMap(int fd, drm_handle_t handle, drmSize size,
Packit 631bab
                      drmAddressPtr address)
Packit 631bab
{
Packit 631bab
    static unsigned long pagesize_mask = 0;
Packit 631bab
Packit 631bab
    if (fd < 0)
Packit 631bab
        return -EINVAL;
Packit 631bab
Packit 631bab
    if (!pagesize_mask)
Packit 631bab
        pagesize_mask = getpagesize() - 1;
Packit 631bab
Packit 631bab
    size = (size + pagesize_mask) & ~pagesize_mask;
Packit 631bab
Packit 631bab
    *address = drm_mmap(0, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, handle);
Packit 631bab
    if (*address == MAP_FAILED)
Packit 631bab
        return -errno;
Packit 631bab
    return 0;
Packit 631bab
}
Packit 631bab
Packit 631bab
Packit 631bab
/**
Packit 631bab
 * Unmap mappings obtained with drmMap().
Packit 631bab
 *
Packit 631bab
 * \param address address as given by drmMap().
Packit 631bab
 * \param size size in bytes. Must match the size used by drmMap().
Packit 631bab
 *
Packit 631bab
 * \return zero on success, or a negative value on failure.
Packit 631bab
 *
Packit 631bab
 * \internal
Packit 631bab
 * This function is a wrapper for munmap().
Packit 631bab
 */
Packit 631bab
drm_public int drmUnmap(drmAddress address, drmSize size)
Packit 631bab
{
Packit 631bab
    return drm_munmap(address, size);
Packit 631bab
}
Packit 631bab
Packit 631bab
drm_public drmBufInfoPtr drmGetBufInfo(int fd)
Packit 631bab
{
Packit 631bab
    drm_buf_info_t info;
Packit 631bab
    drmBufInfoPtr  retval;
Packit 631bab
    int            i;
Packit 631bab
Packit 631bab
    memclear(info);
Packit 631bab
Packit 631bab
    if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info))
Packit 631bab
        return NULL;
Packit 631bab
Packit 631bab
    if (info.count) {
Packit 631bab
        if (!(info.list = drmMalloc(info.count * sizeof(*info.list))))
Packit 631bab
            return NULL;
Packit 631bab
Packit 631bab
        if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info)) {
Packit 631bab
            drmFree(info.list);
Packit 631bab
            return NULL;
Packit 631bab
        }
Packit 631bab
Packit 631bab
        retval = drmMalloc(sizeof(*retval));
Packit 631bab
        retval->count = info.count;
Packit 631bab
        retval->list  = drmMalloc(info.count * sizeof(*retval->list));
Packit 631bab
        for (i = 0; i < info.count; i++) {
Packit 631bab
            retval->list[i].count     = info.list[i].count;
Packit 631bab
            retval->list[i].size      = info.list[i].size;
Packit 631bab
            retval->list[i].low_mark  = info.list[i].low_mark;
Packit 631bab
            retval->list[i].high_mark = info.list[i].high_mark;
Packit 631bab
        }
Packit 631bab
        drmFree(info.list);
Packit 631bab
        return retval;
Packit 631bab
    }
Packit 631bab
    return NULL;
Packit 631bab
}
Packit 631bab
Packit 631bab
/**
Packit 631bab
 * Map all DMA buffers into client-virtual space.
Packit 631bab
 *
Packit 631bab
 * \param fd file descriptor.
Packit 631bab
 *
Packit 631bab
 * \return a pointer to a ::drmBufMap structure.
Packit 631bab
 *
Packit 631bab
 * \note The client may not use these buffers until obtaining buffer indices
Packit 631bab
 * with drmDMA().
Packit 631bab
 *
Packit 631bab
 * \internal
Packit 631bab
 * This function calls the DRM_IOCTL_MAP_BUFS ioctl and copies the returned
Packit 631bab
 * information about the buffers in a drm_buf_map structure into the
Packit 631bab
 * client-visible data structures.
Packit 631bab
 */
Packit 631bab
drm_public drmBufMapPtr drmMapBufs(int fd)
Packit 631bab
{
Packit 631bab
    drm_buf_map_t bufs;
Packit 631bab
    drmBufMapPtr  retval;
Packit 631bab
    int           i;
Packit 631bab
Packit 631bab
    memclear(bufs);
Packit 631bab
    if (drmIoctl(fd, DRM_IOCTL_MAP_BUFS, &bufs))
Packit 631bab
        return NULL;
Packit 631bab
Packit 631bab
    if (!bufs.count)
Packit 631bab
        return NULL;
Packit 631bab
Packit 631bab
    if (!(bufs.list = drmMalloc(bufs.count * sizeof(*bufs.list))))
Packit 631bab
        return NULL;
Packit 631bab
Packit 631bab
    if (drmIoctl(fd, DRM_IOCTL_MAP_BUFS, &bufs)) {
Packit 631bab
        drmFree(bufs.list);
Packit 631bab
        return NULL;
Packit 631bab
    }
Packit 631bab
Packit 631bab
    retval = drmMalloc(sizeof(*retval));
Packit 631bab
    retval->count = bufs.count;
Packit 631bab
    retval->list  = drmMalloc(bufs.count * sizeof(*retval->list));
Packit 631bab
    for (i = 0; i < bufs.count; i++) {
Packit 631bab
        retval->list[i].idx     = bufs.list[i].idx;
Packit 631bab
        retval->list[i].total   = bufs.list[i].total;
Packit 631bab
        retval->list[i].used    = 0;
Packit 631bab
        retval->list[i].address = bufs.list[i].address;
Packit 631bab
    }
Packit 631bab
Packit 631bab
    drmFree(bufs.list);
Packit 631bab
    return retval;
Packit 631bab
}
Packit 631bab
Packit 631bab
Packit 631bab
/**
Packit 631bab
 * Unmap buffers allocated with drmMapBufs().
Packit 631bab
 *
Packit 631bab
 * \return zero on success, or negative value on failure.
Packit 631bab
 *
Packit 631bab
 * \internal
Packit 631bab
 * Calls munmap() for every buffer stored in \p bufs and frees the
Packit 631bab
 * memory allocated by drmMapBufs().
Packit 631bab
 */
Packit 631bab
drm_public int drmUnmapBufs(drmBufMapPtr bufs)
Packit 631bab
{
Packit 631bab
    int i;
Packit 631bab
Packit 631bab
    for (i = 0; i < bufs->count; i++) {
Packit 631bab
        drm_munmap(bufs->list[i].address, bufs->list[i].total);
Packit 631bab
    }
Packit 631bab
Packit 631bab
    drmFree(bufs->list);
Packit 631bab
    drmFree(bufs);
Packit 631bab
    return 0;
Packit 631bab
}
Packit 631bab
Packit 631bab
Packit 631bab
#define DRM_DMA_RETRY  16
Packit 631bab
Packit 631bab
/**
Packit 631bab
 * Reserve DMA buffers.
Packit 631bab
 *
Packit 631bab
 * \param fd file descriptor.
Packit 631bab
 * \param request
Packit 631bab
 *
Packit 631bab
 * \return zero on success, or a negative value on failure.
Packit 631bab
 *
Packit 631bab
 * \internal
Packit 631bab
 * Assemble the arguments into a drm_dma structure and keeps issuing the
Packit 631bab
 * DRM_IOCTL_DMA ioctl until success or until maximum number of retries.
Packit 631bab
 */
Packit 631bab
drm_public int drmDMA(int fd, drmDMAReqPtr request)
Packit 631bab
{
Packit 631bab
    drm_dma_t dma;
Packit 631bab
    int ret, i = 0;
Packit 631bab
Packit 631bab
    dma.context         = request->context;
Packit 631bab
    dma.send_count      = request->send_count;
Packit 631bab
    dma.send_indices    = request->send_list;
Packit 631bab
    dma.send_sizes      = request->send_sizes;
Packit 631bab
    dma.flags           = request->flags;
Packit 631bab
    dma.request_count   = request->request_count;
Packit 631bab
    dma.request_size    = request->request_size;
Packit 631bab
    dma.request_indices = request->request_list;
Packit 631bab
    dma.request_sizes   = request->request_sizes;
Packit 631bab
    dma.granted_count   = 0;
Packit 631bab
Packit 631bab
    do {
Packit 631bab
        ret = ioctl( fd, DRM_IOCTL_DMA, &dma );
Packit 631bab
    } while ( ret && errno == EAGAIN && i++ < DRM_DMA_RETRY );
Packit 631bab
Packit 631bab
    if ( ret == 0 ) {
Packit 631bab
        request->granted_count = dma.granted_count;
Packit 631bab
        return 0;
Packit 631bab
    } else {
Packit 631bab
        return -errno;
Packit 631bab
    }
Packit 631bab
}
Packit 631bab
Packit 631bab
Packit 631bab
/**
Packit 631bab
 * Obtain heavyweight hardware lock.
Packit 631bab
 *
Packit 631bab
 * \param fd file descriptor.
Packit 631bab
 * \param context context.
Packit 631bab
 * \param flags flags that determine the state of the hardware when the function
Packit 631bab
 * returns.
Packit 631bab
 *
Packit 631bab
 * \return always zero.
Packit 631bab
 *
Packit 631bab
 * \internal
Packit 631bab
 * This function translates the arguments into a drm_lock structure and issue
Packit 631bab
 * the DRM_IOCTL_LOCK ioctl until the lock is successfully acquired.
Packit 631bab
 */
Packit 631bab
drm_public int drmGetLock(int fd, drm_context_t context, drmLockFlags flags)
Packit 631bab
{
Packit 631bab
    drm_lock_t lock;
Packit 631bab
Packit 631bab
    memclear(lock);
Packit 631bab
    lock.context = context;
Packit 631bab
    lock.flags   = 0;
Packit 631bab
    if (flags & DRM_LOCK_READY)      lock.flags |= _DRM_LOCK_READY;
Packit 631bab
    if (flags & DRM_LOCK_QUIESCENT)  lock.flags |= _DRM_LOCK_QUIESCENT;
Packit 631bab
    if (flags & DRM_LOCK_FLUSH)      lock.flags |= _DRM_LOCK_FLUSH;
Packit 631bab
    if (flags & DRM_LOCK_FLUSH_ALL)  lock.flags |= _DRM_LOCK_FLUSH_ALL;
Packit 631bab
    if (flags & DRM_HALT_ALL_QUEUES) lock.flags |= _DRM_HALT_ALL_QUEUES;
Packit 631bab
    if (flags & DRM_HALT_CUR_QUEUES) lock.flags |= _DRM_HALT_CUR_QUEUES;
Packit 631bab
Packit 631bab
    while (drmIoctl(fd, DRM_IOCTL_LOCK, &lock))
Packit 631bab
        ;
Packit 631bab
    return 0;
Packit 631bab
}
Packit 631bab
Packit 631bab
/**
Packit 631bab
 * Release the hardware lock.
Packit 631bab
 *
Packit 631bab
 * \param fd file descriptor.
Packit 631bab
 * \param context context.
Packit 631bab
 *
Packit 631bab
 * \return zero on success, or a negative value on failure.
Packit 631bab
 *
Packit 631bab
 * \internal
Packit 631bab
 * This function is a wrapper around the DRM_IOCTL_UNLOCK ioctl, passing the
Packit 631bab
 * argument in a drm_lock structure.
Packit 631bab
 */
Packit 631bab
drm_public int drmUnlock(int fd, drm_context_t context)
Packit 631bab
{
Packit 631bab
    drm_lock_t lock;
Packit 631bab
Packit 631bab
    memclear(lock);
Packit 631bab
    lock.context = context;
Packit 631bab
    return drmIoctl(fd, DRM_IOCTL_UNLOCK, &lock);
Packit 631bab
}
Packit 631bab
Packit 631bab
drm_public drm_context_t *drmGetReservedContextList(int fd, int *count)
Packit 631bab
{
Packit 631bab
    drm_ctx_res_t res;
Packit 631bab
    drm_ctx_t     *list;
Packit 631bab
    drm_context_t * retval;
Packit 631bab
    int           i;
Packit 631bab
Packit 631bab
    memclear(res);
Packit 631bab
    if (drmIoctl(fd, DRM_IOCTL_RES_CTX, &res))
Packit 631bab
        return NULL;
Packit 631bab
Packit 631bab
    if (!res.count)
Packit 631bab
        return NULL;
Packit 631bab
Packit 631bab
    if (!(list   = drmMalloc(res.count * sizeof(*list))))
Packit 631bab
        return NULL;
Packit 631bab
    if (!(retval = drmMalloc(res.count * sizeof(*retval))))
Packit 631bab
        goto err_free_list;
Packit 631bab
Packit 631bab
    res.contexts = list;
Packit 631bab
    if (drmIoctl(fd, DRM_IOCTL_RES_CTX, &res))
Packit 631bab
        goto err_free_context;
Packit 631bab
Packit 631bab
    for (i = 0; i < res.count; i++)
Packit 631bab
        retval[i] = list[i].handle;
Packit 631bab
    drmFree(list);
Packit 631bab
Packit 631bab
    *count = res.count;
Packit 631bab
    return retval;
Packit 631bab
Packit 631bab
err_free_list:
Packit 631bab
    drmFree(list);
Packit 631bab
err_free_context:
Packit 631bab
    drmFree(retval);
Packit 631bab
    return NULL;
Packit 631bab
}
Packit 631bab
Packit 631bab
drm_public void drmFreeReservedContextList(drm_context_t *pt)
Packit 631bab
{
Packit 631bab
    drmFree(pt);
Packit 631bab
}
Packit 631bab
Packit 631bab
/**
Packit 631bab
 * Create context.
Packit 631bab
 *
Packit 631bab
 * Used by the X server during GLXContext initialization. This causes
Packit 631bab
 * per-context kernel-level resources to be allocated.
Packit 631bab
 *
Packit 631bab
 * \param fd file descriptor.
Packit 631bab
 * \param handle is set on success. To be used by the client when requesting DMA
Packit 631bab
 * dispatch with drmDMA().
Packit 631bab
 *
Packit 631bab
 * \return zero on success, or a negative value on failure.
Packit 631bab
 *
Packit 631bab
 * \note May only be called by root.
Packit 631bab
 *
Packit 631bab
 * \internal
Packit 631bab
 * This function is a wrapper around the DRM_IOCTL_ADD_CTX ioctl, passing the
Packit 631bab
 * argument in a drm_ctx structure.
Packit 631bab
 */
Packit 631bab
drm_public int drmCreateContext(int fd, drm_context_t *handle)
Packit 631bab
{
Packit 631bab
    drm_ctx_t ctx;
Packit 631bab
Packit 631bab
    memclear(ctx);
Packit 631bab
    if (drmIoctl(fd, DRM_IOCTL_ADD_CTX, &ctx))
Packit 631bab
        return -errno;
Packit 631bab
    *handle = ctx.handle;
Packit 631bab
    return 0;
Packit 631bab
}
Packit 631bab
Packit 631bab
drm_public int drmSwitchToContext(int fd, drm_context_t context)
Packit 631bab
{
Packit 631bab
    drm_ctx_t ctx;
Packit 631bab
Packit 631bab
    memclear(ctx);
Packit 631bab
    ctx.handle = context;
Packit 631bab
    if (drmIoctl(fd, DRM_IOCTL_SWITCH_CTX, &ctx))
Packit 631bab
        return -errno;
Packit 631bab
    return 0;
Packit 631bab
}
Packit 631bab
Packit 631bab
drm_public int drmSetContextFlags(int fd, drm_context_t context,
Packit 631bab
                                  drm_context_tFlags flags)
Packit 631bab
{
Packit 631bab
    drm_ctx_t ctx;
Packit 631bab
Packit 631bab
    /*
Packit 631bab
     * Context preserving means that no context switches are done between DMA
Packit 631bab
     * buffers from one context and the next.  This is suitable for use in the
Packit 631bab
     * X server (which promises to maintain hardware context), or in the
Packit 631bab
     * client-side library when buffers are swapped on behalf of two threads.
Packit 631bab
     */
Packit 631bab
    memclear(ctx);
Packit 631bab
    ctx.handle = context;
Packit 631bab
    if (flags & DRM_CONTEXT_PRESERVED)
Packit 631bab
        ctx.flags |= _DRM_CONTEXT_PRESERVED;
Packit 631bab
    if (flags & DRM_CONTEXT_2DONLY)
Packit 631bab
        ctx.flags |= _DRM_CONTEXT_2DONLY;
Packit 631bab
    if (drmIoctl(fd, DRM_IOCTL_MOD_CTX, &ctx))
Packit 631bab
        return -errno;
Packit 631bab
    return 0;
Packit 631bab
}
Packit 631bab
Packit 631bab
drm_public int drmGetContextFlags(int fd, drm_context_t context,
Packit 631bab
                                  drm_context_tFlagsPtr flags)
Packit 631bab
{
Packit 631bab
    drm_ctx_t ctx;
Packit 631bab
Packit 631bab
    memclear(ctx);
Packit 631bab
    ctx.handle = context;
Packit 631bab
    if (drmIoctl(fd, DRM_IOCTL_GET_CTX, &ctx))
Packit 631bab
        return -errno;
Packit 631bab
    *flags = 0;
Packit 631bab
    if (ctx.flags & _DRM_CONTEXT_PRESERVED)
Packit 631bab
        *flags |= DRM_CONTEXT_PRESERVED;
Packit 631bab
    if (ctx.flags & _DRM_CONTEXT_2DONLY)
Packit 631bab
        *flags |= DRM_CONTEXT_2DONLY;
Packit 631bab
    return 0;
Packit 631bab
}
Packit 631bab
Packit 631bab
/**
Packit 631bab
 * Destroy context.
Packit 631bab
 *
Packit 631bab
 * Free any kernel-level resources allocated with drmCreateContext() associated
Packit 631bab
 * with the context.
Packit 631bab
 *
Packit 631bab
 * \param fd file descriptor.
Packit 631bab
 * \param handle handle given by drmCreateContext().
Packit 631bab
 *
Packit 631bab
 * \return zero on success, or a negative value on failure.
Packit 631bab
 *
Packit 631bab
 * \note May only be called by root.
Packit 631bab
 *
Packit 631bab
 * \internal
Packit 631bab
 * This function is a wrapper around the DRM_IOCTL_RM_CTX ioctl, passing the
Packit 631bab
 * argument in a drm_ctx structure.
Packit 631bab
 */
Packit 631bab
drm_public int drmDestroyContext(int fd, drm_context_t handle)
Packit 631bab
{
Packit 631bab
    drm_ctx_t ctx;
Packit 631bab
Packit 631bab
    memclear(ctx);
Packit 631bab
    ctx.handle = handle;
Packit 631bab
    if (drmIoctl(fd, DRM_IOCTL_RM_CTX, &ctx))
Packit 631bab
        return -errno;
Packit 631bab
    return 0;
Packit 631bab
}
Packit 631bab
Packit 631bab
drm_public int drmCreateDrawable(int fd, drm_drawable_t *handle)
Packit 631bab
{
Packit 631bab
    drm_draw_t draw;
Packit 631bab
Packit 631bab
    memclear(draw);
Packit 631bab
    if (drmIoctl(fd, DRM_IOCTL_ADD_DRAW, &draw))
Packit 631bab
        return -errno;
Packit 631bab
    *handle = draw.handle;
Packit 631bab
    return 0;
Packit 631bab
}
Packit 631bab
Packit 631bab
drm_public int drmDestroyDrawable(int fd, drm_drawable_t handle)
Packit 631bab
{
Packit 631bab
    drm_draw_t draw;
Packit 631bab
Packit 631bab
    memclear(draw);
Packit 631bab
    draw.handle = handle;
Packit 631bab
    if (drmIoctl(fd, DRM_IOCTL_RM_DRAW, &draw))
Packit 631bab
        return -errno;
Packit 631bab
    return 0;
Packit 631bab
}
Packit 631bab
Packit 631bab
drm_public int drmUpdateDrawableInfo(int fd, drm_drawable_t handle,
Packit 631bab
                                     drm_drawable_info_type_t type,
Packit 631bab
                                     unsigned int num, void *data)
Packit 631bab
{
Packit 631bab
    drm_update_draw_t update;
Packit 631bab
Packit 631bab
    memclear(update);
Packit 631bab
    update.handle = handle;
Packit 631bab
    update.type = type;
Packit 631bab
    update.num = num;
Packit 631bab
    update.data = (unsigned long long)(unsigned long)data;
Packit 631bab
Packit 631bab
    if (drmIoctl(fd, DRM_IOCTL_UPDATE_DRAW, &update))
Packit 631bab
        return -errno;
Packit 631bab
Packit 631bab
    return 0;
Packit 631bab
}
Packit 631bab
Packit 631bab
drm_public int drmCrtcGetSequence(int fd, uint32_t crtcId, uint64_t *sequence,
Packit 631bab
                                  uint64_t *ns)
Packit 631bab
{
Packit 631bab
    struct drm_crtc_get_sequence get_seq;
Packit 631bab
    int ret;
Packit 631bab
Packit 631bab
    memclear(get_seq);
Packit 631bab
    get_seq.crtc_id = crtcId;
Packit 631bab
    ret = drmIoctl(fd, DRM_IOCTL_CRTC_GET_SEQUENCE, &get_seq);
Packit 631bab
    if (ret)
Packit 631bab
        return ret;
Packit 631bab
Packit 631bab
    if (sequence)
Packit 631bab
        *sequence = get_seq.sequence;
Packit 631bab
    if (ns)
Packit 631bab
        *ns = get_seq.sequence_ns;
Packit 631bab
    return 0;
Packit 631bab
}
Packit 631bab
Packit 631bab
drm_public int drmCrtcQueueSequence(int fd, uint32_t crtcId, uint32_t flags,
Packit 631bab
                                    uint64_t sequence,
Packit 631bab
                                    uint64_t *sequence_queued,
Packit 631bab
                                    uint64_t user_data)
Packit 631bab
{
Packit 631bab
    struct drm_crtc_queue_sequence queue_seq;
Packit 631bab
    int ret;
Packit 631bab
Packit 631bab
    memclear(queue_seq);
Packit 631bab
    queue_seq.crtc_id = crtcId;
Packit 631bab
    queue_seq.flags = flags;
Packit 631bab
    queue_seq.sequence = sequence;
Packit 631bab
    queue_seq.user_data = user_data;
Packit 631bab
Packit 631bab
    ret = drmIoctl(fd, DRM_IOCTL_CRTC_QUEUE_SEQUENCE, &queue_seq);
Packit 631bab
    if (ret == 0 && sequence_queued)
Packit 631bab
        *sequence_queued = queue_seq.sequence;
Packit 631bab
Packit 631bab
    return ret;
Packit 631bab
}
Packit 631bab
Packit 631bab
/**
Packit 631bab
 * Acquire the AGP device.
Packit 631bab
 *
Packit 631bab
 * Must be called before any of the other AGP related calls.
Packit 631bab
 *
Packit 631bab
 * \param fd file descriptor.
Packit 631bab
 *
Packit 631bab
 * \return zero on success, or a negative value on failure.
Packit 631bab
 *
Packit 631bab
 * \internal
Packit 631bab
 * This function is a wrapper around the DRM_IOCTL_AGP_ACQUIRE ioctl.
Packit 631bab
 */
Packit 631bab
drm_public int drmAgpAcquire(int fd)
Packit 631bab
{
Packit 631bab
    if (drmIoctl(fd, DRM_IOCTL_AGP_ACQUIRE, NULL))
Packit 631bab
        return -errno;
Packit 631bab
    return 0;
Packit 631bab
}
Packit 631bab
Packit 631bab
Packit 631bab
/**
Packit 631bab
 * Release the AGP device.
Packit 631bab
 *
Packit 631bab
 * \param fd file descriptor.
Packit 631bab
 *
Packit 631bab
 * \return zero on success, or a negative value on failure.
Packit 631bab
 *
Packit 631bab
 * \internal
Packit 631bab
 * This function is a wrapper around the DRM_IOCTL_AGP_RELEASE ioctl.
Packit 631bab
 */
Packit 631bab
drm_public int drmAgpRelease(int fd)
Packit 631bab
{
Packit 631bab
    if (drmIoctl(fd, DRM_IOCTL_AGP_RELEASE, NULL))
Packit 631bab
        return -errno;
Packit 631bab
    return 0;
Packit 631bab
}
Packit 631bab
Packit 631bab
Packit 631bab
/**
Packit 631bab
 * Set the AGP mode.
Packit 631bab
 *
Packit 631bab
 * \param fd file descriptor.
Packit 631bab
 * \param mode AGP mode.
Packit 631bab
 *
Packit 631bab
 * \return zero on success, or a negative value on failure.
Packit 631bab
 *
Packit 631bab
 * \internal
Packit 631bab
 * This function is a wrapper around the DRM_IOCTL_AGP_ENABLE ioctl, passing the
Packit 631bab
 * argument in a drm_agp_mode structure.
Packit 631bab
 */
Packit 631bab
drm_public int drmAgpEnable(int fd, unsigned long mode)
Packit 631bab
{
Packit 631bab
    drm_agp_mode_t m;
Packit 631bab
Packit 631bab
    memclear(m);
Packit 631bab
    m.mode = mode;
Packit 631bab
    if (drmIoctl(fd, DRM_IOCTL_AGP_ENABLE, &m))
Packit 631bab
        return -errno;
Packit 631bab
    return 0;
Packit 631bab
}
Packit 631bab
Packit 631bab
Packit 631bab
/**
Packit 631bab
 * Allocate a chunk of AGP memory.
Packit 631bab
 *
Packit 631bab
 * \param fd file descriptor.
Packit 631bab
 * \param size requested memory size in bytes. Will be rounded to page boundary.
Packit 631bab
 * \param type type of memory to allocate.
Packit 631bab
 * \param address if not zero, will be set to the physical address of the
Packit 631bab
 * allocated memory.
Packit 631bab
 * \param handle on success will be set to a handle of the allocated memory.
Packit 631bab
 *
Packit 631bab
 * \return zero on success, or a negative value on failure.
Packit 631bab
 *
Packit 631bab
 * \internal
Packit 631bab
 * This function is a wrapper around the DRM_IOCTL_AGP_ALLOC ioctl, passing the
Packit 631bab
 * arguments in a drm_agp_buffer structure.
Packit 631bab
 */
Packit 631bab
drm_public int drmAgpAlloc(int fd, unsigned long size, unsigned long type,
Packit 631bab
                           unsigned long *address, drm_handle_t *handle)
Packit 631bab
{
Packit 631bab
    drm_agp_buffer_t b;
Packit 631bab
Packit 631bab
    memclear(b);
Packit 631bab
    *handle = DRM_AGP_NO_HANDLE;
Packit 631bab
    b.size   = size;
Packit 631bab
    b.type   = type;
Packit 631bab
    if (drmIoctl(fd, DRM_IOCTL_AGP_ALLOC, &b))
Packit 631bab
        return -errno;
Packit 631bab
    if (address != 0UL)
Packit 631bab
        *address = b.physical;
Packit 631bab
    *handle = b.handle;
Packit 631bab
    return 0;
Packit 631bab
}
Packit 631bab
Packit 631bab
Packit 631bab
/**
Packit 631bab
 * Free a chunk of AGP memory.
Packit 631bab
 *
Packit 631bab
 * \param fd file descriptor.
Packit 631bab
 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
Packit 631bab
 *
Packit 631bab
 * \return zero on success, or a negative value on failure.
Packit 631bab
 *
Packit 631bab
 * \internal
Packit 631bab
 * This function is a wrapper around the DRM_IOCTL_AGP_FREE ioctl, passing the
Packit 631bab
 * argument in a drm_agp_buffer structure.
Packit 631bab
 */
Packit 631bab
drm_public int drmAgpFree(int fd, drm_handle_t handle)
Packit 631bab
{
Packit 631bab
    drm_agp_buffer_t b;
Packit 631bab
Packit 631bab
    memclear(b);
Packit 631bab
    b.handle = handle;
Packit 631bab
    if (drmIoctl(fd, DRM_IOCTL_AGP_FREE, &b))
Packit 631bab
        return -errno;
Packit 631bab
    return 0;
Packit 631bab
}
Packit 631bab
Packit 631bab
Packit 631bab
/**
Packit 631bab
 * Bind a chunk of AGP memory.
Packit 631bab
 *
Packit 631bab
 * \param fd file descriptor.
Packit 631bab
 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
Packit 631bab
 * \param offset offset in bytes. It will round to page boundary.
Packit 631bab
 *
Packit 631bab
 * \return zero on success, or a negative value on failure.
Packit 631bab
 *
Packit 631bab
 * \internal
Packit 631bab
 * This function is a wrapper around the DRM_IOCTL_AGP_BIND ioctl, passing the
Packit 631bab
 * argument in a drm_agp_binding structure.
Packit 631bab
 */
Packit 631bab
drm_public int drmAgpBind(int fd, drm_handle_t handle, unsigned long offset)
Packit 631bab
{
Packit 631bab
    drm_agp_binding_t b;
Packit 631bab
Packit 631bab
    memclear(b);
Packit 631bab
    b.handle = handle;
Packit 631bab
    b.offset = offset;
Packit 631bab
    if (drmIoctl(fd, DRM_IOCTL_AGP_BIND, &b))
Packit 631bab
        return -errno;
Packit 631bab
    return 0;
Packit 631bab
}
Packit 631bab
Packit 631bab
Packit 631bab
/**
Packit 631bab
 * Unbind a chunk of AGP memory.
Packit 631bab
 *
Packit 631bab
 * \param fd file descriptor.
Packit 631bab
 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
Packit 631bab
 *
Packit 631bab
 * \return zero on success, or a negative value on failure.
Packit 631bab
 *
Packit 631bab
 * \internal
Packit 631bab
 * This function is a wrapper around the DRM_IOCTL_AGP_UNBIND ioctl, passing
Packit 631bab
 * the argument in a drm_agp_binding structure.
Packit 631bab
 */
Packit 631bab
drm_public int drmAgpUnbind(int fd, drm_handle_t handle)
Packit 631bab
{
Packit 631bab
    drm_agp_binding_t b;
Packit 631bab
Packit 631bab
    memclear(b);
Packit 631bab
    b.handle = handle;
Packit 631bab
    if (drmIoctl(fd, DRM_IOCTL_AGP_UNBIND, &b))
Packit 631bab
        return -errno;
Packit 631bab
    return 0;
Packit 631bab
}
Packit 631bab
Packit 631bab
Packit 631bab
/**
Packit 631bab
 * Get AGP driver major version number.
Packit 631bab
 *
Packit 631bab
 * \param fd file descriptor.
Packit 631bab
 *
Packit 631bab
 * \return major version number on success, or a negative value on failure..
Packit 631bab
 *
Packit 631bab
 * \internal
Packit 631bab
 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
Packit 631bab
 * necessary information in a drm_agp_info structure.
Packit 631bab
 */
Packit 631bab
drm_public int drmAgpVersionMajor(int fd)
Packit 631bab
{
Packit 631bab
    drm_agp_info_t i;
Packit 631bab
Packit 631bab
    memclear(i);
Packit 631bab
Packit 631bab
    if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
Packit 631bab
        return -errno;
Packit 631bab
    return i.agp_version_major;
Packit 631bab
}
Packit 631bab
Packit 631bab
Packit 631bab
/**
Packit 631bab
 * Get AGP driver minor version number.
Packit 631bab
 *
Packit 631bab
 * \param fd file descriptor.
Packit 631bab
 *
Packit 631bab
 * \return minor version number on success, or a negative value on failure.
Packit 631bab
 *
Packit 631bab
 * \internal
Packit 631bab
 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
Packit 631bab
 * necessary information in a drm_agp_info structure.
Packit 631bab
 */
Packit 631bab
drm_public int drmAgpVersionMinor(int fd)
Packit 631bab
{
Packit 631bab
    drm_agp_info_t i;
Packit 631bab
Packit 631bab
    memclear(i);
Packit 631bab
Packit 631bab
    if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
Packit 631bab
        return -errno;
Packit 631bab
    return i.agp_version_minor;
Packit 631bab
}
Packit 631bab
Packit 631bab
Packit 631bab
/**
Packit 631bab
 * Get AGP mode.
Packit 631bab
 *
Packit 631bab
 * \param fd file descriptor.
Packit 631bab
 *
Packit 631bab
 * \return mode on success, or zero on failure.
Packit 631bab
 *
Packit 631bab
 * \internal
Packit 631bab
 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
Packit 631bab
 * necessary information in a drm_agp_info structure.
Packit 631bab
 */
Packit 631bab
drm_public unsigned long drmAgpGetMode(int fd)
Packit 631bab
{
Packit 631bab
    drm_agp_info_t i;
Packit 631bab
Packit 631bab
    memclear(i);
Packit 631bab
Packit 631bab
    if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
Packit 631bab
        return 0;
Packit 631bab
    return i.mode;
Packit 631bab
}
Packit 631bab
Packit 631bab
Packit 631bab
/**
Packit 631bab
 * Get AGP aperture base.
Packit 631bab
 *
Packit 631bab
 * \param fd file descriptor.
Packit 631bab
 *
Packit 631bab
 * \return aperture base on success, zero on failure.
Packit 631bab
 *
Packit 631bab
 * \internal
Packit 631bab
 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
Packit 631bab
 * necessary information in a drm_agp_info structure.
Packit 631bab
 */
Packit 631bab
drm_public unsigned long drmAgpBase(int fd)
Packit 631bab
{
Packit 631bab
    drm_agp_info_t i;
Packit 631bab
Packit 631bab
    memclear(i);
Packit 631bab
Packit 631bab
    if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
Packit 631bab
        return 0;
Packit 631bab
    return i.aperture_base;
Packit 631bab
}
Packit 631bab
Packit 631bab
Packit 631bab
/**
Packit 631bab
 * Get AGP aperture size.
Packit 631bab
 *
Packit 631bab
 * \param fd file descriptor.
Packit 631bab
 *
Packit 631bab
 * \return aperture size on success, zero on failure.
Packit 631bab
 *
Packit 631bab
 * \internal
Packit 631bab
 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
Packit 631bab
 * necessary information in a drm_agp_info structure.
Packit 631bab
 */
Packit 631bab
drm_public unsigned long drmAgpSize(int fd)
Packit 631bab
{
Packit 631bab
    drm_agp_info_t i;
Packit 631bab
Packit 631bab
    memclear(i);
Packit 631bab
Packit 631bab
    if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
Packit 631bab
        return 0;
Packit 631bab
    return i.aperture_size;
Packit 631bab
}
Packit 631bab
Packit 631bab
Packit 631bab
/**
Packit 631bab
 * Get used AGP memory.
Packit 631bab
 *
Packit 631bab
 * \param fd file descriptor.
Packit 631bab
 *
Packit 631bab
 * \return memory used on success, or zero on failure.
Packit 631bab
 *
Packit 631bab
 * \internal
Packit 631bab
 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
Packit 631bab
 * necessary information in a drm_agp_info structure.
Packit 631bab
 */
Packit 631bab
drm_public unsigned long drmAgpMemoryUsed(int fd)
Packit 631bab
{
Packit 631bab
    drm_agp_info_t i;
Packit 631bab
Packit 631bab
    memclear(i);
Packit 631bab
Packit 631bab
    if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
Packit 631bab
        return 0;
Packit 631bab
    return i.memory_used;
Packit 631bab
}
Packit 631bab
Packit 631bab
Packit 631bab
/**
Packit 631bab
 * Get available AGP memory.
Packit 631bab
 *
Packit 631bab
 * \param fd file descriptor.
Packit 631bab
 *
Packit 631bab
 * \return memory available on success, or zero on failure.
Packit 631bab
 *
Packit 631bab
 * \internal
Packit 631bab
 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
Packit 631bab
 * necessary information in a drm_agp_info structure.
Packit 631bab
 */
Packit 631bab
drm_public unsigned long drmAgpMemoryAvail(int fd)
Packit 631bab
{
Packit 631bab
    drm_agp_info_t i;
Packit 631bab
Packit 631bab
    memclear(i);
Packit 631bab
Packit 631bab
    if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
Packit 631bab
        return 0;
Packit 631bab
    return i.memory_allowed;
Packit 631bab
}
Packit 631bab
Packit 631bab
Packit 631bab
/**
Packit 631bab
 * Get hardware vendor ID.
Packit 631bab
 *
Packit 631bab
 * \param fd file descriptor.
Packit 631bab
 *
Packit 631bab
 * \return vendor ID on success, or zero on failure.
Packit 631bab
 *
Packit 631bab
 * \internal
Packit 631bab
 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
Packit 631bab
 * necessary information in a drm_agp_info structure.
Packit 631bab
 */
Packit 631bab
drm_public unsigned int drmAgpVendorId(int fd)
Packit 631bab
{
Packit 631bab
    drm_agp_info_t i;
Packit 631bab
Packit 631bab
    memclear(i);
Packit 631bab
Packit 631bab
    if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
Packit 631bab
        return 0;
Packit 631bab
    return i.id_vendor;
Packit 631bab
}
Packit 631bab
Packit 631bab
Packit 631bab
/**
Packit 631bab
 * Get hardware device ID.
Packit 631bab
 *
Packit 631bab
 * \param fd file descriptor.
Packit 631bab
 *
Packit 631bab
 * \return zero on success, or zero on failure.
Packit 631bab
 *
Packit 631bab
 * \internal
Packit 631bab
 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
Packit 631bab
 * necessary information in a drm_agp_info structure.
Packit 631bab
 */
Packit 631bab
drm_public unsigned int drmAgpDeviceId(int fd)
Packit 631bab
{
Packit 631bab
    drm_agp_info_t i;
Packit 631bab
Packit 631bab
    memclear(i);
Packit 631bab
Packit 631bab
    if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
Packit 631bab
        return 0;
Packit 631bab
    return i.id_device;
Packit 631bab
}
Packit 631bab
Packit 631bab
drm_public int drmScatterGatherAlloc(int fd, unsigned long size,
Packit 631bab
                                     drm_handle_t *handle)
Packit 631bab
{
Packit 631bab
    drm_scatter_gather_t sg;
Packit 631bab
Packit 631bab
    memclear(sg);
Packit 631bab
Packit 631bab
    *handle = 0;
Packit 631bab
    sg.size   = size;
Packit 631bab
    if (drmIoctl(fd, DRM_IOCTL_SG_ALLOC, &sg))
Packit 631bab
        return -errno;
Packit 631bab
    *handle = sg.handle;
Packit 631bab
    return 0;
Packit 631bab
}
Packit 631bab
Packit 631bab
drm_public int drmScatterGatherFree(int fd, drm_handle_t handle)
Packit 631bab
{
Packit 631bab
    drm_scatter_gather_t sg;
Packit 631bab
Packit 631bab
    memclear(sg);
Packit 631bab
    sg.handle = handle;
Packit 631bab
    if (drmIoctl(fd, DRM_IOCTL_SG_FREE, &sg))
Packit 631bab
        return -errno;
Packit 631bab
    return 0;
Packit 631bab
}
Packit 631bab
Packit 631bab
/**
Packit 631bab
 * Wait for VBLANK.
Packit 631bab
 *
Packit 631bab
 * \param fd file descriptor.
Packit 631bab
 * \param vbl pointer to a drmVBlank structure.
Packit 631bab
 *
Packit 631bab
 * \return zero on success, or a negative value on failure.
Packit 631bab
 *
Packit 631bab
 * \internal
Packit 631bab
 * This function is a wrapper around the DRM_IOCTL_WAIT_VBLANK ioctl.
Packit 631bab
 */
Packit 631bab
drm_public int drmWaitVBlank(int fd, drmVBlankPtr vbl)
Packit 631bab
{
Packit 631bab
    struct timespec timeout, cur;
Packit 631bab
    int ret;
Packit 631bab
Packit 631bab
    ret = clock_gettime(CLOCK_MONOTONIC, &timeout);
Packit 631bab
    if (ret < 0) {
Packit 631bab
        fprintf(stderr, "clock_gettime failed: %s\n", strerror(errno));
Packit 631bab
        goto out;
Packit 631bab
    }
Packit 631bab
    timeout.tv_sec++;
Packit 631bab
Packit 631bab
    do {
Packit 631bab
       ret = ioctl(fd, DRM_IOCTL_WAIT_VBLANK, vbl);
Packit 631bab
       vbl->request.type &= ~DRM_VBLANK_RELATIVE;
Packit 631bab
       if (ret && errno == EINTR) {
Packit 631bab
           clock_gettime(CLOCK_MONOTONIC, &cur);
Packit 631bab
           /* Timeout after 1s */
Packit 631bab
           if (cur.tv_sec > timeout.tv_sec + 1 ||
Packit 631bab
               (cur.tv_sec == timeout.tv_sec && cur.tv_nsec >=
Packit 631bab
                timeout.tv_nsec)) {
Packit 631bab
                   errno = EBUSY;
Packit 631bab
                   ret = -1;
Packit 631bab
                   break;
Packit 631bab
           }
Packit 631bab
       }
Packit 631bab
    } while (ret && errno == EINTR);
Packit 631bab
Packit 631bab
out:
Packit 631bab
    return ret;
Packit 631bab
}
Packit 631bab
Packit 631bab
drm_public int drmError(int err, const char *label)
Packit 631bab
{
Packit 631bab
    switch (err) {
Packit 631bab
    case DRM_ERR_NO_DEVICE:
Packit 631bab
        fprintf(stderr, "%s: no device\n", label);
Packit 631bab
        break;
Packit 631bab
    case DRM_ERR_NO_ACCESS:
Packit 631bab
        fprintf(stderr, "%s: no access\n", label);
Packit 631bab
        break;
Packit 631bab
    case DRM_ERR_NOT_ROOT:
Packit 631bab
        fprintf(stderr, "%s: not root\n", label);
Packit 631bab
        break;
Packit 631bab
    case DRM_ERR_INVALID:
Packit 631bab
        fprintf(stderr, "%s: invalid args\n", label);
Packit 631bab
        break;
Packit 631bab
    default:
Packit 631bab
        if (err < 0)
Packit 631bab
            err = -err;
Packit 631bab
        fprintf( stderr, "%s: error %d (%s)\n", label, err, strerror(err) );
Packit 631bab
        break;
Packit 631bab
    }
Packit 631bab
Packit 631bab
    return 1;
Packit 631bab
}
Packit 631bab
Packit 631bab
/**
Packit 631bab
 * Install IRQ handler.
Packit 631bab
 *
Packit 631bab
 * \param fd file descriptor.
Packit 631bab
 * \param irq IRQ number.
Packit 631bab
 *
Packit 631bab
 * \return zero on success, or a negative value on failure.
Packit 631bab
 *
Packit 631bab
 * \internal
Packit 631bab
 * This function is a wrapper around the DRM_IOCTL_CONTROL ioctl, passing the
Packit 631bab
 * argument in a drm_control structure.
Packit 631bab
 */
Packit 631bab
drm_public int drmCtlInstHandler(int fd, int irq)
Packit 631bab
{
Packit 631bab
    drm_control_t ctl;
Packit 631bab
Packit 631bab
    memclear(ctl);
Packit 631bab
    ctl.func  = DRM_INST_HANDLER;
Packit 631bab
    ctl.irq   = irq;
Packit 631bab
    if (drmIoctl(fd, DRM_IOCTL_CONTROL, &ctl))
Packit 631bab
        return -errno;
Packit 631bab
    return 0;
Packit 631bab
}
Packit 631bab
Packit 631bab
Packit 631bab
/**
Packit 631bab
 * Uninstall IRQ handler.
Packit 631bab
 *
Packit 631bab
 * \param fd file descriptor.
Packit 631bab
 *
Packit 631bab
 * \return zero on success, or a negative value on failure.
Packit 631bab
 *
Packit 631bab
 * \internal
Packit 631bab
 * This function is a wrapper around the DRM_IOCTL_CONTROL ioctl, passing the
Packit 631bab
 * argument in a drm_control structure.
Packit 631bab
 */
Packit 631bab
drm_public int drmCtlUninstHandler(int fd)
Packit 631bab
{
Packit 631bab
    drm_control_t ctl;
Packit 631bab
Packit 631bab
    memclear(ctl);
Packit 631bab
    ctl.func  = DRM_UNINST_HANDLER;
Packit 631bab
    ctl.irq   = 0;
Packit 631bab
    if (drmIoctl(fd, DRM_IOCTL_CONTROL, &ctl))
Packit 631bab
        return -errno;
Packit 631bab
    return 0;
Packit 631bab
}
Packit 631bab
Packit 631bab
drm_public int drmFinish(int fd, int context, drmLockFlags flags)
Packit 631bab
{
Packit 631bab
    drm_lock_t lock;
Packit 631bab
Packit 631bab
    memclear(lock);
Packit 631bab
    lock.context = context;
Packit 631bab
    if (flags & DRM_LOCK_READY)      lock.flags |= _DRM_LOCK_READY;
Packit 631bab
    if (flags & DRM_LOCK_QUIESCENT)  lock.flags |= _DRM_LOCK_QUIESCENT;
Packit 631bab
    if (flags & DRM_LOCK_FLUSH)      lock.flags |= _DRM_LOCK_FLUSH;
Packit 631bab
    if (flags & DRM_LOCK_FLUSH_ALL)  lock.flags |= _DRM_LOCK_FLUSH_ALL;
Packit 631bab
    if (flags & DRM_HALT_ALL_QUEUES) lock.flags |= _DRM_HALT_ALL_QUEUES;
Packit 631bab
    if (flags & DRM_HALT_CUR_QUEUES) lock.flags |= _DRM_HALT_CUR_QUEUES;
Packit 631bab
    if (drmIoctl(fd, DRM_IOCTL_FINISH, &lock))
Packit 631bab
        return -errno;
Packit 631bab
    return 0;
Packit 631bab
}
Packit 631bab
Packit 631bab
/**
Packit 631bab
 * Get IRQ from bus ID.
Packit 631bab
 *
Packit 631bab
 * \param fd file descriptor.
Packit 631bab
 * \param busnum bus number.
Packit 631bab
 * \param devnum device number.
Packit 631bab
 * \param funcnum function number.
Packit 631bab
 *
Packit 631bab
 * \return IRQ number on success, or a negative value on failure.
Packit 631bab
 *
Packit 631bab
 * \internal
Packit 631bab
 * This function is a wrapper around the DRM_IOCTL_IRQ_BUSID ioctl, passing the
Packit 631bab
 * arguments in a drm_irq_busid structure.
Packit 631bab
 */
Packit 631bab
drm_public int drmGetInterruptFromBusID(int fd, int busnum, int devnum,
Packit 631bab
                                        int funcnum)
Packit 631bab
{
Packit 631bab
    drm_irq_busid_t p;
Packit 631bab
Packit 631bab
    memclear(p);
Packit 631bab
    p.busnum  = busnum;
Packit 631bab
    p.devnum  = devnum;
Packit 631bab
    p.funcnum = funcnum;
Packit 631bab
    if (drmIoctl(fd, DRM_IOCTL_IRQ_BUSID, &p))
Packit 631bab
        return -errno;
Packit 631bab
    return p.irq;
Packit 631bab
}
Packit 631bab
Packit 631bab
drm_public int drmAddContextTag(int fd, drm_context_t context, void *tag)
Packit 631bab
{
Packit 631bab
    drmHashEntry  *entry = drmGetEntry(fd);
Packit 631bab
Packit 631bab
    if (drmHashInsert(entry->tagTable, context, tag)) {
Packit 631bab
        drmHashDelete(entry->tagTable, context);
Packit 631bab
        drmHashInsert(entry->tagTable, context, tag);
Packit 631bab
    }
Packit 631bab
    return 0;
Packit 631bab
}
Packit 631bab
Packit 631bab
drm_public int drmDelContextTag(int fd, drm_context_t context)
Packit 631bab
{
Packit 631bab
    drmHashEntry  *entry = drmGetEntry(fd);
Packit 631bab
Packit 631bab
    return drmHashDelete(entry->tagTable, context);
Packit 631bab
}
Packit 631bab
Packit 631bab
drm_public void *drmGetContextTag(int fd, drm_context_t context)
Packit 631bab
{
Packit 631bab
    drmHashEntry  *entry = drmGetEntry(fd);
Packit 631bab
    void          *value;
Packit 631bab
Packit 631bab
    if (drmHashLookup(entry->tagTable, context, &value))
Packit 631bab
        return NULL;
Packit 631bab
Packit 631bab
    return value;
Packit 631bab
}
Packit 631bab
Packit 631bab
drm_public int drmAddContextPrivateMapping(int fd, drm_context_t ctx_id,
Packit 631bab
                                           drm_handle_t handle)
Packit 631bab
{
Packit 631bab
    drm_ctx_priv_map_t map;
Packit 631bab
Packit 631bab
    memclear(map);
Packit 631bab
    map.ctx_id = ctx_id;
Packit 631bab
    map.handle = (void *)(uintptr_t)handle;
Packit 631bab
Packit 631bab
    if (drmIoctl(fd, DRM_IOCTL_SET_SAREA_CTX, &map))
Packit 631bab
        return -errno;
Packit 631bab
    return 0;
Packit 631bab
}
Packit 631bab
Packit 631bab
drm_public int drmGetContextPrivateMapping(int fd, drm_context_t ctx_id,
Packit 631bab
                                           drm_handle_t *handle)
Packit 631bab
{
Packit 631bab
    drm_ctx_priv_map_t map;
Packit 631bab
Packit 631bab
    memclear(map);
Packit 631bab
    map.ctx_id = ctx_id;
Packit 631bab
Packit 631bab
    if (drmIoctl(fd, DRM_IOCTL_GET_SAREA_CTX, &map))
Packit 631bab
        return -errno;
Packit 631bab
    if (handle)
Packit 631bab
        *handle = (drm_handle_t)(uintptr_t)map.handle;
Packit 631bab
Packit 631bab
    return 0;
Packit 631bab
}
Packit 631bab
Packit 631bab
drm_public int drmGetMap(int fd, int idx, drm_handle_t *offset, drmSize *size,
Packit 631bab
                         drmMapType *type, drmMapFlags *flags,
Packit 631bab
                         drm_handle_t *handle, int *mtrr)
Packit 631bab
{
Packit 631bab
    drm_map_t map;
Packit 631bab
Packit 631bab
    memclear(map);
Packit 631bab
    map.offset = idx;
Packit 631bab
    if (drmIoctl(fd, DRM_IOCTL_GET_MAP, &map))
Packit 631bab
        return -errno;
Packit 631bab
    *offset = map.offset;
Packit 631bab
    *size   = map.size;
Packit 631bab
    *type   = map.type;
Packit 631bab
    *flags  = map.flags;
Packit 631bab
    *handle = (unsigned long)map.handle;
Packit 631bab
    *mtrr   = map.mtrr;
Packit 631bab
    return 0;
Packit 631bab
}
Packit 631bab
Packit 631bab
drm_public int drmGetClient(int fd, int idx, int *auth, int *pid, int *uid,
Packit 631bab
                            unsigned long *magic, unsigned long *iocs)
Packit 631bab
{
Packit 631bab
    drm_client_t client;
Packit 631bab
Packit 631bab
    memclear(client);
Packit 631bab
    client.idx = idx;
Packit 631bab
    if (drmIoctl(fd, DRM_IOCTL_GET_CLIENT, &client))
Packit 631bab
        return -errno;
Packit 631bab
    *auth      = client.auth;
Packit 631bab
    *pid       = client.pid;
Packit 631bab
    *uid       = client.uid;
Packit 631bab
    *magic     = client.magic;
Packit 631bab
    *iocs      = client.iocs;
Packit 631bab
    return 0;
Packit 631bab
}
Packit 631bab
Packit 631bab
drm_public int drmGetStats(int fd, drmStatsT *stats)
Packit 631bab
{
Packit 631bab
    drm_stats_t s;
Packit 631bab
    unsigned    i;
Packit 631bab
Packit 631bab
    memclear(s);
Packit 631bab
    if (drmIoctl(fd, DRM_IOCTL_GET_STATS, &s))
Packit 631bab
        return -errno;
Packit 631bab
Packit 631bab
    stats->count = 0;
Packit 631bab
    memset(stats, 0, sizeof(*stats));
Packit 631bab
    if (s.count > sizeof(stats->data)/sizeof(stats->data[0]))
Packit 631bab
        return -1;
Packit 631bab
Packit 631bab
#define SET_VALUE                              \
Packit 631bab
    stats->data[i].long_format = "%-20.20s";   \
Packit 631bab
    stats->data[i].rate_format = "%8.8s";      \
Packit 631bab
    stats->data[i].isvalue     = 1;            \
Packit 631bab
    stats->data[i].verbose     = 0
Packit 631bab
Packit 631bab
#define SET_COUNT                              \
Packit 631bab
    stats->data[i].long_format = "%-20.20s";   \
Packit 631bab
    stats->data[i].rate_format = "%5.5s";      \
Packit 631bab
    stats->data[i].isvalue     = 0;            \
Packit 631bab
    stats->data[i].mult_names  = "kgm";        \
Packit 631bab
    stats->data[i].mult        = 1000;         \
Packit 631bab
    stats->data[i].verbose     = 0
Packit 631bab
Packit 631bab
#define SET_BYTE                               \
Packit 631bab
    stats->data[i].long_format = "%-20.20s";   \
Packit 631bab
    stats->data[i].rate_format = "%5.5s";      \
Packit 631bab
    stats->data[i].isvalue     = 0;            \
Packit 631bab
    stats->data[i].mult_names  = "KGM";        \
Packit 631bab
    stats->data[i].mult        = 1024;         \
Packit 631bab
    stats->data[i].verbose     = 0
Packit 631bab
Packit 631bab
Packit 631bab
    stats->count = s.count;
Packit 631bab
    for (i = 0; i < s.count; i++) {
Packit 631bab
        stats->data[i].value = s.data[i].value;
Packit 631bab
        switch (s.data[i].type) {
Packit 631bab
        case _DRM_STAT_LOCK:
Packit 631bab
            stats->data[i].long_name = "Lock";
Packit 631bab
            stats->data[i].rate_name = "Lock";
Packit 631bab
            SET_VALUE;
Packit 631bab
            break;
Packit 631bab
        case _DRM_STAT_OPENS:
Packit 631bab
            stats->data[i].long_name = "Opens";
Packit 631bab
            stats->data[i].rate_name = "O";
Packit 631bab
            SET_COUNT;
Packit 631bab
            stats->data[i].verbose   = 1;
Packit 631bab
            break;
Packit 631bab
        case _DRM_STAT_CLOSES:
Packit 631bab
            stats->data[i].long_name = "Closes";
Packit 631bab
            stats->data[i].rate_name = "Lock";
Packit 631bab
            SET_COUNT;
Packit 631bab
            stats->data[i].verbose   = 1;
Packit 631bab
            break;
Packit 631bab
        case _DRM_STAT_IOCTLS:
Packit 631bab
            stats->data[i].long_name = "Ioctls";
Packit 631bab
            stats->data[i].rate_name = "Ioc/s";
Packit 631bab
            SET_COUNT;
Packit 631bab
            break;
Packit 631bab
        case _DRM_STAT_LOCKS:
Packit 631bab
            stats->data[i].long_name = "Locks";
Packit 631bab
            stats->data[i].rate_name = "Lck/s";
Packit 631bab
            SET_COUNT;
Packit 631bab
            break;
Packit 631bab
        case _DRM_STAT_UNLOCKS:
Packit 631bab
            stats->data[i].long_name = "Unlocks";
Packit 631bab
            stats->data[i].rate_name = "Unl/s";
Packit 631bab
            SET_COUNT;
Packit 631bab
            break;
Packit 631bab
        case _DRM_STAT_IRQ:
Packit 631bab
            stats->data[i].long_name = "IRQs";
Packit 631bab
            stats->data[i].rate_name = "IRQ/s";
Packit 631bab
            SET_COUNT;
Packit 631bab
            break;
Packit 631bab
        case _DRM_STAT_PRIMARY:
Packit 631bab
            stats->data[i].long_name = "Primary Bytes";
Packit 631bab
            stats->data[i].rate_name = "PB/s";
Packit 631bab
            SET_BYTE;
Packit 631bab
            break;
Packit 631bab
        case _DRM_STAT_SECONDARY:
Packit 631bab
            stats->data[i].long_name = "Secondary Bytes";
Packit 631bab
            stats->data[i].rate_name = "SB/s";
Packit 631bab
            SET_BYTE;
Packit 631bab
            break;
Packit 631bab
        case _DRM_STAT_DMA:
Packit 631bab
            stats->data[i].long_name = "DMA";
Packit 631bab
            stats->data[i].rate_name = "DMA/s";
Packit 631bab
            SET_COUNT;
Packit 631bab
            break;
Packit 631bab
        case _DRM_STAT_SPECIAL:
Packit 631bab
            stats->data[i].long_name = "Special DMA";
Packit 631bab
            stats->data[i].rate_name = "dma/s";
Packit 631bab
            SET_COUNT;
Packit 631bab
            break;
Packit 631bab
        case _DRM_STAT_MISSED:
Packit 631bab
            stats->data[i].long_name = "Miss";
Packit 631bab
            stats->data[i].rate_name = "Ms/s";
Packit 631bab
            SET_COUNT;
Packit 631bab
            break;
Packit 631bab
        case _DRM_STAT_VALUE:
Packit 631bab
            stats->data[i].long_name = "Value";
Packit 631bab
            stats->data[i].rate_name = "Value";
Packit 631bab
            SET_VALUE;
Packit 631bab
            break;
Packit 631bab
        case _DRM_STAT_BYTE:
Packit 631bab
            stats->data[i].long_name = "Bytes";
Packit 631bab
            stats->data[i].rate_name = "B/s";
Packit 631bab
            SET_BYTE;
Packit 631bab
            break;
Packit 631bab
        case _DRM_STAT_COUNT:
Packit 631bab
        default:
Packit 631bab
            stats->data[i].long_name = "Count";
Packit 631bab
            stats->data[i].rate_name = "Cnt/s";
Packit 631bab
            SET_COUNT;
Packit 631bab
            break;
Packit 631bab
        }
Packit 631bab
    }
Packit 631bab
    return 0;
Packit 631bab
}
Packit 631bab
Packit 631bab
/**
Packit 631bab
 * Issue a set-version ioctl.
Packit 631bab
 *
Packit 631bab
 * \param fd file descriptor.
Packit 631bab
 * \param drmCommandIndex command index
Packit 631bab
 * \param data source pointer of the data to be read and written.
Packit 631bab
 * \param size size of the data to be read and written.
Packit 631bab
 *
Packit 631bab
 * \return zero on success, or a negative value on failure.
Packit 631bab
 *
Packit 631bab
 * \internal
Packit 631bab
 * It issues a read-write ioctl given by
Packit 631bab
 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
Packit 631bab
 */
Packit 631bab
drm_public int drmSetInterfaceVersion(int fd, drmSetVersion *version)
Packit 631bab
{
Packit 631bab
    int retcode = 0;
Packit 631bab
    drm_set_version_t sv;
Packit 631bab
Packit 631bab
    memclear(sv);
Packit 631bab
    sv.drm_di_major = version->drm_di_major;
Packit 631bab
    sv.drm_di_minor = version->drm_di_minor;
Packit 631bab
    sv.drm_dd_major = version->drm_dd_major;
Packit 631bab
    sv.drm_dd_minor = version->drm_dd_minor;
Packit 631bab
Packit 631bab
    if (drmIoctl(fd, DRM_IOCTL_SET_VERSION, &sv)) {
Packit 631bab
        retcode = -errno;
Packit 631bab
    }
Packit 631bab
Packit 631bab
    version->drm_di_major = sv.drm_di_major;
Packit 631bab
    version->drm_di_minor = sv.drm_di_minor;
Packit 631bab
    version->drm_dd_major = sv.drm_dd_major;
Packit 631bab
    version->drm_dd_minor = sv.drm_dd_minor;
Packit 631bab
Packit 631bab
    return retcode;
Packit 631bab
}
Packit 631bab
Packit 631bab
/**
Packit 631bab
 * Send a device-specific command.
Packit 631bab
 *
Packit 631bab
 * \param fd file descriptor.
Packit 631bab
 * \param drmCommandIndex command index
Packit 631bab
 *
Packit 631bab
 * \return zero on success, or a negative value on failure.
Packit 631bab
 *
Packit 631bab
 * \internal
Packit 631bab
 * It issues a ioctl given by
Packit 631bab
 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
Packit 631bab
 */
Packit 631bab
drm_public int drmCommandNone(int fd, unsigned long drmCommandIndex)
Packit 631bab
{
Packit 631bab
    unsigned long request;
Packit 631bab
Packit 631bab
    request = DRM_IO( DRM_COMMAND_BASE + drmCommandIndex);
Packit 631bab
Packit 631bab
    if (drmIoctl(fd, request, NULL)) {
Packit 631bab
        return -errno;
Packit 631bab
    }
Packit 631bab
    return 0;
Packit 631bab
}
Packit 631bab
Packit 631bab
Packit 631bab
/**
Packit 631bab
 * Send a device-specific read command.
Packit 631bab
 *
Packit 631bab
 * \param fd file descriptor.
Packit 631bab
 * \param drmCommandIndex command index
Packit 631bab
 * \param data destination pointer of the data to be read.
Packit 631bab
 * \param size size of the data to be read.
Packit 631bab
 *
Packit 631bab
 * \return zero on success, or a negative value on failure.
Packit 631bab
 *
Packit 631bab
 * \internal
Packit 631bab
 * It issues a read ioctl given by
Packit 631bab
 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
Packit 631bab
 */
Packit 631bab
drm_public int drmCommandRead(int fd, unsigned long drmCommandIndex,
Packit 631bab
                              void *data, unsigned long size)
Packit 631bab
{
Packit 631bab
    unsigned long request;
Packit 631bab
Packit 631bab
    request = DRM_IOC( DRM_IOC_READ, DRM_IOCTL_BASE,
Packit 631bab
        DRM_COMMAND_BASE + drmCommandIndex, size);
Packit 631bab
Packit 631bab
    if (drmIoctl(fd, request, data)) {
Packit 631bab
        return -errno;
Packit 631bab
    }
Packit 631bab
    return 0;
Packit 631bab
}
Packit 631bab
Packit 631bab
Packit 631bab
/**
Packit 631bab
 * Send a device-specific write command.
Packit 631bab
 *
Packit 631bab
 * \param fd file descriptor.
Packit 631bab
 * \param drmCommandIndex command index
Packit 631bab
 * \param data source pointer of the data to be written.
Packit 631bab
 * \param size size of the data to be written.
Packit 631bab
 *
Packit 631bab
 * \return zero on success, or a negative value on failure.
Packit 631bab
 *
Packit 631bab
 * \internal
Packit 631bab
 * It issues a write ioctl given by
Packit 631bab
 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
Packit 631bab
 */
Packit 631bab
drm_public int drmCommandWrite(int fd, unsigned long drmCommandIndex,
Packit 631bab
                               void *data, unsigned long size)
Packit 631bab
{
Packit 631bab
    unsigned long request;
Packit 631bab
Packit 631bab
    request = DRM_IOC( DRM_IOC_WRITE, DRM_IOCTL_BASE,
Packit 631bab
        DRM_COMMAND_BASE + drmCommandIndex, size);
Packit 631bab
Packit 631bab
    if (drmIoctl(fd, request, data)) {
Packit 631bab
        return -errno;
Packit 631bab
    }
Packit 631bab
    return 0;
Packit 631bab
}
Packit 631bab
Packit 631bab
Packit 631bab
/**
Packit 631bab
 * Send a device-specific read-write command.
Packit 631bab
 *
Packit 631bab
 * \param fd file descriptor.
Packit 631bab
 * \param drmCommandIndex command index
Packit 631bab
 * \param data source pointer of the data to be read and written.
Packit 631bab
 * \param size size of the data to be read and written.
Packit 631bab
 *
Packit 631bab
 * \return zero on success, or a negative value on failure.
Packit 631bab
 *
Packit 631bab
 * \internal
Packit 631bab
 * It issues a read-write ioctl given by
Packit 631bab
 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
Packit 631bab
 */
Packit 631bab
drm_public int drmCommandWriteRead(int fd, unsigned long drmCommandIndex,
Packit 631bab
                                   void *data, unsigned long size)
Packit 631bab
{
Packit 631bab
    unsigned long request;
Packit 631bab
Packit 631bab
    request = DRM_IOC( DRM_IOC_READ|DRM_IOC_WRITE, DRM_IOCTL_BASE,
Packit 631bab
        DRM_COMMAND_BASE + drmCommandIndex, size);
Packit 631bab
Packit 631bab
    if (drmIoctl(fd, request, data))
Packit 631bab
        return -errno;
Packit 631bab
    return 0;
Packit 631bab
}
Packit 631bab
Packit 631bab
#define DRM_MAX_FDS 16
Packit 631bab
static struct {
Packit 631bab
    char *BusID;
Packit 631bab
    int fd;
Packit 631bab
    int refcount;
Packit 631bab
    int type;
Packit 631bab
} connection[DRM_MAX_FDS];
Packit 631bab
Packit 631bab
static int nr_fds = 0;
Packit 631bab
Packit 631bab
drm_public int drmOpenOnce(void *unused, const char *BusID, int *newlyopened)
Packit 631bab
{
Packit 631bab
    return drmOpenOnceWithType(BusID, newlyopened, DRM_NODE_PRIMARY);
Packit 631bab
}
Packit 631bab
Packit 631bab
drm_public int drmOpenOnceWithType(const char *BusID, int *newlyopened,
Packit 631bab
                                   int type)
Packit 631bab
{
Packit 631bab
    int i;
Packit 631bab
    int fd;
Packit 631bab
Packit 631bab
    for (i = 0; i < nr_fds; i++)
Packit 631bab
        if ((strcmp(BusID, connection[i].BusID) == 0) &&
Packit 631bab
            (connection[i].type == type)) {
Packit 631bab
            connection[i].refcount++;
Packit 631bab
            *newlyopened = 0;
Packit 631bab
            return connection[i].fd;
Packit 631bab
        }
Packit 631bab
Packit 631bab
    fd = drmOpenWithType(NULL, BusID, type);
Packit 631bab
    if (fd < 0 || nr_fds == DRM_MAX_FDS)
Packit 631bab
        return fd;
Packit 631bab
Packit 631bab
    connection[nr_fds].BusID = strdup(BusID);
Packit 631bab
    connection[nr_fds].fd = fd;
Packit 631bab
    connection[nr_fds].refcount = 1;
Packit 631bab
    connection[nr_fds].type = type;
Packit 631bab
    *newlyopened = 1;
Packit 631bab
Packit 631bab
    if (0)
Packit 631bab
        fprintf(stderr, "saved connection %d for %s %d\n",
Packit 631bab
                nr_fds, connection[nr_fds].BusID,
Packit 631bab
                strcmp(BusID, connection[nr_fds].BusID));
Packit 631bab
Packit 631bab
    nr_fds++;
Packit 631bab
Packit 631bab
    return fd;
Packit 631bab
}
Packit 631bab
Packit 631bab
drm_public void drmCloseOnce(int fd)
Packit 631bab
{
Packit 631bab
    int i;
Packit 631bab
Packit 631bab
    for (i = 0; i < nr_fds; i++) {
Packit 631bab
        if (fd == connection[i].fd) {
Packit 631bab
            if (--connection[i].refcount == 0) {
Packit 631bab
                drmClose(connection[i].fd);
Packit 631bab
                free(connection[i].BusID);
Packit 631bab
Packit 631bab
                if (i < --nr_fds)
Packit 631bab
                    connection[i] = connection[nr_fds];
Packit 631bab
Packit 631bab
                return;
Packit 631bab
            }
Packit 631bab
        }
Packit 631bab
    }
Packit 631bab
}
Packit 631bab
Packit 631bab
drm_public int drmSetMaster(int fd)
Packit 631bab
{
Packit 631bab
        return drmIoctl(fd, DRM_IOCTL_SET_MASTER, NULL);
Packit 631bab
}
Packit 631bab
Packit 631bab
drm_public int drmDropMaster(int fd)
Packit 631bab
{
Packit 631bab
        return drmIoctl(fd, DRM_IOCTL_DROP_MASTER, NULL);
Packit 631bab
}
Packit 631bab
Packit 631bab
drm_public int drmIsMaster(int fd)
Packit 631bab
{
Packit 631bab
        /* Detect master by attempting something that requires master.
Packit 631bab
         *
Packit 631bab
         * Authenticating magic tokens requires master and 0 is an
Packit 631bab
         * internal kernel detail which we could use. Attempting this on
Packit 631bab
         * a master fd would fail therefore fail with EINVAL because 0
Packit 631bab
         * is invalid.
Packit 631bab
         *
Packit 631bab
         * A non-master fd will fail with EACCES, as the kernel checks
Packit 631bab
         * for master before attempting to do anything else.
Packit 631bab
         *
Packit 631bab
         * Since we don't want to leak implementation details, use
Packit 631bab
         * EACCES.
Packit 631bab
         */
Packit 631bab
        return drmAuthMagic(fd, 0) != -EACCES;
Packit 631bab
}
Packit 631bab
Packit 631bab
drm_public char *drmGetDeviceNameFromFd(int fd)
Packit 631bab
{
Packit 631bab
    char name[128];
Packit 631bab
    struct stat sbuf;
Packit 631bab
    dev_t d;
Packit 631bab
    int i;
Packit 631bab
Packit 631bab
    /* The whole drmOpen thing is a fiasco and we need to find a way
Packit 631bab
     * back to just using open(2).  For now, however, lets just make
Packit 631bab
     * things worse with even more ad hoc directory walking code to
Packit 631bab
     * discover the device file name. */
Packit 631bab
Packit 631bab
    fstat(fd, &sbuf);
Packit 631bab
    d = sbuf.st_rdev;
Packit 631bab
Packit 631bab
    for (i = 0; i < DRM_MAX_MINOR; i++) {
Packit 631bab
        snprintf(name, sizeof name, DRM_DEV_NAME, DRM_DIR_NAME, i);
Packit 631bab
        if (stat(name, &sbuf) == 0 && sbuf.st_rdev == d)
Packit 631bab
            break;
Packit 631bab
    }
Packit 631bab
    if (i == DRM_MAX_MINOR)
Packit 631bab
        return NULL;
Packit 631bab
Packit 631bab
    return strdup(name);
Packit 631bab
}
Packit 631bab
Packit 631bab
static bool drmNodeIsDRM(int maj, int min)
Packit 631bab
{
Packit 631bab
#ifdef __linux__
Packit 631bab
    char path[64];
Packit 631bab
    struct stat sbuf;
Packit 631bab
Packit 631bab
    snprintf(path, sizeof(path), "/sys/dev/char/%d:%d/device/drm",
Packit 631bab
             maj, min);
Packit 631bab
    return stat(path, &sbuf) == 0;
Packit 631bab
#else
Packit 631bab
    return maj == DRM_MAJOR;
Packit 631bab
#endif
Packit 631bab
}
Packit 631bab
Packit 631bab
drm_public int drmGetNodeTypeFromFd(int fd)
Packit 631bab
{
Packit 631bab
    struct stat sbuf;
Packit 631bab
    int maj, min, type;
Packit 631bab
Packit 631bab
    if (fstat(fd, &sbuf))
Packit 631bab
        return -1;
Packit 631bab
Packit 631bab
    maj = major(sbuf.st_rdev);
Packit 631bab
    min = minor(sbuf.st_rdev);
Packit 631bab
Packit 631bab
    if (!drmNodeIsDRM(maj, min) || !S_ISCHR(sbuf.st_mode)) {
Packit 631bab
        errno = EINVAL;
Packit 631bab
        return -1;
Packit 631bab
    }
Packit 631bab
Packit 631bab
    type = drmGetMinorType(min);
Packit 631bab
    if (type == -1)
Packit 631bab
        errno = ENODEV;
Packit 631bab
    return type;
Packit 631bab
}
Packit 631bab
Packit 631bab
drm_public int drmPrimeHandleToFD(int fd, uint32_t handle, uint32_t flags,
Packit 631bab
                                  int *prime_fd)
Packit 631bab
{
Packit 631bab
    struct drm_prime_handle args;
Packit 631bab
    int ret;
Packit 631bab
Packit 631bab
    memclear(args);
Packit 631bab
    args.fd = -1;
Packit 631bab
    args.handle = handle;
Packit 631bab
    args.flags = flags;
Packit 631bab
    ret = drmIoctl(fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &args);
Packit 631bab
    if (ret)
Packit 631bab
        return ret;
Packit 631bab
Packit 631bab
    *prime_fd = args.fd;
Packit 631bab
    return 0;
Packit 631bab
}
Packit 631bab
Packit 631bab
drm_public int drmPrimeFDToHandle(int fd, int prime_fd, uint32_t *handle)
Packit 631bab
{
Packit 631bab
    struct drm_prime_handle args;
Packit 631bab
    int ret;
Packit 631bab
Packit 631bab
    memclear(args);
Packit 631bab
    args.fd = prime_fd;
Packit 631bab
    ret = drmIoctl(fd, DRM_IOCTL_PRIME_FD_TO_HANDLE, &args);
Packit 631bab
    if (ret)
Packit 631bab
        return ret;
Packit 631bab
Packit 631bab
    *handle = args.handle;
Packit 631bab
    return 0;
Packit 631bab
}
Packit 631bab
Packit 631bab
static char *drmGetMinorNameForFD(int fd, int type)
Packit 631bab
{
Packit 631bab
#ifdef __linux__
Packit 631bab
    DIR *sysdir;
Packit 631bab
    struct dirent *ent;
Packit 631bab
    struct stat sbuf;
Packit 631bab
    const char *name = drmGetMinorName(type);
Packit 631bab
    int len;
Packit 631bab
    char dev_name[64], buf[64];
Packit 631bab
    int maj, min;
Packit 631bab
Packit 631bab
    if (!name)
Packit 631bab
        return NULL;
Packit 631bab
Packit 631bab
    len = strlen(name);
Packit 631bab
Packit 631bab
    if (fstat(fd, &sbuf))
Packit 631bab
        return NULL;
Packit 631bab
Packit 631bab
    maj = major(sbuf.st_rdev);
Packit 631bab
    min = minor(sbuf.st_rdev);
Packit 631bab
Packit 631bab
    if (!drmNodeIsDRM(maj, min) || !S_ISCHR(sbuf.st_mode))
Packit 631bab
        return NULL;
Packit 631bab
Packit 631bab
    snprintf(buf, sizeof(buf), "/sys/dev/char/%d:%d/device/drm", maj, min);
Packit 631bab
Packit 631bab
    sysdir = opendir(buf);
Packit 631bab
    if (!sysdir)
Packit 631bab
        return NULL;
Packit 631bab
Packit 631bab
    while ((ent = readdir(sysdir))) {
Packit 631bab
        if (strncmp(ent->d_name, name, len) == 0) {
Packit 631bab
            snprintf(dev_name, sizeof(dev_name), DRM_DIR_NAME "/%s",
Packit 631bab
                 ent->d_name);
Packit 631bab
Packit 631bab
            closedir(sysdir);
Packit 631bab
            return strdup(dev_name);
Packit 631bab
        }
Packit 631bab
    }
Packit 631bab
Packit 631bab
    closedir(sysdir);
Packit 631bab
    return NULL;
Packit 631bab
#else
Packit 631bab
    struct stat sbuf;
Packit 631bab
    char buf[PATH_MAX + 1];
Packit 631bab
    const char *dev_name = drmGetDeviceName(type);
Packit 631bab
    unsigned int maj, min;
Packit 631bab
    int n;
Packit 631bab
Packit 631bab
    if (fstat(fd, &sbuf))
Packit 631bab
        return NULL;
Packit 631bab
Packit 631bab
    maj = major(sbuf.st_rdev);
Packit 631bab
    min = minor(sbuf.st_rdev);
Packit 631bab
Packit 631bab
    if (!drmNodeIsDRM(maj, min) || !S_ISCHR(sbuf.st_mode))
Packit 631bab
        return NULL;
Packit 631bab
Packit 631bab
    if (!dev_name)
Packit 631bab
        return NULL;
Packit 631bab
Packit 631bab
    n = snprintf(buf, sizeof(buf), dev_name, DRM_DIR_NAME, min);
Packit 631bab
    if (n == -1 || n >= sizeof(buf))
Packit 631bab
        return NULL;
Packit 631bab
Packit 631bab
    return strdup(buf);
Packit 631bab
#endif
Packit 631bab
}
Packit 631bab
Packit 631bab
drm_public char *drmGetPrimaryDeviceNameFromFd(int fd)
Packit 631bab
{
Packit 631bab
    return drmGetMinorNameForFD(fd, DRM_NODE_PRIMARY);
Packit 631bab
}
Packit 631bab
Packit 631bab
drm_public char *drmGetRenderDeviceNameFromFd(int fd)
Packit 631bab
{
Packit 631bab
    return drmGetMinorNameForFD(fd, DRM_NODE_RENDER);
Packit 631bab
}
Packit 631bab
Packit 631bab
#ifdef __linux__
Packit 631bab
static char * DRM_PRINTFLIKE(2, 3)
Packit 631bab
sysfs_uevent_get(const char *path, const char *fmt, ...)
Packit 631bab
{
Packit 631bab
    char filename[PATH_MAX + 1], *key, *line = NULL, *value = NULL;
Packit 631bab
    size_t size = 0, len;
Packit 631bab
    ssize_t num;
Packit 631bab
    va_list ap;
Packit 631bab
    FILE *fp;
Packit 631bab
Packit 631bab
    va_start(ap, fmt);
Packit 631bab
    num = vasprintf(&key, fmt, ap);
Packit 631bab
    va_end(ap);
Packit 631bab
    len = num;
Packit 631bab
Packit 631bab
    snprintf(filename, sizeof(filename), "%s/uevent", path);
Packit 631bab
Packit 631bab
    fp = fopen(filename, "r");
Packit 631bab
    if (!fp) {
Packit 631bab
        free(key);
Packit 631bab
        return NULL;
Packit 631bab
    }
Packit 631bab
Packit 631bab
    while ((num = getline(&line, &size, fp)) >= 0) {
Packit 631bab
        if ((strncmp(line, key, len) == 0) && (line[len] == '=')) {
Packit 631bab
            char *start = line + len + 1, *end = line + num - 1;
Packit 631bab
Packit 631bab
            if (*end != '\n')
Packit 631bab
                end++;
Packit 631bab
Packit 631bab
            value = strndup(start, end - start);
Packit 631bab
            break;
Packit 631bab
        }
Packit 631bab
    }
Packit 631bab
Packit 631bab
    free(line);
Packit 631bab
    fclose(fp);
Packit 631bab
Packit 631bab
    free(key);
Packit 631bab
Packit 631bab
    return value;
Packit 631bab
}
Packit 631bab
#endif
Packit 631bab
Packit 631bab
/* Little white lie to avoid major rework of the existing code */
Packit 631bab
#define DRM_BUS_VIRTIO 0x10
Packit 631bab
Packit 631bab
#ifdef __linux__
Packit Service 856e4e
static int get_subsystem_type(const char *device_path)
Packit Service 856e4e
{
Packit Service 856e4e
    char path[PATH_MAX + 1] = "";
Packit 631bab
    char link[PATH_MAX + 1] = "";
Packit 631bab
    char *name;
Packit 631bab
    struct {
Packit 631bab
        const char *name;
Packit 631bab
        int bus_type;
Packit 631bab
    } bus_types[] = {
Packit 631bab
        { "/pci", DRM_BUS_PCI },
Packit 631bab
        { "/usb", DRM_BUS_USB },
Packit 631bab
        { "/platform", DRM_BUS_PLATFORM },
Packit 631bab
        { "/spi", DRM_BUS_PLATFORM },
Packit 631bab
        { "/host1x", DRM_BUS_HOST1X },
Packit 631bab
        { "/virtio", DRM_BUS_VIRTIO },
Packit 631bab
    };
Packit 631bab
Packit Service 856e4e
    strncpy(path, device_path, PATH_MAX);
Packit Service 856e4e
    strncat(path, "/subsystem", PATH_MAX);
Packit 631bab
Packit 631bab
    if (readlink(path, link, PATH_MAX) < 0)
Packit 631bab
        return -errno;
Packit 631bab
Packit 631bab
    name = strrchr(link, '/');
Packit 631bab
    if (!name)
Packit 631bab
        return -EINVAL;
Packit 631bab
Packit 631bab
    for (unsigned i = 0; i < ARRAY_SIZE(bus_types); i++) {
Packit 631bab
        if (strncmp(name, bus_types[i].name, strlen(bus_types[i].name)) == 0)
Packit 631bab
            return bus_types[i].bus_type;
Packit 631bab
    }
Packit 631bab
Packit 631bab
    return -EINVAL;
Packit Service 856e4e
}
Packit Service 856e4e
#endif
Packit Service 856e4e
Packit Service 856e4e
static int drmParseSubsystemType(int maj, int min)
Packit Service 856e4e
{
Packit Service 856e4e
#ifdef __linux__
Packit Service 856e4e
    char path[PATH_MAX + 1] = "";
Packit Service 856e4e
    char real_path[PATH_MAX + 1] = "";
Packit Service 856e4e
    int subsystem_type;
Packit Service 856e4e
Packit Service 856e4e
    snprintf(path, sizeof(path), "/sys/dev/char/%d:%d/device", maj, min);
Packit Service 856e4e
    if (!realpath(path, real_path))
Packit Service 856e4e
        return -errno;
Packit Service 856e4e
    snprintf(path, sizeof(path), "%s", real_path);
Packit Service 856e4e
Packit Service 856e4e
    subsystem_type = get_subsystem_type(path);
Packit Service 856e4e
    if (subsystem_type == DRM_BUS_VIRTIO) {
Packit Service 856e4e
        strncat(path, "/..", PATH_MAX);
Packit Service 856e4e
        subsystem_type = get_subsystem_type(path);
Packit Service 856e4e
    }
Packit Service 856e4e
    return subsystem_type;
Packit 631bab
#elif defined(__OpenBSD__) || defined(__DragonFly__)
Packit 631bab
    return DRM_BUS_PCI;
Packit 631bab
#else
Packit 631bab
#warning "Missing implementation of drmParseSubsystemType"
Packit 631bab
    return -EINVAL;
Packit 631bab
#endif
Packit 631bab
}
Packit 631bab
Packit 631bab
static void
Packit 631bab
get_pci_path(int maj, int min, char *pci_path)
Packit 631bab
{
Packit 631bab
    char path[PATH_MAX + 1], *term;
Packit 631bab
Packit 631bab
    snprintf(path, sizeof(path), "/sys/dev/char/%d:%d/device", maj, min);
Packit 631bab
    if (!realpath(path, pci_path)) {
Packit 631bab
        strcpy(pci_path, path);
Packit 631bab
        return;
Packit 631bab
    }
Packit 631bab
Packit 631bab
    term = strrchr(pci_path, '/');
Packit 631bab
    if (term && strncmp(term, "/virtio", 7) == 0)
Packit 631bab
        *term = 0;
Packit 631bab
}
Packit 631bab
Packit 631bab
static int drmParsePciBusInfo(int maj, int min, drmPciBusInfoPtr info)
Packit 631bab
{
Packit 631bab
#ifdef __linux__
Packit 631bab
    unsigned int domain, bus, dev, func;
Packit 631bab
    char pci_path[PATH_MAX + 1], *value;
Packit 631bab
    int num;
Packit 631bab
Packit 631bab
    get_pci_path(maj, min, pci_path);
Packit 631bab
Packit 631bab
    value = sysfs_uevent_get(pci_path, "PCI_SLOT_NAME");
Packit 631bab
    if (!value)
Packit 631bab
        return -ENOENT;
Packit 631bab
Packit 631bab
    num = sscanf(value, "%04x:%02x:%02x.%1u", &domain, &bus, &dev, &func);
Packit 631bab
    free(value);
Packit 631bab
Packit 631bab
    if (num != 4)
Packit 631bab
        return -EINVAL;
Packit 631bab
Packit 631bab
    info->domain = domain;
Packit 631bab
    info->bus = bus;
Packit 631bab
    info->dev = dev;
Packit 631bab
    info->func = func;
Packit 631bab
Packit 631bab
    return 0;
Packit 631bab
#elif defined(__OpenBSD__) || defined(__DragonFly__)
Packit 631bab
    struct drm_pciinfo pinfo;
Packit 631bab
    int fd, type;
Packit 631bab
Packit 631bab
    type = drmGetMinorType(min);
Packit 631bab
    if (type == -1)
Packit 631bab
        return -ENODEV;
Packit 631bab
Packit 631bab
    fd = drmOpenMinor(min, 0, type);
Packit 631bab
    if (fd < 0)
Packit 631bab
        return -errno;
Packit 631bab
Packit 631bab
    if (drmIoctl(fd, DRM_IOCTL_GET_PCIINFO, &pinfo)) {
Packit 631bab
        close(fd);
Packit 631bab
        return -errno;
Packit 631bab
    }
Packit 631bab
    close(fd);
Packit 631bab
Packit 631bab
    info->domain = pinfo.domain;
Packit 631bab
    info->bus = pinfo.bus;
Packit 631bab
    info->dev = pinfo.dev;
Packit 631bab
    info->func = pinfo.func;
Packit 631bab
Packit 631bab
    return 0;
Packit 631bab
#else
Packit 631bab
#warning "Missing implementation of drmParsePciBusInfo"
Packit 631bab
    return -EINVAL;
Packit 631bab
#endif
Packit 631bab
}
Packit 631bab
Packit 631bab
drm_public int drmDevicesEqual(drmDevicePtr a, drmDevicePtr b)
Packit 631bab
{
Packit 631bab
    if (a == NULL || b == NULL)
Packit 631bab
        return 0;
Packit 631bab
Packit 631bab
    if (a->bustype != b->bustype)
Packit 631bab
        return 0;
Packit 631bab
Packit 631bab
    switch (a->bustype) {
Packit 631bab
    case DRM_BUS_PCI:
Packit 631bab
        return memcmp(a->businfo.pci, b->businfo.pci, sizeof(drmPciBusInfo)) == 0;
Packit 631bab
Packit 631bab
    case DRM_BUS_USB:
Packit 631bab
        return memcmp(a->businfo.usb, b->businfo.usb, sizeof(drmUsbBusInfo)) == 0;
Packit 631bab
Packit 631bab
    case DRM_BUS_PLATFORM:
Packit 631bab
        return memcmp(a->businfo.platform, b->businfo.platform, sizeof(drmPlatformBusInfo)) == 0;
Packit 631bab
Packit 631bab
    case DRM_BUS_HOST1X:
Packit 631bab
        return memcmp(a->businfo.host1x, b->businfo.host1x, sizeof(drmHost1xBusInfo)) == 0;
Packit 631bab
Packit 631bab
    default:
Packit 631bab
        break;
Packit 631bab
    }
Packit 631bab
Packit 631bab
    return 0;
Packit 631bab
}
Packit 631bab
Packit 631bab
static int drmGetNodeType(const char *name)
Packit 631bab
{
Packit 631bab
    if (strncmp(name, DRM_CONTROL_MINOR_NAME,
Packit 631bab
        sizeof(DRM_CONTROL_MINOR_NAME ) - 1) == 0)
Packit 631bab
        return DRM_NODE_CONTROL;
Packit 631bab
Packit 631bab
    if (strncmp(name, DRM_RENDER_MINOR_NAME,
Packit 631bab
        sizeof(DRM_RENDER_MINOR_NAME) - 1) == 0)
Packit 631bab
        return DRM_NODE_RENDER;
Packit 631bab
Packit 631bab
    if (strncmp(name, DRM_PRIMARY_MINOR_NAME,
Packit 631bab
        sizeof(DRM_PRIMARY_MINOR_NAME) - 1) == 0)
Packit 631bab
        return DRM_NODE_PRIMARY;
Packit 631bab
Packit 631bab
    return -EINVAL;
Packit 631bab
}
Packit 631bab
Packit 631bab
static int drmGetMaxNodeName(void)
Packit 631bab
{
Packit 631bab
    return sizeof(DRM_DIR_NAME) +
Packit 631bab
           MAX3(sizeof(DRM_PRIMARY_MINOR_NAME),
Packit 631bab
                sizeof(DRM_CONTROL_MINOR_NAME),
Packit 631bab
                sizeof(DRM_RENDER_MINOR_NAME)) +
Packit 631bab
           3 /* length of the node number */;
Packit 631bab
}
Packit 631bab
Packit 631bab
#ifdef __linux__
Packit 631bab
static int parse_separate_sysfs_files(int maj, int min,
Packit 631bab
                                      drmPciDeviceInfoPtr device,
Packit 631bab
                                      bool ignore_revision)
Packit 631bab
{
Packit 631bab
    static const char *attrs[] = {
Packit 631bab
      "revision", /* Older kernels are missing the file, so check for it first */
Packit 631bab
      "vendor",
Packit 631bab
      "device",
Packit 631bab
      "subsystem_vendor",
Packit 631bab
      "subsystem_device",
Packit 631bab
    };
Packit 631bab
    char path[PATH_MAX + 1], pci_path[PATH_MAX + 1];
Packit 631bab
    unsigned int data[ARRAY_SIZE(attrs)];
Packit 631bab
    FILE *fp;
Packit 631bab
    int ret;
Packit 631bab
Packit 631bab
    get_pci_path(maj, min, pci_path);
Packit 631bab
Packit 631bab
    for (unsigned i = ignore_revision ? 1 : 0; i < ARRAY_SIZE(attrs); i++) {
Packit 631bab
        snprintf(path, PATH_MAX, "%s/%s", pci_path, attrs[i]);
Packit 631bab
        fp = fopen(path, "r");
Packit 631bab
        if (!fp)
Packit 631bab
            return -errno;
Packit 631bab
Packit 631bab
        ret = fscanf(fp, "%x", &data[i]);
Packit 631bab
        fclose(fp);
Packit 631bab
        if (ret != 1)
Packit 631bab
            return -errno;
Packit 631bab
Packit 631bab
    }
Packit 631bab
Packit 631bab
    device->revision_id = ignore_revision ? 0xff : data[0] & 0xff;
Packit 631bab
    device->vendor_id = data[1] & 0xffff;
Packit 631bab
    device->device_id = data[2] & 0xffff;
Packit 631bab
    device->subvendor_id = data[3] & 0xffff;
Packit 631bab
    device->subdevice_id = data[4] & 0xffff;
Packit 631bab
Packit 631bab
    return 0;
Packit 631bab
}
Packit 631bab
Packit 631bab
static int parse_config_sysfs_file(int maj, int min,
Packit 631bab
                                   drmPciDeviceInfoPtr device)
Packit 631bab
{
Packit 631bab
    char path[PATH_MAX + 1], pci_path[PATH_MAX + 1];
Packit 631bab
    unsigned char config[64];
Packit 631bab
    int fd, ret;
Packit 631bab
Packit 631bab
    get_pci_path(maj, min, pci_path);
Packit 631bab
Packit 631bab
    snprintf(path, PATH_MAX, "%s/config", pci_path);
Packit 631bab
    fd = open(path, O_RDONLY);
Packit 631bab
    if (fd < 0)
Packit 631bab
        return -errno;
Packit 631bab
Packit 631bab
    ret = read(fd, config, sizeof(config));
Packit 631bab
    close(fd);
Packit 631bab
    if (ret < 0)
Packit 631bab
        return -errno;
Packit 631bab
Packit 631bab
    device->vendor_id = config[0] | (config[1] << 8);
Packit 631bab
    device->device_id = config[2] | (config[3] << 8);
Packit 631bab
    device->revision_id = config[8];
Packit 631bab
    device->subvendor_id = config[44] | (config[45] << 8);
Packit 631bab
    device->subdevice_id = config[46] | (config[47] << 8);
Packit 631bab
Packit 631bab
    return 0;
Packit 631bab
}
Packit 631bab
#endif
Packit 631bab
Packit 631bab
static int drmParsePciDeviceInfo(int maj, int min,
Packit 631bab
                                 drmPciDeviceInfoPtr device,
Packit 631bab
                                 uint32_t flags)
Packit 631bab
{
Packit 631bab
#ifdef __linux__
Packit 631bab
    if (!(flags & DRM_DEVICE_GET_PCI_REVISION))
Packit 631bab
        return parse_separate_sysfs_files(maj, min, device, true);
Packit 631bab
Packit 631bab
    if (parse_separate_sysfs_files(maj, min, device, false))
Packit 631bab
        return parse_config_sysfs_file(maj, min, device);
Packit 631bab
Packit 631bab
    return 0;
Packit 631bab
#elif defined(__OpenBSD__) || defined(__DragonFly__)
Packit 631bab
    struct drm_pciinfo pinfo;
Packit 631bab
    int fd, type;
Packit 631bab
Packit 631bab
    type = drmGetMinorType(min);
Packit 631bab
    if (type == -1)
Packit 631bab
        return -ENODEV;
Packit 631bab
Packit 631bab
    fd = drmOpenMinor(min, 0, type);
Packit 631bab
    if (fd < 0)
Packit 631bab
        return -errno;
Packit 631bab
Packit 631bab
    if (drmIoctl(fd, DRM_IOCTL_GET_PCIINFO, &pinfo)) {
Packit 631bab
        close(fd);
Packit 631bab
        return -errno;
Packit 631bab
    }
Packit 631bab
    close(fd);
Packit 631bab
Packit 631bab
    device->vendor_id = pinfo.vendor_id;
Packit 631bab
    device->device_id = pinfo.device_id;
Packit 631bab
    device->revision_id = pinfo.revision_id;
Packit 631bab
    device->subvendor_id = pinfo.subvendor_id;
Packit 631bab
    device->subdevice_id = pinfo.subdevice_id;
Packit 631bab
Packit 631bab
    return 0;
Packit 631bab
#else
Packit 631bab
#warning "Missing implementation of drmParsePciDeviceInfo"
Packit 631bab
    return -EINVAL;
Packit 631bab
#endif
Packit 631bab
}
Packit 631bab
Packit 631bab
static void drmFreePlatformDevice(drmDevicePtr device)
Packit 631bab
{
Packit 631bab
    if (device->deviceinfo.platform) {
Packit 631bab
        if (device->deviceinfo.platform->compatible) {
Packit 631bab
            char **compatible = device->deviceinfo.platform->compatible;
Packit 631bab
Packit 631bab
            while (*compatible) {
Packit 631bab
                free(*compatible);
Packit 631bab
                compatible++;
Packit 631bab
            }
Packit 631bab
Packit 631bab
            free(device->deviceinfo.platform->compatible);
Packit 631bab
        }
Packit 631bab
    }
Packit 631bab
}
Packit 631bab
Packit 631bab
static void drmFreeHost1xDevice(drmDevicePtr device)
Packit 631bab
{
Packit 631bab
    if (device->deviceinfo.host1x) {
Packit 631bab
        if (device->deviceinfo.host1x->compatible) {
Packit 631bab
            char **compatible = device->deviceinfo.host1x->compatible;
Packit 631bab
Packit 631bab
            while (*compatible) {
Packit 631bab
                free(*compatible);
Packit 631bab
                compatible++;
Packit 631bab
            }
Packit 631bab
Packit 631bab
            free(device->deviceinfo.host1x->compatible);
Packit 631bab
        }
Packit 631bab
    }
Packit 631bab
}
Packit 631bab
Packit 631bab
drm_public void drmFreeDevice(drmDevicePtr *device)
Packit 631bab
{
Packit 631bab
    if (device == NULL)
Packit 631bab
        return;
Packit 631bab
Packit 631bab
    if (*device) {
Packit 631bab
        switch ((*device)->bustype) {
Packit 631bab
        case DRM_BUS_PLATFORM:
Packit 631bab
            drmFreePlatformDevice(*device);
Packit 631bab
            break;
Packit 631bab
Packit 631bab
        case DRM_BUS_HOST1X:
Packit 631bab
            drmFreeHost1xDevice(*device);
Packit 631bab
            break;
Packit 631bab
        }
Packit 631bab
    }
Packit 631bab
Packit 631bab
    free(*device);
Packit 631bab
    *device = NULL;
Packit 631bab
}
Packit 631bab
Packit 631bab
drm_public void drmFreeDevices(drmDevicePtr devices[], int count)
Packit 631bab
{
Packit 631bab
    int i;
Packit 631bab
Packit 631bab
    if (devices == NULL)
Packit 631bab
        return;
Packit 631bab
Packit 631bab
    for (i = 0; i < count; i++)
Packit 631bab
        if (devices[i])
Packit 631bab
            drmFreeDevice(&devices[i]);
Packit 631bab
}
Packit 631bab
Packit 631bab
static drmDevicePtr drmDeviceAlloc(unsigned int type, const char *node,
Packit 631bab
                                   size_t bus_size, size_t device_size,
Packit 631bab
                                   char **ptrp)
Packit 631bab
{
Packit 631bab
    size_t max_node_length, extra, size;
Packit 631bab
    drmDevicePtr device;
Packit 631bab
    unsigned int i;
Packit 631bab
    char *ptr;
Packit 631bab
Packit 631bab
    max_node_length = ALIGN(drmGetMaxNodeName(), sizeof(void *));
Packit 631bab
    extra = DRM_NODE_MAX * (sizeof(void *) + max_node_length);
Packit 631bab
Packit 631bab
    size = sizeof(*device) + extra + bus_size + device_size;
Packit 631bab
Packit 631bab
    device = calloc(1, size);
Packit 631bab
    if (!device)
Packit 631bab
        return NULL;
Packit 631bab
Packit 631bab
    device->available_nodes = 1 << type;
Packit 631bab
Packit 631bab
    ptr = (char *)device + sizeof(*device);
Packit 631bab
    device->nodes = (char **)ptr;
Packit 631bab
Packit 631bab
    ptr += DRM_NODE_MAX * sizeof(void *);
Packit 631bab
Packit 631bab
    for (i = 0; i < DRM_NODE_MAX; i++) {
Packit 631bab
        device->nodes[i] = ptr;
Packit 631bab
        ptr += max_node_length;
Packit 631bab
    }
Packit 631bab
Packit 631bab
    memcpy(device->nodes[type], node, max_node_length);
Packit 631bab
Packit 631bab
    *ptrp = ptr;
Packit 631bab
Packit 631bab
    return device;
Packit 631bab
}
Packit 631bab
Packit 631bab
static int drmProcessPciDevice(drmDevicePtr *device,
Packit 631bab
                               const char *node, int node_type,
Packit 631bab
                               int maj, int min, bool fetch_deviceinfo,
Packit 631bab
                               uint32_t flags)
Packit 631bab
{
Packit 631bab
    drmDevicePtr dev;
Packit 631bab
    char *addr;
Packit 631bab
    int ret;
Packit 631bab
Packit 631bab
    dev = drmDeviceAlloc(node_type, node, sizeof(drmPciBusInfo),
Packit 631bab
                         sizeof(drmPciDeviceInfo), &addr);
Packit 631bab
    if (!dev)
Packit 631bab
        return -ENOMEM;
Packit 631bab
Packit 631bab
    dev->bustype = DRM_BUS_PCI;
Packit 631bab
Packit 631bab
    dev->businfo.pci = (drmPciBusInfoPtr)addr;
Packit 631bab
Packit 631bab
    ret = drmParsePciBusInfo(maj, min, dev->businfo.pci);
Packit 631bab
    if (ret)
Packit 631bab
        goto free_device;
Packit 631bab
Packit 631bab
    // Fetch the device info if the user has requested it
Packit 631bab
    if (fetch_deviceinfo) {
Packit 631bab
        addr += sizeof(drmPciBusInfo);
Packit 631bab
        dev->deviceinfo.pci = (drmPciDeviceInfoPtr)addr;
Packit 631bab
Packit 631bab
        ret = drmParsePciDeviceInfo(maj, min, dev->deviceinfo.pci, flags);
Packit 631bab
        if (ret)
Packit 631bab
            goto free_device;
Packit 631bab
    }
Packit 631bab
Packit 631bab
    *device = dev;
Packit 631bab
Packit 631bab
    return 0;
Packit 631bab
Packit 631bab
free_device:
Packit 631bab
    free(dev);
Packit 631bab
    return ret;
Packit 631bab
}
Packit 631bab
Packit 631bab
static int drmParseUsbBusInfo(int maj, int min, drmUsbBusInfoPtr info)
Packit 631bab
{
Packit 631bab
#ifdef __linux__
Packit 631bab
    char path[PATH_MAX + 1], *value;
Packit 631bab
    unsigned int bus, dev;
Packit 631bab
    int ret;
Packit 631bab
Packit 631bab
    snprintf(path, sizeof(path), "/sys/dev/char/%d:%d/device", maj, min);
Packit 631bab
Packit 631bab
    value = sysfs_uevent_get(path, "BUSNUM");
Packit 631bab
    if (!value)
Packit 631bab
        return -ENOENT;
Packit 631bab
Packit 631bab
    ret = sscanf(value, "%03u", &bus;;
Packit 631bab
    free(value);
Packit 631bab
Packit 631bab
    if (ret <= 0)
Packit 631bab
        return -errno;
Packit 631bab
Packit 631bab
    value = sysfs_uevent_get(path, "DEVNUM");
Packit 631bab
    if (!value)
Packit 631bab
        return -ENOENT;
Packit 631bab
Packit 631bab
    ret = sscanf(value, "%03u", &dev;;
Packit 631bab
    free(value);
Packit 631bab
Packit 631bab
    if (ret <= 0)
Packit 631bab
        return -errno;
Packit 631bab
Packit 631bab
    info->bus = bus;
Packit 631bab
    info->dev = dev;
Packit 631bab
Packit 631bab
    return 0;
Packit 631bab
#else
Packit 631bab
#warning "Missing implementation of drmParseUsbBusInfo"
Packit 631bab
    return -EINVAL;
Packit 631bab
#endif