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>
33 #include <linux/slab.h>
34 #include <linux/export.h>
40 struct drm_prop_enum_list {
45 /* Avoid boilerplate. I'm tired of typing. */
46 #define DRM_ENUM_NAME_FN(fnname, list) \
47 char *fnname(int val) \
50 for (i = 0; i < ARRAY_SIZE(list); i++) { \
51 if (list[i].type == val) \
52 return list[i].name; \
60 static struct drm_prop_enum_list drm_dpms_enum_list[] =
61 { { DRM_MODE_DPMS_ON, "On" },
62 { DRM_MODE_DPMS_STANDBY, "Standby" },
63 { DRM_MODE_DPMS_SUSPEND, "Suspend" },
64 { DRM_MODE_DPMS_OFF, "Off" }
67 DRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list)
72 static struct drm_prop_enum_list drm_scaling_mode_enum_list[] =
74 { DRM_MODE_SCALE_NONE, "None" },
75 { DRM_MODE_SCALE_FULLSCREEN, "Full" },
76 { DRM_MODE_SCALE_CENTER, "Center" },
77 { DRM_MODE_SCALE_ASPECT, "Full aspect" },
80 static struct drm_prop_enum_list drm_dithering_mode_enum_list[] =
82 { DRM_MODE_DITHERING_OFF, "Off" },
83 { DRM_MODE_DITHERING_ON, "On" },
84 { DRM_MODE_DITHERING_AUTO, "Automatic" },
88 * Non-global properties, but "required" for certain connectors.
90 static struct drm_prop_enum_list drm_dvi_i_select_enum_list[] =
92 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
93 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
94 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
97 DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list)
99 static struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list[] =
101 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
102 { DRM_MODE_SUBCONNECTOR_DVID, "DVI-D" }, /* DVI-I */
103 { DRM_MODE_SUBCONNECTOR_DVIA, "DVI-A" }, /* DVI-I */
106 DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name,
107 drm_dvi_i_subconnector_enum_list)
109 static struct drm_prop_enum_list drm_tv_select_enum_list[] =
111 { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
112 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
113 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
114 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
115 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
118 DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
120 static struct drm_prop_enum_list drm_tv_subconnector_enum_list[] =
122 { DRM_MODE_SUBCONNECTOR_Unknown, "Unknown" }, /* DVI-I and TV-out */
123 { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
124 { DRM_MODE_SUBCONNECTOR_SVIDEO, "SVIDEO" }, /* TV-out */
125 { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
126 { DRM_MODE_SUBCONNECTOR_SCART, "SCART" }, /* TV-out */
129 DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,
130 drm_tv_subconnector_enum_list)
132 static struct drm_prop_enum_list drm_dirty_info_enum_list[] = {
133 { DRM_MODE_DIRTY_OFF, "Off" },
134 { DRM_MODE_DIRTY_ON, "On" },
135 { DRM_MODE_DIRTY_ANNOTATE, "Annotate" },
138 DRM_ENUM_NAME_FN(drm_get_dirty_info_name,
139 drm_dirty_info_enum_list)
141 struct drm_conn_prop_enum_list {
148 * Connector and encoder types.
150 static struct drm_conn_prop_enum_list drm_connector_enum_list[] =
151 { { DRM_MODE_CONNECTOR_Unknown, "Unknown", 0 },
152 { DRM_MODE_CONNECTOR_VGA, "VGA", 0 },
153 { DRM_MODE_CONNECTOR_DVII, "DVI-I", 0 },
154 { DRM_MODE_CONNECTOR_DVID, "DVI-D", 0 },
155 { DRM_MODE_CONNECTOR_DVIA, "DVI-A", 0 },
156 { DRM_MODE_CONNECTOR_Composite, "Composite", 0 },
157 { DRM_MODE_CONNECTOR_SVIDEO, "SVIDEO", 0 },
158 { DRM_MODE_CONNECTOR_LVDS, "LVDS", 0 },
159 { DRM_MODE_CONNECTOR_Component, "Component", 0 },
160 { DRM_MODE_CONNECTOR_9PinDIN, "DIN", 0 },
161 { DRM_MODE_CONNECTOR_DisplayPort, "DP", 0 },
162 { DRM_MODE_CONNECTOR_HDMIA, "HDMI-A", 0 },
163 { DRM_MODE_CONNECTOR_HDMIB, "HDMI-B", 0 },
164 { DRM_MODE_CONNECTOR_TV, "TV", 0 },
165 { DRM_MODE_CONNECTOR_eDP, "eDP", 0 },
166 { DRM_MODE_CONNECTOR_VIRTUAL, "Virtual", 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" },
175 { DRM_MODE_ENCODER_VIRTUAL, "Virtual" },
178 char *drm_get_encoder_name(struct drm_encoder *encoder)
182 snprintf(buf, 32, "%s-%d",
183 drm_encoder_enum_list[encoder->encoder_type].name,
187 EXPORT_SYMBOL(drm_get_encoder_name);
189 char *drm_get_connector_name(struct drm_connector *connector)
193 snprintf(buf, 32, "%s-%d",
194 drm_connector_enum_list[connector->connector_type].name,
195 connector->connector_type_id);
198 EXPORT_SYMBOL(drm_get_connector_name);
200 char *drm_get_connector_status_name(enum drm_connector_status status)
202 if (status == connector_status_connected)
204 else if (status == connector_status_disconnected)
205 return "disconnected";
211 * drm_mode_object_get - allocate a new identifier
213 * @ptr: object pointer, used to generate unique ID
218 * Create a unique identifier based on @ptr in @dev's identifier space. Used
219 * for tracking modes, CRTCs and connectors.
222 * New unique (relative to other objects in @dev) integer identifier for the
225 static int drm_mode_object_get(struct drm_device *dev,
226 struct drm_mode_object *obj, uint32_t obj_type)
232 if (idr_pre_get(&dev->mode_config.crtc_idr, GFP_KERNEL) == 0) {
233 DRM_ERROR("Ran out memory getting a mode number\n");
237 mutex_lock(&dev->mode_config.idr_mutex);
238 ret = idr_get_new_above(&dev->mode_config.crtc_idr, obj, 1, &new_id);
239 mutex_unlock(&dev->mode_config.idr_mutex);
244 obj->type = obj_type;
249 * drm_mode_object_put - free an identifer
254 * Caller must hold DRM mode_config lock.
256 * Free @id from @dev's unique identifier pool.
258 static void drm_mode_object_put(struct drm_device *dev,
259 struct drm_mode_object *object)
261 mutex_lock(&dev->mode_config.idr_mutex);
262 idr_remove(&dev->mode_config.crtc_idr, object->id);
263 mutex_unlock(&dev->mode_config.idr_mutex);
266 struct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
267 uint32_t id, uint32_t type)
269 struct drm_mode_object *obj = NULL;
271 mutex_lock(&dev->mode_config.idr_mutex);
272 obj = idr_find(&dev->mode_config.crtc_idr, id);
273 if (!obj || (obj->type != type) || (obj->id != id))
275 mutex_unlock(&dev->mode_config.idr_mutex);
279 EXPORT_SYMBOL(drm_mode_object_find);
282 * drm_framebuffer_init - initialize a framebuffer
286 * Caller must hold mode config lock.
288 * Allocates an ID for the framebuffer's parent mode object, sets its mode
289 * functions & device file and adds it to the master fd list.
292 * Zero on success, error code on failure.
294 int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
295 const struct drm_framebuffer_funcs *funcs)
299 ret = drm_mode_object_get(dev, &fb->base, DRM_MODE_OBJECT_FB);
306 dev->mode_config.num_fb++;
307 list_add(&fb->head, &dev->mode_config.fb_list);
311 EXPORT_SYMBOL(drm_framebuffer_init);
314 * drm_framebuffer_cleanup - remove a framebuffer object
315 * @fb: framebuffer to remove
318 * Caller must hold mode config lock.
320 * Scans all the CRTCs in @dev's mode_config. If they're using @fb, removes
321 * it, setting it to NULL.
323 void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
325 struct drm_device *dev = fb->dev;
326 struct drm_crtc *crtc;
327 struct drm_mode_set set;
330 /* remove from any CRTC */
331 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
332 if (crtc->fb == fb) {
333 /* should turn off the crtc */
334 memset(&set, 0, sizeof(struct drm_mode_set));
337 ret = crtc->funcs->set_config(&set);
339 DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
343 drm_mode_object_put(dev, &fb->base);
345 dev->mode_config.num_fb--;
347 EXPORT_SYMBOL(drm_framebuffer_cleanup);
350 * drm_crtc_init - Initialise a new CRTC object
352 * @crtc: CRTC object to init
353 * @funcs: callbacks for the new CRTC
356 * Caller must hold mode config lock.
358 * Inits a new object created as base part of an driver crtc object.
360 void drm_crtc_init(struct drm_device *dev, struct drm_crtc *crtc,
361 const struct drm_crtc_funcs *funcs)
366 mutex_lock(&dev->mode_config.mutex);
367 drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
369 list_add_tail(&crtc->head, &dev->mode_config.crtc_list);
370 dev->mode_config.num_crtc++;
371 mutex_unlock(&dev->mode_config.mutex);
373 EXPORT_SYMBOL(drm_crtc_init);
376 * drm_crtc_cleanup - Cleans up the core crtc usage.
377 * @crtc: CRTC to cleanup
380 * Caller must hold mode config lock.
382 * Cleanup @crtc. Removes from drm modesetting space
383 * does NOT free object, caller does that.
385 void drm_crtc_cleanup(struct drm_crtc *crtc)
387 struct drm_device *dev = crtc->dev;
389 if (crtc->gamma_store) {
390 kfree(crtc->gamma_store);
391 crtc->gamma_store = NULL;
394 drm_mode_object_put(dev, &crtc->base);
395 list_del(&crtc->head);
396 dev->mode_config.num_crtc--;
398 EXPORT_SYMBOL(drm_crtc_cleanup);
401 * drm_mode_probed_add - add a mode to a connector's probed mode list
402 * @connector: connector the new mode
406 * Caller must hold mode config lock.
408 * Add @mode to @connector's mode list for later use.
410 void drm_mode_probed_add(struct drm_connector *connector,
411 struct drm_display_mode *mode)
413 list_add(&mode->head, &connector->probed_modes);
415 EXPORT_SYMBOL(drm_mode_probed_add);
418 * drm_mode_remove - remove and free a mode
419 * @connector: connector list to modify
420 * @mode: mode to remove
423 * Caller must hold mode config lock.
425 * Remove @mode from @connector's mode list, then free it.
427 void drm_mode_remove(struct drm_connector *connector,
428 struct drm_display_mode *mode)
430 list_del(&mode->head);
433 EXPORT_SYMBOL(drm_mode_remove);
436 * drm_connector_init - Init a preallocated connector
438 * @connector: the connector to init
439 * @funcs: callbacks for this connector
440 * @name: user visible name of the connector
443 * Caller must hold @dev's mode_config lock.
445 * Initialises a preallocated connector. Connectors should be
446 * subclassed as part of driver connector objects.
448 void drm_connector_init(struct drm_device *dev,
449 struct drm_connector *connector,
450 const struct drm_connector_funcs *funcs,
453 mutex_lock(&dev->mode_config.mutex);
455 connector->dev = dev;
456 connector->funcs = funcs;
457 drm_mode_object_get(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR);
458 connector->connector_type = connector_type;
459 connector->connector_type_id =
460 ++drm_connector_enum_list[connector_type].count; /* TODO */
461 INIT_LIST_HEAD(&connector->user_modes);
462 INIT_LIST_HEAD(&connector->probed_modes);
463 INIT_LIST_HEAD(&connector->modes);
464 connector->edid_blob_ptr = NULL;
466 list_add_tail(&connector->head, &dev->mode_config.connector_list);
467 dev->mode_config.num_connector++;
469 if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
470 drm_connector_attach_property(connector,
471 dev->mode_config.edid_property,
474 drm_connector_attach_property(connector,
475 dev->mode_config.dpms_property, 0);
477 mutex_unlock(&dev->mode_config.mutex);
479 EXPORT_SYMBOL(drm_connector_init);
482 * drm_connector_cleanup - cleans up an initialised connector
483 * @connector: connector to cleanup
486 * Caller must hold @dev's mode_config lock.
488 * Cleans up the connector but doesn't free the object.
490 void drm_connector_cleanup(struct drm_connector *connector)
492 struct drm_device *dev = connector->dev;
493 struct drm_display_mode *mode, *t;
495 list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
496 drm_mode_remove(connector, mode);
498 list_for_each_entry_safe(mode, t, &connector->modes, head)
499 drm_mode_remove(connector, mode);
501 list_for_each_entry_safe(mode, t, &connector->user_modes, head)
502 drm_mode_remove(connector, mode);
504 mutex_lock(&dev->mode_config.mutex);
505 drm_mode_object_put(dev, &connector->base);
506 list_del(&connector->head);
507 dev->mode_config.num_connector--;
508 mutex_unlock(&dev->mode_config.mutex);
510 EXPORT_SYMBOL(drm_connector_cleanup);
512 void drm_encoder_init(struct drm_device *dev,
513 struct drm_encoder *encoder,
514 const struct drm_encoder_funcs *funcs,
517 mutex_lock(&dev->mode_config.mutex);
521 drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
522 encoder->encoder_type = encoder_type;
523 encoder->funcs = funcs;
525 list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
526 dev->mode_config.num_encoder++;
528 mutex_unlock(&dev->mode_config.mutex);
530 EXPORT_SYMBOL(drm_encoder_init);
532 void drm_encoder_cleanup(struct drm_encoder *encoder)
534 struct drm_device *dev = encoder->dev;
535 mutex_lock(&dev->mode_config.mutex);
536 drm_mode_object_put(dev, &encoder->base);
537 list_del(&encoder->head);
538 dev->mode_config.num_encoder--;
539 mutex_unlock(&dev->mode_config.mutex);
541 EXPORT_SYMBOL(drm_encoder_cleanup);
544 * drm_mode_create - create a new display mode
548 * Caller must hold DRM mode_config lock.
550 * Create a new drm_display_mode, give it an ID, and return it.
553 * Pointer to new mode on success, NULL on error.
555 struct drm_display_mode *drm_mode_create(struct drm_device *dev)
557 struct drm_display_mode *nmode;
559 nmode = kzalloc(sizeof(struct drm_display_mode), GFP_KERNEL);
563 drm_mode_object_get(dev, &nmode->base, DRM_MODE_OBJECT_MODE);
566 EXPORT_SYMBOL(drm_mode_create);
569 * drm_mode_destroy - remove a mode
571 * @mode: mode to remove
574 * Caller must hold mode config lock.
576 * Free @mode's unique identifier, then free it.
578 void drm_mode_destroy(struct drm_device *dev, struct drm_display_mode *mode)
580 drm_mode_object_put(dev, &mode->base);
584 EXPORT_SYMBOL(drm_mode_destroy);
586 static int drm_mode_create_standard_connector_properties(struct drm_device *dev)
588 struct drm_property *edid;
589 struct drm_property *dpms;
593 * Standard properties (apply to all connectors)
595 edid = drm_property_create(dev, DRM_MODE_PROP_BLOB |
596 DRM_MODE_PROP_IMMUTABLE,
598 dev->mode_config.edid_property = edid;
600 dpms = drm_property_create(dev, DRM_MODE_PROP_ENUM,
601 "DPMS", ARRAY_SIZE(drm_dpms_enum_list));
602 for (i = 0; i < ARRAY_SIZE(drm_dpms_enum_list); i++)
603 drm_property_add_enum(dpms, i, drm_dpms_enum_list[i].type,
604 drm_dpms_enum_list[i].name);
605 dev->mode_config.dpms_property = dpms;
611 * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
614 * Called by a driver the first time a DVI-I connector is made.
616 int drm_mode_create_dvi_i_properties(struct drm_device *dev)
618 struct drm_property *dvi_i_selector;
619 struct drm_property *dvi_i_subconnector;
622 if (dev->mode_config.dvi_i_select_subconnector_property)
626 drm_property_create(dev, DRM_MODE_PROP_ENUM,
627 "select subconnector",
628 ARRAY_SIZE(drm_dvi_i_select_enum_list));
629 for (i = 0; i < ARRAY_SIZE(drm_dvi_i_select_enum_list); i++)
630 drm_property_add_enum(dvi_i_selector, i,
631 drm_dvi_i_select_enum_list[i].type,
632 drm_dvi_i_select_enum_list[i].name);
633 dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
636 drm_property_create(dev, DRM_MODE_PROP_ENUM |
637 DRM_MODE_PROP_IMMUTABLE,
639 ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
640 for (i = 0; i < ARRAY_SIZE(drm_dvi_i_subconnector_enum_list); i++)
641 drm_property_add_enum(dvi_i_subconnector, i,
642 drm_dvi_i_subconnector_enum_list[i].type,
643 drm_dvi_i_subconnector_enum_list[i].name);
644 dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
648 EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
651 * drm_create_tv_properties - create TV specific connector properties
653 * @num_modes: number of different TV formats (modes) supported
654 * @modes: array of pointers to strings containing name of each format
656 * Called by a driver's TV initialization routine, this function creates
657 * the TV specific connector properties for a given device. Caller is
658 * responsible for allocating a list of format names and passing them to
661 int drm_mode_create_tv_properties(struct drm_device *dev, int num_modes,
664 struct drm_property *tv_selector;
665 struct drm_property *tv_subconnector;
668 if (dev->mode_config.tv_select_subconnector_property)
672 * Basic connector properties
674 tv_selector = drm_property_create(dev, DRM_MODE_PROP_ENUM,
675 "select subconnector",
676 ARRAY_SIZE(drm_tv_select_enum_list));
677 for (i = 0; i < ARRAY_SIZE(drm_tv_select_enum_list); i++)
678 drm_property_add_enum(tv_selector, i,
679 drm_tv_select_enum_list[i].type,
680 drm_tv_select_enum_list[i].name);
681 dev->mode_config.tv_select_subconnector_property = tv_selector;
684 drm_property_create(dev, DRM_MODE_PROP_ENUM |
685 DRM_MODE_PROP_IMMUTABLE, "subconnector",
686 ARRAY_SIZE(drm_tv_subconnector_enum_list));
687 for (i = 0; i < ARRAY_SIZE(drm_tv_subconnector_enum_list); i++)
688 drm_property_add_enum(tv_subconnector, i,
689 drm_tv_subconnector_enum_list[i].type,
690 drm_tv_subconnector_enum_list[i].name);
691 dev->mode_config.tv_subconnector_property = tv_subconnector;
694 * Other, TV specific properties: margins & TV modes.
696 dev->mode_config.tv_left_margin_property =
697 drm_property_create(dev, DRM_MODE_PROP_RANGE,
699 dev->mode_config.tv_left_margin_property->values[0] = 0;
700 dev->mode_config.tv_left_margin_property->values[1] = 100;
702 dev->mode_config.tv_right_margin_property =
703 drm_property_create(dev, DRM_MODE_PROP_RANGE,
705 dev->mode_config.tv_right_margin_property->values[0] = 0;
706 dev->mode_config.tv_right_margin_property->values[1] = 100;
708 dev->mode_config.tv_top_margin_property =
709 drm_property_create(dev, DRM_MODE_PROP_RANGE,
711 dev->mode_config.tv_top_margin_property->values[0] = 0;
712 dev->mode_config.tv_top_margin_property->values[1] = 100;
714 dev->mode_config.tv_bottom_margin_property =
715 drm_property_create(dev, DRM_MODE_PROP_RANGE,
717 dev->mode_config.tv_bottom_margin_property->values[0] = 0;
718 dev->mode_config.tv_bottom_margin_property->values[1] = 100;
720 dev->mode_config.tv_mode_property =
721 drm_property_create(dev, DRM_MODE_PROP_ENUM,
723 for (i = 0; i < num_modes; i++)
724 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
727 dev->mode_config.tv_brightness_property =
728 drm_property_create(dev, DRM_MODE_PROP_RANGE,
730 dev->mode_config.tv_brightness_property->values[0] = 0;
731 dev->mode_config.tv_brightness_property->values[1] = 100;
733 dev->mode_config.tv_contrast_property =
734 drm_property_create(dev, DRM_MODE_PROP_RANGE,
736 dev->mode_config.tv_contrast_property->values[0] = 0;
737 dev->mode_config.tv_contrast_property->values[1] = 100;
739 dev->mode_config.tv_flicker_reduction_property =
740 drm_property_create(dev, DRM_MODE_PROP_RANGE,
741 "flicker reduction", 2);
742 dev->mode_config.tv_flicker_reduction_property->values[0] = 0;
743 dev->mode_config.tv_flicker_reduction_property->values[1] = 100;
745 dev->mode_config.tv_overscan_property =
746 drm_property_create(dev, DRM_MODE_PROP_RANGE,
748 dev->mode_config.tv_overscan_property->values[0] = 0;
749 dev->mode_config.tv_overscan_property->values[1] = 100;
751 dev->mode_config.tv_saturation_property =
752 drm_property_create(dev, DRM_MODE_PROP_RANGE,
754 dev->mode_config.tv_saturation_property->values[0] = 0;
755 dev->mode_config.tv_saturation_property->values[1] = 100;
757 dev->mode_config.tv_hue_property =
758 drm_property_create(dev, DRM_MODE_PROP_RANGE,
760 dev->mode_config.tv_hue_property->values[0] = 0;
761 dev->mode_config.tv_hue_property->values[1] = 100;
765 EXPORT_SYMBOL(drm_mode_create_tv_properties);
768 * drm_mode_create_scaling_mode_property - create scaling mode property
771 * Called by a driver the first time it's needed, must be attached to desired
774 int drm_mode_create_scaling_mode_property(struct drm_device *dev)
776 struct drm_property *scaling_mode;
779 if (dev->mode_config.scaling_mode_property)
783 drm_property_create(dev, DRM_MODE_PROP_ENUM, "scaling mode",
784 ARRAY_SIZE(drm_scaling_mode_enum_list));
785 for (i = 0; i < ARRAY_SIZE(drm_scaling_mode_enum_list); i++)
786 drm_property_add_enum(scaling_mode, i,
787 drm_scaling_mode_enum_list[i].type,
788 drm_scaling_mode_enum_list[i].name);
790 dev->mode_config.scaling_mode_property = scaling_mode;
794 EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
797 * drm_mode_create_dithering_property - create dithering property
800 * Called by a driver the first time it's needed, must be attached to desired
803 int drm_mode_create_dithering_property(struct drm_device *dev)
805 struct drm_property *dithering_mode;
808 if (dev->mode_config.dithering_mode_property)
812 drm_property_create(dev, DRM_MODE_PROP_ENUM, "dithering",
813 ARRAY_SIZE(drm_dithering_mode_enum_list));
814 for (i = 0; i < ARRAY_SIZE(drm_dithering_mode_enum_list); i++)
815 drm_property_add_enum(dithering_mode, i,
816 drm_dithering_mode_enum_list[i].type,
817 drm_dithering_mode_enum_list[i].name);
818 dev->mode_config.dithering_mode_property = dithering_mode;
822 EXPORT_SYMBOL(drm_mode_create_dithering_property);
825 * drm_mode_create_dirty_property - create dirty property
828 * Called by a driver the first time it's needed, must be attached to desired
831 int drm_mode_create_dirty_info_property(struct drm_device *dev)
833 struct drm_property *dirty_info;
836 if (dev->mode_config.dirty_info_property)
840 drm_property_create(dev, DRM_MODE_PROP_ENUM |
841 DRM_MODE_PROP_IMMUTABLE,
843 ARRAY_SIZE(drm_dirty_info_enum_list));
844 for (i = 0; i < ARRAY_SIZE(drm_dirty_info_enum_list); i++)
845 drm_property_add_enum(dirty_info, i,
846 drm_dirty_info_enum_list[i].type,
847 drm_dirty_info_enum_list[i].name);
848 dev->mode_config.dirty_info_property = dirty_info;
852 EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
855 * drm_mode_config_init - initialize DRM mode_configuration structure
859 * None, should happen single threaded at init time.
861 * Initialize @dev's mode_config structure, used for tracking the graphics
862 * configuration of @dev.
864 void drm_mode_config_init(struct drm_device *dev)
866 mutex_init(&dev->mode_config.mutex);
867 mutex_init(&dev->mode_config.idr_mutex);
868 INIT_LIST_HEAD(&dev->mode_config.fb_list);
869 INIT_LIST_HEAD(&dev->mode_config.crtc_list);
870 INIT_LIST_HEAD(&dev->mode_config.connector_list);
871 INIT_LIST_HEAD(&dev->mode_config.encoder_list);
872 INIT_LIST_HEAD(&dev->mode_config.property_list);
873 INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
874 idr_init(&dev->mode_config.crtc_idr);
876 mutex_lock(&dev->mode_config.mutex);
877 drm_mode_create_standard_connector_properties(dev);
878 mutex_unlock(&dev->mode_config.mutex);
880 /* Just to be sure */
881 dev->mode_config.num_fb = 0;
882 dev->mode_config.num_connector = 0;
883 dev->mode_config.num_crtc = 0;
884 dev->mode_config.num_encoder = 0;
886 EXPORT_SYMBOL(drm_mode_config_init);
888 int drm_mode_group_init(struct drm_device *dev, struct drm_mode_group *group)
890 uint32_t total_objects = 0;
892 total_objects += dev->mode_config.num_crtc;
893 total_objects += dev->mode_config.num_connector;
894 total_objects += dev->mode_config.num_encoder;
896 group->id_list = kzalloc(total_objects * sizeof(uint32_t), GFP_KERNEL);
900 group->num_crtcs = 0;
901 group->num_connectors = 0;
902 group->num_encoders = 0;
906 int drm_mode_group_init_legacy_group(struct drm_device *dev,
907 struct drm_mode_group *group)
909 struct drm_crtc *crtc;
910 struct drm_encoder *encoder;
911 struct drm_connector *connector;
914 if ((ret = drm_mode_group_init(dev, group)))
917 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
918 group->id_list[group->num_crtcs++] = crtc->base.id;
920 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
921 group->id_list[group->num_crtcs + group->num_encoders++] =
924 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
925 group->id_list[group->num_crtcs + group->num_encoders +
926 group->num_connectors++] = connector->base.id;
932 * drm_mode_config_cleanup - free up DRM mode_config info
936 * Caller must hold mode config lock.
938 * Free up all the connectors and CRTCs associated with this DRM device, then
939 * free up the framebuffers and associated buffer objects.
941 * FIXME: cleanup any dangling user buffer objects too
943 void drm_mode_config_cleanup(struct drm_device *dev)
945 struct drm_connector *connector, *ot;
946 struct drm_crtc *crtc, *ct;
947 struct drm_encoder *encoder, *enct;
948 struct drm_framebuffer *fb, *fbt;
949 struct drm_property *property, *pt;
951 list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
953 encoder->funcs->destroy(encoder);
956 list_for_each_entry_safe(connector, ot,
957 &dev->mode_config.connector_list, head) {
958 connector->funcs->destroy(connector);
961 list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
963 drm_property_destroy(dev, property);
966 list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
967 fb->funcs->destroy(fb);
970 list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
971 crtc->funcs->destroy(crtc);
975 EXPORT_SYMBOL(drm_mode_config_cleanup);
978 * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
979 * @out: drm_mode_modeinfo struct to return to the user
980 * @in: drm_display_mode to use
985 * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
988 void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out,
989 struct drm_display_mode *in)
991 out->clock = in->clock;
992 out->hdisplay = in->hdisplay;
993 out->hsync_start = in->hsync_start;
994 out->hsync_end = in->hsync_end;
995 out->htotal = in->htotal;
996 out->hskew = in->hskew;
997 out->vdisplay = in->vdisplay;
998 out->vsync_start = in->vsync_start;
999 out->vsync_end = in->vsync_end;
1000 out->vtotal = in->vtotal;
1001 out->vscan = in->vscan;
1002 out->vrefresh = in->vrefresh;
1003 out->flags = in->flags;
1004 out->type = in->type;
1005 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1006 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1010 * drm_crtc_convert_to_umode - convert a modeinfo into a drm_display_mode
1011 * @out: drm_display_mode to return to the user
1012 * @in: drm_mode_modeinfo to use
1017 * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
1020 void drm_crtc_convert_umode(struct drm_display_mode *out,
1021 struct drm_mode_modeinfo *in)
1023 out->clock = in->clock;
1024 out->hdisplay = in->hdisplay;
1025 out->hsync_start = in->hsync_start;
1026 out->hsync_end = in->hsync_end;
1027 out->htotal = in->htotal;
1028 out->hskew = in->hskew;
1029 out->vdisplay = in->vdisplay;
1030 out->vsync_start = in->vsync_start;
1031 out->vsync_end = in->vsync_end;
1032 out->vtotal = in->vtotal;
1033 out->vscan = in->vscan;
1034 out->vrefresh = in->vrefresh;
1035 out->flags = in->flags;
1036 out->type = in->type;
1037 strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
1038 out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
1042 * drm_mode_getresources - get graphics configuration
1043 * @inode: inode from the ioctl
1044 * @filp: file * from the ioctl
1045 * @cmd: cmd from ioctl
1046 * @arg: arg from ioctl
1049 * Takes mode config lock.
1051 * Construct a set of configuration description structures and return
1052 * them to the user, including CRTC, connector and framebuffer configuration.
1054 * Called by the user via ioctl.
1057 * Zero on success, errno on failure.
1059 int drm_mode_getresources(struct drm_device *dev, void *data,
1060 struct drm_file *file_priv)
1062 struct drm_mode_card_res *card_res = data;
1063 struct list_head *lh;
1064 struct drm_framebuffer *fb;
1065 struct drm_connector *connector;
1066 struct drm_crtc *crtc;
1067 struct drm_encoder *encoder;
1069 int connector_count = 0;
1072 int encoder_count = 0;
1074 uint32_t __user *fb_id;
1075 uint32_t __user *crtc_id;
1076 uint32_t __user *connector_id;
1077 uint32_t __user *encoder_id;
1078 struct drm_mode_group *mode_group;
1080 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1083 mutex_lock(&dev->mode_config.mutex);
1086 * For the non-control nodes we need to limit the list of resources
1087 * by IDs in the group list for this node
1089 list_for_each(lh, &file_priv->fbs)
1092 mode_group = &file_priv->master->minor->mode_group;
1093 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1095 list_for_each(lh, &dev->mode_config.crtc_list)
1098 list_for_each(lh, &dev->mode_config.connector_list)
1101 list_for_each(lh, &dev->mode_config.encoder_list)
1105 crtc_count = mode_group->num_crtcs;
1106 connector_count = mode_group->num_connectors;
1107 encoder_count = mode_group->num_encoders;
1110 card_res->max_height = dev->mode_config.max_height;
1111 card_res->min_height = dev->mode_config.min_height;
1112 card_res->max_width = dev->mode_config.max_width;
1113 card_res->min_width = dev->mode_config.min_width;
1115 /* handle this in 4 parts */
1117 if (card_res->count_fbs >= fb_count) {
1119 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
1120 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
1121 if (put_user(fb->base.id, fb_id + copied)) {
1128 card_res->count_fbs = fb_count;
1131 if (card_res->count_crtcs >= crtc_count) {
1133 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
1134 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1135 list_for_each_entry(crtc, &dev->mode_config.crtc_list,
1137 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
1138 if (put_user(crtc->base.id, crtc_id + copied)) {
1145 for (i = 0; i < mode_group->num_crtcs; i++) {
1146 if (put_user(mode_group->id_list[i],
1147 crtc_id + copied)) {
1155 card_res->count_crtcs = crtc_count;
1158 if (card_res->count_encoders >= encoder_count) {
1160 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
1161 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1162 list_for_each_entry(encoder,
1163 &dev->mode_config.encoder_list,
1165 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id,
1166 drm_get_encoder_name(encoder));
1167 if (put_user(encoder->base.id, encoder_id +
1175 for (i = mode_group->num_crtcs; i < mode_group->num_crtcs + mode_group->num_encoders; i++) {
1176 if (put_user(mode_group->id_list[i],
1177 encoder_id + copied)) {
1186 card_res->count_encoders = encoder_count;
1189 if (card_res->count_connectors >= connector_count) {
1191 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
1192 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
1193 list_for_each_entry(connector,
1194 &dev->mode_config.connector_list,
1196 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1198 drm_get_connector_name(connector));
1199 if (put_user(connector->base.id,
1200 connector_id + copied)) {
1207 int start = mode_group->num_crtcs +
1208 mode_group->num_encoders;
1209 for (i = start; i < start + mode_group->num_connectors; i++) {
1210 if (put_user(mode_group->id_list[i],
1211 connector_id + copied)) {
1219 card_res->count_connectors = connector_count;
1221 DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs,
1222 card_res->count_connectors, card_res->count_encoders);
1225 mutex_unlock(&dev->mode_config.mutex);
1230 * drm_mode_getcrtc - get CRTC configuration
1231 * @inode: inode from the ioctl
1232 * @filp: file * from the ioctl
1233 * @cmd: cmd from ioctl
1234 * @arg: arg from ioctl
1239 * Construct a CRTC configuration structure to return to the user.
1241 * Called by the user via ioctl.
1244 * Zero on success, errno on failure.
1246 int drm_mode_getcrtc(struct drm_device *dev,
1247 void *data, struct drm_file *file_priv)
1249 struct drm_mode_crtc *crtc_resp = data;
1250 struct drm_crtc *crtc;
1251 struct drm_mode_object *obj;
1254 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1257 mutex_lock(&dev->mode_config.mutex);
1259 obj = drm_mode_object_find(dev, crtc_resp->crtc_id,
1260 DRM_MODE_OBJECT_CRTC);
1265 crtc = obj_to_crtc(obj);
1267 crtc_resp->x = crtc->x;
1268 crtc_resp->y = crtc->y;
1269 crtc_resp->gamma_size = crtc->gamma_size;
1271 crtc_resp->fb_id = crtc->fb->base.id;
1273 crtc_resp->fb_id = 0;
1275 if (crtc->enabled) {
1277 drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
1278 crtc_resp->mode_valid = 1;
1281 crtc_resp->mode_valid = 0;
1285 mutex_unlock(&dev->mode_config.mutex);
1290 * drm_mode_getconnector - get connector configuration
1291 * @inode: inode from the ioctl
1292 * @filp: file * from the ioctl
1293 * @cmd: cmd from ioctl
1294 * @arg: arg from ioctl
1299 * Construct a connector configuration structure to return to the user.
1301 * Called by the user via ioctl.
1304 * Zero on success, errno on failure.
1306 int drm_mode_getconnector(struct drm_device *dev, void *data,
1307 struct drm_file *file_priv)
1309 struct drm_mode_get_connector *out_resp = data;
1310 struct drm_mode_object *obj;
1311 struct drm_connector *connector;
1312 struct drm_display_mode *mode;
1314 int props_count = 0;
1315 int encoders_count = 0;
1319 struct drm_mode_modeinfo u_mode;
1320 struct drm_mode_modeinfo __user *mode_ptr;
1321 uint32_t __user *prop_ptr;
1322 uint64_t __user *prop_values;
1323 uint32_t __user *encoder_ptr;
1325 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1328 memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
1330 DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id);
1332 mutex_lock(&dev->mode_config.mutex);
1334 obj = drm_mode_object_find(dev, out_resp->connector_id,
1335 DRM_MODE_OBJECT_CONNECTOR);
1340 connector = obj_to_connector(obj);
1342 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
1343 if (connector->property_ids[i] != 0) {
1348 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1349 if (connector->encoder_ids[i] != 0) {
1354 if (out_resp->count_modes == 0) {
1355 connector->funcs->fill_modes(connector,
1356 dev->mode_config.max_width,
1357 dev->mode_config.max_height);
1360 /* delayed so we get modes regardless of pre-fill_modes state */
1361 list_for_each_entry(mode, &connector->modes, head)
1364 out_resp->connector_id = connector->base.id;
1365 out_resp->connector_type = connector->connector_type;
1366 out_resp->connector_type_id = connector->connector_type_id;
1367 out_resp->mm_width = connector->display_info.width_mm;
1368 out_resp->mm_height = connector->display_info.height_mm;
1369 out_resp->subpixel = connector->display_info.subpixel_order;
1370 out_resp->connection = connector->status;
1371 if (connector->encoder)
1372 out_resp->encoder_id = connector->encoder->base.id;
1374 out_resp->encoder_id = 0;
1377 * This ioctl is called twice, once to determine how much space is
1378 * needed, and the 2nd time to fill it.
1380 if ((out_resp->count_modes >= mode_count) && mode_count) {
1382 mode_ptr = (struct drm_mode_modeinfo *)(unsigned long)out_resp->modes_ptr;
1383 list_for_each_entry(mode, &connector->modes, head) {
1384 drm_crtc_convert_to_umode(&u_mode, mode);
1385 if (copy_to_user(mode_ptr + copied,
1386 &u_mode, sizeof(u_mode))) {
1393 out_resp->count_modes = mode_count;
1395 if ((out_resp->count_props >= props_count) && props_count) {
1397 prop_ptr = (uint32_t *)(unsigned long)(out_resp->props_ptr);
1398 prop_values = (uint64_t *)(unsigned long)(out_resp->prop_values_ptr);
1399 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
1400 if (connector->property_ids[i] != 0) {
1401 if (put_user(connector->property_ids[i],
1402 prop_ptr + copied)) {
1407 if (put_user(connector->property_values[i],
1408 prop_values + copied)) {
1416 out_resp->count_props = props_count;
1418 if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
1420 encoder_ptr = (uint32_t *)(unsigned long)(out_resp->encoders_ptr);
1421 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
1422 if (connector->encoder_ids[i] != 0) {
1423 if (put_user(connector->encoder_ids[i],
1424 encoder_ptr + copied)) {
1432 out_resp->count_encoders = encoders_count;
1435 mutex_unlock(&dev->mode_config.mutex);
1439 int drm_mode_getencoder(struct drm_device *dev, void *data,
1440 struct drm_file *file_priv)
1442 struct drm_mode_get_encoder *enc_resp = data;
1443 struct drm_mode_object *obj;
1444 struct drm_encoder *encoder;
1447 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1450 mutex_lock(&dev->mode_config.mutex);
1451 obj = drm_mode_object_find(dev, enc_resp->encoder_id,
1452 DRM_MODE_OBJECT_ENCODER);
1457 encoder = obj_to_encoder(obj);
1460 enc_resp->crtc_id = encoder->crtc->base.id;
1462 enc_resp->crtc_id = 0;
1463 enc_resp->encoder_type = encoder->encoder_type;
1464 enc_resp->encoder_id = encoder->base.id;
1465 enc_resp->possible_crtcs = encoder->possible_crtcs;
1466 enc_resp->possible_clones = encoder->possible_clones;
1469 mutex_unlock(&dev->mode_config.mutex);
1474 * drm_mode_setcrtc - set CRTC configuration
1475 * @inode: inode from the ioctl
1476 * @filp: file * from the ioctl
1477 * @cmd: cmd from ioctl
1478 * @arg: arg from ioctl
1483 * Build a new CRTC configuration based on user request.
1485 * Called by the user via ioctl.
1488 * Zero on success, errno on failure.
1490 int drm_mode_setcrtc(struct drm_device *dev, void *data,
1491 struct drm_file *file_priv)
1493 struct drm_mode_config *config = &dev->mode_config;
1494 struct drm_mode_crtc *crtc_req = data;
1495 struct drm_mode_object *obj;
1496 struct drm_crtc *crtc, *crtcfb;
1497 struct drm_connector **connector_set = NULL, *connector;
1498 struct drm_framebuffer *fb = NULL;
1499 struct drm_display_mode *mode = NULL;
1500 struct drm_mode_set set;
1501 uint32_t __user *set_connectors_ptr;
1505 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1508 mutex_lock(&dev->mode_config.mutex);
1509 obj = drm_mode_object_find(dev, crtc_req->crtc_id,
1510 DRM_MODE_OBJECT_CRTC);
1512 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
1516 crtc = obj_to_crtc(obj);
1517 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
1519 if (crtc_req->mode_valid) {
1520 /* If we have a mode we need a framebuffer. */
1521 /* If we pass -1, set the mode with the currently bound fb */
1522 if (crtc_req->fb_id == -1) {
1523 list_for_each_entry(crtcfb,
1524 &dev->mode_config.crtc_list, head) {
1525 if (crtcfb == crtc) {
1526 DRM_DEBUG_KMS("Using current fb for "
1532 obj = drm_mode_object_find(dev, crtc_req->fb_id,
1533 DRM_MODE_OBJECT_FB);
1535 DRM_DEBUG_KMS("Unknown FB ID%d\n",
1540 fb = obj_to_fb(obj);
1543 mode = drm_mode_create(dev);
1544 drm_crtc_convert_umode(mode, &crtc_req->mode);
1545 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
1548 if (crtc_req->count_connectors == 0 && mode) {
1549 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
1554 if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
1555 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
1556 crtc_req->count_connectors);
1561 if (crtc_req->count_connectors > 0) {
1564 /* Avoid unbounded kernel memory allocation */
1565 if (crtc_req->count_connectors > config->num_connector) {
1570 connector_set = kmalloc(crtc_req->count_connectors *
1571 sizeof(struct drm_connector *),
1573 if (!connector_set) {
1578 for (i = 0; i < crtc_req->count_connectors; i++) {
1579 set_connectors_ptr = (uint32_t *)(unsigned long)crtc_req->set_connectors_ptr;
1580 if (get_user(out_id, &set_connectors_ptr[i])) {
1585 obj = drm_mode_object_find(dev, out_id,
1586 DRM_MODE_OBJECT_CONNECTOR);
1588 DRM_DEBUG_KMS("Connector id %d unknown\n",
1593 connector = obj_to_connector(obj);
1594 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
1596 drm_get_connector_name(connector));
1598 connector_set[i] = connector;
1603 set.x = crtc_req->x;
1604 set.y = crtc_req->y;
1606 set.connectors = connector_set;
1607 set.num_connectors = crtc_req->count_connectors;
1609 ret = crtc->funcs->set_config(&set);
1612 kfree(connector_set);
1613 mutex_unlock(&dev->mode_config.mutex);
1617 int drm_mode_cursor_ioctl(struct drm_device *dev,
1618 void *data, struct drm_file *file_priv)
1620 struct drm_mode_cursor *req = data;
1621 struct drm_mode_object *obj;
1622 struct drm_crtc *crtc;
1625 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1629 DRM_ERROR("no operation set\n");
1633 mutex_lock(&dev->mode_config.mutex);
1634 obj = drm_mode_object_find(dev, req->crtc_id, DRM_MODE_OBJECT_CRTC);
1636 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
1640 crtc = obj_to_crtc(obj);
1642 if (req->flags & DRM_MODE_CURSOR_BO) {
1643 if (!crtc->funcs->cursor_set) {
1644 DRM_ERROR("crtc does not support cursor\n");
1648 /* Turns off the cursor if handle is 0 */
1649 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
1650 req->width, req->height);
1653 if (req->flags & DRM_MODE_CURSOR_MOVE) {
1654 if (crtc->funcs->cursor_move) {
1655 ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
1657 DRM_ERROR("crtc does not support cursor\n");
1663 mutex_unlock(&dev->mode_config.mutex);
1668 * drm_mode_addfb - add an FB to the graphics configuration
1669 * @inode: inode from the ioctl
1670 * @filp: file * from the ioctl
1671 * @cmd: cmd from ioctl
1672 * @arg: arg from ioctl
1675 * Takes mode config lock.
1677 * Add a new FB to the specified CRTC, given a user request.
1679 * Called by the user via ioctl.
1682 * Zero on success, errno on failure.
1684 int drm_mode_addfb(struct drm_device *dev,
1685 void *data, struct drm_file *file_priv)
1687 struct drm_mode_fb_cmd *r = data;
1688 struct drm_mode_config *config = &dev->mode_config;
1689 struct drm_framebuffer *fb;
1692 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1695 if ((config->min_width > r->width) || (r->width > config->max_width)) {
1696 DRM_ERROR("mode new framebuffer width not within limits\n");
1699 if ((config->min_height > r->height) || (r->height > config->max_height)) {
1700 DRM_ERROR("mode new framebuffer height not within limits\n");
1704 mutex_lock(&dev->mode_config.mutex);
1706 /* TODO check buffer is sufficiently large */
1707 /* TODO setup destructor callback */
1709 fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
1711 DRM_ERROR("could not create framebuffer\n");
1716 r->fb_id = fb->base.id;
1717 list_add(&fb->filp_head, &file_priv->fbs);
1718 DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
1721 mutex_unlock(&dev->mode_config.mutex);
1726 * drm_mode_rmfb - remove an FB from the configuration
1727 * @inode: inode from the ioctl
1728 * @filp: file * from the ioctl
1729 * @cmd: cmd from ioctl
1730 * @arg: arg from ioctl
1733 * Takes mode config lock.
1735 * Remove the FB specified by the user.
1737 * Called by the user via ioctl.
1740 * Zero on success, errno on failure.
1742 int drm_mode_rmfb(struct drm_device *dev,
1743 void *data, struct drm_file *file_priv)
1745 struct drm_mode_object *obj;
1746 struct drm_framebuffer *fb = NULL;
1747 struct drm_framebuffer *fbl = NULL;
1748 uint32_t *id = data;
1752 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1755 mutex_lock(&dev->mode_config.mutex);
1756 obj = drm_mode_object_find(dev, *id, DRM_MODE_OBJECT_FB);
1757 /* TODO check that we really get a framebuffer back. */
1759 DRM_ERROR("mode invalid framebuffer id\n");
1763 fb = obj_to_fb(obj);
1765 list_for_each_entry(fbl, &file_priv->fbs, filp_head)
1770 DRM_ERROR("tried to remove a fb that we didn't own\n");
1775 /* TODO release all crtc connected to the framebuffer */
1776 /* TODO unhock the destructor from the buffer object */
1778 list_del(&fb->filp_head);
1779 fb->funcs->destroy(fb);
1782 mutex_unlock(&dev->mode_config.mutex);
1787 * drm_mode_getfb - get FB info
1788 * @inode: inode from the ioctl
1789 * @filp: file * from the ioctl
1790 * @cmd: cmd from ioctl
1791 * @arg: arg from ioctl
1796 * Lookup the FB given its ID and return info about it.
1798 * Called by the user via ioctl.
1801 * Zero on success, errno on failure.
1803 int drm_mode_getfb(struct drm_device *dev,
1804 void *data, struct drm_file *file_priv)
1806 struct drm_mode_fb_cmd *r = data;
1807 struct drm_mode_object *obj;
1808 struct drm_framebuffer *fb;
1811 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1814 mutex_lock(&dev->mode_config.mutex);
1815 obj = drm_mode_object_find(dev, r->fb_id, DRM_MODE_OBJECT_FB);
1817 DRM_ERROR("invalid framebuffer id\n");
1821 fb = obj_to_fb(obj);
1823 r->height = fb->height;
1824 r->width = fb->width;
1825 r->depth = fb->depth;
1826 r->bpp = fb->bits_per_pixel;
1827 r->pitch = fb->pitch;
1828 fb->funcs->create_handle(fb, file_priv, &r->handle);
1831 mutex_unlock(&dev->mode_config.mutex);
1835 int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
1836 void *data, struct drm_file *file_priv)
1838 struct drm_clip_rect __user *clips_ptr;
1839 struct drm_clip_rect *clips = NULL;
1840 struct drm_mode_fb_dirty_cmd *r = data;
1841 struct drm_mode_object *obj;
1842 struct drm_framebuffer *fb;
1847 if (!drm_core_check_feature(dev, DRIVER_MODESET))
1850 mutex_lock(&dev->mode_config.mutex);
1851 obj = drm_mode_object_find(dev, r->fb_id, DRM_MODE_OBJECT_FB);
1853 DRM_ERROR("invalid framebuffer id\n");
1857 fb = obj_to_fb(obj);
1859 num_clips = r->num_clips;
1860 clips_ptr = (struct drm_clip_rect *)(unsigned long)r->clips_ptr;
1862 if (!num_clips != !clips_ptr) {
1867 flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
1869 /* If userspace annotates copy, clips must come in pairs */
1870 if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
1875 if (num_clips && clips_ptr) {
1876 clips = kzalloc(num_clips * sizeof(*clips), GFP_KERNEL);
1882 ret = copy_from_user(clips, clips_ptr,
1883 num_clips * sizeof(*clips));
1890 if (fb->funcs->dirty) {
1891 ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
1901 mutex_unlock(&dev->mode_config.mutex);
1907 * drm_fb_release - remove and free the FBs on this file
1908 * @filp: file * from the ioctl
1911 * Takes mode config lock.
1913 * Destroy all the FBs associated with @filp.
1915 * Called by the user via ioctl.
1918 * Zero on success, errno on failure.
1920 void drm_fb_release(struct drm_file *priv)
1922 struct drm_device *dev = priv->minor->dev;
1923 struct drm_framebuffer *fb, *tfb;
1925 mutex_lock(&dev->mode_config.mutex);
1926 list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
1927 list_del(&fb->filp_head);
1928 fb->funcs->destroy(fb);
1930 mutex_unlock(&dev->mode_config.mutex);
1934 * drm_mode_attachmode - add a mode to the user mode list
1936 * @connector: connector to add the mode to
1937 * @mode: mode to add
1939 * Add @mode to @connector's user mode list.
1941 static int drm_mode_attachmode(struct drm_device *dev,
1942 struct drm_connector *connector,
1943 struct drm_display_mode *mode)
1947 list_add_tail(&mode->head, &connector->user_modes);
1951 int drm_mode_attachmode_crtc(struct drm_device *dev, struct drm_crtc *crtc,
1952 struct drm_display_mode *mode)
1954 struct drm_connector *connector;
1956 struct drm_display_mode *dup_mode;
1958 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1959 if (!connector->encoder)
1961 if (connector->encoder->crtc == crtc) {
1963 dup_mode = drm_mode_duplicate(dev, mode);
1966 ret = drm_mode_attachmode(dev, connector, dup_mode);
1974 EXPORT_SYMBOL(drm_mode_attachmode_crtc);
1976 static int drm_mode_detachmode(struct drm_device *dev,
1977 struct drm_connector *connector,
1978 struct drm_display_mode *mode)
1982 struct drm_display_mode *match_mode, *t;
1984 list_for_each_entry_safe(match_mode, t, &connector->user_modes, head) {
1985 if (drm_mode_equal(match_mode, mode)) {
1986 list_del(&match_mode->head);
1987 drm_mode_destroy(dev, match_mode);
1999 int drm_mode_detachmode_crtc(struct drm_device *dev, struct drm_display_mode *mode)
2001 struct drm_connector *connector;
2003 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2004 drm_mode_detachmode(dev, connector, mode);
2008 EXPORT_SYMBOL(drm_mode_detachmode_crtc);
2011 * drm_fb_attachmode - Attach a user mode to an connector
2012 * @inode: inode from the ioctl
2013 * @filp: file * from the ioctl
2014 * @cmd: cmd from ioctl
2015 * @arg: arg from ioctl
2017 * This attaches a user specified mode to an connector.
2018 * Called by the user via ioctl.
2021 * Zero on success, errno on failure.
2023 int drm_mode_attachmode_ioctl(struct drm_device *dev,
2024 void *data, struct drm_file *file_priv)
2026 struct drm_mode_mode_cmd *mode_cmd = data;
2027 struct drm_connector *connector;
2028 struct drm_display_mode *mode;
2029 struct drm_mode_object *obj;
2030 struct drm_mode_modeinfo *umode = &mode_cmd->mode;
2033 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2036 mutex_lock(&dev->mode_config.mutex);
2038 obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
2043 connector = obj_to_connector(obj);
2045 mode = drm_mode_create(dev);
2051 drm_crtc_convert_umode(mode, umode);
2053 ret = drm_mode_attachmode(dev, connector, mode);
2055 mutex_unlock(&dev->mode_config.mutex);
2061 * drm_fb_detachmode - Detach a user specified mode from an connector
2062 * @inode: inode from the ioctl
2063 * @filp: file * from the ioctl
2064 * @cmd: cmd from ioctl
2065 * @arg: arg from ioctl
2067 * Called by the user via ioctl.
2070 * Zero on success, errno on failure.
2072 int drm_mode_detachmode_ioctl(struct drm_device *dev,
2073 void *data, struct drm_file *file_priv)
2075 struct drm_mode_object *obj;
2076 struct drm_mode_mode_cmd *mode_cmd = data;
2077 struct drm_connector *connector;
2078 struct drm_display_mode mode;
2079 struct drm_mode_modeinfo *umode = &mode_cmd->mode;
2082 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2085 mutex_lock(&dev->mode_config.mutex);
2087 obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
2092 connector = obj_to_connector(obj);
2094 drm_crtc_convert_umode(&mode, umode);
2095 ret = drm_mode_detachmode(dev, connector, &mode);
2097 mutex_unlock(&dev->mode_config.mutex);
2101 struct drm_property *drm_property_create(struct drm_device *dev, int flags,
2102 const char *name, int num_values)
2104 struct drm_property *property = NULL;
2106 property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
2111 property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
2112 if (!property->values)
2116 drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
2117 property->flags = flags;
2118 property->num_values = num_values;
2119 INIT_LIST_HEAD(&property->enum_blob_list);
2122 strncpy(property->name, name, DRM_PROP_NAME_LEN);
2124 list_add_tail(&property->head, &dev->mode_config.property_list);
2130 EXPORT_SYMBOL(drm_property_create);
2132 int drm_property_add_enum(struct drm_property *property, int index,
2133 uint64_t value, const char *name)
2135 struct drm_property_enum *prop_enum;
2137 if (!(property->flags & DRM_MODE_PROP_ENUM))
2140 if (!list_empty(&property->enum_blob_list)) {
2141 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
2142 if (prop_enum->value == value) {
2143 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2144 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2150 prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
2154 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
2155 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
2156 prop_enum->value = value;
2158 property->values[index] = value;
2159 list_add_tail(&prop_enum->head, &property->enum_blob_list);
2162 EXPORT_SYMBOL(drm_property_add_enum);
2164 void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
2166 struct drm_property_enum *prop_enum, *pt;
2168 list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
2169 list_del(&prop_enum->head);
2173 if (property->num_values)
2174 kfree(property->values);
2175 drm_mode_object_put(dev, &property->base);
2176 list_del(&property->head);
2179 EXPORT_SYMBOL(drm_property_destroy);
2181 int drm_connector_attach_property(struct drm_connector *connector,
2182 struct drm_property *property, uint64_t init_val)
2186 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
2187 if (connector->property_ids[i] == 0) {
2188 connector->property_ids[i] = property->base.id;
2189 connector->property_values[i] = init_val;
2194 if (i == DRM_CONNECTOR_MAX_PROPERTY)
2198 EXPORT_SYMBOL(drm_connector_attach_property);
2200 int drm_connector_property_set_value(struct drm_connector *connector,
2201 struct drm_property *property, uint64_t value)
2205 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
2206 if (connector->property_ids[i] == property->base.id) {
2207 connector->property_values[i] = value;
2212 if (i == DRM_CONNECTOR_MAX_PROPERTY)
2216 EXPORT_SYMBOL(drm_connector_property_set_value);
2218 int drm_connector_property_get_value(struct drm_connector *connector,
2219 struct drm_property *property, uint64_t *val)
2223 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
2224 if (connector->property_ids[i] == property->base.id) {
2225 *val = connector->property_values[i];
2230 if (i == DRM_CONNECTOR_MAX_PROPERTY)
2234 EXPORT_SYMBOL(drm_connector_property_get_value);
2236 int drm_mode_getproperty_ioctl(struct drm_device *dev,
2237 void *data, struct drm_file *file_priv)
2239 struct drm_mode_object *obj;
2240 struct drm_mode_get_property *out_resp = data;
2241 struct drm_property *property;
2244 int value_count = 0;
2247 struct drm_property_enum *prop_enum;
2248 struct drm_mode_property_enum __user *enum_ptr;
2249 struct drm_property_blob *prop_blob;
2250 uint32_t *blob_id_ptr;
2251 uint64_t __user *values_ptr;
2252 uint32_t __user *blob_length_ptr;
2254 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2257 mutex_lock(&dev->mode_config.mutex);
2258 obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
2263 property = obj_to_property(obj);
2265 if (property->flags & DRM_MODE_PROP_ENUM) {
2266 list_for_each_entry(prop_enum, &property->enum_blob_list, head)
2268 } else if (property->flags & DRM_MODE_PROP_BLOB) {
2269 list_for_each_entry(prop_blob, &property->enum_blob_list, head)
2273 value_count = property->num_values;
2275 strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
2276 out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
2277 out_resp->flags = property->flags;
2279 if ((out_resp->count_values >= value_count) && value_count) {
2280 values_ptr = (uint64_t *)(unsigned long)out_resp->values_ptr;
2281 for (i = 0; i < value_count; i++) {
2282 if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
2288 out_resp->count_values = value_count;
2290 if (property->flags & DRM_MODE_PROP_ENUM) {
2291 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
2293 enum_ptr = (struct drm_mode_property_enum *)(unsigned long)out_resp->enum_blob_ptr;
2294 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
2296 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
2301 if (copy_to_user(&enum_ptr[copied].name,
2302 &prop_enum->name, DRM_PROP_NAME_LEN)) {
2309 out_resp->count_enum_blobs = enum_count;
2312 if (property->flags & DRM_MODE_PROP_BLOB) {
2313 if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
2315 blob_id_ptr = (uint32_t *)(unsigned long)out_resp->enum_blob_ptr;
2316 blob_length_ptr = (uint32_t *)(unsigned long)out_resp->values_ptr;
2318 list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
2319 if (put_user(prop_blob->base.id, blob_id_ptr + copied)) {
2324 if (put_user(prop_blob->length, blob_length_ptr + copied)) {
2332 out_resp->count_enum_blobs = blob_count;
2335 mutex_unlock(&dev->mode_config.mutex);
2339 static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
2342 struct drm_property_blob *blob;
2344 if (!length || !data)
2347 blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
2351 blob->data = (void *)((char *)blob + sizeof(struct drm_property_blob));
2352 blob->length = length;
2354 memcpy(blob->data, data, length);
2356 drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
2358 list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
2362 static void drm_property_destroy_blob(struct drm_device *dev,
2363 struct drm_property_blob *blob)
2365 drm_mode_object_put(dev, &blob->base);
2366 list_del(&blob->head);
2370 int drm_mode_getblob_ioctl(struct drm_device *dev,
2371 void *data, struct drm_file *file_priv)
2373 struct drm_mode_object *obj;
2374 struct drm_mode_get_blob *out_resp = data;
2375 struct drm_property_blob *blob;
2379 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2382 mutex_lock(&dev->mode_config.mutex);
2383 obj = drm_mode_object_find(dev, out_resp->blob_id, DRM_MODE_OBJECT_BLOB);
2388 blob = obj_to_blob(obj);
2390 if (out_resp->length == blob->length) {
2391 blob_ptr = (void *)(unsigned long)out_resp->data;
2392 if (copy_to_user(blob_ptr, blob->data, blob->length)){
2397 out_resp->length = blob->length;
2400 mutex_unlock(&dev->mode_config.mutex);
2404 int drm_mode_connector_update_edid_property(struct drm_connector *connector,
2407 struct drm_device *dev = connector->dev;
2410 if (connector->edid_blob_ptr)
2411 drm_property_destroy_blob(dev, connector->edid_blob_ptr);
2413 /* Delete edid, when there is none. */
2415 connector->edid_blob_ptr = NULL;
2416 ret = drm_connector_property_set_value(connector, dev->mode_config.edid_property, 0);
2420 size = EDID_LENGTH * (1 + edid->extensions);
2421 connector->edid_blob_ptr = drm_property_create_blob(connector->dev,
2424 ret = drm_connector_property_set_value(connector,
2425 dev->mode_config.edid_property,
2426 connector->edid_blob_ptr->base.id);
2430 EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
2432 int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
2433 void *data, struct drm_file *file_priv)
2435 struct drm_mode_connector_set_property *out_resp = data;
2436 struct drm_mode_object *obj;
2437 struct drm_property *property;
2438 struct drm_connector *connector;
2442 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2445 mutex_lock(&dev->mode_config.mutex);
2447 obj = drm_mode_object_find(dev, out_resp->connector_id, DRM_MODE_OBJECT_CONNECTOR);
2451 connector = obj_to_connector(obj);
2453 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
2454 if (connector->property_ids[i] == out_resp->prop_id)
2458 if (i == DRM_CONNECTOR_MAX_PROPERTY) {
2462 obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
2466 property = obj_to_property(obj);
2468 if (property->flags & DRM_MODE_PROP_IMMUTABLE)
2471 if (property->flags & DRM_MODE_PROP_RANGE) {
2472 if (out_resp->value < property->values[0])
2475 if (out_resp->value > property->values[1])
2479 for (i = 0; i < property->num_values; i++) {
2480 if (property->values[i] == out_resp->value) {
2490 /* Do DPMS ourselves */
2491 if (property == connector->dev->mode_config.dpms_property) {
2492 if (connector->funcs->dpms)
2493 (*connector->funcs->dpms)(connector, (int) out_resp->value);
2495 } else if (connector->funcs->set_property)
2496 ret = connector->funcs->set_property(connector, property, out_resp->value);
2498 /* store the property value if successful */
2500 drm_connector_property_set_value(connector, property, out_resp->value);
2502 mutex_unlock(&dev->mode_config.mutex);
2506 int drm_mode_connector_attach_encoder(struct drm_connector *connector,
2507 struct drm_encoder *encoder)
2511 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
2512 if (connector->encoder_ids[i] == 0) {
2513 connector->encoder_ids[i] = encoder->base.id;
2519 EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
2521 void drm_mode_connector_detach_encoder(struct drm_connector *connector,
2522 struct drm_encoder *encoder)
2525 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
2526 if (connector->encoder_ids[i] == encoder->base.id) {
2527 connector->encoder_ids[i] = 0;
2528 if (connector->encoder == encoder)
2529 connector->encoder = NULL;
2534 EXPORT_SYMBOL(drm_mode_connector_detach_encoder);
2536 bool drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
2539 crtc->gamma_size = gamma_size;
2541 crtc->gamma_store = kzalloc(gamma_size * sizeof(uint16_t) * 3, GFP_KERNEL);
2542 if (!crtc->gamma_store) {
2543 crtc->gamma_size = 0;
2549 EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
2551 int drm_mode_gamma_set_ioctl(struct drm_device *dev,
2552 void *data, struct drm_file *file_priv)
2554 struct drm_mode_crtc_lut *crtc_lut = data;
2555 struct drm_mode_object *obj;
2556 struct drm_crtc *crtc;
2557 void *r_base, *g_base, *b_base;
2561 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2564 mutex_lock(&dev->mode_config.mutex);
2565 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
2570 crtc = obj_to_crtc(obj);
2572 /* memcpy into gamma store */
2573 if (crtc_lut->gamma_size != crtc->gamma_size) {
2578 size = crtc_lut->gamma_size * (sizeof(uint16_t));
2579 r_base = crtc->gamma_store;
2580 if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
2585 g_base = r_base + size;
2586 if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
2591 b_base = g_base + size;
2592 if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
2597 crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
2600 mutex_unlock(&dev->mode_config.mutex);
2605 int drm_mode_gamma_get_ioctl(struct drm_device *dev,
2606 void *data, struct drm_file *file_priv)
2608 struct drm_mode_crtc_lut *crtc_lut = data;
2609 struct drm_mode_object *obj;
2610 struct drm_crtc *crtc;
2611 void *r_base, *g_base, *b_base;
2615 if (!drm_core_check_feature(dev, DRIVER_MODESET))
2618 mutex_lock(&dev->mode_config.mutex);
2619 obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
2624 crtc = obj_to_crtc(obj);
2626 /* memcpy into gamma store */
2627 if (crtc_lut->gamma_size != crtc->gamma_size) {
2632 size = crtc_lut->gamma_size * (sizeof(uint16_t));
2633 r_base = crtc->gamma_store;
2634 if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
2639 g_base = r_base + size;
2640 if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
2645 b_base = g_base + size;
2646 if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
2651 mutex_unlock(&dev->mode_config.mutex);
2655 int drm_mode_page_flip_ioctl(struct drm_device *dev,
2656 void *data, struct drm_file *file_priv)
2658 struct drm_mode_crtc_page_flip *page_flip = data;
2659 struct drm_mode_object *obj;
2660 struct drm_crtc *crtc;
2661 struct drm_framebuffer *fb;
2662 struct drm_pending_vblank_event *e = NULL;
2663 unsigned long flags;
2666 if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
2667 page_flip->reserved != 0)
2670 mutex_lock(&dev->mode_config.mutex);
2671 obj = drm_mode_object_find(dev, page_flip->crtc_id, DRM_MODE_OBJECT_CRTC);
2674 crtc = obj_to_crtc(obj);
2676 if (crtc->fb == NULL) {
2677 /* The framebuffer is currently unbound, presumably
2678 * due to a hotplug event, that userspace has not
2685 if (crtc->funcs->page_flip == NULL)
2688 obj = drm_mode_object_find(dev, page_flip->fb_id, DRM_MODE_OBJECT_FB);
2691 fb = obj_to_fb(obj);
2693 if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
2695 spin_lock_irqsave(&dev->event_lock, flags);
2696 if (file_priv->event_space < sizeof e->event) {
2697 spin_unlock_irqrestore(&dev->event_lock, flags);
2700 file_priv->event_space -= sizeof e->event;
2701 spin_unlock_irqrestore(&dev->event_lock, flags);
2703 e = kzalloc(sizeof *e, GFP_KERNEL);
2705 spin_lock_irqsave(&dev->event_lock, flags);
2706 file_priv->event_space += sizeof e->event;
2707 spin_unlock_irqrestore(&dev->event_lock, flags);
2711 e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
2712 e->event.base.length = sizeof e->event;
2713 e->event.user_data = page_flip->user_data;
2714 e->base.event = &e->event.base;
2715 e->base.file_priv = file_priv;
2717 (void (*) (struct drm_pending_event *)) kfree;
2720 ret = crtc->funcs->page_flip(crtc, fb, e);
2722 spin_lock_irqsave(&dev->event_lock, flags);
2723 file_priv->event_space += sizeof e->event;
2724 spin_unlock_irqrestore(&dev->event_lock, flags);
2729 mutex_unlock(&dev->mode_config.mutex);
2733 void drm_mode_config_reset(struct drm_device *dev)
2735 struct drm_crtc *crtc;
2736 struct drm_encoder *encoder;
2737 struct drm_connector *connector;
2739 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
2740 if (crtc->funcs->reset)
2741 crtc->funcs->reset(crtc);
2743 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
2744 if (encoder->funcs->reset)
2745 encoder->funcs->reset(encoder);
2747 list_for_each_entry(connector, &dev->mode_config.connector_list, head)
2748 if (connector->funcs->reset)
2749 connector->funcs->reset(connector);
2751 EXPORT_SYMBOL(drm_mode_config_reset);
2753 int drm_mode_create_dumb_ioctl(struct drm_device *dev,
2754 void *data, struct drm_file *file_priv)
2756 struct drm_mode_create_dumb *args = data;
2758 if (!dev->driver->dumb_create)
2760 return dev->driver->dumb_create(file_priv, dev, args);
2763 int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
2764 void *data, struct drm_file *file_priv)
2766 struct drm_mode_map_dumb *args = data;
2768 /* call driver ioctl to get mmap offset */
2769 if (!dev->driver->dumb_map_offset)
2772 return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
2775 int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
2776 void *data, struct drm_file *file_priv)
2778 struct drm_mode_destroy_dumb *args = data;
2780 if (!dev->driver->dumb_destroy)
2783 return dev->driver->dumb_destroy(file_priv, dev, args->handle);