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;
249 drmModeCrtcPtr drmModeGetCrtc(int fd, uint32_t crtcId)
251 struct drm_mode_crtc crtc;
254 crtc.crtc_id = crtcId;
256 if (drmIoctl(fd, DRM_IOCTL_MODE_GETCRTC, &crtc))
263 if (!(r = drmMalloc(sizeof(*r))))
266 r->crtc_id = crtc.crtc_id;
269 r->mode_valid = crtc.mode_valid;
271 memcpy(&r->mode, &crtc.mode, sizeof(struct drm_mode_modeinfo));
272 r->buffer_id = crtc.fb_id;
273 r->gamma_size = crtc.gamma_size;
278 int drmModeSetCrtc(int fd, uint32_t crtcId, uint32_t bufferId,
279 uint32_t x, uint32_t y, uint32_t *connectors, int count,
280 drmModeModeInfoPtr mode)
282 struct drm_mode_crtc crtc;
286 crtc.crtc_id = crtcId;
287 crtc.fb_id = bufferId;
288 crtc.set_connectors_ptr = VOID2U64(connectors);
289 crtc.count_connectors = count;
291 memcpy(&crtc.mode, mode, sizeof(struct drm_mode_modeinfo));
296 return drmIoctl(fd, DRM_IOCTL_MODE_SETCRTC, &crtc);
300 * Cursor manipulation
303 int drmModeSetCursor(int fd, uint32_t crtcId, uint32_t bo_handle, uint32_t width, uint32_t height)
305 struct drm_mode_cursor arg;
307 arg.flags = DRM_MODE_CURSOR_BO;
308 arg.crtc_id = crtcId;
311 arg.handle = bo_handle;
313 return drmIoctl(fd, DRM_IOCTL_MODE_CURSOR, &arg);
316 int drmModeMoveCursor(int fd, uint32_t crtcId, int x, int y)
318 struct drm_mode_cursor arg;
320 arg.flags = DRM_MODE_CURSOR_MOVE;
321 arg.crtc_id = crtcId;
325 return drmIoctl(fd, DRM_IOCTL_MODE_CURSOR, &arg);
331 drmModeEncoderPtr drmModeGetEncoder(int fd, uint32_t encoder_id)
333 struct drm_mode_get_encoder enc;
334 drmModeEncoderPtr r = NULL;
336 enc.encoder_id = encoder_id;
337 enc.encoder_type = 0;
338 enc.possible_crtcs = 0;
339 enc.possible_clones = 0;
341 if (drmIoctl(fd, DRM_IOCTL_MODE_GETENCODER, &enc))
344 if (!(r = drmMalloc(sizeof(*r))))
347 r->encoder_id = enc.encoder_id;
348 r->crtc_id = enc.crtc_id;
349 r->encoder_type = enc.encoder_type;
350 r->possible_crtcs = enc.possible_crtcs;
351 r->possible_clones = enc.possible_clones;
357 * Connector manipulation
360 drmModeConnectorPtr drmModeGetConnector(int fd, uint32_t connector_id)
362 struct drm_mode_get_connector conn;
363 drmModeConnectorPtr r = NULL;
365 conn.connector_id = connector_id;
366 conn.connector_type_id = 0;
367 conn.connector_type = 0;
368 conn.count_modes = 0;
370 conn.count_props = 0;
372 conn.prop_values_ptr = 0;
373 conn.count_encoders = 0;
374 conn.encoders_ptr = 0;
376 if (drmIoctl(fd, DRM_IOCTL_MODE_GETCONNECTOR, &conn))
379 if (conn.count_props) {
380 conn.props_ptr = VOID2U64(drmMalloc(conn.count_props*sizeof(uint32_t)));
381 conn.prop_values_ptr = VOID2U64(drmMalloc(conn.count_props*sizeof(uint64_t)));
384 if (conn.count_modes)
385 conn.modes_ptr = VOID2U64(drmMalloc(conn.count_modes*sizeof(struct drm_mode_modeinfo)));
387 if (conn.count_encoders)
388 conn.encoders_ptr = VOID2U64(drmMalloc(conn.count_encoders*sizeof(uint32_t)));
390 if (drmIoctl(fd, DRM_IOCTL_MODE_GETCONNECTOR, &conn))
393 if(!(r = drmMalloc(sizeof(*r)))) {
397 r->connector_id = conn.connector_id;
398 r->encoder_id = conn.encoder_id;
399 r->connection = conn.connection;
400 r->mmWidth = conn.mm_width;
401 r->mmHeight = conn.mm_height;
402 /* convert subpixel from kernel to userspace */
403 r->subpixel = conn.subpixel + 1;
404 r->count_modes = conn.count_modes;
405 /* TODO we should test if these alloc & cpy fails. */
406 r->count_props = conn.count_props;
407 r->props = drmAllocCpy(U642VOID(conn.props_ptr), conn.count_props, sizeof(uint32_t));
408 r->prop_values = drmAllocCpy(U642VOID(conn.prop_values_ptr), conn.count_props, sizeof(uint64_t));
409 r->modes = drmAllocCpy(U642VOID(conn.modes_ptr), conn.count_modes, sizeof(struct drm_mode_modeinfo));
410 r->count_encoders = conn.count_encoders;
411 r->encoders = drmAllocCpy(U642VOID(conn.encoders_ptr), conn.count_encoders, sizeof(uint32_t));
412 r->connector_type = conn.connector_type;
413 r->connector_type_id = conn.connector_type_id;
415 if (!r->props || !r->prop_values || !r->modes || !r->encoders)
419 drmFree(U642VOID(conn.prop_values_ptr));
420 drmFree(U642VOID(conn.props_ptr));
421 drmFree(U642VOID(conn.modes_ptr));
422 drmFree(U642VOID(conn.encoders_ptr));
427 int drmModeAttachMode(int fd, uint32_t connector_id, drmModeModeInfoPtr mode_info)
429 struct drm_mode_mode_cmd res;
431 memcpy(&res.mode, mode_info, sizeof(struct drm_mode_modeinfo));
432 res.connector_id = connector_id;
434 return drmIoctl(fd, DRM_IOCTL_MODE_ATTACHMODE, &res);
437 int drmModeDetachMode(int fd, uint32_t connector_id, drmModeModeInfoPtr mode_info)
439 struct drm_mode_mode_cmd res;
441 memcpy(&res.mode, mode_info, sizeof(struct drm_mode_modeinfo));
442 res.connector_id = connector_id;
444 return drmIoctl(fd, DRM_IOCTL_MODE_DETACHMODE, &res);
448 drmModePropertyPtr drmModeGetProperty(int fd, uint32_t property_id)
450 struct drm_mode_get_property prop;
451 drmModePropertyPtr r;
453 prop.prop_id = property_id;
454 prop.count_enum_blobs = 0;
455 prop.count_values = 0;
457 prop.enum_blob_ptr = 0;
460 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPERTY, &prop))
463 if (prop.count_values)
464 prop.values_ptr = VOID2U64(drmMalloc(prop.count_values * sizeof(uint64_t)));
466 if (prop.count_enum_blobs && (prop.flags & DRM_MODE_PROP_ENUM))
467 prop.enum_blob_ptr = VOID2U64(drmMalloc(prop.count_enum_blobs * sizeof(struct drm_mode_property_enum)));
469 if (prop.count_enum_blobs && (prop.flags & DRM_MODE_PROP_BLOB)) {
470 prop.values_ptr = VOID2U64(drmMalloc(prop.count_enum_blobs * sizeof(uint32_t)));
471 prop.enum_blob_ptr = VOID2U64(drmMalloc(prop.count_enum_blobs * sizeof(uint32_t)));
474 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPERTY, &prop)) {
479 if (!(r = drmMalloc(sizeof(*r))))
482 r->prop_id = prop.prop_id;
483 r->count_values = prop.count_values;
485 r->flags = prop.flags;
486 if (prop.count_values)
487 r->values = drmAllocCpy(U642VOID(prop.values_ptr), prop.count_values, sizeof(uint64_t));
488 if (prop.flags & DRM_MODE_PROP_ENUM) {
489 r->count_enums = prop.count_enum_blobs;
490 r->enums = drmAllocCpy(U642VOID(prop.enum_blob_ptr), prop.count_enum_blobs, sizeof(struct drm_mode_property_enum));
491 } else if (prop.flags & DRM_MODE_PROP_BLOB) {
492 r->values = drmAllocCpy(U642VOID(prop.values_ptr), prop.count_enum_blobs, sizeof(uint32_t));
493 r->blob_ids = drmAllocCpy(U642VOID(prop.enum_blob_ptr), prop.count_enum_blobs, sizeof(uint32_t));
494 r->count_blobs = prop.count_enum_blobs;
496 strncpy(r->name, prop.name, DRM_PROP_NAME_LEN);
497 r->name[DRM_PROP_NAME_LEN-1] = 0;
500 drmFree(U642VOID(prop.values_ptr));
501 drmFree(U642VOID(prop.enum_blob_ptr));
506 void drmModeFreeProperty(drmModePropertyPtr ptr)
511 drmFree(ptr->values);
516 drmModePropertyBlobPtr drmModeGetPropertyBlob(int fd, uint32_t blob_id)
518 struct drm_mode_get_blob blob;
519 drmModePropertyBlobPtr r;
523 blob.blob_id = blob_id;
525 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPBLOB, &blob))
529 blob.data = VOID2U64(drmMalloc(blob.length));
531 if (drmIoctl(fd, DRM_IOCTL_MODE_GETPROPBLOB, &blob)) {
536 if (!(r = drmMalloc(sizeof(*r))))
539 r->id = blob.blob_id;
540 r->length = blob.length;
541 r->data = drmAllocCpy(U642VOID(blob.data), 1, blob.length);
544 drmFree(U642VOID(blob.data));
548 void drmModeFreePropertyBlob(drmModePropertyBlobPtr ptr)
557 int drmModeConnectorSetProperty(int fd, uint32_t connector_id, uint32_t property_id,
560 struct drm_mode_connector_set_property osp;
563 osp.connector_id = connector_id;
564 osp.prop_id = property_id;
567 if ((ret = drmIoctl(fd, DRM_IOCTL_MODE_SETPROPERTY, &osp)))
574 * checks if a modesetting capable driver has attached to the pci id
575 * returns 0 if modesetting supported.
576 * -EINVAL or invalid bus id
577 * -ENOSYS if no modesetting support
579 int drmCheckModesettingSupported(const char *busid)
582 char pci_dev_dir[1024];
583 int domain, bus, dev, func;
588 ret = sscanf(busid, "pci:%04x:%02x:%02x.%d", &domain, &bus, &dev, &func);
592 sprintf(pci_dev_dir, "/sys/bus/pci/devices/%04x:%02x:%02x.%d/drm",
593 domain, bus, dev, func);
595 sysdir = opendir(pci_dev_dir);
597 dent = readdir(sysdir);
599 if (!strncmp(dent->d_name, "controlD", 8)) {
604 dent = readdir(sysdir);
611 sprintf(pci_dev_dir, "/sys/bus/pci/devices/%04x:%02x:%02x.%d/",
612 domain, bus, dev, func);
614 sysdir = opendir(pci_dev_dir);
618 dent = readdir(sysdir);
620 if (!strncmp(dent->d_name, "drm:controlD", 12)) {
625 dent = readdir(sysdir);
636 int drmModeCrtcGetGamma(int fd, uint32_t crtc_id, uint32_t size,
637 uint16_t *red, uint16_t *green, uint16_t *blue)
640 struct drm_mode_crtc_lut l;
644 l.red = VOID2U64(red);
645 l.green = VOID2U64(green);
646 l.blue = VOID2U64(blue);
648 if ((ret = drmIoctl(fd, DRM_IOCTL_MODE_GETGAMMA, &l)))
654 int drmModeCrtcSetGamma(int fd, uint32_t crtc_id, uint32_t size,
655 uint16_t *red, uint16_t *green, uint16_t *blue)
658 struct drm_mode_crtc_lut l;
662 l.red = VOID2U64(red);
663 l.green = VOID2U64(green);
664 l.blue = VOID2U64(blue);
666 if ((ret = drmIoctl(fd, DRM_IOCTL_MODE_SETGAMMA, &l)))
672 int drmHandleEvent(int fd, drmEventContextPtr evctx)
677 struct drm_event_vblank *vblank;
679 /* The DRM read semantics guarantees that we always get only
680 * complete events. */
682 len = read(fd, buffer, sizeof buffer);
690 e = (struct drm_event *) &buffer[i];
692 case DRM_EVENT_VBLANK:
693 if (evctx->version < 1 ||
694 evctx->vblank_handler == NULL)
696 vblank = (struct drm_event_vblank *) e;
697 evctx->vblank_handler(fd,
701 U642VOID (vblank->user_data));