wlroots/backend/drm/udev.c

194 lines
4.0 KiB
C
Raw Normal View History

2017-05-01 03:50:19 +00:00
#define _POSIX_C_SOURCE 200809L
2017-05-01 03:20:48 +00:00
#include <libudev.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <xf86drm.h>
#include <xf86drmMode.h>
#include <wayland-server.h>
2017-05-01 03:20:48 +00:00
2017-05-01 05:49:18 +00:00
#include "backend/drm/backend.h"
2017-05-01 03:50:19 +00:00
#include "backend/drm/udev.h"
#include "backend/drm/session.h"
#include "backend/drm/drm.h"
2017-05-02 01:00:25 +00:00
#include "common/log.h"
2017-05-01 03:20:48 +00:00
2017-05-01 05:49:18 +00:00
static bool device_is_kms(struct wlr_udev *udev,
struct wlr_session *session,
struct udev_device *dev,
int *fd_out)
2017-05-01 03:20:48 +00:00
{
const char *path = udev_device_get_devnode(dev);
int fd;
if (!path)
return false;
2017-05-01 05:49:18 +00:00
fd = wlr_session_take_device(session, path, NULL);
2017-05-01 03:20:48 +00:00
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;
2017-05-01 05:49:18 +00:00
if (*fd_out >= 0) {
wlr_session_release_device(session, *fd_out);
free(udev->drm_path);
2017-05-01 03:20:48 +00:00
}
2017-05-01 05:49:18 +00:00
*fd_out = fd;
udev->drm_path = strdup(path);
2017-05-01 03:20:48 +00:00
drmModeFreeResources(res);
return true;
out_res:
drmModeFreeResources(res);
out_fd:
2017-05-01 05:49:18 +00:00
wlr_session_release_device(session, fd);
2017-05-01 03:20:48 +00:00
return false;
}
2017-05-01 05:49:18 +00:00
int wlr_udev_find_gpu(struct wlr_udev *udev, struct wlr_session *session)
2017-05-01 03:20:48 +00:00
{
2017-05-01 05:49:18 +00:00
int fd = -1;
2017-05-01 03:20:48 +00:00
2017-05-01 05:49:18 +00:00
struct udev_enumerate *en = udev_enumerate_new(udev->udev);
2017-05-02 01:00:25 +00:00
if (!en) {
wlr_log(L_ERROR, "Failed to create udev enumeration");
2017-05-01 05:49:18 +00:00
return -1;
2017-05-02 01:00:25 +00:00
}
2017-05-01 03:20:48 +00:00
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);
2017-05-01 05:49:18 +00:00
struct udev_device *dev = udev_device_new_from_syspath(udev->udev, path);
2017-05-01 03:20:48 +00:00
if (!dev)
continue;
const char *seat = udev_device_get_property_value(dev, "ID_SEAT");
if (!seat)
seat = "seat0";
2017-05-01 05:49:18 +00:00
if (strcmp(session->seat, seat) != 0) {
2017-05-01 03:20:48 +00:00
udev_device_unref(dev);
continue;
}
struct udev_device *pci =
2017-05-02 01:00:25 +00:00
udev_device_get_parent_with_subsystem_devtype(dev, "pci", NULL);
2017-05-01 03:20:48 +00:00
if (pci) {
const char *id = udev_device_get_sysattr_value(pci, "boot_vga");
if (id && strcmp(id, "1") == 0)
is_boot_vga = true;
2017-05-02 06:13:17 +00:00
//udev_device_unref(pci);
2017-05-01 03:20:48 +00:00
}
2017-05-02 06:13:17 +00:00
// We already have a valid GPU
2017-05-01 05:49:18 +00:00
if (!is_boot_vga && fd >= 0) {
2017-05-01 03:20:48 +00:00
udev_device_unref(dev);
continue;
}
2017-05-01 05:49:18 +00:00
if (!device_is_kms(udev, session, dev, &fd)) {
2017-05-01 03:20:48 +00:00
udev_device_unref(dev);
continue;
}
2017-05-02 06:13:17 +00:00
udev_device_unref(dev);
if (is_boot_vga)
2017-05-01 03:20:48 +00:00
break;
}
udev_enumerate_unref(en);
2017-05-01 05:49:18 +00:00
return fd;
2017-05-01 03:20:48 +00:00
}
static int udev_event(int fd, uint32_t mask, void *data)
2017-05-01 03:20:48 +00:00
{
struct wlr_drm_backend *backend = data;
struct wlr_udev *udev = &backend->udev;
struct udev_device *dev = udev_monitor_receive_device(udev->mon);
if (!dev)
return 1;
const char *path = udev_device_get_devnode(dev);
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;
wlr_drm_scan_connectors(backend);
out:
udev_device_unref(dev);
return 1;
}
bool wlr_udev_init(struct wlr_drm_backend *backend)
{
struct wlr_udev *udev = &backend->udev;
2017-05-01 05:49:18 +00:00
udev->udev = udev_new();
2017-05-02 01:00:25 +00:00
if (!udev->udev) {
wlr_log(L_ERROR, "Failed to create udev context");
2017-05-01 03:20:48 +00:00
return false;
2017-05-02 01:00:25 +00:00
}
2017-05-01 03:20:48 +00:00
2017-05-01 05:49:18 +00:00
udev->mon = udev_monitor_new_from_netlink(udev->udev, "udev");
if (!udev->mon) {
2017-05-02 01:00:25 +00:00
wlr_log(L_ERROR, "Failed to create udev monitor");
goto error_udev;
2017-05-01 03:20:48 +00:00
}
2017-05-01 05:49:18 +00:00
udev_monitor_filter_add_match_subsystem_devtype(udev->mon, "drm", NULL);
udev_monitor_enable_receiving(udev->mon);
2017-05-01 03:20:48 +00:00
backend->event_src.udev = wl_event_loop_add_fd(backend->event_loop,
udev_monitor_get_fd(udev->mon), WL_EVENT_READABLE,
udev_event, backend);
if (!backend->event_src.udev) {
wlr_log(L_ERROR, "Failed to create udev event source");
goto error_mon;
}
2017-05-01 05:49:18 +00:00
udev->drm_path = NULL;
2017-05-01 03:20:48 +00:00
return true;
error_mon:
udev_monitor_unref(udev->mon);
error_udev:
udev_unref(udev->udev);
return false;
2017-05-01 03:20:48 +00:00
}
2017-05-01 05:49:18 +00:00
void wlr_udev_free(struct wlr_udev *udev)
2017-05-01 03:20:48 +00:00
{
2017-05-01 05:49:18 +00:00
if (!udev)
2017-05-01 03:20:48 +00:00
return;
2017-05-01 05:49:18 +00:00
udev_monitor_unref(udev->mon);
udev_unref(udev->udev);
free(udev->drm_path);
2017-05-01 03:20:48 +00:00
}