blob: da7b4620fa3046f4de224c4fb788d21560bd466c [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
Marcin Slusarz763b6182011-06-05 18:53:16 +020055static inline int DRM_IOCTL(int fd, unsigned long cmd, void *arg)
Chris Wilsonb8039182010-07-01 22:38:54 +010056{
57 int ret = drmIoctl(fd, cmd, arg);
58 return ret < 0 ? -errno : ret;
59}
60
Jesse Barnes731cd552008-12-17 10:09:49 -080061/*
62 * Util functions
63 */
64
65void* 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
Jakob Bornecrantzeb78c532009-02-11 16:43:20 +010086void drmModeFreeModeInfo(drmModeModeInfoPtr ptr)
Jesse Barnes731cd552008-12-17 10:09:49 -080087{
88 if (!ptr)
89 return;
90
91 drmFree(ptr);
92}
93
94void drmModeFreeResources(drmModeResPtr ptr)
95{
96 if (!ptr)
97 return;
98
99 drmFree(ptr);
100
101}
102
103void drmModeFreeFB(drmModeFBPtr ptr)
104{
105 if (!ptr)
106 return;
107
108 /* we might add more frees later. */
109 drmFree(ptr);
110}
111
112void drmModeFreeCrtc(drmModeCrtcPtr ptr)
113{
114 if (!ptr)
115 return;
116
117 drmFree(ptr);
118
119}
120
121void drmModeFreeConnector(drmModeConnectorPtr ptr)
122{
123 if (!ptr)
124 return;
125
Keith Packard0a246542009-09-17 17:28:08 -0700126 drmFree(ptr->encoders);
127 drmFree(ptr->prop_values);
128 drmFree(ptr->props);
Jesse Barnes731cd552008-12-17 10:09:49 -0800129 drmFree(ptr->modes);
130 drmFree(ptr);
131
132}
133
134void drmModeFreeEncoder(drmModeEncoderPtr ptr)
135{
136 drmFree(ptr);
137}
138
139/*
140 * ModeSetting functions.
141 */
142
143drmModeResPtr drmModeGetResources(int fd)
144{
Chris Wilsond1308f42010-01-06 15:19:25 +0000145 struct drm_mode_card_res res, counts;
Jesse Barnes731cd552008-12-17 10:09:49 -0800146 drmModeResPtr r = 0;
147
Chris Wilsond1308f42010-01-06 15:19:25 +0000148retry:
Jesse Barnes731cd552008-12-17 10:09:49 -0800149 memset(&res, 0, sizeof(struct drm_mode_card_res));
Jesse Barnes731cd552008-12-17 10:09:49 -0800150 if (drmIoctl(fd, DRM_IOCTL_MODE_GETRESOURCES, &res))
151 return 0;
152
Chris Wilsond1308f42010-01-06 15:19:25 +0000153 counts = res;
154
Chris Wilson85fb3e52010-01-06 15:39:49 +0000155 if (res.count_fbs) {
Jesse Barnes731cd552008-12-17 10:09:49 -0800156 res.fb_id_ptr = VOID2U64(drmMalloc(res.count_fbs*sizeof(uint32_t)));
Chris Wilson85fb3e52010-01-06 15:39:49 +0000157 if (!res.fb_id_ptr)
158 goto err_allocs;
159 }
160 if (res.count_crtcs) {
Jesse Barnes731cd552008-12-17 10:09:49 -0800161 res.crtc_id_ptr = VOID2U64(drmMalloc(res.count_crtcs*sizeof(uint32_t)));
Chris Wilson85fb3e52010-01-06 15:39:49 +0000162 if (!res.crtc_id_ptr)
163 goto err_allocs;
164 }
165 if (res.count_connectors) {
Jesse Barnes731cd552008-12-17 10:09:49 -0800166 res.connector_id_ptr = VOID2U64(drmMalloc(res.count_connectors*sizeof(uint32_t)));
Chris Wilson85fb3e52010-01-06 15:39:49 +0000167 if (!res.connector_id_ptr)
168 goto err_allocs;
169 }
170 if (res.count_encoders) {
Jesse Barnes731cd552008-12-17 10:09:49 -0800171 res.encoder_id_ptr = VOID2U64(drmMalloc(res.count_encoders*sizeof(uint32_t)));
Chris Wilson85fb3e52010-01-06 15:39:49 +0000172 if (!res.encoder_id_ptr)
173 goto err_allocs;
174 }
Jesse Barnes731cd552008-12-17 10:09:49 -0800175
Chris Wilsond1308f42010-01-06 15:19:25 +0000176 if (drmIoctl(fd, DRM_IOCTL_MODE_GETRESOURCES, &res))
Jesse Barnes731cd552008-12-17 10:09:49 -0800177 goto err_allocs;
Chris Wilsond1308f42010-01-06 15:19:25 +0000178
179 /* The number of available connectors and etc may have changed with a
180 * hotplug event in between the ioctls, in which case the field is
181 * silently ignored by the kernel.
182 */
183 if (counts.count_fbs < res.count_fbs ||
184 counts.count_crtcs < res.count_crtcs ||
185 counts.count_connectors < res.count_connectors ||
186 counts.count_encoders < res.count_encoders)
187 {
188 drmFree(U642VOID(res.fb_id_ptr));
189 drmFree(U642VOID(res.crtc_id_ptr));
190 drmFree(U642VOID(res.connector_id_ptr));
191 drmFree(U642VOID(res.encoder_id_ptr));
192
193 goto retry;
Jesse Barnes731cd552008-12-17 10:09:49 -0800194 }
195
196 /*
197 * return
198 */
Jesse Barnes731cd552008-12-17 10:09:49 -0800199 if (!(r = drmMalloc(sizeof(*r))))
Chris Wilsond1308f42010-01-06 15:19:25 +0000200 goto err_allocs;
Jesse Barnes731cd552008-12-17 10:09:49 -0800201
202 r->min_width = res.min_width;
203 r->max_width = res.max_width;
204 r->min_height = res.min_height;
205 r->max_height = res.max_height;
206 r->count_fbs = res.count_fbs;
207 r->count_crtcs = res.count_crtcs;
208 r->count_connectors = res.count_connectors;
209 r->count_encoders = res.count_encoders;
Chris Wilson85fb3e52010-01-06 15:39:49 +0000210
211 r->fbs = drmAllocCpy(U642VOID(res.fb_id_ptr), res.count_fbs, sizeof(uint32_t));
212 r->crtcs = drmAllocCpy(U642VOID(res.crtc_id_ptr), res.count_crtcs, sizeof(uint32_t));
213 r->connectors = drmAllocCpy(U642VOID(res.connector_id_ptr), res.count_connectors, sizeof(uint32_t));
214 r->encoders = drmAllocCpy(U642VOID(res.encoder_id_ptr), res.count_encoders, sizeof(uint32_t));
Chris Wilsone6c136c2010-01-06 16:53:33 +0000215 if ((res.count_fbs && !r->fbs) ||
216 (res.count_crtcs && !r->crtcs) ||
217 (res.count_connectors && !r->connectors) ||
218 (res.count_encoders && !r->encoders))
219 {
Chris Wilson85fb3e52010-01-06 15:39:49 +0000220 drmFree(r->fbs);
221 drmFree(r->crtcs);
222 drmFree(r->connectors);
223 drmFree(r->encoders);
224 drmFree(r);
225 r = 0;
226 }
Jesse Barnes731cd552008-12-17 10:09:49 -0800227
228err_allocs:
229 drmFree(U642VOID(res.fb_id_ptr));
230 drmFree(U642VOID(res.crtc_id_ptr));
231 drmFree(U642VOID(res.connector_id_ptr));
232 drmFree(U642VOID(res.encoder_id_ptr));
233
234 return r;
235}
236
237int drmModeAddFB(int fd, uint32_t width, uint32_t height, uint8_t depth,
238 uint8_t bpp, uint32_t pitch, uint32_t bo_handle,
239 uint32_t *buf_id)
240{
241 struct drm_mode_fb_cmd f;
242 int ret;
243
244 f.width = width;
245 f.height = height;
246 f.pitch = pitch;
247 f.bpp = bpp;
248 f.depth = depth;
249 f.handle = bo_handle;
250
Chris Wilsonb8039182010-07-01 22:38:54 +0100251 if ((ret = DRM_IOCTL(fd, DRM_IOCTL_MODE_ADDFB, &f)))
Jesse Barnes731cd552008-12-17 10:09:49 -0800252 return ret;
253
254 *buf_id = f.fb_id;
255 return 0;
256}
257
Jesse Barnesac168bf2011-04-29 08:53:53 -0700258int drmModeAddFB2(int fd, uint32_t width, uint32_t height,
259 uint32_t pixel_format, uint32_t bo_handles[4],
260 uint32_t pitches[4], uint32_t offsets[4],
261 uint32_t *buf_id, uint32_t flags)
262{
263 struct drm_mode_fb_cmd2 f;
264 int ret;
265
266 f.width = width;
267 f.height = height;
268 f.pixel_format = pixel_format;
269 f.flags = flags;
270 memcpy(f.handles, bo_handles, sizeof(bo_handles));
271 memcpy(f.pitches, pitches, sizeof(pitches));
272 memcpy(f.offsets, offsets, sizeof(offsets));
273
274 if ((ret = DRM_IOCTL(fd, DRM_IOCTL_MODE_ADDFB2, &f)))
275 return ret;
276
277 *buf_id = f.fb_id;
278 return 0;
279}
280
Jesse Barnes731cd552008-12-17 10:09:49 -0800281int drmModeRmFB(int fd, uint32_t bufferId)
282{
Chris Wilsonb8039182010-07-01 22:38:54 +0100283 return DRM_IOCTL(fd, DRM_IOCTL_MODE_RMFB, &bufferId);
Jesse Barnes731cd552008-12-17 10:09:49 -0800284
285
286}
287
288drmModeFBPtr drmModeGetFB(int fd, uint32_t buf)
289{
290 struct drm_mode_fb_cmd info;
291 drmModeFBPtr r;
292
293 info.fb_id = buf;
294
295 if (drmIoctl(fd, DRM_IOCTL_MODE_GETFB, &info))
296 return NULL;
297
298 if (!(r = drmMalloc(sizeof(*r))))
299 return NULL;
300
301 r->fb_id = info.fb_id;
302 r->width = info.width;
303 r->height = info.height;
304 r->pitch = info.pitch;
305 r->bpp = info.bpp;
306 r->handle = info.handle;
307 r->depth = info.depth;
308
309 return r;
310}
311
Jakob Bornecrantz3e486132009-11-24 18:00:12 +0100312int drmModeDirtyFB(int fd, uint32_t bufferId,
313 drmModeClipPtr clips, uint32_t num_clips)
314{
315 struct drm_mode_fb_dirty_cmd dirty = { 0 };
316
317 dirty.fb_id = bufferId;
318 dirty.clips_ptr = VOID2U64(clips);
319 dirty.num_clips = num_clips;
320
Chris Wilsonb8039182010-07-01 22:38:54 +0100321 return DRM_IOCTL(fd, DRM_IOCTL_MODE_DIRTYFB, &dirty);
Jakob Bornecrantz3e486132009-11-24 18:00:12 +0100322}
323
Jesse Barnes731cd552008-12-17 10:09:49 -0800324
325/*
326 * Crtc functions
327 */
328
329drmModeCrtcPtr drmModeGetCrtc(int fd, uint32_t crtcId)
330{
331 struct drm_mode_crtc crtc;
332 drmModeCrtcPtr r;
333
334 crtc.crtc_id = crtcId;
335
336 if (drmIoctl(fd, DRM_IOCTL_MODE_GETCRTC, &crtc))
337 return 0;
338
339 /*
340 * return
341 */
342
343 if (!(r = drmMalloc(sizeof(*r))))
344 return 0;
345
346 r->crtc_id = crtc.crtc_id;
347 r->x = crtc.x;
348 r->y = crtc.y;
349 r->mode_valid = crtc.mode_valid;
350 if (r->mode_valid)
351 memcpy(&r->mode, &crtc.mode, sizeof(struct drm_mode_modeinfo));
352 r->buffer_id = crtc.fb_id;
353 r->gamma_size = crtc.gamma_size;
354 return r;
355}
356
357
358int drmModeSetCrtc(int fd, uint32_t crtcId, uint32_t bufferId,
359 uint32_t x, uint32_t y, uint32_t *connectors, int count,
Jakob Bornecrantzeb78c532009-02-11 16:43:20 +0100360 drmModeModeInfoPtr mode)
Jesse Barnes731cd552008-12-17 10:09:49 -0800361{
362 struct drm_mode_crtc crtc;
363
364 crtc.x = x;
365 crtc.y = y;
366 crtc.crtc_id = crtcId;
367 crtc.fb_id = bufferId;
368 crtc.set_connectors_ptr = VOID2U64(connectors);
369 crtc.count_connectors = count;
370 if (mode) {
371 memcpy(&crtc.mode, mode, sizeof(struct drm_mode_modeinfo));
372 crtc.mode_valid = 1;
373 } else
374 crtc.mode_valid = 0;
375
Chris Wilsonb8039182010-07-01 22:38:54 +0100376 return DRM_IOCTL(fd, DRM_IOCTL_MODE_SETCRTC, &crtc);
Jesse Barnes731cd552008-12-17 10:09:49 -0800377}
378
379/*
380 * Cursor manipulation
381 */
382
383int drmModeSetCursor(int fd, uint32_t crtcId, uint32_t bo_handle, uint32_t width, uint32_t height)
384{
385 struct drm_mode_cursor arg;
386
387 arg.flags = DRM_MODE_CURSOR_BO;
388 arg.crtc_id = crtcId;
389 arg.width = width;
390 arg.height = height;
391 arg.handle = bo_handle;
392
Chris Wilsonb8039182010-07-01 22:38:54 +0100393 return DRM_IOCTL(fd, DRM_IOCTL_MODE_CURSOR, &arg);
Jesse Barnes731cd552008-12-17 10:09:49 -0800394}
395
396int drmModeMoveCursor(int fd, uint32_t crtcId, int x, int y)
397{
398 struct drm_mode_cursor arg;
399
400 arg.flags = DRM_MODE_CURSOR_MOVE;
401 arg.crtc_id = crtcId;
402 arg.x = x;
403 arg.y = y;
404
Chris Wilsonb8039182010-07-01 22:38:54 +0100405 return DRM_IOCTL(fd, DRM_IOCTL_MODE_CURSOR, &arg);
Jesse Barnes731cd552008-12-17 10:09:49 -0800406}
407
408/*
409 * Encoder get
410 */
411drmModeEncoderPtr drmModeGetEncoder(int fd, uint32_t encoder_id)
412{
413 struct drm_mode_get_encoder enc;
414 drmModeEncoderPtr r = NULL;
415
416 enc.encoder_id = encoder_id;
417 enc.encoder_type = 0;
418 enc.possible_crtcs = 0;
419 enc.possible_clones = 0;
420
421 if (drmIoctl(fd, DRM_IOCTL_MODE_GETENCODER, &enc))
422 return 0;
423
424 if (!(r = drmMalloc(sizeof(*r))))
425 return 0;
426
427 r->encoder_id = enc.encoder_id;
428 r->crtc_id = enc.crtc_id;
429 r->encoder_type = enc.encoder_type;
430 r->possible_crtcs = enc.possible_crtcs;
431 r->possible_clones = enc.possible_clones;
432
433 return r;
434}
435
436/*
437 * Connector manipulation
438 */
439
440drmModeConnectorPtr drmModeGetConnector(int fd, uint32_t connector_id)
441{
Peter Clifton04f90a42010-01-06 20:44:11 +0000442 struct drm_mode_get_connector conn, counts;
Jesse Barnes731cd552008-12-17 10:09:49 -0800443 drmModeConnectorPtr r = NULL;
444
Peter Clifton04f90a42010-01-06 20:44:11 +0000445retry:
446 memset(&conn, 0, sizeof(struct drm_mode_get_connector));
Jesse Barnes731cd552008-12-17 10:09:49 -0800447 conn.connector_id = connector_id;
Jesse Barnes731cd552008-12-17 10:09:49 -0800448
449 if (drmIoctl(fd, DRM_IOCTL_MODE_GETCONNECTOR, &conn))
450 return 0;
451
Peter Clifton04f90a42010-01-06 20:44:11 +0000452 counts = conn;
453
Jesse Barnes731cd552008-12-17 10:09:49 -0800454 if (conn.count_props) {
455 conn.props_ptr = VOID2U64(drmMalloc(conn.count_props*sizeof(uint32_t)));
Peter Clifton04f90a42010-01-06 20:44:11 +0000456 if (!conn.props_ptr)
457 goto err_allocs;
Jesse Barnes731cd552008-12-17 10:09:49 -0800458 conn.prop_values_ptr = VOID2U64(drmMalloc(conn.count_props*sizeof(uint64_t)));
Peter Clifton04f90a42010-01-06 20:44:11 +0000459 if (!conn.prop_values_ptr)
460 goto err_allocs;
Jesse Barnes731cd552008-12-17 10:09:49 -0800461 }
462
Peter Clifton04f90a42010-01-06 20:44:11 +0000463 if (conn.count_modes) {
Jesse Barnes731cd552008-12-17 10:09:49 -0800464 conn.modes_ptr = VOID2U64(drmMalloc(conn.count_modes*sizeof(struct drm_mode_modeinfo)));
Peter Clifton04f90a42010-01-06 20:44:11 +0000465 if (!conn.modes_ptr)
466 goto err_allocs;
467 }
Jesse Barnes731cd552008-12-17 10:09:49 -0800468
Peter Clifton04f90a42010-01-06 20:44:11 +0000469 if (conn.count_encoders) {
Jesse Barnes731cd552008-12-17 10:09:49 -0800470 conn.encoders_ptr = VOID2U64(drmMalloc(conn.count_encoders*sizeof(uint32_t)));
Peter Clifton04f90a42010-01-06 20:44:11 +0000471 if (!conn.encoders_ptr)
472 goto err_allocs;
473 }
Jesse Barnes731cd552008-12-17 10:09:49 -0800474
475 if (drmIoctl(fd, DRM_IOCTL_MODE_GETCONNECTOR, &conn))
476 goto err_allocs;
477
Peter Clifton04f90a42010-01-06 20:44:11 +0000478 /* The number of available connectors and etc may have changed with a
479 * hotplug event in between the ioctls, in which case the field is
480 * silently ignored by the kernel.
481 */
482 if (counts.count_props < conn.count_props ||
483 counts.count_modes < conn.count_modes ||
484 counts.count_encoders < conn.count_encoders) {
485 drmFree(U642VOID(conn.props_ptr));
486 drmFree(U642VOID(conn.prop_values_ptr));
487 drmFree(U642VOID(conn.modes_ptr));
488 drmFree(U642VOID(conn.encoders_ptr));
489
490 goto retry;
491 }
492
Jesse Barnes731cd552008-12-17 10:09:49 -0800493 if(!(r = drmMalloc(sizeof(*r)))) {
494 goto err_allocs;
495 }
496
497 r->connector_id = conn.connector_id;
498 r->encoder_id = conn.encoder_id;
499 r->connection = conn.connection;
500 r->mmWidth = conn.mm_width;
501 r->mmHeight = conn.mm_height;
Dave Airlie412d3702009-04-22 20:25:40 +1000502 /* convert subpixel from kernel to userspace */
503 r->subpixel = conn.subpixel + 1;
Jesse Barnes731cd552008-12-17 10:09:49 -0800504 r->count_modes = conn.count_modes;
Jesse Barnes731cd552008-12-17 10:09:49 -0800505 r->count_props = conn.count_props;
506 r->props = drmAllocCpy(U642VOID(conn.props_ptr), conn.count_props, sizeof(uint32_t));
507 r->prop_values = drmAllocCpy(U642VOID(conn.prop_values_ptr), conn.count_props, sizeof(uint64_t));
508 r->modes = drmAllocCpy(U642VOID(conn.modes_ptr), conn.count_modes, sizeof(struct drm_mode_modeinfo));
509 r->count_encoders = conn.count_encoders;
510 r->encoders = drmAllocCpy(U642VOID(conn.encoders_ptr), conn.count_encoders, sizeof(uint32_t));
511 r->connector_type = conn.connector_type;
512 r->connector_type_id = conn.connector_type_id;
513
Peter Clifton04f90a42010-01-06 20:44:11 +0000514 if ((r->count_props && !r->props) ||
515 (r->count_props && !r->prop_values) ||
516 (r->count_modes && !r->modes) ||
517 (r->count_encoders && !r->encoders)) {
518 drmFree(r->props);
519 drmFree(r->prop_values);
520 drmFree(r->modes);
521 drmFree(r->encoders);
522 drmFree(r);
523 r = 0;
524 }
Jesse Barnes731cd552008-12-17 10:09:49 -0800525
526err_allocs:
527 drmFree(U642VOID(conn.prop_values_ptr));
528 drmFree(U642VOID(conn.props_ptr));
529 drmFree(U642VOID(conn.modes_ptr));
530 drmFree(U642VOID(conn.encoders_ptr));
531
532 return r;
533}
534
Jakob Bornecrantzeb78c532009-02-11 16:43:20 +0100535int drmModeAttachMode(int fd, uint32_t connector_id, drmModeModeInfoPtr mode_info)
Jesse Barnes731cd552008-12-17 10:09:49 -0800536{
537 struct drm_mode_mode_cmd res;
538
539 memcpy(&res.mode, mode_info, sizeof(struct drm_mode_modeinfo));
540 res.connector_id = connector_id;
541
Chris Wilsonb8039182010-07-01 22:38:54 +0100542 return DRM_IOCTL(fd, DRM_IOCTL_MODE_ATTACHMODE, &res);
Jesse Barnes731cd552008-12-17 10:09:49 -0800543}
544
Jakob Bornecrantzeb78c532009-02-11 16:43:20 +0100545int drmModeDetachMode(int fd, uint32_t connector_id, drmModeModeInfoPtr mode_info)
Jesse Barnes731cd552008-12-17 10:09:49 -0800546{
547 struct drm_mode_mode_cmd res;
548
549 memcpy(&res.mode, mode_info, sizeof(struct drm_mode_modeinfo));
550 res.connector_id = connector_id;
551
Chris Wilsonb8039182010-07-01 22:38:54 +0100552 return DRM_IOCTL(fd, DRM_IOCTL_MODE_DETACHMODE, &res);
Jesse Barnes731cd552008-12-17 10:09:49 -0800553}
554
555
556drmModePropertyPtr drmModeGetProperty(int fd, uint32_t property_id)
557{
558 struct drm_mode_get_property prop;
559 drmModePropertyPtr r;
560
561 prop.prop_id = property_id;
562 prop.count_enum_blobs = 0;
563 prop.count_values = 0;
564 prop.flags = 0;
565 prop.enum_blob_ptr = 0;
566 prop.values_ptr = 0;
567
568 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPERTY, &prop))
569 return 0;
570
571 if (prop.count_values)
572 prop.values_ptr = VOID2U64(drmMalloc(prop.count_values * sizeof(uint64_t)));
573
574 if (prop.count_enum_blobs && (prop.flags & DRM_MODE_PROP_ENUM))
575 prop.enum_blob_ptr = VOID2U64(drmMalloc(prop.count_enum_blobs * sizeof(struct drm_mode_property_enum)));
576
577 if (prop.count_enum_blobs && (prop.flags & DRM_MODE_PROP_BLOB)) {
578 prop.values_ptr = VOID2U64(drmMalloc(prop.count_enum_blobs * sizeof(uint32_t)));
579 prop.enum_blob_ptr = VOID2U64(drmMalloc(prop.count_enum_blobs * sizeof(uint32_t)));
580 }
581
582 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPERTY, &prop)) {
583 r = NULL;
584 goto err_allocs;
585 }
586
587 if (!(r = drmMalloc(sizeof(*r))))
588 return NULL;
589
590 r->prop_id = prop.prop_id;
591 r->count_values = prop.count_values;
592
593 r->flags = prop.flags;
594 if (prop.count_values)
595 r->values = drmAllocCpy(U642VOID(prop.values_ptr), prop.count_values, sizeof(uint64_t));
596 if (prop.flags & DRM_MODE_PROP_ENUM) {
597 r->count_enums = prop.count_enum_blobs;
598 r->enums = drmAllocCpy(U642VOID(prop.enum_blob_ptr), prop.count_enum_blobs, sizeof(struct drm_mode_property_enum));
599 } else if (prop.flags & DRM_MODE_PROP_BLOB) {
600 r->values = drmAllocCpy(U642VOID(prop.values_ptr), prop.count_enum_blobs, sizeof(uint32_t));
601 r->blob_ids = drmAllocCpy(U642VOID(prop.enum_blob_ptr), prop.count_enum_blobs, sizeof(uint32_t));
602 r->count_blobs = prop.count_enum_blobs;
603 }
604 strncpy(r->name, prop.name, DRM_PROP_NAME_LEN);
605 r->name[DRM_PROP_NAME_LEN-1] = 0;
606
607err_allocs:
608 drmFree(U642VOID(prop.values_ptr));
609 drmFree(U642VOID(prop.enum_blob_ptr));
610
611 return r;
612}
613
614void drmModeFreeProperty(drmModePropertyPtr ptr)
615{
616 if (!ptr)
617 return;
618
619 drmFree(ptr->values);
620 drmFree(ptr->enums);
621 drmFree(ptr);
622}
623
624drmModePropertyBlobPtr drmModeGetPropertyBlob(int fd, uint32_t blob_id)
625{
626 struct drm_mode_get_blob blob;
627 drmModePropertyBlobPtr r;
628
629 blob.length = 0;
630 blob.data = 0;
631 blob.blob_id = blob_id;
632
633 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPBLOB, &blob))
634 return NULL;
635
636 if (blob.length)
637 blob.data = VOID2U64(drmMalloc(blob.length));
638
639 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPBLOB, &blob)) {
640 r = NULL;
641 goto err_allocs;
642 }
643
644 if (!(r = drmMalloc(sizeof(*r))))
Chris Wilson8a762442010-08-24 21:29:31 +0100645 goto err_allocs;
Jesse Barnes731cd552008-12-17 10:09:49 -0800646
647 r->id = blob.blob_id;
648 r->length = blob.length;
649 r->data = drmAllocCpy(U642VOID(blob.data), 1, blob.length);
650
651err_allocs:
652 drmFree(U642VOID(blob.data));
653 return r;
654}
655
656void drmModeFreePropertyBlob(drmModePropertyBlobPtr ptr)
657{
658 if (!ptr)
659 return;
660
661 drmFree(ptr->data);
662 drmFree(ptr);
663}
664
665int drmModeConnectorSetProperty(int fd, uint32_t connector_id, uint32_t property_id,
666 uint64_t value)
667{
668 struct drm_mode_connector_set_property osp;
Jesse Barnes731cd552008-12-17 10:09:49 -0800669
670 osp.connector_id = connector_id;
671 osp.prop_id = property_id;
672 osp.value = value;
673
Chris Wilsonb8039182010-07-01 22:38:54 +0100674 return DRM_IOCTL(fd, DRM_IOCTL_MODE_SETPROPERTY, &osp);
Jesse Barnes731cd552008-12-17 10:09:49 -0800675}
676
677/*
678 * checks if a modesetting capable driver has attached to the pci id
679 * returns 0 if modesetting supported.
680 * -EINVAL or invalid bus id
681 * -ENOSYS if no modesetting support
682*/
683int drmCheckModesettingSupported(const char *busid)
684{
685#ifdef __linux__
686 char pci_dev_dir[1024];
687 int domain, bus, dev, func;
688 DIR *sysdir;
689 struct dirent *dent;
690 int found = 0, ret;
691
692 ret = sscanf(busid, "pci:%04x:%02x:%02x.%d", &domain, &bus, &dev, &func);
693 if (ret != 4)
694 return -EINVAL;
695
696 sprintf(pci_dev_dir, "/sys/bus/pci/devices/%04x:%02x:%02x.%d/drm",
697 domain, bus, dev, func);
698
699 sysdir = opendir(pci_dev_dir);
700 if (sysdir) {
701 dent = readdir(sysdir);
702 while (dent) {
703 if (!strncmp(dent->d_name, "controlD", 8)) {
704 found = 1;
705 break;
706 }
707
708 dent = readdir(sysdir);
709 }
710 closedir(sysdir);
711 if (found)
712 return 0;
713 }
714
715 sprintf(pci_dev_dir, "/sys/bus/pci/devices/%04x:%02x:%02x.%d/",
716 domain, bus, dev, func);
717
718 sysdir = opendir(pci_dev_dir);
719 if (!sysdir)
720 return -EINVAL;
721
722 dent = readdir(sysdir);
723 while (dent) {
724 if (!strncmp(dent->d_name, "drm:controlD", 12)) {
725 found = 1;
726 break;
727 }
728
729 dent = readdir(sysdir);
730 }
731
732 closedir(sysdir);
733 if (found)
734 return 0;
735#endif
736 return -ENOSYS;
737
738}
739
Jesse Barnes731cd552008-12-17 10:09:49 -0800740int drmModeCrtcGetGamma(int fd, uint32_t crtc_id, uint32_t size,
741 uint16_t *red, uint16_t *green, uint16_t *blue)
742{
Jesse Barnes731cd552008-12-17 10:09:49 -0800743 struct drm_mode_crtc_lut l;
744
745 l.crtc_id = crtc_id;
746 l.gamma_size = size;
747 l.red = VOID2U64(red);
748 l.green = VOID2U64(green);
749 l.blue = VOID2U64(blue);
750
Chris Wilsonb8039182010-07-01 22:38:54 +0100751 return DRM_IOCTL(fd, DRM_IOCTL_MODE_GETGAMMA, &l);
Jesse Barnes731cd552008-12-17 10:09:49 -0800752}
753
754int drmModeCrtcSetGamma(int fd, uint32_t crtc_id, uint32_t size,
755 uint16_t *red, uint16_t *green, uint16_t *blue)
756{
Jesse Barnes731cd552008-12-17 10:09:49 -0800757 struct drm_mode_crtc_lut l;
758
759 l.crtc_id = crtc_id;
760 l.gamma_size = size;
761 l.red = VOID2U64(red);
762 l.green = VOID2U64(green);
763 l.blue = VOID2U64(blue);
764
Chris Wilsonb8039182010-07-01 22:38:54 +0100765 return DRM_IOCTL(fd, DRM_IOCTL_MODE_SETGAMMA, &l);
Jesse Barnes731cd552008-12-17 10:09:49 -0800766}
Kristian Høgsbergb0b96632009-09-11 13:27:35 -0400767
768int drmHandleEvent(int fd, drmEventContextPtr evctx)
769{
770 char buffer[1024];
771 int len, i;
772 struct drm_event *e;
773 struct drm_event_vblank *vblank;
774
775 /* The DRM read semantics guarantees that we always get only
776 * complete events. */
777
778 len = read(fd, buffer, sizeof buffer);
779 if (len == 0)
780 return 0;
781 if (len < sizeof *e)
782 return -1;
783
784 i = 0;
785 while (i < len) {
786 e = (struct drm_event *) &buffer[i];
787 switch (e->type) {
788 case DRM_EVENT_VBLANK:
789 if (evctx->version < 1 ||
790 evctx->vblank_handler == NULL)
791 break;
792 vblank = (struct drm_event_vblank *) e;
793 evctx->vblank_handler(fd,
794 vblank->sequence,
795 vblank->tv_sec,
796 vblank->tv_usec,
797 U642VOID (vblank->user_data));
798 break;
Kristian Høgsbergb80bcff2009-11-12 14:06:45 -0500799 case DRM_EVENT_FLIP_COMPLETE:
Jesse Barnes14f59582009-12-03 14:20:51 -0800800 if (evctx->version < 2 ||
Kristian Høgsbergb80bcff2009-11-12 14:06:45 -0500801 evctx->page_flip_handler == NULL)
802 break;
803 vblank = (struct drm_event_vblank *) e;
804 evctx->page_flip_handler(fd,
805 vblank->sequence,
806 vblank->tv_sec,
807 vblank->tv_usec,
808 U642VOID (vblank->user_data));
809 break;
Kristian Høgsbergb0b96632009-09-11 13:27:35 -0400810 default:
811 break;
812 }
813 i += e->length;
814 }
815
816 return 0;
817}
818
Kristian Høgsbergb80bcff2009-11-12 14:06:45 -0500819int drmModePageFlip(int fd, uint32_t crtc_id, uint32_t fb_id,
820 uint32_t flags, void *user_data)
821{
822 struct drm_mode_crtc_page_flip flip;
823
824 flip.fb_id = fb_id;
825 flip.crtc_id = crtc_id;
826 flip.user_data = VOID2U64(user_data);
827 flip.flags = flags;
828 flip.reserved = 0;
829
Chris Wilsonb8039182010-07-01 22:38:54 +0100830 return DRM_IOCTL(fd, DRM_IOCTL_MODE_PAGE_FLIP, &flip);
Kristian Høgsbergb80bcff2009-11-12 14:06:45 -0500831}
Jesse Barnesac168bf2011-04-29 08:53:53 -0700832
833int drmModeSetPlane(int fd, uint32_t plane_id, uint32_t crtc_id,
834 uint32_t fb_id, uint32_t flags,
835 uint32_t crtc_x, uint32_t crtc_y,
836 uint32_t crtc_w, uint32_t crtc_h,
837 uint32_t src_x, uint32_t src_y,
838 uint32_t src_w, uint32_t src_h)
839
840{
841 struct drm_mode_set_plane s;
842
843 s.plane_id = plane_id;
844 s.crtc_id = crtc_id;
845 s.fb_id = fb_id;
846 s.flags = flags;
847 s.crtc_x = crtc_x;
848 s.crtc_y = crtc_y;
849 s.crtc_w = crtc_w;
850 s.crtc_h = crtc_h;
851 s.src_x = src_x;
852 s.src_y = src_y;
853 s.src_w = src_w;
854 s.src_h = src_h;
855
856 return DRM_IOCTL(fd, DRM_IOCTL_MODE_SETPLANE, &s);
857}
858
859
860drmModePlanePtr drmModeGetPlane(int fd, uint32_t plane_id)
861{
862 struct drm_mode_get_plane ovr, counts;
863 drmModePlanePtr r = 0;
864
865retry:
866 memset(&ovr, 0, sizeof(struct drm_mode_get_plane));
867 ovr.plane_id = plane_id;
868 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPLANE, &ovr))
869 return 0;
870
871 counts = ovr;
872
873 if (ovr.count_format_types) {
874 ovr.format_type_ptr = VOID2U64(drmMalloc(ovr.count_format_types *
875 sizeof(uint32_t)));
876 if (!ovr.format_type_ptr)
877 goto err_allocs;
878 }
879
880 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPLANE, &ovr))
881 goto err_allocs;
882
883 if (counts.count_format_types < ovr.count_format_types) {
884 drmFree(U642VOID(ovr.format_type_ptr));
885 goto retry;
886 }
887
888 if (!(r = drmMalloc(sizeof(*r))))
889 goto err_allocs;
890
891 r->count_formats = ovr.count_format_types;
892 r->plane_id = ovr.plane_id;
893 r->crtc_id = ovr.crtc_id;
894 r->fb_id = ovr.fb_id;
895 r->possible_crtcs = ovr.possible_crtcs;
896 r->gamma_size = ovr.gamma_size;
897 r->formats = drmAllocCpy(U642VOID(ovr.format_type_ptr),
898 ovr.count_format_types, sizeof(uint32_t));
899 if (ovr.count_format_types && !r->formats) {
900 drmFree(r->formats);
901 r = 0;
902 }
903
904err_allocs:
905 drmFree(U642VOID(ovr.format_type_ptr));
906
907 return r;
908}
909
910void drmModeFreePlane(drmModePlanePtr ptr)
911{
912 if (!ptr)
913 return;
914
915 drmFree(ptr->formats);
916 drmFree(ptr);
917}
918
919drmModePlaneResPtr drmModeGetPlaneResources(int fd)
920{
921 struct drm_mode_get_plane_res res, counts;
922 drmModePlaneResPtr r = 0;
923
924retry:
925 memset(&res, 0, sizeof(struct drm_mode_get_plane_res));
926 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPLANERESOURCES, &res))
927 return 0;
928
929 counts = res;
930
931 if (res.count_planes) {
932 res.plane_id_ptr = VOID2U64(drmMalloc(res.count_planes *
933 sizeof(uint32_t)));
934 if (!res.plane_id_ptr)
935 goto err_allocs;
936 }
937
938 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPLANERESOURCES, &res))
939 goto err_allocs;
940
941 if (counts.count_planes < res.count_planes) {
942 drmFree(U642VOID(res.plane_id_ptr));
943 goto retry;
944 }
945
946 if (!(r = drmMalloc(sizeof(*r))))
947 goto err_allocs;
948
949 r->count_planes = res.count_planes;
950 r->planes = drmAllocCpy(U642VOID(res.plane_id_ptr),
951 res.count_planes, sizeof(uint32_t));
952 if (res.count_planes && !r->planes) {
953 drmFree(r->planes);
954 r = 0;
955 }
956
957err_allocs:
958 drmFree(U642VOID(res.plane_id_ptr));
959
960 return r;
961}