OSDN Git Service

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