commit de4b989

Michael Forney  ·  2014-01-22 11:18:45 +0000 UTC
parent 3d91ff6
drm: Remove udev dependency
3 files changed,  +48, -70
+46, -68
  1@@ -1,6 +1,6 @@
  2 /* swc: drm.c
  3  *
  4- * Copyright (c) 2013 Michael Forney
  5+ * Copyright (c) 2013, 2014 Michael Forney
  6  *
  7  * Permission is hereby granted, free of charge, to any person obtaining a copy
  8  * of this software and associated documentation files (the "Software"), to deal
  9@@ -30,12 +30,12 @@
 10 #include "util.h"
 11 #include "wayland_buffer.h"
 12 
 13+#include <dirent.h>
 14 #include <stdio.h>
 15 #include <stdlib.h>
 16 #include <string.h>
 17 #include <fcntl.h>
 18 #include <unistd.h>
 19-#include <libudev.h>
 20 #include <libdrm/drm.h>
 21 #include <xf86drm.h>
 22 #include <wld/wld.h>
 23@@ -47,7 +47,6 @@ struct swc_drm swc_drm;
 24 
 25 static struct
 26 {
 27-    uint32_t id;
 28     char * path;
 29 
 30     uint32_t taken_ids;
 31@@ -149,70 +148,66 @@ static const struct wl_drm_interface drm_implementation = {
 32         .create_prime_buffer = &create_prime_buffer
 33 };
 34 
 35-static struct udev_device * find_primary_drm_device(const char * seat)
 36+static int select_card(const struct dirent * entry)
 37 {
 38-    struct udev_enumerate * enumerate;
 39-    struct udev_list_entry * entry;
 40-    const char * path;
 41-    const char * device_seat;
 42-    const char * boot_vga;
 43-    struct udev_device * pci;
 44-    struct udev_device * device, * drm_device = NULL;
 45+    unsigned num;
 46 
 47-    enumerate = udev_enumerate_new(swc.udev);
 48-    udev_enumerate_add_match_subsystem(enumerate, "drm");
 49-    udev_enumerate_add_match_sysname(enumerate, "card[0-9]*");
 50-
 51-    udev_enumerate_scan_devices(enumerate);
 52+    return sscanf(entry->d_name, "card%u", &num) == 1;
 53+}
 54 
 55-    udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(enumerate))
 56-    {
 57-        path = udev_list_entry_get_name(entry);
 58-        device = udev_device_new_from_syspath(swc.udev, path);
 59+static bool find_primary_drm_device(char ** device_path)
 60+{
 61+    struct dirent ** cards, * card = NULL;
 62+    int num_cards, ret;
 63+    unsigned index;
 64+    char path[64];
 65+    FILE * file;
 66+    unsigned char boot_vga;
 67 
 68-        printf("device node path: %s\n", udev_device_get_devnode(device));
 69+    num_cards = scandir("/dev/dri", &cards, &select_card, &alphasort);
 70 
 71-        device_seat = udev_device_get_property_value(device, "ID_SEAT");
 72+    if (num_cards == -1)
 73+        return false;
 74 
 75-        /* If the ID_SEAT property is not set, the device belongs to seat0. */
 76-        if (!device_seat)
 77-            device_seat = "seat0";
 78-        else
 79-            printf("device seat: %s\n", device_seat);
 80+    for (index = 0; index < num_cards; ++index)
 81+    {
 82+        snprintf(path, sizeof path,
 83+                 "/sys/class/drm/%s/device/boot_vga", cards[index]->d_name);
 84 
 85-        /* Make sure the DRM device belongs to the seat we are in. */
 86-        if (strcmp(device_seat, seat) != 0)
 87+        if ((file = fopen(path, "r")))
 88         {
 89-            udev_device_unref(device);
 90-            continue;
 91-        }
 92-
 93-        pci = udev_device_get_parent_with_subsystem_devtype(device, "pci",
 94-                                                            NULL);
 95+            ret = fscanf(file, "%hhu", &boot_vga);
 96+            fclose(file);
 97 
 98-        if (pci)
 99-        {
100-            /* boot_vga = 1 indicates that this DRM device is the primary GPU. */
101-            boot_vga = udev_device_get_sysattr_value(pci, "boot_vga");
102-            if (boot_vga && strcmp(boot_vga, "1") == 0)
103+            if (ret == 1 && boot_vga)
104             {
105-                if (drm_device)
106-                    udev_device_unref(drm_device);
107-                drm_device = device;
108+                free(card);
109+                card = cards[index];
110+                DEBUG("/dev/dri/%s is the primary GPU\n", card->d_name);
111                 break;
112             }
113         }
114 
115-        /* Make sure we have a backup device. */
116-        if (!drm_device)
117-            drm_device = device;
118+        if (!card)
119+            card = cards[index];
120         else
121-            udev_device_unref(device);
122+            free(cards[index]);
123     }
124 
125-    udev_enumerate_unref(enumerate);
126+    free(cards);
127+
128+    if (!card)
129+        return false;
130 
131-    return drm_device;
132+    *device_path = malloc(sizeof "/dev/dri/" + strlen(card->d_name));
133+
134+    if (!*device_path)
135+        return false;
136+
137+    sprintf(*device_path, "/dev/dri/%s", card->d_name);
138+    free(card);
139+
140+    return true;
141 }
142 
143 static bool find_available_crtc(drmModeRes * resources,
144@@ -306,32 +301,15 @@ static void bind_drm(struct wl_client * client, void * data, uint32_t version,
145     wl_drm_send_format(resource, WL_DRM_FORMAT_ARGB8888);
146 }
147 
148-bool swc_drm_initialize(const char * seat_name)
149+bool swc_drm_initialize()
150 {
151-    const char * sysnum;
152-    char * end;
153-    struct udev_device * drm_device;
154-
155-    if (!(drm_device = find_primary_drm_device(seat_name)))
156+    if (!find_primary_drm_device(&drm.path))
157     {
158         ERROR("Could not find DRM device\n");
159         goto error0;
160     }
161 
162-    /* XXX: Why do we need the sysnum? */
163-    sysnum = udev_device_get_sysnum(drm_device);
164-    drm.id = strtoul(sysnum, &end, 10);
165-
166-    if (*end != '\0')
167-    {
168-        ERROR("Could not get DRM device sysnum\n");
169-        udev_device_unref(drm_device);
170-        goto error0;
171-    }
172-
173     drm.taken_ids = 0;
174-    drm.path = strdup(udev_device_get_devnode(drm_device));
175-    udev_device_unref(drm_device);
176     swc.drm->fd = swc_launch_open_device(drm.path, O_RDWR | O_CLOEXEC);
177 
178     if (swc.drm->fd == -1)
+1, -1
1@@ -17,7 +17,7 @@ struct swc_drm
2     struct wld_renderer * renderer;
3 };
4 
5-bool swc_drm_initialize(const char * seat);
6+bool swc_drm_initialize();
7 void swc_drm_finalize();
8 
9 bool swc_drm_create_screens(struct wl_list * screens);
+1, -1
1@@ -107,7 +107,7 @@ bool swc_initialize(struct wl_display * display,
2         goto error1;
3     }
4 
5-    if (!swc_drm_initialize(default_seat))
6+    if (!swc_drm_initialize())
7     {
8         ERROR("Could not initialize DRM\n");
9         goto error2;