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