OSDN Git Service

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