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 drmModeFreeConnector(drmModeConnectorPtr ptr)
122 void drmModeFreeEncoder(drmModeEncoderPtr ptr)
128 * ModeSetting functions.
131 drmModeResPtr drmModeGetResources(int fd)
133 struct drm_mode_card_res res;
136 memset(&res, 0, sizeof(struct drm_mode_card_res));
138 if (ioctl(fd, DRM_IOCTL_MODE_GETRESOURCES, &res))
142 res.fb_id_ptr = VOID2U64(drmMalloc(res.count_fbs*sizeof(uint32_t)));
144 res.crtc_id_ptr = VOID2U64(drmMalloc(res.count_crtcs*sizeof(uint32_t)));
145 if (res.count_connectors)
146 res.connector_id_ptr = VOID2U64(drmMalloc(res.count_connectors*sizeof(uint32_t)));
147 if (res.count_encoders)
148 res.encoder_id_ptr = VOID2U64(drmMalloc(res.count_encoders*sizeof(uint32_t)));
150 if (ioctl(fd, DRM_IOCTL_MODE_GETRESOURCES, &res)) {
160 if (!(r = drmMalloc(sizeof(*r))))
163 r->min_width = res.min_width;
164 r->max_width = res.max_width;
165 r->min_height = res.min_height;
166 r->max_height = res.max_height;
167 r->count_fbs = res.count_fbs;
168 r->count_crtcs = res.count_crtcs;
169 r->count_connectors = res.count_connectors;
170 r->count_encoders = res.count_encoders;
171 /* TODO we realy should test if these allocs fails. */
172 r->fbs = drmAllocCpy(U642VOID(res.fb_id_ptr), res.count_fbs, sizeof(uint32_t));
173 r->crtcs = drmAllocCpy(U642VOID(res.crtc_id_ptr), res.count_crtcs, sizeof(uint32_t));
174 r->connectors = drmAllocCpy(U642VOID(res.connector_id_ptr), res.count_connectors, sizeof(uint32_t));
175 r->encoders = drmAllocCpy(U642VOID(res.encoder_id_ptr), res.count_encoders, sizeof(uint32_t));
178 drmFree(U642VOID(res.fb_id_ptr));
179 drmFree(U642VOID(res.crtc_id_ptr));
180 drmFree(U642VOID(res.connector_id_ptr));
181 drmFree(U642VOID(res.encoder_id_ptr));
186 int drmModeAddFB(int fd, uint32_t width, uint32_t height, uint8_t depth,
187 uint8_t bpp, uint32_t pitch, uint32_t bo_handle,
190 struct drm_mode_fb_cmd f;
198 f.handle = bo_handle;
200 if ((ret = ioctl(fd, DRM_IOCTL_MODE_ADDFB, &f)))
207 int drmModeRmFB(int fd, uint32_t bufferId)
209 return ioctl(fd, DRM_IOCTL_MODE_RMFB, &bufferId);
214 drmModeFBPtr drmModeGetFB(int fd, uint32_t buf)
216 struct drm_mode_fb_cmd info;
221 if (ioctl(fd, DRM_IOCTL_MODE_GETFB, &info))
224 if (!(r = drmMalloc(sizeof(*r))))
227 r->fb_id = info.fb_id;
228 r->width = info.width;
229 r->height = info.height;
230 r->pitch = info.pitch;
232 r->handle = info.handle;
233 r->depth = info.depth;
243 drmModeCrtcPtr drmModeGetCrtc(int fd, uint32_t crtcId)
245 struct drm_mode_crtc crtc;
248 crtc.crtc_id = crtcId;
250 if (ioctl(fd, DRM_IOCTL_MODE_GETCRTC, &crtc))
257 if (!(r = drmMalloc(sizeof(*r))))
260 r->crtc_id = crtc.crtc_id;
263 r->mode_valid = crtc.mode_valid;
265 memcpy(&r->mode, &crtc.mode, sizeof(struct drm_mode_modeinfo));
266 r->buffer_id = crtc.fb_id;
267 r->gamma_size = crtc.gamma_size;
272 int drmModeSetCrtc(int fd, uint32_t crtcId, uint32_t bufferId,
273 uint32_t x, uint32_t y, uint32_t *connectors, int count,
274 struct drm_mode_modeinfo *mode)
276 struct drm_mode_crtc crtc;
280 crtc.crtc_id = crtcId;
281 crtc.fb_id = bufferId;
282 crtc.set_connectors_ptr = VOID2U64(connectors);
283 crtc.count_connectors = count;
285 memcpy(&crtc.mode, mode, sizeof(struct drm_mode_modeinfo));
290 return ioctl(fd, DRM_IOCTL_MODE_SETCRTC, &crtc);
294 * Cursor manipulation
297 int drmModeSetCursor(int fd, uint32_t crtcId, uint32_t bo_handle, uint32_t width, uint32_t height)
299 struct drm_mode_cursor arg;
301 arg.flags = DRM_MODE_CURSOR_BO;
302 arg.crtc_id = crtcId;
305 arg.handle = bo_handle;
307 return ioctl(fd, DRM_IOCTL_MODE_CURSOR, &arg);
310 int drmModeMoveCursor(int fd, uint32_t crtcId, int x, int y)
312 struct drm_mode_cursor arg;
314 arg.flags = DRM_MODE_CURSOR_MOVE;
315 arg.crtc_id = crtcId;
319 return ioctl(fd, DRM_IOCTL_MODE_CURSOR, &arg);
325 drmModeEncoderPtr drmModeGetEncoder(int fd, uint32_t encoder_id)
327 struct drm_mode_get_encoder enc;
328 drmModeEncoderPtr r = NULL;
330 enc.encoder_id = encoder_id;
331 enc.encoder_type = 0;
332 enc.possible_crtcs = 0;
333 enc.possible_clones = 0;
335 if (ioctl(fd, DRM_IOCTL_MODE_GETENCODER, &enc))
338 if (!(r = drmMalloc(sizeof(*r))))
341 r->encoder_id = enc.encoder_id;
342 r->crtc_id = enc.crtc_id;
343 r->encoder_type = enc.encoder_type;
344 r->possible_crtcs = enc.possible_crtcs;
345 r->possible_clones = enc.possible_clones;
351 * Connector manipulation
354 drmModeConnectorPtr drmModeGetConnector(int fd, uint32_t connector_id)
356 struct drm_mode_get_connector conn;
357 drmModeConnectorPtr r = NULL;
359 conn.connector_id = connector_id;
360 conn.connector_type_id = 0;
361 conn.connector_type = 0;
362 conn.count_modes = 0;
364 conn.count_props = 0;
366 conn.prop_values_ptr = 0;
367 conn.count_encoders = 0;
368 conn.encoders_ptr = 0;
370 if (ioctl(fd, DRM_IOCTL_MODE_GETCONNECTOR, &conn))
373 if (conn.count_props) {
374 conn.props_ptr = VOID2U64(drmMalloc(conn.count_props*sizeof(uint32_t)));
375 conn.prop_values_ptr = VOID2U64(drmMalloc(conn.count_props*sizeof(uint64_t)));
378 if (conn.count_modes)
379 conn.modes_ptr = VOID2U64(drmMalloc(conn.count_modes*sizeof(struct drm_mode_modeinfo)));
381 if (conn.count_encoders)
382 conn.encoders_ptr = VOID2U64(drmMalloc(conn.count_encoders*sizeof(uint32_t)));
384 if (ioctl(fd, DRM_IOCTL_MODE_GETCONNECTOR, &conn))
387 if(!(r = drmMalloc(sizeof(*r)))) {
391 r->connector_id = conn.connector_id;
392 r->encoder_id = conn.encoder_id;
393 r->connection = conn.connection;
394 r->mmWidth = conn.mm_width;
395 r->mmHeight = conn.mm_height;
396 r->subpixel = conn.subpixel;
397 r->count_modes = conn.count_modes;
398 /* TODO we should test if these alloc & cpy fails. */
399 r->count_props = conn.count_props;
400 r->props = drmAllocCpy(U642VOID(conn.props_ptr), conn.count_props, sizeof(uint32_t));
401 r->prop_values = drmAllocCpy(U642VOID(conn.prop_values_ptr), conn.count_props, sizeof(uint64_t));
402 r->modes = drmAllocCpy(U642VOID(conn.modes_ptr), conn.count_modes, sizeof(struct drm_mode_modeinfo));
403 r->count_encoders = conn.count_encoders;
404 r->encoders = drmAllocCpy(U642VOID(conn.encoders_ptr), conn.count_encoders, sizeof(uint32_t));
405 r->connector_type = conn.connector_type;
406 r->connector_type_id = conn.connector_type_id;
408 if (!r->props || !r->prop_values || !r->modes || !r->encoders)
412 drmFree(U642VOID(conn.prop_values_ptr));
413 drmFree(U642VOID(conn.props_ptr));
414 drmFree(U642VOID(conn.modes_ptr));
415 drmFree(U642VOID(conn.encoders_ptr));
420 int drmModeAttachMode(int fd, uint32_t connector_id, struct drm_mode_modeinfo *mode_info)
422 struct drm_mode_mode_cmd res;
424 memcpy(&res.mode, mode_info, sizeof(struct drm_mode_modeinfo));
425 res.connector_id = connector_id;
427 return ioctl(fd, DRM_IOCTL_MODE_ATTACHMODE, &res);
430 int drmModeDetachMode(int fd, uint32_t connector_id, struct drm_mode_modeinfo *mode_info)
432 struct drm_mode_mode_cmd res;
434 memcpy(&res.mode, mode_info, sizeof(struct drm_mode_modeinfo));
435 res.connector_id = connector_id;
437 return ioctl(fd, DRM_IOCTL_MODE_DETACHMODE, &res);
441 drmModePropertyPtr drmModeGetProperty(int fd, uint32_t property_id)
443 struct drm_mode_get_property prop;
444 drmModePropertyPtr r;
446 prop.prop_id = property_id;
447 prop.count_enum_blobs = 0;
448 prop.count_values = 0;
450 prop.enum_blob_ptr = 0;
453 if (ioctl(fd, DRM_IOCTL_MODE_GETPROPERTY, &prop))
456 if (prop.count_values)
457 prop.values_ptr = VOID2U64(drmMalloc(prop.count_values * sizeof(uint64_t)));
459 if (prop.count_enum_blobs && (prop.flags & DRM_MODE_PROP_ENUM))
460 prop.enum_blob_ptr = VOID2U64(drmMalloc(prop.count_enum_blobs * sizeof(struct drm_mode_property_enum)));
462 if (prop.count_enum_blobs && (prop.flags & DRM_MODE_PROP_BLOB)) {
463 prop.values_ptr = VOID2U64(drmMalloc(prop.count_enum_blobs * sizeof(uint32_t)));
464 prop.enum_blob_ptr = VOID2U64(drmMalloc(prop.count_enum_blobs * sizeof(uint32_t)));
467 if (ioctl(fd, DRM_IOCTL_MODE_GETPROPERTY, &prop)) {
472 if (!(r = drmMalloc(sizeof(*r))))
475 r->prop_id = prop.prop_id;
476 r->count_values = prop.count_values;
478 r->flags = prop.flags;
479 if (prop.count_values)
480 r->values = drmAllocCpy(U642VOID(prop.values_ptr), prop.count_values, sizeof(uint64_t));
481 if (prop.flags & DRM_MODE_PROP_ENUM) {
482 r->count_enums = prop.count_enum_blobs;
483 r->enums = drmAllocCpy(U642VOID(prop.enum_blob_ptr), prop.count_enum_blobs, sizeof(struct drm_mode_property_enum));
484 } else if (prop.flags & DRM_MODE_PROP_BLOB) {
485 r->values = drmAllocCpy(U642VOID(prop.values_ptr), prop.count_enum_blobs, sizeof(uint32_t));
486 r->blob_ids = drmAllocCpy(U642VOID(prop.enum_blob_ptr), prop.count_enum_blobs, sizeof(uint32_t));
487 r->count_blobs = prop.count_enum_blobs;
489 strncpy(r->name, prop.name, DRM_PROP_NAME_LEN);
490 r->name[DRM_PROP_NAME_LEN-1] = 0;
493 drmFree(U642VOID(prop.values_ptr));
494 drmFree(U642VOID(prop.enum_blob_ptr));
499 void drmModeFreeProperty(drmModePropertyPtr ptr)
504 drmFree(ptr->values);
509 drmModePropertyBlobPtr drmModeGetPropertyBlob(int fd, uint32_t blob_id)
511 struct drm_mode_get_blob blob;
512 drmModePropertyBlobPtr r;
516 blob.blob_id = blob_id;
518 if (ioctl(fd, DRM_IOCTL_MODE_GETPROPBLOB, &blob))
522 blob.data = VOID2U64(drmMalloc(blob.length));
524 if (ioctl(fd, DRM_IOCTL_MODE_GETPROPBLOB, &blob)) {
529 if (!(r = drmMalloc(sizeof(*r))))
532 r->id = blob.blob_id;
533 r->length = blob.length;
534 r->data = drmAllocCpy(U642VOID(blob.data), 1, blob.length);
537 drmFree(U642VOID(blob.data));
541 void drmModeFreePropertyBlob(drmModePropertyBlobPtr ptr)
550 int drmModeConnectorSetProperty(int fd, uint32_t connector_id, uint32_t property_id,
553 struct drm_mode_connector_set_property osp;
556 osp.connector_id = connector_id;
557 osp.prop_id = property_id;
560 if ((ret = ioctl(fd, DRM_IOCTL_MODE_SETPROPERTY, &osp)))
567 * checks if a modesetting capable driver has attached to the pci id
568 * returns 0 if modesetting supported.
569 * -EINVAL or invalid bus id
570 * -ENOSYS if no modesetting support
572 int drmCheckModesettingSupported(const char *busid)
575 char pci_dev_dir[1024];
576 int domain, bus, dev, func;
581 ret = sscanf(busid, "pci:%04x:%02x:%02x.%d", &domain, &bus, &dev, &func);
585 sprintf(pci_dev_dir, "/sys/bus/pci/devices/%04x:%02x:%02x.%d/drm",
586 domain, bus, dev, func);
588 sysdir = opendir(pci_dev_dir);
590 dent = readdir(sysdir);
592 if (!strncmp(dent->d_name, "controlD", 8)) {
597 dent = readdir(sysdir);
604 sprintf(pci_dev_dir, "/sys/bus/pci/devices/%04x:%02x:%02x.%d/",
605 domain, bus, dev, func);
607 sysdir = opendir(pci_dev_dir);
611 dent = readdir(sysdir);
613 if (!strncmp(dent->d_name, "drm:controlD", 12)) {
618 dent = readdir(sysdir);
629 int drmModeReplaceFB(int fd, uint32_t buffer_id,
630 uint32_t width, uint32_t height, uint8_t depth,
631 uint8_t bpp, uint32_t pitch, uint32_t bo_handle)
633 struct drm_mode_fb_cmd f;
641 f.handle = bo_handle;
644 if ((ret = ioctl(fd, DRM_IOCTL_MODE_REPLACEFB, &f)))
650 int drmModeCrtcGetGamma(int fd, uint32_t crtc_id, uint32_t size,
651 uint16_t *red, uint16_t *green, uint16_t *blue)
654 struct drm_mode_crtc_lut l;
658 l.red = VOID2U64(red);
659 l.green = VOID2U64(green);
660 l.blue = VOID2U64(blue);
662 if ((ret = ioctl(fd, DRM_IOCTL_MODE_GETGAMMA, &l)))
668 int drmModeCrtcSetGamma(int fd, uint32_t crtc_id, uint32_t size,
669 uint16_t *red, uint16_t *green, uint16_t *blue)
672 struct drm_mode_crtc_lut l;
676 l.red = VOID2U64(red);
677 l.green = VOID2U64(green);
678 l.blue = VOID2U64(blue);
680 if ((ret = ioctl(fd, DRM_IOCTL_MODE_SETGAMMA, &l)))