OSDN Git Service

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