You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

170 lines
3.4 KiB

8 years ago
#define _POSIX_C_SOURCE 200809L
#include <libudev.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <xf86drm.h>
#include <xf86drmMode.h>
8 years ago
#include "backend/drm/backend.h"
8 years ago
#include "backend/drm/udev.h"
#include "backend/drm/session.h"
#include "backend/drm/drm.h"
8 years ago
static bool device_is_kms(struct wlr_udev *udev,
struct wlr_session *session,
struct udev_device *dev,
int *fd_out)
{
const char *path = udev_device_get_devnode(dev);
int fd;
if (!path)
return false;
8 years ago
fd = wlr_session_take_device(session, path, NULL);
if (fd < 0)
return false;
drmModeRes *res = drmModeGetResources(fd);
if (!res)
goto out_fd;
if (res->count_crtcs <= 0 || res->count_connectors <= 0 ||
res->count_encoders <= 0)
goto out_res;
8 years ago
if (*fd_out >= 0) {
wlr_session_release_device(session, *fd_out);
free(udev->drm_path);
}
8 years ago
*fd_out = fd;
udev->drm_path = strdup(path);
drmModeFreeResources(res);
return true;
out_res:
drmModeFreeResources(res);
out_fd:
8 years ago
wlr_session_release_device(session, fd);
return false;
}
8 years ago
int wlr_udev_find_gpu(struct wlr_udev *udev, struct wlr_session *session)
{
8 years ago
int fd = -1;
8 years ago
struct udev_enumerate *en = udev_enumerate_new(udev->udev);
if (!en)
8 years ago
return -1;
udev_enumerate_add_match_subsystem(en, "drm");
udev_enumerate_add_match_sysname(en, "card[0-9]*");
udev_enumerate_scan_devices(en);
struct udev_list_entry *entry;
udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(en)) {
bool is_boot_vga = false;
const char *path = udev_list_entry_get_name(entry);
8 years ago
struct udev_device *dev = udev_device_new_from_syspath(udev->udev, path);
if (!dev)
continue;
const char *seat = udev_device_get_property_value(dev, "ID_SEAT");
if (!seat)
seat = "seat0";
8 years ago
if (strcmp(session->seat, seat) != 0) {
udev_device_unref(dev);
continue;
}
struct udev_device *pci =
udev_device_get_parent_with_subsystem_devtype(dev,
"pci", NULL);
if (pci) {
const char *id = udev_device_get_sysattr_value(pci, "boot_vga");
if (id && strcmp(id, "1") == 0)
is_boot_vga = true;
udev_device_unref(pci);
}
8 years ago
if (!is_boot_vga && fd >= 0) {
udev_device_unref(dev);
continue;
}
8 years ago
if (!device_is_kms(udev, session, dev, &fd)) {
udev_device_unref(dev);
continue;
}
if (is_boot_vga) {
break;
}
}
udev_enumerate_unref(en);
8 years ago
return fd;
}
8 years ago
bool wlr_udev_init(struct wlr_udev *udev)
{
8 years ago
udev->udev = udev_new();
if (!udev->udev)
return false;
8 years ago
udev->mon = udev_monitor_new_from_netlink(udev->udev, "udev");
if (!udev->mon) {
udev_unref(udev->udev);
return false;
}
8 years ago
udev_monitor_filter_add_match_subsystem_devtype(udev->mon, "drm", NULL);
udev_monitor_enable_receiving(udev->mon);
8 years ago
udev->mon_fd = udev_monitor_get_fd(udev->mon);
udev->drm_path = NULL;
return true;
}
8 years ago
void wlr_udev_free(struct wlr_udev *udev)
{
8 years ago
if (!udev)
return;
8 years ago
udev_monitor_unref(udev->mon);
udev_unref(udev->udev);
free(udev->drm_path);
}
8 years ago
void wlr_udev_event(struct wlr_drm_backend *backend)
{
8 years ago
struct wlr_udev *udev = &backend->udev;
struct udev_device *dev = udev_monitor_receive_device(udev->mon);
if (!dev)
return;
const char *path = udev_device_get_devnode(dev);
8 years ago
if (!path || strcmp(path, udev->drm_path) != 0)
goto out;
const char *action = udev_device_get_action(dev);
if (!action || strcmp(action, "change") != 0)
goto out;
8 years ago
wlr_drm_scan_connectors(backend);
out:
udev_device_unref(dev);
}