Hint added.
[AROS.git] / workbench / hidds / nouveau / drm / libdrm / arosdrmmode.c
blob3466bb6ae2baf078bea132cde40c08a059c19a99
1 /*
2 * Copyright (c) 2007-2008 Tungsten Graphics, Inc., Cedar Park, Texas.
3 * Copyright (c) 2007-2008 Dave Airlie <airlied@linux.ie>
4 * Copyright (c) 2007-2008 Jakob Bornecrantz <wallbraker@gmail.com>
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22 * IN THE SOFTWARE.
26 #include "arosdrm.h"
27 #include "arosdrmmode.h"
29 #include <string.h>
31 #define U642VOID(x) ((void *)(unsigned long)(x))
32 #define VOID2U64(x) ((uint64_t)(unsigned long)(x))
34 static inline int DRM_IOCTL(int fd, int cmd, void *arg)
36 int ret = drmIoctl(fd, cmd, arg);
37 return ret < 0 ? -errno : ret;
41 * Util functions
44 void* drmAllocCpy(void *array, int count, int entry_size)
46 char *r;
47 int i;
49 if (!count || !array || !entry_size)
50 return 0;
52 if (!(r = drmMalloc(count*entry_size)))
53 return 0;
55 for (i = 0; i < count; i++)
56 memcpy(r+(entry_size*i), array+(entry_size*i), entry_size);
58 return r;
62 * A couple of free functions.
65 void drmModeFreeModeInfo(drmModeModeInfoPtr ptr)
67 if (!ptr)
68 return;
70 drmFree(ptr);
73 void drmModeFreeResources(drmModeResPtr ptr)
75 if (!ptr)
76 return;
78 drmFree(ptr);
82 void drmModeFreeCrtc(drmModeCrtcPtr ptr)
84 if (!ptr)
85 return;
87 drmFree(ptr);
91 void drmModeFreeConnector(drmModeConnectorPtr ptr)
93 if (!ptr)
94 return;
96 drmFree(ptr->encoders);
97 drmFree(ptr->prop_values);
98 drmFree(ptr->props);
99 drmFree(ptr->modes);
100 drmFree(ptr);
104 void drmModeFreeEncoder(drmModeEncoderPtr ptr)
106 drmFree(ptr);
110 * ModeSetting functions.
113 drmModeResPtr drmModeGetResources(int fd)
115 struct drm_mode_card_res res, counts;
116 drmModeResPtr r = 0;
118 retry:
119 memset(&res, 0, sizeof(struct drm_mode_card_res));
120 if (drmIoctl(fd, DRM_IOCTL_MODE_GETRESOURCES, &res))
121 return 0;
123 counts = res;
125 if (res.count_fbs) {
126 res.fb_id_ptr = VOID2U64(drmMalloc(res.count_fbs*sizeof(uint32_t)));
127 if (!res.fb_id_ptr)
128 goto err_allocs;
130 if (res.count_crtcs) {
131 res.crtc_id_ptr = VOID2U64(drmMalloc(res.count_crtcs*sizeof(uint32_t)));
132 if (!res.crtc_id_ptr)
133 goto err_allocs;
135 if (res.count_connectors) {
136 res.connector_id_ptr = VOID2U64(drmMalloc(res.count_connectors*sizeof(uint32_t)));
137 if (!res.connector_id_ptr)
138 goto err_allocs;
140 if (res.count_encoders) {
141 res.encoder_id_ptr = VOID2U64(drmMalloc(res.count_encoders*sizeof(uint32_t)));
142 if (!res.encoder_id_ptr)
143 goto err_allocs;
146 if (drmIoctl(fd, DRM_IOCTL_MODE_GETRESOURCES, &res))
147 goto err_allocs;
149 /* The number of available connectors and etc may have changed with a
150 * hotplug event in between the ioctls, in which case the field is
151 * silently ignored by the kernel.
153 if (counts.count_fbs < res.count_fbs ||
154 counts.count_crtcs < res.count_crtcs ||
155 counts.count_connectors < res.count_connectors ||
156 counts.count_encoders < res.count_encoders)
158 drmFree(U642VOID(res.fb_id_ptr));
159 drmFree(U642VOID(res.crtc_id_ptr));
160 drmFree(U642VOID(res.connector_id_ptr));
161 drmFree(U642VOID(res.encoder_id_ptr));
163 goto retry;
167 * return
169 if (!(r = drmMalloc(sizeof(*r))))
170 goto err_allocs;
172 r->min_width = res.min_width;
173 r->max_width = res.max_width;
174 r->min_height = res.min_height;
175 r->max_height = res.max_height;
176 r->count_fbs = res.count_fbs;
177 r->count_crtcs = res.count_crtcs;
178 r->count_connectors = res.count_connectors;
179 r->count_encoders = res.count_encoders;
181 r->fbs = drmAllocCpy(U642VOID(res.fb_id_ptr), res.count_fbs, sizeof(uint32_t));
182 r->crtcs = drmAllocCpy(U642VOID(res.crtc_id_ptr), res.count_crtcs, sizeof(uint32_t));
183 r->connectors = drmAllocCpy(U642VOID(res.connector_id_ptr), res.count_connectors, sizeof(uint32_t));
184 r->encoders = drmAllocCpy(U642VOID(res.encoder_id_ptr), res.count_encoders, sizeof(uint32_t));
185 if ((res.count_fbs && !r->fbs) ||
186 (res.count_crtcs && !r->crtcs) ||
187 (res.count_connectors && !r->connectors) ||
188 (res.count_encoders && !r->encoders))
190 drmFree(r->fbs);
191 drmFree(r->crtcs);
192 drmFree(r->connectors);
193 drmFree(r->encoders);
194 drmFree(r);
195 r = 0;
198 err_allocs:
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));
204 return r;
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,
209 uint32_t *buf_id)
211 struct drm_mode_fb_cmd f;
212 int ret;
214 f.width = width;
215 f.height = height;
216 f.pitch = pitch;
217 f.bpp = bpp;
218 f.depth = depth;
219 f.handle = bo_handle;
221 if ((ret = DRM_IOCTL(fd, DRM_IOCTL_MODE_ADDFB, &f)))
222 return ret;
224 *buf_id = f.fb_id;
225 return 0;
228 int drmModeRmFB(int fd, uint32_t bufferId)
230 return DRM_IOCTL(fd, DRM_IOCTL_MODE_RMFB, &bufferId);
236 * Crtc functions
239 drmModeCrtcPtr drmModeGetCrtc(int fd, uint32_t crtcId)
241 struct drm_mode_crtc crtc;
242 drmModeCrtcPtr r;
244 crtc.crtc_id = crtcId;
246 if (drmIoctl(fd, DRM_IOCTL_MODE_GETCRTC, &crtc))
247 return 0;
250 * return
253 if (!(r = drmMalloc(sizeof(*r))))
254 return 0;
256 r->crtc_id = crtc.crtc_id;
257 r->x = crtc.x;
258 r->y = crtc.y;
259 r->mode_valid = crtc.mode_valid;
260 if (r->mode_valid)
261 memcpy(&r->mode, &crtc.mode, sizeof(struct drm_mode_modeinfo));
262 r->buffer_id = crtc.fb_id;
263 r->gamma_size = crtc.gamma_size;
264 return r;
268 int drmModeSetCrtc(int fd, uint32_t crtcId, uint32_t bufferId,
269 uint32_t x, uint32_t y, uint32_t *connectors, int count,
270 drmModeModeInfoPtr mode)
272 struct drm_mode_crtc crtc;
274 crtc.x = x;
275 crtc.y = y;
276 crtc.crtc_id = crtcId;
277 crtc.fb_id = bufferId;
278 crtc.set_connectors_ptr = VOID2U64(connectors);
279 crtc.count_connectors = count;
280 if (mode) {
281 memcpy(&crtc.mode, mode, sizeof(struct drm_mode_modeinfo));
282 crtc.mode_valid = 1;
283 } else
284 crtc.mode_valid = 0;
286 return DRM_IOCTL(fd, DRM_IOCTL_MODE_SETCRTC, &crtc);
290 * Cursor manipulation
293 int drmModeSetCursor(int fd, uint32_t crtcId, uint32_t bo_handle, uint32_t width, uint32_t height)
295 struct drm_mode_cursor arg;
297 arg.flags = DRM_MODE_CURSOR_BO;
298 arg.crtc_id = crtcId;
299 arg.width = width;
300 arg.height = height;
301 arg.handle = bo_handle;
303 return DRM_IOCTL(fd, DRM_IOCTL_MODE_CURSOR, &arg);
306 int drmModeMoveCursor(int fd, uint32_t crtcId, int x, int y)
308 struct drm_mode_cursor arg;
310 arg.flags = DRM_MODE_CURSOR_MOVE;
311 arg.crtc_id = crtcId;
312 arg.x = x;
313 arg.y = y;
315 return DRM_IOCTL(fd, DRM_IOCTL_MODE_CURSOR, &arg);
319 * Encoder get
321 drmModeEncoderPtr drmModeGetEncoder(int fd, uint32_t encoder_id)
323 struct drm_mode_get_encoder enc;
324 drmModeEncoderPtr r = NULL;
326 enc.encoder_id = encoder_id;
327 enc.encoder_type = 0;
328 enc.possible_crtcs = 0;
329 enc.possible_clones = 0;
331 if (drmIoctl(fd, DRM_IOCTL_MODE_GETENCODER, &enc))
332 return 0;
334 if (!(r = drmMalloc(sizeof(*r))))
335 return 0;
337 r->encoder_id = enc.encoder_id;
338 r->crtc_id = enc.crtc_id;
339 r->encoder_type = enc.encoder_type;
340 r->possible_crtcs = enc.possible_crtcs;
341 r->possible_clones = enc.possible_clones;
343 return r;
347 * Connector manipulation
350 drmModeConnectorPtr drmModeGetConnector(int fd, uint32_t connector_id)
352 struct drm_mode_get_connector conn, counts;
353 drmModeConnectorPtr r = NULL;
355 retry:
356 memset(&conn, 0, sizeof(struct drm_mode_get_connector));
357 conn.connector_id = connector_id;
359 if (drmIoctl(fd, DRM_IOCTL_MODE_GETCONNECTOR, &conn))
360 return 0;
362 counts = conn;
364 if (conn.count_props) {
365 conn.props_ptr = VOID2U64(drmMalloc(conn.count_props*sizeof(uint32_t)));
366 if (!conn.props_ptr)
367 goto err_allocs;
368 conn.prop_values_ptr = VOID2U64(drmMalloc(conn.count_props*sizeof(uint64_t)));
369 if (!conn.prop_values_ptr)
370 goto err_allocs;
373 if (conn.count_modes) {
374 conn.modes_ptr = VOID2U64(drmMalloc(conn.count_modes*sizeof(struct drm_mode_modeinfo)));
375 if (!conn.modes_ptr)
376 goto err_allocs;
379 if (conn.count_encoders) {
380 conn.encoders_ptr = VOID2U64(drmMalloc(conn.count_encoders*sizeof(uint32_t)));
381 if (!conn.encoders_ptr)
382 goto err_allocs;
385 if (drmIoctl(fd, DRM_IOCTL_MODE_GETCONNECTOR, &conn))
386 goto err_allocs;
388 /* The number of available connectors and etc may have changed with a
389 * hotplug event in between the ioctls, in which case the field is
390 * silently ignored by the kernel.
392 if (counts.count_props < conn.count_props ||
393 counts.count_modes < conn.count_modes ||
394 counts.count_encoders < conn.count_encoders) {
395 drmFree(U642VOID(conn.props_ptr));
396 drmFree(U642VOID(conn.prop_values_ptr));
397 drmFree(U642VOID(conn.modes_ptr));
398 drmFree(U642VOID(conn.encoders_ptr));
400 goto retry;
403 if(!(r = drmMalloc(sizeof(*r)))) {
404 goto err_allocs;
407 r->connector_id = conn.connector_id;
408 r->encoder_id = conn.encoder_id;
409 r->connection = conn.connection;
410 r->mmWidth = conn.mm_width;
411 r->mmHeight = conn.mm_height;
412 /* convert subpixel from kernel to userspace */
413 r->subpixel = conn.subpixel + 1;
414 r->count_modes = conn.count_modes;
415 r->count_props = conn.count_props;
416 r->props = drmAllocCpy(U642VOID(conn.props_ptr), conn.count_props, sizeof(uint32_t));
417 r->prop_values = drmAllocCpy(U642VOID(conn.prop_values_ptr), conn.count_props, sizeof(uint64_t));
418 r->modes = drmAllocCpy(U642VOID(conn.modes_ptr), conn.count_modes, sizeof(struct drm_mode_modeinfo));
419 r->count_encoders = conn.count_encoders;
420 r->encoders = drmAllocCpy(U642VOID(conn.encoders_ptr), conn.count_encoders, sizeof(uint32_t));
421 r->connector_type = conn.connector_type;
422 r->connector_type_id = conn.connector_type_id;
424 if ((r->count_props && !r->props) ||
425 (r->count_props && !r->prop_values) ||
426 (r->count_modes && !r->modes) ||
427 (r->count_encoders && !r->encoders)) {
428 drmFree(r->props);
429 drmFree(r->prop_values);
430 drmFree(r->modes);
431 drmFree(r->encoders);
432 drmFree(r);
433 r = 0;
436 err_allocs:
437 drmFree(U642VOID(conn.prop_values_ptr));
438 drmFree(U642VOID(conn.props_ptr));
439 drmFree(U642VOID(conn.modes_ptr));
440 drmFree(U642VOID(conn.encoders_ptr));
442 return r;
445 int drmModeAttachMode(int fd, uint32_t connector_id, drmModeModeInfoPtr mode_info)
447 struct drm_mode_mode_cmd res;
449 memcpy(&res.mode, mode_info, sizeof(struct drm_mode_modeinfo));
450 res.connector_id = connector_id;
452 return DRM_IOCTL(fd, DRM_IOCTL_MODE_ATTACHMODE, &res);
455 int drmModeDetachMode(int fd, uint32_t connector_id, drmModeModeInfoPtr mode_info)
457 struct drm_mode_mode_cmd res;
459 memcpy(&res.mode, mode_info, sizeof(struct drm_mode_modeinfo));
460 res.connector_id = connector_id;
462 return DRM_IOCTL(fd, DRM_IOCTL_MODE_DETACHMODE, &res);