blob: 7edc625ad7fbb073ef3c3a6763014f62423aadc8 [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
Jesse Barnes731cd552008-12-17 10:09:49 -0800149 if (res.count_fbs)
150 res.fb_id_ptr = VOID2U64(drmMalloc(res.count_fbs*sizeof(uint32_t)));
151 if (res.count_crtcs)
152 res.crtc_id_ptr = VOID2U64(drmMalloc(res.count_crtcs*sizeof(uint32_t)));
153 if (res.count_connectors)
154 res.connector_id_ptr = VOID2U64(drmMalloc(res.count_connectors*sizeof(uint32_t)));
155 if (res.count_encoders)
156 res.encoder_id_ptr = VOID2U64(drmMalloc(res.count_encoders*sizeof(uint32_t)));
157
Chris Wilsond1308f42010-01-06 15:19:25 +0000158 if (drmIoctl(fd, DRM_IOCTL_MODE_GETRESOURCES, &res))
Jesse Barnes731cd552008-12-17 10:09:49 -0800159 goto err_allocs;
Chris Wilsond1308f42010-01-06 15:19:25 +0000160
161 /* The number of available connectors and etc may have changed with a
162 * hotplug event in between the ioctls, in which case the field is
163 * silently ignored by the kernel.
164 */
165 if (counts.count_fbs < res.count_fbs ||
166 counts.count_crtcs < res.count_crtcs ||
167 counts.count_connectors < res.count_connectors ||
168 counts.count_encoders < res.count_encoders)
169 {
170 drmFree(U642VOID(res.fb_id_ptr));
171 drmFree(U642VOID(res.crtc_id_ptr));
172 drmFree(U642VOID(res.connector_id_ptr));
173 drmFree(U642VOID(res.encoder_id_ptr));
174
175 goto retry;
Jesse Barnes731cd552008-12-17 10:09:49 -0800176 }
177
178 /*
179 * return
180 */
Jesse Barnes731cd552008-12-17 10:09:49 -0800181 if (!(r = drmMalloc(sizeof(*r))))
Chris Wilsond1308f42010-01-06 15:19:25 +0000182 goto err_allocs;
Jesse Barnes731cd552008-12-17 10:09:49 -0800183
184 r->min_width = res.min_width;
185 r->max_width = res.max_width;
186 r->min_height = res.min_height;
187 r->max_height = res.max_height;
188 r->count_fbs = res.count_fbs;
189 r->count_crtcs = res.count_crtcs;
190 r->count_connectors = res.count_connectors;
191 r->count_encoders = res.count_encoders;
192 /* TODO we realy should test if these allocs fails. */
193 r->fbs = drmAllocCpy(U642VOID(res.fb_id_ptr), res.count_fbs, sizeof(uint32_t));
194 r->crtcs = drmAllocCpy(U642VOID(res.crtc_id_ptr), res.count_crtcs, sizeof(uint32_t));
195 r->connectors = drmAllocCpy(U642VOID(res.connector_id_ptr), res.count_connectors, sizeof(uint32_t));
196 r->encoders = drmAllocCpy(U642VOID(res.encoder_id_ptr), res.count_encoders, sizeof(uint32_t));
197
198err_allocs:
199 drmFree(U642VOID(res.fb_id_ptr));
200 drmFree(U642VOID(res.crtc_id_ptr));
201 drmFree(U642VOID(res.connector_id_ptr));
202 drmFree(U642VOID(res.encoder_id_ptr));
203
204 return r;
205}
206
207int drmModeAddFB(int fd, uint32_t width, uint32_t height, uint8_t depth,
208 uint8_t bpp, uint32_t pitch, uint32_t bo_handle,
209 uint32_t *buf_id)
210{
211 struct drm_mode_fb_cmd f;
212 int ret;
213
214 f.width = width;
215 f.height = height;
216 f.pitch = pitch;
217 f.bpp = bpp;
218 f.depth = depth;
219 f.handle = bo_handle;
220
221 if ((ret = drmIoctl(fd, DRM_IOCTL_MODE_ADDFB, &f)))
222 return ret;
223
224 *buf_id = f.fb_id;
225 return 0;
226}
227
228int drmModeRmFB(int fd, uint32_t bufferId)
229{
230 return drmIoctl(fd, DRM_IOCTL_MODE_RMFB, &bufferId);
231
232
233}
234
235drmModeFBPtr drmModeGetFB(int fd, uint32_t buf)
236{
237 struct drm_mode_fb_cmd info;
238 drmModeFBPtr r;
239
240 info.fb_id = buf;
241
242 if (drmIoctl(fd, DRM_IOCTL_MODE_GETFB, &info))
243 return NULL;
244
245 if (!(r = drmMalloc(sizeof(*r))))
246 return NULL;
247
248 r->fb_id = info.fb_id;
249 r->width = info.width;
250 r->height = info.height;
251 r->pitch = info.pitch;
252 r->bpp = info.bpp;
253 r->handle = info.handle;
254 r->depth = info.depth;
255
256 return r;
257}
258
Jakob Bornecrantz3e486132009-11-24 18:00:12 +0100259int drmModeDirtyFB(int fd, uint32_t bufferId,
260 drmModeClipPtr clips, uint32_t num_clips)
261{
262 struct drm_mode_fb_dirty_cmd dirty = { 0 };
263
264 dirty.fb_id = bufferId;
265 dirty.clips_ptr = VOID2U64(clips);
266 dirty.num_clips = num_clips;
267
268 return drmIoctl(fd, DRM_IOCTL_MODE_DIRTYFB, &dirty);
269}
270
Jesse Barnes731cd552008-12-17 10:09:49 -0800271
272/*
273 * Crtc functions
274 */
275
276drmModeCrtcPtr drmModeGetCrtc(int fd, uint32_t crtcId)
277{
278 struct drm_mode_crtc crtc;
279 drmModeCrtcPtr r;
280
281 crtc.crtc_id = crtcId;
282
283 if (drmIoctl(fd, DRM_IOCTL_MODE_GETCRTC, &crtc))
284 return 0;
285
286 /*
287 * return
288 */
289
290 if (!(r = drmMalloc(sizeof(*r))))
291 return 0;
292
293 r->crtc_id = crtc.crtc_id;
294 r->x = crtc.x;
295 r->y = crtc.y;
296 r->mode_valid = crtc.mode_valid;
297 if (r->mode_valid)
298 memcpy(&r->mode, &crtc.mode, sizeof(struct drm_mode_modeinfo));
299 r->buffer_id = crtc.fb_id;
300 r->gamma_size = crtc.gamma_size;
301 return r;
302}
303
304
305int drmModeSetCrtc(int fd, uint32_t crtcId, uint32_t bufferId,
306 uint32_t x, uint32_t y, uint32_t *connectors, int count,
Jakob Bornecrantzeb78c532009-02-11 16:43:20 +0100307 drmModeModeInfoPtr mode)
Jesse Barnes731cd552008-12-17 10:09:49 -0800308{
309 struct drm_mode_crtc crtc;
310
311 crtc.x = x;
312 crtc.y = y;
313 crtc.crtc_id = crtcId;
314 crtc.fb_id = bufferId;
315 crtc.set_connectors_ptr = VOID2U64(connectors);
316 crtc.count_connectors = count;
317 if (mode) {
318 memcpy(&crtc.mode, mode, sizeof(struct drm_mode_modeinfo));
319 crtc.mode_valid = 1;
320 } else
321 crtc.mode_valid = 0;
322
323 return drmIoctl(fd, DRM_IOCTL_MODE_SETCRTC, &crtc);
324}
325
326/*
327 * Cursor manipulation
328 */
329
330int drmModeSetCursor(int fd, uint32_t crtcId, uint32_t bo_handle, uint32_t width, uint32_t height)
331{
332 struct drm_mode_cursor arg;
333
334 arg.flags = DRM_MODE_CURSOR_BO;
335 arg.crtc_id = crtcId;
336 arg.width = width;
337 arg.height = height;
338 arg.handle = bo_handle;
339
340 return drmIoctl(fd, DRM_IOCTL_MODE_CURSOR, &arg);
341}
342
343int drmModeMoveCursor(int fd, uint32_t crtcId, int x, int y)
344{
345 struct drm_mode_cursor arg;
346
347 arg.flags = DRM_MODE_CURSOR_MOVE;
348 arg.crtc_id = crtcId;
349 arg.x = x;
350 arg.y = y;
351
352 return drmIoctl(fd, DRM_IOCTL_MODE_CURSOR, &arg);
353}
354
355/*
356 * Encoder get
357 */
358drmModeEncoderPtr drmModeGetEncoder(int fd, uint32_t encoder_id)
359{
360 struct drm_mode_get_encoder enc;
361 drmModeEncoderPtr r = NULL;
362
363 enc.encoder_id = encoder_id;
364 enc.encoder_type = 0;
365 enc.possible_crtcs = 0;
366 enc.possible_clones = 0;
367
368 if (drmIoctl(fd, DRM_IOCTL_MODE_GETENCODER, &enc))
369 return 0;
370
371 if (!(r = drmMalloc(sizeof(*r))))
372 return 0;
373
374 r->encoder_id = enc.encoder_id;
375 r->crtc_id = enc.crtc_id;
376 r->encoder_type = enc.encoder_type;
377 r->possible_crtcs = enc.possible_crtcs;
378 r->possible_clones = enc.possible_clones;
379
380 return r;
381}
382
383/*
384 * Connector manipulation
385 */
386
387drmModeConnectorPtr drmModeGetConnector(int fd, uint32_t connector_id)
388{
389 struct drm_mode_get_connector conn;
390 drmModeConnectorPtr r = NULL;
391
392 conn.connector_id = connector_id;
393 conn.connector_type_id = 0;
394 conn.connector_type = 0;
Jesse Barnes3637dcc2009-04-10 15:24:10 -0700395 conn.count_modes = 0;
396 conn.modes_ptr = 0;
397 conn.count_props = 0;
398 conn.props_ptr = 0;
399 conn.prop_values_ptr = 0;
400 conn.count_encoders = 0;
401 conn.encoders_ptr = 0;
Jesse Barnes731cd552008-12-17 10:09:49 -0800402
403 if (drmIoctl(fd, DRM_IOCTL_MODE_GETCONNECTOR, &conn))
404 return 0;
405
406 if (conn.count_props) {
407 conn.props_ptr = VOID2U64(drmMalloc(conn.count_props*sizeof(uint32_t)));
408 conn.prop_values_ptr = VOID2U64(drmMalloc(conn.count_props*sizeof(uint64_t)));
409 }
410
411 if (conn.count_modes)
412 conn.modes_ptr = VOID2U64(drmMalloc(conn.count_modes*sizeof(struct drm_mode_modeinfo)));
413
414 if (conn.count_encoders)
415 conn.encoders_ptr = VOID2U64(drmMalloc(conn.count_encoders*sizeof(uint32_t)));
416
417 if (drmIoctl(fd, DRM_IOCTL_MODE_GETCONNECTOR, &conn))
418 goto err_allocs;
419
420 if(!(r = drmMalloc(sizeof(*r)))) {
421 goto err_allocs;
422 }
423
424 r->connector_id = conn.connector_id;
425 r->encoder_id = conn.encoder_id;
426 r->connection = conn.connection;
427 r->mmWidth = conn.mm_width;
428 r->mmHeight = conn.mm_height;
Dave Airlie412d3702009-04-22 20:25:40 +1000429 /* convert subpixel from kernel to userspace */
430 r->subpixel = conn.subpixel + 1;
Jesse Barnes731cd552008-12-17 10:09:49 -0800431 r->count_modes = conn.count_modes;
432 /* TODO we should test if these alloc & cpy fails. */
433 r->count_props = conn.count_props;
434 r->props = drmAllocCpy(U642VOID(conn.props_ptr), conn.count_props, sizeof(uint32_t));
435 r->prop_values = drmAllocCpy(U642VOID(conn.prop_values_ptr), conn.count_props, sizeof(uint64_t));
436 r->modes = drmAllocCpy(U642VOID(conn.modes_ptr), conn.count_modes, sizeof(struct drm_mode_modeinfo));
437 r->count_encoders = conn.count_encoders;
438 r->encoders = drmAllocCpy(U642VOID(conn.encoders_ptr), conn.count_encoders, sizeof(uint32_t));
439 r->connector_type = conn.connector_type;
440 r->connector_type_id = conn.connector_type_id;
441
442 if (!r->props || !r->prop_values || !r->modes || !r->encoders)
443 goto err_allocs;
444
445err_allocs:
446 drmFree(U642VOID(conn.prop_values_ptr));
447 drmFree(U642VOID(conn.props_ptr));
448 drmFree(U642VOID(conn.modes_ptr));
449 drmFree(U642VOID(conn.encoders_ptr));
450
451 return r;
452}
453
Jakob Bornecrantzeb78c532009-02-11 16:43:20 +0100454int drmModeAttachMode(int fd, uint32_t connector_id, drmModeModeInfoPtr mode_info)
Jesse Barnes731cd552008-12-17 10:09:49 -0800455{
456 struct drm_mode_mode_cmd res;
457
458 memcpy(&res.mode, mode_info, sizeof(struct drm_mode_modeinfo));
459 res.connector_id = connector_id;
460
461 return drmIoctl(fd, DRM_IOCTL_MODE_ATTACHMODE, &res);
462}
463
Jakob Bornecrantzeb78c532009-02-11 16:43:20 +0100464int drmModeDetachMode(int fd, uint32_t connector_id, drmModeModeInfoPtr mode_info)
Jesse Barnes731cd552008-12-17 10:09:49 -0800465{
466 struct drm_mode_mode_cmd res;
467
468 memcpy(&res.mode, mode_info, sizeof(struct drm_mode_modeinfo));
469 res.connector_id = connector_id;
470
471 return drmIoctl(fd, DRM_IOCTL_MODE_DETACHMODE, &res);
472}
473
474
475drmModePropertyPtr drmModeGetProperty(int fd, uint32_t property_id)
476{
477 struct drm_mode_get_property prop;
478 drmModePropertyPtr r;
479
480 prop.prop_id = property_id;
481 prop.count_enum_blobs = 0;
482 prop.count_values = 0;
483 prop.flags = 0;
484 prop.enum_blob_ptr = 0;
485 prop.values_ptr = 0;
486
487 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPERTY, &prop))
488 return 0;
489
490 if (prop.count_values)
491 prop.values_ptr = VOID2U64(drmMalloc(prop.count_values * sizeof(uint64_t)));
492
493 if (prop.count_enum_blobs && (prop.flags & DRM_MODE_PROP_ENUM))
494 prop.enum_blob_ptr = VOID2U64(drmMalloc(prop.count_enum_blobs * sizeof(struct drm_mode_property_enum)));
495
496 if (prop.count_enum_blobs && (prop.flags & DRM_MODE_PROP_BLOB)) {
497 prop.values_ptr = VOID2U64(drmMalloc(prop.count_enum_blobs * sizeof(uint32_t)));
498 prop.enum_blob_ptr = VOID2U64(drmMalloc(prop.count_enum_blobs * sizeof(uint32_t)));
499 }
500
501 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPERTY, &prop)) {
502 r = NULL;
503 goto err_allocs;
504 }
505
506 if (!(r = drmMalloc(sizeof(*r))))
507 return NULL;
508
509 r->prop_id = prop.prop_id;
510 r->count_values = prop.count_values;
511
512 r->flags = prop.flags;
513 if (prop.count_values)
514 r->values = drmAllocCpy(U642VOID(prop.values_ptr), prop.count_values, sizeof(uint64_t));
515 if (prop.flags & DRM_MODE_PROP_ENUM) {
516 r->count_enums = prop.count_enum_blobs;
517 r->enums = drmAllocCpy(U642VOID(prop.enum_blob_ptr), prop.count_enum_blobs, sizeof(struct drm_mode_property_enum));
518 } else if (prop.flags & DRM_MODE_PROP_BLOB) {
519 r->values = drmAllocCpy(U642VOID(prop.values_ptr), prop.count_enum_blobs, sizeof(uint32_t));
520 r->blob_ids = drmAllocCpy(U642VOID(prop.enum_blob_ptr), prop.count_enum_blobs, sizeof(uint32_t));
521 r->count_blobs = prop.count_enum_blobs;
522 }
523 strncpy(r->name, prop.name, DRM_PROP_NAME_LEN);
524 r->name[DRM_PROP_NAME_LEN-1] = 0;
525
526err_allocs:
527 drmFree(U642VOID(prop.values_ptr));
528 drmFree(U642VOID(prop.enum_blob_ptr));
529
530 return r;
531}
532
533void drmModeFreeProperty(drmModePropertyPtr ptr)
534{
535 if (!ptr)
536 return;
537
538 drmFree(ptr->values);
539 drmFree(ptr->enums);
540 drmFree(ptr);
541}
542
543drmModePropertyBlobPtr drmModeGetPropertyBlob(int fd, uint32_t blob_id)
544{
545 struct drm_mode_get_blob blob;
546 drmModePropertyBlobPtr r;
547
548 blob.length = 0;
549 blob.data = 0;
550 blob.blob_id = blob_id;
551
552 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPBLOB, &blob))
553 return NULL;
554
555 if (blob.length)
556 blob.data = VOID2U64(drmMalloc(blob.length));
557
558 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPBLOB, &blob)) {
559 r = NULL;
560 goto err_allocs;
561 }
562
563 if (!(r = drmMalloc(sizeof(*r))))
564 return NULL;
565
566 r->id = blob.blob_id;
567 r->length = blob.length;
568 r->data = drmAllocCpy(U642VOID(blob.data), 1, blob.length);
569
570err_allocs:
571 drmFree(U642VOID(blob.data));
572 return r;
573}
574
575void drmModeFreePropertyBlob(drmModePropertyBlobPtr ptr)
576{
577 if (!ptr)
578 return;
579
580 drmFree(ptr->data);
581 drmFree(ptr);
582}
583
584int drmModeConnectorSetProperty(int fd, uint32_t connector_id, uint32_t property_id,
585 uint64_t value)
586{
587 struct drm_mode_connector_set_property osp;
588 int ret;
589
590 osp.connector_id = connector_id;
591 osp.prop_id = property_id;
592 osp.value = value;
593
594 if ((ret = drmIoctl(fd, DRM_IOCTL_MODE_SETPROPERTY, &osp)))
595 return ret;
596
597 return 0;
598}
599
600/*
601 * checks if a modesetting capable driver has attached to the pci id
602 * returns 0 if modesetting supported.
603 * -EINVAL or invalid bus id
604 * -ENOSYS if no modesetting support
605*/
606int drmCheckModesettingSupported(const char *busid)
607{
608#ifdef __linux__
609 char pci_dev_dir[1024];
610 int domain, bus, dev, func;
611 DIR *sysdir;
612 struct dirent *dent;
613 int found = 0, ret;
614
615 ret = sscanf(busid, "pci:%04x:%02x:%02x.%d", &domain, &bus, &dev, &func);
616 if (ret != 4)
617 return -EINVAL;
618
619 sprintf(pci_dev_dir, "/sys/bus/pci/devices/%04x:%02x:%02x.%d/drm",
620 domain, bus, dev, func);
621
622 sysdir = opendir(pci_dev_dir);
623 if (sysdir) {
624 dent = readdir(sysdir);
625 while (dent) {
626 if (!strncmp(dent->d_name, "controlD", 8)) {
627 found = 1;
628 break;
629 }
630
631 dent = readdir(sysdir);
632 }
633 closedir(sysdir);
634 if (found)
635 return 0;
636 }
637
638 sprintf(pci_dev_dir, "/sys/bus/pci/devices/%04x:%02x:%02x.%d/",
639 domain, bus, dev, func);
640
641 sysdir = opendir(pci_dev_dir);
642 if (!sysdir)
643 return -EINVAL;
644
645 dent = readdir(sysdir);
646 while (dent) {
647 if (!strncmp(dent->d_name, "drm:controlD", 12)) {
648 found = 1;
649 break;
650 }
651
652 dent = readdir(sysdir);
653 }
654
655 closedir(sysdir);
656 if (found)
657 return 0;
658#endif
659 return -ENOSYS;
660
661}
662
Jesse Barnes731cd552008-12-17 10:09:49 -0800663int drmModeCrtcGetGamma(int fd, uint32_t crtc_id, uint32_t size,
664 uint16_t *red, uint16_t *green, uint16_t *blue)
665{
666 int ret;
667 struct drm_mode_crtc_lut l;
668
669 l.crtc_id = crtc_id;
670 l.gamma_size = size;
671 l.red = VOID2U64(red);
672 l.green = VOID2U64(green);
673 l.blue = VOID2U64(blue);
674
675 if ((ret = drmIoctl(fd, DRM_IOCTL_MODE_GETGAMMA, &l)))
676 return ret;
677
678 return 0;
679}
680
681int drmModeCrtcSetGamma(int fd, uint32_t crtc_id, uint32_t size,
682 uint16_t *red, uint16_t *green, uint16_t *blue)
683{
684 int ret;
685 struct drm_mode_crtc_lut l;
686
687 l.crtc_id = crtc_id;
688 l.gamma_size = size;
689 l.red = VOID2U64(red);
690 l.green = VOID2U64(green);
691 l.blue = VOID2U64(blue);
692
693 if ((ret = drmIoctl(fd, DRM_IOCTL_MODE_SETGAMMA, &l)))
694 return ret;
695
696 return 0;
697}
Kristian Høgsbergb0b96632009-09-11 13:27:35 -0400698
699int drmHandleEvent(int fd, drmEventContextPtr evctx)
700{
701 char buffer[1024];
702 int len, i;
703 struct drm_event *e;
704 struct drm_event_vblank *vblank;
705
706 /* The DRM read semantics guarantees that we always get only
707 * complete events. */
708
709 len = read(fd, buffer, sizeof buffer);
710 if (len == 0)
711 return 0;
712 if (len < sizeof *e)
713 return -1;
714
715 i = 0;
716 while (i < len) {
717 e = (struct drm_event *) &buffer[i];
718 switch (e->type) {
719 case DRM_EVENT_VBLANK:
720 if (evctx->version < 1 ||
721 evctx->vblank_handler == NULL)
722 break;
723 vblank = (struct drm_event_vblank *) e;
724 evctx->vblank_handler(fd,
725 vblank->sequence,
726 vblank->tv_sec,
727 vblank->tv_usec,
728 U642VOID (vblank->user_data));
729 break;
Kristian Høgsbergb80bcff2009-11-12 14:06:45 -0500730 case DRM_EVENT_FLIP_COMPLETE:
Jesse Barnes14f59582009-12-03 14:20:51 -0800731 if (evctx->version < 2 ||
Kristian Høgsbergb80bcff2009-11-12 14:06:45 -0500732 evctx->page_flip_handler == NULL)
733 break;
734 vblank = (struct drm_event_vblank *) e;
735 evctx->page_flip_handler(fd,
736 vblank->sequence,
737 vblank->tv_sec,
738 vblank->tv_usec,
739 U642VOID (vblank->user_data));
740 break;
Kristian Høgsbergb0b96632009-09-11 13:27:35 -0400741 default:
742 break;
743 }
744 i += e->length;
745 }
746
747 return 0;
748}
749
Kristian Høgsbergb80bcff2009-11-12 14:06:45 -0500750int drmModePageFlip(int fd, uint32_t crtc_id, uint32_t fb_id,
751 uint32_t flags, void *user_data)
752{
753 struct drm_mode_crtc_page_flip flip;
754
755 flip.fb_id = fb_id;
756 flip.crtc_id = crtc_id;
757 flip.user_data = VOID2U64(user_data);
758 flip.flags = flags;
759 flip.reserved = 0;
760
761 return drmIoctl(fd, DRM_IOCTL_MODE_PAGE_FLIP, &flip);
762}