Linux Audio

Check our new training course

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