]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - glsdk/libdrm.git/blob - xf86drmMode.c
radeon: simplify ZS buffer checking on r600
[glsdk/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  */
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  */
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>
44 #include "xf86drmMode.h"
45 #include "xf86drm.h"
46 #include <drm.h>
47 #include <string.h>
48 #include <dirent.h>
49 #include <unistd.h>
50 #include <errno.h>
52 #define U642VOID(x) ((void *)(unsigned long)(x))
53 #define VOID2U64(x) ((uint64_t)(unsigned long)(x))
55 static inline int DRM_IOCTL(int fd, unsigned long cmd, void *arg)
56 {
57         int ret = drmIoctl(fd, cmd, arg);
58         return ret < 0 ? -errno : ret;
59 }
61 /*
62  * Util functions
63  */
65 void* drmAllocCpy(void *array, int count, int entry_size)
66 {
67         char *r;
68         int i;
70         if (!count || !array || !entry_size)
71                 return 0;
73         if (!(r = drmMalloc(count*entry_size)))
74                 return 0;
76         for (i = 0; i < count; i++)
77                 memcpy(r+(entry_size*i), array+(entry_size*i), entry_size);
79         return r;
80 }
82 /*
83  * A couple of free functions.
84  */
86 void drmModeFreeModeInfo(drmModeModeInfoPtr ptr)
87 {
88         if (!ptr)
89                 return;
91         drmFree(ptr);
92 }
94 void drmModeFreeResources(drmModeResPtr ptr)
95 {
96         if (!ptr)
97                 return;
99         drmFree(ptr->fbs);
100         drmFree(ptr->crtcs);
101         drmFree(ptr->connectors);
102         drmFree(ptr->encoders);
103         drmFree(ptr);
107 void drmModeFreeFB(drmModeFBPtr ptr)
109         if (!ptr)
110                 return;
112         /* we might add more frees later. */
113         drmFree(ptr);
116 void drmModeFreeCrtc(drmModeCrtcPtr ptr)
118         if (!ptr)
119                 return;
121         drmFree(ptr);
125 void drmModeFreeConnector(drmModeConnectorPtr ptr)
127         if (!ptr)
128                 return;
130         drmFree(ptr->encoders);
131         drmFree(ptr->prop_values);
132         drmFree(ptr->props);
133         drmFree(ptr->modes);
134         drmFree(ptr);
138 void drmModeFreeEncoder(drmModeEncoderPtr ptr)
140         drmFree(ptr);
143 /*
144  * ModeSetting functions.
145  */
147 drmModeResPtr drmModeGetResources(int fd)
149         struct drm_mode_card_res res, counts;
150         drmModeResPtr r = 0;
152 retry:
153         memset(&res, 0, sizeof(struct drm_mode_card_res));
154         if (drmIoctl(fd, DRM_IOCTL_MODE_GETRESOURCES, &res))
155                 return 0;
157         counts = res;
159         if (res.count_fbs) {
160                 res.fb_id_ptr = VOID2U64(drmMalloc(res.count_fbs*sizeof(uint32_t)));
161                 if (!res.fb_id_ptr)
162                         goto err_allocs;
163         }
164         if (res.count_crtcs) {
165                 res.crtc_id_ptr = VOID2U64(drmMalloc(res.count_crtcs*sizeof(uint32_t)));
166                 if (!res.crtc_id_ptr)
167                         goto err_allocs;
168         }
169         if (res.count_connectors) {
170                 res.connector_id_ptr = VOID2U64(drmMalloc(res.count_connectors*sizeof(uint32_t)));
171                 if (!res.connector_id_ptr)
172                         goto err_allocs;
173         }
174         if (res.count_encoders) {
175                 res.encoder_id_ptr = VOID2U64(drmMalloc(res.count_encoders*sizeof(uint32_t)));
176                 if (!res.encoder_id_ptr)
177                         goto err_allocs;
178         }
180         if (drmIoctl(fd, DRM_IOCTL_MODE_GETRESOURCES, &res))
181                 goto err_allocs;
183         /* The number of available connectors and etc may have changed with a
184          * hotplug event in between the ioctls, in which case the field is
185          * silently ignored by the kernel.
186          */
187         if (counts.count_fbs < res.count_fbs ||
188             counts.count_crtcs < res.count_crtcs ||
189             counts.count_connectors < res.count_connectors ||
190             counts.count_encoders < res.count_encoders)
191         {
192                 drmFree(U642VOID(res.fb_id_ptr));
193                 drmFree(U642VOID(res.crtc_id_ptr));
194                 drmFree(U642VOID(res.connector_id_ptr));
195                 drmFree(U642VOID(res.encoder_id_ptr));
197                 goto retry;
198         }
200         /*
201          * return
202          */
203         if (!(r = drmMalloc(sizeof(*r))))
204                 goto err_allocs;
206         r->min_width     = res.min_width;
207         r->max_width     = res.max_width;
208         r->min_height    = res.min_height;
209         r->max_height    = res.max_height;
210         r->count_fbs     = res.count_fbs;
211         r->count_crtcs   = res.count_crtcs;
212         r->count_connectors = res.count_connectors;
213         r->count_encoders = res.count_encoders;
215         r->fbs        = drmAllocCpy(U642VOID(res.fb_id_ptr), res.count_fbs, sizeof(uint32_t));
216         r->crtcs      = drmAllocCpy(U642VOID(res.crtc_id_ptr), res.count_crtcs, sizeof(uint32_t));
217         r->connectors = drmAllocCpy(U642VOID(res.connector_id_ptr), res.count_connectors, sizeof(uint32_t));
218         r->encoders   = drmAllocCpy(U642VOID(res.encoder_id_ptr), res.count_encoders, sizeof(uint32_t));
219         if ((res.count_fbs && !r->fbs) ||
220             (res.count_crtcs && !r->crtcs) ||
221             (res.count_connectors && !r->connectors) ||
222             (res.count_encoders && !r->encoders))
223         {
224                 drmFree(r->fbs);
225                 drmFree(r->crtcs);
226                 drmFree(r->connectors);
227                 drmFree(r->encoders);
228                 drmFree(r);
229                 r = 0;
230         }
232 err_allocs:
233         drmFree(U642VOID(res.fb_id_ptr));
234         drmFree(U642VOID(res.crtc_id_ptr));
235         drmFree(U642VOID(res.connector_id_ptr));
236         drmFree(U642VOID(res.encoder_id_ptr));
238         return r;
241 int drmModeAddFB(int fd, uint32_t width, uint32_t height, uint8_t depth,
242                  uint8_t bpp, uint32_t pitch, uint32_t bo_handle,
243                  uint32_t *buf_id)
245         struct drm_mode_fb_cmd f;
246         int ret;
248         f.width  = width;
249         f.height = height;
250         f.pitch  = pitch;
251         f.bpp    = bpp;
252         f.depth  = depth;
253         f.handle = bo_handle;
255         if ((ret = DRM_IOCTL(fd, DRM_IOCTL_MODE_ADDFB, &f)))
256                 return ret;
258         *buf_id = f.fb_id;
259         return 0;
262 int drmModeAddFB2(int fd, uint32_t width, uint32_t height,
263                   uint32_t pixel_format, uint32_t bo_handles[4],
264                   uint32_t pitches[4], uint32_t offsets[4],
265                   uint32_t *buf_id, uint32_t flags)
267         struct drm_mode_fb_cmd2 f;
268         int ret;
270         f.width  = width;
271         f.height = height;
272         f.pixel_format = pixel_format;
273         f.flags = flags;
274         memcpy(f.handles, bo_handles, 4 * sizeof(bo_handles[0]));
275         memcpy(f.pitches, pitches, 4 * sizeof(pitches[0]));
276         memcpy(f.offsets, offsets, 4 * sizeof(offsets[0]));
278         if ((ret = DRM_IOCTL(fd, DRM_IOCTL_MODE_ADDFB2, &f)))
279                 return ret;
281         *buf_id = f.fb_id;
282         return 0;
285 int drmModeRmFB(int fd, uint32_t bufferId)
287         return DRM_IOCTL(fd, DRM_IOCTL_MODE_RMFB, &bufferId);
292 drmModeFBPtr drmModeGetFB(int fd, uint32_t buf)
294         struct drm_mode_fb_cmd info;
295         drmModeFBPtr r;
297         info.fb_id = buf;
299         if (drmIoctl(fd, DRM_IOCTL_MODE_GETFB, &info))
300                 return NULL;
302         if (!(r = drmMalloc(sizeof(*r))))
303                 return NULL;
305         r->fb_id = info.fb_id;
306         r->width = info.width;
307         r->height = info.height;
308         r->pitch = info.pitch;
309         r->bpp = info.bpp;
310         r->handle = info.handle;
311         r->depth = info.depth;
313         return r;
316 int drmModeDirtyFB(int fd, uint32_t bufferId,
317                    drmModeClipPtr clips, uint32_t num_clips)
319         struct drm_mode_fb_dirty_cmd dirty = { 0 };
321         dirty.fb_id = bufferId;
322         dirty.clips_ptr = VOID2U64(clips);
323         dirty.num_clips = num_clips;
325         return DRM_IOCTL(fd, DRM_IOCTL_MODE_DIRTYFB, &dirty);
329 /*
330  * Crtc functions
331  */
333 drmModeCrtcPtr drmModeGetCrtc(int fd, uint32_t crtcId)
335         struct drm_mode_crtc crtc;
336         drmModeCrtcPtr r;
338         crtc.crtc_id = crtcId;
340         if (drmIoctl(fd, DRM_IOCTL_MODE_GETCRTC, &crtc))
341                 return 0;
343         /*
344          * return
345          */
347         if (!(r = drmMalloc(sizeof(*r))))
348                 return 0;
350         r->crtc_id         = crtc.crtc_id;
351         r->x               = crtc.x;
352         r->y               = crtc.y;
353         r->mode_valid      = crtc.mode_valid;
354         if (r->mode_valid)
355                 memcpy(&r->mode, &crtc.mode, sizeof(struct drm_mode_modeinfo));
356         r->buffer_id       = crtc.fb_id;
357         r->gamma_size      = crtc.gamma_size;
358         return r;
362 int drmModeSetCrtc(int fd, uint32_t crtcId, uint32_t bufferId,
363                    uint32_t x, uint32_t y, uint32_t *connectors, int count,
364                    drmModeModeInfoPtr mode)
366         struct drm_mode_crtc crtc;
368         crtc.x             = x;
369         crtc.y             = y;
370         crtc.crtc_id       = crtcId;
371         crtc.fb_id         = bufferId;
372         crtc.set_connectors_ptr = VOID2U64(connectors);
373         crtc.count_connectors = count;
374         if (mode) {
375           memcpy(&crtc.mode, mode, sizeof(struct drm_mode_modeinfo));
376           crtc.mode_valid = 1;
377         } else
378           crtc.mode_valid = 0;
380         return DRM_IOCTL(fd, DRM_IOCTL_MODE_SETCRTC, &crtc);
383 /*
384  * Cursor manipulation
385  */
387 int drmModeSetCursor(int fd, uint32_t crtcId, uint32_t bo_handle, uint32_t width, uint32_t height)
389         struct drm_mode_cursor arg;
391         arg.flags = DRM_MODE_CURSOR_BO;
392         arg.crtc_id = crtcId;
393         arg.width = width;
394         arg.height = height;
395         arg.handle = bo_handle;
397         return DRM_IOCTL(fd, DRM_IOCTL_MODE_CURSOR, &arg);
400 int drmModeMoveCursor(int fd, uint32_t crtcId, int x, int y)
402         struct drm_mode_cursor arg;
404         arg.flags = DRM_MODE_CURSOR_MOVE;
405         arg.crtc_id = crtcId;
406         arg.x = x;
407         arg.y = y;
409         return DRM_IOCTL(fd, DRM_IOCTL_MODE_CURSOR, &arg);
412 /*
413  * Encoder get
414  */
415 drmModeEncoderPtr drmModeGetEncoder(int fd, uint32_t encoder_id)
417         struct drm_mode_get_encoder enc;
418         drmModeEncoderPtr r = NULL;
420         enc.encoder_id = encoder_id;
421         enc.encoder_type = 0;
422         enc.possible_crtcs = 0;
423         enc.possible_clones = 0;
425         if (drmIoctl(fd, DRM_IOCTL_MODE_GETENCODER, &enc))
426                 return 0;
428         if (!(r = drmMalloc(sizeof(*r))))
429                 return 0;
431         r->encoder_id = enc.encoder_id;
432         r->crtc_id = enc.crtc_id;
433         r->encoder_type = enc.encoder_type;
434         r->possible_crtcs = enc.possible_crtcs;
435         r->possible_clones = enc.possible_clones;
437         return r;
440 /*
441  * Connector manipulation
442  */
444 drmModeConnectorPtr drmModeGetConnector(int fd, uint32_t connector_id)
446         struct drm_mode_get_connector conn, counts;
447         drmModeConnectorPtr r = NULL;
449 retry:
450         memset(&conn, 0, sizeof(struct drm_mode_get_connector));
451         conn.connector_id = connector_id;
453         if (drmIoctl(fd, DRM_IOCTL_MODE_GETCONNECTOR, &conn))
454                 return 0;
456         counts = conn;
458         if (conn.count_props) {
459                 conn.props_ptr = VOID2U64(drmMalloc(conn.count_props*sizeof(uint32_t)));
460                 if (!conn.props_ptr)
461                         goto err_allocs;
462                 conn.prop_values_ptr = VOID2U64(drmMalloc(conn.count_props*sizeof(uint64_t)));
463                 if (!conn.prop_values_ptr)
464                         goto err_allocs;
465         }
467         if (conn.count_modes) {
468                 conn.modes_ptr = VOID2U64(drmMalloc(conn.count_modes*sizeof(struct drm_mode_modeinfo)));
469                 if (!conn.modes_ptr)
470                         goto err_allocs;
471         }
473         if (conn.count_encoders) {
474                 conn.encoders_ptr = VOID2U64(drmMalloc(conn.count_encoders*sizeof(uint32_t)));
475                 if (!conn.encoders_ptr)
476                         goto err_allocs;
477         }
479         if (drmIoctl(fd, DRM_IOCTL_MODE_GETCONNECTOR, &conn))
480                 goto err_allocs;
482         /* The number of available connectors and etc may have changed with a
483          * hotplug event in between the ioctls, in which case the field is
484          * silently ignored by the kernel.
485          */
486         if (counts.count_props < conn.count_props ||
487             counts.count_modes < conn.count_modes ||
488             counts.count_encoders < conn.count_encoders) {
489                 drmFree(U642VOID(conn.props_ptr));
490                 drmFree(U642VOID(conn.prop_values_ptr));
491                 drmFree(U642VOID(conn.modes_ptr));
492                 drmFree(U642VOID(conn.encoders_ptr));
494                 goto retry;
495         }
497         if(!(r = drmMalloc(sizeof(*r)))) {
498                 goto err_allocs;
499         }
501         r->connector_id = conn.connector_id;
502         r->encoder_id = conn.encoder_id;
503         r->connection   = conn.connection;
504         r->mmWidth      = conn.mm_width;
505         r->mmHeight     = conn.mm_height;
506         /* convert subpixel from kernel to userspace */
507         r->subpixel     = conn.subpixel + 1;
508         r->count_modes  = conn.count_modes;
509         r->count_props  = conn.count_props;
510         r->props        = drmAllocCpy(U642VOID(conn.props_ptr), conn.count_props, sizeof(uint32_t));
511         r->prop_values  = drmAllocCpy(U642VOID(conn.prop_values_ptr), conn.count_props, sizeof(uint64_t));
512         r->modes        = drmAllocCpy(U642VOID(conn.modes_ptr), conn.count_modes, sizeof(struct drm_mode_modeinfo));
513         r->count_encoders = conn.count_encoders;
514         r->encoders     = drmAllocCpy(U642VOID(conn.encoders_ptr), conn.count_encoders, sizeof(uint32_t));
515         r->connector_type  = conn.connector_type;
516         r->connector_type_id = conn.connector_type_id;
518         if ((r->count_props && !r->props) ||
519             (r->count_props && !r->prop_values) ||
520             (r->count_modes && !r->modes) ||
521             (r->count_encoders && !r->encoders)) {
522                 drmFree(r->props);
523                 drmFree(r->prop_values);
524                 drmFree(r->modes);
525                 drmFree(r->encoders);
526                 drmFree(r);
527                 r = 0;
528         }
530 err_allocs:
531         drmFree(U642VOID(conn.prop_values_ptr));
532         drmFree(U642VOID(conn.props_ptr));
533         drmFree(U642VOID(conn.modes_ptr));
534         drmFree(U642VOID(conn.encoders_ptr));
536         return r;
539 int drmModeAttachMode(int fd, uint32_t connector_id, drmModeModeInfoPtr mode_info)
541         struct drm_mode_mode_cmd res;
543         memcpy(&res.mode, mode_info, sizeof(struct drm_mode_modeinfo));
544         res.connector_id = connector_id;
546         return DRM_IOCTL(fd, DRM_IOCTL_MODE_ATTACHMODE, &res);
549 int drmModeDetachMode(int fd, uint32_t connector_id, drmModeModeInfoPtr mode_info)
551         struct drm_mode_mode_cmd res;
553         memcpy(&res.mode, mode_info, sizeof(struct drm_mode_modeinfo));
554         res.connector_id = connector_id;
556         return DRM_IOCTL(fd, DRM_IOCTL_MODE_DETACHMODE, &res);
560 drmModePropertyPtr drmModeGetProperty(int fd, uint32_t property_id)
562         struct drm_mode_get_property prop;
563         drmModePropertyPtr r;
565         prop.prop_id = property_id;
566         prop.count_enum_blobs = 0;
567         prop.count_values = 0;
568         prop.flags = 0;
569         prop.enum_blob_ptr = 0;
570         prop.values_ptr = 0;
572         if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPERTY, &prop))
573                 return 0;
575         if (prop.count_values)
576                 prop.values_ptr = VOID2U64(drmMalloc(prop.count_values * sizeof(uint64_t)));
578         if (prop.count_enum_blobs && (prop.flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)))
579                 prop.enum_blob_ptr = VOID2U64(drmMalloc(prop.count_enum_blobs * sizeof(struct drm_mode_property_enum)));
581         if (prop.count_enum_blobs && (prop.flags & DRM_MODE_PROP_BLOB)) {
582                 prop.values_ptr = VOID2U64(drmMalloc(prop.count_enum_blobs * sizeof(uint32_t)));
583                 prop.enum_blob_ptr = VOID2U64(drmMalloc(prop.count_enum_blobs * sizeof(uint32_t)));
584         }
586         if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPERTY, &prop)) {
587                 r = NULL;
588                 goto err_allocs;
589         }
591         if (!(r = drmMalloc(sizeof(*r))))
592                 return NULL;
594         r->prop_id = prop.prop_id;
595         r->count_values = prop.count_values;
597         r->flags = prop.flags;
598         if (prop.count_values)
599                 r->values = drmAllocCpy(U642VOID(prop.values_ptr), prop.count_values, sizeof(uint64_t));
600         if (prop.flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
601                 r->count_enums = prop.count_enum_blobs;
602                 r->enums = drmAllocCpy(U642VOID(prop.enum_blob_ptr), prop.count_enum_blobs, sizeof(struct drm_mode_property_enum));
603         } else if (prop.flags & DRM_MODE_PROP_BLOB) {
604                 r->values = drmAllocCpy(U642VOID(prop.values_ptr), prop.count_enum_blobs, sizeof(uint32_t));
605                 r->blob_ids = drmAllocCpy(U642VOID(prop.enum_blob_ptr), prop.count_enum_blobs, sizeof(uint32_t));
606                 r->count_blobs = prop.count_enum_blobs;
607         }
608         strncpy(r->name, prop.name, DRM_PROP_NAME_LEN);
609         r->name[DRM_PROP_NAME_LEN-1] = 0;
611 err_allocs:
612         drmFree(U642VOID(prop.values_ptr));
613         drmFree(U642VOID(prop.enum_blob_ptr));
615         return r;
618 void drmModeFreeProperty(drmModePropertyPtr ptr)
620         if (!ptr)
621                 return;
623         drmFree(ptr->values);
624         drmFree(ptr->enums);
625         drmFree(ptr);
628 drmModePropertyBlobPtr drmModeGetPropertyBlob(int fd, uint32_t blob_id)
630         struct drm_mode_get_blob blob;
631         drmModePropertyBlobPtr r;
633         blob.length = 0;
634         blob.data = 0;
635         blob.blob_id = blob_id;
637         if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPBLOB, &blob))
638                 return NULL;
640         if (blob.length)
641                 blob.data = VOID2U64(drmMalloc(blob.length));
643         if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPBLOB, &blob)) {
644                 r = NULL;
645                 goto err_allocs;
646         }
648         if (!(r = drmMalloc(sizeof(*r))))
649                 goto err_allocs;
651         r->id = blob.blob_id;
652         r->length = blob.length;
653         r->data = drmAllocCpy(U642VOID(blob.data), 1, blob.length);
655 err_allocs:
656         drmFree(U642VOID(blob.data));
657         return r;
660 void drmModeFreePropertyBlob(drmModePropertyBlobPtr ptr)
662         if (!ptr)
663                 return;
665         drmFree(ptr->data);
666         drmFree(ptr);
669 int drmModeConnectorSetProperty(int fd, uint32_t connector_id, uint32_t property_id,
670                              uint64_t value)
672         struct drm_mode_connector_set_property osp;
674         osp.connector_id = connector_id;
675         osp.prop_id = property_id;
676         osp.value = value;
678         return DRM_IOCTL(fd, DRM_IOCTL_MODE_SETPROPERTY, &osp);
681 /*
682  * checks if a modesetting capable driver has attached to the pci id
683  * returns 0 if modesetting supported.
684  *  -EINVAL or invalid bus id
685  *  -ENOSYS if no modesetting support
686 */
687 int drmCheckModesettingSupported(const char *busid)
689 #ifdef __linux__
690         char pci_dev_dir[1024];
691         int domain, bus, dev, func;
692         DIR *sysdir;
693         struct dirent *dent;
694         int found = 0, ret;
696         ret = sscanf(busid, "pci:%04x:%02x:%02x.%d", &domain, &bus, &dev, &func);
697         if (ret != 4)
698                 return -EINVAL;
700         sprintf(pci_dev_dir, "/sys/bus/pci/devices/%04x:%02x:%02x.%d/drm",
701                 domain, bus, dev, func);
703         sysdir = opendir(pci_dev_dir);
704         if (sysdir) {
705                 dent = readdir(sysdir);
706                 while (dent) {
707                         if (!strncmp(dent->d_name, "controlD", 8)) {
708                                 found = 1;
709                                 break;
710                         }
712                         dent = readdir(sysdir);
713                 }
714                 closedir(sysdir);
715                 if (found)
716                         return 0;
717         }
719         sprintf(pci_dev_dir, "/sys/bus/pci/devices/%04x:%02x:%02x.%d/",
720                 domain, bus, dev, func);
722         sysdir = opendir(pci_dev_dir);
723         if (!sysdir)
724                 return -EINVAL;
726         dent = readdir(sysdir);
727         while (dent) {
728                 if (!strncmp(dent->d_name, "drm:controlD", 12)) {
729                         found = 1;
730                         break;
731                 }
733                 dent = readdir(sysdir);
734         }
736         closedir(sysdir);
737         if (found)
738                 return 0;
739 #endif
740         return -ENOSYS;
744 int drmModeCrtcGetGamma(int fd, uint32_t crtc_id, uint32_t size,
745                         uint16_t *red, uint16_t *green, uint16_t *blue)
747         struct drm_mode_crtc_lut l;
749         l.crtc_id = crtc_id;
750         l.gamma_size = size;
751         l.red = VOID2U64(red);
752         l.green = VOID2U64(green);
753         l.blue = VOID2U64(blue);
755         return DRM_IOCTL(fd, DRM_IOCTL_MODE_GETGAMMA, &l);
758 int drmModeCrtcSetGamma(int fd, uint32_t crtc_id, uint32_t size,
759                         uint16_t *red, uint16_t *green, uint16_t *blue)
761         struct drm_mode_crtc_lut l;
763         l.crtc_id = crtc_id;
764         l.gamma_size = size;
765         l.red = VOID2U64(red);
766         l.green = VOID2U64(green);
767         l.blue = VOID2U64(blue);
769         return DRM_IOCTL(fd, DRM_IOCTL_MODE_SETGAMMA, &l);
772 int drmHandleEvent(int fd, drmEventContextPtr evctx)
774         char buffer[1024];
775         int len, i;
776         struct drm_event *e;
777         struct drm_event_vblank *vblank;
778         
779         /* The DRM read semantics guarantees that we always get only
780          * complete events. */
782         len = read(fd, buffer, sizeof buffer);
783         if (len == 0)
784                 return 0;
785         if (len < sizeof *e)
786                 return -1;
788         i = 0;
789         while (i < len) {
790                 e = (struct drm_event *) &buffer[i];
791                 switch (e->type) {
792                 case DRM_EVENT_VBLANK:
793                         if (evctx->version < 1 ||
794                             evctx->vblank_handler == NULL)
795                                 break;
796                         vblank = (struct drm_event_vblank *) e;
797                         evctx->vblank_handler(fd,
798                                               vblank->sequence, 
799                                               vblank->tv_sec,
800                                               vblank->tv_usec,
801                                               U642VOID (vblank->user_data));
802                         break;
803                 case DRM_EVENT_FLIP_COMPLETE:
804                         if (evctx->version < 2 ||
805                             evctx->page_flip_handler == NULL)
806                                 break;
807                         vblank = (struct drm_event_vblank *) e;
808                         evctx->page_flip_handler(fd,
809                                                  vblank->sequence,
810                                                  vblank->tv_sec,
811                                                  vblank->tv_usec,
812                                                  U642VOID (vblank->user_data));
813                         break;
814                 default:
815                         break;
816                 }
817                 i += e->length;
818         }
820         return 0;
823 int drmModePageFlip(int fd, uint32_t crtc_id, uint32_t fb_id,
824                     uint32_t flags, void *user_data)
826         struct drm_mode_crtc_page_flip flip;
828         flip.fb_id = fb_id;
829         flip.crtc_id = crtc_id;
830         flip.user_data = VOID2U64(user_data);
831         flip.flags = flags;
832         flip.reserved = 0;
834         return DRM_IOCTL(fd, DRM_IOCTL_MODE_PAGE_FLIP, &flip);
837 int drmModeSetPlane(int fd, uint32_t plane_id, uint32_t crtc_id,
838                     uint32_t fb_id, uint32_t flags,
839                     uint32_t crtc_x, uint32_t crtc_y,
840                     uint32_t crtc_w, uint32_t crtc_h,
841                     uint32_t src_x, uint32_t src_y,
842                     uint32_t src_w, uint32_t src_h)
845         struct drm_mode_set_plane s;
847         s.plane_id = plane_id;
848         s.crtc_id = crtc_id;
849         s.fb_id = fb_id;
850         s.flags = flags;
851         s.crtc_x = crtc_x;
852         s.crtc_y = crtc_y;
853         s.crtc_w = crtc_w;
854         s.crtc_h = crtc_h;
855         s.src_x = src_x;
856         s.src_y = src_y;
857         s.src_w = src_w;
858         s.src_h = src_h;
860         return DRM_IOCTL(fd, DRM_IOCTL_MODE_SETPLANE, &s);
864 drmModePlanePtr drmModeGetPlane(int fd, uint32_t plane_id)
866         struct drm_mode_get_plane ovr, counts;
867         drmModePlanePtr r = 0;
869 retry:
870         memset(&ovr, 0, sizeof(struct drm_mode_get_plane));
871         ovr.plane_id = plane_id;
872         if (drmIoctl(fd, DRM_IOCTL_MODE_GETPLANE, &ovr))
873                 return 0;
875         counts = ovr;
877         if (ovr.count_format_types) {
878                 ovr.format_type_ptr = VOID2U64(drmMalloc(ovr.count_format_types *
879                                                          sizeof(uint32_t)));
880                 if (!ovr.format_type_ptr)
881                         goto err_allocs;
882         }
884         if (drmIoctl(fd, DRM_IOCTL_MODE_GETPLANE, &ovr))
885                 goto err_allocs;
887         if (counts.count_format_types < ovr.count_format_types) {
888                 drmFree(U642VOID(ovr.format_type_ptr));
889                 goto retry;
890         }
892         if (!(r = drmMalloc(sizeof(*r))))
893                 goto err_allocs;
895         r->count_formats = ovr.count_format_types;
896         r->plane_id = ovr.plane_id;
897         r->crtc_id = ovr.crtc_id;
898         r->fb_id = ovr.fb_id;
899         r->possible_crtcs = ovr.possible_crtcs;
900         r->gamma_size = ovr.gamma_size;
901         r->formats = drmAllocCpy(U642VOID(ovr.format_type_ptr),
902                                  ovr.count_format_types, sizeof(uint32_t));
903         if (ovr.count_format_types && !r->formats) {
904                 drmFree(r->formats);
905                 drmFree(r);
906                 r = 0;
907         }
909 err_allocs:
910         drmFree(U642VOID(ovr.format_type_ptr));
912         return r;
915 void drmModeFreePlane(drmModePlanePtr ptr)
917         if (!ptr)
918                 return;
920         drmFree(ptr->formats);
921         drmFree(ptr);
924 drmModePlaneResPtr drmModeGetPlaneResources(int fd)
926         struct drm_mode_get_plane_res res, counts;
927         drmModePlaneResPtr r = 0;
929 retry:
930         memset(&res, 0, sizeof(struct drm_mode_get_plane_res));
931         if (drmIoctl(fd, DRM_IOCTL_MODE_GETPLANERESOURCES, &res))
932                 return 0;
934         counts = res;
936         if (res.count_planes) {
937                 res.plane_id_ptr = VOID2U64(drmMalloc(res.count_planes *
938                                                         sizeof(uint32_t)));
939                 if (!res.plane_id_ptr)
940                         goto err_allocs;
941         }
943         if (drmIoctl(fd, DRM_IOCTL_MODE_GETPLANERESOURCES, &res))
944                 goto err_allocs;
946         if (counts.count_planes < res.count_planes) {
947                 drmFree(U642VOID(res.plane_id_ptr));
948                 goto retry;
949         }
951         if (!(r = drmMalloc(sizeof(*r))))
952                 goto err_allocs;
954         r->count_planes = res.count_planes;
955         r->planes = drmAllocCpy(U642VOID(res.plane_id_ptr),
956                                   res.count_planes, sizeof(uint32_t));
957         if (res.count_planes && !r->planes) {
958                 drmFree(r->planes);
959                 drmFree(r);
960                 r = 0;
961         }
963 err_allocs:
964         drmFree(U642VOID(res.plane_id_ptr));
966         return r;
969 void drmModeFreePlaneResources(drmModePlaneResPtr ptr)
971         if (!ptr)
972                 return;
974         drmFree(ptr->planes);
975         drmFree(ptr);
978 drmModeObjectPropertiesPtr drmModeObjectGetProperties(int fd,
979                                                       uint32_t object_id,
980                                                       uint32_t object_type)
982         struct drm_mode_obj_get_properties properties;
983         drmModeObjectPropertiesPtr ret = NULL;
984         uint32_t count;
986 retry:
987         memset(&properties, 0, sizeof(struct drm_mode_obj_get_properties));
988         properties.obj_id = object_id;
989         properties.obj_type = object_type;
991         if (drmIoctl(fd, DRM_IOCTL_MODE_OBJ_GETPROPERTIES, &properties))
992                 return 0;
994         count = properties.count_props;
996         if (count) {
997                 properties.props_ptr = VOID2U64(drmMalloc(count *
998                                                           sizeof(uint32_t)));
999                 if (!properties.props_ptr)
1000                         goto err_allocs;
1001                 properties.prop_values_ptr = VOID2U64(drmMalloc(count *
1002                                                       sizeof(uint64_t)));
1003                 if (!properties.prop_values_ptr)
1004                         goto err_allocs;
1005         }
1007         if (drmIoctl(fd, DRM_IOCTL_MODE_OBJ_GETPROPERTIES, &properties))
1008                 goto err_allocs;
1010         if (count < properties.count_props) {
1011                 drmFree(U642VOID(properties.props_ptr));
1012                 drmFree(U642VOID(properties.prop_values_ptr));
1013                 goto retry;
1014         }
1015         count = properties.count_props;
1017         ret = drmMalloc(sizeof(*ret));
1018         if (!ret)
1019                 goto err_allocs;
1021         ret->count_props = count;
1022         ret->props = drmAllocCpy(U642VOID(properties.props_ptr),
1023                                  count, sizeof(uint32_t));
1024         ret->prop_values = drmAllocCpy(U642VOID(properties.prop_values_ptr),
1025                                        count, sizeof(uint64_t));
1026         if (ret->count_props && (!ret->props || !ret->prop_values)) {
1027                 drmFree(ret->props);
1028                 drmFree(ret->prop_values);
1029                 drmFree(ret);
1030                 ret = NULL;
1031         }
1033 err_allocs:
1034         drmFree(U642VOID(properties.props_ptr));
1035         drmFree(U642VOID(properties.prop_values_ptr));
1036         return ret;
1039 void drmModeFreeObjectProperties(drmModeObjectPropertiesPtr ptr)
1041         if (!ptr)
1042                 return;
1043         drmFree(ptr->props);
1044         drmFree(ptr->prop_values);
1045         drmFree(ptr);
1048 int drmModeObjectSetProperty(int fd, uint32_t object_id, uint32_t object_type,
1049                              uint32_t property_id, uint64_t value)
1051         struct drm_mode_obj_set_property prop;
1053         prop.value = value;
1054         prop.prop_id = property_id;
1055         prop.obj_id = object_id;
1056         prop.obj_type = object_type;
1058         return DRM_IOCTL(fd, DRM_IOCTL_MODE_OBJ_SETPROPERTY, &prop);