linux/drivers/gpu/drm/drm_crtc.c
<<
>>
Prefs
   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)
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);
3832