commit edc0746

shrub  ·  2026-07-06 19:24:14 +0000 UTC
parent bb5d247
use libdrm to get drm device
1 files changed,  +12, -42
M drm.c
M drm.c
+12, -42
 1@@ -24,16 +24,7 @@
 2 #include "drm.h"
 3 #include "drm-private.h"
 4 
 5-#include <sys/types.h>
 6-#ifndef major
 7-
 8-#ifdef __linux__
 9-#include <sys/sysmacros.h>
10-#elif defined(__sun)
11-#include <sys/mkdev.h>
12-#endif
13-
14-#endif
15+#include <xf86drm.h>
16 
17 const static struct drm_driver *drivers[] = {
18 #if WITH_DRM_INTEL
19@@ -48,50 +39,29 @@ const static struct drm_driver *drivers[] = {
20 static const struct drm_driver *
21 find_driver(int fd)
22 {
23-	#ifdef __sun
24-	(void)fd;
25-	return NULL;
26-	#else
27-	char path[64], id[32];
28+	drmDevicePtr device = NULL;
29 	uint32_t vendor_id, device_id;
30-	char *path_part;
31-	struct stat st;
32-	FILE *file;
33 	uint32_t index;
34-	int n;
35+	const struct drm_driver *driver = NULL;
36 
37-	if (fstat(fd, &st) == -1)
38+	if (drmGetDevice2(fd, 0, &device) != 0)
39 		return NULL;
40 
41-	n = snprintf(path, sizeof(path), "/sys/dev/char/%u:%u/device/", major(st.st_rdev), minor(st.st_rdev));
42-	if (n + 6 >= sizeof(path))
43-		return NULL;
44-	path_part = path + n;
45-
46-	strcpy(path_part, "vendor");
47-	file = fopen(path, "r");
48-	if (!file)
49-		return NULL;
50-	fgets(id, sizeof id, file);
51-	fclose(file);
52-	vendor_id = strtoul(id, NULL, 0);
53+	if (device->bustype != DRM_BUS_PCI || !device->deviceinfo.pci)
54+		goto out;
55 
56-	strcpy(path_part, "device");
57-	file = fopen(path, "r");
58-	if (!file)
59-		return NULL;
60-	fgets(id, sizeof id, file);
61-	fclose(file);
62-	device_id = strtoul(id, NULL, 0);
63+	vendor_id = device->deviceinfo.pci->vendor_id;
64+	device_id = device->deviceinfo.pci->device_id;
65 
66 	for (index = 0; index < ARRAY_LENGTH(drivers); ++index) {
67 		DEBUG("Trying DRM driver `%s'\n", drivers[index]->name);
68 		if (drivers[index]->device_supported(vendor_id, device_id))
69-			return drivers[index];
70+			driver = drivers[index];
71 	}
72 
73-	return NULL;
74-	#endif
75+out:
76+	drmFreeDevice(&device);
77+	return driver;
78 }
79 
80 EXPORT