add packaging
[platform/upstream/libdrm.git] / xf86drmMode.c
index 473e734..60ce369 100644 (file)
 #include <sys/ioctl.h>
 #include <stdio.h>
 
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
 #include "xf86drmMode.h"
 #include "xf86drm.h"
 #include <drm.h>
 #include <unistd.h>
 #include <errno.h>
 
+#ifdef HAVE_VALGRIND
+#include <valgrind.h>
+#include <memcheck.h>
+#define VG(x) x
+#else
+#define VG(x)
+#endif
+
+#define VG_CLEAR(s) VG(memset(&s, 0, sizeof(s)))
+
 #define U642VOID(x) ((void *)(unsigned long)(x))
 #define VOID2U64(x) ((uint64_t)(unsigned long)(x))
 
@@ -245,6 +259,7 @@ int drmModeAddFB(int fd, uint32_t width, uint32_t height, uint8_t depth,
        struct drm_mode_fb_cmd f;
        int ret;
 
+       VG_CLEAR(f);
        f.width  = width;
        f.height = height;
        f.pitch  = pitch;
@@ -271,9 +286,9 @@ int drmModeAddFB2(int fd, uint32_t width, uint32_t height,
        f.height = height;
        f.pixel_format = pixel_format;
        f.flags = flags;
-       memcpy(f.handles, bo_handles, sizeof(bo_handles));
-       memcpy(f.pitches, pitches, sizeof(pitches));
-       memcpy(f.offsets, offsets, sizeof(offsets));
+       memcpy(f.handles, bo_handles, 4 * sizeof(bo_handles[0]));
+       memcpy(f.pitches, pitches, 4 * sizeof(pitches[0]));
+       memcpy(f.offsets, offsets, 4 * sizeof(offsets[0]));
 
        if ((ret = DRM_IOCTL(fd, DRM_IOCTL_MODE_ADDFB2, &f)))
                return ret;
@@ -335,6 +350,7 @@ drmModeCrtcPtr drmModeGetCrtc(int fd, uint32_t crtcId)
        struct drm_mode_crtc crtc;
        drmModeCrtcPtr r;
 
+       VG_CLEAR(crtc);
        crtc.crtc_id = crtcId;
 
        if (drmIoctl(fd, DRM_IOCTL_MODE_GETCRTC, &crtc))
@@ -351,8 +367,11 @@ drmModeCrtcPtr drmModeGetCrtc(int fd, uint32_t crtcId)
        r->x               = crtc.x;
        r->y               = crtc.y;
        r->mode_valid      = crtc.mode_valid;
-       if (r->mode_valid)
+       if (r->mode_valid) {
                memcpy(&r->mode, &crtc.mode, sizeof(struct drm_mode_modeinfo));
+               r->width = crtc.mode.hdisplay;
+               r->height = crtc.mode.vdisplay;
+       }
        r->buffer_id       = crtc.fb_id;
        r->gamma_size      = crtc.gamma_size;
        return r;
@@ -365,6 +384,7 @@ int drmModeSetCrtc(int fd, uint32_t crtcId, uint32_t bufferId,
 {
        struct drm_mode_crtc crtc;
 
+       VG_CLEAR(crtc);
        crtc.x             = x;
        crtc.y             = y;
        crtc.crtc_id       = crtcId;
@@ -397,6 +417,21 @@ int drmModeSetCursor(int fd, uint32_t crtcId, uint32_t bo_handle, uint32_t width
        return DRM_IOCTL(fd, DRM_IOCTL_MODE_CURSOR, &arg);
 }
 
+int drmModeSetCursor2(int fd, uint32_t crtcId, uint32_t bo_handle, uint32_t width, uint32_t height, int32_t hot_x, int32_t hot_y)
+{
+       struct drm_mode_cursor2 arg;
+
+       arg.flags = DRM_MODE_CURSOR_BO;
+       arg.crtc_id = crtcId;
+       arg.width = width;
+       arg.height = height;
+       arg.handle = bo_handle;
+       arg.hot_x = hot_x;
+       arg.hot_y = hot_y;
+
+       return DRM_IOCTL(fd, DRM_IOCTL_MODE_CURSOR2, &arg);
+}
+
 int drmModeMoveCursor(int fd, uint32_t crtcId, int x, int y)
 {
        struct drm_mode_cursor arg;
@@ -418,6 +453,7 @@ drmModeEncoderPtr drmModeGetEncoder(int fd, uint32_t encoder_id)
        drmModeEncoderPtr r = NULL;
 
        enc.encoder_id = encoder_id;
+       enc.crtc_id = 0;
        enc.encoder_type = 0;
        enc.possible_crtcs = 0;
        enc.possible_clones = 0;
@@ -562,6 +598,7 @@ drmModePropertyPtr drmModeGetProperty(int fd, uint32_t property_id)
        struct drm_mode_get_property prop;
        drmModePropertyPtr r;
 
+       VG_CLEAR(prop);
        prop.prop_id = property_id;
        prop.count_enum_blobs = 0;
        prop.count_values = 0;
@@ -575,7 +612,7 @@ drmModePropertyPtr drmModeGetProperty(int fd, uint32_t property_id)
        if (prop.count_values)
                prop.values_ptr = VOID2U64(drmMalloc(prop.count_values * sizeof(uint64_t)));
 
-       if (prop.count_enum_blobs && (prop.flags & DRM_MODE_PROP_ENUM))
+       if (prop.count_enum_blobs && (prop.flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)))
                prop.enum_blob_ptr = VOID2U64(drmMalloc(prop.count_enum_blobs * sizeof(struct drm_mode_property_enum)));
 
        if (prop.count_enum_blobs && (prop.flags & DRM_MODE_PROP_BLOB)) {
@@ -597,7 +634,7 @@ drmModePropertyPtr drmModeGetProperty(int fd, uint32_t property_id)
        r->flags = prop.flags;
        if (prop.count_values)
                r->values = drmAllocCpy(U642VOID(prop.values_ptr), prop.count_values, sizeof(uint64_t));
-       if (prop.flags & DRM_MODE_PROP_ENUM) {
+       if (prop.flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
                r->count_enums = prop.count_enum_blobs;
                r->enums = drmAllocCpy(U642VOID(prop.enum_blob_ptr), prop.count_enum_blobs, sizeof(struct drm_mode_property_enum));
        } else if (prop.flags & DRM_MODE_PROP_BLOB) {
@@ -686,7 +723,7 @@ int drmModeConnectorSetProperty(int fd, uint32_t connector_id, uint32_t property
 */
 int drmCheckModesettingSupported(const char *busid)
 {
-#ifdef __linux__
+#if defined (__linux__)
        char pci_dev_dir[1024];
        int domain, bus, dev, func;
        DIR *sysdir;
@@ -736,6 +773,41 @@ int drmCheckModesettingSupported(const char *busid)
        closedir(sysdir);
        if (found)
                return 0;
+#elif defined (__FreeBSD__) || defined (__FreeBSD_kernel__)
+       char kbusid[1024], sbusid[1024];
+       char oid[128];
+       int domain, bus, dev, func;
+       int i, modesetting, ret;
+       size_t len;
+
+       ret = sscanf(busid, "pci:%04x:%02x:%02x.%d", &domain, &bus, &dev,
+           &func);
+       if (ret != 4)
+               return -EINVAL;
+       snprintf(kbusid, sizeof(kbusid), "pci:%04x:%02x:%02x.%d", domain, bus,
+           dev, func);
+
+       /* How many GPUs do we expect in the machine ? */
+       for (i = 0; i < 16; i++) {
+               snprintf(oid, sizeof(oid), "hw.dri.%d.busid", i);
+               len = sizeof(sbusid);
+               ret = sysctlbyname(oid, sbusid, &len, NULL, 0);
+               if (ret == -1) {
+                       if (errno == ENOENT)
+                               continue;
+                       return -EINVAL;
+               }
+               if (strcmp(sbusid, kbusid) != 0)
+                       continue;
+               snprintf(oid, sizeof(oid), "hw.dri.%d.modesetting", i);
+               len = sizeof(modesetting);
+               ret = sysctlbyname(oid, &modesetting, &len, NULL, 0);
+               if (ret == -1 || len != sizeof(modesetting))
+                       return -EINVAL;
+               return (modesetting ? 0 : -ENOSYS);
+       }
+#elif defined(__DragonFly__)
+       return 0;
 #endif
        return -ENOSYS;
 
@@ -836,7 +908,7 @@ int drmModePageFlip(int fd, uint32_t crtc_id, uint32_t fb_id,
 
 int drmModeSetPlane(int fd, uint32_t plane_id, uint32_t crtc_id,
                    uint32_t fb_id, uint32_t flags,
-                   uint32_t crtc_x, uint32_t crtc_y,
+                   int32_t crtc_x, int32_t crtc_y,
                    uint32_t crtc_w, uint32_t crtc_h,
                    uint32_t src_x, uint32_t src_y,
                    uint32_t src_w, uint32_t src_h)
@@ -974,3 +1046,86 @@ void drmModeFreePlaneResources(drmModePlaneResPtr ptr)
        drmFree(ptr->planes);
        drmFree(ptr);
 }
+
+drmModeObjectPropertiesPtr drmModeObjectGetProperties(int fd,
+                                                     uint32_t object_id,
+                                                     uint32_t object_type)
+{
+       struct drm_mode_obj_get_properties properties;
+       drmModeObjectPropertiesPtr ret = NULL;
+       uint32_t count;
+
+retry:
+       memset(&properties, 0, sizeof(struct drm_mode_obj_get_properties));
+       properties.obj_id = object_id;
+       properties.obj_type = object_type;
+
+       if (drmIoctl(fd, DRM_IOCTL_MODE_OBJ_GETPROPERTIES, &properties))
+               return 0;
+
+       count = properties.count_props;
+
+       if (count) {
+               properties.props_ptr = VOID2U64(drmMalloc(count *
+                                                         sizeof(uint32_t)));
+               if (!properties.props_ptr)
+                       goto err_allocs;
+               properties.prop_values_ptr = VOID2U64(drmMalloc(count *
+                                                     sizeof(uint64_t)));
+               if (!properties.prop_values_ptr)
+                       goto err_allocs;
+       }
+
+       if (drmIoctl(fd, DRM_IOCTL_MODE_OBJ_GETPROPERTIES, &properties))
+               goto err_allocs;
+
+       if (count < properties.count_props) {
+               drmFree(U642VOID(properties.props_ptr));
+               drmFree(U642VOID(properties.prop_values_ptr));
+               goto retry;
+       }
+       count = properties.count_props;
+
+       ret = drmMalloc(sizeof(*ret));
+       if (!ret)
+               goto err_allocs;
+
+       ret->count_props = count;
+       ret->props = drmAllocCpy(U642VOID(properties.props_ptr),
+                                count, sizeof(uint32_t));
+       ret->prop_values = drmAllocCpy(U642VOID(properties.prop_values_ptr),
+                                      count, sizeof(uint64_t));
+       if (ret->count_props && (!ret->props || !ret->prop_values)) {
+               drmFree(ret->props);
+               drmFree(ret->prop_values);
+               drmFree(ret);
+               ret = NULL;
+       }
+
+err_allocs:
+       drmFree(U642VOID(properties.props_ptr));
+       drmFree(U642VOID(properties.prop_values_ptr));
+       return ret;
+}
+
+void drmModeFreeObjectProperties(drmModeObjectPropertiesPtr ptr)
+{
+       if (!ptr)
+               return;
+       drmFree(ptr->props);
+       drmFree(ptr->prop_values);
+       drmFree(ptr);
+}
+
+int drmModeObjectSetProperty(int fd, uint32_t object_id, uint32_t object_type,
+                            uint32_t property_id, uint64_t value)
+{
+       struct drm_mode_obj_set_property prop;
+
+       prop.value = value;
+       prop.prop_id = property_id;
+       prop.obj_id = object_id;
+       prop.obj_type = object_type;
+
+       return DRM_IOCTL(fd, DRM_IOCTL_MODE_OBJ_SETPROPERTY, &prop);
+}