OSDN Git Service

intel: add symbols test
[android-x86/external-libdrm.git] / xf86drmMode.c
index 6b60c35..1333da4 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 memclear(s) memset(&s, 0, sizeof(s))
+
 #define U642VOID(x) ((void *)(unsigned long)(x))
 #define VOID2U64(x) ((uint64_t)(unsigned long)(x))
 
@@ -62,7 +76,7 @@ static inline int DRM_IOCTL(int fd, unsigned long cmd, void *arg)
  * Util functions
  */
 
-void* drmAllocCpy(void *array, int count, int entry_size)
+static void* drmAllocCpy(char *array, int count, int entry_size)
 {
        char *r;
        int i;
@@ -150,7 +164,7 @@ drmModeResPtr drmModeGetResources(int fd)
        drmModeResPtr r = 0;
 
 retry:
-       memset(&res, 0, sizeof(struct drm_mode_card_res));
+       memclear(res);
        if (drmIoctl(fd, DRM_IOCTL_MODE_GETRESOURCES, &res))
                return 0;
 
@@ -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;
 
+       memclear(f);
        f.width  = width;
        f.height = height;
        f.pitch  = pitch;
@@ -267,6 +282,7 @@ int drmModeAddFB2(int fd, uint32_t width, uint32_t height,
        struct drm_mode_fb_cmd2 f;
        int ret;
 
+       memclear(f);
        f.width  = width;
        f.height = height;
        f.pixel_format = pixel_format;
@@ -285,8 +301,6 @@ int drmModeAddFB2(int fd, uint32_t width, uint32_t height,
 int drmModeRmFB(int fd, uint32_t bufferId)
 {
        return DRM_IOCTL(fd, DRM_IOCTL_MODE_RMFB, &bufferId);
-
-
 }
 
 drmModeFBPtr drmModeGetFB(int fd, uint32_t buf)
@@ -294,6 +308,7 @@ drmModeFBPtr drmModeGetFB(int fd, uint32_t buf)
        struct drm_mode_fb_cmd info;
        drmModeFBPtr r;
 
+       memclear(info);
        info.fb_id = buf;
 
        if (drmIoctl(fd, DRM_IOCTL_MODE_GETFB, &info))
@@ -316,8 +331,9 @@ drmModeFBPtr drmModeGetFB(int fd, uint32_t buf)
 int drmModeDirtyFB(int fd, uint32_t bufferId,
                   drmModeClipPtr clips, uint32_t num_clips)
 {
-       struct drm_mode_fb_dirty_cmd dirty = { 0 };
+       struct drm_mode_fb_dirty_cmd dirty;
 
+       memclear(dirty);
        dirty.fb_id = bufferId;
        dirty.clips_ptr = VOID2U64(clips);
        dirty.num_clips = num_clips;
@@ -335,6 +351,7 @@ drmModeCrtcPtr drmModeGetCrtc(int fd, uint32_t crtcId)
        struct drm_mode_crtc crtc;
        drmModeCrtcPtr r;
 
+       memclear(crtc);
        crtc.crtc_id = crtcId;
 
        if (drmIoctl(fd, DRM_IOCTL_MODE_GETCRTC, &crtc))
@@ -368,6 +385,7 @@ int drmModeSetCrtc(int fd, uint32_t crtcId, uint32_t bufferId,
 {
        struct drm_mode_crtc crtc;
 
+       memclear(crtc);
        crtc.x             = x;
        crtc.y             = y;
        crtc.crtc_id       = crtcId;
@@ -377,8 +395,7 @@ int drmModeSetCrtc(int fd, uint32_t crtcId, uint32_t bufferId,
        if (mode) {
          memcpy(&crtc.mode, mode, sizeof(struct drm_mode_modeinfo));
          crtc.mode_valid = 1;
-       } else
-         crtc.mode_valid = 0;
+       }
 
        return DRM_IOCTL(fd, DRM_IOCTL_MODE_SETCRTC, &crtc);
 }
@@ -391,6 +408,7 @@ int drmModeSetCursor(int fd, uint32_t crtcId, uint32_t bo_handle, uint32_t width
 {
        struct drm_mode_cursor arg;
 
+       memclear(arg);
        arg.flags = DRM_MODE_CURSOR_BO;
        arg.crtc_id = crtcId;
        arg.width = width;
@@ -404,6 +422,7 @@ int drmModeSetCursor2(int fd, uint32_t crtcId, uint32_t bo_handle, uint32_t widt
 {
        struct drm_mode_cursor2 arg;
 
+       memclear(arg);
        arg.flags = DRM_MODE_CURSOR_BO;
        arg.crtc_id = crtcId;
        arg.width = width;
@@ -419,6 +438,7 @@ int drmModeMoveCursor(int fd, uint32_t crtcId, int x, int y)
 {
        struct drm_mode_cursor arg;
 
+       memclear(arg);
        arg.flags = DRM_MODE_CURSOR_MOVE;
        arg.crtc_id = crtcId;
        arg.x = x;
@@ -435,10 +455,8 @@ drmModeEncoderPtr drmModeGetEncoder(int fd, uint32_t encoder_id)
        struct drm_mode_get_encoder enc;
        drmModeEncoderPtr r = NULL;
 
+       memclear(enc);
        enc.encoder_id = encoder_id;
-       enc.encoder_type = 0;
-       enc.possible_crtcs = 0;
-       enc.possible_clones = 0;
 
        if (drmIoctl(fd, DRM_IOCTL_MODE_GETENCODER, &enc))
                return 0;
@@ -458,19 +476,23 @@ drmModeEncoderPtr drmModeGetEncoder(int fd, uint32_t encoder_id)
 /*
  * Connector manipulation
  */
-
-drmModeConnectorPtr drmModeGetConnector(int fd, uint32_t connector_id)
+static drmModeConnectorPtr
+_drmModeGetConnector(int fd, uint32_t connector_id, int probe)
 {
        struct drm_mode_get_connector conn, counts;
        drmModeConnectorPtr r = NULL;
 
-retry:
-       memset(&conn, 0, sizeof(struct drm_mode_get_connector));
+       memclear(conn);
        conn.connector_id = connector_id;
+       if (!probe) {
+               conn.count_modes = 1;
+               conn.modes_ptr = VOID2U64(drmMalloc(sizeof(struct drm_mode_modeinfo)));
+       }
 
        if (drmIoctl(fd, DRM_IOCTL_MODE_GETCONNECTOR, &conn))
                return 0;
 
+retry:
        counts = conn;
 
        if (conn.count_props) {
@@ -486,6 +508,9 @@ retry:
                conn.modes_ptr = VOID2U64(drmMalloc(conn.count_modes*sizeof(struct drm_mode_modeinfo)));
                if (!conn.modes_ptr)
                        goto err_allocs;
+       } else {
+               conn.count_modes = 1;
+               conn.modes_ptr = VOID2U64(drmMalloc(sizeof(struct drm_mode_modeinfo)));
        }
 
        if (conn.count_encoders) {
@@ -554,10 +579,21 @@ err_allocs:
        return r;
 }
 
+drmModeConnectorPtr drmModeGetConnector(int fd, uint32_t connector_id)
+{
+       return _drmModeGetConnector(fd, connector_id, 1);
+}
+
+drmModeConnectorPtr drmModeGetConnectorCurrent(int fd, uint32_t connector_id)
+{
+       return _drmModeGetConnector(fd, connector_id, 0);
+}
+
 int drmModeAttachMode(int fd, uint32_t connector_id, drmModeModeInfoPtr mode_info)
 {
        struct drm_mode_mode_cmd res;
 
+       memclear(res);
        memcpy(&res.mode, mode_info, sizeof(struct drm_mode_modeinfo));
        res.connector_id = connector_id;
 
@@ -568,6 +604,7 @@ int drmModeDetachMode(int fd, uint32_t connector_id, drmModeModeInfoPtr mode_inf
 {
        struct drm_mode_mode_cmd res;
 
+       memclear(res);
        memcpy(&res.mode, mode_info, sizeof(struct drm_mode_modeinfo));
        res.connector_id = connector_id;
 
@@ -580,12 +617,8 @@ drmModePropertyPtr drmModeGetProperty(int fd, uint32_t property_id)
        struct drm_mode_get_property prop;
        drmModePropertyPtr r;
 
+       memclear(prop);
        prop.prop_id = property_id;
-       prop.count_enum_blobs = 0;
-       prop.count_values = 0;
-       prop.flags = 0;
-       prop.enum_blob_ptr = 0;
-       prop.values_ptr = 0;
 
        if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPERTY, &prop))
                return 0;
@@ -648,8 +681,7 @@ drmModePropertyBlobPtr drmModeGetPropertyBlob(int fd, uint32_t blob_id)
        struct drm_mode_get_blob blob;
        drmModePropertyBlobPtr r;
 
-       blob.length = 0;
-       blob.data = 0;
+       memclear(blob);
        blob.blob_id = blob_id;
 
        if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPBLOB, &blob))
@@ -689,6 +721,7 @@ int drmModeConnectorSetProperty(int fd, uint32_t connector_id, uint32_t property
 {
        struct drm_mode_connector_set_property osp;
 
+       memclear(osp);
        osp.connector_id = connector_id;
        osp.prop_id = property_id;
        osp.value = value;
@@ -704,7 +737,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;
@@ -754,6 +787,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;
 
@@ -764,6 +832,7 @@ int drmModeCrtcGetGamma(int fd, uint32_t crtc_id, uint32_t size,
 {
        struct drm_mode_crtc_lut l;
 
+       memclear(l);
        l.crtc_id = crtc_id;
        l.gamma_size = size;
        l.red = VOID2U64(red);
@@ -778,6 +847,7 @@ int drmModeCrtcSetGamma(int fd, uint32_t crtc_id, uint32_t size,
 {
        struct drm_mode_crtc_lut l;
 
+       memclear(l);
        l.crtc_id = crtc_id;
        l.gamma_size = size;
        l.red = VOID2U64(red);
@@ -800,7 +870,7 @@ int drmHandleEvent(int fd, drmEventContextPtr evctx)
        len = read(fd, buffer, sizeof buffer);
        if (len == 0)
                return 0;
-       if (len < sizeof *e)
+       if (len < (int)sizeof *e)
                return -1;
 
        i = 0;
@@ -843,18 +913,18 @@ int drmModePageFlip(int fd, uint32_t crtc_id, uint32_t fb_id,
 {
        struct drm_mode_crtc_page_flip flip;
 
+       memclear(flip);
        flip.fb_id = fb_id;
        flip.crtc_id = crtc_id;
        flip.user_data = VOID2U64(user_data);
        flip.flags = flags;
-       flip.reserved = 0;
 
        return DRM_IOCTL(fd, DRM_IOCTL_MODE_PAGE_FLIP, &flip);
 }
 
 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)
@@ -862,6 +932,7 @@ int drmModeSetPlane(int fd, uint32_t plane_id, uint32_t crtc_id,
 {
        struct drm_mode_set_plane s;
 
+       memclear(s);
        s.plane_id = plane_id;
        s.crtc_id = crtc_id;
        s.fb_id = fb_id;
@@ -885,7 +956,7 @@ drmModePlanePtr drmModeGetPlane(int fd, uint32_t plane_id)
        drmModePlanePtr r = 0;
 
 retry:
-       memset(&ovr, 0, sizeof(struct drm_mode_get_plane));
+       memclear(ovr);
        ovr.plane_id = plane_id;
        if (drmIoctl(fd, DRM_IOCTL_MODE_GETPLANE, &ovr))
                return 0;
@@ -945,7 +1016,7 @@ drmModePlaneResPtr drmModeGetPlaneResources(int fd)
        drmModePlaneResPtr r = 0;
 
 retry:
-       memset(&res, 0, sizeof(struct drm_mode_get_plane_res));
+       memclear(res);
        if (drmIoctl(fd, DRM_IOCTL_MODE_GETPLANERESOURCES, &res))
                return 0;
 
@@ -1002,7 +1073,7 @@ drmModeObjectPropertiesPtr drmModeObjectGetProperties(int fd,
        uint32_t count;
 
 retry:
-       memset(&properties, 0, sizeof(struct drm_mode_obj_get_properties));
+       memclear(properties);
        properties.obj_id = object_id;
        properties.obj_type = object_type;
 
@@ -1068,6 +1139,7 @@ int drmModeObjectSetProperty(int fd, uint32_t object_id, uint32_t object_type,
 {
        struct drm_mode_obj_set_property prop;
 
+       memclear(prop);
        prop.value = value;
        prop.prop_id = property_id;
        prop.obj_id = object_id;