wlroots/backend/drm/udev.c

218 lines
4.6 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
#include <wlr/session.h>
2017-05-07 14:00:23 +00:00
#include "backend.h"
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/drm.h"
2017-05-02 01:00:25 +00:00
#include "common/log.h"
2017-05-01 03:20:48 +00:00
2017-05-03 09:28:44 +00:00
/* Tests if 'path' is KMS compatible by trying to open it.
* It leaves the open device in *fd_out it it succeeds.
*/
static bool device_is_kms(struct wlr_session *restrict session,
const char *restrict path, int *restrict fd_out) {
2017-05-01 03:20:48 +00:00
int fd;
2017-05-03 09:28:44 +00:00
if (!path) {
2017-05-01 03:20:48 +00:00
return false;
2017-05-03 09:28:44 +00:00
}
2017-05-01 03:20:48 +00:00
fd = wlr_session_open_file(session, path);
2017-05-03 09:28:44 +00:00
if (fd < 0) {
2017-05-01 03:20:48 +00:00
return false;
2017-05-03 09:28:44 +00:00
}
2017-05-01 03:20:48 +00:00
drmModeRes *res = drmModeGetResources(fd);
2017-05-03 09:28:44 +00:00
if (!res) {
2017-05-01 03:20:48 +00:00
goto out_fd;
2017-05-03 09:28:44 +00:00
}
2017-05-01 03:20:48 +00:00
if (res->count_crtcs <= 0 || res->count_connectors <= 0 ||
2017-05-03 09:28:44 +00:00
res->count_encoders <= 0) {
2017-05-01 03:20:48 +00:00
goto out_res;
2017-05-03 09:28:44 +00:00
}
2017-05-01 03:20:48 +00:00
2017-05-01 05:49:18 +00:00
if (*fd_out >= 0) {
wlr_session_close_file(session, *fd_out);
2017-05-01 03:20:48 +00:00
}
2017-05-01 05:49:18 +00:00
*fd_out = fd;
2017-05-01 03:20:48 +00:00
drmModeFreeResources(res);
return true;
out_res:
drmModeFreeResources(res);
out_fd:
wlr_session_close_file(session, fd);
2017-05-01 03:20:48 +00:00
return false;
}
2017-05-03 09:28:44 +00:00
/* Tries to find the primary GPU by checking for the "boot_vga" attribute.
* If it's not found, it returns the first valid GPU it finds.
*/
int wlr_udev_find_gpu(struct wlr_udev *udev, struct wlr_session *session) {
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);
2017-05-03 09:28:44 +00:00
2017-05-01 03:20:48 +00:00
struct udev_list_entry *entry;
2017-05-03 09:28:44 +00:00
int fd = -1;
char *drm_path = NULL;
2017-05-01 03:20:48 +00:00
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-03 09:28:44 +00:00
if (!dev) {
2017-05-01 03:20:48 +00:00
continue;
2017-05-03 09:28:44 +00:00
}
2017-05-01 03:20:48 +00:00
/*
2017-05-01 03:20:48 +00:00
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;
}
*/
2017-05-01 03:20:48 +00:00
2017-05-03 09:28:44 +00:00
// This is owned by 'dev', so we don't need to free it
2017-05-01 03:20:48 +00:00
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");
2017-05-03 09:28:44 +00:00
if (id && strcmp(id, "1") == 0) {
2017-05-01 03:20:48 +00:00
is_boot_vga = true;
2017-05-03 09:28:44 +00:00
}
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-03 09:28:44 +00:00
path = udev_device_get_devnode(dev);
if (!device_is_kms(session, path, &fd)) {
2017-05-01 03:20:48 +00:00
udev_device_unref(dev);
continue;
}
2017-05-03 09:28:44 +00:00
free(drm_path);
drm_path = strdup(path);
2017-05-02 06:13:17 +00:00
udev_device_unref(dev);
2017-05-03 09:28:44 +00:00
// We've found the primary GPU
if (is_boot_vga) {
2017-05-01 03:20:48 +00:00
break;
2017-05-03 09:28:44 +00:00
}
2017-05-01 03:20:48 +00:00
}
udev_enumerate_unref(en);
2017-05-01 05:49:18 +00:00
2017-05-03 09:28:44 +00:00
udev->drm_path = drm_path;
2017-05-01 05:49:18 +00:00
return fd;
2017-05-01 03:20:48 +00:00
}
2017-05-03 09:28:44 +00:00
static int udev_event(int fd, uint32_t mask, void *data) {
struct wlr_udev *udev = data;
2017-05-07 14:00:23 +00:00
struct wlr_backend_state *state = wl_container_of(udev, state, udev);
struct udev_device *dev = udev_monitor_receive_device(udev->mon);
2017-05-03 09:28:44 +00:00
if (!dev) {
return 1;
2017-05-03 09:28:44 +00:00
}
2017-05-31 20:53:59 +00:00
const char *action = udev_device_get_action(dev);
const char *path = udev_device_get_devnode(dev);
2017-05-31 20:53:59 +00:00
wlr_log(L_DEBUG, "udev event for %s (%s)",
udev_device_get_sysname(dev), action);
2017-05-03 09:28:44 +00:00
if (!path || strcmp(path, udev->drm_path) != 0) {
goto out;
2017-05-03 09:28:44 +00:00
}
2017-05-03 09:28:44 +00:00
if (!action || strcmp(action, "change") != 0) {
goto out;
2017-05-03 09:28:44 +00:00
}
2017-05-07 14:00:23 +00:00
wlr_drm_scan_connectors(state);
out:
udev_device_unref(dev);
return 1;
}
2017-05-03 09:28:44 +00:00
bool wlr_udev_init(struct wl_display *display, struct wlr_udev *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
2017-05-03 09:28:44 +00:00
struct wl_event_loop *event_loop = wl_display_get_event_loop(display);
int fd = udev_monitor_get_fd(udev->mon);
udev->event = wl_event_loop_add_fd(event_loop, fd, WL_EVENT_READABLE,
udev_event, udev);
if (!udev->event) {
wlr_log(L_ERROR, "Failed to create udev event source");
goto error_mon;
}
2017-05-31 20:53:59 +00:00
wlr_log(L_DEBUG, "Successfully initialized udev");
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-03 09:28:44 +00:00
void wlr_udev_free(struct wlr_udev *udev) {
if (!udev) {
2017-05-01 03:20:48 +00:00
return;
2017-05-03 09:28:44 +00:00
}
wl_event_source_remove(udev->event);
2017-05-01 03:20:48 +00:00
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
}