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