Blame libkms/linux.c

Packit Service 103f6b
/**************************************************************************
Packit Service 103f6b
 *
Packit Service 103f6b
 * Copyright © 2009 VMware, Inc., Palo Alto, CA., USA
Packit Service 103f6b
 * All Rights Reserved.
Packit Service 103f6b
 *
Packit Service 103f6b
 * Permission is hereby granted, free of charge, to any person obtaining a
Packit Service 103f6b
 * copy of this software and associated documentation files (the
Packit Service 103f6b
 * "Software"), to deal in the Software without restriction, including
Packit Service 103f6b
 * without limitation the rights to use, copy, modify, merge, publish,
Packit Service 103f6b
 * distribute, sub license, and/or sell copies of the Software, and to
Packit Service 103f6b
 * permit persons to whom the Software is furnished to do so, subject to
Packit Service 103f6b
 * the following conditions:
Packit Service 103f6b
 *
Packit Service 103f6b
 * The above copyright notice and this permission notice (including the
Packit Service 103f6b
 * next paragraph) shall be included in all copies or substantial portions
Packit Service 103f6b
 * of the Software.
Packit Service 103f6b
 *
Packit Service 103f6b
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Packit Service 103f6b
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Packit Service 103f6b
 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
Packit Service 103f6b
 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
Packit Service 103f6b
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
Packit Service 103f6b
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
Packit Service 103f6b
 * USE OR OTHER DEALINGS IN THE SOFTWARE.
Packit Service 103f6b
 *
Packit Service 103f6b
 **************************************************************************/
Packit Service 103f6b
/*
Packit Service 103f6b
 * Thanks to krh and jcristau for the tips on
Packit Service 103f6b
 * going from fd to pci id via fstat and udev.
Packit Service 103f6b
 */
Packit Service 103f6b
Packit Service 103f6b
#include <errno.h>
Packit Service 103f6b
#include <stdio.h>
Packit Service 103f6b
#include <stdlib.h>
Packit Service 103f6b
#include <xf86drm.h>
Packit Service 103f6b
#include <string.h>
Packit Service 103f6b
#include <unistd.h>
Packit Service 103f6b
#include <sys/stat.h>
Packit Service 103f6b
#include <sys/types.h>
Packit Service 103f6b
#ifdef MAJOR_IN_MKDEV
Packit Service 103f6b
#include <sys/mkdev.h>
Packit Service 103f6b
#endif
Packit Service 103f6b
#ifdef MAJOR_IN_SYSMACROS
Packit Service 103f6b
#include <sys/sysmacros.h>
Packit Service 103f6b
#endif
Packit Service 103f6b
Packit Service 103f6b
#include "libdrm_macros.h"
Packit Service 103f6b
#include "internal.h"
Packit Service 103f6b
Packit Service 103f6b
#define PATH_SIZE 512
Packit Service 103f6b
Packit Service 103f6b
static int
Packit Service 103f6b
linux_name_from_sysfs(int fd, char **out)
Packit Service 103f6b
{
Packit Service 103f6b
	char path[PATH_SIZE+1] = ""; /* initialize to please valgrind */
Packit Service 103f6b
	char link[PATH_SIZE+1] = "";
Packit Service 103f6b
	struct stat buffer;
Packit Service 103f6b
	unsigned maj, min;
Packit Service 103f6b
	char* slash_name;
Packit Service 103f6b
	int ret;
Packit Service 103f6b
Packit Service 103f6b
	/* 
Packit Service 103f6b
	 * Inside the sysfs directory for the device there is a symlink
Packit Service 103f6b
	 * to the directory representing the driver module, that path
Packit Service 103f6b
	 * happens to hold the name of the driver.
Packit Service 103f6b
	 *
Packit Service 103f6b
	 * So lets get the symlink for the drm device. Then read the link
Packit Service 103f6b
	 * and filter out the last directory which happens to be the name
Packit Service 103f6b
	 * of the driver, which we can use to load the correct interface.
Packit Service 103f6b
	 *
Packit Service 103f6b
	 * Thanks to Ray Strode of Plymouth for the code.
Packit Service 103f6b
	 */
Packit Service 103f6b
Packit Service 103f6b
	ret = fstat(fd, &buffer);
Packit Service 103f6b
	if (ret)
Packit Service 103f6b
		return -EINVAL;
Packit Service 103f6b
Packit Service 103f6b
	if (!S_ISCHR(buffer.st_mode))
Packit Service 103f6b
		return -EINVAL;
Packit Service 103f6b
Packit Service 103f6b
	maj = major(buffer.st_rdev);
Packit Service 103f6b
	min = minor(buffer.st_rdev);
Packit Service 103f6b
Packit Service 103f6b
	snprintf(path, PATH_SIZE, "/sys/dev/char/%d:%d/device/driver", maj, min);
Packit Service 103f6b
Packit Service 103f6b
	if (readlink(path, link, PATH_SIZE) < 0)
Packit Service 103f6b
		return -EINVAL;
Packit Service 103f6b
Packit Service 103f6b
	/* link looks something like this: ../../../bus/pci/drivers/intel */
Packit Service 103f6b
	slash_name = strrchr(link, '/');
Packit Service 103f6b
	if (!slash_name)
Packit Service 103f6b
		return -EINVAL;
Packit Service 103f6b
Packit Service 103f6b
	/* copy name and at the same time remove the slash */
Packit Service 103f6b
	*out = strdup(slash_name + 1);
Packit Service 103f6b
	return 0;
Packit Service 103f6b
}
Packit Service 103f6b
Packit Service 103f6b
static int
Packit Service 103f6b
linux_from_sysfs(int fd, struct kms_driver **out)
Packit Service 103f6b
{
Packit Service 103f6b
	char *name;
Packit Service 103f6b
	int ret;
Packit Service 103f6b
Packit Service 103f6b
	ret = linux_name_from_sysfs(fd, &name);
Packit Service 103f6b
	if (ret)
Packit Service 103f6b
		return ret;
Packit Service 103f6b
Packit Service 103f6b
#if HAVE_INTEL
Packit Service 103f6b
	if (!strcmp(name, "intel"))
Packit Service 103f6b
		ret = intel_create(fd, out);
Packit Service 103f6b
	else
Packit Service 103f6b
#endif
Packit Service 103f6b
#if HAVE_VMWGFX
Packit Service 103f6b
	if (!strcmp(name, "vmwgfx"))
Packit Service 103f6b
		ret = vmwgfx_create(fd, out);
Packit Service 103f6b
	else
Packit Service 103f6b
#endif
Packit Service 103f6b
#if HAVE_NOUVEAU
Packit Service 103f6b
	if (!strcmp(name, "nouveau"))
Packit Service 103f6b
		ret = nouveau_create(fd, out);
Packit Service 103f6b
	else
Packit Service 103f6b
#endif
Packit Service 103f6b
#if HAVE_RADEON
Packit Service 103f6b
	if (!strcmp(name, "radeon"))
Packit Service 103f6b
		ret = radeon_create(fd, out);
Packit Service 103f6b
	else
Packit Service 103f6b
#endif
Packit Service 103f6b
#if HAVE_EXYNOS
Packit Service 103f6b
	if (!strcmp(name, "exynos"))
Packit Service 103f6b
		ret = exynos_create(fd, out);
Packit Service 103f6b
	else
Packit Service 103f6b
#endif
Packit Service 103f6b
		ret = -ENOSYS;
Packit Service 103f6b
Packit Service 103f6b
	free(name);
Packit Service 103f6b
	return ret;
Packit Service 103f6b
}
Packit Service 103f6b
Packit Service 103f6b
drm_private int
Packit Service 103f6b
linux_create(int fd, struct kms_driver **out)
Packit Service 103f6b
{
Packit Service 103f6b
	if (!dumb_create(fd, out))
Packit Service 103f6b
		return 0;
Packit Service 103f6b
Packit Service 103f6b
	return linux_from_sysfs(fd, out);
Packit Service 103f6b
}