diff options
author | Thierry Reding | 2017-01-18 01:29:23 -0600 |
---|---|---|
committer | Thierry Reding | 2017-01-20 09:27:33 -0600 |
commit | 5403cb39c124c444babec51bd4499971cd86ccfa (patch) | |
tree | 7a3a23cfcd24ce1856eca10ba8c067d3f9130a4d | |
parent | 13b99f2a898a67320499e538303a689a270bb7f4 (diff) | |
download | external-libdrm-5403cb39c124c444babec51bd4499971cd86ccfa.tar.gz external-libdrm-5403cb39c124c444babec51bd4499971cd86ccfa.tar.xz external-libdrm-5403cb39c124c444babec51bd4499971cd86ccfa.zip |
xf86drm: Reuse sysfs_uevent_get()
Recent patches for USB, platform and host1x bus support introduced the
sysfs_uevent_get() function that provides a generic way of parsing the
sysfs uevent file that is associated with each device in Linux.
Open-coded variants of this still exist in other places, so make those
reuse the new function to remove some code duplication.
Reviewed-by: Emil Velikov <emil.l.velikov@gmail.com>
Signed-off-by: Thierry Reding <treding@nvidia.com>
-rw-r--r-- | xf86drm.c | 66 |
1 files changed, 18 insertions, 48 deletions
@@ -2971,32 +2971,21 @@ static int drmParseSubsystemType(int maj, int min) | |||
2971 | static int drmParsePciBusInfo(int maj, int min, drmPciBusInfoPtr info) | 2971 | static int drmParsePciBusInfo(int maj, int min, drmPciBusInfoPtr info) |
2972 | { | 2972 | { |
2973 | #ifdef __linux__ | 2973 | #ifdef __linux__ |
2974 | char path[PATH_MAX + 1]; | 2974 | unsigned int domain, bus, dev, func; |
2975 | char data[512 + 1]; | 2975 | char path[PATH_MAX + 1], *value; |
2976 | char *str; | 2976 | int num; |
2977 | int domain, bus, dev, func; | ||
2978 | int fd, ret; | ||
2979 | 2977 | ||
2980 | snprintf(path, PATH_MAX, "/sys/dev/char/%d:%d/device/uevent", maj, min); | 2978 | snprintf(path, sizeof(path), "/sys/dev/char/%d:%d/device", maj, min); |
2981 | fd = open(path, O_RDONLY); | ||
2982 | if (fd < 0) | ||
2983 | return -errno; | ||
2984 | 2979 | ||
2985 | ret = read(fd, data, sizeof(data)-1); | 2980 | value = sysfs_uevent_get(path, "PCI_SLOT_NAME"); |
2986 | close(fd); | 2981 | if (!value) |
2987 | if (ret < 0) | 2982 | return -ENOENT; |
2988 | return -errno; | ||
2989 | data[ret] = '\0'; | ||
2990 | 2983 | ||
2991 | #define TAG "PCI_SLOT_NAME=" | 2984 | num = sscanf(value, "%04x:%02x:%02x.%1u", &domain, &bus, &dev, &func); |
2992 | str = strstr(data, TAG); | 2985 | free(value); |
2993 | if (str == NULL) | ||
2994 | return -EINVAL; | ||
2995 | 2986 | ||
2996 | if (sscanf(str, TAG "%04x:%02x:%02x.%1u", | 2987 | if (num != 4) |
2997 | &domain, &bus, &dev, &func) != 4) | ||
2998 | return -EINVAL; | 2988 | return -EINVAL; |
2999 | #undef TAG | ||
3000 | 2989 | ||
3001 | info->domain = domain; | 2990 | info->domain = domain; |
3002 | info->bus = bus; | 2991 | info->bus = bus; |
@@ -4084,13 +4073,8 @@ char *drmGetDeviceNameFromFd2(int fd) | |||
4084 | { | 4073 | { |
4085 | #ifdef __linux__ | 4074 | #ifdef __linux__ |
4086 | struct stat sbuf; | 4075 | struct stat sbuf; |
4087 | char *device_name = NULL; | 4076 | char path[PATH_MAX + 1], *value; |
4088 | unsigned int maj, min; | 4077 | unsigned int maj, min; |
4089 | FILE *f; | ||
4090 | char buf[512]; | ||
4091 | static const char match[9] = "\nDEVNAME="; | ||
4092 | size_t expected = 1; | ||
4093 | |||
4094 | 4078 | ||
4095 | if (fstat(fd, &sbuf)) | 4079 | if (fstat(fd, &sbuf)) |
4096 | return NULL; | 4080 | return NULL; |
@@ -4101,30 +4085,16 @@ char *drmGetDeviceNameFromFd2(int fd) | |||
4101 | if (maj != DRM_MAJOR || !S_ISCHR(sbuf.st_mode)) | 4085 | if (maj != DRM_MAJOR || !S_ISCHR(sbuf.st_mode)) |
4102 | return NULL; | 4086 | return NULL; |
4103 | 4087 | ||
4104 | snprintf(buf, sizeof(buf), "/sys/dev/char/%d:%d/uevent", maj, min); | 4088 | snprintf(path, sizeof(path), "/sys/dev/char/%d:%d", maj, min); |
4105 | if (!(f = fopen(buf, "r"))) | ||
4106 | return NULL; | ||
4107 | |||
4108 | while (expected < sizeof(match)) { | ||
4109 | int c = getc(f); | ||
4110 | 4089 | ||
4111 | if (c == EOF) { | 4090 | value = sysfs_uevent_get(path, "DEVNAME"); |
4112 | fclose(f); | 4091 | if (!value) |
4113 | return NULL; | 4092 | return NULL; |
4114 | } else if (c == match[expected] ) | ||
4115 | expected++; | ||
4116 | else | ||
4117 | expected = 0; | ||
4118 | } | ||
4119 | 4093 | ||
4120 | strcpy(buf, "/dev/"); | 4094 | snprintf(path, sizeof(path), "/dev/%s", value); |
4121 | if (fgets(buf + 5, sizeof(buf) - 5, f)) { | 4095 | free(value); |
4122 | buf[strcspn(buf, "\n")] = '\0'; | ||
4123 | device_name = strdup(buf); | ||
4124 | } | ||
4125 | 4096 | ||
4126 | fclose(f); | 4097 | return strdup(path); |
4127 | return device_name; | ||
4128 | #else | 4098 | #else |
4129 | struct stat sbuf; | 4099 | struct stat sbuf; |
4130 | char node[PATH_MAX + 1]; | 4100 | char node[PATH_MAX + 1]; |