Jesse Barnes | 731cd55 | 2008-12-17 10:09:49 -0800 | [diff] [blame] | 1 | /* |
| 2 | * \file xf86drmMode.c |
| 3 | * Header for DRM modesetting interface. |
| 4 | * |
| 5 | * \author Jakob Bornecrantz <wallbraker@gmail.com> |
| 6 | * |
| 7 | * \par Acknowledgements: |
| 8 | * Feb 2007, Dave Airlie <airlied@linux.ie> |
| 9 | */ |
| 10 | |
| 11 | /* |
| 12 | * Copyright (c) 2007-2008 Tungsten Graphics, Inc., Cedar Park, Texas. |
| 13 | * Copyright (c) 2007-2008 Dave Airlie <airlied@linux.ie> |
| 14 | * Copyright (c) 2007-2008 Jakob Bornecrantz <wallbraker@gmail.com> |
| 15 | * |
| 16 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 17 | * copy of this software and associated documentation files (the "Software"), |
| 18 | * to deal in the Software without restriction, including without limitation |
| 19 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 20 | * and/or sell copies of the Software, and to permit persons to whom the |
| 21 | * Software is furnished to do so, subject to the following conditions: |
| 22 | * |
| 23 | * The above copyright notice and this permission notice shall be included in |
| 24 | * all copies or substantial portions of the Software. |
| 25 | * |
| 26 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 27 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 28 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 29 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 30 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 31 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 32 | * IN THE SOFTWARE. |
| 33 | * |
| 34 | */ |
| 35 | |
Luigi Santivetti | e641e2a | 2021-03-25 22:42:38 +0000 | [diff] [blame] | 36 | #include <assert.h> |
Ville Syrjälä | ed44e0b | 2015-06-22 17:26:02 +0100 | [diff] [blame] | 37 | #include <limits.h> |
Jesse Barnes | 731cd55 | 2008-12-17 10:09:49 -0800 | [diff] [blame] | 38 | #include <stdint.h> |
Ville Syrjälä | ed44e0b | 2015-06-22 17:26:02 +0100 | [diff] [blame] | 39 | #include <stdlib.h> |
Jesse Barnes | 731cd55 | 2008-12-17 10:09:49 -0800 | [diff] [blame] | 40 | #include <sys/ioctl.h> |
Eric Engestrom | 074947e | 2018-11-07 15:00:24 +0000 | [diff] [blame] | 41 | #if HAVE_SYS_SYSCTL_H |
Eleni Maria Stea | 90c9175 | 2021-06-07 10:19:18 +0300 | [diff] [blame] | 42 | #ifdef __FreeBSD__ |
| 43 | #include <sys/types.h> |
| 44 | #endif |
Julien Cristau | fc8c3e2 | 2015-07-06 12:45:31 +0100 | [diff] [blame] | 45 | #include <sys/sysctl.h> |
| 46 | #endif |
Jesse Barnes | 731cd55 | 2008-12-17 10:09:49 -0800 | [diff] [blame] | 47 | #include <stdio.h> |
Ville Syrjälä | ed44e0b | 2015-06-22 17:26:02 +0100 | [diff] [blame] | 48 | #include <stdbool.h> |
Jesse Barnes | 731cd55 | 2008-12-17 10:09:49 -0800 | [diff] [blame] | 49 | |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 50 | #include "libdrm_macros.h" |
Jesse Barnes | 731cd55 | 2008-12-17 10:09:49 -0800 | [diff] [blame] | 51 | #include "xf86drmMode.h" |
| 52 | #include "xf86drm.h" |
| 53 | #include <drm.h> |
Luigi Santivetti | e641e2a | 2021-03-25 22:42:38 +0000 | [diff] [blame] | 54 | #include <drm_fourcc.h> |
Jesse Barnes | 731cd55 | 2008-12-17 10:09:49 -0800 | [diff] [blame] | 55 | #include <string.h> |
| 56 | #include <dirent.h> |
Kristian Høgsberg | b0b9663 | 2009-09-11 13:27:35 -0400 | [diff] [blame] | 57 | #include <unistd.h> |
Jesse Barnes | 731cd55 | 2008-12-17 10:09:49 -0800 | [diff] [blame] | 58 | #include <errno.h> |
| 59 | |
Daniel Vetter | 7e0460c | 2015-02-11 12:03:12 +0100 | [diff] [blame] | 60 | #define memclear(s) memset(&s, 0, sizeof(s)) |
Eric Anholt | 734de70 | 2013-12-28 22:06:51 -0800 | [diff] [blame] | 61 | |
Jesse Barnes | 731cd55 | 2008-12-17 10:09:49 -0800 | [diff] [blame] | 62 | #define U642VOID(x) ((void *)(unsigned long)(x)) |
| 63 | #define VOID2U64(x) ((uint64_t)(unsigned long)(x)) |
| 64 | |
Marcin Slusarz | 763b618 | 2011-06-05 18:53:16 +0200 | [diff] [blame] | 65 | static inline int DRM_IOCTL(int fd, unsigned long cmd, void *arg) |
Chris Wilson | b803918 | 2010-07-01 22:38:54 +0100 | [diff] [blame] | 66 | { |
| 67 | int ret = drmIoctl(fd, cmd, arg); |
| 68 | return ret < 0 ? -errno : ret; |
| 69 | } |
| 70 | |
Jesse Barnes | 731cd55 | 2008-12-17 10:09:49 -0800 | [diff] [blame] | 71 | /* |
| 72 | * Util functions |
| 73 | */ |
| 74 | |
Jan Vesely | 65041c4 | 2015-02-27 11:51:05 -0500 | [diff] [blame] | 75 | static void* drmAllocCpy(char *array, int count, int entry_size) |
Jesse Barnes | 731cd55 | 2008-12-17 10:09:49 -0800 | [diff] [blame] | 76 | { |
| 77 | char *r; |
| 78 | int i; |
| 79 | |
| 80 | if (!count || !array || !entry_size) |
| 81 | return 0; |
| 82 | |
| 83 | if (!(r = drmMalloc(count*entry_size))) |
| 84 | return 0; |
| 85 | |
| 86 | for (i = 0; i < count; i++) |
| 87 | memcpy(r+(entry_size*i), array+(entry_size*i), entry_size); |
| 88 | |
| 89 | return r; |
| 90 | } |
| 91 | |
| 92 | /* |
| 93 | * A couple of free functions. |
| 94 | */ |
| 95 | |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 96 | drm_public void drmModeFreeModeInfo(drmModeModeInfoPtr ptr) |
Jesse Barnes | 731cd55 | 2008-12-17 10:09:49 -0800 | [diff] [blame] | 97 | { |
| 98 | if (!ptr) |
| 99 | return; |
| 100 | |
| 101 | drmFree(ptr); |
| 102 | } |
| 103 | |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 104 | drm_public void drmModeFreeResources(drmModeResPtr ptr) |
Jesse Barnes | 731cd55 | 2008-12-17 10:09:49 -0800 | [diff] [blame] | 105 | { |
| 106 | if (!ptr) |
| 107 | return; |
| 108 | |
Ville Syrjälä | df497e9 | 2012-02-02 14:53:39 -0500 | [diff] [blame] | 109 | drmFree(ptr->fbs); |
| 110 | drmFree(ptr->crtcs); |
| 111 | drmFree(ptr->connectors); |
| 112 | drmFree(ptr->encoders); |
Jesse Barnes | 731cd55 | 2008-12-17 10:09:49 -0800 | [diff] [blame] | 113 | drmFree(ptr); |
Jesse Barnes | 731cd55 | 2008-12-17 10:09:49 -0800 | [diff] [blame] | 114 | } |
| 115 | |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 116 | drm_public void drmModeFreeFB(drmModeFBPtr ptr) |
Jesse Barnes | 731cd55 | 2008-12-17 10:09:49 -0800 | [diff] [blame] | 117 | { |
| 118 | if (!ptr) |
| 119 | return; |
| 120 | |
| 121 | /* we might add more frees later. */ |
| 122 | drmFree(ptr); |
| 123 | } |
| 124 | |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 125 | drm_public void drmModeFreeCrtc(drmModeCrtcPtr ptr) |
Jesse Barnes | 731cd55 | 2008-12-17 10:09:49 -0800 | [diff] [blame] | 126 | { |
| 127 | if (!ptr) |
| 128 | return; |
| 129 | |
| 130 | drmFree(ptr); |
Jesse Barnes | 731cd55 | 2008-12-17 10:09:49 -0800 | [diff] [blame] | 131 | } |
| 132 | |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 133 | drm_public void drmModeFreeConnector(drmModeConnectorPtr ptr) |
Jesse Barnes | 731cd55 | 2008-12-17 10:09:49 -0800 | [diff] [blame] | 134 | { |
| 135 | if (!ptr) |
| 136 | return; |
| 137 | |
Keith Packard | 0a24654 | 2009-09-17 17:28:08 -0700 | [diff] [blame] | 138 | drmFree(ptr->encoders); |
| 139 | drmFree(ptr->prop_values); |
| 140 | drmFree(ptr->props); |
Jesse Barnes | 731cd55 | 2008-12-17 10:09:49 -0800 | [diff] [blame] | 141 | drmFree(ptr->modes); |
| 142 | drmFree(ptr); |
Jesse Barnes | 731cd55 | 2008-12-17 10:09:49 -0800 | [diff] [blame] | 143 | } |
| 144 | |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 145 | drm_public void drmModeFreeEncoder(drmModeEncoderPtr ptr) |
Jesse Barnes | 731cd55 | 2008-12-17 10:09:49 -0800 | [diff] [blame] | 146 | { |
| 147 | drmFree(ptr); |
| 148 | } |
| 149 | |
| 150 | /* |
| 151 | * ModeSetting functions. |
| 152 | */ |
| 153 | |
Simon Ser | 523b365 | 2021-02-23 18:19:33 +0100 | [diff] [blame] | 154 | drm_public int drmIsKMS(int fd) |
| 155 | { |
| 156 | struct drm_mode_card_res res = {0}; |
| 157 | |
| 158 | if (drmIoctl(fd, DRM_IOCTL_MODE_GETRESOURCES, &res) != 0) |
| 159 | return 0; |
| 160 | |
| 161 | return res.count_crtcs > 0 && res.count_connectors > 0 && res.count_encoders > 0; |
| 162 | } |
| 163 | |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 164 | drm_public drmModeResPtr drmModeGetResources(int fd) |
Jesse Barnes | 731cd55 | 2008-12-17 10:09:49 -0800 | [diff] [blame] | 165 | { |
Chris Wilson | d1308f4 | 2010-01-06 15:19:25 +0000 | [diff] [blame] | 166 | struct drm_mode_card_res res, counts; |
Jesse Barnes | 731cd55 | 2008-12-17 10:09:49 -0800 | [diff] [blame] | 167 | drmModeResPtr r = 0; |
| 168 | |
Chris Wilson | d1308f4 | 2010-01-06 15:19:25 +0000 | [diff] [blame] | 169 | retry: |
Daniel Vetter | 7e0460c | 2015-02-11 12:03:12 +0100 | [diff] [blame] | 170 | memclear(res); |
Jesse Barnes | 731cd55 | 2008-12-17 10:09:49 -0800 | [diff] [blame] | 171 | if (drmIoctl(fd, DRM_IOCTL_MODE_GETRESOURCES, &res)) |
| 172 | return 0; |
| 173 | |
Chris Wilson | d1308f4 | 2010-01-06 15:19:25 +0000 | [diff] [blame] | 174 | counts = res; |
| 175 | |
Chris Wilson | 85fb3e5 | 2010-01-06 15:39:49 +0000 | [diff] [blame] | 176 | if (res.count_fbs) { |
Jesse Barnes | 731cd55 | 2008-12-17 10:09:49 -0800 | [diff] [blame] | 177 | res.fb_id_ptr = VOID2U64(drmMalloc(res.count_fbs*sizeof(uint32_t))); |
Chris Wilson | 85fb3e5 | 2010-01-06 15:39:49 +0000 | [diff] [blame] | 178 | if (!res.fb_id_ptr) |
| 179 | goto err_allocs; |
| 180 | } |
| 181 | if (res.count_crtcs) { |
Jesse Barnes | 731cd55 | 2008-12-17 10:09:49 -0800 | [diff] [blame] | 182 | res.crtc_id_ptr = VOID2U64(drmMalloc(res.count_crtcs*sizeof(uint32_t))); |
Chris Wilson | 85fb3e5 | 2010-01-06 15:39:49 +0000 | [diff] [blame] | 183 | if (!res.crtc_id_ptr) |
| 184 | goto err_allocs; |
| 185 | } |
| 186 | if (res.count_connectors) { |
Jesse Barnes | 731cd55 | 2008-12-17 10:09:49 -0800 | [diff] [blame] | 187 | res.connector_id_ptr = VOID2U64(drmMalloc(res.count_connectors*sizeof(uint32_t))); |
Chris Wilson | 85fb3e5 | 2010-01-06 15:39:49 +0000 | [diff] [blame] | 188 | if (!res.connector_id_ptr) |
| 189 | goto err_allocs; |
| 190 | } |
| 191 | if (res.count_encoders) { |
Jesse Barnes | 731cd55 | 2008-12-17 10:09:49 -0800 | [diff] [blame] | 192 | res.encoder_id_ptr = VOID2U64(drmMalloc(res.count_encoders*sizeof(uint32_t))); |
Chris Wilson | 85fb3e5 | 2010-01-06 15:39:49 +0000 | [diff] [blame] | 193 | if (!res.encoder_id_ptr) |
| 194 | goto err_allocs; |
| 195 | } |
Jesse Barnes | 731cd55 | 2008-12-17 10:09:49 -0800 | [diff] [blame] | 196 | |
Chris Wilson | d1308f4 | 2010-01-06 15:19:25 +0000 | [diff] [blame] | 197 | if (drmIoctl(fd, DRM_IOCTL_MODE_GETRESOURCES, &res)) |
Jesse Barnes | 731cd55 | 2008-12-17 10:09:49 -0800 | [diff] [blame] | 198 | goto err_allocs; |
Chris Wilson | d1308f4 | 2010-01-06 15:19:25 +0000 | [diff] [blame] | 199 | |
| 200 | /* The number of available connectors and etc may have changed with a |
| 201 | * hotplug event in between the ioctls, in which case the field is |
| 202 | * silently ignored by the kernel. |
| 203 | */ |
| 204 | if (counts.count_fbs < res.count_fbs || |
| 205 | counts.count_crtcs < res.count_crtcs || |
| 206 | counts.count_connectors < res.count_connectors || |
| 207 | counts.count_encoders < res.count_encoders) |
| 208 | { |
| 209 | drmFree(U642VOID(res.fb_id_ptr)); |
| 210 | drmFree(U642VOID(res.crtc_id_ptr)); |
| 211 | drmFree(U642VOID(res.connector_id_ptr)); |
| 212 | drmFree(U642VOID(res.encoder_id_ptr)); |
| 213 | |
| 214 | goto retry; |
Jesse Barnes | 731cd55 | 2008-12-17 10:09:49 -0800 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | /* |
| 218 | * return |
| 219 | */ |
Jesse Barnes | 731cd55 | 2008-12-17 10:09:49 -0800 | [diff] [blame] | 220 | if (!(r = drmMalloc(sizeof(*r)))) |
Chris Wilson | d1308f4 | 2010-01-06 15:19:25 +0000 | [diff] [blame] | 221 | goto err_allocs; |
Jesse Barnes | 731cd55 | 2008-12-17 10:09:49 -0800 | [diff] [blame] | 222 | |
| 223 | r->min_width = res.min_width; |
| 224 | r->max_width = res.max_width; |
| 225 | r->min_height = res.min_height; |
| 226 | r->max_height = res.max_height; |
| 227 | r->count_fbs = res.count_fbs; |
| 228 | r->count_crtcs = res.count_crtcs; |
| 229 | r->count_connectors = res.count_connectors; |
| 230 | r->count_encoders = res.count_encoders; |
Chris Wilson | 85fb3e5 | 2010-01-06 15:39:49 +0000 | [diff] [blame] | 231 | |
| 232 | r->fbs = drmAllocCpy(U642VOID(res.fb_id_ptr), res.count_fbs, sizeof(uint32_t)); |
| 233 | r->crtcs = drmAllocCpy(U642VOID(res.crtc_id_ptr), res.count_crtcs, sizeof(uint32_t)); |
| 234 | r->connectors = drmAllocCpy(U642VOID(res.connector_id_ptr), res.count_connectors, sizeof(uint32_t)); |
| 235 | r->encoders = drmAllocCpy(U642VOID(res.encoder_id_ptr), res.count_encoders, sizeof(uint32_t)); |
Chris Wilson | e6c136c | 2010-01-06 16:53:33 +0000 | [diff] [blame] | 236 | if ((res.count_fbs && !r->fbs) || |
| 237 | (res.count_crtcs && !r->crtcs) || |
| 238 | (res.count_connectors && !r->connectors) || |
| 239 | (res.count_encoders && !r->encoders)) |
| 240 | { |
Chris Wilson | 85fb3e5 | 2010-01-06 15:39:49 +0000 | [diff] [blame] | 241 | drmFree(r->fbs); |
| 242 | drmFree(r->crtcs); |
| 243 | drmFree(r->connectors); |
| 244 | drmFree(r->encoders); |
| 245 | drmFree(r); |
| 246 | r = 0; |
| 247 | } |
Jesse Barnes | 731cd55 | 2008-12-17 10:09:49 -0800 | [diff] [blame] | 248 | |
| 249 | err_allocs: |
| 250 | drmFree(U642VOID(res.fb_id_ptr)); |
| 251 | drmFree(U642VOID(res.crtc_id_ptr)); |
| 252 | drmFree(U642VOID(res.connector_id_ptr)); |
| 253 | drmFree(U642VOID(res.encoder_id_ptr)); |
| 254 | |
| 255 | return r; |
| 256 | } |
| 257 | |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 258 | |
| 259 | drm_public int drmModeAddFB(int fd, uint32_t width, uint32_t height, uint8_t depth, |
| 260 | uint8_t bpp, uint32_t pitch, uint32_t bo_handle, |
| 261 | uint32_t *buf_id) |
Jesse Barnes | 731cd55 | 2008-12-17 10:09:49 -0800 | [diff] [blame] | 262 | { |
| 263 | struct drm_mode_fb_cmd f; |
| 264 | int ret; |
| 265 | |
Daniel Vetter | 7e0460c | 2015-02-11 12:03:12 +0100 | [diff] [blame] | 266 | memclear(f); |
Jesse Barnes | 731cd55 | 2008-12-17 10:09:49 -0800 | [diff] [blame] | 267 | f.width = width; |
| 268 | f.height = height; |
| 269 | f.pitch = pitch; |
| 270 | f.bpp = bpp; |
| 271 | f.depth = depth; |
| 272 | f.handle = bo_handle; |
| 273 | |
Chris Wilson | b803918 | 2010-07-01 22:38:54 +0100 | [diff] [blame] | 274 | if ((ret = DRM_IOCTL(fd, DRM_IOCTL_MODE_ADDFB, &f))) |
Jesse Barnes | 731cd55 | 2008-12-17 10:09:49 -0800 | [diff] [blame] | 275 | return ret; |
| 276 | |
| 277 | *buf_id = f.fb_id; |
| 278 | return 0; |
| 279 | } |
| 280 | |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 281 | drm_public int drmModeAddFB2WithModifiers(int fd, uint32_t width, |
| 282 | uint32_t height, uint32_t pixel_format, const uint32_t bo_handles[4], |
| 283 | const uint32_t pitches[4], const uint32_t offsets[4], |
| 284 | const uint64_t modifier[4], uint32_t *buf_id, uint32_t flags) |
Jesse Barnes | ac168bf | 2011-04-29 08:53:53 -0700 | [diff] [blame] | 285 | { |
| 286 | struct drm_mode_fb_cmd2 f; |
| 287 | int ret; |
| 288 | |
Daniel Vetter | 7e0460c | 2015-02-11 12:03:12 +0100 | [diff] [blame] | 289 | memclear(f); |
Jesse Barnes | ac168bf | 2011-04-29 08:53:53 -0700 | [diff] [blame] | 290 | f.width = width; |
| 291 | f.height = height; |
| 292 | f.pixel_format = pixel_format; |
| 293 | f.flags = flags; |
Ville Syrjälä | 76b4a69 | 2012-02-02 14:53:43 -0500 | [diff] [blame] | 294 | memcpy(f.handles, bo_handles, 4 * sizeof(bo_handles[0])); |
| 295 | memcpy(f.pitches, pitches, 4 * sizeof(pitches[0])); |
| 296 | memcpy(f.offsets, offsets, 4 * sizeof(offsets[0])); |
Bas Nieuwenhuizen | 40f73d0 | 2021-04-21 21:31:06 +0200 | [diff] [blame] | 297 | if (modifier) |
Kristian H. Kristensen | abfa680 | 2016-09-08 13:08:59 -0700 | [diff] [blame] | 298 | memcpy(f.modifier, modifier, 4 * sizeof(modifier[0])); |
Jesse Barnes | ac168bf | 2011-04-29 08:53:53 -0700 | [diff] [blame] | 299 | |
| 300 | if ((ret = DRM_IOCTL(fd, DRM_IOCTL_MODE_ADDFB2, &f))) |
| 301 | return ret; |
| 302 | |
| 303 | *buf_id = f.fb_id; |
| 304 | return 0; |
| 305 | } |
| 306 | |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 307 | drm_public int drmModeAddFB2(int fd, uint32_t width, uint32_t height, |
| 308 | uint32_t pixel_format, const uint32_t bo_handles[4], |
| 309 | const uint32_t pitches[4], const uint32_t offsets[4], |
| 310 | uint32_t *buf_id, uint32_t flags) |
Kristian H. Kristensen | abfa680 | 2016-09-08 13:08:59 -0700 | [diff] [blame] | 311 | { |
| 312 | return drmModeAddFB2WithModifiers(fd, width, height, |
| 313 | pixel_format, bo_handles, |
| 314 | pitches, offsets, NULL, |
| 315 | buf_id, flags); |
| 316 | } |
| 317 | |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 318 | drm_public int drmModeRmFB(int fd, uint32_t bufferId) |
Jesse Barnes | 731cd55 | 2008-12-17 10:09:49 -0800 | [diff] [blame] | 319 | { |
Chris Wilson | b803918 | 2010-07-01 22:38:54 +0100 | [diff] [blame] | 320 | return DRM_IOCTL(fd, DRM_IOCTL_MODE_RMFB, &bufferId); |
Jesse Barnes | 731cd55 | 2008-12-17 10:09:49 -0800 | [diff] [blame] | 321 | } |
| 322 | |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 323 | drm_public drmModeFBPtr drmModeGetFB(int fd, uint32_t buf) |
Jesse Barnes | 731cd55 | 2008-12-17 10:09:49 -0800 | [diff] [blame] | 324 | { |
| 325 | struct drm_mode_fb_cmd info; |
| 326 | drmModeFBPtr r; |
| 327 | |
Daniel Vetter | 7e0460c | 2015-02-11 12:03:12 +0100 | [diff] [blame] | 328 | memclear(info); |
Jesse Barnes | 731cd55 | 2008-12-17 10:09:49 -0800 | [diff] [blame] | 329 | info.fb_id = buf; |
| 330 | |
| 331 | if (drmIoctl(fd, DRM_IOCTL_MODE_GETFB, &info)) |
| 332 | return NULL; |
| 333 | |
| 334 | if (!(r = drmMalloc(sizeof(*r)))) |
| 335 | return NULL; |
| 336 | |
| 337 | r->fb_id = info.fb_id; |
| 338 | r->width = info.width; |
| 339 | r->height = info.height; |
| 340 | r->pitch = info.pitch; |
| 341 | r->bpp = info.bpp; |
| 342 | r->handle = info.handle; |
| 343 | r->depth = info.depth; |
| 344 | |
| 345 | return r; |
| 346 | } |
| 347 | |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 348 | drm_public int drmModeDirtyFB(int fd, uint32_t bufferId, |
Jakob Bornecrantz | 3e48613 | 2009-11-24 18:00:12 +0100 | [diff] [blame] | 349 | drmModeClipPtr clips, uint32_t num_clips) |
| 350 | { |
Daniel Vetter | 7e0460c | 2015-02-11 12:03:12 +0100 | [diff] [blame] | 351 | struct drm_mode_fb_dirty_cmd dirty; |
Jakob Bornecrantz | 3e48613 | 2009-11-24 18:00:12 +0100 | [diff] [blame] | 352 | |
Daniel Vetter | 7e0460c | 2015-02-11 12:03:12 +0100 | [diff] [blame] | 353 | memclear(dirty); |
Jakob Bornecrantz | 3e48613 | 2009-11-24 18:00:12 +0100 | [diff] [blame] | 354 | dirty.fb_id = bufferId; |
| 355 | dirty.clips_ptr = VOID2U64(clips); |
| 356 | dirty.num_clips = num_clips; |
| 357 | |
Chris Wilson | b803918 | 2010-07-01 22:38:54 +0100 | [diff] [blame] | 358 | return DRM_IOCTL(fd, DRM_IOCTL_MODE_DIRTYFB, &dirty); |
Jakob Bornecrantz | 3e48613 | 2009-11-24 18:00:12 +0100 | [diff] [blame] | 359 | } |
| 360 | |
Jesse Barnes | 731cd55 | 2008-12-17 10:09:49 -0800 | [diff] [blame] | 361 | /* |
| 362 | * Crtc functions |
| 363 | */ |
| 364 | |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 365 | drm_public drmModeCrtcPtr drmModeGetCrtc(int fd, uint32_t crtcId) |
Jesse Barnes | 731cd55 | 2008-12-17 10:09:49 -0800 | [diff] [blame] | 366 | { |
| 367 | struct drm_mode_crtc crtc; |
| 368 | drmModeCrtcPtr r; |
| 369 | |
Daniel Vetter | 7e0460c | 2015-02-11 12:03:12 +0100 | [diff] [blame] | 370 | memclear(crtc); |
Jesse Barnes | 731cd55 | 2008-12-17 10:09:49 -0800 | [diff] [blame] | 371 | crtc.crtc_id = crtcId; |
| 372 | |
| 373 | if (drmIoctl(fd, DRM_IOCTL_MODE_GETCRTC, &crtc)) |
| 374 | return 0; |
| 375 | |
| 376 | /* |
| 377 | * return |
| 378 | */ |
| 379 | |
| 380 | if (!(r = drmMalloc(sizeof(*r)))) |
| 381 | return 0; |
| 382 | |
| 383 | r->crtc_id = crtc.crtc_id; |
| 384 | r->x = crtc.x; |
| 385 | r->y = crtc.y; |
| 386 | r->mode_valid = crtc.mode_valid; |
Rob Clark | e81acf5 | 2012-10-14 16:55:32 -0500 | [diff] [blame] | 387 | if (r->mode_valid) { |
Jesse Barnes | 731cd55 | 2008-12-17 10:09:49 -0800 | [diff] [blame] | 388 | memcpy(&r->mode, &crtc.mode, sizeof(struct drm_mode_modeinfo)); |
Rob Clark | e81acf5 | 2012-10-14 16:55:32 -0500 | [diff] [blame] | 389 | r->width = crtc.mode.hdisplay; |
| 390 | r->height = crtc.mode.vdisplay; |
| 391 | } |
Jesse Barnes | 731cd55 | 2008-12-17 10:09:49 -0800 | [diff] [blame] | 392 | r->buffer_id = crtc.fb_id; |
| 393 | r->gamma_size = crtc.gamma_size; |
| 394 | return r; |
| 395 | } |
| 396 | |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 397 | drm_public int drmModeSetCrtc(int fd, uint32_t crtcId, uint32_t bufferId, |
Thierry Reding | 5e5a3c4 | 2014-04-09 09:00:49 +0200 | [diff] [blame] | 398 | uint32_t x, uint32_t y, uint32_t *connectors, int count, |
Jakob Bornecrantz | eb78c53 | 2009-02-11 16:43:20 +0100 | [diff] [blame] | 399 | drmModeModeInfoPtr mode) |
Jesse Barnes | 731cd55 | 2008-12-17 10:09:49 -0800 | [diff] [blame] | 400 | { |
| 401 | struct drm_mode_crtc crtc; |
| 402 | |
Daniel Vetter | 7e0460c | 2015-02-11 12:03:12 +0100 | [diff] [blame] | 403 | memclear(crtc); |
Jesse Barnes | 731cd55 | 2008-12-17 10:09:49 -0800 | [diff] [blame] | 404 | crtc.x = x; |
| 405 | crtc.y = y; |
| 406 | crtc.crtc_id = crtcId; |
| 407 | crtc.fb_id = bufferId; |
| 408 | crtc.set_connectors_ptr = VOID2U64(connectors); |
| 409 | crtc.count_connectors = count; |
| 410 | if (mode) { |
| 411 | memcpy(&crtc.mode, mode, sizeof(struct drm_mode_modeinfo)); |
| 412 | crtc.mode_valid = 1; |
Daniel Vetter | 7e0460c | 2015-02-11 12:03:12 +0100 | [diff] [blame] | 413 | } |
Jesse Barnes | 731cd55 | 2008-12-17 10:09:49 -0800 | [diff] [blame] | 414 | |
Chris Wilson | b803918 | 2010-07-01 22:38:54 +0100 | [diff] [blame] | 415 | return DRM_IOCTL(fd, DRM_IOCTL_MODE_SETCRTC, &crtc); |
Jesse Barnes | 731cd55 | 2008-12-17 10:09:49 -0800 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | /* |
| 419 | * Cursor manipulation |
| 420 | */ |
| 421 | |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 422 | drm_public int drmModeSetCursor(int fd, uint32_t crtcId, uint32_t bo_handle, |
| 423 | uint32_t width, uint32_t height) |
Jesse Barnes | 731cd55 | 2008-12-17 10:09:49 -0800 | [diff] [blame] | 424 | { |
| 425 | struct drm_mode_cursor arg; |
| 426 | |
Daniel Vetter | 7e0460c | 2015-02-11 12:03:12 +0100 | [diff] [blame] | 427 | memclear(arg); |
Jesse Barnes | 731cd55 | 2008-12-17 10:09:49 -0800 | [diff] [blame] | 428 | arg.flags = DRM_MODE_CURSOR_BO; |
| 429 | arg.crtc_id = crtcId; |
| 430 | arg.width = width; |
| 431 | arg.height = height; |
| 432 | arg.handle = bo_handle; |
| 433 | |
Chris Wilson | b803918 | 2010-07-01 22:38:54 +0100 | [diff] [blame] | 434 | return DRM_IOCTL(fd, DRM_IOCTL_MODE_CURSOR, &arg); |
Jesse Barnes | 731cd55 | 2008-12-17 10:09:49 -0800 | [diff] [blame] | 435 | } |
| 436 | |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 437 | drm_public int drmModeSetCursor2(int fd, uint32_t crtcId, uint32_t bo_handle, |
| 438 | uint32_t width, uint32_t height, int32_t hot_x, |
| 439 | int32_t hot_y) |
Dave Airlie | 2e0ab62 | 2013-07-02 09:21:06 +0100 | [diff] [blame] | 440 | { |
| 441 | struct drm_mode_cursor2 arg; |
| 442 | |
Daniel Vetter | 7e0460c | 2015-02-11 12:03:12 +0100 | [diff] [blame] | 443 | memclear(arg); |
Dave Airlie | 2e0ab62 | 2013-07-02 09:21:06 +0100 | [diff] [blame] | 444 | arg.flags = DRM_MODE_CURSOR_BO; |
| 445 | arg.crtc_id = crtcId; |
| 446 | arg.width = width; |
| 447 | arg.height = height; |
| 448 | arg.handle = bo_handle; |
| 449 | arg.hot_x = hot_x; |
| 450 | arg.hot_y = hot_y; |
| 451 | |
| 452 | return DRM_IOCTL(fd, DRM_IOCTL_MODE_CURSOR2, &arg); |
| 453 | } |
| 454 | |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 455 | drm_public int drmModeMoveCursor(int fd, uint32_t crtcId, int x, int y) |
Jesse Barnes | 731cd55 | 2008-12-17 10:09:49 -0800 | [diff] [blame] | 456 | { |
| 457 | struct drm_mode_cursor arg; |
| 458 | |
Daniel Vetter | 7e0460c | 2015-02-11 12:03:12 +0100 | [diff] [blame] | 459 | memclear(arg); |
Jesse Barnes | 731cd55 | 2008-12-17 10:09:49 -0800 | [diff] [blame] | 460 | arg.flags = DRM_MODE_CURSOR_MOVE; |
| 461 | arg.crtc_id = crtcId; |
| 462 | arg.x = x; |
| 463 | arg.y = y; |
| 464 | |
Chris Wilson | b803918 | 2010-07-01 22:38:54 +0100 | [diff] [blame] | 465 | return DRM_IOCTL(fd, DRM_IOCTL_MODE_CURSOR, &arg); |
Jesse Barnes | 731cd55 | 2008-12-17 10:09:49 -0800 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | /* |
| 469 | * Encoder get |
| 470 | */ |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 471 | drm_public drmModeEncoderPtr drmModeGetEncoder(int fd, uint32_t encoder_id) |
Jesse Barnes | 731cd55 | 2008-12-17 10:09:49 -0800 | [diff] [blame] | 472 | { |
| 473 | struct drm_mode_get_encoder enc; |
| 474 | drmModeEncoderPtr r = NULL; |
| 475 | |
Daniel Vetter | 7e0460c | 2015-02-11 12:03:12 +0100 | [diff] [blame] | 476 | memclear(enc); |
Jesse Barnes | 731cd55 | 2008-12-17 10:09:49 -0800 | [diff] [blame] | 477 | enc.encoder_id = encoder_id; |
Jesse Barnes | 731cd55 | 2008-12-17 10:09:49 -0800 | [diff] [blame] | 478 | |
| 479 | if (drmIoctl(fd, DRM_IOCTL_MODE_GETENCODER, &enc)) |
| 480 | return 0; |
| 481 | |
| 482 | if (!(r = drmMalloc(sizeof(*r)))) |
| 483 | return 0; |
| 484 | |
| 485 | r->encoder_id = enc.encoder_id; |
| 486 | r->crtc_id = enc.crtc_id; |
| 487 | r->encoder_type = enc.encoder_type; |
| 488 | r->possible_crtcs = enc.possible_crtcs; |
| 489 | r->possible_clones = enc.possible_clones; |
| 490 | |
| 491 | return r; |
| 492 | } |
| 493 | |
| 494 | /* |
| 495 | * Connector manipulation |
| 496 | */ |
Chris Wilson | 5ed5fa1 | 2015-03-04 10:07:19 +0000 | [diff] [blame] | 497 | static drmModeConnectorPtr |
| 498 | _drmModeGetConnector(int fd, uint32_t connector_id, int probe) |
Jesse Barnes | 731cd55 | 2008-12-17 10:09:49 -0800 | [diff] [blame] | 499 | { |
Peter Clifton | 04f90a4 | 2010-01-06 20:44:11 +0000 | [diff] [blame] | 500 | struct drm_mode_get_connector conn, counts; |
Jesse Barnes | 731cd55 | 2008-12-17 10:09:49 -0800 | [diff] [blame] | 501 | drmModeConnectorPtr r = NULL; |
Ville Syrjälä | e342c0f | 2015-12-15 14:18:32 +0200 | [diff] [blame] | 502 | struct drm_mode_modeinfo stack_mode; |
Jesse Barnes | 731cd55 | 2008-12-17 10:09:49 -0800 | [diff] [blame] | 503 | |
Daniel Vetter | 7e0460c | 2015-02-11 12:03:12 +0100 | [diff] [blame] | 504 | memclear(conn); |
Jesse Barnes | 731cd55 | 2008-12-17 10:09:49 -0800 | [diff] [blame] | 505 | conn.connector_id = connector_id; |
Chris Wilson | 5ed5fa1 | 2015-03-04 10:07:19 +0000 | [diff] [blame] | 506 | if (!probe) { |
| 507 | conn.count_modes = 1; |
Ville Syrjälä | e342c0f | 2015-12-15 14:18:32 +0200 | [diff] [blame] | 508 | conn.modes_ptr = VOID2U64(&stack_mode); |
Chris Wilson | 5ed5fa1 | 2015-03-04 10:07:19 +0000 | [diff] [blame] | 509 | } |
Jesse Barnes | 731cd55 | 2008-12-17 10:09:49 -0800 | [diff] [blame] | 510 | |
| 511 | if (drmIoctl(fd, DRM_IOCTL_MODE_GETCONNECTOR, &conn)) |
| 512 | return 0; |
| 513 | |
Chris Wilson | 5ed5fa1 | 2015-03-04 10:07:19 +0000 | [diff] [blame] | 514 | retry: |
Peter Clifton | 04f90a4 | 2010-01-06 20:44:11 +0000 | [diff] [blame] | 515 | counts = conn; |
| 516 | |
Jesse Barnes | 731cd55 | 2008-12-17 10:09:49 -0800 | [diff] [blame] | 517 | if (conn.count_props) { |
| 518 | conn.props_ptr = VOID2U64(drmMalloc(conn.count_props*sizeof(uint32_t))); |
Peter Clifton | 04f90a4 | 2010-01-06 20:44:11 +0000 | [diff] [blame] | 519 | if (!conn.props_ptr) |
| 520 | goto err_allocs; |
Jesse Barnes | 731cd55 | 2008-12-17 10:09:49 -0800 | [diff] [blame] | 521 | conn.prop_values_ptr = VOID2U64(drmMalloc(conn.count_props*sizeof(uint64_t))); |
Peter Clifton | 04f90a4 | 2010-01-06 20:44:11 +0000 | [diff] [blame] | 522 | if (!conn.prop_values_ptr) |
| 523 | goto err_allocs; |
Jesse Barnes | 731cd55 | 2008-12-17 10:09:49 -0800 | [diff] [blame] | 524 | } |
| 525 | |
Peter Clifton | 04f90a4 | 2010-01-06 20:44:11 +0000 | [diff] [blame] | 526 | if (conn.count_modes) { |
Jesse Barnes | 731cd55 | 2008-12-17 10:09:49 -0800 | [diff] [blame] | 527 | conn.modes_ptr = VOID2U64(drmMalloc(conn.count_modes*sizeof(struct drm_mode_modeinfo))); |
Peter Clifton | 04f90a4 | 2010-01-06 20:44:11 +0000 | [diff] [blame] | 528 | if (!conn.modes_ptr) |
| 529 | goto err_allocs; |
Chris Wilson | 5ed5fa1 | 2015-03-04 10:07:19 +0000 | [diff] [blame] | 530 | } else { |
| 531 | conn.count_modes = 1; |
Ville Syrjälä | e342c0f | 2015-12-15 14:18:32 +0200 | [diff] [blame] | 532 | conn.modes_ptr = VOID2U64(&stack_mode); |
Peter Clifton | 04f90a4 | 2010-01-06 20:44:11 +0000 | [diff] [blame] | 533 | } |
Jesse Barnes | 731cd55 | 2008-12-17 10:09:49 -0800 | [diff] [blame] | 534 | |
Peter Clifton | 04f90a4 | 2010-01-06 20:44:11 +0000 | [diff] [blame] | 535 | if (conn.count_encoders) { |
Jesse Barnes | 731cd55 | 2008-12-17 10:09:49 -0800 | [diff] [blame] | 536 | conn.encoders_ptr = VOID2U64(drmMalloc(conn.count_encoders*sizeof(uint32_t))); |
Peter Clifton | 04f90a4 | 2010-01-06 20:44:11 +0000 | [diff] [blame] | 537 | if (!conn.encoders_ptr) |
| 538 | goto err_allocs; |
| 539 | } |
Jesse Barnes | 731cd55 | 2008-12-17 10:09:49 -0800 | [diff] [blame] | 540 | |
| 541 | if (drmIoctl(fd, DRM_IOCTL_MODE_GETCONNECTOR, &conn)) |
| 542 | goto err_allocs; |
| 543 | |
Peter Clifton | 04f90a4 | 2010-01-06 20:44:11 +0000 | [diff] [blame] | 544 | /* The number of available connectors and etc may have changed with a |
| 545 | * hotplug event in between the ioctls, in which case the field is |
| 546 | * silently ignored by the kernel. |
| 547 | */ |
| 548 | if (counts.count_props < conn.count_props || |
| 549 | counts.count_modes < conn.count_modes || |
| 550 | counts.count_encoders < conn.count_encoders) { |
| 551 | drmFree(U642VOID(conn.props_ptr)); |
| 552 | drmFree(U642VOID(conn.prop_values_ptr)); |
Ville Syrjälä | e342c0f | 2015-12-15 14:18:32 +0200 | [diff] [blame] | 553 | if (U642VOID(conn.modes_ptr) != &stack_mode) |
| 554 | drmFree(U642VOID(conn.modes_ptr)); |
Peter Clifton | 04f90a4 | 2010-01-06 20:44:11 +0000 | [diff] [blame] | 555 | drmFree(U642VOID(conn.encoders_ptr)); |
| 556 | |
| 557 | goto retry; |
| 558 | } |
| 559 | |
Jesse Barnes | 731cd55 | 2008-12-17 10:09:49 -0800 | [diff] [blame] | 560 | if(!(r = drmMalloc(sizeof(*r)))) { |
| 561 | goto err_allocs; |
| 562 | } |
| 563 | |
| 564 | r->connector_id = conn.connector_id; |
| 565 | r->encoder_id = conn.encoder_id; |
| 566 | r->connection = conn.connection; |
| 567 | r->mmWidth = conn.mm_width; |
| 568 | r->mmHeight = conn.mm_height; |
Dave Airlie | 412d370 | 2009-04-22 20:25:40 +1000 | [diff] [blame] | 569 | /* convert subpixel from kernel to userspace */ |
| 570 | r->subpixel = conn.subpixel + 1; |
Jesse Barnes | 731cd55 | 2008-12-17 10:09:49 -0800 | [diff] [blame] | 571 | r->count_modes = conn.count_modes; |
Jesse Barnes | 731cd55 | 2008-12-17 10:09:49 -0800 | [diff] [blame] | 572 | r->count_props = conn.count_props; |
| 573 | r->props = drmAllocCpy(U642VOID(conn.props_ptr), conn.count_props, sizeof(uint32_t)); |
| 574 | r->prop_values = drmAllocCpy(U642VOID(conn.prop_values_ptr), conn.count_props, sizeof(uint64_t)); |
| 575 | r->modes = drmAllocCpy(U642VOID(conn.modes_ptr), conn.count_modes, sizeof(struct drm_mode_modeinfo)); |
| 576 | r->count_encoders = conn.count_encoders; |
| 577 | r->encoders = drmAllocCpy(U642VOID(conn.encoders_ptr), conn.count_encoders, sizeof(uint32_t)); |
| 578 | r->connector_type = conn.connector_type; |
| 579 | r->connector_type_id = conn.connector_type_id; |
| 580 | |
Peter Clifton | 04f90a4 | 2010-01-06 20:44:11 +0000 | [diff] [blame] | 581 | if ((r->count_props && !r->props) || |
| 582 | (r->count_props && !r->prop_values) || |
| 583 | (r->count_modes && !r->modes) || |
| 584 | (r->count_encoders && !r->encoders)) { |
| 585 | drmFree(r->props); |
| 586 | drmFree(r->prop_values); |
| 587 | drmFree(r->modes); |
| 588 | drmFree(r->encoders); |
| 589 | drmFree(r); |
| 590 | r = 0; |
| 591 | } |
Jesse Barnes | 731cd55 | 2008-12-17 10:09:49 -0800 | [diff] [blame] | 592 | |
| 593 | err_allocs: |
| 594 | drmFree(U642VOID(conn.prop_values_ptr)); |
| 595 | drmFree(U642VOID(conn.props_ptr)); |
Ville Syrjälä | e342c0f | 2015-12-15 14:18:32 +0200 | [diff] [blame] | 596 | if (U642VOID(conn.modes_ptr) != &stack_mode) |
| 597 | drmFree(U642VOID(conn.modes_ptr)); |
Jesse Barnes | 731cd55 | 2008-12-17 10:09:49 -0800 | [diff] [blame] | 598 | drmFree(U642VOID(conn.encoders_ptr)); |
| 599 | |
| 600 | return r; |
| 601 | } |
| 602 | |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 603 | drm_public drmModeConnectorPtr drmModeGetConnector(int fd, uint32_t connector_id) |
Chris Wilson | 5ed5fa1 | 2015-03-04 10:07:19 +0000 | [diff] [blame] | 604 | { |
| 605 | return _drmModeGetConnector(fd, connector_id, 1); |
| 606 | } |
| 607 | |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 608 | drm_public drmModeConnectorPtr drmModeGetConnectorCurrent(int fd, uint32_t connector_id) |
Chris Wilson | 5ed5fa1 | 2015-03-04 10:07:19 +0000 | [diff] [blame] | 609 | { |
| 610 | return _drmModeGetConnector(fd, connector_id, 0); |
| 611 | } |
| 612 | |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 613 | drm_public int drmModeAttachMode(int fd, uint32_t connector_id, drmModeModeInfoPtr mode_info) |
Jesse Barnes | 731cd55 | 2008-12-17 10:09:49 -0800 | [diff] [blame] | 614 | { |
| 615 | struct drm_mode_mode_cmd res; |
| 616 | |
Daniel Vetter | 7e0460c | 2015-02-11 12:03:12 +0100 | [diff] [blame] | 617 | memclear(res); |
Jesse Barnes | 731cd55 | 2008-12-17 10:09:49 -0800 | [diff] [blame] | 618 | memcpy(&res.mode, mode_info, sizeof(struct drm_mode_modeinfo)); |
| 619 | res.connector_id = connector_id; |
| 620 | |
Chris Wilson | b803918 | 2010-07-01 22:38:54 +0100 | [diff] [blame] | 621 | return DRM_IOCTL(fd, DRM_IOCTL_MODE_ATTACHMODE, &res); |
Jesse Barnes | 731cd55 | 2008-12-17 10:09:49 -0800 | [diff] [blame] | 622 | } |
| 623 | |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 624 | drm_public int drmModeDetachMode(int fd, uint32_t connector_id, drmModeModeInfoPtr mode_info) |
Jesse Barnes | 731cd55 | 2008-12-17 10:09:49 -0800 | [diff] [blame] | 625 | { |
| 626 | struct drm_mode_mode_cmd res; |
| 627 | |
Daniel Vetter | 7e0460c | 2015-02-11 12:03:12 +0100 | [diff] [blame] | 628 | memclear(res); |
Jesse Barnes | 731cd55 | 2008-12-17 10:09:49 -0800 | [diff] [blame] | 629 | memcpy(&res.mode, mode_info, sizeof(struct drm_mode_modeinfo)); |
| 630 | res.connector_id = connector_id; |
| 631 | |
Chris Wilson | b803918 | 2010-07-01 22:38:54 +0100 | [diff] [blame] | 632 | return DRM_IOCTL(fd, DRM_IOCTL_MODE_DETACHMODE, &res); |
Jesse Barnes | 731cd55 | 2008-12-17 10:09:49 -0800 | [diff] [blame] | 633 | } |
| 634 | |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 635 | drm_public drmModePropertyPtr drmModeGetProperty(int fd, uint32_t property_id) |
Jesse Barnes | 731cd55 | 2008-12-17 10:09:49 -0800 | [diff] [blame] | 636 | { |
| 637 | struct drm_mode_get_property prop; |
| 638 | drmModePropertyPtr r; |
| 639 | |
Daniel Vetter | 7e0460c | 2015-02-11 12:03:12 +0100 | [diff] [blame] | 640 | memclear(prop); |
Jesse Barnes | 731cd55 | 2008-12-17 10:09:49 -0800 | [diff] [blame] | 641 | prop.prop_id = property_id; |
Jesse Barnes | 731cd55 | 2008-12-17 10:09:49 -0800 | [diff] [blame] | 642 | |
| 643 | if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPERTY, &prop)) |
| 644 | return 0; |
| 645 | |
| 646 | if (prop.count_values) |
| 647 | prop.values_ptr = VOID2U64(drmMalloc(prop.count_values * sizeof(uint64_t))); |
| 648 | |
Rob Clark | 7b228e9 | 2012-06-05 12:28:22 -0500 | [diff] [blame] | 649 | if (prop.count_enum_blobs && (prop.flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK))) |
Jesse Barnes | 731cd55 | 2008-12-17 10:09:49 -0800 | [diff] [blame] | 650 | prop.enum_blob_ptr = VOID2U64(drmMalloc(prop.count_enum_blobs * sizeof(struct drm_mode_property_enum))); |
| 651 | |
| 652 | if (prop.count_enum_blobs && (prop.flags & DRM_MODE_PROP_BLOB)) { |
| 653 | prop.values_ptr = VOID2U64(drmMalloc(prop.count_enum_blobs * sizeof(uint32_t))); |
| 654 | prop.enum_blob_ptr = VOID2U64(drmMalloc(prop.count_enum_blobs * sizeof(uint32_t))); |
| 655 | } |
| 656 | |
| 657 | if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPERTY, &prop)) { |
| 658 | r = NULL; |
| 659 | goto err_allocs; |
| 660 | } |
| 661 | |
| 662 | if (!(r = drmMalloc(sizeof(*r)))) |
Seung-Woo Kim | b39377d | 2019-04-29 18:10:52 +0900 | [diff] [blame] | 663 | goto err_allocs; |
Jesse Barnes | 731cd55 | 2008-12-17 10:09:49 -0800 | [diff] [blame] | 664 | |
| 665 | r->prop_id = prop.prop_id; |
| 666 | r->count_values = prop.count_values; |
| 667 | |
| 668 | r->flags = prop.flags; |
| 669 | if (prop.count_values) |
| 670 | r->values = drmAllocCpy(U642VOID(prop.values_ptr), prop.count_values, sizeof(uint64_t)); |
Rob Clark | 7b228e9 | 2012-06-05 12:28:22 -0500 | [diff] [blame] | 671 | if (prop.flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) { |
Jesse Barnes | 731cd55 | 2008-12-17 10:09:49 -0800 | [diff] [blame] | 672 | r->count_enums = prop.count_enum_blobs; |
| 673 | r->enums = drmAllocCpy(U642VOID(prop.enum_blob_ptr), prop.count_enum_blobs, sizeof(struct drm_mode_property_enum)); |
| 674 | } else if (prop.flags & DRM_MODE_PROP_BLOB) { |
| 675 | r->values = drmAllocCpy(U642VOID(prop.values_ptr), prop.count_enum_blobs, sizeof(uint32_t)); |
| 676 | r->blob_ids = drmAllocCpy(U642VOID(prop.enum_blob_ptr), prop.count_enum_blobs, sizeof(uint32_t)); |
| 677 | r->count_blobs = prop.count_enum_blobs; |
| 678 | } |
| 679 | strncpy(r->name, prop.name, DRM_PROP_NAME_LEN); |
| 680 | r->name[DRM_PROP_NAME_LEN-1] = 0; |
| 681 | |
| 682 | err_allocs: |
| 683 | drmFree(U642VOID(prop.values_ptr)); |
| 684 | drmFree(U642VOID(prop.enum_blob_ptr)); |
| 685 | |
| 686 | return r; |
| 687 | } |
| 688 | |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 689 | drm_public void drmModeFreeProperty(drmModePropertyPtr ptr) |
Jesse Barnes | 731cd55 | 2008-12-17 10:09:49 -0800 | [diff] [blame] | 690 | { |
| 691 | if (!ptr) |
| 692 | return; |
| 693 | |
| 694 | drmFree(ptr->values); |
| 695 | drmFree(ptr->enums); |
Boram Park | 7915b0a | 2011-10-01 09:30:09 +0900 | [diff] [blame] | 696 | drmFree(ptr->blob_ids); |
Jesse Barnes | 731cd55 | 2008-12-17 10:09:49 -0800 | [diff] [blame] | 697 | drmFree(ptr); |
| 698 | } |
| 699 | |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 700 | drm_public drmModePropertyBlobPtr drmModeGetPropertyBlob(int fd, |
| 701 | uint32_t blob_id) |
Jesse Barnes | 731cd55 | 2008-12-17 10:09:49 -0800 | [diff] [blame] | 702 | { |
| 703 | struct drm_mode_get_blob blob; |
| 704 | drmModePropertyBlobPtr r; |
| 705 | |
Daniel Vetter | 7e0460c | 2015-02-11 12:03:12 +0100 | [diff] [blame] | 706 | memclear(blob); |
Jesse Barnes | 731cd55 | 2008-12-17 10:09:49 -0800 | [diff] [blame] | 707 | blob.blob_id = blob_id; |
| 708 | |
| 709 | if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPBLOB, &blob)) |
| 710 | return NULL; |
| 711 | |
| 712 | if (blob.length) |
| 713 | blob.data = VOID2U64(drmMalloc(blob.length)); |
| 714 | |
| 715 | if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPBLOB, &blob)) { |
| 716 | r = NULL; |
| 717 | goto err_allocs; |
| 718 | } |
| 719 | |
| 720 | if (!(r = drmMalloc(sizeof(*r)))) |
Chris Wilson | 8a76244 | 2010-08-24 21:29:31 +0100 | [diff] [blame] | 721 | goto err_allocs; |
Jesse Barnes | 731cd55 | 2008-12-17 10:09:49 -0800 | [diff] [blame] | 722 | |
| 723 | r->id = blob.blob_id; |
| 724 | r->length = blob.length; |
| 725 | r->data = drmAllocCpy(U642VOID(blob.data), 1, blob.length); |
| 726 | |
| 727 | err_allocs: |
| 728 | drmFree(U642VOID(blob.data)); |
| 729 | return r; |
| 730 | } |
| 731 | |
Luigi Santivetti | e641e2a | 2021-03-25 22:42:38 +0000 | [diff] [blame] | 732 | static inline const uint32_t * |
| 733 | get_formats_ptr(const struct drm_format_modifier_blob *blob) |
| 734 | { |
| 735 | return (const uint32_t *)(((uint8_t *)blob) + blob->formats_offset); |
| 736 | } |
| 737 | |
| 738 | static inline const struct drm_format_modifier * |
| 739 | get_modifiers_ptr(const struct drm_format_modifier_blob *blob) |
| 740 | { |
| 741 | return (const struct drm_format_modifier *)(((uint8_t *)blob) + |
| 742 | blob->modifiers_offset); |
| 743 | } |
| 744 | |
| 745 | static bool _drmModeFormatModifierGetNext(const drmModePropertyBlobRes *blob, |
| 746 | drmModeFormatModifierIterator *iter) |
| 747 | { |
| 748 | const struct drm_format_modifier *blob_modifiers, *mod; |
| 749 | const struct drm_format_modifier_blob *fmt_mod_blob; |
| 750 | const uint32_t *blob_formats; |
| 751 | |
| 752 | assert(blob && iter); |
| 753 | |
| 754 | fmt_mod_blob = blob->data; |
| 755 | blob_modifiers = get_modifiers_ptr(fmt_mod_blob); |
| 756 | blob_formats = get_formats_ptr(fmt_mod_blob); |
| 757 | |
| 758 | /* fmt_idx and mod_idx designate the number of processed formats |
| 759 | * and modifiers. |
| 760 | */ |
| 761 | if (iter->fmt_idx >= fmt_mod_blob->count_formats || |
| 762 | iter->mod_idx >= fmt_mod_blob->count_modifiers) |
| 763 | return false; |
| 764 | |
| 765 | iter->fmt = blob_formats[iter->fmt_idx]; |
| 766 | iter->mod = DRM_FORMAT_MOD_INVALID; |
| 767 | |
| 768 | /* From the latest valid found, get the next valid modifier */ |
| 769 | while (iter->mod_idx < fmt_mod_blob->count_modifiers) { |
| 770 | mod = &blob_modifiers[iter->mod_idx++]; |
| 771 | |
| 772 | /* Check if the format that fmt_idx designates, belongs to |
| 773 | * this modifier 64-bit window selected via mod->offset. |
| 774 | */ |
| 775 | if (iter->fmt_idx < mod->offset || |
| 776 | iter->fmt_idx >= mod->offset + 64) |
| 777 | continue; |
| 778 | if (!(mod->formats & (1 << (iter->fmt_idx - mod->offset)))) |
| 779 | continue; |
| 780 | |
| 781 | iter->mod = mod->modifier; |
| 782 | break; |
| 783 | } |
| 784 | |
| 785 | if (iter->mod_idx == fmt_mod_blob->count_modifiers) { |
| 786 | iter->mod_idx = 0; |
| 787 | iter->fmt_idx++; |
| 788 | } |
| 789 | |
| 790 | /* Since mod_idx reset, in order for the caller to iterate over |
| 791 | * the last modifier of the last format, always return true here |
| 792 | * and early return from the next call. |
| 793 | */ |
| 794 | return true; |
| 795 | } |
| 796 | |
| 797 | /** |
| 798 | * Iterate over formats first and then over modifiers. On each call, iter->fmt |
| 799 | * is retained until all associated modifiers are returned. Then, either update |
| 800 | * iter->fmt with the next format, or exit if there aren't any left. |
| 801 | * |
| 802 | * NOTE: clients should not make any assumption on mod_idx and fmt_idx values |
| 803 | * |
| 804 | * @blob: valid kernel blob holding formats and modifiers |
| 805 | * @iter: input and output iterator data. Iter data must be initialised to zero |
| 806 | * @return: false, on error or there aren't any further formats or modifiers left. |
| 807 | * true, on success and there are more formats or modifiers. |
| 808 | */ |
| 809 | drm_public bool drmModeFormatModifierBlobIterNext(const drmModePropertyBlobRes *blob, |
| 810 | drmModeFormatModifierIterator *iter) |
| 811 | { |
| 812 | drmModeFormatModifierIterator tmp; |
| 813 | bool has_fmt; |
| 814 | |
| 815 | if (!blob || !iter) |
| 816 | return false; |
| 817 | |
| 818 | tmp.fmt_idx = iter->fmt_idx; |
| 819 | tmp.mod_idx = iter->mod_idx; |
| 820 | |
| 821 | /* With the current state of things, DRM/KMS drivers are allowed to |
| 822 | * construct blobs having formats and no modifiers. Userspace can't |
| 823 | * legitimately abort in such cases. |
| 824 | * |
| 825 | * While waiting for the kernel to perhaps disallow formats with no |
| 826 | * modifiers in IN_FORMATS blobs, skip the format altogether. |
| 827 | */ |
| 828 | do { |
| 829 | has_fmt = _drmModeFormatModifierGetNext(blob, &tmp); |
| 830 | if (has_fmt && tmp.mod != DRM_FORMAT_MOD_INVALID) |
| 831 | *iter = tmp; |
| 832 | |
| 833 | } while (has_fmt && tmp.mod == DRM_FORMAT_MOD_INVALID); |
| 834 | |
| 835 | return has_fmt; |
| 836 | } |
| 837 | |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 838 | drm_public void drmModeFreePropertyBlob(drmModePropertyBlobPtr ptr) |
Jesse Barnes | 731cd55 | 2008-12-17 10:09:49 -0800 | [diff] [blame] | 839 | { |
| 840 | if (!ptr) |
| 841 | return; |
| 842 | |
| 843 | drmFree(ptr->data); |
| 844 | drmFree(ptr); |
| 845 | } |
| 846 | |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 847 | drm_public int drmModeConnectorSetProperty(int fd, uint32_t connector_id, |
| 848 | uint32_t property_id, |
| 849 | uint64_t value) |
Jesse Barnes | 731cd55 | 2008-12-17 10:09:49 -0800 | [diff] [blame] | 850 | { |
| 851 | struct drm_mode_connector_set_property osp; |
Jesse Barnes | 731cd55 | 2008-12-17 10:09:49 -0800 | [diff] [blame] | 852 | |
Daniel Vetter | 7e0460c | 2015-02-11 12:03:12 +0100 | [diff] [blame] | 853 | memclear(osp); |
Jesse Barnes | 731cd55 | 2008-12-17 10:09:49 -0800 | [diff] [blame] | 854 | osp.connector_id = connector_id; |
| 855 | osp.prop_id = property_id; |
| 856 | osp.value = value; |
| 857 | |
Chris Wilson | b803918 | 2010-07-01 22:38:54 +0100 | [diff] [blame] | 858 | return DRM_IOCTL(fd, DRM_IOCTL_MODE_SETPROPERTY, &osp); |
Jesse Barnes | 731cd55 | 2008-12-17 10:09:49 -0800 | [diff] [blame] | 859 | } |
| 860 | |
| 861 | /* |
| 862 | * checks if a modesetting capable driver has attached to the pci id |
| 863 | * returns 0 if modesetting supported. |
| 864 | * -EINVAL or invalid bus id |
| 865 | * -ENOSYS if no modesetting support |
| 866 | */ |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 867 | drm_public int drmCheckModesettingSupported(const char *busid) |
Jesse Barnes | 731cd55 | 2008-12-17 10:09:49 -0800 | [diff] [blame] | 868 | { |
Robert Millan | cbb31f2 | 2014-01-23 14:46:05 +0000 | [diff] [blame] | 869 | #if defined (__linux__) |
Jesse Barnes | 731cd55 | 2008-12-17 10:09:49 -0800 | [diff] [blame] | 870 | char pci_dev_dir[1024]; |
| 871 | int domain, bus, dev, func; |
| 872 | DIR *sysdir; |
| 873 | struct dirent *dent; |
| 874 | int found = 0, ret; |
| 875 | |
| 876 | ret = sscanf(busid, "pci:%04x:%02x:%02x.%d", &domain, &bus, &dev, &func); |
| 877 | if (ret != 4) |
| 878 | return -EINVAL; |
| 879 | |
| 880 | sprintf(pci_dev_dir, "/sys/bus/pci/devices/%04x:%02x:%02x.%d/drm", |
| 881 | domain, bus, dev, func); |
| 882 | |
| 883 | sysdir = opendir(pci_dev_dir); |
| 884 | if (sysdir) { |
| 885 | dent = readdir(sysdir); |
| 886 | while (dent) { |
| 887 | if (!strncmp(dent->d_name, "controlD", 8)) { |
| 888 | found = 1; |
| 889 | break; |
| 890 | } |
| 891 | |
| 892 | dent = readdir(sysdir); |
| 893 | } |
| 894 | closedir(sysdir); |
| 895 | if (found) |
| 896 | return 0; |
| 897 | } |
| 898 | |
| 899 | sprintf(pci_dev_dir, "/sys/bus/pci/devices/%04x:%02x:%02x.%d/", |
| 900 | domain, bus, dev, func); |
| 901 | |
| 902 | sysdir = opendir(pci_dev_dir); |
| 903 | if (!sysdir) |
| 904 | return -EINVAL; |
| 905 | |
| 906 | dent = readdir(sysdir); |
| 907 | while (dent) { |
| 908 | if (!strncmp(dent->d_name, "drm:controlD", 12)) { |
| 909 | found = 1; |
| 910 | break; |
| 911 | } |
| 912 | |
| 913 | dent = readdir(sysdir); |
| 914 | } |
| 915 | |
| 916 | closedir(sysdir); |
| 917 | if (found) |
| 918 | return 0; |
Robert Millan | cbb31f2 | 2014-01-23 14:46:05 +0000 | [diff] [blame] | 919 | #elif defined (__FreeBSD__) || defined (__FreeBSD_kernel__) |
Emmanuel Vadot | 24e6852 | 2020-01-21 18:41:38 +0100 | [diff] [blame] | 920 | char sbusid[1024]; |
Robert Millan | cbb31f2 | 2014-01-23 14:46:05 +0000 | [diff] [blame] | 921 | char oid[128]; |
Robert Millan | cbb31f2 | 2014-01-23 14:46:05 +0000 | [diff] [blame] | 922 | int i, modesetting, ret; |
| 923 | size_t len; |
| 924 | |
Robert Millan | cbb31f2 | 2014-01-23 14:46:05 +0000 | [diff] [blame] | 925 | /* How many GPUs do we expect in the machine ? */ |
Emmanuel Vadot | bb584b8 | 2020-01-24 20:43:05 +0100 | [diff] [blame] | 926 | for (i = 0; i < 10; i++) { |
Robert Millan | cbb31f2 | 2014-01-23 14:46:05 +0000 | [diff] [blame] | 927 | snprintf(oid, sizeof(oid), "hw.dri.%d.busid", i); |
| 928 | len = sizeof(sbusid); |
| 929 | ret = sysctlbyname(oid, sbusid, &len, NULL, 0); |
| 930 | if (ret == -1) { |
| 931 | if (errno == ENOENT) |
| 932 | continue; |
| 933 | return -EINVAL; |
| 934 | } |
Emmanuel Vadot | 24e6852 | 2020-01-21 18:41:38 +0100 | [diff] [blame] | 935 | if (strcmp(sbusid, busid) != 0) |
Robert Millan | cbb31f2 | 2014-01-23 14:46:05 +0000 | [diff] [blame] | 936 | continue; |
| 937 | snprintf(oid, sizeof(oid), "hw.dri.%d.modesetting", i); |
| 938 | len = sizeof(modesetting); |
| 939 | ret = sysctlbyname(oid, &modesetting, &len, NULL, 0); |
| 940 | if (ret == -1 || len != sizeof(modesetting)) |
| 941 | return -EINVAL; |
| 942 | return (modesetting ? 0 : -ENOSYS); |
| 943 | } |
François Tigeot | edbb4e5 | 2014-07-26 13:39:58 +0200 | [diff] [blame] | 944 | #elif defined(__DragonFly__) |
| 945 | return 0; |
Eric Engestrom | 00aa374 | 2018-03-22 17:08:37 +0000 | [diff] [blame] | 946 | #elif defined(__OpenBSD__) |
Jonathan Gray | 1d3b823 | 2015-07-19 07:20:37 +1000 | [diff] [blame] | 947 | int fd; |
| 948 | struct drm_mode_card_res res; |
| 949 | drmModeResPtr r = 0; |
Jesse Barnes | 731cd55 | 2008-12-17 10:09:49 -0800 | [diff] [blame] | 950 | |
Jonathan Gray | 1d3b823 | 2015-07-19 07:20:37 +1000 | [diff] [blame] | 951 | if ((fd = drmOpen(NULL, busid)) < 0) |
| 952 | return -EINVAL; |
| 953 | |
| 954 | memset(&res, 0, sizeof(struct drm_mode_card_res)); |
| 955 | |
| 956 | if (drmIoctl(fd, DRM_IOCTL_MODE_GETRESOURCES, &res)) { |
| 957 | drmClose(fd); |
| 958 | return -errno; |
| 959 | } |
| 960 | |
| 961 | drmClose(fd); |
| 962 | return 0; |
| 963 | #endif |
| 964 | return -ENOSYS; |
Jesse Barnes | 731cd55 | 2008-12-17 10:09:49 -0800 | [diff] [blame] | 965 | } |
| 966 | |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 967 | drm_public int drmModeCrtcGetGamma(int fd, uint32_t crtc_id, uint32_t size, |
| 968 | uint16_t *red, uint16_t *green, |
| 969 | uint16_t *blue) |
Jesse Barnes | 731cd55 | 2008-12-17 10:09:49 -0800 | [diff] [blame] | 970 | { |
Jesse Barnes | 731cd55 | 2008-12-17 10:09:49 -0800 | [diff] [blame] | 971 | struct drm_mode_crtc_lut l; |
| 972 | |
Daniel Vetter | 7e0460c | 2015-02-11 12:03:12 +0100 | [diff] [blame] | 973 | memclear(l); |
Jesse Barnes | 731cd55 | 2008-12-17 10:09:49 -0800 | [diff] [blame] | 974 | l.crtc_id = crtc_id; |
| 975 | l.gamma_size = size; |
| 976 | l.red = VOID2U64(red); |
| 977 | l.green = VOID2U64(green); |
| 978 | l.blue = VOID2U64(blue); |
| 979 | |
Chris Wilson | b803918 | 2010-07-01 22:38:54 +0100 | [diff] [blame] | 980 | return DRM_IOCTL(fd, DRM_IOCTL_MODE_GETGAMMA, &l); |
Jesse Barnes | 731cd55 | 2008-12-17 10:09:49 -0800 | [diff] [blame] | 981 | } |
| 982 | |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 983 | drm_public int drmModeCrtcSetGamma(int fd, uint32_t crtc_id, uint32_t size, |
| 984 | uint16_t *red, uint16_t *green, |
| 985 | uint16_t *blue) |
Jesse Barnes | 731cd55 | 2008-12-17 10:09:49 -0800 | [diff] [blame] | 986 | { |
Jesse Barnes | 731cd55 | 2008-12-17 10:09:49 -0800 | [diff] [blame] | 987 | struct drm_mode_crtc_lut l; |
| 988 | |
Daniel Vetter | 7e0460c | 2015-02-11 12:03:12 +0100 | [diff] [blame] | 989 | memclear(l); |
Jesse Barnes | 731cd55 | 2008-12-17 10:09:49 -0800 | [diff] [blame] | 990 | l.crtc_id = crtc_id; |
| 991 | l.gamma_size = size; |
| 992 | l.red = VOID2U64(red); |
| 993 | l.green = VOID2U64(green); |
| 994 | l.blue = VOID2U64(blue); |
| 995 | |
Chris Wilson | b803918 | 2010-07-01 22:38:54 +0100 | [diff] [blame] | 996 | return DRM_IOCTL(fd, DRM_IOCTL_MODE_SETGAMMA, &l); |
Jesse Barnes | 731cd55 | 2008-12-17 10:09:49 -0800 | [diff] [blame] | 997 | } |
Kristian Høgsberg | b0b9663 | 2009-09-11 13:27:35 -0400 | [diff] [blame] | 998 | |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 999 | drm_public int drmHandleEvent(int fd, drmEventContextPtr evctx) |
Kristian Høgsberg | b0b9663 | 2009-09-11 13:27:35 -0400 | [diff] [blame] | 1000 | { |
| 1001 | char buffer[1024]; |
| 1002 | int len, i; |
| 1003 | struct drm_event *e; |
| 1004 | struct drm_event_vblank *vblank; |
Keith Packard | d4331dd | 2017-07-01 00:43:15 -0700 | [diff] [blame] | 1005 | struct drm_event_crtc_sequence *seq; |
Ander Conselvan de Oliveira | 890d43a | 2015-08-17 16:21:24 +0300 | [diff] [blame] | 1006 | void *user_data; |
Hyungwon Hwang | 4bac035 | 2015-08-19 09:58:39 +0900 | [diff] [blame] | 1007 | |
Kristian Høgsberg | b0b9663 | 2009-09-11 13:27:35 -0400 | [diff] [blame] | 1008 | /* The DRM read semantics guarantees that we always get only |
| 1009 | * complete events. */ |
| 1010 | |
| 1011 | len = read(fd, buffer, sizeof buffer); |
| 1012 | if (len == 0) |
| 1013 | return 0; |
Jan Vesely | de8532d | 2014-11-30 12:53:18 -0500 | [diff] [blame] | 1014 | if (len < (int)sizeof *e) |
Kristian Høgsberg | b0b9663 | 2009-09-11 13:27:35 -0400 | [diff] [blame] | 1015 | return -1; |
| 1016 | |
| 1017 | i = 0; |
| 1018 | while (i < len) { |
Thierry Reding | ecc2a09 | 2015-04-13 11:36:59 +0200 | [diff] [blame] | 1019 | e = (struct drm_event *)(buffer + i); |
Kristian Høgsberg | b0b9663 | 2009-09-11 13:27:35 -0400 | [diff] [blame] | 1020 | switch (e->type) { |
| 1021 | case DRM_EVENT_VBLANK: |
| 1022 | if (evctx->version < 1 || |
| 1023 | evctx->vblank_handler == NULL) |
| 1024 | break; |
| 1025 | vblank = (struct drm_event_vblank *) e; |
| 1026 | evctx->vblank_handler(fd, |
Hyungwon Hwang | 4bac035 | 2015-08-19 09:58:39 +0900 | [diff] [blame] | 1027 | vblank->sequence, |
Kristian Høgsberg | b0b9663 | 2009-09-11 13:27:35 -0400 | [diff] [blame] | 1028 | vblank->tv_sec, |
| 1029 | vblank->tv_usec, |
| 1030 | U642VOID (vblank->user_data)); |
| 1031 | break; |
Kristian Høgsberg | b80bcff | 2009-11-12 14:06:45 -0500 | [diff] [blame] | 1032 | case DRM_EVENT_FLIP_COMPLETE: |
Kristian Høgsberg | b80bcff | 2009-11-12 14:06:45 -0500 | [diff] [blame] | 1033 | vblank = (struct drm_event_vblank *) e; |
Ander Conselvan de Oliveira | 890d43a | 2015-08-17 16:21:24 +0300 | [diff] [blame] | 1034 | user_data = U642VOID (vblank->user_data); |
| 1035 | |
| 1036 | if (evctx->version >= 3 && evctx->page_flip_handler2) |
| 1037 | evctx->page_flip_handler2(fd, |
| 1038 | vblank->sequence, |
| 1039 | vblank->tv_sec, |
| 1040 | vblank->tv_usec, |
| 1041 | vblank->crtc_id, |
| 1042 | user_data); |
| 1043 | else if (evctx->version >= 2 && evctx->page_flip_handler) |
| 1044 | evctx->page_flip_handler(fd, |
| 1045 | vblank->sequence, |
| 1046 | vblank->tv_sec, |
| 1047 | vblank->tv_usec, |
| 1048 | user_data); |
Kristian Høgsberg | b80bcff | 2009-11-12 14:06:45 -0500 | [diff] [blame] | 1049 | break; |
Keith Packard | d4331dd | 2017-07-01 00:43:15 -0700 | [diff] [blame] | 1050 | case DRM_EVENT_CRTC_SEQUENCE: |
| 1051 | seq = (struct drm_event_crtc_sequence *) e; |
| 1052 | if (evctx->version >= 4 && evctx->sequence_handler) |
| 1053 | evctx->sequence_handler(fd, |
| 1054 | seq->sequence, |
| 1055 | seq->time_ns, |
| 1056 | seq->user_data); |
| 1057 | break; |
Kristian Høgsberg | b0b9663 | 2009-09-11 13:27:35 -0400 | [diff] [blame] | 1058 | default: |
| 1059 | break; |
| 1060 | } |
| 1061 | i += e->length; |
| 1062 | } |
| 1063 | |
| 1064 | return 0; |
| 1065 | } |
| 1066 | |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 1067 | drm_public int drmModePageFlip(int fd, uint32_t crtc_id, uint32_t fb_id, |
Kristian Høgsberg | b80bcff | 2009-11-12 14:06:45 -0500 | [diff] [blame] | 1068 | uint32_t flags, void *user_data) |
| 1069 | { |
| 1070 | struct drm_mode_crtc_page_flip flip; |
| 1071 | |
Daniel Vetter | 7e0460c | 2015-02-11 12:03:12 +0100 | [diff] [blame] | 1072 | memclear(flip); |
Kristian Høgsberg | b80bcff | 2009-11-12 14:06:45 -0500 | [diff] [blame] | 1073 | flip.fb_id = fb_id; |
| 1074 | flip.crtc_id = crtc_id; |
| 1075 | flip.user_data = VOID2U64(user_data); |
| 1076 | flip.flags = flags; |
Kristian Høgsberg | b80bcff | 2009-11-12 14:06:45 -0500 | [diff] [blame] | 1077 | |
Chris Wilson | b803918 | 2010-07-01 22:38:54 +0100 | [diff] [blame] | 1078 | return DRM_IOCTL(fd, DRM_IOCTL_MODE_PAGE_FLIP, &flip); |
Kristian Høgsberg | b80bcff | 2009-11-12 14:06:45 -0500 | [diff] [blame] | 1079 | } |
Jesse Barnes | ac168bf | 2011-04-29 08:53:53 -0700 | [diff] [blame] | 1080 | |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 1081 | drm_public int drmModePageFlipTarget(int fd, uint32_t crtc_id, uint32_t fb_id, |
Michel Dänzer | 7dd2847 | 2016-06-29 18:07:25 +0900 | [diff] [blame] | 1082 | uint32_t flags, void *user_data, |
| 1083 | uint32_t target_vblank) |
| 1084 | { |
| 1085 | struct drm_mode_crtc_page_flip_target flip_target; |
| 1086 | |
| 1087 | memclear(flip_target); |
| 1088 | flip_target.fb_id = fb_id; |
| 1089 | flip_target.crtc_id = crtc_id; |
| 1090 | flip_target.user_data = VOID2U64(user_data); |
| 1091 | flip_target.flags = flags; |
| 1092 | flip_target.sequence = target_vblank; |
| 1093 | |
| 1094 | return DRM_IOCTL(fd, DRM_IOCTL_MODE_PAGE_FLIP, &flip_target); |
| 1095 | } |
| 1096 | |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 1097 | drm_public int drmModeSetPlane(int fd, uint32_t plane_id, uint32_t crtc_id, |
Jesse Barnes | ac168bf | 2011-04-29 08:53:53 -0700 | [diff] [blame] | 1098 | uint32_t fb_id, uint32_t flags, |
Daniel Kurtz | 828c3e8 | 2014-05-01 19:56:43 +0800 | [diff] [blame] | 1099 | int32_t crtc_x, int32_t crtc_y, |
Jesse Barnes | ac168bf | 2011-04-29 08:53:53 -0700 | [diff] [blame] | 1100 | uint32_t crtc_w, uint32_t crtc_h, |
| 1101 | uint32_t src_x, uint32_t src_y, |
| 1102 | uint32_t src_w, uint32_t src_h) |
Jesse Barnes | ac168bf | 2011-04-29 08:53:53 -0700 | [diff] [blame] | 1103 | { |
| 1104 | struct drm_mode_set_plane s; |
| 1105 | |
Daniel Vetter | 7e0460c | 2015-02-11 12:03:12 +0100 | [diff] [blame] | 1106 | memclear(s); |
Jesse Barnes | ac168bf | 2011-04-29 08:53:53 -0700 | [diff] [blame] | 1107 | s.plane_id = plane_id; |
| 1108 | s.crtc_id = crtc_id; |
| 1109 | s.fb_id = fb_id; |
| 1110 | s.flags = flags; |
| 1111 | s.crtc_x = crtc_x; |
| 1112 | s.crtc_y = crtc_y; |
| 1113 | s.crtc_w = crtc_w; |
| 1114 | s.crtc_h = crtc_h; |
| 1115 | s.src_x = src_x; |
| 1116 | s.src_y = src_y; |
| 1117 | s.src_w = src_w; |
| 1118 | s.src_h = src_h; |
| 1119 | |
| 1120 | return DRM_IOCTL(fd, DRM_IOCTL_MODE_SETPLANE, &s); |
| 1121 | } |
| 1122 | |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 1123 | drm_public drmModePlanePtr drmModeGetPlane(int fd, uint32_t plane_id) |
Jesse Barnes | ac168bf | 2011-04-29 08:53:53 -0700 | [diff] [blame] | 1124 | { |
| 1125 | struct drm_mode_get_plane ovr, counts; |
| 1126 | drmModePlanePtr r = 0; |
| 1127 | |
| 1128 | retry: |
Daniel Vetter | 7e0460c | 2015-02-11 12:03:12 +0100 | [diff] [blame] | 1129 | memclear(ovr); |
Jesse Barnes | ac168bf | 2011-04-29 08:53:53 -0700 | [diff] [blame] | 1130 | ovr.plane_id = plane_id; |
| 1131 | if (drmIoctl(fd, DRM_IOCTL_MODE_GETPLANE, &ovr)) |
| 1132 | return 0; |
| 1133 | |
| 1134 | counts = ovr; |
| 1135 | |
| 1136 | if (ovr.count_format_types) { |
| 1137 | ovr.format_type_ptr = VOID2U64(drmMalloc(ovr.count_format_types * |
| 1138 | sizeof(uint32_t))); |
| 1139 | if (!ovr.format_type_ptr) |
| 1140 | goto err_allocs; |
| 1141 | } |
| 1142 | |
| 1143 | if (drmIoctl(fd, DRM_IOCTL_MODE_GETPLANE, &ovr)) |
| 1144 | goto err_allocs; |
| 1145 | |
| 1146 | if (counts.count_format_types < ovr.count_format_types) { |
| 1147 | drmFree(U642VOID(ovr.format_type_ptr)); |
| 1148 | goto retry; |
| 1149 | } |
| 1150 | |
| 1151 | if (!(r = drmMalloc(sizeof(*r)))) |
| 1152 | goto err_allocs; |
| 1153 | |
| 1154 | r->count_formats = ovr.count_format_types; |
| 1155 | r->plane_id = ovr.plane_id; |
| 1156 | r->crtc_id = ovr.crtc_id; |
| 1157 | r->fb_id = ovr.fb_id; |
| 1158 | r->possible_crtcs = ovr.possible_crtcs; |
| 1159 | r->gamma_size = ovr.gamma_size; |
| 1160 | r->formats = drmAllocCpy(U642VOID(ovr.format_type_ptr), |
| 1161 | ovr.count_format_types, sizeof(uint32_t)); |
| 1162 | if (ovr.count_format_types && !r->formats) { |
| 1163 | drmFree(r->formats); |
Ville Syrjälä | df497e9 | 2012-02-02 14:53:39 -0500 | [diff] [blame] | 1164 | drmFree(r); |
Jesse Barnes | ac168bf | 2011-04-29 08:53:53 -0700 | [diff] [blame] | 1165 | r = 0; |
| 1166 | } |
| 1167 | |
| 1168 | err_allocs: |
| 1169 | drmFree(U642VOID(ovr.format_type_ptr)); |
| 1170 | |
| 1171 | return r; |
| 1172 | } |
| 1173 | |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 1174 | drm_public void drmModeFreePlane(drmModePlanePtr ptr) |
Jesse Barnes | ac168bf | 2011-04-29 08:53:53 -0700 | [diff] [blame] | 1175 | { |
| 1176 | if (!ptr) |
| 1177 | return; |
| 1178 | |
| 1179 | drmFree(ptr->formats); |
| 1180 | drmFree(ptr); |
| 1181 | } |
| 1182 | |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 1183 | drm_public drmModePlaneResPtr drmModeGetPlaneResources(int fd) |
Jesse Barnes | ac168bf | 2011-04-29 08:53:53 -0700 | [diff] [blame] | 1184 | { |
| 1185 | struct drm_mode_get_plane_res res, counts; |
| 1186 | drmModePlaneResPtr r = 0; |
| 1187 | |
| 1188 | retry: |
Daniel Vetter | 7e0460c | 2015-02-11 12:03:12 +0100 | [diff] [blame] | 1189 | memclear(res); |
Jesse Barnes | ac168bf | 2011-04-29 08:53:53 -0700 | [diff] [blame] | 1190 | if (drmIoctl(fd, DRM_IOCTL_MODE_GETPLANERESOURCES, &res)) |
| 1191 | return 0; |
| 1192 | |
| 1193 | counts = res; |
| 1194 | |
| 1195 | if (res.count_planes) { |
| 1196 | res.plane_id_ptr = VOID2U64(drmMalloc(res.count_planes * |
| 1197 | sizeof(uint32_t))); |
| 1198 | if (!res.plane_id_ptr) |
| 1199 | goto err_allocs; |
| 1200 | } |
| 1201 | |
| 1202 | if (drmIoctl(fd, DRM_IOCTL_MODE_GETPLANERESOURCES, &res)) |
| 1203 | goto err_allocs; |
| 1204 | |
| 1205 | if (counts.count_planes < res.count_planes) { |
| 1206 | drmFree(U642VOID(res.plane_id_ptr)); |
| 1207 | goto retry; |
| 1208 | } |
| 1209 | |
| 1210 | if (!(r = drmMalloc(sizeof(*r)))) |
| 1211 | goto err_allocs; |
| 1212 | |
| 1213 | r->count_planes = res.count_planes; |
| 1214 | r->planes = drmAllocCpy(U642VOID(res.plane_id_ptr), |
| 1215 | res.count_planes, sizeof(uint32_t)); |
| 1216 | if (res.count_planes && !r->planes) { |
| 1217 | drmFree(r->planes); |
Ville Syrjälä | df497e9 | 2012-02-02 14:53:39 -0500 | [diff] [blame] | 1218 | drmFree(r); |
Jesse Barnes | ac168bf | 2011-04-29 08:53:53 -0700 | [diff] [blame] | 1219 | r = 0; |
| 1220 | } |
| 1221 | |
| 1222 | err_allocs: |
| 1223 | drmFree(U642VOID(res.plane_id_ptr)); |
| 1224 | |
| 1225 | return r; |
| 1226 | } |
Ville Syrjälä | a14c3dd | 2012-02-02 14:53:41 -0500 | [diff] [blame] | 1227 | |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 1228 | drm_public void drmModeFreePlaneResources(drmModePlaneResPtr ptr) |
Ville Syrjälä | a14c3dd | 2012-02-02 14:53:41 -0500 | [diff] [blame] | 1229 | { |
| 1230 | if (!ptr) |
| 1231 | return; |
| 1232 | |
| 1233 | drmFree(ptr->planes); |
| 1234 | drmFree(ptr); |
| 1235 | } |
Paulo Zanoni | 8c75703 | 2012-05-15 18:38:28 -0300 | [diff] [blame] | 1236 | |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 1237 | drm_public drmModeObjectPropertiesPtr drmModeObjectGetProperties(int fd, |
Paulo Zanoni | 8c75703 | 2012-05-15 18:38:28 -0300 | [diff] [blame] | 1238 | uint32_t object_id, |
| 1239 | uint32_t object_type) |
| 1240 | { |
| 1241 | struct drm_mode_obj_get_properties properties; |
| 1242 | drmModeObjectPropertiesPtr ret = NULL; |
| 1243 | uint32_t count; |
| 1244 | |
| 1245 | retry: |
Daniel Vetter | 7e0460c | 2015-02-11 12:03:12 +0100 | [diff] [blame] | 1246 | memclear(properties); |
Paulo Zanoni | 8c75703 | 2012-05-15 18:38:28 -0300 | [diff] [blame] | 1247 | properties.obj_id = object_id; |
| 1248 | properties.obj_type = object_type; |
| 1249 | |
| 1250 | if (drmIoctl(fd, DRM_IOCTL_MODE_OBJ_GETPROPERTIES, &properties)) |
| 1251 | return 0; |
| 1252 | |
| 1253 | count = properties.count_props; |
| 1254 | |
| 1255 | if (count) { |
| 1256 | properties.props_ptr = VOID2U64(drmMalloc(count * |
| 1257 | sizeof(uint32_t))); |
| 1258 | if (!properties.props_ptr) |
| 1259 | goto err_allocs; |
| 1260 | properties.prop_values_ptr = VOID2U64(drmMalloc(count * |
| 1261 | sizeof(uint64_t))); |
| 1262 | if (!properties.prop_values_ptr) |
| 1263 | goto err_allocs; |
| 1264 | } |
| 1265 | |
| 1266 | if (drmIoctl(fd, DRM_IOCTL_MODE_OBJ_GETPROPERTIES, &properties)) |
| 1267 | goto err_allocs; |
| 1268 | |
| 1269 | if (count < properties.count_props) { |
| 1270 | drmFree(U642VOID(properties.props_ptr)); |
| 1271 | drmFree(U642VOID(properties.prop_values_ptr)); |
| 1272 | goto retry; |
| 1273 | } |
| 1274 | count = properties.count_props; |
| 1275 | |
| 1276 | ret = drmMalloc(sizeof(*ret)); |
| 1277 | if (!ret) |
| 1278 | goto err_allocs; |
| 1279 | |
| 1280 | ret->count_props = count; |
| 1281 | ret->props = drmAllocCpy(U642VOID(properties.props_ptr), |
| 1282 | count, sizeof(uint32_t)); |
| 1283 | ret->prop_values = drmAllocCpy(U642VOID(properties.prop_values_ptr), |
| 1284 | count, sizeof(uint64_t)); |
| 1285 | if (ret->count_props && (!ret->props || !ret->prop_values)) { |
| 1286 | drmFree(ret->props); |
| 1287 | drmFree(ret->prop_values); |
| 1288 | drmFree(ret); |
| 1289 | ret = NULL; |
| 1290 | } |
| 1291 | |
| 1292 | err_allocs: |
| 1293 | drmFree(U642VOID(properties.props_ptr)); |
| 1294 | drmFree(U642VOID(properties.prop_values_ptr)); |
| 1295 | return ret; |
| 1296 | } |
| 1297 | |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 1298 | drm_public void drmModeFreeObjectProperties(drmModeObjectPropertiesPtr ptr) |
Paulo Zanoni | 8c75703 | 2012-05-15 18:38:28 -0300 | [diff] [blame] | 1299 | { |
| 1300 | if (!ptr) |
| 1301 | return; |
| 1302 | drmFree(ptr->props); |
| 1303 | drmFree(ptr->prop_values); |
| 1304 | drmFree(ptr); |
| 1305 | } |
| 1306 | |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 1307 | drm_public int drmModeObjectSetProperty(int fd, uint32_t object_id, uint32_t object_type, |
Paulo Zanoni | 8c75703 | 2012-05-15 18:38:28 -0300 | [diff] [blame] | 1308 | uint32_t property_id, uint64_t value) |
| 1309 | { |
| 1310 | struct drm_mode_obj_set_property prop; |
| 1311 | |
Daniel Vetter | 7e0460c | 2015-02-11 12:03:12 +0100 | [diff] [blame] | 1312 | memclear(prop); |
Paulo Zanoni | 8c75703 | 2012-05-15 18:38:28 -0300 | [diff] [blame] | 1313 | prop.value = value; |
| 1314 | prop.prop_id = property_id; |
| 1315 | prop.obj_id = object_id; |
| 1316 | prop.obj_type = object_type; |
| 1317 | |
| 1318 | return DRM_IOCTL(fd, DRM_IOCTL_MODE_OBJ_SETPROPERTY, &prop); |
| 1319 | } |
Ville Syrjälä | ed44e0b | 2015-06-22 17:26:02 +0100 | [diff] [blame] | 1320 | |
| 1321 | typedef struct _drmModeAtomicReqItem drmModeAtomicReqItem, *drmModeAtomicReqItemPtr; |
| 1322 | |
| 1323 | struct _drmModeAtomicReqItem { |
| 1324 | uint32_t object_id; |
| 1325 | uint32_t property_id; |
| 1326 | uint64_t value; |
| 1327 | }; |
| 1328 | |
| 1329 | struct _drmModeAtomicReq { |
| 1330 | uint32_t cursor; |
| 1331 | uint32_t size_items; |
| 1332 | drmModeAtomicReqItemPtr items; |
| 1333 | }; |
| 1334 | |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 1335 | drm_public drmModeAtomicReqPtr drmModeAtomicAlloc(void) |
Ville Syrjälä | ed44e0b | 2015-06-22 17:26:02 +0100 | [diff] [blame] | 1336 | { |
| 1337 | drmModeAtomicReqPtr req; |
| 1338 | |
| 1339 | req = drmMalloc(sizeof *req); |
| 1340 | if (!req) |
| 1341 | return NULL; |
| 1342 | |
| 1343 | req->items = NULL; |
| 1344 | req->cursor = 0; |
| 1345 | req->size_items = 0; |
| 1346 | |
| 1347 | return req; |
| 1348 | } |
| 1349 | |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 1350 | drm_public drmModeAtomicReqPtr drmModeAtomicDuplicate(drmModeAtomicReqPtr old) |
Ville Syrjälä | ed44e0b | 2015-06-22 17:26:02 +0100 | [diff] [blame] | 1351 | { |
| 1352 | drmModeAtomicReqPtr new; |
| 1353 | |
Emil Velikov | ed3c665 | 2015-09-05 17:20:53 +0100 | [diff] [blame] | 1354 | if (!old) |
| 1355 | return NULL; |
| 1356 | |
Ville Syrjälä | ed44e0b | 2015-06-22 17:26:02 +0100 | [diff] [blame] | 1357 | new = drmMalloc(sizeof *new); |
| 1358 | if (!new) |
| 1359 | return NULL; |
| 1360 | |
| 1361 | new->cursor = old->cursor; |
| 1362 | new->size_items = old->size_items; |
| 1363 | |
| 1364 | if (old->size_items) { |
| 1365 | new->items = drmMalloc(old->size_items * sizeof(*new->items)); |
| 1366 | if (!new->items) { |
| 1367 | free(new); |
| 1368 | return NULL; |
| 1369 | } |
| 1370 | memcpy(new->items, old->items, |
Adrian Salido | 763f646 | 2019-04-24 10:08:40 -0700 | [diff] [blame] | 1371 | old->cursor * sizeof(*new->items)); |
Ville Syrjälä | ed44e0b | 2015-06-22 17:26:02 +0100 | [diff] [blame] | 1372 | } else { |
| 1373 | new->items = NULL; |
| 1374 | } |
| 1375 | |
| 1376 | return new; |
| 1377 | } |
| 1378 | |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 1379 | drm_public int drmModeAtomicMerge(drmModeAtomicReqPtr base, |
| 1380 | drmModeAtomicReqPtr augment) |
Ville Syrjälä | ed44e0b | 2015-06-22 17:26:02 +0100 | [diff] [blame] | 1381 | { |
Emil Velikov | ed3c665 | 2015-09-05 17:20:53 +0100 | [diff] [blame] | 1382 | if (!base) |
| 1383 | return -EINVAL; |
| 1384 | |
Ville Syrjälä | ed44e0b | 2015-06-22 17:26:02 +0100 | [diff] [blame] | 1385 | if (!augment || augment->cursor == 0) |
| 1386 | return 0; |
| 1387 | |
| 1388 | if (base->cursor + augment->cursor >= base->size_items) { |
| 1389 | drmModeAtomicReqItemPtr new; |
| 1390 | int saved_size = base->size_items; |
| 1391 | |
| 1392 | base->size_items = base->cursor + augment->cursor; |
| 1393 | new = realloc(base->items, |
| 1394 | base->size_items * sizeof(*base->items)); |
| 1395 | if (!new) { |
| 1396 | base->size_items = saved_size; |
| 1397 | return -ENOMEM; |
| 1398 | } |
| 1399 | base->items = new; |
| 1400 | } |
| 1401 | |
| 1402 | memcpy(&base->items[base->cursor], augment->items, |
| 1403 | augment->cursor * sizeof(*augment->items)); |
| 1404 | base->cursor += augment->cursor; |
| 1405 | |
| 1406 | return 0; |
| 1407 | } |
| 1408 | |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 1409 | drm_public int drmModeAtomicGetCursor(drmModeAtomicReqPtr req) |
Ville Syrjälä | ed44e0b | 2015-06-22 17:26:02 +0100 | [diff] [blame] | 1410 | { |
Emil Velikov | ed3c665 | 2015-09-05 17:20:53 +0100 | [diff] [blame] | 1411 | if (!req) |
| 1412 | return -EINVAL; |
Ville Syrjälä | ed44e0b | 2015-06-22 17:26:02 +0100 | [diff] [blame] | 1413 | return req->cursor; |
| 1414 | } |
| 1415 | |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 1416 | drm_public void drmModeAtomicSetCursor(drmModeAtomicReqPtr req, int cursor) |
Ville Syrjälä | ed44e0b | 2015-06-22 17:26:02 +0100 | [diff] [blame] | 1417 | { |
Emil Velikov | ed3c665 | 2015-09-05 17:20:53 +0100 | [diff] [blame] | 1418 | if (req) |
| 1419 | req->cursor = cursor; |
Ville Syrjälä | ed44e0b | 2015-06-22 17:26:02 +0100 | [diff] [blame] | 1420 | } |
| 1421 | |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 1422 | drm_public int drmModeAtomicAddProperty(drmModeAtomicReqPtr req, |
| 1423 | uint32_t object_id, |
| 1424 | uint32_t property_id, |
| 1425 | uint64_t value) |
Ville Syrjälä | ed44e0b | 2015-06-22 17:26:02 +0100 | [diff] [blame] | 1426 | { |
Emil Velikov | ed3c665 | 2015-09-05 17:20:53 +0100 | [diff] [blame] | 1427 | if (!req) |
| 1428 | return -EINVAL; |
| 1429 | |
Daniel Stone | 45eee3f | 2018-03-07 12:41:12 +0000 | [diff] [blame] | 1430 | if (object_id == 0 || property_id == 0) |
| 1431 | return -EINVAL; |
| 1432 | |
Ville Syrjälä | ed44e0b | 2015-06-22 17:26:02 +0100 | [diff] [blame] | 1433 | if (req->cursor >= req->size_items) { |
Adrian Salido | 763f646 | 2019-04-24 10:08:40 -0700 | [diff] [blame] | 1434 | const uint32_t item_size_inc = getpagesize() / sizeof(*req->items); |
Ville Syrjälä | ed44e0b | 2015-06-22 17:26:02 +0100 | [diff] [blame] | 1435 | drmModeAtomicReqItemPtr new; |
| 1436 | |
Adrian Salido | 763f646 | 2019-04-24 10:08:40 -0700 | [diff] [blame] | 1437 | req->size_items += item_size_inc; |
Ville Syrjälä | ed44e0b | 2015-06-22 17:26:02 +0100 | [diff] [blame] | 1438 | new = realloc(req->items, req->size_items * sizeof(*req->items)); |
| 1439 | if (!new) { |
Adrian Salido | 763f646 | 2019-04-24 10:08:40 -0700 | [diff] [blame] | 1440 | req->size_items -= item_size_inc; |
Ville Syrjälä | ed44e0b | 2015-06-22 17:26:02 +0100 | [diff] [blame] | 1441 | return -ENOMEM; |
| 1442 | } |
| 1443 | req->items = new; |
| 1444 | } |
| 1445 | |
| 1446 | req->items[req->cursor].object_id = object_id; |
| 1447 | req->items[req->cursor].property_id = property_id; |
| 1448 | req->items[req->cursor].value = value; |
| 1449 | req->cursor++; |
| 1450 | |
| 1451 | return req->cursor; |
| 1452 | } |
| 1453 | |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 1454 | drm_public void drmModeAtomicFree(drmModeAtomicReqPtr req) |
Ville Syrjälä | ed44e0b | 2015-06-22 17:26:02 +0100 | [diff] [blame] | 1455 | { |
| 1456 | if (!req) |
| 1457 | return; |
| 1458 | |
| 1459 | if (req->items) |
| 1460 | drmFree(req->items); |
| 1461 | drmFree(req); |
| 1462 | } |
| 1463 | |
| 1464 | static int sort_req_list(const void *misc, const void *other) |
| 1465 | { |
| 1466 | const drmModeAtomicReqItem *first = misc; |
| 1467 | const drmModeAtomicReqItem *second = other; |
| 1468 | |
| 1469 | if (first->object_id < second->object_id) |
| 1470 | return -1; |
| 1471 | else if (first->object_id > second->object_id) |
| 1472 | return 1; |
| 1473 | else |
| 1474 | return second->property_id - first->property_id; |
| 1475 | } |
| 1476 | |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 1477 | drm_public int drmModeAtomicCommit(int fd, drmModeAtomicReqPtr req, |
| 1478 | uint32_t flags, void *user_data) |
Ville Syrjälä | ed44e0b | 2015-06-22 17:26:02 +0100 | [diff] [blame] | 1479 | { |
Chris Wilson | 1a6efaf | 2015-07-21 13:46:45 +0100 | [diff] [blame] | 1480 | drmModeAtomicReqPtr sorted; |
Ville Syrjälä | ed44e0b | 2015-06-22 17:26:02 +0100 | [diff] [blame] | 1481 | struct drm_mode_atomic atomic; |
| 1482 | uint32_t *objs_ptr = NULL; |
| 1483 | uint32_t *count_props_ptr = NULL; |
| 1484 | uint32_t *props_ptr = NULL; |
| 1485 | uint64_t *prop_values_ptr = NULL; |
| 1486 | uint32_t last_obj_id = 0; |
| 1487 | uint32_t i; |
| 1488 | int obj_idx = -1; |
| 1489 | int ret = -1; |
| 1490 | |
Emil Velikov | ed3c665 | 2015-09-05 17:20:53 +0100 | [diff] [blame] | 1491 | if (!req) |
| 1492 | return -EINVAL; |
| 1493 | |
Chris Wilson | 1a6efaf | 2015-07-21 13:46:45 +0100 | [diff] [blame] | 1494 | if (req->cursor == 0) |
| 1495 | return 0; |
| 1496 | |
| 1497 | sorted = drmModeAtomicDuplicate(req); |
| 1498 | if (sorted == NULL) |
Ville Syrjälä | ed44e0b | 2015-06-22 17:26:02 +0100 | [diff] [blame] | 1499 | return -ENOMEM; |
| 1500 | |
| 1501 | memclear(atomic); |
| 1502 | |
| 1503 | /* Sort the list by object ID, then by property ID. */ |
| 1504 | qsort(sorted->items, sorted->cursor, sizeof(*sorted->items), |
| 1505 | sort_req_list); |
| 1506 | |
| 1507 | /* Now the list is sorted, eliminate duplicate property sets. */ |
| 1508 | for (i = 0; i < sorted->cursor; i++) { |
| 1509 | if (sorted->items[i].object_id != last_obj_id) { |
| 1510 | atomic.count_objs++; |
| 1511 | last_obj_id = sorted->items[i].object_id; |
| 1512 | } |
| 1513 | |
| 1514 | if (i == sorted->cursor - 1) |
| 1515 | continue; |
| 1516 | |
| 1517 | if (sorted->items[i].object_id != sorted->items[i + 1].object_id || |
| 1518 | sorted->items[i].property_id != sorted->items[i + 1].property_id) |
| 1519 | continue; |
| 1520 | |
| 1521 | memmove(&sorted->items[i], &sorted->items[i + 1], |
| 1522 | (sorted->cursor - i - 1) * sizeof(*sorted->items)); |
| 1523 | sorted->cursor--; |
| 1524 | } |
| 1525 | |
| 1526 | objs_ptr = drmMalloc(atomic.count_objs * sizeof objs_ptr[0]); |
| 1527 | if (!objs_ptr) { |
| 1528 | errno = ENOMEM; |
| 1529 | goto out; |
| 1530 | } |
| 1531 | |
| 1532 | count_props_ptr = drmMalloc(atomic.count_objs * sizeof count_props_ptr[0]); |
| 1533 | if (!count_props_ptr) { |
| 1534 | errno = ENOMEM; |
| 1535 | goto out; |
| 1536 | } |
| 1537 | |
| 1538 | props_ptr = drmMalloc(sorted->cursor * sizeof props_ptr[0]); |
| 1539 | if (!props_ptr) { |
| 1540 | errno = ENOMEM; |
| 1541 | goto out; |
| 1542 | } |
| 1543 | |
| 1544 | prop_values_ptr = drmMalloc(sorted->cursor * sizeof prop_values_ptr[0]); |
| 1545 | if (!prop_values_ptr) { |
| 1546 | errno = ENOMEM; |
| 1547 | goto out; |
| 1548 | } |
| 1549 | |
| 1550 | for (i = 0, last_obj_id = 0; i < sorted->cursor; i++) { |
| 1551 | if (sorted->items[i].object_id != last_obj_id) { |
| 1552 | obj_idx++; |
| 1553 | objs_ptr[obj_idx] = sorted->items[i].object_id; |
| 1554 | last_obj_id = objs_ptr[obj_idx]; |
| 1555 | } |
| 1556 | |
| 1557 | count_props_ptr[obj_idx]++; |
| 1558 | props_ptr[i] = sorted->items[i].property_id; |
| 1559 | prop_values_ptr[i] = sorted->items[i].value; |
| 1560 | |
| 1561 | } |
| 1562 | |
| 1563 | atomic.flags = flags; |
| 1564 | atomic.objs_ptr = VOID2U64(objs_ptr); |
| 1565 | atomic.count_props_ptr = VOID2U64(count_props_ptr); |
| 1566 | atomic.props_ptr = VOID2U64(props_ptr); |
| 1567 | atomic.prop_values_ptr = VOID2U64(prop_values_ptr); |
| 1568 | atomic.user_data = VOID2U64(user_data); |
| 1569 | |
| 1570 | ret = DRM_IOCTL(fd, DRM_IOCTL_MODE_ATOMIC, &atomic); |
| 1571 | |
| 1572 | out: |
| 1573 | drmFree(objs_ptr); |
| 1574 | drmFree(count_props_ptr); |
| 1575 | drmFree(props_ptr); |
| 1576 | drmFree(prop_values_ptr); |
| 1577 | drmModeAtomicFree(sorted); |
| 1578 | |
| 1579 | return ret; |
| 1580 | } |
Daniel Stone | 32471b2 | 2015-06-22 17:26:03 +0100 | [diff] [blame] | 1581 | |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 1582 | drm_public int |
| 1583 | drmModeCreatePropertyBlob(int fd, const void *data, size_t length, |
| 1584 | uint32_t *id) |
Daniel Stone | 32471b2 | 2015-06-22 17:26:03 +0100 | [diff] [blame] | 1585 | { |
| 1586 | struct drm_mode_create_blob create; |
| 1587 | int ret; |
| 1588 | |
| 1589 | if (length >= 0xffffffff) |
| 1590 | return -ERANGE; |
| 1591 | |
| 1592 | memclear(create); |
| 1593 | |
| 1594 | create.length = length; |
| 1595 | create.data = (uintptr_t) data; |
| 1596 | create.blob_id = 0; |
| 1597 | *id = 0; |
| 1598 | |
| 1599 | ret = DRM_IOCTL(fd, DRM_IOCTL_MODE_CREATEPROPBLOB, &create); |
| 1600 | if (ret != 0) |
| 1601 | return ret; |
| 1602 | |
| 1603 | *id = create.blob_id; |
| 1604 | return 0; |
| 1605 | } |
| 1606 | |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 1607 | drm_public int |
Daniel Stone | 32471b2 | 2015-06-22 17:26:03 +0100 | [diff] [blame] | 1608 | drmModeDestroyPropertyBlob(int fd, uint32_t id) |
| 1609 | { |
| 1610 | struct drm_mode_destroy_blob destroy; |
| 1611 | |
| 1612 | memclear(destroy); |
| 1613 | destroy.blob_id = id; |
| 1614 | return DRM_IOCTL(fd, DRM_IOCTL_MODE_DESTROYPROPBLOB, &destroy); |
| 1615 | } |
Keith Packard | c417153 | 2017-03-16 18:11:05 -0700 | [diff] [blame] | 1616 | |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 1617 | drm_public int |
| 1618 | drmModeCreateLease(int fd, const uint32_t *objects, int num_objects, int flags, |
| 1619 | uint32_t *lessee_id) |
Keith Packard | c417153 | 2017-03-16 18:11:05 -0700 | [diff] [blame] | 1620 | { |
| 1621 | struct drm_mode_create_lease create; |
| 1622 | int ret; |
| 1623 | |
| 1624 | memclear(create); |
| 1625 | create.object_ids = (uintptr_t) objects; |
| 1626 | create.object_count = num_objects; |
| 1627 | create.flags = flags; |
| 1628 | |
| 1629 | ret = DRM_IOCTL(fd, DRM_IOCTL_MODE_CREATE_LEASE, &create); |
| 1630 | if (ret == 0) { |
| 1631 | *lessee_id = create.lessee_id; |
| 1632 | return create.fd; |
| 1633 | } |
| 1634 | return -errno; |
| 1635 | } |
| 1636 | |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 1637 | drm_public drmModeLesseeListPtr |
Keith Packard | c417153 | 2017-03-16 18:11:05 -0700 | [diff] [blame] | 1638 | drmModeListLessees(int fd) |
| 1639 | { |
| 1640 | struct drm_mode_list_lessees list; |
| 1641 | uint32_t count; |
| 1642 | drmModeLesseeListPtr ret; |
| 1643 | |
| 1644 | memclear(list); |
| 1645 | |
| 1646 | if (DRM_IOCTL(fd, DRM_IOCTL_MODE_LIST_LESSEES, &list)) |
| 1647 | return NULL; |
| 1648 | |
| 1649 | count = list.count_lessees; |
| 1650 | ret = drmMalloc(sizeof (drmModeLesseeListRes) + count * sizeof (ret->lessees[0])); |
| 1651 | if (!ret) |
| 1652 | return NULL; |
| 1653 | |
| 1654 | list.lessees_ptr = VOID2U64(&ret->lessees[0]); |
| 1655 | if (DRM_IOCTL(fd, DRM_IOCTL_MODE_LIST_LESSEES, &list)) { |
| 1656 | drmFree(ret); |
| 1657 | return NULL; |
| 1658 | } |
| 1659 | |
| 1660 | ret->count = count; |
| 1661 | return ret; |
| 1662 | } |
| 1663 | |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 1664 | drm_public drmModeObjectListPtr |
Keith Packard | c417153 | 2017-03-16 18:11:05 -0700 | [diff] [blame] | 1665 | drmModeGetLease(int fd) |
| 1666 | { |
| 1667 | struct drm_mode_get_lease get; |
| 1668 | uint32_t count; |
| 1669 | drmModeObjectListPtr ret; |
| 1670 | |
| 1671 | memclear(get); |
| 1672 | |
| 1673 | if (DRM_IOCTL(fd, DRM_IOCTL_MODE_GET_LEASE, &get)) |
| 1674 | return NULL; |
| 1675 | |
| 1676 | count = get.count_objects; |
| 1677 | ret = drmMalloc(sizeof (drmModeObjectListRes) + count * sizeof (ret->objects[0])); |
| 1678 | if (!ret) |
| 1679 | return NULL; |
| 1680 | |
| 1681 | get.objects_ptr = VOID2U64(&ret->objects[0]); |
| 1682 | if (DRM_IOCTL(fd, DRM_IOCTL_MODE_GET_LEASE, &get)) { |
| 1683 | drmFree(ret); |
| 1684 | return NULL; |
| 1685 | } |
| 1686 | |
| 1687 | ret->count = count; |
| 1688 | return ret; |
| 1689 | } |
| 1690 | |
Lucas De Marchi | 26f9ce5 | 2018-09-13 15:42:26 -0700 | [diff] [blame] | 1691 | drm_public int |
Keith Packard | c417153 | 2017-03-16 18:11:05 -0700 | [diff] [blame] | 1692 | drmModeRevokeLease(int fd, uint32_t lessee_id) |
| 1693 | { |
| 1694 | struct drm_mode_revoke_lease revoke; |
| 1695 | int ret; |
| 1696 | |
| 1697 | memclear(revoke); |
| 1698 | |
| 1699 | revoke.lessee_id = lessee_id; |
| 1700 | |
| 1701 | ret = DRM_IOCTL(fd, DRM_IOCTL_MODE_REVOKE_LEASE, &revoke); |
| 1702 | if (ret == 0) |
| 1703 | return 0; |
| 1704 | return -errno; |
| 1705 | } |
Daniel Stone | d8731e9 | 2020-02-11 12:19:16 -0800 | [diff] [blame] | 1706 | |
| 1707 | drm_public drmModeFB2Ptr |
| 1708 | drmModeGetFB2(int fd, uint32_t fb_id) |
| 1709 | { |
| 1710 | struct drm_mode_fb_cmd2 get = { |
| 1711 | .fb_id = fb_id, |
| 1712 | }; |
| 1713 | drmModeFB2Ptr ret; |
| 1714 | int err; |
| 1715 | |
| 1716 | err = DRM_IOCTL(fd, DRM_IOCTL_MODE_GETFB2, &get); |
| 1717 | if (err != 0) |
| 1718 | return NULL; |
| 1719 | |
| 1720 | ret = drmMalloc(sizeof(drmModeFB2)); |
| 1721 | if (!ret) |
| 1722 | return NULL; |
| 1723 | |
| 1724 | ret->fb_id = fb_id; |
| 1725 | ret->width = get.width; |
| 1726 | ret->height = get.height; |
| 1727 | ret->pixel_format = get.pixel_format; |
| 1728 | ret->flags = get.flags; |
| 1729 | ret->modifier = get.modifier[0]; |
| 1730 | memcpy(ret->handles, get.handles, sizeof(uint32_t) * 4); |
| 1731 | memcpy(ret->pitches, get.pitches, sizeof(uint32_t) * 4); |
| 1732 | memcpy(ret->offsets, get.offsets, sizeof(uint32_t) * 4); |
| 1733 | |
| 1734 | return ret; |
| 1735 | } |
| 1736 | |
| 1737 | drm_public void drmModeFreeFB2(drmModeFB2Ptr ptr) |
| 1738 | { |
| 1739 | drmFree(ptr); |
| 1740 | } |