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