OSDN Git Service

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