Hint added.
[AROS.git] / workbench / hidds / nouveau / drm / drm / drm_crtc.c
blob6c59189aa03f0c74a6c7f2af141a27c3e8518d60
1 /*
2 * Copyright (c) 2006-2008 Intel Corporation
3 * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
4 * Copyright (c) 2008 Red Hat Inc.
6 * DRM core CRTC related functions
8 * Permission to use, copy, modify, distribute, and sell this software and its
9 * documentation for any purpose is hereby granted without fee, provided that
10 * the above copyright notice appear in all copies and that both that copyright
11 * notice and this permission notice appear in supporting documentation, and
12 * that the name of the copyright holders not be used in advertising or
13 * publicity pertaining to distribution of the software without specific,
14 * written prior permission. The copyright holders make no representations
15 * about the suitability of this software for any purpose. It is provided "as
16 * is" without express or implied warranty.
18 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
19 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
20 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
21 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
22 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
23 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
24 * OF THIS SOFTWARE.
26 * Authors:
27 * Keith Packard
28 * Eric Anholt <eric@anholt.net>
29 * Dave Airlie <airlied@linux.ie>
30 * Jesse Barnes <jesse.barnes@intel.com>
32 #if !defined(__AROS__)
33 #include <linux/list.h>
34 #include <linux/slab.h>
35 #endif
36 #include "drm.h"
37 #include "drmP.h"
38 #include "drm_crtc.h"
39 #include "drm_edid.h"
41 struct drm_prop_enum_list {
42 int type;
43 char *name;
46 /* Avoid boilerplate. I'm tired of typing. */
47 #define DRM_ENUM_NAME_FN(fnname, list) \
48 char *fnname(int val) \
49 { \
50 int i; \
51 for (i = 0; i < ARRAY_SIZE(list); i++) { \
52 if (list[i].type == val) \
53 return list[i].name; \
54 } \
55 return "(unknown)"; \
59 * Global properties
61 static struct drm_prop_enum_list drm_dpms_enum_list[] =
62 { { DRM_MODE_DPMS_ON, "On" },
63 { DRM_MODE_DPMS_STANDBY, "Standby" },
64 { DRM_MODE_DPMS_SUSPEND, "Suspend" },
65 { DRM_MODE_DPMS_OFF, "Off" }
68 DRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list)
71 * Optional properties
73 static struct drm_prop_enum_list drm_scaling_mode_enum_list[] =
75 { DRM_MODE_SCALE_NONE, "None" },
76 { DRM_MODE_SCALE_FULLSCREEN, "Full" },
77 { DRM_MODE_SCALE_CENTER, "Center" },
78 { DRM_MODE_SCALE_ASPECT, "Full aspect" },
81 static struct drm_prop_enum_list drm_dithering_mode_enum_list[] =
83 { DRM_MODE_DITHERING_OFF, "Off" },
84 { DRM_MODE_DITHERING_ON, "On" },
85 { DRM_MODE_DITHERING_AUTO, "Automatic" },
89 * Non-global properties, but "required" for certain connectors.
91 static struct drm_prop_enum_list drm_dvi_i_select_enum_list[] =
93 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
94 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
95 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
98 DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list)
100 static struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list[] =
102 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
103 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
104 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
107 DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name,
108 drm_dvi_i_subconnector_enum_list)
110 static struct drm_prop_enum_list drm_tv_select_enum_list[] =
112 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
113 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
114 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
115 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
116 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
119 DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
121 static struct drm_prop_enum_list drm_tv_subconnector_enum_list[] =
123 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
124 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
125 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
126 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
127 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
130 DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,
131 drm_tv_subconnector_enum_list)
133 static struct drm_prop_enum_list drm_dirty_info_enum_list[] = {
134 { DRM_MODE_DIRTY_OFF, "Off" },
135 { DRM_MODE_DIRTY_ON, "On" },
136 { DRM_MODE_DIRTY_ANNOTATE, "Annotate" },
139 DRM_ENUM_NAME_FN(drm_get_dirty_info_name,
140 drm_dirty_info_enum_list)
142 struct drm_conn_prop_enum_list {
143 int type;
144 char *name;
145 int count;
149 * Connector and encoder types.
151 static struct drm_conn_prop_enum_list drm_connector_enum_list[] =
152 { { DRM_MODE_CONNECTOR_Unknown, "Unknown", 0 },
153 { DRM_MODE_CONNECTOR_VGA, "VGA", 0 },
154 { DRM_MODE_CONNECTOR_DVII, "DVI-I", 0 },
155 { DRM_MODE_CONNECTOR_DVID, "DVI-D", 0 },
156 { DRM_MODE_CONNECTOR_DVIA, "DVI-A", 0 },
157 { DRM_MODE_CONNECTOR_Composite, "Composite", 0 },
158 { DRM_MODE_CONNECTOR_SVIDEO, "SVIDEO", 0 },
159 { DRM_MODE_CONNECTOR_LVDS, "LVDS", 0 },
160 { DRM_MODE_CONNECTOR_Component, "Component", 0 },
161 { DRM_MODE_CONNECTOR_9PinDIN, "DIN", 0 },
162 { DRM_MODE_CONNECTOR_DisplayPort, "DP", 0 },
163 { DRM_MODE_CONNECTOR_HDMIA, "HDMI-A", 0 },
164 { DRM_MODE_CONNECTOR_HDMIB, "HDMI-B", 0 },
165 { DRM_MODE_CONNECTOR_TV, "TV", 0 },
166 { DRM_MODE_CONNECTOR_eDP, "eDP", 0 },
169 static struct drm_prop_enum_list drm_encoder_enum_list[] =
170 { { DRM_MODE_ENCODER_NONE, "None" },
171 { DRM_MODE_ENCODER_DAC, "DAC" },
172 { DRM_MODE_ENCODER_TMDS, "TMDS" },
173 { DRM_MODE_ENCODER_LVDS, "LVDS" },
174 { DRM_MODE_ENCODER_TVDAC, "TV" },
177 char *drm_get_encoder_name(struct drm_encoder *encoder)
179 static char buf[32];
181 snprintf(buf, 32, "%s-%d",
182 drm_encoder_enum_list[encoder->encoder_type].name,
183 encoder->base.id);
184 return buf;
186 EXPORT_SYMBOL(drm_get_encoder_name);
188 char *drm_get_connector_name(struct drm_connector *connector)
190 static char buf[32];
192 snprintf(buf, 32, "%s-%d",
193 drm_connector_enum_list[connector->connector_type].name,
194 connector->connector_type_id);
195 return buf;
197 EXPORT_SYMBOL(drm_get_connector_name);
199 char *drm_get_connector_status_name(enum drm_connector_status status)
201 if (status == connector_status_connected)
202 return "connected";
203 else if (status == connector_status_disconnected)
204 return "disconnected";
205 else
206 return "unknown";
210 * drm_mode_object_get - allocate a new identifier
211 * @dev: DRM device
212 * @ptr: object pointer, used to generate unique ID
213 * @type: object type
215 * LOCKING:
217 * Create a unique identifier based on @ptr in @dev's identifier space. Used
218 * for tracking modes, CRTCs and connectors.
220 * RETURNS:
221 * New unique (relative to other objects in @dev) integer identifier for the
222 * object.
224 static int drm_mode_object_get(struct drm_device *dev,
225 struct drm_mode_object *obj, uint32_t obj_type)
227 int new_id = 0;
228 int ret;
230 again:
231 if (idr_pre_get(&dev->mode_config.crtc_idr, GFP_KERNEL) == 0) {
232 DRM_ERROR("Ran out memory getting a mode number\n");
233 return -EINVAL;
236 mutex_lock(&dev->mode_config.idr_mutex);
237 ret = idr_get_new_above(&dev->mode_config.crtc_idr, obj, 1, &new_id);
238 mutex_unlock(&dev->mode_config.idr_mutex);
239 if (ret == -EAGAIN)
240 goto again;
242 obj->id = new_id;
243 obj->type = obj_type;
244 return 0;
248 * drm_mode_object_put - free an identifer
249 * @dev: DRM device
250 * @id: ID to free
252 * LOCKING:
253 * Caller must hold DRM mode_config lock.
255 * Free @id from @dev's unique identifier pool.
257 static void drm_mode_object_put(struct drm_device *dev,
258 struct drm_mode_object *object)
260 mutex_lock(&dev->mode_config.idr_mutex);
261 idr_remove(&dev->mode_config.crtc_idr, object->id);
262 mutex_unlock(&dev->mode_config.idr_mutex);
265 struct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
266 uint32_t id, uint32_t type)
268 struct drm_mode_object *obj = NULL;
270 mutex_lock(&dev->mode_config.idr_mutex);
271 obj = idr_find(&dev->mode_config.crtc_idr, id);
272 if (!obj || (obj->type != type) || (obj->id != id))
273 obj = NULL;
274 mutex_unlock(&dev->mode_config.idr_mutex);
276 return obj;
278 EXPORT_SYMBOL(drm_mode_object_find);
281 * drm_framebuffer_init - initialize a framebuffer
282 * @dev: DRM device
284 * LOCKING:
285 * Caller must hold mode config lock.
287 * Allocates an ID for the framebuffer's parent mode object, sets its mode
288 * functions & device file and adds it to the master fd list.
290 * RETURNS:
291 * Zero on success, error code on failure.
293 int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
294 const struct drm_framebuffer_funcs *funcs)
296 int ret;
298 ret = drm_mode_object_get(dev, &fb->base, DRM_MODE_OBJECT_FB);
299 if (ret) {
300 return ret;
303 fb->dev = dev;
304 fb->funcs = funcs;
305 dev->mode_config.num_fb++;
306 list_add(&fb->head, &dev->mode_config.fb_list);
308 return 0;
310 EXPORT_SYMBOL(drm_framebuffer_init);
313 * drm_framebuffer_cleanup - remove a framebuffer object
314 * @fb: framebuffer to remove
316 * LOCKING:
317 * Caller must hold mode config lock.
319 * Scans all the CRTCs in @dev's mode_config. If they're using @fb, removes
320 * it, setting it to NULL.
322 void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
324 struct drm_device *dev = fb->dev;
325 struct drm_crtc *crtc;
326 struct drm_mode_set set;
327 int ret;
329 /* remove from any CRTC */
330 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
331 if (crtc->fb == fb) {
332 /* should turn off the crtc */
333 memset(&set, 0, sizeof(struct drm_mode_set));
334 set.crtc = crtc;
335 set.fb = NULL;
336 ret = crtc->funcs->set_config(&set);
337 if (ret)
338 DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
342 drm_mode_object_put(dev, &fb->base);
343 list_del(&fb->head);
344 dev->mode_config.num_fb--;
346 EXPORT_SYMBOL(drm_framebuffer_cleanup);
349 * drm_crtc_init - Initialise a new CRTC object
350 * @dev: DRM device
351 * @crtc: CRTC object to init
352 * @funcs: callbacks for the new CRTC
354 * LOCKING:
355 * Caller must hold mode config lock.
357 * Inits a new object created as base part of an driver crtc object.
359 void drm_crtc_init(struct drm_device *dev, struct drm_crtc *crtc,
360 const struct drm_crtc_funcs *funcs)
362 crtc->dev = dev;
363 crtc->funcs = funcs;
365 mutex_lock(&dev->mode_config.mutex);
366 drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
368 list_add_tail(&crtc->head, &dev->mode_config.crtc_list);
369 dev->mode_config.num_crtc++;
370 mutex_unlock(&dev->mode_config.mutex);
372 EXPORT_SYMBOL(drm_crtc_init);
375 * drm_crtc_cleanup - Cleans up the core crtc usage.
376 * @crtc: CRTC to cleanup
378 * LOCKING:
379 * Caller must hold mode config lock.
381 * Cleanup @crtc. Removes from drm modesetting space
382 * does NOT free object, caller does that.
384 void drm_crtc_cleanup(struct drm_crtc *crtc)
386 struct drm_device *dev = crtc->dev;
388 if (crtc->gamma_store) {
389 kfree(crtc->gamma_store);
390 crtc->gamma_store = NULL;
393 drm_mode_object_put(dev, &crtc->base);
394 list_del(&crtc->head);
395 dev->mode_config.num_crtc--;
397 EXPORT_SYMBOL(drm_crtc_cleanup);
400 * drm_mode_probed_add - add a mode to a connector's probed mode list
401 * @connector: connector the new mode
402 * @mode: mode data
404 * LOCKING:
405 * Caller must hold mode config lock.
407 * Add @mode to @connector's mode list for later use.
409 void drm_mode_probed_add(struct drm_connector *connector,
410 struct drm_display_mode *mode)
412 list_add(&mode->head, &connector->probed_modes);
414 EXPORT_SYMBOL(drm_mode_probed_add);
417 * drm_mode_remove - remove and free a mode
418 * @connector: connector list to modify
419 * @mode: mode to remove
421 * LOCKING:
422 * Caller must hold mode config lock.
424 * Remove @mode from @connector's mode list, then free it.
426 void drm_mode_remove(struct drm_connector *connector,
427 struct drm_display_mode *mode)
429 list_del(&mode->head);
430 kfree(mode);
432 EXPORT_SYMBOL(drm_mode_remove);
435 * drm_connector_init - Init a preallocated connector
436 * @dev: DRM device
437 * @connector: the connector to init
438 * @funcs: callbacks for this connector
439 * @name: user visible name of the connector
441 * LOCKING:
442 * Caller must hold @dev's mode_config lock.
444 * Initialises a preallocated connector. Connectors should be
445 * subclassed as part of driver connector objects.
447 void drm_connector_init(struct drm_device *dev,
448 struct drm_connector *connector,
449 const struct drm_connector_funcs *funcs,
450 int connector_type)
452 mutex_lock(&dev->mode_config.mutex);
454 connector->dev = dev;
455 connector->funcs = funcs;
456 drm_mode_object_get(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR);
457 connector->connector_type = connector_type;
458 connector->connector_type_id =
459 ++drm_connector_enum_list[connector_type].count; /* TODO */
460 INIT_LIST_HEAD(&connector->user_modes);
461 INIT_LIST_HEAD(&connector->probed_modes);
462 INIT_LIST_HEAD(&connector->modes);
463 connector->edid_blob_ptr = NULL;
465 list_add_tail(&connector->head, &dev->mode_config.connector_list);
466 dev->mode_config.num_connector++;
468 drm_connector_attach_property(connector,
469 dev->mode_config.edid_property, 0);
471 drm_connector_attach_property(connector,
472 dev->mode_config.dpms_property, 0);
474 mutex_unlock(&dev->mode_config.mutex);
476 EXPORT_SYMBOL(drm_connector_init);
479 * drm_connector_cleanup - cleans up an initialised connector
480 * @connector: connector to cleanup
482 * LOCKING:
483 * Caller must hold @dev's mode_config lock.
485 * Cleans up the connector but doesn't free the object.
487 void drm_connector_cleanup(struct drm_connector *connector)
489 struct drm_device *dev = connector->dev;
490 struct drm_display_mode *mode, *t;
492 list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
493 drm_mode_remove(connector, mode);
495 list_for_each_entry_safe(mode, t, &connector->modes, head)
496 drm_mode_remove(connector, mode);
498 list_for_each_entry_safe(mode, t, &connector->user_modes, head)
499 drm_mode_remove(connector, mode);
501 mutex_lock(&dev->mode_config.mutex);
502 drm_mode_object_put(dev, &connector->base);
503 list_del(&connector->head);
504 mutex_unlock(&dev->mode_config.mutex);
506 EXPORT_SYMBOL(drm_connector_cleanup);
508 void drm_encoder_init(struct drm_device *dev,
509 struct drm_encoder *encoder,
510 const struct drm_encoder_funcs *funcs,
511 int encoder_type)
513 mutex_lock(&dev->mode_config.mutex);
515 encoder->dev = dev;
517 drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
518 encoder->encoder_type = encoder_type;
519 encoder->funcs = funcs;
521 list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
522 dev->mode_config.num_encoder++;
524 mutex_unlock(&dev->mode_config.mutex);
526 EXPORT_SYMBOL(drm_encoder_init);
528 void drm_encoder_cleanup(struct drm_encoder *encoder)
530 struct drm_device *dev = encoder->dev;
531 mutex_lock(&dev->mode_config.mutex);
532 drm_mode_object_put(dev, &encoder->base);
533 list_del(&encoder->head);
534 mutex_unlock(&dev->mode_config.mutex);
536 EXPORT_SYMBOL(drm_encoder_cleanup);
539 * drm_mode_create - create a new display mode
540 * @dev: DRM device
542 * LOCKING:
543 * Caller must hold DRM mode_config lock.
545 * Create a new drm_display_mode, give it an ID, and return it.
547 * RETURNS:
548 * Pointer to new mode on success, NULL on error.
550 struct drm_display_mode *drm_mode_create(struct drm_device *dev)
552 struct drm_display_mode *nmode;
554 nmode = kzalloc(sizeof(struct drm_display_mode), GFP_KERNEL);
555 if (!nmode)
556 return NULL;
558 drm_mode_object_get(dev, &nmode->base, DRM_MODE_OBJECT_MODE);
559 return nmode;
561 EXPORT_SYMBOL(drm_mode_create);
564 * drm_mode_destroy - remove a mode
565 * @dev: DRM device
566 * @mode: mode to remove
568 * LOCKING:
569 * Caller must hold mode config lock.
571 * Free @mode's unique identifier, then free it.
573 void drm_mode_destroy(struct drm_device *dev, struct drm_display_mode *mode)
575 drm_mode_object_put(dev, &mode->base);
577 kfree(mode);
579 EXPORT_SYMBOL(drm_mode_destroy);
581 static int drm_mode_create_standard_connector_properties(struct drm_device *dev)
583 struct drm_property *edid;
584 struct drm_property *dpms;
585 int i;
588 * Standard properties (apply to all connectors)
590 edid = drm_property_create(dev, DRM_MODE_PROP_BLOB |
591 DRM_MODE_PROP_IMMUTABLE,
592 "EDID", 0);
593 dev->mode_config.edid_property = edid;
595 dpms = drm_property_create(dev, DRM_MODE_PROP_ENUM,
596 "DPMS", ARRAY_SIZE(drm_dpms_enum_list));
597 for (i = 0; i < ARRAY_SIZE(drm_dpms_enum_list); i++)
598 drm_property_add_enum(dpms, i, drm_dpms_enum_list[i].type,
599 drm_dpms_enum_list[i].name);
600 dev->mode_config.dpms_property = dpms;
602 return 0;
606 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
607 * @dev: DRM device
609 * Called by a driver the first time a DVI-I connector is made.
611 int drm_mode_create_dvi_i_properties(struct drm_device *dev)
613 struct drm_property *dvi_i_selector;
614 struct drm_property *dvi_i_subconnector;
615 int i;
617 if (dev->mode_config.dvi_i_select_subconnector_property)
618 return 0;
620 dvi_i_selector =
621 drm_property_create(dev, DRM_MODE_PROP_ENUM,
622 "select subconnector",
623 ARRAY_SIZE(drm_dvi_i_select_enum_list));
624 for (i = 0; i < ARRAY_SIZE(drm_dvi_i_select_enum_list); i++)
625 drm_property_add_enum(dvi_i_selector, i,
626 drm_dvi_i_select_enum_list[i].type,
627 drm_dvi_i_select_enum_list[i].name);
628 dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
630 dvi_i_subconnector =
631 drm_property_create(dev, DRM_MODE_PROP_ENUM |
632 DRM_MODE_PROP_IMMUTABLE,
633 "subconnector",
634 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
635 for (i = 0; i < ARRAY_SIZE(drm_dvi_i_subconnector_enum_list); i++)
636 drm_property_add_enum(dvi_i_subconnector, i,
637 drm_dvi_i_subconnector_enum_list[i].type,
638 drm_dvi_i_subconnector_enum_list[i].name);
639 dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
641 return 0;
643 EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
646 * drm_create_tv_properties - create TV specific connector properties
647 * @dev: DRM device
648 * @num_modes: number of different TV formats (modes) supported
649 * @modes: array of pointers to strings containing name of each format
651 * Called by a driver's TV initialization routine, this function creates
652 * the TV specific connector properties for a given device. Caller is
653 * responsible for allocating a list of format names and passing them to
654 * this routine.
656 int drm_mode_create_tv_properties(struct drm_device *dev, int num_modes,
657 char *modes[])
659 struct drm_property *tv_selector;
660 struct drm_property *tv_subconnector;
661 int i;
663 if (dev->mode_config.tv_select_subconnector_property)
664 return 0;
667 * Basic connector properties
669 tv_selector = drm_property_create(dev, DRM_MODE_PROP_ENUM,
670 "select subconnector",
671 ARRAY_SIZE(drm_tv_select_enum_list));
672 for (i = 0; i < ARRAY_SIZE(drm_tv_select_enum_list); i++)
673 drm_property_add_enum(tv_selector, i,
674 drm_tv_select_enum_list[i].type,
675 drm_tv_select_enum_list[i].name);
676 dev->mode_config.tv_select_subconnector_property = tv_selector;
678 tv_subconnector =
679 drm_property_create(dev, DRM_MODE_PROP_ENUM |
680 DRM_MODE_PROP_IMMUTABLE, "subconnector",
681 ARRAY_SIZE(drm_tv_subconnector_enum_list));
682 for (i = 0; i < ARRAY_SIZE(drm_tv_subconnector_enum_list); i++)
683 drm_property_add_enum(tv_subconnector, i,
684 drm_tv_subconnector_enum_list[i].type,
685 drm_tv_subconnector_enum_list[i].name);
686 dev->mode_config.tv_subconnector_property = tv_subconnector;
689 * Other, TV specific properties: margins & TV modes.
691 dev->mode_config.tv_left_margin_property =
692 drm_property_create(dev, DRM_MODE_PROP_RANGE,
693 "left margin", 2);
694 dev->mode_config.tv_left_margin_property->values[0] = 0;
695 dev->mode_config.tv_left_margin_property->values[1] = 100;
697 dev->mode_config.tv_right_margin_property =
698 drm_property_create(dev, DRM_MODE_PROP_RANGE,
699 "right margin", 2);
700 dev->mode_config.tv_right_margin_property->values[0] = 0;
701 dev->mode_config.tv_right_margin_property->values[1] = 100;
703 dev->mode_config.tv_top_margin_property =
704 drm_property_create(dev, DRM_MODE_PROP_RANGE,
705 "top margin", 2);
706 dev->mode_config.tv_top_margin_property->values[0] = 0;
707 dev->mode_config.tv_top_margin_property->values[1] = 100;
709 dev->mode_config.tv_bottom_margin_property =
710 drm_property_create(dev, DRM_MODE_PROP_RANGE,
711 "bottom margin", 2);
712 dev->mode_config.tv_bottom_margin_property->values[0] = 0;
713 dev->mode_config.tv_bottom_margin_property->values[1] = 100;
715 dev->mode_config.tv_mode_property =
716 drm_property_create(dev, DRM_MODE_PROP_ENUM,
717 "mode", num_modes);
718 for (i = 0; i < num_modes; i++)
719 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
720 i, modes[i]);
722 dev->mode_config.tv_brightness_property =
723 drm_property_create(dev, DRM_MODE_PROP_RANGE,
724 "brightness", 2);
725 dev->mode_config.tv_brightness_property->values[0] = 0;
726 dev->mode_config.tv_brightness_property->values[1] = 100;
728 dev->mode_config.tv_contrast_property =
729 drm_property_create(dev, DRM_MODE_PROP_RANGE,
730 "contrast", 2);
731 dev->mode_config.tv_contrast_property->values[0] = 0;
732 dev->mode_config.tv_contrast_property->values[1] = 100;
734 dev->mode_config.tv_flicker_reduction_property =
735 drm_property_create(dev, DRM_MODE_PROP_RANGE,
736 "flicker reduction", 2);
737 dev->mode_config.tv_flicker_reduction_property->values[0] = 0;
738 dev->mode_config.tv_flicker_reduction_property->values[1] = 100;
740 dev->mode_config.tv_overscan_property =
741 drm_property_create(dev, DRM_MODE_PROP_RANGE,
742 "overscan", 2);
743 dev->mode_config.tv_overscan_property->values[0] = 0;
744 dev->mode_config.tv_overscan_property->values[1] = 100;
746 dev->mode_config.tv_saturation_property =
747 drm_property_create(dev, DRM_MODE_PROP_RANGE,
748 "saturation", 2);
749 dev->mode_config.tv_saturation_property->values[0] = 0;
750 dev->mode_config.tv_saturation_property->values[1] = 100;
752 dev->mode_config.tv_hue_property =
753 drm_property_create(dev, DRM_MODE_PROP_RANGE,
754 "hue", 2);
755 dev->mode_config.tv_hue_property->values[0] = 0;
756 dev->mode_config.tv_hue_property->values[1] = 100;
758 return 0;
760 EXPORT_SYMBOL(drm_mode_create_tv_properties);
763 * drm_mode_create_scaling_mode_property - create scaling mode property
764 * @dev: DRM device
766 * Called by a driver the first time it's needed, must be attached to desired
767 * connectors.
769 int drm_mode_create_scaling_mode_property(struct drm_device *dev)
771 struct drm_property *scaling_mode;
772 int i;
774 if (dev->mode_config.scaling_mode_property)
775 return 0;
777 scaling_mode =
778 drm_property_create(dev, DRM_MODE_PROP_ENUM, "scaling mode",
779 ARRAY_SIZE(drm_scaling_mode_enum_list));
780 for (i = 0; i < ARRAY_SIZE(drm_scaling_mode_enum_list); i++)
781 drm_property_add_enum(scaling_mode, i,
782 drm_scaling_mode_enum_list[i].type,
783 drm_scaling_mode_enum_list[i].name);
785 dev->mode_config.scaling_mode_property = scaling_mode;
787 return 0;
789 EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
792 * drm_mode_create_dithering_property - create dithering property
793 * @dev: DRM device
795 * Called by a driver the first time it's needed, must be attached to desired
796 * connectors.
798 int drm_mode_create_dithering_property(struct drm_device *dev)
800 struct drm_property *dithering_mode;
801 int i;
803 if (dev->mode_config.dithering_mode_property)
804 return 0;
806 dithering_mode =
807 drm_property_create(dev, DRM_MODE_PROP_ENUM, "dithering",
808 ARRAY_SIZE(drm_dithering_mode_enum_list));
809 for (i = 0; i < ARRAY_SIZE(drm_dithering_mode_enum_list); i++)
810 drm_property_add_enum(dithering_mode, i,
811 drm_dithering_mode_enum_list[i].type,
812 drm_dithering_mode_enum_list[i].name);
813 dev->mode_config.dithering_mode_property = dithering_mode;
815 return 0;
817 EXPORT_SYMBOL(drm_mode_create_dithering_property);
819 #if !defined(__AROS__)
821 * drm_mode_create_dirty_property - create dirty property
822 * @dev: DRM device
824 * Called by a driver the first time it's needed, must be attached to desired
825 * connectors.
827 int drm_mode_create_dirty_info_property(struct drm_device *dev)
829 struct drm_property *dirty_info;
830 int i;
832 if (dev->mode_config.dirty_info_property)
833 return 0;
835 dirty_info =
836 drm_property_create(dev, DRM_MODE_PROP_ENUM |
837 DRM_MODE_PROP_IMMUTABLE,
838 "dirty",
839 ARRAY_SIZE(drm_dirty_info_enum_list));
840 for (i = 0; i < ARRAY_SIZE(drm_dirty_info_enum_list); i++)
841 drm_property_add_enum(dirty_info, i,
842 drm_dirty_info_enum_list[i].type,
843 drm_dirty_info_enum_list[i].name);
844 dev->mode_config.dirty_info_property = dirty_info;
846 return 0;
848 EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
849 #endif
852 * drm_mode_config_init - initialize DRM mode_configuration structure
853 * @dev: DRM device
855 * LOCKING:
856 * None, should happen single threaded at init time.
858 * Initialize @dev's mode_config structure, used for tracking the graphics
859 * configuration of @dev.
861 void drm_mode_config_init(struct drm_device *dev)
863 mutex_init(&dev->mode_config.mutex);
864 mutex_init(&dev->mode_config.idr_mutex);
865 INIT_LIST_HEAD(&dev->mode_config.fb_list);
866 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
867 INIT_LIST_HEAD(&dev->mode_config.connector_list);
868 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
869 INIT_LIST_HEAD(&dev->mode_config.property_list);
870 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
871 idr_init(&dev->mode_config.crtc_idr);
873 mutex_lock(&dev->mode_config.mutex);
874 drm_mode_create_standard_connector_properties(dev);
875 mutex_unlock(&dev->mode_config.mutex);
877 /* Just to be sure */
878 dev->mode_config.num_fb = 0;
879 dev->mode_config.num_connector = 0;
880 dev->mode_config.num_crtc = 0;
881 dev->mode_config.num_encoder = 0;
883 EXPORT_SYMBOL(drm_mode_config_init);
885 #if !defined(__AROS__)
886 int drm_mode_group_init(struct drm_device *dev, struct drm_mode_group *group)
888 uint32_t total_objects = 0;
890 total_objects += dev->mode_config.num_crtc;
891 total_objects += dev->mode_config.num_connector;
892 total_objects += dev->mode_config.num_encoder;
894 group->id_list = kzalloc(total_objects * sizeof(uint32_t), GFP_KERNEL);
895 if (!group->id_list)
896 return -ENOMEM;
898 group->num_crtcs = 0;
899 group->num_connectors = 0;
900 group->num_encoders = 0;
901 return 0;
904 int drm_mode_group_init_legacy_group(struct drm_device *dev,
905 struct drm_mode_group *group)
907 struct drm_crtc *crtc;
908 struct drm_encoder *encoder;
909 struct drm_connector *connector;
910 int ret;
912 if ((ret = drm_mode_group_init(dev, group)))
913 return ret;
915 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
916 group->id_list[group->num_crtcs++] = crtc->base.id;
918 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
919 group->id_list[group->num_crtcs + group->num_encoders++] =
920 encoder->base.id;
922 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
923 group->id_list[group->num_crtcs + group->num_encoders +
924 group->num_connectors++] = connector->base.id;
926 return 0;
928 #endif
931 * drm_mode_config_cleanup - free up DRM mode_config info
932 * @dev: DRM device
934 * LOCKING:
935 * Caller must hold mode config lock.
937 * Free up all the connectors and CRTCs associated with this DRM device, then
938 * free up the framebuffers and associated buffer objects.
940 * FIXME: cleanup any dangling user buffer objects too
942 void drm_mode_config_cleanup(struct drm_device *dev)
944 struct drm_connector *connector, *ot;
945 struct drm_crtc *crtc, *ct;
946 struct drm_encoder *encoder, *enct;
947 struct drm_framebuffer *fb, *fbt;
948 struct drm_property *property, *pt;
950 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
951 head) {
952 encoder->funcs->destroy(encoder);
955 list_for_each_entry_safe(connector, ot,
956 &dev->mode_config.connector_list, head) {
957 connector->funcs->destroy(connector);
960 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
961 head) {
962 drm_property_destroy(dev, property);
965 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
966 fb->funcs->destroy(fb);
969 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
970 crtc->funcs->destroy(crtc);
974 EXPORT_SYMBOL(drm_mode_config_cleanup);
977 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
978 * @out: drm_mode_modeinfo struct to return to the user
979 * @in: drm_display_mode to use
981 * LOCKING:
982 * None.
984 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
985 * the user.
987 void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out,
988 struct drm_display_mode *in)
990 out->clock = in->clock;
991 out->hdisplay = in->hdisplay;
992 out->hsync_start = in->hsync_start;
993 out->hsync_end = in->hsync_end;
994 out->htotal = in->htotal;
995 out->hskew = in->hskew;
996 out->vdisplay = in->vdisplay;
997 out->vsync_start = in->vsync_start;
998 out->vsync_end = in->vsync_end;
999 out->vtotal = in->vtotal;
1000 out->vscan = in->vscan;
1001 out->vrefresh = in->vrefresh;
1002 out->flags = in->flags;
1003 out->type = in->type;
1004 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1005 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1009 * drm_crtc_convert_to_umode - convert a modeinfo into a drm_display_mode
1010 * @out: drm_display_mode to return to the user
1011 * @in: drm_mode_modeinfo to use
1013 * LOCKING:
1014 * None.
1016 * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
1017 * the caller.
1019 void drm_crtc_convert_umode(struct drm_display_mode *out,
1020 struct drm_mode_modeinfo *in)
1022 out->clock = in->clock;
1023 out->hdisplay = in->hdisplay;
1024 out->hsync_start = in->hsync_start;
1025 out->hsync_end = in->hsync_end;
1026 out->htotal = in->htotal;
1027 out->hskew = in->hskew;
1028 out->vdisplay = in->vdisplay;
1029 out->vsync_start = in->vsync_start;
1030 out->vsync_end = in->vsync_end;
1031 out->vtotal = in->vtotal;
1032 out->vscan = in->vscan;
1033 out->vrefresh = in->vrefresh;
1034 out->flags = in->flags;
1035 out->type = in->type;
1036 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1037 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1041 * drm_mode_getresources - get graphics configuration
1042 * @inode: inode from the ioctl
1043 * @filp: file * from the ioctl
1044 * @cmd: cmd from ioctl
1045 * @arg: arg from ioctl
1047 * LOCKING:
1048 * Takes mode config lock.
1050 * Construct a set of configuration description structures and return
1051 * them to the user, including CRTC, connector and framebuffer configuration.
1053 * Called by the user via ioctl.
1055 * RETURNS:
1056 * Zero on success, errno on failure.
1058 int drm_mode_getresources(struct drm_device *dev, void *data,
1059 struct drm_file *file_priv)
1061 struct drm_mode_card_res *card_res = data;
1062 struct list_head *lh;
1063 struct drm_framebuffer *fb;
1064 struct drm_connector *connector;
1065 struct drm_crtc *crtc;
1066 struct drm_encoder *encoder;
1067 int ret = 0;
1068 int connector_count = 0;
1069 int crtc_count = 0;
1070 int fb_count = 0;
1071 int encoder_count = 0;
1072 int copied = 0, i;
1073 uint32_t __user *fb_id;
1074 uint32_t __user *crtc_id;
1075 uint32_t __user *connector_id;
1076 uint32_t __user *encoder_id;
1077 struct drm_mode_group *mode_group;
1079 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1080 return -EINVAL;
1082 mutex_lock(&dev->mode_config.mutex);
1085 * For the non-control nodes we need to limit the list of resources
1086 * by IDs in the group list for this node
1088 list_for_each(lh, &file_priv->fbs)
1089 fb_count++;
1091 #if !defined(__AROS__)
1092 mode_group = &file_priv->master->minor->mode_group;
1093 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1094 #else
1095 if (1) {
1096 #endif
1098 list_for_each(lh, &dev->mode_config.crtc_list)
1099 crtc_count++;
1101 list_for_each(lh, &dev->mode_config.connector_list)
1102 connector_count++;
1104 list_for_each(lh, &dev->mode_config.encoder_list)
1105 encoder_count++;
1106 } else {
1108 crtc_count = mode_group->num_crtcs;
1109 connector_count = mode_group->num_connectors;
1110 encoder_count = mode_group->num_encoders;
1113 card_res->max_height = dev->mode_config.max_height;
1114 card_res->min_height = dev->mode_config.min_height;
1115 card_res->max_width = dev->mode_config.max_width;
1116 card_res->min_width = dev->mode_config.min_width;
1118 /* handle this in 4 parts */
1119 /* FBs */
1120 if (card_res->count_fbs >= fb_count) {
1121 copied = 0;
1122 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
1123 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
1124 if (put_user(fb->base.id, fb_id + copied)) {
1125 ret = -EFAULT;
1126 goto out;
1128 copied++;
1131 card_res->count_fbs = fb_count;
1133 /* CRTCs */
1134 if (card_res->count_crtcs >= crtc_count) {
1135 copied = 0;
1136 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
1137 #if !defined(__AROS__)
1138 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1139 #else
1140 if (1) {
1141 #endif
1142 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
1143 head) {
1144 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
1145 if (put_user(crtc->base.id, crtc_id + copied)) {
1146 ret = -EFAULT;
1147 goto out;
1149 copied++;
1151 } else {
1152 for (i = 0; i < mode_group->num_crtcs; i++) {
1153 if (put_user(mode_group->id_list[i],
1154 crtc_id + copied)) {
1155 ret = -EFAULT;
1156 goto out;
1158 copied++;
1162 card_res->count_crtcs = crtc_count;
1164 /* Encoders */
1165 if (card_res->count_encoders >= encoder_count) {
1166 copied = 0;
1167 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
1168 #if !defined(__AROS__)
1169 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1170 #else
1171 if (1) {
1172 #endif
1173 list_for_each_entry(encoder,
1174 &dev->mode_config.encoder_list,
1175 head) {
1176 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id,
1177 drm_get_encoder_name(encoder));
1178 if (put_user(encoder->base.id, encoder_id +
1179 copied)) {
1180 ret = -EFAULT;
1181 goto out;
1183 copied++;
1185 } else {
1186 for (i = mode_group->num_crtcs; i < mode_group->num_crtcs + mode_group->num_encoders; i++) {
1187 if (put_user(mode_group->id_list[i],
1188 encoder_id + copied)) {
1189 ret = -EFAULT;
1190 goto out;
1192 copied++;
1197 card_res->count_encoders = encoder_count;
1199 /* Connectors */
1200 if (card_res->count_connectors >= connector_count) {
1201 copied = 0;
1202 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
1203 #if !defined(__AROS__)
1204 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1205 #else
1206 if (1) {
1207 #endif
1208 list_for_each_entry(connector,
1209 &dev->mode_config.connector_list,
1210 head) {
1211 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1212 connector->base.id,
1213 drm_get_connector_name(connector));
1214 if (put_user(connector->base.id,
1215 connector_id + copied)) {
1216 ret = -EFAULT;
1217 goto out;
1219 copied++;
1221 } else {
1222 int start = mode_group->num_crtcs +
1223 mode_group->num_encoders;
1224 for (i = start; i < start + mode_group->num_connectors; i++) {
1225 if (put_user(mode_group->id_list[i],
1226 connector_id + copied)) {
1227 ret = -EFAULT;
1228 goto out;
1230 copied++;
1234 card_res->count_connectors = connector_count;
1236 DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs,
1237 card_res->count_connectors, card_res->count_encoders);
1239 out:
1240 mutex_unlock(&dev->mode_config.mutex);
1241 return ret;
1245 * drm_mode_getcrtc - get CRTC configuration
1246 * @inode: inode from the ioctl
1247 * @filp: file * from the ioctl
1248 * @cmd: cmd from ioctl
1249 * @arg: arg from ioctl
1251 * LOCKING:
1252 * Caller? (FIXME)
1254 * Construct a CRTC configuration structure to return to the user.
1256 * Called by the user via ioctl.
1258 * RETURNS:
1259 * Zero on success, errno on failure.
1261 int drm_mode_getcrtc(struct drm_device *dev,
1262 void *data, struct drm_file *file_priv)
1264 struct drm_mode_crtc *crtc_resp = data;
1265 struct drm_crtc *crtc;
1266 struct drm_mode_object *obj;
1267 int ret = 0;
1269 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1270 return -EINVAL;
1272 mutex_lock(&dev->mode_config.mutex);
1274 obj = drm_mode_object_find(dev, crtc_resp->crtc_id,
1275 DRM_MODE_OBJECT_CRTC);
1276 if (!obj) {
1277 ret = -EINVAL;
1278 goto out;
1280 crtc = obj_to_crtc(obj);
1282 crtc_resp->x = crtc->x;
1283 crtc_resp->y = crtc->y;
1284 crtc_resp->gamma_size = crtc->gamma_size;
1285 if (crtc->fb)
1286 crtc_resp->fb_id = crtc->fb->base.id;
1287 else
1288 crtc_resp->fb_id = 0;
1290 if (crtc->enabled) {
1292 drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
1293 crtc_resp->mode_valid = 1;
1295 } else {
1296 crtc_resp->mode_valid = 0;
1299 out:
1300 mutex_unlock(&dev->mode_config.mutex);
1301 return ret;
1305 * drm_mode_getconnector - get connector configuration
1306 * @inode: inode from the ioctl
1307 * @filp: file * from the ioctl
1308 * @cmd: cmd from ioctl
1309 * @arg: arg from ioctl
1311 * LOCKING:
1312 * Caller? (FIXME)
1314 * Construct a connector configuration structure to return to the user.
1316 * Called by the user via ioctl.
1318 * RETURNS:
1319 * Zero on success, errno on failure.
1321 int drm_mode_getconnector(struct drm_device *dev, void *data,
1322 struct drm_file *file_priv)
1324 struct drm_mode_get_connector *out_resp = data;
1325 struct drm_mode_object *obj;
1326 struct drm_connector *connector;
1327 struct drm_display_mode *mode;
1328 int mode_count = 0;
1329 int props_count = 0;
1330 int encoders_count = 0;
1331 int ret = 0;
1332 int copied = 0;
1333 int i;
1334 struct drm_mode_modeinfo u_mode;
1335 struct drm_mode_modeinfo __user *mode_ptr;
1336 uint32_t __user *prop_ptr;
1337 uint64_t __user *prop_values;
1338 uint32_t __user *encoder_ptr;
1340 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1341 return -EINVAL;
1343 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1345 DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id);
1347 mutex_lock(&dev->mode_config.mutex);
1349 obj = drm_mode_object_find(dev, out_resp->connector_id,
1350 DRM_MODE_OBJECT_CONNECTOR);
1351 if (!obj) {
1352 ret = -EINVAL;
1353 goto out;
1355 connector = obj_to_connector(obj);
1357 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
1358 if (connector->property_ids[i] != 0) {
1359 props_count++;
1363 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1364 if (connector->encoder_ids[i] != 0) {
1365 encoders_count++;
1369 if (out_resp->count_modes == 0) {
1370 connector->funcs->fill_modes(connector,
1371 dev->mode_config.max_width,
1372 dev->mode_config.max_height);
1375 /* delayed so we get modes regardless of pre-fill_modes state */
1376 list_for_each_entry(mode, &connector->modes, head)
1377 mode_count++;
1379 out_resp->connector_id = connector->base.id;
1380 out_resp->connector_type = connector->connector_type;
1381 out_resp->connector_type_id = connector->connector_type_id;
1382 out_resp->mm_width = connector->display_info.width_mm;
1383 out_resp->mm_height = connector->display_info.height_mm;
1384 out_resp->subpixel = connector->display_info.subpixel_order;
1385 out_resp->connection = connector->status;
1386 if (connector->encoder)
1387 out_resp->encoder_id = connector->encoder->base.id;
1388 else
1389 out_resp->encoder_id = 0;
1392 * This ioctl is called twice, once to determine how much space is
1393 * needed, and the 2nd time to fill it.
1395 if ((out_resp->count_modes >= mode_count) && mode_count) {
1396 copied = 0;
1397 mode_ptr = (struct drm_mode_modeinfo *)(unsigned long)out_resp->modes_ptr;
1398 list_for_each_entry(mode, &connector->modes, head) {
1399 drm_crtc_convert_to_umode(&u_mode, mode);
1400 if (copy_to_user(mode_ptr + copied,
1401 &u_mode, sizeof(u_mode))) {
1402 ret = -EFAULT;
1403 goto out;
1405 copied++;
1408 out_resp->count_modes = mode_count;
1410 if ((out_resp->count_props >= props_count) && props_count) {
1411 copied = 0;
1412 prop_ptr = (uint32_t *)(unsigned long)(out_resp->props_ptr);
1413 prop_values = (uint64_t *)(unsigned long)(out_resp->prop_values_ptr);
1414 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
1415 if (connector->property_ids[i] != 0) {
1416 if (put_user(connector->property_ids[i],
1417 prop_ptr + copied)) {
1418 ret = -EFAULT;
1419 goto out;
1422 if (put_user(connector->property_values[i],
1423 prop_values + copied)) {
1424 ret = -EFAULT;
1425 goto out;
1427 copied++;
1431 out_resp->count_props = props_count;
1433 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
1434 copied = 0;
1435 encoder_ptr = (uint32_t *)(unsigned long)(out_resp->encoders_ptr);
1436 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1437 if (connector->encoder_ids[i] != 0) {
1438 if (put_user(connector->encoder_ids[i],
1439 encoder_ptr + copied)) {
1440 ret = -EFAULT;
1441 goto out;
1443 copied++;
1447 out_resp->count_encoders = encoders_count;
1449 out:
1450 mutex_unlock(&dev->mode_config.mutex);
1451 return ret;
1454 int drm_mode_getencoder(struct drm_device *dev, void *data,
1455 struct drm_file *file_priv)
1457 struct drm_mode_get_encoder *enc_resp = data;
1458 struct drm_mode_object *obj;
1459 struct drm_encoder *encoder;
1460 int ret = 0;
1462 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1463 return -EINVAL;
1465 mutex_lock(&dev->mode_config.mutex);
1466 obj = drm_mode_object_find(dev, enc_resp->encoder_id,
1467 DRM_MODE_OBJECT_ENCODER);
1468 if (!obj) {
1469 ret = -EINVAL;
1470 goto out;
1472 encoder = obj_to_encoder(obj);
1474 if (encoder->crtc)
1475 enc_resp->crtc_id = encoder->crtc->base.id;
1476 else
1477 enc_resp->crtc_id = 0;
1478 enc_resp->encoder_type = encoder->encoder_type;
1479 enc_resp->encoder_id = encoder->base.id;
1480 enc_resp->possible_crtcs = encoder->possible_crtcs;
1481 enc_resp->possible_clones = encoder->possible_clones;
1483 out:
1484 mutex_unlock(&dev->mode_config.mutex);
1485 return ret;
1489 * drm_mode_setcrtc - set CRTC configuration
1490 * @inode: inode from the ioctl
1491 * @filp: file * from the ioctl
1492 * @cmd: cmd from ioctl
1493 * @arg: arg from ioctl
1495 * LOCKING:
1496 * Caller? (FIXME)
1498 * Build a new CRTC configuration based on user request.
1500 * Called by the user via ioctl.
1502 * RETURNS:
1503 * Zero on success, errno on failure.
1505 int drm_mode_setcrtc(struct drm_device *dev, void *data,
1506 struct drm_file *file_priv)
1508 struct drm_mode_config *config = &dev->mode_config;
1509 struct drm_mode_crtc *crtc_req = data;
1510 struct drm_mode_object *obj;
1511 struct drm_crtc *crtc, *crtcfb;
1512 struct drm_connector **connector_set = NULL, *connector;
1513 struct drm_framebuffer *fb = NULL;
1514 struct drm_display_mode *mode = NULL;
1515 struct drm_mode_set set;
1516 uint32_t __user *set_connectors_ptr;
1517 int ret = 0;
1518 int i;
1520 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1521 return -EINVAL;
1523 mutex_lock(&dev->mode_config.mutex);
1524 obj = drm_mode_object_find(dev, crtc_req->crtc_id,
1525 DRM_MODE_OBJECT_CRTC);
1526 if (!obj) {
1527 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
1528 ret = -EINVAL;
1529 goto out;
1531 crtc = obj_to_crtc(obj);
1532 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
1534 if (crtc_req->mode_valid) {
1535 /* If we have a mode we need a framebuffer. */
1536 /* If we pass -1, set the mode with the currently bound fb */
1537 if (crtc_req->fb_id == -1) {
1538 list_for_each_entry(crtcfb,
1539 &dev->mode_config.crtc_list, head) {
1540 if (crtcfb == crtc) {
1541 DRM_DEBUG_KMS("Using current fb for "
1542 "setmode\n");
1543 fb = crtc->fb;
1546 } else {
1547 obj = drm_mode_object_find(dev, crtc_req->fb_id,
1548 DRM_MODE_OBJECT_FB);
1549 if (!obj) {
1550 DRM_DEBUG_KMS("Unknown FB ID%d\n",
1551 crtc_req->fb_id);
1552 ret = -EINVAL;
1553 goto out;
1555 fb = obj_to_fb(obj);
1558 mode = drm_mode_create(dev);
1559 drm_crtc_convert_umode(mode, &crtc_req->mode);
1560 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
1563 if (crtc_req->count_connectors == 0 && mode) {
1564 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
1565 ret = -EINVAL;
1566 goto out;
1569 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
1570 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
1571 crtc_req->count_connectors);
1572 ret = -EINVAL;
1573 goto out;
1576 if (crtc_req->count_connectors > 0) {
1577 u32 out_id;
1579 /* Avoid unbounded kernel memory allocation */
1580 if (crtc_req->count_connectors > config->num_connector) {
1581 ret = -EINVAL;
1582 goto out;
1585 connector_set = kmalloc(crtc_req->count_connectors *
1586 sizeof(struct drm_connector *),
1587 GFP_KERNEL);
1588 if (!connector_set) {
1589 ret = -ENOMEM;
1590 goto out;
1593 for (i = 0; i < crtc_req->count_connectors; i++) {
1594 set_connectors_ptr = (uint32_t *)(unsigned long)crtc_req->set_connectors_ptr;
1595 if (get_user(out_id, &set_connectors_ptr[i])) {
1596 ret = -EFAULT;
1597 goto out;
1600 obj = drm_mode_object_find(dev, out_id,
1601 DRM_MODE_OBJECT_CONNECTOR);
1602 if (!obj) {
1603 DRM_DEBUG_KMS("Connector id %d unknown\n",
1604 out_id);
1605 ret = -EINVAL;
1606 goto out;
1608 connector = obj_to_connector(obj);
1609 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1610 connector->base.id,
1611 drm_get_connector_name(connector));
1613 connector_set[i] = connector;
1617 set.crtc = crtc;
1618 set.x = crtc_req->x;
1619 set.y = crtc_req->y;
1620 set.mode = mode;
1621 set.connectors = connector_set;
1622 set.num_connectors = crtc_req->count_connectors;
1623 set.fb = fb;
1624 ret = crtc->funcs->set_config(&set);
1626 out:
1627 kfree(connector_set);
1628 mutex_unlock(&dev->mode_config.mutex);
1629 return ret;
1632 int drm_mode_cursor_ioctl(struct drm_device *dev,
1633 void *data, struct drm_file *file_priv)
1635 struct drm_mode_cursor *req = data;
1636 struct drm_mode_object *obj;
1637 struct drm_crtc *crtc;
1638 int ret = 0;
1640 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1641 return -EINVAL;
1643 if (!req->flags) {
1644 DRM_ERROR("no operation set\n");
1645 return -EINVAL;
1648 mutex_lock(&dev->mode_config.mutex);
1649 obj = drm_mode_object_find(dev, req->crtc_id, DRM_MODE_OBJECT_CRTC);
1650 if (!obj) {
1651 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
1652 ret = -EINVAL;
1653 goto out;
1655 crtc = obj_to_crtc(obj);
1657 if (req->flags & DRM_MODE_CURSOR_BO) {
1658 if (!crtc->funcs->cursor_set) {
1659 DRM_ERROR("crtc does not support cursor\n");
1660 ret = -ENXIO;
1661 goto out;
1663 /* Turns off the cursor if handle is 0 */
1664 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
1665 req->width, req->height);
1668 if (req->flags & DRM_MODE_CURSOR_MOVE) {
1669 if (crtc->funcs->cursor_move) {
1670 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
1671 } else {
1672 DRM_ERROR("crtc does not support cursor\n");
1673 ret = -EFAULT;
1674 goto out;
1677 out:
1678 mutex_unlock(&dev->mode_config.mutex);
1679 return ret;
1683 * drm_mode_addfb - add an FB to the graphics configuration
1684 * @inode: inode from the ioctl
1685 * @filp: file * from the ioctl
1686 * @cmd: cmd from ioctl
1687 * @arg: arg from ioctl
1689 * LOCKING:
1690 * Takes mode config lock.
1692 * Add a new FB to the specified CRTC, given a user request.
1694 * Called by the user via ioctl.
1696 * RETURNS:
1697 * Zero on success, errno on failure.
1699 int drm_mode_addfb(struct drm_device *dev,
1700 void *data, struct drm_file *file_priv)
1702 struct drm_mode_fb_cmd *r = data;
1703 struct drm_mode_config *config = &dev->mode_config;
1704 struct drm_framebuffer *fb;
1705 int ret = 0;
1707 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1708 return -EINVAL;
1710 if ((config->min_width > r->width) || (r->width > config->max_width)) {
1711 DRM_ERROR("mode new framebuffer width not within limits\n");
1712 return -EINVAL;
1714 if ((config->min_height > r->height) || (r->height > config->max_height)) {
1715 DRM_ERROR("mode new framebuffer height not within limits\n");
1716 return -EINVAL;
1719 mutex_lock(&dev->mode_config.mutex);
1721 /* TODO check buffer is sufficiently large */
1722 /* TODO setup destructor callback */
1724 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
1725 if (IS_ERR(fb)) {
1726 DRM_ERROR("could not create framebuffer\n");
1727 ret = PTR_ERR(fb);
1728 goto out;
1731 r->fb_id = fb->base.id;
1732 list_add(&fb->filp_head, &file_priv->fbs);
1733 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
1735 out:
1736 mutex_unlock(&dev->mode_config.mutex);
1737 return ret;
1741 * drm_mode_rmfb - remove an FB from the configuration
1742 * @inode: inode from the ioctl
1743 * @filp: file * from the ioctl
1744 * @cmd: cmd from ioctl
1745 * @arg: arg from ioctl
1747 * LOCKING:
1748 * Takes mode config lock.
1750 * Remove the FB specified by the user.
1752 * Called by the user via ioctl.
1754 * RETURNS:
1755 * Zero on success, errno on failure.
1757 int drm_mode_rmfb(struct drm_device *dev,
1758 void *data, struct drm_file *file_priv)
1760 struct drm_mode_object *obj;
1761 struct drm_framebuffer *fb = NULL;
1762 struct drm_framebuffer *fbl = NULL;
1763 uint32_t *id = data;
1764 int ret = 0;
1765 int found = 0;
1767 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1768 return -EINVAL;
1770 mutex_lock(&dev->mode_config.mutex);
1771 obj = drm_mode_object_find(dev, *id, DRM_MODE_OBJECT_FB);
1772 /* TODO check that we really get a framebuffer back. */
1773 if (!obj) {
1774 DRM_ERROR("mode invalid framebuffer id\n");
1775 ret = -EINVAL;
1776 goto out;
1778 fb = obj_to_fb(obj);
1780 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
1781 if (fb == fbl)
1782 found = 1;
1784 if (!found) {
1785 DRM_ERROR("tried to remove a fb that we didn't own\n");
1786 ret = -EINVAL;
1787 goto out;
1790 /* TODO release all crtc connected to the framebuffer */
1791 /* TODO unhock the destructor from the buffer object */
1793 list_del(&fb->filp_head);
1794 fb->funcs->destroy(fb);
1796 out:
1797 mutex_unlock(&dev->mode_config.mutex);
1798 return ret;
1801 #if !defined(__AROS__)
1803 * drm_mode_getfb - get FB info
1804 * @inode: inode from the ioctl
1805 * @filp: file * from the ioctl
1806 * @cmd: cmd from ioctl
1807 * @arg: arg from ioctl
1809 * LOCKING:
1810 * Caller? (FIXME)
1812 * Lookup the FB given its ID and return info about it.
1814 * Called by the user via ioctl.
1816 * RETURNS:
1817 * Zero on success, errno on failure.
1819 int drm_mode_getfb(struct drm_device *dev,
1820 void *data, struct drm_file *file_priv)
1822 struct drm_mode_fb_cmd *r = data;
1823 struct drm_mode_object *obj;
1824 struct drm_framebuffer *fb;
1825 int ret = 0;
1827 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1828 return -EINVAL;
1830 mutex_lock(&dev->mode_config.mutex);
1831 obj = drm_mode_object_find(dev, r->fb_id, DRM_MODE_OBJECT_FB);
1832 if (!obj) {
1833 DRM_ERROR("invalid framebuffer id\n");
1834 ret = -EINVAL;
1835 goto out;
1837 fb = obj_to_fb(obj);
1839 r->height = fb->height;
1840 r->width = fb->width;
1841 r->depth = fb->depth;
1842 r->bpp = fb->bits_per_pixel;
1843 r->pitch = fb->pitch;
1844 fb->funcs->create_handle(fb, file_priv, &r->handle);
1846 out:
1847 mutex_unlock(&dev->mode_config.mutex);
1848 return ret;
1851 int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
1852 void *data, struct drm_file *file_priv)
1854 struct drm_clip_rect __user *clips_ptr;
1855 struct drm_clip_rect *clips = NULL;
1856 struct drm_mode_fb_dirty_cmd *r = data;
1857 struct drm_mode_object *obj;
1858 struct drm_framebuffer *fb;
1859 unsigned flags;
1860 int num_clips;
1861 int ret = 0;
1863 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1864 return -EINVAL;
1866 mutex_lock(&dev->mode_config.mutex);
1867 obj = drm_mode_object_find(dev, r->fb_id, DRM_MODE_OBJECT_FB);
1868 if (!obj) {
1869 DRM_ERROR("invalid framebuffer id\n");
1870 ret = -EINVAL;
1871 goto out_err1;
1873 fb = obj_to_fb(obj);
1875 num_clips = r->num_clips;
1876 clips_ptr = (struct drm_clip_rect *)(unsigned long)r->clips_ptr;
1878 if (!num_clips != !clips_ptr) {
1879 ret = -EINVAL;
1880 goto out_err1;
1883 flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
1885 /* If userspace annotates copy, clips must come in pairs */
1886 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
1887 ret = -EINVAL;
1888 goto out_err1;
1891 if (num_clips && clips_ptr) {
1892 clips = kzalloc(num_clips * sizeof(*clips), GFP_KERNEL);
1893 if (!clips) {
1894 ret = -ENOMEM;
1895 goto out_err1;
1898 ret = copy_from_user(clips, clips_ptr,
1899 num_clips * sizeof(*clips));
1900 if (ret) {
1901 ret = -EFAULT;
1902 goto out_err2;
1906 if (fb->funcs->dirty) {
1907 ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
1908 clips, num_clips);
1909 } else {
1910 ret = -ENOSYS;
1911 goto out_err2;
1914 out_err2:
1915 kfree(clips);
1916 out_err1:
1917 mutex_unlock(&dev->mode_config.mutex);
1918 return ret;
1923 * drm_fb_release - remove and free the FBs on this file
1924 * @filp: file * from the ioctl
1926 * LOCKING:
1927 * Takes mode config lock.
1929 * Destroy all the FBs associated with @filp.
1931 * Called by the user via ioctl.
1933 * RETURNS:
1934 * Zero on success, errno on failure.
1936 void drm_fb_release(struct drm_file *priv)
1938 struct drm_device *dev = priv->minor->dev;
1939 struct drm_framebuffer *fb, *tfb;
1941 mutex_lock(&dev->mode_config.mutex);
1942 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
1943 list_del(&fb->filp_head);
1944 fb->funcs->destroy(fb);
1946 mutex_unlock(&dev->mode_config.mutex);
1950 * drm_mode_attachmode - add a mode to the user mode list
1951 * @dev: DRM device
1952 * @connector: connector to add the mode to
1953 * @mode: mode to add
1955 * Add @mode to @connector's user mode list.
1957 static int drm_mode_attachmode(struct drm_device *dev,
1958 struct drm_connector *connector,
1959 struct drm_display_mode *mode)
1961 int ret = 0;
1963 list_add_tail(&mode->head, &connector->user_modes);
1964 return ret;
1967 int drm_mode_attachmode_crtc(struct drm_device *dev, struct drm_crtc *crtc,
1968 struct drm_display_mode *mode)
1970 struct drm_connector *connector;
1971 int ret = 0;
1972 struct drm_display_mode *dup_mode;
1973 int need_dup = 0;
1974 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1975 if (!connector->encoder)
1976 break;
1977 if (connector->encoder->crtc == crtc) {
1978 if (need_dup)
1979 dup_mode = drm_mode_duplicate(dev, mode);
1980 else
1981 dup_mode = mode;
1982 ret = drm_mode_attachmode(dev, connector, dup_mode);
1983 if (ret)
1984 return ret;
1985 need_dup = 1;
1988 return 0;
1990 EXPORT_SYMBOL(drm_mode_attachmode_crtc);
1992 static int drm_mode_detachmode(struct drm_device *dev,
1993 struct drm_connector *connector,
1994 struct drm_display_mode *mode)
1996 int found = 0;
1997 int ret = 0;
1998 struct drm_display_mode *match_mode, *t;
2000 list_for_each_entry_safe(match_mode, t, &connector->user_modes, head) {
2001 if (drm_mode_equal(match_mode, mode)) {
2002 list_del(&match_mode->head);
2003 drm_mode_destroy(dev, match_mode);
2004 found = 1;
2005 break;
2009 if (!found)
2010 ret = -EINVAL;
2012 return ret;
2015 int drm_mode_detachmode_crtc(struct drm_device *dev, struct drm_display_mode *mode)
2017 struct drm_connector *connector;
2019 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2020 drm_mode_detachmode(dev, connector, mode);
2022 return 0;
2024 EXPORT_SYMBOL(drm_mode_detachmode_crtc);
2027 * drm_fb_attachmode - Attach a user mode to an connector
2028 * @inode: inode from the ioctl
2029 * @filp: file * from the ioctl
2030 * @cmd: cmd from ioctl
2031 * @arg: arg from ioctl
2033 * This attaches a user specified mode to an connector.
2034 * Called by the user via ioctl.
2036 * RETURNS:
2037 * Zero on success, errno on failure.
2039 int drm_mode_attachmode_ioctl(struct drm_device *dev,
2040 void *data, struct drm_file *file_priv)
2042 struct drm_mode_mode_cmd *mode_cmd = data;
2043 struct drm_connector *connector;
2044 struct drm_display_mode *mode;
2045 struct drm_mode_object *obj;
2046 struct drm_mode_modeinfo *umode = &mode_cmd->mode;
2047 int ret = 0;
2049 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2050 return -EINVAL;
2052 mutex_lock(&dev->mode_config.mutex);
2054 obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
2055 if (!obj) {
2056 ret = -EINVAL;
2057 goto out;
2059 connector = obj_to_connector(obj);
2061 mode = drm_mode_create(dev);
2062 if (!mode) {
2063 ret = -ENOMEM;
2064 goto out;
2067 drm_crtc_convert_umode(mode, umode);
2069 ret = drm_mode_attachmode(dev, connector, mode);
2070 out:
2071 mutex_unlock(&dev->mode_config.mutex);
2072 return ret;
2077 * drm_fb_detachmode - Detach a user specified mode from an connector
2078 * @inode: inode from the ioctl
2079 * @filp: file * from the ioctl
2080 * @cmd: cmd from ioctl
2081 * @arg: arg from ioctl
2083 * Called by the user via ioctl.
2085 * RETURNS:
2086 * Zero on success, errno on failure.
2088 int drm_mode_detachmode_ioctl(struct drm_device *dev,
2089 void *data, struct drm_file *file_priv)
2091 struct drm_mode_object *obj;
2092 struct drm_mode_mode_cmd *mode_cmd = data;
2093 struct drm_connector *connector;
2094 struct drm_display_mode mode;
2095 struct drm_mode_modeinfo *umode = &mode_cmd->mode;
2096 int ret = 0;
2098 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2099 return -EINVAL;
2101 mutex_lock(&dev->mode_config.mutex);
2103 obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
2104 if (!obj) {
2105 ret = -EINVAL;
2106 goto out;
2108 connector = obj_to_connector(obj);
2110 drm_crtc_convert_umode(&mode, umode);
2111 ret = drm_mode_detachmode(dev, connector, &mode);
2112 out:
2113 mutex_unlock(&dev->mode_config.mutex);
2114 return ret;
2116 #endif
2118 struct drm_property *drm_property_create(struct drm_device *dev, int flags,
2119 const char *name, int num_values)
2121 struct drm_property *property = NULL;
2123 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
2124 if (!property)
2125 return NULL;
2127 if (num_values) {
2128 property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
2129 if (!property->values)
2130 goto fail;
2133 drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
2134 property->flags = flags;
2135 property->num_values = num_values;
2136 INIT_LIST_HEAD(&property->enum_blob_list);
2138 if (name)
2139 strncpy(property->name, name, DRM_PROP_NAME_LEN);
2141 list_add_tail(&property->head, &dev->mode_config.property_list);
2142 return property;
2143 fail:
2144 kfree(property);
2145 return NULL;
2147 EXPORT_SYMBOL(drm_property_create);
2149 int drm_property_add_enum(struct drm_property *property, int index,
2150 uint64_t value, const char *name)
2152 struct drm_property_enum *prop_enum;
2154 if (!(property->flags & DRM_MODE_PROP_ENUM))
2155 return -EINVAL;
2157 if (!list_empty(&property->enum_blob_list)) {
2158 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
2159 if (prop_enum->value == value) {
2160 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2161 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2162 return 0;
2167 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
2168 if (!prop_enum)
2169 return -ENOMEM;
2171 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2172 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2173 prop_enum->value = value;
2175 property->values[index] = value;
2176 list_add_tail(&prop_enum->head, &property->enum_blob_list);
2177 return 0;
2179 EXPORT_SYMBOL(drm_property_add_enum);
2181 void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
2183 struct drm_property_enum *prop_enum, *pt;
2185 list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
2186 list_del(&prop_enum->head);
2187 kfree(prop_enum);
2190 if (property->num_values)
2191 kfree(property->values);
2192 drm_mode_object_put(dev, &property->base);
2193 list_del(&property->head);
2194 kfree(property);
2196 EXPORT_SYMBOL(drm_property_destroy);
2198 int drm_connector_attach_property(struct drm_connector *connector,
2199 struct drm_property *property, uint64_t init_val)
2201 int i;
2203 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
2204 if (connector->property_ids[i] == 0) {
2205 connector->property_ids[i] = property->base.id;
2206 connector->property_values[i] = init_val;
2207 break;
2211 if (i == DRM_CONNECTOR_MAX_PROPERTY)
2212 return -EINVAL;
2213 return 0;
2215 EXPORT_SYMBOL(drm_connector_attach_property);
2217 int drm_connector_property_set_value(struct drm_connector *connector,
2218 struct drm_property *property, uint64_t value)
2220 int i;
2222 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
2223 if (connector->property_ids[i] == property->base.id) {
2224 connector->property_values[i] = value;
2225 break;
2229 if (i == DRM_CONNECTOR_MAX_PROPERTY)
2230 return -EINVAL;
2231 return 0;
2233 EXPORT_SYMBOL(drm_connector_property_set_value);
2235 #if !defined(__AROS__)
2236 int drm_connector_property_get_value(struct drm_connector *connector,
2237 struct drm_property *property, uint64_t *val)
2239 int i;
2241 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
2242 if (connector->property_ids[i] == property->base.id) {
2243 *val = connector->property_values[i];
2244 break;
2248 if (i == DRM_CONNECTOR_MAX_PROPERTY)
2249 return -EINVAL;
2250 return 0;
2252 EXPORT_SYMBOL(drm_connector_property_get_value);
2254 int drm_mode_getproperty_ioctl(struct drm_device *dev,
2255 void *data, struct drm_file *file_priv)
2257 struct drm_mode_object *obj;
2258 struct drm_mode_get_property *out_resp = data;
2259 struct drm_property *property;
2260 int enum_count = 0;
2261 int blob_count = 0;
2262 int value_count = 0;
2263 int ret = 0, i;
2264 int copied;
2265 struct drm_property_enum *prop_enum;
2266 struct drm_mode_property_enum __user *enum_ptr;
2267 struct drm_property_blob *prop_blob;
2268 uint32_t *blob_id_ptr;
2269 uint64_t __user *values_ptr;
2270 uint32_t __user *blob_length_ptr;
2272 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2273 return -EINVAL;
2275 mutex_lock(&dev->mode_config.mutex);
2276 obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
2277 if (!obj) {
2278 ret = -EINVAL;
2279 goto done;
2281 property = obj_to_property(obj);
2283 if (property->flags & DRM_MODE_PROP_ENUM) {
2284 list_for_each_entry(prop_enum, &property->enum_blob_list, head)
2285 enum_count++;
2286 } else if (property->flags & DRM_MODE_PROP_BLOB) {
2287 list_for_each_entry(prop_blob, &property->enum_blob_list, head)
2288 blob_count++;
2291 value_count = property->num_values;
2293 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
2294 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
2295 out_resp->flags = property->flags;
2297 if ((out_resp->count_values >= value_count) && value_count) {
2298 values_ptr = (uint64_t *)(unsigned long)out_resp->values_ptr;
2299 for (i = 0; i < value_count; i++) {
2300 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
2301 ret = -EFAULT;
2302 goto done;
2306 out_resp->count_values = value_count;
2308 if (property->flags & DRM_MODE_PROP_ENUM) {
2309 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
2310 copied = 0;
2311 enum_ptr = (struct drm_mode_property_enum *)(unsigned long)out_resp->enum_blob_ptr;
2312 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
2314 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
2315 ret = -EFAULT;
2316 goto done;
2319 if (copy_to_user(&enum_ptr[copied].name,
2320 &prop_enum->name, DRM_PROP_NAME_LEN)) {
2321 ret = -EFAULT;
2322 goto done;
2324 copied++;
2327 out_resp->count_enum_blobs = enum_count;
2330 if (property->flags & DRM_MODE_PROP_BLOB) {
2331 if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
2332 copied = 0;
2333 blob_id_ptr = (uint32_t *)(unsigned long)out_resp->enum_blob_ptr;
2334 blob_length_ptr = (uint32_t *)(unsigned long)out_resp->values_ptr;
2336 list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
2337 if (put_user(prop_blob->base.id, blob_id_ptr + copied)) {
2338 ret = -EFAULT;
2339 goto done;
2342 if (put_user(prop_blob->length, blob_length_ptr + copied)) {
2343 ret = -EFAULT;
2344 goto done;
2347 copied++;
2350 out_resp->count_enum_blobs = blob_count;
2352 done:
2353 mutex_unlock(&dev->mode_config.mutex);
2354 return ret;
2356 #endif
2358 static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
2359 void *data)
2361 struct drm_property_blob *blob;
2363 if (!length || !data)
2364 return NULL;
2366 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
2367 if (!blob)
2368 return NULL;
2370 blob->data = (void *)((char *)blob + sizeof(struct drm_property_blob));
2371 blob->length = length;
2373 memcpy(blob->data, data, length);
2375 drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
2377 list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
2378 return blob;
2381 static void drm_property_destroy_blob(struct drm_device *dev,
2382 struct drm_property_blob *blob)
2384 drm_mode_object_put(dev, &blob->base);
2385 list_del(&blob->head);
2386 kfree(blob);
2389 #if !defined(__AROS__)
2390 int drm_mode_getblob_ioctl(struct drm_device *dev,
2391 void *data, struct drm_file *file_priv)
2393 struct drm_mode_object *obj;
2394 struct drm_mode_get_blob *out_resp = data;
2395 struct drm_property_blob *blob;
2396 int ret = 0;
2397 void *blob_ptr;
2399 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2400 return -EINVAL;
2402 mutex_lock(&dev->mode_config.mutex);
2403 obj = drm_mode_object_find(dev, out_resp->blob_id, DRM_MODE_OBJECT_BLOB);
2404 if (!obj) {
2405 ret = -EINVAL;
2406 goto done;
2408 blob = obj_to_blob(obj);
2410 if (out_resp->length == blob->length) {
2411 blob_ptr = (void *)(unsigned long)out_resp->data;
2412 if (copy_to_user(blob_ptr, blob->data, blob->length)){
2413 ret = -EFAULT;
2414 goto done;
2417 out_resp->length = blob->length;
2419 done:
2420 mutex_unlock(&dev->mode_config.mutex);
2421 return ret;
2423 #endif
2425 int drm_mode_connector_update_edid_property(struct drm_connector *connector,
2426 struct edid *edid)
2428 struct drm_device *dev = connector->dev;
2429 int ret = 0, size;
2431 if (connector->edid_blob_ptr)
2432 drm_property_destroy_blob(dev, connector->edid_blob_ptr);
2434 /* Delete edid, when there is none. */
2435 if (!edid) {
2436 connector->edid_blob_ptr = NULL;
2437 ret = drm_connector_property_set_value(connector, dev->mode_config.edid_property, 0);
2438 return ret;
2441 size = EDID_LENGTH * (1 + edid->extensions);
2442 connector->edid_blob_ptr = drm_property_create_blob(connector->dev,
2443 size, edid);
2445 ret = drm_connector_property_set_value(connector,
2446 dev->mode_config.edid_property,
2447 connector->edid_blob_ptr->base.id);
2449 return ret;
2451 EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
2453 #if !defined(__AROS__)
2454 int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
2455 void *data, struct drm_file *file_priv)
2457 struct drm_mode_connector_set_property *out_resp = data;
2458 struct drm_mode_object *obj;
2459 struct drm_property *property;
2460 struct drm_connector *connector;
2461 int ret = -EINVAL;
2462 int i;
2464 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2465 return -EINVAL;
2467 mutex_lock(&dev->mode_config.mutex);
2469 obj = drm_mode_object_find(dev, out_resp->connector_id, DRM_MODE_OBJECT_CONNECTOR);
2470 if (!obj) {
2471 goto out;
2473 connector = obj_to_connector(obj);
2475 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
2476 if (connector->property_ids[i] == out_resp->prop_id)
2477 break;
2480 if (i == DRM_CONNECTOR_MAX_PROPERTY) {
2481 goto out;
2484 obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
2485 if (!obj) {
2486 goto out;
2488 property = obj_to_property(obj);
2490 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
2491 goto out;
2493 if (property->flags & DRM_MODE_PROP_RANGE) {
2494 if (out_resp->value < property->values[0])
2495 goto out;
2497 if (out_resp->value > property->values[1])
2498 goto out;
2499 } else {
2500 int found = 0;
2501 for (i = 0; i < property->num_values; i++) {
2502 if (property->values[i] == out_resp->value) {
2503 found = 1;
2504 break;
2507 if (!found) {
2508 goto out;
2512 /* Do DPMS ourselves */
2513 if (property == connector->dev->mode_config.dpms_property) {
2514 if (connector->funcs->dpms)
2515 (*connector->funcs->dpms)(connector, (int) out_resp->value);
2516 ret = 0;
2517 } else if (connector->funcs->set_property)
2518 ret = connector->funcs->set_property(connector, property, out_resp->value);
2520 /* store the property value if successful */
2521 if (!ret)
2522 drm_connector_property_set_value(connector, property, out_resp->value);
2523 out:
2524 mutex_unlock(&dev->mode_config.mutex);
2525 return ret;
2527 #endif
2529 int drm_mode_connector_attach_encoder(struct drm_connector *connector,
2530 struct drm_encoder *encoder)
2532 int i;
2534 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
2535 if (connector->encoder_ids[i] == 0) {
2536 connector->encoder_ids[i] = encoder->base.id;
2537 return 0;
2540 return -ENOMEM;
2542 EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
2544 #if !defined(__AROS__)
2545 void drm_mode_connector_detach_encoder(struct drm_connector *connector,
2546 struct drm_encoder *encoder)
2548 int i;
2549 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
2550 if (connector->encoder_ids[i] == encoder->base.id) {
2551 connector->encoder_ids[i] = 0;
2552 if (connector->encoder == encoder)
2553 connector->encoder = NULL;
2554 break;
2558 EXPORT_SYMBOL(drm_mode_connector_detach_encoder);
2559 #endif
2561 bool drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
2562 int gamma_size)
2564 crtc->gamma_size = gamma_size;
2566 crtc->gamma_store = kzalloc(gamma_size * sizeof(uint16_t) * 3, GFP_KERNEL);
2567 if (!crtc->gamma_store) {
2568 crtc->gamma_size = 0;
2569 return false;
2572 return true;
2574 EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
2576 #if !defined(__AROS__)
2577 int drm_mode_gamma_set_ioctl(struct drm_device *dev,
2578 void *data, struct drm_file *file_priv)
2580 struct drm_mode_crtc_lut *crtc_lut = data;
2581 struct drm_mode_object *obj;
2582 struct drm_crtc *crtc;
2583 void *r_base, *g_base, *b_base;
2584 int size;
2585 int ret = 0;
2587 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2588 return -EINVAL;
2590 mutex_lock(&dev->mode_config.mutex);
2591 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
2592 if (!obj) {
2593 ret = -EINVAL;
2594 goto out;
2596 crtc = obj_to_crtc(obj);
2598 /* memcpy into gamma store */
2599 if (crtc_lut->gamma_size != crtc->gamma_size) {
2600 ret = -EINVAL;
2601 goto out;
2604 size = crtc_lut->gamma_size * (sizeof(uint16_t));
2605 r_base = crtc->gamma_store;
2606 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
2607 ret = -EFAULT;
2608 goto out;
2611 g_base = r_base + size;
2612 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
2613 ret = -EFAULT;
2614 goto out;
2617 b_base = g_base + size;
2618 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
2619 ret = -EFAULT;
2620 goto out;
2623 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
2625 out:
2626 mutex_unlock(&dev->mode_config.mutex);
2627 return ret;
2631 int drm_mode_gamma_get_ioctl(struct drm_device *dev,
2632 void *data, struct drm_file *file_priv)
2634 struct drm_mode_crtc_lut *crtc_lut = data;
2635 struct drm_mode_object *obj;
2636 struct drm_crtc *crtc;
2637 void *r_base, *g_base, *b_base;
2638 int size;
2639 int ret = 0;
2641 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2642 return -EINVAL;
2644 mutex_lock(&dev->mode_config.mutex);
2645 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
2646 if (!obj) {
2647 ret = -EINVAL;
2648 goto out;
2650 crtc = obj_to_crtc(obj);
2652 /* memcpy into gamma store */
2653 if (crtc_lut->gamma_size != crtc->gamma_size) {
2654 ret = -EINVAL;
2655 goto out;
2658 size = crtc_lut->gamma_size * (sizeof(uint16_t));
2659 r_base = crtc->gamma_store;
2660 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
2661 ret = -EFAULT;
2662 goto out;
2665 g_base = r_base + size;
2666 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
2667 ret = -EFAULT;
2668 goto out;
2671 b_base = g_base + size;
2672 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
2673 ret = -EFAULT;
2674 goto out;
2676 out:
2677 mutex_unlock(&dev->mode_config.mutex);
2678 return ret;
2681 int drm_mode_page_flip_ioctl(struct drm_device *dev,
2682 void *data, struct drm_file *file_priv)
2684 struct drm_mode_crtc_page_flip *page_flip = data;
2685 struct drm_mode_object *obj;
2686 struct drm_crtc *crtc;
2687 struct drm_framebuffer *fb;
2688 struct drm_pending_vblank_event *e = NULL;
2689 unsigned long flags;
2690 int ret = -EINVAL;
2692 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
2693 page_flip->reserved != 0)
2694 return -EINVAL;
2696 mutex_lock(&dev->mode_config.mutex);
2697 obj = drm_mode_object_find(dev, page_flip->crtc_id, DRM_MODE_OBJECT_CRTC);
2698 if (!obj)
2699 goto out;
2700 crtc = obj_to_crtc(obj);
2702 if (crtc->fb == NULL) {
2703 /* The framebuffer is currently unbound, presumably
2704 * due to a hotplug event, that userspace has not
2705 * yet discovered.
2707 ret = -EBUSY;
2708 goto out;
2711 if (crtc->funcs->page_flip == NULL)
2712 goto out;
2714 obj = drm_mode_object_find(dev, page_flip->fb_id, DRM_MODE_OBJECT_FB);
2715 if (!obj)
2716 goto out;
2717 fb = obj_to_fb(obj);
2719 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
2720 ret = -ENOMEM;
2721 spin_lock_irqsave(&dev->event_lock, flags);
2722 if (file_priv->event_space < sizeof e->event) {
2723 spin_unlock_irqrestore(&dev->event_lock, flags);
2724 goto out;
2726 file_priv->event_space -= sizeof e->event;
2727 spin_unlock_irqrestore(&dev->event_lock, flags);
2729 e = kzalloc(sizeof *e, GFP_KERNEL);
2730 if (e == NULL) {
2731 spin_lock_irqsave(&dev->event_lock, flags);
2732 file_priv->event_space += sizeof e->event;
2733 spin_unlock_irqrestore(&dev->event_lock, flags);
2734 goto out;
2737 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
2738 e->event.base.length = sizeof e->event;
2739 e->event.user_data = page_flip->user_data;
2740 e->base.event = &e->event.base;
2741 e->base.file_priv = file_priv;
2742 e->base.destroy =
2743 (void (*) (struct drm_pending_event *)) kfree;
2746 ret = crtc->funcs->page_flip(crtc, fb, e);
2747 if (ret) {
2748 spin_lock_irqsave(&dev->event_lock, flags);
2749 file_priv->event_space += sizeof e->event;
2750 spin_unlock_irqrestore(&dev->event_lock, flags);
2751 kfree(e);
2754 out:
2755 mutex_unlock(&dev->mode_config.mutex);
2756 return ret;
2759 void drm_mode_config_reset(struct drm_device *dev)
2761 struct drm_crtc *crtc;
2762 struct drm_encoder *encoder;
2763 struct drm_connector *connector;
2765 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
2766 if (crtc->funcs->reset)
2767 crtc->funcs->reset(crtc);
2769 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
2770 if (encoder->funcs->reset)
2771 encoder->funcs->reset(encoder);
2773 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
2774 if (connector->funcs->reset)
2775 connector->funcs->reset(connector);
2777 EXPORT_SYMBOL(drm_mode_config_reset);
2779 int drm_mode_create_dumb_ioctl(struct drm_device *dev,
2780 void *data, struct drm_file *file_priv)
2782 struct drm_mode_create_dumb *args = data;
2784 if (!dev->driver->dumb_create)
2785 return -ENOSYS;
2786 return dev->driver->dumb_create(file_priv, dev, args);
2789 int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
2790 void *data, struct drm_file *file_priv)
2792 struct drm_mode_map_dumb *args = data;
2794 /* call driver ioctl to get mmap offset */
2795 if (!dev->driver->dumb_map_offset)
2796 return -ENOSYS;
2798 return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
2801 int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
2802 void *data, struct drm_file *file_priv)
2804 struct drm_mode_destroy_dumb *args = data;
2806 if (!dev->driver->dumb_destroy)
2807 return -ENOSYS;
2809 return dev->driver->dumb_destroy(file_priv, dev, args->handle);
2811 #endif