From: Robert Foss <robert.foss@collabora.com>
To: Emil Velikov <emil.l.velikov@gmail.com>, dri-devel@lists.freedesktop.org
Subject: Re: [PATCH libdrm 05/10] xf86drm: introduce a get_real_pci_path() helper
Date: Thu, 28 Jun 2018 18:06:41 +0200 [thread overview]
Message-ID: <c8c23d03-d119-d1dc-00be-007f1139b3e2@collabora.com> (raw)
In-Reply-To: <20180625173626.8046-5-emil.l.velikov@gmail.com>
Feel free to add my r-b to this patch.
On 2018-06-25 19:36, Emil Velikov wrote:
> From: Emil Velikov <emil.velikov@collabora.com>
>
> Introduce a helper which gets the real sysfs path for the given pci
> device.
>
> In other words, instead opening the /sys/dev/char/*/device symlink, we
> opt for the actual /sys/devices/pci*/*/
>
> It folds three (nearly identical) snprintf's and paves the way of adding
> extra devices (see next patch) a piece of pie.
>
> Signed-off-by: Emil Velikov <emil.velikov@collabora.com>
> ---
> xf86drm.c | 62 ++++++++++++++++++++++++++++++++++++++++++++-----------
> 1 file changed, 50 insertions(+), 12 deletions(-)
>
> diff --git a/xf86drm.c b/xf86drm.c
> index d4810740..8ccd528f 100644
> --- a/xf86drm.c
> +++ b/xf86drm.c
> @@ -2992,16 +2992,37 @@ static int drmParseSubsystemType(int maj, int min)
> #endif
> }
>
> +static char *
> +get_real_pci_path(int maj, int min)
> +{
> + char path[PATH_MAX + 1];
> + char *real_path = malloc(PATH_MAX);
> +
> + if (!real_path)
> + return NULL;
> +
> + snprintf(path, sizeof(path), "/sys/dev/char/%d:%d/device", maj, min);
> + if (!realpath(path, real_path)) {
> + free(real_path);
> + return NULL;
> + }
> +
> + return real_path;
> +}
> +
> static int drmParsePciBusInfo(int maj, int min, drmPciBusInfoPtr info)
> {
> #ifdef __linux__
> unsigned int domain, bus, dev, func;
> - char path[PATH_MAX + 1], *value;
> + char *real_path, *value;
> int num;
>
> - snprintf(path, sizeof(path), "/sys/dev/char/%d:%d/device", maj, min);
> + real_path = get_real_pci_path(maj, min);
> + if (!real_path)
> + return -ENOENT;
>
> - value = sysfs_uevent_get(path, "PCI_SLOT_NAME");
> + value = sysfs_uevent_get(real_path, "PCI_SLOT_NAME");
> + free(real_path);
> if (!value)
> return -ENOENT;
>
> @@ -3114,24 +3135,32 @@ static int parse_separate_sysfs_files(int maj, int min,
> "subsystem_vendor",
> "subsystem_device",
> };
> - char path[PATH_MAX + 1];
> + char path[PATH_MAX + 1], *real_path;
> unsigned int data[ARRAY_SIZE(attrs)];
> FILE *fp;
> int ret;
>
> + real_path = get_real_pci_path(maj, min);
> + if (!real_path)
> + return -ENOENT;
> +
> for (unsigned i = ignore_revision ? 1 : 0; i < ARRAY_SIZE(attrs); i++) {
> - snprintf(path, PATH_MAX, "/sys/dev/char/%d:%d/device/%s", maj, min,
> - attrs[i]);
> + snprintf(path, PATH_MAX, "%s/%s", real_path, attrs[i]);
> fp = fopen(path, "r");
> - if (!fp)
> + if (!fp) {
> + free(real_path);
> return -errno;
> + }
>
> ret = fscanf(fp, "%x", &data[i]);
> fclose(fp);
> - if (ret != 1)
> + if (ret != 1) {
> + free(real_path);
> return -errno;
> + }
>
> }
> + free(real_path);
>
> device->revision_id = ignore_revision ? 0xff : data[0] & 0xff;
> device->vendor_id = data[1] & 0xffff;
> @@ -3145,19 +3174,28 @@ static int parse_separate_sysfs_files(int maj, int min,
> static int parse_config_sysfs_file(int maj, int min,
> drmPciDeviceInfoPtr device)
> {
> - char path[PATH_MAX + 1];
> + char path[PATH_MAX + 1], *real_path;
> unsigned char config[64];
> int fd, ret;
>
> - snprintf(path, PATH_MAX, "/sys/dev/char/%d:%d/device/config", maj, min);
> + real_path = get_real_pci_path(maj, min);
> + if (!real_path)
> + return -ENOENT;
> +
> + snprintf(path, PATH_MAX, "%s/config", real_path);
> fd = open(path, O_RDONLY);
> - if (fd < 0)
> + if (fd < 0) {
> + free(real_path);
> return -errno;
> + }
>
> ret = read(fd, config, sizeof(config));
> close(fd);
> - if (ret < 0)
> + if (ret < 0) {
> + free(real_path);
> return -errno;
> + }
> + free(real_path);
>
> device->vendor_id = config[0] | (config[1] << 8);
> device->device_id = config[2] | (config[3] << 8);
>
_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/dri-devel
next prev parent reply other threads:[~2018-06-28 16:06 UTC|newest]
Thread overview: 31+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-06-25 17:36 [PATCH libdrm 01/10] xf86drm: drmGetDevice2: error out if the fd has unknown subsys Emil Velikov
2018-06-25 17:36 ` [PATCH libdrm 02/10] xf86drm: introduce drm_device_has_rdev() helper Emil Velikov
2018-06-28 10:14 ` Robert Foss
2018-06-25 17:36 ` [PATCH libdrm 03/10] xf86drm: Fold drmDevice processing into process_device() helper Emil Velikov
2018-06-28 11:50 ` Robert Foss
2018-06-25 17:36 ` [PATCH libdrm 04/10] xf86drm: Allocate drmDevicePtr's on stack Emil Velikov
2018-06-28 12:52 ` Robert Foss
2018-06-28 17:07 ` Emil Velikov
2018-06-29 15:20 ` [PATCH libdrm v2 " Emil Velikov
2018-06-29 15:49 ` Robert Foss
2018-06-29 15:22 ` [PATCH libdrm v2 05/10] xf86drm: introduce a get_real_pci_path() helper Emil Velikov
2018-06-25 17:36 ` [PATCH libdrm " Emil Velikov
2018-06-28 10:21 ` Eric Engestrom
2018-06-28 10:23 ` Eric Engestrom
2018-06-28 16:42 ` Emil Velikov
2018-06-28 16:06 ` Robert Foss [this message]
2018-06-25 17:36 ` [PATCH libdrm 06/10] xf86drm: Add drmDevice support for virtio_gpu Emil Velikov
2018-06-28 16:08 ` Robert Foss
2018-06-25 17:36 ` [PATCH libdrm 07/10] tests/drmdevices: install alongside other utilities Emil Velikov
2018-06-28 16:09 ` Robert Foss
2018-06-25 17:36 ` [PATCH libdrm 08/10] tests/drmdevice: add a couple of printf headers Emil Velikov
2018-06-28 16:09 ` Robert Foss
2018-06-25 17:36 ` [PATCH libdrm 09/10] drmdevice: convert the tabbed output into a tree Emil Velikov
2018-06-28 10:19 ` Eric Engestrom
2018-06-28 16:43 ` Emil Velikov
2018-06-28 16:09 ` Robert Foss
2018-06-29 15:24 ` [PATCH libdrm v2 " Emil Velikov
2018-06-25 17:36 ` [PATCH libdrm 10/10] drmdevice: print the correct host1x information Emil Velikov
2018-06-28 16:09 ` Robert Foss
2018-06-28 10:11 ` [PATCH libdrm 01/10] xf86drm: drmGetDevice2: error out if the fd has unknown subsys Robert Foss
2018-06-28 10:11 ` Robert Foss
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=c8c23d03-d119-d1dc-00be-007f1139b3e2@collabora.com \
--to=robert.foss@collabora.com \
--cc=dri-devel@lists.freedesktop.org \
--cc=emil.l.velikov@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox