Subversion Repositories Kolibri OS

Rev

Rev 1221 | Rev 1404 | 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/list.h>
  33. #include "drm.h"
  34. #include "drmP.h"
  35. #include "drm_crtc.h"
  36.  
  37. struct drm_prop_enum_list {
  38.         int type;
  39.         char *name;
  40. };
  41.  
  42. /* Avoid boilerplate.  I'm tired of typing. */
  43. #define DRM_ENUM_NAME_FN(fnname, list)                          \
  44.         char *fnname(int val)                                   \
  45.         {                                                       \
  46.                 int i;                                          \
  47.                 for (i = 0; i < ARRAY_SIZE(list); i++) {        \
  48.                         if (list[i].type == val)                \
  49.                                 return list[i].name;            \
  50.                 }                                               \
  51.                 return "(unknown)";                             \
  52.         }
  53.  
  54. /*
  55.  * Global properties
  56.  */
  57. static struct drm_prop_enum_list drm_dpms_enum_list[] =
  58. {       { DRM_MODE_DPMS_ON, "On" },
  59.         { DRM_MODE_DPMS_STANDBY, "Standby" },
  60.         { DRM_MODE_DPMS_SUSPEND, "Suspend" },
  61.         { DRM_MODE_DPMS_OFF, "Off" }
  62. };
  63.  
  64. DRM_ENUM_NAME_FN(drm_get_dpms_name, drm_dpms_enum_list)
  65.  
  66. /*
  67.  * Optional properties
  68.  */
  69. static struct drm_prop_enum_list drm_scaling_mode_enum_list[] =
  70. {
  71.         { DRM_MODE_SCALE_NONE, "None" },
  72.         { DRM_MODE_SCALE_FULLSCREEN, "Full" },
  73.         { DRM_MODE_SCALE_CENTER, "Center" },
  74.         { DRM_MODE_SCALE_ASPECT, "Full aspect" },
  75. };
  76.  
  77. static struct drm_prop_enum_list drm_dithering_mode_enum_list[] =
  78. {
  79.         { DRM_MODE_DITHERING_OFF, "Off" },
  80.         { DRM_MODE_DITHERING_ON, "On" },
  81. };
  82.  
  83. /*
  84.  * Non-global properties, but "required" for certain connectors.
  85.  */
  86. static struct drm_prop_enum_list drm_dvi_i_select_enum_list[] =
  87. {
  88.         { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
  89.         { DRM_MODE_SUBCONNECTOR_DVID,      "DVI-D"     }, /* DVI-I  */
  90.         { DRM_MODE_SUBCONNECTOR_DVIA,      "DVI-A"     }, /* DVI-I  */
  91. };
  92.  
  93. DRM_ENUM_NAME_FN(drm_get_dvi_i_select_name, drm_dvi_i_select_enum_list)
  94.  
  95. static struct drm_prop_enum_list drm_dvi_i_subconnector_enum_list[] =
  96. {
  97.         { DRM_MODE_SUBCONNECTOR_Unknown,   "Unknown"   }, /* DVI-I and TV-out */
  98.         { DRM_MODE_SUBCONNECTOR_DVID,      "DVI-D"     }, /* DVI-I  */
  99.         { DRM_MODE_SUBCONNECTOR_DVIA,      "DVI-A"     }, /* DVI-I  */
  100. };
  101.  
  102. DRM_ENUM_NAME_FN(drm_get_dvi_i_subconnector_name,
  103.                  drm_dvi_i_subconnector_enum_list)
  104.  
  105. static struct drm_prop_enum_list drm_tv_select_enum_list[] =
  106. {
  107.         { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */
  108.         { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
  109.         { DRM_MODE_SUBCONNECTOR_SVIDEO,    "SVIDEO"    }, /* TV-out */
  110.         { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
  111.         { DRM_MODE_SUBCONNECTOR_SCART,     "SCART"     }, /* TV-out */
  112. };
  113.  
  114. DRM_ENUM_NAME_FN(drm_get_tv_select_name, drm_tv_select_enum_list)
  115.  
  116. static struct drm_prop_enum_list drm_tv_subconnector_enum_list[] =
  117. {
  118.         { DRM_MODE_SUBCONNECTOR_Unknown,   "Unknown"   }, /* DVI-I and TV-out */
  119.         { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */
  120.         { DRM_MODE_SUBCONNECTOR_SVIDEO,    "SVIDEO"    }, /* TV-out */
  121.         { DRM_MODE_SUBCONNECTOR_Component, "Component" }, /* TV-out */
  122.         { DRM_MODE_SUBCONNECTOR_SCART,     "SCART"     }, /* TV-out */
  123. };
  124.  
  125. DRM_ENUM_NAME_FN(drm_get_tv_subconnector_name,
  126.                  drm_tv_subconnector_enum_list)
  127.  
  128. static struct drm_prop_enum_list drm_dirty_info_enum_list[] = {
  129.         { DRM_MODE_DIRTY_OFF,      "Off"      },
  130.         { DRM_MODE_DIRTY_ON,       "On"       },
  131.         { DRM_MODE_DIRTY_ANNOTATE, "Annotate" },
  132. };
  133.  
  134. DRM_ENUM_NAME_FN(drm_get_dirty_info_name,
  135.                  drm_dirty_info_enum_list)
  136.  
  137. struct drm_conn_prop_enum_list {
  138.         int type;
  139.         char *name;
  140.         int count;
  141. };
  142.  
  143. /*
  144.  * Connector and encoder types.
  145.  */
  146. static struct drm_conn_prop_enum_list drm_connector_enum_list[] =
  147. {       { DRM_MODE_CONNECTOR_Unknown, "Unknown", 0 },
  148.         { DRM_MODE_CONNECTOR_VGA, "VGA", 0 },
  149.         { DRM_MODE_CONNECTOR_DVII, "DVI-I", 0 },
  150.         { DRM_MODE_CONNECTOR_DVID, "DVI-D", 0 },
  151.         { DRM_MODE_CONNECTOR_DVIA, "DVI-A", 0 },
  152.         { DRM_MODE_CONNECTOR_Composite, "Composite", 0 },
  153.         { DRM_MODE_CONNECTOR_SVIDEO, "SVIDEO", 0 },
  154.         { DRM_MODE_CONNECTOR_LVDS, "LVDS", 0 },
  155.         { DRM_MODE_CONNECTOR_Component, "Component", 0 },
  156.         { DRM_MODE_CONNECTOR_9PinDIN, "9-pin DIN", 0 },
  157.         { DRM_MODE_CONNECTOR_DisplayPort, "DisplayPort", 0 },
  158.         { DRM_MODE_CONNECTOR_HDMIA, "HDMI Type A", 0 },
  159.         { DRM_MODE_CONNECTOR_HDMIB, "HDMI Type B", 0 },
  160.         { DRM_MODE_CONNECTOR_TV, "TV", 0 },
  161. };
  162.  
  163. static struct drm_prop_enum_list drm_encoder_enum_list[] =
  164. {       { DRM_MODE_ENCODER_NONE, "None" },
  165.         { DRM_MODE_ENCODER_DAC, "DAC" },
  166.         { DRM_MODE_ENCODER_TMDS, "TMDS" },
  167.         { DRM_MODE_ENCODER_LVDS, "LVDS" },
  168.         { DRM_MODE_ENCODER_TVDAC, "TV" },
  169. };
  170.  
  171. char *drm_get_encoder_name(struct drm_encoder *encoder)
  172. {
  173.         static char buf[32];
  174.  
  175.         snprintf(buf, 32, "%s-%d",
  176.                  drm_encoder_enum_list[encoder->encoder_type].name,
  177.                  encoder->base.id);
  178.         return buf;
  179. }
  180. EXPORT_SYMBOL(drm_get_encoder_name);
  181.  
  182. char *drm_get_connector_name(struct drm_connector *connector)
  183. {
  184.         static char buf[32];
  185.  
  186.         snprintf(buf, 32, "%s-%d",
  187.                  drm_connector_enum_list[connector->connector_type].name,
  188.                  connector->connector_type_id);
  189.         return buf;
  190. }
  191. EXPORT_SYMBOL(drm_get_connector_name);
  192.  
  193. char *drm_get_connector_status_name(enum drm_connector_status status)
  194. {
  195.         if (status == connector_status_connected)
  196.                 return "connected";
  197.         else if (status == connector_status_disconnected)
  198.                 return "disconnected";
  199.         else
  200.                 return "unknown";
  201. }
  202.  
  203. /**
  204.  * drm_mode_object_get - allocate a new identifier
  205.  * @dev: DRM device
  206.  * @ptr: object pointer, used to generate unique ID
  207.  * @type: object type
  208.  *
  209.  * LOCKING:
  210.  *
  211.  * Create a unique identifier based on @ptr in @dev's identifier space.  Used
  212.  * for tracking modes, CRTCs and connectors.
  213.  *
  214.  * RETURNS:
  215.  * New unique (relative to other objects in @dev) integer identifier for the
  216.  * object.
  217.  */
  218. static int drm_mode_object_get(struct drm_device *dev,
  219.                                struct drm_mode_object *obj, uint32_t obj_type)
  220. {
  221.         int new_id = 0;
  222.         int ret;
  223.  
  224. again:
  225.         if (idr_pre_get(&dev->mode_config.crtc_idr, GFP_KERNEL) == 0) {
  226.                 DRM_ERROR("Ran out memory getting a mode number\n");
  227.                 return -EINVAL;
  228.         }
  229.  
  230.         mutex_lock(&dev->mode_config.idr_mutex);
  231.         ret = idr_get_new_above(&dev->mode_config.crtc_idr, obj, 1, &new_id);
  232.         mutex_unlock(&dev->mode_config.idr_mutex);
  233.         if (ret == -EAGAIN)
  234.         goto again;
  235.  
  236.         obj->id = new_id;
  237.         obj->type = obj_type;
  238.     return 0;
  239. }
  240.  
  241. /**
  242.  * drm_mode_object_put - free an identifer
  243.  * @dev: DRM device
  244.  * @id: ID to free
  245.  *
  246.  * LOCKING:
  247.  * Caller must hold DRM mode_config lock.
  248.  *
  249.  * Free @id from @dev's unique identifier pool.
  250.  */
  251. static void drm_mode_object_put(struct drm_device *dev,
  252.                                 struct drm_mode_object *object)
  253. {
  254.         mutex_lock(&dev->mode_config.idr_mutex);
  255.         idr_remove(&dev->mode_config.crtc_idr, object->id);
  256.         mutex_unlock(&dev->mode_config.idr_mutex);
  257. }
  258.  
  259. struct drm_mode_object *drm_mode_object_find(struct drm_device *dev,
  260.                 uint32_t id, uint32_t type)
  261. {
  262.         struct drm_mode_object *obj = NULL;
  263.  
  264.         mutex_lock(&dev->mode_config.idr_mutex);
  265.         obj = idr_find(&dev->mode_config.crtc_idr, id);
  266.         if (!obj || (obj->type != type) || (obj->id != id))
  267.                 obj = NULL;
  268.         mutex_unlock(&dev->mode_config.idr_mutex);
  269.  
  270.         return obj;
  271. }
  272. EXPORT_SYMBOL(drm_mode_object_find);
  273.  
  274. /**
  275.  * drm_framebuffer_init - initialize a framebuffer
  276.  * @dev: DRM device
  277.  *
  278.  * LOCKING:
  279.  * Caller must hold mode config lock.
  280.  *
  281.  * Allocates an ID for the framebuffer's parent mode object, sets its mode
  282.  * functions & device file and adds it to the master fd list.
  283.  *
  284.  * RETURNS:
  285.  * Zero on success, error code on failure.
  286.  */
  287. int drm_framebuffer_init(struct drm_device *dev, struct drm_framebuffer *fb,
  288.                          const struct drm_framebuffer_funcs *funcs)
  289. {
  290.         int ret;
  291.  
  292.         ret = drm_mode_object_get(dev, &fb->base, DRM_MODE_OBJECT_FB);
  293.         if (ret) {
  294.                 return ret;
  295.         }
  296.  
  297.         fb->dev = dev;
  298.         fb->funcs = funcs;
  299.         dev->mode_config.num_fb++;
  300.         list_add(&fb->head, &dev->mode_config.fb_list);
  301.  
  302.         return 0;
  303. }
  304. EXPORT_SYMBOL(drm_framebuffer_init);
  305.  
  306. /**
  307.  * drm_framebuffer_cleanup - remove a framebuffer object
  308.  * @fb: framebuffer to remove
  309.  *
  310.  * LOCKING:
  311.  * Caller must hold mode config lock.
  312.  *
  313.  * Scans all the CRTCs in @dev's mode_config.  If they're using @fb, removes
  314.  * it, setting it to NULL.
  315.  */
  316. void drm_framebuffer_cleanup(struct drm_framebuffer *fb)
  317. {
  318.         struct drm_device *dev = fb->dev;
  319.         struct drm_crtc *crtc;
  320.         struct drm_mode_set set;
  321.         int ret;
  322.  
  323.         /* remove from any CRTC */
  324.         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  325.                 if (crtc->fb == fb) {
  326.                         /* should turn off the crtc */
  327.                         memset(&set, 0, sizeof(struct drm_mode_set));
  328.                         set.crtc = crtc;
  329.                         set.fb = NULL;
  330.                         ret = crtc->funcs->set_config(&set);
  331.                         if (ret)
  332.                                 DRM_ERROR("failed to reset crtc %p when fb was deleted\n", crtc);
  333.            }
  334.         }
  335.  
  336.         drm_mode_object_put(dev, &fb->base);
  337.         list_del(&fb->head);
  338.         dev->mode_config.num_fb--;
  339. }
  340. EXPORT_SYMBOL(drm_framebuffer_cleanup);
  341.  
  342. /**
  343.  * drm_crtc_init - Initialise a new CRTC object
  344.  * @dev: DRM device
  345.  * @crtc: CRTC object to init
  346.  * @funcs: callbacks for the new CRTC
  347.  *
  348.  * LOCKING:
  349.  * Caller must hold mode config lock.
  350.  *
  351.  * Inits a new object created as base part of an driver crtc object.
  352.  */
  353. void drm_crtc_init(struct drm_device *dev, struct drm_crtc *crtc,
  354.                    const struct drm_crtc_funcs *funcs)
  355. {
  356.         crtc->dev = dev;
  357.         crtc->funcs = funcs;
  358.  
  359.         mutex_lock(&dev->mode_config.mutex);
  360.         drm_mode_object_get(dev, &crtc->base, DRM_MODE_OBJECT_CRTC);
  361.  
  362.         list_add_tail(&crtc->head, &dev->mode_config.crtc_list);
  363.         dev->mode_config.num_crtc++;
  364.         mutex_unlock(&dev->mode_config.mutex);
  365. }
  366. EXPORT_SYMBOL(drm_crtc_init);
  367.  
  368. /**
  369.  * drm_crtc_cleanup - Cleans up the core crtc usage.
  370.  * @crtc: CRTC to cleanup
  371.  *
  372.  * LOCKING:
  373.  * Caller must hold mode config lock.
  374.  *
  375.  * Cleanup @crtc. Removes from drm modesetting space
  376.  * does NOT free object, caller does that.
  377.  */
  378. void drm_crtc_cleanup(struct drm_crtc *crtc)
  379. {
  380.         struct drm_device *dev = crtc->dev;
  381.  
  382.         if (crtc->gamma_store) {
  383.                 kfree(crtc->gamma_store);
  384.                 crtc->gamma_store = NULL;
  385.         }
  386.  
  387.         drm_mode_object_put(dev, &crtc->base);
  388.         list_del(&crtc->head);
  389.         dev->mode_config.num_crtc--;
  390. }
  391. EXPORT_SYMBOL(drm_crtc_cleanup);
  392.  
  393. /**
  394.  * drm_mode_probed_add - add a mode to a connector's probed mode list
  395.  * @connector: connector the new mode
  396.  * @mode: mode data
  397.  *
  398.  * LOCKING:
  399.  * Caller must hold mode config lock.
  400.  *
  401.  * Add @mode to @connector's mode list for later use.
  402.  */
  403. void drm_mode_probed_add(struct drm_connector *connector,
  404.                          struct drm_display_mode *mode)
  405. {
  406.         list_add(&mode->head, &connector->probed_modes);
  407. }
  408. EXPORT_SYMBOL(drm_mode_probed_add);
  409.  
  410. /**
  411.  * drm_mode_remove - remove and free a mode
  412.  * @connector: connector list to modify
  413.  * @mode: mode to remove
  414.  *
  415.  * LOCKING:
  416.  * Caller must hold mode config lock.
  417.  *
  418.  * Remove @mode from @connector's mode list, then free it.
  419.  */
  420. void drm_mode_remove(struct drm_connector *connector,
  421.                      struct drm_display_mode *mode)
  422. {
  423.         list_del(&mode->head);
  424.         kfree(mode);
  425. }
  426. EXPORT_SYMBOL(drm_mode_remove);
  427.  
  428. /**
  429.  * drm_connector_init - Init a preallocated connector
  430.  * @dev: DRM device
  431.  * @connector: the connector to init
  432.  * @funcs: callbacks for this connector
  433.  * @name: user visible name of the connector
  434.  *
  435.  * LOCKING:
  436.  * Caller must hold @dev's mode_config lock.
  437.  *
  438.  * Initialises a preallocated connector. Connectors should be
  439.  * subclassed as part of driver connector objects.
  440.  */
  441. void drm_connector_init(struct drm_device *dev,
  442.                      struct drm_connector *connector,
  443.                      const struct drm_connector_funcs *funcs,
  444.                      int connector_type)
  445. {
  446.         mutex_lock(&dev->mode_config.mutex);
  447.  
  448.         connector->dev = dev;
  449.         connector->funcs = funcs;
  450.         drm_mode_object_get(dev, &connector->base, DRM_MODE_OBJECT_CONNECTOR);
  451.         connector->connector_type = connector_type;
  452.         connector->connector_type_id =
  453.                 ++drm_connector_enum_list[connector_type].count; /* TODO */
  454.         INIT_LIST_HEAD(&connector->user_modes);
  455.         INIT_LIST_HEAD(&connector->probed_modes);
  456.         INIT_LIST_HEAD(&connector->modes);
  457.         connector->edid_blob_ptr = NULL;
  458.  
  459.         list_add_tail(&connector->head, &dev->mode_config.connector_list);
  460.         dev->mode_config.num_connector++;
  461.  
  462.         drm_connector_attach_property(connector,
  463.                                       dev->mode_config.edid_property, 0);
  464.  
  465.         drm_connector_attach_property(connector,
  466.                                       dev->mode_config.dpms_property, 0);
  467.  
  468.         mutex_unlock(&dev->mode_config.mutex);
  469. }
  470. EXPORT_SYMBOL(drm_connector_init);
  471.  
  472. /**
  473.  * drm_connector_cleanup - cleans up an initialised connector
  474.  * @connector: connector to cleanup
  475.  *
  476.  * LOCKING:
  477.  * Caller must hold @dev's mode_config lock.
  478.  *
  479.  * Cleans up the connector but doesn't free the object.
  480.  */
  481. void drm_connector_cleanup(struct drm_connector *connector)
  482. {
  483.         struct drm_device *dev = connector->dev;
  484.         struct drm_display_mode *mode, *t;
  485.  
  486.         list_for_each_entry_safe(mode, t, &connector->probed_modes, head)
  487.                 drm_mode_remove(connector, mode);
  488.  
  489.         list_for_each_entry_safe(mode, t, &connector->modes, head)
  490.                 drm_mode_remove(connector, mode);
  491.  
  492.         list_for_each_entry_safe(mode, t, &connector->user_modes, head)
  493.                 drm_mode_remove(connector, mode);
  494.  
  495.         kfree(connector->fb_helper_private);
  496.         mutex_lock(&dev->mode_config.mutex);
  497.         drm_mode_object_put(dev, &connector->base);
  498.         list_del(&connector->head);
  499.         mutex_unlock(&dev->mode_config.mutex);
  500. }
  501. EXPORT_SYMBOL(drm_connector_cleanup);
  502.  
  503. void drm_encoder_init(struct drm_device *dev,
  504.                       struct drm_encoder *encoder,
  505.                       const struct drm_encoder_funcs *funcs,
  506.                       int encoder_type)
  507. {
  508.         mutex_lock(&dev->mode_config.mutex);
  509.  
  510.         encoder->dev = dev;
  511.  
  512.         drm_mode_object_get(dev, &encoder->base, DRM_MODE_OBJECT_ENCODER);
  513.         encoder->encoder_type = encoder_type;
  514.         encoder->funcs = funcs;
  515.  
  516.         list_add_tail(&encoder->head, &dev->mode_config.encoder_list);
  517.         dev->mode_config.num_encoder++;
  518.  
  519.         mutex_unlock(&dev->mode_config.mutex);
  520. }
  521. EXPORT_SYMBOL(drm_encoder_init);
  522.  
  523. void drm_encoder_cleanup(struct drm_encoder *encoder)
  524. {
  525.         struct drm_device *dev = encoder->dev;
  526.         mutex_lock(&dev->mode_config.mutex);
  527.         drm_mode_object_put(dev, &encoder->base);
  528.         list_del(&encoder->head);
  529.         mutex_unlock(&dev->mode_config.mutex);
  530. }
  531. EXPORT_SYMBOL(drm_encoder_cleanup);
  532.  
  533. /**
  534.  * drm_mode_create - create a new display mode
  535.  * @dev: DRM device
  536.  *
  537.  * LOCKING:
  538.  * Caller must hold DRM mode_config lock.
  539.  *
  540.  * Create a new drm_display_mode, give it an ID, and return it.
  541.  *
  542.  * RETURNS:
  543.  * Pointer to new mode on success, NULL on error.
  544.  */
  545. struct drm_display_mode *drm_mode_create(struct drm_device *dev)
  546. {
  547.         struct drm_display_mode *nmode;
  548.  
  549.         nmode = kzalloc(sizeof(struct drm_display_mode), GFP_KERNEL);
  550.         if (!nmode)
  551.                 return NULL;
  552.  
  553.         drm_mode_object_get(dev, &nmode->base, DRM_MODE_OBJECT_MODE);
  554.         return nmode;
  555. }
  556. EXPORT_SYMBOL(drm_mode_create);
  557.  
  558. /**
  559.  * drm_mode_destroy - remove a mode
  560.  * @dev: DRM device
  561.  * @mode: mode to remove
  562.  *
  563.  * LOCKING:
  564.  * Caller must hold mode config lock.
  565.  *
  566.  * Free @mode's unique identifier, then free it.
  567.  */
  568. void drm_mode_destroy(struct drm_device *dev, struct drm_display_mode *mode)
  569. {
  570.         drm_mode_object_put(dev, &mode->base);
  571.  
  572.         kfree(mode);
  573. }
  574. EXPORT_SYMBOL(drm_mode_destroy);
  575.  
  576. static int drm_mode_create_standard_connector_properties(struct drm_device *dev)
  577. {
  578.         struct drm_property *edid;
  579.         struct drm_property *dpms;
  580.         int i;
  581.  
  582.         /*
  583.          * Standard properties (apply to all connectors)
  584.          */
  585.         edid = drm_property_create(dev, DRM_MODE_PROP_BLOB |
  586.                                    DRM_MODE_PROP_IMMUTABLE,
  587.                                    "EDID", 0);
  588.         dev->mode_config.edid_property = edid;
  589.  
  590.         dpms = drm_property_create(dev, DRM_MODE_PROP_ENUM,
  591.                                    "DPMS", ARRAY_SIZE(drm_dpms_enum_list));
  592.         for (i = 0; i < ARRAY_SIZE(drm_dpms_enum_list); i++)
  593.                 drm_property_add_enum(dpms, i, drm_dpms_enum_list[i].type,
  594.                                       drm_dpms_enum_list[i].name);
  595.         dev->mode_config.dpms_property = dpms;
  596.  
  597.         return 0;
  598. }
  599.  
  600. /**
  601.  * drm_mode_create_dvi_i_properties - create DVI-I specific connector properties
  602.  * @dev: DRM device
  603.  *
  604.  * Called by a driver the first time a DVI-I connector is made.
  605.  */
  606. int drm_mode_create_dvi_i_properties(struct drm_device *dev)
  607. {
  608.         struct drm_property *dvi_i_selector;
  609.         struct drm_property *dvi_i_subconnector;
  610.         int i;
  611.  
  612.         if (dev->mode_config.dvi_i_select_subconnector_property)
  613.                 return 0;
  614.  
  615.         dvi_i_selector =
  616.                 drm_property_create(dev, DRM_MODE_PROP_ENUM,
  617.                                     "select subconnector",
  618.                                     ARRAY_SIZE(drm_dvi_i_select_enum_list));
  619.         for (i = 0; i < ARRAY_SIZE(drm_dvi_i_select_enum_list); i++)
  620.                 drm_property_add_enum(dvi_i_selector, i,
  621.                                       drm_dvi_i_select_enum_list[i].type,
  622.                                       drm_dvi_i_select_enum_list[i].name);
  623.         dev->mode_config.dvi_i_select_subconnector_property = dvi_i_selector;
  624.  
  625.         dvi_i_subconnector =
  626.                 drm_property_create(dev, DRM_MODE_PROP_ENUM |
  627.                                     DRM_MODE_PROP_IMMUTABLE,
  628.                                     "subconnector",
  629.                                     ARRAY_SIZE(drm_dvi_i_subconnector_enum_list));
  630.         for (i = 0; i < ARRAY_SIZE(drm_dvi_i_subconnector_enum_list); i++)
  631.                 drm_property_add_enum(dvi_i_subconnector, i,
  632.                                       drm_dvi_i_subconnector_enum_list[i].type,
  633.                                       drm_dvi_i_subconnector_enum_list[i].name);
  634.         dev->mode_config.dvi_i_subconnector_property = dvi_i_subconnector;
  635.  
  636.         return 0;
  637. }
  638. EXPORT_SYMBOL(drm_mode_create_dvi_i_properties);
  639.  
  640. /**
  641.  * drm_create_tv_properties - create TV specific connector properties
  642.  * @dev: DRM device
  643.  * @num_modes: number of different TV formats (modes) supported
  644.  * @modes: array of pointers to strings containing name of each format
  645.  *
  646.  * Called by a driver's TV initialization routine, this function creates
  647.  * the TV specific connector properties for a given device.  Caller is
  648.  * responsible for allocating a list of format names and passing them to
  649.  * this routine.
  650.  */
  651. int drm_mode_create_tv_properties(struct drm_device *dev, int num_modes,
  652.                                   char *modes[])
  653. {
  654.         struct drm_property *tv_selector;
  655.         struct drm_property *tv_subconnector;
  656.         int i;
  657.  
  658.         if (dev->mode_config.tv_select_subconnector_property)
  659.                 return 0;
  660.  
  661.         /*
  662.          * Basic connector properties
  663.          */
  664.         tv_selector = drm_property_create(dev, DRM_MODE_PROP_ENUM,
  665.                                           "select subconnector",
  666.                                           ARRAY_SIZE(drm_tv_select_enum_list));
  667.         for (i = 0; i < ARRAY_SIZE(drm_tv_select_enum_list); i++)
  668.                 drm_property_add_enum(tv_selector, i,
  669.                                       drm_tv_select_enum_list[i].type,
  670.                                       drm_tv_select_enum_list[i].name);
  671.         dev->mode_config.tv_select_subconnector_property = tv_selector;
  672.  
  673.         tv_subconnector =
  674.                 drm_property_create(dev, DRM_MODE_PROP_ENUM |
  675.                                     DRM_MODE_PROP_IMMUTABLE, "subconnector",
  676.                                     ARRAY_SIZE(drm_tv_subconnector_enum_list));
  677.         for (i = 0; i < ARRAY_SIZE(drm_tv_subconnector_enum_list); i++)
  678.                 drm_property_add_enum(tv_subconnector, i,
  679.                                       drm_tv_subconnector_enum_list[i].type,
  680.                                       drm_tv_subconnector_enum_list[i].name);
  681.         dev->mode_config.tv_subconnector_property = tv_subconnector;
  682.  
  683.         /*
  684.          * Other, TV specific properties: margins & TV modes.
  685.          */
  686.         dev->mode_config.tv_left_margin_property =
  687.                 drm_property_create(dev, DRM_MODE_PROP_RANGE,
  688.                                     "left margin", 2);
  689.         dev->mode_config.tv_left_margin_property->values[0] = 0;
  690.         dev->mode_config.tv_left_margin_property->values[1] = 100;
  691.  
  692.         dev->mode_config.tv_right_margin_property =
  693.                 drm_property_create(dev, DRM_MODE_PROP_RANGE,
  694.                                     "right margin", 2);
  695.         dev->mode_config.tv_right_margin_property->values[0] = 0;
  696.         dev->mode_config.tv_right_margin_property->values[1] = 100;
  697.  
  698.         dev->mode_config.tv_top_margin_property =
  699.                 drm_property_create(dev, DRM_MODE_PROP_RANGE,
  700.                                     "top margin", 2);
  701.         dev->mode_config.tv_top_margin_property->values[0] = 0;
  702.         dev->mode_config.tv_top_margin_property->values[1] = 100;
  703.  
  704.         dev->mode_config.tv_bottom_margin_property =
  705.                 drm_property_create(dev, DRM_MODE_PROP_RANGE,
  706.                                     "bottom margin", 2);
  707.         dev->mode_config.tv_bottom_margin_property->values[0] = 0;
  708.         dev->mode_config.tv_bottom_margin_property->values[1] = 100;
  709.  
  710.         dev->mode_config.tv_mode_property =
  711.                 drm_property_create(dev, DRM_MODE_PROP_ENUM,
  712.                                     "mode", num_modes);
  713.         for (i = 0; i < num_modes; i++)
  714.                 drm_property_add_enum(dev->mode_config.tv_mode_property, i,
  715.                                       i, modes[i]);
  716.  
  717.         dev->mode_config.tv_brightness_property =
  718.                 drm_property_create(dev, DRM_MODE_PROP_RANGE,
  719.                                     "brightness", 2);
  720.         dev->mode_config.tv_brightness_property->values[0] = 0;
  721.         dev->mode_config.tv_brightness_property->values[1] = 100;
  722.  
  723.         dev->mode_config.tv_contrast_property =
  724.                 drm_property_create(dev, DRM_MODE_PROP_RANGE,
  725.                                     "contrast", 2);
  726.         dev->mode_config.tv_contrast_property->values[0] = 0;
  727.         dev->mode_config.tv_contrast_property->values[1] = 100;
  728.  
  729.         dev->mode_config.tv_flicker_reduction_property =
  730.                 drm_property_create(dev, DRM_MODE_PROP_RANGE,
  731.                                     "flicker reduction", 2);
  732.         dev->mode_config.tv_flicker_reduction_property->values[0] = 0;
  733.         dev->mode_config.tv_flicker_reduction_property->values[1] = 100;
  734.  
  735.         dev->mode_config.tv_overscan_property =
  736.                 drm_property_create(dev, DRM_MODE_PROP_RANGE,
  737.                                     "overscan", 2);
  738.         dev->mode_config.tv_overscan_property->values[0] = 0;
  739.         dev->mode_config.tv_overscan_property->values[1] = 100;
  740.  
  741.         dev->mode_config.tv_saturation_property =
  742.                 drm_property_create(dev, DRM_MODE_PROP_RANGE,
  743.                                     "saturation", 2);
  744.         dev->mode_config.tv_saturation_property->values[0] = 0;
  745.         dev->mode_config.tv_saturation_property->values[1] = 100;
  746.  
  747.         dev->mode_config.tv_hue_property =
  748.                 drm_property_create(dev, DRM_MODE_PROP_RANGE,
  749.                                     "hue", 2);
  750.         dev->mode_config.tv_hue_property->values[0] = 0;
  751.         dev->mode_config.tv_hue_property->values[1] = 100;
  752.  
  753.         return 0;
  754. }
  755. EXPORT_SYMBOL(drm_mode_create_tv_properties);
  756.  
  757. /**
  758.  * drm_mode_create_scaling_mode_property - create scaling mode property
  759.  * @dev: DRM device
  760.  *
  761.  * Called by a driver the first time it's needed, must be attached to desired
  762.  * connectors.
  763.  */
  764. int drm_mode_create_scaling_mode_property(struct drm_device *dev)
  765. {
  766.         struct drm_property *scaling_mode;
  767.         int i;
  768.  
  769.         if (dev->mode_config.scaling_mode_property)
  770.                 return 0;
  771.  
  772.         scaling_mode =
  773.                 drm_property_create(dev, DRM_MODE_PROP_ENUM, "scaling mode",
  774.                                     ARRAY_SIZE(drm_scaling_mode_enum_list));
  775.         for (i = 0; i < ARRAY_SIZE(drm_scaling_mode_enum_list); i++)
  776.                 drm_property_add_enum(scaling_mode, i,
  777.                                       drm_scaling_mode_enum_list[i].type,
  778.                                       drm_scaling_mode_enum_list[i].name);
  779.  
  780.         dev->mode_config.scaling_mode_property = scaling_mode;
  781.  
  782.         return 0;
  783. }
  784. EXPORT_SYMBOL(drm_mode_create_scaling_mode_property);
  785.  
  786. /**
  787.  * drm_mode_create_dithering_property - create dithering property
  788.  * @dev: DRM device
  789.  *
  790.  * Called by a driver the first time it's needed, must be attached to desired
  791.  * connectors.
  792.  */
  793. int drm_mode_create_dithering_property(struct drm_device *dev)
  794. {
  795.         struct drm_property *dithering_mode;
  796.         int i;
  797.  
  798.         if (dev->mode_config.dithering_mode_property)
  799.                 return 0;
  800.  
  801.         dithering_mode =
  802.                 drm_property_create(dev, DRM_MODE_PROP_ENUM, "dithering",
  803.                                     ARRAY_SIZE(drm_dithering_mode_enum_list));
  804.         for (i = 0; i < ARRAY_SIZE(drm_dithering_mode_enum_list); i++)
  805.                 drm_property_add_enum(dithering_mode, i,
  806.                                       drm_dithering_mode_enum_list[i].type,
  807.                                       drm_dithering_mode_enum_list[i].name);
  808.         dev->mode_config.dithering_mode_property = dithering_mode;
  809.  
  810.         return 0;
  811. }
  812. EXPORT_SYMBOL(drm_mode_create_dithering_property);
  813.  
  814. /**
  815.  * drm_mode_create_dirty_property - create dirty property
  816.  * @dev: DRM device
  817.  *
  818.  * Called by a driver the first time it's needed, must be attached to desired
  819.  * connectors.
  820.  */
  821. int drm_mode_create_dirty_info_property(struct drm_device *dev)
  822. {
  823.         struct drm_property *dirty_info;
  824.         int i;
  825.  
  826.         if (dev->mode_config.dirty_info_property)
  827.                 return 0;
  828.  
  829.         dirty_info =
  830.                 drm_property_create(dev, DRM_MODE_PROP_ENUM |
  831.                                     DRM_MODE_PROP_IMMUTABLE,
  832.                                     "dirty",
  833.                                     ARRAY_SIZE(drm_dirty_info_enum_list));
  834.         for (i = 0; i < ARRAY_SIZE(drm_dirty_info_enum_list); i++)
  835.                 drm_property_add_enum(dirty_info, i,
  836.                                       drm_dirty_info_enum_list[i].type,
  837.                                       drm_dirty_info_enum_list[i].name);
  838.         dev->mode_config.dirty_info_property = dirty_info;
  839.  
  840.         return 0;
  841. }
  842. EXPORT_SYMBOL(drm_mode_create_dirty_info_property);
  843.  
  844. /**
  845.  * drm_mode_config_init - initialize DRM mode_configuration structure
  846.  * @dev: DRM device
  847.  *
  848.  * LOCKING:
  849.  * None, should happen single threaded at init time.
  850.  *
  851.  * Initialize @dev's mode_config structure, used for tracking the graphics
  852.  * configuration of @dev.
  853.  */
  854. void drm_mode_config_init(struct drm_device *dev)
  855. {
  856. //   mutex_init(&dev->mode_config.mutex);
  857. //   mutex_init(&dev->mode_config.idr_mutex);
  858.         INIT_LIST_HEAD(&dev->mode_config.fb_list);
  859.         INIT_LIST_HEAD(&dev->mode_config.fb_kernel_list);
  860.         INIT_LIST_HEAD(&dev->mode_config.crtc_list);
  861.         INIT_LIST_HEAD(&dev->mode_config.connector_list);
  862.         INIT_LIST_HEAD(&dev->mode_config.encoder_list);
  863.         INIT_LIST_HEAD(&dev->mode_config.property_list);
  864.         INIT_LIST_HEAD(&dev->mode_config.property_blob_list);
  865.         idr_init(&dev->mode_config.crtc_idr);
  866.  
  867.         mutex_lock(&dev->mode_config.mutex);
  868.         drm_mode_create_standard_connector_properties(dev);
  869.         mutex_unlock(&dev->mode_config.mutex);
  870.  
  871.         /* Just to be sure */
  872.         dev->mode_config.num_fb = 0;
  873.         dev->mode_config.num_connector = 0;
  874.         dev->mode_config.num_crtc = 0;
  875.         dev->mode_config.num_encoder = 0;
  876. }
  877. EXPORT_SYMBOL(drm_mode_config_init);
  878.  
  879. int drm_mode_group_init(struct drm_device *dev, struct drm_mode_group *group)
  880. {
  881.         uint32_t total_objects = 0;
  882.  
  883.         total_objects += dev->mode_config.num_crtc;
  884.         total_objects += dev->mode_config.num_connector;
  885.         total_objects += dev->mode_config.num_encoder;
  886.  
  887.         if (total_objects == 0)
  888.                 return -EINVAL;
  889.  
  890.         group->id_list = kzalloc(total_objects * sizeof(uint32_t), GFP_KERNEL);
  891.         if (!group->id_list)
  892.                 return -ENOMEM;
  893.  
  894.         group->num_crtcs = 0;
  895.         group->num_connectors = 0;
  896.         group->num_encoders = 0;
  897.         return 0;
  898. }
  899.  
  900. int drm_mode_group_init_legacy_group(struct drm_device *dev,
  901.                                      struct drm_mode_group *group)
  902. {
  903.         struct drm_crtc *crtc;
  904.         struct drm_encoder *encoder;
  905.         struct drm_connector *connector;
  906.         int ret;
  907.  
  908.         if ((ret = drm_mode_group_init(dev, group)))
  909.                 return ret;
  910.  
  911.         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
  912.                 group->id_list[group->num_crtcs++] = crtc->base.id;
  913.  
  914.         list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
  915.                 group->id_list[group->num_crtcs + group->num_encoders++] =
  916.                 encoder->base.id;
  917.  
  918.         list_for_each_entry(connector, &dev->mode_config.connector_list, head)
  919.                 group->id_list[group->num_crtcs + group->num_encoders +
  920.                                group->num_connectors++] = connector->base.id;
  921.  
  922.         return 0;
  923. }
  924.  
  925. /**
  926.  * drm_mode_config_cleanup - free up DRM mode_config info
  927.  * @dev: DRM device
  928.  *
  929.  * LOCKING:
  930.  * Caller must hold mode config lock.
  931.  *
  932.  * Free up all the connectors and CRTCs associated with this DRM device, then
  933.  * free up the framebuffers and associated buffer objects.
  934.  *
  935.  * FIXME: cleanup any dangling user buffer objects too
  936.  */
  937. void drm_mode_config_cleanup(struct drm_device *dev)
  938. {
  939.         struct drm_connector *connector, *ot;
  940.         struct drm_crtc *crtc, *ct;
  941.         struct drm_encoder *encoder, *enct;
  942.         struct drm_framebuffer *fb, *fbt;
  943.         struct drm_property *property, *pt;
  944.  
  945.         list_for_each_entry_safe(encoder, enct, &dev->mode_config.encoder_list,
  946.                                  head) {
  947.                 encoder->funcs->destroy(encoder);
  948.         }
  949.  
  950.         list_for_each_entry_safe(connector, ot,
  951.                                  &dev->mode_config.connector_list, head) {
  952.                 connector->funcs->destroy(connector);
  953.         }
  954.  
  955.         list_for_each_entry_safe(property, pt, &dev->mode_config.property_list,
  956.                                  head) {
  957.                 drm_property_destroy(dev, property);
  958.         }
  959.  
  960.         list_for_each_entry_safe(fb, fbt, &dev->mode_config.fb_list, head) {
  961.                 fb->funcs->destroy(fb);
  962.         }
  963.  
  964.         list_for_each_entry_safe(crtc, ct, &dev->mode_config.crtc_list, head) {
  965.                 crtc->funcs->destroy(crtc);
  966.         }
  967.  
  968. }
  969. EXPORT_SYMBOL(drm_mode_config_cleanup);
  970.  
  971. /**
  972.  * drm_crtc_convert_to_umode - convert a drm_display_mode into a modeinfo
  973.  * @out: drm_mode_modeinfo struct to return to the user
  974.  * @in: drm_display_mode to use
  975.  *
  976.  * LOCKING:
  977.  * None.
  978.  *
  979.  * Convert a drm_display_mode into a drm_mode_modeinfo structure to return to
  980.  * the user.
  981.  */
  982. void drm_crtc_convert_to_umode(struct drm_mode_modeinfo *out,
  983.                                struct drm_display_mode *in)
  984. {
  985.         out->clock = in->clock;
  986.         out->hdisplay = in->hdisplay;
  987.         out->hsync_start = in->hsync_start;
  988.         out->hsync_end = in->hsync_end;
  989.         out->htotal = in->htotal;
  990.         out->hskew = in->hskew;
  991.         out->vdisplay = in->vdisplay;
  992.         out->vsync_start = in->vsync_start;
  993.         out->vsync_end = in->vsync_end;
  994.         out->vtotal = in->vtotal;
  995.         out->vscan = in->vscan;
  996.         out->vrefresh = in->vrefresh;
  997.         out->flags = in->flags;
  998.         out->type = in->type;
  999.         strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
  1000.         out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
  1001. }
  1002.  
  1003. /**
  1004.  * drm_crtc_convert_to_umode - convert a modeinfo into a drm_display_mode
  1005.  * @out: drm_display_mode to return to the user
  1006.  * @in: drm_mode_modeinfo to use
  1007.  *
  1008.  * LOCKING:
  1009.  * None.
  1010.  *
  1011.  * Convert a drm_mode_modeinfo into a drm_display_mode structure to return to
  1012.  * the caller.
  1013.  */
  1014. void drm_crtc_convert_umode(struct drm_display_mode *out,
  1015.                             struct drm_mode_modeinfo *in)
  1016. {
  1017.         out->clock = in->clock;
  1018.         out->hdisplay = in->hdisplay;
  1019.         out->hsync_start = in->hsync_start;
  1020.         out->hsync_end = in->hsync_end;
  1021.         out->htotal = in->htotal;
  1022.         out->hskew = in->hskew;
  1023.         out->vdisplay = in->vdisplay;
  1024.         out->vsync_start = in->vsync_start;
  1025.         out->vsync_end = in->vsync_end;
  1026.         out->vtotal = in->vtotal;
  1027.         out->vscan = in->vscan;
  1028.         out->vrefresh = in->vrefresh;
  1029.         out->flags = in->flags;
  1030.         out->type = in->type;
  1031.         strncpy(out->name, in->name, DRM_DISPLAY_MODE_LEN);
  1032.         out->name[DRM_DISPLAY_MODE_LEN-1] = 0;
  1033. }
  1034.  
  1035.  
  1036. #if 0
  1037. /**
  1038.  * drm_mode_getresources - get graphics configuration
  1039.  * @inode: inode from the ioctl
  1040.  * @filp: file * from the ioctl
  1041.  * @cmd: cmd from ioctl
  1042.  * @arg: arg from ioctl
  1043.  *
  1044.  * LOCKING:
  1045.  * Takes mode config lock.
  1046.  *
  1047.  * Construct a set of configuration description structures and return
  1048.  * them to the user, including CRTC, connector and framebuffer configuration.
  1049.  *
  1050.  * Called by the user via ioctl.
  1051.  *
  1052.  * RETURNS:
  1053.  * Zero on success, errno on failure.
  1054.  */
  1055. int drm_mode_getresources(struct drm_device *dev, void *data,
  1056.                           struct drm_file *file_priv)
  1057. {
  1058.         struct drm_mode_card_res *card_res = data;
  1059.         struct list_head *lh;
  1060.         struct drm_framebuffer *fb;
  1061.         struct drm_connector *connector;
  1062.         struct drm_crtc *crtc;
  1063.         struct drm_encoder *encoder;
  1064.         int ret = 0;
  1065.         int connector_count = 0;
  1066.         int crtc_count = 0;
  1067.         int fb_count = 0;
  1068.         int encoder_count = 0;
  1069.         int copied = 0, i;
  1070.         uint32_t __user *fb_id;
  1071.         uint32_t __user *crtc_id;
  1072.         uint32_t __user *connector_id;
  1073.         uint32_t __user *encoder_id;
  1074.         struct drm_mode_group *mode_group;
  1075.  
  1076.         mutex_lock(&dev->mode_config.mutex);
  1077.  
  1078.         /*
  1079.          * For the non-control nodes we need to limit the list of resources
  1080.          * by IDs in the group list for this node
  1081.          */
  1082.         list_for_each(lh, &file_priv->fbs)
  1083.                 fb_count++;
  1084.  
  1085.         mode_group = &file_priv->master->minor->mode_group;
  1086.         if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
  1087.  
  1088.                 list_for_each(lh, &dev->mode_config.crtc_list)
  1089.                         crtc_count++;
  1090.  
  1091.                 list_for_each(lh, &dev->mode_config.connector_list)
  1092.                         connector_count++;
  1093.  
  1094.                 list_for_each(lh, &dev->mode_config.encoder_list)
  1095.                         encoder_count++;
  1096.         } else {
  1097.  
  1098.                 crtc_count = mode_group->num_crtcs;
  1099.                 connector_count = mode_group->num_connectors;
  1100.                 encoder_count = mode_group->num_encoders;
  1101.         }
  1102.  
  1103.         card_res->max_height = dev->mode_config.max_height;
  1104.         card_res->min_height = dev->mode_config.min_height;
  1105.         card_res->max_width = dev->mode_config.max_width;
  1106.         card_res->min_width = dev->mode_config.min_width;
  1107.  
  1108.         /* handle this in 4 parts */
  1109.         /* FBs */
  1110.         if (card_res->count_fbs >= fb_count) {
  1111.                 copied = 0;
  1112.                 fb_id = (uint32_t __user *)(unsigned long)card_res->fb_id_ptr;
  1113.                 list_for_each_entry(fb, &file_priv->fbs, head) {
  1114.                         if (put_user(fb->base.id, fb_id + copied)) {
  1115.                                 ret = -EFAULT;
  1116.                                 goto out;
  1117.                         }
  1118.                         copied++;
  1119.                 }
  1120.         }
  1121.         card_res->count_fbs = fb_count;
  1122.  
  1123.         /* CRTCs */
  1124.         if (card_res->count_crtcs >= crtc_count) {
  1125.                 copied = 0;
  1126.                 crtc_id = (uint32_t __user *)(unsigned long)card_res->crtc_id_ptr;
  1127.                 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
  1128.                         list_for_each_entry(crtc, &dev->mode_config.crtc_list,
  1129.                                             head) {
  1130.                                 DRM_DEBUG_KMS("CRTC ID is %d\n", crtc->base.id);
  1131.                                 if (put_user(crtc->base.id, crtc_id + copied)) {
  1132.                                         ret = -EFAULT;
  1133.                                         goto out;
  1134.                                 }
  1135.                                 copied++;
  1136.                         }
  1137.                 } else {
  1138.                         for (i = 0; i < mode_group->num_crtcs; i++) {
  1139.                                 if (put_user(mode_group->id_list[i],
  1140.                                              crtc_id + copied)) {
  1141.                                         ret = -EFAULT;
  1142.                                         goto out;
  1143.                                 }
  1144.                                 copied++;
  1145.                         }
  1146.                 }
  1147.         }
  1148.         card_res->count_crtcs = crtc_count;
  1149.  
  1150.         /* Encoders */
  1151.         if (card_res->count_encoders >= encoder_count) {
  1152.                 copied = 0;
  1153.                 encoder_id = (uint32_t __user *)(unsigned long)card_res->encoder_id_ptr;
  1154.                 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
  1155.                         list_for_each_entry(encoder,
  1156.                                             &dev->mode_config.encoder_list,
  1157.                                             head) {
  1158.                                 DRM_DEBUG_KMS("ENCODER ID is %d\n",
  1159.                                           encoder->base.id);
  1160.                                 if (put_user(encoder->base.id, encoder_id +
  1161.                                              copied)) {
  1162.                                         ret = -EFAULT;
  1163.                                         goto out;
  1164.                                 }
  1165.                                 copied++;
  1166.                         }
  1167.                 } else {
  1168.                         for (i = mode_group->num_crtcs; i < mode_group->num_crtcs + mode_group->num_encoders; i++) {
  1169.                                 if (put_user(mode_group->id_list[i],
  1170.                                              encoder_id + copied)) {
  1171.                                         ret = -EFAULT;
  1172.                                         goto out;
  1173.                                 }
  1174.                                 copied++;
  1175.                         }
  1176.  
  1177.                 }
  1178.         }
  1179.         card_res->count_encoders = encoder_count;
  1180.  
  1181.         /* Connectors */
  1182.         if (card_res->count_connectors >= connector_count) {
  1183.                 copied = 0;
  1184.                 connector_id = (uint32_t __user *)(unsigned long)card_res->connector_id_ptr;
  1185.                 if (file_priv->master->minor->type == DRM_MINOR_CONTROL) {
  1186.                         list_for_each_entry(connector,
  1187.                                             &dev->mode_config.connector_list,
  1188.                                             head) {
  1189.                                 DRM_DEBUG_KMS("CONNECTOR ID is %d\n",
  1190.                                           connector->base.id);
  1191.                                 if (put_user(connector->base.id,
  1192.                                              connector_id + copied)) {
  1193.                                         ret = -EFAULT;
  1194.                                         goto out;
  1195.                                 }
  1196.                                 copied++;
  1197.                         }
  1198.                 } else {
  1199.                         int start = mode_group->num_crtcs +
  1200.                                 mode_group->num_encoders;
  1201.                         for (i = start; i < start + mode_group->num_connectors; i++) {
  1202.                                 if (put_user(mode_group->id_list[i],
  1203.                                              connector_id + copied)) {
  1204.                                         ret = -EFAULT;
  1205.                                         goto out;
  1206.                                 }
  1207.                                 copied++;
  1208.                         }
  1209.                 }
  1210.         }
  1211.         card_res->count_connectors = connector_count;
  1212.  
  1213.         DRM_DEBUG_KMS("Counted %d %d %d\n", card_res->count_crtcs,
  1214.                   card_res->count_connectors, card_res->count_encoders);
  1215.  
  1216. out:
  1217.         mutex_unlock(&dev->mode_config.mutex);
  1218.         return ret;
  1219. }
  1220.  
  1221. /**
  1222.  * drm_mode_getcrtc - get CRTC configuration
  1223.  * @inode: inode from the ioctl
  1224.  * @filp: file * from the ioctl
  1225.  * @cmd: cmd from ioctl
  1226.  * @arg: arg from ioctl
  1227.  *
  1228.  * LOCKING:
  1229.  * Caller? (FIXME)
  1230.  *
  1231.  * Construct a CRTC configuration structure to return to the user.
  1232.  *
  1233.  * Called by the user via ioctl.
  1234.  *
  1235.  * RETURNS:
  1236.  * Zero on success, errno on failure.
  1237.  */
  1238. int drm_mode_getcrtc(struct drm_device *dev,
  1239.                      void *data, struct drm_file *file_priv)
  1240. {
  1241.         struct drm_mode_crtc *crtc_resp = data;
  1242.         struct drm_crtc *crtc;
  1243.         struct drm_mode_object *obj;
  1244.         int ret = 0;
  1245.  
  1246.         mutex_lock(&dev->mode_config.mutex);
  1247.  
  1248.         obj = drm_mode_object_find(dev, crtc_resp->crtc_id,
  1249.                                    DRM_MODE_OBJECT_CRTC);
  1250.         if (!obj) {
  1251.                 ret = -EINVAL;
  1252.                 goto out;
  1253.         }
  1254.         crtc = obj_to_crtc(obj);
  1255.  
  1256.         crtc_resp->x = crtc->x;
  1257.         crtc_resp->y = crtc->y;
  1258.         crtc_resp->gamma_size = crtc->gamma_size;
  1259.         if (crtc->fb)
  1260.                 crtc_resp->fb_id = crtc->fb->base.id;
  1261.         else
  1262.                 crtc_resp->fb_id = 0;
  1263.  
  1264.         if (crtc->enabled) {
  1265.  
  1266.                 drm_crtc_convert_to_umode(&crtc_resp->mode, &crtc->mode);
  1267.                 crtc_resp->mode_valid = 1;
  1268.  
  1269.         } else {
  1270.                 crtc_resp->mode_valid = 0;
  1271.         }
  1272.  
  1273. out:
  1274.         mutex_unlock(&dev->mode_config.mutex);
  1275.         return ret;
  1276. }
  1277.  
  1278. /**
  1279.  * drm_mode_getconnector - get connector configuration
  1280.  * @inode: inode from the ioctl
  1281.  * @filp: file * from the ioctl
  1282.  * @cmd: cmd from ioctl
  1283.  * @arg: arg from ioctl
  1284.  *
  1285.  * LOCKING:
  1286.  * Caller? (FIXME)
  1287.  *
  1288.  * Construct a connector configuration structure to return to the user.
  1289.  *
  1290.  * Called by the user via ioctl.
  1291.  *
  1292.  * RETURNS:
  1293.  * Zero on success, errno on failure.
  1294.  */
  1295. int drm_mode_getconnector(struct drm_device *dev, void *data,
  1296.                           struct drm_file *file_priv)
  1297. {
  1298.         struct drm_mode_get_connector *out_resp = data;
  1299.         struct drm_mode_object *obj;
  1300.         struct drm_connector *connector;
  1301.         struct drm_display_mode *mode;
  1302.         int mode_count = 0;
  1303.         int props_count = 0;
  1304.         int encoders_count = 0;
  1305.         int ret = 0;
  1306.         int copied = 0;
  1307.         int i;
  1308.         struct drm_mode_modeinfo u_mode;
  1309.         struct drm_mode_modeinfo __user *mode_ptr;
  1310.         uint32_t __user *prop_ptr;
  1311.         uint64_t __user *prop_values;
  1312.         uint32_t __user *encoder_ptr;
  1313.  
  1314.         memset(&u_mode, 0, sizeof(struct drm_mode_modeinfo));
  1315.  
  1316.         DRM_DEBUG_KMS("connector id %d:\n", out_resp->connector_id);
  1317.  
  1318.         mutex_lock(&dev->mode_config.mutex);
  1319.  
  1320.         obj = drm_mode_object_find(dev, out_resp->connector_id,
  1321.                                    DRM_MODE_OBJECT_CONNECTOR);
  1322.         if (!obj) {
  1323.                 ret = -EINVAL;
  1324.                 goto out;
  1325.         }
  1326.         connector = obj_to_connector(obj);
  1327.  
  1328.         for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
  1329.                 if (connector->property_ids[i] != 0) {
  1330.                         props_count++;
  1331.                 }
  1332.         }
  1333.  
  1334.         for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
  1335.                 if (connector->encoder_ids[i] != 0) {
  1336.                         encoders_count++;
  1337.                 }
  1338.         }
  1339.  
  1340.         if (out_resp->count_modes == 0) {
  1341.                 connector->funcs->fill_modes(connector,
  1342.                                              dev->mode_config.max_width,
  1343.                                              dev->mode_config.max_height);
  1344.         }
  1345.  
  1346.         /* delayed so we get modes regardless of pre-fill_modes state */
  1347.         list_for_each_entry(mode, &connector->modes, head)
  1348.                 mode_count++;
  1349.  
  1350.         out_resp->connector_id = connector->base.id;
  1351.         out_resp->connector_type = connector->connector_type;
  1352.         out_resp->connector_type_id = connector->connector_type_id;
  1353.         out_resp->mm_width = connector->display_info.width_mm;
  1354.         out_resp->mm_height = connector->display_info.height_mm;
  1355.         out_resp->subpixel = connector->display_info.subpixel_order;
  1356.         out_resp->connection = connector->status;
  1357.         if (connector->encoder)
  1358.                 out_resp->encoder_id = connector->encoder->base.id;
  1359.         else
  1360.                 out_resp->encoder_id = 0;
  1361.  
  1362.         /*
  1363.          * This ioctl is called twice, once to determine how much space is
  1364.          * needed, and the 2nd time to fill it.
  1365.          */
  1366.         if ((out_resp->count_modes >= mode_count) && mode_count) {
  1367.                 copied = 0;
  1368.                 mode_ptr = (struct drm_mode_modeinfo *)(unsigned long)out_resp->modes_ptr;
  1369.                 list_for_each_entry(mode, &connector->modes, head) {
  1370.                         drm_crtc_convert_to_umode(&u_mode, mode);
  1371.                         if (copy_to_user(mode_ptr + copied,
  1372.                                          &u_mode, sizeof(u_mode))) {
  1373.                                 ret = -EFAULT;
  1374.                                 goto out;
  1375.                         }
  1376.                         copied++;
  1377.                 }
  1378.         }
  1379.         out_resp->count_modes = mode_count;
  1380.  
  1381.         if ((out_resp->count_props >= props_count) && props_count) {
  1382.                 copied = 0;
  1383.                 prop_ptr = (uint32_t *)(unsigned long)(out_resp->props_ptr);
  1384.                 prop_values = (uint64_t *)(unsigned long)(out_resp->prop_values_ptr);
  1385.                 for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
  1386.                         if (connector->property_ids[i] != 0) {
  1387.                                 if (put_user(connector->property_ids[i],
  1388.                                              prop_ptr + copied)) {
  1389.                                         ret = -EFAULT;
  1390.                                         goto out;
  1391.                                 }
  1392.  
  1393.                                 if (put_user(connector->property_values[i],
  1394.                                              prop_values + copied)) {
  1395.                                         ret = -EFAULT;
  1396.                                         goto out;
  1397.                                 }
  1398.                                 copied++;
  1399.                         }
  1400.                 }
  1401.         }
  1402.         out_resp->count_props = props_count;
  1403.  
  1404.         if ((out_resp->count_encoders >= encoders_count) && encoders_count) {
  1405.                 copied = 0;
  1406.                 encoder_ptr = (uint32_t *)(unsigned long)(out_resp->encoders_ptr);
  1407.                 for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
  1408.                         if (connector->encoder_ids[i] != 0) {
  1409.                                 if (put_user(connector->encoder_ids[i],
  1410.                                              encoder_ptr + copied)) {
  1411.                                         ret = -EFAULT;
  1412.                                         goto out;
  1413.                                 }
  1414.                                 copied++;
  1415.                         }
  1416.                 }
  1417.         }
  1418.         out_resp->count_encoders = encoders_count;
  1419.  
  1420. out:
  1421.         mutex_unlock(&dev->mode_config.mutex);
  1422.         return ret;
  1423. }
  1424.  
  1425. int drm_mode_getencoder(struct drm_device *dev, void *data,
  1426.                         struct drm_file *file_priv)
  1427. {
  1428.         struct drm_mode_get_encoder *enc_resp = data;
  1429.         struct drm_mode_object *obj;
  1430.         struct drm_encoder *encoder;
  1431.         int ret = 0;
  1432.  
  1433.         mutex_lock(&dev->mode_config.mutex);
  1434.         obj = drm_mode_object_find(dev, enc_resp->encoder_id,
  1435.                                    DRM_MODE_OBJECT_ENCODER);
  1436.         if (!obj) {
  1437.                 ret = -EINVAL;
  1438.                 goto out;
  1439.         }
  1440.         encoder = obj_to_encoder(obj);
  1441.  
  1442.         if (encoder->crtc)
  1443.                 enc_resp->crtc_id = encoder->crtc->base.id;
  1444.         else
  1445.                 enc_resp->crtc_id = 0;
  1446.         enc_resp->encoder_type = encoder->encoder_type;
  1447.         enc_resp->encoder_id = encoder->base.id;
  1448.         enc_resp->possible_crtcs = encoder->possible_crtcs;
  1449.         enc_resp->possible_clones = encoder->possible_clones;
  1450.  
  1451. out:
  1452.         mutex_unlock(&dev->mode_config.mutex);
  1453.         return ret;
  1454. }
  1455.  
  1456. /**
  1457.  * drm_mode_setcrtc - set CRTC configuration
  1458.  * @inode: inode from the ioctl
  1459.  * @filp: file * from the ioctl
  1460.  * @cmd: cmd from ioctl
  1461.  * @arg: arg from ioctl
  1462.  *
  1463.  * LOCKING:
  1464.  * Caller? (FIXME)
  1465.  *
  1466.  * Build a new CRTC configuration based on user request.
  1467.  *
  1468.  * Called by the user via ioctl.
  1469.  *
  1470.  * RETURNS:
  1471.  * Zero on success, errno on failure.
  1472.  */
  1473. int drm_mode_setcrtc(struct drm_device *dev, void *data,
  1474.                      struct drm_file *file_priv)
  1475. {
  1476.         struct drm_mode_config *config = &dev->mode_config;
  1477.         struct drm_mode_crtc *crtc_req = data;
  1478.         struct drm_mode_object *obj;
  1479.         struct drm_crtc *crtc, *crtcfb;
  1480.         struct drm_connector **connector_set = NULL, *connector;
  1481.         struct drm_framebuffer *fb = NULL;
  1482.         struct drm_display_mode *mode = NULL;
  1483.         struct drm_mode_set set;
  1484.         uint32_t __user *set_connectors_ptr;
  1485.         int ret = 0;
  1486.         int i;
  1487.  
  1488.         mutex_lock(&dev->mode_config.mutex);
  1489.         obj = drm_mode_object_find(dev, crtc_req->crtc_id,
  1490.                                    DRM_MODE_OBJECT_CRTC);
  1491.         if (!obj) {
  1492.                 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", crtc_req->crtc_id);
  1493.                 ret = -EINVAL;
  1494.                 goto out;
  1495.         }
  1496.         crtc = obj_to_crtc(obj);
  1497.  
  1498.         if (crtc_req->mode_valid) {
  1499.                 /* If we have a mode we need a framebuffer. */
  1500.                 /* If we pass -1, set the mode with the currently bound fb */
  1501.                 if (crtc_req->fb_id == -1) {
  1502.                         list_for_each_entry(crtcfb,
  1503.                                             &dev->mode_config.crtc_list, head) {
  1504.                                 if (crtcfb == crtc) {
  1505.                                         DRM_DEBUG_KMS("Using current fb for "
  1506.                                                         "setmode\n");
  1507.                                         fb = crtc->fb;
  1508.                                 }
  1509.                         }
  1510.                 } else {
  1511.                         obj = drm_mode_object_find(dev, crtc_req->fb_id,
  1512.                                                    DRM_MODE_OBJECT_FB);
  1513.                         if (!obj) {
  1514.                                 DRM_DEBUG_KMS("Unknown FB ID%d\n",
  1515.                                                 crtc_req->fb_id);
  1516.                                 ret = -EINVAL;
  1517.                                 goto out;
  1518.                         }
  1519.                         fb = obj_to_fb(obj);
  1520.                 }
  1521.  
  1522.                 mode = drm_mode_create(dev);
  1523.                 drm_crtc_convert_umode(mode, &crtc_req->mode);
  1524.                 drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
  1525.         }
  1526.  
  1527.         if (crtc_req->count_connectors == 0 && mode) {
  1528.                 DRM_DEBUG_KMS("Count connectors is 0 but mode set\n");
  1529.                 ret = -EINVAL;
  1530.                 goto out;
  1531.         }
  1532.  
  1533.         if (crtc_req->count_connectors > 0 && (!mode || !fb)) {
  1534.                 DRM_DEBUG_KMS("Count connectors is %d but no mode or fb set\n",
  1535.                           crtc_req->count_connectors);
  1536.                 ret = -EINVAL;
  1537.                 goto out;
  1538.         }
  1539.  
  1540.         if (crtc_req->count_connectors > 0) {
  1541.                 u32 out_id;
  1542.  
  1543.                 /* Avoid unbounded kernel memory allocation */
  1544.                 if (crtc_req->count_connectors > config->num_connector) {
  1545.                         ret = -EINVAL;
  1546.                         goto out;
  1547.                 }
  1548.  
  1549.                 connector_set = kmalloc(crtc_req->count_connectors *
  1550.                                         sizeof(struct drm_connector *),
  1551.                                         GFP_KERNEL);
  1552.                 if (!connector_set) {
  1553.                         ret = -ENOMEM;
  1554.                         goto out;
  1555.                 }
  1556.  
  1557.                 for (i = 0; i < crtc_req->count_connectors; i++) {
  1558.                         set_connectors_ptr = (uint32_t *)(unsigned long)crtc_req->set_connectors_ptr;
  1559.                         if (get_user(out_id, &set_connectors_ptr[i])) {
  1560.                                 ret = -EFAULT;
  1561.                                 goto out;
  1562.                         }
  1563.  
  1564.                         obj = drm_mode_object_find(dev, out_id,
  1565.                                                    DRM_MODE_OBJECT_CONNECTOR);
  1566.                         if (!obj) {
  1567.                                 DRM_DEBUG_KMS("Connector id %d unknown\n",
  1568.                                                 out_id);
  1569.                                 ret = -EINVAL;
  1570.                                 goto out;
  1571.                         }
  1572.                         connector = obj_to_connector(obj);
  1573.  
  1574.                         connector_set[i] = connector;
  1575.                 }
  1576.         }
  1577.  
  1578.         set.crtc = crtc;
  1579.         set.x = crtc_req->x;
  1580.         set.y = crtc_req->y;
  1581.         set.mode = mode;
  1582.         set.connectors = connector_set;
  1583.         set.num_connectors = crtc_req->count_connectors;
  1584.         set.fb = fb;
  1585.         ret = crtc->funcs->set_config(&set);
  1586.  
  1587. out:
  1588.         kfree(connector_set);
  1589.         mutex_unlock(&dev->mode_config.mutex);
  1590.         return ret;
  1591. }
  1592.  
  1593. int drm_mode_cursor_ioctl(struct drm_device *dev,
  1594.                         void *data, struct drm_file *file_priv)
  1595. {
  1596.         struct drm_mode_cursor *req = data;
  1597.         struct drm_mode_object *obj;
  1598.         struct drm_crtc *crtc;
  1599.         int ret = 0;
  1600.  
  1601.         if (!req->flags) {
  1602.                 DRM_ERROR("no operation set\n");
  1603.                 return -EINVAL;
  1604.         }
  1605.  
  1606.         mutex_lock(&dev->mode_config.mutex);
  1607.         obj = drm_mode_object_find(dev, req->crtc_id, DRM_MODE_OBJECT_CRTC);
  1608.         if (!obj) {
  1609.                 DRM_DEBUG_KMS("Unknown CRTC ID %d\n", req->crtc_id);
  1610.                 ret = -EINVAL;
  1611.                 goto out;
  1612.         }
  1613.         crtc = obj_to_crtc(obj);
  1614.  
  1615.         if (req->flags & DRM_MODE_CURSOR_BO) {
  1616.                 if (!crtc->funcs->cursor_set) {
  1617.                         DRM_ERROR("crtc does not support cursor\n");
  1618.                         ret = -ENXIO;
  1619.                         goto out;
  1620.                 }
  1621.                 /* Turns off the cursor if handle is 0 */
  1622.                 ret = crtc->funcs->cursor_set(crtc, file_priv, req->handle,
  1623.                                               req->width, req->height);
  1624.         }
  1625.  
  1626.         if (req->flags & DRM_MODE_CURSOR_MOVE) {
  1627.                 if (crtc->funcs->cursor_move) {
  1628.                         ret = crtc->funcs->cursor_move(crtc, req->x, req->y);
  1629.                 } else {
  1630.                         DRM_ERROR("crtc does not support cursor\n");
  1631.                         ret = -EFAULT;
  1632.                         goto out;
  1633.                 }
  1634.         }
  1635. out:
  1636.         mutex_unlock(&dev->mode_config.mutex);
  1637.         return ret;
  1638. }
  1639.  
  1640. /**
  1641.  * drm_mode_addfb - add an FB to the graphics configuration
  1642.  * @inode: inode from the ioctl
  1643.  * @filp: file * from the ioctl
  1644.  * @cmd: cmd from ioctl
  1645.  * @arg: arg from ioctl
  1646.  *
  1647.  * LOCKING:
  1648.  * Takes mode config lock.
  1649.  *
  1650.  * Add a new FB to the specified CRTC, given a user request.
  1651.  *
  1652.  * Called by the user via ioctl.
  1653.  *
  1654.  * RETURNS:
  1655.  * Zero on success, errno on failure.
  1656.  */
  1657. int drm_mode_addfb(struct drm_device *dev,
  1658.                    void *data, struct drm_file *file_priv)
  1659. {
  1660.         struct drm_mode_fb_cmd *r = data;
  1661.         struct drm_mode_config *config = &dev->mode_config;
  1662.         struct drm_framebuffer *fb;
  1663.         int ret = 0;
  1664.  
  1665.         if ((config->min_width > r->width) || (r->width > config->max_width)) {
  1666.                 DRM_ERROR("mode new framebuffer width not within limits\n");
  1667.                 return -EINVAL;
  1668.         }
  1669.         if ((config->min_height > r->height) || (r->height > config->max_height)) {
  1670.                 DRM_ERROR("mode new framebuffer height not within limits\n");
  1671.                 return -EINVAL;
  1672.         }
  1673.  
  1674.         mutex_lock(&dev->mode_config.mutex);
  1675.  
  1676.         /* TODO check buffer is sufficently large */
  1677.         /* TODO setup destructor callback */
  1678.  
  1679.         fb = dev->mode_config.funcs->fb_create(dev, file_priv, r);
  1680.         if (!fb) {
  1681.                 DRM_ERROR("could not create framebuffer\n");
  1682.                 ret = -EINVAL;
  1683.                 goto out;
  1684.         }
  1685.  
  1686.         r->fb_id = fb->base.id;
  1687.         list_add(&fb->filp_head, &file_priv->fbs);
  1688.  
  1689. out:
  1690.         mutex_unlock(&dev->mode_config.mutex);
  1691.         return ret;
  1692. }
  1693.  
  1694. /**
  1695.  * drm_mode_rmfb - remove an FB from the configuration
  1696.  * @inode: inode from the ioctl
  1697.  * @filp: file * from the ioctl
  1698.  * @cmd: cmd from ioctl
  1699.  * @arg: arg from ioctl
  1700.  *
  1701.  * LOCKING:
  1702.  * Takes mode config lock.
  1703.  *
  1704.  * Remove the FB specified by the user.
  1705.  *
  1706.  * Called by the user via ioctl.
  1707.  *
  1708.  * RETURNS:
  1709.  * Zero on success, errno on failure.
  1710.  */
  1711. int drm_mode_rmfb(struct drm_device *dev,
  1712.                    void *data, struct drm_file *file_priv)
  1713. {
  1714.         struct drm_mode_object *obj;
  1715.         struct drm_framebuffer *fb = NULL;
  1716.         struct drm_framebuffer *fbl = NULL;
  1717.         uint32_t *id = data;
  1718.         int ret = 0;
  1719.         int found = 0;
  1720.  
  1721.         mutex_lock(&dev->mode_config.mutex);
  1722.         obj = drm_mode_object_find(dev, *id, DRM_MODE_OBJECT_FB);
  1723.         /* TODO check that we realy get a framebuffer back. */
  1724.         if (!obj) {
  1725.                 DRM_ERROR("mode invalid framebuffer id\n");
  1726.                 ret = -EINVAL;
  1727.                 goto out;
  1728.         }
  1729.         fb = obj_to_fb(obj);
  1730.  
  1731.         list_for_each_entry(fbl, &file_priv->fbs, filp_head)
  1732.                 if (fb == fbl)
  1733.                         found = 1;
  1734.  
  1735.         if (!found) {
  1736.                 DRM_ERROR("tried to remove a fb that we didn't own\n");
  1737.                 ret = -EINVAL;
  1738.                 goto out;
  1739.         }
  1740.  
  1741.         /* TODO release all crtc connected to the framebuffer */
  1742.         /* TODO unhock the destructor from the buffer object */
  1743.  
  1744.         list_del(&fb->filp_head);
  1745.         fb->funcs->destroy(fb);
  1746.  
  1747. out:
  1748.         mutex_unlock(&dev->mode_config.mutex);
  1749.         return ret;
  1750. }
  1751.  
  1752. /**
  1753.  * drm_mode_getfb - get FB info
  1754.  * @inode: inode from the ioctl
  1755.  * @filp: file * from the ioctl
  1756.  * @cmd: cmd from ioctl
  1757.  * @arg: arg from ioctl
  1758.  *
  1759.  * LOCKING:
  1760.  * Caller? (FIXME)
  1761.  *
  1762.  * Lookup the FB given its ID and return info about it.
  1763.  *
  1764.  * Called by the user via ioctl.
  1765.  *
  1766.  * RETURNS:
  1767.  * Zero on success, errno on failure.
  1768.  */
  1769. int drm_mode_getfb(struct drm_device *dev,
  1770.                    void *data, struct drm_file *file_priv)
  1771. {
  1772.         struct drm_mode_fb_cmd *r = data;
  1773.         struct drm_mode_object *obj;
  1774.         struct drm_framebuffer *fb;
  1775.         int ret = 0;
  1776.  
  1777.         mutex_lock(&dev->mode_config.mutex);
  1778.         obj = drm_mode_object_find(dev, r->fb_id, DRM_MODE_OBJECT_FB);
  1779.         if (!obj) {
  1780.                 DRM_ERROR("invalid framebuffer id\n");
  1781.                 ret = -EINVAL;
  1782.                 goto out;
  1783.         }
  1784.         fb = obj_to_fb(obj);
  1785.  
  1786.         r->height = fb->height;
  1787.         r->width = fb->width;
  1788.         r->depth = fb->depth;
  1789.         r->bpp = fb->bits_per_pixel;
  1790.         r->pitch = fb->pitch;
  1791.         fb->funcs->create_handle(fb, file_priv, &r->handle);
  1792.  
  1793. out:
  1794.         mutex_unlock(&dev->mode_config.mutex);
  1795.         return ret;
  1796. }
  1797.  
  1798. /**
  1799.  * drm_fb_release - remove and free the FBs on this file
  1800.  * @filp: file * from the ioctl
  1801.  *
  1802.  * LOCKING:
  1803.  * Takes mode config lock.
  1804.  *
  1805.  * Destroy all the FBs associated with @filp.
  1806.  *
  1807.  * Called by the user via ioctl.
  1808.  *
  1809.  * RETURNS:
  1810.  * Zero on success, errno on failure.
  1811.  */
  1812. void drm_fb_release(struct drm_file *priv)
  1813. {
  1814.         struct drm_device *dev = priv->minor->dev;
  1815.         struct drm_framebuffer *fb, *tfb;
  1816.  
  1817.         mutex_lock(&dev->mode_config.mutex);
  1818.         list_for_each_entry_safe(fb, tfb, &priv->fbs, filp_head) {
  1819.                 list_del(&fb->filp_head);
  1820.                 fb->funcs->destroy(fb);
  1821.         }
  1822.         mutex_unlock(&dev->mode_config.mutex);
  1823. }
  1824. #endif
  1825.  
  1826. /**
  1827.  * drm_mode_attachmode - add a mode to the user mode list
  1828.  * @dev: DRM device
  1829.  * @connector: connector to add the mode to
  1830.  * @mode: mode to add
  1831.  *
  1832.  * Add @mode to @connector's user mode list.
  1833.  */
  1834. static int drm_mode_attachmode(struct drm_device *dev,
  1835.                                struct drm_connector *connector,
  1836.                                struct drm_display_mode *mode)
  1837. {
  1838.         int ret = 0;
  1839.  
  1840.         list_add_tail(&mode->head, &connector->user_modes);
  1841.         return ret;
  1842. }
  1843.  
  1844. int drm_mode_attachmode_crtc(struct drm_device *dev, struct drm_crtc *crtc,
  1845.                              struct drm_display_mode *mode)
  1846. {
  1847.         struct drm_connector *connector;
  1848.         int ret = 0;
  1849.         struct drm_display_mode *dup_mode;
  1850.         int need_dup = 0;
  1851.         list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  1852.                 if (!connector->encoder)
  1853.                         break;
  1854.                 if (connector->encoder->crtc == crtc) {
  1855.                         if (need_dup)
  1856.                                 dup_mode = drm_mode_duplicate(dev, mode);
  1857.                         else
  1858.                                 dup_mode = mode;
  1859.                         ret = drm_mode_attachmode(dev, connector, dup_mode);
  1860.                         if (ret)
  1861.                                 return ret;
  1862.                         need_dup = 1;
  1863.                 }
  1864.         }
  1865.         return 0;
  1866. }
  1867. EXPORT_SYMBOL(drm_mode_attachmode_crtc);
  1868.  
  1869. static int drm_mode_detachmode(struct drm_device *dev,
  1870.                                struct drm_connector *connector,
  1871.                                struct drm_display_mode *mode)
  1872. {
  1873.         int found = 0;
  1874.         int ret = 0;
  1875.         struct drm_display_mode *match_mode, *t;
  1876.  
  1877.         list_for_each_entry_safe(match_mode, t, &connector->user_modes, head) {
  1878.                 if (drm_mode_equal(match_mode, mode)) {
  1879.                         list_del(&match_mode->head);
  1880.                         drm_mode_destroy(dev, match_mode);
  1881.                         found = 1;
  1882.                         break;
  1883.                 }
  1884.         }
  1885.  
  1886.         if (!found)
  1887.                 ret = -EINVAL;
  1888.  
  1889.         return ret;
  1890. }
  1891.  
  1892. int drm_mode_detachmode_crtc(struct drm_device *dev, struct drm_display_mode *mode)
  1893. {
  1894.         struct drm_connector *connector;
  1895.  
  1896.         list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  1897.                 drm_mode_detachmode(dev, connector, mode);
  1898.         }
  1899.         return 0;
  1900. }
  1901. EXPORT_SYMBOL(drm_mode_detachmode_crtc);
  1902.  
  1903. #if 0
  1904.  
  1905. /**
  1906.  * drm_fb_attachmode - Attach a user mode to an connector
  1907.  * @inode: inode from the ioctl
  1908.  * @filp: file * from the ioctl
  1909.  * @cmd: cmd from ioctl
  1910.  * @arg: arg from ioctl
  1911.  *
  1912.  * This attaches a user specified mode to an connector.
  1913.  * Called by the user via ioctl.
  1914.  *
  1915.  * RETURNS:
  1916.  * Zero on success, errno on failure.
  1917.  */
  1918. int drm_mode_attachmode_ioctl(struct drm_device *dev,
  1919.                               void *data, struct drm_file *file_priv)
  1920. {
  1921.         struct drm_mode_mode_cmd *mode_cmd = data;
  1922.         struct drm_connector *connector;
  1923.         struct drm_display_mode *mode;
  1924.         struct drm_mode_object *obj;
  1925.         struct drm_mode_modeinfo *umode = &mode_cmd->mode;
  1926.         int ret = 0;
  1927.  
  1928.         mutex_lock(&dev->mode_config.mutex);
  1929.  
  1930.         obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
  1931.         if (!obj) {
  1932.                 ret = -EINVAL;
  1933.                 goto out;
  1934.         }
  1935.         connector = obj_to_connector(obj);
  1936.  
  1937.         mode = drm_mode_create(dev);
  1938.         if (!mode) {
  1939.                 ret = -ENOMEM;
  1940.                 goto out;
  1941.         }
  1942.  
  1943.         drm_crtc_convert_umode(mode, umode);
  1944.  
  1945.         ret = drm_mode_attachmode(dev, connector, mode);
  1946. out:
  1947.         mutex_unlock(&dev->mode_config.mutex);
  1948.         return ret;
  1949. }
  1950.  
  1951.  
  1952. /**
  1953.  * drm_fb_detachmode - Detach a user specified mode from an connector
  1954.  * @inode: inode from the ioctl
  1955.  * @filp: file * from the ioctl
  1956.  * @cmd: cmd from ioctl
  1957.  * @arg: arg from ioctl
  1958.  *
  1959.  * Called by the user via ioctl.
  1960.  *
  1961.  * RETURNS:
  1962.  * Zero on success, errno on failure.
  1963.  */
  1964. int drm_mode_detachmode_ioctl(struct drm_device *dev,
  1965.                               void *data, struct drm_file *file_priv)
  1966. {
  1967.         struct drm_mode_object *obj;
  1968.         struct drm_mode_mode_cmd *mode_cmd = data;
  1969.         struct drm_connector *connector;
  1970.         struct drm_display_mode mode;
  1971.         struct drm_mode_modeinfo *umode = &mode_cmd->mode;
  1972.         int ret = 0;
  1973.  
  1974.         mutex_lock(&dev->mode_config.mutex);
  1975.  
  1976.         obj = drm_mode_object_find(dev, mode_cmd->connector_id, DRM_MODE_OBJECT_CONNECTOR);
  1977.         if (!obj) {
  1978.                 ret = -EINVAL;
  1979.                 goto out;
  1980.         }
  1981.         connector = obj_to_connector(obj);
  1982.  
  1983.         drm_crtc_convert_umode(&mode, umode);
  1984.         ret = drm_mode_detachmode(dev, connector, &mode);
  1985. out:
  1986.         mutex_unlock(&dev->mode_config.mutex);
  1987.         return ret;
  1988. }
  1989. #endif
  1990.  
  1991. struct drm_property *drm_property_create(struct drm_device *dev, int flags,
  1992.                                          const char *name, int num_values)
  1993. {
  1994.         struct drm_property *property = NULL;
  1995.  
  1996.         property = kzalloc(sizeof(struct drm_property), GFP_KERNEL);
  1997.         if (!property)
  1998.                 return NULL;
  1999.  
  2000.         if (num_values) {
  2001.                 property->values = kzalloc(sizeof(uint64_t)*num_values, GFP_KERNEL);
  2002.                 if (!property->values)
  2003.                         goto fail;
  2004.         }
  2005.  
  2006.         drm_mode_object_get(dev, &property->base, DRM_MODE_OBJECT_PROPERTY);
  2007.         property->flags = flags;
  2008.         property->num_values = num_values;
  2009.         INIT_LIST_HEAD(&property->enum_blob_list);
  2010.  
  2011.         if (name)
  2012.                 strncpy(property->name, name, DRM_PROP_NAME_LEN);
  2013.  
  2014.         list_add_tail(&property->head, &dev->mode_config.property_list);
  2015.         return property;
  2016. fail:
  2017.         kfree(property);
  2018.         return NULL;
  2019. }
  2020. EXPORT_SYMBOL(drm_property_create);
  2021.  
  2022. int drm_property_add_enum(struct drm_property *property, int index,
  2023.                           uint64_t value, const char *name)
  2024. {
  2025.         struct drm_property_enum *prop_enum;
  2026.  
  2027.         if (!(property->flags & DRM_MODE_PROP_ENUM))
  2028.                 return -EINVAL;
  2029.  
  2030.         if (!list_empty(&property->enum_blob_list)) {
  2031.                 list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
  2032.                         if (prop_enum->value == value) {
  2033.                                 strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
  2034.                                 prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
  2035.                                 return 0;
  2036.                         }
  2037.                 }
  2038.         }
  2039.  
  2040.         prop_enum = kzalloc(sizeof(struct drm_property_enum), GFP_KERNEL);
  2041.         if (!prop_enum)
  2042.                 return -ENOMEM;
  2043.  
  2044.         strncpy(prop_enum->name, name, DRM_PROP_NAME_LEN);
  2045.         prop_enum->name[DRM_PROP_NAME_LEN-1] = '\0';
  2046.         prop_enum->value = value;
  2047.  
  2048.         property->values[index] = value;
  2049.         list_add_tail(&prop_enum->head, &property->enum_blob_list);
  2050.         return 0;
  2051. }
  2052. EXPORT_SYMBOL(drm_property_add_enum);
  2053.  
  2054. void drm_property_destroy(struct drm_device *dev, struct drm_property *property)
  2055. {
  2056.         struct drm_property_enum *prop_enum, *pt;
  2057.  
  2058.         list_for_each_entry_safe(prop_enum, pt, &property->enum_blob_list, head) {
  2059.                 list_del(&prop_enum->head);
  2060.                 kfree(prop_enum);
  2061.         }
  2062.  
  2063.         if (property->num_values)
  2064.                 kfree(property->values);
  2065.         drm_mode_object_put(dev, &property->base);
  2066.         list_del(&property->head);
  2067.         kfree(property);
  2068. }
  2069. EXPORT_SYMBOL(drm_property_destroy);
  2070.  
  2071. int drm_connector_attach_property(struct drm_connector *connector,
  2072.                                struct drm_property *property, uint64_t init_val)
  2073. {
  2074.         int i;
  2075.  
  2076.         for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
  2077.                 if (connector->property_ids[i] == 0) {
  2078.                         connector->property_ids[i] = property->base.id;
  2079.                         connector->property_values[i] = init_val;
  2080.                         break;
  2081.                 }
  2082.         }
  2083.  
  2084.         if (i == DRM_CONNECTOR_MAX_PROPERTY)
  2085.                 return -EINVAL;
  2086.         return 0;
  2087. }
  2088. EXPORT_SYMBOL(drm_connector_attach_property);
  2089.  
  2090. int drm_connector_property_set_value(struct drm_connector *connector,
  2091.                                   struct drm_property *property, uint64_t value)
  2092. {
  2093.         int i;
  2094.  
  2095.         for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
  2096.                 if (connector->property_ids[i] == property->base.id) {
  2097.                         connector->property_values[i] = value;
  2098.                         break;
  2099.                 }
  2100.         }
  2101.  
  2102.         if (i == DRM_CONNECTOR_MAX_PROPERTY)
  2103.                 return -EINVAL;
  2104.         return 0;
  2105. }
  2106. EXPORT_SYMBOL(drm_connector_property_set_value);
  2107.  
  2108. int drm_connector_property_get_value(struct drm_connector *connector,
  2109.                                   struct drm_property *property, uint64_t *val)
  2110. {
  2111.         int i;
  2112.  
  2113.         for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
  2114.                 if (connector->property_ids[i] == property->base.id) {
  2115.                         *val = connector->property_values[i];
  2116.                         break;
  2117.                 }
  2118.         }
  2119.  
  2120.         if (i == DRM_CONNECTOR_MAX_PROPERTY)
  2121.                 return -EINVAL;
  2122.         return 0;
  2123. }
  2124. EXPORT_SYMBOL(drm_connector_property_get_value);
  2125.  
  2126. #if 0
  2127. int drm_mode_getproperty_ioctl(struct drm_device *dev,
  2128.                                void *data, struct drm_file *file_priv)
  2129. {
  2130.         struct drm_mode_object *obj;
  2131.         struct drm_mode_get_property *out_resp = data;
  2132.         struct drm_property *property;
  2133.         int enum_count = 0;
  2134.         int blob_count = 0;
  2135.         int value_count = 0;
  2136.         int ret = 0, i;
  2137.         int copied;
  2138.         struct drm_property_enum *prop_enum;
  2139.         struct drm_mode_property_enum __user *enum_ptr;
  2140.         struct drm_property_blob *prop_blob;
  2141.         uint32_t *blob_id_ptr;
  2142.         uint64_t __user *values_ptr;
  2143.         uint32_t __user *blob_length_ptr;
  2144.  
  2145.         mutex_lock(&dev->mode_config.mutex);
  2146.         obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
  2147.         if (!obj) {
  2148.                 ret = -EINVAL;
  2149.                 goto done;
  2150.         }
  2151.         property = obj_to_property(obj);
  2152.  
  2153.         if (property->flags & DRM_MODE_PROP_ENUM) {
  2154.                 list_for_each_entry(prop_enum, &property->enum_blob_list, head)
  2155.                         enum_count++;
  2156.         } else if (property->flags & DRM_MODE_PROP_BLOB) {
  2157.                 list_for_each_entry(prop_blob, &property->enum_blob_list, head)
  2158.                         blob_count++;
  2159.         }
  2160.  
  2161.         value_count = property->num_values;
  2162.  
  2163.         strncpy(out_resp->name, property->name, DRM_PROP_NAME_LEN);
  2164.         out_resp->name[DRM_PROP_NAME_LEN-1] = 0;
  2165.         out_resp->flags = property->flags;
  2166.  
  2167.         if ((out_resp->count_values >= value_count) && value_count) {
  2168.                 values_ptr = (uint64_t *)(unsigned long)out_resp->values_ptr;
  2169.                 for (i = 0; i < value_count; i++) {
  2170.                         if (copy_to_user(values_ptr + i, &property->values[i], sizeof(uint64_t))) {
  2171.                                 ret = -EFAULT;
  2172.                                 goto done;
  2173.                         }
  2174.                 }
  2175.         }
  2176.         out_resp->count_values = value_count;
  2177.  
  2178.         if (property->flags & DRM_MODE_PROP_ENUM) {
  2179.                 if ((out_resp->count_enum_blobs >= enum_count) && enum_count) {
  2180.                         copied = 0;
  2181.                         enum_ptr = (struct drm_mode_property_enum *)(unsigned long)out_resp->enum_blob_ptr;
  2182.                         list_for_each_entry(prop_enum, &property->enum_blob_list, head) {
  2183.  
  2184.                                 if (copy_to_user(&enum_ptr[copied].value, &prop_enum->value, sizeof(uint64_t))) {
  2185.                                         ret = -EFAULT;
  2186.                                         goto done;
  2187.                                 }
  2188.  
  2189.                                 if (copy_to_user(&enum_ptr[copied].name,
  2190.                                                  &prop_enum->name, DRM_PROP_NAME_LEN)) {
  2191.                                         ret = -EFAULT;
  2192.                                         goto done;
  2193.                                 }
  2194.                                 copied++;
  2195.                         }
  2196.                 }
  2197.                 out_resp->count_enum_blobs = enum_count;
  2198.         }
  2199.  
  2200.         if (property->flags & DRM_MODE_PROP_BLOB) {
  2201.                 if ((out_resp->count_enum_blobs >= blob_count) && blob_count) {
  2202.                         copied = 0;
  2203.                         blob_id_ptr = (uint32_t *)(unsigned long)out_resp->enum_blob_ptr;
  2204.                         blob_length_ptr = (uint32_t *)(unsigned long)out_resp->values_ptr;
  2205.  
  2206.                         list_for_each_entry(prop_blob, &property->enum_blob_list, head) {
  2207.                                 if (put_user(prop_blob->base.id, blob_id_ptr + copied)) {
  2208.                                         ret = -EFAULT;
  2209.                                         goto done;
  2210.                                 }
  2211.  
  2212.                                 if (put_user(prop_blob->length, blob_length_ptr + copied)) {
  2213.                                         ret = -EFAULT;
  2214.                                         goto done;
  2215.                                 }
  2216.  
  2217.                                 copied++;
  2218.                         }
  2219.                 }
  2220.                 out_resp->count_enum_blobs = blob_count;
  2221.         }
  2222. done:
  2223.         mutex_unlock(&dev->mode_config.mutex);
  2224.         return ret;
  2225. }
  2226. #endif
  2227.  
  2228. static struct drm_property_blob *drm_property_create_blob(struct drm_device *dev, int length,
  2229.                                                           void *data)
  2230. {
  2231.         struct drm_property_blob *blob;
  2232.  
  2233.         if (!length || !data)
  2234.                 return NULL;
  2235.  
  2236.         blob = kzalloc(sizeof(struct drm_property_blob)+length, GFP_KERNEL);
  2237.         if (!blob)
  2238.                 return NULL;
  2239.  
  2240.         blob->data = (void *)((char *)blob + sizeof(struct drm_property_blob));
  2241.         blob->length = length;
  2242.  
  2243.         memcpy(blob->data, data, length);
  2244.  
  2245.         drm_mode_object_get(dev, &blob->base, DRM_MODE_OBJECT_BLOB);
  2246.  
  2247.         list_add_tail(&blob->head, &dev->mode_config.property_blob_list);
  2248.         return blob;
  2249. }
  2250.  
  2251. static void drm_property_destroy_blob(struct drm_device *dev,
  2252.                                struct drm_property_blob *blob)
  2253. {
  2254.         drm_mode_object_put(dev, &blob->base);
  2255.         list_del(&blob->head);
  2256.         kfree(blob);
  2257. }
  2258.  
  2259. #if 0
  2260. int drm_mode_getblob_ioctl(struct drm_device *dev,
  2261.                            void *data, struct drm_file *file_priv)
  2262. {
  2263.         struct drm_mode_object *obj;
  2264.         struct drm_mode_get_blob *out_resp = data;
  2265.         struct drm_property_blob *blob;
  2266.         int ret = 0;
  2267.         void *blob_ptr;
  2268.  
  2269.         mutex_lock(&dev->mode_config.mutex);
  2270.         obj = drm_mode_object_find(dev, out_resp->blob_id, DRM_MODE_OBJECT_BLOB);
  2271.         if (!obj) {
  2272.                 ret = -EINVAL;
  2273.                 goto done;
  2274.         }
  2275.         blob = obj_to_blob(obj);
  2276.  
  2277.         if (out_resp->length == blob->length) {
  2278.                 blob_ptr = (void *)(unsigned long)out_resp->data;
  2279.                 if (copy_to_user(blob_ptr, blob->data, blob->length)){
  2280.                         ret = -EFAULT;
  2281.                         goto done;
  2282.                 }
  2283.         }
  2284.         out_resp->length = blob->length;
  2285.  
  2286. done:
  2287.         mutex_unlock(&dev->mode_config.mutex);
  2288.         return ret;
  2289. }
  2290. #endif
  2291.  
  2292. int drm_mode_connector_update_edid_property(struct drm_connector *connector,
  2293.                                             struct edid *edid)
  2294. {
  2295.         struct drm_device *dev = connector->dev;
  2296.         int ret = 0;
  2297.  
  2298.         if (connector->edid_blob_ptr)
  2299.                 drm_property_destroy_blob(dev, connector->edid_blob_ptr);
  2300.  
  2301.         /* Delete edid, when there is none. */
  2302.         if (!edid) {
  2303.                 connector->edid_blob_ptr = NULL;
  2304.                 ret = drm_connector_property_set_value(connector, dev->mode_config.edid_property, 0);
  2305.                 return ret;
  2306.         }
  2307.  
  2308.         connector->edid_blob_ptr = drm_property_create_blob(connector->dev, 128, edid);
  2309.  
  2310.         ret = drm_connector_property_set_value(connector,
  2311.                                                dev->mode_config.edid_property,
  2312.                                                connector->edid_blob_ptr->base.id);
  2313.  
  2314.         return ret;
  2315. }
  2316. EXPORT_SYMBOL(drm_mode_connector_update_edid_property);
  2317.  
  2318. #if 0
  2319. int drm_mode_connector_property_set_ioctl(struct drm_device *dev,
  2320.                                        void *data, struct drm_file *file_priv)
  2321. {
  2322.         struct drm_mode_connector_set_property *out_resp = data;
  2323.         struct drm_mode_object *obj;
  2324.         struct drm_property *property;
  2325.         struct drm_connector *connector;
  2326.         int ret = -EINVAL;
  2327.         int i;
  2328.  
  2329.         mutex_lock(&dev->mode_config.mutex);
  2330.  
  2331.         obj = drm_mode_object_find(dev, out_resp->connector_id, DRM_MODE_OBJECT_CONNECTOR);
  2332.         if (!obj) {
  2333.                 goto out;
  2334.         }
  2335.         connector = obj_to_connector(obj);
  2336.  
  2337.         for (i = 0; i < DRM_CONNECTOR_MAX_PROPERTY; i++) {
  2338.                 if (connector->property_ids[i] == out_resp->prop_id)
  2339.                         break;
  2340.         }
  2341.  
  2342.         if (i == DRM_CONNECTOR_MAX_PROPERTY) {
  2343.                 goto out;
  2344.         }
  2345.  
  2346.         obj = drm_mode_object_find(dev, out_resp->prop_id, DRM_MODE_OBJECT_PROPERTY);
  2347.         if (!obj) {
  2348.                 goto out;
  2349.         }
  2350.         property = obj_to_property(obj);
  2351.  
  2352.         if (property->flags & DRM_MODE_PROP_IMMUTABLE)
  2353.                 goto out;
  2354.  
  2355.         if (property->flags & DRM_MODE_PROP_RANGE) {
  2356.                 if (out_resp->value < property->values[0])
  2357.                         goto out;
  2358.  
  2359.                 if (out_resp->value > property->values[1])
  2360.                         goto out;
  2361.         } else {
  2362.                 int found = 0;
  2363.                 for (i = 0; i < property->num_values; i++) {
  2364.                         if (property->values[i] == out_resp->value) {
  2365.                                 found = 1;
  2366.                                 break;
  2367.                         }
  2368.                 }
  2369.                 if (!found) {
  2370.                         goto out;
  2371.                 }
  2372.         }
  2373.  
  2374.         /* Do DPMS ourselves */
  2375.         if (property == connector->dev->mode_config.dpms_property) {
  2376.                 if (connector->funcs->dpms)
  2377.                         (*connector->funcs->dpms)(connector, (int) out_resp->value);
  2378.                 ret = 0;
  2379.         } else if (connector->funcs->set_property)
  2380.                 ret = connector->funcs->set_property(connector, property, out_resp->value);
  2381.  
  2382.         /* store the property value if successful */
  2383.         if (!ret)
  2384.                 drm_connector_property_set_value(connector, property, out_resp->value);
  2385. out:
  2386.         mutex_unlock(&dev->mode_config.mutex);
  2387.         return ret;
  2388. }
  2389. #endif
  2390.  
  2391. int drm_mode_connector_attach_encoder(struct drm_connector *connector,
  2392.                                       struct drm_encoder *encoder)
  2393. {
  2394.         int i;
  2395.  
  2396.         for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
  2397.                 if (connector->encoder_ids[i] == 0) {
  2398.                         connector->encoder_ids[i] = encoder->base.id;
  2399.                         return 0;
  2400.                 }
  2401.         }
  2402.         return -ENOMEM;
  2403. }
  2404. EXPORT_SYMBOL(drm_mode_connector_attach_encoder);
  2405.  
  2406. void drm_mode_connector_detach_encoder(struct drm_connector *connector,
  2407.                                     struct drm_encoder *encoder)
  2408. {
  2409.         int i;
  2410.         for (i = 0; i < DRM_CONNECTOR_MAX_ENCODER; i++) {
  2411.                 if (connector->encoder_ids[i] == encoder->base.id) {
  2412.                         connector->encoder_ids[i] = 0;
  2413.                         if (connector->encoder == encoder)
  2414.                                 connector->encoder = NULL;
  2415.                         break;
  2416.                 }
  2417.         }
  2418. }
  2419. EXPORT_SYMBOL(drm_mode_connector_detach_encoder);
  2420.  
  2421. bool drm_mode_crtc_set_gamma_size(struct drm_crtc *crtc,
  2422.                                   int gamma_size)
  2423. {
  2424.         crtc->gamma_size = gamma_size;
  2425.  
  2426.         crtc->gamma_store = kzalloc(gamma_size * sizeof(uint16_t) * 3, GFP_KERNEL);
  2427.         if (!crtc->gamma_store) {
  2428.                 crtc->gamma_size = 0;
  2429.                 return false;
  2430.         }
  2431.  
  2432.         return true;
  2433. }
  2434. EXPORT_SYMBOL(drm_mode_crtc_set_gamma_size);
  2435.  
  2436. #if 0
  2437. int drm_mode_gamma_set_ioctl(struct drm_device *dev,
  2438.                              void *data, struct drm_file *file_priv)
  2439. {
  2440.         struct drm_mode_crtc_lut *crtc_lut = data;
  2441.         struct drm_mode_object *obj;
  2442.         struct drm_crtc *crtc;
  2443.         void *r_base, *g_base, *b_base;
  2444.         int size;
  2445.         int ret = 0;
  2446.  
  2447.         mutex_lock(&dev->mode_config.mutex);
  2448.         obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
  2449.         if (!obj) {
  2450.                 ret = -EINVAL;
  2451.                 goto out;
  2452.         }
  2453.         crtc = obj_to_crtc(obj);
  2454.  
  2455.         /* memcpy into gamma store */
  2456.         if (crtc_lut->gamma_size != crtc->gamma_size) {
  2457.                 ret = -EINVAL;
  2458.                 goto out;
  2459.         }
  2460.  
  2461.         size = crtc_lut->gamma_size * (sizeof(uint16_t));
  2462.         r_base = crtc->gamma_store;
  2463.         if (copy_from_user(r_base, (void __user *)(unsigned long)crtc_lut->red, size)) {
  2464.                 ret = -EFAULT;
  2465.                 goto out;
  2466.         }
  2467.  
  2468.         g_base = r_base + size;
  2469.         if (copy_from_user(g_base, (void __user *)(unsigned long)crtc_lut->green, size)) {
  2470.                 ret = -EFAULT;
  2471.                 goto out;
  2472.         }
  2473.  
  2474.         b_base = g_base + size;
  2475.         if (copy_from_user(b_base, (void __user *)(unsigned long)crtc_lut->blue, size)) {
  2476.                 ret = -EFAULT;
  2477.                 goto out;
  2478.         }
  2479.  
  2480.         crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, crtc->gamma_size);
  2481.  
  2482. out:
  2483.         mutex_unlock(&dev->mode_config.mutex);
  2484.         return ret;
  2485.  
  2486. }
  2487.  
  2488. int drm_mode_gamma_get_ioctl(struct drm_device *dev,
  2489.                              void *data, struct drm_file *file_priv)
  2490. {
  2491.         struct drm_mode_crtc_lut *crtc_lut = data;
  2492.         struct drm_mode_object *obj;
  2493.         struct drm_crtc *crtc;
  2494.         void *r_base, *g_base, *b_base;
  2495.         int size;
  2496.         int ret = 0;
  2497.  
  2498.         mutex_lock(&dev->mode_config.mutex);
  2499.         obj = drm_mode_object_find(dev, crtc_lut->crtc_id, DRM_MODE_OBJECT_CRTC);
  2500.         if (!obj) {
  2501.                 ret = -EINVAL;
  2502.                 goto out;
  2503.         }
  2504.         crtc = obj_to_crtc(obj);
  2505.  
  2506.         /* memcpy into gamma store */
  2507.         if (crtc_lut->gamma_size != crtc->gamma_size) {
  2508.                 ret = -EINVAL;
  2509.                 goto out;
  2510.         }
  2511.  
  2512.         size = crtc_lut->gamma_size * (sizeof(uint16_t));
  2513.         r_base = crtc->gamma_store;
  2514.         if (copy_to_user((void __user *)(unsigned long)crtc_lut->red, r_base, size)) {
  2515.                 ret = -EFAULT;
  2516.                 goto out;
  2517.         }
  2518.  
  2519.         g_base = r_base + size;
  2520.         if (copy_to_user((void __user *)(unsigned long)crtc_lut->green, g_base, size)) {
  2521.                 ret = -EFAULT;
  2522.                 goto out;
  2523.         }
  2524.  
  2525.         b_base = g_base + size;
  2526.         if (copy_to_user((void __user *)(unsigned long)crtc_lut->blue, b_base, size)) {
  2527.                 ret = -EFAULT;
  2528.                 goto out;
  2529.         }
  2530. out:
  2531.         mutex_unlock(&dev->mode_config.mutex);
  2532.         return ret;
  2533. }
  2534.  
  2535. #endif
  2536.  
  2537.  
  2538.