3 * Header for DRM modesetting interface.
5 * \author Jakob Bornecrantz <wallbraker@gmail.com>
7 * \par Acknowledgements:
8 * Feb 2007, Dave Airlie <airlied@linux.ie>
12 * Copyright (c) <year> <copyright holders>
14 * Permission is hereby granted, free of charge, to any person obtaining a
15 * copy of this software and associated documentation files (the "Software"),
16 * to deal in the Software without restriction, including without limitation
17 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
18 * and/or sell copies of the Software, and to permit persons to whom the
19 * Software is furnished to do so, subject to the following conditions:
21 * The above copyright notice and this permission notice shall be included in
22 * all copies or substantial portions of the Software.
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
29 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
35 * TODO the types we are after are defined in diffrent headers on diffrent
36 * platforms find which headers to include to get uint32_t
39 #include <sys/ioctl.h>
42 #include "xf86drmMode.h"
49 #define U642VOID(x) ((void *)(unsigned long)(x))
50 #define VOID2U64(x) ((uint64_t)(unsigned long)(x))
56 void* drmAllocCpy(void *array, int count, int entry_size)
61 if (!count || !array || !entry_size)
64 if (!(r = drmMalloc(count*entry_size)))
67 for (i = 0; i < count; i++)
68 memcpy(r+(entry_size*i), array+(entry_size*i), entry_size);
74 * A couple of free functions.
77 void drmModeFreeModeInfo(struct drm_mode_modeinfo *ptr)
85 void drmModeFreeResources(drmModeResPtr ptr)
94 void drmModeFreeFB(drmModeFBPtr ptr)
99 /* we might add more frees later. */
103 void drmModeFreeCrtc(drmModeCrtcPtr ptr)
112 void drmModeFreeOutput(drmModeOutputPtr ptr)
123 * ModeSetting functions.
126 drmModeResPtr drmModeGetResources(int fd)
128 struct drm_mode_card_res res;
131 memset(&res, 0, sizeof(struct drm_mode_card_res));
133 if (ioctl(fd, DRM_IOCTL_MODE_GETRESOURCES, &res))
137 res.fb_id_ptr = VOID2U64(drmMalloc(res.count_fbs*sizeof(uint32_t)));
139 res.crtc_id_ptr = VOID2U64(drmMalloc(res.count_crtcs*sizeof(uint32_t)));
140 if (res.count_outputs)
141 res.output_id_ptr = VOID2U64(drmMalloc(res.count_outputs*sizeof(uint32_t)));
143 if (ioctl(fd, DRM_IOCTL_MODE_GETRESOURCES, &res)) {
153 if (!(r = drmMalloc(sizeof(*r))))
156 r->min_width = res.min_width;
157 r->max_width = res.max_width;
158 r->min_height = res.min_height;
159 r->max_height = res.max_height;
160 r->count_fbs = res.count_fbs;
161 r->count_crtcs = res.count_crtcs;
162 r->count_outputs = res.count_outputs;
163 /* TODO we realy should test if these allocs fails. */
164 r->fbs = drmAllocCpy(U642VOID(res.fb_id_ptr), res.count_fbs, sizeof(uint32_t));
165 r->crtcs = drmAllocCpy(U642VOID(res.crtc_id_ptr), res.count_crtcs, sizeof(uint32_t));
166 r->outputs = drmAllocCpy(U642VOID(res.output_id_ptr), res.count_outputs, sizeof(uint32_t));
169 drmFree(U642VOID(res.fb_id_ptr));
170 drmFree(U642VOID(res.crtc_id_ptr));
171 drmFree(U642VOID(res.output_id_ptr));
176 uint32_t drmModeGetHotplug(int fd)
178 struct drm_mode_hotplug arg;
181 ioctl(fd, DRM_IOCTL_MODE_HOTPLUG, &arg);
185 int drmModeAddFB(int fd, uint32_t width, uint32_t height, uint8_t depth,
186 uint8_t bpp, uint32_t pitch, uint32_t bo_handle,
189 struct drm_mode_fb_cmd f;
197 f.handle = bo_handle;
199 if ((ret = ioctl(fd, DRM_IOCTL_MODE_ADDFB, &f)))
202 *buf_id = f.buffer_id;
206 int drmModeRmFB(int fd, uint32_t bufferId)
208 return ioctl(fd, DRM_IOCTL_MODE_RMFB, &bufferId);
213 drmModeFBPtr drmModeGetFB(int fd, uint32_t buf)
215 struct drm_mode_fb_cmd info;
218 info.buffer_id = buf;
220 if (ioctl(fd, DRM_IOCTL_MODE_GETFB, &info))
223 if (!(r = drmMalloc(sizeof(*r))))
226 r->buffer_id = info.buffer_id;
227 r->width = info.width;
228 r->height = info.height;
229 r->pitch = info.pitch;
231 r->handle = info.handle;
232 r->depth = info.depth;
242 drmModeCrtcPtr drmModeGetCrtc(int fd, uint32_t crtcId)
244 struct drm_mode_crtc crtc;
247 crtc.count_outputs = 0;
249 crtc.count_possibles = 0;
251 crtc.crtc_id = crtcId;
253 if (ioctl(fd, DRM_IOCTL_MODE_GETCRTC, &crtc))
260 if (!(r = drmMalloc(sizeof(*r))))
263 r->crtc_id = crtc.crtc_id;
266 r->mode_valid = crtc.mode_valid;
268 memcpy(&r->mode, &crtc.mode, sizeof(struct drm_mode_modeinfo));
269 r->buffer_id = crtc.fb_id;
270 r->gamma_size = crtc.gamma_size;
271 r->count_outputs = crtc.count_outputs;
272 r->count_possibles = crtc.count_possibles;
273 /* TODO we realy should test if these alloc & cpy fails. */
274 r->outputs = crtc.outputs;
275 r->possibles = crtc.possibles;
281 int drmModeSetCrtc(int fd, uint32_t crtcId, uint32_t bufferId,
282 uint32_t x, uint32_t y, uint32_t *outputs, int count,
283 struct drm_mode_modeinfo *mode)
285 struct drm_mode_crtc crtc;
287 crtc.count_outputs = 0;
289 crtc.count_possibles = 0;
294 crtc.crtc_id = crtcId;
295 crtc.fb_id = bufferId;
296 crtc.set_outputs_ptr = VOID2U64(outputs);
297 crtc.count_outputs = count;
299 memcpy(&crtc.mode, mode, sizeof(struct drm_mode_modeinfo));
304 return ioctl(fd, DRM_IOCTL_MODE_SETCRTC, &crtc);
308 * Cursor manipulation
311 int drmModeSetCursor(int fd, uint32_t crtcId, drmBO *bo, uint32_t width, uint32_t height)
313 struct drm_mode_cursor arg;
315 arg.flags = DRM_MODE_CURSOR_BO;
320 arg.handle = bo->handle;
324 return ioctl(fd, DRM_IOCTL_MODE_CURSOR, &arg);
327 int drmModeMoveCursor(int fd, uint32_t crtcId, int x, int y)
329 struct drm_mode_cursor arg;
331 arg.flags = DRM_MODE_CURSOR_MOVE;
336 return ioctl(fd, DRM_IOCTL_MODE_CURSOR, &arg);
340 * Output manipulation
343 drmModeOutputPtr drmModeGetOutput(int fd, uint32_t output_id)
345 struct drm_mode_get_output out;
346 drmModeOutputPtr r = NULL;
348 out.output = output_id;
349 out.output_type_id = 0;
353 out.count_clones = 0;
359 out.prop_values_ptr = 0;
361 if (ioctl(fd, DRM_IOCTL_MODE_GETOUTPUT, &out))
364 if (out.count_props) {
365 out.props_ptr = VOID2U64(drmMalloc(out.count_props*sizeof(uint32_t)));
366 out.prop_values_ptr = VOID2U64(drmMalloc(out.count_props*sizeof(uint64_t)));
370 out.modes_ptr = VOID2U64(drmMalloc(out.count_modes*sizeof(struct drm_mode_modeinfo)));
372 if (ioctl(fd, DRM_IOCTL_MODE_GETOUTPUT, &out))
375 if(!(r = drmMalloc(sizeof(*r)))) {
379 r->output_id = out.output;
381 r->connection = out.connection;
382 r->mmWidth = out.mm_width;
383 r->mmHeight = out.mm_height;
384 r->subpixel = out.subpixel;
385 r->count_crtcs = out.count_crtcs;
386 r->count_clones = out.count_clones;
387 r->count_modes = out.count_modes;
388 /* TODO we should test if these alloc & cpy fails. */
389 r->crtcs = out.crtcs;
390 r->clones = out.clones;
391 r->count_props = out.count_props;
392 r->props = drmAllocCpy(U642VOID(out.props_ptr), out.count_props, sizeof(uint32_t));
393 r->prop_values = drmAllocCpy(U642VOID(out.prop_values_ptr), out.count_props, sizeof(uint64_t));
394 r->modes = drmAllocCpy(U642VOID(out.modes_ptr), out.count_modes, sizeof(struct drm_mode_modeinfo));
395 r->output_type = out.output_type;
396 r->output_type_id = out.output_type_id;
399 drmFree(U642VOID(out.prop_values_ptr));
400 drmFree(U642VOID(out.props_ptr));
401 drmFree(U642VOID(out.modes_ptr));
406 int drmModeAttachMode(int fd, uint32_t output_id, struct drm_mode_modeinfo *mode_info)
408 struct drm_mode_mode_cmd res;
410 memcpy(&res.mode, mode_info, sizeof(struct drm_mode_modeinfo));
411 res.output_id = output_id;
413 return ioctl(fd, DRM_IOCTL_MODE_ATTACHMODE, &res);
416 int drmModeDetachMode(int fd, uint32_t output_id, struct drm_mode_modeinfo *mode_info)
418 struct drm_mode_mode_cmd res;
420 memcpy(&res.mode, mode_info, sizeof(struct drm_mode_modeinfo));
421 res.output_id = output_id;
423 return ioctl(fd, DRM_IOCTL_MODE_DETACHMODE, &res);
427 drmModePropertyPtr drmModeGetProperty(int fd, uint32_t property_id)
429 struct drm_mode_get_property prop;
430 drmModePropertyPtr r;
432 prop.prop_id = property_id;
433 prop.count_enum_blobs = 0;
434 prop.count_values = 0;
436 prop.enum_blob_ptr = 0;
439 if (ioctl(fd, DRM_IOCTL_MODE_GETPROPERTY, &prop))
442 if (prop.count_values)
443 prop.values_ptr = VOID2U64(drmMalloc(prop.count_values * sizeof(uint64_t)));
445 if (prop.count_enum_blobs && (prop.flags & DRM_MODE_PROP_ENUM))
446 prop.enum_blob_ptr = VOID2U64(drmMalloc(prop.count_enum_blobs * sizeof(struct drm_mode_property_enum)));
448 if (prop.count_enum_blobs && (prop.flags & DRM_MODE_PROP_BLOB)) {
449 prop.values_ptr = VOID2U64(drmMalloc(prop.count_enum_blobs * sizeof(uint32_t)));
450 prop.enum_blob_ptr = VOID2U64(drmMalloc(prop.count_enum_blobs * sizeof(uint32_t)));
453 if (ioctl(fd, DRM_IOCTL_MODE_GETPROPERTY, &prop)) {
458 if (!(r = drmMalloc(sizeof(*r))))
461 r->prop_id = prop.prop_id;
462 r->count_values = prop.count_values;
464 r->flags = prop.flags;
465 if (prop.count_values)
466 r->values = drmAllocCpy(U642VOID(prop.values_ptr), prop.count_values, sizeof(uint64_t));
467 if (prop.flags & DRM_MODE_PROP_ENUM) {
468 r->count_enums = prop.count_enum_blobs;
469 r->enums = drmAllocCpy(U642VOID(prop.enum_blob_ptr), prop.count_enum_blobs, sizeof(struct drm_mode_property_enum));
470 } else if (prop.flags & DRM_MODE_PROP_ENUM) {
471 r->values = drmAllocCpy(U642VOID(prop.values_ptr), prop.count_enum_blobs, sizeof(uint32_t));
472 r->blob_ids = drmAllocCpy(U642VOID(prop.enum_blob_ptr), prop.count_enum_blobs, sizeof(uint32_t));
473 r->count_blobs = prop.count_enum_blobs;
475 strncpy(r->name, prop.name, DRM_PROP_NAME_LEN);
476 r->name[DRM_PROP_NAME_LEN-1] = 0;
479 drmFree(U642VOID(prop.values_ptr));
480 drmFree(U642VOID(prop.enum_blob_ptr));
485 void drmModeFreeProperty(drmModePropertyPtr ptr)
490 drmFree(ptr->values);
495 drmModePropertyBlobPtr drmModeGetPropertyBlob(int fd, uint32_t blob_id)
497 struct drm_mode_get_blob blob;
498 drmModePropertyBlobPtr r;
502 blob.blob_id = blob_id;
504 if (ioctl(fd, DRM_IOCTL_MODE_GETPROPBLOB, &blob))
508 blob.data = VOID2U64(drmMalloc(blob.length));
510 if (ioctl(fd, DRM_IOCTL_MODE_GETPROPBLOB, &blob)) {
515 if (!(r = drmMalloc(sizeof(*r))))
518 r->id = blob.blob_id;
519 r->length = blob.length;
520 r->data = drmAllocCpy(U642VOID(blob.data), 1, blob.length);
523 drmFree(U642VOID(blob.data));
527 void drmModeFreePropertyBlob(drmModePropertyBlobPtr ptr)
536 int drmModeOutputSetProperty(int fd, uint32_t output_id, uint32_t property_id,
539 struct drm_mode_output_set_property osp;
542 osp.output_id = output_id;
543 osp.prop_id = property_id;
546 if ((ret = ioctl(fd, DRM_IOCTL_MODE_SETPROPERTY, &osp)))
553 * checks if a modesetting capable driver has attached to the pci id
554 * returns 0 if modesetting supported.
555 * -EINVAL or invalid bus id
556 * -ENOSYS if no modesetting support
558 int drmCheckModesettingSupported(const char *busid)
561 char pci_dev_dir[1024];
562 int domain, bus, dev, func;
567 ret = sscanf(busid, "pci:%04x:%02x:%02x.%d", &domain, &bus, &dev, &func);
571 sprintf(pci_dev_dir, "/sys/bus/pci/devices/%04x:%02x:%02x.%d/drm",
572 domain, bus, dev, func);
574 sysdir = opendir(pci_dev_dir);
576 dent = readdir(sysdir);
578 if (!strncmp(dent->d_name, "controlD", 8)) {
583 dent = readdir(sysdir);
590 sprintf(pci_dev_dir, "/sys/bus/pci/devices/%04x:%02x:%02x.%d/",
591 domain, bus, dev, func);
593 sysdir = opendir(pci_dev_dir);
597 dent = readdir(sysdir);
599 if (!strncmp(dent->d_name, "drm:controlD", 12)) {
604 dent = readdir(sysdir);