diff mbox

[libdrm,5/5] xf86drm: implement an OpenBSD specific drmGetDevice

Message ID 20161126004034.53376-5-jsg@jsg.id.au (mailing list archive)
State New, archived
Headers show

Commit Message

Jonathan Gray Nov. 26, 2016, 12:40 a.m. UTC
This avoids walking all of /dev and directly maps the fd to a path to a
primary node.

Signed-off-by: Jonathan Gray <jsg@jsg.id.au>
---
 xf86drm.c | 41 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 41 insertions(+)

Comments

Emil Velikov Nov. 29, 2016, 8:03 p.m. UTC | #1
On 26 November 2016 at 00:40, Jonathan Gray <jsg@jsg.id.au> wrote:
> This avoids walking all of /dev and directly maps the fd to a path to a
> primary node.
>
I realise that the code is pretty ugly/bad/etc, but I would stay way
from similar optimisations. As-is it will just work as you guys get
support for render nodes/other.
That is unless things are noticeably slow [or bad in general].

Thanks
Emil
Jonathan Gray Nov. 30, 2016, midnight UTC | #2
On Tue, Nov 29, 2016 at 08:03:58PM +0000, Emil Velikov wrote:
> On 26 November 2016 at 00:40, Jonathan Gray <jsg@jsg.id.au> wrote:
> > This avoids walking all of /dev and directly maps the fd to a path to a
> > primary node.
> >
> I realise that the code is pretty ugly/bad/etc, but I would stay way
> from similar optimisations. As-is it will just work as you guys get
> support for render nodes/other.
> That is unless things are noticeably slow [or bad in general].
> 
> Thanks
> Emil

/dev/ has 1200 files on a machine here, drm nodes aren't in a drm
specific directory as on linux.
Emil Velikov Nov. 30, 2016, 4:32 p.m. UTC | #3
On 30 November 2016 at 00:00, Jonathan Gray <jsg@jsg.id.au> wrote:
> On Tue, Nov 29, 2016 at 08:03:58PM +0000, Emil Velikov wrote:
>> On 26 November 2016 at 00:40, Jonathan Gray <jsg@jsg.id.au> wrote:
>> > This avoids walking all of /dev and directly maps the fd to a path to a
>> > primary node.
>> >
>> I realise that the code is pretty ugly/bad/etc, but I would stay way
>> from similar optimisations. As-is it will just work as you guys get
>> support for render nodes/other.
>> That is unless things are noticeably slow [or bad in general].
>>
>> Thanks
>> Emil
>
> /dev/ has 1200 files on a machine here, drm nodes aren't in a drm
> specific directory as on linux.
Eeek ...1200, there's only ~160 over here.

Is it against OpenBSD policy/philosophy to nest things (using
sub-folders), a matter of carefully updating this to avoid breakage
(shortage to time/manpower), or it's mostly a matter of personal taste
?

Regardless of the reason, please include your comment in the code.
Also please mention that this works only for card nodes.
And don't forget the (dummy) drmParseSubsystemType call.

Thanks
Emil
diff mbox

Patch

diff --git a/xf86drm.c b/xf86drm.c
index 2a60b2e..a4b2506 100644
--- a/xf86drm.c
+++ b/xf86drm.c
@@ -3175,6 +3175,46 @@  static void drmFoldDuplicatedDevices(drmDevicePtr local_devices[], int count)
  */
 int drmGetDevice(int fd, drmDevicePtr *device)
 {
+#ifdef __OpenBSD__
+    drmDevicePtr d;
+    struct stat sbuf;
+    char node[PATH_MAX + 1];
+    char d_name[PATH_MAX + 1];
+    int maj, min, n;
+    int ret;
+    int max_count = 1;
+
+    if (fd == -1 || device == NULL)
+        return -EINVAL;
+
+    if (fstat(fd, &sbuf))
+        return -errno;
+
+    maj = major(sbuf.st_rdev);
+    min = minor(sbuf.st_rdev);
+
+    if (maj != DRM_MAJOR || !S_ISCHR(sbuf.st_mode))
+        return -EINVAL;
+
+    n = snprintf(d_name, PATH_MAX, "drm%d", min);
+    if (n == -1 || n >= PATH_MAX)
+      return -errno;
+
+    n = snprintf(node, PATH_MAX, DRM_DEV_NAME, DRM_DIR_NAME, min);
+    if (n == -1 || n >= PATH_MAX)
+      return -errno;
+    if (stat(node, &sbuf))
+        return -EINVAL;
+
+    ret = drmProcessPciDevice(&d, d_name, node, DRM_NODE_PRIMARY,
+			      maj, min, true);
+    if (ret)
+        return ret;
+
+    *device = d;
+
+    return 0;
+#else
     drmDevicePtr *local_devices;
     drmDevicePtr d;
     DIR *sysdir;
@@ -3282,6 +3322,7 @@  free_devices:
 free_locals:
     free(local_devices);
     return ret;
+#endif
 }
 
 /**