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)));
154 if (res.count_crtcs) {
155 res.crtc_id_ptr = VOID2U64(drmMalloc(res.count_crtcs*sizeof(uint32_t)));
156 if (!res.crtc_id_ptr)
159 if (res.count_connectors) {
160 res.connector_id_ptr = VOID2U64(drmMalloc(res.count_connectors*sizeof(uint32_t)));
161 if (!res.connector_id_ptr)
164 if (res.count_encoders) {
165 res.encoder_id_ptr = VOID2U64(drmMalloc(res.count_encoders*sizeof(uint32_t)));
166 if (!res.encoder_id_ptr)
170 if (drmIoctl(fd, DRM_IOCTL_MODE_GETRESOURCES, &res))
173 /* The number of available connectors and etc may have changed with a
174 * hotplug event in between the ioctls, in which case the field is
175 * silently ignored by the kernel.
177 if (counts.count_fbs < res.count_fbs ||
178 counts.count_crtcs < res.count_crtcs ||
179 counts.count_connectors < res.count_connectors ||
180 counts.count_encoders < res.count_encoders)
182 drmFree(U642VOID(res.fb_id_ptr));
183 drmFree(U642VOID(res.crtc_id_ptr));
184 drmFree(U642VOID(res.connector_id_ptr));
185 drmFree(U642VOID(res.encoder_id_ptr));
193 if (!(r = drmMalloc(sizeof(*r))))
196 r->min_width = res.min_width;
197 r->max_width = res.max_width;
198 r->min_height = res.min_height;
199 r->max_height = res.max_height;
200 r->count_fbs = res.count_fbs;
201 r->count_crtcs = res.count_crtcs;
202 r->count_connectors = res.count_connectors;
203 r->count_encoders = res.count_encoders;
205 r->fbs = drmAllocCpy(U642VOID(res.fb_id_ptr), res.count_fbs, sizeof(uint32_t));
206 r->crtcs = drmAllocCpy(U642VOID(res.crtc_id_ptr), res.count_crtcs, sizeof(uint32_t));
207 r->connectors = drmAllocCpy(U642VOID(res.connector_id_ptr), res.count_connectors, sizeof(uint32_t));
208 r->encoders = drmAllocCpy(U642VOID(res.encoder_id_ptr), res.count_encoders, sizeof(uint32_t));
209 if ((res.count_fbs && !r->fbs) ||
210 (res.count_crtcs && !r->crtcs) ||
211 (res.count_connectors && !r->connectors) ||
212 (res.count_encoders && !r->encoders))
216 drmFree(r->connectors);
217 drmFree(r->encoders);
223 drmFree(U642VOID(res.fb_id_ptr));
224 drmFree(U642VOID(res.crtc_id_ptr));
225 drmFree(U642VOID(res.connector_id_ptr));
226 drmFree(U642VOID(res.encoder_id_ptr));
231 int drmModeAddFB(int fd, uint32_t width, uint32_t height, uint8_t depth,
232 uint8_t bpp, uint32_t pitch, uint32_t bo_handle,
235 struct drm_mode_fb_cmd f;
243 f.handle = bo_handle;
245 if ((ret = drmIoctl(fd, DRM_IOCTL_MODE_ADDFB, &f)))
252 int drmModeRmFB(int fd, uint32_t bufferId)
254 return drmIoctl(fd, DRM_IOCTL_MODE_RMFB, &bufferId);
259 drmModeFBPtr drmModeGetFB(int fd, uint32_t buf)
261 struct drm_mode_fb_cmd info;
266 if (drmIoctl(fd, DRM_IOCTL_MODE_GETFB, &info))
269 if (!(r = drmMalloc(sizeof(*r))))
272 r->fb_id = info.fb_id;
273 r->width = info.width;
274 r->height = info.height;
275 r->pitch = info.pitch;
277 r->handle = info.handle;
278 r->depth = info.depth;
283 int drmModeDirtyFB(int fd, uint32_t bufferId,
284 drmModeClipPtr clips, uint32_t num_clips)
286 struct drm_mode_fb_dirty_cmd dirty = { 0 };
288 dirty.fb_id = bufferId;
289 dirty.clips_ptr = VOID2U64(clips);
290 dirty.num_clips = num_clips;
292 return drmIoctl(fd, DRM_IOCTL_MODE_DIRTYFB, &dirty);
300 drmModeCrtcPtr drmModeGetCrtc(int fd, uint32_t crtcId)
302 struct drm_mode_crtc crtc;
305 crtc.crtc_id = crtcId;
307 if (drmIoctl(fd, DRM_IOCTL_MODE_GETCRTC, &crtc))
314 if (!(r = drmMalloc(sizeof(*r))))
317 r->crtc_id = crtc.crtc_id;
320 r->mode_valid = crtc.mode_valid;
322 memcpy(&r->mode, &crtc.mode, sizeof(struct drm_mode_modeinfo));
323 r->buffer_id = crtc.fb_id;
324 r->gamma_size = crtc.gamma_size;
329 int drmModeSetCrtc(int fd, uint32_t crtcId, uint32_t bufferId,
330 uint32_t x, uint32_t y, uint32_t *connectors, int count,
331 drmModeModeInfoPtr mode)
333 struct drm_mode_crtc crtc;
337 crtc.crtc_id = crtcId;
338 crtc.fb_id = bufferId;
339 crtc.set_connectors_ptr = VOID2U64(connectors);
340 crtc.count_connectors = count;
342 memcpy(&crtc.mode, mode, sizeof(struct drm_mode_modeinfo));
347 return drmIoctl(fd, DRM_IOCTL_MODE_SETCRTC, &crtc);
351 * Cursor manipulation
354 int drmModeSetCursor(int fd, uint32_t crtcId, uint32_t bo_handle, uint32_t width, uint32_t height)
356 struct drm_mode_cursor arg;
358 arg.flags = DRM_MODE_CURSOR_BO;
359 arg.crtc_id = crtcId;
362 arg.handle = bo_handle;
364 return drmIoctl(fd, DRM_IOCTL_MODE_CURSOR, &arg);
367 int drmModeMoveCursor(int fd, uint32_t crtcId, int x, int y)
369 struct drm_mode_cursor arg;
371 arg.flags = DRM_MODE_CURSOR_MOVE;
372 arg.crtc_id = crtcId;
376 return drmIoctl(fd, DRM_IOCTL_MODE_CURSOR, &arg);
382 drmModeEncoderPtr drmModeGetEncoder(int fd, uint32_t encoder_id)
384 struct drm_mode_get_encoder enc;
385 drmModeEncoderPtr r = NULL;
387 enc.encoder_id = encoder_id;
388 enc.encoder_type = 0;
389 enc.possible_crtcs = 0;
390 enc.possible_clones = 0;
392 if (drmIoctl(fd, DRM_IOCTL_MODE_GETENCODER, &enc))
395 if (!(r = drmMalloc(sizeof(*r))))
398 r->encoder_id = enc.encoder_id;
399 r->crtc_id = enc.crtc_id;
400 r->encoder_type = enc.encoder_type;
401 r->possible_crtcs = enc.possible_crtcs;
402 r->possible_clones = enc.possible_clones;
408 * Connector manipulation
411 drmModeConnectorPtr drmModeGetConnector(int fd, uint32_t connector_id)
413 struct drm_mode_get_connector conn;
414 drmModeConnectorPtr r = NULL;
416 conn.connector_id = connector_id;
417 conn.connector_type_id = 0;
418 conn.connector_type = 0;
419 conn.count_modes = 0;
421 conn.count_props = 0;
423 conn.prop_values_ptr = 0;
424 conn.count_encoders = 0;
425 conn.encoders_ptr = 0;
427 if (drmIoctl(fd, DRM_IOCTL_MODE_GETCONNECTOR, &conn))
430 if (conn.count_props) {
431 conn.props_ptr = VOID2U64(drmMalloc(conn.count_props*sizeof(uint32_t)));
432 conn.prop_values_ptr = VOID2U64(drmMalloc(conn.count_props*sizeof(uint64_t)));
435 if (conn.count_modes)
436 conn.modes_ptr = VOID2U64(drmMalloc(conn.count_modes*sizeof(struct drm_mode_modeinfo)));
438 if (conn.count_encoders)
439 conn.encoders_ptr = VOID2U64(drmMalloc(conn.count_encoders*sizeof(uint32_t)));
441 if (drmIoctl(fd, DRM_IOCTL_MODE_GETCONNECTOR, &conn))
444 if(!(r = drmMalloc(sizeof(*r)))) {
448 r->connector_id = conn.connector_id;
449 r->encoder_id = conn.encoder_id;
450 r->connection = conn.connection;
451 r->mmWidth = conn.mm_width;
452 r->mmHeight = conn.mm_height;
453 /* convert subpixel from kernel to userspace */
454 r->subpixel = conn.subpixel + 1;
455 r->count_modes = conn.count_modes;
456 /* TODO we should test if these alloc & cpy fails. */
457 r->count_props = conn.count_props;
458 r->props = drmAllocCpy(U642VOID(conn.props_ptr), conn.count_props, sizeof(uint32_t));
459 r->prop_values = drmAllocCpy(U642VOID(conn.prop_values_ptr), conn.count_props, sizeof(uint64_t));
460 r->modes = drmAllocCpy(U642VOID(conn.modes_ptr), conn.count_modes, sizeof(struct drm_mode_modeinfo));
461 r->count_encoders = conn.count_encoders;
462 r->encoders = drmAllocCpy(U642VOID(conn.encoders_ptr), conn.count_encoders, sizeof(uint32_t));
463 r->connector_type = conn.connector_type;
464 r->connector_type_id = conn.connector_type_id;
466 if (!r->props || !r->prop_values || !r->modes || !r->encoders)
470 drmFree(U642VOID(conn.prop_values_ptr));
471 drmFree(U642VOID(conn.props_ptr));
472 drmFree(U642VOID(conn.modes_ptr));
473 drmFree(U642VOID(conn.encoders_ptr));
478 int drmModeAttachMode(int fd, uint32_t connector_id, drmModeModeInfoPtr mode_info)
480 struct drm_mode_mode_cmd res;
482 memcpy(&res.mode, mode_info, sizeof(struct drm_mode_modeinfo));
483 res.connector_id = connector_id;
485 return drmIoctl(fd, DRM_IOCTL_MODE_ATTACHMODE, &res);
488 int drmModeDetachMode(int fd, uint32_t connector_id, drmModeModeInfoPtr mode_info)
490 struct drm_mode_mode_cmd res;
492 memcpy(&res.mode, mode_info, sizeof(struct drm_mode_modeinfo));
493 res.connector_id = connector_id;
495 return drmIoctl(fd, DRM_IOCTL_MODE_DETACHMODE, &res);
499 drmModePropertyPtr drmModeGetProperty(int fd, uint32_t property_id)
501 struct drm_mode_get_property prop;
502 drmModePropertyPtr r;
504 prop.prop_id = property_id;
505 prop.count_enum_blobs = 0;
506 prop.count_values = 0;
508 prop.enum_blob_ptr = 0;
511 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPERTY, &prop))
514 if (prop.count_values)
515 prop.values_ptr = VOID2U64(drmMalloc(prop.count_values * sizeof(uint64_t)));
517 if (prop.count_enum_blobs && (prop.flags & DRM_MODE_PROP_ENUM))
518 prop.enum_blob_ptr = VOID2U64(drmMalloc(prop.count_enum_blobs * sizeof(struct drm_mode_property_enum)));
520 if (prop.count_enum_blobs && (prop.flags & DRM_MODE_PROP_BLOB)) {
521 prop.values_ptr = VOID2U64(drmMalloc(prop.count_enum_blobs * sizeof(uint32_t)));
522 prop.enum_blob_ptr = VOID2U64(drmMalloc(prop.count_enum_blobs * sizeof(uint32_t)));
525 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPERTY, &prop)) {
530 if (!(r = drmMalloc(sizeof(*r))))
533 r->prop_id = prop.prop_id;
534 r->count_values = prop.count_values;
536 r->flags = prop.flags;
537 if (prop.count_values)
538 r->values = drmAllocCpy(U642VOID(prop.values_ptr), prop.count_values, sizeof(uint64_t));
539 if (prop.flags & DRM_MODE_PROP_ENUM) {
540 r->count_enums = prop.count_enum_blobs;
541 r->enums = drmAllocCpy(U642VOID(prop.enum_blob_ptr), prop.count_enum_blobs, sizeof(struct drm_mode_property_enum));
542 } else if (prop.flags & DRM_MODE_PROP_BLOB) {
543 r->values = drmAllocCpy(U642VOID(prop.values_ptr), prop.count_enum_blobs, sizeof(uint32_t));
544 r->blob_ids = drmAllocCpy(U642VOID(prop.enum_blob_ptr), prop.count_enum_blobs, sizeof(uint32_t));
545 r->count_blobs = prop.count_enum_blobs;
547 strncpy(r->name, prop.name, DRM_PROP_NAME_LEN);
548 r->name[DRM_PROP_NAME_LEN-1] = 0;
551 drmFree(U642VOID(prop.values_ptr));
552 drmFree(U642VOID(prop.enum_blob_ptr));
557 void drmModeFreeProperty(drmModePropertyPtr ptr)
562 drmFree(ptr->values);
567 drmModePropertyBlobPtr drmModeGetPropertyBlob(int fd, uint32_t blob_id)
569 struct drm_mode_get_blob blob;
570 drmModePropertyBlobPtr r;
574 blob.blob_id = blob_id;
576 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPBLOB, &blob))
580 blob.data = VOID2U64(drmMalloc(blob.length));
582 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPBLOB, &blob)) {
587 if (!(r = drmMalloc(sizeof(*r))))
590 r->id = blob.blob_id;
591 r->length = blob.length;
592 r->data = drmAllocCpy(U642VOID(blob.data), 1, blob.length);
595 drmFree(U642VOID(blob.data));
599 void drmModeFreePropertyBlob(drmModePropertyBlobPtr ptr)
608 int drmModeConnectorSetProperty(int fd, uint32_t connector_id, uint32_t property_id,
611 struct drm_mode_connector_set_property osp;
614 osp.connector_id = connector_id;
615 osp.prop_id = property_id;
618 if ((ret = drmIoctl(fd, DRM_IOCTL_MODE_SETPROPERTY, &osp)))
625 * checks if a modesetting capable driver has attached to the pci id
626 * returns 0 if modesetting supported.
627 * -EINVAL or invalid bus id
628 * -ENOSYS if no modesetting support
630 int drmCheckModesettingSupported(const char *busid)
633 char pci_dev_dir[1024];
634 int domain, bus, dev, func;
639 ret = sscanf(busid, "pci:%04x:%02x:%02x.%d", &domain, &bus, &dev, &func);
643 sprintf(pci_dev_dir, "/sys/bus/pci/devices/%04x:%02x:%02x.%d/drm",
644 domain, bus, dev, func);
646 sysdir = opendir(pci_dev_dir);
648 dent = readdir(sysdir);
650 if (!strncmp(dent->d_name, "controlD", 8)) {
655 dent = readdir(sysdir);
662 sprintf(pci_dev_dir, "/sys/bus/pci/devices/%04x:%02x:%02x.%d/",
663 domain, bus, dev, func);
665 sysdir = opendir(pci_dev_dir);
669 dent = readdir(sysdir);
671 if (!strncmp(dent->d_name, "drm:controlD", 12)) {
676 dent = readdir(sysdir);
687 int drmModeCrtcGetGamma(int fd, uint32_t crtc_id, uint32_t size,
688 uint16_t *red, uint16_t *green, uint16_t *blue)
691 struct drm_mode_crtc_lut l;
695 l.red = VOID2U64(red);
696 l.green = VOID2U64(green);
697 l.blue = VOID2U64(blue);
699 if ((ret = drmIoctl(fd, DRM_IOCTL_MODE_GETGAMMA, &l)))
705 int drmModeCrtcSetGamma(int fd, uint32_t crtc_id, uint32_t size,
706 uint16_t *red, uint16_t *green, uint16_t *blue)
709 struct drm_mode_crtc_lut l;
713 l.red = VOID2U64(red);
714 l.green = VOID2U64(green);
715 l.blue = VOID2U64(blue);
717 if ((ret = drmIoctl(fd, DRM_IOCTL_MODE_SETGAMMA, &l)))
723 int drmHandleEvent(int fd, drmEventContextPtr evctx)
728 struct drm_event_vblank *vblank;
730 /* The DRM read semantics guarantees that we always get only
731 * complete events. */
733 len = read(fd, buffer, sizeof buffer);
741 e = (struct drm_event *) &buffer[i];
743 case DRM_EVENT_VBLANK:
744 if (evctx->version < 1 ||
745 evctx->vblank_handler == NULL)
747 vblank = (struct drm_event_vblank *) e;
748 evctx->vblank_handler(fd,
752 U642VOID (vblank->user_data));
754 case DRM_EVENT_FLIP_COMPLETE:
755 if (evctx->version < 2 ||
756 evctx->page_flip_handler == NULL)
758 vblank = (struct drm_event_vblank *) e;
759 evctx->page_flip_handler(fd,
763 U642VOID (vblank->user_data));
774 int drmModePageFlip(int fd, uint32_t crtc_id, uint32_t fb_id,
775 uint32_t flags, void *user_data)
777 struct drm_mode_crtc_page_flip flip;
780 flip.crtc_id = crtc_id;
781 flip.user_data = VOID2U64(user_data);
785 return drmIoctl(fd, DRM_IOCTL_MODE_PAGE_FLIP, &flip);