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