#endif
#include <stdio.h>
#include <stdlib.h>
+#include <stdbool.h>
#include <unistd.h>
#include <string.h>
#include <strings.h>
#include <ctype.h>
+#include <dirent.h>
+#include <stddef.h>
#include <fcntl.h>
#include <errno.h>
+#include <limits.h>
#include <signal.h>
#include <time.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#include <stdarg.h>
+#ifdef HAVE_SYS_MKDEV_H
+# include <sys/mkdev.h> /* defines major(), minor(), and makedev() on Solaris */
+#endif
+#include <math.h>
/* Not all systems have MAP_FAILED defined */
#ifndef MAP_FAILED
#endif
#include "xf86drm.h"
-#include "libdrm.h"
+#include "libdrm_macros.h"
+
+#include "util_math.h"
+
+#ifdef __OpenBSD__
+#define DRM_PRIMARY_MINOR_NAME "drm"
+#define DRM_CONTROL_MINOR_NAME "drmC"
+#define DRM_RENDER_MINOR_NAME "drmR"
+#else
+#define DRM_PRIMARY_MINOR_NAME "card"
+#define DRM_CONTROL_MINOR_NAME "controlD"
+#define DRM_RENDER_MINOR_NAME "renderD"
+#endif
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
#define DRM_MAJOR 145
#define DRM_MAJOR 34
#endif
-# ifdef __OpenBSD__
-# define DRM_MAJOR 81
-# endif
+#ifdef __OpenBSD__
+#ifdef __i386__
+#define DRM_MAJOR 88
+#else
+#define DRM_MAJOR 87
+#endif
+#endif /* __OpenBSD__ */
#ifndef DRM_MAJOR
#define DRM_MAJOR 226 /* Linux */
#endif
-/*
- * This definition needs to be changed on some systems if dev_t is a structure.
- * If there is a header file we can get it from, there would be best.
- */
-#ifndef makedev
-#define makedev(x,y) ((dev_t)(((x) << 8) | (y)))
-#endif
-
#define DRM_MSG_VERBOSITY 3
-#define DRM_NODE_CONTROL 0
-#define DRM_NODE_PRIMARY 1
-#define DRM_NODE_RENDER 2
-
#define memclear(s) memset(&s, 0, sizeof(s))
static drmServerInfoPtr drm_server_info;
return vfprintf(stderr, format, ap);
}
-typedef int DRM_PRINTFLIKE(1, 0) (*debug_msg_func_t)(const char *format,
- va_list ap);
-
-static debug_msg_func_t drm_debug_print = drmDebugPrint;
-
void
drmMsg(const char *format, ...)
{
va_list ap;
const char *env;
- if (((env = getenv("LIBGL_DEBUG")) && strstr(env, "verbose")) || drm_server_info)
+ if (((env = getenv("LIBGL_DEBUG")) && strstr(env, "verbose")) ||
+ (drm_server_info && drm_server_info->debug_print))
{
va_start(ap, format);
if (drm_server_info) {
drm_server_info->debug_print(format,ap);
} else {
- drm_debug_print(format, ap);
+ drmDebugPrint(format, ap);
}
va_end(ap);
}
}
-void
-drmSetDebugMsgFunction(debug_msg_func_t debug_msg_ptr)
-{
- drm_debug_print = debug_msg_ptr;
-}
-
static void *drmHashTable = NULL; /* Context switch callbacks */
void *drmGetHashTable(void)
void *drmMalloc(int size)
{
- void *pt;
- if ((pt = malloc(size)))
- memset(pt, 0, size);
- return pt;
+ return calloc(1, size);
}
void drmFree(void *pt)
{
- if (pt)
- free(pt);
+ free(pt);
}
/**
* If any other failure happened then it will output error mesage using
* drmMsg() call.
*/
+#if !defined(UDEV)
static int chown_check_return(const char *path, uid_t owner, gid_t group)
{
int rv;
path, errno, strerror(errno));
return -1;
}
+#endif
/**
* Open the DRM device, creating it if necessary.
char buf[64];
int fd;
mode_t devmode = DRM_DEV_MODE, serv_mode;
+ gid_t serv_group;
+#if !defined(UDEV)
int isroot = !geteuid();
uid_t user = DRM_DEV_UID;
- gid_t group = DRM_DEV_GID, serv_group;
-
+ gid_t group = DRM_DEV_GID;
+#endif
+
switch (type) {
case DRM_NODE_PRIMARY:
dev_name = DRM_DEV_NAME;
sprintf(buf, dev_name, DRM_DIR_NAME, minor);
drmMsg("drmOpenDevice: node name is %s\n", buf);
- if (drm_server_info) {
+ if (drm_server_info && drm_server_info->get_perms) {
drm_server_info->get_perms(&serv_group, &serv_mode);
devmode = serv_mode ? serv_mode : DRM_DEV_MODE;
devmode &= ~(S_IXUSR|S_IXGRP|S_IXOTH);
- group = (serv_group >= 0) ? serv_group : DRM_DEV_GID;
}
#if !defined(UDEV)
mknod(buf, S_IFCHR | devmode, dev);
}
- if (drm_server_info) {
+ if (drm_server_info && drm_server_info->get_perms) {
+ group = ((int)serv_group >= 0) ? serv_group : DRM_DEV_GID;
chown_check_return(buf, user, group);
chmod(buf, devmode);
}
return DRM_ERR_NOT_ROOT;
remove(buf);
mknod(buf, S_IFCHR | devmode, dev);
- if (drm_server_info) {
+ if (drm_server_info && drm_server_info->get_perms) {
chown_check_return(buf, user, group);
chmod(buf, devmode);
}
return retval;
}
+static int drmGetMinorBase(int type)
+{
+ switch (type) {
+ case DRM_NODE_PRIMARY:
+ return 0;
+ case DRM_NODE_CONTROL:
+ return 64;
+ case DRM_NODE_RENDER:
+ return 128;
+ default:
+ return -1;
+ };
+}
+
+static int drmGetMinorType(int minor)
+{
+ int type = minor >> 6;
+
+ if (minor < 0)
+ return -1;
+
+ switch (type) {
+ case DRM_NODE_PRIMARY:
+ case DRM_NODE_CONTROL:
+ case DRM_NODE_RENDER:
+ return type;
+ default:
+ return -1;
+ }
+}
+
+static const char *drmGetMinorName(int type)
+{
+ switch (type) {
+ case DRM_NODE_PRIMARY:
+ return DRM_PRIMARY_MINOR_NAME;
+ case DRM_NODE_CONTROL:
+ return DRM_CONTROL_MINOR_NAME;
+ case DRM_NODE_RENDER:
+ return DRM_RENDER_MINOR_NAME;
+ default:
+ return NULL;
+ }
+}
/**
* Open the device by bus ID.
*
* \param busid bus ID.
+ * \param type device node type.
*
* \return a file descriptor on success, or a negative value on error.
*
*
* \sa drmOpenMinor() and drmGetBusid().
*/
-static int drmOpenByBusid(const char *busid)
+static int drmOpenByBusid(const char *busid, int type)
{
int i, pci_domain_ok = 1;
int fd;
const char *buf;
drmSetVersion sv;
+ int base = drmGetMinorBase(type);
+
+ if (base < 0)
+ return -1;
drmMsg("drmOpenByBusid: Searching for BusID %s\n", busid);
- for (i = 0; i < DRM_MAX_MINOR; i++) {
- fd = drmOpenMinor(i, 1, DRM_NODE_PRIMARY);
+ for (i = base; i < base + DRM_MAX_MINOR; i++) {
+ fd = drmOpenMinor(i, 1, type);
drmMsg("drmOpenByBusid: drmOpenMinor returns %d\n", fd);
if (fd >= 0) {
/* We need to try for 1.4 first for proper PCI domain support
* Open the device by name.
*
* \param name driver name.
+ * \param type the device node type.
*
* \return a file descriptor on success, or a negative value on error.
*
*
* \sa drmOpenMinor(), drmGetVersion() and drmGetBusid().
*/
-static int drmOpenByName(const char *name)
+static int drmOpenByName(const char *name, int type)
{
int i;
int fd;
drmVersionPtr version;
char * id;
+ int base = drmGetMinorBase(type);
+
+ if (base < 0)
+ return -1;
/*
* Open the first minor number that matches the driver name and isn't
* already in use. If it's in use it will have a busid assigned already.
*/
- for (i = 0; i < DRM_MAX_MINOR; i++) {
- if ((fd = drmOpenMinor(i, 1, DRM_NODE_PRIMARY)) >= 0) {
+ for (i = base; i < base + DRM_MAX_MINOR; i++) {
+ if ((fd = drmOpenMinor(i, 1, type)) >= 0) {
if ((version = drmGetVersion(fd))) {
if (!strcmp(version->name, name)) {
drmFreeVersion(version);
for (devstring = ++pt; *pt && *pt != ' '; ++pt)
;
if (*pt) { /* Found busid */
- return drmOpenByBusid(++pt);
+ return drmOpenByBusid(++pt, type);
} else { /* No busid */
- return drmOpenDevice(strtol(devstring, NULL, 0),i, DRM_NODE_PRIMARY);
+ return drmOpenDevice(strtol(devstring, NULL, 0),i, type);
}
}
}
*/
int drmOpen(const char *name, const char *busid)
{
- if (!drmAvailable() && name != NULL && drm_server_info) {
- /* try to load the kernel */
+ return drmOpenWithType(name, busid, DRM_NODE_PRIMARY);
+}
+
+/**
+ * Open the DRM device with specified type.
+ *
+ * Looks up the specified name and bus ID, and opens the device found. The
+ * entry in /dev/dri is created if necessary and if called by root.
+ *
+ * \param name driver name. Not referenced if bus ID is supplied.
+ * \param busid bus ID. Zero if not known.
+ * \param type the device node type to open, PRIMARY, CONTROL or RENDER
+ *
+ * \return a file descriptor on success, or a negative value on error.
+ *
+ * \internal
+ * It calls drmOpenByBusid() if \p busid is specified or drmOpenByName()
+ * otherwise.
+ */
+int drmOpenWithType(const char *name, const char *busid, int type)
+{
+ if (!drmAvailable() && name != NULL && drm_server_info &&
+ drm_server_info->load_module) {
+ /* try to load the kernel module */
if (!drm_server_info->load_module(name)) {
drmMsg("[drm] failed to load kernel module \"%s\"\n", name);
return -1;
}
if (busid) {
- int fd = drmOpenByBusid(busid);
+ int fd = drmOpenByBusid(busid, type);
if (fd >= 0)
return fd;
}
if (name)
- return drmOpenByName(name);
+ return drmOpenByName(name, type);
return -1;
}
drmVersionPtr retval;
drm_version_t *version = drmMalloc(sizeof(*version));
- memclear(version);
+ memclear(*version);
if (drmIoctl(fd, DRM_IOCTL_VERSION, version)) {
drmFreeKernelVersion(version);
{
drm_agp_mode_t m;
- memclear(mode);
+ memclear(m);
m.mode = mode;
if (drmIoctl(fd, DRM_IOCTL_AGP_ENABLE, &m))
return -errno;
char *BusID;
int fd;
int refcount;
+ int type;
} connection[DRM_MAX_FDS];
static int nr_fds = 0;
const char *BusID,
int *newlyopened)
{
+ return drmOpenOnceWithType(BusID, newlyopened, DRM_NODE_PRIMARY);
+}
+
+int drmOpenOnceWithType(const char *BusID, int *newlyopened, int type)
+{
int i;
int fd;
for (i = 0; i < nr_fds; i++)
- if (strcmp(BusID, connection[i].BusID) == 0) {
+ if ((strcmp(BusID, connection[i].BusID) == 0) &&
+ (connection[i].type == type)) {
connection[i].refcount++;
*newlyopened = 0;
return connection[i].fd;
}
- fd = drmOpen(unused, BusID);
- if (fd <= 0 || nr_fds == DRM_MAX_FDS)
+ fd = drmOpenWithType(NULL, BusID, type);
+ if (fd < 0 || nr_fds == DRM_MAX_FDS)
return fd;
connection[nr_fds].BusID = strdup(BusID);
connection[nr_fds].fd = fd;
connection[nr_fds].refcount = 1;
+ connection[nr_fds].type = type;
*newlyopened = 1;
if (0)
return strdup(name);
}
+int drmGetNodeTypeFromFd(int fd)
+{
+ struct stat sbuf;
+ int maj, min, type;
+
+ if (fstat(fd, &sbuf))
+ return -1;
+
+ maj = major(sbuf.st_rdev);
+ min = minor(sbuf.st_rdev);
+
+ if (maj != DRM_MAJOR || !S_ISCHR(sbuf.st_mode)) {
+ errno = EINVAL;
+ return -1;
+ }
+
+ type = drmGetMinorType(min);
+ if (type == -1)
+ errno = ENODEV;
+ return type;
+}
+
int drmPrimeHandleToFD(int fd, uint32_t handle, uint32_t flags, int *prime_fd)
{
struct drm_prime_handle args;
int ret;
+ memclear(args);
+ args.fd = -1;
args.handle = handle;
args.flags = flags;
ret = drmIoctl(fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &args);
struct drm_prime_handle args;
int ret;
+ memclear(args);
args.fd = prime_fd;
- args.flags = 0;
ret = drmIoctl(fd, DRM_IOCTL_PRIME_FD_TO_HANDLE, &args);
if (ret)
return ret;
return 0;
}
+static char *drmGetMinorNameForFD(int fd, int type)
+{
+#ifdef __linux__
+ DIR *sysdir;
+ struct dirent *pent, *ent;
+ struct stat sbuf;
+ const char *name = drmGetMinorName(type);
+ int len;
+ char dev_name[64], buf[64];
+ long name_max;
+ int maj, min;
+
+ if (!name)
+ return NULL;
+
+ len = strlen(name);
+
+ if (fstat(fd, &sbuf))
+ return NULL;
+
+ maj = major(sbuf.st_rdev);
+ min = minor(sbuf.st_rdev);
+
+ if (maj != DRM_MAJOR || !S_ISCHR(sbuf.st_mode))
+ return NULL;
+
+ snprintf(buf, sizeof(buf), "/sys/dev/char/%d:%d/device/drm", maj, min);
+
+ sysdir = opendir(buf);
+ if (!sysdir)
+ return NULL;
+
+ name_max = fpathconf(dirfd(sysdir), _PC_NAME_MAX);
+ if (name_max == -1)
+ goto out_close_dir;
+
+ pent = malloc(offsetof(struct dirent, d_name) + name_max + 1);
+ if (pent == NULL)
+ goto out_close_dir;
+
+ while (readdir_r(sysdir, pent, &ent) == 0 && ent != NULL) {
+ if (strncmp(ent->d_name, name, len) == 0) {
+ snprintf(dev_name, sizeof(dev_name), DRM_DIR_NAME "/%s",
+ ent->d_name);
+
+ free(pent);
+ closedir(sysdir);
+
+ return strdup(dev_name);
+ }
+ }
+
+ free(pent);
+
+out_close_dir:
+ closedir(sysdir);
+#else
+#warning "Missing implementation of drmGetMinorNameForFD"
+#endif
+ return NULL;
+}
+
+char *drmGetPrimaryDeviceNameFromFd(int fd)
+{
+ return drmGetMinorNameForFD(fd, DRM_NODE_PRIMARY);
+}
+
+char *drmGetRenderDeviceNameFromFd(int fd)
+{
+ return drmGetMinorNameForFD(fd, DRM_NODE_RENDER);
+}
+
+static int drmParseSubsystemType(int maj, int min)
+{
+#ifdef __linux__
+ char path[PATH_MAX + 1];
+ char link[PATH_MAX + 1] = "";
+ char *name;
+
+ snprintf(path, PATH_MAX, "/sys/dev/char/%d:%d/device/subsystem",
+ maj, min);
+
+ if (readlink(path, link, PATH_MAX) < 0)
+ return -errno;
+
+ name = strrchr(link, '/');
+ if (!name)
+ return -EINVAL;
+
+ if (strncmp(name, "/pci", 4) == 0)
+ return DRM_BUS_PCI;
+
+ return -EINVAL;
+#else
+#warning "Missing implementation of drmParseSubsystemType"
+ return -EINVAL;
+#endif
+}
+
+static int drmParsePciBusInfo(int maj, int min, drmPciBusInfoPtr info)
+{
+#ifdef __linux__
+ char path[PATH_MAX + 1];
+ char data[128 + 1];
+ char *str;
+ int domain, bus, dev, func;
+ int fd, ret;
+
+ snprintf(path, PATH_MAX, "/sys/dev/char/%d:%d/device/uevent", maj, min);
+ fd = open(path, O_RDONLY);
+ if (fd < 0)
+ return -errno;
+
+ ret = read(fd, data, sizeof(data));
+ data[128] = '\0';
+ close(fd);
+ if (ret < 0)
+ return -errno;
+
+#define TAG "PCI_SLOT_NAME="
+ str = strstr(data, TAG);
+ if (str == NULL)
+ return -EINVAL;
+
+ if (sscanf(str, TAG "%04x:%02x:%02x.%1u",
+ &domain, &bus, &dev, &func) != 4)
+ return -EINVAL;
+#undef TAG
+
+ info->domain = domain;
+ info->bus = bus;
+ info->dev = dev;
+ info->func = func;
+
+ return 0;
+#else
+#warning "Missing implementation of drmParsePciBusInfo"
+ return -EINVAL;
+#endif
+}
+
+static int drmCompareBusInfo(drmDevicePtr a, drmDevicePtr b)
+{
+ if (a == NULL || b == NULL)
+ return -1;
+
+ if (a->bustype != b->bustype)
+ return -1;
+
+ switch (a->bustype) {
+ case DRM_BUS_PCI:
+ return memcmp(a->businfo.pci, b->businfo.pci, sizeof(drmPciBusInfo));
+ default:
+ break;
+ }
+
+ return -1;
+}
+
+static int drmGetNodeType(const char *name)
+{
+ if (strncmp(name, DRM_PRIMARY_MINOR_NAME,
+ sizeof(DRM_PRIMARY_MINOR_NAME) - 1) == 0)
+ return DRM_NODE_PRIMARY;
+
+ if (strncmp(name, DRM_CONTROL_MINOR_NAME,
+ sizeof(DRM_CONTROL_MINOR_NAME ) - 1) == 0)
+ return DRM_NODE_CONTROL;
+
+ if (strncmp(name, DRM_RENDER_MINOR_NAME,
+ sizeof(DRM_RENDER_MINOR_NAME) - 1) == 0)
+ return DRM_NODE_RENDER;
+
+ return -EINVAL;
+}
+
+static int drmGetMaxNodeName(void)
+{
+ return sizeof(DRM_DIR_NAME) +
+ MAX3(sizeof(DRM_PRIMARY_MINOR_NAME),
+ sizeof(DRM_CONTROL_MINOR_NAME),
+ sizeof(DRM_RENDER_MINOR_NAME)) +
+ 3 /* lenght of the node number */;
+}
+
+static int drmParsePciDeviceInfo(const char *d_name,
+ drmPciDeviceInfoPtr device)
+{
+#ifdef __linux__
+ char path[PATH_MAX + 1];
+ unsigned char config[64];
+ int fd, ret;
+
+ snprintf(path, PATH_MAX, "/sys/class/drm/%s/device/config", d_name);
+ fd = open(path, O_RDONLY);
+ if (fd < 0)
+ return -errno;
+
+ ret = read(fd, config, sizeof(config));
+ close(fd);
+ if (ret < 0)
+ return -errno;
+
+ device->vendor_id = config[0] | (config[1] << 8);
+ device->device_id = config[2] | (config[3] << 8);
+ device->revision_id = config[8];
+ device->subvendor_id = config[44] | (config[45] << 8);
+ device->subdevice_id = config[46] | (config[47] << 8);
+
+ return 0;
+#else
+#warning "Missing implementation of drmParsePciDeviceInfo"
+ return -EINVAL;
+#endif
+}
+
+void drmFreeDevice(drmDevicePtr *device)
+{
+ if (device == NULL)
+ return;
+
+ free(*device);
+ *device = NULL;
+}
+
+void drmFreeDevices(drmDevicePtr devices[], int count)
+{
+ int i;
+
+ if (devices == NULL)
+ return;
+
+ for (i = 0; i < count && devices[i] != NULL; i++)
+ drmFreeDevice(&devices[i]);
+}
+
+static int drmProcessPciDevice(drmDevicePtr *device, const char *d_name,
+ const char *node, int node_type,
+ int maj, int min, bool fetch_deviceinfo)
+{
+ const int max_node_str = drmGetMaxNodeName();
+ int ret, i;
+ char *addr;
+
+ *device = calloc(1, sizeof(drmDevice) +
+ (DRM_NODE_MAX * (sizeof(void *) + max_node_str)) +
+ sizeof(drmPciBusInfo) +
+ sizeof(drmPciDeviceInfo));
+ if (!*device)
+ return -ENOMEM;
+
+ addr = (char*)*device;
+
+ (*device)->bustype = DRM_BUS_PCI;
+ (*device)->available_nodes = 1 << node_type;
+
+ addr += sizeof(drmDevice);
+ (*device)->nodes = (char**)addr;
+
+ addr += DRM_NODE_MAX * sizeof(void *);
+ for (i = 0; i < DRM_NODE_MAX; i++) {
+ (*device)->nodes[i] = addr;
+ addr += max_node_str;
+ }
+ memcpy((*device)->nodes[node_type], node, max_node_str);
+
+ (*device)->businfo.pci = (drmPciBusInfoPtr)addr;
+
+ ret = drmParsePciBusInfo(maj, min, (*device)->businfo.pci);
+ if (ret)
+ goto free_device;
+
+ // Fetch the device info if the user has requested it
+ if (fetch_deviceinfo) {
+ addr += sizeof(drmPciBusInfo);
+ (*device)->deviceinfo.pci = (drmPciDeviceInfoPtr)addr;
+
+ ret = drmParsePciDeviceInfo(d_name, (*device)->deviceinfo.pci);
+ if (ret)
+ goto free_device;
+ }
+ return 0;
+
+free_device:
+ free(*device);
+ *device = NULL;
+ return ret;
+}
+
+static void drmFoldDuplicatedDevices(drmDevicePtr local_devices[], int count)
+{
+ int node_type, i, j;
+
+ for (i = 0; i < count; i++) {
+ for (j = i + 1; j < count; j++) {
+ if (drmCompareBusInfo(local_devices[i], local_devices[j]) == 0) {
+ local_devices[i]->available_nodes |= local_devices[j]->available_nodes;
+ node_type = log2(local_devices[j]->available_nodes);
+ memcpy(local_devices[i]->nodes[node_type],
+ local_devices[j]->nodes[node_type], drmGetMaxNodeName());
+ drmFreeDevice(&local_devices[j]);
+ }
+ }
+ }
+}
+
+/**
+ * Get information about the opened drm device
+ *
+ * \param fd file descriptor of the drm device
+ * \param device the address of a drmDevicePtr where the information
+ * will be allocated in stored
+ *
+ * \return zero on success, negative error code otherwise.
+ */
+int drmGetDevice(int fd, drmDevicePtr *device)
+{
+ drmDevicePtr *local_devices;
+ drmDevicePtr d;
+ DIR *sysdir;
+ struct dirent *dent;
+ struct stat sbuf;
+ char node[PATH_MAX + 1];
+ int node_type, subsystem_type;
+ int maj, min;
+ int ret, i, node_count;
+ int max_count = 16;
+
+ if (fd == -1 || device == NULL)
+ return -EINVAL;
+
+ if (fstat(fd, &sbuf))
+ return -errno;
+
+ maj = major(sbuf.st_rdev);
+ min = minor(sbuf.st_rdev);
+
+ if (maj != DRM_MAJOR || !S_ISCHR(sbuf.st_mode))
+ return -EINVAL;
+
+ subsystem_type = drmParseSubsystemType(maj, min);
+
+ local_devices = calloc(max_count, sizeof(drmDevicePtr));
+ if (local_devices == NULL)
+ return -ENOMEM;
+
+ sysdir = opendir(DRM_DIR_NAME);
+ if (!sysdir) {
+ ret = -errno;
+ goto free_locals;
+ }
+
+ i = 0;
+ while ((dent = readdir(sysdir))) {
+ node_type = drmGetNodeType(dent->d_name);
+ if (node_type < 0)
+ continue;
+
+ snprintf(node, PATH_MAX, "%s/%s", DRM_DIR_NAME, dent->d_name);
+ if (stat(node, &sbuf))
+ continue;
+
+ maj = major(sbuf.st_rdev);
+ min = minor(sbuf.st_rdev);
+
+ if (maj != DRM_MAJOR || !S_ISCHR(sbuf.st_mode))
+ continue;
+
+ if (drmParseSubsystemType(maj, min) != subsystem_type)
+ continue;
+
+ switch (subsystem_type) {
+ case DRM_BUS_PCI:
+ ret = drmProcessPciDevice(&d, dent->d_name, node, node_type,
+ maj, min, true);
+ if (ret)
+ goto free_devices;
+
+ break;
+ default:
+ fprintf(stderr, "The subsystem type is not supported yet\n");
+ continue;
+ }
+
+ if (i >= max_count) {
+ drmDevicePtr *temp;
+
+ max_count += 16;
+ temp = realloc(local_devices, max_count * sizeof(drmDevicePtr));
+ if (!temp)
+ goto free_devices;
+ local_devices = temp;
+ }
+
+ local_devices[i] = d;
+ i++;
+ }
+ node_count = i;
+
+ /* Fold nodes into a single device if they share the same bus info */
+ drmFoldDuplicatedDevices(local_devices, node_count);
+
+ *device = local_devices[0];
+ for (i = 1; i < node_count && local_devices[i]; i++)
+ drmFreeDevice(&local_devices[i]);
+
+ closedir(sysdir);
+ free(local_devices);
+ return 0;
+
+free_devices:
+ drmFreeDevices(local_devices, i);
+ closedir(sysdir);
+
+free_locals:
+ free(local_devices);
+ return ret;
+}
+
+/**
+ * Get drm devices on the system
+ *
+ * \param devices the array of devices with drmDevicePtr elements
+ * can be NULL to get the device number first
+ * \param max_devices the maximum number of devices for the array
+ *
+ * \return on error - negative error code,
+ * if devices is NULL - total number of devices available on the system,
+ * alternatively the number of devices stored in devices[], which is
+ * capped by the max_devices.
+ */
+int drmGetDevices(drmDevicePtr devices[], int max_devices)
+{
+ drmDevicePtr *local_devices;
+ drmDevicePtr device;
+ DIR *sysdir;
+ struct dirent *dent;
+ struct stat sbuf;
+ char node[PATH_MAX + 1];
+ int node_type, subsystem_type;
+ int maj, min;
+ int ret, i, node_count, device_count;
+ int max_count = 16;
+
+ local_devices = calloc(max_count, sizeof(drmDevicePtr));
+ if (local_devices == NULL)
+ return -ENOMEM;
+
+ sysdir = opendir(DRM_DIR_NAME);
+ if (!sysdir) {
+ ret = -errno;
+ goto free_locals;
+ }
+
+ i = 0;
+ while ((dent = readdir(sysdir))) {
+ node_type = drmGetNodeType(dent->d_name);
+ if (node_type < 0)
+ continue;
+
+ snprintf(node, PATH_MAX, "%s/%s", DRM_DIR_NAME, dent->d_name);
+ if (stat(node, &sbuf))
+ continue;
+
+ maj = major(sbuf.st_rdev);
+ min = minor(sbuf.st_rdev);
+
+ if (maj != DRM_MAJOR || !S_ISCHR(sbuf.st_mode))
+ continue;
+
+ subsystem_type = drmParseSubsystemType(maj, min);
+
+ if (subsystem_type < 0)
+ continue;
+
+ switch (subsystem_type) {
+ case DRM_BUS_PCI:
+ ret = drmProcessPciDevice(&device, dent->d_name, node, node_type,
+ maj, min, devices != NULL);
+ if (ret)
+ goto free_devices;
+
+ break;
+ default:
+ fprintf(stderr, "The subsystem type is not supported yet\n");
+ continue;
+ }
+
+ if (i >= max_count) {
+ drmDevicePtr *temp;
+
+ max_count += 16;
+ temp = realloc(local_devices, max_count * sizeof(drmDevicePtr));
+ if (!temp)
+ goto free_devices;
+ local_devices = temp;
+ }
+
+ local_devices[i] = device;
+ i++;
+ }
+ node_count = i;
+
+ /* Fold nodes into a single device if they share the same bus info */
+ drmFoldDuplicatedDevices(local_devices, node_count);
+
+ device_count = 0;
+ for (i = 0; i < node_count && local_devices[i]; i++) {
+ if ((devices != NULL) && (device_count < max_devices))
+ devices[device_count] = local_devices[i];
+ else
+ drmFreeDevice(&local_devices[i]);
+
+ device_count++;
+ }
+
+ closedir(sysdir);
+ free(local_devices);
+ return device_count;
+
+free_devices:
+ drmFreeDevices(local_devices, i);
+ closedir(sysdir);
+
+free_locals:
+ free(local_devices);
+ return ret;
+}