Subversion Repositories Kolibri OS

Rev

Rev 4560 | Rev 5271 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  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/ctype.h>
  33. #include <linux/list.h>
  34. #include <linux/slab.h>
  35. #include <linux/export.h>
  36. #include <drm/drmP.h>
  37. #include <drm/drm_crtc.h>
  38. #include <drm/drm_edid.h>
  39. #include <drm/drm_fourcc.h>
  40. #include <drm/drm_modeset_lock.h>
  41.  
  42. #include "drm_crtc_internal.h"
  43.  
  44. static struct drm_framebuffer *add_framebuffer_internal(struct drm_device *dev,
  45.                                                         struct drm_mode_fb_cmd2 *r,
  46.                                                         struct drm_file *file_priv);
  47.  
  48. /**
  49.  * drm_modeset_lock_all - take all modeset locks
  50.  * @dev: drm device
  51.  *
  52.  * This function takes all modeset locks, suitable where a more fine-grained
  53.  * scheme isn't (yet) implemented. Locks must be dropped with
  54.  * drm_modeset_unlock_all.
  55.  */
  56. void drm_modeset_lock_all(struct drm_device *dev)
  57. {
  58.         struct drm_mode_config *config = &dev->mode_config;
  59.         struct drm_modeset_acquire_ctx *ctx;
  60.         int ret;
  61.  
  62.         ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  63.         if (WARN_ON(!ctx))
  64.                 return;
  65.  
  66.         mutex_lock(&config->mutex);
  67.  
  68.         drm_modeset_acquire_init(ctx, 0);
  69.  
  70. retry:
  71.         ret = drm_modeset_lock(&config->connection_mutex, ctx);
  72.         if (ret)
  73.                 goto fail;
  74.         ret = drm_modeset_lock_all_crtcs(dev, ctx);
  75.         if (ret)
  76.                 goto fail;
  77.  
  78.         WARN_ON(config->acquire_ctx);
  79.  
  80.         /* now we hold the locks, so now that it is safe, stash the
  81.          * ctx for drm_modeset_unlock_all():
  82.          */
  83.         config->acquire_ctx = ctx;
  84.  
  85.         drm_warn_on_modeset_not_all_locked(dev);
  86.  
  87.         return;
  88.  
  89. fail:
  90.         if (ret == -EDEADLK) {
  91.                 drm_modeset_backoff(ctx);
  92.                 goto retry;
  93.         }
  94. }
  95. EXPORT_SYMBOL(drm_modeset_lock_all);
  96.  
  97. /**
  98.  * drm_modeset_unlock_all - drop all modeset locks
  99.  * @dev: device
  100.  *
  101.  * This function drop all modeset locks taken by drm_modeset_lock_all.
  102.  */
  103. void drm_modeset_unlock_all(struct drm_device *dev)
  104. {
  105.         struct drm_mode_config *config = &dev->mode_config;
  106.         struct drm_modeset_acquire_ctx *ctx = config->acquire_ctx;
  107.  
  108.         if (WARN_ON(!ctx))
  109.                 return;
  110.  
  111.         config->acquire_ctx = NULL;
  112.         drm_modeset_drop_locks(ctx);
  113.         drm_modeset_acquire_fini(ctx);
  114.  
  115.         kfree(ctx);
  116.  
  117.         mutex_unlock(&dev->mode_config.mutex);
  118. }
  119. EXPORT_SYMBOL(drm_modeset_unlock_all);
  120.  
  121. /**
  122.  * drm_warn_on_modeset_not_all_locked - check that all modeset locks are locked
  123.  * @dev: device
  124.  *
  125.  * Useful as a debug assert.
  126.  */
  127. void drm_warn_on_modeset_not_all_locked(struct drm_device *dev)
  128. {
  129.         struct drm_crtc *crtc;
  130.  
  131.         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
  132.                 WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
  133.  
  134.         WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
  135.         WARN_ON(!mutex_is_locked(&dev->mode_config.mutex));
  136. }
  137. EXPORT_SYMBOL(drm_warn_on_modeset_not_all_locked);
  138.  
  139. /* Avoid boilerplate.  I'm tired of typing. */
  140. #define DRM_ENUM_NAME_FN(fnname, list)                          \
  141.         const char *fnname(int val)                             \
  142.         {                                                       \
  143.                 int i;                                          \
  144.                 for (i = 0; i < ARRAY_SIZE(list); i++) {        \
  145.                         if (list[i].type == val)                \
  146.                                 return list[i].name;            \
  147.                 }                                               \
  148.                 return "(unknown)";                             \
  149.         }
  150.  
  151. /*
  152.  * Global properties
  153.  */
  154. static const struct drm_prop_enum_list drm_dpms_enum_list[] =
  155. {       { DRM_MODE_DPMS_ON, "On" },
  156.         { DRM_MODE_DPMS_STANDBY, "Standby" },
  157.         { DRM_MODE_DPMS_SUSPEND, "Suspend" },
  158.         { DRM_MODE_DPMS_OFF, "Off" }
  159. };
  160.  
  161. DRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list)
  162.  
  163. static const struct drm_prop_enum_list drm_plane_type_enum_list[] =
  164. {
  165.         { DRM_PLANE_TYPE_OVERLAY, "Overlay" },
  166.         { DRM_PLANE_TYPE_PRIMARY, "Primary" },
  167.         { DRM_PLANE_TYPE_CURSOR, "Cursor" },
  168. };
  169.  
  170. /*
  171.  * Optional properties
  172.  */
  173. static const struct drm_prop_enum_list drm_scaling_mode_enum_list[] =
  174. {
  175.         { DRM_MODE_SCALE_NONE, "None" },
  176.         { DRM_MODE_SCALE_FULLSCREEN, "Full" },
  177.         { DRM_MODE_SCALE_CENTER, "Center" },
  178.         { DRM_MODE_SCALE_ASPECT, "Full aspect" },
  179. };
  180.  
  181. static const struct drm_prop_enum_list drm_aspect_ratio_enum_list[] = {
  182.         { DRM_MODE_PICTURE_ASPECT_NONE, "Automatic" },
  183.         { DRM_MODE_PICTURE_ASPECT_4_3, "4:3" },
  184.         { DRM_MODE_PICTURE_ASPECT_16_9, "16:9" },
  185. };
  186.  
  187. /*
  188.  * Non-global properties, but "required" for certain connectors.
  189.  */
  190. static const struct drm_prop_enum_list drm_dvi_i_select_enum_list[] =
  191. {
  192.         { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
  193.         { DRM_MODE_SUBCONNECTOR_DVID,      "DVI-D"     }, /* DVI-I  */
  194.         { DRM_MODE_SUBCONNECTOR_DVIA,      "DVI-A"     }, /* DVI-I  */
  195. };
  196.  
  197. DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list)
  198.  
  199. static const struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list[] =
  200. {
  201.         { DRM_MODE_SUBCONNECTOR_Unknown,   "Unknown"   }, /* DVI-I and TV-out */
  202.         { DRM_MODE_SUBCONNECTOR_DVID,      "DVI-D"     }, /* DVI-I  */
  203.         { DRM_MODE_SUBCONNECTOR_DVIA,      "DVI-A"     }, /* DVI-I  */
  204. };
  205.  
  206. DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name,
  207.                  drm_dvi_i_subconnector_enum_list)
  208.  
  209. static const struct drm_prop_enum_list drm_tv_select_enum_list[] =
  210. {
  211.         { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
  212.         { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
  213.         { DRM_MODE_SUBCONNECTOR_SVIDEO,    "SVIDEO"    }, /* TV-out */
  214.         { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
  215.         { DRM_MODE_SUBCONNECTOR_SCART,     "SCART"     }, /* TV-out */
  216. };
  217.  
  218. DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
  219.  
  220. static const struct drm_prop_enum_list drm_tv_subconnector_enum_list[] =
  221. {
  222.         { DRM_MODE_SUBCONNECTOR_Unknown,   "Unknown"   }, /* DVI-I and TV-out */
  223.         { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
  224.         { DRM_MODE_SUBCONNECTOR_SVIDEO,    "SVIDEO"    }, /* TV-out */
  225.         { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
  226.         { DRM_MODE_SUBCONNECTOR_SCART,     "SCART"     }, /* TV-out */
  227. };
  228.  
  229. DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,
  230.                  drm_tv_subconnector_enum_list)
  231.  
  232. static const struct drm_prop_enum_list drm_dirty_info_enum_list[] = {
  233.         { DRM_MODE_DIRTY_OFF,      "Off"      },
  234.         { DRM_MODE_DIRTY_ON,       "On"       },
  235.         { DRM_MODE_DIRTY_ANNOTATE, "Annotate" },
  236. };
  237.  
  238. struct drm_conn_prop_enum_list {
  239.         int type;
  240.         const char *name;
  241.         struct ida ida;
  242. };
  243.  
  244. /*
  245.  * Connector and encoder types.
  246.  */
  247. static struct drm_conn_prop_enum_list drm_connector_enum_list[] =
  248. {       { DRM_MODE_CONNECTOR_Unknown, "Unknown" },
  249.         { DRM_MODE_CONNECTOR_VGA, "VGA" },
  250.         { DRM_MODE_CONNECTOR_DVII, "DVI-I" },
  251.         { DRM_MODE_CONNECTOR_DVID, "DVI-D" },
  252.         { DRM_MODE_CONNECTOR_DVIA, "DVI-A" },
  253.         { DRM_MODE_CONNECTOR_Composite, "Composite" },
  254.         { DRM_MODE_CONNECTOR_SVIDEO, "SVIDEO" },
  255.         { DRM_MODE_CONNECTOR_LVDS, "LVDS" },
  256.         { DRM_MODE_CONNECTOR_Component, "Component" },
  257.         { DRM_MODE_CONNECTOR_9PinDIN, "DIN" },
  258.         { DRM_MODE_CONNECTOR_DisplayPort, "DP" },
  259.         { DRM_MODE_CONNECTOR_HDMIA, "HDMI-A" },
  260.         { DRM_MODE_CONNECTOR_HDMIB, "HDMI-B" },
  261.         { DRM_MODE_CONNECTOR_TV, "TV" },
  262.         { DRM_MODE_CONNECTOR_eDP, "eDP" },
  263.         { DRM_MODE_CONNECTOR_VIRTUAL, "Virtual" },
  264.         { DRM_MODE_CONNECTOR_DSI, "DSI" },
  265. };
  266.  
  267. static const struct drm_prop_enum_list drm_encoder_enum_list[] =
  268. {       { DRM_MODE_ENCODER_NONE, "None" },
  269.         { DRM_MODE_ENCODER_DAC, "DAC" },
  270.         { DRM_MODE_ENCODER_TMDS, "TMDS" },
  271.         { DRM_MODE_ENCODER_LVDS, "LVDS" },
  272.         { DRM_MODE_ENCODER_TVDAC, "TV" },
  273.         { DRM_MODE_ENCODER_VIRTUAL, "Virtual" },
  274.         { DRM_MODE_ENCODER_DSI, "DSI" },
  275.         { DRM_MODE_ENCODER_DPMST, "DP MST" },
  276. };
  277.  
  278. static const struct drm_prop_enum_list drm_subpixel_enum_list[] =
  279. {
  280.         { SubPixelUnknown, "Unknown" },
  281.         { SubPixelHorizontalRGB, "Horizontal RGB" },
  282.         { SubPixelHorizontalBGR, "Horizontal BGR" },
  283.         { SubPixelVerticalRGB, "Vertical RGB" },
  284.         { SubPixelVerticalBGR, "Vertical BGR" },
  285.         { SubPixelNone, "None" },
  286. };
  287.  
  288. void drm_connector_ida_init(void)
  289. {
  290.         int i;
  291.  
  292.         for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
  293.                 ida_init(&drm_connector_enum_list[i].ida);
  294. }
  295.  
  296. void drm_connector_ida_destroy(void)
  297. {
  298.         int i;
  299.  
  300.         for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
  301.                 ida_destroy(&drm_connector_enum_list[i].ida);
  302. }
  303.  
  304. /**
  305.  * drm_get_connector_status_name - return a string for connector status
  306.  * @status: connector status to compute name of
  307.  *
  308.  * In contrast to the other drm_get_*_name functions this one here returns a
  309.  * const pointer and hence is threadsafe.
  310.  */
  311. const char *drm_get_connector_status_name(enum drm_connector_status status)
  312. {
  313.         if (status == connector_status_connected)
  314.                 return "connected";
  315.         else if (status == connector_status_disconnected)
  316.                 return "disconnected";
  317.         else
  318.                 return "unknown";
  319. }
  320. EXPORT_SYMBOL(drm_get_connector_status_name);
  321.  
  322. /**
  323.  * drm_get_subpixel_order_name - return a string for a given subpixel enum
  324.  * @order: enum of subpixel_order
  325.  *
  326.  * Note you could abuse this and return something out of bounds, but that
  327.  * would be a caller error.  No unscrubbed user data should make it here.
  328.  */
  329. const char *drm_get_subpixel_order_name(enum subpixel_order order)
  330. {
  331.         return drm_subpixel_enum_list[order].name;
  332. }
  333. EXPORT_SYMBOL(drm_get_subpixel_order_name);
  334.  
  335. static char printable_char(int c)
  336. {
  337.         return isascii(c) && isprint(c) ? c : '?';
  338. }
  339.  
  340. /**
  341.  * drm_get_format_name - return a string for drm fourcc format
  342.  * @format: format to compute name of
  343.  *
  344.  * Note that the buffer used by this function is globally shared and owned by
  345.  * the function itself.
  346.  *
  347.  * FIXME: This isn't really multithreading safe.
  348.  */
  349. const char *drm_get_format_name(uint32_t format)
  350. {
  351.         static char buf[32];
  352.  
  353.         snprintf(buf, sizeof(buf),
  354.                  "%c%c%c%c %s-endian (0x%08x)",
  355.                  printable_char(format & 0xff),
  356.                  printable_char((format >> 8) & 0xff),
  357.                  printable_char((format >> 16) & 0xff),
  358.                  printable_char((format >> 24) & 0x7f),
  359.                  format & DRM_FORMAT_BIG_ENDIAN ? "big" : "little",
  360.                  format);
  361.  
  362.         return buf;
  363. }
  364. EXPORT_SYMBOL(drm_get_format_name);
  365.  
  366. /*
  367.  * Internal function to assign a slot in the object idr and optionally
  368.  * register the object into the idr.
  369.  */
  370. static int drm_mode_object_get_reg(struct drm_device *dev,
  371.                                    struct drm_mode_object *obj,
  372.                                    uint32_t obj_type,
  373.                                    bool register_obj)
  374. {
  375.         int ret;
  376.  
  377.         mutex_lock(&dev->mode_config.idr_mutex);
  378.         ret = idr_alloc(&dev->mode_config.crtc_idr, register_obj ? obj : NULL, 1, 0, GFP_KERNEL);
  379.         if (ret >= 0) {
  380.                 /*
  381.                  * Set up the object linking under the protection of the idr
  382.                  * lock so that other users can't see inconsistent state.
  383.                  */
  384.                 obj->id = ret;
  385.                 obj->type = obj_type;
  386.         }
  387.         mutex_unlock(&dev->mode_config.idr_mutex);
  388.  
  389.         return ret < 0 ? ret : 0;
  390. }
  391.  
  392. /**
  393.  * drm_mode_object_get - allocate a new modeset identifier
  394.  * @dev: DRM device
  395.  * @obj: object pointer, used to generate unique ID
  396.  * @obj_type: object type
  397.  *
  398.  * Create a unique identifier based on @ptr in @dev's identifier space.  Used
  399.  * for tracking modes, CRTCs and connectors. Note that despite the _get postfix
  400.  * modeset identifiers are _not_ reference counted. Hence don't use this for
  401.  * reference counted modeset objects like framebuffers.
  402.  *
  403.  * Returns:
  404.  * New unique (relative to other objects in @dev) integer identifier for the
  405.  * object.
  406.  */
  407. int drm_mode_object_get(struct drm_device *dev,
  408.                                struct drm_mode_object *obj, uint32_t obj_type)
  409. {
  410.         return drm_mode_object_get_reg(dev, obj, obj_type, true);
  411. }
  412.  
  413. static void drm_mode_object_register(struct drm_device *dev,
  414.                                      struct drm_mode_object *obj)
  415. {
  416.         mutex_lock(&dev->mode_config.idr_mutex);
  417.         idr_replace(&dev->mode_config.crtc_idr, obj, obj->id);
  418.         mutex_unlock(&dev->mode_config.idr_mutex);
  419. }
  420.  
  421. /**
  422.  * drm_mode_object_put - free a modeset identifer
  423.  * @dev: DRM device
  424.  * @object: object to free
  425.  *
  426.  * Free @id from @dev's unique identifier pool. Note that despite the _get
  427.  * postfix modeset identifiers are _not_ reference counted. Hence don't use this
  428.  * for reference counted modeset objects like framebuffers.
  429.  */
  430. void drm_mode_object_put(struct drm_device *dev,
  431.                                 struct drm_mode_object *object)
  432. {
  433.         mutex_lock(&dev->mode_config.idr_mutex);
  434.         idr_remove(&dev->mode_config.crtc_idr, object->id);
  435.         mutex_unlock(&dev->mode_config.idr_mutex);
  436. }
  437.  
  438. static struct drm_mode_object *_object_find(struct drm_device *dev,
  439.                 uint32_t id, uint32_t type)
  440. {
  441.         struct drm_mode_object *obj = NULL;
  442.  
  443.         mutex_lock(&dev->mode_config.idr_mutex);
  444.         obj = idr_find(&dev->mode_config.crtc_idr, id);
  445.         if (obj && type != DRM_MODE_OBJECT_ANY && obj->type != type)
  446.                 obj = NULL;
  447.         if (obj && obj->id != id)
  448.                 obj = NULL;
  449.         /* don't leak out unref'd fb's */
  450.         if (obj && (obj->type == DRM_MODE_OBJECT_FB))
  451.                 obj = NULL;
  452.         mutex_unlock(&dev->mode_config.idr_mutex);
  453.  
  454.         return obj;
  455. }
  456.  
  457. /**
  458.  * drm_mode_object_find - look up a drm object with static lifetime
  459.  * @dev: drm device
  460.  * @id: id of the mode object
  461.  * @type: type of the mode object
  462.  *
  463.  * Note that framebuffers cannot be looked up with this functions - since those
  464.  * are reference counted, they need special treatment.  Even with
  465.  * DRM_MODE_OBJECT_ANY (although that will simply return NULL
  466.  * rather than WARN_ON()).
  467.  */
  468. struct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
  469.                 uint32_t id, uint32_t type)
  470. {
  471.         struct drm_mode_object *obj = NULL;
  472.  
  473.         /* Framebuffers are reference counted and need their own lookup
  474.          * function.*/
  475.         WARN_ON(type == DRM_MODE_OBJECT_FB);
  476.         obj = _object_find(dev, id, type);
  477.         return obj;
  478. }
  479. EXPORT_SYMBOL(drm_mode_object_find);
  480.  
  481. /**
  482.  * drm_framebuffer_init - initialize a framebuffer
  483.  * @dev: DRM device
  484.  * @fb: framebuffer to be initialized
  485.  * @funcs: ... with these functions
  486.  *
  487.  * Allocates an ID for the framebuffer's parent mode object, sets its mode
  488.  * functions & device file and adds it to the master fd list.
  489.  *
  490.  * IMPORTANT:
  491.  * This functions publishes the fb and makes it available for concurrent access
  492.  * by other users. Which means by this point the fb _must_ be fully set up -
  493.  * since all the fb attributes are invariant over its lifetime, no further
  494.  * locking but only correct reference counting is required.
  495.  *
  496.  * Returns:
  497.  * Zero on success, error code on failure.
  498.  */
  499. int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
  500.                          const struct drm_framebuffer_funcs *funcs)
  501. {
  502.         int ret;
  503.  
  504.         mutex_lock(&dev->mode_config.fb_lock);
  505.         kref_init(&fb->refcount);
  506.         INIT_LIST_HEAD(&fb->filp_head);
  507.         fb->dev = dev;
  508.         fb->funcs = funcs;
  509.  
  510.         ret = drm_mode_object_get(dev, &fb->base, DRM_MODE_OBJECT_FB);
  511.         if (ret)
  512.                 goto out;
  513.  
  514.         /* Grab the idr reference. */
  515.         drm_framebuffer_reference(fb);
  516.  
  517.         dev->mode_config.num_fb++;
  518.         list_add(&fb->head, &dev->mode_config.fb_list);
  519. out:
  520.         mutex_unlock(&dev->mode_config.fb_lock);
  521.  
  522.         return 0;
  523. }
  524. EXPORT_SYMBOL(drm_framebuffer_init);
  525.  
  526. static void drm_framebuffer_free(struct kref *kref)
  527. {
  528.         struct drm_framebuffer *fb =
  529.                         container_of(kref, struct drm_framebuffer, refcount);
  530.         fb->funcs->destroy(fb);
  531. }
  532.  
  533. static struct drm_framebuffer *__drm_framebuffer_lookup(struct drm_device *dev,
  534.                                                         uint32_t id)
  535. {
  536.         struct drm_mode_object *obj = NULL;
  537.         struct drm_framebuffer *fb;
  538.  
  539.         mutex_lock(&dev->mode_config.idr_mutex);
  540.         obj = idr_find(&dev->mode_config.crtc_idr, id);
  541.         if (!obj || (obj->type != DRM_MODE_OBJECT_FB) || (obj->id != id))
  542.                 fb = NULL;
  543.         else
  544.                 fb = obj_to_fb(obj);
  545.         mutex_unlock(&dev->mode_config.idr_mutex);
  546.  
  547.         return fb;
  548. }
  549.  
  550. /**
  551.  * drm_framebuffer_lookup - look up a drm framebuffer and grab a reference
  552.  * @dev: drm device
  553.  * @id: id of the fb object
  554.  *
  555.  * If successful, this grabs an additional reference to the framebuffer -
  556.  * callers need to make sure to eventually unreference the returned framebuffer
  557.  * again, using @drm_framebuffer_unreference.
  558.  */
  559. struct drm_framebuffer *drm_framebuffer_lookup(struct drm_device *dev,
  560.                                                uint32_t id)
  561. {
  562.         struct drm_framebuffer *fb;
  563.  
  564.         mutex_lock(&dev->mode_config.fb_lock);
  565.         fb = __drm_framebuffer_lookup(dev, id);
  566.         if (fb)
  567.                 drm_framebuffer_reference(fb);
  568.         mutex_unlock(&dev->mode_config.fb_lock);
  569.  
  570.         return fb;
  571. }
  572. EXPORT_SYMBOL(drm_framebuffer_lookup);
  573.  
  574. /**
  575.  * drm_framebuffer_unreference - unref a framebuffer
  576.  * @fb: framebuffer to unref
  577.  *
  578.  * This functions decrements the fb's refcount and frees it if it drops to zero.
  579.  */
  580. void drm_framebuffer_unreference(struct drm_framebuffer *fb)
  581. {
  582.         DRM_DEBUG("%p: FB ID: %d (%d)\n", fb, fb->base.id, atomic_read(&fb->refcount.refcount));
  583.         kref_put(&fb->refcount, drm_framebuffer_free);
  584. }
  585. EXPORT_SYMBOL(drm_framebuffer_unreference);
  586.  
  587. /**
  588.  * drm_framebuffer_reference - incr the fb refcnt
  589.  * @fb: framebuffer
  590.  *
  591.  * This functions increments the fb's refcount.
  592.  */
  593. void drm_framebuffer_reference(struct drm_framebuffer *fb)
  594. {
  595.         DRM_DEBUG("%p: FB ID: %d (%d)\n", fb, fb->base.id, atomic_read(&fb->refcount.refcount));
  596.         kref_get(&fb->refcount);
  597. }
  598. EXPORT_SYMBOL(drm_framebuffer_reference);
  599.  
  600. static void drm_framebuffer_free_bug(struct kref *kref)
  601. {
  602.         BUG();
  603. }
  604.  
  605. static void __drm_framebuffer_unreference(struct drm_framebuffer *fb)
  606. {
  607.         DRM_DEBUG("%p: FB ID: %d (%d)\n", fb, fb->base.id, atomic_read(&fb->refcount.refcount));
  608.         kref_put(&fb->refcount, drm_framebuffer_free_bug);
  609. }
  610.  
  611. /* dev->mode_config.fb_lock must be held! */
  612. static void __drm_framebuffer_unregister(struct drm_device *dev,
  613.                                          struct drm_framebuffer *fb)
  614. {
  615.         mutex_lock(&dev->mode_config.idr_mutex);
  616.         idr_remove(&dev->mode_config.crtc_idr, fb->base.id);
  617.         mutex_unlock(&dev->mode_config.idr_mutex);
  618.  
  619.         fb->base.id = 0;
  620.  
  621.         __drm_framebuffer_unreference(fb);
  622. }
  623.  
  624. /**
  625.  * drm_framebuffer_unregister_private - unregister a private fb from the lookup idr
  626.  * @fb: fb to unregister
  627.  *
  628.  * Drivers need to call this when cleaning up driver-private framebuffers, e.g.
  629.  * those used for fbdev. Note that the caller must hold a reference of it's own,
  630.  * i.e. the object may not be destroyed through this call (since it'll lead to a
  631.  * locking inversion).
  632.  */
  633. void drm_framebuffer_unregister_private(struct drm_framebuffer *fb)
  634. {
  635.         struct drm_device *dev = fb->dev;
  636.  
  637.         mutex_lock(&dev->mode_config.fb_lock);
  638.         /* Mark fb as reaped and drop idr ref. */
  639.         __drm_framebuffer_unregister(dev, fb);
  640.         mutex_unlock(&dev->mode_config.fb_lock);
  641. }
  642. EXPORT_SYMBOL(drm_framebuffer_unregister_private);
  643.  
  644. /**
  645.  * drm_framebuffer_cleanup - remove a framebuffer object
  646.  * @fb: framebuffer to remove
  647.  *
  648.  * Cleanup framebuffer. This function is intended to be used from the drivers
  649.  * ->destroy callback. It can also be used to clean up driver private
  650.  *  framebuffers embedded into a larger structure.
  651.  *
  652.  * Note that this function does not remove the fb from active usuage - if it is
  653.  * still used anywhere, hilarity can ensue since userspace could call getfb on
  654.  * the id and get back -EINVAL. Obviously no concern at driver unload time.
  655.  *
  656.  * Also, the framebuffer will not be removed from the lookup idr - for
  657.  * user-created framebuffers this will happen in in the rmfb ioctl. For
  658.  * driver-private objects (e.g. for fbdev) drivers need to explicitly call
  659.  * drm_framebuffer_unregister_private.
  660.  */
  661. void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
  662. {
  663.         struct drm_device *dev = fb->dev;
  664.  
  665.         mutex_lock(&dev->mode_config.fb_lock);
  666.         list_del(&fb->head);
  667.         dev->mode_config.num_fb--;
  668.         mutex_unlock(&dev->mode_config.fb_lock);
  669. }
  670. EXPORT_SYMBOL(drm_framebuffer_cleanup);
  671.  
  672. /**
  673.  * drm_framebuffer_remove - remove and unreference a framebuffer object
  674.  * @fb: framebuffer to remove
  675.  *
  676.  * Scans all the CRTCs and planes in @dev's mode_config.  If they're
  677.  * using @fb, removes it, setting it to NULL. Then drops the reference to the
  678.  * passed-in framebuffer. Might take the modeset locks.
  679.  *
  680.  * Note that this function optimizes the cleanup away if the caller holds the
  681.  * last reference to the framebuffer. It is also guaranteed to not take the
  682.  * modeset locks in this case.
  683.  */
  684. void drm_framebuffer_remove(struct drm_framebuffer *fb)
  685. {
  686.         struct drm_device *dev = fb->dev;
  687.         struct drm_crtc *crtc;
  688.         struct drm_plane *plane;
  689.         struct drm_mode_set set;
  690.         int ret;
  691.  
  692.         WARN_ON(!list_empty(&fb->filp_head));
  693.  
  694.         /*
  695.          * drm ABI mandates that we remove any deleted framebuffers from active
  696.          * useage. But since most sane clients only remove framebuffers they no
  697.          * longer need, try to optimize this away.
  698.          *
  699.          * Since we're holding a reference ourselves, observing a refcount of 1
  700.          * means that we're the last holder and can skip it. Also, the refcount
  701.          * can never increase from 1 again, so we don't need any barriers or
  702.          * locks.
  703.          *
  704.          * Note that userspace could try to race with use and instate a new
  705.          * usage _after_ we've cleared all current ones. End result will be an
  706.          * in-use fb with fb-id == 0. Userspace is allowed to shoot its own foot
  707.          * in this manner.
  708.          */
  709.         if (atomic_read(&fb->refcount.refcount) > 1) {
  710.                 drm_modeset_lock_all(dev);
  711.                 /* remove from any CRTC */
  712.                 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  713.                         if (crtc->primary->fb == fb) {
  714.                                 /* should turn off the crtc */
  715.                                 memset(&set, 0, sizeof(struct drm_mode_set));
  716.                                 set.crtc = crtc;
  717.                                 set.fb = NULL;
  718.                                 ret = drm_mode_set_config_internal(&set);
  719.                                 if (ret)
  720.                                         DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
  721.                         }
  722.                 }
  723.  
  724.                 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
  725.                         if (plane->fb == fb)
  726.                                 drm_plane_force_disable(plane);
  727.                 }
  728.                 drm_modeset_unlock_all(dev);
  729.         }
  730.  
  731.         drm_framebuffer_unreference(fb);
  732. }
  733. EXPORT_SYMBOL(drm_framebuffer_remove);
  734.  
  735. DEFINE_WW_CLASS(crtc_ww_class);
  736.  
  737. /**
  738.  * drm_crtc_init_with_planes - Initialise a new CRTC object with
  739.  *    specified primary and cursor planes.
  740.  * @dev: DRM device
  741.  * @crtc: CRTC object to init
  742.  * @primary: Primary plane for CRTC
  743.  * @cursor: Cursor plane for CRTC
  744.  * @funcs: callbacks for the new CRTC
  745.  *
  746.  * Inits a new object created as base part of a driver crtc object.
  747.  *
  748.  * Returns:
  749.  * Zero on success, error code on failure.
  750.  */
  751. int drm_crtc_init_with_planes(struct drm_device *dev, struct drm_crtc *crtc,
  752.                               struct drm_plane *primary,
  753.                               struct drm_plane *cursor,
  754.                    const struct drm_crtc_funcs *funcs)
  755. {
  756.         struct drm_mode_config *config = &dev->mode_config;
  757.         int ret;
  758.  
  759.         crtc->dev = dev;
  760.         crtc->funcs = funcs;
  761.         crtc->invert_dimensions = false;
  762.  
  763.         drm_modeset_lock_all(dev);
  764.         drm_modeset_lock_init(&crtc->mutex);
  765.         /* dropped by _unlock_all(): */
  766.         drm_modeset_lock(&crtc->mutex, config->acquire_ctx);
  767.  
  768.         ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
  769.         if (ret)
  770.                 goto out;
  771.  
  772.         crtc->base.properties = &crtc->properties;
  773.  
  774.         list_add_tail(&crtc->head, &config->crtc_list);
  775.         config->num_crtc++;
  776.  
  777.         crtc->primary = primary;
  778.         crtc->cursor = cursor;
  779.         if (primary)
  780.                 primary->possible_crtcs = 1 << drm_crtc_index(crtc);
  781.         if (cursor)
  782.                 cursor->possible_crtcs = 1 << drm_crtc_index(crtc);
  783.  
  784.  out:
  785.         drm_modeset_unlock_all(dev);
  786.  
  787.         return ret;
  788. }
  789. EXPORT_SYMBOL(drm_crtc_init_with_planes);
  790.  
  791. /**
  792.  * drm_crtc_cleanup - Clean up the core crtc usage
  793.  * @crtc: CRTC to cleanup
  794.  *
  795.  * This function cleans up @crtc and removes it from the DRM mode setting
  796.  * core. Note that the function does *not* free the crtc structure itself,
  797.  * this is the responsibility of the caller.
  798.  */
  799. void drm_crtc_cleanup(struct drm_crtc *crtc)
  800. {
  801.         struct drm_device *dev = crtc->dev;
  802.  
  803.                 kfree(crtc->gamma_store);
  804.                 crtc->gamma_store = NULL;
  805.  
  806.         drm_modeset_lock_fini(&crtc->mutex);
  807.  
  808.         drm_mode_object_put(dev, &crtc->base);
  809.         list_del(&crtc->head);
  810.         dev->mode_config.num_crtc--;
  811. }
  812. EXPORT_SYMBOL(drm_crtc_cleanup);
  813.  
  814. /**
  815.  * drm_crtc_index - find the index of a registered CRTC
  816.  * @crtc: CRTC to find index for
  817.  *
  818.  * Given a registered CRTC, return the index of that CRTC within a DRM
  819.  * device's list of CRTCs.
  820.  */
  821. unsigned int drm_crtc_index(struct drm_crtc *crtc)
  822. {
  823.         unsigned int index = 0;
  824.         struct drm_crtc *tmp;
  825.  
  826.         list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head) {
  827.                 if (tmp == crtc)
  828.                         return index;
  829.  
  830.                 index++;
  831.         }
  832.  
  833.         BUG();
  834. }
  835. EXPORT_SYMBOL(drm_crtc_index);
  836.  
  837. /*
  838.  * drm_mode_remove - remove and free a mode
  839.  * @connector: connector list to modify
  840.  * @mode: mode to remove
  841.  *
  842.  * Remove @mode from @connector's mode list, then free it.
  843.  */
  844. static void drm_mode_remove(struct drm_connector *connector,
  845.                      struct drm_display_mode *mode)
  846. {
  847.         list_del(&mode->head);
  848.         drm_mode_destroy(connector->dev, mode);
  849. }
  850.  
  851. /**
  852.  * drm_connector_init - Init a preallocated connector
  853.  * @dev: DRM device
  854.  * @connector: the connector to init
  855.  * @funcs: callbacks for this connector
  856.  * @connector_type: user visible type of the connector
  857.  *
  858.  * Initialises a preallocated connector. Connectors should be
  859.  * subclassed as part of driver connector objects.
  860.  *
  861.  * Returns:
  862.  * Zero on success, error code on failure.
  863.  */
  864. int drm_connector_init(struct drm_device *dev,
  865.                      struct drm_connector *connector,
  866.                      const struct drm_connector_funcs *funcs,
  867.                      int connector_type)
  868. {
  869.         int ret;
  870.         struct ida *connector_ida =
  871.                 &drm_connector_enum_list[connector_type].ida;
  872.  
  873.         drm_modeset_lock_all(dev);
  874.  
  875.         ret = drm_mode_object_get_reg(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR, false);
  876.         if (ret)
  877.                 goto out_unlock;
  878.  
  879.         connector->base.properties = &connector->properties;
  880.         connector->dev = dev;
  881.         connector->funcs = funcs;
  882.         connector->connector_type = connector_type;
  883.         connector->connector_type_id =
  884.                 ida_simple_get(connector_ida, 1, 0, GFP_KERNEL);
  885.         if (connector->connector_type_id < 0) {
  886.                 ret = connector->connector_type_id;
  887.                 goto out_put;
  888.         }
  889.         connector->name =
  890.                 kasprintf(GFP_KERNEL, "%s-%d",
  891.                           drm_connector_enum_list[connector_type].name,
  892.                           connector->connector_type_id);
  893.         if (!connector->name) {
  894.                 ret = -ENOMEM;
  895.                 goto out_put;
  896.         }
  897.  
  898.         INIT_LIST_HEAD(&connector->probed_modes);
  899.         INIT_LIST_HEAD(&connector->modes);
  900.         connector->edid_blob_ptr = NULL;
  901.         connector->status = connector_status_unknown;
  902.  
  903.         list_add_tail(&connector->head, &dev->mode_config.connector_list);
  904.         dev->mode_config.num_connector++;
  905.  
  906.         if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
  907.                 drm_object_attach_property(&connector->base,
  908.                                               dev->mode_config.edid_property,
  909.                                               0);
  910.  
  911.         drm_object_attach_property(&connector->base,
  912.                                       dev->mode_config.dpms_property, 0);
  913.  
  914.         connector->debugfs_entry = NULL;
  915.  
  916. out_put:
  917.         if (ret)
  918.                 drm_mode_object_put(dev, &connector->base);
  919.  
  920. out_unlock:
  921.         drm_modeset_unlock_all(dev);
  922.  
  923.         return ret;
  924. }
  925. EXPORT_SYMBOL(drm_connector_init);
  926.  
  927. /**
  928.  * drm_connector_cleanup - cleans up an initialised connector
  929.  * @connector: connector to cleanup
  930.  *
  931.  * Cleans up the connector but doesn't free the object.
  932.  */
  933. void drm_connector_cleanup(struct drm_connector *connector)
  934. {
  935.         struct drm_device *dev = connector->dev;
  936.         struct drm_display_mode *mode, *t;
  937.  
  938.         list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
  939.                 drm_mode_remove(connector, mode);
  940.  
  941.         list_for_each_entry_safe(mode, t, &connector->modes, head)
  942.                 drm_mode_remove(connector, mode);
  943.  
  944.         ida_remove(&drm_connector_enum_list[connector->connector_type].ida,
  945.                    connector->connector_type_id);
  946.  
  947.         drm_mode_object_put(dev, &connector->base);
  948.         kfree(connector->name);
  949.         connector->name = NULL;
  950.         list_del(&connector->head);
  951.         dev->mode_config.num_connector--;
  952. }
  953. EXPORT_SYMBOL(drm_connector_cleanup);
  954.  
  955. /**
  956.  * drm_connector_register - register a connector
  957.  * @connector: the connector to register
  958.  *
  959.  * Register userspace interfaces for a connector
  960.  *
  961.  * Returns:
  962.  * Zero on success, error code on failure.
  963.  */
  964. int drm_connector_register(struct drm_connector *connector)
  965. {
  966.         int ret;
  967.  
  968.         drm_mode_object_register(connector->dev, &connector->base);
  969.  
  970.         return 0;
  971. }
  972. EXPORT_SYMBOL(drm_connector_register);
  973.  
  974. /**
  975.  * drm_connector_unregister - unregister a connector
  976.  * @connector: the connector to unregister
  977.  *
  978.  * Unregister userspace interfaces for a connector
  979.  */
  980. void drm_connector_unregister(struct drm_connector *connector)
  981. {
  982. }
  983. EXPORT_SYMBOL(drm_connector_unregister);
  984.  
  985.  
  986. /**
  987.  * drm_connector_unplug_all - unregister connector userspace interfaces
  988.  * @dev: drm device
  989.  *
  990.  * This function unregisters all connector userspace interfaces in sysfs. Should
  991.  * be call when the device is disconnected, e.g. from an usb driver's
  992.  * ->disconnect callback.
  993.  */
  994. void drm_connector_unplug_all(struct drm_device *dev)
  995. {
  996.         struct drm_connector *connector;
  997.  
  998.         /* taking the mode config mutex ends up in a clash with sysfs */
  999.         list_for_each_entry(connector, &dev->mode_config.connector_list, head)
  1000.                 drm_connector_unregister(connector);
  1001.  
  1002. }
  1003. EXPORT_SYMBOL(drm_connector_unplug_all);
  1004.  
  1005. /**
  1006.  * drm_bridge_init - initialize a drm transcoder/bridge
  1007.  * @dev: drm device
  1008.  * @bridge: transcoder/bridge to set up
  1009.  * @funcs: bridge function table
  1010.  *
  1011.  * Initialises a preallocated bridge. Bridges should be
  1012.  * subclassed as part of driver connector objects.
  1013.  *
  1014.  * Returns:
  1015.  * Zero on success, error code on failure.
  1016.  */
  1017. int drm_bridge_init(struct drm_device *dev, struct drm_bridge *bridge,
  1018.                 const struct drm_bridge_funcs *funcs)
  1019. {
  1020.         int ret;
  1021.  
  1022.         drm_modeset_lock_all(dev);
  1023.  
  1024.         ret = drm_mode_object_get(dev, &bridge->base, DRM_MODE_OBJECT_BRIDGE);
  1025.         if (ret)
  1026.                 goto out;
  1027.  
  1028.         bridge->dev = dev;
  1029.         bridge->funcs = funcs;
  1030.  
  1031.         list_add_tail(&bridge->head, &dev->mode_config.bridge_list);
  1032.         dev->mode_config.num_bridge++;
  1033.  
  1034.  out:
  1035.         drm_modeset_unlock_all(dev);
  1036.         return ret;
  1037. }
  1038. EXPORT_SYMBOL(drm_bridge_init);
  1039.  
  1040. /**
  1041.  * drm_bridge_cleanup - cleans up an initialised bridge
  1042.  * @bridge: bridge to cleanup
  1043.  *
  1044.  * Cleans up the bridge but doesn't free the object.
  1045.  */
  1046. void drm_bridge_cleanup(struct drm_bridge *bridge)
  1047. {
  1048.         struct drm_device *dev = bridge->dev;
  1049.  
  1050.         drm_modeset_lock_all(dev);
  1051.         drm_mode_object_put(dev, &bridge->base);
  1052.         list_del(&bridge->head);
  1053.         dev->mode_config.num_bridge--;
  1054.         drm_modeset_unlock_all(dev);
  1055. }
  1056. EXPORT_SYMBOL(drm_bridge_cleanup);
  1057.  
  1058. /**
  1059.  * drm_encoder_init - Init a preallocated encoder
  1060.  * @dev: drm device
  1061.  * @encoder: the encoder to init
  1062.  * @funcs: callbacks for this encoder
  1063.  * @encoder_type: user visible type of the encoder
  1064.  *
  1065.  * Initialises a preallocated encoder. Encoder should be
  1066.  * subclassed as part of driver encoder objects.
  1067.  *
  1068.  * Returns:
  1069.  * Zero on success, error code on failure.
  1070.  */
  1071. int drm_encoder_init(struct drm_device *dev,
  1072.                       struct drm_encoder *encoder,
  1073.                       const struct drm_encoder_funcs *funcs,
  1074.                       int encoder_type)
  1075. {
  1076.         int ret;
  1077.  
  1078.         drm_modeset_lock_all(dev);
  1079.  
  1080.         ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
  1081.         if (ret)
  1082.                 goto out_unlock;
  1083.  
  1084.         encoder->dev = dev;
  1085.         encoder->encoder_type = encoder_type;
  1086.         encoder->funcs = funcs;
  1087.         encoder->name = kasprintf(GFP_KERNEL, "%s-%d",
  1088.                                   drm_encoder_enum_list[encoder_type].name,
  1089.                                   encoder->base.id);
  1090.         if (!encoder->name) {
  1091.                 ret = -ENOMEM;
  1092.                 goto out_put;
  1093.         }
  1094.  
  1095.         list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
  1096.         dev->mode_config.num_encoder++;
  1097.  
  1098. out_put:
  1099.         if (ret)
  1100.                 drm_mode_object_put(dev, &encoder->base);
  1101.  
  1102. out_unlock:
  1103.         drm_modeset_unlock_all(dev);
  1104.  
  1105.         return ret;
  1106. }
  1107. EXPORT_SYMBOL(drm_encoder_init);
  1108.  
  1109. /**
  1110.  * drm_encoder_cleanup - cleans up an initialised encoder
  1111.  * @encoder: encoder to cleanup
  1112.  *
  1113.  * Cleans up the encoder but doesn't free the object.
  1114.  */
  1115. void drm_encoder_cleanup(struct drm_encoder *encoder)
  1116. {
  1117.         struct drm_device *dev = encoder->dev;
  1118.         drm_modeset_lock_all(dev);
  1119.         drm_mode_object_put(dev, &encoder->base);
  1120.         kfree(encoder->name);
  1121.         encoder->name = NULL;
  1122.         list_del(&encoder->head);
  1123.         dev->mode_config.num_encoder--;
  1124.         drm_modeset_unlock_all(dev);
  1125. }
  1126. EXPORT_SYMBOL(drm_encoder_cleanup);
  1127.  
  1128. /**
  1129.  * drm_universal_plane_init - Initialize a new universal plane object
  1130.  * @dev: DRM device
  1131.  * @plane: plane object to init
  1132.  * @possible_crtcs: bitmask of possible CRTCs
  1133.  * @funcs: callbacks for the new plane
  1134.  * @formats: array of supported formats (%DRM_FORMAT_*)
  1135.  * @format_count: number of elements in @formats
  1136.  * @type: type of plane (overlay, primary, cursor)
  1137.  *
  1138.  * Initializes a plane object of type @type.
  1139.  *
  1140.  * Returns:
  1141.  * Zero on success, error code on failure.
  1142.  */
  1143. int drm_universal_plane_init(struct drm_device *dev, struct drm_plane *plane,
  1144.                    unsigned long possible_crtcs,
  1145.                    const struct drm_plane_funcs *funcs,
  1146.                    const uint32_t *formats, uint32_t format_count,
  1147.                              enum drm_plane_type type)
  1148. {
  1149.         int ret;
  1150.  
  1151.         drm_modeset_lock_all(dev);
  1152.  
  1153.         ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
  1154.         if (ret)
  1155.                 goto out;
  1156.  
  1157.         plane->base.properties = &plane->properties;
  1158.         plane->dev = dev;
  1159.         plane->funcs = funcs;
  1160.         plane->format_types = kmalloc(sizeof(uint32_t) * format_count,
  1161.                                       GFP_KERNEL);
  1162.         if (!plane->format_types) {
  1163.                 DRM_DEBUG_KMS("out of memory when allocating plane\n");
  1164.                 drm_mode_object_put(dev, &plane->base);
  1165.                 ret = -ENOMEM;
  1166.                 goto out;
  1167.         }
  1168.  
  1169.         memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
  1170.         plane->format_count = format_count;
  1171.         plane->possible_crtcs = possible_crtcs;
  1172.         plane->type = type;
  1173.  
  1174.                 list_add_tail(&plane->head, &dev->mode_config.plane_list);
  1175.         dev->mode_config.num_total_plane++;
  1176.         if (plane->type == DRM_PLANE_TYPE_OVERLAY)
  1177.                 dev->mode_config.num_overlay_plane++;
  1178.  
  1179.         drm_object_attach_property(&plane->base,
  1180.                                    dev->mode_config.plane_type_property,
  1181.                                    plane->type);
  1182.  
  1183.  out:
  1184.         drm_modeset_unlock_all(dev);
  1185.  
  1186.         return ret;
  1187. }
  1188. EXPORT_SYMBOL(drm_universal_plane_init);
  1189.  
  1190. /**
  1191.  * drm_plane_init - Initialize a legacy plane
  1192.  * @dev: DRM device
  1193.  * @plane: plane object to init
  1194.  * @possible_crtcs: bitmask of possible CRTCs
  1195.  * @funcs: callbacks for the new plane
  1196.  * @formats: array of supported formats (%DRM_FORMAT_*)
  1197.  * @format_count: number of elements in @formats
  1198.  * @is_primary: plane type (primary vs overlay)
  1199.  *
  1200.  * Legacy API to initialize a DRM plane.
  1201.  *
  1202.  * New drivers should call drm_universal_plane_init() instead.
  1203.  *
  1204.  * Returns:
  1205.  * Zero on success, error code on failure.
  1206.  */
  1207. int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
  1208.                    unsigned long possible_crtcs,
  1209.                    const struct drm_plane_funcs *funcs,
  1210.                    const uint32_t *formats, uint32_t format_count,
  1211.                    bool is_primary)
  1212. {
  1213.         enum drm_plane_type type;
  1214.  
  1215.         type = is_primary ? DRM_PLANE_TYPE_PRIMARY : DRM_PLANE_TYPE_OVERLAY;
  1216.         return drm_universal_plane_init(dev, plane, possible_crtcs, funcs,
  1217.                                         formats, format_count, type);
  1218. }
  1219. EXPORT_SYMBOL(drm_plane_init);
  1220.  
  1221. /**
  1222.  * drm_plane_cleanup - Clean up the core plane usage
  1223.  * @plane: plane to cleanup
  1224.  *
  1225.  * This function cleans up @plane and removes it from the DRM mode setting
  1226.  * core. Note that the function does *not* free the plane structure itself,
  1227.  * this is the responsibility of the caller.
  1228.  */
  1229. void drm_plane_cleanup(struct drm_plane *plane)
  1230. {
  1231.         struct drm_device *dev = plane->dev;
  1232.  
  1233.         drm_modeset_lock_all(dev);
  1234.         kfree(plane->format_types);
  1235.         drm_mode_object_put(dev, &plane->base);
  1236.  
  1237.         BUG_ON(list_empty(&plane->head));
  1238.  
  1239.                 list_del(&plane->head);
  1240.         dev->mode_config.num_total_plane--;
  1241.         if (plane->type == DRM_PLANE_TYPE_OVERLAY)
  1242.                 dev->mode_config.num_overlay_plane--;
  1243.         drm_modeset_unlock_all(dev);
  1244. }
  1245. EXPORT_SYMBOL(drm_plane_cleanup);
  1246.  
  1247. /**
  1248.  * drm_plane_force_disable - Forcibly disable a plane
  1249.  * @plane: plane to disable
  1250.  *
  1251.  * Forces the plane to be disabled.
  1252.  *
  1253.  * Used when the plane's current framebuffer is destroyed,
  1254.  * and when restoring fbdev mode.
  1255.  */
  1256. void drm_plane_force_disable(struct drm_plane *plane)
  1257. {
  1258.         struct drm_framebuffer *old_fb = plane->fb;
  1259.         int ret;
  1260.  
  1261.         if (!old_fb)
  1262.                 return;
  1263.  
  1264.         ret = plane->funcs->disable_plane(plane);
  1265.         if (ret) {
  1266.                 DRM_ERROR("failed to disable plane with busy fb\n");
  1267.                 return;
  1268.         }
  1269.         /* disconnect the plane from the fb and crtc: */
  1270.         __drm_framebuffer_unreference(old_fb);
  1271.         plane->fb = NULL;
  1272.         plane->crtc = NULL;
  1273. }
  1274. EXPORT_SYMBOL(drm_plane_force_disable);
  1275.  
  1276. static int drm_mode_create_standard_connector_properties(struct drm_device *dev)
  1277. {
  1278.         struct drm_property *edid;
  1279.         struct drm_property *dpms;
  1280.         struct drm_property *dev_path;
  1281.  
  1282.         /*
  1283.          * Standard properties (apply to all connectors)
  1284.          */
  1285.         edid = drm_property_create(dev, DRM_MODE_PROP_BLOB |
  1286.                                    DRM_MODE_PROP_IMMUTABLE,
  1287.                                    "EDID", 0);
  1288.         dev->mode_config.edid_property = edid;
  1289.  
  1290.         dpms = drm_property_create_enum(dev, 0,
  1291.                                    "DPMS", drm_dpms_enum_list,
  1292.                                    ARRAY_SIZE(drm_dpms_enum_list));
  1293.         dev->mode_config.dpms_property = dpms;
  1294.  
  1295.         dev_path = drm_property_create(dev,
  1296.                                        DRM_MODE_PROP_BLOB |
  1297.                                        DRM_MODE_PROP_IMMUTABLE,
  1298.                                        "PATH", 0);
  1299.         dev->mode_config.path_property = dev_path;
  1300.  
  1301.         return 0;
  1302. }
  1303.  
  1304. static int drm_mode_create_standard_plane_properties(struct drm_device *dev)
  1305. {
  1306.         struct drm_property *type;
  1307.  
  1308.         /*
  1309.          * Standard properties (apply to all planes)
  1310.          */
  1311.         type = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
  1312.                                         "type", drm_plane_type_enum_list,
  1313.                                         ARRAY_SIZE(drm_plane_type_enum_list));
  1314.         dev->mode_config.plane_type_property = type;
  1315.  
  1316.         return 0;
  1317. }
  1318.  
  1319. /**
  1320.  * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
  1321.  * @dev: DRM device
  1322.  *
  1323.  * Called by a driver the first time a DVI-I connector is made.
  1324.  */
  1325. int drm_mode_create_dvi_i_properties(struct drm_device *dev)
  1326. {
  1327.         struct drm_property *dvi_i_selector;
  1328.         struct drm_property *dvi_i_subconnector;
  1329.  
  1330.         if (dev->mode_config.dvi_i_select_subconnector_property)
  1331.                 return 0;
  1332.  
  1333.         dvi_i_selector =
  1334.                 drm_property_create_enum(dev, 0,
  1335.                                     "select subconnector",
  1336.                                     drm_dvi_i_select_enum_list,
  1337.                                     ARRAY_SIZE(drm_dvi_i_select_enum_list));
  1338.         dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
  1339.  
  1340.         dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
  1341.                                     "subconnector",
  1342.                                     drm_dvi_i_subconnector_enum_list,
  1343.                                     ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
  1344.         dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
  1345.  
  1346.         return 0;
  1347. }
  1348. EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
  1349.  
  1350. /**
  1351.  * drm_create_tv_properties - create TV specific connector properties
  1352.  * @dev: DRM device
  1353.  * @num_modes: number of different TV formats (modes) supported
  1354.  * @modes: array of pointers to strings containing name of each format
  1355.  *
  1356.  * Called by a driver's TV initialization routine, this function creates
  1357.  * the TV specific connector properties for a given device.  Caller is
  1358.  * responsible for allocating a list of format names and passing them to
  1359.  * this routine.
  1360.  */
  1361. int drm_mode_create_tv_properties(struct drm_device *dev, int num_modes,
  1362.                                   char *modes[])
  1363. {
  1364.         struct drm_property *tv_selector;
  1365.         struct drm_property *tv_subconnector;
  1366.         int i;
  1367.  
  1368.         if (dev->mode_config.tv_select_subconnector_property)
  1369.                 return 0;
  1370.  
  1371.         /*
  1372.          * Basic connector properties
  1373.          */
  1374.         tv_selector = drm_property_create_enum(dev, 0,
  1375.                                           "select subconnector",
  1376.                                           drm_tv_select_enum_list,
  1377.                                           ARRAY_SIZE(drm_tv_select_enum_list));
  1378.         dev->mode_config.tv_select_subconnector_property = tv_selector;
  1379.  
  1380.         tv_subconnector =
  1381.                 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
  1382.                                     "subconnector",
  1383.                                     drm_tv_subconnector_enum_list,
  1384.                                     ARRAY_SIZE(drm_tv_subconnector_enum_list));
  1385.         dev->mode_config.tv_subconnector_property = tv_subconnector;
  1386.  
  1387.         /*
  1388.          * Other, TV specific properties: margins & TV modes.
  1389.          */
  1390.         dev->mode_config.tv_left_margin_property =
  1391.                 drm_property_create_range(dev, 0, "left margin", 0, 100);
  1392.  
  1393.         dev->mode_config.tv_right_margin_property =
  1394.                 drm_property_create_range(dev, 0, "right margin", 0, 100);
  1395.  
  1396.         dev->mode_config.tv_top_margin_property =
  1397.                 drm_property_create_range(dev, 0, "top margin", 0, 100);
  1398.  
  1399.         dev->mode_config.tv_bottom_margin_property =
  1400.                 drm_property_create_range(dev, 0, "bottom margin", 0, 100);
  1401.  
  1402.         dev->mode_config.tv_mode_property =
  1403.                 drm_property_create(dev, DRM_MODE_PROP_ENUM,
  1404.                                     "mode", num_modes);
  1405.         for (i = 0; i < num_modes; i++)
  1406.                 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
  1407.                                       i, modes[i]);
  1408.  
  1409.         dev->mode_config.tv_brightness_property =
  1410.                 drm_property_create_range(dev, 0, "brightness", 0, 100);
  1411.  
  1412.         dev->mode_config.tv_contrast_property =
  1413.                 drm_property_create_range(dev, 0, "contrast", 0, 100);
  1414.  
  1415.         dev->mode_config.tv_flicker_reduction_property =
  1416.                 drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
  1417.  
  1418.         dev->mode_config.tv_overscan_property =
  1419.                 drm_property_create_range(dev, 0, "overscan", 0, 100);
  1420.  
  1421.         dev->mode_config.tv_saturation_property =
  1422.                 drm_property_create_range(dev, 0, "saturation", 0, 100);
  1423.  
  1424.         dev->mode_config.tv_hue_property =
  1425.                 drm_property_create_range(dev, 0, "hue", 0, 100);
  1426.  
  1427.         return 0;
  1428. }
  1429. EXPORT_SYMBOL(drm_mode_create_tv_properties);
  1430.  
  1431. /**
  1432.  * drm_mode_create_scaling_mode_property - create scaling mode property
  1433.  * @dev: DRM device
  1434.  *
  1435.  * Called by a driver the first time it's needed, must be attached to desired
  1436.  * connectors.
  1437.  */
  1438. int drm_mode_create_scaling_mode_property(struct drm_device *dev)
  1439. {
  1440.         struct drm_property *scaling_mode;
  1441.  
  1442.         if (dev->mode_config.scaling_mode_property)
  1443.                 return 0;
  1444.  
  1445.         scaling_mode =
  1446.                 drm_property_create_enum(dev, 0, "scaling mode",
  1447.                                 drm_scaling_mode_enum_list,
  1448.                                     ARRAY_SIZE(drm_scaling_mode_enum_list));
  1449.  
  1450.         dev->mode_config.scaling_mode_property = scaling_mode;
  1451.  
  1452.         return 0;
  1453. }
  1454. EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
  1455.  
  1456. /**
  1457.  * drm_mode_create_aspect_ratio_property - create aspect ratio property
  1458.  * @dev: DRM device
  1459.  *
  1460.  * Called by a driver the first time it's needed, must be attached to desired
  1461.  * connectors.
  1462.  *
  1463.  * Returns:
  1464.  * Zero on success, errno on failure.
  1465.  */
  1466. int drm_mode_create_aspect_ratio_property(struct drm_device *dev)
  1467. {
  1468.         if (dev->mode_config.aspect_ratio_property)
  1469.                 return 0;
  1470.  
  1471.         dev->mode_config.aspect_ratio_property =
  1472.                 drm_property_create_enum(dev, 0, "aspect ratio",
  1473.                                 drm_aspect_ratio_enum_list,
  1474.                                 ARRAY_SIZE(drm_aspect_ratio_enum_list));
  1475.  
  1476.         if (dev->mode_config.aspect_ratio_property == NULL)
  1477.                 return -ENOMEM;
  1478.  
  1479.         return 0;
  1480. }
  1481. EXPORT_SYMBOL(drm_mode_create_aspect_ratio_property);
  1482.  
  1483. /**
  1484.  * drm_mode_create_dirty_property - create dirty property
  1485.  * @dev: DRM device
  1486.  *
  1487.  * Called by a driver the first time it's needed, must be attached to desired
  1488.  * connectors.
  1489.  */
  1490. int drm_mode_create_dirty_info_property(struct drm_device *dev)
  1491. {
  1492.         struct drm_property *dirty_info;
  1493.  
  1494.         if (dev->mode_config.dirty_info_property)
  1495.                 return 0;
  1496.  
  1497.         dirty_info =
  1498.                 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
  1499.                                     "dirty",
  1500.                                     drm_dirty_info_enum_list,
  1501.                                     ARRAY_SIZE(drm_dirty_info_enum_list));
  1502.         dev->mode_config.dirty_info_property = dirty_info;
  1503.  
  1504.         return 0;
  1505. }
  1506. EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
  1507.  
  1508. static int drm_mode_group_init(struct drm_device *dev, struct drm_mode_group *group)
  1509. {
  1510.         uint32_t total_objects = 0;
  1511.  
  1512.         total_objects += dev->mode_config.num_crtc;
  1513.         total_objects += dev->mode_config.num_connector;
  1514.         total_objects += dev->mode_config.num_encoder;
  1515.         total_objects += dev->mode_config.num_bridge;
  1516.  
  1517.         group->id_list = kzalloc(total_objects * sizeof(uint32_t), GFP_KERNEL);
  1518.         if (!group->id_list)
  1519.                 return -ENOMEM;
  1520.  
  1521.         group->num_crtcs = 0;
  1522.         group->num_connectors = 0;
  1523.         group->num_encoders = 0;
  1524.         group->num_bridges = 0;
  1525.         return 0;
  1526. }
  1527.  
  1528. void drm_mode_group_destroy(struct drm_mode_group *group)
  1529. {
  1530.         kfree(group->id_list);
  1531.         group->id_list = NULL;
  1532. }
  1533.  
  1534. /*
  1535.  * NOTE: Driver's shouldn't ever call drm_mode_group_init_legacy_group - it is
  1536.  * the drm core's responsibility to set up mode control groups.
  1537.  */
  1538. int drm_mode_group_init_legacy_group(struct drm_device *dev,
  1539.                                      struct drm_mode_group *group)
  1540. {
  1541.         struct drm_crtc *crtc;
  1542.         struct drm_encoder *encoder;
  1543.         struct drm_connector *connector;
  1544.         struct drm_bridge *bridge;
  1545.         int ret;
  1546.  
  1547.         if ((ret = drm_mode_group_init(dev, group)))
  1548.                 return ret;
  1549.  
  1550.         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
  1551.                 group->id_list[group->num_crtcs++] = crtc->base.id;
  1552.  
  1553.         list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
  1554.                 group->id_list[group->num_crtcs + group->num_encoders++] =
  1555.                 encoder->base.id;
  1556.  
  1557.         list_for_each_entry(connector, &dev->mode_config.connector_list, head)
  1558.                 group->id_list[group->num_crtcs + group->num_encoders +
  1559.                                group->num_connectors++] = connector->base.id;
  1560.  
  1561.         list_for_each_entry(bridge, &dev->mode_config.bridge_list, head)
  1562.                 group->id_list[group->num_crtcs + group->num_encoders +
  1563.                                group->num_connectors + group->num_bridges++] =
  1564.                                         bridge->base.id;
  1565.  
  1566.         return 0;
  1567. }
  1568. EXPORT_SYMBOL(drm_mode_group_init_legacy_group);
  1569.  
  1570. void drm_reinit_primary_mode_group(struct drm_device *dev)
  1571. {
  1572.         drm_modeset_lock_all(dev);
  1573.         drm_mode_group_destroy(&dev->primary->mode_group);
  1574.         drm_mode_group_init_legacy_group(dev, &dev->primary->mode_group);
  1575.         drm_modeset_unlock_all(dev);
  1576. }
  1577. EXPORT_SYMBOL(drm_reinit_primary_mode_group);
  1578.  
  1579. /**
  1580.  * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
  1581.  * @out: drm_mode_modeinfo struct to return to the user
  1582.  * @in: drm_display_mode to use
  1583.  *
  1584.  * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
  1585.  * the user.
  1586.  */
  1587. static void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out,
  1588.                                       const struct drm_display_mode *in)
  1589. {
  1590.         WARN(in->hdisplay > USHRT_MAX || in->hsync_start > USHRT_MAX ||
  1591.              in->hsync_end > USHRT_MAX || in->htotal > USHRT_MAX ||
  1592.              in->hskew > USHRT_MAX || in->vdisplay > USHRT_MAX ||
  1593.              in->vsync_start > USHRT_MAX || in->vsync_end > USHRT_MAX ||
  1594.              in->vtotal > USHRT_MAX || in->vscan > USHRT_MAX,
  1595.              "timing values too large for mode info\n");
  1596.  
  1597.         out->clock = in->clock;
  1598.         out->hdisplay = in->hdisplay;
  1599.         out->hsync_start = in->hsync_start;
  1600.         out->hsync_end = in->hsync_end;
  1601.         out->htotal = in->htotal;
  1602.         out->hskew = in->hskew;
  1603.         out->vdisplay = in->vdisplay;
  1604.         out->vsync_start = in->vsync_start;
  1605.         out->vsync_end = in->vsync_end;
  1606.         out->vtotal = in->vtotal;
  1607.         out->vscan = in->vscan;
  1608.         out->vrefresh = in->vrefresh;
  1609.         out->flags = in->flags;
  1610.         out->type = in->type;
  1611.         strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
  1612.         out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
  1613. }
  1614.  
  1615. /**
  1616.  * drm_crtc_convert_umode - convert a modeinfo into a drm_display_mode
  1617.  * @out: drm_display_mode to return to the user
  1618.  * @in: drm_mode_modeinfo to use
  1619.  *
  1620.  * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
  1621.  * the caller.
  1622.  *
  1623.  * Returns:
  1624.  * Zero on success, errno on failure.
  1625.  */
  1626. static int drm_crtc_convert_umode(struct drm_display_mode *out,
  1627.                                   const struct drm_mode_modeinfo *in)
  1628. {
  1629.         if (in->clock > INT_MAX || in->vrefresh > INT_MAX)
  1630.                 return -ERANGE;
  1631.  
  1632.         if ((in->flags & DRM_MODE_FLAG_3D_MASK) > DRM_MODE_FLAG_3D_MAX)
  1633.                 return -EINVAL;
  1634.  
  1635.         out->clock = in->clock;
  1636.         out->hdisplay = in->hdisplay;
  1637.         out->hsync_start = in->hsync_start;
  1638.         out->hsync_end = in->hsync_end;
  1639.         out->htotal = in->htotal;
  1640.         out->hskew = in->hskew;
  1641.         out->vdisplay = in->vdisplay;
  1642.         out->vsync_start = in->vsync_start;
  1643.         out->vsync_end = in->vsync_end;
  1644.         out->vtotal = in->vtotal;
  1645.         out->vscan = in->vscan;
  1646.         out->vrefresh = in->vrefresh;
  1647.         out->flags = in->flags;
  1648.         out->type = in->type;
  1649.         strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
  1650.         out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
  1651.  
  1652.         return 0;
  1653. }
  1654.  
  1655.  
  1656. #if 0
  1657. /**
  1658.  * drm_mode_getresources - get graphics configuration
  1659.  * @dev: drm device for the ioctl
  1660.  * @data: data pointer for the ioctl
  1661.  * @file_priv: drm file for the ioctl call
  1662.  *
  1663.  * Construct a set of configuration description structures and return
  1664.  * them to the user, including CRTC, connector and framebuffer configuration.
  1665.  *
  1666.  * Called by the user via ioctl.
  1667.  *
  1668.  * Returns:
  1669.  * Zero on success, errno on failure.
  1670.  */
  1671. int drm_mode_getresources(struct drm_device *dev, void *data,
  1672.                           struct drm_file *file_priv)
  1673. {
  1674.         struct drm_mode_card_res *card_res = data;
  1675.         struct list_head *lh;
  1676.         struct drm_framebuffer *fb;
  1677.         struct drm_connector *connector;
  1678.         struct drm_crtc *crtc;
  1679.         struct drm_encoder *encoder;
  1680.         int ret = 0;
  1681.         int connector_count = 0;
  1682.         int crtc_count = 0;
  1683.         int fb_count = 0;
  1684.         int encoder_count = 0;
  1685.         int copied = 0, i;
  1686.         uint32_t __user *fb_id;
  1687.         uint32_t __user *crtc_id;
  1688.         uint32_t __user *connector_id;
  1689.         uint32_t __user *encoder_id;
  1690.         struct drm_mode_group *mode_group;
  1691.  
  1692.         if (!drm_core_check_feature(dev, DRIVER_MODESET))
  1693.                 return -EINVAL;
  1694.  
  1695.  
  1696.         mutex_lock(&file_priv->fbs_lock);
  1697.         /*
  1698.          * For the non-control nodes we need to limit the list of resources
  1699.          * by IDs in the group list for this node
  1700.          */
  1701.         list_for_each(lh, &file_priv->fbs)
  1702.                 fb_count++;
  1703.  
  1704.         /* handle this in 4 parts */
  1705.         /* FBs */
  1706.         if (card_res->count_fbs >= fb_count) {
  1707.                 copied = 0;
  1708.                 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
  1709.                 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
  1710.                         if (put_user(fb->base.id, fb_id + copied)) {
  1711.                                 mutex_unlock(&file_priv->fbs_lock);
  1712.                                 return -EFAULT;
  1713.                         }
  1714.                         copied++;
  1715.                 }
  1716.         }
  1717.         card_res->count_fbs = fb_count;
  1718.         mutex_unlock(&file_priv->fbs_lock);
  1719.  
  1720.         drm_modeset_lock_all(dev);
  1721.         if (!drm_is_primary_client(file_priv)) {
  1722.  
  1723.                 mode_group = NULL;
  1724.                 list_for_each(lh, &dev->mode_config.crtc_list)
  1725.                         crtc_count++;
  1726.  
  1727.                 list_for_each(lh, &dev->mode_config.connector_list)
  1728.                         connector_count++;
  1729.  
  1730.                 list_for_each(lh, &dev->mode_config.encoder_list)
  1731.                         encoder_count++;
  1732.         } else {
  1733.  
  1734.                 mode_group = &file_priv->master->minor->mode_group;
  1735.                 crtc_count = mode_group->num_crtcs;
  1736.                 connector_count = mode_group->num_connectors;
  1737.                 encoder_count = mode_group->num_encoders;
  1738.         }
  1739.  
  1740.         card_res->max_height = dev->mode_config.max_height;
  1741.         card_res->min_height = dev->mode_config.min_height;
  1742.         card_res->max_width = dev->mode_config.max_width;
  1743.         card_res->min_width = dev->mode_config.min_width;
  1744.  
  1745.         /* CRTCs */
  1746.         if (card_res->count_crtcs >= crtc_count) {
  1747.                 copied = 0;
  1748.                 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
  1749.                 if (!mode_group) {
  1750.                         list_for_each_entry(crtc, &dev->mode_config.crtc_list,
  1751.                                             head) {
  1752.                                 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
  1753.                                 if (put_user(crtc->base.id, crtc_id + copied)) {
  1754.                                         ret = -EFAULT;
  1755.                                         goto out;
  1756.                                 }
  1757.                                 copied++;
  1758.                         }
  1759.                 } else {
  1760.                         for (i = 0; i < mode_group->num_crtcs; i++) {
  1761.                                 if (put_user(mode_group->id_list[i],
  1762.                                              crtc_id + copied)) {
  1763.                                         ret = -EFAULT;
  1764.                                         goto out;
  1765.                                 }
  1766.                                 copied++;
  1767.                         }
  1768.                 }
  1769.         }
  1770.         card_res->count_crtcs = crtc_count;
  1771.  
  1772.         /* Encoders */
  1773.         if (card_res->count_encoders >= encoder_count) {
  1774.                 copied = 0;
  1775.                 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
  1776.                 if (!mode_group) {
  1777.                         list_for_each_entry(encoder,
  1778.                                             &dev->mode_config.encoder_list,
  1779.                                             head) {
  1780.                                 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id,
  1781.                                                 encoder->name);
  1782.                                 if (put_user(encoder->base.id, encoder_id +
  1783.                                              copied)) {
  1784.                                         ret = -EFAULT;
  1785.                                         goto out;
  1786.                                 }
  1787.                                 copied++;
  1788.                         }
  1789.                 } else {
  1790.                         for (i = mode_group->num_crtcs; i < mode_group->num_crtcs + mode_group->num_encoders; i++) {
  1791.                                 if (put_user(mode_group->id_list[i],
  1792.                                              encoder_id + copied)) {
  1793.                                         ret = -EFAULT;
  1794.                                         goto out;
  1795.                                 }
  1796.                                 copied++;
  1797.                         }
  1798.  
  1799.                 }
  1800.         }
  1801.         card_res->count_encoders = encoder_count;
  1802.  
  1803.         /* Connectors */
  1804.         if (card_res->count_connectors >= connector_count) {
  1805.                 copied = 0;
  1806.                 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
  1807.                 if (!mode_group) {
  1808.                         list_for_each_entry(connector,
  1809.                                             &dev->mode_config.connector_list,
  1810.                                             head) {
  1811.                                 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  1812.                                         connector->base.id,
  1813.                                         connector->name);
  1814.                                 if (put_user(connector->base.id,
  1815.                                              connector_id + copied)) {
  1816.                                         ret = -EFAULT;
  1817.                                         goto out;
  1818.                                 }
  1819.                                 copied++;
  1820.                         }
  1821.                 } else {
  1822.                         int start = mode_group->num_crtcs +
  1823.                                 mode_group->num_encoders;
  1824.                         for (i = start; i < start + mode_group->num_connectors; i++) {
  1825.                                 if (put_user(mode_group->id_list[i],
  1826.                                              connector_id + copied)) {
  1827.                                         ret = -EFAULT;
  1828.                                         goto out;
  1829.                                 }
  1830.                                 copied++;
  1831.                         }
  1832.                 }
  1833.         }
  1834.         card_res->count_connectors = connector_count;
  1835.  
  1836.         DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs,
  1837.                   card_res->count_connectors, card_res->count_encoders);
  1838.  
  1839. out:
  1840.         drm_modeset_unlock_all(dev);
  1841.         return ret;
  1842. }
  1843.  
  1844. /**
  1845.  * drm_mode_getcrtc - get CRTC configuration
  1846.  * @dev: drm device for the ioctl
  1847.  * @data: data pointer for the ioctl
  1848.  * @file_priv: drm file for the ioctl call
  1849.  *
  1850.  * Construct a CRTC configuration structure to return to the user.
  1851.  *
  1852.  * Called by the user via ioctl.
  1853.  *
  1854.  * Returns:
  1855.  * Zero on success, errno on failure.
  1856.  */
  1857. int drm_mode_getcrtc(struct drm_device *dev,
  1858.                      void *data, struct drm_file *file_priv)
  1859. {
  1860.         struct drm_mode_crtc *crtc_resp = data;
  1861.         struct drm_crtc *crtc;
  1862.         int ret = 0;
  1863.  
  1864.         if (!drm_core_check_feature(dev, DRIVER_MODESET))
  1865.                 return -EINVAL;
  1866.  
  1867.         drm_modeset_lock_all(dev);
  1868.  
  1869.         crtc = drm_crtc_find(dev, crtc_resp->crtc_id);
  1870.         if (!crtc) {
  1871.                 ret = -ENOENT;
  1872.                 goto out;
  1873.         }
  1874.  
  1875.         crtc_resp->x = crtc->x;
  1876.         crtc_resp->y = crtc->y;
  1877.         crtc_resp->gamma_size = crtc->gamma_size;
  1878.         if (crtc->primary->fb)
  1879.                 crtc_resp->fb_id = crtc->primary->fb->base.id;
  1880.         else
  1881.                 crtc_resp->fb_id = 0;
  1882.  
  1883.         if (crtc->enabled) {
  1884.  
  1885.                 drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
  1886.                 crtc_resp->mode_valid = 1;
  1887.  
  1888.         } else {
  1889.                 crtc_resp->mode_valid = 0;
  1890.         }
  1891.  
  1892. out:
  1893.         drm_modeset_unlock_all(dev);
  1894.         return ret;
  1895. }
  1896.  
  1897. static bool drm_mode_expose_to_userspace(const struct drm_display_mode *mode,
  1898.                                          const struct drm_file *file_priv)
  1899. {
  1900.         /*
  1901.          * If user-space hasn't configured the driver to expose the stereo 3D
  1902.          * modes, don't expose them.
  1903.          */
  1904.         if (!file_priv->stereo_allowed && drm_mode_is_stereo(mode))
  1905.                 return false;
  1906.  
  1907.         return true;
  1908. }
  1909.  
  1910. /**
  1911.  * drm_mode_getconnector - get connector configuration
  1912.  * @dev: drm device for the ioctl
  1913.  * @data: data pointer for the ioctl
  1914.  * @file_priv: drm file for the ioctl call
  1915.  *
  1916.  * Construct a connector configuration structure to return to the user.
  1917.  *
  1918.  * Called by the user via ioctl.
  1919.  *
  1920.  * Returns:
  1921.  * Zero on success, errno on failure.
  1922.  */
  1923. int drm_mode_getconnector(struct drm_device *dev, void *data,
  1924.                           struct drm_file *file_priv)
  1925. {
  1926.         struct drm_mode_get_connector *out_resp = data;
  1927.         struct drm_connector *connector;
  1928.         struct drm_display_mode *mode;
  1929.         int mode_count = 0;
  1930.         int props_count = 0;
  1931.         int encoders_count = 0;
  1932.         int ret = 0;
  1933.         int copied = 0;
  1934.         int i;
  1935.         struct drm_mode_modeinfo u_mode;
  1936.         struct drm_mode_modeinfo __user *mode_ptr;
  1937.         uint32_t __user *prop_ptr;
  1938.         uint64_t __user *prop_values;
  1939.         uint32_t __user *encoder_ptr;
  1940.  
  1941.         if (!drm_core_check_feature(dev, DRIVER_MODESET))
  1942.                 return -EINVAL;
  1943.  
  1944.         memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
  1945.  
  1946.         DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id);
  1947.  
  1948.         mutex_lock(&dev->mode_config.mutex);
  1949.  
  1950.         connector = drm_connector_find(dev, out_resp->connector_id);
  1951.         if (!connector) {
  1952.                 ret = -ENOENT;
  1953.                 goto out;
  1954.         }
  1955.  
  1956.         props_count = connector->properties.count;
  1957.  
  1958.         for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
  1959.                 if (connector->encoder_ids[i] != 0) {
  1960.                         encoders_count++;
  1961.                 }
  1962.         }
  1963.  
  1964.         if (out_resp->count_modes == 0) {
  1965.                 connector->funcs->fill_modes(connector,
  1966.                                              dev->mode_config.max_width,
  1967.                                              dev->mode_config.max_height);
  1968.         }
  1969.  
  1970.         /* delayed so we get modes regardless of pre-fill_modes state */
  1971.         list_for_each_entry(mode, &connector->modes, head)
  1972.                 if (drm_mode_expose_to_userspace(mode, file_priv))
  1973.                 mode_count++;
  1974.  
  1975.         out_resp->connector_id = connector->base.id;
  1976.         out_resp->connector_type = connector->connector_type;
  1977.         out_resp->connector_type_id = connector->connector_type_id;
  1978.         out_resp->mm_width = connector->display_info.width_mm;
  1979.         out_resp->mm_height = connector->display_info.height_mm;
  1980.         out_resp->subpixel = connector->display_info.subpixel_order;
  1981.         out_resp->connection = connector->status;
  1982.         drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
  1983.         if (connector->encoder)
  1984.                 out_resp->encoder_id = connector->encoder->base.id;
  1985.         else
  1986.                 out_resp->encoder_id = 0;
  1987.         drm_modeset_unlock(&dev->mode_config.connection_mutex);
  1988.  
  1989.         /*
  1990.          * This ioctl is called twice, once to determine how much space is
  1991.          * needed, and the 2nd time to fill it.
  1992.          */
  1993.         if ((out_resp->count_modes >= mode_count) && mode_count) {
  1994.                 copied = 0;
  1995.                 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
  1996.                 list_for_each_entry(mode, &connector->modes, head) {
  1997.                         if (!drm_mode_expose_to_userspace(mode, file_priv))
  1998.                                 continue;
  1999.  
  2000.                         drm_crtc_convert_to_umode(&u_mode, mode);
  2001.                         if (copy_to_user(mode_ptr + copied,
  2002.                                          &u_mode, sizeof(u_mode))) {
  2003.                                 ret = -EFAULT;
  2004.                                 goto out;
  2005.                         }
  2006.                         copied++;
  2007.                 }
  2008.         }
  2009.         out_resp->count_modes = mode_count;
  2010.  
  2011.         if ((out_resp->count_props >= props_count) && props_count) {
  2012.                 copied = 0;
  2013.                 prop_ptr = (uint32_t __user *)(unsigned long)(out_resp->props_ptr);
  2014.                 prop_values = (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr);
  2015.                 for (i = 0; i < connector->properties.count; i++) {
  2016.                         if (put_user(connector->properties.ids[i],
  2017.                                              prop_ptr + copied)) {
  2018.                                         ret = -EFAULT;
  2019.                                         goto out;
  2020.                                 }
  2021.  
  2022.                         if (put_user(connector->properties.values[i],
  2023.                                              prop_values + copied)) {
  2024.                                         ret = -EFAULT;
  2025.                                         goto out;
  2026.                                 }
  2027.                                 copied++;
  2028.                         }
  2029.                 }
  2030.         out_resp->count_props = props_count;
  2031.  
  2032.         if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
  2033.                 copied = 0;
  2034.                 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
  2035.                 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
  2036.                         if (connector->encoder_ids[i] != 0) {
  2037.                                 if (put_user(connector->encoder_ids[i],
  2038.                                              encoder_ptr + copied)) {
  2039.                                         ret = -EFAULT;
  2040.                                         goto out;
  2041.                                 }
  2042.                                 copied++;
  2043.                         }
  2044.                 }
  2045.         }
  2046.         out_resp->count_encoders = encoders_count;
  2047.  
  2048. out:
  2049.         mutex_unlock(&dev->mode_config.mutex);
  2050.  
  2051.         return ret;
  2052. }
  2053.  
  2054. /**
  2055.  * drm_mode_getencoder - get encoder configuration
  2056.  * @dev: drm device for the ioctl
  2057.  * @data: data pointer for the ioctl
  2058.  * @file_priv: drm file for the ioctl call
  2059.  *
  2060.  * Construct a encoder configuration structure to return to the user.
  2061.  *
  2062.  * Called by the user via ioctl.
  2063.  *
  2064.  * Returns:
  2065.  * Zero on success, errno on failure.
  2066.  */
  2067. int drm_mode_getencoder(struct drm_device *dev, void *data,
  2068.                         struct drm_file *file_priv)
  2069. {
  2070.         struct drm_mode_get_encoder *enc_resp = data;
  2071.         struct drm_encoder *encoder;
  2072.         int ret = 0;
  2073.  
  2074.         if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2075.                 return -EINVAL;
  2076.  
  2077.         drm_modeset_lock_all(dev);
  2078.         encoder = drm_encoder_find(dev, enc_resp->encoder_id);
  2079.         if (!encoder) {
  2080.                 ret = -ENOENT;
  2081.                 goto out;
  2082.         }
  2083.  
  2084.         if (encoder->crtc)
  2085.                 enc_resp->crtc_id = encoder->crtc->base.id;
  2086.         else
  2087.                 enc_resp->crtc_id = 0;
  2088.         enc_resp->encoder_type = encoder->encoder_type;
  2089.         enc_resp->encoder_id = encoder->base.id;
  2090.         enc_resp->possible_crtcs = encoder->possible_crtcs;
  2091.         enc_resp->possible_clones = encoder->possible_clones;
  2092.  
  2093. out:
  2094.         drm_modeset_unlock_all(dev);
  2095.         return ret;
  2096. }
  2097.  
  2098. /**
  2099.  * drm_mode_getplane_res - enumerate all plane resources
  2100.  * @dev: DRM device
  2101.  * @data: ioctl data
  2102.  * @file_priv: DRM file info
  2103.  *
  2104.  * Construct a list of plane ids to return to the user.
  2105.  *
  2106.  * Called by the user via ioctl.
  2107.  *
  2108.  * Returns:
  2109.  * Zero on success, errno on failure.
  2110.  */
  2111. int drm_mode_getplane_res(struct drm_device *dev, void *data,
  2112.                             struct drm_file *file_priv)
  2113. {
  2114.         struct drm_mode_get_plane_res *plane_resp = data;
  2115.         struct drm_mode_config *config;
  2116.         struct drm_plane *plane;
  2117.         uint32_t __user *plane_ptr;
  2118.         int copied = 0, ret = 0;
  2119.         unsigned num_planes;
  2120.  
  2121.         if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2122.                 return -EINVAL;
  2123.  
  2124.         drm_modeset_lock_all(dev);
  2125.         config = &dev->mode_config;
  2126.  
  2127.         if (file_priv->universal_planes)
  2128.                 num_planes = config->num_total_plane;
  2129.         else
  2130.                 num_planes = config->num_overlay_plane;
  2131.  
  2132.         /*
  2133.          * This ioctl is called twice, once to determine how much space is
  2134.          * needed, and the 2nd time to fill it.
  2135.          */
  2136.         if (num_planes &&
  2137.             (plane_resp->count_planes >= num_planes)) {
  2138.                 plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
  2139.  
  2140.                 list_for_each_entry(plane, &config->plane_list, head) {
  2141.                         /*
  2142.                          * Unless userspace set the 'universal planes'
  2143.                          * capability bit, only advertise overlays.
  2144.                          */
  2145.                         if (plane->type != DRM_PLANE_TYPE_OVERLAY &&
  2146.                             !file_priv->universal_planes)
  2147.                                 continue;
  2148.  
  2149.                         if (put_user(plane->base.id, plane_ptr + copied)) {
  2150.                                 ret = -EFAULT;
  2151.                                 goto out;
  2152.                         }
  2153.                         copied++;
  2154.                 }
  2155.         }
  2156.         plane_resp->count_planes = num_planes;
  2157.  
  2158. out:
  2159.         drm_modeset_unlock_all(dev);
  2160.         return ret;
  2161. }
  2162.  
  2163. /**
  2164.  * drm_mode_getplane - get plane configuration
  2165.  * @dev: DRM device
  2166.  * @data: ioctl data
  2167.  * @file_priv: DRM file info
  2168.  *
  2169.  * Construct a plane configuration structure to return to the user.
  2170.  *
  2171.  * Called by the user via ioctl.
  2172.  *
  2173.  * Returns:
  2174.  * Zero on success, errno on failure.
  2175.  */
  2176. int drm_mode_getplane(struct drm_device *dev, void *data,
  2177.                         struct drm_file *file_priv)
  2178. {
  2179.         struct drm_mode_get_plane *plane_resp = data;
  2180.         struct drm_plane *plane;
  2181.         uint32_t __user *format_ptr;
  2182.         int ret = 0;
  2183.  
  2184.         if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2185.                 return -EINVAL;
  2186.  
  2187.         drm_modeset_lock_all(dev);
  2188.         plane = drm_plane_find(dev, plane_resp->plane_id);
  2189.         if (!plane) {
  2190.                 ret = -ENOENT;
  2191.                 goto out;
  2192.         }
  2193.  
  2194.         if (plane->crtc)
  2195.                 plane_resp->crtc_id = plane->crtc->base.id;
  2196.         else
  2197.                 plane_resp->crtc_id = 0;
  2198.  
  2199.         if (plane->fb)
  2200.                 plane_resp->fb_id = plane->fb->base.id;
  2201.         else
  2202.                 plane_resp->fb_id = 0;
  2203.  
  2204.         plane_resp->plane_id = plane->base.id;
  2205.         plane_resp->possible_crtcs = plane->possible_crtcs;
  2206.         plane_resp->gamma_size = 0;
  2207.  
  2208.         /*
  2209.          * This ioctl is called twice, once to determine how much space is
  2210.          * needed, and the 2nd time to fill it.
  2211.          */
  2212.         if (plane->format_count &&
  2213.             (plane_resp->count_format_types >= plane->format_count)) {
  2214.                 format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
  2215.                 if (copy_to_user(format_ptr,
  2216.                                  plane->format_types,
  2217.                                  sizeof(uint32_t) * plane->format_count)) {
  2218.                         ret = -EFAULT;
  2219.                         goto out;
  2220.                 }
  2221.         }
  2222.         plane_resp->count_format_types = plane->format_count;
  2223.  
  2224. out:
  2225.         drm_modeset_unlock_all(dev);
  2226.         return ret;
  2227. }
  2228.  
  2229. /*
  2230.  * setplane_internal - setplane handler for internal callers
  2231.  *
  2232.  * Note that we assume an extra reference has already been taken on fb.  If the
  2233.  * update fails, this reference will be dropped before return; if it succeeds,
  2234.  * the previous framebuffer (if any) will be unreferenced instead.
  2235.  *
  2236.  * src_{x,y,w,h} are provided in 16.16 fixed point format
  2237.  */
  2238. static int setplane_internal(struct drm_plane *plane,
  2239.                              struct drm_crtc *crtc,
  2240.                              struct drm_framebuffer *fb,
  2241.                              int32_t crtc_x, int32_t crtc_y,
  2242.                              uint32_t crtc_w, uint32_t crtc_h,
  2243.                              /* src_{x,y,w,h} values are 16.16 fixed point */
  2244.                              uint32_t src_x, uint32_t src_y,
  2245.                              uint32_t src_w, uint32_t src_h)
  2246. {
  2247.         struct drm_device *dev = plane->dev;
  2248.         struct drm_framebuffer *old_fb = NULL;
  2249.         int ret = 0;
  2250.         unsigned int fb_width, fb_height;
  2251.         int i;
  2252.  
  2253.         /* No fb means shut it down */
  2254.         if (!fb) {
  2255.                 drm_modeset_lock_all(dev);
  2256.                 old_fb = plane->fb;
  2257.                 ret = plane->funcs->disable_plane(plane);
  2258.                 if (!ret) {
  2259.                 plane->crtc = NULL;
  2260.                 plane->fb = NULL;
  2261.                 } else {
  2262.                         old_fb = NULL;
  2263.                 }
  2264.                 drm_modeset_unlock_all(dev);
  2265.                 goto out;
  2266.         }
  2267.  
  2268.         /* Check whether this plane is usable on this CRTC */
  2269.         if (!(plane->possible_crtcs & drm_crtc_mask(crtc))) {
  2270.                 DRM_DEBUG_KMS("Invalid crtc for plane\n");
  2271.                 ret = -EINVAL;
  2272.                 goto out;
  2273.         }
  2274.  
  2275.         /* Check whether this plane supports the fb pixel format. */
  2276.         for (i = 0; i < plane->format_count; i++)
  2277.                 if (fb->pixel_format == plane->format_types[i])
  2278.                         break;
  2279.         if (i == plane->format_count) {
  2280.                 DRM_DEBUG_KMS("Invalid pixel format %s\n",
  2281.                               drm_get_format_name(fb->pixel_format));
  2282.                 ret = -EINVAL;
  2283.                 goto out;
  2284.         }
  2285.  
  2286.         fb_width = fb->width << 16;
  2287.         fb_height = fb->height << 16;
  2288.  
  2289.         /* Make sure source coordinates are inside the fb. */
  2290.         if (src_w > fb_width ||
  2291.             src_x > fb_width - src_w ||
  2292.             src_h > fb_height ||
  2293.             src_y > fb_height - src_h) {
  2294.                 DRM_DEBUG_KMS("Invalid source coordinates "
  2295.                               "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
  2296.                               src_w >> 16, ((src_w & 0xffff) * 15625) >> 10,
  2297.                               src_h >> 16, ((src_h & 0xffff) * 15625) >> 10,
  2298.                               src_x >> 16, ((src_x & 0xffff) * 15625) >> 10,
  2299.                               src_y >> 16, ((src_y & 0xffff) * 15625) >> 10);
  2300.                 ret = -ENOSPC;
  2301.                 goto out;
  2302.         }
  2303.  
  2304.         drm_modeset_lock_all(dev);
  2305.         old_fb = plane->fb;
  2306.         ret = plane->funcs->update_plane(plane, crtc, fb,
  2307.                                          crtc_x, crtc_y, crtc_w, crtc_h,
  2308.                                          src_x, src_y, src_w, src_h);
  2309.         if (!ret) {
  2310.                 plane->crtc = crtc;
  2311.                 plane->fb = fb;
  2312.                 fb = NULL;
  2313.         } else {
  2314.                 old_fb = NULL;
  2315.         }
  2316.         drm_modeset_unlock_all(dev);
  2317.  
  2318. out:
  2319.         if (fb)
  2320.                 drm_framebuffer_unreference(fb);
  2321.         if (old_fb)
  2322.                 drm_framebuffer_unreference(old_fb);
  2323.  
  2324.         return ret;
  2325.  
  2326. }
  2327.  
  2328. /**
  2329.  * drm_mode_setplane - configure a plane's configuration
  2330.  * @dev: DRM device
  2331.  * @data: ioctl data*
  2332.  * @file_priv: DRM file info
  2333.  *
  2334.  * Set plane configuration, including placement, fb, scaling, and other factors.
  2335.  * Or pass a NULL fb to disable (planes may be disabled without providing a
  2336.  * valid crtc).
  2337.  *
  2338.  * Returns:
  2339.  * Zero on success, errno on failure.
  2340.  */
  2341. int drm_mode_setplane(struct drm_device *dev, void *data,
  2342.                       struct drm_file *file_priv)
  2343. {
  2344.         struct drm_mode_set_plane *plane_req = data;
  2345.         struct drm_mode_object *obj;
  2346.         struct drm_plane *plane;
  2347.         struct drm_crtc *crtc = NULL;
  2348.         struct drm_framebuffer *fb = NULL;
  2349.  
  2350.         if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2351.                 return -EINVAL;
  2352.  
  2353.         /* Give drivers some help against integer overflows */
  2354.         if (plane_req->crtc_w > INT_MAX ||
  2355.             plane_req->crtc_x > INT_MAX - (int32_t) plane_req->crtc_w ||
  2356.             plane_req->crtc_h > INT_MAX ||
  2357.             plane_req->crtc_y > INT_MAX - (int32_t) plane_req->crtc_h) {
  2358.                 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
  2359.                               plane_req->crtc_w, plane_req->crtc_h,
  2360.                               plane_req->crtc_x, plane_req->crtc_y);
  2361.                 return -ERANGE;
  2362.         }
  2363.  
  2364.         /*
  2365.          * First, find the plane, crtc, and fb objects.  If not available,
  2366.          * we don't bother to call the driver.
  2367.          */
  2368.         obj = drm_mode_object_find(dev, plane_req->plane_id,
  2369.                                    DRM_MODE_OBJECT_PLANE);
  2370.         if (!obj) {
  2371.                 DRM_DEBUG_KMS("Unknown plane ID %d\n",
  2372.                               plane_req->plane_id);
  2373.                 return -ENOENT;
  2374.         }
  2375.         plane = obj_to_plane(obj);
  2376.  
  2377.         if (plane_req->fb_id) {
  2378.                 fb = drm_framebuffer_lookup(dev, plane_req->fb_id);
  2379.                 if (!fb) {
  2380.                         DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
  2381.                                       plane_req->fb_id);
  2382.                         return -ENOENT;
  2383.                 }
  2384.  
  2385.                 obj = drm_mode_object_find(dev, plane_req->crtc_id,
  2386.                                            DRM_MODE_OBJECT_CRTC);
  2387.                 if (!obj) {
  2388.                         DRM_DEBUG_KMS("Unknown crtc ID %d\n",
  2389.                                       plane_req->crtc_id);
  2390.                         return -ENOENT;
  2391.                 }
  2392.                 crtc = obj_to_crtc(obj);
  2393.         }
  2394.  
  2395.         /*
  2396.          * setplane_internal will take care of deref'ing either the old or new
  2397.          * framebuffer depending on success.
  2398.          */
  2399.         return setplane_internal(plane, crtc, fb,
  2400.                                  plane_req->crtc_x, plane_req->crtc_y,
  2401.                                  plane_req->crtc_w, plane_req->crtc_h,
  2402.                                  plane_req->src_x, plane_req->src_y,
  2403.                                  plane_req->src_w, plane_req->src_h);
  2404. }
  2405. #endif
  2406.  
  2407. /**
  2408.  * drm_mode_set_config_internal - helper to call ->set_config
  2409.  * @set: modeset config to set
  2410.  *
  2411.  * This is a little helper to wrap internal calls to the ->set_config driver
  2412.  * interface. The only thing it adds is correct refcounting dance.
  2413.  *
  2414.  * Returns:
  2415.  * Zero on success, errno on failure.
  2416.  */
  2417. int drm_mode_set_config_internal(struct drm_mode_set *set)
  2418. {
  2419.         struct drm_crtc *crtc = set->crtc;
  2420.         struct drm_framebuffer *fb;
  2421.         struct drm_crtc *tmp;
  2422.         int ret;
  2423.  
  2424.         /*
  2425.          * NOTE: ->set_config can also disable other crtcs (if we steal all
  2426.          * connectors from it), hence we need to refcount the fbs across all
  2427.          * crtcs. Atomic modeset will have saner semantics ...
  2428.          */
  2429.         list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head)
  2430.                 tmp->old_fb = tmp->primary->fb;
  2431.  
  2432.         fb = set->fb;
  2433.  
  2434.         ret = crtc->funcs->set_config(set);
  2435.         if (ret == 0) {
  2436.                 crtc->primary->crtc = crtc;
  2437.                 crtc->primary->fb = fb;
  2438.         }
  2439.  
  2440.         list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head) {
  2441.                 if (tmp->primary->fb)
  2442.                         drm_framebuffer_reference(tmp->primary->fb);
  2443. //              if (tmp->old_fb)
  2444. //                      drm_framebuffer_unreference(tmp->old_fb);
  2445.         }
  2446.  
  2447.         return ret;
  2448. }
  2449. EXPORT_SYMBOL(drm_mode_set_config_internal);
  2450.  
  2451. #if 0
  2452. /**
  2453.  * drm_crtc_check_viewport - Checks that a framebuffer is big enough for the
  2454.  *     CRTC viewport
  2455.  * @crtc: CRTC that framebuffer will be displayed on
  2456.  * @x: x panning
  2457.  * @y: y panning
  2458.  * @mode: mode that framebuffer will be displayed under
  2459.  * @fb: framebuffer to check size of
  2460.  */
  2461. int drm_crtc_check_viewport(const struct drm_crtc *crtc,
  2462.                                    int x, int y,
  2463.                                    const struct drm_display_mode *mode,
  2464.                                    const struct drm_framebuffer *fb)
  2465.  
  2466. {
  2467.         int hdisplay, vdisplay;
  2468.  
  2469.         hdisplay = mode->hdisplay;
  2470.         vdisplay = mode->vdisplay;
  2471.  
  2472.         if (drm_mode_is_stereo(mode)) {
  2473.                 struct drm_display_mode adjusted = *mode;
  2474.  
  2475.                 drm_mode_set_crtcinfo(&adjusted, CRTC_STEREO_DOUBLE);
  2476.                 hdisplay = adjusted.crtc_hdisplay;
  2477.                 vdisplay = adjusted.crtc_vdisplay;
  2478.         }
  2479.  
  2480.         if (crtc->invert_dimensions)
  2481.                 swap(hdisplay, vdisplay);
  2482.  
  2483.         if (hdisplay > fb->width ||
  2484.             vdisplay > fb->height ||
  2485.             x > fb->width - hdisplay ||
  2486.             y > fb->height - vdisplay) {
  2487.                 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
  2488.                               fb->width, fb->height, hdisplay, vdisplay, x, y,
  2489.                               crtc->invert_dimensions ? " (inverted)" : "");
  2490.                 return -ENOSPC;
  2491.         }
  2492.  
  2493.         return 0;
  2494. }
  2495. EXPORT_SYMBOL(drm_crtc_check_viewport);
  2496.  
  2497. /**
  2498.  * drm_mode_setcrtc - set CRTC configuration
  2499.  * @dev: drm device for the ioctl
  2500.  * @data: data pointer for the ioctl
  2501.  * @file_priv: drm file for the ioctl call
  2502.  *
  2503.  * Build a new CRTC configuration based on user request.
  2504.  *
  2505.  * Called by the user via ioctl.
  2506.  *
  2507.  * Returns:
  2508.  * Zero on success, errno on failure.
  2509.  */
  2510. int drm_mode_setcrtc(struct drm_device *dev, void *data,
  2511.                      struct drm_file *file_priv)
  2512. {
  2513.         struct drm_mode_config *config = &dev->mode_config;
  2514.         struct drm_mode_crtc *crtc_req = data;
  2515.         struct drm_crtc *crtc;
  2516.         struct drm_connector **connector_set = NULL, *connector;
  2517.         struct drm_framebuffer *fb = NULL;
  2518.         struct drm_display_mode *mode = NULL;
  2519.         struct drm_mode_set set;
  2520.         uint32_t __user *set_connectors_ptr;
  2521.         int ret;
  2522.         int i;
  2523.  
  2524.         if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2525.                 return -EINVAL;
  2526.  
  2527.         /* For some reason crtc x/y offsets are signed internally. */
  2528.         if (crtc_req->x > INT_MAX || crtc_req->y > INT_MAX)
  2529.                 return -ERANGE;
  2530.  
  2531.         drm_modeset_lock_all(dev);
  2532.         crtc = drm_crtc_find(dev, crtc_req->crtc_id);
  2533.         if (!crtc) {
  2534.                 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
  2535.                 ret = -ENOENT;
  2536.                 goto out;
  2537.         }
  2538.         DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
  2539.  
  2540.         if (crtc_req->mode_valid) {
  2541.                 /* If we have a mode we need a framebuffer. */
  2542.                 /* If we pass -1, set the mode with the currently bound fb */
  2543.                 if (crtc_req->fb_id == -1) {
  2544.                         if (!crtc->primary->fb) {
  2545.                                 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
  2546.                                 ret = -EINVAL;
  2547.                                 goto out;
  2548.                         }
  2549.                         fb = crtc->primary->fb;
  2550.                         /* Make refcounting symmetric with the lookup path. */
  2551.                         drm_framebuffer_reference(fb);
  2552.                 } else {
  2553.                         fb = drm_framebuffer_lookup(dev, crtc_req->fb_id);
  2554.                         if (!fb) {
  2555.                                 DRM_DEBUG_KMS("Unknown FB ID%d\n",
  2556.                                                 crtc_req->fb_id);
  2557.                                 ret = -ENOENT;
  2558.                                 goto out;
  2559.                         }
  2560.                 }
  2561.  
  2562.                 mode = drm_mode_create(dev);
  2563.                 if (!mode) {
  2564.                         ret = -ENOMEM;
  2565.                         goto out;
  2566.                 }
  2567.  
  2568.                 ret = drm_crtc_convert_umode(mode, &crtc_req->mode);
  2569.                 if (ret) {
  2570.                         DRM_DEBUG_KMS("Invalid mode\n");
  2571.                         goto out;
  2572.                 }
  2573.  
  2574.                 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
  2575.  
  2576.                 ret = drm_crtc_check_viewport(crtc, crtc_req->x, crtc_req->y,
  2577.                                               mode, fb);
  2578.                 if (ret)
  2579.                         goto out;
  2580.  
  2581.         }
  2582.  
  2583.         if (crtc_req->count_connectors == 0 && mode) {
  2584.                 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
  2585.                 ret = -EINVAL;
  2586.                 goto out;
  2587.         }
  2588.  
  2589.         if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
  2590.                 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
  2591.                           crtc_req->count_connectors);
  2592.                 ret = -EINVAL;
  2593.                 goto out;
  2594.         }
  2595.  
  2596.         if (crtc_req->count_connectors > 0) {
  2597.                 u32 out_id;
  2598.  
  2599.                 /* Avoid unbounded kernel memory allocation */
  2600.                 if (crtc_req->count_connectors > config->num_connector) {
  2601.                         ret = -EINVAL;
  2602.                         goto out;
  2603.                 }
  2604.  
  2605.                 connector_set = kmalloc(crtc_req->count_connectors *
  2606.                                         sizeof(struct drm_connector *),
  2607.                                         GFP_KERNEL);
  2608.                 if (!connector_set) {
  2609.                         ret = -ENOMEM;
  2610.                         goto out;
  2611.                 }
  2612.  
  2613.                 for (i = 0; i < crtc_req->count_connectors; i++) {
  2614.                         set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
  2615.                         if (get_user(out_id, &set_connectors_ptr[i])) {
  2616.                                 ret = -EFAULT;
  2617.                                 goto out;
  2618.                         }
  2619.  
  2620.                         connector = drm_connector_find(dev, out_id);
  2621.                         if (!connector) {
  2622.                                 DRM_DEBUG_KMS("Connector id %d unknown\n",
  2623.                                                 out_id);
  2624.                                 ret = -ENOENT;
  2625.                                 goto out;
  2626.                         }
  2627.                         DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  2628.                                         connector->base.id,
  2629.                                         connector->name);
  2630.  
  2631.                         connector_set[i] = connector;
  2632.                 }
  2633.         }
  2634.  
  2635.         set.crtc = crtc;
  2636.         set.x = crtc_req->x;
  2637.         set.y = crtc_req->y;
  2638.         set.mode = mode;
  2639.         set.connectors = connector_set;
  2640.         set.num_connectors = crtc_req->count_connectors;
  2641.         set.fb = fb;
  2642.         ret = drm_mode_set_config_internal(&set);
  2643.  
  2644. out:
  2645.         if (fb)
  2646.                 drm_framebuffer_unreference(fb);
  2647.  
  2648.         kfree(connector_set);
  2649.         drm_mode_destroy(dev, mode);
  2650.         drm_modeset_unlock_all(dev);
  2651.         return ret;
  2652. }
  2653.  
  2654. static int drm_mode_cursor_common(struct drm_device *dev,
  2655.                                   struct drm_mode_cursor2 *req,
  2656.                                   struct drm_file *file_priv)
  2657. {
  2658.         struct drm_crtc *crtc;
  2659.         int ret = 0;
  2660.  
  2661.         if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2662.                 return -EINVAL;
  2663.  
  2664.         if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
  2665.                 return -EINVAL;
  2666.  
  2667.         crtc = drm_crtc_find(dev, req->crtc_id);
  2668.         if (!crtc) {
  2669.                 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
  2670.                 return -ENOENT;
  2671.         }
  2672.  
  2673.         /*
  2674.          * If this crtc has a universal cursor plane, call that plane's update
  2675.          * handler rather than using legacy cursor handlers.
  2676.          */
  2677.         if (crtc->cursor)
  2678.                 return drm_mode_cursor_universal(crtc, req, file_priv);
  2679.  
  2680.         drm_modeset_lock(&crtc->mutex, NULL);
  2681.         if (req->flags & DRM_MODE_CURSOR_BO) {
  2682.                 if (!crtc->funcs->cursor_set && !crtc->funcs->cursor_set2) {
  2683.                         ret = -ENXIO;
  2684.                         goto out;
  2685.                 }
  2686.                 /* Turns off the cursor if handle is 0 */
  2687.                 if (crtc->funcs->cursor_set2)
  2688.                         ret = crtc->funcs->cursor_set2(crtc, file_priv, req->handle,
  2689.                                                       req->width, req->height, req->hot_x, req->hot_y);
  2690.                 else
  2691.                 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
  2692.                                               req->width, req->height);
  2693.         }
  2694.  
  2695.         if (req->flags & DRM_MODE_CURSOR_MOVE) {
  2696.                 if (crtc->funcs->cursor_move) {
  2697.                         ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
  2698.                 } else {
  2699.                         ret = -EFAULT;
  2700.                         goto out;
  2701.                 }
  2702.         }
  2703. out:
  2704.         drm_modeset_unlock(&crtc->mutex);
  2705.  
  2706.         return ret;
  2707.  
  2708. }
  2709.  
  2710.  
  2711. /**
  2712.  * drm_mode_cursor_ioctl - set CRTC's cursor configuration
  2713.  * @dev: drm device for the ioctl
  2714.  * @data: data pointer for the ioctl
  2715.  * @file_priv: drm file for the ioctl call
  2716.  *
  2717.  * Set the cursor configuration based on user request.
  2718.  *
  2719.  * Called by the user via ioctl.
  2720.  *
  2721.  * Returns:
  2722.  * Zero on success, errno on failure.
  2723.  */
  2724. int drm_mode_cursor_ioctl(struct drm_device *dev,
  2725.                         void *data, struct drm_file *file_priv)
  2726. {
  2727.         struct drm_mode_cursor *req = data;
  2728.         struct drm_mode_cursor2 new_req;
  2729.  
  2730.         memcpy(&new_req, req, sizeof(struct drm_mode_cursor));
  2731.         new_req.hot_x = new_req.hot_y = 0;
  2732.  
  2733.         return drm_mode_cursor_common(dev, &new_req, file_priv);
  2734. }
  2735.  
  2736. /**
  2737.  * drm_mode_cursor2_ioctl - set CRTC's cursor configuration
  2738.  * @dev: drm device for the ioctl
  2739.  * @data: data pointer for the ioctl
  2740.  * @file_priv: drm file for the ioctl call
  2741.  *
  2742.  * Set the cursor configuration based on user request. This implements the 2nd
  2743.  * version of the cursor ioctl, which allows userspace to additionally specify
  2744.  * the hotspot of the pointer.
  2745.  *
  2746.  * Called by the user via ioctl.
  2747.  *
  2748.  * Returns:
  2749.  * Zero on success, errno on failure.
  2750.  */
  2751. int drm_mode_cursor2_ioctl(struct drm_device *dev,
  2752.                            void *data, struct drm_file *file_priv)
  2753. {
  2754.         struct drm_mode_cursor2 *req = data;
  2755.         return drm_mode_cursor_common(dev, req, file_priv);
  2756. }
  2757. #endif
  2758.  
  2759. /**
  2760.  * drm_mode_legacy_fb_format - compute drm fourcc code from legacy description
  2761.  * @bpp: bits per pixels
  2762.  * @depth: bit depth per pixel
  2763.  *
  2764.  * Computes a drm fourcc pixel format code for the given @bpp/@depth values.
  2765.  * Useful in fbdev emulation code, since that deals in those values.
  2766.  */
  2767. uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
  2768. {
  2769.         uint32_t fmt;
  2770.  
  2771.         switch (bpp) {
  2772.         case 8:
  2773.                 fmt = DRM_FORMAT_C8;
  2774.                 break;
  2775.         case 16:
  2776.                 if (depth == 15)
  2777.                         fmt = DRM_FORMAT_XRGB1555;
  2778.                 else
  2779.                         fmt = DRM_FORMAT_RGB565;
  2780.                 break;
  2781.         case 24:
  2782.                 fmt = DRM_FORMAT_RGB888;
  2783.                 break;
  2784.         case 32:
  2785.                 if (depth == 24)
  2786.                         fmt = DRM_FORMAT_XRGB8888;
  2787.                 else if (depth == 30)
  2788.                         fmt = DRM_FORMAT_XRGB2101010;
  2789.                 else
  2790.                         fmt = DRM_FORMAT_ARGB8888;
  2791.                 break;
  2792.         default:
  2793.                 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
  2794.                 fmt = DRM_FORMAT_XRGB8888;
  2795.                 break;
  2796.         }
  2797.  
  2798.         return fmt;
  2799. }
  2800. EXPORT_SYMBOL(drm_mode_legacy_fb_format);
  2801. #if 0
  2802. /**
  2803.  * drm_mode_addfb - add an FB to the graphics configuration
  2804.  * @dev: drm device for the ioctl
  2805.  * @data: data pointer for the ioctl
  2806.  * @file_priv: drm file for the ioctl call
  2807.  *
  2808.  * Add a new FB to the specified CRTC, given a user request. This is the
  2809.  * original addfb ioclt which only supported RGB formats.
  2810.  *
  2811.  * Called by the user via ioctl.
  2812.  *
  2813.  * Returns:
  2814.  * Zero on success, errno on failure.
  2815.  */
  2816. int drm_mode_addfb(struct drm_device *dev,
  2817.                    void *data, struct drm_file *file_priv)
  2818. {
  2819.         struct drm_mode_fb_cmd *or = data;
  2820.         struct drm_mode_fb_cmd2 r = {};
  2821.         struct drm_mode_config *config = &dev->mode_config;
  2822.         struct drm_framebuffer *fb;
  2823.         int ret = 0;
  2824.  
  2825.         /* Use new struct with format internally */
  2826.         r.fb_id = or->fb_id;
  2827.         r.width = or->width;
  2828.         r.height = or->height;
  2829.         r.pitches[0] = or->pitch;
  2830.         r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
  2831.         r.handles[0] = or->handle;
  2832.  
  2833.         if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2834.                 return -EINVAL;
  2835.  
  2836.         if ((config->min_width > r.width) || (r.width > config->max_width))
  2837.                 return -EINVAL;
  2838.  
  2839.         if ((config->min_height > r.height) || (r.height > config->max_height))
  2840.                 return -EINVAL;
  2841.  
  2842.         fb = dev->mode_config.funcs->fb_create(dev, file_priv, &r);
  2843.         if (IS_ERR(fb)) {
  2844.                 DRM_DEBUG_KMS("could not create framebuffer\n");
  2845.                 return PTR_ERR(fb);
  2846.         }
  2847.  
  2848.         mutex_lock(&file_priv->fbs_lock);
  2849.         or->fb_id = fb->base.id;
  2850.         list_add(&fb->filp_head, &file_priv->fbs);
  2851.         DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
  2852.         mutex_unlock(&file_priv->fbs_lock);
  2853.  
  2854.         return ret;
  2855. }
  2856.  
  2857. static int format_check(const struct drm_mode_fb_cmd2 *r)
  2858. {
  2859.         uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
  2860.  
  2861.         switch (format) {
  2862.         case DRM_FORMAT_C8:
  2863.         case DRM_FORMAT_RGB332:
  2864.         case DRM_FORMAT_BGR233:
  2865.         case DRM_FORMAT_XRGB4444:
  2866.         case DRM_FORMAT_XBGR4444:
  2867.         case DRM_FORMAT_RGBX4444:
  2868.         case DRM_FORMAT_BGRX4444:
  2869.         case DRM_FORMAT_ARGB4444:
  2870.         case DRM_FORMAT_ABGR4444:
  2871.         case DRM_FORMAT_RGBA4444:
  2872.         case DRM_FORMAT_BGRA4444:
  2873.         case DRM_FORMAT_XRGB1555:
  2874.         case DRM_FORMAT_XBGR1555:
  2875.         case DRM_FORMAT_RGBX5551:
  2876.         case DRM_FORMAT_BGRX5551:
  2877.         case DRM_FORMAT_ARGB1555:
  2878.         case DRM_FORMAT_ABGR1555:
  2879.         case DRM_FORMAT_RGBA5551:
  2880.         case DRM_FORMAT_BGRA5551:
  2881.         case DRM_FORMAT_RGB565:
  2882.         case DRM_FORMAT_BGR565:
  2883.         case DRM_FORMAT_RGB888:
  2884.         case DRM_FORMAT_BGR888:
  2885.         case DRM_FORMAT_XRGB8888:
  2886.         case DRM_FORMAT_XBGR8888:
  2887.         case DRM_FORMAT_RGBX8888:
  2888.         case DRM_FORMAT_BGRX8888:
  2889.         case DRM_FORMAT_ARGB8888:
  2890.         case DRM_FORMAT_ABGR8888:
  2891.         case DRM_FORMAT_RGBA8888:
  2892.         case DRM_FORMAT_BGRA8888:
  2893.         case DRM_FORMAT_XRGB2101010:
  2894.         case DRM_FORMAT_XBGR2101010:
  2895.         case DRM_FORMAT_RGBX1010102:
  2896.         case DRM_FORMAT_BGRX1010102:
  2897.         case DRM_FORMAT_ARGB2101010:
  2898.         case DRM_FORMAT_ABGR2101010:
  2899.         case DRM_FORMAT_RGBA1010102:
  2900.         case DRM_FORMAT_BGRA1010102:
  2901.         case DRM_FORMAT_YUYV:
  2902.         case DRM_FORMAT_YVYU:
  2903.         case DRM_FORMAT_UYVY:
  2904.         case DRM_FORMAT_VYUY:
  2905.         case DRM_FORMAT_AYUV:
  2906.         case DRM_FORMAT_NV12:
  2907.         case DRM_FORMAT_NV21:
  2908.         case DRM_FORMAT_NV16:
  2909.         case DRM_FORMAT_NV61:
  2910.         case DRM_FORMAT_NV24:
  2911.         case DRM_FORMAT_NV42:
  2912.         case DRM_FORMAT_YUV410:
  2913.         case DRM_FORMAT_YVU410:
  2914.         case DRM_FORMAT_YUV411:
  2915.         case DRM_FORMAT_YVU411:
  2916.         case DRM_FORMAT_YUV420:
  2917.         case DRM_FORMAT_YVU420:
  2918.         case DRM_FORMAT_YUV422:
  2919.         case DRM_FORMAT_YVU422:
  2920.         case DRM_FORMAT_YUV444:
  2921.         case DRM_FORMAT_YVU444:
  2922.                 return 0;
  2923.         default:
  2924.                 DRM_DEBUG_KMS("invalid pixel format %s\n",
  2925.                               drm_get_format_name(r->pixel_format));
  2926.                 return -EINVAL;
  2927.         }
  2928. }
  2929.  
  2930. static int framebuffer_check(const struct drm_mode_fb_cmd2 *r)
  2931. {
  2932.         int ret, hsub, vsub, num_planes, i;
  2933.  
  2934.         ret = format_check(r);
  2935.         if (ret) {
  2936.                 DRM_DEBUG_KMS("bad framebuffer format %s\n",
  2937.                               drm_get_format_name(r->pixel_format));
  2938.                 return ret;
  2939.         }
  2940.  
  2941.         hsub = drm_format_horz_chroma_subsampling(r->pixel_format);
  2942.         vsub = drm_format_vert_chroma_subsampling(r->pixel_format);
  2943.         num_planes = drm_format_num_planes(r->pixel_format);
  2944.  
  2945.         if (r->width == 0 || r->width % hsub) {
  2946.                 DRM_DEBUG_KMS("bad framebuffer width %u\n", r->height);
  2947.                 return -EINVAL;
  2948.         }
  2949.  
  2950.         if (r->height == 0 || r->height % vsub) {
  2951.                 DRM_DEBUG_KMS("bad framebuffer height %u\n", r->height);
  2952.                 return -EINVAL;
  2953.         }
  2954.  
  2955.         for (i = 0; i < num_planes; i++) {
  2956.                 unsigned int width = r->width / (i != 0 ? hsub : 1);
  2957.                 unsigned int height = r->height / (i != 0 ? vsub : 1);
  2958.                 unsigned int cpp = drm_format_plane_cpp(r->pixel_format, i);
  2959.  
  2960.                 if (!r->handles[i]) {
  2961.                         DRM_DEBUG_KMS("no buffer object handle for plane %d\n", i);
  2962.                         return -EINVAL;
  2963.                 }
  2964.  
  2965.                 if ((uint64_t) width * cpp > UINT_MAX)
  2966.                         return -ERANGE;
  2967.  
  2968.                 if ((uint64_t) height * r->pitches[i] + r->offsets[i] > UINT_MAX)
  2969.                         return -ERANGE;
  2970.  
  2971.                 if (r->pitches[i] < width * cpp) {
  2972.                         DRM_DEBUG_KMS("bad pitch %u for plane %d\n", r->pitches[i], i);
  2973.                         return -EINVAL;
  2974.                 }
  2975.         }
  2976.  
  2977.         return 0;
  2978. }
  2979.  
  2980. static struct drm_framebuffer *add_framebuffer_internal(struct drm_device *dev,
  2981.                                                         struct drm_mode_fb_cmd2 *r,
  2982.                                                         struct drm_file *file_priv)
  2983. {
  2984.         struct drm_mode_config *config = &dev->mode_config;
  2985.         struct drm_framebuffer *fb;
  2986.         int ret;
  2987.  
  2988.         if (r->flags & ~DRM_MODE_FB_INTERLACED) {
  2989.                 DRM_DEBUG_KMS("bad framebuffer flags 0x%08x\n", r->flags);
  2990.                 return ERR_PTR(-EINVAL);
  2991.         }
  2992.  
  2993.         if ((config->min_width > r->width) || (r->width > config->max_width)) {
  2994.                 DRM_DEBUG_KMS("bad framebuffer width %d, should be >= %d && <= %d\n",
  2995.                           r->width, config->min_width, config->max_width);
  2996.                 return ERR_PTR(-EINVAL);
  2997.         }
  2998.         if ((config->min_height > r->height) || (r->height > config->max_height)) {
  2999.                 DRM_DEBUG_KMS("bad framebuffer height %d, should be >= %d && <= %d\n",
  3000.                           r->height, config->min_height, config->max_height);
  3001.                 return ERR_PTR(-EINVAL);
  3002.         }
  3003.  
  3004.         ret = framebuffer_check(r);
  3005.         if (ret)
  3006.                 return ERR_PTR(ret);
  3007.  
  3008.         fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
  3009.         if (IS_ERR(fb)) {
  3010.                 DRM_DEBUG_KMS("could not create framebuffer\n");
  3011.                 return fb;
  3012.         }
  3013.  
  3014.         mutex_lock(&file_priv->fbs_lock);
  3015.         r->fb_id = fb->base.id;
  3016.         list_add(&fb->filp_head, &file_priv->fbs);
  3017.         DRM_DEBUG_KMS("[FB:%d]\n", fb->base.id);
  3018.         mutex_unlock(&file_priv->fbs_lock);
  3019.  
  3020.         return fb;
  3021. }
  3022.  
  3023. /**
  3024.  * drm_mode_addfb2 - add an FB to the graphics configuration
  3025.  * @dev: drm device for the ioctl
  3026.  * @data: data pointer for the ioctl
  3027.  * @file_priv: drm file for the ioctl call
  3028.  *
  3029.  * Add a new FB to the specified CRTC, given a user request with format. This is
  3030.  * the 2nd version of the addfb ioctl, which supports multi-planar framebuffers
  3031.  * and uses fourcc codes as pixel format specifiers.
  3032.  *
  3033.  * Called by the user via ioctl.
  3034.  *
  3035.  * Returns:
  3036.  * Zero on success, errno on failure.
  3037.  */
  3038. int drm_mode_addfb2(struct drm_device *dev,
  3039.                     void *data, struct drm_file *file_priv)
  3040. {
  3041.         struct drm_framebuffer *fb;
  3042.  
  3043.         if (!drm_core_check_feature(dev, DRIVER_MODESET))
  3044.                 return -EINVAL;
  3045.  
  3046.         fb = add_framebuffer_internal(dev, data, file_priv);
  3047.         if (IS_ERR(fb))
  3048.                 return PTR_ERR(fb);
  3049.  
  3050.         return 0;
  3051. }
  3052.  
  3053. /**
  3054.  * drm_mode_rmfb - remove an FB from the configuration
  3055.  * @dev: drm device for the ioctl
  3056.  * @data: data pointer for the ioctl
  3057.  * @file_priv: drm file for the ioctl call
  3058.  *
  3059.  * Remove the FB specified by the user.
  3060.  *
  3061.  * Called by the user via ioctl.
  3062.  *
  3063.  * Returns:
  3064.  * Zero on success, errno on failure.
  3065.  */
  3066. int drm_mode_rmfb(struct drm_device *dev,
  3067.                    void *data, struct drm_file *file_priv)
  3068. {
  3069.         struct drm_framebuffer *fb = NULL;
  3070.         struct drm_framebuffer *fbl = NULL;
  3071.         uint32_t *id = data;
  3072.         int found = 0;
  3073.  
  3074.         if (!drm_core_check_feature(dev, DRIVER_MODESET))
  3075.                 return -EINVAL;
  3076.  
  3077.         mutex_lock(&file_priv->fbs_lock);
  3078.         mutex_lock(&dev->mode_config.fb_lock);
  3079.         fb = __drm_framebuffer_lookup(dev, *id);
  3080.         if (!fb)
  3081.                 goto fail_lookup;
  3082.  
  3083.         list_for_each_entry(fbl, &file_priv->fbs, filp_head)
  3084.                 if (fb == fbl)
  3085.                         found = 1;
  3086.         if (!found)
  3087.                 goto fail_lookup;
  3088.  
  3089.         /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
  3090.         __drm_framebuffer_unregister(dev, fb);
  3091.  
  3092.         list_del_init(&fb->filp_head);
  3093.         mutex_unlock(&dev->mode_config.fb_lock);
  3094.         mutex_unlock(&file_priv->fbs_lock);
  3095.  
  3096.         drm_framebuffer_remove(fb);
  3097.  
  3098.         return 0;
  3099.  
  3100. fail_lookup:
  3101.         mutex_unlock(&dev->mode_config.fb_lock);
  3102.         mutex_unlock(&file_priv->fbs_lock);
  3103.  
  3104.         return -ENOENT;
  3105. }
  3106.  
  3107. /**
  3108.  * drm_mode_getfb - get FB info
  3109.  * @dev: drm device for the ioctl
  3110.  * @data: data pointer for the ioctl
  3111.  * @file_priv: drm file for the ioctl call
  3112.  *
  3113.  * Lookup the FB given its ID and return info about it.
  3114.  *
  3115.  * Called by the user via ioctl.
  3116.  *
  3117.  * Returns:
  3118.  * Zero on success, errno on failure.
  3119.  */
  3120. int drm_mode_getfb(struct drm_device *dev,
  3121.                    void *data, struct drm_file *file_priv)
  3122. {
  3123.         struct drm_mode_fb_cmd *r = data;
  3124.         struct drm_framebuffer *fb;
  3125.         int ret;
  3126.  
  3127.         if (!drm_core_check_feature(dev, DRIVER_MODESET))
  3128.                 return -EINVAL;
  3129.  
  3130.         fb = drm_framebuffer_lookup(dev, r->fb_id);
  3131.         if (!fb)
  3132.                 return -ENOENT;
  3133.  
  3134.         r->height = fb->height;
  3135.         r->width = fb->width;
  3136.         r->depth = fb->depth;
  3137.         r->bpp = fb->bits_per_pixel;
  3138.         r->pitch = fb->pitches[0];
  3139.         if (fb->funcs->create_handle) {
  3140.                 if (file_priv->is_master || capable(CAP_SYS_ADMIN)) {
  3141.                         ret = fb->funcs->create_handle(fb, file_priv,
  3142.                                                        &r->handle);
  3143.                 } else {
  3144.                         /* GET_FB() is an unprivileged ioctl so we must not
  3145.                          * return a buffer-handle to non-master processes! For
  3146.                          * backwards-compatibility reasons, we cannot make
  3147.                          * GET_FB() privileged, so just return an invalid handle
  3148.                          * for non-masters. */
  3149.                         r->handle = 0;
  3150.                         ret = 0;
  3151.                 }
  3152.         } else {
  3153.                 ret = -ENODEV;
  3154.         }
  3155.  
  3156.         drm_framebuffer_unreference(fb);
  3157.  
  3158.         return ret;
  3159. }
  3160.  
  3161. /**
  3162.  * drm_mode_dirtyfb_ioctl - flush frontbuffer rendering on an FB
  3163.  * @dev: drm device for the ioctl
  3164.  * @data: data pointer for the ioctl
  3165.  * @file_priv: drm file for the ioctl call
  3166.  *
  3167.  * Lookup the FB and flush out the damaged area supplied by userspace as a clip
  3168.  * rectangle list. Generic userspace which does frontbuffer rendering must call
  3169.  * this ioctl to flush out the changes on manual-update display outputs, e.g.
  3170.  * usb display-link, mipi manual update panels or edp panel self refresh modes.
  3171.  *
  3172.  * Modesetting drivers which always update the frontbuffer do not need to
  3173.  * implement the corresponding ->dirty framebuffer callback.
  3174.  *
  3175.  * Called by the user via ioctl.
  3176.  *
  3177.  * Returns:
  3178.  * Zero on success, errno on failure.
  3179.  */
  3180. int drm_mode_dirtyfb_ioctl(struct drm_device *dev,
  3181.                            void *data, struct drm_file *file_priv)
  3182. {
  3183.         struct drm_clip_rect __user *clips_ptr;
  3184.         struct drm_clip_rect *clips = NULL;
  3185.         struct drm_mode_fb_dirty_cmd *r = data;
  3186.         struct drm_framebuffer *fb;
  3187.         unsigned flags;
  3188.         int num_clips;
  3189.         int ret;
  3190.  
  3191.         if (!drm_core_check_feature(dev, DRIVER_MODESET))
  3192.                 return -EINVAL;
  3193.  
  3194.         fb = drm_framebuffer_lookup(dev, r->fb_id);
  3195.         if (!fb)
  3196.                 return -ENOENT;
  3197.  
  3198.         num_clips = r->num_clips;
  3199.         clips_ptr = (struct drm_clip_rect __user *)(unsigned long)r->clips_ptr;
  3200.  
  3201.         if (!num_clips != !clips_ptr) {
  3202.                 ret = -EINVAL;
  3203.                 goto out_err1;
  3204.         }
  3205.  
  3206.         flags = DRM_MODE_FB_DIRTY_FLAGS & r->flags;
  3207.  
  3208.         /* If userspace annotates copy, clips must come in pairs */
  3209.         if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY && (num_clips % 2)) {
  3210.                 ret = -EINVAL;
  3211.                 goto out_err1;
  3212.         }
  3213.  
  3214.         if (num_clips && clips_ptr) {
  3215.                 if (num_clips < 0 || num_clips > DRM_MODE_FB_DIRTY_MAX_CLIPS) {
  3216.                         ret = -EINVAL;
  3217.                         goto out_err1;
  3218.                 }
  3219.                 clips = kzalloc(num_clips * sizeof(*clips), GFP_KERNEL);
  3220.                 if (!clips) {
  3221.                         ret = -ENOMEM;
  3222.                         goto out_err1;
  3223.                 }
  3224.  
  3225.                 ret = copy_from_user(clips, clips_ptr,
  3226.                                      num_clips * sizeof(*clips));
  3227.                 if (ret) {
  3228.                         ret = -EFAULT;
  3229.                         goto out_err2;
  3230.         }
  3231.         }
  3232.  
  3233.         if (fb->funcs->dirty) {
  3234.                 ret = fb->funcs->dirty(fb, file_priv, flags, r->color,
  3235.                                        clips, num_clips);
  3236.         } else {
  3237.                 ret = -ENOSYS;
  3238.         }
  3239.  
  3240. out_err2:
  3241.         kfree(clips);
  3242. out_err1:
  3243.         drm_framebuffer_unreference(fb);
  3244.  
  3245.         return ret;
  3246. }
  3247.  
  3248.  
  3249. /**
  3250.  * drm_fb_release - remove and free the FBs on this file
  3251.  * @priv: drm file for the ioctl
  3252.  *
  3253.  * Destroy all the FBs associated with @filp.
  3254.  *
  3255.  * Called by the user via ioctl.
  3256.  *
  3257.  * Returns:
  3258.  * Zero on success, errno on failure.
  3259.  */
  3260. void drm_fb_release(struct drm_file *priv)
  3261. {
  3262.         struct drm_device *dev = priv->minor->dev;
  3263.         struct drm_framebuffer *fb, *tfb;
  3264.  
  3265.         mutex_lock(&priv->fbs_lock);
  3266.         list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
  3267.  
  3268.                 mutex_lock(&dev->mode_config.fb_lock);
  3269.                 /* Mark fb as reaped, we still have a ref from fpriv->fbs. */
  3270.                 __drm_framebuffer_unregister(dev, fb);
  3271.                 mutex_unlock(&dev->mode_config.fb_lock);
  3272.  
  3273.                 list_del_init(&fb->filp_head);
  3274.  
  3275.                 /* This will also drop the fpriv->fbs reference. */
  3276.                 drm_framebuffer_remove(fb);
  3277.         }
  3278.         mutex_unlock(&priv->fbs_lock);
  3279. }
  3280. #endif
  3281.  
  3282.  
  3283. /**
  3284.  * drm_property_create - create a new property type
  3285.  * @dev: drm device
  3286.  * @flags: flags specifying the property type
  3287.  * @name: name of the property
  3288.  * @num_values: number of pre-defined values
  3289.  *
  3290.  * This creates a new generic drm property which can then be attached to a drm
  3291.  * object with drm_object_attach_property. The returned property object must be
  3292.  * freed with drm_property_destroy.
  3293.  *
  3294.  * Returns:
  3295.  * A pointer to the newly created property on success, NULL on failure.
  3296.  */
  3297. struct drm_property *drm_property_create(struct drm_device *dev, int flags,
  3298.                                          const char *name, int num_values)
  3299. {
  3300.         struct drm_property *property = NULL;
  3301.         int ret;
  3302.  
  3303.         property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
  3304.         if (!property)
  3305.                 return NULL;
  3306.  
  3307.         property->dev = dev;
  3308.  
  3309.         if (num_values) {
  3310.                 property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
  3311.                 if (!property->values)
  3312.                         goto fail;
  3313.         }
  3314.  
  3315.         ret = drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
  3316.         if (ret)
  3317.                 goto fail;
  3318.  
  3319.         property->flags = flags;
  3320.         property->num_values = num_values;
  3321.         INIT_LIST_HEAD(&property->enum_blob_list);
  3322.  
  3323.         if (name) {
  3324.                 strncpy(property->name, name, DRM_PROP_NAME_LEN);
  3325.                 property->name[DRM_PROP_NAME_LEN-1] = '\0';
  3326.         }
  3327.  
  3328.         list_add_tail(&property->head, &dev->mode_config.property_list);
  3329.  
  3330.         WARN_ON(!drm_property_type_valid(property));
  3331.  
  3332.         return property;
  3333. fail:
  3334.         kfree(property->values);
  3335.         kfree(property);
  3336.         return NULL;
  3337. }
  3338. EXPORT_SYMBOL(drm_property_create);
  3339.  
  3340. /**
  3341.  * drm_property_create_enum - create a new enumeration property type
  3342.  * @dev: drm device
  3343.  * @flags: flags specifying the property type
  3344.  * @name: name of the property
  3345.  * @props: enumeration lists with property values
  3346.  * @num_values: number of pre-defined values
  3347.  *
  3348.  * This creates a new generic drm property which can then be attached to a drm
  3349.  * object with drm_object_attach_property. The returned property object must be
  3350.  * freed with drm_property_destroy.
  3351.  *
  3352.  * Userspace is only allowed to set one of the predefined values for enumeration
  3353.  * properties.
  3354.  *
  3355.  * Returns:
  3356.  * A pointer to the newly created property on success, NULL on failure.
  3357.  */
  3358. struct drm_property *drm_property_create_enum(struct drm_device *dev, int flags,
  3359.                                          const char *name,
  3360.                                          const struct drm_prop_enum_list *props,
  3361.                                          int num_values)
  3362. {
  3363.         struct drm_property *property;
  3364.         int i, ret;
  3365.  
  3366.         flags |= DRM_MODE_PROP_ENUM;
  3367.  
  3368.         property = drm_property_create(dev, flags, name, num_values);
  3369.         if (!property)
  3370.                 return NULL;
  3371.  
  3372.         for (i = 0; i < num_values; i++) {
  3373.                 ret = drm_property_add_enum(property, i,
  3374.                                       props[i].type,
  3375.                                       props[i].name);
  3376.                 if (ret) {
  3377.                         drm_property_destroy(dev, property);
  3378.                         return NULL;
  3379.                 }
  3380.         }
  3381.  
  3382.         return property;
  3383. }
  3384. EXPORT_SYMBOL(drm_property_create_enum);
  3385.  
  3386. /**
  3387.  * drm_property_create_bitmask - create a new bitmask property type
  3388.  * @dev: drm device
  3389.  * @flags: flags specifying the property type
  3390.  * @name: name of the property
  3391.  * @props: enumeration lists with property bitflags
  3392.  * @num_values: number of pre-defined values
  3393.  *
  3394.  * This creates a new generic drm property which can then be attached to a drm
  3395.  * object with drm_object_attach_property. The returned property object must be
  3396.  * freed with drm_property_destroy.
  3397.  *
  3398.  * Compared to plain enumeration properties userspace is allowed to set any
  3399.  * or'ed together combination of the predefined property bitflag values
  3400.  *
  3401.  * Returns:
  3402.  * A pointer to the newly created property on success, NULL on failure.
  3403.  */
  3404. struct drm_property *drm_property_create_bitmask(struct drm_device *dev,
  3405.                                          int flags, const char *name,
  3406.                                          const struct drm_prop_enum_list *props,
  3407.                                          int num_props,
  3408.                                          uint64_t supported_bits)
  3409. {
  3410.         struct drm_property *property;
  3411.         int i, ret, index = 0;
  3412.         int num_values = hweight64(supported_bits);
  3413.  
  3414.         flags |= DRM_MODE_PROP_BITMASK;
  3415.  
  3416.         property = drm_property_create(dev, flags, name, num_values);
  3417.         if (!property)
  3418.                 return NULL;
  3419.         for (i = 0; i < num_props; i++) {
  3420.                 if (!(supported_bits & (1ULL << props[i].type)))
  3421.                         continue;
  3422.  
  3423.                 if (WARN_ON(index >= num_values)) {
  3424.                         drm_property_destroy(dev, property);
  3425.                         return NULL;
  3426.                 }
  3427.  
  3428.                 ret = drm_property_add_enum(property, index++,
  3429.                                       props[i].type,
  3430.                                       props[i].name);
  3431.                 if (ret) {
  3432.                         drm_property_destroy(dev, property);
  3433.                         return NULL;
  3434.                 }
  3435.         }
  3436.  
  3437.         return property;
  3438. }
  3439. EXPORT_SYMBOL(drm_property_create_bitmask);
  3440.  
  3441. static struct drm_property *property_create_range(struct drm_device *dev,
  3442.                                          int flags, const char *name,
  3443.                                          uint64_t min, uint64_t max)
  3444. {
  3445.         struct drm_property *property;
  3446.  
  3447.         property = drm_property_create(dev, flags, name, 2);
  3448.         if (!property)
  3449.                 return NULL;
  3450.  
  3451.         property->values[0] = min;
  3452.         property->values[1] = max;
  3453.  
  3454.         return property;
  3455. }
  3456.  
  3457. /**
  3458.  * drm_property_create_range - create a new ranged property type
  3459.  * @dev: drm device
  3460.  * @flags: flags specifying the property type
  3461.  * @name: name of the property
  3462.  * @min: minimum value of the property
  3463.  * @max: maximum value of the property
  3464.  *
  3465.  * This creates a new generic drm property which can then be attached to a drm
  3466.  * object with drm_object_attach_property. The returned property object must be
  3467.  * freed with drm_property_destroy.
  3468.  *
  3469.  * Userspace is allowed to set any interger value in the (min, max) range
  3470.  * inclusive.
  3471.  *
  3472.  * Returns:
  3473.  * A pointer to the newly created property on success, NULL on failure.
  3474.  */
  3475. struct drm_property *drm_property_create_range(struct drm_device *dev, int flags,
  3476.                                          const char *name,
  3477.                                          uint64_t min, uint64_t max)
  3478. {
  3479.         return property_create_range(dev, DRM_MODE_PROP_RANGE | flags,
  3480.                         name, min, max);
  3481. }
  3482. EXPORT_SYMBOL(drm_property_create_range);
  3483.  
  3484. struct drm_property *drm_property_create_signed_range(struct drm_device *dev,
  3485.                                          int flags, const char *name,
  3486.                                          int64_t min, int64_t max)
  3487. {
  3488.         return property_create_range(dev, DRM_MODE_PROP_SIGNED_RANGE | flags,
  3489.                         name, I642U64(min), I642U64(max));
  3490. }
  3491. EXPORT_SYMBOL(drm_property_create_signed_range);
  3492.  
  3493. struct drm_property *drm_property_create_object(struct drm_device *dev,
  3494.                                          int flags, const char *name, uint32_t type)
  3495. {
  3496.         struct drm_property *property;
  3497.  
  3498.         flags |= DRM_MODE_PROP_OBJECT;
  3499.  
  3500.         property = drm_property_create(dev, flags, name, 1);
  3501.         if (!property)
  3502.                 return NULL;
  3503.  
  3504.         property->values[0] = type;
  3505.  
  3506.         return property;
  3507. }
  3508. EXPORT_SYMBOL(drm_property_create_object);
  3509.  
  3510. /**
  3511.  * drm_property_add_enum - add a possible value to an enumeration property
  3512.  * @property: enumeration property to change
  3513.  * @index: index of the new enumeration
  3514.  * @value: value of the new enumeration
  3515.  * @name: symbolic name of the new enumeration
  3516.  *
  3517.  * This functions adds enumerations to a property.
  3518.  *
  3519.  * It's use is deprecated, drivers should use one of the more specific helpers
  3520.  * to directly create the property with all enumerations already attached.
  3521.  *
  3522.  * Returns:
  3523.  * Zero on success, error code on failure.
  3524.  */
  3525. int drm_property_add_enum(struct drm_property *property, int index,
  3526.                           uint64_t value, const char *name)
  3527. {
  3528.         struct drm_property_enum *prop_enum;
  3529.  
  3530.         if (!(drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
  3531.                         drm_property_type_is(property, DRM_MODE_PROP_BITMASK)))
  3532.                 return -EINVAL;
  3533.  
  3534.         /*
  3535.          * Bitmask enum properties have the additional constraint of values
  3536.          * from 0 to 63
  3537.          */
  3538.         if (drm_property_type_is(property, DRM_MODE_PROP_BITMASK) &&
  3539.                         (value > 63))
  3540.                 return -EINVAL;
  3541.  
  3542.         if (!list_empty(&property->enum_blob_list)) {
  3543.                 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
  3544.                         if (prop_enum->value == value) {
  3545.                                 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
  3546.                                 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
  3547.                                 return 0;
  3548.                         }
  3549.                 }
  3550.         }
  3551.  
  3552.         prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
  3553.         if (!prop_enum)
  3554.                 return -ENOMEM;
  3555.  
  3556.         strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
  3557.         prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
  3558.         prop_enum->value = value;
  3559.  
  3560.         property->values[index] = value;
  3561.         list_add_tail(&prop_enum->head, &property->enum_blob_list);
  3562.         return 0;
  3563. }
  3564. EXPORT_SYMBOL(drm_property_add_enum);
  3565.  
  3566. /**
  3567.  * drm_property_destroy - destroy a drm property
  3568.  * @dev: drm device
  3569.  * @property: property to destry
  3570.  *
  3571.  * This function frees a property including any attached resources like
  3572.  * enumeration values.
  3573.  */
  3574. void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
  3575. {
  3576.         struct drm_property_enum *prop_enum, *pt;
  3577.  
  3578.         list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
  3579.                 list_del(&prop_enum->head);
  3580.                 kfree(prop_enum);
  3581.         }
  3582.  
  3583.         if (property->num_values)
  3584.                 kfree(property->values);
  3585.         drm_mode_object_put(dev, &property->base);
  3586.         list_del(&property->head);
  3587.         kfree(property);
  3588. }
  3589. EXPORT_SYMBOL(drm_property_destroy);
  3590.  
  3591. /**
  3592.  * drm_object_attach_property - attach a property to a modeset object
  3593.  * @obj: drm modeset object
  3594.  * @property: property to attach
  3595.  * @init_val: initial value of the property
  3596.  *
  3597.  * This attaches the given property to the modeset object with the given initial
  3598.  * value. Currently this function cannot fail since the properties are stored in
  3599.  * a statically sized array.
  3600.  */
  3601. void drm_object_attach_property(struct drm_mode_object *obj,
  3602.                                 struct drm_property *property,
  3603.                                 uint64_t init_val)
  3604. {
  3605.         int count = obj->properties->count;
  3606.  
  3607.         if (count == DRM_OBJECT_MAX_PROPERTY) {
  3608.                 WARN(1, "Failed to attach object property (type: 0x%x). Please "
  3609.                         "increase DRM_OBJECT_MAX_PROPERTY by 1 for each time "
  3610.                         "you see this message on the same object type.\n",
  3611.                         obj->type);
  3612.                 return;
  3613.         }
  3614.  
  3615.         obj->properties->ids[count] = property->base.id;
  3616.         obj->properties->values[count] = init_val;
  3617.         obj->properties->count++;
  3618. }
  3619. EXPORT_SYMBOL(drm_object_attach_property);
  3620.  
  3621. /**
  3622.  * drm_object_property_set_value - set the value of a property
  3623.  * @obj: drm mode object to set property value for
  3624.  * @property: property to set
  3625.  * @val: value the property should be set to
  3626.  *
  3627.  * This functions sets a given property on a given object. This function only
  3628.  * changes the software state of the property, it does not call into the
  3629.  * driver's ->set_property callback.
  3630.  *
  3631.  * Returns:
  3632.  * Zero on success, error code on failure.
  3633.  */
  3634. int drm_object_property_set_value(struct drm_mode_object *obj,
  3635.                                   struct drm_property *property, uint64_t val)
  3636. {
  3637.         int i;
  3638.  
  3639.         for (i = 0; i < obj->properties->count; i++) {
  3640.                 if (obj->properties->ids[i] == property->base.id) {
  3641.                         obj->properties->values[i] = val;
  3642.                         return 0;
  3643.                 }
  3644.         }
  3645.  
  3646.                 return -EINVAL;
  3647. }
  3648. EXPORT_SYMBOL(drm_object_property_set_value);
  3649.  
  3650. /**
  3651.  * drm_object_property_get_value - retrieve the value of a property
  3652.  * @obj: drm mode object to get property value from
  3653.  * @property: property to retrieve
  3654.  * @val: storage for the property value
  3655.  *
  3656.  * This function retrieves the softare state of the given property for the given
  3657.  * property. Since there is no driver callback to retrieve the current property
  3658.  * value this might be out of sync with the hardware, depending upon the driver
  3659.  * and property.
  3660.  *
  3661.  * Returns:
  3662.  * Zero on success, error code on failure.
  3663.  */
  3664. int drm_object_property_get_value(struct drm_mode_object *obj,
  3665.                                   struct drm_property *property, uint64_t *val)
  3666. {
  3667.         int i;
  3668.  
  3669.         for (i = 0; i < obj->properties->count; i++) {
  3670.                 if (obj->properties->ids[i] == property->base.id) {
  3671.                         *val = obj->properties->values[i];
  3672.                         return 0;
  3673.                 }
  3674.         }
  3675.  
  3676.                 return -EINVAL;
  3677. }
  3678. EXPORT_SYMBOL(drm_object_property_get_value);
  3679.  
  3680. #if 0
  3681. /**
  3682.  * drm_mode_getproperty_ioctl - get the current value of a connector's property
  3683.  * @dev: DRM device
  3684.  * @data: ioctl data
  3685.  * @file_priv: DRM file info
  3686.  *
  3687.  * This function retrieves the current value for an connectors's property.
  3688.  *
  3689.  * Called by the user via ioctl.
  3690.  *
  3691.  * Returns:
  3692.  * Zero on success, errno on failure.
  3693.  */
  3694. int drm_mode_getproperty_ioctl(struct drm_device *dev,
  3695.                                void *data, struct drm_file *file_priv)
  3696. {
  3697.         struct drm_mode_get_property *out_resp = data;
  3698.         struct drm_property *property;
  3699.         int enum_count = 0;
  3700.         int blob_count = 0;
  3701.         int value_count = 0;
  3702.         int ret = 0, i;
  3703.         int copied;
  3704.         struct drm_property_enum *prop_enum;
  3705.         struct drm_mode_property_enum __user *enum_ptr;
  3706.         struct drm_property_blob *prop_blob;
  3707.         uint32_t __user *blob_id_ptr;
  3708.         uint64_t __user *values_ptr;
  3709.         uint32_t __user *blob_length_ptr;
  3710.  
  3711.         if (!drm_core_check_feature(dev, DRIVER_MODESET))
  3712.                 return -EINVAL;
  3713.  
  3714.         drm_modeset_lock_all(dev);
  3715.         property = drm_property_find(dev, out_resp->prop_id);
  3716.         if (!property) {
  3717.                 ret = -ENOENT;
  3718.                 goto done;
  3719.         }
  3720.  
  3721.         if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
  3722.                         drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
  3723.                 list_for_each_entry(prop_enum, &property->enum_blob_list, head)
  3724.                         enum_count++;
  3725.         } else if (drm_property_type_is(property, DRM_MODE_PROP_BLOB)) {
  3726.                 list_for_each_entry(prop_blob, &property->enum_blob_list, head)
  3727.                         blob_count++;
  3728.         }
  3729.  
  3730.         value_count = property->num_values;
  3731.  
  3732.         strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
  3733.         out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
  3734.         out_resp->flags = property->flags;
  3735.  
  3736.         if ((out_resp->count_values >= value_count) && value_count) {
  3737.                 values_ptr = (uint64_t __user *)(unsigned long)out_resp->values_ptr;
  3738.                 for (i = 0; i < value_count; i++) {
  3739.                         if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
  3740.                                 ret = -EFAULT;
  3741.                                 goto done;
  3742.                         }
  3743.                 }
  3744.         }
  3745.         out_resp->count_values = value_count;
  3746.  
  3747.         if (drm_property_type_is(property, DRM_MODE_PROP_ENUM) ||
  3748.                         drm_property_type_is(property, DRM_MODE_PROP_BITMASK)) {
  3749.                 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
  3750.                         copied = 0;
  3751.                         enum_ptr = (struct drm_mode_property_enum __user *)(unsigned long)out_resp->enum_blob_ptr;
  3752.                         list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
  3753.  
  3754.                                 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
  3755.                                         ret = -EFAULT;
  3756.                                         goto done;
  3757.                                 }
  3758.  
  3759.                                 if (copy_to_user(&enum_ptr[copied].name,
  3760.                                                  &prop_enum->name, DRM_PROP_NAME_LEN)) {
  3761.                                         ret = -EFAULT;
  3762.                                         goto done;
  3763.                                 }
  3764.                                 copied++;
  3765.                         }
  3766.                 }
  3767.                 out_resp->count_enum_blobs = enum_count;
  3768.         }
  3769.  
  3770.         if (drm_property_type_is(property, DRM_MODE_PROP_BLOB)) {
  3771.                 if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
  3772.                         copied = 0;
  3773.                         blob_id_ptr = (uint32_t __user *)(unsigned long)out_resp->enum_blob_ptr;
  3774.                         blob_length_ptr = (uint32_t __user *)(unsigned long)out_resp->values_ptr;
  3775.  
  3776.                         list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
  3777.                                 if (put_user(prop_blob->base.id, blob_id_ptr + copied)) {
  3778.                                         ret = -EFAULT;
  3779.                                         goto done;
  3780.                                 }
  3781.  
  3782.                                 if (put_user(prop_blob->length, blob_length_ptr + copied)) {
  3783.                                         ret = -EFAULT;
  3784.                                         goto done;
  3785.                                 }
  3786.  
  3787.                                 copied++;
  3788.                         }
  3789.                 }
  3790.                 out_resp->count_enum_blobs = blob_count;
  3791.         }
  3792. done:
  3793.         drm_modeset_unlock_all(dev);
  3794.         return ret;
  3795. }
  3796. #endif
  3797.  
  3798. static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
  3799.                                                           void *data)
  3800. {
  3801.         struct drm_property_blob *blob;
  3802.         int ret;
  3803.  
  3804.         if (!length || !data)
  3805.                 return NULL;
  3806.  
  3807.         blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
  3808.         if (!blob)
  3809.                 return NULL;
  3810.  
  3811.         ret = drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
  3812.         if (ret) {
  3813.                 kfree(blob);
  3814.                 return NULL;
  3815.         }
  3816.  
  3817.         blob->length = length;
  3818.  
  3819.         memcpy(blob->data, data, length);
  3820.  
  3821.         list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
  3822.         return blob;
  3823. }
  3824.  
  3825. static void drm_property_destroy_blob(struct drm_device *dev,
  3826.                                struct drm_property_blob *blob)
  3827. {
  3828.         drm_mode_object_put(dev, &blob->base);
  3829.         list_del(&blob->head);
  3830.         kfree(blob);
  3831. }
  3832.  
  3833. #if 0
  3834. /**
  3835.  * drm_mode_getblob_ioctl - get the contents of a blob property value
  3836.  * @dev: DRM device
  3837.  * @data: ioctl data
  3838.  * @file_priv: DRM file info
  3839.  *
  3840.  * This function retrieves the contents of a blob property. The value stored in
  3841.  * an object's blob property is just a normal modeset object id.
  3842.  *
  3843.  * Called by the user via ioctl.
  3844.  *
  3845.  * Returns:
  3846.  * Zero on success, errno on failure.
  3847.  */
  3848. int drm_mode_getblob_ioctl(struct drm_device *dev,
  3849.                            void *data, struct drm_file *file_priv)
  3850. {
  3851.         struct drm_mode_get_blob *out_resp = data;
  3852.         struct drm_property_blob *blob;
  3853.         int ret = 0;
  3854.         void __user *blob_ptr;
  3855.  
  3856.         if (!drm_core_check_feature(dev, DRIVER_MODESET))
  3857.                 return -EINVAL;
  3858.  
  3859.         drm_modeset_lock_all(dev);
  3860.         blob = drm_property_blob_find(dev, out_resp->blob_id);
  3861.         if (!blob) {
  3862.                 ret = -ENOENT;
  3863.                 goto done;
  3864.         }
  3865.  
  3866.         if (out_resp->length == blob->length) {
  3867.                 blob_ptr = (void __user *)(unsigned long)out_resp->data;
  3868.                 if (copy_to_user(blob_ptr, blob->data, blob->length)){
  3869.                         ret = -EFAULT;
  3870.                         goto done;
  3871.                 }
  3872.         }
  3873.         out_resp->length = blob->length;
  3874.  
  3875. done:
  3876.         drm_modeset_unlock_all(dev);
  3877.         return ret;
  3878. }
  3879. #endif
  3880.  
  3881. int drm_mode_connector_set_path_property(struct drm_connector *connector,
  3882.                                          char *path)
  3883. {
  3884.         struct drm_device *dev = connector->dev;
  3885.         int ret, size;
  3886.         size = strlen(path) + 1;
  3887.  
  3888.         connector->path_blob_ptr = drm_property_create_blob(connector->dev,
  3889.                                                             size, path);
  3890.         if (!connector->path_blob_ptr)
  3891.                 return -EINVAL;
  3892.  
  3893.         ret = drm_object_property_set_value(&connector->base,
  3894.                                             dev->mode_config.path_property,
  3895.                                             connector->path_blob_ptr->base.id);
  3896.         return ret;
  3897. }
  3898. EXPORT_SYMBOL(drm_mode_connector_set_path_property);
  3899.  
  3900. /**
  3901.  * drm_mode_connector_update_edid_property - update the edid property of a connector
  3902.  * @connector: drm connector
  3903.  * @edid: new value of the edid property
  3904.  *
  3905.  * This function creates a new blob modeset object and assigns its id to the
  3906.  * connector's edid property.
  3907.  *
  3908.  * Returns:
  3909.  * Zero on success, errno on failure.
  3910.  */
  3911. int drm_mode_connector_update_edid_property(struct drm_connector *connector,
  3912.                                             struct edid *edid)
  3913. {
  3914.         struct drm_device *dev = connector->dev;
  3915.         int ret, size;
  3916.  
  3917.         /* ignore requests to set edid when overridden */
  3918.         if (connector->override_edid)
  3919.                 return 0;
  3920.  
  3921.         if (connector->edid_blob_ptr)
  3922.                 drm_property_destroy_blob(dev, connector->edid_blob_ptr);
  3923.  
  3924.         /* Delete edid, when there is none. */
  3925.         if (!edid) {
  3926.                 connector->edid_blob_ptr = NULL;
  3927.                 ret = drm_object_property_set_value(&connector->base, dev->mode_config.edid_property, 0);
  3928.                 return ret;
  3929.         }
  3930.  
  3931.         size = EDID_LENGTH * (1 + edid->extensions);
  3932.         connector->edid_blob_ptr = drm_property_create_blob(connector->dev,
  3933.                                                             size, edid);
  3934.         if (!connector->edid_blob_ptr)
  3935.                 return -EINVAL;
  3936.  
  3937.         ret = drm_object_property_set_value(&connector->base,
  3938.                                                dev->mode_config.edid_property,
  3939.                                                connector->edid_blob_ptr->base.id);
  3940.  
  3941.         return ret;
  3942. }
  3943. EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
  3944.  
  3945. #if 0
  3946.  
  3947. static int drm_mode_connector_set_obj_prop(struct drm_mode_object *obj,
  3948.                                            struct drm_property *property,
  3949.                                            uint64_t value)
  3950. {
  3951.         int ret = -EINVAL;
  3952.         struct drm_connector *connector = obj_to_connector(obj);
  3953.  
  3954.         /* Do DPMS ourselves */
  3955.         if (property == connector->dev->mode_config.dpms_property) {
  3956.                 if (connector->funcs->dpms)
  3957.                         (*connector->funcs->dpms)(connector, (int)value);
  3958.                 ret = 0;
  3959.         } else if (connector->funcs->set_property)
  3960.                 ret = connector->funcs->set_property(connector, property, value);
  3961.  
  3962.         /* store the property value if successful */
  3963.         if (!ret)
  3964.                 drm_object_property_set_value(&connector->base, property, value);
  3965.         return ret;
  3966. }
  3967.  
  3968. static int drm_mode_crtc_set_obj_prop(struct drm_mode_object *obj,
  3969.                                       struct drm_property *property,
  3970.                                       uint64_t value)
  3971. {
  3972.         int ret = -EINVAL;
  3973.         struct drm_crtc *crtc = obj_to_crtc(obj);
  3974.  
  3975.         if (crtc->funcs->set_property)
  3976.                 ret = crtc->funcs->set_property(crtc, property, value);
  3977.         if (!ret)
  3978.                 drm_object_property_set_value(obj, property, value);
  3979.  
  3980.         return ret;
  3981. }
  3982.  
  3983. static int drm_mode_plane_set_obj_prop(struct drm_mode_object *obj,
  3984.                                       struct drm_property *property,
  3985.                                       uint64_t value)
  3986. {
  3987.         int ret = -EINVAL;
  3988.         struct drm_plane *plane = obj_to_plane(obj);
  3989.  
  3990.         if (plane->funcs->set_property)
  3991.                 ret = plane->funcs->set_property(plane, property, value);
  3992.         if (!ret)
  3993.                 drm_object_property_set_value(obj, property, value);
  3994.  
  3995.         return ret;
  3996. }
  3997.  
  3998. /**
  3999.  * drm_mode_getproperty_ioctl - get the current value of a object's property
  4000.  * @dev: DRM device
  4001.  * @data: ioctl data
  4002.  * @file_priv: DRM file info
  4003.  *
  4004.  * This function retrieves the current value for an object's property. Compared
  4005.  * to the connector specific ioctl this one is extended to also work on crtc and
  4006.  * plane objects.
  4007.  *
  4008.  * Called by the user via ioctl.
  4009.  *
  4010.  * Returns:
  4011.  * Zero on success, errno on failure.
  4012.  */
  4013. int drm_mode_obj_get_properties_ioctl(struct drm_device *dev, void *data,
  4014.                                       struct drm_file *file_priv)
  4015. {
  4016.         struct drm_mode_obj_get_properties *arg = data;
  4017.         struct drm_mode_object *obj;
  4018.         int ret = 0;
  4019.         int i;
  4020.         int copied = 0;
  4021.         int props_count = 0;
  4022.         uint32_t __user *props_ptr;
  4023.         uint64_t __user *prop_values_ptr;
  4024.  
  4025.         if (!drm_core_check_feature(dev, DRIVER_MODESET))
  4026.                 return -EINVAL;
  4027.  
  4028.         drm_modeset_lock_all(dev);
  4029.  
  4030.         obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
  4031.         if (!obj) {
  4032.                 ret = -ENOENT;
  4033.                 goto out;
  4034.         }
  4035.         if (!obj->properties) {
  4036.                 ret = -EINVAL;
  4037.                 goto out;
  4038.         }
  4039.  
  4040.         props_count = obj->properties->count;
  4041.  
  4042.         /* This ioctl is called twice, once to determine how much space is
  4043.          * needed, and the 2nd time to fill it. */
  4044.         if ((arg->count_props >= props_count) && props_count) {
  4045.                 copied = 0;
  4046.                 props_ptr = (uint32_t __user *)(unsigned long)(arg->props_ptr);
  4047.                 prop_values_ptr = (uint64_t __user *)(unsigned long)
  4048.                                   (arg->prop_values_ptr);
  4049.                 for (i = 0; i < props_count; i++) {
  4050.                         if (put_user(obj->properties->ids[i],
  4051.                                      props_ptr + copied)) {
  4052.                                 ret = -EFAULT;
  4053.                                 goto out;
  4054.         }
  4055.                         if (put_user(obj->properties->values[i],
  4056.                                      prop_values_ptr + copied)) {
  4057.                                 ret = -EFAULT;
  4058.                 goto out;
  4059.         }
  4060.                         copied++;
  4061.                 }
  4062.         }
  4063.         arg->count_props = props_count;
  4064. out:
  4065.         drm_modeset_unlock_all(dev);
  4066.         return ret;
  4067. }
  4068.  
  4069. /**
  4070.  * drm_mode_obj_set_property_ioctl - set the current value of an object's property
  4071.  * @dev: DRM device
  4072.  * @data: ioctl data
  4073.  * @file_priv: DRM file info
  4074.  *
  4075.  * This function sets the current value for an object's property. It also calls
  4076.  * into a driver's ->set_property callback to update the hardware state.
  4077.  * Compared to the connector specific ioctl this one is extended to also work on
  4078.  * crtc and plane objects.
  4079.  *
  4080.  * Called by the user via ioctl.
  4081.  *
  4082.  * Returns:
  4083.  * Zero on success, errno on failure.
  4084.  */
  4085. int drm_mode_obj_set_property_ioctl(struct drm_device *dev, void *data,
  4086.                                     struct drm_file *file_priv)
  4087. {
  4088.         struct drm_mode_obj_set_property *arg = data;
  4089.         struct drm_mode_object *arg_obj;
  4090.         struct drm_mode_object *prop_obj;
  4091.         struct drm_property *property;
  4092.         int ret = -EINVAL;
  4093.         int i;
  4094.  
  4095.         if (!drm_core_check_feature(dev, DRIVER_MODESET))
  4096.                 return -EINVAL;
  4097.  
  4098.         drm_modeset_lock_all(dev);
  4099.  
  4100.         arg_obj = drm_mode_object_find(dev, arg->obj_id, arg->obj_type);
  4101.         if (!arg_obj) {
  4102.                 ret = -ENOENT;
  4103.                 goto out;
  4104.         }
  4105.         if (!arg_obj->properties)
  4106.                 goto out;
  4107.  
  4108.         for (i = 0; i < arg_obj->properties->count; i++)
  4109.                 if (arg_obj->properties->ids[i] == arg->prop_id)
  4110.                         break;
  4111.  
  4112.         if (i == arg_obj->properties->count)
  4113.                 goto out;
  4114.  
  4115.         prop_obj = drm_mode_object_find(dev, arg->prop_id,
  4116.                                         DRM_MODE_OBJECT_PROPERTY);
  4117.         if (!prop_obj) {
  4118.                 ret = -ENOENT;
  4119.                         goto out;
  4120.         }
  4121.         property = obj_to_property(prop_obj);
  4122.  
  4123.         if (!drm_property_change_is_valid(property, arg->value))
  4124.                         goto out;
  4125.  
  4126.         switch (arg_obj->type) {
  4127.         case DRM_MODE_OBJECT_CONNECTOR:
  4128.                 ret = drm_mode_connector_set_obj_prop(arg_obj, property,
  4129.                                                       arg->value);
  4130.                 break;
  4131.         case DRM_MODE_OBJECT_CRTC:
  4132.                 ret = drm_mode_crtc_set_obj_prop(arg_obj, property, arg->value);
  4133.                 break;
  4134.         case DRM_MODE_OBJECT_PLANE:
  4135.                 ret = drm_mode_plane_set_obj_prop(arg_obj, property, arg->value);
  4136.                                 break;
  4137.                         }
  4138.  
  4139. out:
  4140.         drm_modeset_unlock_all(dev);
  4141.         return ret;
  4142. }
  4143. #endif
  4144.  
  4145. /**
  4146.  * drm_mode_connector_attach_encoder - attach a connector to an encoder
  4147.  * @connector: connector to attach
  4148.  * @encoder: encoder to attach @connector to
  4149.  *
  4150.  * This function links up a connector to an encoder. Note that the routing
  4151.  * restrictions between encoders and crtcs are exposed to userspace through the
  4152.  * possible_clones and possible_crtcs bitmasks.
  4153.  *
  4154.  * Returns:
  4155.  * Zero on success, errno on failure.
  4156.  */
  4157. int drm_mode_connector_attach_encoder(struct drm_connector *connector,
  4158.                                       struct drm_encoder *encoder)
  4159. {
  4160.         int i;
  4161.  
  4162.         for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
  4163.                 if (connector->encoder_ids[i] == 0) {
  4164.                         connector->encoder_ids[i] = encoder->base.id;
  4165.                         return 0;
  4166.                 }
  4167.         }
  4168.         return -ENOMEM;
  4169. }
  4170. EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
  4171.  
  4172. /**
  4173.  * drm_mode_crtc_set_gamma_size - set the gamma table size
  4174.  * @crtc: CRTC to set the gamma table size for
  4175.  * @gamma_size: size of the gamma table
  4176.  *
  4177.  * Drivers which support gamma tables should set this to the supported gamma
  4178.  * table size when initializing the CRTC. Currently the drm core only supports a
  4179.  * fixed gamma table size.
  4180.  *
  4181.  * Returns:
  4182.  * Zero on success, errno on failure.
  4183.  */
  4184. int drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
  4185.                                   int gamma_size)
  4186. {
  4187.         crtc->gamma_size = gamma_size;
  4188.  
  4189.         crtc->gamma_store = kzalloc(gamma_size * sizeof(uint16_t) * 3, GFP_KERNEL);
  4190.         if (!crtc->gamma_store) {
  4191.                 crtc->gamma_size = 0;
  4192.                 return -ENOMEM;
  4193.         }
  4194.  
  4195.         return 0;
  4196. }
  4197. EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
  4198.  
  4199. #if 0
  4200. /**
  4201.  * drm_mode_gamma_set_ioctl - set the gamma table
  4202.  * @dev: DRM device
  4203.  * @data: ioctl data
  4204.  * @file_priv: DRM file info
  4205.  *
  4206.  * Set the gamma table of a CRTC to the one passed in by the user. Userspace can
  4207.  * inquire the required gamma table size through drm_mode_gamma_get_ioctl.
  4208.  *
  4209.  * Called by the user via ioctl.
  4210.  *
  4211.  * Returns:
  4212.  * Zero on success, errno on failure.
  4213.  */
  4214. int drm_mode_gamma_set_ioctl(struct drm_device *dev,
  4215.                              void *data, struct drm_file *file_priv)
  4216. {
  4217.         struct drm_mode_crtc_lut *crtc_lut = data;
  4218.         struct drm_crtc *crtc;
  4219.         void *r_base, *g_base, *b_base;
  4220.         int size;
  4221.         int ret = 0;
  4222.  
  4223.         if (!drm_core_check_feature(dev, DRIVER_MODESET))
  4224.                 return -EINVAL;
  4225.  
  4226.         drm_modeset_lock_all(dev);
  4227.         crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
  4228.         if (!crtc) {
  4229.                 ret = -ENOENT;
  4230.                 goto out;
  4231.         }
  4232.  
  4233.         if (crtc->funcs->gamma_set == NULL) {
  4234.                 ret = -ENOSYS;
  4235.                 goto out;
  4236.         }
  4237.  
  4238.         /* memcpy into gamma store */
  4239.         if (crtc_lut->gamma_size != crtc->gamma_size) {
  4240.                 ret = -EINVAL;
  4241.                 goto out;
  4242.         }
  4243.  
  4244.         size = crtc_lut->gamma_size * (sizeof(uint16_t));
  4245.         r_base = crtc->gamma_store;
  4246.         if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
  4247.                 ret = -EFAULT;
  4248.                 goto out;
  4249.         }
  4250.  
  4251.         g_base = r_base + size;
  4252.         if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
  4253.                 ret = -EFAULT;
  4254.                 goto out;
  4255.         }
  4256.  
  4257.         b_base = g_base + size;
  4258.         if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
  4259.                 ret = -EFAULT;
  4260.                 goto out;
  4261.         }
  4262.  
  4263.         crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
  4264.  
  4265. out:
  4266.         drm_modeset_unlock_all(dev);
  4267.         return ret;
  4268.  
  4269. }
  4270.  
  4271. /**
  4272.  * drm_mode_gamma_get_ioctl - get the gamma table
  4273.  * @dev: DRM device
  4274.  * @data: ioctl data
  4275.  * @file_priv: DRM file info
  4276.  *
  4277.  * Copy the current gamma table into the storage provided. This also provides
  4278.  * the gamma table size the driver expects, which can be used to size the
  4279.  * allocated storage.
  4280.  *
  4281.  * Called by the user via ioctl.
  4282.  *
  4283.  * Returns:
  4284.  * Zero on success, errno on failure.
  4285.  */
  4286. int drm_mode_gamma_get_ioctl(struct drm_device *dev,
  4287.                              void *data, struct drm_file *file_priv)
  4288. {
  4289.         struct drm_mode_crtc_lut *crtc_lut = data;
  4290.         struct drm_crtc *crtc;
  4291.         void *r_base, *g_base, *b_base;
  4292.         int size;
  4293.         int ret = 0;
  4294.  
  4295.         if (!drm_core_check_feature(dev, DRIVER_MODESET))
  4296.                 return -EINVAL;
  4297.  
  4298.         drm_modeset_lock_all(dev);
  4299.         crtc = drm_crtc_find(dev, crtc_lut->crtc_id);
  4300.         if (!crtc) {
  4301.                 ret = -ENOENT;
  4302.                 goto out;
  4303.         }
  4304.  
  4305.         /* memcpy into gamma store */
  4306.         if (crtc_lut->gamma_size != crtc->gamma_size) {
  4307.                 ret = -EINVAL;
  4308.                 goto out;
  4309.         }
  4310.  
  4311.         size = crtc_lut->gamma_size * (sizeof(uint16_t));
  4312.         r_base = crtc->gamma_store;
  4313.         if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
  4314.                 ret = -EFAULT;
  4315.                 goto out;
  4316.         }
  4317.  
  4318.         g_base = r_base + size;
  4319.         if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
  4320.                 ret = -EFAULT;
  4321.                 goto out;
  4322.         }
  4323.  
  4324.         b_base = g_base + size;
  4325.         if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
  4326.                 ret = -EFAULT;
  4327.                 goto out;
  4328.         }
  4329. out:
  4330.         drm_modeset_unlock_all(dev);
  4331.         return ret;
  4332. }
  4333.  
  4334. #endif
  4335.  
  4336.  
  4337. /**
  4338.  * drm_mode_config_reset - call ->reset callbacks
  4339.  * @dev: drm device
  4340.  *
  4341.  * This functions calls all the crtc's, encoder's and connector's ->reset
  4342.  * callback. Drivers can use this in e.g. their driver load or resume code to
  4343.  * reset hardware and software state.
  4344.  */
  4345. void drm_mode_config_reset(struct drm_device *dev)
  4346. {
  4347.         struct drm_crtc *crtc;
  4348.         struct drm_encoder *encoder;
  4349.         struct drm_connector *connector;
  4350.  
  4351.         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
  4352.                 if (crtc->funcs->reset)
  4353.                         crtc->funcs->reset(crtc);
  4354.  
  4355.         list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
  4356.                 if (encoder->funcs->reset)
  4357.                         encoder->funcs->reset(encoder);
  4358.  
  4359.         list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  4360.                 connector->status = connector_status_unknown;
  4361.  
  4362.                 if (connector->funcs->reset)
  4363.                         connector->funcs->reset(connector);
  4364.         }
  4365. }
  4366. EXPORT_SYMBOL(drm_mode_config_reset);
  4367. /*
  4368.  * Just need to support RGB formats here for compat with code that doesn't
  4369.  * use pixel formats directly yet.
  4370.  */
  4371. void drm_fb_get_bpp_depth(uint32_t format, unsigned int *depth,
  4372.                           int *bpp)
  4373. {
  4374.         switch (format) {
  4375.         case DRM_FORMAT_C8:
  4376.         case DRM_FORMAT_RGB332:
  4377.         case DRM_FORMAT_BGR233:
  4378.                 *depth = 8;
  4379.                 *bpp = 8;
  4380.                 break;
  4381.         case DRM_FORMAT_XRGB1555:
  4382.         case DRM_FORMAT_XBGR1555:
  4383.         case DRM_FORMAT_RGBX5551:
  4384.         case DRM_FORMAT_BGRX5551:
  4385.         case DRM_FORMAT_ARGB1555:
  4386.         case DRM_FORMAT_ABGR1555:
  4387.         case DRM_FORMAT_RGBA5551:
  4388.         case DRM_FORMAT_BGRA5551:
  4389.                 *depth = 15;
  4390.                 *bpp = 16;
  4391.                 break;
  4392.         case DRM_FORMAT_RGB565:
  4393.         case DRM_FORMAT_BGR565:
  4394.                 *depth = 16;
  4395.                 *bpp = 16;
  4396.                 break;
  4397.         case DRM_FORMAT_RGB888:
  4398.         case DRM_FORMAT_BGR888:
  4399.                 *depth = 24;
  4400.                 *bpp = 24;
  4401.                 break;
  4402.         case DRM_FORMAT_XRGB8888:
  4403.         case DRM_FORMAT_XBGR8888:
  4404.         case DRM_FORMAT_RGBX8888:
  4405.         case DRM_FORMAT_BGRX8888:
  4406.                 *depth = 24;
  4407.                 *bpp = 32;
  4408.                 break;
  4409.         case DRM_FORMAT_XRGB2101010:
  4410.         case DRM_FORMAT_XBGR2101010:
  4411.         case DRM_FORMAT_RGBX1010102:
  4412.         case DRM_FORMAT_BGRX1010102:
  4413.         case DRM_FORMAT_ARGB2101010:
  4414.         case DRM_FORMAT_ABGR2101010:
  4415.         case DRM_FORMAT_RGBA1010102:
  4416.         case DRM_FORMAT_BGRA1010102:
  4417.                 *depth = 30;
  4418.                 *bpp = 32;
  4419.                 break;
  4420.         case DRM_FORMAT_ARGB8888:
  4421.         case DRM_FORMAT_ABGR8888:
  4422.         case DRM_FORMAT_RGBA8888:
  4423.         case DRM_FORMAT_BGRA8888:
  4424.                 *depth = 32;
  4425.                 *bpp = 32;
  4426.                 break;
  4427.         default:
  4428.                 DRM_DEBUG_KMS("unsupported pixel format %s\n",
  4429.                               drm_get_format_name(format));
  4430.                 *depth = 0;
  4431.                 *bpp = 0;
  4432.                 break;
  4433.         }
  4434. }
  4435. EXPORT_SYMBOL(drm_fb_get_bpp_depth);
  4436.  
  4437. /**
  4438.  * drm_format_num_planes - get the number of planes for format
  4439.  * @format: pixel format (DRM_FORMAT_*)
  4440.  *
  4441.  * Returns:
  4442.  * The number of planes used by the specified pixel format.
  4443.  */
  4444. int drm_format_num_planes(uint32_t format)
  4445. {
  4446.         switch (format) {
  4447.         case DRM_FORMAT_YUV410:
  4448.         case DRM_FORMAT_YVU410:
  4449.         case DRM_FORMAT_YUV411:
  4450.         case DRM_FORMAT_YVU411:
  4451.         case DRM_FORMAT_YUV420:
  4452.         case DRM_FORMAT_YVU420:
  4453.         case DRM_FORMAT_YUV422:
  4454.         case DRM_FORMAT_YVU422:
  4455.         case DRM_FORMAT_YUV444:
  4456.         case DRM_FORMAT_YVU444:
  4457.                 return 3;
  4458.         case DRM_FORMAT_NV12:
  4459.         case DRM_FORMAT_NV21:
  4460.         case DRM_FORMAT_NV16:
  4461.         case DRM_FORMAT_NV61:
  4462.         case DRM_FORMAT_NV24:
  4463.         case DRM_FORMAT_NV42:
  4464.                 return 2;
  4465.         default:
  4466.                 return 1;
  4467.         }
  4468. }
  4469. EXPORT_SYMBOL(drm_format_num_planes);
  4470.  
  4471. /**
  4472.  * drm_format_plane_cpp - determine the bytes per pixel value
  4473.  * @format: pixel format (DRM_FORMAT_*)
  4474.  * @plane: plane index
  4475.  *
  4476.  * Returns:
  4477.  * The bytes per pixel value for the specified plane.
  4478.  */
  4479. int drm_format_plane_cpp(uint32_t format, int plane)
  4480. {
  4481.         unsigned int depth;
  4482.         int bpp;
  4483.  
  4484.         if (plane >= drm_format_num_planes(format))
  4485.                 return 0;
  4486.  
  4487.         switch (format) {
  4488.         case DRM_FORMAT_YUYV:
  4489.         case DRM_FORMAT_YVYU:
  4490.         case DRM_FORMAT_UYVY:
  4491.         case DRM_FORMAT_VYUY:
  4492.                 return 2;
  4493.         case DRM_FORMAT_NV12:
  4494.         case DRM_FORMAT_NV21:
  4495.         case DRM_FORMAT_NV16:
  4496.         case DRM_FORMAT_NV61:
  4497.         case DRM_FORMAT_NV24:
  4498.         case DRM_FORMAT_NV42:
  4499.                 return plane ? 2 : 1;
  4500.         case DRM_FORMAT_YUV410:
  4501.         case DRM_FORMAT_YVU410:
  4502.         case DRM_FORMAT_YUV411:
  4503.         case DRM_FORMAT_YVU411:
  4504.         case DRM_FORMAT_YUV420:
  4505.         case DRM_FORMAT_YVU420:
  4506.         case DRM_FORMAT_YUV422:
  4507.         case DRM_FORMAT_YVU422:
  4508.         case DRM_FORMAT_YUV444:
  4509.         case DRM_FORMAT_YVU444:
  4510.                 return 1;
  4511.         default:
  4512.                 drm_fb_get_bpp_depth(format, &depth, &bpp);
  4513.                 return bpp >> 3;
  4514.         }
  4515. }
  4516. EXPORT_SYMBOL(drm_format_plane_cpp);
  4517.  
  4518. /**
  4519.  * drm_format_horz_chroma_subsampling - get the horizontal chroma subsampling factor
  4520.  * @format: pixel format (DRM_FORMAT_*)
  4521.  *
  4522.  * Returns:
  4523.  * The horizontal chroma subsampling factor for the
  4524.  * specified pixel format.
  4525.  */
  4526. int drm_format_horz_chroma_subsampling(uint32_t format)
  4527. {
  4528.         switch (format) {
  4529.         case DRM_FORMAT_YUV411:
  4530.         case DRM_FORMAT_YVU411:
  4531.         case DRM_FORMAT_YUV410:
  4532.         case DRM_FORMAT_YVU410:
  4533.                 return 4;
  4534.         case DRM_FORMAT_YUYV:
  4535.         case DRM_FORMAT_YVYU:
  4536.         case DRM_FORMAT_UYVY:
  4537.         case DRM_FORMAT_VYUY:
  4538.         case DRM_FORMAT_NV12:
  4539.         case DRM_FORMAT_NV21:
  4540.         case DRM_FORMAT_NV16:
  4541.         case DRM_FORMAT_NV61:
  4542.         case DRM_FORMAT_YUV422:
  4543.         case DRM_FORMAT_YVU422:
  4544.         case DRM_FORMAT_YUV420:
  4545.         case DRM_FORMAT_YVU420:
  4546.                 return 2;
  4547.         default:
  4548.                 return 1;
  4549.         }
  4550. }
  4551. EXPORT_SYMBOL(drm_format_horz_chroma_subsampling);
  4552.  
  4553. /**
  4554.  * drm_format_vert_chroma_subsampling - get the vertical chroma subsampling factor
  4555.  * @format: pixel format (DRM_FORMAT_*)
  4556.  *
  4557.  * Returns:
  4558.  * The vertical chroma subsampling factor for the
  4559.  * specified pixel format.
  4560.  */
  4561. int drm_format_vert_chroma_subsampling(uint32_t format)
  4562. {
  4563.         switch (format) {
  4564.         case DRM_FORMAT_YUV410:
  4565.         case DRM_FORMAT_YVU410:
  4566.                 return 4;
  4567.         case DRM_FORMAT_YUV420:
  4568.         case DRM_FORMAT_YVU420:
  4569.         case DRM_FORMAT_NV12:
  4570.         case DRM_FORMAT_NV21:
  4571.                 return 2;
  4572.         default:
  4573.                 return 1;
  4574.         }
  4575. }
  4576. EXPORT_SYMBOL(drm_format_vert_chroma_subsampling);
  4577.  
  4578. /**
  4579.  * drm_mode_config_init - initialize DRM mode_configuration structure
  4580.  * @dev: DRM device
  4581.  *
  4582.  * Initialize @dev's mode_config structure, used for tracking the graphics
  4583.  * configuration of @dev.
  4584.  *
  4585.  * Since this initializes the modeset locks, no locking is possible. Which is no
  4586.  * problem, since this should happen single threaded at init time. It is the
  4587.  * driver's problem to ensure this guarantee.
  4588.  *
  4589.  */
  4590. void drm_mode_config_init(struct drm_device *dev)
  4591. {
  4592.         mutex_init(&dev->mode_config.mutex);
  4593.         drm_modeset_lock_init(&dev->mode_config.connection_mutex);
  4594.         mutex_init(&dev->mode_config.idr_mutex);
  4595.         mutex_init(&dev->mode_config.fb_lock);
  4596.         INIT_LIST_HEAD(&dev->mode_config.fb_list);
  4597.         INIT_LIST_HEAD(&dev->mode_config.crtc_list);
  4598.         INIT_LIST_HEAD(&dev->mode_config.connector_list);
  4599.         INIT_LIST_HEAD(&dev->mode_config.bridge_list);
  4600.         INIT_LIST_HEAD(&dev->mode_config.encoder_list);
  4601.         INIT_LIST_HEAD(&dev->mode_config.property_list);
  4602.         INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
  4603.         INIT_LIST_HEAD(&dev->mode_config.plane_list);
  4604.         idr_init(&dev->mode_config.crtc_idr);
  4605.  
  4606.         drm_modeset_lock_all(dev);
  4607.         drm_mode_create_standard_connector_properties(dev);
  4608.         drm_mode_create_standard_plane_properties(dev);
  4609.         drm_modeset_unlock_all(dev);
  4610.  
  4611.         /* Just to be sure */
  4612.         dev->mode_config.num_fb = 0;
  4613.         dev->mode_config.num_connector = 0;
  4614.         dev->mode_config.num_crtc = 0;
  4615.         dev->mode_config.num_encoder = 0;
  4616.         dev->mode_config.num_overlay_plane = 0;
  4617.         dev->mode_config.num_total_plane = 0;
  4618. }
  4619. EXPORT_SYMBOL(drm_mode_config_init);
  4620.  
  4621. /**
  4622.  * drm_mode_config_cleanup - free up DRM mode_config info
  4623.  * @dev: DRM device
  4624.  *
  4625.  * Free up all the connectors and CRTCs associated with this DRM device, then
  4626.  * free up the framebuffers and associated buffer objects.
  4627.  *
  4628.  * Note that since this /should/ happen single-threaded at driver/device
  4629.  * teardown time, no locking is required. It's the driver's job to ensure that
  4630.  * this guarantee actually holds true.
  4631.  *
  4632.  * FIXME: cleanup any dangling user buffer objects too
  4633.  */
  4634.