Subversion Repositories Kolibri OS

Rev

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