OSDN Git Service

drm: add hotspot cursor interface support.
[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 #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>
51
52 #define U642VOID(x) ((void *)(unsigned long)(x))
53 #define VOID2U64(x) ((uint64_t)(unsigned long)(x))
54
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 }
60
61 /*
62  * Util functions
63  */
64
65 void* drmAllocCpy(void *array, int count, int entry_size)
66 {
67         char *r;
68         int i;
69
70         if (!count || !array || !entry_size)
71                 return 0;
72
73         if (!(r = drmMalloc(count*entry_size)))
74                 return 0;
75
76         for (i = 0; i < count; i++)
77                 memcpy(r+(entry_size*i), array+(entry_size*i), entry_size);
78
79         return r;
80 }
81
82 /*
83  * A couple of free functions.
84  */
85
86 void drmModeFreeModeInfo(drmModeModeInfoPtr ptr)
87 {
88         if (!ptr)
89                 return;
90
91         drmFree(ptr);
92 }
93
94 void drmModeFreeResources(drmModeResPtr ptr)
95 {
96         if (!ptr)
97                 return;
98
99         drmFree(ptr->fbs);
100         drmFree(ptr->crtcs);
101         drmFree(ptr->connectors);
102         drmFree(ptr->encoders);
103         drmFree(ptr);
104
105 }
106
107 void drmModeFreeFB(drmModeFBPtr ptr)
108 {
109         if (!ptr)
110                 return;
111
112         /* we might add more frees later. */
113         drmFree(ptr);
114 }
115
116 void drmModeFreeCrtc(drmModeCrtcPtr ptr)
117 {
118         if (!ptr)
119                 return;
120
121         drmFree(ptr);
122
123 }
124
125 void drmModeFreeConnector(drmModeConnectorPtr ptr)
126 {
127         if (!ptr)
128                 return;
129
130         drmFree(ptr->encoders);
131         drmFree(ptr->prop_values);
132         drmFree(ptr->props);
133         drmFree(ptr->modes);
134         drmFree(ptr);
135
136 }
137
138 void drmModeFreeEncoder(drmModeEncoderPtr ptr)
139 {
140         drmFree(ptr);
141 }
142
143 /*
144  * ModeSetting functions.
145  */
146
147 drmModeResPtr drmModeGetResources(int fd)
148 {
149         struct drm_mode_card_res res, counts;
150         drmModeResPtr r = 0;
151
152 retry:
153         memset(&res, 0, sizeof(struct drm_mode_card_res));
154         if (drmIoctl(fd, DRM_IOCTL_MODE_GETRESOURCES, &res))
155                 return 0;
156
157         counts = res;
158
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         }
179
180         if (drmIoctl(fd, DRM_IOCTL_MODE_GETRESOURCES, &res))
181                 goto err_allocs;
182
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));
196
197                 goto retry;
198         }
199
200         /*
201          * return
202          */
203         if (!(r = drmMalloc(sizeof(*r))))
204                 goto err_allocs;
205
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;
214
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         }
231
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));
237
238         return r;
239 }
240
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)
244 {
245         struct drm_mode_fb_cmd f;
246         int ret;
247
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;
254
255         if ((ret = DRM_IOCTL(fd, DRM_IOCTL_MODE_ADDFB, &f)))
256                 return ret;
257
258         *buf_id = f.fb_id;
259         return 0;
260 }
261
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)
266 {
267         struct drm_mode_fb_cmd2 f;
268         int ret;
269
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]));
277
278         if ((ret = DRM_IOCTL(fd, DRM_IOCTL_MODE_ADDFB2, &f)))
279                 return ret;
280
281         *buf_id = f.fb_id;
282         return 0;
283 }
284
285 int drmModeRmFB(int fd, uint32_t bufferId)
286 {
287         return DRM_IOCTL(fd, DRM_IOCTL_MODE_RMFB, &bufferId);
288
289
290 }
291
292 drmModeFBPtr drmModeGetFB(int fd, uint32_t buf)
293 {
294         struct drm_mode_fb_cmd info;
295         drmModeFBPtr r;
296
297         info.fb_id = buf;
298
299         if (drmIoctl(fd, DRM_IOCTL_MODE_GETFB, &info))
300                 return NULL;
301
302         if (!(r = drmMalloc(sizeof(*r))))
303                 return NULL;
304
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;
312
313         return r;
314 }
315
316 int drmModeDirtyFB(int fd, uint32_t bufferId,
317                    drmModeClipPtr clips, uint32_t num_clips)
318 {
319         struct drm_mode_fb_dirty_cmd dirty = { 0 };
320
321         dirty.fb_id = bufferId;
322         dirty.clips_ptr = VOID2U64(clips);
323         dirty.num_clips = num_clips;
324
325         return DRM_IOCTL(fd, DRM_IOCTL_MODE_DIRTYFB, &dirty);
326 }
327
328
329 /*
330  * Crtc functions
331  */
332
333 drmModeCrtcPtr drmModeGetCrtc(int fd, uint32_t crtcId)
334 {
335         struct drm_mode_crtc crtc;
336         drmModeCrtcPtr r;
337
338         crtc.crtc_id = crtcId;
339
340         if (drmIoctl(fd, DRM_IOCTL_MODE_GETCRTC, &crtc))
341                 return 0;
342
343         /*
344          * return
345          */
346
347         if (!(r = drmMalloc(sizeof(*r))))
348                 return 0;
349
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->width = crtc.mode.hdisplay;
357                 r->height = crtc.mode.vdisplay;
358         }
359         r->buffer_id       = crtc.fb_id;
360         r->gamma_size      = crtc.gamma_size;
361         return r;
362 }
363
364
365 int drmModeSetCrtc(int fd, uint32_t crtcId, uint32_t bufferId,
366                    uint32_t x, uint32_t y, uint32_t *connectors, int count,
367                    drmModeModeInfoPtr mode)
368 {
369         struct drm_mode_crtc crtc;
370
371         crtc.x             = x;
372         crtc.y             = y;
373         crtc.crtc_id       = crtcId;
374         crtc.fb_id         = bufferId;
375         crtc.set_connectors_ptr = VOID2U64(connectors);
376         crtc.count_connectors = count;
377         if (mode) {
378           memcpy(&crtc.mode, mode, sizeof(struct drm_mode_modeinfo));
379           crtc.mode_valid = 1;
380         } else
381           crtc.mode_valid = 0;
382
383         return DRM_IOCTL(fd, DRM_IOCTL_MODE_SETCRTC, &crtc);
384 }
385
386 /*
387  * Cursor manipulation
388  */
389
390 int drmModeSetCursor(int fd, uint32_t crtcId, uint32_t bo_handle, uint32_t width, uint32_t height)
391 {
392         struct drm_mode_cursor arg;
393
394         arg.flags = DRM_MODE_CURSOR_BO;
395         arg.crtc_id = crtcId;
396         arg.width = width;
397         arg.height = height;
398         arg.handle = bo_handle;
399
400         return DRM_IOCTL(fd, DRM_IOCTL_MODE_CURSOR, &arg);
401 }
402
403 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)
404 {
405         struct drm_mode_cursor2 arg;
406
407         arg.flags = DRM_MODE_CURSOR_BO;
408         arg.crtc_id = crtcId;
409         arg.width = width;
410         arg.height = height;
411         arg.handle = bo_handle;
412         arg.hot_x = hot_x;
413         arg.hot_y = hot_y;
414
415         return DRM_IOCTL(fd, DRM_IOCTL_MODE_CURSOR2, &arg);
416 }
417
418 int drmModeMoveCursor(int fd, uint32_t crtcId, int x, int y)
419 {
420         struct drm_mode_cursor arg;
421
422         arg.flags = DRM_MODE_CURSOR_MOVE;
423         arg.crtc_id = crtcId;
424         arg.x = x;
425         arg.y = y;
426
427         return DRM_IOCTL(fd, DRM_IOCTL_MODE_CURSOR, &arg);
428 }
429
430 /*
431  * Encoder get
432  */
433 drmModeEncoderPtr drmModeGetEncoder(int fd, uint32_t encoder_id)
434 {
435         struct drm_mode_get_encoder enc;
436         drmModeEncoderPtr r = NULL;
437
438         enc.encoder_id = encoder_id;
439         enc.encoder_type = 0;
440         enc.possible_crtcs = 0;
441         enc.possible_clones = 0;
442
443         if (drmIoctl(fd, DRM_IOCTL_MODE_GETENCODER, &enc))
444                 return 0;
445
446         if (!(r = drmMalloc(sizeof(*r))))
447                 return 0;
448
449         r->encoder_id = enc.encoder_id;
450         r->crtc_id = enc.crtc_id;
451         r->encoder_type = enc.encoder_type;
452         r->possible_crtcs = enc.possible_crtcs;
453         r->possible_clones = enc.possible_clones;
454
455         return r;
456 }
457
458 /*
459  * Connector manipulation
460  */
461
462 drmModeConnectorPtr drmModeGetConnector(int fd, uint32_t connector_id)
463 {
464         struct drm_mode_get_connector conn, counts;
465         drmModeConnectorPtr r = NULL;
466
467 retry:
468         memset(&conn, 0, sizeof(struct drm_mode_get_connector));
469         conn.connector_id = connector_id;
470
471         if (drmIoctl(fd, DRM_IOCTL_MODE_GETCONNECTOR, &conn))
472                 return 0;
473
474         counts = conn;
475
476         if (conn.count_props) {
477                 conn.props_ptr = VOID2U64(drmMalloc(conn.count_props*sizeof(uint32_t)));
478                 if (!conn.props_ptr)
479                         goto err_allocs;
480                 conn.prop_values_ptr = VOID2U64(drmMalloc(conn.count_props*sizeof(uint64_t)));
481                 if (!conn.prop_values_ptr)
482                         goto err_allocs;
483         }
484
485         if (conn.count_modes) {
486                 conn.modes_ptr = VOID2U64(drmMalloc(conn.count_modes*sizeof(struct drm_mode_modeinfo)));
487                 if (!conn.modes_ptr)
488                         goto err_allocs;
489         }
490
491         if (conn.count_encoders) {
492                 conn.encoders_ptr = VOID2U64(drmMalloc(conn.count_encoders*sizeof(uint32_t)));
493                 if (!conn.encoders_ptr)
494                         goto err_allocs;
495         }
496
497         if (drmIoctl(fd, DRM_IOCTL_MODE_GETCONNECTOR, &conn))
498                 goto err_allocs;
499
500         /* The number of available connectors and etc may have changed with a
501          * hotplug event in between the ioctls, in which case the field is
502          * silently ignored by the kernel.
503          */
504         if (counts.count_props < conn.count_props ||
505             counts.count_modes < conn.count_modes ||
506             counts.count_encoders < conn.count_encoders) {
507                 drmFree(U642VOID(conn.props_ptr));
508                 drmFree(U642VOID(conn.prop_values_ptr));
509                 drmFree(U642VOID(conn.modes_ptr));
510                 drmFree(U642VOID(conn.encoders_ptr));
511
512                 goto retry;
513         }
514
515         if(!(r = drmMalloc(sizeof(*r)))) {
516                 goto err_allocs;
517         }
518
519         r->connector_id = conn.connector_id;
520         r->encoder_id = conn.encoder_id;
521         r->connection   = conn.connection;
522         r->mmWidth      = conn.mm_width;
523         r->mmHeight     = conn.mm_height;
524         /* convert subpixel from kernel to userspace */
525         r->subpixel     = conn.subpixel + 1;
526         r->count_modes  = conn.count_modes;
527         r->count_props  = conn.count_props;
528         r->props        = drmAllocCpy(U642VOID(conn.props_ptr), conn.count_props, sizeof(uint32_t));
529         r->prop_values  = drmAllocCpy(U642VOID(conn.prop_values_ptr), conn.count_props, sizeof(uint64_t));
530         r->modes        = drmAllocCpy(U642VOID(conn.modes_ptr), conn.count_modes, sizeof(struct drm_mode_modeinfo));
531         r->count_encoders = conn.count_encoders;
532         r->encoders     = drmAllocCpy(U642VOID(conn.encoders_ptr), conn.count_encoders, sizeof(uint32_t));
533         r->connector_type  = conn.connector_type;
534         r->connector_type_id = conn.connector_type_id;
535
536         if ((r->count_props && !r->props) ||
537             (r->count_props && !r->prop_values) ||
538             (r->count_modes && !r->modes) ||
539             (r->count_encoders && !r->encoders)) {
540                 drmFree(r->props);
541                 drmFree(r->prop_values);
542                 drmFree(r->modes);
543                 drmFree(r->encoders);
544                 drmFree(r);
545                 r = 0;
546         }
547
548 err_allocs:
549         drmFree(U642VOID(conn.prop_values_ptr));
550         drmFree(U642VOID(conn.props_ptr));
551         drmFree(U642VOID(conn.modes_ptr));
552         drmFree(U642VOID(conn.encoders_ptr));
553
554         return r;
555 }
556
557 int drmModeAttachMode(int fd, uint32_t connector_id, drmModeModeInfoPtr mode_info)
558 {
559         struct drm_mode_mode_cmd res;
560
561         memcpy(&res.mode, mode_info, sizeof(struct drm_mode_modeinfo));
562         res.connector_id = connector_id;
563
564         return DRM_IOCTL(fd, DRM_IOCTL_MODE_ATTACHMODE, &res);
565 }
566
567 int drmModeDetachMode(int fd, uint32_t connector_id, drmModeModeInfoPtr mode_info)
568 {
569         struct drm_mode_mode_cmd res;
570
571         memcpy(&res.mode, mode_info, sizeof(struct drm_mode_modeinfo));
572         res.connector_id = connector_id;
573
574         return DRM_IOCTL(fd, DRM_IOCTL_MODE_DETACHMODE, &res);
575 }
576
577
578 drmModePropertyPtr drmModeGetProperty(int fd, uint32_t property_id)
579 {
580         struct drm_mode_get_property prop;
581         drmModePropertyPtr r;
582
583         prop.prop_id = property_id;
584         prop.count_enum_blobs = 0;
585         prop.count_values = 0;
586         prop.flags = 0;
587         prop.enum_blob_ptr = 0;
588         prop.values_ptr = 0;
589
590         if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPERTY, &prop))
591                 return 0;
592
593         if (prop.count_values)
594                 prop.values_ptr = VOID2U64(drmMalloc(prop.count_values * sizeof(uint64_t)));
595
596         if (prop.count_enum_blobs && (prop.flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)))
597                 prop.enum_blob_ptr = VOID2U64(drmMalloc(prop.count_enum_blobs * sizeof(struct drm_mode_property_enum)));
598
599         if (prop.count_enum_blobs && (prop.flags & DRM_MODE_PROP_BLOB)) {
600                 prop.values_ptr = VOID2U64(drmMalloc(prop.count_enum_blobs * sizeof(uint32_t)));
601                 prop.enum_blob_ptr = VOID2U64(drmMalloc(prop.count_enum_blobs * sizeof(uint32_t)));
602         }
603
604         if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPERTY, &prop)) {
605                 r = NULL;
606                 goto err_allocs;
607         }
608
609         if (!(r = drmMalloc(sizeof(*r))))
610                 return NULL;
611
612         r->prop_id = prop.prop_id;
613         r->count_values = prop.count_values;
614
615         r->flags = prop.flags;
616         if (prop.count_values)
617                 r->values = drmAllocCpy(U642VOID(prop.values_ptr), prop.count_values, sizeof(uint64_t));
618         if (prop.flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
619                 r->count_enums = prop.count_enum_blobs;
620                 r->enums = drmAllocCpy(U642VOID(prop.enum_blob_ptr), prop.count_enum_blobs, sizeof(struct drm_mode_property_enum));
621         } else if (prop.flags & DRM_MODE_PROP_BLOB) {
622                 r->values = drmAllocCpy(U642VOID(prop.values_ptr), prop.count_enum_blobs, sizeof(uint32_t));
623                 r->blob_ids = drmAllocCpy(U642VOID(prop.enum_blob_ptr), prop.count_enum_blobs, sizeof(uint32_t));
624                 r->count_blobs = prop.count_enum_blobs;
625         }
626         strncpy(r->name, prop.name, DRM_PROP_NAME_LEN);
627         r->name[DRM_PROP_NAME_LEN-1] = 0;
628
629 err_allocs:
630         drmFree(U642VOID(prop.values_ptr));
631         drmFree(U642VOID(prop.enum_blob_ptr));
632
633         return r;
634 }
635
636 void drmModeFreeProperty(drmModePropertyPtr ptr)
637 {
638         if (!ptr)
639                 return;
640
641         drmFree(ptr->values);
642         drmFree(ptr->enums);
643         drmFree(ptr);
644 }
645
646 drmModePropertyBlobPtr drmModeGetPropertyBlob(int fd, uint32_t blob_id)
647 {
648         struct drm_mode_get_blob blob;
649         drmModePropertyBlobPtr r;
650
651         blob.length = 0;
652         blob.data = 0;
653         blob.blob_id = blob_id;
654
655         if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPBLOB, &blob))
656                 return NULL;
657
658         if (blob.length)
659                 blob.data = VOID2U64(drmMalloc(blob.length));
660
661         if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPBLOB, &blob)) {
662                 r = NULL;
663                 goto err_allocs;
664         }
665
666         if (!(r = drmMalloc(sizeof(*r))))
667                 goto err_allocs;
668
669         r->id = blob.blob_id;
670         r->length = blob.length;
671         r->data = drmAllocCpy(U642VOID(blob.data), 1, blob.length);
672
673 err_allocs:
674         drmFree(U642VOID(blob.data));
675         return r;
676 }
677
678 void drmModeFreePropertyBlob(drmModePropertyBlobPtr ptr)
679 {
680         if (!ptr)
681                 return;
682
683         drmFree(ptr->data);
684         drmFree(ptr);
685 }
686
687 int drmModeConnectorSetProperty(int fd, uint32_t connector_id, uint32_t property_id,
688                              uint64_t value)
689 {
690         struct drm_mode_connector_set_property osp;
691
692         osp.connector_id = connector_id;
693         osp.prop_id = property_id;
694         osp.value = value;
695
696         return DRM_IOCTL(fd, DRM_IOCTL_MODE_SETPROPERTY, &osp);
697 }
698
699 /*
700  * checks if a modesetting capable driver has attached to the pci id
701  * returns 0 if modesetting supported.
702  *  -EINVAL or invalid bus id
703  *  -ENOSYS if no modesetting support
704 */
705 int drmCheckModesettingSupported(const char *busid)
706 {
707 #ifdef __linux__
708         char pci_dev_dir[1024];
709         int domain, bus, dev, func;
710         DIR *sysdir;
711         struct dirent *dent;
712         int found = 0, ret;
713
714         ret = sscanf(busid, "pci:%04x:%02x:%02x.%d", &domain, &bus, &dev, &func);
715         if (ret != 4)
716                 return -EINVAL;
717
718         sprintf(pci_dev_dir, "/sys/bus/pci/devices/%04x:%02x:%02x.%d/drm",
719                 domain, bus, dev, func);
720
721         sysdir = opendir(pci_dev_dir);
722         if (sysdir) {
723                 dent = readdir(sysdir);
724                 while (dent) {
725                         if (!strncmp(dent->d_name, "controlD", 8)) {
726                                 found = 1;
727                                 break;
728                         }
729
730                         dent = readdir(sysdir);
731                 }
732                 closedir(sysdir);
733                 if (found)
734                         return 0;
735         }
736
737         sprintf(pci_dev_dir, "/sys/bus/pci/devices/%04x:%02x:%02x.%d/",
738                 domain, bus, dev, func);
739
740         sysdir = opendir(pci_dev_dir);
741         if (!sysdir)
742                 return -EINVAL;
743
744         dent = readdir(sysdir);
745         while (dent) {
746                 if (!strncmp(dent->d_name, "drm:controlD", 12)) {
747                         found = 1;
748                         break;
749                 }
750
751                 dent = readdir(sysdir);
752         }
753
754         closedir(sysdir);
755         if (found)
756                 return 0;
757 #endif
758         return -ENOSYS;
759
760 }
761
762 int drmModeCrtcGetGamma(int fd, uint32_t crtc_id, uint32_t size,
763                         uint16_t *red, uint16_t *green, uint16_t *blue)
764 {
765         struct drm_mode_crtc_lut l;
766
767         l.crtc_id = crtc_id;
768         l.gamma_size = size;
769         l.red = VOID2U64(red);
770         l.green = VOID2U64(green);
771         l.blue = VOID2U64(blue);
772
773         return DRM_IOCTL(fd, DRM_IOCTL_MODE_GETGAMMA, &l);
774 }
775
776 int drmModeCrtcSetGamma(int fd, uint32_t crtc_id, uint32_t size,
777                         uint16_t *red, uint16_t *green, uint16_t *blue)
778 {
779         struct drm_mode_crtc_lut l;
780
781         l.crtc_id = crtc_id;
782         l.gamma_size = size;
783         l.red = VOID2U64(red);
784         l.green = VOID2U64(green);
785         l.blue = VOID2U64(blue);
786
787         return DRM_IOCTL(fd, DRM_IOCTL_MODE_SETGAMMA, &l);
788 }
789
790 int drmHandleEvent(int fd, drmEventContextPtr evctx)
791 {
792         char buffer[1024];
793         int len, i;
794         struct drm_event *e;
795         struct drm_event_vblank *vblank;
796         
797         /* The DRM read semantics guarantees that we always get only
798          * complete events. */
799
800         len = read(fd, buffer, sizeof buffer);
801         if (len == 0)
802                 return 0;
803         if (len < sizeof *e)
804                 return -1;
805
806         i = 0;
807         while (i < len) {
808                 e = (struct drm_event *) &buffer[i];
809                 switch (e->type) {
810                 case DRM_EVENT_VBLANK:
811                         if (evctx->version < 1 ||
812                             evctx->vblank_handler == NULL)
813                                 break;
814                         vblank = (struct drm_event_vblank *) e;
815                         evctx->vblank_handler(fd,
816                                               vblank->sequence, 
817                                               vblank->tv_sec,
818                                               vblank->tv_usec,
819                                               U642VOID (vblank->user_data));
820                         break;
821                 case DRM_EVENT_FLIP_COMPLETE:
822                         if (evctx->version < 2 ||
823                             evctx->page_flip_handler == NULL)
824                                 break;
825                         vblank = (struct drm_event_vblank *) e;
826                         evctx->page_flip_handler(fd,
827                                                  vblank->sequence,
828                                                  vblank->tv_sec,
829                                                  vblank->tv_usec,
830                                                  U642VOID (vblank->user_data));
831                         break;
832                 default:
833                         break;
834                 }
835                 i += e->length;
836         }
837
838         return 0;
839 }
840
841 int drmModePageFlip(int fd, uint32_t crtc_id, uint32_t fb_id,
842                     uint32_t flags, void *user_data)
843 {
844         struct drm_mode_crtc_page_flip flip;
845
846         flip.fb_id = fb_id;
847         flip.crtc_id = crtc_id;
848         flip.user_data = VOID2U64(user_data);
849         flip.flags = flags;
850         flip.reserved = 0;
851
852         return DRM_IOCTL(fd, DRM_IOCTL_MODE_PAGE_FLIP, &flip);
853 }
854
855 int drmModeSetPlane(int fd, uint32_t plane_id, uint32_t crtc_id,
856                     uint32_t fb_id, uint32_t flags,
857                     uint32_t crtc_x, uint32_t crtc_y,
858                     uint32_t crtc_w, uint32_t crtc_h,
859                     uint32_t src_x, uint32_t src_y,
860                     uint32_t src_w, uint32_t src_h)
861
862 {
863         struct drm_mode_set_plane s;
864
865         s.plane_id = plane_id;
866         s.crtc_id = crtc_id;
867         s.fb_id = fb_id;
868         s.flags = flags;
869         s.crtc_x = crtc_x;
870         s.crtc_y = crtc_y;
871         s.crtc_w = crtc_w;
872         s.crtc_h = crtc_h;
873         s.src_x = src_x;
874         s.src_y = src_y;
875         s.src_w = src_w;
876         s.src_h = src_h;
877
878         return DRM_IOCTL(fd, DRM_IOCTL_MODE_SETPLANE, &s);
879 }
880
881
882 drmModePlanePtr drmModeGetPlane(int fd, uint32_t plane_id)
883 {
884         struct drm_mode_get_plane ovr, counts;
885         drmModePlanePtr r = 0;
886
887 retry:
888         memset(&ovr, 0, sizeof(struct drm_mode_get_plane));
889         ovr.plane_id = plane_id;
890         if (drmIoctl(fd, DRM_IOCTL_MODE_GETPLANE, &ovr))
891                 return 0;
892
893         counts = ovr;
894
895         if (ovr.count_format_types) {
896                 ovr.format_type_ptr = VOID2U64(drmMalloc(ovr.count_format_types *
897                                                          sizeof(uint32_t)));
898                 if (!ovr.format_type_ptr)
899                         goto err_allocs;
900         }
901
902         if (drmIoctl(fd, DRM_IOCTL_MODE_GETPLANE, &ovr))
903                 goto err_allocs;
904
905         if (counts.count_format_types < ovr.count_format_types) {
906                 drmFree(U642VOID(ovr.format_type_ptr));
907                 goto retry;
908         }
909
910         if (!(r = drmMalloc(sizeof(*r))))
911                 goto err_allocs;
912
913         r->count_formats = ovr.count_format_types;
914         r->plane_id = ovr.plane_id;
915         r->crtc_id = ovr.crtc_id;
916         r->fb_id = ovr.fb_id;
917         r->possible_crtcs = ovr.possible_crtcs;
918         r->gamma_size = ovr.gamma_size;
919         r->formats = drmAllocCpy(U642VOID(ovr.format_type_ptr),
920                                  ovr.count_format_types, sizeof(uint32_t));
921         if (ovr.count_format_types && !r->formats) {
922                 drmFree(r->formats);
923                 drmFree(r);
924                 r = 0;
925         }
926
927 err_allocs:
928         drmFree(U642VOID(ovr.format_type_ptr));
929
930         return r;
931 }
932
933 void drmModeFreePlane(drmModePlanePtr ptr)
934 {
935         if (!ptr)
936                 return;
937
938         drmFree(ptr->formats);
939         drmFree(ptr);
940 }
941
942 drmModePlaneResPtr drmModeGetPlaneResources(int fd)
943 {
944         struct drm_mode_get_plane_res res, counts;
945         drmModePlaneResPtr r = 0;
946
947 retry:
948         memset(&res, 0, sizeof(struct drm_mode_get_plane_res));
949         if (drmIoctl(fd, DRM_IOCTL_MODE_GETPLANERESOURCES, &res))
950                 return 0;
951
952         counts = res;
953
954         if (res.count_planes) {
955                 res.plane_id_ptr = VOID2U64(drmMalloc(res.count_planes *
956                                                         sizeof(uint32_t)));
957                 if (!res.plane_id_ptr)
958                         goto err_allocs;
959         }
960
961         if (drmIoctl(fd, DRM_IOCTL_MODE_GETPLANERESOURCES, &res))
962                 goto err_allocs;
963
964         if (counts.count_planes < res.count_planes) {
965                 drmFree(U642VOID(res.plane_id_ptr));
966                 goto retry;
967         }
968
969         if (!(r = drmMalloc(sizeof(*r))))
970                 goto err_allocs;
971
972         r->count_planes = res.count_planes;
973         r->planes = drmAllocCpy(U642VOID(res.plane_id_ptr),
974                                   res.count_planes, sizeof(uint32_t));
975         if (res.count_planes && !r->planes) {
976                 drmFree(r->planes);
977                 drmFree(r);
978                 r = 0;
979         }
980
981 err_allocs:
982         drmFree(U642VOID(res.plane_id_ptr));
983
984         return r;
985 }
986
987 void drmModeFreePlaneResources(drmModePlaneResPtr ptr)
988 {
989         if (!ptr)
990                 return;
991
992         drmFree(ptr->planes);
993         drmFree(ptr);
994 }
995
996 drmModeObjectPropertiesPtr drmModeObjectGetProperties(int fd,
997                                                       uint32_t object_id,
998                                                       uint32_t object_type)
999 {
1000         struct drm_mode_obj_get_properties properties;
1001         drmModeObjectPropertiesPtr ret = NULL;
1002         uint32_t count;
1003
1004 retry:
1005         memset(&properties, 0, sizeof(struct drm_mode_obj_get_properties));
1006         properties.obj_id = object_id;
1007         properties.obj_type = object_type;
1008
1009         if (drmIoctl(fd, DRM_IOCTL_MODE_OBJ_GETPROPERTIES, &properties))
1010                 return 0;
1011
1012         count = properties.count_props;
1013
1014         if (count) {
1015                 properties.props_ptr = VOID2U64(drmMalloc(count *
1016                                                           sizeof(uint32_t)));
1017                 if (!properties.props_ptr)
1018                         goto err_allocs;
1019                 properties.prop_values_ptr = VOID2U64(drmMalloc(count *
1020                                                       sizeof(uint64_t)));
1021                 if (!properties.prop_values_ptr)
1022                         goto err_allocs;
1023         }
1024
1025         if (drmIoctl(fd, DRM_IOCTL_MODE_OBJ_GETPROPERTIES, &properties))
1026                 goto err_allocs;
1027
1028         if (count < properties.count_props) {
1029                 drmFree(U642VOID(properties.props_ptr));
1030                 drmFree(U642VOID(properties.prop_values_ptr));
1031                 goto retry;
1032         }
1033         count = properties.count_props;
1034
1035         ret = drmMalloc(sizeof(*ret));
1036         if (!ret)
1037                 goto err_allocs;
1038
1039         ret->count_props = count;
1040         ret->props = drmAllocCpy(U642VOID(properties.props_ptr),
1041                                  count, sizeof(uint32_t));
1042         ret->prop_values = drmAllocCpy(U642VOID(properties.prop_values_ptr),
1043                                        count, sizeof(uint64_t));
1044         if (ret->count_props && (!ret->props || !ret->prop_values)) {
1045                 drmFree(ret->props);
1046                 drmFree(ret->prop_values);
1047                 drmFree(ret);
1048                 ret = NULL;
1049         }
1050
1051 err_allocs:
1052         drmFree(U642VOID(properties.props_ptr));
1053         drmFree(U642VOID(properties.prop_values_ptr));
1054         return ret;
1055 }
1056
1057 void drmModeFreeObjectProperties(drmModeObjectPropertiesPtr ptr)
1058 {
1059         if (!ptr)
1060                 return;
1061         drmFree(ptr->props);
1062         drmFree(ptr->prop_values);
1063         drmFree(ptr);
1064 }
1065
1066 int drmModeObjectSetProperty(int fd, uint32_t object_id, uint32_t object_type,
1067                              uint32_t property_id, uint64_t value)
1068 {
1069         struct drm_mode_obj_set_property prop;
1070
1071         prop.value = value;
1072         prop.prop_id = property_id;
1073         prop.obj_id = object_id;
1074         prop.obj_type = object_type;
1075
1076         return DRM_IOCTL(fd, DRM_IOCTL_MODE_OBJ_SETPROPERTY, &prop);
1077 }