OSDN Git Service

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