Subversion Repositories Kolibri OS

Rev

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