xf86drm: dedupe drmGetDeviceName() logic

Signed-off-by: Eric Engestrom <eric.engestrom@intel.com>
main
Eric Engestrom 2018-12-19 13:53:41 +00:00
parent 6869e4cea7
commit 331e51e32f
1 changed files with 23 additions and 63 deletions

View File

@ -300,6 +300,19 @@ static int chown_check_return(const char *path, uid_t owner, gid_t group)
} }
#endif #endif
static const char *drmGetDeviceName(int type)
{
switch (type) {
case DRM_NODE_PRIMARY:
return DRM_DEV_NAME;
case DRM_NODE_CONTROL:
return DRM_CONTROL_DEV_NAME;
case DRM_NODE_RENDER:
return DRM_RENDER_DEV_NAME;
}
return NULL;
}
/** /**
* Open the DRM device, creating it if necessary. * Open the DRM device, creating it if necessary.
* *
@ -316,7 +329,7 @@ static int chown_check_return(const char *path, uid_t owner, gid_t group)
static int drmOpenDevice(dev_t dev, int minor, int type) static int drmOpenDevice(dev_t dev, int minor, int type)
{ {
stat_t st; stat_t st;
const char *dev_name; const char *dev_name = drmGetDeviceName(type);
char buf[DRM_NODE_NAME_MAX]; char buf[DRM_NODE_NAME_MAX];
int fd; int fd;
mode_t devmode = DRM_DEV_MODE, serv_mode; mode_t devmode = DRM_DEV_MODE, serv_mode;
@ -327,19 +340,8 @@ static int drmOpenDevice(dev_t dev, int minor, int type)
gid_t group = DRM_DEV_GID; gid_t group = DRM_DEV_GID;
#endif #endif
switch (type) { if (!dev_name)
case DRM_NODE_PRIMARY:
dev_name = DRM_DEV_NAME;
break;
case DRM_NODE_CONTROL:
dev_name = DRM_CONTROL_DEV_NAME;
break;
case DRM_NODE_RENDER:
dev_name = DRM_RENDER_DEV_NAME;
break;
default:
return -EINVAL; return -EINVAL;
};
sprintf(buf, dev_name, DRM_DIR_NAME, minor); sprintf(buf, dev_name, DRM_DIR_NAME, minor);
drmMsg("drmOpenDevice: node name is %s\n", buf); drmMsg("drmOpenDevice: node name is %s\n", buf);
@ -446,24 +448,13 @@ static int drmOpenMinor(int minor, int create, int type)
{ {
int fd; int fd;
char buf[DRM_NODE_NAME_MAX]; char buf[DRM_NODE_NAME_MAX];
const char *dev_name; const char *dev_name = drmGetDeviceName(type);
if (create) if (create)
return drmOpenDevice(makedev(DRM_MAJOR, minor), minor, type); return drmOpenDevice(makedev(DRM_MAJOR, minor), minor, type);
switch (type) { if (!dev_name)
case DRM_NODE_PRIMARY:
dev_name = DRM_DEV_NAME;
break;
case DRM_NODE_CONTROL:
dev_name = DRM_CONTROL_DEV_NAME;
break;
case DRM_NODE_RENDER:
dev_name = DRM_RENDER_DEV_NAME;
break;
default:
return -EINVAL; return -EINVAL;
};
sprintf(buf, dev_name, DRM_DIR_NAME, minor); sprintf(buf, dev_name, DRM_DIR_NAME, minor);
if ((fd = open(buf, O_RDWR | O_CLOEXEC, 0)) >= 0) if ((fd = open(buf, O_RDWR | O_CLOEXEC, 0)) >= 0)
@ -2892,7 +2883,7 @@ static char *drmGetMinorNameForFD(int fd, int type)
#else #else
struct stat sbuf; struct stat sbuf;
char buf[PATH_MAX + 1]; char buf[PATH_MAX + 1];
const char *dev_name; const char *dev_name = drmGetDeviceName(type);
unsigned int maj, min; unsigned int maj, min;
int n; int n;
@ -2905,19 +2896,8 @@ static char *drmGetMinorNameForFD(int fd, int type)
if (!drmNodeIsDRM(maj, min) || !S_ISCHR(sbuf.st_mode)) if (!drmNodeIsDRM(maj, min) || !S_ISCHR(sbuf.st_mode))
return NULL; return NULL;
switch (type) { if (!dev_name)
case DRM_NODE_PRIMARY:
dev_name = DRM_DEV_NAME;
break;
case DRM_NODE_CONTROL:
dev_name = DRM_CONTROL_DEV_NAME;
break;
case DRM_NODE_RENDER:
dev_name = DRM_RENDER_DEV_NAME;
break;
default:
return NULL; return NULL;
};
n = snprintf(buf, sizeof(buf), dev_name, DRM_DIR_NAME, min); n = snprintf(buf, sizeof(buf), dev_name, DRM_DIR_NAME, min);
if (n == -1 || n >= sizeof(buf)) if (n == -1 || n >= sizeof(buf))
@ -3831,19 +3811,9 @@ drm_public int drmGetDevice2(int fd, uint32_t flags, drmDevicePtr *device)
if (node_type == -1) if (node_type == -1)
return -ENODEV; return -ENODEV;
switch (node_type) { dev_name = drmGetDeviceName(node_type);
case DRM_NODE_PRIMARY: if (!dev_name)
dev_name = DRM_DEV_NAME;
break;
case DRM_NODE_CONTROL:
dev_name = DRM_CONTROL_DEV_NAME;
break;
case DRM_NODE_RENDER:
dev_name = DRM_RENDER_DEV_NAME;
break;
default:
return -EINVAL; return -EINVAL;
};
n = snprintf(node, PATH_MAX, dev_name, DRM_DIR_NAME, min); n = snprintf(node, PATH_MAX, dev_name, DRM_DIR_NAME, min);
if (n == -1 || n >= PATH_MAX) if (n == -1 || n >= PATH_MAX)
@ -4080,19 +4050,9 @@ drm_public char *drmGetDeviceNameFromFd2(int fd)
if (node_type == -1) if (node_type == -1)
return NULL; return NULL;
switch (node_type) { dev_name = drmGetDeviceName(node_type);
case DRM_NODE_PRIMARY: if (!dev_name)
dev_name = DRM_DEV_NAME;
break;
case DRM_NODE_CONTROL:
dev_name = DRM_CONTROL_DEV_NAME;
break;
case DRM_NODE_RENDER:
dev_name = DRM_RENDER_DEV_NAME;
break;
default:
return NULL; return NULL;
};
n = snprintf(node, PATH_MAX, dev_name, DRM_DIR_NAME, min); n = snprintf(node, PATH_MAX, dev_name, DRM_DIR_NAME, min);
if (n == -1 || n >= PATH_MAX) if (n == -1 || n >= PATH_MAX)