blob: cfdee4a90d540612c4a5d6679ee89601fc4d7968 [file] [log] [blame]
Jesse Barnes731cd552008-12-17 10:09:49 -08001/*
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>
Kristian Høgsbergb0b96632009-09-11 13:27:35 -040049#include <unistd.h>
Jesse Barnes731cd552008-12-17 10:09:49 -080050#include <errno.h>
51
52#define U642VOID(x) ((void *)(unsigned long)(x))
53#define VOID2U64(x) ((uint64_t)(unsigned long)(x))
54
55/*
56 * Util functions
57 */
58
59void* drmAllocCpy(void *array, int count, int entry_size)
60{
61 char *r;
62 int i;
63
64 if (!count || !array || !entry_size)
65 return 0;
66
67 if (!(r = drmMalloc(count*entry_size)))
68 return 0;
69
70 for (i = 0; i < count; i++)
71 memcpy(r+(entry_size*i), array+(entry_size*i), entry_size);
72
73 return r;
74}
75
76/*
77 * A couple of free functions.
78 */
79
Jakob Bornecrantzeb78c532009-02-11 16:43:20 +010080void drmModeFreeModeInfo(drmModeModeInfoPtr ptr)
Jesse Barnes731cd552008-12-17 10:09:49 -080081{
82 if (!ptr)
83 return;
84
85 drmFree(ptr);
86}
87
88void drmModeFreeResources(drmModeResPtr ptr)
89{
90 if (!ptr)
91 return;
92
93 drmFree(ptr);
94
95}
96
97void drmModeFreeFB(drmModeFBPtr ptr)
98{
99 if (!ptr)
100 return;
101
102 /* we might add more frees later. */
103 drmFree(ptr);
104}
105
106void drmModeFreeCrtc(drmModeCrtcPtr ptr)
107{
108 if (!ptr)
109 return;
110
111 drmFree(ptr);
112
113}
114
115void drmModeFreeConnector(drmModeConnectorPtr ptr)
116{
117 if (!ptr)
118 return;
119
Keith Packard0a246542009-09-17 17:28:08 -0700120 drmFree(ptr->encoders);
121 drmFree(ptr->prop_values);
122 drmFree(ptr->props);
Jesse Barnes731cd552008-12-17 10:09:49 -0800123 drmFree(ptr->modes);
124 drmFree(ptr);
125
126}
127
128void drmModeFreeEncoder(drmModeEncoderPtr ptr)
129{
130 drmFree(ptr);
131}
132
133/*
134 * ModeSetting functions.
135 */
136
137drmModeResPtr drmModeGetResources(int fd)
138{
Chris Wilsond1308f42010-01-06 15:19:25 +0000139 struct drm_mode_card_res res, counts;
Jesse Barnes731cd552008-12-17 10:09:49 -0800140 drmModeResPtr r = 0;
141
Chris Wilsond1308f42010-01-06 15:19:25 +0000142retry:
Jesse Barnes731cd552008-12-17 10:09:49 -0800143 memset(&res, 0, sizeof(struct drm_mode_card_res));
Jesse Barnes731cd552008-12-17 10:09:49 -0800144 if (drmIoctl(fd, DRM_IOCTL_MODE_GETRESOURCES, &res))
145 return 0;
146
Chris Wilsond1308f42010-01-06 15:19:25 +0000147 counts = res;
148
Chris Wilson85fb3e52010-01-06 15:39:49 +0000149 if (res.count_fbs) {
Jesse Barnes731cd552008-12-17 10:09:49 -0800150 res.fb_id_ptr = VOID2U64(drmMalloc(res.count_fbs*sizeof(uint32_t)));
Chris Wilson85fb3e52010-01-06 15:39:49 +0000151 if (!res.fb_id_ptr)
152 goto err_allocs;
153 }
154 if (res.count_crtcs) {
Jesse Barnes731cd552008-12-17 10:09:49 -0800155 res.crtc_id_ptr = VOID2U64(drmMalloc(res.count_crtcs*sizeof(uint32_t)));
Chris Wilson85fb3e52010-01-06 15:39:49 +0000156 if (!res.crtc_id_ptr)
157 goto err_allocs;
158 }
159 if (res.count_connectors) {
Jesse Barnes731cd552008-12-17 10:09:49 -0800160 res.connector_id_ptr = VOID2U64(drmMalloc(res.count_connectors*sizeof(uint32_t)));
Chris Wilson85fb3e52010-01-06 15:39:49 +0000161 if (!res.connector_id_ptr)
162 goto err_allocs;
163 }
164 if (res.count_encoders) {
Jesse Barnes731cd552008-12-17 10:09:49 -0800165 res.encoder_id_ptr = VOID2U64(drmMalloc(res.count_encoders*sizeof(uint32_t)));
Chris Wilson85fb3e52010-01-06 15:39:49 +0000166 if (!res.encoder_id_ptr)
167 goto err_allocs;
168 }
Jesse Barnes731cd552008-12-17 10:09:49 -0800169
Chris Wilsond1308f42010-01-06 15:19:25 +0000170 if (drmIoctl(fd, DRM_IOCTL_MODE_GETRESOURCES, &res))
Jesse Barnes731cd552008-12-17 10:09:49 -0800171 goto err_allocs;
Chris Wilsond1308f42010-01-06 15:19:25 +0000172
173 /* The number of available connectors and etc may have changed with a
174 * hotplug event in between the ioctls, in which case the field is
175 * silently ignored by the kernel.
176 */
177 if (counts.count_fbs < res.count_fbs ||
178 counts.count_crtcs < res.count_crtcs ||
179 counts.count_connectors < res.count_connectors ||
180 counts.count_encoders < res.count_encoders)
181 {
182 drmFree(U642VOID(res.fb_id_ptr));
183 drmFree(U642VOID(res.crtc_id_ptr));
184 drmFree(U642VOID(res.connector_id_ptr));
185 drmFree(U642VOID(res.encoder_id_ptr));
186
187 goto retry;
Jesse Barnes731cd552008-12-17 10:09:49 -0800188 }
189
190 /*
191 * return
192 */
Jesse Barnes731cd552008-12-17 10:09:49 -0800193 if (!(r = drmMalloc(sizeof(*r))))
Chris Wilsond1308f42010-01-06 15:19:25 +0000194 goto err_allocs;
Jesse Barnes731cd552008-12-17 10:09:49 -0800195
196 r->min_width = res.min_width;
197 r->max_width = res.max_width;
198 r->min_height = res.min_height;
199 r->max_height = res.max_height;
200 r->count_fbs = res.count_fbs;
201 r->count_crtcs = res.count_crtcs;
202 r->count_connectors = res.count_connectors;
203 r->count_encoders = res.count_encoders;
Chris Wilson85fb3e52010-01-06 15:39:49 +0000204
205 r->fbs = drmAllocCpy(U642VOID(res.fb_id_ptr), res.count_fbs, sizeof(uint32_t));
206 r->crtcs = drmAllocCpy(U642VOID(res.crtc_id_ptr), res.count_crtcs, sizeof(uint32_t));
207 r->connectors = drmAllocCpy(U642VOID(res.connector_id_ptr), res.count_connectors, sizeof(uint32_t));
208 r->encoders = drmAllocCpy(U642VOID(res.encoder_id_ptr), res.count_encoders, sizeof(uint32_t));
Chris Wilsone6c136c2010-01-06 16:53:33 +0000209 if ((res.count_fbs && !r->fbs) ||
210 (res.count_crtcs && !r->crtcs) ||
211 (res.count_connectors && !r->connectors) ||
212 (res.count_encoders && !r->encoders))
213 {
Chris Wilson85fb3e52010-01-06 15:39:49 +0000214 drmFree(r->fbs);
215 drmFree(r->crtcs);
216 drmFree(r->connectors);
217 drmFree(r->encoders);
218 drmFree(r);
219 r = 0;
220 }
Jesse Barnes731cd552008-12-17 10:09:49 -0800221
222err_allocs:
223 drmFree(U642VOID(res.fb_id_ptr));
224 drmFree(U642VOID(res.crtc_id_ptr));
225 drmFree(U642VOID(res.connector_id_ptr));
226 drmFree(U642VOID(res.encoder_id_ptr));
227
228 return r;
229}
230
231int drmModeAddFB(int fd, uint32_t width, uint32_t height, uint8_t depth,
232 uint8_t bpp, uint32_t pitch, uint32_t bo_handle,
233 uint32_t *buf_id)
234{
235 struct drm_mode_fb_cmd f;
236 int ret;
237
238 f.width = width;
239 f.height = height;
240 f.pitch = pitch;
241 f.bpp = bpp;
242 f.depth = depth;
243 f.handle = bo_handle;
244
245 if ((ret = drmIoctl(fd, DRM_IOCTL_MODE_ADDFB, &f)))
246 return ret;
247
248 *buf_id = f.fb_id;
249 return 0;
250}
251
252int drmModeRmFB(int fd, uint32_t bufferId)
253{
254 return drmIoctl(fd, DRM_IOCTL_MODE_RMFB, &bufferId);
255
256
257}
258
259drmModeFBPtr drmModeGetFB(int fd, uint32_t buf)
260{
261 struct drm_mode_fb_cmd info;
262 drmModeFBPtr r;
263
264 info.fb_id = buf;
265
266 if (drmIoctl(fd, DRM_IOCTL_MODE_GETFB, &info))
267 return NULL;
268
269 if (!(r = drmMalloc(sizeof(*r))))
270 return NULL;
271
272 r->fb_id = info.fb_id;
273 r->width = info.width;
274 r->height = info.height;
275 r->pitch = info.pitch;
276 r->bpp = info.bpp;
277 r->handle = info.handle;
278 r->depth = info.depth;
279
280 return r;
281}
282
Jakob Bornecrantz3e486132009-11-24 18:00:12 +0100283int drmModeDirtyFB(int fd, uint32_t bufferId,
284 drmModeClipPtr clips, uint32_t num_clips)
285{
286 struct drm_mode_fb_dirty_cmd dirty = { 0 };
287
288 dirty.fb_id = bufferId;
289 dirty.clips_ptr = VOID2U64(clips);
290 dirty.num_clips = num_clips;
291
292 return drmIoctl(fd, DRM_IOCTL_MODE_DIRTYFB, &dirty);
293}
294
Jesse Barnes731cd552008-12-17 10:09:49 -0800295
296/*
297 * Crtc functions
298 */
299
300drmModeCrtcPtr drmModeGetCrtc(int fd, uint32_t crtcId)
301{
302 struct drm_mode_crtc crtc;
303 drmModeCrtcPtr r;
304
305 crtc.crtc_id = crtcId;
306
307 if (drmIoctl(fd, DRM_IOCTL_MODE_GETCRTC, &crtc))
308 return 0;
309
310 /*
311 * return
312 */
313
314 if (!(r = drmMalloc(sizeof(*r))))
315 return 0;
316
317 r->crtc_id = crtc.crtc_id;
318 r->x = crtc.x;
319 r->y = crtc.y;
320 r->mode_valid = crtc.mode_valid;
321 if (r->mode_valid)
322 memcpy(&r->mode, &crtc.mode, sizeof(struct drm_mode_modeinfo));
323 r->buffer_id = crtc.fb_id;
324 r->gamma_size = crtc.gamma_size;
325 return r;
326}
327
328
329int drmModeSetCrtc(int fd, uint32_t crtcId, uint32_t bufferId,
330 uint32_t x, uint32_t y, uint32_t *connectors, int count,
Jakob Bornecrantzeb78c532009-02-11 16:43:20 +0100331 drmModeModeInfoPtr mode)
Jesse Barnes731cd552008-12-17 10:09:49 -0800332{
333 struct drm_mode_crtc crtc;
334
335 crtc.x = x;
336 crtc.y = y;
337 crtc.crtc_id = crtcId;
338 crtc.fb_id = bufferId;
339 crtc.set_connectors_ptr = VOID2U64(connectors);
340 crtc.count_connectors = count;
341 if (mode) {
342 memcpy(&crtc.mode, mode, sizeof(struct drm_mode_modeinfo));
343 crtc.mode_valid = 1;
344 } else
345 crtc.mode_valid = 0;
346
347 return drmIoctl(fd, DRM_IOCTL_MODE_SETCRTC, &crtc);
348}
349
350/*
351 * Cursor manipulation
352 */
353
354int drmModeSetCursor(int fd, uint32_t crtcId, uint32_t bo_handle, uint32_t width, uint32_t height)
355{
356 struct drm_mode_cursor arg;
357
358 arg.flags = DRM_MODE_CURSOR_BO;
359 arg.crtc_id = crtcId;
360 arg.width = width;
361 arg.height = height;
362 arg.handle = bo_handle;
363
364 return drmIoctl(fd, DRM_IOCTL_MODE_CURSOR, &arg);
365}
366
367int drmModeMoveCursor(int fd, uint32_t crtcId, int x, int y)
368{
369 struct drm_mode_cursor arg;
370
371 arg.flags = DRM_MODE_CURSOR_MOVE;
372 arg.crtc_id = crtcId;
373 arg.x = x;
374 arg.y = y;
375
376 return drmIoctl(fd, DRM_IOCTL_MODE_CURSOR, &arg);
377}
378
379/*
380 * Encoder get
381 */
382drmModeEncoderPtr drmModeGetEncoder(int fd, uint32_t encoder_id)
383{
384 struct drm_mode_get_encoder enc;
385 drmModeEncoderPtr r = NULL;
386
387 enc.encoder_id = encoder_id;
388 enc.encoder_type = 0;
389 enc.possible_crtcs = 0;
390 enc.possible_clones = 0;
391
392 if (drmIoctl(fd, DRM_IOCTL_MODE_GETENCODER, &enc))
393 return 0;
394
395 if (!(r = drmMalloc(sizeof(*r))))
396 return 0;
397
398 r->encoder_id = enc.encoder_id;
399 r->crtc_id = enc.crtc_id;
400 r->encoder_type = enc.encoder_type;
401 r->possible_crtcs = enc.possible_crtcs;
402 r->possible_clones = enc.possible_clones;
403
404 return r;
405}
406
407/*
408 * Connector manipulation
409 */
410
411drmModeConnectorPtr drmModeGetConnector(int fd, uint32_t connector_id)
412{
413 struct drm_mode_get_connector conn;
414 drmModeConnectorPtr r = NULL;
415
416 conn.connector_id = connector_id;
417 conn.connector_type_id = 0;
418 conn.connector_type = 0;
Jesse Barnes3637dcc2009-04-10 15:24:10 -0700419 conn.count_modes = 0;
420 conn.modes_ptr = 0;
421 conn.count_props = 0;
422 conn.props_ptr = 0;
423 conn.prop_values_ptr = 0;
424 conn.count_encoders = 0;
425 conn.encoders_ptr = 0;
Jesse Barnes731cd552008-12-17 10:09:49 -0800426
427 if (drmIoctl(fd, DRM_IOCTL_MODE_GETCONNECTOR, &conn))
428 return 0;
429
430 if (conn.count_props) {
431 conn.props_ptr = VOID2U64(drmMalloc(conn.count_props*sizeof(uint32_t)));
432 conn.prop_values_ptr = VOID2U64(drmMalloc(conn.count_props*sizeof(uint64_t)));
433 }
434
435 if (conn.count_modes)
436 conn.modes_ptr = VOID2U64(drmMalloc(conn.count_modes*sizeof(struct drm_mode_modeinfo)));
437
438 if (conn.count_encoders)
439 conn.encoders_ptr = VOID2U64(drmMalloc(conn.count_encoders*sizeof(uint32_t)));
440
441 if (drmIoctl(fd, DRM_IOCTL_MODE_GETCONNECTOR, &conn))
442 goto err_allocs;
443
444 if(!(r = drmMalloc(sizeof(*r)))) {
445 goto err_allocs;
446 }
447
448 r->connector_id = conn.connector_id;
449 r->encoder_id = conn.encoder_id;
450 r->connection = conn.connection;
451 r->mmWidth = conn.mm_width;
452 r->mmHeight = conn.mm_height;
Dave Airlie412d3702009-04-22 20:25:40 +1000453 /* convert subpixel from kernel to userspace */
454 r->subpixel = conn.subpixel + 1;
Jesse Barnes731cd552008-12-17 10:09:49 -0800455 r->count_modes = conn.count_modes;
456 /* TODO we should test if these alloc & cpy fails. */
457 r->count_props = conn.count_props;
458 r->props = drmAllocCpy(U642VOID(conn.props_ptr), conn.count_props, sizeof(uint32_t));
459 r->prop_values = drmAllocCpy(U642VOID(conn.prop_values_ptr), conn.count_props, sizeof(uint64_t));
460 r->modes = drmAllocCpy(U642VOID(conn.modes_ptr), conn.count_modes, sizeof(struct drm_mode_modeinfo));
461 r->count_encoders = conn.count_encoders;
462 r->encoders = drmAllocCpy(U642VOID(conn.encoders_ptr), conn.count_encoders, sizeof(uint32_t));
463 r->connector_type = conn.connector_type;
464 r->connector_type_id = conn.connector_type_id;
465
466 if (!r->props || !r->prop_values || !r->modes || !r->encoders)
467 goto err_allocs;
468
469err_allocs:
470 drmFree(U642VOID(conn.prop_values_ptr));
471 drmFree(U642VOID(conn.props_ptr));
472 drmFree(U642VOID(conn.modes_ptr));
473 drmFree(U642VOID(conn.encoders_ptr));
474
475 return r;
476}
477
Jakob Bornecrantzeb78c532009-02-11 16:43:20 +0100478int drmModeAttachMode(int fd, uint32_t connector_id, drmModeModeInfoPtr mode_info)
Jesse Barnes731cd552008-12-17 10:09:49 -0800479{
480 struct drm_mode_mode_cmd res;
481
482 memcpy(&res.mode, mode_info, sizeof(struct drm_mode_modeinfo));
483 res.connector_id = connector_id;
484
485 return drmIoctl(fd, DRM_IOCTL_MODE_ATTACHMODE, &res);
486}
487
Jakob Bornecrantzeb78c532009-02-11 16:43:20 +0100488int drmModeDetachMode(int fd, uint32_t connector_id, drmModeModeInfoPtr mode_info)
Jesse Barnes731cd552008-12-17 10:09:49 -0800489{
490 struct drm_mode_mode_cmd res;
491
492 memcpy(&res.mode, mode_info, sizeof(struct drm_mode_modeinfo));
493 res.connector_id = connector_id;
494
495 return drmIoctl(fd, DRM_IOCTL_MODE_DETACHMODE, &res);
496}
497
498
499drmModePropertyPtr drmModeGetProperty(int fd, uint32_t property_id)
500{
501 struct drm_mode_get_property prop;
502 drmModePropertyPtr r;
503
504 prop.prop_id = property_id;
505 prop.count_enum_blobs = 0;
506 prop.count_values = 0;
507 prop.flags = 0;
508 prop.enum_blob_ptr = 0;
509 prop.values_ptr = 0;
510
511 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPERTY, &prop))
512 return 0;
513
514 if (prop.count_values)
515 prop.values_ptr = VOID2U64(drmMalloc(prop.count_values * sizeof(uint64_t)));
516
517 if (prop.count_enum_blobs && (prop.flags & DRM_MODE_PROP_ENUM))
518 prop.enum_blob_ptr = VOID2U64(drmMalloc(prop.count_enum_blobs * sizeof(struct drm_mode_property_enum)));
519
520 if (prop.count_enum_blobs && (prop.flags & DRM_MODE_PROP_BLOB)) {
521 prop.values_ptr = VOID2U64(drmMalloc(prop.count_enum_blobs * sizeof(uint32_t)));
522 prop.enum_blob_ptr = VOID2U64(drmMalloc(prop.count_enum_blobs * sizeof(uint32_t)));
523 }
524
525 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPERTY, &prop)) {
526 r = NULL;
527 goto err_allocs;
528 }
529
530 if (!(r = drmMalloc(sizeof(*r))))
531 return NULL;
532
533 r->prop_id = prop.prop_id;
534 r->count_values = prop.count_values;
535
536 r->flags = prop.flags;
537 if (prop.count_values)
538 r->values = drmAllocCpy(U642VOID(prop.values_ptr), prop.count_values, sizeof(uint64_t));
539 if (prop.flags & DRM_MODE_PROP_ENUM) {
540 r->count_enums = prop.count_enum_blobs;
541 r->enums = drmAllocCpy(U642VOID(prop.enum_blob_ptr), prop.count_enum_blobs, sizeof(struct drm_mode_property_enum));
542 } else if (prop.flags & DRM_MODE_PROP_BLOB) {
543 r->values = drmAllocCpy(U642VOID(prop.values_ptr), prop.count_enum_blobs, sizeof(uint32_t));
544 r->blob_ids = drmAllocCpy(U642VOID(prop.enum_blob_ptr), prop.count_enum_blobs, sizeof(uint32_t));
545 r->count_blobs = prop.count_enum_blobs;
546 }
547 strncpy(r->name, prop.name, DRM_PROP_NAME_LEN);
548 r->name[DRM_PROP_NAME_LEN-1] = 0;
549
550err_allocs:
551 drmFree(U642VOID(prop.values_ptr));
552 drmFree(U642VOID(prop.enum_blob_ptr));
553
554 return r;
555}
556
557void drmModeFreeProperty(drmModePropertyPtr ptr)
558{
559 if (!ptr)
560 return;
561
562 drmFree(ptr->values);
563 drmFree(ptr->enums);
564 drmFree(ptr);
565}
566
567drmModePropertyBlobPtr drmModeGetPropertyBlob(int fd, uint32_t blob_id)
568{
569 struct drm_mode_get_blob blob;
570 drmModePropertyBlobPtr r;
571
572 blob.length = 0;
573 blob.data = 0;
574 blob.blob_id = blob_id;
575
576 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPBLOB, &blob))
577 return NULL;
578
579 if (blob.length)
580 blob.data = VOID2U64(drmMalloc(blob.length));
581
582 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPBLOB, &blob)) {
583 r = NULL;
584 goto err_allocs;
585 }
586
587 if (!(r = drmMalloc(sizeof(*r))))
588 return NULL;
589
590 r->id = blob.blob_id;
591 r->length = blob.length;
592 r->data = drmAllocCpy(U642VOID(blob.data), 1, blob.length);
593
594err_allocs:
595 drmFree(U642VOID(blob.data));
596 return r;
597}
598
599void drmModeFreePropertyBlob(drmModePropertyBlobPtr ptr)
600{
601 if (!ptr)
602 return;
603
604 drmFree(ptr->data);
605 drmFree(ptr);
606}
607
608int drmModeConnectorSetProperty(int fd, uint32_t connector_id, uint32_t property_id,
609 uint64_t value)
610{
611 struct drm_mode_connector_set_property osp;
612 int ret;
613
614 osp.connector_id = connector_id;
615 osp.prop_id = property_id;
616 osp.value = value;
617
618 if ((ret = drmIoctl(fd, DRM_IOCTL_MODE_SETPROPERTY, &osp)))
619 return ret;
620
621 return 0;
622}
623
624/*
625 * checks if a modesetting capable driver has attached to the pci id
626 * returns 0 if modesetting supported.
627 * -EINVAL or invalid bus id
628 * -ENOSYS if no modesetting support
629*/
630int drmCheckModesettingSupported(const char *busid)
631{
632#ifdef __linux__
633 char pci_dev_dir[1024];
634 int domain, bus, dev, func;
635 DIR *sysdir;
636 struct dirent *dent;
637 int found = 0, ret;
638
639 ret = sscanf(busid, "pci:%04x:%02x:%02x.%d", &domain, &bus, &dev, &func);
640 if (ret != 4)
641 return -EINVAL;
642
643 sprintf(pci_dev_dir, "/sys/bus/pci/devices/%04x:%02x:%02x.%d/drm",
644 domain, bus, dev, func);
645
646 sysdir = opendir(pci_dev_dir);
647 if (sysdir) {
648 dent = readdir(sysdir);
649 while (dent) {
650 if (!strncmp(dent->d_name, "controlD", 8)) {
651 found = 1;
652 break;
653 }
654
655 dent = readdir(sysdir);
656 }
657 closedir(sysdir);
658 if (found)
659 return 0;
660 }
661
662 sprintf(pci_dev_dir, "/sys/bus/pci/devices/%04x:%02x:%02x.%d/",
663 domain, bus, dev, func);
664
665 sysdir = opendir(pci_dev_dir);
666 if (!sysdir)
667 return -EINVAL;
668
669 dent = readdir(sysdir);
670 while (dent) {
671 if (!strncmp(dent->d_name, "drm:controlD", 12)) {
672 found = 1;
673 break;
674 }
675
676 dent = readdir(sysdir);
677 }
678
679 closedir(sysdir);
680 if (found)
681 return 0;
682#endif
683 return -ENOSYS;
684
685}
686
Jesse Barnes731cd552008-12-17 10:09:49 -0800687int drmModeCrtcGetGamma(int fd, uint32_t crtc_id, uint32_t size,
688 uint16_t *red, uint16_t *green, uint16_t *blue)
689{
690 int ret;
691 struct drm_mode_crtc_lut l;
692
693 l.crtc_id = crtc_id;
694 l.gamma_size = size;
695 l.red = VOID2U64(red);
696 l.green = VOID2U64(green);
697 l.blue = VOID2U64(blue);
698
699 if ((ret = drmIoctl(fd, DRM_IOCTL_MODE_GETGAMMA, &l)))
700 return ret;
701
702 return 0;
703}
704
705int drmModeCrtcSetGamma(int fd, uint32_t crtc_id, uint32_t size,
706 uint16_t *red, uint16_t *green, uint16_t *blue)
707{
708 int ret;
709 struct drm_mode_crtc_lut l;
710
711 l.crtc_id = crtc_id;
712 l.gamma_size = size;
713 l.red = VOID2U64(red);
714 l.green = VOID2U64(green);
715 l.blue = VOID2U64(blue);
716
717 if ((ret = drmIoctl(fd, DRM_IOCTL_MODE_SETGAMMA, &l)))
718 return ret;
719
720 return 0;
721}
Kristian Høgsbergb0b96632009-09-11 13:27:35 -0400722
723int drmHandleEvent(int fd, drmEventContextPtr evctx)
724{
725 char buffer[1024];
726 int len, i;
727 struct drm_event *e;
728 struct drm_event_vblank *vblank;
729
730 /* The DRM read semantics guarantees that we always get only
731 * complete events. */
732
733 len = read(fd, buffer, sizeof buffer);
734 if (len == 0)
735 return 0;
736 if (len < sizeof *e)
737 return -1;
738
739 i = 0;
740 while (i < len) {
741 e = (struct drm_event *) &buffer[i];
742 switch (e->type) {
743 case DRM_EVENT_VBLANK:
744 if (evctx->version < 1 ||
745 evctx->vblank_handler == NULL)
746 break;
747 vblank = (struct drm_event_vblank *) e;
748 evctx->vblank_handler(fd,
749 vblank->sequence,
750 vblank->tv_sec,
751 vblank->tv_usec,
752 U642VOID (vblank->user_data));
753 break;
Kristian Høgsbergb80bcff2009-11-12 14:06:45 -0500754 case DRM_EVENT_FLIP_COMPLETE:
Jesse Barnes14f59582009-12-03 14:20:51 -0800755 if (evctx->version < 2 ||
Kristian Høgsbergb80bcff2009-11-12 14:06:45 -0500756 evctx->page_flip_handler == NULL)
757 break;
758 vblank = (struct drm_event_vblank *) e;
759 evctx->page_flip_handler(fd,
760 vblank->sequence,
761 vblank->tv_sec,
762 vblank->tv_usec,
763 U642VOID (vblank->user_data));
764 break;
Kristian Høgsbergb0b96632009-09-11 13:27:35 -0400765 default:
766 break;
767 }
768 i += e->length;
769 }
770
771 return 0;
772}
773
Kristian Høgsbergb80bcff2009-11-12 14:06:45 -0500774int drmModePageFlip(int fd, uint32_t crtc_id, uint32_t fb_id,
775 uint32_t flags, void *user_data)
776{
777 struct drm_mode_crtc_page_flip flip;
778
779 flip.fb_id = fb_id;
780 flip.crtc_id = crtc_id;
781 flip.user_data = VOID2U64(user_data);
782 flip.flags = flags;
783 flip.reserved = 0;
784
785 return drmIoctl(fd, DRM_IOCTL_MODE_PAGE_FLIP, &flip);
786}