* [RFC PATCH 0/2] libdrm: Add master <> render node helpers
@ 2015-02-02 0:14 Emil Velikov
2015-02-02 0:14 ` [PATCH 1/2] drm: add drmGet{Device, Render}NameFrom{Render, Device}Fd helpers Emil Velikov
` (2 more replies)
0 siblings, 3 replies; 16+ messages in thread
From: Emil Velikov @ 2015-02-02 0:14 UTC (permalink / raw)
To: dri-devel
Cc: Mark Kettenis, François Tigeot, emil.l.velikov,
Thomas Klausner, Robert Millan, Konstantin Belousov
Hi all,
As mentioned a couple of days ago at #dri-devel some(most) users of
render nodes tend to rely on strict mapping between the primary and
render node. I.e. something along the lines of
fstat(render_fd, &sbuf);
sprintf(primary_node, "/dev/dri/card%d",
((sbuf.st_rdev & 0x3f) | 0x80));
Currently the following are (ab)using the above code:
- xf86-video-nouveau
- xf86-video-intel
- libva (vaapi)
As reminded by David Herrman, this is not the correct solution - thus
I've added a couple of helpers which walk through sysfs of the
respecitive device and return the correct device name.
I'm not 100% happy with the function names, so suggestions are greatly
appreciated. Any other comments are also welcome :)
Note: BSD guys - you'll likely need your own version of these functions.
Cheers,
Emil
Cc: Thomas Klausner <wiz@NetBSD.org>
Cc: Konstantin Belousov <kib@freebsd.org>
Cc: Robert Millan <rmh@freebsd.org>
Cc: Mark Kettenis <kettenis@openbsd.org>
Cc: Jonathan Gray <jsg@jsg.id.au>
Cc: François Tigeot <ftigeot@wolfpond.org>
_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
http://lists.freedesktop.org/mailman/listinfo/dri-devel
^ permalink raw reply [flat|nested] 16+ messages in thread
* [PATCH 1/2] drm: add drmGet{Device, Render}NameFrom{Render, Device}Fd helpers
2015-02-02 0:14 [RFC PATCH 0/2] libdrm: Add master <> render node helpers Emil Velikov
@ 2015-02-02 0:14 ` Emil Velikov
2015-02-13 11:07 ` Frank Binns
2015-02-23 12:22 ` [PATCH libdrm v2] drm: add drmGet(Master|Render)NameFrom(Render|Master)FD functions Emil Velikov
2015-02-02 0:14 ` [PATCH 2/2] libdrm: add drmGetNodeType() helper Emil Velikov
2015-02-10 22:37 ` [RFC PATCH 0/2] libdrm: Add master <> render node helpers Emil Velikov
2 siblings, 2 replies; 16+ messages in thread
From: Emil Velikov @ 2015-02-02 0:14 UTC (permalink / raw)
To: dri-devel; +Cc: David Herrmann, Daniel Vetter, emil.l.velikov
Currently most places assume reliable primary <> render node mapping.
Although this may work in some cases, it is not correct.
Add a couple of helpers that hide the details and safes all the
guesswork for the user.
Cc: Daniel Vetter <daniel.vetter@ffwll.ch>
Cc: David Herrmann <dh.herrmann@googlemail.com>
Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
---
xf86drm.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
xf86drm.h | 3 +++
2 files changed, 57 insertions(+)
diff --git a/xf86drm.c b/xf86drm.c
index 345325a..6af7ac0 100644
--- a/xf86drm.c
+++ b/xf86drm.c
@@ -2607,3 +2607,57 @@ int drmPrimeFDToHandle(int fd, int prime_fd, uint32_t *handle)
return 0;
}
+char *drmGetDeviceNameFromRenderFD(int fd)
+{
+
+ struct stat sbuf;
+ char name[64], buf[64];
+ int maj, min, i;
+
+ if (!fstat(fd, &sbuf))
+ return NULL;
+
+ if (!S_ISCHR(sbuf.st_mode))
+ return NULL;
+
+ maj = major(sbuf.st_rdev);
+ min = minor(sbuf.st_rdev);
+
+ snprintf(buf, sizeof(buf), "/sys/dev/char/%d:%d/device/drm", maj, min);
+
+ for (i = 0; i < DRM_MAX_MINOR; i++) {
+ snprintf(name, sizeof(name), DRM_DEV_NAME, buf, i);
+ if (stat(name, &sbuf) == 0) {
+ snprintf(name, sizeof(name), DRM_DEV_NAME, DRM_DIR_NAME, i);
+ return strdup(name);
+ }
+ }
+ return NULL;
+}
+
+char *drmGetRenderNameFromDeviceFD(int fd)
+{
+ struct stat sbuf;
+ char name[64], buf[64];
+ int maj, min, i;
+
+ if (!fstat(fd, &sbuf))
+ return NULL;
+
+ if (!S_ISCHR(sbuf.st_mode))
+ return NULL;
+
+ maj = major(sbuf.st_rdev);
+ min = minor(sbuf.st_rdev);
+
+ snprintf(buf, sizeof(buf), "/sys/dev/char/%d:%d/device/drm", maj, min);
+
+ for (i = 128; i < (128 + DRM_MAX_MINOR); i++) {
+ snprintf(name, sizeof(name), DRM_DEV_NAME, buf, i);
+ if (stat(name, &sbuf) == 0) {
+ snprintf(name, sizeof(name), DRM_RENDER_DEV_NAME, DRM_DIR_NAME, i);
+ return strdup(name);
+ }
+ }
+ return NULL;
+}
diff --git a/xf86drm.h b/xf86drm.h
index bfd0670..bca5887 100644
--- a/xf86drm.h
+++ b/xf86drm.h
@@ -740,6 +740,9 @@ extern char *drmGetDeviceNameFromFd(int fd);
extern int drmPrimeHandleToFD(int fd, uint32_t handle, uint32_t flags, int *prime_fd);
extern int drmPrimeFDToHandle(int fd, int prime_fd, uint32_t *handle);
+extern char *drmGetRenderNameFromDeviceFD(int fd);
+extern char *drmGetDeviceNameFromRenderFD(int fd);
+
#if defined(__cplusplus) || defined(c_plusplus)
}
#endif
--
2.2.2
_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
http://lists.freedesktop.org/mailman/listinfo/dri-devel
^ permalink raw reply related [flat|nested] 16+ messages in thread
* [PATCH 2/2] libdrm: add drmGetNodeType() helper
2015-02-02 0:14 [RFC PATCH 0/2] libdrm: Add master <> render node helpers Emil Velikov
2015-02-02 0:14 ` [PATCH 1/2] drm: add drmGet{Device, Render}NameFrom{Render, Device}Fd helpers Emil Velikov
@ 2015-02-02 0:14 ` Emil Velikov
2015-02-13 10:50 ` Frank Binns
2015-02-10 22:37 ` [RFC PATCH 0/2] libdrm: Add master <> render node helpers Emil Velikov
2 siblings, 1 reply; 16+ messages in thread
From: Emil Velikov @ 2015-02-02 0:14 UTC (permalink / raw)
To: dri-devel; +Cc: David Herrmann, Daniel Vetter, emil.l.velikov
The add a simple helper which returns the node type of the opened fd.
Likely to be used in conjunction with the previous two helpers.
Cc: Daniel Vetter <daniel.vetter@ffwll.ch>
Cc: David Herrmann <dh.herrmann@googlemail.com>
Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
---
xf86drm.c | 43 +++++++++++++++++++++++++++++++++++++++----
xf86drm.h | 7 +++++++
2 files changed, 46 insertions(+), 4 deletions(-)
diff --git a/xf86drm.c b/xf86drm.c
index 6af7ac0..a70c0dd 100644
--- a/xf86drm.c
+++ b/xf86drm.c
@@ -85,10 +85,6 @@
#define DRM_MSG_VERBOSITY 3
-#define DRM_NODE_CONTROL 0
-#define DRM_NODE_PRIMARY 1
-#define DRM_NODE_RENDER 2
-
static drmServerInfoPtr drm_server_info;
void drmSetServerInfo(drmServerInfoPtr info)
@@ -2607,6 +2603,45 @@ int drmPrimeFDToHandle(int fd, int prime_fd, uint32_t *handle)
return 0;
}
+int drmGetNodeType(int fd, drmNodeType *type)
+{
+ struct stat sbuf;
+ char name[64];
+ dev_t d;
+ int i;
+
+ if (!fstat(fd, &sbuf))
+ return -errno;
+
+ d = sbuf.st_rdev;
+
+ for (i = 0; i < DRM_MAX_MINOR; i++) {
+ snprintf(name, sizeof(name), DRM_DEV_NAME, DRM_DIR_NAME, i);
+ if (stat(name, &sbuf) == 0 && sbuf.st_rdev == d) {
+ *type = DRM_NODE_PRIMARY;
+ return 0;
+ }
+ }
+
+ for (i = 64; i < (64 + DRM_MAX_MINOR); i++) {
+ snprintf(name, sizeof(name), DRM_CONTROL_DEV_NAME, DRM_DIR_NAME, i);
+ if (stat(name, &sbuf) == 0 && sbuf.st_rdev == d) {
+ *type = DRM_NODE_CONTROL;
+ return 0;
+ }
+ }
+
+ for (i = 128; i < (128 + DRM_MAX_MINOR); i++) {
+ snprintf(name, sizeof(name), DRM_RENDER_DEV_NAME, DRM_DIR_NAME, i);
+ if (stat(name, &sbuf) == 0 && sbuf.st_rdev == d) {
+ *type = DRM_NODE_RENDER;
+ return 0;
+ }
+ }
+
+ return -EINVAL;
+}
+
char *drmGetDeviceNameFromRenderFD(int fd)
{
diff --git a/xf86drm.h b/xf86drm.h
index bca5887..7d67df9 100644
--- a/xf86drm.h
+++ b/xf86drm.h
@@ -740,6 +740,13 @@ extern char *drmGetDeviceNameFromFd(int fd);
extern int drmPrimeHandleToFD(int fd, uint32_t handle, uint32_t flags, int *prime_fd);
extern int drmPrimeFDToHandle(int fd, int prime_fd, uint32_t *handle);
+typedef enum _drmNodeType {
+ DRM_NODE_CONTROL = 0,
+ DRM_NODE_PRIMARY = 1,
+ DRM_NODE_RENDER = 2
+} drmNodeType, *drmNodeTypePtr;
+
+extern int drmGetNodeType(int fd, drmNodeType *type);
extern char *drmGetRenderNameFromDeviceFD(int fd);
extern char *drmGetDeviceNameFromRenderFD(int fd);
--
2.2.2
_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
http://lists.freedesktop.org/mailman/listinfo/dri-devel
^ permalink raw reply related [flat|nested] 16+ messages in thread
* Re: [RFC PATCH 0/2] libdrm: Add master <> render node helpers
2015-02-02 0:14 [RFC PATCH 0/2] libdrm: Add master <> render node helpers Emil Velikov
2015-02-02 0:14 ` [PATCH 1/2] drm: add drmGet{Device, Render}NameFrom{Render, Device}Fd helpers Emil Velikov
2015-02-02 0:14 ` [PATCH 2/2] libdrm: add drmGetNodeType() helper Emil Velikov
@ 2015-02-10 22:37 ` Emil Velikov
2015-02-10 23:32 ` Jonathan Gray
2015-02-12 18:21 ` David Herrmann
2 siblings, 2 replies; 16+ messages in thread
From: Emil Velikov @ 2015-02-10 22:37 UTC (permalink / raw)
To: dri-devel
Cc: Mark Kettenis, François Tigeot, emil.l.velikov,
Thomas Klausner, Robert Millan, Konstantin Belousov
On 02/02/15 00:14, Emil Velikov wrote:
> Hi all,
>
> As mentioned a couple of days ago at #dri-devel some(most) users of
> render nodes tend to rely on strict mapping between the primary and
> render node. I.e. something along the lines of
>
> fstat(render_fd, &sbuf);
> sprintf(primary_node, "/dev/dri/card%d",
> ((sbuf.st_rdev & 0x3f) | 0x80));
>
> Currently the following are (ab)using the above code:
> - xf86-video-nouveau
> - xf86-video-intel
> - libva (vaapi)
>
> As reminded by David Herrman, this is not the correct solution - thus
> I've added a couple of helpers which walk through sysfs of the
> respecitive device and return the correct device name.
>
> I'm not 100% happy with the function names, so suggestions are greatly
> appreciated. Any other comments are also welcome :)
>
> Note: BSD guys - you'll likely need your own version of these functions.
>
David, Daniel
Can you please take a look at these two patches. Would be great if we
can minimize the above assumptions before they get too wide spread.
*BSD guys,
Please take a look and/or forward this to the relevant people. I fear
that my *BSD knowledge is virtually none :\
Cheers,
Emil
_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
http://lists.freedesktop.org/mailman/listinfo/dri-devel
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [RFC PATCH 0/2] libdrm: Add master <> render node helpers
2015-02-10 22:37 ` [RFC PATCH 0/2] libdrm: Add master <> render node helpers Emil Velikov
@ 2015-02-10 23:32 ` Jonathan Gray
2015-02-12 18:21 ` David Herrmann
1 sibling, 0 replies; 16+ messages in thread
From: Jonathan Gray @ 2015-02-10 23:32 UTC (permalink / raw)
To: Emil Velikov
Cc: Mark Kettenis, François Tigeot, dri-devel,
Konstantin Belousov, Robert Millan, Thomas Klausner
On Tue, Feb 10, 2015 at 10:37:16PM +0000, Emil Velikov wrote:
> On 02/02/15 00:14, Emil Velikov wrote:
> > Hi all,
> >
> > As mentioned a couple of days ago at #dri-devel some(most) users of
> > render nodes tend to rely on strict mapping between the primary and
> > render node. I.e. something along the lines of
> >
> > fstat(render_fd, &sbuf);
> > sprintf(primary_node, "/dev/dri/card%d",
> > ((sbuf.st_rdev & 0x3f) | 0x80));
> >
> > Currently the following are (ab)using the above code:
> > - xf86-video-nouveau
> > - xf86-video-intel
> > - libva (vaapi)
> >
> > As reminded by David Herrman, this is not the correct solution - thus
> > I've added a couple of helpers which walk through sysfs of the
> > respecitive device and return the correct device name.
> >
> > I'm not 100% happy with the function names, so suggestions are greatly
> > appreciated. Any other comments are also welcome :)
> >
> > Note: BSD guys - you'll likely need your own version of these functions.
> >
> David, Daniel
>
> Can you please take a look at these two patches. Would be great if we
> can minimize the above assumptions before they get too wide spread.
>
> *BSD guys,
>
> Please take a look and/or forward this to the relevant people. I fear
> that my *BSD knowledge is virtually none :\
>
> Cheers,
> Emil
I can't speak for other projects but OpenBSD drm is currently based
on Linux 3.8 with no render nodes or prime.
There is no sysfs/udev etc so when/if render nodes work it will
likely involve either a fixed mapping or ioctls.
_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
http://lists.freedesktop.org/mailman/listinfo/dri-devel
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [RFC PATCH 0/2] libdrm: Add master <> render node helpers
2015-02-10 22:37 ` [RFC PATCH 0/2] libdrm: Add master <> render node helpers Emil Velikov
2015-02-10 23:32 ` Jonathan Gray
@ 2015-02-12 18:21 ` David Herrmann
2015-02-13 9:18 ` Emil Velikov
1 sibling, 1 reply; 16+ messages in thread
From: David Herrmann @ 2015-02-12 18:21 UTC (permalink / raw)
To: Emil Velikov
Cc: Mark Kettenis, François Tigeot, dri-devel,
Konstantin Belousov, Robert Millan, Thomas Klausner
Hi
On Tue, Feb 10, 2015 at 11:37 PM, Emil Velikov <emil.l.velikov@gmail.com> wrote:
> On 02/02/15 00:14, Emil Velikov wrote:
>> Hi all,
>>
>> As mentioned a couple of days ago at #dri-devel some(most) users of
>> render nodes tend to rely on strict mapping between the primary and
>> render node. I.e. something along the lines of
>>
>> fstat(render_fd, &sbuf);
>> sprintf(primary_node, "/dev/dri/card%d",
>> ((sbuf.st_rdev & 0x3f) | 0x80));
>>
>> Currently the following are (ab)using the above code:
>> - xf86-video-nouveau
>> - xf86-video-intel
>> - libva (vaapi)
>>
>> As reminded by David Herrman, this is not the correct solution - thus
>> I've added a couple of helpers which walk through sysfs of the
>> respecitive device and return the correct device name.
>>
>> I'm not 100% happy with the function names, so suggestions are greatly
>> appreciated. Any other comments are also welcome :)
>>
>> Note: BSD guys - you'll likely need your own version of these functions.
>>
> David, Daniel
>
> Can you please take a look at these two patches. Would be great if we
> can minimize the above assumptions before they get too wide spread.
The patches look ok. I really dislike the direct file-probing, though.
I'd recommend using libudev or readdir_r() on /dev/dri/, but I'm not
the one to ask about coding-style for libdrm, so fine with me.
Thanks
David
_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
http://lists.freedesktop.org/mailman/listinfo/dri-devel
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [RFC PATCH 0/2] libdrm: Add master <> render node helpers
2015-02-12 18:21 ` David Herrmann
@ 2015-02-13 9:18 ` Emil Velikov
2015-02-13 11:18 ` Frank Binns
0 siblings, 1 reply; 16+ messages in thread
From: Emil Velikov @ 2015-02-13 9:18 UTC (permalink / raw)
To: David Herrmann
Cc: Mark Kettenis, François Tigeot, emil.l.velikov, dri-devel,
Thomas Klausner, Robert Millan, Konstantin Belousov
On 12/02/15 18:21, David Herrmann wrote:
> Hi
>
> On Tue, Feb 10, 2015 at 11:37 PM, Emil Velikov <emil.l.velikov@gmail.com> wrote:
>> On 02/02/15 00:14, Emil Velikov wrote:
>>> Hi all,
>>>
>>> As mentioned a couple of days ago at #dri-devel some(most) users of
>>> render nodes tend to rely on strict mapping between the primary and
>>> render node. I.e. something along the lines of
>>>
>>> fstat(render_fd, &sbuf);
>>> sprintf(primary_node, "/dev/dri/card%d",
>>> ((sbuf.st_rdev & 0x3f) | 0x80));
>>>
>>> Currently the following are (ab)using the above code:
>>> - xf86-video-nouveau
>>> - xf86-video-intel
>>> - libva (vaapi)
>>>
>>> As reminded by David Herrman, this is not the correct solution - thus
>>> I've added a couple of helpers which walk through sysfs of the
>>> respecitive device and return the correct device name.
>>>
>>> I'm not 100% happy with the function names, so suggestions are greatly
>>> appreciated. Any other comments are also welcome :)
>>>
>>> Note: BSD guys - you'll likely need your own version of these functions.
>>>
>> David, Daniel
>>
>> Can you please take a look at these two patches. Would be great if we
>> can minimize the above assumptions before they get too wide spread.
>
> The patches look ok. I really dislike the direct file-probing, though.
> I'd recommend using libudev or readdir_r() on /dev/dri/, but I'm not
> the one to ask about coding-style for libdrm, so fine with me.
>
Thanks for having a look. libudev is no used in libdrm so I will stay
away for now. I would prefer to use readdir on /dev/dri/ but AFAICT
there is no way to get the mapping from those alone. Am I missing
something ?
I'm also thinking that changing the prototypes, as below, might be
useful - i.e. return 0 on success, -ENODEV when the corresponding device
lacks render/primary device, and other negative value on error ?
char *drmGetRenderNameFromDeviceFD(int fd);
char *drmGetDeviceNameFromRenderFD(int fd);
int drmGetRenderNameFromDeviceFD(int fd, char **render);
int drmGetDeviceNameFromRenderFD(int fd, char **device);
Cheers,
Emil
_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
http://lists.freedesktop.org/mailman/listinfo/dri-devel
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [PATCH 2/2] libdrm: add drmGetNodeType() helper
2015-02-02 0:14 ` [PATCH 2/2] libdrm: add drmGetNodeType() helper Emil Velikov
@ 2015-02-13 10:50 ` Frank Binns
0 siblings, 0 replies; 16+ messages in thread
From: Frank Binns @ 2015-02-13 10:50 UTC (permalink / raw)
To: Emil Velikov, dri-devel; +Cc: David Herrmann, Daniel Vetter
Hi Emil,
On 02/02/15 00:14, Emil Velikov wrote:
> The add a simple helper which returns the node type of the opened fd.
> Likely to be used in conjunction with the previous two helpers.
>
> Cc: Daniel Vetter <daniel.vetter@ffwll.ch>
> Cc: David Herrmann <dh.herrmann@googlemail.com>
> Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
> ---
> xf86drm.c | 43 +++++++++++++++++++++++++++++++++++++++----
> xf86drm.h | 7 +++++++
> 2 files changed, 46 insertions(+), 4 deletions(-)
>
> diff --git a/xf86drm.c b/xf86drm.c
> index 6af7ac0..a70c0dd 100644
> --- a/xf86drm.c
> +++ b/xf86drm.c
> @@ -85,10 +85,6 @@
>
> #define DRM_MSG_VERBOSITY 3
>
> -#define DRM_NODE_CONTROL 0
> -#define DRM_NODE_PRIMARY 1
> -#define DRM_NODE_RENDER 2
> -
> static drmServerInfoPtr drm_server_info;
>
> void drmSetServerInfo(drmServerInfoPtr info)
> @@ -2607,6 +2603,45 @@ int drmPrimeFDToHandle(int fd, int prime_fd, uint32_t *handle)
> return 0;
> }
>
> +int drmGetNodeType(int fd, drmNodeType *type)
> +{
> + struct stat sbuf;
> + char name[64];
> + dev_t d;
> + int i;
> +
> + if (!fstat(fd, &sbuf))
> + return -errno;
> +
> + d = sbuf.st_rdev;
> +
> + for (i = 0; i < DRM_MAX_MINOR; i++) {
> + snprintf(name, sizeof(name), DRM_DEV_NAME, DRM_DIR_NAME, i);
> + if (stat(name, &sbuf) == 0 && sbuf.st_rdev == d) {
> + *type = DRM_NODE_PRIMARY;
> + return 0;
> + }
> + }
> +
> + for (i = 64; i < (64 + DRM_MAX_MINOR); i++) {
> + snprintf(name, sizeof(name), DRM_CONTROL_DEV_NAME, DRM_DIR_NAME, i);
> + if (stat(name, &sbuf) == 0 && sbuf.st_rdev == d) {
> + *type = DRM_NODE_CONTROL;
> + return 0;
> + }
> + }
> +
> + for (i = 128; i < (128 + DRM_MAX_MINOR); i++) {
> + snprintf(name, sizeof(name), DRM_RENDER_DEV_NAME, DRM_DIR_NAME, i);
> + if (stat(name, &sbuf) == 0 && sbuf.st_rdev == d) {
> + *type = DRM_NODE_RENDER;
> + return 0;
> + }
> + }
It would seem easier (and faster) to use minor(sbuf.st_rdev) to
determine the type of node.
I'm not sure what the etiquette is here but I've prepared a patch that
does it this way instead, which will follow this email. Please feel free
to add your own signed-off-by, suggested-by, etc as I don't want to
steal all your credit :)
Thanks
Frank
> +
> + return -EINVAL;
> +}
> +
> char *drmGetDeviceNameFromRenderFD(int fd)
> {
>
> diff --git a/xf86drm.h b/xf86drm.h
> index bca5887..7d67df9 100644
> --- a/xf86drm.h
> +++ b/xf86drm.h
> @@ -740,6 +740,13 @@ extern char *drmGetDeviceNameFromFd(int fd);
> extern int drmPrimeHandleToFD(int fd, uint32_t handle, uint32_t flags, int *prime_fd);
> extern int drmPrimeFDToHandle(int fd, int prime_fd, uint32_t *handle);
>
> +typedef enum _drmNodeType {
> + DRM_NODE_CONTROL = 0,
> + DRM_NODE_PRIMARY = 1,
> + DRM_NODE_RENDER = 2
> +} drmNodeType, *drmNodeTypePtr;
> +
> +extern int drmGetNodeType(int fd, drmNodeType *type);
> extern char *drmGetRenderNameFromDeviceFD(int fd);
> extern char *drmGetDeviceNameFromRenderFD(int fd);
>
_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
http://lists.freedesktop.org/mailman/listinfo/dri-devel
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [PATCH 1/2] drm: add drmGet{Device, Render}NameFrom{Render, Device}Fd helpers
2015-02-02 0:14 ` [PATCH 1/2] drm: add drmGet{Device, Render}NameFrom{Render, Device}Fd helpers Emil Velikov
@ 2015-02-13 11:07 ` Frank Binns
2015-02-23 12:22 ` [PATCH libdrm v2] drm: add drmGet(Master|Render)NameFrom(Render|Master)FD functions Emil Velikov
1 sibling, 0 replies; 16+ messages in thread
From: Frank Binns @ 2015-02-13 11:07 UTC (permalink / raw)
To: Emil Velikov, dri-devel; +Cc: David Herrmann, Daniel Vetter
Hi Emil,
On 02/02/15 00:14, Emil Velikov wrote:
> Currently most places assume reliable primary <> render node mapping.
> Although this may work in some cases, it is not correct.
>
> Add a couple of helpers that hide the details and safes all the
> guesswork for the user.
>
> Cc: Daniel Vetter <daniel.vetter@ffwll.ch>
> Cc: David Herrmann <dh.herrmann@googlemail.com>
> Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
> ---
> xf86drm.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
> xf86drm.h | 3 +++
> 2 files changed, 57 insertions(+)
>
> diff --git a/xf86drm.c b/xf86drm.c
> index 345325a..6af7ac0 100644
> --- a/xf86drm.c
> +++ b/xf86drm.c
> @@ -2607,3 +2607,57 @@ int drmPrimeFDToHandle(int fd, int prime_fd, uint32_t *handle)
> return 0;
> }
>
> +char *drmGetDeviceNameFromRenderFD(int fd)
> +{
> +
> + struct stat sbuf;
> + char name[64], buf[64];
> + int maj, min, i;
> +
> + if (!fstat(fd, &sbuf))
> + return NULL;
> +
fstat returns 0 on success and -1 otherwise so this should be
if (fstat(fd, &sbuf))
return NULL;
> + if (!S_ISCHR(sbuf.st_mode))
> + return NULL;
> +
> + maj = major(sbuf.st_rdev);
> + min = minor(sbuf.st_rdev);
> +
> + snprintf(buf, sizeof(buf), "/sys/dev/char/%d:%d/device/drm", maj, min);
> +
> + for (i = 0; i < DRM_MAX_MINOR; i++) {
> + snprintf(name, sizeof(name), DRM_DEV_NAME, buf, i);
> + if (stat(name, &sbuf) == 0) {
> + snprintf(name, sizeof(name), DRM_DEV_NAME, DRM_DIR_NAME, i);
> + return strdup(name);
> + }
> + }
> + return NULL;
> +}
> +
> +char *drmGetRenderNameFromDeviceFD(int fd)
> +{
> + struct stat sbuf;
> + char name[64], buf[64];
> + int maj, min, i;
> +
> + if (!fstat(fd, &sbuf))
> + return NULL;
> +
Same here.
> + if (!S_ISCHR(sbuf.st_mode))
> + return NULL;
> +
> + maj = major(sbuf.st_rdev);
> + min = minor(sbuf.st_rdev);
> +
> + snprintf(buf, sizeof(buf), "/sys/dev/char/%d:%d/device/drm", maj, min);
> +
> + for (i = 128; i < (128 + DRM_MAX_MINOR); i++) {
> + snprintf(name, sizeof(name), DRM_DEV_NAME, buf, i);
> + if (stat(name, &sbuf) == 0) {
> + snprintf(name, sizeof(name), DRM_RENDER_DEV_NAME, DRM_DIR_NAME, i);
> + return strdup(name);
> + }
> + }
> + return NULL;
> +}
> diff --git a/xf86drm.h b/xf86drm.h
> index bfd0670..bca5887 100644
> --- a/xf86drm.h
> +++ b/xf86drm.h
> @@ -740,6 +740,9 @@ extern char *drmGetDeviceNameFromFd(int fd);
> extern int drmPrimeHandleToFD(int fd, uint32_t handle, uint32_t flags, int *prime_fd);
> extern int drmPrimeFDToHandle(int fd, int prime_fd, uint32_t *handle);
>
> +extern char *drmGetRenderNameFromDeviceFD(int fd);
> +extern char *drmGetDeviceNameFromRenderFD(int fd);
> +
> #if defined(__cplusplus) || defined(c_plusplus)
> }
> #endif
_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
http://lists.freedesktop.org/mailman/listinfo/dri-devel
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [RFC PATCH 0/2] libdrm: Add master <> render node helpers
2015-02-13 9:18 ` Emil Velikov
@ 2015-02-13 11:18 ` Frank Binns
2015-02-13 18:19 ` Emil Velikov
0 siblings, 1 reply; 16+ messages in thread
From: Frank Binns @ 2015-02-13 11:18 UTC (permalink / raw)
To: Emil Velikov, David Herrmann
Cc: Mark Kettenis, François Tigeot, dri-devel,
Konstantin Belousov, Robert Millan, Thomas Klausner
Hi Emil,
On 13/02/15 09:18, Emil Velikov wrote:
> On 12/02/15 18:21, David Herrmann wrote:
>> Hi
>>
>> On Tue, Feb 10, 2015 at 11:37 PM, Emil Velikov <emil.l.velikov@gmail.com> wrote:
>>> On 02/02/15 00:14, Emil Velikov wrote:
>>>> Hi all,
>>>>
>>>> As mentioned a couple of days ago at #dri-devel some(most) users of
>>>> render nodes tend to rely on strict mapping between the primary and
>>>> render node. I.e. something along the lines of
>>>>
>>>> fstat(render_fd, &sbuf);
>>>> sprintf(primary_node, "/dev/dri/card%d",
>>>> ((sbuf.st_rdev & 0x3f) | 0x80));
>>>>
>>>> Currently the following are (ab)using the above code:
>>>> - xf86-video-nouveau
>>>> - xf86-video-intel
>>>> - libva (vaapi)
>>>>
>>>> As reminded by David Herrman, this is not the correct solution - thus
>>>> I've added a couple of helpers which walk through sysfs of the
>>>> respecitive device and return the correct device name.
>>>>
>>>> I'm not 100% happy with the function names, so suggestions are greatly
>>>> appreciated. Any other comments are also welcome :)
>>>>
>>>> Note: BSD guys - you'll likely need your own version of these functions.
>>>>
>>> David, Daniel
>>>
>>> Can you please take a look at these two patches. Would be great if we
>>> can minimize the above assumptions before they get too wide spread.
>> The patches look ok. I really dislike the direct file-probing, though.
>> I'd recommend using libudev or readdir_r() on /dev/dri/, but I'm not
>> the one to ask about coding-style for libdrm, so fine with me.
>>
> Thanks for having a look. libudev is no used in libdrm so I will stay
> away for now. I would prefer to use readdir on /dev/dri/ but AFAICT
> there is no way to get the mapping from those alone. Am I missing
> something ?
Can you you not just use use readdir on /dev/dri and stat on each
entry's d_name comparing the result to an fstat on the fd? Once you find
a match you just
strdup the d_name.
>
> I'm also thinking that changing the prototypes, as below, might be
> useful - i.e. return 0 on success, -ENODEV when the corresponding device
> lacks render/primary device, and other negative value on error ?
>
> char *drmGetRenderNameFromDeviceFD(int fd);
> char *drmGetDeviceNameFromRenderFD(int fd);
I personally prefer this as it fits in better with what we already have.
>
> int drmGetRenderNameFromDeviceFD(int fd, char **render);
> int drmGetDeviceNameFromRenderFD(int fd, char **device);
>
>
> Cheers,
> Emil
>
> _______________________________________________
> dri-devel mailing list
> dri-devel@lists.freedesktop.org
> http://lists.freedesktop.org/mailman/listinfo/dri-devel
_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
http://lists.freedesktop.org/mailman/listinfo/dri-devel
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [RFC PATCH 0/2] libdrm: Add master <> render node helpers
2015-02-13 11:18 ` Frank Binns
@ 2015-02-13 18:19 ` Emil Velikov
0 siblings, 0 replies; 16+ messages in thread
From: Emil Velikov @ 2015-02-13 18:19 UTC (permalink / raw)
To: Frank Binns, David Herrmann
Cc: Mark Kettenis, François Tigeot, emil.l.velikov, dri-devel,
Thomas Klausner, Robert Millan, Konstantin Belousov
On 13/02/15 11:18, Frank Binns wrote:
> Hi Emil,
>
> On 13/02/15 09:18, Emil Velikov wrote:
>> On 12/02/15 18:21, David Herrmann wrote:
>>> Hi
>>>
>>> On Tue, Feb 10, 2015 at 11:37 PM, Emil Velikov <emil.l.velikov@gmail.com> wrote:
>>>> On 02/02/15 00:14, Emil Velikov wrote:
>>>>> Hi all,
>>>>>
>>>>> As mentioned a couple of days ago at #dri-devel some(most) users of
>>>>> render nodes tend to rely on strict mapping between the primary and
>>>>> render node. I.e. something along the lines of
>>>>>
>>>>> fstat(render_fd, &sbuf);
>>>>> sprintf(primary_node, "/dev/dri/card%d",
>>>>> ((sbuf.st_rdev & 0x3f) | 0x80));
>>>>>
>>>>> Currently the following are (ab)using the above code:
>>>>> - xf86-video-nouveau
>>>>> - xf86-video-intel
>>>>> - libva (vaapi)
>>>>>
>>>>> As reminded by David Herrman, this is not the correct solution - thus
>>>>> I've added a couple of helpers which walk through sysfs of the
>>>>> respecitive device and return the correct device name.
>>>>>
>>>>> I'm not 100% happy with the function names, so suggestions are greatly
>>>>> appreciated. Any other comments are also welcome :)
>>>>>
>>>>> Note: BSD guys - you'll likely need your own version of these functions.
>>>>>
>>>> David, Daniel
>>>>
>>>> Can you please take a look at these two patches. Would be great if we
>>>> can minimize the above assumptions before they get too wide spread.
>>> The patches look ok. I really dislike the direct file-probing, though.
>>> I'd recommend using libudev or readdir_r() on /dev/dri/, but I'm not
>>> the one to ask about coding-style for libdrm, so fine with me.
>>>
>> Thanks for having a look. libudev is no used in libdrm so I will stay
>> away for now. I would prefer to use readdir on /dev/dri/ but AFAICT
>> there is no way to get the mapping from those alone. Am I missing
>> something ?
> Can you you not just use use readdir on /dev/dri and stat on each
> entry's d_name comparing the result to an fstat on the fd?
Unless I've misunderstood something, a primary device will have
226:{0-63}, while render device 226:{128-191}.
One cannot just mask out the 7th bit, as this is the (wrong) solution
what I've mentioned above. Or maybe I'm missing something ?
Afaict the information/"link" between the two is present at the drm
module level - thus the ugly sysfs dance.
> Once you find a match you just strdup the d_name.
>
>>
>> I'm also thinking that changing the prototypes, as below, might be
>> useful - i.e. return 0 on success, -ENODEV when the corresponding device
>> lacks render/primary device, and other negative value on error ?
>>
>> char *drmGetRenderNameFromDeviceFD(int fd);
>> char *drmGetDeviceNameFromRenderFD(int fd);
> I personally prefer this as it fits in better with what we already have.
>
The recent inspiration came from the prime helpers. I don't feel
strongly either way.
Cheers,
Emil
_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
http://lists.freedesktop.org/mailman/listinfo/dri-devel
^ permalink raw reply [flat|nested] 16+ messages in thread
* [PATCH libdrm v2] drm: add drmGet(Master|Render)NameFrom(Render|Master)FD functions
2015-02-02 0:14 ` [PATCH 1/2] drm: add drmGet{Device, Render}NameFrom{Render, Device}Fd helpers Emil Velikov
2015-02-13 11:07 ` Frank Binns
@ 2015-02-23 12:22 ` Emil Velikov
2015-02-23 14:35 ` Frank Binns
2015-03-07 0:58 ` [PATCH libdrm v3] " Emil Velikov
1 sibling, 2 replies; 16+ messages in thread
From: Emil Velikov @ 2015-02-23 12:22 UTC (permalink / raw)
To: dri-devel; +Cc: David Herrmann, emil.l.velikov, Daniel Vetter
Currently most places assume reliable master <> render node mapping.
Although this may work in some cases, it is not correct.
Add a couple of helpers that hide the details and provide the name of
the master/render device name, given an render/master FD.
v2:
- Rename Device and Primary to Master (aka the /dev/dri/cardX device).
- Check for the file via readdir_r() rather than stat().
- Wrap the check into a single function.
- Return NULL for non-linux platforms.
Cc: Frank Binns <frank.binns@imgtec.com>
Cc: Daniel Vetter <daniel.vetter@ffwll.ch>
Cc: David Herrmann <dh.herrmann@googlemail.com>
Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
---
xf86drm.c | 82 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
xf86drm.h | 3 +++
2 files changed, 85 insertions(+)
diff --git a/xf86drm.c b/xf86drm.c
index e117bc6..d4a4dc6 100644
--- a/xf86drm.c
+++ b/xf86drm.c
@@ -40,6 +40,8 @@
#include <string.h>
#include <strings.h>
#include <ctype.h>
+#include <dirent.h>
+#include <stddef.h>
#include <fcntl.h>
#include <errno.h>
#include <signal.h>
@@ -522,6 +524,20 @@ static int drmGetMinorType(int minor)
}
}
+static const char *drmGetMinorName(int type)
+{
+ switch (type) {
+ case DRM_NODE_PRIMARY:
+ return "card";
+ case DRM_NODE_CONTROL:
+ return "controlD";
+ case DRM_NODE_RENDER:
+ return "renderD";
+ default:
+ return NULL;
+ }
+}
+
/**
* Open the device by bus ID.
*
@@ -2736,3 +2752,69 @@ int drmPrimeFDToHandle(int fd, int prime_fd, uint32_t *handle)
return 0;
}
+static char *drmGetMinorNameForFD(int fd, int type)
+{
+#ifdef __linux__
+ DIR *sysdir;
+ struct dirent *pent, *ent;
+ struct stat sbuf;
+ const char *name = drmGetMinorName(type);
+ const int len = strlen(name);
+ char dev_name[64], buf[64];
+ long name_max;
+ int maj, min;
+
+ if (!name)
+ return NULL;
+
+ if (fstat(fd, &sbuf))
+ return NULL;
+
+ maj = major(sbuf.st_rdev);
+ min = minor(sbuf.st_rdev);
+
+ if (maj != DRM_MAJOR || !S_ISCHR(sbuf.st_mode))
+ return NULL;
+
+ snprintf(buf, sizeof(buf), "/sys/dev/char/%d:%d/device/drm", maj, min);
+
+ sysdir = opendir(buf);
+ if (!sysdir)
+ return NULL;
+
+ name_max = fpathconf(dirfd(sysdir), _PC_NAME_MAX);
+ if (name_max == -1)
+ goto out_close_dir;
+
+ pent = malloc(offsetof(struct dirent, d_name) + name_max + 1);
+ if (pent == NULL)
+ goto out_close_dir;
+
+ while (readdir_r(sysdir, pent, &ent) == 0 && ent != NULL) {
+ if (strncmp(ent->d_name, name, len) == 0) {
+ free(pent);
+ closedir(sysdir);
+
+ snprintf(dev_name, sizeof(dev_name), DRM_DIR_NAME "/%s",
+ ent->d_name);
+ return strdup(dev_name);
+ }
+ }
+
+ free(pent);
+
+out_close_dir:
+ closedir(sysdir);
+#endif
+ return NULL;
+}
+
+char *drmGetMasterNameFromRenderFD(int fd)
+{
+ return drmGetMinorNameForFD(fd, DRM_NODE_PRIMARY);
+}
+
+char *drmGetRenderNameFromMasterFD(int fd)
+{
+ return drmGetMinorNameForFD(fd, DRM_NODE_RENDER);
+}
diff --git a/xf86drm.h b/xf86drm.h
index afd38a1..5fdf27b 100644
--- a/xf86drm.h
+++ b/xf86drm.h
@@ -749,6 +749,9 @@ extern int drmGetNodeTypeFromFd(int fd);
extern int drmPrimeHandleToFD(int fd, uint32_t handle, uint32_t flags, int *prime_fd);
extern int drmPrimeFDToHandle(int fd, int prime_fd, uint32_t *handle);
+extern char *drmGetRenderNameFromMasterFD(int fd);
+extern char *drmGetMasterNameFromRenderFD(int fd);
+
#if defined(__cplusplus) || defined(c_plusplus)
}
#endif
--
2.3.0
_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
http://lists.freedesktop.org/mailman/listinfo/dri-devel
^ permalink raw reply related [flat|nested] 16+ messages in thread
* Re: [PATCH libdrm v2] drm: add drmGet(Master|Render)NameFrom(Render|Master)FD functions
2015-02-23 12:22 ` [PATCH libdrm v2] drm: add drmGet(Master|Render)NameFrom(Render|Master)FD functions Emil Velikov
@ 2015-02-23 14:35 ` Frank Binns
2015-02-23 15:03 ` Emil Velikov
2015-03-07 0:58 ` [PATCH libdrm v3] " Emil Velikov
1 sibling, 1 reply; 16+ messages in thread
From: Frank Binns @ 2015-02-23 14:35 UTC (permalink / raw)
To: Emil Velikov, dri-devel; +Cc: David Herrmann, Daniel Vetter
Hi Emil,
On 23/02/15 12:22, Emil Velikov wrote:
> Currently most places assume reliable master <> render node mapping.
> Although this may work in some cases, it is not correct.
>
> Add a couple of helpers that hide the details and provide the name of
> the master/render device name, given an render/master FD.
>
> v2:
> - Rename Device and Primary to Master (aka the /dev/dri/cardX device).
> - Check for the file via readdir_r() rather than stat().
> - Wrap the check into a single function.
> - Return NULL for non-linux platforms.
>
> Cc: Frank Binns <frank.binns@imgtec.com>
> Cc: Daniel Vetter <daniel.vetter@ffwll.ch>
> Cc: David Herrmann <dh.herrmann@googlemail.com>
> Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
> ---
> xf86drm.c | 82 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
> xf86drm.h | 3 +++
> 2 files changed, 85 insertions(+)
>
> diff --git a/xf86drm.c b/xf86drm.c
> index e117bc6..d4a4dc6 100644
> --- a/xf86drm.c
> +++ b/xf86drm.c
> @@ -40,6 +40,8 @@
> #include <string.h>
> #include <strings.h>
> #include <ctype.h>
> +#include <dirent.h>
> +#include <stddef.h>
> #include <fcntl.h>
> #include <errno.h>
> #include <signal.h>
> @@ -522,6 +524,20 @@ static int drmGetMinorType(int minor)
> }
> }
>
> +static const char *drmGetMinorName(int type)
> +{
> + switch (type) {
> + case DRM_NODE_PRIMARY:
> + return "card";
> + case DRM_NODE_CONTROL:
> + return "controlD";
> + case DRM_NODE_RENDER:
> + return "renderD";
> + default:
> + return NULL;
> + }
> +}
> +
> /**
> * Open the device by bus ID.
> *
> @@ -2736,3 +2752,69 @@ int drmPrimeFDToHandle(int fd, int prime_fd, uint32_t *handle)
> return 0;
> }
>
> +static char *drmGetMinorNameForFD(int fd, int type)
> +{
> +#ifdef __linux__
> + DIR *sysdir;
> + struct dirent *pent, *ent;
> + struct stat sbuf;
> + const char *name = drmGetMinorName(type);
> + const int len = strlen(name);
This will cause a segfault if 'name' is NULL.
> + char dev_name[64], buf[64];
> + long name_max;
> + int maj, min;
> +
> + if (!name)
> + return NULL;
> +
> + if (fstat(fd, &sbuf))
> + return NULL;
> +
> + maj = major(sbuf.st_rdev);
> + min = minor(sbuf.st_rdev);
> +
> + if (maj != DRM_MAJOR || !S_ISCHR(sbuf.st_mode))
> + return NULL;
> +
> + snprintf(buf, sizeof(buf), "/sys/dev/char/%d:%d/device/drm", maj, min);
> +
> + sysdir = opendir(buf);
> + if (!sysdir)
> + return NULL;
> +
> + name_max = fpathconf(dirfd(sysdir), _PC_NAME_MAX);
> + if (name_max == -1)
> + goto out_close_dir;
> +
> + pent = malloc(offsetof(struct dirent, d_name) + name_max + 1);
> + if (pent == NULL)
> + goto out_close_dir;
> +
> + while (readdir_r(sysdir, pent, &ent) == 0 && ent != NULL) {
> + if (strncmp(ent->d_name, name, len) == 0) {
> + free(pent);
> + closedir(sysdir);
> +
> + snprintf(dev_name, sizeof(dev_name), DRM_DIR_NAME "/%s",
> + ent->d_name);
> + return strdup(dev_name);
> + }
> + }
> +
> + free(pent);
> +
> +out_close_dir:
> + closedir(sysdir);
> +#endif
> + return NULL;
> +}
> +
> +char *drmGetMasterNameFromRenderFD(int fd)
I think drmGetPrimaryDeviceNameFromFd would be more appropriate given
the node type is 'primary', the type of the fd doesn't matter afaics and
for consistency with other drmGet* functions. However, given that's a
bit of a mouthful I guess the 'Device' part could be dropped.
> +{
> + return drmGetMinorNameForFD(fd, DRM_NODE_PRIMARY);
> +}
> +
> +char *drmGetRenderNameFromMasterFD(int fd)
As above, maybe drmGetRenderDeviceNameFromFd?
With those things changed/fixed you can have a:
Reviewed-by: Frank Binns <frank.binns@imgtec.com>
Thanks
Frank
> +{
> + return drmGetMinorNameForFD(fd, DRM_NODE_RENDER);
> +}
> diff --git a/xf86drm.h b/xf86drm.h
> index afd38a1..5fdf27b 100644
> --- a/xf86drm.h
> +++ b/xf86drm.h
> @@ -749,6 +749,9 @@ extern int drmGetNodeTypeFromFd(int fd);
> extern int drmPrimeHandleToFD(int fd, uint32_t handle, uint32_t flags, int *prime_fd);
> extern int drmPrimeFDToHandle(int fd, int prime_fd, uint32_t *handle);
>
> +extern char *drmGetRenderNameFromMasterFD(int fd);
> +extern char *drmGetMasterNameFromRenderFD(int fd);
> +
> #if defined(__cplusplus) || defined(c_plusplus)
> }
> #endif
_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
http://lists.freedesktop.org/mailman/listinfo/dri-devel
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [PATCH libdrm v2] drm: add drmGet(Master|Render)NameFrom(Render|Master)FD functions
2015-02-23 14:35 ` Frank Binns
@ 2015-02-23 15:03 ` Emil Velikov
0 siblings, 0 replies; 16+ messages in thread
From: Emil Velikov @ 2015-02-23 15:03 UTC (permalink / raw)
To: Frank Binns; +Cc: David Herrmann, Daniel Vetter, ML dri-devel
On 23 February 2015 at 14:35, Frank Binns <frank.binns@imgtec.com> wrote:
> Hi Emil,
>
> On 23/02/15 12:22, Emil Velikov wrote:
>> Currently most places assume reliable master <> render node mapping.
>> Although this may work in some cases, it is not correct.
>>
>> Add a couple of helpers that hide the details and provide the name of
>> the master/render device name, given an render/master FD.
>>
>> v2:
>> - Rename Device and Primary to Master (aka the /dev/dri/cardX device).
>> - Check for the file via readdir_r() rather than stat().
>> - Wrap the check into a single function.
>> - Return NULL for non-linux platforms.
>>
>> Cc: Frank Binns <frank.binns@imgtec.com>
>> Cc: Daniel Vetter <daniel.vetter@ffwll.ch>
>> Cc: David Herrmann <dh.herrmann@googlemail.com>
>> Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
>> ---
>> xf86drm.c | 82 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
>> xf86drm.h | 3 +++
>> 2 files changed, 85 insertions(+)
>>
>> diff --git a/xf86drm.c b/xf86drm.c
>> index e117bc6..d4a4dc6 100644
>> --- a/xf86drm.c
>> +++ b/xf86drm.c
>> @@ -40,6 +40,8 @@
>> #include <string.h>
>> #include <strings.h>
>> #include <ctype.h>
>> +#include <dirent.h>
>> +#include <stddef.h>
>> #include <fcntl.h>
>> #include <errno.h>
>> #include <signal.h>
>> @@ -522,6 +524,20 @@ static int drmGetMinorType(int minor)
>> }
>> }
>>
>> +static const char *drmGetMinorName(int type)
>> +{
>> + switch (type) {
>> + case DRM_NODE_PRIMARY:
>> + return "card";
>> + case DRM_NODE_CONTROL:
>> + return "controlD";
>> + case DRM_NODE_RENDER:
>> + return "renderD";
>> + default:
>> + return NULL;
>> + }
>> +}
>> +
>> /**
>> * Open the device by bus ID.
>> *
>> @@ -2736,3 +2752,69 @@ int drmPrimeFDToHandle(int fd, int prime_fd, uint32_t *handle)
>> return 0;
>> }
>>
>> +static char *drmGetMinorNameForFD(int fd, int type)
>> +{
>> +#ifdef __linux__
>> + DIR *sysdir;
>> + struct dirent *pent, *ent;
>> + struct stat sbuf;
>> + const char *name = drmGetMinorName(type);
>> + const int len = strlen(name);
> This will cause a segfault if 'name' is NULL.
>
>> + char dev_name[64], buf[64];
>> + long name_max;
>> + int maj, min;
>> +
>> + if (!name)
>> + return NULL;
>> +
>> + if (fstat(fd, &sbuf))
>> + return NULL;
>> +
>> + maj = major(sbuf.st_rdev);
>> + min = minor(sbuf.st_rdev);
>> +
>> + if (maj != DRM_MAJOR || !S_ISCHR(sbuf.st_mode))
>> + return NULL;
>> +
>> + snprintf(buf, sizeof(buf), "/sys/dev/char/%d:%d/device/drm", maj, min);
>> +
>> + sysdir = opendir(buf);
>> + if (!sysdir)
>> + return NULL;
>> +
>> + name_max = fpathconf(dirfd(sysdir), _PC_NAME_MAX);
>> + if (name_max == -1)
>> + goto out_close_dir;
>> +
>> + pent = malloc(offsetof(struct dirent, d_name) + name_max + 1);
>> + if (pent == NULL)
>> + goto out_close_dir;
>> +
>> + while (readdir_r(sysdir, pent, &ent) == 0 && ent != NULL) {
>> + if (strncmp(ent->d_name, name, len) == 0) {
>> + free(pent);
>> + closedir(sysdir);
>> +
>> + snprintf(dev_name, sizeof(dev_name), DRM_DIR_NAME "/%s",
>> + ent->d_name);
>> + return strdup(dev_name);
>> + }
>> + }
>> +
>> + free(pent);
>> +
>> +out_close_dir:
>> + closedir(sysdir);
>> +#endif
>> + return NULL;
>> +}
>> +
>> +char *drmGetMasterNameFromRenderFD(int fd)
> I think drmGetPrimaryDeviceNameFromFd would be more appropriate given
> the node type is 'primary',
Most places that I've seen call "/dev/dri/cardX" master node, although
with the introduction of DRM_NODE_PRIMARY I believe your suggestion
may be better.
> the type of the fd doesn't matter afaics and
> for consistency with other drmGet* functions. However, given that's a
> bit of a mouthful I guess the 'Device' part could be dropped.
>
I was thinking about this initially then decided to keep the
Render/MasterFD explicit. Mainly because I'm don't know how cumbersome
the *BSD implementation will be.
But with that said it none of the BSD guys objects within 1-2 weeks
I'll go with your suggestion.
Thanks
Emil
_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
http://lists.freedesktop.org/mailman/listinfo/dri-devel
^ permalink raw reply [flat|nested] 16+ messages in thread
* [PATCH libdrm v3] drm: add drmGet(Master|Render)NameFrom(Render|Master)FD functions
2015-02-23 12:22 ` [PATCH libdrm v2] drm: add drmGet(Master|Render)NameFrom(Render|Master)FD functions Emil Velikov
2015-02-23 14:35 ` Frank Binns
@ 2015-03-07 0:58 ` Emil Velikov
2015-03-07 1:08 ` Emil Velikov
1 sibling, 1 reply; 16+ messages in thread
From: Emil Velikov @ 2015-03-07 0:58 UTC (permalink / raw)
To: dri-devel; +Cc: David Herrmann, emil.l.velikov, Daniel Vetter
Currently most places assume reliable master <> render node mapping.
Although this may work in some cases, it is not correct.
Add a couple of helpers that hide the details and provide the name of
the master/render device name, given an render/master FD.
v2:
- Rename Device and Primary to Master (aka the /dev/dri/cardX device).
- Check for the file via readdir_r() rather than stat().
- Wrap the check into a single function.
- Return NULL for non-linux platforms.
v3:
- Don't segfault if name is NULL.
- Update function names, as suggested by Frank Binns.
Cc: Frank Binns <frank.binns@imgtec.com>
Cc: Daniel Vetter <daniel.vetter@ffwll.ch>
Cc: David Herrmann <dh.herrmann@googlemail.com>
Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Reviewed-by: Frank Binns <frank.binns@imgtec.com>
---
Hi all,
I'm planning to push this Monday afternoon. If you have any comments or
suggestions please go ahead.
Cheers,
Emil
xf86drm.c | 84 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
xf86drm.h | 3 +++
2 files changed, 87 insertions(+)
diff --git a/xf86drm.c b/xf86drm.c
index e117bc6..194cd35 100644
--- a/xf86drm.c
+++ b/xf86drm.c
@@ -40,6 +40,8 @@
#include <string.h>
#include <strings.h>
#include <ctype.h>
+#include <dirent.h>
+#include <stddef.h>
#include <fcntl.h>
#include <errno.h>
#include <signal.h>
@@ -522,6 +524,20 @@ static int drmGetMinorType(int minor)
}
}
+static const char *drmGetMinorName(int type)
+{
+ switch (type) {
+ case DRM_NODE_PRIMARY:
+ return "card";
+ case DRM_NODE_CONTROL:
+ return "controlD";
+ case DRM_NODE_RENDER:
+ return "renderD";
+ default:
+ return NULL;
+ }
+}
+
/**
* Open the device by bus ID.
*
@@ -2736,3 +2752,71 @@ int drmPrimeFDToHandle(int fd, int prime_fd, uint32_t *handle)
return 0;
}
+static char *drmGetMinorNameForFD(int fd, int type)
+{
+#ifdef __linux__
+ DIR *sysdir;
+ struct dirent *pent, *ent;
+ struct stat sbuf;
+ const char *name = drmGetMinorName(type);
+ int len;
+ char dev_name[64], buf[64];
+ long name_max;
+ int maj, min;
+
+ if (!name)
+ return NULL;
+
+ len = strlen(name);
+
+ if (fstat(fd, &sbuf))
+ return NULL;
+
+ maj = major(sbuf.st_rdev);
+ min = minor(sbuf.st_rdev);
+
+ if (maj != DRM_MAJOR || !S_ISCHR(sbuf.st_mode))
+ return NULL;
+
+ snprintf(buf, sizeof(buf), "/sys/dev/char/%d:%d/device/drm", maj, min);
+
+ sysdir = opendir(buf);
+ if (!sysdir)
+ return NULL;
+
+ name_max = fpathconf(dirfd(sysdir), _PC_NAME_MAX);
+ if (name_max == -1)
+ goto out_close_dir;
+
+ pent = malloc(offsetof(struct dirent, d_name) + name_max + 1);
+ if (pent == NULL)
+ goto out_close_dir;
+
+ while (readdir_r(sysdir, pent, &ent) == 0 && ent != NULL) {
+ if (strncmp(ent->d_name, name, len) == 0) {
+ free(pent);
+ closedir(sysdir);
+
+ snprintf(dev_name, sizeof(dev_name), DRM_DIR_NAME "/%s",
+ ent->d_name);
+ return strdup(dev_name);
+ }
+ }
+
+ free(pent);
+
+out_close_dir:
+ closedir(sysdir);
+#endif
+ return NULL;
+}
+
+char *drmGetPrimaryDeviceNameFromFd(int fd)
+{
+ return drmGetMinorNameForFD(fd, DRM_NODE_PRIMARY);
+}
+
+char *drmGetRenderDeviceNameFromFd(int fd)
+{
+ return drmGetMinorNameForFD(fd, DRM_NODE_RENDER);
+}
diff --git a/xf86drm.h b/xf86drm.h
index afd38a1..40c55c9 100644
--- a/xf86drm.h
+++ b/xf86drm.h
@@ -749,6 +749,9 @@ extern int drmGetNodeTypeFromFd(int fd);
extern int drmPrimeHandleToFD(int fd, uint32_t handle, uint32_t flags, int *prime_fd);
extern int drmPrimeFDToHandle(int fd, int prime_fd, uint32_t *handle);
+extern char *drmGetPrimaryDeviceNameFromFd(int fd);
+extern char *drmGetRenderDeviceNameFromFd(int fd);
+
#if defined(__cplusplus) || defined(c_plusplus)
}
#endif
--
2.3.1
_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
http://lists.freedesktop.org/mailman/listinfo/dri-devel
^ permalink raw reply related [flat|nested] 16+ messages in thread
* Re: [PATCH libdrm v3] drm: add drmGet(Master|Render)NameFrom(Render|Master)FD functions
2015-03-07 0:58 ` [PATCH libdrm v3] " Emil Velikov
@ 2015-03-07 1:08 ` Emil Velikov
0 siblings, 0 replies; 16+ messages in thread
From: Emil Velikov @ 2015-03-07 1:08 UTC (permalink / raw)
To: ML dri-devel; +Cc: Emil Velikov
On 7 March 2015 at 00:58, Emil Velikov <emil.l.velikov@gmail.com> wrote:
> Currently most places assume reliable master <> render node mapping.
> Although this may work in some cases, it is not correct.
>
> Add a couple of helpers that hide the details and provide the name of
> the master/render device name, given an render/master FD.
>
> v2:
> - Rename Device and Primary to Master (aka the /dev/dri/cardX device).
> - Check for the file via readdir_r() rather than stat().
> - Wrap the check into a single function.
> - Return NULL for non-linux platforms.
>
> v3:
> - Don't segfault if name is NULL.
> - Update function names, as suggested by Frank Binns.
And seemingly I've forgot to update the patch summary to reflect the
change. I'll do so before pushing
-Emil
_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
http://lists.freedesktop.org/mailman/listinfo/dri-devel
^ permalink raw reply [flat|nested] 16+ messages in thread
end of thread, other threads:[~2015-03-07 1:08 UTC | newest]
Thread overview: 16+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-02-02 0:14 [RFC PATCH 0/2] libdrm: Add master <> render node helpers Emil Velikov
2015-02-02 0:14 ` [PATCH 1/2] drm: add drmGet{Device, Render}NameFrom{Render, Device}Fd helpers Emil Velikov
2015-02-13 11:07 ` Frank Binns
2015-02-23 12:22 ` [PATCH libdrm v2] drm: add drmGet(Master|Render)NameFrom(Render|Master)FD functions Emil Velikov
2015-02-23 14:35 ` Frank Binns
2015-02-23 15:03 ` Emil Velikov
2015-03-07 0:58 ` [PATCH libdrm v3] " Emil Velikov
2015-03-07 1:08 ` Emil Velikov
2015-02-02 0:14 ` [PATCH 2/2] libdrm: add drmGetNodeType() helper Emil Velikov
2015-02-13 10:50 ` Frank Binns
2015-02-10 22:37 ` [RFC PATCH 0/2] libdrm: Add master <> render node helpers Emil Velikov
2015-02-10 23:32 ` Jonathan Gray
2015-02-12 18:21 ` David Herrmann
2015-02-13 9:18 ` Emil Velikov
2015-02-13 11:18 ` Frank Binns
2015-02-13 18:19 ` Emil Velikov
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.