Home | History | Annotate | Line # | Download | only in drm
drm_crtc.c revision 1.1.1.1.2.11
      1 /*
      2  * Copyright (c) 2006-2008 Intel Corporation
      3  * Copyright (c) 2007 Dave Airlie <airlied (at) linux.ie>
      4  * Copyright (c) 2008 Red Hat Inc.
      5  *
      6  * DRM core CRTC related functions
      7  *
      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.
     17  *
     18  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
     19  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
     20  * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
     21  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
     22  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
     23  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
     24  * OF THIS SOFTWARE.
     25  *
     26  * Authors:
     27  *      Keith Packard
     28  *	Eric Anholt <eric (at) anholt.net>
     29  *      Dave Airlie <airlied (at) linux.ie>
     30  *      Jesse Barnes <jesse.barnes (at) intel.com>
     31  */
     32 #include <linux/err.h>
     33 #include <linux/spinlock.h>
     34 #include <linux/list.h>
     35 #include <linux/slab.h>
     36 #include <linux/export.h>
     37 #include <drm/drmP.h>
     38 #include <drm/drm_crtc.h>
     39 #include <drm/drm_edid.h>
     40 #include <drm/drm_fourcc.h>
     41 
     42 /* Avoid boilerplate.  I'm tired of typing. */
     43 #ifdef __NetBSD__
     44 /* XXX Does nobody build this code with -Wwrite-strings?  */
     45 #define DRM_ENUM_NAME_FN(fnname, list)				\
     46 	const char *fnname(int val)				\
     47 	{							\
     48 		int i;						\
     49 		for (i = 0; i < ARRAY_SIZE(list); i++) {	\
     50 			if (list[i].type == val)		\
     51 				return list[i].name;		\
     52 		}						\
     53 		return "(unknown)";				\
     54 	}
     55 #else
     56 #define DRM_ENUM_NAME_FN(fnname, list)				\
     57 	char *fnname(int val)					\
     58 	{							\
     59 		int i;						\
     60 		for (i = 0; i < ARRAY_SIZE(list); i++) {	\
     61 			if (list[i].type == val)		\
     62 				return list[i].name;		\
     63 		}						\
     64 		return "(unknown)";				\
     65 	}
     66 #endif
     67 
     68 /*
     69  * Global properties
     70  */
     71 static struct drm_prop_enum_list drm_dpms_enum_list[] =
     72 {	{ DRM_MODE_DPMS_ON, "On" },
     73 	{ DRM_MODE_DPMS_STANDBY, "Standby" },
     74 	{ DRM_MODE_DPMS_SUSPEND, "Suspend" },
     75 	{ DRM_MODE_DPMS_OFF, "Off" }
     76 };
     77 
     78 DRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list)
     79 
     80 /*
     81  * Optional properties
     82  */
     83 static struct drm_prop_enum_list drm_scaling_mode_enum_list[] =
     84 {
     85 	{ DRM_MODE_SCALE_NONE, "None" },
     86 	{ DRM_MODE_SCALE_FULLSCREEN, "Full" },
     87 	{ DRM_MODE_SCALE_CENTER, "Center" },
     88 	{ DRM_MODE_SCALE_ASPECT, "Full aspect" },
     89 };
     90 
     91 static struct drm_prop_enum_list drm_dithering_mode_enum_list[] =
     92 {
     93 	{ DRM_MODE_DITHERING_OFF, "Off" },
     94 	{ DRM_MODE_DITHERING_ON, "On" },
     95 	{ DRM_MODE_DITHERING_AUTO, "Automatic" },
     96 };
     97 
     98 /*
     99  * Non-global properties, but "required" for certain connectors.
    100  */
    101 static struct drm_prop_enum_list drm_dvi_i_select_enum_list[] =
    102 {
    103 	{ DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
    104 	{ DRM_MODE_SUBCONNECTOR_DVID,      "DVI-D"     }, /* DVI-I  */
    105 	{ DRM_MODE_SUBCONNECTOR_DVIA,      "DVI-A"     }, /* DVI-I  */
    106 };
    107 
    108 DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list)
    109 
    110 static struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list[] =
    111 {
    112 	{ DRM_MODE_SUBCONNECTOR_Unknown,   "Unknown"   }, /* DVI-I and TV-out */
    113 	{ DRM_MODE_SUBCONNECTOR_DVID,      "DVI-D"     }, /* DVI-I  */
    114 	{ DRM_MODE_SUBCONNECTOR_DVIA,      "DVI-A"     }, /* DVI-I  */
    115 };
    116 
    117 DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name,
    118 		 drm_dvi_i_subconnector_enum_list)
    119 
    120 static struct drm_prop_enum_list drm_tv_select_enum_list[] =
    121 {
    122 	{ DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* 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 */
    127 };
    128 
    129 DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
    130 
    131 static struct drm_prop_enum_list drm_tv_subconnector_enum_list[] =
    132 {
    133 	{ DRM_MODE_SUBCONNECTOR_Unknown,   "Unknown"   }, /* DVI-I and TV-out */
    134 	{ DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
    135 	{ DRM_MODE_SUBCONNECTOR_SVIDEO,    "SVIDEO"    }, /* TV-out */
    136 	{ DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
    137 	{ DRM_MODE_SUBCONNECTOR_SCART,     "SCART"     }, /* TV-out */
    138 };
    139 
    140 DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,
    141 		 drm_tv_subconnector_enum_list)
    142 
    143 static struct drm_prop_enum_list drm_dirty_info_enum_list[] = {
    144 	{ DRM_MODE_DIRTY_OFF,      "Off"      },
    145 	{ DRM_MODE_DIRTY_ON,       "On"       },
    146 	{ DRM_MODE_DIRTY_ANNOTATE, "Annotate" },
    147 };
    148 
    149 #ifndef __NetBSD__
    150 /* XXX Doesn't seem to be used...  */
    151 DRM_ENUM_NAME_FN(drm_get_dirty_info_name,
    152 		 drm_dirty_info_enum_list)
    153 #endif
    154 
    155 struct drm_conn_prop_enum_list {
    156 	int type;
    157 #ifdef __NetBSD__
    158 	const char *name;
    159 #else
    160 	char *name;
    161 #endif
    162 	int count;
    163 };
    164 
    165 /*
    166  * Connector and encoder types.
    167  */
    168 static struct drm_conn_prop_enum_list drm_connector_enum_list[] =
    169 {	{ DRM_MODE_CONNECTOR_Unknown, "Unknown", 0 },
    170 	{ DRM_MODE_CONNECTOR_VGA, "VGA", 0 },
    171 	{ DRM_MODE_CONNECTOR_DVII, "DVI-I", 0 },
    172 	{ DRM_MODE_CONNECTOR_DVID, "DVI-D", 0 },
    173 	{ DRM_MODE_CONNECTOR_DVIA, "DVI-A", 0 },
    174 	{ DRM_MODE_CONNECTOR_Composite, "Composite", 0 },
    175 	{ DRM_MODE_CONNECTOR_SVIDEO, "SVIDEO", 0 },
    176 	{ DRM_MODE_CONNECTOR_LVDS, "LVDS", 0 },
    177 	{ DRM_MODE_CONNECTOR_Component, "Component", 0 },
    178 	{ DRM_MODE_CONNECTOR_9PinDIN, "DIN", 0 },
    179 	{ DRM_MODE_CONNECTOR_DisplayPort, "DP", 0 },
    180 	{ DRM_MODE_CONNECTOR_HDMIA, "HDMI-A", 0 },
    181 	{ DRM_MODE_CONNECTOR_HDMIB, "HDMI-B", 0 },
    182 	{ DRM_MODE_CONNECTOR_TV, "TV", 0 },
    183 	{ DRM_MODE_CONNECTOR_eDP, "eDP", 0 },
    184 	{ DRM_MODE_CONNECTOR_VIRTUAL, "Virtual", 0},
    185 };
    186 
    187 static struct drm_prop_enum_list drm_encoder_enum_list[] =
    188 {	{ DRM_MODE_ENCODER_NONE, "None" },
    189 	{ DRM_MODE_ENCODER_DAC, "DAC" },
    190 	{ DRM_MODE_ENCODER_TMDS, "TMDS" },
    191 	{ DRM_MODE_ENCODER_LVDS, "LVDS" },
    192 	{ DRM_MODE_ENCODER_TVDAC, "TV" },
    193 	{ DRM_MODE_ENCODER_VIRTUAL, "Virtual" },
    194 };
    195 
    196 char *drm_get_encoder_name(struct drm_encoder *encoder)
    197 {
    198 	static char buf[32];
    199 
    200 	snprintf(buf, 32, "%s-%d",
    201 		 drm_encoder_enum_list[encoder->encoder_type].name,
    202 		 encoder->base.id);
    203 	return buf;
    204 }
    205 EXPORT_SYMBOL(drm_get_encoder_name);
    206 
    207 char *drm_get_connector_name(struct drm_connector *connector)
    208 {
    209 	static char buf[32];
    210 
    211 	snprintf(buf, 32, "%s-%d",
    212 		 drm_connector_enum_list[connector->connector_type].name,
    213 		 connector->connector_type_id);
    214 	return buf;
    215 }
    216 EXPORT_SYMBOL(drm_get_connector_name);
    217 
    218 #ifndef __NetBSD__
    219 char *drm_get_connector_status_name(enum drm_connector_status status)
    220 {
    221 	if (status == connector_status_connected)
    222 		return "connected";
    223 	else if (status == connector_status_disconnected)
    224 		return "disconnected";
    225 	else
    226 		return "unknown";
    227 }
    228 #endif
    229 
    230 /**
    231  * drm_mode_object_get - allocate a new identifier
    232  * @dev: DRM device
    233  * @ptr: object pointer, used to generate unique ID
    234  * @type: object type
    235  *
    236  * LOCKING:
    237  *
    238  * Create a unique identifier based on @ptr in @dev's identifier space.  Used
    239  * for tracking modes, CRTCs and connectors.
    240  *
    241  * RETURNS:
    242  * New unique (relative to other objects in @dev) integer identifier for the
    243  * object.
    244  */
    245 static int drm_mode_object_get(struct drm_device *dev,
    246 			       struct drm_mode_object *obj, uint32_t obj_type)
    247 {
    248 	int new_id = 0;
    249 	int ret;
    250 
    251 again:
    252 	if (idr_pre_get(&dev->mode_config.crtc_idr, GFP_KERNEL) == 0) {
    253 		DRM_ERROR("Ran out memory getting a mode number\n");
    254 		return -ENOMEM;
    255 	}
    256 
    257 	mutex_lock(&dev->mode_config.idr_mutex);
    258 	ret = idr_get_new_above(&dev->mode_config.crtc_idr, obj, 1, &new_id);
    259 	mutex_unlock(&dev->mode_config.idr_mutex);
    260 	if (ret == -EAGAIN)
    261 		goto again;
    262 	else if (ret)
    263 		return ret;
    264 
    265 	obj->id = new_id;
    266 	obj->type = obj_type;
    267 	return 0;
    268 }
    269 
    270 /**
    271  * drm_mode_object_put - free an identifer
    272  * @dev: DRM device
    273  * @id: ID to free
    274  *
    275  * LOCKING:
    276  * Caller must hold DRM mode_config lock.
    277  *
    278  * Free @id from @dev's unique identifier pool.
    279  */
    280 static void drm_mode_object_put(struct drm_device *dev,
    281 				struct drm_mode_object *object)
    282 {
    283 	mutex_lock(&dev->mode_config.idr_mutex);
    284 	idr_remove(&dev->mode_config.crtc_idr, object->id);
    285 	mutex_unlock(&dev->mode_config.idr_mutex);
    286 }
    287 
    288 struct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
    289 		uint32_t id, uint32_t type)
    290 {
    291 	struct drm_mode_object *obj = NULL;
    292 
    293 	mutex_lock(&dev->mode_config.idr_mutex);
    294 	obj = idr_find(&dev->mode_config.crtc_idr, id);
    295 	if (!obj || (obj->type != type) || (obj->id != id))
    296 		obj = NULL;
    297 	mutex_unlock(&dev->mode_config.idr_mutex);
    298 
    299 	return obj;
    300 }
    301 EXPORT_SYMBOL(drm_mode_object_find);
    302 
    303 /**
    304  * drm_framebuffer_init - initialize a framebuffer
    305  * @dev: DRM device
    306  *
    307  * LOCKING:
    308  * Caller must hold mode config lock.
    309  *
    310  * Allocates an ID for the framebuffer's parent mode object, sets its mode
    311  * functions & device file and adds it to the master fd list.
    312  *
    313  * RETURNS:
    314  * Zero on success, error code on failure.
    315  */
    316 int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
    317 			 const struct drm_framebuffer_funcs *funcs)
    318 {
    319 	int ret;
    320 
    321 	kref_init(&fb->refcount);
    322 
    323 	ret = drm_mode_object_get(dev, &fb->base, DRM_MODE_OBJECT_FB);
    324 	if (ret)
    325 		return ret;
    326 
    327 	fb->dev = dev;
    328 	fb->funcs = funcs;
    329 	dev->mode_config.num_fb++;
    330 	list_add(&fb->head, &dev->mode_config.fb_list);
    331 
    332 	return 0;
    333 }
    334 EXPORT_SYMBOL(drm_framebuffer_init);
    335 
    336 static void drm_framebuffer_free(struct kref *kref)
    337 {
    338 	struct drm_framebuffer *fb =
    339 			container_of(kref, struct drm_framebuffer, refcount);
    340 	fb->funcs->destroy(fb);
    341 }
    342 
    343 /**
    344  * drm_framebuffer_unreference - unref a framebuffer
    345  *
    346  * LOCKING:
    347  * Caller must hold mode config lock.
    348  */
    349 void drm_framebuffer_unreference(struct drm_framebuffer *fb)
    350 {
    351 	struct drm_device *dev = fb->dev;
    352 	DRM_DEBUG("FB ID: %d\n", fb->base.id);
    353 	WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
    354 	kref_put(&fb->refcount, drm_framebuffer_free);
    355 }
    356 EXPORT_SYMBOL(drm_framebuffer_unreference);
    357 
    358 /**
    359  * drm_framebuffer_reference - incr the fb refcnt
    360  */
    361 void drm_framebuffer_reference(struct drm_framebuffer *fb)
    362 {
    363 	DRM_DEBUG("FB ID: %d\n", fb->base.id);
    364 	kref_get(&fb->refcount);
    365 }
    366 EXPORT_SYMBOL(drm_framebuffer_reference);
    367 
    368 /**
    369  * drm_framebuffer_cleanup - remove a framebuffer object
    370  * @fb: framebuffer to remove
    371  *
    372  * LOCKING:
    373  * Caller must hold mode config lock.
    374  *
    375  * Scans all the CRTCs in @dev's mode_config.  If they're using @fb, removes
    376  * it, setting it to NULL.
    377  */
    378 void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
    379 {
    380 	struct drm_device *dev = fb->dev;
    381 	/*
    382 	 * This could be moved to drm_framebuffer_remove(), but for
    383 	 * debugging is nice to keep around the list of fb's that are
    384 	 * no longer associated w/ a drm_file but are not unreferenced
    385 	 * yet.  (i915 and omapdrm have debugfs files which will show
    386 	 * this.)
    387 	 */
    388 	drm_mode_object_put(dev, &fb->base);
    389 	list_del(&fb->head);
    390 	dev->mode_config.num_fb--;
    391 }
    392 EXPORT_SYMBOL(drm_framebuffer_cleanup);
    393 
    394 /**
    395  * drm_framebuffer_remove - remove and unreference a framebuffer object
    396  * @fb: framebuffer to remove
    397  *
    398  * LOCKING:
    399  * Caller must hold mode config lock.
    400  *
    401  * Scans all the CRTCs and planes in @dev's mode_config.  If they're
    402  * using @fb, removes it, setting it to NULL.
    403  */
    404 void drm_framebuffer_remove(struct drm_framebuffer *fb)
    405 {
    406 	struct drm_device *dev = fb->dev;
    407 	struct drm_crtc *crtc;
    408 	struct drm_plane *plane;
    409 	struct drm_mode_set set;
    410 	int ret;
    411 
    412 	/* remove from any CRTC */
    413 	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
    414 		if (crtc->fb == fb) {
    415 			/* should turn off the crtc */
    416 			memset(&set, 0, sizeof(struct drm_mode_set));
    417 			set.crtc = crtc;
    418 			set.fb = NULL;
    419 			ret = crtc->funcs->set_config(&set);
    420 			if (ret)
    421 				DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
    422 		}
    423 	}
    424 
    425 	list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
    426 		if (plane->fb == fb) {
    427 			/* should turn off the crtc */
    428 			ret = plane->funcs->disable_plane(plane);
    429 			if (ret)
    430 				DRM_ERROR("failed to disable plane with busy fb\n");
    431 			/* disconnect the plane from the fb and crtc: */
    432 			plane->fb = NULL;
    433 			plane->crtc = NULL;
    434 		}
    435 	}
    436 
    437 	list_del(&fb->filp_head);
    438 
    439 	drm_framebuffer_unreference(fb);
    440 }
    441 EXPORT_SYMBOL(drm_framebuffer_remove);
    442 
    443 /**
    444  * drm_crtc_init - Initialise a new CRTC object
    445  * @dev: DRM device
    446  * @crtc: CRTC object to init
    447  * @funcs: callbacks for the new CRTC
    448  *
    449  * LOCKING:
    450  * Takes mode_config lock.
    451  *
    452  * Inits a new object created as base part of an driver crtc object.
    453  *
    454  * RETURNS:
    455  * Zero on success, error code on failure.
    456  */
    457 int drm_crtc_init(struct drm_device *dev, struct drm_crtc *crtc,
    458 		   const struct drm_crtc_funcs *funcs)
    459 {
    460 	int ret;
    461 
    462 	crtc->dev = dev;
    463 	crtc->funcs = funcs;
    464 	crtc->invert_dimensions = false;
    465 
    466 	mutex_lock(&dev->mode_config.mutex);
    467 
    468 	ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
    469 	if (ret)
    470 		goto out;
    471 
    472 	crtc->base.properties = &crtc->properties;
    473 
    474 	list_add_tail(&crtc->head, &dev->mode_config.crtc_list);
    475 	dev->mode_config.num_crtc++;
    476 
    477  out:
    478 	mutex_unlock(&dev->mode_config.mutex);
    479 
    480 	return ret;
    481 }
    482 EXPORT_SYMBOL(drm_crtc_init);
    483 
    484 /**
    485  * drm_crtc_cleanup - Cleans up the core crtc usage.
    486  * @crtc: CRTC to cleanup
    487  *
    488  * LOCKING:
    489  * Caller must hold mode config lock.
    490  *
    491  * Cleanup @crtc. Removes from drm modesetting space
    492  * does NOT free object, caller does that.
    493  */
    494 void drm_crtc_cleanup(struct drm_crtc *crtc)
    495 {
    496 	struct drm_device *dev = crtc->dev;
    497 
    498 	kfree(crtc->gamma_store);
    499 	crtc->gamma_store = NULL;
    500 
    501 	drm_mode_object_put(dev, &crtc->base);
    502 	list_del(&crtc->head);
    503 	dev->mode_config.num_crtc--;
    504 }
    505 EXPORT_SYMBOL(drm_crtc_cleanup);
    506 
    507 /**
    508  * drm_mode_probed_add - add a mode to a connector's probed mode list
    509  * @connector: connector the new mode
    510  * @mode: mode data
    511  *
    512  * LOCKING:
    513  * Caller must hold mode config lock.
    514  *
    515  * Add @mode to @connector's mode list for later use.
    516  */
    517 void drm_mode_probed_add(struct drm_connector *connector,
    518 			 struct drm_display_mode *mode)
    519 {
    520 	list_add(&mode->head, &connector->probed_modes);
    521 }
    522 EXPORT_SYMBOL(drm_mode_probed_add);
    523 
    524 /**
    525  * drm_mode_remove - remove and free a mode
    526  * @connector: connector list to modify
    527  * @mode: mode to remove
    528  *
    529  * LOCKING:
    530  * Caller must hold mode config lock.
    531  *
    532  * Remove @mode from @connector's mode list, then free it.
    533  */
    534 void drm_mode_remove(struct drm_connector *connector,
    535 		     struct drm_display_mode *mode)
    536 {
    537 	list_del(&mode->head);
    538 	drm_mode_destroy(connector->dev, mode);
    539 }
    540 EXPORT_SYMBOL(drm_mode_remove);
    541 
    542 /**
    543  * drm_connector_init - Init a preallocated connector
    544  * @dev: DRM device
    545  * @connector: the connector to init
    546  * @funcs: callbacks for this connector
    547  * @name: user visible name of the connector
    548  *
    549  * LOCKING:
    550  * Takes mode config lock.
    551  *
    552  * Initialises a preallocated connector. Connectors should be
    553  * subclassed as part of driver connector objects.
    554  *
    555  * RETURNS:
    556  * Zero on success, error code on failure.
    557  */
    558 int drm_connector_init(struct drm_device *dev,
    559 		       struct drm_connector *connector,
    560 		       const struct drm_connector_funcs *funcs,
    561 		       int connector_type)
    562 {
    563 	int ret;
    564 
    565 	mutex_lock(&dev->mode_config.mutex);
    566 
    567 	ret = drm_mode_object_get(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR);
    568 	if (ret)
    569 		goto out;
    570 
    571 	connector->base.properties = &connector->properties;
    572 	connector->dev = dev;
    573 	connector->funcs = funcs;
    574 	connector->connector_type = connector_type;
    575 	connector->connector_type_id =
    576 		++drm_connector_enum_list[connector_type].count; /* TODO */
    577 	INIT_LIST_HEAD(&connector->user_modes);
    578 	INIT_LIST_HEAD(&connector->probed_modes);
    579 	INIT_LIST_HEAD(&connector->modes);
    580 	connector->edid_blob_ptr = NULL;
    581 	connector->status = connector_status_unknown;
    582 
    583 	list_add_tail(&connector->head, &dev->mode_config.connector_list);
    584 	dev->mode_config.num_connector++;
    585 
    586 	if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
    587 		drm_object_attach_property(&connector->base,
    588 					      dev->mode_config.edid_property,
    589 					      0);
    590 
    591 	drm_object_attach_property(&connector->base,
    592 				      dev->mode_config.dpms_property, 0);
    593 
    594  out:
    595 	mutex_unlock(&dev->mode_config.mutex);
    596 
    597 	return ret;
    598 }
    599 EXPORT_SYMBOL(drm_connector_init);
    600 
    601 /**
    602  * drm_connector_cleanup - cleans up an initialised connector
    603  * @connector: connector to cleanup
    604  *
    605  * LOCKING:
    606  * Takes mode config lock.
    607  *
    608  * Cleans up the connector but doesn't free the object.
    609  */
    610 void drm_connector_cleanup(struct drm_connector *connector)
    611 {
    612 	struct drm_device *dev = connector->dev;
    613 	struct drm_display_mode *mode, *t;
    614 
    615 	list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
    616 		drm_mode_remove(connector, mode);
    617 
    618 	list_for_each_entry_safe(mode, t, &connector->modes, head)
    619 		drm_mode_remove(connector, mode);
    620 
    621 	list_for_each_entry_safe(mode, t, &connector->user_modes, head)
    622 		drm_mode_remove(connector, mode);
    623 
    624 	mutex_lock(&dev->mode_config.mutex);
    625 	drm_mode_object_put(dev, &connector->base);
    626 	list_del(&connector->head);
    627 	dev->mode_config.num_connector--;
    628 	mutex_unlock(&dev->mode_config.mutex);
    629 }
    630 EXPORT_SYMBOL(drm_connector_cleanup);
    631 
    632 void drm_connector_unplug_all(struct drm_device *dev)
    633 {
    634 	struct drm_connector *connector;
    635 
    636 	/* taking the mode config mutex ends up in a clash with sysfs */
    637 	list_for_each_entry(connector, &dev->mode_config.connector_list, head)
    638 		drm_sysfs_connector_remove(connector);
    639 
    640 }
    641 EXPORT_SYMBOL(drm_connector_unplug_all);
    642 
    643 int drm_encoder_init(struct drm_device *dev,
    644 		      struct drm_encoder *encoder,
    645 		      const struct drm_encoder_funcs *funcs,
    646 		      int encoder_type)
    647 {
    648 	int ret;
    649 
    650 	mutex_lock(&dev->mode_config.mutex);
    651 
    652 	ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
    653 	if (ret)
    654 		goto out;
    655 
    656 	encoder->dev = dev;
    657 	encoder->encoder_type = encoder_type;
    658 	encoder->funcs = funcs;
    659 
    660 	list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
    661 	dev->mode_config.num_encoder++;
    662 
    663  out:
    664 	mutex_unlock(&dev->mode_config.mutex);
    665 
    666 	return ret;
    667 }
    668 EXPORT_SYMBOL(drm_encoder_init);
    669 
    670 void drm_encoder_cleanup(struct drm_encoder *encoder)
    671 {
    672 	struct drm_device *dev = encoder->dev;
    673 	mutex_lock(&dev->mode_config.mutex);
    674 	drm_mode_object_put(dev, &encoder->base);
    675 	list_del(&encoder->head);
    676 	dev->mode_config.num_encoder--;
    677 	mutex_unlock(&dev->mode_config.mutex);
    678 }
    679 EXPORT_SYMBOL(drm_encoder_cleanup);
    680 
    681 int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
    682 		   unsigned long possible_crtcs,
    683 		   const struct drm_plane_funcs *funcs,
    684 		   const uint32_t *formats, uint32_t format_count,
    685 		   bool priv)
    686 {
    687 	int ret;
    688 
    689 	mutex_lock(&dev->mode_config.mutex);
    690 
    691 	ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
    692 	if (ret)
    693 		goto out;
    694 
    695 	plane->base.properties = &plane->properties;
    696 	plane->dev = dev;
    697 	plane->funcs = funcs;
    698 	plane->format_types = kmalloc(sizeof(uint32_t) * format_count,
    699 				      GFP_KERNEL);
    700 	if (!plane->format_types) {
    701 		DRM_DEBUG_KMS("out of memory when allocating plane\n");
    702 		drm_mode_object_put(dev, &plane->base);
    703 		ret = -ENOMEM;
    704 		goto out;
    705 	}
    706 
    707 	memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
    708 	plane->format_count = format_count;
    709 	plane->possible_crtcs = possible_crtcs;
    710 
    711 	/* private planes are not exposed to userspace, but depending on
    712 	 * display hardware, might be convenient to allow sharing programming
    713 	 * for the scanout engine with the crtc implementation.
    714 	 */
    715 	if (!priv) {
    716 		list_add_tail(&plane->head, &dev->mode_config.plane_list);
    717 		dev->mode_config.num_plane++;
    718 	} else {
    719 		INIT_LIST_HEAD(&plane->head);
    720 	}
    721 
    722  out:
    723 	mutex_unlock(&dev->mode_config.mutex);
    724 
    725 	return ret;
    726 }
    727 EXPORT_SYMBOL(drm_plane_init);
    728 
    729 void drm_plane_cleanup(struct drm_plane *plane)
    730 {
    731 	struct drm_device *dev = plane->dev;
    732 
    733 	mutex_lock(&dev->mode_config.mutex);
    734 	kfree(plane->format_types);
    735 	drm_mode_object_put(dev, &plane->base);
    736 	/* if not added to a list, it must be a private plane */
    737 	if (!list_empty(&plane->head)) {
    738 		list_del(&plane->head);
    739 		dev->mode_config.num_plane--;
    740 	}
    741 	mutex_unlock(&dev->mode_config.mutex);
    742 }
    743 EXPORT_SYMBOL(drm_plane_cleanup);
    744 
    745 /**
    746  * drm_mode_create - create a new display mode
    747  * @dev: DRM device
    748  *
    749  * LOCKING:
    750  * Caller must hold DRM mode_config lock.
    751  *
    752  * Create a new drm_display_mode, give it an ID, and return it.
    753  *
    754  * RETURNS:
    755  * Pointer to new mode on success, NULL on error.
    756  */
    757 struct drm_display_mode *drm_mode_create(struct drm_device *dev)
    758 {
    759 	struct drm_display_mode *nmode;
    760 
    761 	nmode = kzalloc(sizeof(struct drm_display_mode), GFP_KERNEL);
    762 	if (!nmode)
    763 		return NULL;
    764 
    765 	if (drm_mode_object_get(dev, &nmode->base, DRM_MODE_OBJECT_MODE)) {
    766 		kfree(nmode);
    767 		return NULL;
    768 	}
    769 
    770 	return nmode;
    771 }
    772 EXPORT_SYMBOL(drm_mode_create);
    773 
    774 /**
    775  * drm_mode_destroy - remove a mode
    776  * @dev: DRM device
    777  * @mode: mode to remove
    778  *
    779  * LOCKING:
    780  * Caller must hold mode config lock.
    781  *
    782  * Free @mode's unique identifier, then free it.
    783  */
    784 void drm_mode_destroy(struct drm_device *dev, struct drm_display_mode *mode)
    785 {
    786 	if (!mode)
    787 		return;
    788 
    789 	drm_mode_object_put(dev, &mode->base);
    790 
    791 	kfree(mode);
    792 }
    793 EXPORT_SYMBOL(drm_mode_destroy);
    794 
    795 static int drm_mode_create_standard_connector_properties(struct drm_device *dev)
    796 {
    797 	struct drm_property *edid;
    798 	struct drm_property *dpms;
    799 
    800 	/*
    801 	 * Standard properties (apply to all connectors)
    802 	 */
    803 	edid = drm_property_create(dev, DRM_MODE_PROP_BLOB |
    804 				   DRM_MODE_PROP_IMMUTABLE,
    805 				   "EDID", 0);
    806 	dev->mode_config.edid_property = edid;
    807 
    808 	dpms = drm_property_create_enum(dev, 0,
    809 				   "DPMS", drm_dpms_enum_list,
    810 				   ARRAY_SIZE(drm_dpms_enum_list));
    811 	dev->mode_config.dpms_property = dpms;
    812 
    813 	return 0;
    814 }
    815 
    816 /**
    817  * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
    818  * @dev: DRM device
    819  *
    820  * Called by a driver the first time a DVI-I connector is made.
    821  */
    822 int drm_mode_create_dvi_i_properties(struct drm_device *dev)
    823 {
    824 	struct drm_property *dvi_i_selector;
    825 	struct drm_property *dvi_i_subconnector;
    826 
    827 	if (dev->mode_config.dvi_i_select_subconnector_property)
    828 		return 0;
    829 
    830 	dvi_i_selector =
    831 		drm_property_create_enum(dev, 0,
    832 				    "select subconnector",
    833 				    drm_dvi_i_select_enum_list,
    834 				    ARRAY_SIZE(drm_dvi_i_select_enum_list));
    835 	dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
    836 
    837 	dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
    838 				    "subconnector",
    839 				    drm_dvi_i_subconnector_enum_list,
    840 				    ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
    841 	dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
    842 
    843 	return 0;
    844 }
    845 EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
    846 
    847 /**
    848  * drm_create_tv_properties - create TV specific connector properties
    849  * @dev: DRM device
    850  * @num_modes: number of different TV formats (modes) supported
    851  * @modes: array of pointers to strings containing name of each format
    852  *
    853  * Called by a driver's TV initialization routine, this function creates
    854  * the TV specific connector properties for a given device.  Caller is
    855  * responsible for allocating a list of format names and passing them to
    856  * this routine.
    857  */
    858 int drm_mode_create_tv_properties(struct drm_device *dev, int num_modes,
    859 				  char *modes[])
    860 {
    861 	struct drm_property *tv_selector;
    862 	struct drm_property *tv_subconnector;
    863 	int i;
    864 
    865 	if (dev->mode_config.tv_select_subconnector_property)
    866 		return 0;
    867 
    868 	/*
    869 	 * Basic connector properties
    870 	 */
    871 	tv_selector = drm_property_create_enum(dev, 0,
    872 					  "select subconnector",
    873 					  drm_tv_select_enum_list,
    874 					  ARRAY_SIZE(drm_tv_select_enum_list));
    875 	dev->mode_config.tv_select_subconnector_property = tv_selector;
    876 
    877 	tv_subconnector =
    878 		drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
    879 				    "subconnector",
    880 				    drm_tv_subconnector_enum_list,
    881 				    ARRAY_SIZE(drm_tv_subconnector_enum_list));
    882 	dev->mode_config.tv_subconnector_property = tv_subconnector;
    883 
    884 	/*
    885 	 * Other, TV specific properties: margins & TV modes.
    886 	 */
    887 	dev->mode_config.tv_left_margin_property =
    888 		drm_property_create_range(dev, 0, "left margin", 0, 100);
    889 
    890 	dev->mode_config.tv_right_margin_property =
    891 		drm_property_create_range(dev, 0, "right margin", 0, 100);
    892 
    893 	dev->mode_config.tv_top_margin_property =
    894 		drm_property_create_range(dev, 0, "top margin", 0, 100);
    895 
    896 	dev->mode_config.tv_bottom_margin_property =
    897 		drm_property_create_range(dev, 0, "bottom margin", 0, 100);
    898 
    899 	dev->mode_config.tv_mode_property =
    900 		drm_property_create(dev, DRM_MODE_PROP_ENUM,
    901 				    "mode", num_modes);
    902 	for (i = 0; i < num_modes; i++)
    903 		drm_property_add_enum(dev->mode_config.tv_mode_property, i,
    904 				      i, modes[i]);
    905 
    906 	dev->mode_config.tv_brightness_property =
    907 		drm_property_create_range(dev, 0, "brightness", 0, 100);
    908 
    909 	dev->mode_config.tv_contrast_property =
    910 		drm_property_create_range(dev, 0, "contrast", 0, 100);
    911 
    912 	dev->mode_config.tv_flicker_reduction_property =
    913 		drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
    914 
    915 	dev->mode_config.tv_overscan_property =
    916 		drm_property_create_range(dev, 0, "overscan", 0, 100);
    917 
    918 	dev->mode_config.tv_saturation_property =
    919 		drm_property_create_range(dev, 0, "saturation", 0, 100);
    920 
    921 	dev->mode_config.tv_hue_property =
    922 		drm_property_create_range(dev, 0, "hue", 0, 100);
    923 
    924 	return 0;
    925 }
    926 EXPORT_SYMBOL(drm_mode_create_tv_properties);
    927 
    928 /**
    929  * drm_mode_create_scaling_mode_property - create scaling mode property
    930  * @dev: DRM device
    931  *
    932  * Called by a driver the first time it's needed, must be attached to desired
    933  * connectors.
    934  */
    935 int drm_mode_create_scaling_mode_property(struct drm_device *dev)
    936 {
    937 	struct drm_property *scaling_mode;
    938 
    939 	if (dev->mode_config.scaling_mode_property)
    940 		return 0;
    941 
    942 	scaling_mode =
    943 		drm_property_create_enum(dev, 0, "scaling mode",
    944 				drm_scaling_mode_enum_list,
    945 				    ARRAY_SIZE(drm_scaling_mode_enum_list));
    946 
    947 	dev->mode_config.scaling_mode_property = scaling_mode;
    948 
    949 	return 0;
    950 }
    951 EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
    952 
    953 /**
    954  * drm_mode_create_dithering_property - create dithering property
    955  * @dev: DRM device
    956  *
    957  * Called by a driver the first time it's needed, must be attached to desired
    958  * connectors.
    959  */
    960 int drm_mode_create_dithering_property(struct drm_device *dev)
    961 {
    962 	struct drm_property *dithering_mode;
    963 
    964 	if (dev->mode_config.dithering_mode_property)
    965 		return 0;
    966 
    967 	dithering_mode =
    968 		drm_property_create_enum(dev, 0, "dithering",
    969 				drm_dithering_mode_enum_list,
    970 				    ARRAY_SIZE(drm_dithering_mode_enum_list));
    971 	dev->mode_config.dithering_mode_property = dithering_mode;
    972 
    973 	return 0;
    974 }
    975 EXPORT_SYMBOL(drm_mode_create_dithering_property);
    976 
    977 /**
    978  * drm_mode_create_dirty_property - create dirty property
    979  * @dev: DRM device
    980  *
    981  * Called by a driver the first time it's needed, must be attached to desired
    982  * connectors.
    983  */
    984 int drm_mode_create_dirty_info_property(struct drm_device *dev)
    985 {
    986 	struct drm_property *dirty_info;
    987 
    988 	if (dev->mode_config.dirty_info_property)
    989 		return 0;
    990 
    991 	dirty_info =
    992 		drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
    993 				    "dirty",
    994 				    drm_dirty_info_enum_list,
    995 				    ARRAY_SIZE(drm_dirty_info_enum_list));
    996 	dev->mode_config.dirty_info_property = dirty_info;
    997 
    998 	return 0;
    999 }
   1000 EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
   1001 
   1002 /**
   1003  * drm_mode_config_init - initialize DRM mode_configuration structure
   1004  * @dev: DRM device
   1005  *
   1006  * LOCKING:
   1007  * None, should happen single threaded at init time.
   1008  *
   1009  * Initialize @dev's mode_config structure, used for tracking the graphics
   1010  * configuration of @dev.
   1011  */
   1012 void drm_mode_config_init(struct drm_device *dev)
   1013 {
   1014 #ifdef __NetBSD__
   1015 	linux_mutex_init(&dev->mode_config.mutex);
   1016 	linux_mutex_init(&dev->mode_config.idr_mutex);
   1017 #else
   1018 	mutex_init(&dev->mode_config.mutex);
   1019 	mutex_init(&dev->mode_config.idr_mutex);
   1020 #endif
   1021 	INIT_LIST_HEAD(&dev->mode_config.fb_list);
   1022 	INIT_LIST_HEAD(&dev->mode_config.crtc_list);
   1023 	INIT_LIST_HEAD(&dev->mode_config.connector_list);
   1024 	INIT_LIST_HEAD(&dev->mode_config.encoder_list);
   1025 	INIT_LIST_HEAD(&dev->mode_config.property_list);
   1026 	INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
   1027 	INIT_LIST_HEAD(&dev->mode_config.plane_list);
   1028 	idr_init(&dev->mode_config.crtc_idr);
   1029 
   1030 	mutex_lock(&dev->mode_config.mutex);
   1031 	drm_mode_create_standard_connector_properties(dev);
   1032 	mutex_unlock(&dev->mode_config.mutex);
   1033 
   1034 	/* Just to be sure */
   1035 	dev->mode_config.num_fb = 0;
   1036 	dev->mode_config.num_connector = 0;
   1037 	dev->mode_config.num_crtc = 0;
   1038 	dev->mode_config.num_encoder = 0;
   1039 }
   1040 EXPORT_SYMBOL(drm_mode_config_init);
   1041 
   1042 #ifdef __NetBSD__
   1043 static
   1044 #endif
   1045 int drm_mode_group_init(struct drm_device *dev, struct drm_mode_group *group)
   1046 {
   1047 	uint32_t total_objects = 0;
   1048 
   1049 	total_objects += dev->mode_config.num_crtc;
   1050 	total_objects += dev->mode_config.num_connector;
   1051 	total_objects += dev->mode_config.num_encoder;
   1052 
   1053 	group->id_list = kzalloc(total_objects * sizeof(uint32_t), GFP_KERNEL);
   1054 	if (!group->id_list)
   1055 		return -ENOMEM;
   1056 
   1057 	group->num_crtcs = 0;
   1058 	group->num_connectors = 0;
   1059 	group->num_encoders = 0;
   1060 	return 0;
   1061 }
   1062 
   1063 int drm_mode_group_init_legacy_group(struct drm_device *dev,
   1064 				     struct drm_mode_group *group)
   1065 {
   1066 	struct drm_crtc *crtc;
   1067 	struct drm_encoder *encoder;
   1068 	struct drm_connector *connector;
   1069 	int ret;
   1070 
   1071 	if ((ret = drm_mode_group_init(dev, group)))
   1072 		return ret;
   1073 
   1074 	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
   1075 		group->id_list[group->num_crtcs++] = crtc->base.id;
   1076 
   1077 	list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
   1078 		group->id_list[group->num_crtcs + group->num_encoders++] =
   1079 		encoder->base.id;
   1080 
   1081 	list_for_each_entry(connector, &dev->mode_config.connector_list, head)
   1082 		group->id_list[group->num_crtcs + group->num_encoders +
   1083 			       group->num_connectors++] = connector->base.id;
   1084 
   1085 	return 0;
   1086 }
   1087 EXPORT_SYMBOL(drm_mode_group_init_legacy_group);
   1088 
   1089 /**
   1090  * drm_mode_config_cleanup - free up DRM mode_config info
   1091  * @dev: DRM device
   1092  *
   1093  * LOCKING:
   1094  * Caller must hold mode config lock.
   1095  *
   1096  * Free up all the connectors and CRTCs associated with this DRM device, then
   1097  * free up the framebuffers and associated buffer objects.
   1098  *
   1099  * FIXME: cleanup any dangling user buffer objects too
   1100  */
   1101 void drm_mode_config_cleanup(struct drm_device *dev)
   1102 {
   1103 	struct drm_connector *connector, *ot;
   1104 	struct drm_crtc *crtc, *ct;
   1105 	struct drm_encoder *encoder, *enct;
   1106 	struct drm_framebuffer *fb, *fbt;
   1107 	struct drm_property *property, *pt;
   1108 	struct drm_plane *plane, *plt;
   1109 
   1110 	list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
   1111 				 head) {
   1112 		encoder->funcs->destroy(encoder);
   1113 	}
   1114 
   1115 	list_for_each_entry_safe(connector, ot,
   1116 				 &dev->mode_config.connector_list, head) {
   1117 		connector->funcs->destroy(connector);
   1118 	}
   1119 
   1120 	list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
   1121 				 head) {
   1122 		drm_property_destroy(dev, property);
   1123 	}
   1124 
   1125 	list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
   1126 		drm_framebuffer_remove(fb);
   1127 	}
   1128 
   1129 	list_for_each_entry_safe(plane, plt, &dev->mode_config.plane_list,
   1130 				 head) {
   1131 		plane->funcs->destroy(plane);
   1132 	}
   1133 
   1134 	list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
   1135 		crtc->funcs->destroy(crtc);
   1136 	}
   1137 
   1138 	idr_remove_all(&dev->mode_config.crtc_idr);
   1139 	idr_destroy(&dev->mode_config.crtc_idr);
   1140 }
   1141 EXPORT_SYMBOL(drm_mode_config_cleanup);
   1142 
   1143 /**
   1144  * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
   1145  * @out: drm_mode_modeinfo struct to return to the user
   1146  * @in: drm_display_mode to use
   1147  *
   1148  * LOCKING:
   1149  * None.
   1150  *
   1151  * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
   1152  * the user.
   1153  */
   1154 static void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out,
   1155 				      const struct drm_display_mode *in)
   1156 {
   1157 	WARN(in->hdisplay > USHRT_MAX || in->hsync_start > USHRT_MAX ||
   1158 	     in->hsync_end > USHRT_MAX || in->htotal > USHRT_MAX ||
   1159 	     in->hskew > USHRT_MAX || in->vdisplay > USHRT_MAX ||
   1160 	     in->vsync_start > USHRT_MAX || in->vsync_end > USHRT_MAX ||
   1161 	     in->vtotal > USHRT_MAX || in->vscan > USHRT_MAX,
   1162 	     "timing values too large for mode info\n");
   1163 
   1164 	out->clock = in->clock;
   1165 	out->hdisplay = in->hdisplay;
   1166 	out->hsync_start = in->hsync_start;
   1167 	out->hsync_end = in->hsync_end;
   1168 	out->htotal = in->htotal;
   1169 	out->hskew = in->hskew;
   1170 	out->vdisplay = in->vdisplay;
   1171 	out->vsync_start = in->vsync_start;
   1172 	out->vsync_end = in->vsync_end;
   1173 	out->vtotal = in->vtotal;
   1174 	out->vscan = in->vscan;
   1175 	out->vrefresh = in->vrefresh;
   1176 	out->flags = in->flags;
   1177 	out->type = in->type;
   1178 	strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
   1179 	out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
   1180 }
   1181 
   1182 /**
   1183  * drm_crtc_convert_to_umode - convert a modeinfo into a drm_display_mode
   1184  * @out: drm_display_mode to return to the user
   1185  * @in: drm_mode_modeinfo to use
   1186  *
   1187  * LOCKING:
   1188  * None.
   1189  *
   1190  * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
   1191  * the caller.
   1192  *
   1193  * RETURNS:
   1194  * Zero on success, errno on failure.
   1195  */
   1196 static int drm_crtc_convert_umode(struct drm_display_mode *out,
   1197 				  const struct drm_mode_modeinfo *in)
   1198 {
   1199 	if (in->clock > INT_MAX || in->vrefresh > INT_MAX)
   1200 		return -ERANGE;
   1201 
   1202 	out->clock = in->clock;
   1203 	out->hdisplay = in->hdisplay;
   1204 	out->hsync_start = in->hsync_start;
   1205 	out->hsync_end = in->hsync_end;
   1206 	out->htotal = in->htotal;
   1207 	out->hskew = in->hskew;
   1208 	out->vdisplay = in->vdisplay;
   1209 	out->vsync_start = in->vsync_start;
   1210 	out->vsync_end = in->vsync_end;
   1211 	out->vtotal = in->vtotal;
   1212 	out->vscan = in->vscan;
   1213 	out->vrefresh = in->vrefresh;
   1214 	out->flags = in->flags;
   1215 	out->type = in->type;
   1216 	strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
   1217 	out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
   1218 
   1219 	return 0;
   1220 }
   1221 
   1222 /**
   1223  * drm_mode_getresources - get graphics configuration
   1224  * @inode: inode from the ioctl
   1225  * @filp: file * from the ioctl
   1226  * @cmd: cmd from ioctl
   1227  * @arg: arg from ioctl
   1228  *
   1229  * LOCKING:
   1230  * Takes mode config lock.
   1231  *
   1232  * Construct a set of configuration description structures and return
   1233  * them to the user, including CRTC, connector and framebuffer configuration.
   1234  *
   1235  * Called by the user via ioctl.
   1236  *
   1237  * RETURNS:
   1238  * Zero on success, errno on failure.
   1239  */
   1240 int drm_mode_getresources(struct drm_device *dev, void *data,
   1241 			  struct drm_file *file_priv)
   1242 {
   1243 	struct drm_mode_card_res *card_res = data;
   1244 	struct list_head *lh;
   1245 	struct drm_framebuffer *fb;
   1246 	struct drm_connector *connector;
   1247 	struct drm_crtc *crtc;
   1248 	struct drm_encoder *encoder;
   1249 	int ret = 0;
   1250 	int connector_count = 0;
   1251 	int crtc_count = 0;
   1252 	int fb_count = 0;
   1253 	int encoder_count = 0;
   1254 	int copied = 0, i;
   1255 	uint32_t __user *fb_id;
   1256 	uint32_t __user *crtc_id;
   1257 	uint32_t __user *connector_id;
   1258 	uint32_t __user *encoder_id;
   1259 	struct drm_mode_group *mode_group;
   1260 
   1261 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
   1262 		return -EINVAL;
   1263 
   1264 	mutex_lock(&dev->mode_config.mutex);
   1265 
   1266 	/*
   1267 	 * For the non-control nodes we need to limit the list of resources
   1268 	 * by IDs in the group list for this node
   1269 	 */
   1270 	list_for_each(lh, &file_priv->fbs)
   1271 		fb_count++;
   1272 
   1273 	mode_group = &file_priv->master->minor->mode_group;
   1274 	if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
   1275 
   1276 		list_for_each(lh, &dev->mode_config.crtc_list)
   1277 			crtc_count++;
   1278 
   1279 		list_for_each(lh, &dev->mode_config.connector_list)
   1280 			connector_count++;
   1281 
   1282 		list_for_each(lh, &dev->mode_config.encoder_list)
   1283 			encoder_count++;
   1284 	} else {
   1285 
   1286 		crtc_count = mode_group->num_crtcs;
   1287 		connector_count = mode_group->num_connectors;
   1288 		encoder_count = mode_group->num_encoders;
   1289 	}
   1290 
   1291 	card_res->max_height = dev->mode_config.max_height;
   1292 	card_res->min_height = dev->mode_config.min_height;
   1293 	card_res->max_width = dev->mode_config.max_width;
   1294 	card_res->min_width = dev->mode_config.min_width;
   1295 
   1296 	/* handle this in 4 parts */
   1297 	/* FBs */
   1298 	if (card_res->count_fbs >= fb_count) {
   1299 		copied = 0;
   1300 		fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
   1301 		list_for_each_entry(fb, &file_priv->fbs, filp_head) {
   1302 			if (put_user(fb->base.id, fb_id + copied)) {
   1303 				ret = -EFAULT;
   1304 				goto out;
   1305 			}
   1306 			copied++;
   1307 		}
   1308 	}
   1309 	card_res->count_fbs = fb_count;
   1310 
   1311 	/* CRTCs */
   1312 	if (card_res->count_crtcs >= crtc_count) {
   1313 		copied = 0;
   1314 		crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
   1315 		if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
   1316 			list_for_each_entry(crtc, &dev->mode_config.crtc_list,
   1317 					    head) {
   1318 				DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
   1319 				if (put_user(crtc->base.id, crtc_id + copied)) {
   1320 					ret = -EFAULT;
   1321 					goto out;
   1322 				}
   1323 				copied++;
   1324 			}
   1325 		} else {
   1326 			for (i = 0; i < mode_group->num_crtcs; i++) {
   1327 				if (put_user(mode_group->id_list[i],
   1328 					     crtc_id + copied)) {
   1329 					ret = -EFAULT;
   1330 					goto out;
   1331 				}
   1332 				copied++;
   1333 			}
   1334 		}
   1335 	}
   1336 	card_res->count_crtcs = crtc_count;
   1337 
   1338 	/* Encoders */
   1339 	if (card_res->count_encoders >= encoder_count) {
   1340 		copied = 0;
   1341 		encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
   1342 		if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
   1343 			list_for_each_entry(encoder,
   1344 					    &dev->mode_config.encoder_list,
   1345 					    head) {
   1346 				DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id,
   1347 						drm_get_encoder_name(encoder));
   1348 				if (put_user(encoder->base.id, encoder_id +
   1349 					     copied)) {
   1350 					ret = -EFAULT;
   1351 					goto out;
   1352 				}
   1353 				copied++;
   1354 			}
   1355 		} else {
   1356 			for (i = mode_group->num_crtcs; i < mode_group->num_crtcs + mode_group->num_encoders; i++) {
   1357 				if (put_user(mode_group->id_list[i],
   1358 					     encoder_id + copied)) {
   1359 					ret = -EFAULT;
   1360 					goto out;
   1361 				}
   1362 				copied++;
   1363 			}
   1364 
   1365 		}
   1366 	}
   1367 	card_res->count_encoders = encoder_count;
   1368 
   1369 	/* Connectors */
   1370 	if (card_res->count_connectors >= connector_count) {
   1371 		copied = 0;
   1372 		connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
   1373 		if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
   1374 			list_for_each_entry(connector,
   1375 					    &dev->mode_config.connector_list,
   1376 					    head) {
   1377 				DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
   1378 					connector->base.id,
   1379 					drm_get_connector_name(connector));
   1380 				if (put_user(connector->base.id,
   1381 					     connector_id + copied)) {
   1382 					ret = -EFAULT;
   1383 					goto out;
   1384 				}
   1385 				copied++;
   1386 			}
   1387 		} else {
   1388 			int start = mode_group->num_crtcs +
   1389 				mode_group->num_encoders;
   1390 			for (i = start; i < start + mode_group->num_connectors; i++) {
   1391 				if (put_user(mode_group->id_list[i],
   1392 					     connector_id + copied)) {
   1393 					ret = -EFAULT;
   1394 					goto out;
   1395 				}
   1396 				copied++;
   1397 			}
   1398 		}
   1399 	}
   1400 	card_res->count_connectors = connector_count;
   1401 
   1402 	DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs,
   1403 		  card_res->count_connectors, card_res->count_encoders);
   1404 
   1405 out:
   1406 	mutex_unlock(&dev->mode_config.mutex);
   1407 	return ret;
   1408 }
   1409 
   1410 /**
   1411  * drm_mode_getcrtc - get CRTC configuration
   1412  * @inode: inode from the ioctl
   1413  * @filp: file * from the ioctl
   1414  * @cmd: cmd from ioctl
   1415  * @arg: arg from ioctl
   1416  *
   1417  * LOCKING:
   1418  * Takes mode config lock.
   1419  *
   1420  * Construct a CRTC configuration structure to return to the user.
   1421  *
   1422  * Called by the user via ioctl.
   1423  *
   1424  * RETURNS:
   1425  * Zero on success, errno on failure.
   1426  */
   1427 int drm_mode_getcrtc(struct drm_device *dev,
   1428 		     void *data, struct drm_file *file_priv)
   1429 {
   1430 	struct drm_mode_crtc *crtc_resp = data;
   1431 	struct drm_crtc *crtc;
   1432 	struct drm_mode_object *obj;
   1433 	int ret = 0;
   1434 
   1435 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
   1436 		return -EINVAL;
   1437 
   1438 	mutex_lock(&dev->mode_config.mutex);
   1439 
   1440 	obj = drm_mode_object_find(dev, crtc_resp->crtc_id,
   1441 				   DRM_MODE_OBJECT_CRTC);
   1442 	if (!obj) {
   1443 		ret = -EINVAL;
   1444 		goto out;
   1445 	}
   1446 	crtc = obj_to_crtc(obj);
   1447 
   1448 	crtc_resp->x = crtc->x;
   1449 	crtc_resp->y = crtc->y;
   1450 	crtc_resp->gamma_size = crtc->gamma_size;
   1451 	if (crtc->fb)
   1452 		crtc_resp->fb_id = crtc->fb->base.id;
   1453 	else
   1454 		crtc_resp->fb_id = 0;
   1455 
   1456 	if (crtc->enabled) {
   1457 
   1458 		drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
   1459 		crtc_resp->mode_valid = 1;
   1460 
   1461 	} else {
   1462 		crtc_resp->mode_valid = 0;
   1463 	}
   1464 
   1465 out:
   1466 	mutex_unlock(&dev->mode_config.mutex);
   1467 	return ret;
   1468 }
   1469 
   1470 /**
   1471  * drm_mode_getconnector - get connector configuration
   1472  * @inode: inode from the ioctl
   1473  * @filp: file * from the ioctl
   1474  * @cmd: cmd from ioctl
   1475  * @arg: arg from ioctl
   1476  *
   1477  * LOCKING:
   1478  * Takes mode config lock.
   1479  *
   1480  * Construct a connector configuration structure to return to the user.
   1481  *
   1482  * Called by the user via ioctl.
   1483  *
   1484  * RETURNS:
   1485  * Zero on success, errno on failure.
   1486  */
   1487 int drm_mode_getconnector(struct drm_device *dev, void *data,
   1488 			  struct drm_file *file_priv)
   1489 {
   1490 	struct drm_mode_get_connector *out_resp = data;
   1491 	struct drm_mode_object *obj;
   1492 	struct drm_connector *connector;
   1493 	struct drm_display_mode *mode;
   1494 	int mode_count = 0;
   1495 	int props_count = 0;
   1496 	int encoders_count = 0;
   1497 	int ret = 0;
   1498 	int copied = 0;
   1499 	int i;
   1500 	struct drm_mode_modeinfo u_mode;
   1501 	struct drm_mode_modeinfo __user *mode_ptr;
   1502 	uint32_t __user *prop_ptr;
   1503 	uint64_t __user *prop_values;
   1504 	uint32_t __user *encoder_ptr;
   1505 
   1506 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
   1507 		return -EINVAL;
   1508 
   1509 	memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
   1510 
   1511 	DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id);
   1512 
   1513 	mutex_lock(&dev->mode_config.mutex);
   1514 
   1515 	obj = drm_mode_object_find(dev, out_resp->connector_id,
   1516 				   DRM_MODE_OBJECT_CONNECTOR);
   1517 	if (!obj) {
   1518 		ret = -EINVAL;
   1519 		goto out;
   1520 	}
   1521 	connector = obj_to_connector(obj);
   1522 
   1523 	props_count = connector->properties.count;
   1524 
   1525 	for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
   1526 		if (connector->encoder_ids[i] != 0) {
   1527 			encoders_count++;
   1528 		}
   1529 	}
   1530 
   1531 	if (out_resp->count_modes == 0) {
   1532 		connector->funcs->fill_modes(connector,
   1533 					     dev->mode_config.max_width,
   1534 					     dev->mode_config.max_height);
   1535 	}
   1536 
   1537 	/* delayed so we get modes regardless of pre-fill_modes state */
   1538 	list_for_each_entry(mode, &connector->modes, head)
   1539 		mode_count++;
   1540 
   1541 	out_resp->connector_id = connector->base.id;
   1542 	out_resp->connector_type = connector->connector_type;
   1543 	out_resp->connector_type_id = connector->connector_type_id;
   1544 	out_resp->mm_width = connector->display_info.width_mm;
   1545 	out_resp->mm_height = connector->display_info.height_mm;
   1546 	out_resp->subpixel = connector->display_info.subpixel_order;
   1547 	out_resp->connection = connector->status;
   1548 	if (connector->encoder)
   1549 		out_resp->encoder_id = connector->encoder->base.id;
   1550 	else
   1551 		out_resp->encoder_id = 0;
   1552 
   1553 	/*
   1554 	 * This ioctl is called twice, once to determine how much space is
   1555 	 * needed, and the 2nd time to fill it.
   1556 	 */
   1557 	if ((out_resp->count_modes >= mode_count) && mode_count) {
   1558 		copied = 0;
   1559 		mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
   1560 		list_for_each_entry(mode, &connector->modes, head) {
   1561 			drm_crtc_convert_to_umode(&u_mode, mode);
   1562 			if (copy_to_user(mode_ptr + copied,
   1563 					 &u_mode, sizeof(u_mode))) {
   1564 				ret = -EFAULT;
   1565 				goto out;
   1566 			}
   1567 			copied++;
   1568 		}
   1569 	}
   1570 	out_resp->count_modes = mode_count;
   1571 
   1572 	if ((out_resp->count_props >= props_count) && props_count) {
   1573 		copied = 0;
   1574 		prop_ptr = (uint32_t __user *)(unsigned long)(out_resp->props_ptr);
   1575 		prop_values = (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr);
   1576 		for (i = 0; i < connector->properties.count; i++) {
   1577 			if (put_user(connector->properties.ids[i],
   1578 				     prop_ptr + copied)) {
   1579 				ret = -EFAULT;
   1580 				goto out;
   1581 			}
   1582 
   1583 			if (put_user(connector->properties.values[i],
   1584 				     prop_values + copied)) {
   1585 				ret = -EFAULT;
   1586 				goto out;
   1587 			}
   1588 			copied++;
   1589 		}
   1590 	}
   1591 	out_resp->count_props = props_count;
   1592 
   1593 	if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
   1594 		copied = 0;
   1595 		encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
   1596 		for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
   1597 			if (connector->encoder_ids[i] != 0) {
   1598 				if (put_user(connector->encoder_ids[i],
   1599 					     encoder_ptr + copied)) {
   1600 					ret = -EFAULT;
   1601 					goto out;
   1602 				}
   1603 				copied++;
   1604 			}
   1605 		}
   1606 	}
   1607 	out_resp->count_encoders = encoders_count;
   1608 
   1609 out:
   1610 	mutex_unlock(&dev->mode_config.mutex);
   1611 	return ret;
   1612 }
   1613 
   1614 int drm_mode_getencoder(struct drm_device *dev, void *data,
   1615 			struct drm_file *file_priv)
   1616 {
   1617 	struct drm_mode_get_encoder *enc_resp = data;
   1618 	struct drm_mode_object *obj;
   1619 	struct drm_encoder *encoder;
   1620 	int ret = 0;
   1621 
   1622 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
   1623 		return -EINVAL;
   1624 
   1625 	mutex_lock(&dev->mode_config.mutex);
   1626 	obj = drm_mode_object_find(dev, enc_resp->encoder_id,
   1627 				   DRM_MODE_OBJECT_ENCODER);
   1628 	if (!obj) {
   1629 		ret = -EINVAL;
   1630 		goto out;
   1631 	}
   1632 	encoder = obj_to_encoder(obj);
   1633 
   1634 	if (encoder->crtc)
   1635 		enc_resp->crtc_id = encoder->crtc->base.id;
   1636 	else
   1637 		enc_resp->crtc_id = 0;
   1638 	enc_resp->encoder_type = encoder->encoder_type;
   1639 	enc_resp->encoder_id = encoder->base.id;
   1640 	enc_resp->possible_crtcs = encoder->possible_crtcs;
   1641 	enc_resp->possible_clones = encoder->possible_clones;
   1642 
   1643 out:
   1644 	mutex_unlock(&dev->mode_config.mutex);
   1645 	return ret;
   1646 }
   1647 
   1648 /**
   1649  * drm_mode_getplane_res - get plane info
   1650  * @dev: DRM device
   1651  * @data: ioctl data
   1652  * @file_priv: DRM file info
   1653  *
   1654  * LOCKING:
   1655  * Takes mode config lock.
   1656  *
   1657  * Return an plane count and set of IDs.
   1658  */
   1659 int drm_mode_getplane_res(struct drm_device *dev, void *data,
   1660 			    struct drm_file *file_priv)
   1661 {
   1662 	struct drm_mode_get_plane_res *plane_resp = data;
   1663 	struct drm_mode_config *config;
   1664 	struct drm_plane *plane;
   1665 	uint32_t __user *plane_ptr;
   1666 	int copied = 0, ret = 0;
   1667 
   1668 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
   1669 		return -EINVAL;
   1670 
   1671 	mutex_lock(&dev->mode_config.mutex);
   1672 	config = &dev->mode_config;
   1673 
   1674 	/*
   1675 	 * This ioctl is called twice, once to determine how much space is
   1676 	 * needed, and the 2nd time to fill it.
   1677 	 */
   1678 	if (config->num_plane &&
   1679 	    (plane_resp->count_planes >= config->num_plane)) {
   1680 		plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
   1681 
   1682 		list_for_each_entry(plane, &config->plane_list, head) {
   1683 			if (put_user(plane->base.id, plane_ptr + copied)) {
   1684 				ret = -EFAULT;
   1685 				goto out;
   1686 			}
   1687 			copied++;
   1688 		}
   1689 	}
   1690 	plane_resp->count_planes = config->num_plane;
   1691 
   1692 out:
   1693 	mutex_unlock(&dev->mode_config.mutex);
   1694 	return ret;
   1695 }
   1696 
   1697 /**
   1698  * drm_mode_getplane - get plane info
   1699  * @dev: DRM device
   1700  * @data: ioctl data
   1701  * @file_priv: DRM file info
   1702  *
   1703  * LOCKING:
   1704  * Takes mode config lock.
   1705  *
   1706  * Return plane info, including formats supported, gamma size, any
   1707  * current fb, etc.
   1708  */
   1709 int drm_mode_getplane(struct drm_device *dev, void *data,
   1710 			struct drm_file *file_priv)
   1711 {
   1712 	struct drm_mode_get_plane *plane_resp = data;
   1713 	struct drm_mode_object *obj;
   1714 	struct drm_plane *plane;
   1715 	uint32_t __user *format_ptr;
   1716 	int ret = 0;
   1717 
   1718 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
   1719 		return -EINVAL;
   1720 
   1721 	mutex_lock(&dev->mode_config.mutex);
   1722 	obj = drm_mode_object_find(dev, plane_resp->plane_id,
   1723 				   DRM_MODE_OBJECT_PLANE);
   1724 	if (!obj) {
   1725 		ret = -ENOENT;
   1726 		goto out;
   1727 	}
   1728 	plane = obj_to_plane(obj);
   1729 
   1730 	if (plane->crtc)
   1731 		plane_resp->crtc_id = plane->crtc->base.id;
   1732 	else
   1733 		plane_resp->crtc_id = 0;
   1734 
   1735 	if (plane->fb)
   1736 		plane_resp->fb_id = plane->fb->base.id;
   1737 	else
   1738 		plane_resp->fb_id = 0;
   1739 
   1740 	plane_resp->plane_id = plane->base.id;
   1741 	plane_resp->possible_crtcs = plane->possible_crtcs;
   1742 	plane_resp->gamma_size = plane->gamma_size;
   1743 
   1744 	/*
   1745 	 * This ioctl is called twice, once to determine how much space is
   1746 	 * needed, and the 2nd time to fill it.
   1747 	 */
   1748 	if (plane->format_count &&
   1749 	    (plane_resp->count_format_types >= plane->format_count)) {
   1750 		format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
   1751 		if (copy_to_user(format_ptr,
   1752 				 plane->format_types,
   1753 				 sizeof(uint32_t) * plane->format_count)) {
   1754 			ret = -EFAULT;
   1755 			goto out;
   1756 		}
   1757 	}
   1758 	plane_resp->count_format_types = plane->format_count;
   1759 
   1760 out:
   1761 	mutex_unlock(&dev->mode_config.mutex);
   1762 	return ret;
   1763 }
   1764 
   1765 /**
   1766  * drm_mode_setplane - set up or tear down an plane
   1767  * @dev: DRM device
   1768  * @data: ioctl data*
   1769  * @file_prive: DRM file info
   1770  *
   1771  * LOCKING:
   1772  * Takes mode config lock.
   1773  *
   1774  * Set plane info, including placement, fb, scaling, and other factors.
   1775  * Or pass a NULL fb to disable.
   1776  */
   1777 int drm_mode_setplane(struct drm_device *dev, void *data,
   1778 			struct drm_file *file_priv)
   1779 {
   1780 	struct drm_mode_set_plane *plane_req = data;
   1781 	struct drm_mode_object *obj;
   1782 	struct drm_plane *plane;
   1783 	struct drm_crtc *crtc;
   1784 	struct drm_framebuffer *fb;
   1785 	int ret = 0;
   1786 	unsigned int fb_width, fb_height;
   1787 	int i;
   1788 
   1789 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
   1790 		return -EINVAL;
   1791 
   1792 	mutex_lock(&dev->mode_config.mutex);
   1793 
   1794 	/*
   1795 	 * First, find the plane, crtc, and fb objects.  If not available,
   1796 	 * we don't bother to call the driver.
   1797 	 */
   1798 	obj = drm_mode_object_find(dev, plane_req->plane_id,
   1799 				   DRM_MODE_OBJECT_PLANE);
   1800 	if (!obj) {
   1801 		DRM_DEBUG_KMS("Unknown plane ID %d\n",
   1802 			      plane_req->plane_id);
   1803 		ret = -ENOENT;
   1804 		goto out;
   1805 	}
   1806 	plane = obj_to_plane(obj);
   1807 
   1808 	/* No fb means shut it down */
   1809 	if (!plane_req->fb_id) {
   1810 		plane->funcs->disable_plane(plane);
   1811 		plane->crtc = NULL;
   1812 		plane->fb = NULL;
   1813 		goto out;
   1814 	}
   1815 
   1816 	obj = drm_mode_object_find(dev, plane_req->crtc_id,
   1817 				   DRM_MODE_OBJECT_CRTC);
   1818 	if (!obj) {
   1819 		DRM_DEBUG_KMS("Unknown crtc ID %d\n",
   1820 			      plane_req->crtc_id);
   1821 		ret = -ENOENT;
   1822 		goto out;
   1823 	}
   1824 	crtc = obj_to_crtc(obj);
   1825 
   1826 	obj = drm_mode_object_find(dev, plane_req->fb_id,
   1827 				   DRM_MODE_OBJECT_FB);
   1828 	if (!obj) {
   1829 		DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
   1830 			      plane_req->fb_id);
   1831 		ret = -ENOENT;
   1832 		goto out;
   1833 	}
   1834 	fb = obj_to_fb(obj);
   1835 
   1836 	/* Check whether this plane supports the fb pixel format. */
   1837 	for (i = 0; i < plane->format_count; i++)
   1838 		if (fb->pixel_format == plane->format_types[i])
   1839 			break;
   1840 	if (i == plane->format_count) {
   1841 		DRM_DEBUG_KMS("Invalid pixel format 0x%08x\n", fb->pixel_format);
   1842 		ret = -EINVAL;
   1843 		goto out;
   1844 	}
   1845 
   1846 	fb_width = fb->width << 16;
   1847 	fb_height = fb->height << 16;
   1848 
   1849 	/* Make sure source coordinates are inside the fb. */
   1850 	if (plane_req->src_w > fb_width ||
   1851 	    plane_req->src_x > fb_width - plane_req->src_w ||
   1852 	    plane_req->src_h > fb_height ||
   1853 	    plane_req->src_y > fb_height - plane_req->src_h) {
   1854 		DRM_DEBUG_KMS("Invalid source coordinates "
   1855 			      "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
   1856 			      plane_req->src_w >> 16,
   1857 			      ((plane_req->src_w & 0xffff) * 15625) >> 10,
   1858 			      plane_req->src_h >> 16,
   1859 			      ((plane_req->src_h & 0xffff) * 15625) >> 10,
   1860 			      plane_req->src_x >> 16,
   1861 			      ((plane_req->src_x & 0xffff) * 15625) >> 10,
   1862 			      plane_req->src_y >> 16,
   1863 			      ((plane_req->src_y & 0xffff) * 15625) >> 10);
   1864 		ret = -ENOSPC;
   1865 		goto out;
   1866 	}
   1867 
   1868 	/* Give drivers some help against integer overflows */
   1869 	if (plane_req->crtc_w > INT_MAX ||
   1870 	    plane_req->crtc_x > INT_MAX - (int32_t) plane_req->crtc_w ||
   1871 	    plane_req->crtc_h > INT_MAX ||
   1872 	    plane_req->crtc_y > INT_MAX - (int32_t) plane_req->crtc_h) {
   1873 		DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
   1874 			      plane_req->crtc_w, plane_req->crtc_h,
   1875 			      plane_req->crtc_x, plane_req->crtc_y);
   1876 		ret = -ERANGE;
   1877 		goto out;
   1878 	}
   1879 
   1880 	ret = plane->funcs->update_plane(plane, crtc, fb,
   1881 					 plane_req->crtc_x, plane_req->crtc_y,
   1882 					 plane_req->crtc_w, plane_req->crtc_h,
   1883 					 plane_req->src_x, plane_req->src_y,
   1884 					 plane_req->src_w, plane_req->src_h);
   1885 	if (!ret) {
   1886 		plane->crtc = crtc;
   1887 		plane->fb = fb;
   1888 	}
   1889 
   1890 out:
   1891 	mutex_unlock(&dev->mode_config.mutex);
   1892 
   1893 	return ret;
   1894 }
   1895 
   1896 /**
   1897  * drm_mode_setcrtc - set CRTC configuration
   1898  * @inode: inode from the ioctl
   1899  * @filp: file * from the ioctl
   1900  * @cmd: cmd from ioctl
   1901  * @arg: arg from ioctl
   1902  *
   1903  * LOCKING:
   1904  * Takes mode config lock.
   1905  *
   1906  * Build a new CRTC configuration based on user request.
   1907  *
   1908  * Called by the user via ioctl.
   1909  *
   1910  * RETURNS:
   1911  * Zero on success, errno on failure.
   1912  */
   1913 int drm_mode_setcrtc(struct drm_device *dev, void *data,
   1914 		     struct drm_file *file_priv)
   1915 {
   1916 	struct drm_mode_config *config = &dev->mode_config;
   1917 	struct drm_mode_crtc *crtc_req = data;
   1918 	struct drm_mode_object *obj;
   1919 	struct drm_crtc *crtc;
   1920 	struct drm_connector **connector_set = NULL, *connector;
   1921 	struct drm_framebuffer *fb = NULL;
   1922 	struct drm_display_mode *mode = NULL;
   1923 	struct drm_mode_set set;
   1924 	uint32_t __user *set_connectors_ptr;
   1925 	int ret;
   1926 	int i;
   1927 
   1928 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
   1929 		return -EINVAL;
   1930 
   1931 	/* For some reason crtc x/y offsets are signed internally. */
   1932 	if (crtc_req->x > INT_MAX || crtc_req->y > INT_MAX)
   1933 		return -ERANGE;
   1934 
   1935 	mutex_lock(&dev->mode_config.mutex);
   1936 	obj = drm_mode_object_find(dev, crtc_req->crtc_id,
   1937 				   DRM_MODE_OBJECT_CRTC);
   1938 	if (!obj) {
   1939 		DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
   1940 		ret = -EINVAL;
   1941 		goto out;
   1942 	}
   1943 	crtc = obj_to_crtc(obj);
   1944 	DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
   1945 
   1946 	if (crtc_req->mode_valid) {
   1947 		int hdisplay, vdisplay;
   1948 		/* If we have a mode we need a framebuffer. */
   1949 		/* If we pass -1, set the mode with the currently bound fb */
   1950 		if (crtc_req->fb_id == -1) {
   1951 			if (!crtc->fb) {
   1952 				DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
   1953 				ret = -EINVAL;
   1954 				goto out;
   1955 			}
   1956 			fb = crtc->fb;
   1957 		} else {
   1958 			obj = drm_mode_object_find(dev, crtc_req->fb_id,
   1959 						   DRM_MODE_OBJECT_FB);
   1960 			if (!obj) {
   1961 				DRM_DEBUG_KMS("Unknown FB ID%d\n",
   1962 						crtc_req->fb_id);
   1963 				ret = -EINVAL;
   1964 				goto out;
   1965 			}
   1966 			fb = obj_to_fb(obj);
   1967 		}
   1968 
   1969 		mode = drm_mode_create(dev);
   1970 		if (!mode) {
   1971 			ret = -ENOMEM;
   1972 			goto out;
   1973 		}
   1974 
   1975 		ret = drm_crtc_convert_umode(mode, &crtc_req->mode);
   1976 		if (ret) {
   1977 			DRM_DEBUG_KMS("Invalid mode\n");
   1978 			goto out;
   1979 		}
   1980 
   1981 		drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
   1982 
   1983 		hdisplay = mode->hdisplay;
   1984 		vdisplay = mode->vdisplay;
   1985 
   1986 		if (crtc->invert_dimensions)
   1987 			swap(hdisplay, vdisplay);
   1988 
   1989 		if (hdisplay > fb->width ||
   1990 		    vdisplay > fb->height ||
   1991 		    crtc_req->x > fb->width - hdisplay ||
   1992 		    crtc_req->y > fb->height - vdisplay) {
   1993 			DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
   1994 				      fb->width, fb->height,
   1995 				      hdisplay, vdisplay, crtc_req->x, crtc_req->y,
   1996 				      crtc->invert_dimensions ? " (inverted)" : "");
   1997 			ret = -ENOSPC;
   1998 			goto out;
   1999 		}
   2000 	}
   2001 
   2002 	if (crtc_req->count_connectors == 0 && mode) {
   2003 		DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
   2004 		ret = -EINVAL;
   2005 		goto out;
   2006 	}
   2007 
   2008 	if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
   2009 		DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
   2010 			  crtc_req->count_connectors);
   2011 		ret = -EINVAL;
   2012 		goto out;
   2013 	}
   2014 
   2015 	if (crtc_req->count_connectors > 0) {
   2016 		u32 out_id;
   2017 
   2018 		/* Avoid unbounded kernel memory allocation */
   2019 		if (crtc_req->count_connectors > config->num_connector) {
   2020 			ret = -EINVAL;
   2021 			goto out;
   2022 		}
   2023 
   2024 		connector_set = kmalloc(crtc_req->count_connectors *
   2025 					sizeof(struct drm_connector *),
   2026 					GFP_KERNEL);
   2027 		if (!connector_set) {
   2028 			ret = -ENOMEM;
   2029 			goto out;
   2030 		}
   2031 
   2032 		for (i = 0; i < crtc_req->count_connectors; i++) {
   2033 			set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
   2034 			if (get_user(out_id, &set_connectors_ptr[i])) {
   2035 				ret = -EFAULT;
   2036 				goto out;
   2037 			}
   2038 
   2039 			obj = drm_mode_object_find(dev, out_id,
   2040 						   DRM_MODE_OBJECT_CONNECTOR);
   2041 			if (!obj) {
   2042 				DRM_DEBUG_KMS("Connector id %d unknown\n",
   2043 						out_id);
   2044 				ret = -EINVAL;
   2045 				goto out;
   2046 			}
   2047 			connector = obj_to_connector(obj);
   2048 			DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
   2049 					connector->base.id,
   2050 					drm_get_connector_name(connector));
   2051 
   2052 			connector_set[i] = connector;
   2053 		}
   2054 	}
   2055 
   2056 	set.crtc = crtc;
   2057 	set.x = crtc_req->x;
   2058 	set.y = crtc_req->y;
   2059 	set.mode = mode;
   2060 	set.connectors = connector_set;
   2061 	set.num_connectors = crtc_req->count_connectors;
   2062 	set.fb = fb;
   2063 	ret = crtc->funcs->set_config(&set);
   2064 
   2065 out:
   2066 	kfree(connector_set);
   2067 	drm_mode_destroy(dev, mode);
   2068 	mutex_unlock(&dev->mode_config.mutex);
   2069 	return ret;
   2070 }
   2071 
   2072 int drm_mode_cursor_ioctl(struct drm_device *dev,
   2073 			void *data, struct drm_file *file_priv)
   2074 {
   2075 	struct drm_mode_cursor *req = data;
   2076 	struct drm_mode_object *obj;
   2077 	struct drm_crtc *crtc;
   2078 	int ret = 0;
   2079 
   2080 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
   2081 		return -EINVAL;
   2082 
   2083 	if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
   2084 		return -EINVAL;
   2085 
   2086 	mutex_lock(&dev->mode_config.mutex);
   2087 	obj = drm_mode_object_find(dev, req->crtc_id, DRM_MODE_OBJECT_CRTC);
   2088 	if (!obj) {
   2089 		DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
   2090 		ret = -EINVAL;
   2091 		goto out;
   2092 	}
   2093 	crtc = obj_to_crtc(obj);
   2094 
   2095 	if (req->flags & DRM_MODE_CURSOR_BO) {
   2096 		if (!crtc->funcs->cursor_set) {
   2097 			ret = -ENXIO;
   2098 			goto out;
   2099 		}
   2100 		/* Turns off the cursor if handle is 0 */
   2101 		ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
   2102 					      req->width, req->height);
   2103 	}
   2104 
   2105 	if (req->flags & DRM_MODE_CURSOR_MOVE) {
   2106 		if (crtc->funcs->cursor_move) {
   2107 			ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
   2108 		} else {
   2109 			ret = -EFAULT;
   2110 			goto out;
   2111 		}
   2112 	}
   2113 out:
   2114 	mutex_unlock(&dev->mode_config.mutex);
   2115 	return ret;
   2116 }
   2117 
   2118 /* Original addfb only supported RGB formats, so figure out which one */
   2119 uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
   2120 {
   2121 	uint32_t fmt;
   2122 
   2123 	switch (bpp) {
   2124 	case 8:
   2125 		fmt = DRM_FORMAT_RGB332;
   2126 		break;
   2127 	case 16:
   2128 		if (depth == 15)
   2129 			fmt = DRM_FORMAT_XRGB1555;
   2130 		else
   2131 			fmt = DRM_FORMAT_RGB565;
   2132 		break;
   2133 	case 24:
   2134 		fmt = DRM_FORMAT_RGB888;
   2135 		break;
   2136 	case 32:
   2137 		if (depth == 24)
   2138 			fmt = DRM_FORMAT_XRGB8888;
   2139 		else if (depth == 30)
   2140 			fmt = DRM_FORMAT_XRGB2101010;
   2141 		else
   2142 			fmt = DRM_FORMAT_ARGB8888;
   2143 		break;
   2144 	default:
   2145 		DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
   2146 		fmt = DRM_FORMAT_XRGB8888;
   2147 		break;
   2148 	}
   2149 
   2150 	return fmt;
   2151 }
   2152 EXPORT_SYMBOL(drm_mode_legacy_fb_format);
   2153 
   2154 /**
   2155  * drm_mode_addfb - add an FB to the graphics configuration
   2156  * @inode: inode from the ioctl
   2157  * @filp: file * from the ioctl
   2158  * @cmd: cmd from ioctl
   2159  * @arg: arg from ioctl
   2160  *
   2161  * LOCKING:
   2162  * Takes mode config lock.
   2163  *
   2164  * Add a new FB to the specified CRTC, given a user request.
   2165  *
   2166  * Called by the user via ioctl.
   2167  *
   2168  * RETURNS:
   2169  * Zero on success, errno on failure.
   2170  */
   2171 int drm_mode_addfb(struct drm_device *dev,
   2172 		   void *data, struct drm_file *file_priv)
   2173 {
   2174 	struct drm_mode_fb_cmd *or = data;
   2175 #ifdef __NetBSD__		/* XXX ...or just use memset.  */
   2176 	static const struct drm_mode_fb_cmd2 zero_fbcmd;
   2177 	struct drm_mode_fb_cmd2 r = zero_fbcmd;
   2178 #else
   2179 	struct drm_mode_fb_cmd2 r = {};
   2180 #endif
   2181 	struct drm_mode_config *config = &dev->mode_config;
   2182 	struct drm_framebuffer *fb;
   2183 	int ret = 0;
   2184 
   2185 	/* Use new struct with format internally */
   2186 	r.fb_id = or->fb_id;
   2187 	r.width = or->width;
   2188 	r.height = or->height;
   2189 	r.pitches[0] = or->pitch;
   2190 	r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
   2191 	r.handles[0] = or->handle;
   2192 
   2193 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
   2194 		return -EINVAL;
   2195 
   2196 	if ((config->min_width > r.width) || (r.width > config->max_width))
   2197 		return -EINVAL;
   2198 
   2199 	if ((config->min_height > r.height) || (r.height > config->max_height))
   2200 		return -EINVAL;
   2201 
   2202 	mutex_lock(&dev->mode_config.mutex);
   2203 
   2204 	/* TODO check buffer is sufficiently large */
   2205 	/* TODO setup destructor callback */
   2206 
   2207 	fb = dev->mode_config.funcs->fb_create(dev, file_priv, &r);
   2208 	if (IS_ERR(fb)) {
   2209 		DRM_DEBUG_KMS("could not create framebuffer\n");
   2210 		ret = PTR_ERR(fb);
   2211 		goto out;
   2212 	}
   2213 
   2214 	or->fb_id = fb->base.id;
   2215 	list_add(&fb->filp_head, &file_priv->fbs);
   2216 	DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
   2217 
   2218 out:
   2219 	mutex_unlock(&dev->mode_config.mutex);
   2220 	return ret;
   2221 }
   2222 
   2223 static int format_check(const struct drm_mode_fb_cmd2 *r)
   2224 {
   2225 	uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
   2226 
   2227 	switch (format) {
   2228 	case DRM_FORMAT_C8:
   2229 	case DRM_FORMAT_RGB332:
   2230 	case DRM_FORMAT_BGR233:
   2231 	case DRM_FORMAT_XRGB4444:
   2232 	case DRM_FORMAT_XBGR4444:
   2233 	case DRM_FORMAT_RGBX4444:
   2234 	case DRM_FORMAT_BGRX4444:
   2235 	case DRM_FORMAT_ARGB4444:
   2236 	case DRM_FORMAT_ABGR4444:
   2237 	case DRM_FORMAT_RGBA4444:
   2238 	case DRM_FORMAT_BGRA4444:
   2239 	case DRM_FORMAT_XRGB1555:
   2240 	case DRM_FORMAT_XBGR1555:
   2241 	case DRM_FORMAT_RGBX5551:
   2242 	case DRM_FORMAT_BGRX5551:
   2243 	case DRM_FORMAT_ARGB1555:
   2244 	case DRM_FORMAT_ABGR1555:
   2245 	case DRM_FORMAT_RGBA5551:
   2246 	case DRM_FORMAT_BGRA5551:
   2247 	case DRM_FORMAT_RGB565:
   2248 	case DRM_FORMAT_BGR565:
   2249 	case DRM_FORMAT_RGB888:
   2250 	case DRM_FORMAT_BGR888:
   2251 	case DRM_FORMAT_XRGB8888:
   2252 	case DRM_FORMAT_XBGR8888:
   2253 	case DRM_FORMAT_RGBX8888:
   2254 	case DRM_FORMAT_BGRX8888:
   2255 	case DRM_FORMAT_ARGB8888:
   2256 	case DRM_FORMAT_ABGR8888:
   2257 	case DRM_FORMAT_RGBA8888:
   2258 	case DRM_FORMAT_BGRA8888:
   2259 	case DRM_FORMAT_XRGB2101010:
   2260 	case DRM_FORMAT_XBGR2101010:
   2261 	case DRM_FORMAT_RGBX1010102:
   2262 	case DRM_FORMAT_BGRX1010102:
   2263 	case DRM_FORMAT_ARGB2101010:
   2264 	case DRM_FORMAT_ABGR2101010:
   2265 	case DRM_FORMAT_RGBA1010102:
   2266 	case DRM_FORMAT_BGRA1010102:
   2267 	case DRM_FORMAT_YUYV:
   2268 	case DRM_FORMAT_YVYU:
   2269 	case DRM_FORMAT_UYVY:
   2270 	case DRM_FORMAT_VYUY:
   2271 	case DRM_FORMAT_AYUV:
   2272 	case DRM_FORMAT_NV12:
   2273 	case DRM_FORMAT_NV21:
   2274 	case DRM_FORMAT_NV16:
   2275 	case DRM_FORMAT_NV61:
   2276 	case DRM_FORMAT_NV24:
   2277 	case DRM_FORMAT_NV42:
   2278 	case DRM_FORMAT_YUV410:
   2279 	case DRM_FORMAT_YVU410:
   2280 	case DRM_FORMAT_YUV411:
   2281 	case DRM_FORMAT_YVU411:
   2282 	case DRM_FORMAT_YUV420:
   2283 	case DRM_FORMAT_YVU420:
   2284 	case DRM_FORMAT_YUV422:
   2285 	case DRM_FORMAT_YVU422:
   2286 	case DRM_FORMAT_YUV444:
   2287 	case DRM_FORMAT_YVU444:
   2288 		return 0;
   2289 	default:
   2290 		return -EINVAL;
   2291 	}
   2292 }
   2293 
   2294 static int framebuffer_check(const struct drm_mode_fb_cmd2 *r)
   2295 {
   2296 	int ret, hsub, vsub, num_planes, i;
   2297 
   2298 	ret = format_check(r);
   2299 	if (ret) {
   2300 		DRM_DEBUG_KMS("bad framebuffer format 0x%08x\n", r->pixel_format);
   2301 		return ret;
   2302 	}
   2303 
   2304 	hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
   2305 	vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
   2306 	num_planes = drm_format_num_planes(r->pixel_format);
   2307 
   2308 	if (r->width == 0 || r->width % hsub) {
   2309 		DRM_DEBUG_KMS("bad framebuffer width %u\n", r->height);
   2310 		return -EINVAL;
   2311 	}
   2312 
   2313 	if (r->height == 0 || r->height % vsub) {
   2314 		DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
   2315 		return -EINVAL;
   2316 	}
   2317 
   2318 	for (i = 0; i < num_planes; i++) {
   2319 		unsigned int width = r->width / (i != 0 ? hsub : 1);
   2320 		unsigned int height = r->height / (i != 0 ? vsub : 1);
   2321 		unsigned int cpp = drm_format_plane_cpp(r->pixel_format, i);
   2322 
   2323 		if (!r->handles[i]) {
   2324 			DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
   2325 			return -EINVAL;
   2326 		}
   2327 
   2328 		if ((uint64_t) width * cpp > UINT_MAX)
   2329 			return -ERANGE;
   2330 
   2331 		if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
   2332 			return -ERANGE;
   2333 
   2334 		if (r->pitches[i] < width * cpp) {
   2335 			DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
   2336 			return -EINVAL;
   2337 		}
   2338 	}
   2339 
   2340 	return 0;
   2341 }
   2342 
   2343 /**
   2344  * drm_mode_addfb2 - add an FB to the graphics configuration
   2345  * @inode: inode from the ioctl
   2346  * @filp: file * from the ioctl
   2347  * @cmd: cmd from ioctl
   2348  * @arg: arg from ioctl
   2349  *
   2350  * LOCKING:
   2351  * Takes mode config lock.
   2352  *
   2353  * Add a new FB to the specified CRTC, given a user request with format.
   2354  *
   2355  * Called by the user via ioctl.
   2356  *
   2357  * RETURNS:
   2358  * Zero on success, errno on failure.
   2359  */
   2360 int drm_mode_addfb2(struct drm_device *dev,
   2361 		    void *data, struct drm_file *file_priv)
   2362 {
   2363 	struct drm_mode_fb_cmd2 *r = data;
   2364 	struct drm_mode_config *config = &dev->mode_config;
   2365 	struct drm_framebuffer *fb;
   2366 	int ret;
   2367 
   2368 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
   2369 		return -EINVAL;
   2370 
   2371 	if (r->flags & ~DRM_MODE_FB_INTERLACED) {
   2372 		DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);
   2373 		return -EINVAL;
   2374 	}
   2375 
   2376 	if ((config->min_width > r->width) || (r->width > config->max_width)) {
   2377 		DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
   2378 			  r->width, config->min_width, config->max_width);
   2379 		return -EINVAL;
   2380 	}
   2381 	if ((config->min_height > r->height) || (r->height > config->max_height)) {
   2382 		DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
   2383 			  r->height, config->min_height, config->max_height);
   2384 		return -EINVAL;
   2385 	}
   2386 
   2387 	ret = framebuffer_check(r);
   2388 	if (ret)
   2389 		return ret;
   2390 
   2391 	mutex_lock(&dev->mode_config.mutex);
   2392 
   2393 	fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
   2394 	if (IS_ERR(fb)) {
   2395 		DRM_DEBUG_KMS("could not create framebuffer\n");
   2396 		ret = PTR_ERR(fb);
   2397 		goto out;
   2398 	}
   2399 
   2400 	r->fb_id = fb->base.id;
   2401 	list_add(&fb->filp_head, &file_priv->fbs);
   2402 	DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
   2403 
   2404 out:
   2405 	mutex_unlock(&dev->mode_config.mutex);
   2406 	return ret;
   2407 }
   2408 
   2409 /**
   2410  * drm_mode_rmfb - remove an FB from the configuration
   2411  * @inode: inode from the ioctl
   2412  * @filp: file * from the ioctl
   2413  * @cmd: cmd from ioctl
   2414  * @arg: arg from ioctl
   2415  *
   2416  * LOCKING:
   2417  * Takes mode config lock.
   2418  *
   2419  * Remove the FB specified by the user.
   2420  *
   2421  * Called by the user via ioctl.
   2422  *
   2423  * RETURNS:
   2424  * Zero on success, errno on failure.
   2425  */
   2426 int drm_mode_rmfb(struct drm_device *dev,
   2427 		   void *data, struct drm_file *file_priv)
   2428 {
   2429 	struct drm_mode_object *obj;
   2430 	struct drm_framebuffer *fb = NULL;
   2431 	struct drm_framebuffer *fbl = NULL;
   2432 	uint32_t *id = data;
   2433 	int ret = 0;
   2434 	int found = 0;
   2435 
   2436 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
   2437 		return -EINVAL;
   2438 
   2439 	mutex_lock(&dev->mode_config.mutex);
   2440 	obj = drm_mode_object_find(dev, *id, DRM_MODE_OBJECT_FB);
   2441 	/* TODO check that we really get a framebuffer back. */
   2442 	if (!obj) {
   2443 		ret = -EINVAL;
   2444 		goto out;
   2445 	}
   2446 	fb = obj_to_fb(obj);
   2447 
   2448 	list_for_each_entry(fbl, &file_priv->fbs, filp_head)
   2449 		if (fb == fbl)
   2450 			found = 1;
   2451 
   2452 	if (!found) {
   2453 		ret = -EINVAL;
   2454 		goto out;
   2455 	}
   2456 
   2457 	drm_framebuffer_remove(fb);
   2458 
   2459 out:
   2460 	mutex_unlock(&dev->mode_config.mutex);
   2461 	return ret;
   2462 }
   2463 
   2464 /**
   2465  * drm_mode_getfb - get FB info
   2466  * @inode: inode from the ioctl
   2467  * @filp: file * from the ioctl
   2468  * @cmd: cmd from ioctl
   2469  * @arg: arg from ioctl
   2470  *
   2471  * LOCKING:
   2472  * Takes mode config lock.
   2473  *
   2474  * Lookup the FB given its ID and return info about it.
   2475  *
   2476  * Called by the user via ioctl.
   2477  *
   2478  * RETURNS:
   2479  * Zero on success, errno on failure.
   2480  */
   2481 int drm_mode_getfb(struct drm_device *dev,
   2482 		   void *data, struct drm_file *file_priv)
   2483 {
   2484 	struct drm_mode_fb_cmd *r = data;
   2485 	struct drm_mode_object *obj;
   2486 	struct drm_framebuffer *fb;
   2487 	int ret = 0;
   2488 
   2489 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
   2490 		return -EINVAL;
   2491 
   2492 	mutex_lock(&dev->mode_config.mutex);
   2493 	obj = drm_mode_object_find(dev, r->fb_id, DRM_MODE_OBJECT_FB);
   2494 	if (!obj) {
   2495 		ret = -EINVAL;
   2496 		goto out;
   2497 	}
   2498 	fb = obj_to_fb(obj);
   2499 
   2500 	r->height = fb->height;
   2501 	r->width = fb->width;
   2502 	r->depth = fb->depth;
   2503 	r->bpp = fb->bits_per_pixel;
   2504 	r->pitch = fb->pitches[0];
   2505 	fb->funcs->create_handle(fb, file_priv, &r->handle);
   2506 
   2507 out:
   2508 	mutex_unlock(&dev->mode_config.mutex);
   2509 	return ret;
   2510 }
   2511 
   2512 int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
   2513 			   void *data, struct drm_file *file_priv)
   2514 {
   2515 	struct drm_clip_rect __user *clips_ptr;
   2516 	struct drm_clip_rect *clips = NULL;
   2517 	struct drm_mode_fb_dirty_cmd *r = data;
   2518 	struct drm_mode_object *obj;
   2519 	struct drm_framebuffer *fb;
   2520 	unsigned flags;
   2521 	int num_clips;
   2522 	int ret;
   2523 
   2524 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
   2525 		return -EINVAL;
   2526 
   2527 	mutex_lock(&dev->mode_config.mutex);
   2528 	obj = drm_mode_object_find(dev, r->fb_id, DRM_MODE_OBJECT_FB);
   2529 	if (!obj) {
   2530 		ret = -EINVAL;
   2531 		goto out_err1;
   2532 	}
   2533 	fb = obj_to_fb(obj);
   2534 
   2535 	num_clips = r->num_clips;
   2536 	clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
   2537 
   2538 	if (!num_clips != !clips_ptr) {
   2539 		ret = -EINVAL;
   2540 		goto out_err1;
   2541 	}
   2542 
   2543 	flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
   2544 
   2545 	/* If userspace annotates copy, clips must come in pairs */
   2546 	if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
   2547 		ret = -EINVAL;
   2548 		goto out_err1;
   2549 	}
   2550 
   2551 	if (num_clips && clips_ptr) {
   2552 		if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
   2553 			ret = -EINVAL;
   2554 			goto out_err1;
   2555 		}
   2556 		clips = kzalloc(num_clips * sizeof(*clips), GFP_KERNEL);
   2557 		if (!clips) {
   2558 			ret = -ENOMEM;
   2559 			goto out_err1;
   2560 		}
   2561 
   2562 		ret = copy_from_user(clips, clips_ptr,
   2563 				     num_clips * sizeof(*clips));
   2564 		if (ret) {
   2565 			ret = -EFAULT;
   2566 			goto out_err2;
   2567 		}
   2568 	}
   2569 
   2570 	if (fb->funcs->dirty) {
   2571 		ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
   2572 				       clips, num_clips);
   2573 	} else {
   2574 		ret = -ENOSYS;
   2575 		goto out_err2;
   2576 	}
   2577 
   2578 out_err2:
   2579 	kfree(clips);
   2580 out_err1:
   2581 	mutex_unlock(&dev->mode_config.mutex);
   2582 	return ret;
   2583 }
   2584 
   2585 
   2586 /**
   2587  * drm_fb_release - remove and free the FBs on this file
   2588  * @filp: file * from the ioctl
   2589  *
   2590  * LOCKING:
   2591  * Takes mode config lock.
   2592  *
   2593  * Destroy all the FBs associated with @filp.
   2594  *
   2595  * Called by the user via ioctl.
   2596  *
   2597  * RETURNS:
   2598  * Zero on success, errno on failure.
   2599  */
   2600 void drm_fb_release(struct drm_file *priv)
   2601 {
   2602 	struct drm_device *dev = priv->minor->dev;
   2603 	struct drm_framebuffer *fb, *tfb;
   2604 
   2605 	mutex_lock(&dev->mode_config.mutex);
   2606 	list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
   2607 		drm_framebuffer_remove(fb);
   2608 	}
   2609 	mutex_unlock(&dev->mode_config.mutex);
   2610 }
   2611 
   2612 /**
   2613  * drm_mode_attachmode - add a mode to the user mode list
   2614  * @dev: DRM device
   2615  * @connector: connector to add the mode to
   2616  * @mode: mode to add
   2617  *
   2618  * Add @mode to @connector's user mode list.
   2619  */
   2620 static void drm_mode_attachmode(struct drm_device *dev,
   2621 				struct drm_connector *connector,
   2622 				struct drm_display_mode *mode)
   2623 {
   2624 	list_add_tail(&mode->head, &connector->user_modes);
   2625 }
   2626 
   2627 int drm_mode_attachmode_crtc(struct drm_device *dev, struct drm_crtc *crtc,
   2628 			     const struct drm_display_mode *mode)
   2629 {
   2630 	struct drm_connector *connector;
   2631 	int ret = 0;
   2632 	struct drm_display_mode *dup_mode, *next;
   2633 #ifdef __NetBSD__
   2634 	/* LIST_HEAD has another meaning in NetBSD.  */
   2635 	struct list_head list = LIST_HEAD_INIT(list);
   2636 #else
   2637 	LIST_HEAD(list);
   2638 #endif
   2639 
   2640 	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
   2641 		if (!connector->encoder)
   2642 			continue;
   2643 		if (connector->encoder->crtc == crtc) {
   2644 			dup_mode = drm_mode_duplicate(dev, mode);
   2645 			if (!dup_mode) {
   2646 				ret = -ENOMEM;
   2647 				goto out;
   2648 			}
   2649 			list_add_tail(&dup_mode->head, &list);
   2650 		}
   2651 	}
   2652 
   2653 	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
   2654 		if (!connector->encoder)
   2655 			continue;
   2656 		if (connector->encoder->crtc == crtc)
   2657 #ifdef __NetBSD__
   2658 			list_move_tail(list_next(&list),
   2659 			    &connector->user_modes);
   2660 #else
   2661 			list_move_tail(list.next, &connector->user_modes);
   2662 #endif
   2663 	}
   2664 
   2665 	WARN_ON(!list_empty(&list));
   2666 
   2667  out:
   2668 	list_for_each_entry_safe(dup_mode, next, &list, head)
   2669 		drm_mode_destroy(dev, dup_mode);
   2670 
   2671 	return ret;
   2672 }
   2673 EXPORT_SYMBOL(drm_mode_attachmode_crtc);
   2674 
   2675 static int drm_mode_detachmode(struct drm_device *dev,
   2676 			       struct drm_connector *connector,
   2677 			       struct drm_display_mode *mode)
   2678 {
   2679 	int found = 0;
   2680 	int ret = 0;
   2681 	struct drm_display_mode *match_mode, *t;
   2682 
   2683 	list_for_each_entry_safe(match_mode, t, &connector->user_modes, head) {
   2684 		if (drm_mode_equal(match_mode, mode)) {
   2685 			list_del(&match_mode->head);
   2686 			drm_mode_destroy(dev, match_mode);
   2687 			found = 1;
   2688 			break;
   2689 		}
   2690 	}
   2691 
   2692 	if (!found)
   2693 		ret = -EINVAL;
   2694 
   2695 	return ret;
   2696 }
   2697 
   2698 int drm_mode_detachmode_crtc(struct drm_device *dev, struct drm_display_mode *mode)
   2699 {
   2700 	struct drm_connector *connector;
   2701 
   2702 	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
   2703 		drm_mode_detachmode(dev, connector, mode);
   2704 	}
   2705 	return 0;
   2706 }
   2707 EXPORT_SYMBOL(drm_mode_detachmode_crtc);
   2708 
   2709 /**
   2710  * drm_fb_attachmode - Attach a user mode to an connector
   2711  * @inode: inode from the ioctl
   2712  * @filp: file * from the ioctl
   2713  * @cmd: cmd from ioctl
   2714  * @arg: arg from ioctl
   2715  *
   2716  * This attaches a user specified mode to an connector.
   2717  * Called by the user via ioctl.
   2718  *
   2719  * RETURNS:
   2720  * Zero on success, errno on failure.
   2721  */
   2722 int drm_mode_attachmode_ioctl(struct drm_device *dev,
   2723 			      void *data, struct drm_file *file_priv)
   2724 {
   2725 	struct drm_mode_mode_cmd *mode_cmd = data;
   2726 	struct drm_connector *connector;
   2727 	struct drm_display_mode *mode;
   2728 	struct drm_mode_object *obj;
   2729 	struct drm_mode_modeinfo *umode = &mode_cmd->mode;
   2730 	int ret;
   2731 
   2732 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
   2733 		return -EINVAL;
   2734 
   2735 	mutex_lock(&dev->mode_config.mutex);
   2736 
   2737 	obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
   2738 	if (!obj) {
   2739 		ret = -EINVAL;
   2740 		goto out;
   2741 	}
   2742 	connector = obj_to_connector(obj);
   2743 
   2744 	mode = drm_mode_create(dev);
   2745 	if (!mode) {
   2746 		ret = -ENOMEM;
   2747 		goto out;
   2748 	}
   2749 
   2750 	ret = drm_crtc_convert_umode(mode, umode);
   2751 	if (ret) {
   2752 		DRM_DEBUG_KMS("Invalid mode\n");
   2753 		drm_mode_destroy(dev, mode);
   2754 		goto out;
   2755 	}
   2756 
   2757 	drm_mode_attachmode(dev, connector, mode);
   2758 out:
   2759 	mutex_unlock(&dev->mode_config.mutex);
   2760 	return ret;
   2761 }
   2762 
   2763 
   2764 /**
   2765  * drm_fb_detachmode - Detach a user specified mode from an connector
   2766  * @inode: inode from the ioctl
   2767  * @filp: file * from the ioctl
   2768  * @cmd: cmd from ioctl
   2769  * @arg: arg from ioctl
   2770  *
   2771  * Called by the user via ioctl.
   2772  *
   2773  * RETURNS:
   2774  * Zero on success, errno on failure.
   2775  */
   2776 int drm_mode_detachmode_ioctl(struct drm_device *dev,
   2777 			      void *data, struct drm_file *file_priv)
   2778 {
   2779 	struct drm_mode_object *obj;
   2780 	struct drm_mode_mode_cmd *mode_cmd = data;
   2781 	struct drm_connector *connector;
   2782 	struct drm_display_mode mode;
   2783 	struct drm_mode_modeinfo *umode = &mode_cmd->mode;
   2784 	int ret;
   2785 
   2786 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
   2787 		return -EINVAL;
   2788 
   2789 	mutex_lock(&dev->mode_config.mutex);
   2790 
   2791 	obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
   2792 	if (!obj) {
   2793 		ret = -EINVAL;
   2794 		goto out;
   2795 	}
   2796 	connector = obj_to_connector(obj);
   2797 
   2798 	ret = drm_crtc_convert_umode(&mode, umode);
   2799 	if (ret) {
   2800 		DRM_DEBUG_KMS("Invalid mode\n");
   2801 		goto out;
   2802 	}
   2803 
   2804 	ret = drm_mode_detachmode(dev, connector, &mode);
   2805 out:
   2806 	mutex_unlock(&dev->mode_config.mutex);
   2807 	return ret;
   2808 }
   2809 
   2810 struct drm_property *drm_property_create(struct drm_device *dev, int flags,
   2811 					 const char *name, int num_values)
   2812 {
   2813 	struct drm_property *property = NULL;
   2814 	int ret;
   2815 
   2816 	property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
   2817 	if (!property)
   2818 		return NULL;
   2819 
   2820 	if (num_values) {
   2821 		property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
   2822 		if (!property->values)
   2823 			goto fail;
   2824 	}
   2825 
   2826 	ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
   2827 	if (ret)
   2828 		goto fail;
   2829 
   2830 	property->flags = flags;
   2831 	property->num_values = num_values;
   2832 	INIT_LIST_HEAD(&property->enum_blob_list);
   2833 
   2834 	if (name) {
   2835 		strncpy(property->name, name, DRM_PROP_NAME_LEN);
   2836 		property->name[DRM_PROP_NAME_LEN-1] = '\0';
   2837 	}
   2838 
   2839 	list_add_tail(&property->head, &dev->mode_config.property_list);
   2840 	return property;
   2841 fail:
   2842 	kfree(property->values);
   2843 	kfree(property);
   2844 	return NULL;
   2845 }
   2846 EXPORT_SYMBOL(drm_property_create);
   2847 
   2848 struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
   2849 					 const char *name,
   2850 					 const struct drm_prop_enum_list *props,
   2851 					 int num_values)
   2852 {
   2853 	struct drm_property *property;
   2854 	int i, ret;
   2855 
   2856 	flags |= DRM_MODE_PROP_ENUM;
   2857 
   2858 	property = drm_property_create(dev, flags, name, num_values);
   2859 	if (!property)
   2860 		return NULL;
   2861 
   2862 	for (i = 0; i < num_values; i++) {
   2863 		ret = drm_property_add_enum(property, i,
   2864 				      props[i].type,
   2865 				      props[i].name);
   2866 		if (ret) {
   2867 			drm_property_destroy(dev, property);
   2868 			return NULL;
   2869 		}
   2870 	}
   2871 
   2872 	return property;
   2873 }
   2874 EXPORT_SYMBOL(drm_property_create_enum);
   2875 
   2876 struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
   2877 					 int flags, const char *name,
   2878 					 const struct drm_prop_enum_list *props,
   2879 					 int num_values)
   2880 {
   2881 	struct drm_property *property;
   2882 	int i, ret;
   2883 
   2884 	flags |= DRM_MODE_PROP_BITMASK;
   2885 
   2886 	property = drm_property_create(dev, flags, name, num_values);
   2887 	if (!property)
   2888 		return NULL;
   2889 
   2890 	for (i = 0; i < num_values; i++) {
   2891 		ret = drm_property_add_enum(property, i,
   2892 				      props[i].type,
   2893 				      props[i].name);
   2894 		if (ret) {
   2895 			drm_property_destroy(dev, property);
   2896 			return NULL;
   2897 		}
   2898 	}
   2899 
   2900 	return property;
   2901 }
   2902 EXPORT_SYMBOL(drm_property_create_bitmask);
   2903 
   2904 #ifdef __NetBSD__
   2905 /* XXX Whattakludge...  */
   2906 #  define	min	min_hack
   2907 #  define	max	max_hack
   2908 #endif
   2909 
   2910 struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
   2911 					 const char *name,
   2912 					 uint64_t min, uint64_t max)
   2913 {
   2914 	struct drm_property *property;
   2915 
   2916 	flags |= DRM_MODE_PROP_RANGE;
   2917 
   2918 	property = drm_property_create(dev, flags, name, 2);
   2919 	if (!property)
   2920 		return NULL;
   2921 
   2922 	property->values[0] = min;
   2923 	property->values[1] = max;
   2924 
   2925 	return property;
   2926 }
   2927 EXPORT_SYMBOL(drm_property_create_range);
   2928 
   2929 #ifdef __NetBSD__
   2930 #  undef	min
   2931 #  undef	max
   2932 #endif
   2933 
   2934 int drm_property_add_enum(struct drm_property *property, int index,
   2935 			  uint64_t value, const char *name)
   2936 {
   2937 	struct drm_property_enum *prop_enum;
   2938 
   2939 	if (!(property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)))
   2940 		return -EINVAL;
   2941 
   2942 	/*
   2943 	 * Bitmask enum properties have the additional constraint of values
   2944 	 * from 0 to 63
   2945 	 */
   2946 	if ((property->flags & DRM_MODE_PROP_BITMASK) && (value > 63))
   2947 		return -EINVAL;
   2948 
   2949 	if (!list_empty(&property->enum_blob_list)) {
   2950 		list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
   2951 			if (prop_enum->value == value) {
   2952 				strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
   2953 				prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
   2954 				return 0;
   2955 			}
   2956 		}
   2957 	}
   2958 
   2959 	prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
   2960 	if (!prop_enum)
   2961 		return -ENOMEM;
   2962 
   2963 	strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
   2964 	prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
   2965 	prop_enum->value = value;
   2966 
   2967 	property->values[index] = value;
   2968 	list_add_tail(&prop_enum->head, &property->enum_blob_list);
   2969 	return 0;
   2970 }
   2971 EXPORT_SYMBOL(drm_property_add_enum);
   2972 
   2973 void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
   2974 {
   2975 	struct drm_property_enum *prop_enum, *pt;
   2976 
   2977 	list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
   2978 		list_del(&prop_enum->head);
   2979 		kfree(prop_enum);
   2980 	}
   2981 
   2982 	if (property->num_values)
   2983 		kfree(property->values);
   2984 	drm_mode_object_put(dev, &property->base);
   2985 	list_del(&property->head);
   2986 	kfree(property);
   2987 }
   2988 EXPORT_SYMBOL(drm_property_destroy);
   2989 
   2990 void drm_object_attach_property(struct drm_mode_object *obj,
   2991 				struct drm_property *property,
   2992 				uint64_t init_val)
   2993 {
   2994 	int count = obj->properties->count;
   2995 
   2996 	if (count == DRM_OBJECT_MAX_PROPERTY) {
   2997 		WARN(1, "Failed to attach object property (type: 0x%x). Please "
   2998 			"increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
   2999 			"you see this message on the same object type.\n",
   3000 			obj->type);
   3001 		return;
   3002 	}
   3003 
   3004 	obj->properties->ids[count] = property->base.id;
   3005 	obj->properties->values[count] = init_val;
   3006 	obj->properties->count++;
   3007 }
   3008 EXPORT_SYMBOL(drm_object_attach_property);
   3009 
   3010 int drm_object_property_set_value(struct drm_mode_object *obj,
   3011 				  struct drm_property *property, uint64_t val)
   3012 {
   3013 	int i;
   3014 
   3015 	for (i = 0; i < obj->properties->count; i++) {
   3016 		if (obj->properties->ids[i] == property->base.id) {
   3017 			obj->properties->values[i] = val;
   3018 			return 0;
   3019 		}
   3020 	}
   3021 
   3022 	return -EINVAL;
   3023 }
   3024 EXPORT_SYMBOL(drm_object_property_set_value);
   3025 
   3026 int drm_object_property_get_value(struct drm_mode_object *obj,
   3027 				  struct drm_property *property, uint64_t *val)
   3028 {
   3029 	int i;
   3030 
   3031 	for (i = 0; i < obj->properties->count; i++) {
   3032 		if (obj->properties->ids[i] == property->base.id) {
   3033 			*val = obj->properties->values[i];
   3034 			return 0;
   3035 		}
   3036 	}
   3037 
   3038 	return -EINVAL;
   3039 }
   3040 EXPORT_SYMBOL(drm_object_property_get_value);
   3041 
   3042 int drm_mode_getproperty_ioctl(struct drm_device *dev,
   3043 			       void *data, struct drm_file *file_priv)
   3044 {
   3045 	struct drm_mode_object *obj;
   3046 	struct drm_mode_get_property *out_resp = data;
   3047 	struct drm_property *property;
   3048 	int enum_count = 0;
   3049 	int blob_count = 0;
   3050 	int value_count = 0;
   3051 	int ret = 0, i;
   3052 	int copied;
   3053 	struct drm_property_enum *prop_enum;
   3054 	struct drm_mode_property_enum __user *enum_ptr;
   3055 	struct drm_property_blob *prop_blob;
   3056 	uint32_t __user *blob_id_ptr;
   3057 	uint64_t __user *values_ptr;
   3058 	uint32_t __user *blob_length_ptr;
   3059 
   3060 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
   3061 		return -EINVAL;
   3062 
   3063 	mutex_lock(&dev->mode_config.mutex);
   3064 	obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
   3065 	if (!obj) {
   3066 		ret = -EINVAL;
   3067 		goto done;
   3068 	}
   3069 	property = obj_to_property(obj);
   3070 
   3071 	if (property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
   3072 		list_for_each_entry(prop_enum, &property->enum_blob_list, head)
   3073 			enum_count++;
   3074 	} else if (property->flags & DRM_MODE_PROP_BLOB) {
   3075 		list_for_each_entry(prop_blob, &property->enum_blob_list, head)
   3076 			blob_count++;
   3077 	}
   3078 
   3079 	value_count = property->num_values;
   3080 
   3081 	strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
   3082 	out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
   3083 	out_resp->flags = property->flags;
   3084 
   3085 	if ((out_resp->count_values >= value_count) && value_count) {
   3086 		values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
   3087 		for (i = 0; i < value_count; i++) {
   3088 			if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
   3089 				ret = -EFAULT;
   3090 				goto done;
   3091 			}
   3092 		}
   3093 	}
   3094 	out_resp->count_values = value_count;
   3095 
   3096 	if (property->flags & (DRM_MODE_PROP_ENUM | DRM_MODE_PROP_BITMASK)) {
   3097 		if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
   3098 			copied = 0;
   3099 			enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
   3100 			list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
   3101 
   3102 				if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
   3103 					ret = -EFAULT;
   3104 					goto done;
   3105 				}
   3106 
   3107 				if (copy_to_user(&enum_ptr[copied].name,
   3108 						 &prop_enum->name, DRM_PROP_NAME_LEN)) {
   3109 					ret = -EFAULT;
   3110 					goto done;
   3111 				}
   3112 				copied++;
   3113 			}
   3114 		}
   3115 		out_resp->count_enum_blobs = enum_count;
   3116 	}
   3117 
   3118 	if (property->flags & DRM_MODE_PROP_BLOB) {
   3119 		if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
   3120 			copied = 0;
   3121 			blob_id_ptr = (uint32_t __user *)(unsigned long)out_resp->enum_blob_ptr;
   3122 			blob_length_ptr = (uint32_t __user *)(unsigned long)out_resp->values_ptr;
   3123 
   3124 			list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
   3125 				if (put_user(prop_blob->base.id, blob_id_ptr + copied)) {
   3126 					ret = -EFAULT;
   3127 					goto done;
   3128 				}
   3129 
   3130 				if (put_user(prop_blob->length, blob_length_ptr + copied)) {
   3131 					ret = -EFAULT;
   3132 					goto done;
   3133 				}
   3134 
   3135 				copied++;
   3136 			}
   3137 		}
   3138 		out_resp->count_enum_blobs = blob_count;
   3139 	}
   3140 done:
   3141 	mutex_unlock(&dev->mode_config.mutex);
   3142 	return ret;
   3143 }
   3144 
   3145 static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
   3146 							  void *data)
   3147 {
   3148 	struct drm_property_blob *blob;
   3149 	int ret;
   3150 
   3151 	if (!length || !data)
   3152 		return NULL;
   3153 
   3154 	blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
   3155 	if (!blob)
   3156 		return NULL;
   3157 
   3158 	ret = drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
   3159 	if (ret) {
   3160 		kfree(blob);
   3161 		return NULL;
   3162 	}
   3163 
   3164 	blob->length = length;
   3165 
   3166 	memcpy(blob->data, data, length);
   3167 
   3168 	list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
   3169 	return blob;
   3170 }
   3171 
   3172 static void drm_property_destroy_blob(struct drm_device *dev,
   3173 			       struct drm_property_blob *blob)
   3174 {
   3175 	drm_mode_object_put(dev, &blob->base);
   3176 	list_del(&blob->head);
   3177 	kfree(blob);
   3178 }
   3179 
   3180 int drm_mode_getblob_ioctl(struct drm_device *dev,
   3181 			   void *data, struct drm_file *file_priv)
   3182 {
   3183 	struct drm_mode_object *obj;
   3184 	struct drm_mode_get_blob *out_resp = data;
   3185 	struct drm_property_blob *blob;
   3186 	int ret = 0;
   3187 	void __user *blob_ptr;
   3188 
   3189 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
   3190 		return -EINVAL;
   3191 
   3192 	mutex_lock(&dev->mode_config.mutex);
   3193 	obj = drm_mode_object_find(dev, out_resp->blob_id, DRM_MODE_OBJECT_BLOB);
   3194 	if (!obj) {
   3195 		ret = -EINVAL;
   3196 		goto done;
   3197 	}
   3198 	blob = obj_to_blob(obj);
   3199 
   3200 	if (out_resp->length == blob->length) {
   3201 		blob_ptr = (void __user *)(unsigned long)out_resp->data;
   3202 		if (copy_to_user(blob_ptr, blob->data, blob->length)){
   3203 			ret = -EFAULT;
   3204 			goto done;
   3205 		}
   3206 	}
   3207 	out_resp->length = blob->length;
   3208 
   3209 done:
   3210 	mutex_unlock(&dev->mode_config.mutex);
   3211 	return ret;
   3212 }
   3213 
   3214 int drm_mode_connector_update_edid_property(struct drm_connector *connector,
   3215 					    struct edid *edid)
   3216 {
   3217 	struct drm_device *dev = connector->dev;
   3218 	int ret, size;
   3219 
   3220 	if (connector->edid_blob_ptr)
   3221 		drm_property_destroy_blob(dev, connector->edid_blob_ptr);
   3222 
   3223 	/* Delete edid, when there is none. */
   3224 	if (!edid) {
   3225 		connector->edid_blob_ptr = NULL;
   3226 		ret = drm_object_property_set_value(&connector->base, dev->mode_config.edid_property, 0);
   3227 		return ret;
   3228 	}
   3229 
   3230 	size = EDID_LENGTH * (1 + edid->extensions);
   3231 	connector->edid_blob_ptr = drm_property_create_blob(connector->dev,
   3232 							    size, edid);
   3233 	if (!connector->edid_blob_ptr)
   3234 		return -EINVAL;
   3235 
   3236 	ret = drm_object_property_set_value(&connector->base,
   3237 					       dev->mode_config.edid_property,
   3238 					       connector->edid_blob_ptr->base.id);
   3239 
   3240 	return ret;
   3241 }
   3242 EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
   3243 
   3244 static bool drm_property_change_is_valid(struct drm_property *property,
   3245 					 uint64_t value)
   3246 {
   3247 	if (property->flags & DRM_MODE_PROP_IMMUTABLE)
   3248 		return false;
   3249 	if (property->flags & DRM_MODE_PROP_RANGE) {
   3250 		if (value < property->values[0] || value > property->values[1])
   3251 			return false;
   3252 		return true;
   3253 	} else if (property->flags & DRM_MODE_PROP_BITMASK) {
   3254 		int i;
   3255 		uint64_t valid_mask = 0;
   3256 		for (i = 0; i < property->num_values; i++)
   3257 			valid_mask |= (1ULL << property->values[i]);
   3258 		return !(value & ~valid_mask);
   3259 	} else if (property->flags & DRM_MODE_PROP_BLOB) {
   3260 		/* Only the driver knows */
   3261 		return true;
   3262 	} else {
   3263 		int i;
   3264 		for (i = 0; i < property->num_values; i++)
   3265 			if (property->values[i] == value)
   3266 				return true;
   3267 		return false;
   3268 	}
   3269 }
   3270 
   3271 int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
   3272 				       void *data, struct drm_file *file_priv)
   3273 {
   3274 	struct drm_mode_connector_set_property *conn_set_prop = data;
   3275 	struct drm_mode_obj_set_property obj_set_prop = {
   3276 		.value = conn_set_prop->value,
   3277 		.prop_id = conn_set_prop->prop_id,
   3278 		.obj_id = conn_set_prop->connector_id,
   3279 		.obj_type = DRM_MODE_OBJECT_CONNECTOR
   3280 	};
   3281 
   3282 	/* It does all the locking and checking we need */
   3283 	return drm_mode_obj_set_property_ioctl(dev, &obj_set_prop, file_priv);
   3284 }
   3285 
   3286 static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
   3287 					   struct drm_property *property,
   3288 					   uint64_t value)
   3289 {
   3290 	int ret = -EINVAL;
   3291 	struct drm_connector *connector = obj_to_connector(obj);
   3292 
   3293 	/* Do DPMS ourselves */
   3294 	if (property == connector->dev->mode_config.dpms_property) {
   3295 		if (connector->funcs->dpms)
   3296 			(*connector->funcs->dpms)(connector, (int)value);
   3297 		ret = 0;
   3298 	} else if (connector->funcs->set_property)
   3299 		ret = connector->funcs->set_property(connector, property, value);
   3300 
   3301 	/* store the property value if successful */
   3302 	if (!ret)
   3303 		drm_object_property_set_value(&connector->base, property, value);
   3304 	return ret;
   3305 }
   3306 
   3307 static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
   3308 				      struct drm_property *property,
   3309 				      uint64_t value)
   3310 {
   3311 	int ret = -EINVAL;
   3312 	struct drm_crtc *crtc = obj_to_crtc(obj);
   3313 
   3314 	if (crtc->funcs->set_property)
   3315 		ret = crtc->funcs->set_property(crtc, property, value);
   3316 	if (!ret)
   3317 		drm_object_property_set_value(obj, property, value);
   3318 
   3319 	return ret;
   3320 }
   3321 
   3322 static int drm_mode_plane_set_obj_prop(struct drm_mode_object *obj,
   3323 				      struct drm_property *property,
   3324 				      uint64_t value)
   3325 {
   3326 	int ret = -EINVAL;
   3327 	struct drm_plane *plane = obj_to_plane(obj);
   3328 
   3329 	if (plane->funcs->set_property)
   3330 		ret = plane->funcs->set_property(plane, property, value);
   3331 	if (!ret)
   3332 		drm_object_property_set_value(obj, property, value);
   3333 
   3334 	return ret;
   3335 }
   3336 
   3337 int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
   3338 				      struct drm_file *file_priv)
   3339 {
   3340 	struct drm_mode_obj_get_properties *arg = data;
   3341 	struct drm_mode_object *obj;
   3342 	int ret = 0;
   3343 	int i;
   3344 	int copied = 0;
   3345 	int props_count = 0;
   3346 	uint32_t __user *props_ptr;
   3347 	uint64_t __user *prop_values_ptr;
   3348 
   3349 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
   3350 		return -EINVAL;
   3351 
   3352 	mutex_lock(&dev->mode_config.mutex);
   3353 
   3354 	obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
   3355 	if (!obj) {
   3356 		ret = -EINVAL;
   3357 		goto out;
   3358 	}
   3359 	if (!obj->properties) {
   3360 		ret = -EINVAL;
   3361 		goto out;
   3362 	}
   3363 
   3364 	props_count = obj->properties->count;
   3365 
   3366 	/* This ioctl is called twice, once to determine how much space is
   3367 	 * needed, and the 2nd time to fill it. */
   3368 	if ((arg->count_props >= props_count) && props_count) {
   3369 		copied = 0;
   3370 		props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
   3371 		prop_values_ptr = (uint64_t __user *)(unsigned long)
   3372 				  (arg->prop_values_ptr);
   3373 		for (i = 0; i < props_count; i++) {
   3374 			if (put_user(obj->properties->ids[i],
   3375 				     props_ptr + copied)) {
   3376 				ret = -EFAULT;
   3377 				goto out;
   3378 			}
   3379 			if (put_user(obj->properties->values[i],
   3380 				     prop_values_ptr + copied)) {
   3381 				ret = -EFAULT;
   3382 				goto out;
   3383 			}
   3384 			copied++;
   3385 		}
   3386 	}
   3387 	arg->count_props = props_count;
   3388 out:
   3389 	mutex_unlock(&dev->mode_config.mutex);
   3390 	return ret;
   3391 }
   3392 
   3393 int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
   3394 				    struct drm_file *file_priv)
   3395 {
   3396 	struct drm_mode_obj_set_property *arg = data;
   3397 	struct drm_mode_object *arg_obj;
   3398 	struct drm_mode_object *prop_obj;
   3399 	struct drm_property *property;
   3400 	int ret = -EINVAL;
   3401 	int i;
   3402 
   3403 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
   3404 		return -EINVAL;
   3405 
   3406 	mutex_lock(&dev->mode_config.mutex);
   3407 
   3408 	arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
   3409 	if (!arg_obj)
   3410 		goto out;
   3411 	if (!arg_obj->properties)
   3412 		goto out;
   3413 
   3414 	for (i = 0; i < arg_obj->properties->count; i++)
   3415 		if (arg_obj->properties->ids[i] == arg->prop_id)
   3416 			break;
   3417 
   3418 	if (i == arg_obj->properties->count)
   3419 		goto out;
   3420 
   3421 	prop_obj = drm_mode_object_find(dev, arg->prop_id,
   3422 					DRM_MODE_OBJECT_PROPERTY);
   3423 	if (!prop_obj)
   3424 		goto out;
   3425 	property = obj_to_property(prop_obj);
   3426 
   3427 	if (!drm_property_change_is_valid(property, arg->value))
   3428 		goto out;
   3429 
   3430 	switch (arg_obj->type) {
   3431 	case DRM_MODE_OBJECT_CONNECTOR:
   3432 		ret = drm_mode_connector_set_obj_prop(arg_obj, property,
   3433 						      arg->value);
   3434 		break;
   3435 	case DRM_MODE_OBJECT_CRTC:
   3436 		ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
   3437 		break;
   3438 	case DRM_MODE_OBJECT_PLANE:
   3439 		ret = drm_mode_plane_set_obj_prop(arg_obj, property, arg->value);
   3440 		break;
   3441 	}
   3442 
   3443 out:
   3444 	mutex_unlock(&dev->mode_config.mutex);
   3445 	return ret;
   3446 }
   3447 
   3448 int drm_mode_connector_attach_encoder(struct drm_connector *connector,
   3449 				      struct drm_encoder *encoder)
   3450 {
   3451 	int i;
   3452 
   3453 	for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
   3454 		if (connector->encoder_ids[i] == 0) {
   3455 			connector->encoder_ids[i] = encoder->base.id;
   3456 			return 0;
   3457 		}
   3458 	}
   3459 	return -ENOMEM;
   3460 }
   3461 EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
   3462 
   3463 void drm_mode_connector_detach_encoder(struct drm_connector *connector,
   3464 				    struct drm_encoder *encoder)
   3465 {
   3466 	int i;
   3467 	for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
   3468 		if (connector->encoder_ids[i] == encoder->base.id) {
   3469 			connector->encoder_ids[i] = 0;
   3470 			if (connector->encoder == encoder)
   3471 				connector->encoder = NULL;
   3472 			break;
   3473 		}
   3474 	}
   3475 }
   3476 EXPORT_SYMBOL(drm_mode_connector_detach_encoder);
   3477 
   3478 int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
   3479 				  int gamma_size)
   3480 {
   3481 	crtc->gamma_size = gamma_size;
   3482 
   3483 	crtc->gamma_store = kzalloc(gamma_size * sizeof(uint16_t) * 3, GFP_KERNEL);
   3484 	if (!crtc->gamma_store) {
   3485 		crtc->gamma_size = 0;
   3486 		return -ENOMEM;
   3487 	}
   3488 
   3489 	return 0;
   3490 }
   3491 EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
   3492 
   3493 int drm_mode_gamma_set_ioctl(struct drm_device *dev,
   3494 			     void *data, struct drm_file *file_priv)
   3495 {
   3496 	struct drm_mode_crtc_lut *crtc_lut = data;
   3497 	struct drm_mode_object *obj;
   3498 	struct drm_crtc *crtc;
   3499 	void *r_base, *g_base, *b_base;
   3500 	int size;
   3501 	int ret = 0;
   3502 
   3503 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
   3504 		return -EINVAL;
   3505 
   3506 	mutex_lock(&dev->mode_config.mutex);
   3507 	obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
   3508 	if (!obj) {
   3509 		ret = -EINVAL;
   3510 		goto out;
   3511 	}
   3512 	crtc = obj_to_crtc(obj);
   3513 
   3514 	if (crtc->funcs->gamma_set == NULL) {
   3515 		ret = -ENOSYS;
   3516 		goto out;
   3517 	}
   3518 
   3519 	/* memcpy into gamma store */
   3520 	if (crtc_lut->gamma_size != crtc->gamma_size) {
   3521 		ret = -EINVAL;
   3522 		goto out;
   3523 	}
   3524 
   3525 	size = crtc_lut->gamma_size * (sizeof(uint16_t));
   3526 	r_base = crtc->gamma_store;
   3527 	if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
   3528 		ret = -EFAULT;
   3529 		goto out;
   3530 	}
   3531 
   3532 #ifdef __NetBSD__
   3533 	g_base = (char *)r_base + size;
   3534 #else
   3535 	g_base = r_base + size;
   3536 #endif
   3537 	if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
   3538 		ret = -EFAULT;
   3539 		goto out;
   3540 	}
   3541 
   3542 #ifdef __NetBSD__
   3543 	b_base = (char *)g_base + size;
   3544 #else
   3545 	b_base = g_base + size;
   3546 #endif
   3547 	if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
   3548 		ret = -EFAULT;
   3549 		goto out;
   3550 	}
   3551 
   3552 	crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
   3553 
   3554 out:
   3555 	mutex_unlock(&dev->mode_config.mutex);
   3556 	return ret;
   3557 
   3558 }
   3559 
   3560 int drm_mode_gamma_get_ioctl(struct drm_device *dev,
   3561 			     void *data, struct drm_file *file_priv)
   3562 {
   3563 	struct drm_mode_crtc_lut *crtc_lut = data;
   3564 	struct drm_mode_object *obj;
   3565 	struct drm_crtc *crtc;
   3566 	void *r_base, *g_base, *b_base;
   3567 	int size;
   3568 	int ret = 0;
   3569 
   3570 	if (!drm_core_check_feature(dev, DRIVER_MODESET))
   3571 		return -EINVAL;
   3572 
   3573 	mutex_lock(&dev->mode_config.mutex);
   3574 	obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
   3575 	if (!obj) {
   3576 		ret = -EINVAL;
   3577 		goto out;
   3578 	}
   3579 	crtc = obj_to_crtc(obj);
   3580 
   3581 	/* memcpy into gamma store */
   3582 	if (crtc_lut->gamma_size != crtc->gamma_size) {
   3583 		ret = -EINVAL;
   3584 		goto out;
   3585 	}
   3586 
   3587 	size = crtc_lut->gamma_size * (sizeof(uint16_t));
   3588 	r_base = crtc->gamma_store;
   3589 	if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
   3590 		ret = -EFAULT;
   3591 		goto out;
   3592 	}
   3593 
   3594 #ifdef __NetBSD__
   3595 	g_base = (char *)r_base + size;
   3596 #else
   3597 	g_base = (char *)r_base + size;
   3598 #endif
   3599 	if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
   3600 		ret = -EFAULT;
   3601 		goto out;
   3602 	}
   3603 
   3604 #ifdef __NetBSD__
   3605 	b_base = (char *)g_base + size;
   3606 #else
   3607 	b_base = g_base + size;
   3608 #endif
   3609 	if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
   3610 		ret = -EFAULT;
   3611 		goto out;
   3612 	}
   3613 out:
   3614 	mutex_unlock(&dev->mode_config.mutex);
   3615 	return ret;
   3616 }
   3617 
   3618 int drm_mode_page_flip_ioctl(struct drm_device *dev,
   3619 			     void *data, struct drm_file *file_priv)
   3620 {
   3621 	struct drm_mode_crtc_page_flip *page_flip = data;
   3622 	struct drm_mode_object *obj;
   3623 	struct drm_crtc *crtc;
   3624 	struct drm_framebuffer *fb;
   3625 	struct drm_pending_vblank_event *e = NULL;
   3626 	unsigned long flags;
   3627 	int hdisplay, vdisplay;
   3628 	int ret = -EINVAL;
   3629 
   3630 	if (page_flip->flags & ~DRM_MODE_PAGE_FLIP_FLAGS ||
   3631 	    page_flip->reserved != 0)
   3632 		return -EINVAL;
   3633 
   3634 	mutex_lock(&dev->mode_config.mutex);
   3635 	obj = drm_mode_object_find(dev, page_flip->crtc_id, DRM_MODE_OBJECT_CRTC);
   3636 	if (!obj)
   3637 		goto out;
   3638 	crtc = obj_to_crtc(obj);
   3639 
   3640 	if (crtc->fb == NULL) {
   3641 		/* The framebuffer is currently unbound, presumably
   3642 		 * due to a hotplug event, that userspace has not
   3643 		 * yet discovered.
   3644 		 */
   3645 		ret = -EBUSY;
   3646 		goto out;
   3647 	}
   3648 
   3649 	if (crtc->funcs->page_flip == NULL)
   3650 		goto out;
   3651 
   3652 	obj = drm_mode_object_find(dev, page_flip->fb_id, DRM_MODE_OBJECT_FB);
   3653 	if (!obj)
   3654 		goto out;
   3655 	fb = obj_to_fb(obj);
   3656 
   3657 	hdisplay = crtc->mode.hdisplay;
   3658 	vdisplay = crtc->mode.vdisplay;
   3659 
   3660 	if (crtc->invert_dimensions)
   3661 		swap(hdisplay, vdisplay);
   3662 
   3663 	if (hdisplay > fb->width ||
   3664 	    vdisplay > fb->height ||
   3665 	    crtc->x > fb->width - hdisplay ||
   3666 	    crtc->y > fb->height - vdisplay) {
   3667 		DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
   3668 			      fb->width, fb->height, hdisplay, vdisplay, crtc->x, crtc->y,
   3669 			      crtc->invert_dimensions ? " (inverted)" : "");
   3670 		ret = -ENOSPC;
   3671 		goto out;
   3672 	}
   3673 
   3674 	if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
   3675 		ret = -ENOMEM;
   3676 		spin_lock_irqsave(&dev->event_lock, flags);
   3677 		if (file_priv->event_space < sizeof e->event) {
   3678 			spin_unlock_irqrestore(&dev->event_lock, flags);
   3679 			goto out;
   3680 		}
   3681 		file_priv->event_space -= sizeof e->event;
   3682 		spin_unlock_irqrestore(&dev->event_lock, flags);
   3683 
   3684 		e = kzalloc(sizeof *e, GFP_KERNEL);
   3685 		if (e == NULL) {
   3686 			spin_lock_irqsave(&dev->event_lock, flags);
   3687 			file_priv->event_space += sizeof e->event;
   3688 			spin_unlock_irqrestore(&dev->event_lock, flags);
   3689 			goto out;
   3690 		}
   3691 
   3692 		e->event.base.type = DRM_EVENT_FLIP_COMPLETE;
   3693 		e->event.base.length = sizeof e->event;
   3694 		e->event.user_data = page_flip->user_data;
   3695 		e->base.event = &e->event.base;
   3696 		e->base.file_priv = file_priv;
   3697 		e->base.destroy =
   3698 			(void (*) (struct drm_pending_event *)) kfree;
   3699 	}
   3700 
   3701 	ret = crtc->funcs->page_flip(crtc, fb, e);
   3702 	if (ret) {
   3703 		if (page_flip->flags & DRM_MODE_PAGE_FLIP_EVENT) {
   3704 			spin_lock_irqsave(&dev->event_lock, flags);
   3705 			file_priv->event_space += sizeof e->event;
   3706 			spin_unlock_irqrestore(&dev->event_lock, flags);
   3707 			kfree(e);
   3708 		}
   3709 	}
   3710 
   3711 out:
   3712 	mutex_unlock(&dev->mode_config.mutex);
   3713 	return ret;
   3714 }
   3715 
   3716 void drm_mode_config_reset(struct drm_device *dev)
   3717 {
   3718 	struct drm_crtc *crtc;
   3719 	struct drm_encoder *encoder;
   3720 	struct drm_connector *connector;
   3721 
   3722 	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
   3723 		if (crtc->funcs->reset)
   3724 			crtc->funcs->reset(crtc);
   3725 
   3726 	list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
   3727 		if (encoder->funcs->reset)
   3728 			encoder->funcs->reset(encoder);
   3729 
   3730 	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
   3731 		connector->status = connector_status_unknown;
   3732 
   3733 		if (connector->funcs->reset)
   3734 			connector->funcs->reset(connector);
   3735 	}
   3736 }
   3737 EXPORT_SYMBOL(drm_mode_config_reset);
   3738 
   3739 int drm_mode_create_dumb_ioctl(struct drm_device *dev,
   3740 			       void *data, struct drm_file *file_priv)
   3741 {
   3742 	struct drm_mode_create_dumb *args = data;
   3743 
   3744 	if (!dev->driver->dumb_create)
   3745 		return -ENOSYS;
   3746 	return dev->driver->dumb_create(file_priv, dev, args);
   3747 }
   3748 
   3749 int drm_mode_mmap_dumb_ioctl(struct drm_device *dev,
   3750 			     void *data, struct drm_file *file_priv)
   3751 {
   3752 	struct drm_mode_map_dumb *args = data;
   3753 
   3754 	/* call driver ioctl to get mmap offset */
   3755 	if (!dev->driver->dumb_map_offset)
   3756 		return -ENOSYS;
   3757 
   3758 	return dev->driver->dumb_map_offset(file_priv, dev, args->handle, &args->offset);
   3759 }
   3760 
   3761 int drm_mode_destroy_dumb_ioctl(struct drm_device *dev,
   3762 				void *data, struct drm_file *file_priv)
   3763 {
   3764 	struct drm_mode_destroy_dumb *args = data;
   3765 
   3766 	if (!dev->driver->dumb_destroy)
   3767 		return -ENOSYS;
   3768 
   3769 	return dev->driver->dumb_destroy(file_priv, dev, args->handle);
   3770 }
   3771 
   3772 /*
   3773  * Just need to support RGB formats here for compat with code that doesn't
   3774  * use pixel formats directly yet.
   3775  */
   3776 void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
   3777 			  int *bpp)
   3778 {
   3779 	switch (format) {
   3780 	case DRM_FORMAT_RGB332:
   3781 	case DRM_FORMAT_BGR233:
   3782 		*depth = 8;
   3783 		*bpp = 8;
   3784 		break;
   3785 	case DRM_FORMAT_XRGB1555:
   3786 	case DRM_FORMAT_XBGR1555:
   3787 	case DRM_FORMAT_RGBX5551:
   3788 	case DRM_FORMAT_BGRX5551:
   3789 	case DRM_FORMAT_ARGB1555:
   3790 	case DRM_FORMAT_ABGR1555:
   3791 	case DRM_FORMAT_RGBA5551:
   3792 	case DRM_FORMAT_BGRA5551:
   3793 		*depth = 15;
   3794 		*bpp = 16;
   3795 		break;
   3796 	case DRM_FORMAT_RGB565:
   3797 	case DRM_FORMAT_BGR565:
   3798 		*depth = 16;
   3799 		*bpp = 16;
   3800 		break;
   3801 	case DRM_FORMAT_RGB888:
   3802 	case DRM_FORMAT_BGR888:
   3803 		*depth = 24;
   3804 		*bpp = 24;
   3805 		break;
   3806 	case DRM_FORMAT_XRGB8888:
   3807 	case DRM_FORMAT_XBGR8888:
   3808 	case DRM_FORMAT_RGBX8888:
   3809 	case DRM_FORMAT_BGRX8888:
   3810 		*depth = 24;
   3811 		*bpp = 32;
   3812 		break;
   3813 	case DRM_FORMAT_XRGB2101010:
   3814 	case DRM_FORMAT_XBGR2101010:
   3815 	case DRM_FORMAT_RGBX1010102:
   3816 	case DRM_FORMAT_BGRX1010102:
   3817 	case DRM_FORMAT_ARGB2101010:
   3818 	case DRM_FORMAT_ABGR2101010:
   3819 	case DRM_FORMAT_RGBA1010102:
   3820 	case DRM_FORMAT_BGRA1010102:
   3821 		*depth = 30;
   3822 		*bpp = 32;
   3823 		break;
   3824 	case DRM_FORMAT_ARGB8888:
   3825 	case DRM_FORMAT_ABGR8888:
   3826 	case DRM_FORMAT_RGBA8888:
   3827 	case DRM_FORMAT_BGRA8888:
   3828 		*depth = 32;
   3829 		*bpp = 32;
   3830 		break;
   3831 	default:
   3832 		DRM_DEBUG_KMS("unsupported pixel format\n");
   3833 		*depth = 0;
   3834 		*bpp = 0;
   3835 		break;
   3836 	}
   3837 }
   3838 EXPORT_SYMBOL(drm_fb_get_bpp_depth);
   3839 
   3840 /**
   3841  * drm_format_num_planes - get the number of planes for format
   3842  * @format: pixel format (DRM_FORMAT_*)
   3843  *
   3844  * RETURNS:
   3845  * The number of planes used by the specified pixel format.
   3846  */
   3847 int drm_format_num_planes(uint32_t format)
   3848 {
   3849 	switch (format) {
   3850 	case DRM_FORMAT_YUV410:
   3851 	case DRM_FORMAT_YVU410:
   3852 	case DRM_FORMAT_YUV411:
   3853 	case DRM_FORMAT_YVU411:
   3854 	case DRM_FORMAT_YUV420:
   3855 	case DRM_FORMAT_YVU420:
   3856 	case DRM_FORMAT_YUV422:
   3857 	case DRM_FORMAT_YVU422:
   3858 	case DRM_FORMAT_YUV444:
   3859 	case DRM_FORMAT_YVU444:
   3860 		return 3;
   3861 	case DRM_FORMAT_NV12:
   3862 	case DRM_FORMAT_NV21:
   3863 	case DRM_FORMAT_NV16:
   3864 	case DRM_FORMAT_NV61:
   3865 	case DRM_FORMAT_NV24:
   3866 	case DRM_FORMAT_NV42:
   3867 		return 2;
   3868 	default:
   3869 		return 1;
   3870 	}
   3871 }
   3872 EXPORT_SYMBOL(drm_format_num_planes);
   3873 
   3874 /**
   3875  * drm_format_plane_cpp - determine the bytes per pixel value
   3876  * @format: pixel format (DRM_FORMAT_*)
   3877  * @plane: plane index
   3878  *
   3879  * RETURNS:
   3880  * The bytes per pixel value for the specified plane.
   3881  */
   3882 int drm_format_plane_cpp(uint32_t format, int plane)
   3883 {
   3884 	unsigned int depth;
   3885 	int bpp;
   3886 
   3887 	if (plane >= drm_format_num_planes(format))
   3888 		return 0;
   3889 
   3890 	switch (format) {
   3891 	case DRM_FORMAT_YUYV:
   3892 	case DRM_FORMAT_YVYU:
   3893 	case DRM_FORMAT_UYVY:
   3894 	case DRM_FORMAT_VYUY:
   3895 		return 2;
   3896 	case DRM_FORMAT_NV12:
   3897 	case DRM_FORMAT_NV21:
   3898 	case DRM_FORMAT_NV16:
   3899 	case DRM_FORMAT_NV61:
   3900 	case DRM_FORMAT_NV24:
   3901 	case DRM_FORMAT_NV42:
   3902 		return plane ? 2 : 1;
   3903 	case DRM_FORMAT_YUV410:
   3904 	case DRM_FORMAT_YVU410:
   3905 	case DRM_FORMAT_YUV411:
   3906 	case DRM_FORMAT_YVU411:
   3907 	case DRM_FORMAT_YUV420:
   3908 	case DRM_FORMAT_YVU420:
   3909 	case DRM_FORMAT_YUV422:
   3910 	case DRM_FORMAT_YVU422:
   3911 	case DRM_FORMAT_YUV444:
   3912 	case DRM_FORMAT_YVU444:
   3913 		return 1;
   3914 	default:
   3915 		drm_fb_get_bpp_depth(format, &depth, &bpp);
   3916 		return bpp >> 3;
   3917 	}
   3918 }
   3919 EXPORT_SYMBOL(drm_format_plane_cpp);
   3920 
   3921 /**
   3922  * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
   3923  * @format: pixel format (DRM_FORMAT_*)
   3924  *
   3925  * RETURNS:
   3926  * The horizontal chroma subsampling factor for the
   3927  * specified pixel format.
   3928  */
   3929 int drm_format_horz_chroma_subsampling(uint32_t format)
   3930 {
   3931 	switch (format) {
   3932 	case DRM_FORMAT_YUV411:
   3933 	case DRM_FORMAT_YVU411:
   3934 	case DRM_FORMAT_YUV410:
   3935 	case DRM_FORMAT_YVU410:
   3936 		return 4;
   3937 	case DRM_FORMAT_YUYV:
   3938 	case DRM_FORMAT_YVYU:
   3939 	case DRM_FORMAT_UYVY:
   3940 	case DRM_FORMAT_VYUY:
   3941 	case DRM_FORMAT_NV12:
   3942 	case DRM_FORMAT_NV21:
   3943 	case DRM_FORMAT_NV16:
   3944 	case DRM_FORMAT_NV61:
   3945 	case DRM_FORMAT_YUV422:
   3946 	case DRM_FORMAT_YVU422:
   3947 	case DRM_FORMAT_YUV420:
   3948 	case DRM_FORMAT_YVU420:
   3949 		return 2;
   3950 	default:
   3951 		return 1;
   3952 	}
   3953 }
   3954 EXPORT_SYMBOL(drm_format_horz_chroma_subsampling);
   3955 
   3956 /**
   3957  * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
   3958  * @format: pixel format (DRM_FORMAT_*)
   3959  *
   3960  * RETURNS:
   3961  * The vertical chroma subsampling factor for the
   3962  * specified pixel format.
   3963  */
   3964 int drm_format_vert_chroma_subsampling(uint32_t format)
   3965 {
   3966 	switch (format) {
   3967 	case DRM_FORMAT_YUV410:
   3968 	case DRM_FORMAT_YVU410:
   3969 		return 4;
   3970 	case DRM_FORMAT_YUV420:
   3971 	case DRM_FORMAT_YVU420:
   3972 	case DRM_FORMAT_NV12:
   3973 	case DRM_FORMAT_NV21:
   3974 		return 2;
   3975 	default:
   3976 		return 1;
   3977 	}
   3978 }
   3979 EXPORT_SYMBOL(drm_format_vert_chroma_subsampling);
   3980