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;
142 memset(&res, 0, sizeof(struct drm_mode_card_res));
144 if (drmIoctl(fd, DRM_IOCTL_MODE_GETRESOURCES, &res))
148 res.fb_id_ptr = VOID2U64(drmMalloc(res.count_fbs*sizeof(uint32_t)));
150 res.crtc_id_ptr = VOID2U64(drmMalloc(res.count_crtcs*sizeof(uint32_t)));
151 if (res.count_connectors)
152 res.connector_id_ptr = VOID2U64(drmMalloc(res.count_connectors*sizeof(uint32_t)));
153 if (res.count_encoders)
154 res.encoder_id_ptr = VOID2U64(drmMalloc(res.count_encoders*sizeof(uint32_t)));
156 if (drmIoctl(fd, DRM_IOCTL_MODE_GETRESOURCES, &res)) {
166 if (!(r = drmMalloc(sizeof(*r))))
169 r->min_width = res.min_width;
170 r->max_width = res.max_width;
171 r->min_height = res.min_height;
172 r->max_height = res.max_height;
173 r->count_fbs = res.count_fbs;
174 r->count_crtcs = res.count_crtcs;
175 r->count_connectors = res.count_connectors;
176 r->count_encoders = res.count_encoders;
177 /* TODO we realy should test if these allocs fails. */
178 r->fbs = drmAllocCpy(U642VOID(res.fb_id_ptr), res.count_fbs, sizeof(uint32_t));
179 r->crtcs = drmAllocCpy(U642VOID(res.crtc_id_ptr), res.count_crtcs, sizeof(uint32_t));
180 r->connectors = drmAllocCpy(U642VOID(res.connector_id_ptr), res.count_connectors, sizeof(uint32_t));
181 r->encoders = drmAllocCpy(U642VOID(res.encoder_id_ptr), res.count_encoders, sizeof(uint32_t));
184 drmFree(U642VOID(res.fb_id_ptr));
185 drmFree(U642VOID(res.crtc_id_ptr));
186 drmFree(U642VOID(res.connector_id_ptr));
187 drmFree(U642VOID(res.encoder_id_ptr));
192 int drmModeAddFB(int fd, uint32_t width, uint32_t height, uint8_t depth,
193 uint8_t bpp, uint32_t pitch, uint32_t bo_handle,
196 struct drm_mode_fb_cmd f;
204 f.handle = bo_handle;
206 if ((ret = drmIoctl(fd, DRM_IOCTL_MODE_ADDFB, &f)))
213 int drmModeRmFB(int fd, uint32_t bufferId)
215 return drmIoctl(fd, DRM_IOCTL_MODE_RMFB, &bufferId);
220 drmModeFBPtr drmModeGetFB(int fd, uint32_t buf)
222 struct drm_mode_fb_cmd info;
227 if (drmIoctl(fd, DRM_IOCTL_MODE_GETFB, &info))
230 if (!(r = drmMalloc(sizeof(*r))))
233 r->fb_id = info.fb_id;
234 r->width = info.width;
235 r->height = info.height;
236 r->pitch = info.pitch;
238 r->handle = info.handle;
239 r->depth = info.depth;
244 int drmModeDirtyFB(int fd, uint32_t bufferId,
245 drmModeClipPtr clips, uint32_t num_clips)
247 struct drm_mode_fb_dirty_cmd dirty = { 0 };
249 dirty.fb_id = bufferId;
250 dirty.clips_ptr = VOID2U64(clips);
251 dirty.num_clips = num_clips;
253 return drmIoctl(fd, DRM_IOCTL_MODE_DIRTYFB, &dirty);
261 drmModeCrtcPtr drmModeGetCrtc(int fd, uint32_t crtcId)
263 struct drm_mode_crtc crtc;
266 crtc.crtc_id = crtcId;
268 if (drmIoctl(fd, DRM_IOCTL_MODE_GETCRTC, &crtc))
275 if (!(r = drmMalloc(sizeof(*r))))
278 r->crtc_id = crtc.crtc_id;
281 r->mode_valid = crtc.mode_valid;
283 memcpy(&r->mode, &crtc.mode, sizeof(struct drm_mode_modeinfo));
284 r->buffer_id = crtc.fb_id;
285 r->gamma_size = crtc.gamma_size;
290 int drmModeSetCrtc(int fd, uint32_t crtcId, uint32_t bufferId,
291 uint32_t x, uint32_t y, uint32_t *connectors, int count,
292 drmModeModeInfoPtr mode)
294 struct drm_mode_crtc crtc;
298 crtc.crtc_id = crtcId;
299 crtc.fb_id = bufferId;
300 crtc.set_connectors_ptr = VOID2U64(connectors);
301 crtc.count_connectors = count;
303 memcpy(&crtc.mode, mode, sizeof(struct drm_mode_modeinfo));
308 return drmIoctl(fd, DRM_IOCTL_MODE_SETCRTC, &crtc);
312 * Cursor manipulation
315 int drmModeSetCursor(int fd, uint32_t crtcId, uint32_t bo_handle, uint32_t width, uint32_t height)
317 struct drm_mode_cursor arg;
319 arg.flags = DRM_MODE_CURSOR_BO;
320 arg.crtc_id = crtcId;
323 arg.handle = bo_handle;
325 return drmIoctl(fd, DRM_IOCTL_MODE_CURSOR, &arg);
328 int drmModeMoveCursor(int fd, uint32_t crtcId, int x, int y)
330 struct drm_mode_cursor arg;
332 arg.flags = DRM_MODE_CURSOR_MOVE;
333 arg.crtc_id = crtcId;
337 return drmIoctl(fd, DRM_IOCTL_MODE_CURSOR, &arg);
343 drmModeEncoderPtr drmModeGetEncoder(int fd, uint32_t encoder_id)
345 struct drm_mode_get_encoder enc;
346 drmModeEncoderPtr r = NULL;
348 enc.encoder_id = encoder_id;
349 enc.encoder_type = 0;
350 enc.possible_crtcs = 0;
351 enc.possible_clones = 0;
353 if (drmIoctl(fd, DRM_IOCTL_MODE_GETENCODER, &enc))
356 if (!(r = drmMalloc(sizeof(*r))))
359 r->encoder_id = enc.encoder_id;
360 r->crtc_id = enc.crtc_id;
361 r->encoder_type = enc.encoder_type;
362 r->possible_crtcs = enc.possible_crtcs;
363 r->possible_clones = enc.possible_clones;
369 * Connector manipulation
372 drmModeConnectorPtr drmModeGetConnector(int fd, uint32_t connector_id)
374 struct drm_mode_get_connector conn;
375 drmModeConnectorPtr r = NULL;
377 conn.connector_id = connector_id;
378 conn.connector_type_id = 0;
379 conn.connector_type = 0;
380 conn.count_modes = 0;
382 conn.count_props = 0;
384 conn.prop_values_ptr = 0;
385 conn.count_encoders = 0;
386 conn.encoders_ptr = 0;
388 if (drmIoctl(fd, DRM_IOCTL_MODE_GETCONNECTOR, &conn))
391 if (conn.count_props) {
392 conn.props_ptr = VOID2U64(drmMalloc(conn.count_props*sizeof(uint32_t)));
393 conn.prop_values_ptr = VOID2U64(drmMalloc(conn.count_props*sizeof(uint64_t)));
396 if (conn.count_modes)
397 conn.modes_ptr = VOID2U64(drmMalloc(conn.count_modes*sizeof(struct drm_mode_modeinfo)));
399 if (conn.count_encoders)
400 conn.encoders_ptr = VOID2U64(drmMalloc(conn.count_encoders*sizeof(uint32_t)));
402 if (drmIoctl(fd, DRM_IOCTL_MODE_GETCONNECTOR, &conn))
405 if(!(r = drmMalloc(sizeof(*r)))) {
409 r->connector_id = conn.connector_id;
410 r->encoder_id = conn.encoder_id;
411 r->connection = conn.connection;
412 r->mmWidth = conn.mm_width;
413 r->mmHeight = conn.mm_height;
414 /* convert subpixel from kernel to userspace */
415 r->subpixel = conn.subpixel + 1;
416 r->count_modes = conn.count_modes;
417 /* TODO we should test if these alloc & cpy fails. */
418 r->count_props = conn.count_props;
419 r->props = drmAllocCpy(U642VOID(conn.props_ptr), conn.count_props, sizeof(uint32_t));
420 r->prop_values = drmAllocCpy(U642VOID(conn.prop_values_ptr), conn.count_props, sizeof(uint64_t));
421 r->modes = drmAllocCpy(U642VOID(conn.modes_ptr), conn.count_modes, sizeof(struct drm_mode_modeinfo));
422 r->count_encoders = conn.count_encoders;
423 r->encoders = drmAllocCpy(U642VOID(conn.encoders_ptr), conn.count_encoders, sizeof(uint32_t));
424 r->connector_type = conn.connector_type;
425 r->connector_type_id = conn.connector_type_id;
427 if (!r->props || !r->prop_values || !r->modes || !r->encoders)
431 drmFree(U642VOID(conn.prop_values_ptr));
432 drmFree(U642VOID(conn.props_ptr));
433 drmFree(U642VOID(conn.modes_ptr));
434 drmFree(U642VOID(conn.encoders_ptr));
439 int drmModeAttachMode(int fd, uint32_t connector_id, drmModeModeInfoPtr mode_info)
441 struct drm_mode_mode_cmd res;
443 memcpy(&res.mode, mode_info, sizeof(struct drm_mode_modeinfo));
444 res.connector_id = connector_id;
446 return drmIoctl(fd, DRM_IOCTL_MODE_ATTACHMODE, &res);
449 int drmModeDetachMode(int fd, uint32_t connector_id, drmModeModeInfoPtr mode_info)
451 struct drm_mode_mode_cmd res;
453 memcpy(&res.mode, mode_info, sizeof(struct drm_mode_modeinfo));
454 res.connector_id = connector_id;
456 return drmIoctl(fd, DRM_IOCTL_MODE_DETACHMODE, &res);
460 drmModePropertyPtr drmModeGetProperty(int fd, uint32_t property_id)
462 struct drm_mode_get_property prop;
463 drmModePropertyPtr r;
465 prop.prop_id = property_id;
466 prop.count_enum_blobs = 0;
467 prop.count_values = 0;
469 prop.enum_blob_ptr = 0;
472 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPERTY, &prop))
475 if (prop.count_values)
476 prop.values_ptr = VOID2U64(drmMalloc(prop.count_values * sizeof(uint64_t)));
478 if (prop.count_enum_blobs && (prop.flags & DRM_MODE_PROP_ENUM))
479 prop.enum_blob_ptr = VOID2U64(drmMalloc(prop.count_enum_blobs * sizeof(struct drm_mode_property_enum)));
481 if (prop.count_enum_blobs && (prop.flags & DRM_MODE_PROP_BLOB)) {
482 prop.values_ptr = VOID2U64(drmMalloc(prop.count_enum_blobs * sizeof(uint32_t)));
483 prop.enum_blob_ptr = VOID2U64(drmMalloc(prop.count_enum_blobs * sizeof(uint32_t)));
486 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPERTY, &prop)) {
491 if (!(r = drmMalloc(sizeof(*r))))
494 r->prop_id = prop.prop_id;
495 r->count_values = prop.count_values;
497 r->flags = prop.flags;
498 if (prop.count_values)
499 r->values = drmAllocCpy(U642VOID(prop.values_ptr), prop.count_values, sizeof(uint64_t));
500 if (prop.flags & DRM_MODE_PROP_ENUM) {
501 r->count_enums = prop.count_enum_blobs;
502 r->enums = drmAllocCpy(U642VOID(prop.enum_blob_ptr), prop.count_enum_blobs, sizeof(struct drm_mode_property_enum));
503 } else if (prop.flags & DRM_MODE_PROP_BLOB) {
504 r->values = drmAllocCpy(U642VOID(prop.values_ptr), prop.count_enum_blobs, sizeof(uint32_t));
505 r->blob_ids = drmAllocCpy(U642VOID(prop.enum_blob_ptr), prop.count_enum_blobs, sizeof(uint32_t));
506 r->count_blobs = prop.count_enum_blobs;
508 strncpy(r->name, prop.name, DRM_PROP_NAME_LEN);
509 r->name[DRM_PROP_NAME_LEN-1] = 0;
512 drmFree(U642VOID(prop.values_ptr));
513 drmFree(U642VOID(prop.enum_blob_ptr));
518 void drmModeFreeProperty(drmModePropertyPtr ptr)
523 drmFree(ptr->values);
528 drmModePropertyBlobPtr drmModeGetPropertyBlob(int fd, uint32_t blob_id)
530 struct drm_mode_get_blob blob;
531 drmModePropertyBlobPtr r;
535 blob.blob_id = blob_id;
537 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPBLOB, &blob))
541 blob.data = VOID2U64(drmMalloc(blob.length));
543 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPBLOB, &blob)) {
548 if (!(r = drmMalloc(sizeof(*r))))
551 r->id = blob.blob_id;
552 r->length = blob.length;
553 r->data = drmAllocCpy(U642VOID(blob.data), 1, blob.length);
556 drmFree(U642VOID(blob.data));
560 void drmModeFreePropertyBlob(drmModePropertyBlobPtr ptr)
569 int drmModeConnectorSetProperty(int fd, uint32_t connector_id, uint32_t property_id,
572 struct drm_mode_connector_set_property osp;
575 osp.connector_id = connector_id;
576 osp.prop_id = property_id;
579 if ((ret = drmIoctl(fd, DRM_IOCTL_MODE_SETPROPERTY, &osp)))
586 * checks if a modesetting capable driver has attached to the pci id
587 * returns 0 if modesetting supported.
588 * -EINVAL or invalid bus id
589 * -ENOSYS if no modesetting support
591 int drmCheckModesettingSupported(const char *busid)
594 char pci_dev_dir[1024];
595 int domain, bus, dev, func;
600 ret = sscanf(busid, "pci:%04x:%02x:%02x.%d", &domain, &bus, &dev, &func);
604 sprintf(pci_dev_dir, "/sys/bus/pci/devices/%04x:%02x:%02x.%d/drm",
605 domain, bus, dev, func);
607 sysdir = opendir(pci_dev_dir);
609 dent = readdir(sysdir);
611 if (!strncmp(dent->d_name, "controlD", 8)) {
616 dent = readdir(sysdir);
623 sprintf(pci_dev_dir, "/sys/bus/pci/devices/%04x:%02x:%02x.%d/",
624 domain, bus, dev, func);
626 sysdir = opendir(pci_dev_dir);
630 dent = readdir(sysdir);
632 if (!strncmp(dent->d_name, "drm:controlD", 12)) {
637 dent = readdir(sysdir);
648 int drmModeCrtcGetGamma(int fd, uint32_t crtc_id, uint32_t size,
649 uint16_t *red, uint16_t *green, uint16_t *blue)
652 struct drm_mode_crtc_lut l;
656 l.red = VOID2U64(red);
657 l.green = VOID2U64(green);
658 l.blue = VOID2U64(blue);
660 if ((ret = drmIoctl(fd, DRM_IOCTL_MODE_GETGAMMA, &l)))
666 int drmModeCrtcSetGamma(int fd, uint32_t crtc_id, uint32_t size,
667 uint16_t *red, uint16_t *green, uint16_t *blue)
670 struct drm_mode_crtc_lut l;
674 l.red = VOID2U64(red);
675 l.green = VOID2U64(green);
676 l.blue = VOID2U64(blue);
678 if ((ret = drmIoctl(fd, DRM_IOCTL_MODE_SETGAMMA, &l)))
684 int drmHandleEvent(int fd, drmEventContextPtr evctx)
689 struct drm_event_vblank *vblank;
691 /* The DRM read semantics guarantees that we always get only
692 * complete events. */
694 len = read(fd, buffer, sizeof buffer);
702 e = (struct drm_event *) &buffer[i];
704 case DRM_EVENT_VBLANK:
705 if (evctx->version < 1 ||
706 evctx->vblank_handler == NULL)
708 vblank = (struct drm_event_vblank *) e;
709 evctx->vblank_handler(fd,
713 U642VOID (vblank->user_data));