blob: f603ceb2bc57d1fcdec242f0a59dae9edbf65035 [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
Ville Syrjälädf497e92012-02-02 14:53:39 -050099 drmFree(ptr->fbs);
100 drmFree(ptr->crtcs);
101 drmFree(ptr->connectors);
102 drmFree(ptr->encoders);
Jesse Barnes731cd552008-12-17 10:09:49 -0800103 drmFree(ptr);
104
105}
106
107void drmModeFreeFB(drmModeFBPtr ptr)
108{
109 if (!ptr)
110 return;
111
112 /* we might add more frees later. */
113 drmFree(ptr);
114}
115
116void drmModeFreeCrtc(drmModeCrtcPtr ptr)
117{
118 if (!ptr)
119 return;
120
121 drmFree(ptr);
122
123}
124
125void drmModeFreeConnector(drmModeConnectorPtr ptr)
126{
127 if (!ptr)
128 return;
129
Keith Packard0a246542009-09-17 17:28:08 -0700130 drmFree(ptr->encoders);
131 drmFree(ptr->prop_values);
132 drmFree(ptr->props);
Jesse Barnes731cd552008-12-17 10:09:49 -0800133 drmFree(ptr->modes);
134 drmFree(ptr);
135
136}
137
138void drmModeFreeEncoder(drmModeEncoderPtr ptr)
139{
140 drmFree(ptr);
141}
142
143/*
144 * ModeSetting functions.
145 */
146
147drmModeResPtr drmModeGetResources(int fd)
148{
Chris Wilsond1308f42010-01-06 15:19:25 +0000149 struct drm_mode_card_res res, counts;
Jesse Barnes731cd552008-12-17 10:09:49 -0800150 drmModeResPtr r = 0;
151
Chris Wilsond1308f42010-01-06 15:19:25 +0000152retry:
Jesse Barnes731cd552008-12-17 10:09:49 -0800153 memset(&res, 0, sizeof(struct drm_mode_card_res));
Jesse Barnes731cd552008-12-17 10:09:49 -0800154 if (drmIoctl(fd, DRM_IOCTL_MODE_GETRESOURCES, &res))
155 return 0;
156
Chris Wilsond1308f42010-01-06 15:19:25 +0000157 counts = res;
158
Chris Wilson85fb3e52010-01-06 15:39:49 +0000159 if (res.count_fbs) {
Jesse Barnes731cd552008-12-17 10:09:49 -0800160 res.fb_id_ptr = VOID2U64(drmMalloc(res.count_fbs*sizeof(uint32_t)));
Chris Wilson85fb3e52010-01-06 15:39:49 +0000161 if (!res.fb_id_ptr)
162 goto err_allocs;
163 }
164 if (res.count_crtcs) {
Jesse Barnes731cd552008-12-17 10:09:49 -0800165 res.crtc_id_ptr = VOID2U64(drmMalloc(res.count_crtcs*sizeof(uint32_t)));
Chris Wilson85fb3e52010-01-06 15:39:49 +0000166 if (!res.crtc_id_ptr)
167 goto err_allocs;
168 }
169 if (res.count_connectors) {
Jesse Barnes731cd552008-12-17 10:09:49 -0800170 res.connector_id_ptr = VOID2U64(drmMalloc(res.count_connectors*sizeof(uint32_t)));
Chris Wilson85fb3e52010-01-06 15:39:49 +0000171 if (!res.connector_id_ptr)
172 goto err_allocs;
173 }
174 if (res.count_encoders) {
Jesse Barnes731cd552008-12-17 10:09:49 -0800175 res.encoder_id_ptr = VOID2U64(drmMalloc(res.count_encoders*sizeof(uint32_t)));
Chris Wilson85fb3e52010-01-06 15:39:49 +0000176 if (!res.encoder_id_ptr)
177 goto err_allocs;
178 }
Jesse Barnes731cd552008-12-17 10:09:49 -0800179
Chris Wilsond1308f42010-01-06 15:19:25 +0000180 if (drmIoctl(fd, DRM_IOCTL_MODE_GETRESOURCES, &res))
Jesse Barnes731cd552008-12-17 10:09:49 -0800181 goto err_allocs;
Chris Wilsond1308f42010-01-06 15:19:25 +0000182
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;
Jesse Barnes731cd552008-12-17 10:09:49 -0800198 }
199
200 /*
201 * return
202 */
Jesse Barnes731cd552008-12-17 10:09:49 -0800203 if (!(r = drmMalloc(sizeof(*r))))
Chris Wilsond1308f42010-01-06 15:19:25 +0000204 goto err_allocs;
Jesse Barnes731cd552008-12-17 10:09:49 -0800205
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;
Chris Wilson85fb3e52010-01-06 15:39:49 +0000214
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));
Chris Wilsone6c136c2010-01-06 16:53:33 +0000219 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 {
Chris Wilson85fb3e52010-01-06 15:39:49 +0000224 drmFree(r->fbs);
225 drmFree(r->crtcs);
226 drmFree(r->connectors);
227 drmFree(r->encoders);
228 drmFree(r);
229 r = 0;
230 }
Jesse Barnes731cd552008-12-17 10:09:49 -0800231
232err_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
241int 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
Chris Wilsonb8039182010-07-01 22:38:54 +0100255 if ((ret = DRM_IOCTL(fd, DRM_IOCTL_MODE_ADDFB, &f)))
Jesse Barnes731cd552008-12-17 10:09:49 -0800256 return ret;
257
258 *buf_id = f.fb_id;
259 return 0;
260}
261
Jesse Barnesac168bf2011-04-29 08:53:53 -0700262int 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;
Ville Syrjälä76b4a692012-02-02 14:53:43 -0500274 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]));
Jesse Barnesac168bf2011-04-29 08:53:53 -0700277
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
Jesse Barnes731cd552008-12-17 10:09:49 -0800285int drmModeRmFB(int fd, uint32_t bufferId)
286{
Chris Wilsonb8039182010-07-01 22:38:54 +0100287 return DRM_IOCTL(fd, DRM_IOCTL_MODE_RMFB, &bufferId);
Jesse Barnes731cd552008-12-17 10:09:49 -0800288
289
290}
291
292drmModeFBPtr 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
Jakob Bornecrantz3e486132009-11-24 18:00:12 +0100316int 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
Chris Wilsonb8039182010-07-01 22:38:54 +0100325 return DRM_IOCTL(fd, DRM_IOCTL_MODE_DIRTYFB, &dirty);
Jakob Bornecrantz3e486132009-11-24 18:00:12 +0100326}
327
Jesse Barnes731cd552008-12-17 10:09:49 -0800328
329/*
330 * Crtc functions
331 */
332
333drmModeCrtcPtr 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;
Rob Clarke81acf52012-10-14 16:55:32 -0500354 if (r->mode_valid) {
Jesse Barnes731cd552008-12-17 10:09:49 -0800355 memcpy(&r->mode, &crtc.mode, sizeof(struct drm_mode_modeinfo));
Rob Clarke81acf52012-10-14 16:55:32 -0500356 r->width = crtc.mode.hdisplay;
357 r->height = crtc.mode.vdisplay;
358 }
Jesse Barnes731cd552008-12-17 10:09:49 -0800359 r->buffer_id = crtc.fb_id;
360 r->gamma_size = crtc.gamma_size;
361 return r;
362}
363
364
365int drmModeSetCrtc(int fd, uint32_t crtcId, uint32_t bufferId,
366 uint32_t x, uint32_t y, uint32_t *connectors, int count,
Jakob Bornecrantzeb78c532009-02-11 16:43:20 +0100367 drmModeModeInfoPtr mode)
Jesse Barnes731cd552008-12-17 10:09:49 -0800368{
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
Chris Wilsonb8039182010-07-01 22:38:54 +0100383 return DRM_IOCTL(fd, DRM_IOCTL_MODE_SETCRTC, &crtc);
Jesse Barnes731cd552008-12-17 10:09:49 -0800384}
385
386/*
387 * Cursor manipulation
388 */
389
390int 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
Chris Wilsonb8039182010-07-01 22:38:54 +0100400 return DRM_IOCTL(fd, DRM_IOCTL_MODE_CURSOR, &arg);
Jesse Barnes731cd552008-12-17 10:09:49 -0800401}
402
403int drmModeMoveCursor(int fd, uint32_t crtcId, int x, int y)
404{
405 struct drm_mode_cursor arg;
406
407 arg.flags = DRM_MODE_CURSOR_MOVE;
408 arg.crtc_id = crtcId;
409 arg.x = x;
410 arg.y = y;
411
Chris Wilsonb8039182010-07-01 22:38:54 +0100412 return DRM_IOCTL(fd, DRM_IOCTL_MODE_CURSOR, &arg);
Jesse Barnes731cd552008-12-17 10:09:49 -0800413}
414
415/*
416 * Encoder get
417 */
418drmModeEncoderPtr drmModeGetEncoder(int fd, uint32_t encoder_id)
419{
420 struct drm_mode_get_encoder enc;
421 drmModeEncoderPtr r = NULL;
422
423 enc.encoder_id = encoder_id;
424 enc.encoder_type = 0;
425 enc.possible_crtcs = 0;
426 enc.possible_clones = 0;
427
428 if (drmIoctl(fd, DRM_IOCTL_MODE_GETENCODER, &enc))
429 return 0;
430
431 if (!(r = drmMalloc(sizeof(*r))))
432 return 0;
433
434 r->encoder_id = enc.encoder_id;
435 r->crtc_id = enc.crtc_id;
436 r->encoder_type = enc.encoder_type;
437 r->possible_crtcs = enc.possible_crtcs;
438 r->possible_clones = enc.possible_clones;
439
440 return r;
441}
442
443/*
444 * Connector manipulation
445 */
446
447drmModeConnectorPtr drmModeGetConnector(int fd, uint32_t connector_id)
448{
Peter Clifton04f90a42010-01-06 20:44:11 +0000449 struct drm_mode_get_connector conn, counts;
Jesse Barnes731cd552008-12-17 10:09:49 -0800450 drmModeConnectorPtr r = NULL;
451
Peter Clifton04f90a42010-01-06 20:44:11 +0000452retry:
453 memset(&conn, 0, sizeof(struct drm_mode_get_connector));
Jesse Barnes731cd552008-12-17 10:09:49 -0800454 conn.connector_id = connector_id;
Jesse Barnes731cd552008-12-17 10:09:49 -0800455
456 if (drmIoctl(fd, DRM_IOCTL_MODE_GETCONNECTOR, &conn))
457 return 0;
458
Peter Clifton04f90a42010-01-06 20:44:11 +0000459 counts = conn;
460
Jesse Barnes731cd552008-12-17 10:09:49 -0800461 if (conn.count_props) {
462 conn.props_ptr = VOID2U64(drmMalloc(conn.count_props*sizeof(uint32_t)));
Peter Clifton04f90a42010-01-06 20:44:11 +0000463 if (!conn.props_ptr)
464 goto err_allocs;
Jesse Barnes731cd552008-12-17 10:09:49 -0800465 conn.prop_values_ptr = VOID2U64(drmMalloc(conn.count_props*sizeof(uint64_t)));
Peter Clifton04f90a42010-01-06 20:44:11 +0000466 if (!conn.prop_values_ptr)
467 goto err_allocs;
Jesse Barnes731cd552008-12-17 10:09:49 -0800468 }
469
Peter Clifton04f90a42010-01-06 20:44:11 +0000470 if (conn.count_modes) {
Jesse Barnes731cd552008-12-17 10:09:49 -0800471 conn.modes_ptr = VOID2U64(drmMalloc(conn.count_modes*sizeof(struct drm_mode_modeinfo)));
Peter Clifton04f90a42010-01-06 20:44:11 +0000472 if (!conn.modes_ptr)
473 goto err_allocs;
474 }
Jesse Barnes731cd552008-12-17 10:09:49 -0800475
Peter Clifton04f90a42010-01-06 20:44:11 +0000476 if (conn.count_encoders) {
Jesse Barnes731cd552008-12-17 10:09:49 -0800477 conn.encoders_ptr = VOID2U64(drmMalloc(conn.count_encoders*sizeof(uint32_t)));
Peter Clifton04f90a42010-01-06 20:44:11 +0000478 if (!conn.encoders_ptr)
479 goto err_allocs;
480 }
Jesse Barnes731cd552008-12-17 10:09:49 -0800481
482 if (drmIoctl(fd, DRM_IOCTL_MODE_GETCONNECTOR, &conn))
483 goto err_allocs;
484
Peter Clifton04f90a42010-01-06 20:44:11 +0000485 /* The number of available connectors and etc may have changed with a
486 * hotplug event in between the ioctls, in which case the field is
487 * silently ignored by the kernel.
488 */
489 if (counts.count_props < conn.count_props ||
490 counts.count_modes < conn.count_modes ||
491 counts.count_encoders < conn.count_encoders) {
492 drmFree(U642VOID(conn.props_ptr));
493 drmFree(U642VOID(conn.prop_values_ptr));
494 drmFree(U642VOID(conn.modes_ptr));
495 drmFree(U642VOID(conn.encoders_ptr));
496
497 goto retry;
498 }
499
Jesse Barnes731cd552008-12-17 10:09:49 -0800500 if(!(r = drmMalloc(sizeof(*r)))) {
501 goto err_allocs;
502 }
503
504 r->connector_id = conn.connector_id;
505 r->encoder_id = conn.encoder_id;
506 r->connection = conn.connection;
507 r->mmWidth = conn.mm_width;
508 r->mmHeight = conn.mm_height;
Dave Airlie412d3702009-04-22 20:25:40 +1000509 /* convert subpixel from kernel to userspace */
510 r->subpixel = conn.subpixel + 1;
Jesse Barnes731cd552008-12-17 10:09:49 -0800511 r->count_modes = conn.count_modes;
Jesse Barnes731cd552008-12-17 10:09:49 -0800512 r->count_props = conn.count_props;
513 r->props = drmAllocCpy(U642VOID(conn.props_ptr), conn.count_props, sizeof(uint32_t));
514 r->prop_values = drmAllocCpy(U642VOID(conn.prop_values_ptr), conn.count_props, sizeof(uint64_t));
515 r->modes = drmAllocCpy(U642VOID(conn.modes_ptr), conn.count_modes, sizeof(struct drm_mode_modeinfo));
516 r->count_encoders = conn.count_encoders;
517 r->encoders = drmAllocCpy(U642VOID(conn.encoders_ptr), conn.count_encoders, sizeof(uint32_t));
518 r->connector_type = conn.connector_type;
519 r->connector_type_id = conn.connector_type_id;
520
Peter Clifton04f90a42010-01-06 20:44:11 +0000521 if ((r->count_props && !r->props) ||
522 (r->count_props && !r->prop_values) ||
523 (r->count_modes && !r->modes) ||
524 (r->count_encoders && !r->encoders)) {
525 drmFree(r->props);
526 drmFree(r->prop_values);
527 drmFree(r->modes);
528 drmFree(r->encoders);
529 drmFree(r);
530 r = 0;
531 }
Jesse Barnes731cd552008-12-17 10:09:49 -0800532
533err_allocs:
534 drmFree(U642VOID(conn.prop_values_ptr));
535 drmFree(U642VOID(conn.props_ptr));
536 drmFree(U642VOID(conn.modes_ptr));
537 drmFree(U642VOID(conn.encoders_ptr));
538
539 return r;
540}
541
Jakob Bornecrantzeb78c532009-02-11 16:43:20 +0100542int drmModeAttachMode(int fd, uint32_t connector_id, drmModeModeInfoPtr mode_info)
Jesse Barnes731cd552008-12-17 10:09:49 -0800543{
544 struct drm_mode_mode_cmd res;
545
546 memcpy(&res.mode, mode_info, sizeof(struct drm_mode_modeinfo));
547 res.connector_id = connector_id;
548
Chris Wilsonb8039182010-07-01 22:38:54 +0100549 return DRM_IOCTL(fd, DRM_IOCTL_MODE_ATTACHMODE, &res);
Jesse Barnes731cd552008-12-17 10:09:49 -0800550}
551
Jakob Bornecrantzeb78c532009-02-11 16:43:20 +0100552int drmModeDetachMode(int fd, uint32_t connector_id, drmModeModeInfoPtr mode_info)
Jesse Barnes731cd552008-12-17 10:09:49 -0800553{
554 struct drm_mode_mode_cmd res;
555
556 memcpy(&res.mode, mode_info, sizeof(struct drm_mode_modeinfo));
557 res.connector_id = connector_id;
558
Chris Wilsonb8039182010-07-01 22:38:54 +0100559 return DRM_IOCTL(fd, DRM_IOCTL_MODE_DETACHMODE, &res);
Jesse Barnes731cd552008-12-17 10:09:49 -0800560}
561
562
563drmModePropertyPtr drmModeGetProperty(int fd, uint32_t property_id)
564{
565 struct drm_mode_get_property prop;
566 drmModePropertyPtr r;
567
568 prop.prop_id = property_id;
569 prop.count_enum_blobs = 0;
570 prop.count_values = 0;
571 prop.flags = 0;
572 prop.enum_blob_ptr = 0;
573 prop.values_ptr = 0;
574
575 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPERTY, &prop))
576 return 0;
577
578 if (prop.count_values)
579 prop.values_ptr = VOID2U64(drmMalloc(prop.count_values * sizeof(uint64_t)));
580
Rob Clark7b228e92012-06-05 12:28:22 -0500581 if (prop.count_enum_blobs && (prop.flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)))
Jesse Barnes731cd552008-12-17 10:09:49 -0800582 prop.enum_blob_ptr = VOID2U64(drmMalloc(prop.count_enum_blobs * sizeof(struct drm_mode_property_enum)));
583
584 if (prop.count_enum_blobs && (prop.flags & DRM_MODE_PROP_BLOB)) {
585 prop.values_ptr = VOID2U64(drmMalloc(prop.count_enum_blobs * sizeof(uint32_t)));
586 prop.enum_blob_ptr = VOID2U64(drmMalloc(prop.count_enum_blobs * sizeof(uint32_t)));
587 }
588
589 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPERTY, &prop)) {
590 r = NULL;
591 goto err_allocs;
592 }
593
594 if (!(r = drmMalloc(sizeof(*r))))
595 return NULL;
596
597 r->prop_id = prop.prop_id;
598 r->count_values = prop.count_values;
599
600 r->flags = prop.flags;
601 if (prop.count_values)
602 r->values = drmAllocCpy(U642VOID(prop.values_ptr), prop.count_values, sizeof(uint64_t));
Rob Clark7b228e92012-06-05 12:28:22 -0500603 if (prop.flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
Jesse Barnes731cd552008-12-17 10:09:49 -0800604 r->count_enums = prop.count_enum_blobs;
605 r->enums = drmAllocCpy(U642VOID(prop.enum_blob_ptr), prop.count_enum_blobs, sizeof(struct drm_mode_property_enum));
606 } else if (prop.flags & DRM_MODE_PROP_BLOB) {
607 r->values = drmAllocCpy(U642VOID(prop.values_ptr), prop.count_enum_blobs, sizeof(uint32_t));
608 r->blob_ids = drmAllocCpy(U642VOID(prop.enum_blob_ptr), prop.count_enum_blobs, sizeof(uint32_t));
609 r->count_blobs = prop.count_enum_blobs;
610 }
611 strncpy(r->name, prop.name, DRM_PROP_NAME_LEN);
612 r->name[DRM_PROP_NAME_LEN-1] = 0;
613
614err_allocs:
615 drmFree(U642VOID(prop.values_ptr));
616 drmFree(U642VOID(prop.enum_blob_ptr));
617
618 return r;
619}
620
621void drmModeFreeProperty(drmModePropertyPtr ptr)
622{
623 if (!ptr)
624 return;
625
626 drmFree(ptr->values);
627 drmFree(ptr->enums);
628 drmFree(ptr);
629}
630
631drmModePropertyBlobPtr drmModeGetPropertyBlob(int fd, uint32_t blob_id)
632{
633 struct drm_mode_get_blob blob;
634 drmModePropertyBlobPtr r;
635
636 blob.length = 0;
637 blob.data = 0;
638 blob.blob_id = blob_id;
639
640 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPBLOB, &blob))
641 return NULL;
642
643 if (blob.length)
644 blob.data = VOID2U64(drmMalloc(blob.length));
645
646 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPBLOB, &blob)) {
647 r = NULL;
648 goto err_allocs;
649 }
650
651 if (!(r = drmMalloc(sizeof(*r))))
Chris Wilson8a762442010-08-24 21:29:31 +0100652 goto err_allocs;
Jesse Barnes731cd552008-12-17 10:09:49 -0800653
654 r->id = blob.blob_id;
655 r->length = blob.length;
656 r->data = drmAllocCpy(U642VOID(blob.data), 1, blob.length);
657
658err_allocs:
659 drmFree(U642VOID(blob.data));
660 return r;
661}
662
663void drmModeFreePropertyBlob(drmModePropertyBlobPtr ptr)
664{
665 if (!ptr)
666 return;
667
668 drmFree(ptr->data);
669 drmFree(ptr);
670}
671
672int drmModeConnectorSetProperty(int fd, uint32_t connector_id, uint32_t property_id,
673 uint64_t value)
674{
675 struct drm_mode_connector_set_property osp;
Jesse Barnes731cd552008-12-17 10:09:49 -0800676
677 osp.connector_id = connector_id;
678 osp.prop_id = property_id;
679 osp.value = value;
680
Chris Wilsonb8039182010-07-01 22:38:54 +0100681 return DRM_IOCTL(fd, DRM_IOCTL_MODE_SETPROPERTY, &osp);
Jesse Barnes731cd552008-12-17 10:09:49 -0800682}
683
684/*
685 * checks if a modesetting capable driver has attached to the pci id
686 * returns 0 if modesetting supported.
687 * -EINVAL or invalid bus id
688 * -ENOSYS if no modesetting support
689*/
690int drmCheckModesettingSupported(const char *busid)
691{
692#ifdef __linux__
693 char pci_dev_dir[1024];
694 int domain, bus, dev, func;
695 DIR *sysdir;
696 struct dirent *dent;
697 int found = 0, ret;
698
699 ret = sscanf(busid, "pci:%04x:%02x:%02x.%d", &domain, &bus, &dev, &func);
700 if (ret != 4)
701 return -EINVAL;
702
703 sprintf(pci_dev_dir, "/sys/bus/pci/devices/%04x:%02x:%02x.%d/drm",
704 domain, bus, dev, func);
705
706 sysdir = opendir(pci_dev_dir);
707 if (sysdir) {
708 dent = readdir(sysdir);
709 while (dent) {
710 if (!strncmp(dent->d_name, "controlD", 8)) {
711 found = 1;
712 break;
713 }
714
715 dent = readdir(sysdir);
716 }
717 closedir(sysdir);
718 if (found)
719 return 0;
720 }
721
722 sprintf(pci_dev_dir, "/sys/bus/pci/devices/%04x:%02x:%02x.%d/",
723 domain, bus, dev, func);
724
725 sysdir = opendir(pci_dev_dir);
726 if (!sysdir)
727 return -EINVAL;
728
729 dent = readdir(sysdir);
730 while (dent) {
731 if (!strncmp(dent->d_name, "drm:controlD", 12)) {
732 found = 1;
733 break;
734 }
735
736 dent = readdir(sysdir);
737 }
738
739 closedir(sysdir);
740 if (found)
741 return 0;
742#endif
743 return -ENOSYS;
744
745}
746
Jesse Barnes731cd552008-12-17 10:09:49 -0800747int drmModeCrtcGetGamma(int fd, uint32_t crtc_id, uint32_t size,
748 uint16_t *red, uint16_t *green, uint16_t *blue)
749{
Jesse Barnes731cd552008-12-17 10:09:49 -0800750 struct drm_mode_crtc_lut l;
751
752 l.crtc_id = crtc_id;
753 l.gamma_size = size;
754 l.red = VOID2U64(red);
755 l.green = VOID2U64(green);
756 l.blue = VOID2U64(blue);
757
Chris Wilsonb8039182010-07-01 22:38:54 +0100758 return DRM_IOCTL(fd, DRM_IOCTL_MODE_GETGAMMA, &l);
Jesse Barnes731cd552008-12-17 10:09:49 -0800759}
760
761int drmModeCrtcSetGamma(int fd, uint32_t crtc_id, uint32_t size,
762 uint16_t *red, uint16_t *green, uint16_t *blue)
763{
Jesse Barnes731cd552008-12-17 10:09:49 -0800764 struct drm_mode_crtc_lut l;
765
766 l.crtc_id = crtc_id;
767 l.gamma_size = size;
768 l.red = VOID2U64(red);
769 l.green = VOID2U64(green);
770 l.blue = VOID2U64(blue);
771
Chris Wilsonb8039182010-07-01 22:38:54 +0100772 return DRM_IOCTL(fd, DRM_IOCTL_MODE_SETGAMMA, &l);
Jesse Barnes731cd552008-12-17 10:09:49 -0800773}
Kristian Høgsbergb0b96632009-09-11 13:27:35 -0400774
775int drmHandleEvent(int fd, drmEventContextPtr evctx)
776{
777 char buffer[1024];
778 int len, i;
779 struct drm_event *e;
780 struct drm_event_vblank *vblank;
781
782 /* The DRM read semantics guarantees that we always get only
783 * complete events. */
784
785 len = read(fd, buffer, sizeof buffer);
786 if (len == 0)
787 return 0;
788 if (len < sizeof *e)
789 return -1;
790
791 i = 0;
792 while (i < len) {
793 e = (struct drm_event *) &buffer[i];
794 switch (e->type) {
795 case DRM_EVENT_VBLANK:
796 if (evctx->version < 1 ||
797 evctx->vblank_handler == NULL)
798 break;
799 vblank = (struct drm_event_vblank *) e;
800 evctx->vblank_handler(fd,
801 vblank->sequence,
802 vblank->tv_sec,
803 vblank->tv_usec,
804 U642VOID (vblank->user_data));
805 break;
Kristian Høgsbergb80bcff2009-11-12 14:06:45 -0500806 case DRM_EVENT_FLIP_COMPLETE:
Jesse Barnes14f59582009-12-03 14:20:51 -0800807 if (evctx->version < 2 ||
Kristian Høgsbergb80bcff2009-11-12 14:06:45 -0500808 evctx->page_flip_handler == NULL)
809 break;
810 vblank = (struct drm_event_vblank *) e;
811 evctx->page_flip_handler(fd,
812 vblank->sequence,
813 vblank->tv_sec,
814 vblank->tv_usec,
815 U642VOID (vblank->user_data));
816 break;
Kristian Høgsbergb0b96632009-09-11 13:27:35 -0400817 default:
818 break;
819 }
820 i += e->length;
821 }
822
823 return 0;
824}
825
Kristian Høgsbergb80bcff2009-11-12 14:06:45 -0500826int drmModePageFlip(int fd, uint32_t crtc_id, uint32_t fb_id,
827 uint32_t flags, void *user_data)
828{
829 struct drm_mode_crtc_page_flip flip;
830
831 flip.fb_id = fb_id;
832 flip.crtc_id = crtc_id;
833 flip.user_data = VOID2U64(user_data);
834 flip.flags = flags;
835 flip.reserved = 0;
836
Chris Wilsonb8039182010-07-01 22:38:54 +0100837 return DRM_IOCTL(fd, DRM_IOCTL_MODE_PAGE_FLIP, &flip);
Kristian Høgsbergb80bcff2009-11-12 14:06:45 -0500838}
Jesse Barnesac168bf2011-04-29 08:53:53 -0700839
840int drmModeSetPlane(int fd, uint32_t plane_id, uint32_t crtc_id,
841 uint32_t fb_id, uint32_t flags,
842 uint32_t crtc_x, uint32_t crtc_y,
843 uint32_t crtc_w, uint32_t crtc_h,
844 uint32_t src_x, uint32_t src_y,
845 uint32_t src_w, uint32_t src_h)
846
847{
848 struct drm_mode_set_plane s;
849
850 s.plane_id = plane_id;
851 s.crtc_id = crtc_id;
852 s.fb_id = fb_id;
853 s.flags = flags;
854 s.crtc_x = crtc_x;
855 s.crtc_y = crtc_y;
856 s.crtc_w = crtc_w;
857 s.crtc_h = crtc_h;
858 s.src_x = src_x;
859 s.src_y = src_y;
860 s.src_w = src_w;
861 s.src_h = src_h;
862
863 return DRM_IOCTL(fd, DRM_IOCTL_MODE_SETPLANE, &s);
864}
865
866
867drmModePlanePtr drmModeGetPlane(int fd, uint32_t plane_id)
868{
869 struct drm_mode_get_plane ovr, counts;
870 drmModePlanePtr r = 0;
871
872retry:
873 memset(&ovr, 0, sizeof(struct drm_mode_get_plane));
874 ovr.plane_id = plane_id;
875 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPLANE, &ovr))
876 return 0;
877
878 counts = ovr;
879
880 if (ovr.count_format_types) {
881 ovr.format_type_ptr = VOID2U64(drmMalloc(ovr.count_format_types *
882 sizeof(uint32_t)));
883 if (!ovr.format_type_ptr)
884 goto err_allocs;
885 }
886
887 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPLANE, &ovr))
888 goto err_allocs;
889
890 if (counts.count_format_types < ovr.count_format_types) {
891 drmFree(U642VOID(ovr.format_type_ptr));
892 goto retry;
893 }
894
895 if (!(r = drmMalloc(sizeof(*r))))
896 goto err_allocs;
897
898 r->count_formats = ovr.count_format_types;
899 r->plane_id = ovr.plane_id;
900 r->crtc_id = ovr.crtc_id;
901 r->fb_id = ovr.fb_id;
902 r->possible_crtcs = ovr.possible_crtcs;
903 r->gamma_size = ovr.gamma_size;
904 r->formats = drmAllocCpy(U642VOID(ovr.format_type_ptr),
905 ovr.count_format_types, sizeof(uint32_t));
906 if (ovr.count_format_types && !r->formats) {
907 drmFree(r->formats);
Ville Syrjälädf497e92012-02-02 14:53:39 -0500908 drmFree(r);
Jesse Barnesac168bf2011-04-29 08:53:53 -0700909 r = 0;
910 }
911
912err_allocs:
913 drmFree(U642VOID(ovr.format_type_ptr));
914
915 return r;
916}
917
918void drmModeFreePlane(drmModePlanePtr ptr)
919{
920 if (!ptr)
921 return;
922
923 drmFree(ptr->formats);
924 drmFree(ptr);
925}
926
927drmModePlaneResPtr drmModeGetPlaneResources(int fd)
928{
929 struct drm_mode_get_plane_res res, counts;
930 drmModePlaneResPtr r = 0;
931
932retry:
933 memset(&res, 0, sizeof(struct drm_mode_get_plane_res));
934 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPLANERESOURCES, &res))
935 return 0;
936
937 counts = res;
938
939 if (res.count_planes) {
940 res.plane_id_ptr = VOID2U64(drmMalloc(res.count_planes *
941 sizeof(uint32_t)));
942 if (!res.plane_id_ptr)
943 goto err_allocs;
944 }
945
946 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPLANERESOURCES, &res))
947 goto err_allocs;
948
949 if (counts.count_planes < res.count_planes) {
950 drmFree(U642VOID(res.plane_id_ptr));
951 goto retry;
952 }
953
954 if (!(r = drmMalloc(sizeof(*r))))
955 goto err_allocs;
956
957 r->count_planes = res.count_planes;
958 r->planes = drmAllocCpy(U642VOID(res.plane_id_ptr),
959 res.count_planes, sizeof(uint32_t));
960 if (res.count_planes && !r->planes) {
961 drmFree(r->planes);
Ville Syrjälädf497e92012-02-02 14:53:39 -0500962 drmFree(r);
Jesse Barnesac168bf2011-04-29 08:53:53 -0700963 r = 0;
964 }
965
966err_allocs:
967 drmFree(U642VOID(res.plane_id_ptr));
968
969 return r;
970}
Ville Syrjäläa14c3dd2012-02-02 14:53:41 -0500971
972void drmModeFreePlaneResources(drmModePlaneResPtr ptr)
973{
974 if (!ptr)
975 return;
976
977 drmFree(ptr->planes);
978 drmFree(ptr);
979}
Paulo Zanoni8c757032012-05-15 18:38:28 -0300980
981drmModeObjectPropertiesPtr drmModeObjectGetProperties(int fd,
982 uint32_t object_id,
983 uint32_t object_type)
984{
985 struct drm_mode_obj_get_properties properties;
986 drmModeObjectPropertiesPtr ret = NULL;
987 uint32_t count;
988
989retry:
990 memset(&properties, 0, sizeof(struct drm_mode_obj_get_properties));
991 properties.obj_id = object_id;
992 properties.obj_type = object_type;
993
994 if (drmIoctl(fd, DRM_IOCTL_MODE_OBJ_GETPROPERTIES, &properties))
995 return 0;
996
997 count = properties.count_props;
998
999 if (count) {
1000 properties.props_ptr = VOID2U64(drmMalloc(count *
1001 sizeof(uint32_t)));
1002 if (!properties.props_ptr)
1003 goto err_allocs;
1004 properties.prop_values_ptr = VOID2U64(drmMalloc(count *
1005 sizeof(uint64_t)));
1006 if (!properties.prop_values_ptr)
1007 goto err_allocs;
1008 }
1009
1010 if (drmIoctl(fd, DRM_IOCTL_MODE_OBJ_GETPROPERTIES, &properties))
1011 goto err_allocs;
1012
1013 if (count < properties.count_props) {
1014 drmFree(U642VOID(properties.props_ptr));
1015 drmFree(U642VOID(properties.prop_values_ptr));
1016 goto retry;
1017 }
1018 count = properties.count_props;
1019
1020 ret = drmMalloc(sizeof(*ret));
1021 if (!ret)
1022 goto err_allocs;
1023
1024 ret->count_props = count;
1025 ret->props = drmAllocCpy(U642VOID(properties.props_ptr),
1026 count, sizeof(uint32_t));
1027 ret->prop_values = drmAllocCpy(U642VOID(properties.prop_values_ptr),
1028 count, sizeof(uint64_t));
1029 if (ret->count_props && (!ret->props || !ret->prop_values)) {
1030 drmFree(ret->props);
1031 drmFree(ret->prop_values);
1032 drmFree(ret);
1033 ret = NULL;
1034 }
1035
1036err_allocs:
1037 drmFree(U642VOID(properties.props_ptr));
1038 drmFree(U642VOID(properties.prop_values_ptr));
1039 return ret;
1040}
1041
1042void drmModeFreeObjectProperties(drmModeObjectPropertiesPtr ptr)
1043{
1044 if (!ptr)
1045 return;
1046 drmFree(ptr->props);
1047 drmFree(ptr->prop_values);
1048 drmFree(ptr);
1049}
1050
1051int drmModeObjectSetProperty(int fd, uint32_t object_id, uint32_t object_type,
1052 uint32_t property_id, uint64_t value)
1053{
1054 struct drm_mode_obj_set_property prop;
1055
1056 prop.value = value;
1057 prop.prop_id = property_id;
1058 prop.obj_id = object_id;
1059 prop.obj_type = object_type;
1060
1061 return DRM_IOCTL(fd, DRM_IOCTL_MODE_OBJ_SETPROPERTY, &prop);
1062}