Subversion Repositories Kolibri OS

Rev

Rev 5271 | Rev 6084 | 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. #include "drm_internal.h"
  44.  
  45. static struct drm_framebuffer *add_framebuffer_internal(struct drm_device *dev,
  46.                                                         struct drm_mode_fb_cmd2 *r,
  47.                                                         struct drm_file *file_priv);
  48.  
  49. /* Avoid boilerplate.  I'm tired of typing. */
  50. #define DRM_ENUM_NAME_FN(fnname, list)                          \
  51.         const char *fnname(int val)                             \
  52.         {                                                       \
  53.                 int i;                                          \
  54.                 for (i = 0; i < ARRAY_SIZE(list); i++) {        \
  55.                         if (list[i].type == val)                \
  56.                                 return list[i].name;            \
  57.                 }                                               \
  58.                 return "(unknown)";                             \
  59.         }
  60.  
  61. /*
  62.  * Global properties
  63.  */
  64. static const struct drm_prop_enum_list drm_dpms_enum_list[] =
  65. {       { DRM_MODE_DPMS_ON, "On" },
  66.         { DRM_MODE_DPMS_STANDBY, "Standby" },
  67.         { DRM_MODE_DPMS_SUSPEND, "Suspend" },
  68.         { DRM_MODE_DPMS_OFF, "Off" }
  69. };
  70.  
  71. DRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list)
  72.  
  73. static const struct drm_prop_enum_list drm_plane_type_enum_list[] =
  74. {
  75.         { DRM_PLANE_TYPE_OVERLAY, "Overlay" },
  76.         { DRM_PLANE_TYPE_PRIMARY, "Primary" },
  77.         { DRM_PLANE_TYPE_CURSOR, "Cursor" },
  78. };
  79.  
  80. /*
  81.  * Optional properties
  82.  */
  83. static const struct drm_prop_enum_list drm_scaling_mode_enum_list[] =
  84. {
  85.         { DRM_MODE_SCALE_NONE, "None" },
  86.         { DRM_MODE_SCALE_FULLSCREEN, "Full" },
  87.         { DRM_MODE_SCALE_CENTER, "Center" },
  88.         { DRM_MODE_SCALE_ASPECT, "Full aspect" },
  89. };
  90.  
  91. static const struct drm_prop_enum_list drm_aspect_ratio_enum_list[] = {
  92.         { DRM_MODE_PICTURE_ASPECT_NONE, "Automatic" },
  93.         { DRM_MODE_PICTURE_ASPECT_4_3, "4:3" },
  94.         { DRM_MODE_PICTURE_ASPECT_16_9, "16:9" },
  95. };
  96.  
  97. /*
  98.  * Non-global properties, but "required" for certain connectors.
  99.  */
  100. static const struct drm_prop_enum_list drm_dvi_i_select_enum_list[] =
  101. {
  102.         { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
  103.         { DRM_MODE_SUBCONNECTOR_DVID,      "DVI-D"     }, /* DVI-I  */
  104.         { DRM_MODE_SUBCONNECTOR_DVIA,      "DVI-A"     }, /* DVI-I  */
  105. };
  106.  
  107. DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list)
  108.  
  109. static const struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list[] =
  110. {
  111.         { DRM_MODE_SUBCONNECTOR_Unknown,   "Unknown"   }, /* DVI-I and TV-out */
  112.         { DRM_MODE_SUBCONNECTOR_DVID,      "DVI-D"     }, /* DVI-I  */
  113.         { DRM_MODE_SUBCONNECTOR_DVIA,      "DVI-A"     }, /* DVI-I  */
  114. };
  115.  
  116. DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name,
  117.                  drm_dvi_i_subconnector_enum_list)
  118.  
  119. static const struct drm_prop_enum_list drm_tv_select_enum_list[] =
  120. {
  121.         { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
  122.         { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
  123.         { DRM_MODE_SUBCONNECTOR_SVIDEO,    "SVIDEO"    }, /* TV-out */
  124.         { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
  125.         { DRM_MODE_SUBCONNECTOR_SCART,     "SCART"     }, /* TV-out */
  126. };
  127.  
  128. DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
  129.  
  130. static const struct drm_prop_enum_list drm_tv_subconnector_enum_list[] =
  131. {
  132.         { DRM_MODE_SUBCONNECTOR_Unknown,   "Unknown"   }, /* DVI-I and TV-out */
  133.         { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
  134.         { DRM_MODE_SUBCONNECTOR_SVIDEO,    "SVIDEO"    }, /* TV-out */
  135.         { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
  136.         { DRM_MODE_SUBCONNECTOR_SCART,     "SCART"     }, /* TV-out */
  137. };
  138.  
  139. DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,
  140.                  drm_tv_subconnector_enum_list)
  141.  
  142. static const struct drm_prop_enum_list drm_dirty_info_enum_list[] = {
  143.         { DRM_MODE_DIRTY_OFF,      "Off"      },
  144.         { DRM_MODE_DIRTY_ON,       "On"       },
  145.         { DRM_MODE_DIRTY_ANNOTATE, "Annotate" },
  146. };
  147.  
  148. struct drm_conn_prop_enum_list {
  149.         int type;
  150.         const char *name;
  151.         struct ida ida;
  152. };
  153.  
  154. /*
  155.  * Connector and encoder types.
  156.  */
  157. static struct drm_conn_prop_enum_list drm_connector_enum_list[] =
  158. {       { DRM_MODE_CONNECTOR_Unknown, "Unknown" },
  159.         { DRM_MODE_CONNECTOR_VGA, "VGA" },
  160.         { DRM_MODE_CONNECTOR_DVII, "DVI-I" },
  161.         { DRM_MODE_CONNECTOR_DVID, "DVI-D" },
  162.         { DRM_MODE_CONNECTOR_DVIA, "DVI-A" },
  163.         { DRM_MODE_CONNECTOR_Composite, "Composite" },
  164.         { DRM_MODE_CONNECTOR_SVIDEO, "SVIDEO" },
  165.         { DRM_MODE_CONNECTOR_LVDS, "LVDS" },
  166.         { DRM_MODE_CONNECTOR_Component, "Component" },
  167.         { DRM_MODE_CONNECTOR_9PinDIN, "DIN" },
  168.         { DRM_MODE_CONNECTOR_DisplayPort, "DP" },
  169.         { DRM_MODE_CONNECTOR_HDMIA, "HDMI-A" },
  170.         { DRM_MODE_CONNECTOR_HDMIB, "HDMI-B" },
  171.         { DRM_MODE_CONNECTOR_TV, "TV" },
  172.         { DRM_MODE_CONNECTOR_eDP, "eDP" },
  173.         { DRM_MODE_CONNECTOR_VIRTUAL, "Virtual" },
  174.         { DRM_MODE_CONNECTOR_DSI, "DSI" },
  175. };
  176.  
  177. static const struct drm_prop_enum_list drm_encoder_enum_list[] =
  178. {       { DRM_MODE_ENCODER_NONE, "None" },
  179.         { DRM_MODE_ENCODER_DAC, "DAC" },
  180.         { DRM_MODE_ENCODER_TMDS, "TMDS" },
  181.         { DRM_MODE_ENCODER_LVDS, "LVDS" },
  182.         { DRM_MODE_ENCODER_TVDAC, "TV" },
  183.         { DRM_MODE_ENCODER_VIRTUAL, "Virtual" },
  184.         { DRM_MODE_ENCODER_DSI, "DSI" },
  185.         { DRM_MODE_ENCODER_DPMST, "DP MST" },
  186. };
  187.  
  188. static const struct drm_prop_enum_list drm_subpixel_enum_list[] =
  189. {
  190.         { SubPixelUnknown, "Unknown" },
  191.         { SubPixelHorizontalRGB, "Horizontal RGB" },
  192.         { SubPixelHorizontalBGR, "Horizontal BGR" },
  193.         { SubPixelVerticalRGB, "Vertical RGB" },
  194.         { SubPixelVerticalBGR, "Vertical BGR" },
  195.         { SubPixelNone, "None" },
  196. };
  197.  
  198. void drm_connector_ida_init(void)
  199. {
  200.         int i;
  201.  
  202.         for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
  203.                 ida_init(&drm_connector_enum_list[i].ida);
  204. }
  205.  
  206. void drm_connector_ida_destroy(void)
  207. {
  208.         int i;
  209.  
  210.         for (i = 0; i < ARRAY_SIZE(drm_connector_enum_list); i++)
  211.                 ida_destroy(&drm_connector_enum_list[i].ida);
  212. }
  213.  
  214. /**
  215.  * drm_get_connector_status_name - return a string for connector status
  216.  * @status: connector status to compute name of
  217.  *
  218.  * In contrast to the other drm_get_*_name functions this one here returns a
  219.  * const pointer and hence is threadsafe.
  220.  */
  221. const char *drm_get_connector_status_name(enum drm_connector_status status)
  222. {
  223.         if (status == connector_status_connected)
  224.                 return "connected";
  225.         else if (status == connector_status_disconnected)
  226.                 return "disconnected";
  227.         else
  228.                 return "unknown";
  229. }
  230. EXPORT_SYMBOL(drm_get_connector_status_name);
  231.  
  232. /**
  233.  * drm_get_subpixel_order_name - return a string for a given subpixel enum
  234.  * @order: enum of subpixel_order
  235.  *
  236.  * Note you could abuse this and return something out of bounds, but that
  237.  * would be a caller error.  No unscrubbed user data should make it here.
  238.  */
  239. const char *drm_get_subpixel_order_name(enum subpixel_order order)
  240. {
  241.         return drm_subpixel_enum_list[order].name;
  242. }
  243. EXPORT_SYMBOL(drm_get_subpixel_order_name);
  244.  
  245. static char printable_char(int c)
  246. {
  247.         return isascii(c) && isprint(c) ? c : '?';
  248. }
  249.  
  250. /**
  251.  * drm_get_format_name - return a string for drm fourcc format
  252.  * @format: format to compute name of
  253.  *
  254.  * Note that the buffer used by this function is globally shared and owned by
  255.  * the function itself.
  256.  *
  257.  * FIXME: This isn't really multithreading safe.
  258.  */
  259. const char *drm_get_format_name(uint32_t format)
  260. {
  261.         static char buf[32];
  262.  
  263.         snprintf(buf, sizeof(buf),
  264.                  "%c%c%c%c %s-endian (0x%08x)",
  265.                  printable_char(format & 0xff),
  266.                  printable_char((format >> 8) & 0xff),
  267.                  printable_char((format >> 16) & 0xff),
  268.                  printable_char((format >> 24) & 0x7f),
  269.                  format & DRM_FORMAT_BIG_ENDIAN ? "big" : "little",
  270.                  format);
  271.  
  272.         return buf;
  273. }
  274. EXPORT_SYMBOL(drm_get_format_name);
  275.  
  276. /*
  277.  * Internal function to assign a slot in the object idr and optionally
  278.  * register the object into the idr.
  279.  */
  280. static int drm_mode_object_get_reg(struct drm_device *dev,
  281.                                    struct drm_mode_object *obj,
  282.                                    uint32_t obj_type,
  283.                                    bool register_obj)
  284. {
  285.         int ret;
  286.  
  287.         mutex_lock(&dev->mode_config.idr_mutex);
  288.         ret = idr_alloc(&dev->mode_config.crtc_idr, register_obj ? obj : NULL, 1, 0, GFP_KERNEL);
  289.         if (ret >= 0) {
  290.                 /*
  291.                  * Set up the object linking under the protection of the idr
  292.                  * lock so that other users can't see inconsistent state.
  293.                  */
  294.                 obj->id = ret;
  295.                 obj->type = obj_type;
  296.         }
  297.         mutex_unlock(&dev->mode_config.idr_mutex);
  298.  
  299.         return ret < 0 ? ret : 0;
  300. }
  301.  
  302. /**
  303.  * drm_mode_object_get - allocate a new modeset identifier
  304.  * @dev: DRM device
  305.  * @obj: object pointer, used to generate unique ID
  306.  * @obj_type: object type
  307.  *
  308.  * Create a unique identifier based on @ptr in @dev's identifier space.  Used
  309.  * for tracking modes, CRTCs and connectors. Note that despite the _get postfix
  310.  * modeset identifiers are _not_ reference counted. Hence don't use this for
  311.  * reference counted modeset objects like framebuffers.
  312.  *
  313.  * Returns:
  314.  * New unique (relative to other objects in @dev) integer identifier for the
  315.  * object.
  316.  */
  317. int drm_mode_object_get(struct drm_device *dev,
  318.                                struct drm_mode_object *obj, uint32_t obj_type)
  319. {
  320.         return drm_mode_object_get_reg(dev, obj, obj_type, true);
  321. }
  322.  
  323. static void drm_mode_object_register(struct drm_device *dev,
  324.                                      struct drm_mode_object *obj)
  325. {
  326.         mutex_lock(&dev->mode_config.idr_mutex);
  327.         idr_replace(&dev->mode_config.crtc_idr, obj, obj->id);
  328.         mutex_unlock(&dev->mode_config.idr_mutex);
  329. }
  330.  
  331. /**
  332.  * drm_mode_object_put - free a modeset identifer
  333.  * @dev: DRM device
  334.  * @object: object to free
  335.  *
  336.  * Free @id from @dev's unique identifier pool. Note that despite the _get
  337.  * postfix modeset identifiers are _not_ reference counted. Hence don't use this
  338.  * for reference counted modeset objects like framebuffers.
  339.  */
  340. void drm_mode_object_put(struct drm_device *dev,
  341.                                 struct drm_mode_object *object)
  342. {
  343.         mutex_lock(&dev->mode_config.idr_mutex);
  344.         idr_remove(&dev->mode_config.crtc_idr, object->id);
  345.         mutex_unlock(&dev->mode_config.idr_mutex);
  346. }
  347.  
  348. static struct drm_mode_object *_object_find(struct drm_device *dev,
  349.                 uint32_t id, uint32_t type)
  350. {
  351.         struct drm_mode_object *obj = NULL;
  352.  
  353.         mutex_lock(&dev->mode_config.idr_mutex);
  354.         obj = idr_find(&dev->mode_config.crtc_idr, id);
  355.         if (obj && type != DRM_MODE_OBJECT_ANY && obj->type != type)
  356.                 obj = NULL;
  357.         if (obj && obj->id != id)
  358.                 obj = NULL;
  359.         /* don't leak out unref'd fb's */
  360.         if (obj && (obj->type == DRM_MODE_OBJECT_FB))
  361.                 obj = NULL;
  362.         mutex_unlock(&dev->mode_config.idr_mutex);
  363.  
  364.         return obj;
  365. }
  366.  
  367. /**
  368.  * drm_mode_object_find - look up a drm object with static lifetime
  369.  * @dev: drm device
  370.  * @id: id of the mode object
  371.  * @type: type of the mode object
  372.  *
  373.  * Note that framebuffers cannot be looked up with this functions - since those
  374.  * are reference counted, they need special treatment.  Even with
  375.  * DRM_MODE_OBJECT_ANY (although that will simply return NULL
  376.  * rather than WARN_ON()).
  377.  */
  378. struct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
  379.                 uint32_t id, uint32_t type)
  380. {
  381.         struct drm_mode_object *obj = NULL;
  382.  
  383.         /* Framebuffers are reference counted and need their own lookup
  384.          * function.*/
  385.         WARN_ON(type == DRM_MODE_OBJECT_FB);
  386.         obj = _object_find(dev, id, type);
  387.         return obj;
  388. }
  389. EXPORT_SYMBOL(drm_mode_object_find);
  390.  
  391. /**
  392.  * drm_framebuffer_init - initialize a framebuffer
  393.  * @dev: DRM device
  394.  * @fb: framebuffer to be initialized
  395.  * @funcs: ... with these functions
  396.  *
  397.  * Allocates an ID for the framebuffer's parent mode object, sets its mode
  398.  * functions & device file and adds it to the master fd list.
  399.  *
  400.  * IMPORTANT:
  401.  * This functions publishes the fb and makes it available for concurrent access
  402.  * by other users. Which means by this point the fb _must_ be fully set up -
  403.  * since all the fb attributes are invariant over its lifetime, no further
  404.  * locking but only correct reference counting is required.
  405.  *
  406.  * Returns:
  407.  * Zero on success, error code on failure.
  408.  */
  409. int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
  410.                          const struct drm_framebuffer_funcs *funcs)
  411. {
  412.         int ret;
  413.  
  414.         mutex_lock(&dev->mode_config.fb_lock);
  415.         kref_init(&fb->refcount);
  416.         INIT_LIST_HEAD(&fb->filp_head);
  417.         fb->dev = dev;
  418.         fb->funcs = funcs;
  419.  
  420.         ret = drm_mode_object_get(dev, &fb->base, DRM_MODE_OBJECT_FB);
  421.         if (ret)
  422.                 goto out;
  423.  
  424.         dev->mode_config.num_fb++;
  425.         list_add(&fb->head, &dev->mode_config.fb_list);
  426. out:
  427.         mutex_unlock(&dev->mode_config.fb_lock);
  428.  
  429.         return 0;
  430. }
  431. EXPORT_SYMBOL(drm_framebuffer_init);
  432.  
  433. /* dev->mode_config.fb_lock must be held! */
  434. static void __drm_framebuffer_unregister(struct drm_device *dev,
  435.                                          struct drm_framebuffer *fb)
  436. {
  437.         mutex_lock(&dev->mode_config.idr_mutex);
  438.         idr_remove(&dev->mode_config.crtc_idr, fb->base.id);
  439.         mutex_unlock(&dev->mode_config.idr_mutex);
  440.  
  441.         fb->base.id = 0;
  442. }
  443.  
  444. static void drm_framebuffer_free(struct kref *kref)
  445. {
  446.         struct drm_framebuffer *fb =
  447.                         container_of(kref, struct drm_framebuffer, refcount);
  448.         struct drm_device *dev = fb->dev;
  449.  
  450.         /*
  451.          * The lookup idr holds a weak reference, which has not necessarily been
  452.          * removed at this point. Check for that.
  453.          */
  454.         mutex_lock(&dev->mode_config.fb_lock);
  455.         if (fb->base.id) {
  456.                 /* Mark fb as reaped and drop idr ref. */
  457.                 __drm_framebuffer_unregister(dev, fb);
  458.         }
  459.         mutex_unlock(&dev->mode_config.fb_lock);
  460.  
  461.         fb->funcs->destroy(fb);
  462. }
  463.  
  464. static struct drm_framebuffer *__drm_framebuffer_lookup(struct drm_device *dev,
  465.                                                         uint32_t id)
  466. {
  467.         struct drm_mode_object *obj = NULL;
  468.         struct drm_framebuffer *fb;
  469.  
  470.         mutex_lock(&dev->mode_config.idr_mutex);
  471.         obj = idr_find(&dev->mode_config.crtc_idr, id);
  472.         if (!obj || (obj->type != DRM_MODE_OBJECT_FB) || (obj->id != id))
  473.                 fb = NULL;
  474.         else
  475.                 fb = obj_to_fb(obj);
  476.         mutex_unlock(&dev->mode_config.idr_mutex);
  477.  
  478.         return fb;
  479. }
  480.  
  481. /**
  482.  * drm_framebuffer_lookup - look up a drm framebuffer and grab a reference
  483.  * @dev: drm device
  484.  * @id: id of the fb object
  485.  *
  486.  * If successful, this grabs an additional reference to the framebuffer -
  487.  * callers need to make sure to eventually unreference the returned framebuffer
  488.  * again, using @drm_framebuffer_unreference.
  489.  */
  490. struct drm_framebuffer *drm_framebuffer_lookup(struct drm_device *dev,
  491.                                                uint32_t id)
  492. {
  493.         struct drm_framebuffer *fb;
  494.  
  495.         mutex_lock(&dev->mode_config.fb_lock);
  496.         fb = __drm_framebuffer_lookup(dev, id);
  497.         if (fb)
  498.                 drm_framebuffer_reference(fb);
  499.         mutex_unlock(&dev->mode_config.fb_lock);
  500.  
  501.         return fb;
  502. }
  503. EXPORT_SYMBOL(drm_framebuffer_lookup);
  504.  
  505. /**
  506.  * drm_framebuffer_unreference - unref a framebuffer
  507.  * @fb: framebuffer to unref
  508.  *
  509.  * This functions decrements the fb's refcount and frees it if it drops to zero.
  510.  */
  511. void drm_framebuffer_unreference(struct drm_framebuffer *fb)
  512. {
  513.         DRM_DEBUG("%p: FB ID: %d (%d)\n", fb, fb->base.id, atomic_read(&fb->refcount.refcount));
  514.         kref_put(&fb->refcount, drm_framebuffer_free);
  515. }
  516. EXPORT_SYMBOL(drm_framebuffer_unreference);
  517.  
  518. /**
  519.  * drm_framebuffer_reference - incr the fb refcnt
  520.  * @fb: framebuffer
  521.  *
  522.  * This functions increments the fb's refcount.
  523.  */
  524. void drm_framebuffer_reference(struct drm_framebuffer *fb)
  525. {
  526.         DRM_DEBUG("%p: FB ID: %d (%d)\n", fb, fb->base.id, atomic_read(&fb->refcount.refcount));
  527.         kref_get(&fb->refcount);
  528. }
  529. EXPORT_SYMBOL(drm_framebuffer_reference);
  530.  
  531. static void drm_framebuffer_free_bug(struct kref *kref)
  532. {
  533.         BUG();
  534. }
  535.  
  536. static void __drm_framebuffer_unreference(struct drm_framebuffer *fb)
  537. {
  538.         DRM_DEBUG("%p: FB ID: %d (%d)\n", fb, fb->base.id, atomic_read(&fb->refcount.refcount));
  539.         kref_put(&fb->refcount, drm_framebuffer_free_bug);
  540. }
  541.  
  542. /**
  543.  * drm_framebuffer_unregister_private - unregister a private fb from the lookup idr
  544.  * @fb: fb to unregister
  545.  *
  546.  * Drivers need to call this when cleaning up driver-private framebuffers, e.g.
  547.  * those used for fbdev. Note that the caller must hold a reference of it's own,
  548.  * i.e. the object may not be destroyed through this call (since it'll lead to a
  549.  * locking inversion).
  550.  */
  551. void drm_framebuffer_unregister_private(struct drm_framebuffer *fb)
  552. {
  553.         struct drm_device *dev = fb->dev;
  554.  
  555.         mutex_lock(&dev->mode_config.fb_lock);
  556.         /* Mark fb as reaped and drop idr ref. */
  557.         __drm_framebuffer_unregister(dev, fb);
  558.         mutex_unlock(&dev->mode_config.fb_lock);
  559. }
  560. EXPORT_SYMBOL(drm_framebuffer_unregister_private);
  561.  
  562. /**
  563.  * drm_framebuffer_cleanup - remove a framebuffer object
  564.  * @fb: framebuffer to remove
  565.  *
  566.  * Cleanup framebuffer. This function is intended to be used from the drivers
  567.  * ->destroy callback. It can also be used to clean up driver private
  568.  *  framebuffers embedded into a larger structure.
  569.  *
  570.  * Note that this function does not remove the fb from active usuage - if it is
  571.  * still used anywhere, hilarity can ensue since userspace could call getfb on
  572.  * the id and get back -EINVAL. Obviously no concern at driver unload time.
  573.  *
  574.  * Also, the framebuffer will not be removed from the lookup idr - for
  575.  * user-created framebuffers this will happen in in the rmfb ioctl. For
  576.  * driver-private objects (e.g. for fbdev) drivers need to explicitly call
  577.  * drm_framebuffer_unregister_private.
  578.  */
  579. void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
  580. {
  581.         struct drm_device *dev = fb->dev;
  582.  
  583.         mutex_lock(&dev->mode_config.fb_lock);
  584.         list_del(&fb->head);
  585.         dev->mode_config.num_fb--;
  586.         mutex_unlock(&dev->mode_config.fb_lock);
  587. }
  588. EXPORT_SYMBOL(drm_framebuffer_cleanup);
  589.  
  590. /**
  591.  * drm_framebuffer_remove - remove and unreference a framebuffer object
  592.  * @fb: framebuffer to remove
  593.  *
  594.  * Scans all the CRTCs and planes in @dev's mode_config.  If they're
  595.  * using @fb, removes it, setting it to NULL. Then drops the reference to the
  596.  * passed-in framebuffer. Might take the modeset locks.
  597.  *
  598.  * Note that this function optimizes the cleanup away if the caller holds the
  599.  * last reference to the framebuffer. It is also guaranteed to not take the
  600.  * modeset locks in this case.
  601.  */
  602. void drm_framebuffer_remove(struct drm_framebuffer *fb)
  603. {
  604.         struct drm_device *dev = fb->dev;
  605.         struct drm_crtc *crtc;
  606.         struct drm_plane *plane;
  607.         struct drm_mode_set set;
  608.         int ret;
  609.  
  610.         WARN_ON(!list_empty(&fb->filp_head));
  611.  
  612.         /*
  613.          * drm ABI mandates that we remove any deleted framebuffers from active
  614.          * useage. But since most sane clients only remove framebuffers they no
  615.          * longer need, try to optimize this away.
  616.          *
  617.          * Since we're holding a reference ourselves, observing a refcount of 1
  618.          * means that we're the last holder and can skip it. Also, the refcount
  619.          * can never increase from 1 again, so we don't need any barriers or
  620.          * locks.
  621.          *
  622.          * Note that userspace could try to race with use and instate a new
  623.          * usage _after_ we've cleared all current ones. End result will be an
  624.          * in-use fb with fb-id == 0. Userspace is allowed to shoot its own foot
  625.          * in this manner.
  626.          */
  627.         if (atomic_read(&fb->refcount.refcount) > 1) {
  628.                 drm_modeset_lock_all(dev);
  629.                 /* remove from any CRTC */
  630.                 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  631.                         if (crtc->primary->fb == fb) {
  632.                                 /* should turn off the crtc */
  633.                                 memset(&set, 0, sizeof(struct drm_mode_set));
  634.                                 set.crtc = crtc;
  635.                                 set.fb = NULL;
  636.                                 ret = drm_mode_set_config_internal(&set);
  637.                                 if (ret)
  638.                                         DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
  639.                         }
  640.                 }
  641.  
  642.                 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
  643.                         if (plane->fb == fb)
  644.                                 drm_plane_force_disable(plane);
  645.                 }
  646.                 drm_modeset_unlock_all(dev);
  647.         }
  648.  
  649.         drm_framebuffer_unreference(fb);
  650. }
  651. EXPORT_SYMBOL(drm_framebuffer_remove);
  652.  
  653. DEFINE_WW_CLASS(crtc_ww_class);
  654.  
  655. /**
  656.  * drm_crtc_init_with_planes - Initialise a new CRTC object with
  657.  *    specified primary and cursor planes.
  658.  * @dev: DRM device
  659.  * @crtc: CRTC object to init
  660.  * @primary: Primary plane for CRTC
  661.  * @cursor: Cursor plane for CRTC
  662.  * @funcs: callbacks for the new CRTC
  663.  *
  664.  * Inits a new object created as base part of a driver crtc object.
  665.  *
  666.  * Returns:
  667.  * Zero on success, error code on failure.
  668.  */
  669. int drm_crtc_init_with_planes(struct drm_device *dev, struct drm_crtc *crtc,
  670.                               struct drm_plane *primary,
  671.                               struct drm_plane *cursor,
  672.                    const struct drm_crtc_funcs *funcs)
  673. {
  674.         struct drm_mode_config *config = &dev->mode_config;
  675.         int ret;
  676.  
  677.         crtc->dev = dev;
  678.         crtc->funcs = funcs;
  679.         crtc->invert_dimensions = false;
  680.  
  681.         drm_modeset_lock_init(&crtc->mutex);
  682.         ret = drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
  683.         if (ret)
  684.                 return ret;
  685.  
  686.         crtc->base.properties = &crtc->properties;
  687.  
  688.         list_add_tail(&crtc->head, &config->crtc_list);
  689.         config->num_crtc++;
  690.  
  691.         crtc->primary = primary;
  692.         crtc->cursor = cursor;
  693.         if (primary)
  694.                 primary->possible_crtcs = 1 << drm_crtc_index(crtc);
  695.         if (cursor)
  696.                 cursor->possible_crtcs = 1 << drm_crtc_index(crtc);
  697.  
  698.         return 0;
  699. }
  700. EXPORT_SYMBOL(drm_crtc_init_with_planes);
  701.  
  702. /**
  703.  * drm_crtc_cleanup - Clean up the core crtc usage
  704.  * @crtc: CRTC to cleanup
  705.  *
  706.  * This function cleans up @crtc and removes it from the DRM mode setting
  707.  * core. Note that the function does *not* free the crtc structure itself,
  708.  * this is the responsibility of the caller.
  709.  */
  710. void drm_crtc_cleanup(struct drm_crtc *crtc)
  711. {
  712.         struct drm_device *dev = crtc->dev;
  713.  
  714.                 kfree(crtc->gamma_store);
  715.                 crtc->gamma_store = NULL;
  716.  
  717.         drm_modeset_lock_fini(&crtc->mutex);
  718.  
  719.         drm_mode_object_put(dev, &crtc->base);
  720.         list_del(&crtc->head);
  721.         dev->mode_config.num_crtc--;
  722.  
  723.         WARN_ON(crtc->state && !crtc->funcs->atomic_destroy_state);
  724.         if (crtc->state && crtc->funcs->atomic_destroy_state)
  725.                 crtc->funcs->atomic_destroy_state(crtc, crtc->state);
  726.  
  727.         memset(crtc, 0, sizeof(*crtc));
  728. }
  729. EXPORT_SYMBOL(drm_crtc_cleanup);
  730.  
  731. /**
  732.  * drm_crtc_index - find the index of a registered CRTC
  733.  * @crtc: CRTC to find index for
  734.  *
  735.  * Given a registered CRTC, return the index of that CRTC within a DRM
  736.  * device's list of CRTCs.
  737.  */
  738. unsigned int drm_crtc_index(struct drm_crtc *crtc)
  739. {
  740.         unsigned int index = 0;
  741.         struct drm_crtc *tmp;
  742.  
  743.         list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head) {
  744.                 if (tmp == crtc)
  745.                         return index;
  746.  
  747.                 index++;
  748.         }
  749.  
  750.         BUG();
  751. }
  752. EXPORT_SYMBOL(drm_crtc_index);
  753.  
  754. /*
  755.  * drm_mode_remove - remove and free a mode
  756.  * @connector: connector list to modify
  757.  * @mode: mode to remove
  758.  *
  759.  * Remove @mode from @connector's mode list, then free it.
  760.  */
  761. static void drm_mode_remove(struct drm_connector *connector,
  762.                      struct drm_display_mode *mode)
  763. {
  764.         list_del(&mode->head);
  765.         drm_mode_destroy(connector->dev, mode);
  766. }
  767.  
  768. /**
  769.  * drm_connector_init - Init a preallocated connector
  770.  * @dev: DRM device
  771.  * @connector: the connector to init
  772.  * @funcs: callbacks for this connector
  773.  * @connector_type: user visible type of the connector
  774.  *
  775.  * Initialises a preallocated connector. Connectors should be
  776.  * subclassed as part of driver connector objects.
  777.  *
  778.  * Returns:
  779.  * Zero on success, error code on failure.
  780.  */
  781. int drm_connector_init(struct drm_device *dev,
  782.                      struct drm_connector *connector,
  783.                      const struct drm_connector_funcs *funcs,
  784.                      int connector_type)
  785. {
  786.         int ret;
  787.         struct ida *connector_ida =
  788.                 &drm_connector_enum_list[connector_type].ida;
  789.  
  790.         drm_modeset_lock_all(dev);
  791.  
  792.         ret = drm_mode_object_get_reg(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR, false);
  793.         if (ret)
  794.                 goto out_unlock;
  795.  
  796.         connector->base.properties = &connector->properties;
  797.         connector->dev = dev;
  798.         connector->funcs = funcs;
  799.         connector->connector_type = connector_type;
  800.         connector->connector_type_id =
  801.                 ida_simple_get(connector_ida, 1, 0, GFP_KERNEL);
  802.         if (connector->connector_type_id < 0) {
  803.                 ret = connector->connector_type_id;
  804.                 goto out_put;
  805.         }
  806.         connector->name =
  807.                 kasprintf(GFP_KERNEL, "%s-%d",
  808.                           drm_connector_enum_list[connector_type].name,
  809.                           connector->connector_type_id);
  810.         if (!connector->name) {
  811.                 ret = -ENOMEM;
  812.                 goto out_put;
  813.         }
  814.  
  815.         INIT_LIST_HEAD(&connector->probed_modes);
  816.         INIT_LIST_HEAD(&connector->modes);
  817.         connector->edid_blob_ptr = NULL;
  818.         connector->status = connector_status_unknown;
  819.  
  820.         list_add_tail(&connector->head, &dev->mode_config.connector_list);
  821.         dev->mode_config.num_connector++;
  822.  
  823.         if (connector_type != DRM_MODE_CONNECTOR_VIRTUAL)
  824.                 drm_object_attach_property(&connector->base,
  825.                                               dev->mode_config.edid_property,
  826.                                               0);
  827.  
  828.         drm_object_attach_property(&connector->base,
  829.                                       dev->mode_config.dpms_property, 0);
  830.  
  831.         connector->debugfs_entry = NULL;
  832.  
  833. out_put:
  834.         if (ret)
  835.                 drm_mode_object_put(dev, &connector->base);
  836.  
  837. out_unlock:
  838.         drm_modeset_unlock_all(dev);
  839.  
  840.         return ret;
  841. }
  842. EXPORT_SYMBOL(drm_connector_init);
  843.  
  844. /**
  845.  * drm_connector_cleanup - cleans up an initialised connector
  846.  * @connector: connector to cleanup
  847.  *
  848.  * Cleans up the connector but doesn't free the object.
  849.  */
  850. void drm_connector_cleanup(struct drm_connector *connector)
  851. {
  852.         struct drm_device *dev = connector->dev;
  853.         struct drm_display_mode *mode, *t;
  854.  
  855.         list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
  856.                 drm_mode_remove(connector, mode);
  857.  
  858.         list_for_each_entry_safe(mode, t, &connector->modes, head)
  859.                 drm_mode_remove(connector, mode);
  860.  
  861.         ida_remove(&drm_connector_enum_list[connector->connector_type].ida,
  862.                    connector->connector_type_id);
  863.  
  864.         drm_mode_object_put(dev, &connector->base);
  865.         kfree(connector->name);
  866.         connector->name = NULL;
  867.         list_del(&connector->head);
  868.         dev->mode_config.num_connector--;
  869.  
  870.         WARN_ON(connector->state && !connector->funcs->atomic_destroy_state);
  871.         if (connector->state && connector->funcs->atomic_destroy_state)
  872.                 connector->funcs->atomic_destroy_state(connector,
  873.                                                        connector->state);
  874.  
  875.         memset(connector, 0, sizeof(*connector));
  876. }
  877. EXPORT_SYMBOL(drm_connector_cleanup);
  878.  
  879. /**
  880.  * drm_connector_index - find the index of a registered connector
  881.  * @connector: connector to find index for
  882.  *
  883.  * Given a registered connector, return the index of that connector within a DRM
  884.  * device's list of connectors.
  885.  */
  886. unsigned int drm_connector_index(struct drm_connector *connector)
  887. {
  888.         unsigned int index = 0;
  889.         struct drm_connector *tmp;
  890.         struct drm_mode_config *config = &connector->dev->mode_config;
  891.  
  892.         WARN_ON(!drm_modeset_is_locked(&config->connection_mutex));
  893.  
  894.         list_for_each_entry(tmp, &connector->dev->mode_config.connector_list, head) {
  895.                 if (tmp == connector)
  896.                         return index;
  897.  
  898.                 index++;
  899.         }
  900.  
  901.         BUG();
  902. }
  903. EXPORT_SYMBOL(drm_connector_index);
  904.  
  905. /**
  906.  * drm_connector_register - register a connector
  907.  * @connector: the connector to register
  908.  *
  909.  * Register userspace interfaces for a connector
  910.  *
  911.  * Returns:
  912.  * Zero on success, error code on failure.
  913.  */
  914. int drm_connector_register(struct drm_connector *connector)
  915. {
  916.         int ret;
  917.  
  918.         drm_mode_object_register(connector->dev, &connector->base);
  919.  
  920.         return 0;
  921. }
  922. EXPORT_SYMBOL(drm_connector_register);
  923.  
  924. /**
  925.  * drm_connector_unregister - unregister a connector
  926.  * @connector: the connector to unregister
  927.  *
  928.  * Unregister userspace interfaces for a connector
  929.  */
  930. void drm_connector_unregister(struct drm_connector *connector)
  931. {
  932. }
  933. EXPORT_SYMBOL(drm_connector_unregister);
  934.  
  935.  
  936. /**
  937.  * drm_connector_unplug_all - unregister connector userspace interfaces
  938.  * @dev: drm device
  939.  *
  940.  * This function unregisters all connector userspace interfaces in sysfs. Should
  941.  * be call when the device is disconnected, e.g. from an usb driver's
  942.  * ->disconnect callback.
  943.  */
  944. void drm_connector_unplug_all(struct drm_device *dev)
  945. {
  946.         struct drm_connector *connector;
  947.  
  948.         /* taking the mode config mutex ends up in a clash with sysfs */
  949.         list_for_each_entry(connector, &dev->mode_config.connector_list, head)
  950.                 drm_connector_unregister(connector);
  951.  
  952. }
  953. EXPORT_SYMBOL(drm_connector_unplug_all);
  954.  
  955. /**
  956.  * drm_bridge_init - initialize a drm transcoder/bridge
  957.  * @dev: drm device
  958.  * @bridge: transcoder/bridge to set up
  959.  * @funcs: bridge function table
  960.  *
  961.  * Initialises a preallocated bridge. Bridges should be
  962.  * subclassed as part of driver connector objects.
  963.  *
  964.  * Returns:
  965.  * Zero on success, error code on failure.
  966.  */
  967. int drm_bridge_init(struct drm_device *dev, struct drm_bridge *bridge,
  968.                 const struct drm_bridge_funcs *funcs)
  969. {
  970.         int ret;
  971.  
  972.         drm_modeset_lock_all(dev);
  973.  
  974.         ret = drm_mode_object_get(dev, &bridge->base, DRM_MODE_OBJECT_BRIDGE);
  975.         if (ret)
  976.                 goto out;
  977.  
  978.         bridge->dev = dev;
  979.         bridge->funcs = funcs;
  980.  
  981.         list_add_tail(&bridge->head, &dev->mode_config.bridge_list);
  982.         dev->mode_config.num_bridge++;
  983.  
  984.  out:
  985.         drm_modeset_unlock_all(dev);
  986.         return ret;
  987. }
  988. EXPORT_SYMBOL(drm_bridge_init);
  989.  
  990. /**
  991.  * drm_bridge_cleanup - cleans up an initialised bridge
  992.  * @bridge: bridge to cleanup
  993.  *
  994.  * Cleans up the bridge but doesn't free the object.
  995.  */
  996. void drm_bridge_cleanup(struct drm_bridge *bridge)
  997. {
  998.         struct drm_device *dev = bridge->dev;
  999.  
  1000.         drm_modeset_lock_all(dev);
  1001.         drm_mode_object_put(dev, &bridge->base);
  1002.         list_del(&bridge->head);
  1003.         dev->mode_config.num_bridge--;
  1004.         drm_modeset_unlock_all(dev);
  1005.  
  1006.         memset(bridge, 0, sizeof(*bridge));
  1007. }
  1008. EXPORT_SYMBOL(drm_bridge_cleanup);
  1009.  
  1010. /**
  1011.  * drm_encoder_init - Init a preallocated encoder
  1012.  * @dev: drm device
  1013.  * @encoder: the encoder to init
  1014.  * @funcs: callbacks for this encoder
  1015.  * @encoder_type: user visible type of the encoder
  1016.  *
  1017.  * Initialises a preallocated encoder. Encoder should be
  1018.  * subclassed as part of driver encoder objects.
  1019.  *
  1020.  * Returns:
  1021.  * Zero on success, error code on failure.
  1022.  */
  1023. int drm_encoder_init(struct drm_device *dev,
  1024.                       struct drm_encoder *encoder,
  1025.                       const struct drm_encoder_funcs *funcs,
  1026.                       int encoder_type)
  1027. {
  1028.         int ret;
  1029.  
  1030.         drm_modeset_lock_all(dev);
  1031.  
  1032.         ret = drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
  1033.         if (ret)
  1034.                 goto out_unlock;
  1035.  
  1036.         encoder->dev = dev;
  1037.         encoder->encoder_type = encoder_type;
  1038.         encoder->funcs = funcs;
  1039.         encoder->name = kasprintf(GFP_KERNEL, "%s-%d",
  1040.                                   drm_encoder_enum_list[encoder_type].name,
  1041.                                   encoder->base.id);
  1042.         if (!encoder->name) {
  1043.                 ret = -ENOMEM;
  1044.                 goto out_put;
  1045.         }
  1046.  
  1047.         list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
  1048.         dev->mode_config.num_encoder++;
  1049.  
  1050. out_put:
  1051.         if (ret)
  1052.                 drm_mode_object_put(dev, &encoder->base);
  1053.  
  1054. out_unlock:
  1055.         drm_modeset_unlock_all(dev);
  1056.  
  1057.         return ret;
  1058. }
  1059. EXPORT_SYMBOL(drm_encoder_init);
  1060.  
  1061. /**
  1062.  * drm_encoder_cleanup - cleans up an initialised encoder
  1063.  * @encoder: encoder to cleanup
  1064.  *
  1065.  * Cleans up the encoder but doesn't free the object.
  1066.  */
  1067. void drm_encoder_cleanup(struct drm_encoder *encoder)
  1068. {
  1069.         struct drm_device *dev = encoder->dev;
  1070.         drm_modeset_lock_all(dev);
  1071.         drm_mode_object_put(dev, &encoder->base);
  1072.         kfree(encoder->name);
  1073.         list_del(&encoder->head);
  1074.         dev->mode_config.num_encoder--;
  1075.         drm_modeset_unlock_all(dev);
  1076.  
  1077.         memset(encoder, 0, sizeof(*encoder));
  1078. }
  1079. EXPORT_SYMBOL(drm_encoder_cleanup);
  1080.  
  1081. /**
  1082.  * drm_universal_plane_init - Initialize a new universal plane object
  1083.  * @dev: DRM device
  1084.  * @plane: plane object to init
  1085.  * @possible_crtcs: bitmask of possible CRTCs
  1086.  * @funcs: callbacks for the new plane
  1087.  * @formats: array of supported formats (%DRM_FORMAT_*)
  1088.  * @format_count: number of elements in @formats
  1089.  * @type: type of plane (overlay, primary, cursor)
  1090.  *
  1091.  * Initializes a plane object of type @type.
  1092.  *
  1093.  * Returns:
  1094.  * Zero on success, error code on failure.
  1095.  */
  1096. int drm_universal_plane_init(struct drm_device *dev, struct drm_plane *plane,
  1097.                    unsigned long possible_crtcs,
  1098.                    const struct drm_plane_funcs *funcs,
  1099.                    const uint32_t *formats, uint32_t format_count,
  1100.                              enum drm_plane_type type)
  1101. {
  1102.         int ret;
  1103.  
  1104.         ret = drm_mode_object_get(dev, &plane->base, DRM_MODE_OBJECT_PLANE);
  1105.         if (ret)
  1106.                 return ret;
  1107.  
  1108.         drm_modeset_lock_init(&plane->mutex);
  1109.  
  1110.         plane->base.properties = &plane->properties;
  1111.         plane->dev = dev;
  1112.         plane->funcs = funcs;
  1113.         plane->format_types = kmalloc(sizeof(uint32_t) * format_count,
  1114.                                       GFP_KERNEL);
  1115.         if (!plane->format_types) {
  1116.                 DRM_DEBUG_KMS("out of memory when allocating plane\n");
  1117.                 drm_mode_object_put(dev, &plane->base);
  1118.                 return -ENOMEM;
  1119.         }
  1120.  
  1121.         memcpy(plane->format_types, formats, format_count * sizeof(uint32_t));
  1122.         plane->format_count = format_count;
  1123.         plane->possible_crtcs = possible_crtcs;
  1124.         plane->type = type;
  1125.  
  1126.                 list_add_tail(&plane->head, &dev->mode_config.plane_list);
  1127.         dev->mode_config.num_total_plane++;
  1128.         if (plane->type == DRM_PLANE_TYPE_OVERLAY)
  1129.                 dev->mode_config.num_overlay_plane++;
  1130.  
  1131.         drm_object_attach_property(&plane->base,
  1132.                                    dev->mode_config.plane_type_property,
  1133.                                    plane->type);
  1134.  
  1135.         return 0;
  1136. }
  1137. EXPORT_SYMBOL(drm_universal_plane_init);
  1138.  
  1139. /**
  1140.  * drm_plane_init - Initialize a legacy plane
  1141.  * @dev: DRM device
  1142.  * @plane: plane object to init
  1143.  * @possible_crtcs: bitmask of possible CRTCs
  1144.  * @funcs: callbacks for the new plane
  1145.  * @formats: array of supported formats (%DRM_FORMAT_*)
  1146.  * @format_count: number of elements in @formats
  1147.  * @is_primary: plane type (primary vs overlay)
  1148.  *
  1149.  * Legacy API to initialize a DRM plane.
  1150.  *
  1151.  * New drivers should call drm_universal_plane_init() instead.
  1152.  *
  1153.  * Returns:
  1154.  * Zero on success, error code on failure.
  1155.  */
  1156. int drm_plane_init(struct drm_device *dev, struct drm_plane *plane,
  1157.                    unsigned long possible_crtcs,
  1158.                    const struct drm_plane_funcs *funcs,
  1159.                    const uint32_t *formats, uint32_t format_count,
  1160.                    bool is_primary)
  1161. {
  1162.         enum drm_plane_type type;
  1163.  
  1164.         type = is_primary ? DRM_PLANE_TYPE_PRIMARY : DRM_PLANE_TYPE_OVERLAY;
  1165.         return drm_universal_plane_init(dev, plane, possible_crtcs, funcs,
  1166.                                         formats, format_count, type);
  1167. }
  1168. EXPORT_SYMBOL(drm_plane_init);
  1169.  
  1170. /**
  1171.  * drm_plane_cleanup - Clean up the core plane usage
  1172.  * @plane: plane to cleanup
  1173.  *
  1174.  * This function cleans up @plane and removes it from the DRM mode setting
  1175.  * core. Note that the function does *not* free the plane structure itself,
  1176.  * this is the responsibility of the caller.
  1177.  */
  1178. void drm_plane_cleanup(struct drm_plane *plane)
  1179. {
  1180.         struct drm_device *dev = plane->dev;
  1181.  
  1182.         drm_modeset_lock_all(dev);
  1183.         kfree(plane->format_types);
  1184.         drm_mode_object_put(dev, &plane->base);
  1185.  
  1186.         BUG_ON(list_empty(&plane->head));
  1187.  
  1188.                 list_del(&plane->head);
  1189.         dev->mode_config.num_total_plane--;
  1190.         if (plane->type == DRM_PLANE_TYPE_OVERLAY)
  1191.                 dev->mode_config.num_overlay_plane--;
  1192.         drm_modeset_unlock_all(dev);
  1193.  
  1194.         WARN_ON(plane->state && !plane->funcs->atomic_destroy_state);
  1195.         if (plane->state && plane->funcs->atomic_destroy_state)
  1196.                 plane->funcs->atomic_destroy_state(plane, plane->state);
  1197.  
  1198.         memset(plane, 0, sizeof(*plane));
  1199. }
  1200. EXPORT_SYMBOL(drm_plane_cleanup);
  1201.  
  1202. /**
  1203.  * drm_plane_index - find the index of a registered plane
  1204.  * @plane: plane to find index for
  1205.  *
  1206.  * Given a registered plane, return the index of that CRTC within a DRM
  1207.  * device's list of planes.
  1208.  */
  1209. unsigned int drm_plane_index(struct drm_plane *plane)
  1210. {
  1211.         unsigned int index = 0;
  1212.         struct drm_plane *tmp;
  1213.  
  1214.         list_for_each_entry(tmp, &plane->dev->mode_config.plane_list, head) {
  1215.                 if (tmp == plane)
  1216.                         return index;
  1217.  
  1218.                 index++;
  1219.         }
  1220.  
  1221.         BUG();
  1222. }
  1223. EXPORT_SYMBOL(drm_plane_index);
  1224.  
  1225. /**
  1226.  * drm_plane_force_disable - Forcibly disable a plane
  1227.  * @plane: plane to disable
  1228.  *
  1229.  * Forces the plane to be disabled.
  1230.  *
  1231.  * Used when the plane's current framebuffer is destroyed,
  1232.  * and when restoring fbdev mode.
  1233.  */
  1234. void drm_plane_force_disable(struct drm_plane *plane)
  1235. {
  1236.         int ret;
  1237.  
  1238.         if (!plane->fb)
  1239.                 return;
  1240.  
  1241.         plane->old_fb = plane->fb;
  1242.         ret = plane->funcs->disable_plane(plane);
  1243.         if (ret) {
  1244.                 DRM_ERROR("failed to disable plane with busy fb\n");
  1245.                 plane->old_fb = NULL;
  1246.                 return;
  1247.         }
  1248.         /* disconnect the plane from the fb and crtc: */
  1249.         __drm_framebuffer_unreference(plane->old_fb);
  1250.         plane->old_fb = NULL;
  1251.         plane->fb = NULL;
  1252.         plane->crtc = NULL;
  1253. }
  1254. EXPORT_SYMBOL(drm_plane_force_disable);
  1255.  
  1256. static int drm_mode_create_standard_connector_properties(struct drm_device *dev)
  1257. {
  1258.         struct drm_property *edid;
  1259.         struct drm_property *dpms;
  1260.         struct drm_property *dev_path;
  1261.  
  1262.         /*
  1263.          * Standard properties (apply to all connectors)
  1264.          */
  1265.         edid = drm_property_create(dev, DRM_MODE_PROP_BLOB |
  1266.                                    DRM_MODE_PROP_IMMUTABLE,
  1267.                                    "EDID", 0);
  1268.         dev->mode_config.edid_property = edid;
  1269.  
  1270.         dpms = drm_property_create_enum(dev, 0,
  1271.                                    "DPMS", drm_dpms_enum_list,
  1272.                                    ARRAY_SIZE(drm_dpms_enum_list));
  1273.         dev->mode_config.dpms_property = dpms;
  1274.  
  1275.         dev_path = drm_property_create(dev,
  1276.                                        DRM_MODE_PROP_BLOB |
  1277.                                        DRM_MODE_PROP_IMMUTABLE,
  1278.                                        "PATH", 0);
  1279.         dev->mode_config.path_property = dev_path;
  1280.  
  1281.         dev->mode_config.tile_property = drm_property_create(dev,
  1282.                                                              DRM_MODE_PROP_BLOB |
  1283.                                                              DRM_MODE_PROP_IMMUTABLE,
  1284.                                                              "TILE", 0);
  1285.  
  1286.         return 0;
  1287. }
  1288.  
  1289. static int drm_mode_create_standard_plane_properties(struct drm_device *dev)
  1290. {
  1291.         struct drm_property *type;
  1292.  
  1293.         /*
  1294.          * Standard properties (apply to all planes)
  1295.          */
  1296.         type = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
  1297.                                         "type", drm_plane_type_enum_list,
  1298.                                         ARRAY_SIZE(drm_plane_type_enum_list));
  1299.         dev->mode_config.plane_type_property = type;
  1300.  
  1301.         return 0;
  1302. }
  1303.  
  1304. /**
  1305.  * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
  1306.  * @dev: DRM device
  1307.  *
  1308.  * Called by a driver the first time a DVI-I connector is made.
  1309.  */
  1310. int drm_mode_create_dvi_i_properties(struct drm_device *dev)
  1311. {
  1312.         struct drm_property *dvi_i_selector;
  1313.         struct drm_property *dvi_i_subconnector;
  1314.  
  1315.         if (dev->mode_config.dvi_i_select_subconnector_property)
  1316.                 return 0;
  1317.  
  1318.         dvi_i_selector =
  1319.                 drm_property_create_enum(dev, 0,
  1320.                                     "select subconnector",
  1321.                                     drm_dvi_i_select_enum_list,
  1322.                                     ARRAY_SIZE(drm_dvi_i_select_enum_list));
  1323.         dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
  1324.  
  1325.         dvi_i_subconnector = drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
  1326.                                     "subconnector",
  1327.                                     drm_dvi_i_subconnector_enum_list,
  1328.                                     ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
  1329.         dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
  1330.  
  1331.         return 0;
  1332. }
  1333. EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
  1334.  
  1335. /**
  1336.  * drm_create_tv_properties - create TV specific connector properties
  1337.  * @dev: DRM device
  1338.  * @num_modes: number of different TV formats (modes) supported
  1339.  * @modes: array of pointers to strings containing name of each format
  1340.  *
  1341.  * Called by a driver's TV initialization routine, this function creates
  1342.  * the TV specific connector properties for a given device.  Caller is
  1343.  * responsible for allocating a list of format names and passing them to
  1344.  * this routine.
  1345.  */
  1346. int drm_mode_create_tv_properties(struct drm_device *dev,
  1347.                                   unsigned int num_modes,
  1348.                                   char *modes[])
  1349. {
  1350.         struct drm_property *tv_selector;
  1351.         struct drm_property *tv_subconnector;
  1352.         unsigned int i;
  1353.  
  1354.         if (dev->mode_config.tv_select_subconnector_property)
  1355.                 return 0;
  1356.  
  1357.         /*
  1358.          * Basic connector properties
  1359.          */
  1360.         tv_selector = drm_property_create_enum(dev, 0,
  1361.                                           "select subconnector",
  1362.                                           drm_tv_select_enum_list,
  1363.                                           ARRAY_SIZE(drm_tv_select_enum_list));
  1364.         dev->mode_config.tv_select_subconnector_property = tv_selector;
  1365.  
  1366.         tv_subconnector =
  1367.                 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
  1368.                                     "subconnector",
  1369.                                     drm_tv_subconnector_enum_list,
  1370.                                     ARRAY_SIZE(drm_tv_subconnector_enum_list));
  1371.         dev->mode_config.tv_subconnector_property = tv_subconnector;
  1372.  
  1373.         /*
  1374.          * Other, TV specific properties: margins & TV modes.
  1375.          */
  1376.         dev->mode_config.tv_left_margin_property =
  1377.                 drm_property_create_range(dev, 0, "left margin", 0, 100);
  1378.  
  1379.         dev->mode_config.tv_right_margin_property =
  1380.                 drm_property_create_range(dev, 0, "right margin", 0, 100);
  1381.  
  1382.         dev->mode_config.tv_top_margin_property =
  1383.                 drm_property_create_range(dev, 0, "top margin", 0, 100);
  1384.  
  1385.         dev->mode_config.tv_bottom_margin_property =
  1386.                 drm_property_create_range(dev, 0, "bottom margin", 0, 100);
  1387.  
  1388.         dev->mode_config.tv_mode_property =
  1389.                 drm_property_create(dev, DRM_MODE_PROP_ENUM,
  1390.                                     "mode", num_modes);
  1391.         for (i = 0; i < num_modes; i++)
  1392.                 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
  1393.                                       i, modes[i]);
  1394.  
  1395.         dev->mode_config.tv_brightness_property =
  1396.                 drm_property_create_range(dev, 0, "brightness", 0, 100);
  1397.  
  1398.         dev->mode_config.tv_contrast_property =
  1399.                 drm_property_create_range(dev, 0, "contrast", 0, 100);
  1400.  
  1401.         dev->mode_config.tv_flicker_reduction_property =
  1402.                 drm_property_create_range(dev, 0, "flicker reduction", 0, 100);
  1403.  
  1404.         dev->mode_config.tv_overscan_property =
  1405.                 drm_property_create_range(dev, 0, "overscan", 0, 100);
  1406.  
  1407.         dev->mode_config.tv_saturation_property =
  1408.                 drm_property_create_range(dev, 0, "saturation", 0, 100);
  1409.  
  1410.         dev->mode_config.tv_hue_property =
  1411.                 drm_property_create_range(dev, 0, "hue", 0, 100);
  1412.  
  1413.         return 0;
  1414. }
  1415. EXPORT_SYMBOL(drm_mode_create_tv_properties);
  1416.  
  1417. /**
  1418.  * drm_mode_create_scaling_mode_property - create scaling mode property
  1419.  * @dev: DRM device
  1420.  *
  1421.  * Called by a driver the first time it's needed, must be attached to desired
  1422.  * connectors.
  1423.  */
  1424. int drm_mode_create_scaling_mode_property(struct drm_device *dev)
  1425. {
  1426.         struct drm_property *scaling_mode;
  1427.  
  1428.         if (dev->mode_config.scaling_mode_property)
  1429.                 return 0;
  1430.  
  1431.         scaling_mode =
  1432.                 drm_property_create_enum(dev, 0, "scaling mode",
  1433.                                 drm_scaling_mode_enum_list,
  1434.                                     ARRAY_SIZE(drm_scaling_mode_enum_list));
  1435.  
  1436.         dev->mode_config.scaling_mode_property = scaling_mode;
  1437.  
  1438.         return 0;
  1439. }
  1440. EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
  1441.  
  1442. /**
  1443.  * drm_mode_create_aspect_ratio_property - create aspect ratio property
  1444.  * @dev: DRM device
  1445.  *
  1446.  * Called by a driver the first time it's needed, must be attached to desired
  1447.  * connectors.
  1448.  *
  1449.  * Returns:
  1450.  * Zero on success, negative errno on failure.
  1451.  */
  1452. int drm_mode_create_aspect_ratio_property(struct drm_device *dev)
  1453. {
  1454.         if (dev->mode_config.aspect_ratio_property)
  1455.                 return 0;
  1456.  
  1457.         dev->mode_config.aspect_ratio_property =
  1458.                 drm_property_create_enum(dev, 0, "aspect ratio",
  1459.                                 drm_aspect_ratio_enum_list,
  1460.                                 ARRAY_SIZE(drm_aspect_ratio_enum_list));
  1461.  
  1462.         if (dev->mode_config.aspect_ratio_property == NULL)
  1463.                 return -ENOMEM;
  1464.  
  1465.         return 0;
  1466. }
  1467. EXPORT_SYMBOL(drm_mode_create_aspect_ratio_property);
  1468.  
  1469. /**
  1470.  * drm_mode_create_dirty_property - create dirty property
  1471.  * @dev: DRM device
  1472.  *
  1473.  * Called by a driver the first time it's needed, must be attached to desired
  1474.  * connectors.
  1475.  */
  1476. int drm_mode_create_dirty_info_property(struct drm_device *dev)
  1477. {
  1478.         struct drm_property *dirty_info;
  1479.  
  1480.         if (dev->mode_config.dirty_info_property)
  1481.                 return 0;
  1482.  
  1483.         dirty_info =
  1484.                 drm_property_create_enum(dev, DRM_MODE_PROP_IMMUTABLE,
  1485.                                     "dirty",
  1486.                                     drm_dirty_info_enum_list,
  1487.                                     ARRAY_SIZE(drm_dirty_info_enum_list));
  1488.         dev->mode_config.dirty_info_property = dirty_info;
  1489.  
  1490.         return 0;
  1491. }
  1492. EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
  1493.  
  1494. /**
  1495.  * drm_mode_create_suggested_offset_properties - create suggests offset properties
  1496.  * @dev: DRM device
  1497.  *
  1498.  * Create the the suggested x/y offset property for connectors.
  1499.  */
  1500. int drm_mode_create_suggested_offset_properties(struct drm_device *dev)
  1501. {
  1502.         if (dev->mode_config.suggested_x_property && dev->mode_config.suggested_y_property)
  1503.                 return 0;
  1504.  
  1505.         dev->mode_config.suggested_x_property =
  1506.                 drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested X", 0, 0xffffffff);
  1507.  
  1508.         dev->mode_config.suggested_y_property =
  1509.                 drm_property_create_range(dev, DRM_MODE_PROP_IMMUTABLE, "suggested Y", 0, 0xffffffff);
  1510.  
  1511.         if (dev->mode_config.suggested_x_property == NULL ||
  1512.             dev->mode_config.suggested_y_property == NULL)
  1513.                 return -ENOMEM;
  1514.         return 0;
  1515. }
  1516. EXPORT_SYMBOL(drm_mode_create_suggested_offset_properties);
  1517.  
  1518. static int drm_mode_group_init(struct drm_device *dev, struct drm_mode_group *group)
  1519. {
  1520.         uint32_t total_objects = 0;
  1521.  
  1522.         total_objects += dev->mode_config.num_crtc;
  1523.         total_objects += dev->mode_config.num_connector;
  1524.         total_objects += dev->mode_config.num_encoder;
  1525.         total_objects += dev->mode_config.num_bridge;
  1526.  
  1527.         group->id_list = kzalloc(total_objects * sizeof(uint32_t), GFP_KERNEL);
  1528.         if (!group->id_list)
  1529.                 return -ENOMEM;
  1530.  
  1531.         group->num_crtcs = 0;
  1532.         group->num_connectors = 0;
  1533.         group->num_encoders = 0;
  1534.         group->num_bridges = 0;
  1535.         return 0;
  1536. }
  1537.  
  1538. void drm_mode_group_destroy(struct drm_mode_group *group)
  1539. {
  1540.         kfree(group->id_list);
  1541.         group->id_list = NULL;
  1542. }
  1543.  
  1544. /*
  1545.  * NOTE: Driver's shouldn't ever call drm_mode_group_init_legacy_group - it is
  1546.  * the drm core's responsibility to set up mode control groups.
  1547.  */
  1548. int drm_mode_group_init_legacy_group(struct drm_device *dev,
  1549.                                      struct drm_mode_group *group)
  1550. {
  1551.         struct drm_crtc *crtc;
  1552.         struct drm_encoder *encoder;
  1553.         struct drm_connector *connector;
  1554.         struct drm_bridge *bridge;
  1555.         int ret;
  1556.  
  1557.         if ((ret = drm_mode_group_init(dev, group)))
  1558.                 return ret;
  1559.  
  1560.         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
  1561.                 group->id_list[group->num_crtcs++] = crtc->base.id;
  1562.  
  1563.         list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
  1564.                 group->id_list[group->num_crtcs + group->num_encoders++] =
  1565.                 encoder->base.id;
  1566.  
  1567.         list_for_each_entry(connector, &dev->mode_config.connector_list, head)
  1568.                 group->id_list[group->num_crtcs + group->num_encoders +
  1569.                                group->num_connectors++] = connector->base.id;
  1570.  
  1571.         list_for_each_entry(bridge, &dev->mode_config.bridge_list, head)
  1572.                 group->id_list[group->num_crtcs + group->num_encoders +
  1573.                                group->num_connectors + group->num_bridges++] =
  1574.                                         bridge->base.id;
  1575.  
  1576.         return 0;
  1577. }
  1578. EXPORT_SYMBOL(drm_mode_group_init_legacy_group);
  1579.  
  1580. void drm_reinit_primary_mode_group(struct drm_device *dev)
  1581. {
  1582.         drm_modeset_lock_all(dev);
  1583.         drm_mode_group_destroy(&dev->primary->mode_group);
  1584.         drm_mode_group_init_legacy_group(dev, &dev->primary->mode_group);
  1585.         drm_modeset_unlock_all(dev);
  1586. }
  1587. EXPORT_SYMBOL(drm_reinit_primary_mode_group);
  1588.  
  1589. /**
  1590.  * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
  1591.  * @out: drm_mode_modeinfo struct to return to the user
  1592.  * @in: drm_display_mode to use
  1593.  *
  1594.  * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
  1595.  * the user.
  1596.  */
  1597. static void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out,
  1598.                                       const struct drm_display_mode *in)
  1599. {
  1600.         WARN(in->hdisplay > USHRT_MAX || in->hsync_start > USHRT_MAX ||
  1601.              in->hsync_end > USHRT_MAX || in->htotal > USHRT_MAX ||
  1602.              in->hskew > USHRT_MAX || in->vdisplay > USHRT_MAX ||
  1603.              in->vsync_start > USHRT_MAX || in->vsync_end > USHRT_MAX ||
  1604.              in->vtotal > USHRT_MAX || in->vscan > USHRT_MAX,
  1605.              "timing values too large for mode info\n");
  1606.  
  1607.         out->clock = in->clock;
  1608.         out->hdisplay = in->hdisplay;
  1609.         out->hsync_start = in->hsync_start;
  1610.         out->hsync_end = in->hsync_end;
  1611.         out->htotal = in->htotal;
  1612.         out->hskew = in->hskew;
  1613.         out->vdisplay = in->vdisplay;
  1614.         out->vsync_start = in->vsync_start;
  1615.         out->vsync_end = in->vsync_end;
  1616.         out->vtotal = in->vtotal;
  1617.         out->vscan = in->vscan;
  1618.         out->vrefresh = in->vrefresh;
  1619.         out->flags = in->flags;
  1620.         out->type = in->type;
  1621.         strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
  1622.         out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
  1623. }
  1624.  
  1625. /**
  1626.  * drm_crtc_convert_umode - convert a modeinfo into a drm_display_mode
  1627.  * @out: drm_display_mode to return to the user
  1628.  * @in: drm_mode_modeinfo to use
  1629.  *
  1630.  * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
  1631.  * the caller.
  1632.  *
  1633.  * Returns:
  1634.  * Zero on success, negative errno on failure.
  1635.  */
  1636. static int drm_crtc_convert_umode(struct drm_display_mode *out,
  1637.                                   const struct drm_mode_modeinfo *in)
  1638. {
  1639.         if (in->clock > INT_MAX || in->vrefresh > INT_MAX)
  1640.                 return -ERANGE;
  1641.  
  1642.         if ((in->flags & DRM_MODE_FLAG_3D_MASK) > DRM_MODE_FLAG_3D_MAX)
  1643.                 return -EINVAL;
  1644.  
  1645.         out->clock = in->clock;
  1646.         out->hdisplay = in->hdisplay;
  1647.         out->hsync_start = in->hsync_start;
  1648.         out->hsync_end = in->hsync_end;
  1649.         out->htotal = in->htotal;
  1650.         out->hskew = in->hskew;
  1651.         out->vdisplay = in->vdisplay;
  1652.         out->vsync_start = in->vsync_start;
  1653.         out->vsync_end = in->vsync_end;
  1654.         out->vtotal = in->vtotal;
  1655.         out->vscan = in->vscan;
  1656.         out->vrefresh = in->vrefresh;
  1657.         out->flags = in->flags;
  1658.         out->type = in->type;
  1659.         strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
  1660.         out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
  1661.  
  1662.         return 0;
  1663. }
  1664.  
  1665.  
  1666. #if 0
  1667. /**
  1668.  * drm_mode_getresources - get graphics configuration
  1669.  * @dev: drm device for the ioctl
  1670.  * @data: data pointer for the ioctl
  1671.  * @file_priv: drm file for the ioctl call
  1672.  *
  1673.  * Construct a set of configuration description structures and return
  1674.  * them to the user, including CRTC, connector and framebuffer configuration.
  1675.  *
  1676.  * Called by the user via ioctl.
  1677.  *
  1678.  * Returns:
  1679.  * Zero on success, negative errno on failure.
  1680.  */
  1681. int drm_mode_getresources(struct drm_device *dev, void *data,
  1682.                           struct drm_file *file_priv)
  1683. {
  1684.         struct drm_mode_card_res *card_res = data;
  1685.         struct list_head *lh;
  1686.         struct drm_framebuffer *fb;
  1687.         struct drm_connector *connector;
  1688.         struct drm_crtc *crtc;
  1689.         struct drm_encoder *encoder;
  1690.         int ret = 0;
  1691.         int connector_count = 0;
  1692.         int crtc_count = 0;
  1693.         int fb_count = 0;
  1694.         int encoder_count = 0;
  1695.         int copied = 0, i;
  1696.         uint32_t __user *fb_id;
  1697.         uint32_t __user *crtc_id;
  1698.         uint32_t __user *connector_id;
  1699.         uint32_t __user *encoder_id;
  1700.         struct drm_mode_group *mode_group;
  1701.  
  1702.         if (!drm_core_check_feature(dev, DRIVER_MODESET))
  1703.                 return -EINVAL;
  1704.  
  1705.  
  1706.         mutex_lock(&file_priv->fbs_lock);
  1707.         /*
  1708.          * For the non-control nodes we need to limit the list of resources
  1709.          * by IDs in the group list for this node
  1710.          */
  1711.         list_for_each(lh, &file_priv->fbs)
  1712.                 fb_count++;
  1713.  
  1714.         /* handle this in 4 parts */
  1715.         /* FBs */
  1716.         if (card_res->count_fbs >= fb_count) {
  1717.                 copied = 0;
  1718.                 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
  1719.                 list_for_each_entry(fb, &file_priv->fbs, filp_head) {
  1720.                         if (put_user(fb->base.id, fb_id + copied)) {
  1721.                                 mutex_unlock(&file_priv->fbs_lock);
  1722.                                 return -EFAULT;
  1723.                         }
  1724.                         copied++;
  1725.                 }
  1726.         }
  1727.         card_res->count_fbs = fb_count;
  1728.         mutex_unlock(&file_priv->fbs_lock);
  1729.  
  1730.         /* mode_config.mutex protects the connector list against e.g. DP MST
  1731.          * connector hot-adding. CRTC/Plane lists are invariant. */
  1732.         mutex_lock(&dev->mode_config.mutex);
  1733.         if (!drm_is_primary_client(file_priv)) {
  1734.  
  1735.                 mode_group = NULL;
  1736.                 list_for_each(lh, &dev->mode_config.crtc_list)
  1737.                         crtc_count++;
  1738.  
  1739.                 list_for_each(lh, &dev->mode_config.connector_list)
  1740.                         connector_count++;
  1741.  
  1742.                 list_for_each(lh, &dev->mode_config.encoder_list)
  1743.                         encoder_count++;
  1744.         } else {
  1745.  
  1746.                 mode_group = &file_priv->master->minor->mode_group;
  1747.                 crtc_count = mode_group->num_crtcs;
  1748.                 connector_count = mode_group->num_connectors;
  1749.                 encoder_count = mode_group->num_encoders;
  1750.         }
  1751.  
  1752.         card_res->max_height = dev->mode_config.max_height;
  1753.         card_res->min_height = dev->mode_config.min_height;
  1754.         card_res->max_width = dev->mode_config.max_width;
  1755.         card_res->min_width = dev->mode_config.min_width;
  1756.  
  1757.         /* CRTCs */
  1758.         if (card_res->count_crtcs >= crtc_count) {
  1759.                 copied = 0;
  1760.                 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
  1761.                 if (!mode_group) {
  1762.                         list_for_each_entry(crtc, &dev->mode_config.crtc_list,
  1763.                                             head) {
  1764.                                 DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
  1765.                                 if (put_user(crtc->base.id, crtc_id + copied)) {
  1766.                                         ret = -EFAULT;
  1767.                                         goto out;
  1768.                                 }
  1769.                                 copied++;
  1770.                         }
  1771.                 } else {
  1772.                         for (i = 0; i < mode_group->num_crtcs; i++) {
  1773.                                 if (put_user(mode_group->id_list[i],
  1774.                                              crtc_id + copied)) {
  1775.                                         ret = -EFAULT;
  1776.                                         goto out;
  1777.                                 }
  1778.                                 copied++;
  1779.                         }
  1780.                 }
  1781.         }
  1782.         card_res->count_crtcs = crtc_count;
  1783.  
  1784.         /* Encoders */
  1785.         if (card_res->count_encoders >= encoder_count) {
  1786.                 copied = 0;
  1787.                 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
  1788.                 if (!mode_group) {
  1789.                         list_for_each_entry(encoder,
  1790.                                             &dev->mode_config.encoder_list,
  1791.                                             head) {
  1792.                                 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n", encoder->base.id,
  1793.                                                 encoder->name);
  1794.                                 if (put_user(encoder->base.id, encoder_id +
  1795.                                              copied)) {
  1796.                                         ret = -EFAULT;
  1797.                                         goto out;
  1798.                                 }
  1799.                                 copied++;
  1800.                         }
  1801.                 } else {
  1802.                         for (i = mode_group->num_crtcs; i < mode_group->num_crtcs + mode_group->num_encoders; i++) {
  1803.                                 if (put_user(mode_group->id_list[i],
  1804.                                              encoder_id + copied)) {
  1805.                                         ret = -EFAULT;
  1806.                                         goto out;
  1807.                                 }
  1808.                                 copied++;
  1809.                         }
  1810.  
  1811.                 }
  1812.         }
  1813.         card_res->count_encoders = encoder_count;
  1814.  
  1815.         /* Connectors */
  1816.         if (card_res->count_connectors >= connector_count) {
  1817.                 copied = 0;
  1818.                 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
  1819.                 if (!mode_group) {
  1820.                         list_for_each_entry(connector,
  1821.                                             &dev->mode_config.connector_list,
  1822.                                             head) {
  1823.                                 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  1824.                                         connector->base.id,
  1825.                                         connector->name);
  1826.                                 if (put_user(connector->base.id,
  1827.                                              connector_id + copied)) {
  1828.                                         ret = -EFAULT;
  1829.                                         goto out;
  1830.                                 }
  1831.                                 copied++;
  1832.                         }
  1833.                 } else {
  1834.                         int start = mode_group->num_crtcs +
  1835.                                 mode_group->num_encoders;
  1836.                         for (i = start; i < start + mode_group->num_connectors; i++) {
  1837.                                 if (put_user(mode_group->id_list[i],
  1838.                                              connector_id + copied)) {
  1839.                                         ret = -EFAULT;
  1840.                                         goto out;
  1841.                                 }
  1842.                                 copied++;
  1843.                         }
  1844.                 }
  1845.         }
  1846.         card_res->count_connectors = connector_count;
  1847.  
  1848.         DRM_DEBUG_KMS("CRTC[%d] CONNECTORS[%d] ENCODERS[%d]\n", card_res->count_crtcs,
  1849.                   card_res->count_connectors, card_res->count_encoders);
  1850.  
  1851. out:
  1852.         mutex_unlock(&dev->mode_config.mutex);
  1853.         return ret;
  1854. }
  1855.  
  1856. /**
  1857.  * drm_mode_getcrtc - get CRTC configuration
  1858.  * @dev: drm device for the ioctl
  1859.  * @data: data pointer for the ioctl
  1860.  * @file_priv: drm file for the ioctl call
  1861.  *
  1862.  * Construct a CRTC configuration structure to return to the user.
  1863.  *
  1864.  * Called by the user via ioctl.
  1865.  *
  1866.  * Returns:
  1867.  * Zero on success, negative errno on failure.
  1868.  */
  1869. int drm_mode_getcrtc(struct drm_device *dev,
  1870.                      void *data, struct drm_file *file_priv)
  1871. {
  1872.         struct drm_mode_crtc *crtc_resp = data;
  1873.         struct drm_crtc *crtc;
  1874.  
  1875.         if (!drm_core_check_feature(dev, DRIVER_MODESET))
  1876.                 return -EINVAL;
  1877.  
  1878.         crtc = drm_crtc_find(dev, crtc_resp->crtc_id);
  1879.         if (!crtc)
  1880.                 return -ENOENT;
  1881.  
  1882.         drm_modeset_lock_crtc(crtc, crtc->primary);
  1883.         crtc_resp->x = crtc->x;
  1884.         crtc_resp->y = crtc->y;
  1885.         crtc_resp->gamma_size = crtc->gamma_size;
  1886.         if (crtc->primary->fb)
  1887.                 crtc_resp->fb_id = crtc->primary->fb->base.id;
  1888.         else
  1889.                 crtc_resp->fb_id = 0;
  1890.  
  1891.         if (crtc->enabled) {
  1892.  
  1893.                 drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
  1894.                 crtc_resp->mode_valid = 1;
  1895.  
  1896.         } else {
  1897.                 crtc_resp->mode_valid = 0;
  1898.         }
  1899.         drm_modeset_unlock_crtc(crtc);
  1900.  
  1901.         return 0;
  1902. }
  1903.  
  1904. static bool drm_mode_expose_to_userspace(const struct drm_display_mode *mode,
  1905.                                          const struct drm_file *file_priv)
  1906. {
  1907.         /*
  1908.          * If user-space hasn't configured the driver to expose the stereo 3D
  1909.          * modes, don't expose them.
  1910.          */
  1911.         if (!file_priv->stereo_allowed && drm_mode_is_stereo(mode))
  1912.                 return false;
  1913.  
  1914.         return true;
  1915. }
  1916.  
  1917. static struct drm_encoder *drm_connector_get_encoder(struct drm_connector *connector)
  1918. {
  1919.         /* For atomic drivers only state objects are synchronously updated and
  1920.          * protected by modeset locks, so check those first. */
  1921.         if (connector->state)
  1922.                 return connector->state->best_encoder;
  1923.         return connector->encoder;
  1924. }
  1925.  
  1926. /**
  1927.  * drm_mode_getconnector - get connector configuration
  1928.  * @dev: drm device for the ioctl
  1929.  * @data: data pointer for the ioctl
  1930.  * @file_priv: drm file for the ioctl call
  1931.  *
  1932.  * Construct a connector configuration structure to return to the user.
  1933.  *
  1934.  * Called by the user via ioctl.
  1935.  *
  1936.  * Returns:
  1937.  * Zero on success, negative errno on failure.
  1938.  */
  1939. int drm_mode_getconnector(struct drm_device *dev, void *data,
  1940.                           struct drm_file *file_priv)
  1941. {
  1942.         struct drm_mode_get_connector *out_resp = data;
  1943.         struct drm_connector *connector;
  1944.         struct drm_encoder *encoder;
  1945.         struct drm_display_mode *mode;
  1946.         int mode_count = 0;
  1947.         int props_count = 0;
  1948.         int encoders_count = 0;
  1949.         int ret = 0;
  1950.         int copied = 0;
  1951.         int i;
  1952.         struct drm_mode_modeinfo u_mode;
  1953.         struct drm_mode_modeinfo __user *mode_ptr;
  1954.         uint32_t __user *prop_ptr;
  1955.         uint64_t __user *prop_values;
  1956.         uint32_t __user *encoder_ptr;
  1957.  
  1958.         if (!drm_core_check_feature(dev, DRIVER_MODESET))
  1959.                 return -EINVAL;
  1960.  
  1961.         memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
  1962.  
  1963.         DRM_DEBUG_KMS("[CONNECTOR:%d:?]\n", out_resp->connector_id);
  1964.  
  1965.         mutex_lock(&dev->mode_config.mutex);
  1966.  
  1967.         connector = drm_connector_find(dev, out_resp->connector_id);
  1968.         if (!connector) {
  1969.                 ret = -ENOENT;
  1970.                 goto out;
  1971.         }
  1972.  
  1973.         props_count = connector->properties.count;
  1974.  
  1975.         for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
  1976.                 if (connector->encoder_ids[i] != 0) {
  1977.                         encoders_count++;
  1978.                 }
  1979.         }
  1980.  
  1981.         if (out_resp->count_modes == 0) {
  1982.                 connector->funcs->fill_modes(connector,
  1983.                                              dev->mode_config.max_width,
  1984.                                              dev->mode_config.max_height);
  1985.         }
  1986.  
  1987.         /* delayed so we get modes regardless of pre-fill_modes state */
  1988.         list_for_each_entry(mode, &connector->modes, head)
  1989.                 if (drm_mode_expose_to_userspace(mode, file_priv))
  1990.                 mode_count++;
  1991.  
  1992.         out_resp->connector_id = connector->base.id;
  1993.         out_resp->connector_type = connector->connector_type;
  1994.         out_resp->connector_type_id = connector->connector_type_id;
  1995.         out_resp->mm_width = connector->display_info.width_mm;
  1996.         out_resp->mm_height = connector->display_info.height_mm;
  1997.         out_resp->subpixel = connector->display_info.subpixel_order;
  1998.         out_resp->connection = connector->status;
  1999.         drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
  2000.  
  2001.         encoder = drm_connector_get_encoder(connector);
  2002.         if (encoder)
  2003.                 out_resp->encoder_id = encoder->base.id;
  2004.         else
  2005.                 out_resp->encoder_id = 0;
  2006.         drm_modeset_unlock(&dev->mode_config.connection_mutex);
  2007.  
  2008.         /*
  2009.          * This ioctl is called twice, once to determine how much space is
  2010.          * needed, and the 2nd time to fill it.
  2011.          */
  2012.         if ((out_resp->count_modes >= mode_count) && mode_count) {
  2013.                 copied = 0;
  2014.                 mode_ptr = (struct drm_mode_modeinfo __user *)(unsigned long)out_resp->modes_ptr;
  2015.                 list_for_each_entry(mode, &connector->modes, head) {
  2016.                         if (!drm_mode_expose_to_userspace(mode, file_priv))
  2017.                                 continue;
  2018.  
  2019.                         drm_crtc_convert_to_umode(&u_mode, mode);
  2020.                         if (copy_to_user(mode_ptr + copied,
  2021.                                          &u_mode, sizeof(u_mode))) {
  2022.                                 ret = -EFAULT;
  2023.                                 goto out;
  2024.                         }
  2025.                         copied++;
  2026.                 }
  2027.         }
  2028.         out_resp->count_modes = mode_count;
  2029.  
  2030.         if ((out_resp->count_props >= props_count) && props_count) {
  2031.                 copied = 0;
  2032.                 prop_ptr = (uint32_t __user *)(unsigned long)(out_resp->props_ptr);
  2033.                 prop_values = (uint64_t __user *)(unsigned long)(out_resp->prop_values_ptr);
  2034.                 for (i = 0; i < connector->properties.count; i++) {
  2035.                         if (put_user(connector->properties.ids[i],
  2036.                                              prop_ptr + copied)) {
  2037.                                         ret = -EFAULT;
  2038.                                         goto out;
  2039.                                 }
  2040.  
  2041.                         if (put_user(connector->properties.values[i],
  2042.                                              prop_values + copied)) {
  2043.                                         ret = -EFAULT;
  2044.                                         goto out;
  2045.                                 }
  2046.                                 copied++;
  2047.                         }
  2048.                 }
  2049.         out_resp->count_props = props_count;
  2050.  
  2051.         if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
  2052.                 copied = 0;
  2053.                 encoder_ptr = (uint32_t __user *)(unsigned long)(out_resp->encoders_ptr);
  2054.                 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
  2055.                         if (connector->encoder_ids[i] != 0) {
  2056.                                 if (put_user(connector->encoder_ids[i],
  2057.                                              encoder_ptr + copied)) {
  2058.                                         ret = -EFAULT;
  2059.                                         goto out;
  2060.                                 }
  2061.                                 copied++;
  2062.                         }
  2063.                 }
  2064.         }
  2065.         out_resp->count_encoders = encoders_count;
  2066.  
  2067. out:
  2068.         mutex_unlock(&dev->mode_config.mutex);
  2069.  
  2070.         return ret;
  2071. }
  2072.  
  2073. static struct drm_crtc *drm_encoder_get_crtc(struct drm_encoder *encoder)
  2074. {
  2075.         struct drm_connector *connector;
  2076.         struct drm_device *dev = encoder->dev;
  2077.         bool uses_atomic = false;
  2078.  
  2079.         /* For atomic drivers only state objects are synchronously updated and
  2080.          * protected by modeset locks, so check those first. */
  2081.         list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  2082.                 if (!connector->state)
  2083.                         continue;
  2084.  
  2085.                 uses_atomic = true;
  2086.  
  2087.                 if (connector->state->best_encoder != encoder)
  2088.                         continue;
  2089.  
  2090.                 return connector->state->crtc;
  2091.         }
  2092.  
  2093.         /* Don't return stale data (e.g. pending async disable). */
  2094.         if (uses_atomic)
  2095.                 return NULL;
  2096.  
  2097.         return encoder->crtc;
  2098. }
  2099.  
  2100. /**
  2101.  * drm_mode_getencoder - get encoder configuration
  2102.  * @dev: drm device for the ioctl
  2103.  * @data: data pointer for the ioctl
  2104.  * @file_priv: drm file for the ioctl call
  2105.  *
  2106.  * Construct a encoder configuration structure to return to the user.
  2107.  *
  2108.  * Called by the user via ioctl.
  2109.  *
  2110.  * Returns:
  2111.  * Zero on success, negative errno on failure.
  2112.  */
  2113. int drm_mode_getencoder(struct drm_device *dev, void *data,
  2114.                         struct drm_file *file_priv)
  2115. {
  2116.         struct drm_mode_get_encoder *enc_resp = data;
  2117.         struct drm_encoder *encoder;
  2118.         struct drm_crtc *crtc;
  2119.  
  2120.         if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2121.                 return -EINVAL;
  2122.  
  2123.         encoder = drm_encoder_find(dev, enc_resp->encoder_id);
  2124.         if (!encoder)
  2125.                 return -ENOENT;
  2126.  
  2127.         drm_modeset_lock(&dev->mode_config.connection_mutex, NULL);
  2128.         crtc = drm_encoder_get_crtc(encoder);
  2129.         if (crtc)
  2130.                 enc_resp->crtc_id = crtc->base.id;
  2131.         else if (encoder->crtc)
  2132.                 enc_resp->crtc_id = encoder->crtc->base.id;
  2133.         else
  2134.                 enc_resp->crtc_id = 0;
  2135.         drm_modeset_unlock(&dev->mode_config.connection_mutex);
  2136.  
  2137.         enc_resp->encoder_type = encoder->encoder_type;
  2138.         enc_resp->encoder_id = encoder->base.id;
  2139.         enc_resp->possible_crtcs = encoder->possible_crtcs;
  2140.         enc_resp->possible_clones = encoder->possible_clones;
  2141.  
  2142.         return 0;
  2143. }
  2144.  
  2145. /**
  2146.  * drm_mode_getplane_res - enumerate all plane resources
  2147.  * @dev: DRM device
  2148.  * @data: ioctl data
  2149.  * @file_priv: DRM file info
  2150.  *
  2151.  * Construct a list of plane ids to return to the user.
  2152.  *
  2153.  * Called by the user via ioctl.
  2154.  *
  2155.  * Returns:
  2156.  * Zero on success, negative errno on failure.
  2157.  */
  2158. int drm_mode_getplane_res(struct drm_device *dev, void *data,
  2159.                             struct drm_file *file_priv)
  2160. {
  2161.         struct drm_mode_get_plane_res *plane_resp = data;
  2162.         struct drm_mode_config *config;
  2163.         struct drm_plane *plane;
  2164.         uint32_t __user *plane_ptr;
  2165.         int copied = 0;
  2166.         unsigned num_planes;
  2167.  
  2168.         if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2169.                 return -EINVAL;
  2170.  
  2171.         config = &dev->mode_config;
  2172.  
  2173.         if (file_priv->universal_planes)
  2174.                 num_planes = config->num_total_plane;
  2175.         else
  2176.                 num_planes = config->num_overlay_plane;
  2177.  
  2178.         /*
  2179.          * This ioctl is called twice, once to determine how much space is
  2180.          * needed, and the 2nd time to fill it.
  2181.          */
  2182.         if (num_planes &&
  2183.             (plane_resp->count_planes >= num_planes)) {
  2184.                 plane_ptr = (uint32_t __user *)(unsigned long)plane_resp->plane_id_ptr;
  2185.  
  2186.                 /* Plane lists are invariant, no locking needed. */
  2187.                 list_for_each_entry(plane, &config->plane_list, head) {
  2188.                         /*
  2189.                          * Unless userspace set the 'universal planes'
  2190.                          * capability bit, only advertise overlays.
  2191.                          */
  2192.                         if (plane->type != DRM_PLANE_TYPE_OVERLAY &&
  2193.                             !file_priv->universal_planes)
  2194.                                 continue;
  2195.  
  2196.                         if (put_user(plane->base.id, plane_ptr + copied))
  2197.                                 return -EFAULT;
  2198.                         copied++;
  2199.                 }
  2200.         }
  2201.         plane_resp->count_planes = num_planes;
  2202.  
  2203.         return 0;
  2204. }
  2205.  
  2206. /**
  2207.  * drm_mode_getplane - get plane configuration
  2208.  * @dev: DRM device
  2209.  * @data: ioctl data
  2210.  * @file_priv: DRM file info
  2211.  *
  2212.  * Construct a plane configuration structure to return to the user.
  2213.  *
  2214.  * Called by the user via ioctl.
  2215.  *
  2216.  * Returns:
  2217.  * Zero on success, negative errno on failure.
  2218.  */
  2219. int drm_mode_getplane(struct drm_device *dev, void *data,
  2220.                         struct drm_file *file_priv)
  2221. {
  2222.         struct drm_mode_get_plane *plane_resp = data;
  2223.         struct drm_plane *plane;
  2224.         uint32_t __user *format_ptr;
  2225.  
  2226.         if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2227.                 return -EINVAL;
  2228.  
  2229.         plane = drm_plane_find(dev, plane_resp->plane_id);
  2230.         if (!plane)
  2231.                 return -ENOENT;
  2232.  
  2233.         drm_modeset_lock(&plane->mutex, NULL);
  2234.         if (plane->crtc)
  2235.                 plane_resp->crtc_id = plane->crtc->base.id;
  2236.         else
  2237.                 plane_resp->crtc_id = 0;
  2238.  
  2239.         if (plane->fb)
  2240.                 plane_resp->fb_id = plane->fb->base.id;
  2241.         else
  2242.                 plane_resp->fb_id = 0;
  2243.         drm_modeset_unlock(&plane->mutex);
  2244.  
  2245.         plane_resp->plane_id = plane->base.id;
  2246.         plane_resp->possible_crtcs = plane->possible_crtcs;
  2247.         plane_resp->gamma_size = 0;
  2248.  
  2249.         /*
  2250.          * This ioctl is called twice, once to determine how much space is
  2251.          * needed, and the 2nd time to fill it.
  2252.          */
  2253.         if (plane->format_count &&
  2254.             (plane_resp->count_format_types >= plane->format_count)) {
  2255.                 format_ptr = (uint32_t __user *)(unsigned long)plane_resp->format_type_ptr;
  2256.                 if (copy_to_user(format_ptr,
  2257.                                  plane->format_types,
  2258.                                  sizeof(uint32_t) * plane->format_count)) {
  2259.                         return -EFAULT;
  2260.                 }
  2261.         }
  2262.         plane_resp->count_format_types = plane->format_count;
  2263.  
  2264.         return 0;
  2265. }
  2266.  
  2267. /*
  2268.  * setplane_internal - setplane handler for internal callers
  2269.  *
  2270.  * Note that we assume an extra reference has already been taken on fb.  If the
  2271.  * update fails, this reference will be dropped before return; if it succeeds,
  2272.  * the previous framebuffer (if any) will be unreferenced instead.
  2273.  *
  2274.  * src_{x,y,w,h} are provided in 16.16 fixed point format
  2275.  */
  2276. static int __setplane_internal(struct drm_plane *plane,
  2277.                              struct drm_crtc *crtc,
  2278.                              struct drm_framebuffer *fb,
  2279.                              int32_t crtc_x, int32_t crtc_y,
  2280.                              uint32_t crtc_w, uint32_t crtc_h,
  2281.                              /* src_{x,y,w,h} values are 16.16 fixed point */
  2282.                              uint32_t src_x, uint32_t src_y,
  2283.                              uint32_t src_w, uint32_t src_h)
  2284. {
  2285.         int ret = 0;
  2286.         unsigned int fb_width, fb_height;
  2287.         unsigned int i;
  2288.  
  2289.         /* No fb means shut it down */
  2290.         if (!fb) {
  2291.                 plane->old_fb = plane->fb;
  2292.                 ret = plane->funcs->disable_plane(plane);
  2293.                 if (!ret) {
  2294.                 plane->crtc = NULL;
  2295.                 plane->fb = NULL;
  2296.                 } else {
  2297.                         plane->old_fb = NULL;
  2298.                 }
  2299.                 goto out;
  2300.         }
  2301.  
  2302.         /* Check whether this plane is usable on this CRTC */
  2303.         if (!(plane->possible_crtcs & drm_crtc_mask(crtc))) {
  2304.                 DRM_DEBUG_KMS("Invalid crtc for plane\n");
  2305.                 ret = -EINVAL;
  2306.                 goto out;
  2307.         }
  2308.  
  2309.         /* Check whether this plane supports the fb pixel format. */
  2310.         for (i = 0; i < plane->format_count; i++)
  2311.                 if (fb->pixel_format == plane->format_types[i])
  2312.                         break;
  2313.         if (i == plane->format_count) {
  2314.                 DRM_DEBUG_KMS("Invalid pixel format %s\n",
  2315.                               drm_get_format_name(fb->pixel_format));
  2316.                 ret = -EINVAL;
  2317.                 goto out;
  2318.         }
  2319.  
  2320.         fb_width = fb->width << 16;
  2321.         fb_height = fb->height << 16;
  2322.  
  2323.         /* Make sure source coordinates are inside the fb. */
  2324.         if (src_w > fb_width ||
  2325.             src_x > fb_width - src_w ||
  2326.             src_h > fb_height ||
  2327.             src_y > fb_height - src_h) {
  2328.                 DRM_DEBUG_KMS("Invalid source coordinates "
  2329.                               "%u.%06ux%u.%06u+%u.%06u+%u.%06u\n",
  2330.                               src_w >> 16, ((src_w & 0xffff) * 15625) >> 10,
  2331.                               src_h >> 16, ((src_h & 0xffff) * 15625) >> 10,
  2332.                               src_x >> 16, ((src_x & 0xffff) * 15625) >> 10,
  2333.                               src_y >> 16, ((src_y & 0xffff) * 15625) >> 10);
  2334.                 ret = -ENOSPC;
  2335.                 goto out;
  2336.         }
  2337.  
  2338.         plane->old_fb = plane->fb;
  2339.         ret = plane->funcs->update_plane(plane, crtc, fb,
  2340.                                          crtc_x, crtc_y, crtc_w, crtc_h,
  2341.                                          src_x, src_y, src_w, src_h);
  2342.         if (!ret) {
  2343.                 plane->crtc = crtc;
  2344.                 plane->fb = fb;
  2345.                 fb = NULL;
  2346.         } else {
  2347.                 plane->old_fb = NULL;
  2348.         }
  2349.  
  2350. out:
  2351.         if (fb)
  2352.                 drm_framebuffer_unreference(fb);
  2353.         if (plane->old_fb)
  2354.                 drm_framebuffer_unreference(plane->old_fb);
  2355.         plane->old_fb = NULL;
  2356.  
  2357.         return ret;
  2358. }
  2359.  
  2360. static int setplane_internal(struct drm_plane *plane,
  2361.                              struct drm_crtc *crtc,
  2362.                              struct drm_framebuffer *fb,
  2363.                              int32_t crtc_x, int32_t crtc_y,
  2364.                              uint32_t crtc_w, uint32_t crtc_h,
  2365.                              /* src_{x,y,w,h} values are 16.16 fixed point */
  2366.                              uint32_t src_x, uint32_t src_y,
  2367.                              uint32_t src_w, uint32_t src_h)
  2368. {
  2369.         int ret;
  2370.  
  2371.         drm_modeset_lock_all(plane->dev);
  2372.         ret = __setplane_internal(plane, crtc, fb,
  2373.                                   crtc_x, crtc_y, crtc_w, crtc_h,
  2374.                                   src_x, src_y, src_w, src_h);
  2375.         drm_modeset_unlock_all(plane->dev);
  2376.  
  2377.         return ret;
  2378. }
  2379.  
  2380. /**
  2381.  * drm_mode_setplane - configure a plane's configuration
  2382.  * @dev: DRM device
  2383.  * @data: ioctl data*
  2384.  * @file_priv: DRM file info
  2385.  *
  2386.  * Set plane configuration, including placement, fb, scaling, and other factors.
  2387.  * Or pass a NULL fb to disable (planes may be disabled without providing a
  2388.  * valid crtc).
  2389.  *
  2390.  * Returns:
  2391.  * Zero on success, negative errno on failure.
  2392.  */
  2393. int drm_mode_setplane(struct drm_device *dev, void *data,
  2394.                       struct drm_file *file_priv)
  2395. {
  2396.         struct drm_mode_set_plane *plane_req = data;
  2397.         struct drm_plane *plane;
  2398.         struct drm_crtc *crtc = NULL;
  2399.         struct drm_framebuffer *fb = NULL;
  2400.  
  2401.         if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2402.                 return -EINVAL;
  2403.  
  2404.         /* Give drivers some help against integer overflows */
  2405.         if (plane_req->crtc_w > INT_MAX ||
  2406.             plane_req->crtc_x > INT_MAX - (int32_t) plane_req->crtc_w ||
  2407.             plane_req->crtc_h > INT_MAX ||
  2408.             plane_req->crtc_y > INT_MAX - (int32_t) plane_req->crtc_h) {
  2409.                 DRM_DEBUG_KMS("Invalid CRTC coordinates %ux%u+%d+%d\n",
  2410.                               plane_req->crtc_w, plane_req->crtc_h,
  2411.                               plane_req->crtc_x, plane_req->crtc_y);
  2412.                 return -ERANGE;
  2413.         }
  2414.  
  2415.         /*
  2416.          * First, find the plane, crtc, and fb objects.  If not available,
  2417.          * we don't bother to call the driver.
  2418.          */
  2419.         plane = drm_plane_find(dev, plane_req->plane_id);
  2420.         if (!plane) {
  2421.                 DRM_DEBUG_KMS("Unknown plane ID %d\n",
  2422.                               plane_req->plane_id);
  2423.                 return -ENOENT;
  2424.         }
  2425.  
  2426.         if (plane_req->fb_id) {
  2427.                 fb = drm_framebuffer_lookup(dev, plane_req->fb_id);
  2428.                 if (!fb) {
  2429.                         DRM_DEBUG_KMS("Unknown framebuffer ID %d\n",
  2430.                                       plane_req->fb_id);
  2431.                         return -ENOENT;
  2432.                 }
  2433.  
  2434.                 crtc = drm_crtc_find(dev, plane_req->crtc_id);
  2435.                 if (!crtc) {
  2436.                         DRM_DEBUG_KMS("Unknown crtc ID %d\n",
  2437.                                       plane_req->crtc_id);
  2438.                         return -ENOENT;
  2439.                 }
  2440.         }
  2441.  
  2442.         /*
  2443.          * setplane_internal will take care of deref'ing either the old or new
  2444.          * framebuffer depending on success.
  2445.          */
  2446.         return setplane_internal(plane, crtc, fb,
  2447.                                  plane_req->crtc_x, plane_req->crtc_y,
  2448.                                  plane_req->crtc_w, plane_req->crtc_h,
  2449.                                  plane_req->src_x, plane_req->src_y,
  2450.                                  plane_req->src_w, plane_req->src_h);
  2451. }
  2452. #endif
  2453.  
  2454. /**
  2455.  * drm_mode_set_config_internal - helper to call ->set_config
  2456.  * @set: modeset config to set
  2457.  *
  2458.  * This is a little helper to wrap internal calls to the ->set_config driver
  2459.  * interface. The only thing it adds is correct refcounting dance.
  2460.  *
  2461.  * Returns:
  2462.  * Zero on success, negative errno on failure.
  2463.  */
  2464. int drm_mode_set_config_internal(struct drm_mode_set *set)
  2465. {
  2466.         struct drm_crtc *crtc = set->crtc;
  2467.         struct drm_framebuffer *fb;
  2468.         struct drm_crtc *tmp;
  2469.         int ret;
  2470.  
  2471.         /*
  2472.          * NOTE: ->set_config can also disable other crtcs (if we steal all
  2473.          * connectors from it), hence we need to refcount the fbs across all
  2474.          * crtcs. Atomic modeset will have saner semantics ...
  2475.          */
  2476.         list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head)
  2477.                 tmp->primary->old_fb = tmp->primary->fb;
  2478.  
  2479.         fb = set->fb;
  2480.  
  2481.         ret = crtc->funcs->set_config(set);
  2482.         if (ret == 0) {
  2483.                 crtc->primary->crtc = crtc;
  2484.                 crtc->primary->fb = fb;
  2485.         }
  2486.  
  2487.         list_for_each_entry(tmp, &crtc->dev->mode_config.crtc_list, head) {
  2488.                 if (tmp->primary->fb)
  2489.                         drm_framebuffer_reference(tmp->primary->fb);
  2490. //              if (tmp->old_fb)
  2491. //                      drm_framebuffer_unreference(tmp->old_fb);
  2492.         }
  2493.  
  2494.         return ret;
  2495. }
  2496. EXPORT_SYMBOL(drm_mode_set_config_internal);
  2497.  
  2498. #if 0
  2499. /**
  2500.  * drm_crtc_check_viewport - Checks that a framebuffer is big enough for the
  2501.  *     CRTC viewport
  2502.  * @crtc: CRTC that framebuffer will be displayed on
  2503.  * @x: x panning
  2504.  * @y: y panning
  2505.  * @mode: mode that framebuffer will be displayed under
  2506.  * @fb: framebuffer to check size of
  2507.  */
  2508. int drm_crtc_check_viewport(const struct drm_crtc *crtc,
  2509.                                    int x, int y,
  2510.                                    const struct drm_display_mode *mode,
  2511.                                    const struct drm_framebuffer *fb)
  2512.  
  2513. {
  2514.         int hdisplay, vdisplay;
  2515.  
  2516.         hdisplay = mode->hdisplay;
  2517.         vdisplay = mode->vdisplay;
  2518.  
  2519.         if (drm_mode_is_stereo(mode)) {
  2520.                 struct drm_display_mode adjusted = *mode;
  2521.  
  2522.                 drm_mode_set_crtcinfo(&adjusted, CRTC_STEREO_DOUBLE);
  2523.                 hdisplay = adjusted.crtc_hdisplay;
  2524.                 vdisplay = adjusted.crtc_vdisplay;
  2525.         }
  2526.  
  2527.         if (crtc->invert_dimensions)
  2528.                 swap(hdisplay, vdisplay);
  2529.  
  2530.         if (hdisplay > fb->width ||
  2531.             vdisplay > fb->height ||
  2532.             x > fb->width - hdisplay ||
  2533.             y > fb->height - vdisplay) {
  2534.                 DRM_DEBUG_KMS("Invalid fb size %ux%u for CRTC viewport %ux%u+%d+%d%s.\n",
  2535.                               fb->width, fb->height, hdisplay, vdisplay, x, y,
  2536.                               crtc->invert_dimensions ? " (inverted)" : "");
  2537.                 return -ENOSPC;
  2538.         }
  2539.  
  2540.         return 0;
  2541. }
  2542. EXPORT_SYMBOL(drm_crtc_check_viewport);
  2543.  
  2544. /**
  2545.  * drm_mode_setcrtc - set CRTC configuration
  2546.  * @dev: drm device for the ioctl
  2547.  * @data: data pointer for the ioctl
  2548.  * @file_priv: drm file for the ioctl call
  2549.  *
  2550.  * Build a new CRTC configuration based on user request.
  2551.  *
  2552.  * Called by the user via ioctl.
  2553.  *
  2554.  * Returns:
  2555.  * Zero on success, negative errno on failure.
  2556.  */
  2557. int drm_mode_setcrtc(struct drm_device *dev, void *data,
  2558.                      struct drm_file *file_priv)
  2559. {
  2560.         struct drm_mode_config *config = &dev->mode_config;
  2561.         struct drm_mode_crtc *crtc_req = data;
  2562.         struct drm_crtc *crtc;
  2563.         struct drm_connector **connector_set = NULL, *connector;
  2564.         struct drm_framebuffer *fb = NULL;
  2565.         struct drm_display_mode *mode = NULL;
  2566.         struct drm_mode_set set;
  2567.         uint32_t __user *set_connectors_ptr;
  2568.         int ret;
  2569.         int i;
  2570.  
  2571.         if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2572.                 return -EINVAL;
  2573.  
  2574.         /* For some reason crtc x/y offsets are signed internally. */
  2575.         if (crtc_req->x > INT_MAX || crtc_req->y > INT_MAX)
  2576.                 return -ERANGE;
  2577.  
  2578.         drm_modeset_lock_all(dev);
  2579.         crtc = drm_crtc_find(dev, crtc_req->crtc_id);
  2580.         if (!crtc) {
  2581.                 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
  2582.                 ret = -ENOENT;
  2583.                 goto out;
  2584.         }
  2585.         DRM_DEBUG_KMS("[CRTC:%d]\n", crtc->base.id);
  2586.  
  2587.         if (crtc_req->mode_valid) {
  2588.                 /* If we have a mode we need a framebuffer. */
  2589.                 /* If we pass -1, set the mode with the currently bound fb */
  2590.                 if (crtc_req->fb_id == -1) {
  2591.                         if (!crtc->primary->fb) {
  2592.                                 DRM_DEBUG_KMS("CRTC doesn't have current FB\n");
  2593.                                 ret = -EINVAL;
  2594.                                 goto out;
  2595.                         }
  2596.                         fb = crtc->primary->fb;
  2597.                         /* Make refcounting symmetric with the lookup path. */
  2598.                         drm_framebuffer_reference(fb);
  2599.                 } else {
  2600.                         fb = drm_framebuffer_lookup(dev, crtc_req->fb_id);
  2601.                         if (!fb) {
  2602.                                 DRM_DEBUG_KMS("Unknown FB ID%d\n",
  2603.                                                 crtc_req->fb_id);
  2604.                                 ret = -ENOENT;
  2605.                                 goto out;
  2606.                         }
  2607.                 }
  2608.  
  2609.                 mode = drm_mode_create(dev);
  2610.                 if (!mode) {
  2611.                         ret = -ENOMEM;
  2612.                         goto out;
  2613.                 }
  2614.  
  2615.                 ret = drm_crtc_convert_umode(mode, &crtc_req->mode);
  2616.                 if (ret) {
  2617.                         DRM_DEBUG_KMS("Invalid mode\n");
  2618.                         goto out;
  2619.                 }
  2620.  
  2621.                 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
  2622.  
  2623.                 ret = drm_crtc_check_viewport(crtc, crtc_req->x, crtc_req->y,
  2624.                                               mode, fb);
  2625.                 if (ret)
  2626.                         goto out;
  2627.  
  2628.         }
  2629.  
  2630.         if (crtc_req->count_connectors == 0 && mode) {
  2631.                 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
  2632.                 ret = -EINVAL;
  2633.                 goto out;
  2634.         }
  2635.  
  2636.         if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
  2637.                 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
  2638.                           crtc_req->count_connectors);
  2639.                 ret = -EINVAL;
  2640.                 goto out;
  2641.         }
  2642.  
  2643.         if (crtc_req->count_connectors > 0) {
  2644.                 u32 out_id;
  2645.  
  2646.                 /* Avoid unbounded kernel memory allocation */
  2647.                 if (crtc_req->count_connectors > config->num_connector) {
  2648.                         ret = -EINVAL;
  2649.                         goto out;
  2650.                 }
  2651.  
  2652.                 connector_set = kmalloc(crtc_req->count_connectors *
  2653.                                         sizeof(struct drm_connector *),
  2654.                                         GFP_KERNEL);
  2655.                 if (!connector_set) {
  2656.                         ret = -ENOMEM;
  2657.                         goto out;
  2658.                 }
  2659.  
  2660.                 for (i = 0; i < crtc_req->count_connectors; i++) {
  2661.                         set_connectors_ptr = (uint32_t __user *)(unsigned long)crtc_req->set_connectors_ptr;
  2662.                         if (get_user(out_id, &set_connectors_ptr[i])) {
  2663.                                 ret = -EFAULT;
  2664.                                 goto out;
  2665.                         }
  2666.  
  2667.                         connector = drm_connector_find(dev, out_id);
  2668.                         if (!connector) {
  2669.                                 DRM_DEBUG_KMS("Connector id %d unknown\n",
  2670.                                                 out_id);
  2671.                                 ret = -ENOENT;
  2672.                                 goto out;
  2673.                         }
  2674.                         DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
  2675.                                         connector->base.id,
  2676.                                         connector->name);
  2677.  
  2678.                         connector_set[i] = connector;
  2679.                 }
  2680.         }
  2681.  
  2682.         set.crtc = crtc;
  2683.         set.x = crtc_req->x;
  2684.         set.y = crtc_req->y;
  2685.         set.mode = mode;
  2686.         set.connectors = connector_set;
  2687.         set.num_connectors = crtc_req->count_connectors;
  2688.         set.fb = fb;
  2689.         ret = drm_mode_set_config_internal(&set);
  2690.  
  2691. out:
  2692.         if (fb)
  2693.                 drm_framebuffer_unreference(fb);
  2694.  
  2695.         kfree(connector_set);
  2696.         drm_mode_destroy(dev, mode);
  2697.         drm_modeset_unlock_all(dev);
  2698.         return ret;
  2699. }
  2700.  
  2701. static int drm_mode_cursor_common(struct drm_device *dev,
  2702.                                   struct drm_mode_cursor2 *req,
  2703.                                   struct drm_file *file_priv)
  2704. {
  2705.         struct drm_crtc *crtc;
  2706.         int ret = 0;
  2707.  
  2708.         if (!drm_core_check_feature(dev, DRIVER_MODESET))
  2709.                 return -EINVAL;
  2710.  
  2711.         if (!req->flags || (~DRM_MODE_CURSOR_FLAGS & req->flags))
  2712.                 return -EINVAL;
  2713.  
  2714.         crtc = drm_crtc_find(dev, req->crtc_id);
  2715.         if (!crtc) {
  2716.                 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
  2717.                 return -ENOENT;
  2718.         }
  2719.  
  2720.         /*
  2721.          * If this crtc has a universal cursor plane, call that plane's update
  2722.          * handler rather than using legacy cursor handlers.
  2723.          */
  2724.         drm_modeset_lock_crtc(crtc, crtc->cursor);
  2725.         if (crtc->cursor) {
  2726.                 ret = drm_mode_cursor_universal(crtc, req, file_priv);
  2727.                 goto out;
  2728.         }
  2729.  
  2730.         if (req->flags & DRM_MODE_CURSOR_BO) {
  2731.                 if (!crtc->funcs->cursor_set && !crtc->funcs->cursor_set2) {
  2732.                         ret = -ENXIO;
  2733.                         goto out;
  2734.                 }
  2735.                 /* Turns off the cursor if handle is 0 */
  2736.                 if (crtc->funcs->cursor_set2)
  2737.                         ret = crtc->funcs->cursor_set2(crtc, file_priv, req->handle,
  2738.                                                       req->width, req->height, req->hot_x, req->hot_y);
  2739.                 else
  2740.                 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
  2741.                                               req->width, req->height);
  2742.         }
  2743.  
  2744.         if (req->flags & DRM_MODE_CURSOR_MOVE) {
  2745.                 if (crtc->funcs->cursor_move) {
  2746.                         ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
  2747.                 } else {
  2748.                         ret = -EFAULT;
  2749.                         goto out;
  2750.                 }
  2751.         }
  2752. out:
  2753.         drm_modeset_unlock_crtc(crtc);
  2754.  
  2755.         return ret;
  2756.  
  2757. }
  2758.  
  2759.  
  2760. /**
  2761.  * drm_mode_cursor_ioctl - set CRTC's cursor configuration
  2762.  * @dev: drm device for the ioctl
  2763.  * @data: data pointer for the ioctl
  2764.  * @file_priv: drm file for the ioctl call
  2765.  *
  2766.  * Set the cursor configuration based on user request.
  2767.  *
  2768.  * Called by the user via ioctl.
  2769.  *
  2770.  * Returns:
  2771.  * Zero on success, negative errno on failure.
  2772.  */
  2773. int drm_mode_cursor_ioctl(struct drm_device *dev,
  2774.                         void *data, struct drm_file *file_priv)
  2775. {
  2776.         struct drm_mode_cursor *req = data;
  2777.         struct drm_mode_cursor2 new_req;
  2778.  
  2779.         memcpy(&new_req, req, sizeof(struct drm_mode_cursor));
  2780.         new_req.hot_x = new_req.hot_y = 0;
  2781.  
  2782.         return drm_mode_cursor_common(dev, &new_req, file_priv);
  2783. }
  2784.  
  2785. /**
  2786.  * drm_mode_cursor2_ioctl - set CRTC's cursor configuration
  2787.  * @dev: drm device for the ioctl
  2788.  * @data: data pointer for the ioctl
  2789.  * @file_priv: drm file for the ioctl call
  2790.  *
  2791.  * Set the cursor configuration based on user request. This implements the 2nd
  2792.  * version of the cursor ioctl, which allows userspace to additionally specify
  2793.  * the hotspot of the pointer.
  2794.  *
  2795.  * Called by the user via ioctl.
  2796.  *
  2797.  * Returns:
  2798.  * Zero on success, negative errno on failure.
  2799.  */
  2800. int drm_mode_cursor2_ioctl(struct drm_device *dev,
  2801.                            void *data, struct drm_file *file_priv)
  2802. {
  2803.         struct drm_mode_cursor2 *req = data;
  2804.         return drm_mode_cursor_common(dev, req, file_priv);
  2805. }
  2806. #endif
  2807.  
  2808. /**
  2809.  * drm_mode_legacy_fb_format - compute drm fourcc code from legacy description
  2810.  * @bpp: bits per pixels
  2811.  * @depth: bit depth per pixel
  2812.  *
  2813.  * Computes a drm fourcc pixel format code for the given @bpp/@depth values.
  2814.  * Useful in fbdev emulation code, since that deals in those values.
  2815.  */
  2816. uint32_t drm_mode_legacy_fb_format(uint32_t bpp, uint32_t depth)
  2817. {
  2818.         uint32_t fmt;
  2819.  
  2820.         switch (bpp) {
  2821.         case 8:
  2822.                 fmt = DRM_FORMAT_C8;
  2823.                 break;
  2824.         case 16:
  2825.                 if (depth == 15)
  2826.                         fmt = DRM_FORMAT_XRGB1555;
  2827.                 else
  2828.                         fmt = DRM_FORMAT_RGB565;
  2829.                 break;
  2830.         case 24:
  2831.                 fmt = DRM_FORMAT_RGB888;
  2832.                 break;
  2833.         case 32:
  2834.                 if (depth == 24)
  2835.                         fmt = DRM_FORMAT_XRGB8888;
  2836.                 else if (depth == 30)
  2837.                         fmt = DRM_FORMAT_XRGB2101010;
  2838.                 else
  2839.                         fmt = DRM_FORMAT_ARGB8888;
  2840.                 break;
  2841.         default:
  2842.                 DRM_ERROR("bad bpp, assuming x8r8g8b8 pixel format\n");
  2843.                 fmt = DRM_FORMAT_XRGB8888;
  2844.                 break;
  2845.         }
  2846.  
  2847.         return fmt;
  2848. }
  2849. EXPORT_SYMBOL(drm_mode_legacy_fb_format);
  2850. #if 0
  2851. /**
  2852.  * drm_mode_addfb - add an FB to the graphics configuration
  2853.  * @dev: drm device for the ioctl
  2854.  * @data: data pointer for the ioctl
  2855.  * @file_priv: drm file for the ioctl call
  2856.  *
  2857.  * Add a new FB to the specified CRTC, given a user request. This is the
  2858.  * original addfb ioctl which only supported RGB formats.
  2859.  *
  2860.  * Called by the user via ioctl.
  2861.  *
  2862.  * Returns:
  2863.  * Zero on success, negative errno on failure.
  2864.  */
  2865. int drm_mode_addfb(struct drm_device *dev,
  2866.                    void *data, struct drm_file *file_priv)
  2867. {
  2868.         struct drm_mode_fb_cmd *or = data;
  2869.         struct drm_mode_fb_cmd2 r = {};
  2870.         int ret;
  2871.  
  2872.         /* convert to new format and call new ioctl */
  2873.         r.fb_id = or->fb_id;
  2874.         r.width = or->width;
  2875.         r.height = or->height;
  2876.         r.pitches[0] = or->pitch;
  2877.         r.pixel_format = drm_mode_legacy_fb_format(or->bpp, or->depth);
  2878.         r.handles[0] = or->handle;
  2879.  
  2880.         ret = drm_mode_addfb2(dev, &r, file_priv);
  2881.         if (ret)
  2882.                 return ret;
  2883.  
  2884.         or->fb_id = r.fb_id;
  2885.  
  2886.         return 0;
  2887. }
  2888.  
  2889. static int format_check(const struct drm_mode_fb_cmd2 *r)
  2890. {
  2891.         uint32_t format = r->pixel_format & ~DRM_FORMAT_BIG_ENDIAN;
  2892.  
  2893.         switch (format) {
  2894.         case DRM_FORMAT_C8:
  2895.         case DRM_FORMAT_RGB332:
  2896.         case DRM_FORMAT_BGR233:
  2897.         case DRM_FORMAT_XRGB4444:
  2898.         case DRM_FORMAT_XBGR4444:
  2899.         case DRM_FORMAT_RGBX4444:
  2900.         case DRM_FORMAT_BGRX4444:
  2901.         case DRM_FORMAT_ARGB4444:
  2902.         case DRM_FORMAT_ABGR4444:
  2903.         case DRM_FORMAT_RGBA4444:
  2904.         case DRM_FORMAT_BGRA4444:
  2905.         case DRM_FORMAT_XRGB1555:
  2906.         case DRM_FORMAT_XBGR1555:
  2907.         case DRM_FORMAT_RGBX5551:
  2908.         case DRM_FORMAT_BGRX5551:
  2909.         case DRM_FORMAT_ARGB1555:
  2910.         case DRM_FORMAT_ABGR1555:
  2911.         case DRM_FORMAT_RGBA5551:
  2912.         case DRM_FORMAT_BGRA5551:
  2913.         case DRM_FORMAT_RGB565:
  2914.         case DRM_FORMAT_BGR565:
  2915.         case DRM_FORMAT_RGB888:
  2916.         case DRM_FORMAT_BGR888:
  2917.         case DRM_FORMAT_XRGB8888:
  2918.         case DRM_FORMAT_XBGR8888:
  2919.         case DRM_FORMAT_RGBX8888:
  2920.         case DRM_FORMAT_BGRX8888:
  2921.         case DRM_FORMAT_ARGB8888:
  2922.         case DRM_FORMAT_ABGR8888:
  2923.         case DRM_FORMAT_RGBA8888:
  2924.         case DRM_FORMAT_BGRA8888:
  2925.         case DRM_FORMAT_XRGB2101010:
  2926.         case DRM_FORMAT_XBGR2101010:
  2927.         case DRM_FORMAT_RGBX1010102:
  2928.         case DRM_FORMAT_BGRX1010102: