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