aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThierry Reding2016-12-21 10:54:48 -0600
committerThierry Reding2017-01-20 09:25:35 -0600
commit2e57bba870399926e1a0d0be3f4918a0a8432474 (patch)
tree12c5e1b5d7f8c9368b66164f508fa56300c2a0ee /xf86drm.c
parent9e4c686aa36dfc09b715269a6bb11a452f87ec71 (diff)
downloadexternal-libdrm-2e57bba870399926e1a0d0be3f4918a0a8432474.tar.gz
external-libdrm-2e57bba870399926e1a0d0be3f4918a0a8432474.tar.xz
external-libdrm-2e57bba870399926e1a0d0be3f4918a0a8432474.zip
xf86drm: Factor out drmDeviceAlloc()
Subsequent patches will add support for other bus types to drmDevice and they will duplicate a lot of the code to allocate a drmDevice. Factor out the common code so it can be reused. Reviewed-by: Emil Velikov <emil.l.velikov@gmail.com> Signed-off-by: Thierry Reding <treding@nvidia.com>
Diffstat (limited to 'xf86drm.c')
-rw-r--r--xf86drm.c78
1 files changed, 51 insertions, 27 deletions
diff --git a/xf86drm.c b/xf86drm.c
index 7b78dc6b..7766bfe9 100644
--- a/xf86drm.c
+++ b/xf86drm.c
@@ -3157,57 +3157,81 @@ void drmFreeDevices(drmDevicePtr devices[], int count)
3157 drmFreeDevice(&devices[i]); 3157 drmFreeDevice(&devices[i]);
3158} 3158}
3159 3159
3160static drmDevicePtr drmDeviceAlloc(unsigned int type, const char *node,
3161 size_t bus_size, size_t device_size,
3162 char **ptrp)
3163{
3164 size_t max_node_length, extra, size;
3165 drmDevicePtr device;
3166 unsigned int i;
3167 char *ptr;
3168
3169 max_node_length = ALIGN(drmGetMaxNodeName(), sizeof(void *));
3170 extra = DRM_NODE_MAX * (sizeof(void *) + max_node_length);
3171
3172 size = sizeof(*device) + extra + bus_size + device_size;
3173
3174 device = calloc(1, size);
3175 if (!device)
3176 return NULL;
3177
3178 device->available_nodes = 1 << type;
3179
3180 ptr = (char *)device + sizeof(*device);
3181 device->nodes = (char **)ptr;
3182
3183 ptr += DRM_NODE_MAX * sizeof(void *);
3184
3185 for (i = 0; i < DRM_NODE_MAX; i++) {
3186 device->nodes[i] = ptr;
3187 ptr += max_node_length;
3188 }
3189
3190 memcpy(device->nodes[type], node, max_node_length);
3191
3192 *ptrp = ptr;
3193
3194 return device;
3195}
3196
3160static int drmProcessPciDevice(drmDevicePtr *device, 3197static int drmProcessPciDevice(drmDevicePtr *device,
3161 const char *node, int node_type, 3198 const char *node, int node_type,
3162 int maj, int min, bool fetch_deviceinfo, 3199 int maj, int min, bool fetch_deviceinfo,
3163 uint32_t flags) 3200 uint32_t flags)
3164{ 3201{
3165 const int max_node_str = ALIGN(drmGetMaxNodeName(), sizeof(void *)); 3202 drmDevicePtr dev;
3166 int ret, i;
3167 char *addr; 3203 char *addr;
3204 int ret;
3168 3205
3169 *device = calloc(1, sizeof(drmDevice) + 3206 dev = drmDeviceAlloc(node_type, node, sizeof(drmPciBusInfo),
3170 (DRM_NODE_MAX * (sizeof(void *) + max_node_str)) + 3207 sizeof(drmPciDeviceInfo), &addr);
3171 sizeof(drmPciBusInfo) + 3208 if (!dev)
3172 sizeof(drmPciDeviceInfo));
3173 if (!*device)
3174 return -ENOMEM; 3209 return -ENOMEM;
3175 3210
3176 addr = (char*)*device; 3211 dev->bustype = DRM_BUS_PCI;
3177 3212
3178 (*device)->bustype = DRM_BUS_PCI; 3213 dev->businfo.pci = (drmPciBusInfoPtr)addr;
3179 (*device)->available_nodes = 1 << node_type;
3180 3214
3181 addr += sizeof(drmDevice); 3215 ret = drmParsePciBusInfo(maj, min, dev->businfo.pci);
3182 (*device)->nodes = (char**)addr;
3183
3184 addr += DRM_NODE_MAX * sizeof(void *);
3185 for (i = 0; i < DRM_NODE_MAX; i++) {
3186 (*device)->nodes[i] = addr;
3187 addr += max_node_str;
3188 }
3189 memcpy((*device)->nodes[node_type], node, max_node_str);
3190
3191 (*device)->businfo.pci = (drmPciBusInfoPtr)addr;
3192
3193 ret = drmParsePciBusInfo(maj, min, (*device)->businfo.pci);
3194 if (ret) 3216 if (ret)
3195 goto free_device; 3217 goto free_device;
3196 3218
3197 // Fetch the device info if the user has requested it 3219 // Fetch the device info if the user has requested it
3198 if (fetch_deviceinfo) { 3220 if (fetch_deviceinfo) {
3199 addr += sizeof(drmPciBusInfo); 3221 addr += sizeof(drmPciBusInfo);
3200 (*device)->deviceinfo.pci = (drmPciDeviceInfoPtr)addr; 3222 dev->deviceinfo.pci = (drmPciDeviceInfoPtr)addr;
3201 3223
3202 ret = drmParsePciDeviceInfo(maj, min, (*device)->deviceinfo.pci, flags); 3224 ret = drmParsePciDeviceInfo(maj, min, dev->deviceinfo.pci, flags);
3203 if (ret) 3225 if (ret)
3204 goto free_device; 3226 goto free_device;
3205 } 3227 }
3228
3229 *device = dev;
3230
3206 return 0; 3231 return 0;
3207 3232
3208free_device: 3233free_device:
3209 free(*device); 3234 free(dev);
3210 *device = NULL;
3211 return ret; 3235 return ret;
3212} 3236}
3213 3237