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
28 * Eric Anholt <eric@anholt.net>
29 * Dave Airlie <airlied@linux.ie>
30 * Jesse Barnes <jesse.barnes@intel.com>
32 #include <linux/list.h>
37 struct drm_prop_enum_list {
42 /* Avoid boilerplate. I'm tired of typing. */
43 #define DRM_ENUM_NAME_FN(fnname, list) \
44 char *fnname(int val) \
47 for (i = 0; i < ARRAY_SIZE(list); i++) { \
48 if (list[i].type == val) \
49 return list[i].name; \
57 static struct drm_prop_enum_list drm_dpms_enum_list[] =
58 { { DRM_MODE_DPMS_ON, "On" },
59 { DRM_MODE_DPMS_STANDBY, "Standby" },
60 { DRM_MODE_DPMS_SUSPEND, "Suspend" },
61 { DRM_MODE_DPMS_OFF, "Off" }
64 DRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list)
69 static struct drm_prop_enum_list drm_scaling_mode_enum_list[] =
71 { DRM_MODE_SCALE_NON_GPU, "Non-GPU" },
72 { DRM_MODE_SCALE_FULLSCREEN, "Fullscreen" },
73 { DRM_MODE_SCALE_NO_SCALE, "No scale" },
74 { DRM_MODE_SCALE_ASPECT, "Aspect" },
77 static struct drm_prop_enum_list drm_dithering_mode_enum_list[] =
79 { DRM_MODE_DITHERING_OFF, "Off" },
80 { DRM_MODE_DITHERING_ON, "On" },
84 * Non-global properties, but "required" for certain connectors.
86 static struct drm_prop_enum_list drm_dvi_i_select_enum_list[] =
88 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
89 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
90 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
93 DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list)
95 static struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list[] =
97 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
98 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
99 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
102 DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name,
103 drm_dvi_i_subconnector_enum_list)
105 static struct drm_prop_enum_list drm_tv_select_enum_list[] =
107 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
108 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
109 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
110 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
113 DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
115 static struct drm_prop_enum_list drm_tv_subconnector_enum_list[] =
117 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
118 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
119 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
120 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
123 DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,
124 drm_tv_subconnector_enum_list)
126 struct drm_conn_prop_enum_list {
133 * Connector and encoder types.
135 static struct drm_conn_prop_enum_list drm_connector_enum_list[] =
136 { { DRM_MODE_CONNECTOR_Unknown, "Unknown", 0 },
137 { DRM_MODE_CONNECTOR_VGA, "VGA", 0 },
138 { DRM_MODE_CONNECTOR_DVII, "DVI-I", 0 },
139 { DRM_MODE_CONNECTOR_DVID, "DVI-D", 0 },
140 { DRM_MODE_CONNECTOR_DVIA, "DVI-A", 0 },
141 { DRM_MODE_CONNECTOR_Composite, "Composite", 0 },
142 { DRM_MODE_CONNECTOR_SVIDEO, "SVIDEO", 0 },
143 { DRM_MODE_CONNECTOR_LVDS, "LVDS", 0 },
144 { DRM_MODE_CONNECTOR_Component, "Component", 0 },
145 { DRM_MODE_CONNECTOR_9PinDIN, "9-pin DIN", 0 },
146 { DRM_MODE_CONNECTOR_DisplayPort, "DisplayPort", 0 },
147 { DRM_MODE_CONNECTOR_HDMIA, "HDMI Type A", 0 },
148 { DRM_MODE_CONNECTOR_HDMIB, "HDMI Type B", 0 },
151 static struct drm_prop_enum_list drm_encoder_enum_list[] =
152 { { DRM_MODE_ENCODER_NONE, "None" },
153 { DRM_MODE_ENCODER_DAC, "DAC" },
154 { DRM_MODE_ENCODER_TMDS, "TMDS" },
155 { DRM_MODE_ENCODER_LVDS, "LVDS" },
156 { DRM_MODE_ENCODER_TVDAC, "TV" },
159 char *drm_get_encoder_name(struct drm_encoder *encoder)
163 snprintf(buf, 32, "%s-%d",
164 drm_encoder_enum_list[encoder->encoder_type].name,
169 char *drm_get_connector_name(struct drm_connector *connector)
173 snprintf(buf, 32, "%s-%d",
174 drm_connector_enum_list[connector->connector_type].name,
175 connector->connector_type_id);
178 EXPORT_SYMBOL(drm_get_connector_name);
180 char *drm_get_connector_status_name(enum drm_connector_status status)
182 if (status == connector_status_connected)
184 else if (status == connector_status_disconnected)
185 return "disconnected";
191 * drm_mode_object_get - allocate a new identifier
193 * @ptr: object pointer, used to generate unique ID
197 * Caller must hold DRM mode_config lock.
199 * Create a unique identifier based on @ptr in @dev's identifier space. Used
200 * for tracking modes, CRTCs and connectors.
203 * New unique (relative to other objects in @dev) integer identifier for the
206 static int drm_mode_object_get(struct drm_device *dev,
207 struct drm_mode_object *obj, uint32_t obj_type)
212 WARN(!mutex_is_locked(&dev->mode_config.mutex),
213 "%s called w/o mode_config lock\n", __FUNCTION__);
215 if (idr_pre_get(&dev->mode_config.crtc_idr, GFP_KERNEL) == 0) {
216 DRM_ERROR("Ran out memory getting a mode number\n");
220 ret = idr_get_new_above(&dev->mode_config.crtc_idr, obj, 1, &new_id);
225 obj->type = obj_type;
230 * drm_mode_object_put - free an identifer
235 * Caller must hold DRM mode_config lock.
237 * Free @id from @dev's unique identifier pool.
239 static void drm_mode_object_put(struct drm_device *dev,
240 struct drm_mode_object *object)
242 idr_remove(&dev->mode_config.crtc_idr, object->id);
245 void *drm_mode_object_find(struct drm_device *dev, uint32_t id, uint32_t type)
247 struct drm_mode_object *obj;
249 obj = idr_find(&dev->mode_config.crtc_idr, id);
250 if (!obj || (obj->type != type) || (obj->id != id))
255 EXPORT_SYMBOL(drm_mode_object_find);
258 * drm_crtc_from_fb - find the CRTC structure associated with an fb
260 * @fb: framebuffer in question
263 * Caller must hold mode_config lock.
265 * Find CRTC in the mode_config structure that matches @fb.
268 * Pointer to the CRTC or NULL if it wasn't found.
270 struct drm_crtc *drm_crtc_from_fb(struct drm_device *dev,
271 struct drm_framebuffer *fb)
273 struct drm_crtc *crtc;
275 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
283 * drm_framebuffer_init - initialize a framebuffer
287 * Caller must hold mode config lock.
289 * Allocates an ID for the framebuffer's parent mode object, sets its mode
290 * functions & device file and adds it to the master fd list.
293 * Zero on success, error code on falure.
295 int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
296 const struct drm_framebuffer_funcs *funcs)
300 ret = drm_mode_object_get(dev, &fb->base, DRM_MODE_OBJECT_FB);
307 dev->mode_config.num_fb++;
308 list_add(&fb->head, &dev->mode_config.fb_list);
312 EXPORT_SYMBOL(drm_framebuffer_init);
315 * drm_framebuffer_cleanup - remove a framebuffer object
316 * @fb: framebuffer to remove
319 * Caller must hold mode config lock.
321 * Scans all the CRTCs in @dev's mode_config. If they're using @fb, removes
322 * it, setting it to NULL.
324 void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
326 struct drm_device *dev = fb->dev;
327 struct drm_crtc *crtc;
329 /* remove from any CRTC */
330 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
335 drm_mode_object_put(dev, &fb->base);
337 dev->mode_config.num_fb--;
339 EXPORT_SYMBOL(drm_framebuffer_cleanup);
342 * drm_crtc_init - Initialise a new CRTC object
344 * @crtc: CRTC object to init
345 * @funcs: callbacks for the new CRTC
348 * Caller must hold mode config lock.
350 * Inits a new object created as base part of an driver crtc object.
352 void drm_crtc_init(struct drm_device *dev, struct drm_crtc *crtc,
353 const struct drm_crtc_funcs *funcs)
358 mutex_lock(&dev->mode_config.mutex);
359 drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
361 list_add_tail(&crtc->head, &dev->mode_config.crtc_list);
362 dev->mode_config.num_crtc++;
363 mutex_unlock(&dev->mode_config.mutex);
365 EXPORT_SYMBOL(drm_crtc_init);
368 * drm_crtc_cleanup - Cleans up the core crtc usage.
369 * @crtc: CRTC to cleanup
372 * Caller must hold mode config lock.
374 * Cleanup @crtc. Removes from drm modesetting space
375 * does NOT free object, caller does that.
377 void drm_crtc_cleanup(struct drm_crtc *crtc)
379 struct drm_device *dev = crtc->dev;
381 if (crtc->gamma_store) {
382 kfree(crtc->gamma_store);
383 crtc->gamma_store = NULL;
386 drm_mode_object_put(dev, &crtc->base);
387 list_del(&crtc->head);
388 dev->mode_config.num_crtc--;
390 EXPORT_SYMBOL(drm_crtc_cleanup);
393 * drm_mode_probed_add - add a mode to a connector's probed mode list
394 * @connector: connector the new mode
398 * Caller must hold mode config lock.
400 * Add @mode to @connector's mode list for later use.
402 void drm_mode_probed_add(struct drm_connector *connector,
403 struct drm_display_mode *mode)
405 list_add(&mode->head, &connector->probed_modes);
407 EXPORT_SYMBOL(drm_mode_probed_add);
410 * drm_mode_remove - remove and free a mode
411 * @connector: connector list to modify
412 * @mode: mode to remove
415 * Caller must hold mode config lock.
417 * Remove @mode from @connector's mode list, then free it.
419 void drm_mode_remove(struct drm_connector *connector,
420 struct drm_display_mode *mode)
422 list_del(&mode->head);
425 EXPORT_SYMBOL(drm_mode_remove);
428 * drm_connector_init - Init a preallocated connector
430 * @connector: the connector to init
431 * @funcs: callbacks for this connector
432 * @name: user visible name of the connector
435 * Caller must hold @dev's mode_config lock.
437 * Initialises a preallocated connector. Connectors should be
438 * subclassed as part of driver connector objects.
440 void drm_connector_init(struct drm_device *dev,
441 struct drm_connector *connector,
442 const struct drm_connector_funcs *funcs,
445 mutex_lock(&dev->mode_config.mutex);
447 connector->dev = dev;
448 connector->funcs = funcs;
449 drm_mode_object_get(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR);
450 connector->connector_type = connector_type;
451 connector->connector_type_id =
452 ++drm_connector_enum_list[connector_type].count; /* TODO */
453 INIT_LIST_HEAD(&connector->user_modes);
454 INIT_LIST_HEAD(&connector->probed_modes);
455 INIT_LIST_HEAD(&connector->modes);
456 connector->edid_blob_ptr = NULL;
458 list_add_tail(&connector->head, &dev->mode_config.connector_list);
459 dev->mode_config.num_connector++;
461 drm_connector_attach_property(connector,
462 dev->mode_config.edid_property, 0);
464 drm_connector_attach_property(connector,
465 dev->mode_config.dpms_property, 0);
467 mutex_unlock(&dev->mode_config.mutex);
469 EXPORT_SYMBOL(drm_connector_init);
472 * drm_connector_cleanup - cleans up an initialised connector
473 * @connector: connector to cleanup
476 * Caller must hold @dev's mode_config lock.
478 * Cleans up the connector but doesn't free the object.
480 void drm_connector_cleanup(struct drm_connector *connector)
482 struct drm_device *dev = connector->dev;
483 struct drm_display_mode *mode, *t;
485 list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
486 drm_mode_remove(connector, mode);
488 list_for_each_entry_safe(mode, t, &connector->modes, head)
489 drm_mode_remove(connector, mode);
491 list_for_each_entry_safe(mode, t, &connector->user_modes, head)
492 drm_mode_remove(connector, mode);
494 mutex_lock(&dev->mode_config.mutex);
495 drm_mode_object_put(dev, &connector->base);
496 list_del(&connector->head);
497 mutex_unlock(&dev->mode_config.mutex);
499 EXPORT_SYMBOL(drm_connector_cleanup);
501 void drm_encoder_init(struct drm_device *dev,
502 struct drm_encoder *encoder,
503 const struct drm_encoder_funcs *funcs,
506 mutex_lock(&dev->mode_config.mutex);
510 drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
511 encoder->encoder_type = encoder_type;
512 encoder->funcs = funcs;
514 list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
515 dev->mode_config.num_encoder++;
517 mutex_unlock(&dev->mode_config.mutex);
519 EXPORT_SYMBOL(drm_encoder_init);
521 void drm_encoder_cleanup(struct drm_encoder *encoder)
523 struct drm_device *dev = encoder->dev;
524 mutex_lock(&dev->mode_config.mutex);
525 drm_mode_object_put(dev, &encoder->base);
526 list_del(&encoder->head);
527 mutex_unlock(&dev->mode_config.mutex);
529 EXPORT_SYMBOL(drm_encoder_cleanup);
532 * drm_mode_create - create a new display mode
536 * Caller must hold DRM mode_config lock.
538 * Create a new drm_display_mode, give it an ID, and return it.
541 * Pointer to new mode on success, NULL on error.
543 struct drm_display_mode *drm_mode_create(struct drm_device *dev)
545 struct drm_display_mode *nmode;
547 nmode = kzalloc(sizeof(struct drm_display_mode), GFP_KERNEL);
551 drm_mode_object_get(dev, &nmode->base, DRM_MODE_OBJECT_MODE);
554 EXPORT_SYMBOL(drm_mode_create);
557 * drm_mode_destroy - remove a mode
559 * @mode: mode to remove
562 * Caller must hold mode config lock.
564 * Free @mode's unique identifier, then free it.
566 void drm_mode_destroy(struct drm_device *dev, struct drm_display_mode *mode)
568 drm_mode_object_put(dev, &mode->base);
572 EXPORT_SYMBOL(drm_mode_destroy);
574 static int drm_mode_create_standard_connector_properties(struct drm_device *dev)
576 struct drm_property *edid;
577 struct drm_property *dpms;
581 * Standard properties (apply to all connectors)
583 edid = drm_property_create(dev, DRM_MODE_PROP_BLOB |
584 DRM_MODE_PROP_IMMUTABLE,
586 dev->mode_config.edid_property = edid;
588 dpms = drm_property_create(dev, DRM_MODE_PROP_ENUM,
589 "DPMS", ARRAY_SIZE(drm_dpms_enum_list));
590 for (i = 0; i < ARRAY_SIZE(drm_dpms_enum_list); i++)
591 drm_property_add_enum(dpms, i, drm_dpms_enum_list[i].type,
592 drm_dpms_enum_list[i].name);
593 dev->mode_config.dpms_property = dpms;
599 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
602 * Called by a driver the first time a DVI-I connector is made.
604 int drm_mode_create_dvi_i_properties(struct drm_device *dev)
606 struct drm_property *dvi_i_selector;
607 struct drm_property *dvi_i_subconnector;
610 if (dev->mode_config.dvi_i_select_subconnector_property)
614 drm_property_create(dev, DRM_MODE_PROP_ENUM,
615 "select subconnector",
616 ARRAY_SIZE(drm_dvi_i_select_enum_list));
617 for (i = 0; i < ARRAY_SIZE(drm_dvi_i_select_enum_list); i++)
618 drm_property_add_enum(dvi_i_selector, i,
619 drm_dvi_i_select_enum_list[i].type,
620 drm_dvi_i_select_enum_list[i].name);
621 dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
624 drm_property_create(dev, DRM_MODE_PROP_ENUM |
625 DRM_MODE_PROP_IMMUTABLE,
627 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
628 for (i = 0; i < ARRAY_SIZE(drm_dvi_i_subconnector_enum_list); i++)
629 drm_property_add_enum(dvi_i_subconnector, i,
630 drm_dvi_i_subconnector_enum_list[i].type,
631 drm_dvi_i_subconnector_enum_list[i].name);
632 dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
636 EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
639 * drm_create_tv_properties - create TV specific connector properties
641 * @num_modes: number of different TV formats (modes) supported
642 * @modes: array of pointers to strings containing name of each format
644 * Called by a driver's TV initialization routine, this function creates
645 * the TV specific connector properties for a given device. Caller is
646 * responsible for allocating a list of format names and passing them to
649 int drm_mode_create_tv_properties(struct drm_device *dev, int num_modes,
652 struct drm_property *tv_selector;
653 struct drm_property *tv_subconnector;
656 if (dev->mode_config.tv_select_subconnector_property)
660 * Basic connector properties
662 tv_selector = drm_property_create(dev, DRM_MODE_PROP_ENUM,
663 "select subconnector",
664 ARRAY_SIZE(drm_tv_select_enum_list));
665 for (i = 0; i < ARRAY_SIZE(drm_tv_select_enum_list); i++)
666 drm_property_add_enum(tv_selector, i,
667 drm_tv_select_enum_list[i].type,
668 drm_tv_select_enum_list[i].name);
669 dev->mode_config.tv_select_subconnector_property = tv_selector;
672 drm_property_create(dev, DRM_MODE_PROP_ENUM |
673 DRM_MODE_PROP_IMMUTABLE, "subconnector",
674 ARRAY_SIZE(drm_tv_subconnector_enum_list));
675 for (i = 0; i < ARRAY_SIZE(drm_tv_subconnector_enum_list); i++)
676 drm_property_add_enum(tv_subconnector, i,
677 drm_tv_subconnector_enum_list[i].type,
678 drm_tv_subconnector_enum_list[i].name);
679 dev->mode_config.tv_subconnector_property = tv_subconnector;
682 * Other, TV specific properties: margins & TV modes.
684 dev->mode_config.tv_left_margin_property =
685 drm_property_create(dev, DRM_MODE_PROP_RANGE,
687 dev->mode_config.tv_left_margin_property->values[0] = 0;
688 dev->mode_config.tv_left_margin_property->values[1] = 100;
690 dev->mode_config.tv_right_margin_property =
691 drm_property_create(dev, DRM_MODE_PROP_RANGE,
693 dev->mode_config.tv_right_margin_property->values[0] = 0;
694 dev->mode_config.tv_right_margin_property->values[1] = 100;
696 dev->mode_config.tv_top_margin_property =
697 drm_property_create(dev, DRM_MODE_PROP_RANGE,
699 dev->mode_config.tv_top_margin_property->values[0] = 0;
700 dev->mode_config.tv_top_margin_property->values[1] = 100;
702 dev->mode_config.tv_bottom_margin_property =
703 drm_property_create(dev, DRM_MODE_PROP_RANGE,
705 dev->mode_config.tv_bottom_margin_property->values[0] = 0;
706 dev->mode_config.tv_bottom_margin_property->values[1] = 100;
708 dev->mode_config.tv_mode_property =
709 drm_property_create(dev, DRM_MODE_PROP_ENUM,
711 for (i = 0; i < num_modes; i++)
712 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
717 EXPORT_SYMBOL(drm_mode_create_tv_properties);
720 * drm_mode_create_scaling_mode_property - create scaling mode property
723 * Called by a driver the first time it's needed, must be attached to desired
726 int drm_mode_create_scaling_mode_property(struct drm_device *dev)
728 struct drm_property *scaling_mode;
731 if (dev->mode_config.scaling_mode_property)
735 drm_property_create(dev, DRM_MODE_PROP_ENUM, "scaling mode",
736 ARRAY_SIZE(drm_scaling_mode_enum_list));
737 for (i = 0; i < ARRAY_SIZE(drm_scaling_mode_enum_list); i++)
738 drm_property_add_enum(scaling_mode, i,
739 drm_scaling_mode_enum_list[i].type,
740 drm_scaling_mode_enum_list[i].name);
742 dev->mode_config.scaling_mode_property = scaling_mode;
746 EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
749 * drm_mode_create_dithering_property - create dithering property
752 * Called by a driver the first time it's needed, must be attached to desired
755 int drm_mode_create_dithering_property(struct drm_device *dev)
757 struct drm_property *dithering_mode;
760 if (dev->mode_config.dithering_mode_property)
764 drm_property_create(dev, DRM_MODE_PROP_ENUM, "dithering",
765 ARRAY_SIZE(drm_dithering_mode_enum_list));
766 for (i = 0; i < ARRAY_SIZE(drm_dithering_mode_enum_list); i++)
767 drm_property_add_enum(dithering_mode, i,
768 drm_dithering_mode_enum_list[i].type,
769 drm_dithering_mode_enum_list[i].name);
770 dev->mode_config.dithering_mode_property = dithering_mode;
774 EXPORT_SYMBOL(drm_mode_create_dithering_property);
777 * drm_mode_config_init - initialize DRM mode_configuration structure
781 * None, should happen single threaded at init time.
783 * Initialize @dev's mode_config structure, used for tracking the graphics
784 * configuration of @dev.
786 void drm_mode_config_init(struct drm_device *dev)
788 mutex_init(&dev->mode_config.mutex);
789 INIT_LIST_HEAD(&dev->mode_config.fb_list);
790 INIT_LIST_HEAD(&dev->mode_config.fb_kernel_list);
791 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
792 INIT_LIST_HEAD(&dev->mode_config.connector_list);
793 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
794 INIT_LIST_HEAD(&dev->mode_config.property_list);
795 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
796 idr_init(&dev->mode_config.crtc_idr);
798 mutex_lock(&dev->mode_config.mutex);
799 drm_mode_create_standard_connector_properties(dev);
800 mutex_unlock(&dev->mode_config.mutex);
802 /* Just to be sure */
803 dev->mode_config.num_fb = 0;
804 dev->mode_config.num_connector = 0;
805 dev->mode_config.num_crtc = 0;
806 dev->mode_config.num_encoder = 0;
808 EXPORT_SYMBOL(drm_mode_config_init);
810 int drm_mode_group_init(struct drm_device *dev, struct drm_mode_group *group)
812 uint32_t total_objects = 0;
814 total_objects += dev->mode_config.num_crtc;
815 total_objects += dev->mode_config.num_connector;
816 total_objects += dev->mode_config.num_encoder;
818 if (total_objects == 0)
821 group->id_list = kzalloc(total_objects * sizeof(uint32_t), GFP_KERNEL);
825 group->num_crtcs = 0;
826 group->num_connectors = 0;
827 group->num_encoders = 0;
831 int drm_mode_group_init_legacy_group(struct drm_device *dev,
832 struct drm_mode_group *group)
834 struct drm_crtc *crtc;
835 struct drm_encoder *encoder;
836 struct drm_connector *connector;
839 if ((ret = drm_mode_group_init(dev, group)))
842 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
843 group->id_list[group->num_crtcs++] = crtc->base.id;
845 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
846 group->id_list[group->num_crtcs + group->num_encoders++] =
849 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
850 group->id_list[group->num_crtcs + group->num_encoders +
851 group->num_connectors++] = connector->base.id;
857 * drm_mode_config_cleanup - free up DRM mode_config info
861 * Caller must hold mode config lock.
863 * Free up all the connectors and CRTCs associated with this DRM device, then
864 * free up the framebuffers and associated buffer objects.
866 * FIXME: cleanup any dangling user buffer objects too
868 void drm_mode_config_cleanup(struct drm_device *dev)
870 struct drm_connector *connector, *ot;
871 struct drm_crtc *crtc, *ct;
872 struct drm_encoder *encoder, *enct;
873 struct drm_framebuffer *fb, *fbt;
874 struct drm_property *property, *pt;
876 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
878 encoder->funcs->destroy(encoder);
881 list_for_each_entry_safe(connector, ot,
882 &dev->mode_config.connector_list, head) {
883 connector->funcs->destroy(connector);
886 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
888 drm_property_destroy(dev, property);
891 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
892 fb->funcs->destroy(fb);
895 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
896 crtc->funcs->destroy(crtc);
900 EXPORT_SYMBOL(drm_mode_config_cleanup);
903 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
904 * @out: drm_mode_modeinfo struct to return to the user
905 * @in: drm_display_mode to use
910 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
913 void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out,
914 struct drm_display_mode *in)
916 out->clock = in->clock;
917 out->hdisplay = in->hdisplay;
918 out->hsync_start = in->hsync_start;
919 out->hsync_end = in->hsync_end;
920 out->htotal = in->htotal;
921 out->hskew = in->hskew;
922 out->vdisplay = in->vdisplay;
923 out->vsync_start = in->vsync_start;
924 out->vsync_end = in->vsync_end;
925 out->vtotal = in->vtotal;
926 out->vscan = in->vscan;
927 out->vrefresh = in->vrefresh;
928 out->flags = in->flags;
929 out->type = in->type;
930 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
931 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
935 * drm_crtc_convert_to_umode - convert a modeinfo into a drm_display_mode
936 * @out: drm_display_mode to return to the user
937 * @in: drm_mode_modeinfo to use
942 * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
945 void drm_crtc_convert_umode(struct drm_display_mode *out,
946 struct drm_mode_modeinfo *in)
948 out->clock = in->clock;
949 out->hdisplay = in->hdisplay;
950 out->hsync_start = in->hsync_start;
951 out->hsync_end = in->hsync_end;
952 out->htotal = in->htotal;
953 out->hskew = in->hskew;
954 out->vdisplay = in->vdisplay;
955 out->vsync_start = in->vsync_start;
956 out->vsync_end = in->vsync_end;
957 out->vtotal = in->vtotal;
958 out->vscan = in->vscan;
959 out->vrefresh = in->vrefresh;
960 out->flags = in->flags;
961 out->type = in->type;
962 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
963 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
967 * drm_mode_getresources - get graphics configuration
968 * @inode: inode from the ioctl
969 * @filp: file * from the ioctl
970 * @cmd: cmd from ioctl
971 * @arg: arg from ioctl
974 * Takes mode config lock.
976 * Construct a set of configuration description structures and return
977 * them to the user, including CRTC, connector and framebuffer configuration.
979 * Called by the user via ioctl.
982 * Zero on success, errno on failure.
984 int drm_mode_getresources(struct drm_device *dev, void *data,
985 struct drm_file *file_priv)
987 struct drm_mode_card_res *card_res = data;
988 struct list_head *lh;
989 struct drm_framebuffer *fb;
990 struct drm_connector *connector;
991 struct drm_crtc *crtc;
992 struct drm_encoder *encoder;
994 int connector_count = 0;
997 int encoder_count = 0;
999 uint32_t __user *fb_id;
1000 uint32_t __user *crtc_id;
1001 uint32_t __user *connector_id;
1002 uint32_t __user *encoder_id;
1003 struct drm_mode_group *mode_group;
1005 mutex_lock(&dev->mode_config.mutex);
1008 * For the non-control nodes we need to limit the list of resources
1009 * by IDs in the group list for this node
1011 list_for_each(lh, &file_priv->fbs)
1014 mode_group = &file_priv->master->minor->mode_group;
1015 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1017 list_for_each(lh, &dev->mode_config.crtc_list)
1020 list_for_each(lh, &dev->mode_config.connector_list)
1023 list_for_each(lh, &dev->mode_config.encoder_list)
1027 crtc_count = mode_group->num_crtcs;
1028 connector_count = mode_group->num_connectors;
1029 encoder_count = mode_group->num_encoders;
1032 card_res->max_height = dev->mode_config.max_height;
1033 card_res->min_height = dev->mode_config.min_height;
1034 card_res->max_width = dev->mode_config.max_width;
1035 card_res->min_width = dev->mode_config.min_width;
1037 /* handle this in 4 parts */
1039 if (card_res->count_fbs >= fb_count) {
1041 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
1042 list_for_each_entry(fb, &file_priv->fbs, head) {
1043 if (put_user(fb->base.id, fb_id + copied)) {
1050 card_res->count_fbs = fb_count;
1053 if (card_res->count_crtcs >= crtc_count) {
1055 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
1056 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1057 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
1059 DRM_DEBUG("CRTC ID is %d\n", crtc->base.id);
1060 if (put_user(crtc->base.id, crtc_id + copied)) {
1067 for (i = 0; i < mode_group->num_crtcs; i++) {
1068 if (put_user(mode_group->id_list[i],
1069 crtc_id + copied)) {
1077 card_res->count_crtcs = crtc_count;
1080 if (card_res->count_encoders >= encoder_count) {
1082 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
1083 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1084 list_for_each_entry(encoder,
1085 &dev->mode_config.encoder_list,
1087 DRM_DEBUG("ENCODER ID is %d\n",
1089 if (put_user(encoder->base.id, encoder_id +
1097 for (i = mode_group->num_crtcs; i < mode_group->num_crtcs + mode_group->num_encoders; i++) {
1098 if (put_user(mode_group->id_list[i],
1099 encoder_id + copied)) {
1108 card_res->count_encoders = encoder_count;
1111 if (card_res->count_connectors >= connector_count) {
1113 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
1114 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1115 list_for_each_entry(connector,
1116 &dev->mode_config.connector_list,
1118 DRM_DEBUG("CONNECTOR ID is %d\n",
1119 connector->base.id);
1120 if (put_user(connector->base.id,
1121 connector_id + copied)) {
1128 int start = mode_group->num_crtcs +
1129 mode_group->num_encoders;
1130 for (i = start; i < start + mode_group->num_connectors; i++) {
1131 if (put_user(mode_group->id_list[i],
1132 connector_id + copied)) {
1140 card_res->count_connectors = connector_count;
1142 DRM_DEBUG("Counted %d %d %d\n", card_res->count_crtcs,
1143 card_res->count_connectors, card_res->count_encoders);
1146 mutex_unlock(&dev->mode_config.mutex);
1151 * drm_mode_getcrtc - get CRTC configuration
1152 * @inode: inode from the ioctl
1153 * @filp: file * from the ioctl
1154 * @cmd: cmd from ioctl
1155 * @arg: arg from ioctl
1160 * Construct a CRTC configuration structure to return to the user.
1162 * Called by the user via ioctl.
1165 * Zero on success, errno on failure.
1167 int drm_mode_getcrtc(struct drm_device *dev,
1168 void *data, struct drm_file *file_priv)
1170 struct drm_mode_crtc *crtc_resp = data;
1171 struct drm_crtc *crtc;
1172 struct drm_mode_object *obj;
1175 mutex_lock(&dev->mode_config.mutex);
1177 obj = drm_mode_object_find(dev, crtc_resp->crtc_id,
1178 DRM_MODE_OBJECT_CRTC);
1183 crtc = obj_to_crtc(obj);
1185 crtc_resp->x = crtc->x;
1186 crtc_resp->y = crtc->y;
1187 crtc_resp->gamma_size = crtc->gamma_size;
1189 crtc_resp->fb_id = crtc->fb->base.id;
1191 crtc_resp->fb_id = 0;
1193 if (crtc->enabled) {
1195 drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
1196 crtc_resp->mode_valid = 1;
1199 crtc_resp->mode_valid = 0;
1203 mutex_unlock(&dev->mode_config.mutex);
1208 * drm_mode_getconnector - get connector configuration
1209 * @inode: inode from the ioctl
1210 * @filp: file * from the ioctl
1211 * @cmd: cmd from ioctl
1212 * @arg: arg from ioctl
1217 * Construct a connector configuration structure to return to the user.
1219 * Called by the user via ioctl.
1222 * Zero on success, errno on failure.
1224 int drm_mode_getconnector(struct drm_device *dev, void *data,
1225 struct drm_file *file_priv)
1227 struct drm_mode_get_connector *out_resp = data;
1228 struct drm_mode_object *obj;
1229 struct drm_connector *connector;
1230 struct drm_display_mode *mode;
1232 int props_count = 0;
1233 int encoders_count = 0;
1237 struct drm_mode_modeinfo u_mode;
1238 struct drm_mode_modeinfo __user *mode_ptr;
1239 uint32_t __user *prop_ptr;
1240 uint64_t __user *prop_values;
1241 uint32_t __user *encoder_ptr;
1243 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1245 DRM_DEBUG("connector id %d:\n", out_resp->connector_id);
1247 mutex_lock(&dev->mode_config.mutex);
1249 obj = drm_mode_object_find(dev, out_resp->connector_id,
1250 DRM_MODE_OBJECT_CONNECTOR);
1255 connector = obj_to_connector(obj);
1257 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
1258 if (connector->property_ids[i] != 0) {
1263 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1264 if (connector->encoder_ids[i] != 0) {
1269 if (out_resp->count_modes == 0) {
1270 connector->funcs->fill_modes(connector,
1271 dev->mode_config.max_width,
1272 dev->mode_config.max_height);
1275 /* delayed so we get modes regardless of pre-fill_modes state */
1276 list_for_each_entry(mode, &connector->modes, head)
1279 out_resp->connector_id = connector->base.id;
1280 out_resp->connector_type = connector->connector_type;
1281 out_resp->connector_type_id = connector->connector_type_id;
1282 out_resp->mm_width = connector->display_info.width_mm;
1283 out_resp->mm_height = connector->display_info.height_mm;
1284 out_resp->subpixel = connector->display_info.subpixel_order;
1285 out_resp->connection = connector->status;
1286 if (connector->encoder)
1287 out_resp->encoder_id = connector->encoder->base.id;
1289 out_resp->encoder_id = 0;
1292 * This ioctl is called twice, once to determine how much space is
1293 * needed, and the 2nd time to fill it.
1295 if ((out_resp->count_modes >= mode_count) && mode_count) {
1297 mode_ptr = (struct drm_mode_modeinfo *)(unsigned long)out_resp->modes_ptr;
1298 list_for_each_entry(mode, &connector->modes, head) {
1299 drm_crtc_convert_to_umode(&u_mode, mode);
1300 if (copy_to_user(mode_ptr + copied,
1301 &u_mode, sizeof(u_mode))) {
1308 out_resp->count_modes = mode_count;
1310 if ((out_resp->count_props >= props_count) && props_count) {
1312 prop_ptr = (uint32_t *)(unsigned long)(out_resp->props_ptr);
1313 prop_values = (uint64_t *)(unsigned long)(out_resp->prop_values_ptr);
1314 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
1315 if (connector->property_ids[i] != 0) {
1316 if (put_user(connector->property_ids[i],
1317 prop_ptr + copied)) {
1322 if (put_user(connector->property_values[i],
1323 prop_values + copied)) {
1331 out_resp->count_props = props_count;
1333 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
1335 encoder_ptr = (uint32_t *)(unsigned long)(out_resp->encoders_ptr);
1336 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1337 if (connector->encoder_ids[i] != 0) {
1338 if (put_user(connector->encoder_ids[i],
1339 encoder_ptr + copied)) {
1347 out_resp->count_encoders = encoders_count;
1350 mutex_unlock(&dev->mode_config.mutex);
1354 int drm_mode_getencoder(struct drm_device *dev, void *data,
1355 struct drm_file *file_priv)
1357 struct drm_mode_get_encoder *enc_resp = data;
1358 struct drm_mode_object *obj;
1359 struct drm_encoder *encoder;
1362 mutex_lock(&dev->mode_config.mutex);
1363 obj = drm_mode_object_find(dev, enc_resp->encoder_id,
1364 DRM_MODE_OBJECT_ENCODER);
1369 encoder = obj_to_encoder(obj);
1372 enc_resp->crtc_id = encoder->crtc->base.id;
1374 enc_resp->crtc_id = 0;
1375 enc_resp->encoder_type = encoder->encoder_type;
1376 enc_resp->encoder_id = encoder->base.id;
1377 enc_resp->possible_crtcs = encoder->possible_crtcs;
1378 enc_resp->possible_clones = encoder->possible_clones;
1381 mutex_unlock(&dev->mode_config.mutex);
1386 * drm_mode_setcrtc - set CRTC configuration
1387 * @inode: inode from the ioctl
1388 * @filp: file * from the ioctl
1389 * @cmd: cmd from ioctl
1390 * @arg: arg from ioctl
1395 * Build a new CRTC configuration based on user request.
1397 * Called by the user via ioctl.
1400 * Zero on success, errno on failure.
1402 int drm_mode_setcrtc(struct drm_device *dev, void *data,
1403 struct drm_file *file_priv)
1405 struct drm_mode_config *config = &dev->mode_config;
1406 struct drm_mode_crtc *crtc_req = data;
1407 struct drm_mode_object *obj;
1408 struct drm_crtc *crtc, *crtcfb;
1409 struct drm_connector **connector_set = NULL, *connector;
1410 struct drm_framebuffer *fb = NULL;
1411 struct drm_display_mode *mode = NULL;
1412 struct drm_mode_set set;
1413 uint32_t __user *set_connectors_ptr;
1417 mutex_lock(&dev->mode_config.mutex);
1418 obj = drm_mode_object_find(dev, crtc_req->crtc_id,
1419 DRM_MODE_OBJECT_CRTC);
1421 DRM_DEBUG("Unknown CRTC ID %d\n", crtc_req->crtc_id);
1425 crtc = obj_to_crtc(obj);
1427 if (crtc_req->mode_valid) {
1428 /* If we have a mode we need a framebuffer. */
1429 /* If we pass -1, set the mode with the currently bound fb */
1430 if (crtc_req->fb_id == -1) {
1431 list_for_each_entry(crtcfb,
1432 &dev->mode_config.crtc_list, head) {
1433 if (crtcfb == crtc) {
1434 DRM_DEBUG("Using current fb for setmode\n");
1439 obj = drm_mode_object_find(dev, crtc_req->fb_id,
1440 DRM_MODE_OBJECT_FB);
1442 DRM_DEBUG("Unknown FB ID%d\n", crtc_req->fb_id);
1446 fb = obj_to_fb(obj);
1449 mode = drm_mode_create(dev);
1450 drm_crtc_convert_umode(mode, &crtc_req->mode);
1451 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
1454 if (crtc_req->count_connectors == 0 && mode) {
1455 DRM_DEBUG("Count connectors is 0 but mode set\n");
1460 if (crtc_req->count_connectors > 0 && !mode && !fb) {
1461 DRM_DEBUG("Count connectors is %d but no mode or fb set\n",
1462 crtc_req->count_connectors);
1467 if (crtc_req->count_connectors > 0) {
1470 /* Avoid unbounded kernel memory allocation */
1471 if (crtc_req->count_connectors > config->num_connector) {
1476 connector_set = kmalloc(crtc_req->count_connectors *
1477 sizeof(struct drm_connector *),
1479 if (!connector_set) {
1484 for (i = 0; i < crtc_req->count_connectors; i++) {
1485 set_connectors_ptr = (uint32_t *)(unsigned long)crtc_req->set_connectors_ptr;
1486 if (get_user(out_id, &set_connectors_ptr[i])) {
1491 obj = drm_mode_object_find(dev, out_id,
1492 DRM_MODE_OBJECT_CONNECTOR);
1494 DRM_DEBUG("Connector id %d unknown\n", out_id);
1498 connector = obj_to_connector(obj);
1500 connector_set[i] = connector;
1505 set.x = crtc_req->x;
1506 set.y = crtc_req->y;
1508 set.connectors = connector_set;
1509 set.num_connectors = crtc_req->count_connectors;
1511 ret = crtc->funcs->set_config(&set);
1514 kfree(connector_set);
1515 mutex_unlock(&dev->mode_config.mutex);
1519 int drm_mode_cursor_ioctl(struct drm_device *dev,
1520 void *data, struct drm_file *file_priv)
1522 struct drm_mode_cursor *req = data;
1523 struct drm_mode_object *obj;
1524 struct drm_crtc *crtc;
1530 DRM_ERROR("no operation set\n");
1534 mutex_lock(&dev->mode_config.mutex);
1535 obj = drm_mode_object_find(dev, req->crtc_id, DRM_MODE_OBJECT_CRTC);
1537 DRM_DEBUG("Unknown CRTC ID %d\n", req->crtc_id);
1541 crtc = obj_to_crtc(obj);
1543 if (req->flags & DRM_MODE_CURSOR_BO) {
1544 if (!crtc->funcs->cursor_set) {
1545 DRM_ERROR("crtc does not support cursor\n");
1549 /* Turns off the cursor if handle is 0 */
1550 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
1551 req->width, req->height);
1554 if (req->flags & DRM_MODE_CURSOR_MOVE) {
1555 if (crtc->funcs->cursor_move) {
1556 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
1558 DRM_ERROR("crtc does not support cursor\n");
1564 mutex_unlock(&dev->mode_config.mutex);
1569 * drm_mode_addfb - add an FB to the graphics configuration
1570 * @inode: inode from the ioctl
1571 * @filp: file * from the ioctl
1572 * @cmd: cmd from ioctl
1573 * @arg: arg from ioctl
1576 * Takes mode config lock.
1578 * Add a new FB to the specified CRTC, given a user request.
1580 * Called by the user via ioctl.
1583 * Zero on success, errno on failure.
1585 int drm_mode_addfb(struct drm_device *dev,
1586 void *data, struct drm_file *file_priv)
1588 struct drm_mode_fb_cmd *r = data;
1589 struct drm_mode_config *config = &dev->mode_config;
1590 struct drm_framebuffer *fb;
1593 if ((config->min_width > r->width) || (r->width > config->max_width)) {
1594 DRM_ERROR("mode new framebuffer width not within limits\n");
1597 if ((config->min_height > r->height) || (r->height > config->max_height)) {
1598 DRM_ERROR("mode new framebuffer height not within limits\n");
1602 mutex_lock(&dev->mode_config.mutex);
1604 /* TODO check buffer is sufficently large */
1605 /* TODO setup destructor callback */
1607 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
1609 DRM_ERROR("could not create framebuffer\n");
1614 r->fb_id = fb->base.id;
1615 list_add(&fb->filp_head, &file_priv->fbs);
1618 mutex_unlock(&dev->mode_config.mutex);
1623 * drm_mode_rmfb - remove an FB from the configuration
1624 * @inode: inode from the ioctl
1625 * @filp: file * from the ioctl
1626 * @cmd: cmd from ioctl
1627 * @arg: arg from ioctl
1630 * Takes mode config lock.
1632 * Remove the FB specified by the user.
1634 * Called by the user via ioctl.
1637 * Zero on success, errno on failure.
1639 int drm_mode_rmfb(struct drm_device *dev,
1640 void *data, struct drm_file *file_priv)
1642 struct drm_mode_object *obj;
1643 struct drm_framebuffer *fb = NULL;
1644 struct drm_framebuffer *fbl = NULL;
1645 uint32_t *id = data;
1649 mutex_lock(&dev->mode_config.mutex);
1650 obj = drm_mode_object_find(dev, *id, DRM_MODE_OBJECT_FB);
1651 /* TODO check that we realy get a framebuffer back. */
1653 DRM_ERROR("mode invalid framebuffer id\n");
1657 fb = obj_to_fb(obj);
1659 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
1664 DRM_ERROR("tried to remove a fb that we didn't own\n");
1669 /* TODO release all crtc connected to the framebuffer */
1670 /* TODO unhock the destructor from the buffer object */
1672 list_del(&fb->filp_head);
1673 fb->funcs->destroy(fb);
1676 mutex_unlock(&dev->mode_config.mutex);
1681 * drm_mode_getfb - get FB info
1682 * @inode: inode from the ioctl
1683 * @filp: file * from the ioctl
1684 * @cmd: cmd from ioctl
1685 * @arg: arg from ioctl
1690 * Lookup the FB given its ID and return info about it.
1692 * Called by the user via ioctl.
1695 * Zero on success, errno on failure.
1697 int drm_mode_getfb(struct drm_device *dev,
1698 void *data, struct drm_file *file_priv)
1700 struct drm_mode_fb_cmd *r = data;
1701 struct drm_mode_object *obj;
1702 struct drm_framebuffer *fb;
1705 mutex_lock(&dev->mode_config.mutex);
1706 obj = drm_mode_object_find(dev, r->fb_id, DRM_MODE_OBJECT_FB);
1708 DRM_ERROR("invalid framebuffer id\n");
1712 fb = obj_to_fb(obj);
1714 r->height = fb->height;
1715 r->width = fb->width;
1716 r->depth = fb->depth;
1717 r->bpp = fb->bits_per_pixel;
1718 r->pitch = fb->pitch;
1719 fb->funcs->create_handle(fb, file_priv, &r->handle);
1722 mutex_unlock(&dev->mode_config.mutex);
1727 * drm_fb_release - remove and free the FBs on this file
1728 * @filp: file * from the ioctl
1731 * Takes mode config lock.
1733 * Destroy all the FBs associated with @filp.
1735 * Called by the user via ioctl.
1738 * Zero on success, errno on failure.
1740 void drm_fb_release(struct file *filp)
1742 struct drm_file *priv = filp->private_data;
1743 struct drm_device *dev = priv->minor->dev;
1744 struct drm_framebuffer *fb, *tfb;
1746 mutex_lock(&dev->mode_config.mutex);
1747 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
1748 list_del(&fb->filp_head);
1749 fb->funcs->destroy(fb);
1751 mutex_unlock(&dev->mode_config.mutex);
1755 * drm_mode_attachmode - add a mode to the user mode list
1757 * @connector: connector to add the mode to
1758 * @mode: mode to add
1760 * Add @mode to @connector's user mode list.
1762 static int drm_mode_attachmode(struct drm_device *dev,
1763 struct drm_connector *connector,
1764 struct drm_display_mode *mode)
1768 list_add_tail(&mode->head, &connector->user_modes);
1772 int drm_mode_attachmode_crtc(struct drm_device *dev, struct drm_crtc *crtc,
1773 struct drm_display_mode *mode)
1775 struct drm_connector *connector;
1777 struct drm_display_mode *dup_mode;
1779 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1780 if (!connector->encoder)
1782 if (connector->encoder->crtc == crtc) {
1784 dup_mode = drm_mode_duplicate(dev, mode);
1787 ret = drm_mode_attachmode(dev, connector, dup_mode);
1795 EXPORT_SYMBOL(drm_mode_attachmode_crtc);
1797 static int drm_mode_detachmode(struct drm_device *dev,
1798 struct drm_connector *connector,
1799 struct drm_display_mode *mode)
1803 struct drm_display_mode *match_mode, *t;
1805 list_for_each_entry_safe(match_mode, t, &connector->user_modes, head) {
1806 if (drm_mode_equal(match_mode, mode)) {
1807 list_del(&match_mode->head);
1808 drm_mode_destroy(dev, match_mode);
1820 int drm_mode_detachmode_crtc(struct drm_device *dev, struct drm_display_mode *mode)
1822 struct drm_connector *connector;
1824 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1825 drm_mode_detachmode(dev, connector, mode);
1829 EXPORT_SYMBOL(drm_mode_detachmode_crtc);
1832 * drm_fb_attachmode - Attach a user mode to an connector
1833 * @inode: inode from the ioctl
1834 * @filp: file * from the ioctl
1835 * @cmd: cmd from ioctl
1836 * @arg: arg from ioctl
1838 * This attaches a user specified mode to an connector.
1839 * Called by the user via ioctl.
1842 * Zero on success, errno on failure.
1844 int drm_mode_attachmode_ioctl(struct drm_device *dev,
1845 void *data, struct drm_file *file_priv)
1847 struct drm_mode_mode_cmd *mode_cmd = data;
1848 struct drm_connector *connector;
1849 struct drm_display_mode *mode;
1850 struct drm_mode_object *obj;
1851 struct drm_mode_modeinfo *umode = &mode_cmd->mode;
1854 mutex_lock(&dev->mode_config.mutex);
1856 obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
1861 connector = obj_to_connector(obj);
1863 mode = drm_mode_create(dev);
1869 drm_crtc_convert_umode(mode, umode);
1871 ret = drm_mode_attachmode(dev, connector, mode);
1873 mutex_unlock(&dev->mode_config.mutex);
1879 * drm_fb_detachmode - Detach a user specified mode from an connector
1880 * @inode: inode from the ioctl
1881 * @filp: file * from the ioctl
1882 * @cmd: cmd from ioctl
1883 * @arg: arg from ioctl
1885 * Called by the user via ioctl.
1888 * Zero on success, errno on failure.
1890 int drm_mode_detachmode_ioctl(struct drm_device *dev,
1891 void *data, struct drm_file *file_priv)
1893 struct drm_mode_object *obj;
1894 struct drm_mode_mode_cmd *mode_cmd = data;
1895 struct drm_connector *connector;
1896 struct drm_display_mode mode;
1897 struct drm_mode_modeinfo *umode = &mode_cmd->mode;
1900 mutex_lock(&dev->mode_config.mutex);
1902 obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
1907 connector = obj_to_connector(obj);
1909 drm_crtc_convert_umode(&mode, umode);
1910 ret = drm_mode_detachmode(dev, connector, &mode);
1912 mutex_unlock(&dev->mode_config.mutex);
1916 struct drm_property *drm_property_create(struct drm_device *dev, int flags,
1917 const char *name, int num_values)
1919 struct drm_property *property = NULL;
1921 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
1926 property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
1927 if (!property->values)
1931 drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
1932 property->flags = flags;
1933 property->num_values = num_values;
1934 INIT_LIST_HEAD(&property->enum_blob_list);
1937 strncpy(property->name, name, DRM_PROP_NAME_LEN);
1939 list_add_tail(&property->head, &dev->mode_config.property_list);
1945 EXPORT_SYMBOL(drm_property_create);
1947 int drm_property_add_enum(struct drm_property *property, int index,
1948 uint64_t value, const char *name)
1950 struct drm_property_enum *prop_enum;
1952 if (!(property->flags & DRM_MODE_PROP_ENUM))
1955 if (!list_empty(&property->enum_blob_list)) {
1956 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
1957 if (prop_enum->value == value) {
1958 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
1959 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
1965 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
1969 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
1970 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
1971 prop_enum->value = value;
1973 property->values[index] = value;
1974 list_add_tail(&prop_enum->head, &property->enum_blob_list);
1977 EXPORT_SYMBOL(drm_property_add_enum);
1979 void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
1981 struct drm_property_enum *prop_enum, *pt;
1983 list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
1984 list_del(&prop_enum->head);
1988 if (property->num_values)
1989 kfree(property->values);
1990 drm_mode_object_put(dev, &property->base);
1991 list_del(&property->head);
1994 EXPORT_SYMBOL(drm_property_destroy);
1996 int drm_connector_attach_property(struct drm_connector *connector,
1997 struct drm_property *property, uint64_t init_val)
2001 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
2002 if (connector->property_ids[i] == 0) {
2003 connector->property_ids[i] = property->base.id;
2004 connector->property_values[i] = init_val;
2009 if (i == DRM_CONNECTOR_MAX_PROPERTY)
2013 EXPORT_SYMBOL(drm_connector_attach_property);
2015 int drm_connector_property_set_value(struct drm_connector *connector,
2016 struct drm_property *property, uint64_t value)
2020 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
2021 if (connector->property_ids[i] == property->base.id) {
2022 connector->property_values[i] = value;
2027 if (i == DRM_CONNECTOR_MAX_PROPERTY)
2031 EXPORT_SYMBOL(drm_connector_property_set_value);
2033 int drm_connector_property_get_value(struct drm_connector *connector,
2034 struct drm_property *property, uint64_t *val)
2038 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
2039 if (connector->property_ids[i] == property->base.id) {
2040 *val = connector->property_values[i];
2045 if (i == DRM_CONNECTOR_MAX_PROPERTY)
2049 EXPORT_SYMBOL(drm_connector_property_get_value);
2051 int drm_mode_getproperty_ioctl(struct drm_device *dev,
2052 void *data, struct drm_file *file_priv)
2054 struct drm_mode_object *obj;
2055 struct drm_mode_get_property *out_resp = data;
2056 struct drm_property *property;
2059 int value_count = 0;
2062 struct drm_property_enum *prop_enum;
2063 struct drm_mode_property_enum __user *enum_ptr;
2064 struct drm_property_blob *prop_blob;
2065 uint32_t *blob_id_ptr;
2066 uint64_t __user *values_ptr;
2067 uint32_t __user *blob_length_ptr;
2069 mutex_lock(&dev->mode_config.mutex);
2070 obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
2075 property = obj_to_property(obj);
2077 if (property->flags & DRM_MODE_PROP_ENUM) {
2078 list_for_each_entry(prop_enum, &property->enum_blob_list, head)
2080 } else if (property->flags & DRM_MODE_PROP_BLOB) {
2081 list_for_each_entry(prop_blob, &property->enum_blob_list, head)
2085 value_count = property->num_values;
2087 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
2088 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
2089 out_resp->flags = property->flags;
2091 if ((out_resp->count_values >= value_count) && value_count) {
2092 values_ptr = (uint64_t *)(unsigned long)out_resp->values_ptr;
2093 for (i = 0; i < value_count; i++) {
2094 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
2100 out_resp->count_values = value_count;
2102 if (property->flags & DRM_MODE_PROP_ENUM) {
2103 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
2105 enum_ptr = (struct drm_mode_property_enum *)(unsigned long)out_resp->enum_blob_ptr;
2106 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
2108 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
2113 if (copy_to_user(&enum_ptr[copied].name,
2114 &prop_enum->name, DRM_PROP_NAME_LEN)) {
2121 out_resp->count_enum_blobs = enum_count;
2124 if (property->flags & DRM_MODE_PROP_BLOB) {
2125 if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
2127 blob_id_ptr = (uint32_t *)(unsigned long)out_resp->enum_blob_ptr;
2128 blob_length_ptr = (uint32_t *)(unsigned long)out_resp->values_ptr;
2130 list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
2131 if (put_user(prop_blob->base.id, blob_id_ptr + copied)) {
2136 if (put_user(prop_blob->length, blob_length_ptr + copied)) {
2144 out_resp->count_enum_blobs = blob_count;
2147 mutex_unlock(&dev->mode_config.mutex);
2151 static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
2154 struct drm_property_blob *blob;
2156 if (!length || !data)
2159 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
2163 blob->data = (void *)((char *)blob + sizeof(struct drm_property_blob));
2164 blob->length = length;
2166 memcpy(blob->data, data, length);
2168 drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
2170 list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
2174 static void drm_property_destroy_blob(struct drm_device *dev,
2175 struct drm_property_blob *blob)
2177 drm_mode_object_put(dev, &blob->base);
2178 list_del(&blob->head);
2182 int drm_mode_getblob_ioctl(struct drm_device *dev,
2183 void *data, struct drm_file *file_priv)
2185 struct drm_mode_object *obj;
2186 struct drm_mode_get_blob *out_resp = data;
2187 struct drm_property_blob *blob;
2191 mutex_lock(&dev->mode_config.mutex);
2192 obj = drm_mode_object_find(dev, out_resp->blob_id, DRM_MODE_OBJECT_BLOB);
2197 blob = obj_to_blob(obj);
2199 if (out_resp->length == blob->length) {
2200 blob_ptr = (void *)(unsigned long)out_resp->data;
2201 if (copy_to_user(blob_ptr, blob->data, blob->length)){
2206 out_resp->length = blob->length;
2209 mutex_unlock(&dev->mode_config.mutex);
2213 int drm_mode_connector_update_edid_property(struct drm_connector *connector,
2216 struct drm_device *dev = connector->dev;
2219 if (connector->edid_blob_ptr)
2220 drm_property_destroy_blob(dev, connector->edid_blob_ptr);
2222 /* Delete edid, when there is none. */
2224 connector->edid_blob_ptr = NULL;
2225 ret = drm_connector_property_set_value(connector, dev->mode_config.edid_property, 0);
2229 connector->edid_blob_ptr = drm_property_create_blob(connector->dev, 128, edid);
2231 ret = drm_connector_property_set_value(connector,
2232 dev->mode_config.edid_property,
2233 connector->edid_blob_ptr->base.id);
2237 EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
2239 int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
2240 void *data, struct drm_file *file_priv)
2242 struct drm_mode_connector_set_property *out_resp = data;
2243 struct drm_mode_object *obj;
2244 struct drm_property *property;
2245 struct drm_connector *connector;
2249 mutex_lock(&dev->mode_config.mutex);
2251 obj = drm_mode_object_find(dev, out_resp->connector_id, DRM_MODE_OBJECT_CONNECTOR);
2255 connector = obj_to_connector(obj);
2257 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
2258 if (connector->property_ids[i] == out_resp->prop_id)
2262 if (i == DRM_CONNECTOR_MAX_PROPERTY) {
2266 obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
2270 property = obj_to_property(obj);
2272 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
2275 if (property->flags & DRM_MODE_PROP_RANGE) {
2276 if (out_resp->value < property->values[0])
2279 if (out_resp->value > property->values[1])
2283 for (i = 0; i < property->num_values; i++) {
2284 if (property->values[i] == out_resp->value) {
2294 if (connector->funcs->set_property)
2295 ret = connector->funcs->set_property(connector, property, out_resp->value);
2297 /* store the property value if succesful */
2299 drm_connector_property_set_value(connector, property, out_resp->value);
2301 mutex_unlock(&dev->mode_config.mutex);
2305 int drm_mode_connector_attach_encoder(struct drm_connector *connector,
2306 struct drm_encoder *encoder)
2310 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
2311 if (connector->encoder_ids[i] == 0) {
2312 connector->encoder_ids[i] = encoder->base.id;
2318 EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
2320 void drm_mode_connector_detach_encoder(struct drm_connector *connector,
2321 struct drm_encoder *encoder)
2324 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
2325 if (connector->encoder_ids[i] == encoder->base.id) {
2326 connector->encoder_ids[i] = 0;
2327 if (connector->encoder == encoder)
2328 connector->encoder = NULL;
2333 EXPORT_SYMBOL(drm_mode_connector_detach_encoder);
2335 bool drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
2338 crtc->gamma_size = gamma_size;
2340 crtc->gamma_store = kzalloc(gamma_size * sizeof(uint16_t) * 3, GFP_KERNEL);
2341 if (!crtc->gamma_store) {
2342 crtc->gamma_size = 0;
2348 EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
2350 int drm_mode_gamma_set_ioctl(struct drm_device *dev,
2351 void *data, struct drm_file *file_priv)
2353 struct drm_mode_crtc_lut *crtc_lut = data;
2354 struct drm_mode_object *obj;
2355 struct drm_crtc *crtc;
2356 void *r_base, *g_base, *b_base;
2360 mutex_lock(&dev->mode_config.mutex);
2361 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
2366 crtc = obj_to_crtc(obj);
2368 /* memcpy into gamma store */
2369 if (crtc_lut->gamma_size != crtc->gamma_size) {
2374 size = crtc_lut->gamma_size * (sizeof(uint16_t));
2375 r_base = crtc->gamma_store;
2376 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
2381 g_base = r_base + size;
2382 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
2387 b_base = g_base + size;
2388 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
2393 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, crtc->gamma_size);
2396 mutex_unlock(&dev->mode_config.mutex);
2401 int drm_mode_gamma_get_ioctl(struct drm_device *dev,
2402 void *data, struct drm_file *file_priv)
2404 struct drm_mode_crtc_lut *crtc_lut = data;
2405 struct drm_mode_object *obj;
2406 struct drm_crtc *crtc;
2407 void *r_base, *g_base, *b_base;
2411 mutex_lock(&dev->mode_config.mutex);
2412 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
2417 crtc = obj_to_crtc(obj);
2419 /* memcpy into gamma store */
2420 if (crtc_lut->gamma_size != crtc->gamma_size) {
2425 size = crtc_lut->gamma_size * (sizeof(uint16_t));
2426 r_base = crtc->gamma_store;
2427 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
2432 g_base = r_base + size;
2433 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
2438 b_base = g_base + size;
2439 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
2444 mutex_unlock(&dev->mode_config.mutex);