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 uint32_t drmModeGetHotplug(int fd)
188 struct drm_mode_hotplug arg;
191 ioctl(fd, DRM_IOCTL_MODE_HOTPLUG, &arg);
195 int drmModeAddFB(int fd, uint32_t width, uint32_t height, uint8_t depth,
196 uint8_t bpp, uint32_t pitch, uint32_t bo_handle,
199 struct drm_mode_fb_cmd f;
207 f.handle = bo_handle;
209 if ((ret = ioctl(fd, DRM_IOCTL_MODE_ADDFB, &f)))
212 *buf_id = f.buffer_id;
216 int drmModeRmFB(int fd, uint32_t bufferId)
218 return ioctl(fd, DRM_IOCTL_MODE_RMFB, &bufferId);
223 drmModeFBPtr drmModeGetFB(int fd, uint32_t buf)
225 struct drm_mode_fb_cmd info;
228 info.buffer_id = buf;
230 if (ioctl(fd, DRM_IOCTL_MODE_GETFB, &info))
233 if (!(r = drmMalloc(sizeof(*r))))
236 r->buffer_id = info.buffer_id;
237 r->width = info.width;
238 r->height = info.height;
239 r->pitch = info.pitch;
241 r->handle = info.handle;
242 r->depth = info.depth;
252 drmModeCrtcPtr drmModeGetCrtc(int fd, uint32_t crtcId)
254 struct drm_mode_crtc crtc;
257 crtc.crtc_id = crtcId;
259 if (ioctl(fd, DRM_IOCTL_MODE_GETCRTC, &crtc))
266 if (!(r = drmMalloc(sizeof(*r))))
269 r->crtc_id = crtc.crtc_id;
272 r->mode_valid = crtc.mode_valid;
274 memcpy(&r->mode, &crtc.mode, sizeof(struct drm_mode_modeinfo));
275 r->buffer_id = crtc.fb_id;
276 r->gamma_size = crtc.gamma_size;
281 int drmModeSetCrtc(int fd, uint32_t crtcId, uint32_t bufferId,
282 uint32_t x, uint32_t y, uint32_t *connectors, int count,
283 struct drm_mode_modeinfo *mode)
285 struct drm_mode_crtc crtc;
289 crtc.crtc_id = crtcId;
290 crtc.fb_id = bufferId;
291 crtc.set_connectors_ptr = VOID2U64(connectors);
292 crtc.count_connectors = count;
294 memcpy(&crtc.mode, mode, sizeof(struct drm_mode_modeinfo));
299 return ioctl(fd, DRM_IOCTL_MODE_SETCRTC, &crtc);
303 * Cursor manipulation
306 int drmModeSetCursor(int fd, uint32_t crtcId, uint32_t bo_handle, uint32_t width, uint32_t height)
308 struct drm_mode_cursor arg;
310 arg.flags = DRM_MODE_CURSOR_BO;
311 arg.crtc_id = crtcId;
314 arg.handle = bo_handle;
316 return ioctl(fd, DRM_IOCTL_MODE_CURSOR, &arg);
319 int drmModeMoveCursor(int fd, uint32_t crtcId, int x, int y)
321 struct drm_mode_cursor arg;
323 arg.flags = DRM_MODE_CURSOR_MOVE;
324 arg.crtc_id = crtcId;
328 return ioctl(fd, DRM_IOCTL_MODE_CURSOR, &arg);
334 drmModeEncoderPtr drmModeGetEncoder(int fd, uint32_t encoder_id)
336 struct drm_mode_get_encoder enc;
337 drmModeEncoderPtr r = NULL;
339 enc.encoder_id = encoder_id;
340 enc.encoder_type = 0;
341 enc.possible_crtcs = 0;
342 enc.possible_clones = 0;
344 if (ioctl(fd, DRM_IOCTL_MODE_GETENCODER, &enc))
347 if (!(r = drmMalloc(sizeof(*r))))
350 r->encoder_id = enc.encoder_id;
351 r->crtc_id = enc.crtc_id;
352 r->encoder_type = enc.encoder_type;
353 r->possible_crtcs = enc.possible_crtcs;
354 r->possible_clones = enc.possible_clones;
360 * Connector manipulation
363 drmModeConnectorPtr drmModeGetConnector(int fd, uint32_t connector_id)
365 struct drm_mode_get_connector conn;
366 drmModeConnectorPtr r = NULL;
368 conn.connector_id = connector_id;
369 conn.connector_type_id = 0;
370 conn.connector_type = 0;
371 conn.count_modes = 0;
373 conn.count_props = 0;
375 conn.prop_values_ptr = 0;
376 conn.count_encoders = 0;
377 conn.encoders_ptr = 0;
379 if (ioctl(fd, DRM_IOCTL_MODE_GETCONNECTOR, &conn))
382 if (conn.count_props) {
383 conn.props_ptr = VOID2U64(drmMalloc(conn.count_props*sizeof(uint32_t)));
384 conn.prop_values_ptr = VOID2U64(drmMalloc(conn.count_props*sizeof(uint64_t)));
387 if (conn.count_modes)
388 conn.modes_ptr = VOID2U64(drmMalloc(conn.count_modes*sizeof(struct drm_mode_modeinfo)));
390 if (conn.count_encoders)
391 conn.encoders_ptr = VOID2U64(drmMalloc(conn.count_encoders*sizeof(uint32_t)));
393 if (ioctl(fd, DRM_IOCTL_MODE_GETCONNECTOR, &conn))
396 if(!(r = drmMalloc(sizeof(*r)))) {
400 r->connector_id = conn.connector_id;
401 r->encoder_id = conn.encoder_id;
402 r->connection = conn.connection;
403 r->mmWidth = conn.mm_width;
404 r->mmHeight = conn.mm_height;
405 r->subpixel = conn.subpixel;
406 r->count_modes = conn.count_modes;
407 /* TODO we should test if these alloc & cpy fails. */
408 r->count_props = conn.count_props;
409 r->props = drmAllocCpy(U642VOID(conn.props_ptr), conn.count_props, sizeof(uint32_t));
410 r->prop_values = drmAllocCpy(U642VOID(conn.prop_values_ptr), conn.count_props, sizeof(uint64_t));
411 r->modes = drmAllocCpy(U642VOID(conn.modes_ptr), conn.count_modes, sizeof(struct drm_mode_modeinfo));
412 r->count_encoders = conn.count_encoders;
413 r->encoders = drmAllocCpy(U642VOID(conn.encoders_ptr), conn.count_encoders, sizeof(uint32_t));
414 r->connector_type = conn.connector_type;
415 r->connector_type_id = conn.connector_type_id;
417 if (!r->props || !r->prop_values || !r->modes || !r->encoders)
421 drmFree(U642VOID(conn.prop_values_ptr));
422 drmFree(U642VOID(conn.props_ptr));
423 drmFree(U642VOID(conn.modes_ptr));
424 drmFree(U642VOID(conn.encoders_ptr));
429 int drmModeAttachMode(int fd, uint32_t connector_id, struct drm_mode_modeinfo *mode_info)
431 struct drm_mode_mode_cmd res;
433 memcpy(&res.mode, mode_info, sizeof(struct drm_mode_modeinfo));
434 res.connector_id = connector_id;
436 return ioctl(fd, DRM_IOCTL_MODE_ATTACHMODE, &res);
439 int drmModeDetachMode(int fd, uint32_t connector_id, struct drm_mode_modeinfo *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 ioctl(fd, DRM_IOCTL_MODE_DETACHMODE, &res);
450 drmModePropertyPtr drmModeGetProperty(int fd, uint32_t property_id)
452 struct drm_mode_get_property prop;
453 drmModePropertyPtr r;
455 prop.prop_id = property_id;
456 prop.count_enum_blobs = 0;
457 prop.count_values = 0;
459 prop.enum_blob_ptr = 0;
462 if (ioctl(fd, DRM_IOCTL_MODE_GETPROPERTY, &prop))
465 if (prop.count_values)
466 prop.values_ptr = VOID2U64(drmMalloc(prop.count_values * sizeof(uint64_t)));
468 if (prop.count_enum_blobs && (prop.flags & DRM_MODE_PROP_ENUM))
469 prop.enum_blob_ptr = VOID2U64(drmMalloc(prop.count_enum_blobs * sizeof(struct drm_mode_property_enum)));
471 if (prop.count_enum_blobs && (prop.flags & DRM_MODE_PROP_BLOB)) {
472 prop.values_ptr = VOID2U64(drmMalloc(prop.count_enum_blobs * sizeof(uint32_t)));
473 prop.enum_blob_ptr = VOID2U64(drmMalloc(prop.count_enum_blobs * sizeof(uint32_t)));
476 if (ioctl(fd, DRM_IOCTL_MODE_GETPROPERTY, &prop)) {
481 if (!(r = drmMalloc(sizeof(*r))))
484 r->prop_id = prop.prop_id;
485 r->count_values = prop.count_values;
487 r->flags = prop.flags;
488 if (prop.count_values)
489 r->values = drmAllocCpy(U642VOID(prop.values_ptr), prop.count_values, sizeof(uint64_t));
490 if (prop.flags & DRM_MODE_PROP_ENUM) {
491 r->count_enums = prop.count_enum_blobs;
492 r->enums = drmAllocCpy(U642VOID(prop.enum_blob_ptr), prop.count_enum_blobs, sizeof(struct drm_mode_property_enum));
493 } else if (prop.flags & DRM_MODE_PROP_BLOB) {
494 r->values = drmAllocCpy(U642VOID(prop.values_ptr), prop.count_enum_blobs, sizeof(uint32_t));
495 r->blob_ids = drmAllocCpy(U642VOID(prop.enum_blob_ptr), prop.count_enum_blobs, sizeof(uint32_t));
496 r->count_blobs = prop.count_enum_blobs;
498 strncpy(r->name, prop.name, DRM_PROP_NAME_LEN);
499 r->name[DRM_PROP_NAME_LEN-1] = 0;
502 drmFree(U642VOID(prop.values_ptr));
503 drmFree(U642VOID(prop.enum_blob_ptr));
508 void drmModeFreeProperty(drmModePropertyPtr ptr)
513 drmFree(ptr->values);
518 drmModePropertyBlobPtr drmModeGetPropertyBlob(int fd, uint32_t blob_id)
520 struct drm_mode_get_blob blob;
521 drmModePropertyBlobPtr r;
525 blob.blob_id = blob_id;
527 if (ioctl(fd, DRM_IOCTL_MODE_GETPROPBLOB, &blob))
531 blob.data = VOID2U64(drmMalloc(blob.length));
533 if (ioctl(fd, DRM_IOCTL_MODE_GETPROPBLOB, &blob)) {
538 if (!(r = drmMalloc(sizeof(*r))))
541 r->id = blob.blob_id;
542 r->length = blob.length;
543 r->data = drmAllocCpy(U642VOID(blob.data), 1, blob.length);
546 drmFree(U642VOID(blob.data));
550 void drmModeFreePropertyBlob(drmModePropertyBlobPtr ptr)
559 int drmModeConnectorSetProperty(int fd, uint32_t connector_id, uint32_t property_id,
562 struct drm_mode_connector_set_property osp;
565 osp.connector_id = connector_id;
566 osp.prop_id = property_id;
569 if ((ret = ioctl(fd, DRM_IOCTL_MODE_SETPROPERTY, &osp)))
576 * checks if a modesetting capable driver has attached to the pci id
577 * returns 0 if modesetting supported.
578 * -EINVAL or invalid bus id
579 * -ENOSYS if no modesetting support
581 int drmCheckModesettingSupported(const char *busid)
584 char pci_dev_dir[1024];
585 int domain, bus, dev, func;
590 ret = sscanf(busid, "pci:%04x:%02x:%02x.%d", &domain, &bus, &dev, &func);
594 sprintf(pci_dev_dir, "/sys/bus/pci/devices/%04x:%02x:%02x.%d/drm",
595 domain, bus, dev, func);
597 sysdir = opendir(pci_dev_dir);
599 dent = readdir(sysdir);
601 if (!strncmp(dent->d_name, "controlD", 8)) {
606 dent = readdir(sysdir);
613 sprintf(pci_dev_dir, "/sys/bus/pci/devices/%04x:%02x:%02x.%d/",
614 domain, bus, dev, func);
616 sysdir = opendir(pci_dev_dir);
620 dent = readdir(sysdir);
622 if (!strncmp(dent->d_name, "drm:controlD", 12)) {
627 dent = readdir(sysdir);
638 int drmModeReplaceFB(int fd, uint32_t buffer_id,
639 uint32_t width, uint32_t height, uint8_t depth,
640 uint8_t bpp, uint32_t pitch, uint32_t bo_handle)
642 struct drm_mode_fb_cmd f;
650 f.handle = bo_handle;
651 f.buffer_id = buffer_id;
653 if ((ret = ioctl(fd, DRM_IOCTL_MODE_REPLACEFB, &f)))
659 int drmModeCrtcGetGamma(int fd, uint32_t crtc_id, uint32_t size,
660 uint16_t *red, uint16_t *green, uint16_t *blue)
663 struct drm_mode_crtc_lut l;
667 l.red = VOID2U64(red);
668 l.green = VOID2U64(green);
669 l.blue = VOID2U64(blue);
671 if ((ret = ioctl(fd, DRM_IOCTL_MODE_GETGAMMA, &l)))
677 int drmModeCrtcSetGamma(int fd, uint32_t crtc_id, uint32_t size,
678 uint16_t *red, uint16_t *green, uint16_t *blue)
681 struct drm_mode_crtc_lut l;
685 l.red = VOID2U64(red);
686 l.green = VOID2U64(green);
687 l.blue = VOID2U64(blue);
689 if ((ret = ioctl(fd, DRM_IOCTL_MODE_SETGAMMA, &l)))