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) 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>
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:
23 * The above copyright notice and this permission notice shall be included in
24 * all copies or substantial portions of the Software.
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
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
41 #include <sys/ioctl.h>
44 #include "xf86drmMode.h"
52 #define U642VOID(x) ((void *)(unsigned long)(x))
53 #define VOID2U64(x) ((uint64_t)(unsigned long)(x))
59 void* drmAllocCpy(void *array, int count, int entry_size)
64 if (!count || !array || !entry_size)
67 if (!(r = drmMalloc(count*entry_size)))
70 for (i = 0; i < count; i++)
71 memcpy(r+(entry_size*i), array+(entry_size*i), entry_size);
77 * A couple of free functions.
80 void drmModeFreeModeInfo(drmModeModeInfoPtr ptr)
88 void drmModeFreeResources(drmModeResPtr ptr)
97 void drmModeFreeFB(drmModeFBPtr ptr)
102 /* we might add more frees later. */
106 void drmModeFreeCrtc(drmModeCrtcPtr ptr)
115 void drmModeFreeConnector(drmModeConnectorPtr ptr)
120 drmFree(ptr->encoders);
121 drmFree(ptr->prop_values);
128 void drmModeFreeEncoder(drmModeEncoderPtr ptr)
134 * ModeSetting functions.
137 drmModeResPtr drmModeGetResources(int fd)
139 struct drm_mode_card_res res, counts;
143 memset(&res, 0, sizeof(struct drm_mode_card_res));
144 if (drmIoctl(fd, DRM_IOCTL_MODE_GETRESOURCES, &res))
150 res.fb_id_ptr = VOID2U64(drmMalloc(res.count_fbs*sizeof(uint32_t)));
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)));
158 if (drmIoctl(fd, DRM_IOCTL_MODE_GETRESOURCES, &res))
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.
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)
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));
181 if (!(r = drmMalloc(sizeof(*r))))
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));
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));
207 int drmModeAddFB(int fd, uint32_t width, uint32_t height, uint8_t depth,
208 uint8_t bpp, uint32_t pitch, uint32_t bo_handle,
211 struct drm_mode_fb_cmd f;
219 f.handle = bo_handle;
221 if ((ret = drmIoctl(fd, DRM_IOCTL_MODE_ADDFB, &f)))
228 int drmModeRmFB(int fd, uint32_t bufferId)
230 return drmIoctl(fd, DRM_IOCTL_MODE_RMFB, &bufferId);
235 drmModeFBPtr drmModeGetFB(int fd, uint32_t buf)
237 struct drm_mode_fb_cmd info;
242 if (drmIoctl(fd, DRM_IOCTL_MODE_GETFB, &info))
245 if (!(r = drmMalloc(sizeof(*r))))
248 r->fb_id = info.fb_id;
249 r->width = info.width;
250 r->height = info.height;
251 r->pitch = info.pitch;
253 r->handle = info.handle;
254 r->depth = info.depth;
259 int drmModeDirtyFB(int fd, uint32_t bufferId,
260 drmModeClipPtr clips, uint32_t num_clips)
262 struct drm_mode_fb_dirty_cmd dirty = { 0 };
264 dirty.fb_id = bufferId;
265 dirty.clips_ptr = VOID2U64(clips);
266 dirty.num_clips = num_clips;
268 return drmIoctl(fd, DRM_IOCTL_MODE_DIRTYFB, &dirty);
276 drmModeCrtcPtr drmModeGetCrtc(int fd, uint32_t crtcId)
278 struct drm_mode_crtc crtc;
281 crtc.crtc_id = crtcId;
283 if (drmIoctl(fd, DRM_IOCTL_MODE_GETCRTC, &crtc))
290 if (!(r = drmMalloc(sizeof(*r))))
293 r->crtc_id = crtc.crtc_id;
296 r->mode_valid = crtc.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;
305 int drmModeSetCrtc(int fd, uint32_t crtcId, uint32_t bufferId,
306 uint32_t x, uint32_t y, uint32_t *connectors, int count,
307 drmModeModeInfoPtr mode)
309 struct drm_mode_crtc crtc;
313 crtc.crtc_id = crtcId;
314 crtc.fb_id = bufferId;
315 crtc.set_connectors_ptr = VOID2U64(connectors);
316 crtc.count_connectors = count;
318 memcpy(&crtc.mode, mode, sizeof(struct drm_mode_modeinfo));
323 return drmIoctl(fd, DRM_IOCTL_MODE_SETCRTC, &crtc);
327 * Cursor manipulation
330 int drmModeSetCursor(int fd, uint32_t crtcId, uint32_t bo_handle, uint32_t width, uint32_t height)
332 struct drm_mode_cursor arg;
334 arg.flags = DRM_MODE_CURSOR_BO;
335 arg.crtc_id = crtcId;
338 arg.handle = bo_handle;
340 return drmIoctl(fd, DRM_IOCTL_MODE_CURSOR, &arg);
343 int drmModeMoveCursor(int fd, uint32_t crtcId, int x, int y)
345 struct drm_mode_cursor arg;
347 arg.flags = DRM_MODE_CURSOR_MOVE;
348 arg.crtc_id = crtcId;
352 return drmIoctl(fd, DRM_IOCTL_MODE_CURSOR, &arg);
358 drmModeEncoderPtr drmModeGetEncoder(int fd, uint32_t encoder_id)
360 struct drm_mode_get_encoder enc;
361 drmModeEncoderPtr r = NULL;
363 enc.encoder_id = encoder_id;
364 enc.encoder_type = 0;
365 enc.possible_crtcs = 0;
366 enc.possible_clones = 0;
368 if (drmIoctl(fd, DRM_IOCTL_MODE_GETENCODER, &enc))
371 if (!(r = drmMalloc(sizeof(*r))))
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;
384 * Connector manipulation
387 drmModeConnectorPtr drmModeGetConnector(int fd, uint32_t connector_id)
389 struct drm_mode_get_connector conn;
390 drmModeConnectorPtr r = NULL;
392 conn.connector_id = connector_id;
393 conn.connector_type_id = 0;
394 conn.connector_type = 0;
395 conn.count_modes = 0;
397 conn.count_props = 0;
399 conn.prop_values_ptr = 0;
400 conn.count_encoders = 0;
401 conn.encoders_ptr = 0;
403 if (drmIoctl(fd, DRM_IOCTL_MODE_GETCONNECTOR, &conn))
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)));
411 if (conn.count_modes)
412 conn.modes_ptr = VOID2U64(drmMalloc(conn.count_modes*sizeof(struct drm_mode_modeinfo)));
414 if (conn.count_encoders)
415 conn.encoders_ptr = VOID2U64(drmMalloc(conn.count_encoders*sizeof(uint32_t)));
417 if (drmIoctl(fd, DRM_IOCTL_MODE_GETCONNECTOR, &conn))
420 if(!(r = drmMalloc(sizeof(*r)))) {
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;
429 /* convert subpixel from kernel to userspace */
430 r->subpixel = conn.subpixel + 1;
431 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;
442 if (!r->props || !r->prop_values || !r->modes || !r->encoders)
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));
454 int drmModeAttachMode(int fd, uint32_t connector_id, drmModeModeInfoPtr mode_info)
456 struct drm_mode_mode_cmd res;
458 memcpy(&res.mode, mode_info, sizeof(struct drm_mode_modeinfo));
459 res.connector_id = connector_id;
461 return drmIoctl(fd, DRM_IOCTL_MODE_ATTACHMODE, &res);
464 int drmModeDetachMode(int fd, uint32_t connector_id, drmModeModeInfoPtr mode_info)
466 struct drm_mode_mode_cmd res;
468 memcpy(&res.mode, mode_info, sizeof(struct drm_mode_modeinfo));
469 res.connector_id = connector_id;
471 return drmIoctl(fd, DRM_IOCTL_MODE_DETACHMODE, &res);
475 drmModePropertyPtr drmModeGetProperty(int fd, uint32_t property_id)
477 struct drm_mode_get_property prop;
478 drmModePropertyPtr r;
480 prop.prop_id = property_id;
481 prop.count_enum_blobs = 0;
482 prop.count_values = 0;
484 prop.enum_blob_ptr = 0;
487 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPERTY, &prop))
490 if (prop.count_values)
491 prop.values_ptr = VOID2U64(drmMalloc(prop.count_values * sizeof(uint64_t)));
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)));
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)));
501 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPERTY, &prop)) {
506 if (!(r = drmMalloc(sizeof(*r))))
509 r->prop_id = prop.prop_id;
510 r->count_values = prop.count_values;
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;
523 strncpy(r->name, prop.name, DRM_PROP_NAME_LEN);
524 r->name[DRM_PROP_NAME_LEN-1] = 0;
527 drmFree(U642VOID(prop.values_ptr));
528 drmFree(U642VOID(prop.enum_blob_ptr));
533 void drmModeFreeProperty(drmModePropertyPtr ptr)
538 drmFree(ptr->values);
543 drmModePropertyBlobPtr drmModeGetPropertyBlob(int fd, uint32_t blob_id)
545 struct drm_mode_get_blob blob;
546 drmModePropertyBlobPtr r;
550 blob.blob_id = blob_id;
552 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPBLOB, &blob))
556 blob.data = VOID2U64(drmMalloc(blob.length));
558 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPBLOB, &blob)) {
563 if (!(r = drmMalloc(sizeof(*r))))
566 r->id = blob.blob_id;
567 r->length = blob.length;
568 r->data = drmAllocCpy(U642VOID(blob.data), 1, blob.length);
571 drmFree(U642VOID(blob.data));
575 void drmModeFreePropertyBlob(drmModePropertyBlobPtr ptr)
584 int drmModeConnectorSetProperty(int fd, uint32_t connector_id, uint32_t property_id,
587 struct drm_mode_connector_set_property osp;
590 osp.connector_id = connector_id;
591 osp.prop_id = property_id;
594 if ((ret = drmIoctl(fd, DRM_IOCTL_MODE_SETPROPERTY, &osp)))
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
606 int drmCheckModesettingSupported(const char *busid)
609 char pci_dev_dir[1024];
610 int domain, bus, dev, func;
615 ret = sscanf(busid, "pci:%04x:%02x:%02x.%d", &domain, &bus, &dev, &func);
619 sprintf(pci_dev_dir, "/sys/bus/pci/devices/%04x:%02x:%02x.%d/drm",
620 domain, bus, dev, func);
622 sysdir = opendir(pci_dev_dir);
624 dent = readdir(sysdir);
626 if (!strncmp(dent->d_name, "controlD", 8)) {
631 dent = readdir(sysdir);
638 sprintf(pci_dev_dir, "/sys/bus/pci/devices/%04x:%02x:%02x.%d/",
639 domain, bus, dev, func);
641 sysdir = opendir(pci_dev_dir);
645 dent = readdir(sysdir);
647 if (!strncmp(dent->d_name, "drm:controlD", 12)) {
652 dent = readdir(sysdir);
663 int drmModeCrtcGetGamma(int fd, uint32_t crtc_id, uint32_t size,
664 uint16_t *red, uint16_t *green, uint16_t *blue)
667 struct drm_mode_crtc_lut l;
671 l.red = VOID2U64(red);
672 l.green = VOID2U64(green);
673 l.blue = VOID2U64(blue);
675 if ((ret = drmIoctl(fd, DRM_IOCTL_MODE_GETGAMMA, &l)))
681 int drmModeCrtcSetGamma(int fd, uint32_t crtc_id, uint32_t size,
682 uint16_t *red, uint16_t *green, uint16_t *blue)
685 struct drm_mode_crtc_lut l;
689 l.red = VOID2U64(red);
690 l.green = VOID2U64(green);
691 l.blue = VOID2U64(blue);
693 if ((ret = drmIoctl(fd, DRM_IOCTL_MODE_SETGAMMA, &l)))
699 int drmHandleEvent(int fd, drmEventContextPtr evctx)
704 struct drm_event_vblank *vblank;
706 /* The DRM read semantics guarantees that we always get only
707 * complete events. */
709 len = read(fd, buffer, sizeof buffer);
717 e = (struct drm_event *) &buffer[i];
719 case DRM_EVENT_VBLANK:
720 if (evctx->version < 1 ||
721 evctx->vblank_handler == NULL)
723 vblank = (struct drm_event_vblank *) e;
724 evctx->vblank_handler(fd,
728 U642VOID (vblank->user_data));
730 case DRM_EVENT_FLIP_COMPLETE:
731 if (evctx->version < 2 ||
732 evctx->page_flip_handler == NULL)
734 vblank = (struct drm_event_vblank *) e;
735 evctx->page_flip_handler(fd,
739 U642VOID (vblank->user_data));
750 int drmModePageFlip(int fd, uint32_t crtc_id, uint32_t fb_id,
751 uint32_t flags, void *user_data)
753 struct drm_mode_crtc_page_flip flip;
756 flip.crtc_id = crtc_id;
757 flip.user_data = VOID2U64(user_data);
761 return drmIoctl(fd, DRM_IOCTL_MODE_PAGE_FLIP, &flip);